Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 1 | /** |
| 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 Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 17 | #include "power_control.hpp" |
| 18 | |
| 19 | #include "types.hpp" |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 20 | #include "ucd90320_monitor.hpp" |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 21 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 22 | #include <fmt/format.h> |
| 23 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/elog-errors.hpp> |
| 25 | #include <phosphor-logging/elog.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 27 | #include <xyz/openbmc_project/Common/error.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 28 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 29 | #include <exception> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 30 | #include <string> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 31 | |
| 32 | using namespace phosphor::logging; |
| 33 | |
| 34 | namespace phosphor::power::sequencer |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 35 | { |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 36 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 37 | const std::string interfaceName = "xyz.openbmc_project.Configuration.UCD90320"; |
| 38 | const std::string addressPropertyName = "Address"; |
| 39 | const std::string busPropertyName = "Bus"; |
| 40 | const std::string namePropertyName = "Name"; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 41 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 42 | PowerControl::PowerControl(sdbusplus::bus::bus& bus, |
| 43 | const sdeventplus::Event& event) : |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 44 | PowerObject{bus, POWER_OBJ_PATH, true}, |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 45 | bus{bus}, device{std::make_unique<PowerSequencerMonitor>(bus)}, |
| 46 | match{bus, |
| 47 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 48 | sdbusplus::bus::match::rules::sender( |
| 49 | "xyz.openbmc_project.EntityManager"), |
| 50 | std::bind(&PowerControl::interfacesAddedHandler, this, |
| 51 | std::placeholders::_1)}, |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 52 | timer{event, std::bind(&PowerControl::pollPgood, this), pollInterval} |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 53 | { |
| 54 | // Obtain dbus service name |
| 55 | bus.request_name(POWER_IFACE); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 56 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 57 | setUpDevice(); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 58 | setUpGpio(); |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 59 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 60 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 61 | void PowerControl::getDeviceProperties(util::DbusPropertyMap& properties) |
| 62 | { |
| 63 | uint64_t* i2cBus = nullptr; |
| 64 | uint64_t* i2cAddress = nullptr; |
| 65 | std::string* name = nullptr; |
| 66 | |
| 67 | for (const auto& property : properties) |
| 68 | { |
| 69 | try |
| 70 | { |
| 71 | if (property.first == busPropertyName) |
| 72 | { |
| 73 | i2cBus = std::get_if<uint64_t>(&properties[busPropertyName]); |
| 74 | } |
| 75 | else if (property.first == addressPropertyName) |
| 76 | { |
| 77 | i2cAddress = |
| 78 | std::get_if<uint64_t>(&properties[addressPropertyName]); |
| 79 | } |
| 80 | else if (property.first == namePropertyName) |
| 81 | { |
| 82 | name = std::get_if<std::string>(&properties[namePropertyName]); |
| 83 | } |
| 84 | } |
| 85 | catch (std::exception& e) |
| 86 | {} |
| 87 | } |
| 88 | |
| 89 | if (i2cBus && i2cAddress && name && !name->empty()) |
| 90 | { |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 91 | log<level::DEBUG>( |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 92 | fmt::format( |
| 93 | "Found power sequencer device properties, name: {}, bus: {} addr: {:#02x} ", |
| 94 | *name, *i2cBus, *i2cAddress) |
| 95 | .c_str()); |
| 96 | // Create device object |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 97 | device = std::make_unique<UCD90320Monitor>(bus, *i2cBus, *i2cAddress); |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 98 | deviceFound = true; |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 102 | int PowerControl::getPgood() const |
| 103 | { |
| 104 | return pgood; |
| 105 | } |
| 106 | |
| 107 | int PowerControl::getPgoodTimeout() const |
| 108 | { |
| 109 | return timeout.count(); |
| 110 | } |
| 111 | |
| 112 | int PowerControl::getState() const |
| 113 | { |
| 114 | return state; |
| 115 | } |
| 116 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 117 | void PowerControl::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 118 | { |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 119 | // Only continue if message is valid and device has not already been found |
| 120 | if (!msg || deviceFound) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 121 | { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | try |
| 126 | { |
| 127 | // Read the dbus message |
| 128 | sdbusplus::message::object_path objPath; |
| 129 | std::map<std::string, std::map<std::string, util::DbusVariant>> |
| 130 | interfaces; |
| 131 | msg.read(objPath, interfaces); |
| 132 | |
| 133 | // Find the device interface, if present |
| 134 | auto itIntf = interfaces.find(interfaceName); |
| 135 | if (itIntf != interfaces.cend()) |
| 136 | { |
| 137 | log<level::INFO>( |
| 138 | fmt::format("InterfacesAdded for: {}", interfaceName).c_str()); |
| 139 | getDeviceProperties(itIntf->second); |
| 140 | } |
| 141 | } |
| 142 | catch (const std::exception&) |
| 143 | { |
| 144 | // Error trying to read interfacesAdded message. |
| 145 | } |
| 146 | } |
| 147 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 148 | void PowerControl::pollPgood() |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 149 | { |
| 150 | if (inStateTransition) |
| 151 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 152 | // In transition between power on and off, check for timeout |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 153 | const auto now = std::chrono::steady_clock::now(); |
| 154 | if (now > pgoodTimeoutTime) |
| 155 | { |
| 156 | log<level::ERR>("ERROR PowerControl: Pgood poll timeout"); |
| 157 | inStateTransition = false; |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 158 | |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 159 | if (state) |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 160 | { |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 161 | // Time out powering on |
| 162 | device->onFailure(true, powerSupplyError); |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 163 | } |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 164 | else |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 165 | { |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 166 | // Time out powering off |
| 167 | std::map<std::string, std::string> additionalData{}; |
| 168 | device->logError( |
| 169 | "xyz.openbmc_project.Power.Error.PowerOffTimeout", |
| 170 | additionalData); |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 171 | } |
| 172 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int pgoodState = pgoodLine.get_value(); |
| 178 | if (pgoodState != pgood) |
| 179 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 180 | // Power good has changed since last read |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 181 | pgood = pgoodState; |
| 182 | if (pgoodState == 0) |
| 183 | { |
| 184 | emitPowerLostSignal(); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | emitPowerGoodSignal(); |
Jim Wright | abd64b9 | 2022-02-11 09:56:56 -0600 | [diff] [blame] | 189 | // Clear any power supply error on the transition to power on |
| 190 | powerSupplyError.clear(); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 191 | } |
| 192 | emitPropertyChangedSignal("pgood"); |
| 193 | } |
| 194 | if (pgoodState == state) |
| 195 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 196 | // Power good matches requested state |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 197 | inStateTransition = false; |
| 198 | } |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 199 | else if (!inStateTransition && (pgoodState == 0)) |
| 200 | { |
| 201 | // Not in power off state, not changing state, and power good is off |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 202 | device->onFailure(false, powerSupplyError); |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 203 | // Power good has failed, call for chassis hard power off |
| 204 | log<level::ERR>("Chassis pgood failure"); |
| 205 | |
| 206 | auto method = |
| 207 | bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT, |
| 208 | util::SYSTEMD_INTERFACE, "StartUnit"); |
| 209 | method.append(util::POWEROFF_TARGET); |
| 210 | method.append("replace"); |
| 211 | bus.call_noreply(method); |
| 212 | } |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 213 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 214 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 215 | void PowerControl::setPgoodTimeout(int t) |
| 216 | { |
| 217 | if (timeout.count() != t) |
| 218 | { |
| 219 | timeout = std::chrono::seconds(t); |
| 220 | emitPropertyChangedSignal("pgood_timeout"); |
| 221 | } |
| 222 | } |
| 223 | |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 224 | void PowerControl::setPowerSupplyError(const std::string& error) |
| 225 | { |
| 226 | powerSupplyError = error; |
| 227 | } |
| 228 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 229 | void PowerControl::setState(int s) |
| 230 | { |
| 231 | if (state == s) |
| 232 | { |
| 233 | log<level::INFO>( |
| 234 | fmt::format("Power already at requested state: {}", state).c_str()); |
| 235 | return; |
| 236 | } |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 237 | if (s == 0) |
| 238 | { |
| 239 | // Wait for two seconds when powering down. This is to allow host and |
| 240 | // other BMC applications time to complete power off processing |
| 241 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 242 | } |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 243 | |
| 244 | log<level::INFO>(fmt::format("setState: {}", s).c_str()); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 245 | powerControlLine.request( |
| 246 | {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0}); |
| 247 | powerControlLine.set_value(s); |
| 248 | powerControlLine.release(); |
| 249 | |
| 250 | pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout; |
| 251 | inStateTransition = true; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 252 | state = s; |
| 253 | emitPropertyChangedSignal("state"); |
| 254 | } |
| 255 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 256 | void PowerControl::setUpDevice() |
| 257 | { |
| 258 | try |
| 259 | { |
| 260 | auto objects = util::getSubTree(bus, "/", interfaceName, 0); |
| 261 | |
| 262 | // Search for matching interface in returned objects |
| 263 | for (const auto& [path, services] : objects) |
| 264 | { |
| 265 | auto service = services.begin()->first; |
| 266 | |
| 267 | if (path.empty() || service.empty()) |
| 268 | { |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | // Get the properties for the device interface |
| 273 | auto properties = |
| 274 | util::getAllProperties(bus, path, interfaceName, service); |
| 275 | |
| 276 | getDeviceProperties(properties); |
| 277 | } |
| 278 | } |
| 279 | catch (const std::exception& e) |
| 280 | { |
| 281 | // Interface or property not found. Let the Interfaces Added callback |
| 282 | // process the information once the interfaces are added to D-Bus. |
| 283 | } |
| 284 | } |
| 285 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 286 | void PowerControl::setUpGpio() |
| 287 | { |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 288 | const std::string powerControlLineName = "power-chassis-control"; |
| 289 | const std::string pgoodLineName = "power-chassis-good"; |
| 290 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 291 | pgoodLine = gpiod::find_line(pgoodLineName); |
| 292 | if (!pgoodLine) |
| 293 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 294 | std::string errorString{"GPIO line name not found: " + pgoodLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 295 | log<level::ERR>(errorString.c_str()); |
| 296 | report< |
| 297 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 298 | throw std::runtime_error(errorString); |
| 299 | } |
| 300 | powerControlLine = gpiod::find_line(powerControlLineName); |
| 301 | if (!powerControlLine) |
| 302 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 303 | std::string errorString{"GPIO line name not found: " + |
| 304 | powerControlLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 305 | log<level::ERR>(errorString.c_str()); |
| 306 | report< |
| 307 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 308 | throw std::runtime_error(errorString); |
| 309 | } |
| 310 | |
| 311 | pgoodLine.request( |
| 312 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 313 | int pgoodState = pgoodLine.get_value(); |
| 314 | pgood = pgoodState; |
| 315 | state = pgoodState; |
| 316 | log<level::INFO>(fmt::format("Pgood state: {}", pgoodState).c_str()); |
| 317 | } |
| 318 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 319 | } // namespace phosphor::power::sequencer |