blob: 7426932e3760e4a7ee2e2c8b3e9df40fd2615d0b [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 Wright9d7d95c2021-11-16 11:32:23 -060020#include "utility.hpp"
Jim Wright539b6082021-08-02 14:50:23 -050021
Jim Wright22318a32021-08-27 15:56:09 -050022#include <fmt/format.h>
23
Jim Wright7a5dd992021-08-31 16:56:52 -050024#include <phosphor-logging/elog-errors.hpp>
25#include <phosphor-logging/elog.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050026#include <phosphor-logging/log.hpp>
Jim Wright7a5dd992021-08-31 16:56:52 -050027#include <xyz/openbmc_project/Common/error.hpp>
Jim Wright539b6082021-08-02 14:50:23 -050028
Jim Wright539b6082021-08-02 14:50:23 -050029#include <exception>
Jim Wright7a5dd992021-08-31 16:56:52 -050030#include <string>
Jim Wright539b6082021-08-02 14:50:23 -050031
32using namespace phosphor::logging;
33
34namespace phosphor::power::sequencer
Jim Wright10eb00f2021-07-21 12:10:38 -050035{
Jim Wright539b6082021-08-02 14:50:23 -050036
Jim Wright7a5dd992021-08-31 16:56:52 -050037const std::string powerControlLineName = "power-chassis-control";
38const std::string pgoodLineName = "power-chassis-good";
39
Jim Wright539b6082021-08-02 14:50:23 -050040PowerControl::PowerControl(sdbusplus::bus::bus& bus,
41 const sdeventplus::Event& event) :
Jim Wright22318a32021-08-27 15:56:09 -050042 PowerObject{bus, POWER_OBJ_PATH, true},
43 bus{bus}, timer{event, std::bind(&PowerControl::pollPgood, this),
44 pollInterval}
Jim Wright539b6082021-08-02 14:50:23 -050045{
46 // Obtain dbus service name
47 bus.request_name(POWER_IFACE);
Jim Wright7a5dd992021-08-31 16:56:52 -050048 setUpGpio();
Jim Wright10eb00f2021-07-21 12:10:38 -050049}
Jim Wright539b6082021-08-02 14:50:23 -050050
Jim Wright22318a32021-08-27 15:56:09 -050051int PowerControl::getPgood() const
52{
53 return pgood;
54}
55
56int PowerControl::getPgoodTimeout() const
57{
58 return timeout.count();
59}
60
61int PowerControl::getState() const
62{
63 return state;
64}
65
Jim Wright539b6082021-08-02 14:50:23 -050066void PowerControl::pollPgood()
Jim Wright7a5dd992021-08-31 16:56:52 -050067{
68 if (inStateTransition)
69 {
Jim Wright9d7d95c2021-11-16 11:32:23 -060070 // In transition between power on and off, check for timeout
Jim Wright7a5dd992021-08-31 16:56:52 -050071 const auto now = std::chrono::steady_clock::now();
72 if (now > pgoodTimeoutTime)
73 {
74 log<level::ERR>("ERROR PowerControl: Pgood poll timeout");
75 inStateTransition = false;
76 return;
77 }
78 }
79
80 int pgoodState = pgoodLine.get_value();
81 if (pgoodState != pgood)
82 {
Jim Wright9d7d95c2021-11-16 11:32:23 -060083 // Power good has changed since last read
Jim Wright7a5dd992021-08-31 16:56:52 -050084 pgood = pgoodState;
85 if (pgoodState == 0)
86 {
87 emitPowerLostSignal();
88 }
89 else
90 {
91 emitPowerGoodSignal();
92 }
93 emitPropertyChangedSignal("pgood");
94 }
95 if (pgoodState == state)
96 {
Jim Wright9d7d95c2021-11-16 11:32:23 -060097 // Power good matches requested state
Jim Wright7a5dd992021-08-31 16:56:52 -050098 inStateTransition = false;
99 }
Jim Wright9d7d95c2021-11-16 11:32:23 -0600100 else if (!inStateTransition && (pgoodState == 0))
101 {
102 // Not in power off state, not changing state, and power good is off
103 // Power good has failed, call for chassis hard power off
104 log<level::ERR>("Chassis pgood failure");
105
106 auto method =
107 bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT,
108 util::SYSTEMD_INTERFACE, "StartUnit");
109 method.append(util::POWEROFF_TARGET);
110 method.append("replace");
111 bus.call_noreply(method);
112 }
Jim Wright7a5dd992021-08-31 16:56:52 -0500113}
Jim Wright539b6082021-08-02 14:50:23 -0500114
Jim Wright22318a32021-08-27 15:56:09 -0500115void PowerControl::setPgoodTimeout(int t)
116{
117 if (timeout.count() != t)
118 {
119 timeout = std::chrono::seconds(t);
120 emitPropertyChangedSignal("pgood_timeout");
121 }
122}
123
124void PowerControl::setState(int s)
125{
126 if (state == s)
127 {
128 log<level::INFO>(
129 fmt::format("Power already at requested state: {}", state).c_str());
130 return;
131 }
Jim Wright209690b2021-11-11 14:46:42 -0600132 if (s == 0)
133 {
134 // Wait for two seconds when powering down. This is to allow host and
135 // other BMC applications time to complete power off processing
136 std::this_thread::sleep_for(std::chrono::seconds(2));
137 }
Jim Wright22318a32021-08-27 15:56:09 -0500138
139 log<level::INFO>(fmt::format("setState: {}", s).c_str());
Jim Wright7a5dd992021-08-31 16:56:52 -0500140 powerControlLine.request(
141 {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0});
142 powerControlLine.set_value(s);
143 powerControlLine.release();
144
145 pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout;
146 inStateTransition = true;
Jim Wright22318a32021-08-27 15:56:09 -0500147 state = s;
148 emitPropertyChangedSignal("state");
149}
150
Jim Wright7a5dd992021-08-31 16:56:52 -0500151void PowerControl::setUpGpio()
152{
153 pgoodLine = gpiod::find_line(pgoodLineName);
154 if (!pgoodLine)
155 {
Jim Wright209690b2021-11-11 14:46:42 -0600156 std::string errorString{"GPIO line name not found: " + pgoodLineName};
Jim Wright7a5dd992021-08-31 16:56:52 -0500157 log<level::ERR>(errorString.c_str());
158 report<
159 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
160 throw std::runtime_error(errorString);
161 }
162 powerControlLine = gpiod::find_line(powerControlLineName);
163 if (!powerControlLine)
164 {
Jim Wright209690b2021-11-11 14:46:42 -0600165 std::string errorString{"GPIO line name not found: " +
166 powerControlLineName};
Jim Wright7a5dd992021-08-31 16:56:52 -0500167 log<level::ERR>(errorString.c_str());
168 report<
169 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
170 throw std::runtime_error(errorString);
171 }
172
173 pgoodLine.request(
174 {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0});
175 int pgoodState = pgoodLine.get_value();
176 pgood = pgoodState;
177 state = pgoodState;
178 log<level::INFO>(fmt::format("Pgood state: {}", pgoodState).c_str());
179}
180
Jim Wright539b6082021-08-02 14:50:23 -0500181} // namespace phosphor::power::sequencer