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 | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 22 | #include <fmt/chrono.h> |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 23 | #include <fmt/format.h> |
| 24 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 25 | #include <phosphor-logging/elog-errors.hpp> |
| 26 | #include <phosphor-logging/elog.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 27 | #include <phosphor-logging/log.hpp> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 28 | #include <xyz/openbmc_project/Common/error.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 29 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 30 | #include <exception> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 31 | #include <string> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 32 | |
| 33 | using namespace phosphor::logging; |
| 34 | |
| 35 | namespace phosphor::power::sequencer |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 36 | { |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 37 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 38 | const std::string interfaceName = "xyz.openbmc_project.Configuration.UCD90320"; |
| 39 | const std::string addressPropertyName = "Address"; |
| 40 | const std::string busPropertyName = "Bus"; |
| 41 | const std::string namePropertyName = "Name"; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 42 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 43 | PowerControl::PowerControl(sdbusplus::bus::bus& bus, |
| 44 | const sdeventplus::Event& event) : |
Patrick Williams | 3fa31a7 | 2022-04-05 21:22:58 -0500 | [diff] [blame] | 45 | PowerObject{bus, POWER_OBJ_PATH, PowerObject::action::defer_emit}, |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 46 | 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 Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 53 | powerOnAllowedTime{std::chrono::steady_clock::now() + minimumColdStartTime}, |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 54 | timer{event, std::bind(&PowerControl::pollPgood, this), pollInterval} |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 55 | { |
| 56 | // Obtain dbus service name |
| 57 | bus.request_name(POWER_IFACE); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 58 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 59 | setUpDevice(); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 60 | setUpGpio(); |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 61 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 62 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 63 | void 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 | } |
Jim Wright | c91eed0 | 2022-07-22 11:27:06 -0500 | [diff] [blame] | 87 | catch (const std::exception&) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 88 | {} |
| 89 | } |
| 90 | |
| 91 | if (i2cBus && i2cAddress && name && !name->empty()) |
| 92 | { |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 93 | log<level::DEBUG>( |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 94 | fmt::format( |
| 95 | "Found power sequencer device properties, name: {}, bus: {} addr: {:#02x} ", |
| 96 | *name, *i2cBus, *i2cAddress) |
| 97 | .c_str()); |
| 98 | // Create device object |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 99 | device = std::make_unique<UCD90320Monitor>(bus, *i2cBus, *i2cAddress); |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 100 | deviceFound = true; |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 104 | int PowerControl::getPgood() const |
| 105 | { |
| 106 | return pgood; |
| 107 | } |
| 108 | |
| 109 | int PowerControl::getPgoodTimeout() const |
| 110 | { |
| 111 | return timeout.count(); |
| 112 | } |
| 113 | |
| 114 | int PowerControl::getState() const |
| 115 | { |
| 116 | return state; |
| 117 | } |
| 118 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 119 | void PowerControl::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 120 | { |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 121 | // Only continue if message is valid and device has not already been found |
| 122 | if (!msg || deviceFound) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 123 | { |
| 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 Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 150 | void PowerControl::pollPgood() |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 151 | { |
| 152 | if (inStateTransition) |
| 153 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 154 | // In transition between power on and off, check for timeout |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 155 | const auto now = std::chrono::steady_clock::now(); |
| 156 | if (now > pgoodTimeoutTime) |
| 157 | { |
Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 158 | log<level::ERR>( |
| 159 | fmt::format("Power state transition timeout, state: {}", state) |
| 160 | .c_str()); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 161 | inStateTransition = false; |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 162 | |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 163 | if (state) |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 164 | { |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 165 | // Time out powering on |
| 166 | device->onFailure(true, powerSupplyError); |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 167 | } |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 168 | else |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 169 | { |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 170 | // Time out powering off |
| 171 | std::map<std::string, std::string> additionalData{}; |
| 172 | device->logError( |
| 173 | "xyz.openbmc_project.Power.Error.PowerOffTimeout", |
| 174 | additionalData); |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 175 | } |
| 176 | |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 177 | failureFound = true; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | int pgoodState = pgoodLine.get_value(); |
| 183 | if (pgoodState != pgood) |
| 184 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 185 | // Power good has changed since last read |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 186 | pgood = pgoodState; |
| 187 | if (pgoodState == 0) |
| 188 | { |
| 189 | emitPowerLostSignal(); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | emitPowerGoodSignal(); |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 194 | // Clear any errors on the transition to power on |
Jim Wright | abd64b9 | 2022-02-11 09:56:56 -0600 | [diff] [blame] | 195 | powerSupplyError.clear(); |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 196 | failureFound = false; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 197 | } |
| 198 | emitPropertyChangedSignal("pgood"); |
| 199 | } |
| 200 | if (pgoodState == state) |
| 201 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 202 | // Power good matches requested state |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 203 | inStateTransition = false; |
| 204 | } |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 205 | else if (!inStateTransition && (pgoodState == 0) && !failureFound) |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 206 | { |
| 207 | // Not in power off state, not changing state, and power good is off |
Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 208 | log<level::ERR>("Chassis pgood failure"); |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 209 | device->onFailure(false, powerSupplyError); |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 210 | failureFound = true; |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 211 | |
Jim Wright | 213ffe9 | 2022-06-03 08:54:30 -0500 | [diff] [blame] | 212 | // Power good has failed, call for chassis hard power off |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 213 | auto method = |
| 214 | bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT, |
| 215 | util::SYSTEMD_INTERFACE, "StartUnit"); |
| 216 | method.append(util::POWEROFF_TARGET); |
| 217 | method.append("replace"); |
| 218 | bus.call_noreply(method); |
| 219 | } |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 220 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 221 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 222 | void PowerControl::setPgoodTimeout(int t) |
| 223 | { |
| 224 | if (timeout.count() != t) |
| 225 | { |
| 226 | timeout = std::chrono::seconds(t); |
| 227 | emitPropertyChangedSignal("pgood_timeout"); |
| 228 | } |
| 229 | } |
| 230 | |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 231 | void PowerControl::setPowerSupplyError(const std::string& error) |
| 232 | { |
| 233 | powerSupplyError = error; |
| 234 | } |
| 235 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 236 | void PowerControl::setState(int s) |
| 237 | { |
| 238 | if (state == s) |
| 239 | { |
| 240 | log<level::INFO>( |
| 241 | fmt::format("Power already at requested state: {}", state).c_str()); |
| 242 | return; |
| 243 | } |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 244 | if (s == 0) |
| 245 | { |
| 246 | // Wait for two seconds when powering down. This is to allow host and |
| 247 | // other BMC applications time to complete power off processing |
| 248 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 249 | } |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 250 | else |
| 251 | { |
| 252 | // If minimum power off time has not passed, wait |
| 253 | if (powerOnAllowedTime > std::chrono::steady_clock::now()) |
| 254 | { |
| 255 | log<level::INFO>( |
| 256 | fmt::format( |
| 257 | "Waiting {} seconds until power on allowed", |
| 258 | std::chrono::duration_cast<std::chrono::seconds>( |
| 259 | powerOnAllowedTime - std::chrono::steady_clock::now()) |
| 260 | .count()) |
| 261 | .c_str()); |
| 262 | } |
| 263 | std::this_thread::sleep_until(powerOnAllowedTime); |
| 264 | } |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 265 | |
| 266 | log<level::INFO>(fmt::format("setState: {}", s).c_str()); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 267 | powerControlLine.request( |
| 268 | {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0}); |
| 269 | powerControlLine.set_value(s); |
| 270 | powerControlLine.release(); |
| 271 | |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 272 | if (s == 0) |
| 273 | { |
| 274 | // Set a minimum amount of time to wait before next power on |
| 275 | powerOnAllowedTime = |
| 276 | std::chrono::steady_clock::now() + minimumPowerOffTime; |
| 277 | } |
| 278 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 279 | pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout; |
| 280 | inStateTransition = true; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 281 | state = s; |
| 282 | emitPropertyChangedSignal("state"); |
| 283 | } |
| 284 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 285 | void PowerControl::setUpDevice() |
| 286 | { |
| 287 | try |
| 288 | { |
| 289 | auto objects = util::getSubTree(bus, "/", interfaceName, 0); |
| 290 | |
| 291 | // Search for matching interface in returned objects |
| 292 | for (const auto& [path, services] : objects) |
| 293 | { |
| 294 | auto service = services.begin()->first; |
| 295 | |
| 296 | if (path.empty() || service.empty()) |
| 297 | { |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | // Get the properties for the device interface |
| 302 | auto properties = |
| 303 | util::getAllProperties(bus, path, interfaceName, service); |
| 304 | |
| 305 | getDeviceProperties(properties); |
| 306 | } |
| 307 | } |
Jim Wright | c91eed0 | 2022-07-22 11:27:06 -0500 | [diff] [blame] | 308 | catch (const std::exception&) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 309 | { |
| 310 | // Interface or property not found. Let the Interfaces Added callback |
| 311 | // process the information once the interfaces are added to D-Bus. |
| 312 | } |
| 313 | } |
| 314 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 315 | void PowerControl::setUpGpio() |
| 316 | { |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 317 | const std::string powerControlLineName = "power-chassis-control"; |
| 318 | const std::string pgoodLineName = "power-chassis-good"; |
| 319 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 320 | pgoodLine = gpiod::find_line(pgoodLineName); |
| 321 | if (!pgoodLine) |
| 322 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 323 | std::string errorString{"GPIO line name not found: " + pgoodLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 324 | log<level::ERR>(errorString.c_str()); |
| 325 | report< |
| 326 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 327 | throw std::runtime_error(errorString); |
| 328 | } |
| 329 | powerControlLine = gpiod::find_line(powerControlLineName); |
| 330 | if (!powerControlLine) |
| 331 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 332 | std::string errorString{"GPIO line name not found: " + |
| 333 | powerControlLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 334 | log<level::ERR>(errorString.c_str()); |
| 335 | report< |
| 336 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 337 | throw std::runtime_error(errorString); |
| 338 | } |
| 339 | |
| 340 | pgoodLine.request( |
| 341 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 342 | int pgoodState = pgoodLine.get_value(); |
| 343 | pgood = pgoodState; |
| 344 | state = pgoodState; |
| 345 | log<level::INFO>(fmt::format("Pgood state: {}", pgoodState).c_str()); |
| 346 | } |
| 347 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 348 | } // namespace phosphor::power::sequencer |