blob: 2f0ed3923006dfd9682140f72e45fb731b03772b [file] [log] [blame]
Jim Wright539b6082021-08-02 14:50:23 -05001#pragma once
2
Andrew Geissler21254392025-04-02 10:25:39 -05003#include "config.h"
4
Shawn McCarney1f8b1102024-06-21 18:57:46 -05005#include "compatible_system_types_finder.hpp"
Jim Wright22318a32021-08-27 15:56:09 -05006#include "power_interface.hpp"
Shawn McCarney1f8b1102024-06-21 18:57:46 -05007#include "services.hpp"
Shawn McCarney5a8aefe2025-11-13 14:38:53 -06008#include "system.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>
Shawn McCarney5a8aefe2025-11-13 14:38:53 -060019#include <map>
Shawn McCarney1f8b1102024-06-21 18:57:46 -050020#include <memory>
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
Jim Wright539b6082021-08-02 14:50:23 -050077 private:
78 /**
79 * The D-Bus bus object
80 */
Patrick Williams7354ce62022-07-22 19:26:56 -050081 sdbusplus::bus_t& bus;
Jim Wright539b6082021-08-02 14:50:23 -050082
83 /**
Shawn McCarney1f8b1102024-06-21 18:57:46 -050084 * System services like hardware presence and the journal.
Jim Wright7945dd22021-04-06 16:55:15 -050085 */
Shawn McCarney1f8b1102024-06-21 18:57:46 -050086 BMCServices services;
Jim Wrightccea2d22021-12-10 14:10:46 -060087
88 /**
Shawn McCarney1f8b1102024-06-21 18:57:46 -050089 * Object that finds the compatible system types for the current system.
Jim Wrightccea2d22021-12-10 14:10:46 -060090 */
Shawn McCarney1f8b1102024-06-21 18:57:46 -050091 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 McCarney5a8aefe2025-11-13 14:38:53 -0600100 * 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 McCarney1f8b1102024-06-21 18:57:46 -0500104 */
Shawn McCarney5a8aefe2025-11-13 14:38:53 -0600105 std::unique_ptr<System> system;
Jim Wright7945dd22021-04-06 16:55:15 -0500106
107 /**
Jim Wright48752622022-02-28 20:37:53 -0600108 * 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 Wright7a5dd992021-08-31 16:56:52 -0500114 */
115 bool inStateTransition{false};
116
117 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600118 * 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 Wright22318a32021-08-27 15:56:09 -0500128 * Power good
Jim Wright539b6082021-08-02 14:50:23 -0500129 */
Jim Wright22318a32021-08-27 15:56:09 -0500130 int pgood{0};
131
132 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500133 * GPIO line object for chassis power good
134 */
135 gpiod::line pgoodLine;
136
137 /**
Jim Wright22318a32021-08-27 15:56:09 -0500138 * Power good timeout constant
139 */
Andrew Geissler21254392025-04-02 10:25:39 -0500140 static constexpr std::chrono::seconds pgoodTimeout{PGOOD_TIMEOUT};
Jim Wright22318a32021-08-27 15:56:09 -0500141
142 /**
Jim Wright7a5dd992021-08-31 16:56:52 -0500143 * 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 Wright1b639532022-11-19 17:41:33 -0600148 * 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 Wright22318a32021-08-27 15:56:09 -0500154 * Poll interval constant
155 */
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600156 static constexpr std::chrono::milliseconds pollInterval{3000};
Jim Wright22318a32021-08-27 15:56:09 -0500157
158 /**
Jim Wright213ffe92022-06-03 08:54:30 -0500159 * GPIO line object for power on / power off control
Jim Wright7a5dd992021-08-31 16:56:52 -0500160 */
161 gpiod::line powerControlLine;
162
163 /**
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600164 * 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 Wright48752622022-02-28 20:37:53 -0600169 * Power supply error. Cleared at power on.
Jim Wrightccea2d22021-12-10 14:10:46 -0600170 */
171 std::string powerSupplyError;
172
173 /**
Jim Wright22318a32021-08-27 15:56:09 -0500174 * Power state
175 */
176 int state{0};
177
178 /**
179 * Power good timeout
180 */
181 std::chrono::seconds timeout{pgoodTimeout};
Jim Wright539b6082021-08-02 14:50:23 -0500182
183 /**
184 * Timer to poll the pgood
185 */
186 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
187
188 /**
Jim Wright1b639532022-11-19 17:41:33 -0600189 * Callback to begin failure processing after observing pgood failure wait
190 */
191 void onFailureCallback();
192
193 /**
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500194 * Begin pgood failure processing
195 *
196 * @param wasTimeOut Indicates whether failure state was determined by
197 * timing out
Jim Wright1b639532022-11-19 17:41:33 -0600198 */
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500199 void onFailure(bool wasTimeOut);
Jim Wright1b639532022-11-19 17:41:33 -0600200
201 /**
Shawn McCarney5a8aefe2025-11-13 14:38:53 -0600202 * 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 Wright539b6082021-08-02 14:50:23 -0500218 * Polling method for monitoring the system power good
219 */
220 void pollPgood();
Jim Wright7a5dd992021-08-31 16:56:52 -0500221
222 /**
223 * Set up GPIOs
224 */
225 void setUpGpio();
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500226
227 /**
Shawn McCarney5a8aefe2025-11-13 14:38:53 -0600228 * Loads the JSON configuration file.
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500229 *
Shawn McCarney5a8aefe2025-11-13 14:38:53 -0600230 * 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 McCarney1f8b1102024-06-21 18:57:46 -0500234 */
Shawn McCarney5a8aefe2025-11-13 14:38:53 -0600235 void loadConfigFile();
Shawn McCarney1f8b1102024-06-21 18:57:46 -0500236
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 Wright539b6082021-08-02 14:50:23 -0500246};
247
248} // namespace phosphor::power::sequencer