blob: 78a1d740aaed3868684416847b7ff3bdf8c7441c [file] [log] [blame]
Jim Wright10eb00f2021-07-21 12:10:38 -05001/**
2 * Copyright © 2021 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jim Wright539b6082021-08-02 14:50:23 -050017#include "power_control.hpp"
18
19#include "types.hpp"
Jim Wright7945dd22021-04-06 16:55:15 -050020#include "ucd90320_monitor.hpp"
Jim Wright539b6082021-08-02 14:50:23 -050021
Jim Wrightb4ad95d2022-03-08 17:24:01 -060022#include <fmt/chrono.h>
Jim Wright22318a32021-08-27 15:56:09 -050023#include <fmt/format.h>
24
Jim Wright7a5dd992021-08-31 16:56:52 -050025#include <phosphor-logging/elog-errors.hpp>
26#include <phosphor-logging/elog.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050027#include <phosphor-logging/log.hpp>
Jim Wright7a5dd992021-08-31 16:56:52 -050028#include <xyz/openbmc_project/Common/error.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050029
Jim Wright539b6082021-08-02 14:50:23 -050030#include <exception>
Jim Wright7a5dd992021-08-31 16:56:52 -050031#include <string>
Jim Wright539b6082021-08-02 14:50:23 -050032
33using namespace phosphor::logging;
34
35namespace phosphor::power::sequencer
Jim Wright10eb00f2021-07-21 12:10:38 -050036{
Jim Wright539b6082021-08-02 14:50:23 -050037
Jim Wright2d99bf72021-11-19 11:18:12 -060038const std::string interfaceName = "xyz.openbmc_project.Configuration.UCD90320";
39const std::string addressPropertyName = "Address";
40const std::string busPropertyName = "Bus";
41const std::string namePropertyName = "Name";
Jim Wright7a5dd992021-08-31 16:56:52 -050042
Jim Wright539b6082021-08-02 14:50:23 -050043PowerControl::PowerControl(sdbusplus::bus::bus& bus,
44 const sdeventplus::Event& event) :
Patrick Williams3fa31a72022-04-05 21:22:58 -050045 PowerObject{bus, POWER_OBJ_PATH, PowerObject::action::defer_emit},
Jim Wright930458c2022-01-24 14:37:27 -060046 bus{bus}, device{std::make_unique<PowerSequencerMonitor>(bus)},
47 match{bus,
48 sdbusplus::bus::match::rules::interfacesAdded() +
49 sdbusplus::bus::match::rules::sender(
50 "xyz.openbmc_project.EntityManager"),
51 std::bind(&PowerControl::interfacesAddedHandler, this,
52 std::placeholders::_1)},
Jim Wrightb4ad95d2022-03-08 17:24:01 -060053 powerOnAllowedTime{std::chrono::steady_clock::now() + minimumColdStartTime},
Jim Wrightccea2d22021-12-10 14:10:46 -060054 timer{event, std::bind(&PowerControl::pollPgood, this), pollInterval}
Jim Wright539b6082021-08-02 14:50:23 -050055{
56 // Obtain dbus service name
57 bus.request_name(POWER_IFACE);
Jim Wright2d99bf72021-11-19 11:18:12 -060058
Jim Wright2d99bf72021-11-19 11:18:12 -060059 setUpDevice();
Jim Wright7a5dd992021-08-31 16:56:52 -050060 setUpGpio();
Jim Wright10eb00f2021-07-21 12:10:38 -050061}
Jim Wright539b6082021-08-02 14:50:23 -050062
Jim Wright2d99bf72021-11-19 11:18:12 -060063void PowerControl::getDeviceProperties(util::DbusPropertyMap& properties)
64{
65 uint64_t* i2cBus = nullptr;
66 uint64_t* i2cAddress = nullptr;
67 std::string* name = nullptr;
68
69 for (const auto& property : properties)
70 {
71 try
72 {
73 if (property.first == busPropertyName)
74 {
75 i2cBus = std::get_if<uint64_t>(&properties[busPropertyName]);
76 }
77 else if (property.first == addressPropertyName)
78 {
79 i2cAddress =
80 std::get_if<uint64_t>(&properties[addressPropertyName]);
81 }
82 else if (property.first == namePropertyName)
83 {
84 name = std::get_if<std::string>(&properties[namePropertyName]);
85 }
86 }
87 catch (std::exception& e)
88 {}
89 }
90
91 if (i2cBus && i2cAddress && name && !name->empty())
92 {
Jim Wright7945dd22021-04-06 16:55:15 -050093 log<level::DEBUG>(
Jim Wright2d99bf72021-11-19 11:18:12 -060094 fmt::format(
95 "Found power sequencer device properties, name: {}, bus: {} addr: {:#02x} ",
96 *name, *i2cBus, *i2cAddress)
97 .c_str());
98 // Create device object
Jim Wright7945dd22021-04-06 16:55:15 -050099 device = std::make_unique<UCD90320Monitor>(bus, *i2cBus, *i2cAddress);
Jim Wrightccea2d22021-12-10 14:10:46 -0600100 deviceFound = true;
Jim Wright2d99bf72021-11-19 11:18:12 -0600101 }
102}
103
Jim Wright22318a32021-08-27 15:56:09 -0500104int PowerControl::getPgood() const
105{
106 return pgood;
107}
108
109int PowerControl::getPgoodTimeout() const
110{
111 return timeout.count();
112}
113
114int PowerControl::getState() const
115{
116 return state;
117}
118
Jim Wright2d99bf72021-11-19 11:18:12 -0600119void PowerControl::interfacesAddedHandler(sdbusplus::message::message& msg)
120{
Jim Wrightccea2d22021-12-10 14:10:46 -0600121 // Only continue if message is valid and device has not already been found
122 if (!msg || deviceFound)
Jim Wright2d99bf72021-11-19 11:18:12 -0600123 {
124 return;
125 }
126
127 try
128 {
129 // Read the dbus message
130 sdbusplus::message::object_path objPath;
131 std::map<std::string, std::map<std::string, util::DbusVariant>>
132 interfaces;
133 msg.read(objPath, interfaces);
134
135 // Find the device interface, if present
136 auto itIntf = interfaces.find(interfaceName);
137 if (itIntf != interfaces.cend())
138 {
139 log<level::INFO>(
140 fmt::format("InterfacesAdded for: {}", interfaceName).c_str());
141 getDeviceProperties(itIntf->second);
142 }
143 }
144 catch (const std::exception&)
145 {
146 // Error trying to read interfacesAdded message.
147 }
148}
149
Jim Wright539b6082021-08-02 14:50:23 -0500150void PowerControl::pollPgood()
Jim Wright7a5dd992021-08-31 16:56:52 -0500151{
152 if (inStateTransition)
153 {
Jim Wright9d7d95c2021-11-16 11:32:23 -0600154 // In transition between power on and off, check for timeout
Jim Wright7a5dd992021-08-31 16:56:52 -0500155 const auto now = std::chrono::steady_clock::now();
156 if (now > pgoodTimeoutTime)
157 {
158 log<level::ERR>("ERROR PowerControl: Pgood poll timeout");
159 inStateTransition = false;
Jim Wright4e25df52021-11-17 09:38:00 -0600160
Jim Wright930458c2022-01-24 14:37:27 -0600161 if (state)
Jim Wright4e25df52021-11-17 09:38:00 -0600162 {
Jim Wright930458c2022-01-24 14:37:27 -0600163 // Time out powering on
164 device->onFailure(true, powerSupplyError);
Jim Wright4e25df52021-11-17 09:38:00 -0600165 }
Jim Wright930458c2022-01-24 14:37:27 -0600166 else
Jim Wright4e25df52021-11-17 09:38:00 -0600167 {
Jim Wright930458c2022-01-24 14:37:27 -0600168 // Time out powering off
169 std::map<std::string, std::string> additionalData{};
170 device->logError(
171 "xyz.openbmc_project.Power.Error.PowerOffTimeout",
172 additionalData);
Jim Wright4e25df52021-11-17 09:38:00 -0600173 }
174
Jim Wright48752622022-02-28 20:37:53 -0600175 failureFound = true;
Jim Wright7a5dd992021-08-31 16:56:52 -0500176 return;
177 }
178 }
179
180 int pgoodState = pgoodLine.get_value();
181 if (pgoodState != pgood)
182 {
Jim Wright9d7d95c2021-11-16 11:32:23 -0600183 // Power good has changed since last read
Jim Wright7a5dd992021-08-31 16:56:52 -0500184 pgood = pgoodState;
185 if (pgoodState == 0)
186 {
187 emitPowerLostSignal();
188 }
189 else
190 {
191 emitPowerGoodSignal();
Jim Wright48752622022-02-28 20:37:53 -0600192 // Clear any errors on the transition to power on
Jim Wrightabd64b92022-02-11 09:56:56 -0600193 powerSupplyError.clear();
Jim Wright48752622022-02-28 20:37:53 -0600194 failureFound = false;
Jim Wright7a5dd992021-08-31 16:56:52 -0500195 }
196 emitPropertyChangedSignal("pgood");
197 }
198 if (pgoodState == state)
199 {
Jim Wright9d7d95c2021-11-16 11:32:23 -0600200 // Power good matches requested state
Jim Wright7a5dd992021-08-31 16:56:52 -0500201 inStateTransition = false;
202 }
Jim Wright48752622022-02-28 20:37:53 -0600203 else if (!inStateTransition && (pgoodState == 0) && !failureFound)
Jim Wright9d7d95c2021-11-16 11:32:23 -0600204 {
205 // Not in power off state, not changing state, and power good is off
Jim Wright930458c2022-01-24 14:37:27 -0600206 device->onFailure(false, powerSupplyError);
Jim Wright48752622022-02-28 20:37:53 -0600207 failureFound = true;
Jim Wright9d7d95c2021-11-16 11:32:23 -0600208 // Power good has failed, call for chassis hard power off
209 log<level::ERR>("Chassis pgood failure");
210
211 auto method =
212 bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT,
213 util::SYSTEMD_INTERFACE, "StartUnit");
214 method.append(util::POWEROFF_TARGET);
215 method.append("replace");
216 bus.call_noreply(method);
217 }
Jim Wright7a5dd992021-08-31 16:56:52 -0500218}
Jim Wright539b6082021-08-02 14:50:23 -0500219
Jim Wright22318a32021-08-27 15:56:09 -0500220void PowerControl::setPgoodTimeout(int t)
221{
222 if (timeout.count() != t)
223 {
224 timeout = std::chrono::seconds(t);
225 emitPropertyChangedSignal("pgood_timeout");
226 }
227}
228
Jim Wrightccea2d22021-12-10 14:10:46 -0600229void PowerControl::setPowerSupplyError(const std::string& error)
230{
231 powerSupplyError = error;
232}
233
Jim Wright22318a32021-08-27 15:56:09 -0500234void PowerControl::setState(int s)
235{
236 if (state == s)
237 {
238 log<level::INFO>(
239 fmt::format("Power already at requested state: {}", state).c_str());
240 return;
241 }
Jim Wright209690b2021-11-11 14:46:42 -0600242 if (s == 0)
243 {
244 // Wait for two seconds when powering down. This is to allow host and
245 // other BMC applications time to complete power off processing
246 std::this_thread::sleep_for(std::chrono::seconds(2));
247 }
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600248 else
249 {
250 // If minimum power off time has not passed, wait
251 if (powerOnAllowedTime > std::chrono::steady_clock::now())
252 {
253 log<level::INFO>(
254 fmt::format(
255 "Waiting {} seconds until power on allowed",
256 std::chrono::duration_cast<std::chrono::seconds>(
257 powerOnAllowedTime - std::chrono::steady_clock::now())
258 .count())
259 .c_str());
260 }
261 std::this_thread::sleep_until(powerOnAllowedTime);
262 }
Jim Wright22318a32021-08-27 15:56:09 -0500263
264 log<level::INFO>(fmt::format("setState: {}", s).c_str());
Jim Wright7a5dd992021-08-31 16:56:52 -0500265 powerControlLine.request(
266 {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0});
267 powerControlLine.set_value(s);
268 powerControlLine.release();
269
Jim Wrightb4ad95d2022-03-08 17:24:01 -0600270 if (s == 0)
271 {
272 // Set a minimum amount of time to wait before next power on
273 powerOnAllowedTime =
274 std::chrono::steady_clock::now() + minimumPowerOffTime;
275 }
276
Jim Wright7a5dd992021-08-31 16:56:52 -0500277 pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout;
278 inStateTransition = true;
Jim Wright22318a32021-08-27 15:56:09 -0500279 state = s;
280 emitPropertyChangedSignal("state");
281}
282
Jim Wright2d99bf72021-11-19 11:18:12 -0600283void PowerControl::setUpDevice()
284{
285 try
286 {
287 auto objects = util::getSubTree(bus, "/", interfaceName, 0);
288
289 // Search for matching interface in returned objects
290 for (const auto& [path, services] : objects)
291 {
292 auto service = services.begin()->first;
293
294 if (path.empty() || service.empty())
295 {
296 continue;
297 }
298
299 // Get the properties for the device interface
300 auto properties =
301 util::getAllProperties(bus, path, interfaceName, service);
302
303 getDeviceProperties(properties);
304 }
305 }
306 catch (const std::exception& e)
307 {
308 // Interface or property not found. Let the Interfaces Added callback
309 // process the information once the interfaces are added to D-Bus.
310 }
311}
312
Jim Wright7a5dd992021-08-31 16:56:52 -0500313void PowerControl::setUpGpio()
314{
Jim Wright2d99bf72021-11-19 11:18:12 -0600315 const std::string powerControlLineName = "power-chassis-control";
316 const std::string pgoodLineName = "power-chassis-good";
317
Jim Wright7a5dd992021-08-31 16:56:52 -0500318 pgoodLine = gpiod::find_line(pgoodLineName);
319 if (!pgoodLine)
320 {
Jim Wright209690b2021-11-11 14:46:42 -0600321 std::string errorString{"GPIO line name not found: " + pgoodLineName};
Jim Wright7a5dd992021-08-31 16:56:52 -0500322 log<level::ERR>(errorString.c_str());
323 report<
324 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
325 throw std::runtime_error(errorString);
326 }
327 powerControlLine = gpiod::find_line(powerControlLineName);
328 if (!powerControlLine)
329 {
Jim Wright209690b2021-11-11 14:46:42 -0600330 std::string errorString{"GPIO line name not found: " +
331 powerControlLineName};
Jim Wright7a5dd992021-08-31 16:56:52 -0500332 log<level::ERR>(errorString.c_str());
333 report<
334 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
335 throw std::runtime_error(errorString);
336 }
337
338 pgoodLine.request(
339 {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0});
340 int pgoodState = pgoodLine.get_value();
341 pgood = pgoodState;
342 state = pgoodState;
343 log<level::INFO>(fmt::format("Pgood state: {}", pgoodState).c_str());
344}
345
Jim Wright539b6082021-08-02 14:50:23 -0500346} // namespace phosphor::power::sequencer