| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| Andrew Geissler | 2125439 | 2025-04-02 10:25:39 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 5 | #include "compatible_system_types_finder.hpp" |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 6 | #include "power_interface.hpp" |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 7 | #include "services.hpp" |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 8 | #include "system.hpp" |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 9 | |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 10 | #include <gpiod.hpp> |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 12 | #include <sdbusplus/server/object.hpp> |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 13 | #include <sdeventplus/clock.hpp> |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 14 | #include <sdeventplus/event.hpp> |
| 15 | #include <sdeventplus/utility/timer.hpp> |
| 16 | |
| 17 | #include <chrono> |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 18 | #include <filesystem> |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 19 | #include <map> |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 20 | #include <memory> |
| Jim Wright | eae2d61 | 2022-12-23 13:14:17 -0600 | [diff] [blame] | 21 | #include <string> |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 22 | #include <vector> |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 23 | |
| 24 | namespace phosphor::power::sequencer |
| 25 | { |
| 26 | |
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 27 | using PowerObject = sdbusplus::server::object_t<PowerInterface>; |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 28 | |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 29 | /** |
| 30 | * @class PowerControl |
| 31 | * This class implements GPIO control of power on / off, and monitoring of the |
| 32 | * chassis power good. |
| 33 | */ |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 34 | class PowerControl : public PowerObject |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | PowerControl() = delete; |
| 38 | PowerControl(const PowerControl&) = delete; |
| 39 | PowerControl& operator=(const PowerControl&) = delete; |
| 40 | PowerControl(PowerControl&&) = delete; |
| 41 | PowerControl& operator=(PowerControl&&) = delete; |
| 42 | ~PowerControl() = default; |
| 43 | |
| 44 | /** |
| 45 | * Creates a controller object for power on and off. |
| Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 46 | * @param bus D-Bus bus object |
| 47 | * @param event event object |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 48 | */ |
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 49 | PowerControl(sdbusplus::bus_t& bus, const sdeventplus::Event& event); |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 50 | |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 51 | /** @copydoc PowerInterface::getPgood() */ |
| 52 | int getPgood() const override; |
| 53 | |
| 54 | /** @copydoc PowerInterface::getPgoodTimeout() */ |
| 55 | int getPgoodTimeout() const override; |
| 56 | |
| 57 | /** @copydoc PowerInterface::getState() */ |
| 58 | int getState() const override; |
| 59 | |
| 60 | /** @copydoc PowerInterface::setPgoodTimeout() */ |
| 61 | void setPgoodTimeout(int timeout) override; |
| 62 | |
| 63 | /** @copydoc PowerInterface::setState() */ |
| 64 | void setState(int state) override; |
| 65 | |
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 66 | /** @copydoc PowerInterface::setPowerSupplyError() */ |
| 67 | void setPowerSupplyError(const std::string& error) override; |
| 68 | |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 69 | /** |
| 70 | * Callback that is called when a list of compatible system types is found. |
| 71 | * |
| 72 | * @param types Compatible system types for the current system ordered from |
| 73 | * most to least specific |
| 74 | */ |
| 75 | void compatibleSystemTypesFound(const std::vector<std::string>& types); |
| 76 | |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 77 | private: |
| 78 | /** |
| 79 | * The D-Bus bus object |
| 80 | */ |
| Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 81 | sdbusplus::bus_t& bus; |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 82 | |
| 83 | /** |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 84 | * System services like hardware presence and the journal. |
| Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 85 | */ |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 86 | BMCServices services; |
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 87 | |
| 88 | /** |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 89 | * Object that finds the compatible system types for the current system. |
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 90 | */ |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 91 | std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder; |
| 92 | |
| 93 | /** |
| 94 | * Compatible system types for the current system ordered from most to least |
| 95 | * specific. |
| 96 | */ |
| 97 | std::vector<std::string> compatibleSystemTypes; |
| 98 | |
| 99 | /** |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 100 | * Computer system being controlled and monitored by the BMC. |
| 101 | * |
| 102 | * Contains the information loaded from the JSON configuration file. |
| 103 | * Contains nullptr if the configuration file has not been loaded. |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 104 | */ |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 105 | std::unique_ptr<System> system; |
| Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 106 | |
| 107 | /** |
| Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 108 | * Indicates if a failure has already been found. Cleared at power on. |
| 109 | */ |
| 110 | bool failureFound{false}; |
| 111 | |
| 112 | /** |
| 113 | * Indicates if a state transition is taking place |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 114 | */ |
| 115 | bool inStateTransition{false}; |
| 116 | |
| 117 | /** |
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 118 | * Minimum time from cold start to power on constant |
| 119 | */ |
| 120 | static constexpr std::chrono::seconds minimumColdStartTime{15}; |
| 121 | |
| 122 | /** |
| 123 | * Minimum time from power off to power on constant |
| 124 | */ |
| 125 | static constexpr std::chrono::seconds minimumPowerOffTime{25}; |
| 126 | |
| 127 | /** |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 128 | * Power good |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 129 | */ |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 130 | int pgood{0}; |
| 131 | |
| 132 | /** |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 133 | * GPIO line object for chassis power good |
| 134 | */ |
| 135 | gpiod::line pgoodLine; |
| 136 | |
| 137 | /** |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 138 | * Power good timeout constant |
| 139 | */ |
| Andrew Geissler | 2125439 | 2025-04-02 10:25:39 -0500 | [diff] [blame] | 140 | static constexpr std::chrono::seconds pgoodTimeout{PGOOD_TIMEOUT}; |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 143 | * Point in time at which power good timeout will take place |
| 144 | */ |
| 145 | std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime; |
| 146 | |
| 147 | /** |
| Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 148 | * Timer to wait after pgood failure. This is to allow the power supplies |
| 149 | * and other hardware time to complete failure processing. |
| 150 | */ |
| 151 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pgoodWaitTimer; |
| 152 | |
| 153 | /** |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 154 | * Poll interval constant |
| 155 | */ |
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 156 | static constexpr std::chrono::milliseconds pollInterval{3000}; |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 157 | |
| 158 | /** |
| Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 159 | * GPIO line object for power on / power off control |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 160 | */ |
| 161 | gpiod::line powerControlLine; |
| 162 | |
| 163 | /** |
| Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 164 | * Point in time at which minumum power off time will have passed |
| 165 | */ |
| 166 | std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime; |
| 167 | |
| 168 | /** |
| Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 169 | * Power supply error. Cleared at power on. |
| Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 170 | */ |
| 171 | std::string powerSupplyError; |
| 172 | |
| 173 | /** |
| Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 174 | * Power state |
| 175 | */ |
| 176 | int state{0}; |
| 177 | |
| 178 | /** |
| 179 | * Power good timeout |
| 180 | */ |
| 181 | std::chrono::seconds timeout{pgoodTimeout}; |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Timer to poll the pgood |
| 185 | */ |
| 186 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
| 187 | |
| 188 | /** |
| Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 189 | * Callback to begin failure processing after observing pgood failure wait |
| 190 | */ |
| 191 | void onFailureCallback(); |
| 192 | |
| 193 | /** |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 194 | * Begin pgood failure processing |
| 195 | * |
| 196 | * @param wasTimeOut Indicates whether failure state was determined by |
| 197 | * timing out |
| Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 198 | */ |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 199 | void onFailure(bool wasTimeOut); |
| Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 200 | |
| 201 | /** |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 202 | * Checks whether a pgood fault has occurred on one of the rails being |
| 203 | * monitored. |
| 204 | * |
| 205 | * If a pgood fault was found, this method returns a string containing the |
| 206 | * error that should be logged. If no fault was found, an empty string is |
| 207 | * returned. |
| 208 | * |
| 209 | * @param additionalData Additional data to include in the error log if |
| 210 | * a pgood fault was found |
| 211 | * @return error that should be logged if a pgood fault was found, or an |
| 212 | * empty string if no pgood fault was found |
| 213 | */ |
| 214 | std::string findPgoodFault( |
| 215 | std::map<std::string, std::string>& additionalData); |
| 216 | |
| 217 | /** |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 218 | * Polling method for monitoring the system power good |
| 219 | */ |
| 220 | void pollPgood(); |
| Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * Set up GPIOs |
| 224 | */ |
| 225 | void setUpGpio(); |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 226 | |
| 227 | /** |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 228 | * Loads the JSON configuration file. |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 229 | * |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 230 | * Looks for the config file using findConfigFile(). |
| 231 | * |
| 232 | * If the config file is found, it is parsed and the resulting information |
| 233 | * is stored in the system data member. |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 234 | */ |
| Shawn McCarney | 5a8aefe | 2025-11-13 14:38:53 -0600 | [diff] [blame] | 235 | void loadConfigFile(); |
| Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Finds the JSON configuration file for the current system based on the |
| 239 | * compatible system types. |
| 240 | * |
| 241 | * Does nothing if the compatible system types have not been found yet. |
| 242 | * |
| 243 | * @return absolute path to the config file, or empty path if file not found |
| 244 | */ |
| 245 | std::filesystem::path findConfigFile(); |
| Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | } // namespace phosphor::power::sequencer |