Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 3 | #include "power_supply.hpp" |
| 4 | #include "types.hpp" |
| 5 | #include "utility.hpp" |
| 6 | |
| 7 | #include <phosphor-logging/log.hpp> |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus/match.hpp> |
Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 9 | #include <sdbusplus/server/manager.hpp> |
| 10 | #include <sdbusplus/server/object.hpp> |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 11 | #include <sdeventplus/event.hpp> |
| 12 | #include <sdeventplus/utility/timer.hpp> |
Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 13 | #include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp> |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 14 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 15 | struct sys_properties |
| 16 | { |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 17 | int powerSupplyCount; |
| 18 | std::vector<uint64_t> inputVoltage; |
Adriana Kobylak | 886574c | 2021-11-01 18:22:28 +0000 | [diff] [blame] | 19 | bool powerConfigFullLoad; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 20 | }; |
| 21 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 22 | using namespace phosphor::power::psu; |
| 23 | using namespace phosphor::logging; |
| 24 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 25 | namespace phosphor::power::manager |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 26 | { |
| 27 | |
Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 28 | using PowerSystemInputsInterface = sdbusplus::xyz::openbmc_project::State:: |
| 29 | Decorator::server::PowerSystemInputs; |
| 30 | using PowerSystemInputsObject = |
| 31 | sdbusplus::server::object_t<PowerSystemInputsInterface>; |
| 32 | |
Adriana Kobylak | 2aba2b2 | 2021-10-11 16:00:05 +0000 | [diff] [blame] | 33 | // Validation timeout. Allow 10s to detect if new EM interfaces show up in D-Bus |
| 34 | // before performing the validation. |
| 35 | constexpr auto validationTimeout = std::chrono::seconds(10); |
Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 36 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 37 | /** |
Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 38 | * @class PowerSystemInputs |
| 39 | * @brief A concrete implementation for the PowerSystemInputs interface. |
| 40 | */ |
| 41 | class PowerSystemInputs : public PowerSystemInputsObject |
| 42 | { |
| 43 | public: |
| 44 | PowerSystemInputs(sdbusplus::bus::bus& bus, const std::string& path) : |
| 45 | PowerSystemInputsObject(bus, path.c_str()) |
| 46 | {} |
| 47 | }; |
| 48 | |
| 49 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 50 | * @class PSUManager |
| 51 | * |
| 52 | * This class will create an object used to manage and monitor a list of power |
| 53 | * supply devices. |
| 54 | */ |
| 55 | class PSUManager |
| 56 | { |
| 57 | public: |
| 58 | PSUManager() = delete; |
| 59 | ~PSUManager() = default; |
| 60 | PSUManager(const PSUManager&) = delete; |
| 61 | PSUManager& operator=(const PSUManager&) = delete; |
| 62 | PSUManager(PSUManager&&) = delete; |
| 63 | PSUManager& operator=(PSUManager&&) = delete; |
| 64 | |
| 65 | /** |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 66 | * Constructor to read configuration from D-Bus. |
| 67 | * |
| 68 | * @param[in] bus - D-Bus bus object |
| 69 | * @param[in] e - event object |
| 70 | */ |
| 71 | PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e); |
| 72 | |
| 73 | /** |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 74 | * Get PSU properties from D-Bus, use that to build a power supply |
| 75 | * object. |
| 76 | * |
| 77 | * @param[in] properties - A map of property names and values |
| 78 | * |
| 79 | */ |
| 80 | void getPSUProperties(util::DbusPropertyMap& properties); |
| 81 | |
| 82 | /** |
| 83 | * Get PSU configuration from D-Bus |
| 84 | */ |
| 85 | void getPSUConfiguration(); |
| 86 | |
| 87 | /** |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 88 | * @brief Initialize the system properties from the Supported Configuration |
| 89 | * D-Bus object provided by Entity Manager. |
| 90 | */ |
| 91 | void getSystemProperties(); |
| 92 | |
| 93 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 94 | * Initializes the manager. |
| 95 | * |
| 96 | * Get current BMC state, ... |
| 97 | */ |
| 98 | void initialize() |
| 99 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 100 | // When state = 1, system is powered on |
| 101 | int32_t state = 0; |
| 102 | |
| 103 | try |
| 104 | { |
| 105 | // Use getProperty utility function to get power state. |
| 106 | util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, |
| 107 | powerService, bus, state); |
| 108 | |
| 109 | if (state) |
| 110 | { |
| 111 | powerOn = true; |
Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 112 | validationTimer->restartOnce(validationTimeout); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 113 | } |
| 114 | else |
| 115 | { |
| 116 | powerOn = false; |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 117 | runValidateConfig = true; |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 120 | catch (const std::exception& e) |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 121 | { |
| 122 | log<level::INFO>("Failed to get power state. Assuming it is off."); |
| 123 | powerOn = false; |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 124 | runValidateConfig = true; |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 127 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 128 | clearFaults(); |
| 129 | updateInventory(); |
Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 130 | setPowerConfigGPIO(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Starts the timer to start monitoring the list of devices. |
| 135 | */ |
| 136 | int run() |
| 137 | { |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 138 | return timer->get_event().loop(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 142 | * Write PMBus ON_OFF_CONFIG |
| 143 | * |
| 144 | * This function will be called to cause the PMBus device driver to send the |
| 145 | * ON_OFF_CONFIG command. Takes one byte of data. |
| 146 | */ |
| 147 | void onOffConfig(const uint8_t data) |
| 148 | { |
| 149 | for (auto& psu : psus) |
| 150 | { |
| 151 | psu->onOffConfig(data); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 156 | * This function will be called in various situations in order to clear |
| 157 | * any fault status bits that may have been set, in order to start over |
| 158 | * with a clean state. Presence changes and power state changes will want |
| 159 | * to clear any faults logged. |
| 160 | */ |
| 161 | void clearFaults() |
| 162 | { |
Brandon Wyman | 10fc6e8 | 2022-02-08 20:51:22 +0000 | [diff] [blame] | 163 | setPowerSupplyError(""); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 164 | for (auto& psu : psus) |
| 165 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 166 | psu->clearFaults(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 167 | } |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private: |
| 171 | /** |
| 172 | * The D-Bus object |
| 173 | */ |
| 174 | sdbusplus::bus::bus& bus; |
| 175 | |
| 176 | /** |
| 177 | * The timer that runs to periodically check the power supplies. |
| 178 | */ |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 179 | std::unique_ptr< |
| 180 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 181 | timer; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 182 | |
| 183 | /** |
Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 184 | * The timer that performs power supply validation as the entity manager |
| 185 | * interfaces show up in d-bus. |
| 186 | */ |
| 187 | std::unique_ptr< |
| 188 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 189 | validationTimer; |
| 190 | |
| 191 | /** |
Brandon Wyman | 10fc6e8 | 2022-02-08 20:51:22 +0000 | [diff] [blame] | 192 | * Let power control/sequencer application know of PSU error(s). |
| 193 | * |
| 194 | * @param[in] psuErrorString - string for power supply error |
| 195 | */ |
| 196 | void setPowerSupplyError(const std::string& psuErrorString); |
| 197 | |
| 198 | /** |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 199 | * Create an error |
| 200 | * |
| 201 | * @param[in] faultName - 'name' message for the BMC error log entry |
Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 202 | * @param[in,out] additionalData - The AdditionalData property for the error |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 203 | */ |
| 204 | void createError(const std::string& faultName, |
Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 205 | std::map<std::string, std::string>& additionalData); |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 206 | |
| 207 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 208 | * Analyze the status of each of the power supplies. |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 209 | * |
| 210 | * Log errors for faults, when and where appropriate. |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 211 | */ |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 212 | void analyze(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 213 | |
| 214 | /** @brief True if the power is on. */ |
| 215 | bool powerOn = false; |
| 216 | |
Adriana Kobylak | 2549d79 | 2022-01-26 20:51:30 +0000 | [diff] [blame] | 217 | /** @brief True if an error for a brownout has already been logged. */ |
| 218 | bool brownoutLogged = false; |
| 219 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 220 | /** @brief Used as part of subscribing to power on state changes*/ |
| 221 | std::string powerService; |
| 222 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 223 | /** @brief Used to subscribe to D-Bus power on state changes */ |
| 224 | std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch; |
| 225 | |
Adriana Kobylak | 9ba3823 | 2021-11-16 20:27:45 +0000 | [diff] [blame] | 226 | /** @brief Used to subscribe to D-Bus power supply presence changes */ |
| 227 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> presenceMatches; |
| 228 | |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 229 | /** @brief Used to subscribe to Entity Manager interfaces added */ |
| 230 | std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch; |
| 231 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 232 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 233 | * @brief Callback for power state property changes |
| 234 | * |
| 235 | * Process changes to the powered on state property for the system. |
| 236 | * |
| 237 | * @param[in] msg - Data associated with the power state signal |
| 238 | */ |
| 239 | void powerStateChanged(sdbusplus::message::message& msg); |
| 240 | |
| 241 | /** |
Adriana Kobylak | 9ba3823 | 2021-11-16 20:27:45 +0000 | [diff] [blame] | 242 | * @brief Callback for inventory property changes |
| 243 | * |
| 244 | * Process change of the Present property for power supply. |
| 245 | * |
| 246 | * @param[in] msg - Data associated with the Present change signal |
| 247 | **/ |
| 248 | void presenceChanged(sdbusplus::message::message& msg); |
| 249 | |
| 250 | /** |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 251 | * @brief Callback for entity-manager interface added |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 252 | * |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 253 | * Process the information from the supported configuration and or IBM CFFPS |
| 254 | * Connector interface being added. |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 255 | * |
| 256 | * @param[in] msg - Data associated with the interfaces added signal |
| 257 | */ |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 258 | void entityManagerIfaceAdded(sdbusplus::message::message& msg); |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 259 | |
| 260 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 261 | * @brief Adds properties to the inventory. |
| 262 | * |
| 263 | * Reads the values from the devices and writes them to the associated |
| 264 | * power supply D-Bus inventory objects. |
| 265 | * |
| 266 | * This needs to be done on startup, and each time the presence state |
| 267 | * changes. |
| 268 | */ |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 269 | void updateInventory() |
| 270 | { |
| 271 | for (auto& psu : psus) |
| 272 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 273 | psu->updateInventory(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 278 | * @brief Helper function to populate the system properties |
| 279 | * |
| 280 | * @param[in] properties - A map of property names and values |
| 281 | */ |
| 282 | void populateSysProperties(const util::DbusPropertyMap& properties); |
| 283 | |
| 284 | /** |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 285 | * @brief Perform power supply configuration validation. |
| 286 | * @details Validates if the existing power supply properties are a |
| 287 | * supported configuration, and acts on its findings such as logging errors. |
| 288 | */ |
| 289 | void validateConfig(); |
| 290 | |
| 291 | /** |
| 292 | * @brief Flag to indicate if the validateConfig() function should be run. |
| 293 | * Set to false once the configuration has been validated to avoid running |
| 294 | * multiple times due to interfaces added signal. Set to true during power |
| 295 | * off to trigger the validation on power on. |
| 296 | */ |
| 297 | bool runValidateConfig = true; |
| 298 | |
| 299 | /** |
Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 300 | * @brief Check that all PSUs have the same model name and that the system |
| 301 | * has the required number of PSUs present as specified in the Supported |
| 302 | * Configuration interface. |
| 303 | * |
| 304 | * @param[out] additionalData - Contains debug information on why the check |
| 305 | * might have failed. Can be used to fill in error logs. |
| 306 | * @return true if all the required PSUs are present, false otherwise. |
| 307 | */ |
| 308 | bool hasRequiredPSUs(std::map<std::string, std::string>& additionalData); |
| 309 | |
| 310 | /** |
Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 311 | * @brief Helper function to validate that all PSUs have the same model name |
| 312 | * |
| 313 | * @param[out] model - The model name. Empty if there is a mismatch. |
| 314 | * @param[out] additionalData - If there is a mismatch, it contains debug |
| 315 | * information such as the mismatched model name. |
| 316 | * @return true if all the PSUs have the same model name, false otherwise. |
| 317 | */ |
| 318 | bool validateModelName(std::string& model, |
| 319 | std::map<std::string, std::string>& additionalData); |
| 320 | |
| 321 | /** |
Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 322 | * @brief Set the power-config-full-load GPIO depending on the EM full load |
| 323 | * property value. |
| 324 | */ |
| 325 | void setPowerConfigGPIO(); |
| 326 | |
| 327 | /** |
Adriana Kobylak | e5b1e08 | 2022-03-02 15:37:32 +0000 | [diff] [blame] | 328 | * @brief Indicate that the system is in a brownout condition by creating an |
| 329 | * error log and setting the PowerSystemInputs status property to Fault. |
| 330 | * |
| 331 | * @param[in] additionalData - Contains debug information on the number of |
| 332 | * PSUs in fault state or not present. |
| 333 | */ |
| 334 | void setBrownout(std::map<std::string, std::string>& additionalData); |
| 335 | |
| 336 | /** |
| 337 | * @brief Indicate that the system is no longer in a brownout condition by |
| 338 | * setting the PowerSystemInputs status property to Good. |
| 339 | */ |
| 340 | void clearBrownout(); |
| 341 | |
| 342 | /** |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 343 | * @brief Map of supported PSU configurations that include the model name |
| 344 | * and their properties. |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 345 | */ |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 346 | std::map<std::string, sys_properties> supportedConfigs; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 347 | |
| 348 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 349 | * @brief The vector for power supplies. |
| 350 | */ |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 351 | std::vector<std::unique_ptr<PowerSupply>> psus; |
Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 352 | |
| 353 | /** |
| 354 | * @brief The libgpiod object for setting the power supply config |
| 355 | */ |
| 356 | std::unique_ptr<GPIOInterfaceBase> powerConfigGPIO = nullptr; |
Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 357 | |
| 358 | /** |
| 359 | * @brief PowerSystemInputs object |
| 360 | */ |
| 361 | PowerSystemInputs powerSystemInputs; |
| 362 | |
| 363 | /** |
| 364 | * @brief Implement the org.freedesktop.DBus.ObjectManager interface |
| 365 | */ |
| 366 | sdbusplus::server::manager_t objectManager; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 367 | }; |
| 368 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 369 | } // namespace phosphor::power::manager |