blob: 926c2d186ea0b39af1a7b460bf8ae755b87919d2 [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 Doman16a2ced2021-11-01 11:13:22 -070050constexpr uint64_t bit(uint8_t index)
51{
52 return (1ull << index);
53}
54
55/**
56 * Extract a bitfield from an input data by shifting and masking.
57 *
58 * @tparam Dest Destination type - mostly useful to avoid an extra static_cast
59 * at the call site. Defaults to the Src type if unspecified.
60 * @tparam Src Automatically deduced from the first positional parameter.
61 *
62 * @param data Input data.
63 * @param loBit 0-based index of the least significant bit to return.
64 * @param hiBit 0-based index of the most significant bit to return.
65 */
66template <typename Dest = std::monostate, typename Src>
67auto mask(Src data, unsigned int loBit, unsigned int hiBit)
68{
69 assert(hiBit >= loBit);
70 uint64_t d = data;
71 d >>= loBit;
72 d &= (1ull << (hiBit - loBit + 1)) - 1;
73 if constexpr (std::is_same_v<Dest, std::monostate>)
74 {
75 return static_cast<Src>(d);
76 }
77 else
78 {
79 return static_cast<Dest>(d);
80 }
81}
82
83namespace dbus
84{
85boost::asio::io_context& getIOContext();
86std::shared_ptr<sdbusplus::asio::connection> getConnection();
87} // namespace dbus
88
Jonathan Domanee03a9b2020-11-11 12:56:23 -080089} // namespace cpu_info