blob: d57203ba6f8b3322e20e57a34742a1eb692d177f [file] [log] [blame]
Jonathan Domanee03a9b2020-11-11 12:56:23 -08001// Copyright (c) 2021 Intel Corporation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <boost/asio/io_context.hpp>
18#include <sdbusplus/asio/connection.hpp>
19
20#define DEBUG_PRINT \
21 if constexpr (false) \
22 std::cerr
23
24namespace cpu_info
25{
26
27/** Host states which are of interest just to cpuinfo use cases. */
28enum class HostState
29{
30 /** Host CPU is powered off. */
31 off,
32 /** Host CPU is powered on, but BIOS has not completed POST. */
33 postInProgress,
34 /** BIOS has completed POST. */
35 postComplete
36};
37
38/** Current host state - initialized to "off" */
39extern HostState hostState;
40
41/**
42 * Register D-Bus match handlers to keep hostState current. The D-Bus logic is
43 * entirely asynchronous, so hostState will never change from "off" until after
44 * this function is called and the asio event loop is running.
45 *
46 * @param[in] conn D-Bus ASIO connection.
47 */
48void hostStateSetup(const std::shared_ptr<sdbusplus::asio::connection>& conn);
49
Jonathan Doman49ea8302022-05-26 14:29:46 -070050/**
51 * Callback which is run whenever the HostState changes. First parameter is the
52 * old state, and second parameter is the new current state.
53 */
54using HostStateHandler = std::function<void(HostState, HostState)>;
55void addHostStateCallback(HostStateHandler cb);
56
Jonathan Doman16a2ced2021-11-01 11:13:22 -070057constexpr uint64_t bit(uint8_t index)
58{
59 return (1ull << index);
60}
61
62/**
63 * Extract a bitfield from an input data by shifting and masking.
64 *
65 * @tparam Dest Destination type - mostly useful to avoid an extra static_cast
66 * at the call site. Defaults to the Src type if unspecified.
67 * @tparam Src Automatically deduced from the first positional parameter.
68 *
69 * @param data Input data.
70 * @param loBit 0-based index of the least significant bit to return.
71 * @param hiBit 0-based index of the most significant bit to return.
72 */
73template <typename Dest = std::monostate, typename Src>
74auto mask(Src data, unsigned int loBit, unsigned int hiBit)
75{
76 assert(hiBit >= loBit);
77 uint64_t d = data;
78 d >>= loBit;
79 d &= (1ull << (hiBit - loBit + 1)) - 1;
80 if constexpr (std::is_same_v<Dest, std::monostate>)
81 {
82 return static_cast<Src>(d);
83 }
84 else
85 {
86 return static_cast<Dest>(d);
87 }
88}
89
90namespace dbus
91{
92boost::asio::io_context& getIOContext();
93std::shared_ptr<sdbusplus::asio::connection> getConnection();
94} // namespace dbus
95
Jonathan Domanee03a9b2020-11-11 12:56:23 -080096} // namespace cpu_info