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> |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 25 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/elog-errors.hpp> |
| 27 | #include <phosphor-logging/elog.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 28 | #include <phosphor-logging/log.hpp> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 29 | #include <xyz/openbmc_project/Common/error.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 30 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 31 | #include <exception> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 32 | #include <string> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 33 | |
| 34 | using namespace phosphor::logging; |
| 35 | |
| 36 | namespace phosphor::power::sequencer |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 37 | { |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 38 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 39 | const std::string powerControlLineName = "power-chassis-control"; |
| 40 | const std::string pgoodLineName = "power-chassis-good"; |
| 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}, |
| 45 | bus{bus}, timer{event, std::bind(&PowerControl::pollPgood, this), |
| 46 | pollInterval} |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 47 | { |
| 48 | // Obtain dbus service name |
| 49 | bus.request_name(POWER_IFACE); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 50 | setUpGpio(); |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 51 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 52 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 53 | int PowerControl::getPgood() const |
| 54 | { |
| 55 | return pgood; |
| 56 | } |
| 57 | |
| 58 | int PowerControl::getPgoodTimeout() const |
| 59 | { |
| 60 | return timeout.count(); |
| 61 | } |
| 62 | |
| 63 | int PowerControl::getState() const |
| 64 | { |
| 65 | return state; |
| 66 | } |
| 67 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 68 | void PowerControl::pollPgood() |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 69 | { |
| 70 | if (inStateTransition) |
| 71 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 72 | // In transition between power on and off, check for timeout |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 73 | const auto now = std::chrono::steady_clock::now(); |
| 74 | if (now > pgoodTimeoutTime) |
| 75 | { |
| 76 | log<level::ERR>("ERROR PowerControl: Pgood poll timeout"); |
| 77 | inStateTransition = false; |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 78 | |
| 79 | try |
| 80 | { |
| 81 | auto method = bus.new_method_call( |
| 82 | "xyz.openbmc_project.Logging", |
| 83 | "/xyz/openbmc_project/logging", |
| 84 | "xyz.openbmc_project.Logging.Create", "Create"); |
| 85 | |
| 86 | std::map<std::string, std::string> additionalData; |
| 87 | // Add PID to AdditionalData |
| 88 | additionalData.emplace("_PID", std::to_string(getpid())); |
| 89 | |
| 90 | method.append( |
| 91 | state ? "xyz.openbmc_project.Power.Error.PowerOnTimeout" |
| 92 | : "xyz.openbmc_project.Power.Error.PowerOffTimeout", |
| 93 | sdbusplus::xyz::openbmc_project::Logging::server::Entry:: |
| 94 | Level::Critical, |
| 95 | additionalData); |
| 96 | bus.call_noreply(method); |
| 97 | } |
| 98 | catch (const std::exception& e) |
| 99 | { |
| 100 | log<level::ERR>( |
| 101 | fmt::format( |
| 102 | "Unable to log timeout error, state: {}, error {}", |
| 103 | state, e.what()) |
| 104 | .c_str()); |
| 105 | } |
| 106 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | int pgoodState = pgoodLine.get_value(); |
| 112 | if (pgoodState != pgood) |
| 113 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 114 | // Power good has changed since last read |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 115 | pgood = pgoodState; |
| 116 | if (pgoodState == 0) |
| 117 | { |
| 118 | emitPowerLostSignal(); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | emitPowerGoodSignal(); |
| 123 | } |
| 124 | emitPropertyChangedSignal("pgood"); |
| 125 | } |
| 126 | if (pgoodState == state) |
| 127 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 128 | // Power good matches requested state |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 129 | inStateTransition = false; |
| 130 | } |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 131 | else if (!inStateTransition && (pgoodState == 0)) |
| 132 | { |
| 133 | // Not in power off state, not changing state, and power good is off |
| 134 | // Power good has failed, call for chassis hard power off |
| 135 | log<level::ERR>("Chassis pgood failure"); |
| 136 | |
| 137 | auto method = |
| 138 | bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT, |
| 139 | util::SYSTEMD_INTERFACE, "StartUnit"); |
| 140 | method.append(util::POWEROFF_TARGET); |
| 141 | method.append("replace"); |
| 142 | bus.call_noreply(method); |
| 143 | } |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 144 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 145 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 146 | void PowerControl::setPgoodTimeout(int t) |
| 147 | { |
| 148 | if (timeout.count() != t) |
| 149 | { |
| 150 | timeout = std::chrono::seconds(t); |
| 151 | emitPropertyChangedSignal("pgood_timeout"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void PowerControl::setState(int s) |
| 156 | { |
| 157 | if (state == s) |
| 158 | { |
| 159 | log<level::INFO>( |
| 160 | fmt::format("Power already at requested state: {}", state).c_str()); |
| 161 | return; |
| 162 | } |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 163 | if (s == 0) |
| 164 | { |
| 165 | // Wait for two seconds when powering down. This is to allow host and |
| 166 | // other BMC applications time to complete power off processing |
| 167 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 168 | } |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 169 | |
| 170 | log<level::INFO>(fmt::format("setState: {}", s).c_str()); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 171 | powerControlLine.request( |
| 172 | {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0}); |
| 173 | powerControlLine.set_value(s); |
| 174 | powerControlLine.release(); |
| 175 | |
| 176 | pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout; |
| 177 | inStateTransition = true; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 178 | state = s; |
| 179 | emitPropertyChangedSignal("state"); |
| 180 | } |
| 181 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 182 | void PowerControl::setUpGpio() |
| 183 | { |
| 184 | pgoodLine = gpiod::find_line(pgoodLineName); |
| 185 | if (!pgoodLine) |
| 186 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 187 | std::string errorString{"GPIO line name not found: " + pgoodLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 188 | log<level::ERR>(errorString.c_str()); |
| 189 | report< |
| 190 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 191 | throw std::runtime_error(errorString); |
| 192 | } |
| 193 | powerControlLine = gpiod::find_line(powerControlLineName); |
| 194 | if (!powerControlLine) |
| 195 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 196 | std::string errorString{"GPIO line name not found: " + |
| 197 | powerControlLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 198 | log<level::ERR>(errorString.c_str()); |
| 199 | report< |
| 200 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 201 | throw std::runtime_error(errorString); |
| 202 | } |
| 203 | |
| 204 | pgoodLine.request( |
| 205 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 206 | int pgoodState = pgoodLine.get_value(); |
| 207 | pgood = pgoodState; |
| 208 | state = pgoodState; |
| 209 | log<level::INFO>(fmt::format("Pgood state: {}", pgoodState).c_str()); |
| 210 | } |
| 211 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 212 | } // namespace phosphor::power::sequencer |