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 | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame^] | 20 | #include "utility.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 | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 37 | const std::string powerControlLineName = "power-chassis-control"; |
| 38 | const std::string pgoodLineName = "power-chassis-good"; |
| 39 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 40 | PowerControl::PowerControl(sdbusplus::bus::bus& bus, |
| 41 | const sdeventplus::Event& event) : |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 42 | PowerObject{bus, POWER_OBJ_PATH, true}, |
| 43 | bus{bus}, timer{event, std::bind(&PowerControl::pollPgood, this), |
| 44 | pollInterval} |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 45 | { |
| 46 | // Obtain dbus service name |
| 47 | bus.request_name(POWER_IFACE); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 48 | setUpGpio(); |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 49 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 50 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 51 | int PowerControl::getPgood() const |
| 52 | { |
| 53 | return pgood; |
| 54 | } |
| 55 | |
| 56 | int PowerControl::getPgoodTimeout() const |
| 57 | { |
| 58 | return timeout.count(); |
| 59 | } |
| 60 | |
| 61 | int PowerControl::getState() const |
| 62 | { |
| 63 | return state; |
| 64 | } |
| 65 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 66 | void PowerControl::pollPgood() |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 67 | { |
| 68 | if (inStateTransition) |
| 69 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame^] | 70 | // In transition between power on and off, check for timeout |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 71 | 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 Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame^] | 83 | // Power good has changed since last read |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 84 | 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 Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame^] | 97 | // Power good matches requested state |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 98 | inStateTransition = false; |
| 99 | } |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame^] | 100 | 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 Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 113 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 114 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 115 | void PowerControl::setPgoodTimeout(int t) |
| 116 | { |
| 117 | if (timeout.count() != t) |
| 118 | { |
| 119 | timeout = std::chrono::seconds(t); |
| 120 | emitPropertyChangedSignal("pgood_timeout"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void 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 Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 132 | 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 Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 138 | |
| 139 | log<level::INFO>(fmt::format("setState: {}", s).c_str()); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 140 | 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 Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 147 | state = s; |
| 148 | emitPropertyChangedSignal("state"); |
| 149 | } |
| 150 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 151 | void PowerControl::setUpGpio() |
| 152 | { |
| 153 | pgoodLine = gpiod::find_line(pgoodLineName); |
| 154 | if (!pgoodLine) |
| 155 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 156 | std::string errorString{"GPIO line name not found: " + pgoodLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 157 | 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 Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 165 | std::string errorString{"GPIO line name not found: " + |
| 166 | powerControlLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 167 | 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 Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 181 | } // namespace phosphor::power::sequencer |