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" |
| 6 | #include "device_finder.hpp" |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 7 | #include "power_interface.hpp" |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 8 | #include "power_sequencer_device.hpp" |
| 9 | #include "rail.hpp" |
| 10 | #include "services.hpp" |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 11 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 12 | #include <gpiod.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 14 | #include <sdbusplus/server/object.hpp> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 15 | #include <sdeventplus/clock.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 16 | #include <sdeventplus/event.hpp> |
| 17 | #include <sdeventplus/utility/timer.hpp> |
| 18 | |
| 19 | #include <chrono> |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 20 | #include <filesystem> |
| 21 | #include <memory> |
| 22 | #include <optional> |
Jim Wright | eae2d61 | 2022-12-23 13:14:17 -0600 | [diff] [blame] | 23 | #include <string> |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 24 | #include <vector> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 25 | |
| 26 | namespace phosphor::power::sequencer |
| 27 | { |
| 28 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 29 | using PowerObject = sdbusplus::server::object_t<PowerInterface>; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 30 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 31 | /** |
| 32 | * @class PowerControl |
| 33 | * This class implements GPIO control of power on / off, and monitoring of the |
| 34 | * chassis power good. |
| 35 | */ |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 36 | class PowerControl : public PowerObject |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 37 | { |
| 38 | public: |
| 39 | PowerControl() = delete; |
| 40 | PowerControl(const PowerControl&) = delete; |
| 41 | PowerControl& operator=(const PowerControl&) = delete; |
| 42 | PowerControl(PowerControl&&) = delete; |
| 43 | PowerControl& operator=(PowerControl&&) = delete; |
| 44 | ~PowerControl() = default; |
| 45 | |
| 46 | /** |
| 47 | * Creates a controller object for power on and off. |
Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 48 | * @param bus D-Bus bus object |
| 49 | * @param event event object |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 50 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 51 | PowerControl(sdbusplus::bus_t& bus, const sdeventplus::Event& event); |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 52 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 53 | /** @copydoc PowerInterface::getPgood() */ |
| 54 | int getPgood() const override; |
| 55 | |
| 56 | /** @copydoc PowerInterface::getPgoodTimeout() */ |
| 57 | int getPgoodTimeout() const override; |
| 58 | |
| 59 | /** @copydoc PowerInterface::getState() */ |
| 60 | int getState() const override; |
| 61 | |
| 62 | /** @copydoc PowerInterface::setPgoodTimeout() */ |
| 63 | void setPgoodTimeout(int timeout) override; |
| 64 | |
| 65 | /** @copydoc PowerInterface::setState() */ |
| 66 | void setState(int state) override; |
| 67 | |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 68 | /** @copydoc PowerInterface::setPowerSupplyError() */ |
| 69 | void setPowerSupplyError(const std::string& error) override; |
| 70 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 71 | /** |
| 72 | * Callback that is called when a list of compatible system types is found. |
| 73 | * |
| 74 | * @param types Compatible system types for the current system ordered from |
| 75 | * most to least specific |
| 76 | */ |
| 77 | void compatibleSystemTypesFound(const std::vector<std::string>& types); |
| 78 | |
| 79 | /** |
| 80 | * Callback that is called when a power sequencer device is found. |
| 81 | * |
| 82 | * @param properties Properties of device that was found |
| 83 | */ |
| 84 | void deviceFound(const DeviceProperties& properties); |
| 85 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 86 | private: |
| 87 | /** |
| 88 | * The D-Bus bus object |
| 89 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 90 | sdbusplus::bus_t& bus; |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 91 | |
| 92 | /** |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 93 | * System services like hardware presence and the journal. |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 94 | */ |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 95 | BMCServices services; |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 96 | |
| 97 | /** |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 98 | * Object that finds the compatible system types for the current system. |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 99 | */ |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 100 | std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder; |
| 101 | |
| 102 | /** |
| 103 | * Compatible system types for the current system ordered from most to least |
| 104 | * specific. |
| 105 | */ |
| 106 | std::vector<std::string> compatibleSystemTypes; |
| 107 | |
| 108 | /** |
| 109 | * Object that finds the power sequencer device in the system. |
| 110 | */ |
| 111 | std::unique_ptr<DeviceFinder> deviceFinder; |
| 112 | |
| 113 | /** |
| 114 | * Power sequencer device properties. |
| 115 | */ |
| 116 | std::optional<DeviceProperties> deviceProperties; |
| 117 | |
| 118 | /** |
| 119 | * Power sequencer device that enables and monitors the voltage rails. |
| 120 | */ |
| 121 | std::unique_ptr<PowerSequencerDevice> device; |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 122 | |
| 123 | /** |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 124 | * Indicates if a failure has already been found. Cleared at power on. |
| 125 | */ |
| 126 | bool failureFound{false}; |
| 127 | |
| 128 | /** |
| 129 | * Indicates if a state transition is taking place |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 130 | */ |
| 131 | bool inStateTransition{false}; |
| 132 | |
| 133 | /** |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 134 | * Minimum time from cold start to power on constant |
| 135 | */ |
| 136 | static constexpr std::chrono::seconds minimumColdStartTime{15}; |
| 137 | |
| 138 | /** |
| 139 | * Minimum time from power off to power on constant |
| 140 | */ |
| 141 | static constexpr std::chrono::seconds minimumPowerOffTime{25}; |
| 142 | |
| 143 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 144 | * Power good |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 145 | */ |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 146 | int pgood{0}; |
| 147 | |
| 148 | /** |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 149 | * GPIO line object for chassis power good |
| 150 | */ |
| 151 | gpiod::line pgoodLine; |
| 152 | |
| 153 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 154 | * Power good timeout constant |
| 155 | */ |
Andrew Geissler | 2125439 | 2025-04-02 10:25:39 -0500 | [diff] [blame] | 156 | static constexpr std::chrono::seconds pgoodTimeout{PGOOD_TIMEOUT}; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 157 | |
| 158 | /** |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 159 | * Point in time at which power good timeout will take place |
| 160 | */ |
| 161 | std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime; |
| 162 | |
| 163 | /** |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 164 | * Timer to wait after pgood failure. This is to allow the power supplies |
| 165 | * and other hardware time to complete failure processing. |
| 166 | */ |
| 167 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pgoodWaitTimer; |
| 168 | |
| 169 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 170 | * Poll interval constant |
| 171 | */ |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 172 | static constexpr std::chrono::milliseconds pollInterval{3000}; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 173 | |
| 174 | /** |
Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 175 | * GPIO line object for power on / power off control |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 176 | */ |
| 177 | gpiod::line powerControlLine; |
| 178 | |
| 179 | /** |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 180 | * Point in time at which minumum power off time will have passed |
| 181 | */ |
| 182 | std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime; |
| 183 | |
| 184 | /** |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 185 | * Power supply error. Cleared at power on. |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 186 | */ |
| 187 | std::string powerSupplyError; |
| 188 | |
| 189 | /** |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 190 | * Power state |
| 191 | */ |
| 192 | int state{0}; |
| 193 | |
| 194 | /** |
| 195 | * Power good timeout |
| 196 | */ |
| 197 | std::chrono::seconds timeout{pgoodTimeout}; |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Timer to poll the pgood |
| 201 | */ |
| 202 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
| 203 | |
| 204 | /** |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 205 | * Callback to begin failure processing after observing pgood failure wait |
| 206 | */ |
| 207 | void onFailureCallback(); |
| 208 | |
| 209 | /** |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 210 | * Begin pgood failure processing |
| 211 | * |
| 212 | * @param wasTimeOut Indicates whether failure state was determined by |
| 213 | * timing out |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 214 | */ |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 215 | void onFailure(bool wasTimeOut); |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 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 | /** |
| 228 | * Loads the JSON configuration file and creates the power sequencer device |
| 229 | * object. |
| 230 | * |
| 231 | * Does nothing if the compatible system types or device properties have not |
| 232 | * been found yet. These are obtained from D-Bus. The order in which they |
| 233 | * are found and the time to find them varies. |
| 234 | */ |
| 235 | void loadConfigFileAndCreateDevice(); |
| 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(); |
| 246 | |
| 247 | /** |
| 248 | * Parses the specified JSON configuration file. |
| 249 | * |
| 250 | * Returns the resulting vector of Rail objects in the output parameter. |
| 251 | * |
| 252 | * @param configFile Absolute path to the config file |
| 253 | * @param rails Rail objects within the config file |
| 254 | * @return true if file was parsed successfully, false otherwise |
| 255 | */ |
| 256 | bool parseConfigFile(const std::filesystem::path& configFile, |
| 257 | std::vector<std::unique_ptr<Rail>>& rails); |
| 258 | |
| 259 | /** |
| 260 | * Creates the power sequencer device object based on the device properties. |
| 261 | * |
| 262 | * Does nothing if the device properties have not been found yet. |
| 263 | * |
| 264 | * @param rails Voltage rails that are enabled and monitored by the device |
| 265 | */ |
| 266 | void createDevice(std::vector<std::unique_ptr<Rail>> rails); |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | } // namespace phosphor::power::sequencer |