Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 3 | #include "occ_pass_through.hpp" |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 4 | #include "occ_status.hpp" |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 5 | #ifdef PLDM |
| 6 | #include "pldm.hpp" |
| 7 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 8 | #include "powercap.hpp" |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 9 | #include "utils.hpp" |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 10 | #ifdef POWER10 |
| 11 | #include "powermode.hpp" |
| 12 | #endif |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 13 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 14 | #include <cstring> |
| 15 | #include <functional> |
| 16 | #include <sdbusplus/bus.hpp> |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 17 | #include <sdeventplus/event.hpp> |
| 18 | #include <sdeventplus/utility/timer.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 21 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 22 | namespace open_power |
| 23 | { |
| 24 | namespace occ |
| 25 | { |
| 26 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 27 | /** @brief Default time, in seconds, between OCC poll commands */ |
| 28 | constexpr unsigned int defaultPollingInterval = 10; |
| 29 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 30 | /** @class Manager |
| 31 | * @brief Builds and manages OCC objects |
| 32 | */ |
| 33 | struct Manager |
| 34 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 35 | public: |
| 36 | Manager() = delete; |
| 37 | Manager(const Manager&) = delete; |
| 38 | Manager& operator=(const Manager&) = delete; |
| 39 | Manager(Manager&&) = delete; |
| 40 | Manager& operator=(Manager&&) = delete; |
| 41 | ~Manager() = default; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 42 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 43 | /** @brief Adds OCC pass-through and status objects on the bus |
| 44 | * when corresponding CPU inventory is created. |
| 45 | * |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 46 | * @param[in] event - Unique ptr reference to sd_event |
| 47 | */ |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 48 | Manager(EventPtr& event) : |
| 49 | event(event), pollInterval(defaultPollingInterval), |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 50 | sdpEvent(sdeventplus::Event::get_default()), |
| 51 | _pollTimer( |
| 52 | std::make_unique< |
| 53 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 54 | sdpEvent, std::bind(&Manager::pollerTimerExpired, this))) |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 55 | #ifdef PLDM |
| 56 | , |
| 57 | pldmHandle(std::make_unique<pldm::Interface>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 58 | std::bind(std::mem_fn(&Manager::updateOCCActive), this, |
| 59 | std::placeholders::_1, std::placeholders::_2))) |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 60 | #endif |
| 61 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 62 | { |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 63 | #ifdef I2C_OCC |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 64 | // I2C OCC status objects are initialized directly |
| 65 | initStatusObjects(); |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 66 | #else |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 67 | findAndCreateObjects(); |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 68 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 69 | } |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 70 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 71 | /** @brief Return the number of bound OCCs */ |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 72 | inline auto getNumOCCs() const |
| 73 | { |
| 74 | return activeCount; |
| 75 | } |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 76 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 77 | private: |
| 78 | /** @brief Checks if the CPU inventory is present and if so, creates |
| 79 | * the occ D-Bus objects. Else, registers a handler to be |
| 80 | * called when inventory is created. |
| 81 | */ |
| 82 | void findAndCreateObjects(); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 83 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 84 | /** @brief Callback that responds to cpu creation in the inventory - |
| 85 | * by creating the needed objects. |
| 86 | * |
| 87 | * @param[in] msg - bus message |
| 88 | * |
| 89 | * @returns 0 to indicate success |
| 90 | */ |
| 91 | int cpuCreated(sdbusplus::message::message& msg); |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 92 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 93 | /** @brief Create child OCC objects. |
| 94 | * |
| 95 | * @param[in] occ - the occ name, such as occ0. |
| 96 | */ |
| 97 | void createObjects(const std::string& occ); |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 98 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 99 | /** @brief Callback handler invoked by Status object when the OccActive |
| 100 | * property is changed. This is needed to make sure that the |
| 101 | * error detection is started only after all the OCCs are bound. |
| 102 | * Similarly, when one of the OCC gets its OccActive property |
| 103 | * un-set, then the OCC error detection needs to be stopped on |
| 104 | * all the OCCs |
| 105 | * |
| 106 | * @param[in] status - OccActive status |
| 107 | */ |
| 108 | void statusCallBack(bool status); |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 109 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 110 | /** @brief Sends a Heartbeat command to host control command handler */ |
| 111 | void sendHeartBeat(); |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 112 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 113 | /** @brief reference to sd_event wrapped in unique_ptr */ |
| 114 | EventPtr& event; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 115 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 116 | /** @brief OCC pass-through objects */ |
| 117 | std::vector<std::unique_ptr<PassThrough>> passThroughObjects; |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 118 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 119 | /** @brief OCC Status objects */ |
| 120 | std::vector<std::unique_ptr<Status>> statusObjects; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 121 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 122 | /** @brief Power cap monitor and occ notification object */ |
| 123 | std::unique_ptr<open_power::occ::powercap::PowerCap> pcap; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 124 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 125 | #ifdef POWER10 |
| 126 | /** @brief Power mode monitor and notification object */ |
| 127 | std::unique_ptr<open_power::occ::powermode::PowerMode> pmode; |
| 128 | #endif |
| 129 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 130 | /** @brief sbdbusplus match objects */ |
| 131 | std::vector<sdbusplus::bus::match_t> cpuMatches; |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 132 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 133 | /** @brief Number of OCCs that are bound */ |
| 134 | uint8_t activeCount = 0; |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 135 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 136 | /** @brief Number of seconds between poll commands */ |
| 137 | uint8_t pollInterval; |
| 138 | |
| 139 | /** @brief Poll timer event */ |
| 140 | sdeventplus::Event sdpEvent; |
| 141 | |
| 142 | /** |
| 143 | * @brief The timer to be used once the OCC goes active. When it expires, |
| 144 | * a POLL command will be sent to the OCC and then timer restarted. |
| 145 | */ |
| 146 | std::unique_ptr< |
| 147 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 148 | _pollTimer; |
| 149 | |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 150 | #ifdef I2C_OCC |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 151 | /** @brief Init Status objects for I2C OCC devices |
| 152 | * |
| 153 | * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices |
| 154 | * and creates status objects. |
| 155 | */ |
| 156 | void initStatusObjects(); |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 157 | #endif |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 158 | |
| 159 | #ifdef PLDM |
| 160 | /** @brief Callback handler invoked by the PLDM event handler when state of |
| 161 | * the OCC is toggled by the host. The caller passes the instance |
| 162 | * of the OCC and state of the OCC. |
| 163 | * |
| 164 | * @param[in] instance - instance of the OCC |
| 165 | * @param[in] status - true when the OCC goes active and false when the OCC |
| 166 | * goes inactive |
| 167 | * |
| 168 | * @return true if setting the state of OCC is successful and false if it |
| 169 | * fails. |
| 170 | */ |
| 171 | bool updateOCCActive(instanceID instance, bool status); |
| 172 | |
| 173 | std::unique_ptr<pldm::Interface> pldmHandle = nullptr; |
| 174 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * @brief Called when poll timer expires and forces a POLL command to the |
| 178 | * OCC. The poll timer will then be restarted. |
| 179 | * */ |
| 180 | void pollerTimerExpired(); |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace occ |
| 184 | } // namespace open_power |