blob: d6f17d28297971778784531a8e787b29f20c27ae [file] [log] [blame]
Jim Wright539b6082021-08-02 14:50:23 -05001#pragma once
2
Shawn McCarney1f8b1102024-06-21 18:57:46 -05003#include "compatible_system_types_finder.hpp"
4#include "device_finder.hpp"
Jim Wright22318a32021-08-27 15:56:09 -05005#include "power_interface.hpp"
Shawn McCarney1f8b1102024-06-21 18:57:46 -05006#include "power_sequencer_device.hpp"
7#include "rail.hpp"
8#include "services.hpp"
Jim Wright22318a32021-08-27 15:56:09 -05009
Jim Wright7a5dd992021-08-31 16:56:52 -050010#include <gpiod.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050011#include <sdbusplus/bus.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050012#include <sdbusplus/server/object.hpp>
Jim Wright7a5dd992021-08-31 16:56:52 -050013#include <sdeventplus/clock.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050014#include <sdeventplus/event.hpp>
15#include <sdeventplus/utility/timer.hpp>
16
17#include <chrono>
Shawn McCarney1f8b1102024-06-21 18:57:46 -050018#include <filesystem>
19#include <memory>
20#include <optional>
Jim Wrighteae2d612022-12-23 13:14:17 -060021#include <string>
Shawn McCarney1f8b1102024-06-21 18:57:46 -050022#include <vector>
Jim Wright539b6082021-08-02 14:50:23 -050023
24namespace phosphor::power::sequencer
25{
26
Patrick Williams7354ce62022-07-22 19:26:56 -050027using PowerObject = sdbusplus::server::object_t<PowerInterface>;
Jim Wright22318a32021-08-27 15:56:09 -050028
Jim Wright539b6082021-08-02 14:50:23 -050029/**
30 * @class PowerControl
31 * This class implements GPIO control of power on / off, and monitoring of the
32 * chassis power good.
33 */
Jim Wright22318a32021-08-27 15:56:09 -050034class PowerControl : public PowerObject
Jim Wright539b6082021-08-02 14:50:23 -050035{
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 Wright213ffe92022-06-03 08:54:30 -050046 * @param bus D-Bus bus object
47 * @param event event object
Jim Wright539b6082021-08-02 14:50:23 -050048 */
Patrick Williams7354ce62022-07-22 19:26:56 -050049 PowerControl(sdbusplus::bus_t& bus, const sdeventplus::Event& event);
Jim Wright539b6082021-08-02 14:50:23 -050050
Jim Wright22318a32021-08-27 15:56:09 -050051 /** @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 Wrightccea2d22021-12-10 14:10:46 -060066 /** @copydoc PowerInterface::setPowerSupplyError() */
67 void setPowerSupplyError(const std::string& error) override;
68
Shawn McCarney1f8b1102024-06-21 18:57:46 -050069 /**
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
77 /**
78 * Callback that is called when a power sequencer device is found.
79 *
80 * @param properties Properties of device that was found
81 */
82 void deviceFound(const DeviceProperties& properties);
83
Jim Wright539b6082021-08-02 14:50:23 -050084 private:
85 /**
86 * The D-Bus bus object
87 */
Patrick Williams7354ce62022-07-22 19:26:56 -050088 sdbusplus::bus_t& bus;
Jim Wright539b6082021-08-02 14:50:23 -050089
90 /**
Shawn McCarney1f8b1102024-06-21 18:57:46 -050091 * System services like hardware presence and the journal.
Jim Wright7945dd22021-04-06 16:55:15 -050092 */
Shawn McCarney1f8b1102024-06-21 18:57:46 -050093 BMCServices services;
Jim Wrightccea2d22021-12-10 14:10:46 -060094
95 /**
Shawn McCarney1f8b1102024-06-21 18:57:46 -050096 * Object that finds the compatible system types for the current system.
Jim Wrightccea2d22021-12-10 14:10:46 -060097 */
Shawn McCarney1f8b1102024-06-21 18:57:46 -050098 std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder;
99
100 /**
101 * Compatible system types for the current system ordered from most to least
102 * specific.
103 */
104 std::vector<std::string> compatibleSystemTypes;
105
106 /**
107 * Object that finds the power sequencer device in the system.
108 */
109 std::unique_ptr<DeviceFinder> deviceFinder;
110
111 /**
112 * Power sequencer device properties.
113 */
114 std::optional<DeviceProperties> deviceProperties;
115
116 /**
117 * Power sequencer device that enables and monitors the voltage rails.
118 */
119 std::unique_ptr<PowerSequencerDevice> device;
Jim Wright7945dd22021-04-06 16:55:15 -0500120
121 /**
Jim Wright48752622022-02-28 20:37:53 -0600122 * Indicates if a failure has already been found. Cleared at power on.
123 */
124 bool failureFound{false};
125
126 /**
127 * Indicates if a state transition is taking place
Jim Wright7a5dd992021-08-31 16:56:52 -0500128 */
129 bool inStateTransition{false};
130
131 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600132 * Minimum time from cold start to power on constant
133 */
134 static constexpr std::chrono::seconds minimumColdStartTime{15};
135
136 /**
137 * Minimum time from power off to power on constant
138 */
139 static constexpr std::chrono::seconds minimumPowerOffTime{25};
140
141 /**
Jim Wright22318a32021-08-27 15:56:09 -0500142 * Power good
Jim Wright539b6082021-08-02 14:50:23 -0500143 */
Jim Wright22318a32021-08-27 15:56:09 -0500144 int pgood{0};
145
146 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500147 * GPIO line object for chassis power good
148 */
149 gpiod::line pgoodLine;
150
151 /**
Jim Wright22318a32021-08-27 15:56:09 -0500152 * Power good timeout constant
153 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600154 static constexpr std::chrono::seconds pgoodTimeout{10};
Jim Wright22318a32021-08-27 15:56:09 -0500155
156 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500157 * Point in time at which power good timeout will take place
158 */
159 std::chrono::time_point<std::chrono::steady_clock> pgoodTimeoutTime;
160
161 /**
Jim Wright1b639532022-11-19 17:41:33 -0600162 * Timer to wait after pgood failure. This is to allow the power supplies
163 * and other hardware time to complete failure processing.
164 */
165 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pgoodWaitTimer;
166
167 /**
Jim Wright22318a32021-08-27 15:56:09 -0500168 * Poll interval constant
169 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600170 static constexpr std::chrono::milliseconds pollInterval{3000};
Jim Wright22318a32021-08-27 15:56:09 -0500171
172 /**
Jim Wright213ffe92022-06-03 08:54:30 -0500173 * GPIO line object for power on / power off control
Jim Wright7a5dd992021-08-31 16:56:52 -0500174 */
175 gpiod::line powerControlLine;
176
177 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600178 * Point in time at which minumum power off time will have passed
179 */
180 std::chrono::time_point<std::chrono::steady_clock> powerOnAllowedTime;
181
182 /**
Jim Wright48752622022-02-28 20:37:53 -0600183 * Power supply error. Cleared at power on.
Jim Wrightccea2d22021-12-10 14:10:46 -0600184 */
185 std::string powerSupplyError;
186
187 /**
Jim Wright22318a32021-08-27 15:56:09 -0500188 * Power state
189 */
190 int state{0};
191
192 /**
193 * Power good timeout
194 */
195 std::chrono::seconds timeout{pgoodTimeout};
Jim Wright539b6082021-08-02 14:50:23 -0500196
197 /**
198 * Timer to poll the pgood
199 */
200 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
201
202 /**
Jim Wright1b639532022-11-19 17:41:33 -0600203 * Callback to begin failure processing after observing pgood failure wait
204 */
205 void onFailureCallback();
206
207 /**
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500208 * Begin pgood failure processing
209 *
210 * @param wasTimeOut Indicates whether failure state was determined by
211 * timing out
Jim Wright1b639532022-11-19 17:41:33 -0600212 */
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500213 void onFailure(bool wasTimeOut);
Jim Wright1b639532022-11-19 17:41:33 -0600214
215 /**
Jim Wright539b6082021-08-02 14:50:23 -0500216 * Polling method for monitoring the system power good
217 */
218 void pollPgood();
Jim Wright7a5dd992021-08-31 16:56:52 -0500219
220 /**
221 * Set up GPIOs
222 */
223 void setUpGpio();
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500224
225 /**
226 * Loads the JSON configuration file and creates the power sequencer device
227 * object.
228 *
229 * Does nothing if the compatible system types or device properties have not
230 * been found yet. These are obtained from D-Bus. The order in which they
231 * are found and the time to find them varies.
232 */
233 void loadConfigFileAndCreateDevice();
234
235 /**
236 * Finds the JSON configuration file for the current system based on the
237 * compatible system types.
238 *
239 * Does nothing if the compatible system types have not been found yet.
240 *
241 * @return absolute path to the config file, or empty path if file not found
242 */
243 std::filesystem::path findConfigFile();
244
245 /**
246 * Parses the specified JSON configuration file.
247 *
248 * Returns the resulting vector of Rail objects in the output parameter.
249 *
250 * @param configFile Absolute path to the config file
251 * @param rails Rail objects within the config file
252 * @return true if file was parsed successfully, false otherwise
253 */
254 bool parseConfigFile(const std::filesystem::path& configFile,
255 std::vector<std::unique_ptr<Rail>>& rails);
256
257 /**
258 * Creates the power sequencer device object based on the device properties.
259 *
260 * Does nothing if the device properties have not been found yet.
261 *
262 * @param rails Voltage rails that are enabled and monitored by the device
263 */
264 void createDevice(std::vector<std::unique_ptr<Rail>> rails);
Jim Wright539b6082021-08-02 14:50:23 -0500265};
266
267} // namespace phosphor::power::sequencer