Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018-2019 Intel 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 | */ |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 16 | #include <sys/sysinfo.h> |
| 17 | #include <systemd/sd-journal.h> |
| 18 | |
Jason M. Bills | e63dea0 | 2020-08-27 12:07:35 -0700 | [diff] [blame] | 19 | #include <boost/asio/io_service.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 20 | #include <boost/asio/posix/stream_descriptor.hpp> |
Jason M. Bills | e63dea0 | 2020-08-27 12:07:35 -0700 | [diff] [blame] | 21 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_set.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 24 | #include <gpiod.hpp> |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 25 | #include <nlohmann/json.hpp> |
Vijay Khemka | fc1ecc5 | 2020-04-01 10:49:28 -0700 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 27 | #include <sdbusplus/asio/object_server.hpp> |
Vijay Khemka | 2b6f442 | 2020-05-29 11:13:23 -0700 | [diff] [blame] | 28 | |
| 29 | #include <filesystem> |
| 30 | #include <fstream> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 31 | #include <string_view> |
| 32 | |
| 33 | namespace power_control |
| 34 | { |
| 35 | static boost::asio::io_service io; |
| 36 | std::shared_ptr<sdbusplus::asio::connection> conn; |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 37 | |
| 38 | static std::string node = "0"; |
| 39 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 40 | enum class DbusConfigType |
| 41 | { |
| 42 | name = 1, |
| 43 | path, |
| 44 | interface, |
| 45 | property |
| 46 | }; |
| 47 | boost::container::flat_map<DbusConfigType, std::string> dbusParams = { |
| 48 | {DbusConfigType::name, "DbusName"}, |
| 49 | {DbusConfigType::path, "Path"}, |
| 50 | {DbusConfigType::interface, "Interface"}, |
| 51 | {DbusConfigType::property, "Property"}}; |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 52 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 53 | enum class ConfigType |
| 54 | { |
| 55 | GPIO = 1, |
| 56 | DBUS |
| 57 | }; |
| 58 | |
| 59 | struct ConfigData |
| 60 | { |
| 61 | std::string name; |
| 62 | std::string lineName; |
| 63 | std::string dbusName; |
| 64 | std::string path; |
| 65 | std::string interface; |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 66 | bool polarity; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 67 | ConfigType type; |
| 68 | }; |
| 69 | |
| 70 | static ConfigData powerOutConfig; |
| 71 | static ConfigData powerOkConfig; |
| 72 | static ConfigData resetOutConfig; |
| 73 | static ConfigData nmiOutConfig; |
| 74 | static ConfigData sioPwrGoodConfig; |
| 75 | static ConfigData sioOnControlConfig; |
| 76 | static ConfigData sioS5Config; |
| 77 | static ConfigData postCompleteConfig; |
| 78 | static ConfigData powerButtonConfig; |
| 79 | static ConfigData resetButtonConfig; |
| 80 | static ConfigData idButtonConfig; |
| 81 | static ConfigData nmiButtonConfig; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 82 | static ConfigData slotPowerConfig; |
| 83 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 84 | // map for storing list of gpio parameters whose config are to be read from x86 |
| 85 | // power control json config |
| 86 | boost::container::flat_map<std::string, ConfigData*> powerSignalMap = { |
| 87 | {"PowerOut", &powerOutConfig}, |
| 88 | {"PowerOk", &powerOkConfig}, |
| 89 | {"ResetOut", &resetOutConfig}, |
| 90 | {"NMIOut", &nmiOutConfig}, |
| 91 | {"SioPowerGood", &sioPwrGoodConfig}, |
| 92 | {"SioOnControl", &sioOnControlConfig}, |
| 93 | {"SIOS5", &sioS5Config}, |
| 94 | {"PostComplete", &postCompleteConfig}, |
| 95 | {"PowerButton", &powerButtonConfig}, |
| 96 | {"ResetButton", &resetButtonConfig}, |
| 97 | {"IdButton", &idButtonConfig}, |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 98 | {"NMIButton", &nmiButtonConfig}, |
| 99 | {"SlotPower", &slotPowerConfig}}; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 100 | |
| 101 | static std::string hostDbusName = "xyz.openbmc_project.State.Host"; |
| 102 | static std::string chassisDbusName = "xyz.openbmc_project.State.Chassis"; |
| 103 | static std::string osDbusName = "xyz.openbmc_project.State.OperatingSystem"; |
| 104 | static std::string buttonDbusName = "xyz.openbmc_project.Chassis.Buttons"; |
| 105 | static std::string nmiDbusName = "xyz.openbmc_project.Control.Host.NMI"; |
| 106 | static std::string rstCauseDbusName = |
| 107 | "xyz.openbmc_project.Control.Host.RestartCause"; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 108 | static std::shared_ptr<sdbusplus::asio::dbus_interface> hostIface; |
| 109 | static std::shared_ptr<sdbusplus::asio::dbus_interface> chassisIface; |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 110 | #ifdef CHASSIS_SYSTEM_RESET |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 111 | static std::shared_ptr<sdbusplus::asio::dbus_interface> chassisSysIface; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 112 | static std::shared_ptr<sdbusplus::asio::dbus_interface> chassisSlotIface; |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 113 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 114 | static std::shared_ptr<sdbusplus::asio::dbus_interface> powerButtonIface; |
| 115 | static std::shared_ptr<sdbusplus::asio::dbus_interface> resetButtonIface; |
| 116 | static std::shared_ptr<sdbusplus::asio::dbus_interface> nmiButtonIface; |
| 117 | static std::shared_ptr<sdbusplus::asio::dbus_interface> osIface; |
| 118 | static std::shared_ptr<sdbusplus::asio::dbus_interface> idButtonIface; |
Chen Yugang | 174ec66 | 2019-08-19 19:58:49 +0800 | [diff] [blame] | 119 | static std::shared_ptr<sdbusplus::asio::dbus_interface> nmiOutIface; |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 120 | static std::shared_ptr<sdbusplus::asio::dbus_interface> restartCauseIface; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 121 | |
| 122 | static gpiod::line powerButtonMask; |
| 123 | static gpiod::line resetButtonMask; |
| 124 | static bool nmiButtonMasked = false; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 125 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 126 | // This map contains all timer values that are to be read from json config |
| 127 | boost::container::flat_map<std::string, int> TimerMap = { |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 128 | {"PowerPulseMs", 200}, |
| 129 | {"ForceOffPulseMs", 15000}, |
| 130 | {"ResetPulseMs", 500}, |
| 131 | {"PowerCycleMs", 5000}, |
| 132 | {"SioPowerGoodWatchdogMs", 1000}, |
| 133 | {"PsPowerOKWatchdogMs", 8000}, |
| 134 | {"GracefulPowerOffS", (5 * 60)}, |
| 135 | {"WarmResetCheckMs", 500}, |
| 136 | {"PowerOffSaveMs", 7000}, |
| 137 | {"SlotPowerCycleMs", 200}}; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 138 | const static std::filesystem::path powerControlDir = "/var/lib/power-control"; |
| 139 | const static constexpr std::string_view powerStateFile = "power-state"; |
| 140 | |
| 141 | static bool nmiEnabled = true; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 142 | static bool sioEnabled = true; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 143 | |
| 144 | // Timers |
| 145 | // Time holding GPIOs asserted |
| 146 | static boost::asio::steady_timer gpioAssertTimer(io); |
| 147 | // Time between off and on during a power cycle |
| 148 | static boost::asio::steady_timer powerCycleTimer(io); |
| 149 | // Time OS gracefully powering off |
| 150 | static boost::asio::steady_timer gracefulPowerOffTimer(io); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 151 | // Time the warm reset check |
| 152 | static boost::asio::steady_timer warmResetCheckTimer(io); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 153 | // Time power supply power OK assertion on power-on |
| 154 | static boost::asio::steady_timer psPowerOKWatchdogTimer(io); |
| 155 | // Time SIO power good assertion on power-on |
| 156 | static boost::asio::steady_timer sioPowerGoodWatchdogTimer(io); |
| 157 | // Time power-off state save for power loss tracking |
| 158 | static boost::asio::steady_timer powerStateSaveTimer(io); |
| 159 | // POH timer |
| 160 | static boost::asio::steady_timer pohCounterTimer(io); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 161 | // Time when to allow restart cause updates |
| 162 | static boost::asio::steady_timer restartCauseTimer(io); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 163 | static boost::asio::steady_timer slotPowerCycleTimer(io); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 164 | |
| 165 | // GPIO Lines and Event Descriptors |
| 166 | static gpiod::line psPowerOKLine; |
| 167 | static boost::asio::posix::stream_descriptor psPowerOKEvent(io); |
| 168 | static gpiod::line sioPowerGoodLine; |
| 169 | static boost::asio::posix::stream_descriptor sioPowerGoodEvent(io); |
| 170 | static gpiod::line sioOnControlLine; |
| 171 | static boost::asio::posix::stream_descriptor sioOnControlEvent(io); |
| 172 | static gpiod::line sioS5Line; |
| 173 | static boost::asio::posix::stream_descriptor sioS5Event(io); |
| 174 | static gpiod::line powerButtonLine; |
| 175 | static boost::asio::posix::stream_descriptor powerButtonEvent(io); |
| 176 | static gpiod::line resetButtonLine; |
| 177 | static boost::asio::posix::stream_descriptor resetButtonEvent(io); |
| 178 | static gpiod::line nmiButtonLine; |
| 179 | static boost::asio::posix::stream_descriptor nmiButtonEvent(io); |
| 180 | static gpiod::line idButtonLine; |
| 181 | static boost::asio::posix::stream_descriptor idButtonEvent(io); |
| 182 | static gpiod::line postCompleteLine; |
| 183 | static boost::asio::posix::stream_descriptor postCompleteEvent(io); |
| 184 | static gpiod::line nmiOutLine; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 185 | static gpiod::line slotPowerLine; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 186 | |
| 187 | static constexpr uint8_t beepPowerFail = 8; |
| 188 | |
| 189 | static void beep(const uint8_t& beepPriority) |
| 190 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 191 | std::string logMsg = "Beep with priority: " + std::to_string(beepPriority); |
| 192 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 193 | |
| 194 | conn->async_method_call( |
| 195 | [](boost::system::error_code ec) { |
| 196 | if (ec) |
| 197 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 198 | std::string errMsg = |
| 199 | "beep returned error with async_method_call (ec = " + |
| 200 | ec.message(); |
| 201 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 202 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | }, |
| 206 | "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode", |
| 207 | "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority)); |
| 208 | } |
| 209 | |
| 210 | enum class PowerState |
| 211 | { |
| 212 | on, |
| 213 | waitForPSPowerOK, |
| 214 | waitForSIOPowerGood, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 215 | off, |
| 216 | transitionToOff, |
| 217 | gracefulTransitionToOff, |
| 218 | cycleOff, |
| 219 | transitionToCycleOff, |
| 220 | gracefulTransitionToCycleOff, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 221 | checkForWarmReset, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 222 | }; |
| 223 | static PowerState powerState; |
| 224 | static std::string getPowerStateName(PowerState state) |
| 225 | { |
| 226 | switch (state) |
| 227 | { |
| 228 | case PowerState::on: |
| 229 | return "On"; |
| 230 | break; |
| 231 | case PowerState::waitForPSPowerOK: |
| 232 | return "Wait for Power Supply Power OK"; |
| 233 | break; |
| 234 | case PowerState::waitForSIOPowerGood: |
| 235 | return "Wait for SIO Power Good"; |
| 236 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 237 | case PowerState::off: |
| 238 | return "Off"; |
| 239 | break; |
| 240 | case PowerState::transitionToOff: |
| 241 | return "Transition to Off"; |
| 242 | break; |
| 243 | case PowerState::gracefulTransitionToOff: |
| 244 | return "Graceful Transition to Off"; |
| 245 | break; |
| 246 | case PowerState::cycleOff: |
| 247 | return "Power Cycle Off"; |
| 248 | break; |
| 249 | case PowerState::transitionToCycleOff: |
| 250 | return "Transition to Power Cycle Off"; |
| 251 | break; |
| 252 | case PowerState::gracefulTransitionToCycleOff: |
| 253 | return "Graceful Transition to Power Cycle Off"; |
| 254 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 255 | case PowerState::checkForWarmReset: |
| 256 | return "Check for Warm Reset"; |
| 257 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 258 | default: |
| 259 | return "unknown state: " + std::to_string(static_cast<int>(state)); |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | static void logStateTransition(const PowerState state) |
| 264 | { |
Naveen Moses | ec972d8 | 2021-07-16 21:19:23 +0530 | [diff] [blame] | 265 | std::string logMsg = "Host" + node + ": Moving to \"" + |
| 266 | getPowerStateName(state) + "\" state"; |
Vijay Khemka | fc1ecc5 | 2020-04-01 10:49:28 -0700 | [diff] [blame] | 267 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 268 | logMsg.c_str(), |
Vijay Khemka | d6c5ad1 | 2020-05-27 14:57:52 -0700 | [diff] [blame] | 269 | phosphor::logging::entry("STATE=%s", getPowerStateName(state).c_str()), |
Naveen Moses | ec972d8 | 2021-07-16 21:19:23 +0530 | [diff] [blame] | 270 | phosphor::logging::entry("HOST=%s", node.c_str())); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | enum class Event |
| 274 | { |
| 275 | psPowerOKAssert, |
| 276 | psPowerOKDeAssert, |
| 277 | sioPowerGoodAssert, |
| 278 | sioPowerGoodDeAssert, |
| 279 | sioS5Assert, |
| 280 | sioS5DeAssert, |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 281 | pltRstAssert, |
| 282 | pltRstDeAssert, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 283 | postCompleteAssert, |
| 284 | postCompleteDeAssert, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 285 | powerButtonPressed, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 286 | resetButtonPressed, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 287 | powerCycleTimerExpired, |
| 288 | psPowerOKWatchdogTimerExpired, |
| 289 | sioPowerGoodWatchdogTimerExpired, |
| 290 | gracefulPowerOffTimerExpired, |
| 291 | powerOnRequest, |
| 292 | powerOffRequest, |
| 293 | powerCycleRequest, |
| 294 | resetRequest, |
| 295 | gracefulPowerOffRequest, |
| 296 | gracefulPowerCycleRequest, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 297 | warmResetDetected, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 298 | }; |
| 299 | static std::string getEventName(Event event) |
| 300 | { |
| 301 | switch (event) |
| 302 | { |
| 303 | case Event::psPowerOKAssert: |
| 304 | return "power supply power OK assert"; |
| 305 | break; |
| 306 | case Event::psPowerOKDeAssert: |
| 307 | return "power supply power OK de-assert"; |
| 308 | break; |
| 309 | case Event::sioPowerGoodAssert: |
| 310 | return "SIO power good assert"; |
| 311 | break; |
| 312 | case Event::sioPowerGoodDeAssert: |
| 313 | return "SIO power good de-assert"; |
| 314 | break; |
| 315 | case Event::sioS5Assert: |
| 316 | return "SIO S5 assert"; |
| 317 | break; |
| 318 | case Event::sioS5DeAssert: |
| 319 | return "SIO S5 de-assert"; |
| 320 | break; |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 321 | case Event::pltRstAssert: |
| 322 | return "PLT_RST assert"; |
| 323 | break; |
| 324 | case Event::pltRstDeAssert: |
| 325 | return "PLT_RST de-assert"; |
| 326 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 327 | case Event::postCompleteAssert: |
| 328 | return "POST Complete assert"; |
| 329 | break; |
| 330 | case Event::postCompleteDeAssert: |
| 331 | return "POST Complete de-assert"; |
| 332 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 333 | case Event::powerButtonPressed: |
| 334 | return "power button pressed"; |
| 335 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 336 | case Event::resetButtonPressed: |
| 337 | return "reset button pressed"; |
| 338 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 339 | case Event::powerCycleTimerExpired: |
| 340 | return "power cycle timer expired"; |
| 341 | break; |
| 342 | case Event::psPowerOKWatchdogTimerExpired: |
| 343 | return "power supply power OK watchdog timer expired"; |
| 344 | break; |
| 345 | case Event::sioPowerGoodWatchdogTimerExpired: |
| 346 | return "SIO power good watchdog timer expired"; |
| 347 | break; |
| 348 | case Event::gracefulPowerOffTimerExpired: |
| 349 | return "graceful power-off timer expired"; |
| 350 | break; |
| 351 | case Event::powerOnRequest: |
| 352 | return "power-on request"; |
| 353 | break; |
| 354 | case Event::powerOffRequest: |
| 355 | return "power-off request"; |
| 356 | break; |
| 357 | case Event::powerCycleRequest: |
| 358 | return "power-cycle request"; |
| 359 | break; |
| 360 | case Event::resetRequest: |
| 361 | return "reset request"; |
| 362 | break; |
| 363 | case Event::gracefulPowerOffRequest: |
| 364 | return "graceful power-off request"; |
| 365 | break; |
| 366 | case Event::gracefulPowerCycleRequest: |
| 367 | return "graceful power-cycle request"; |
| 368 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 369 | case Event::warmResetDetected: |
| 370 | return "warm reset detected"; |
| 371 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 372 | default: |
| 373 | return "unknown event: " + std::to_string(static_cast<int>(event)); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | static void logEvent(const std::string_view stateHandler, const Event event) |
| 378 | { |
Vijay Khemka | fc1ecc5 | 2020-04-01 10:49:28 -0700 | [diff] [blame] | 379 | std::string logMsg{stateHandler}; |
| 380 | logMsg += ": " + getEventName(event) + " event received"; |
| 381 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 382 | logMsg.c_str(), |
| 383 | phosphor::logging::entry("EVENT=%s", getEventName(event).c_str())); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // Power state handlers |
| 387 | static void powerStateOn(const Event event); |
| 388 | static void powerStateWaitForPSPowerOK(const Event event); |
| 389 | static void powerStateWaitForSIOPowerGood(const Event event); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 390 | static void powerStateOff(const Event event); |
| 391 | static void powerStateTransitionToOff(const Event event); |
| 392 | static void powerStateGracefulTransitionToOff(const Event event); |
| 393 | static void powerStateCycleOff(const Event event); |
| 394 | static void powerStateTransitionToCycleOff(const Event event); |
| 395 | static void powerStateGracefulTransitionToCycleOff(const Event event); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 396 | static void powerStateCheckForWarmReset(const Event event); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 397 | |
| 398 | static std::function<void(const Event)> getPowerStateHandler(PowerState state) |
| 399 | { |
| 400 | switch (state) |
| 401 | { |
| 402 | case PowerState::on: |
| 403 | return powerStateOn; |
| 404 | break; |
| 405 | case PowerState::waitForPSPowerOK: |
| 406 | return powerStateWaitForPSPowerOK; |
| 407 | break; |
| 408 | case PowerState::waitForSIOPowerGood: |
| 409 | return powerStateWaitForSIOPowerGood; |
| 410 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 411 | case PowerState::off: |
| 412 | return powerStateOff; |
| 413 | break; |
| 414 | case PowerState::transitionToOff: |
| 415 | return powerStateTransitionToOff; |
| 416 | break; |
| 417 | case PowerState::gracefulTransitionToOff: |
| 418 | return powerStateGracefulTransitionToOff; |
| 419 | break; |
| 420 | case PowerState::cycleOff: |
| 421 | return powerStateCycleOff; |
| 422 | break; |
| 423 | case PowerState::transitionToCycleOff: |
| 424 | return powerStateTransitionToCycleOff; |
| 425 | break; |
| 426 | case PowerState::gracefulTransitionToCycleOff: |
| 427 | return powerStateGracefulTransitionToCycleOff; |
| 428 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 429 | case PowerState::checkForWarmReset: |
| 430 | return powerStateCheckForWarmReset; |
| 431 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 432 | default: |
Zev Weiss | 047bcb5 | 2020-08-20 21:28:11 +0000 | [diff] [blame] | 433 | return nullptr; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | static void sendPowerControlEvent(const Event event) |
| 439 | { |
| 440 | std::function<void(const Event)> handler = getPowerStateHandler(powerState); |
| 441 | if (handler == nullptr) |
| 442 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 443 | std::string errMsg = "Failed to find handler for power state: " + |
| 444 | std::to_string(static_cast<int>(powerState)); |
| 445 | phosphor::logging::log<phosphor::logging::level::INFO>(errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | handler(event); |
| 449 | } |
| 450 | |
| 451 | static uint64_t getCurrentTimeMs() |
| 452 | { |
| 453 | struct timespec time = {}; |
| 454 | |
| 455 | if (clock_gettime(CLOCK_REALTIME, &time) < 0) |
| 456 | { |
| 457 | return 0; |
| 458 | } |
| 459 | uint64_t currentTimeMs = static_cast<uint64_t>(time.tv_sec) * 1000; |
| 460 | currentTimeMs += static_cast<uint64_t>(time.tv_nsec) / 1000 / 1000; |
| 461 | |
| 462 | return currentTimeMs; |
| 463 | } |
| 464 | |
| 465 | static constexpr std::string_view getHostState(const PowerState state) |
| 466 | { |
| 467 | switch (state) |
| 468 | { |
| 469 | case PowerState::on: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 470 | case PowerState::gracefulTransitionToOff: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 471 | case PowerState::gracefulTransitionToCycleOff: |
| 472 | return "xyz.openbmc_project.State.Host.HostState.Running"; |
| 473 | break; |
| 474 | case PowerState::waitForPSPowerOK: |
| 475 | case PowerState::waitForSIOPowerGood: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 476 | case PowerState::off: |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 477 | case PowerState::transitionToOff: |
| 478 | case PowerState::transitionToCycleOff: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 479 | case PowerState::cycleOff: |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 480 | case PowerState::checkForWarmReset: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 481 | return "xyz.openbmc_project.State.Host.HostState.Off"; |
| 482 | break; |
| 483 | default: |
| 484 | return ""; |
| 485 | break; |
| 486 | } |
| 487 | }; |
| 488 | static constexpr std::string_view getChassisState(const PowerState state) |
| 489 | { |
| 490 | switch (state) |
| 491 | { |
| 492 | case PowerState::on: |
| 493 | case PowerState::transitionToOff: |
| 494 | case PowerState::gracefulTransitionToOff: |
| 495 | case PowerState::transitionToCycleOff: |
| 496 | case PowerState::gracefulTransitionToCycleOff: |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 497 | case PowerState::checkForWarmReset: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 498 | return "xyz.openbmc_project.State.Chassis.PowerState.On"; |
| 499 | break; |
| 500 | case PowerState::waitForPSPowerOK: |
| 501 | case PowerState::waitForSIOPowerGood: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 502 | case PowerState::off: |
| 503 | case PowerState::cycleOff: |
| 504 | return "xyz.openbmc_project.State.Chassis.PowerState.Off"; |
| 505 | break; |
| 506 | default: |
| 507 | return ""; |
| 508 | break; |
| 509 | } |
| 510 | }; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 511 | #ifdef CHASSIS_SYSTEM_RESET |
| 512 | enum class SlotPowerState |
| 513 | { |
| 514 | on, |
| 515 | off, |
| 516 | }; |
| 517 | static SlotPowerState slotPowerState; |
| 518 | static constexpr std::string_view getSlotState(const SlotPowerState state) |
| 519 | { |
| 520 | switch (state) |
| 521 | { |
| 522 | case SlotPowerState::on: |
| 523 | return "xyz.openbmc_project.State.Chassis.PowerState.On"; |
| 524 | break; |
| 525 | case SlotPowerState::off: |
| 526 | return "xyz.openbmc_project.State.Chassis.PowerState.Off"; |
| 527 | break; |
| 528 | default: |
| 529 | return ""; |
| 530 | break; |
| 531 | } |
| 532 | }; |
| 533 | static void setSlotPowerState(const SlotPowerState state) |
| 534 | { |
| 535 | slotPowerState = state; |
| 536 | chassisSlotIface->set_property("CurrentPowerState", |
| 537 | std::string(getSlotState(slotPowerState))); |
| 538 | chassisSlotIface->set_property("LastStateChangeTime", getCurrentTimeMs()); |
| 539 | } |
| 540 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 541 | static void savePowerState(const PowerState state) |
| 542 | { |
| 543 | powerStateSaveTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 544 | std::chrono::milliseconds(TimerMap["PowerOffSaveMs"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 545 | powerStateSaveTimer.async_wait([state](const boost::system::error_code ec) { |
| 546 | if (ec) |
| 547 | { |
| 548 | // operation_aborted is expected if timer is canceled before |
| 549 | // completion. |
| 550 | if (ec != boost::asio::error::operation_aborted) |
| 551 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 552 | std::string errMsg = |
| 553 | "Power-state save async_wait failed: " + ec.message(); |
| 554 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 555 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 556 | } |
| 557 | return; |
| 558 | } |
| 559 | std::ofstream powerStateStream(powerControlDir / powerStateFile); |
| 560 | powerStateStream << getChassisState(state); |
| 561 | }); |
| 562 | } |
| 563 | static void setPowerState(const PowerState state) |
| 564 | { |
| 565 | powerState = state; |
| 566 | logStateTransition(state); |
| 567 | |
| 568 | hostIface->set_property("CurrentHostState", |
| 569 | std::string(getHostState(powerState))); |
| 570 | |
| 571 | chassisIface->set_property("CurrentPowerState", |
| 572 | std::string(getChassisState(powerState))); |
| 573 | chassisIface->set_property("LastStateChangeTime", getCurrentTimeMs()); |
| 574 | |
| 575 | // Save the power state for the restore policy |
| 576 | savePowerState(state); |
| 577 | } |
| 578 | |
| 579 | enum class RestartCause |
| 580 | { |
| 581 | command, |
| 582 | resetButton, |
| 583 | powerButton, |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 584 | watchdog, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 585 | powerPolicyOn, |
| 586 | powerPolicyRestore, |
| 587 | softReset, |
| 588 | }; |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 589 | static boost::container::flat_set<RestartCause> causeSet; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 590 | static std::string getRestartCause(RestartCause cause) |
| 591 | { |
| 592 | switch (cause) |
| 593 | { |
| 594 | case RestartCause::command: |
| 595 | return "xyz.openbmc_project.State.Host.RestartCause.IpmiCommand"; |
| 596 | break; |
| 597 | case RestartCause::resetButton: |
| 598 | return "xyz.openbmc_project.State.Host.RestartCause.ResetButton"; |
| 599 | break; |
| 600 | case RestartCause::powerButton: |
| 601 | return "xyz.openbmc_project.State.Host.RestartCause.PowerButton"; |
| 602 | break; |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 603 | case RestartCause::watchdog: |
| 604 | return "xyz.openbmc_project.State.Host.RestartCause.WatchdogTimer"; |
| 605 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 606 | case RestartCause::powerPolicyOn: |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 607 | return "xyz.openbmc_project.State.Host.RestartCause.PowerPolicyAlwaysOn"; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 608 | break; |
| 609 | case RestartCause::powerPolicyRestore: |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 610 | return "xyz.openbmc_project.State.Host.RestartCause.PowerPolicyPreviousState"; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 611 | break; |
| 612 | case RestartCause::softReset: |
| 613 | return "xyz.openbmc_project.State.Host.RestartCause.SoftReset"; |
| 614 | break; |
| 615 | default: |
| 616 | return "xyz.openbmc_project.State.Host.RestartCause.Unknown"; |
| 617 | break; |
| 618 | } |
| 619 | } |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 620 | static void addRestartCause(const RestartCause cause) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 621 | { |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 622 | // Add this to the set of causes for this restart |
| 623 | causeSet.insert(cause); |
| 624 | } |
| 625 | static void clearRestartCause() |
| 626 | { |
| 627 | // Clear the set for the next restart |
| 628 | causeSet.clear(); |
| 629 | } |
| 630 | static void setRestartCauseProperty(const std::string& cause) |
| 631 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 632 | std::string logMsg = "RestartCause set to " + cause; |
| 633 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 634 | restartCauseIface->set_property("RestartCause", cause); |
| 635 | } |
Rashmi RV | 89f6131 | 2020-01-22 15:41:50 +0530 | [diff] [blame] | 636 | |
| 637 | static void resetACBootProperty() |
| 638 | { |
| 639 | if ((causeSet.contains(RestartCause::command)) || |
| 640 | (causeSet.contains(RestartCause::softReset))) |
| 641 | { |
| 642 | conn->async_method_call( |
| 643 | [](boost::system::error_code ec) { |
| 644 | if (ec) |
| 645 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 646 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 647 | "failed to reset ACBoot property"); |
Rashmi RV | 89f6131 | 2020-01-22 15:41:50 +0530 | [diff] [blame] | 648 | } |
| 649 | }, |
| 650 | "xyz.openbmc_project.Settings", |
| 651 | "/xyz/openbmc_project/control/host0/ac_boot", |
| 652 | "org.freedesktop.DBus.Properties", "Set", |
| 653 | "xyz.openbmc_project.Common.ACBoot", "ACBoot", |
| 654 | std::variant<std::string>{"False"}); |
| 655 | } |
| 656 | } |
| 657 | |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 658 | static void setRestartCause() |
| 659 | { |
| 660 | // Determine the actual restart cause based on the set of causes |
| 661 | std::string restartCause = |
| 662 | "xyz.openbmc_project.State.Host.RestartCause.Unknown"; |
| 663 | if (causeSet.contains(RestartCause::watchdog)) |
| 664 | { |
| 665 | restartCause = getRestartCause(RestartCause::watchdog); |
| 666 | } |
| 667 | else if (causeSet.contains(RestartCause::command)) |
| 668 | { |
| 669 | restartCause = getRestartCause(RestartCause::command); |
| 670 | } |
| 671 | else if (causeSet.contains(RestartCause::resetButton)) |
| 672 | { |
| 673 | restartCause = getRestartCause(RestartCause::resetButton); |
| 674 | } |
| 675 | else if (causeSet.contains(RestartCause::powerButton)) |
| 676 | { |
| 677 | restartCause = getRestartCause(RestartCause::powerButton); |
| 678 | } |
| 679 | else if (causeSet.contains(RestartCause::powerPolicyOn)) |
| 680 | { |
| 681 | restartCause = getRestartCause(RestartCause::powerPolicyOn); |
| 682 | } |
| 683 | else if (causeSet.contains(RestartCause::powerPolicyRestore)) |
| 684 | { |
| 685 | restartCause = getRestartCause(RestartCause::powerPolicyRestore); |
| 686 | } |
| 687 | else if (causeSet.contains(RestartCause::softReset)) |
| 688 | { |
| 689 | restartCause = getRestartCause(RestartCause::softReset); |
| 690 | } |
| 691 | |
| 692 | setRestartCauseProperty(restartCause); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 695 | static void systemPowerGoodFailedLog() |
| 696 | { |
| 697 | sd_journal_send( |
| 698 | "MESSAGE=PowerControl: system power good failed to assert (VR failure)", |
| 699 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 700 | "OpenBMC.0.1.SystemPowerGoodFailed", "REDFISH_MESSAGE_ARGS=%d", |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 701 | TimerMap["SioPowerGoodWatchdogMs"], NULL); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static void psPowerOKFailedLog() |
| 705 | { |
| 706 | sd_journal_send( |
| 707 | "MESSAGE=PowerControl: power supply power good failed to assert", |
| 708 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 709 | "OpenBMC.0.1.PowerSupplyPowerGoodFailed", "REDFISH_MESSAGE_ARGS=%d", |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 710 | TimerMap["PsPowerOKWatchdogMs"], NULL); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 713 | static void powerRestorePolicyLog() |
| 714 | { |
| 715 | sd_journal_send("MESSAGE=PowerControl: power restore policy applied", |
| 716 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 717 | "OpenBMC.0.1.PowerRestorePolicyApplied", NULL); |
| 718 | } |
| 719 | |
| 720 | static void powerButtonPressLog() |
| 721 | { |
| 722 | sd_journal_send("MESSAGE=PowerControl: power button pressed", "PRIORITY=%i", |
| 723 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 724 | "OpenBMC.0.1.PowerButtonPressed", NULL); |
| 725 | } |
| 726 | |
| 727 | static void resetButtonPressLog() |
| 728 | { |
| 729 | sd_journal_send("MESSAGE=PowerControl: reset button pressed", "PRIORITY=%i", |
| 730 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 731 | "OpenBMC.0.1.ResetButtonPressed", NULL); |
| 732 | } |
| 733 | |
| 734 | static void nmiButtonPressLog() |
| 735 | { |
| 736 | sd_journal_send("MESSAGE=PowerControl: NMI button pressed", "PRIORITY=%i", |
| 737 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 738 | "OpenBMC.0.1.NMIButtonPressed", NULL); |
| 739 | } |
| 740 | |
| 741 | static void nmiDiagIntLog() |
| 742 | { |
| 743 | sd_journal_send("MESSAGE=PowerControl: NMI Diagnostic Interrupt", |
| 744 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 745 | "OpenBMC.0.1.NMIDiagnosticInterrupt", NULL); |
| 746 | } |
| 747 | |
| 748 | static int initializePowerStateStorage() |
| 749 | { |
| 750 | // create the power control directory if it doesn't exist |
| 751 | std::error_code ec; |
| 752 | if (!(std::filesystem::create_directories(powerControlDir, ec))) |
| 753 | { |
| 754 | if (ec.value() != 0) |
| 755 | { |
Zev Weiss | 6b8e3e0 | 2021-09-01 22:36:03 -0500 | [diff] [blame] | 756 | std::string errMsg = "failed to create " + |
| 757 | powerControlDir.string() + ": " + ec.message(); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 758 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 759 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 760 | return -1; |
| 761 | } |
| 762 | } |
| 763 | // Create the power state file if it doesn't exist |
| 764 | if (!std::filesystem::exists(powerControlDir / powerStateFile)) |
| 765 | { |
| 766 | std::ofstream powerStateStream(powerControlDir / powerStateFile); |
| 767 | powerStateStream << getChassisState(powerState); |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static bool wasPowerDropped() |
| 773 | { |
| 774 | std::ifstream powerStateStream(powerControlDir / powerStateFile); |
| 775 | if (!powerStateStream.is_open()) |
| 776 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 777 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 778 | "Failed to open power state file"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 779 | return false; |
| 780 | } |
| 781 | |
| 782 | std::string state; |
| 783 | std::getline(powerStateStream, state); |
| 784 | return state == "xyz.openbmc_project.State.Chassis.PowerState.On"; |
| 785 | } |
| 786 | |
| 787 | static void invokePowerRestorePolicy(const std::string& policy) |
| 788 | { |
| 789 | // Async events may call this twice, but we only want to run once |
| 790 | static bool policyInvoked = false; |
| 791 | if (policyInvoked) |
| 792 | { |
| 793 | return; |
| 794 | } |
| 795 | policyInvoked = true; |
| 796 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 797 | std::string logMsg = "Power restore delay expired, invoking " + policy; |
| 798 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 799 | if (policy == |
| 800 | "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn") |
| 801 | { |
| 802 | sendPowerControlEvent(Event::powerOnRequest); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 803 | setRestartCauseProperty(getRestartCause(RestartCause::powerPolicyOn)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 804 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 805 | else if (policy == |
| 806 | "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore") |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 807 | { |
| 808 | if (wasPowerDropped()) |
| 809 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 810 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 811 | "Power was dropped, restoring Host On state"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 812 | sendPowerControlEvent(Event::powerOnRequest); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 813 | setRestartCauseProperty( |
| 814 | getRestartCause(RestartCause::powerPolicyRestore)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 815 | } |
| 816 | else |
| 817 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 818 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 819 | "No power drop, restoring Host Off state"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
Jason M. Bills | 94ce8eb | 2019-09-30 10:13:25 -0700 | [diff] [blame] | 822 | // We're done with the previous power state for the restore policy, so store |
| 823 | // the current state |
| 824 | savePowerState(powerState); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static void powerRestorePolicyDelay(int delay) |
| 828 | { |
| 829 | // Async events may call this twice, but we only want to run once |
| 830 | static bool delayStarted = false; |
| 831 | if (delayStarted) |
| 832 | { |
| 833 | return; |
| 834 | } |
| 835 | delayStarted = true; |
| 836 | // Calculate the delay from now to meet the requested delay |
| 837 | // Subtract the approximate uboot time |
| 838 | static constexpr const int ubootSeconds = 20; |
| 839 | delay -= ubootSeconds; |
| 840 | // Subtract the time since boot |
| 841 | struct sysinfo info = {}; |
| 842 | if (sysinfo(&info) == 0) |
| 843 | { |
| 844 | delay -= info.uptime; |
| 845 | } |
| 846 | // 0 is the minimum delay |
| 847 | delay = std::max(delay, 0); |
| 848 | |
| 849 | static boost::asio::steady_timer powerRestorePolicyTimer(io); |
| 850 | powerRestorePolicyTimer.expires_after(std::chrono::seconds(delay)); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 851 | std::string logMsg = |
| 852 | "Power restore delay of " + std::to_string(delay) + " seconds started"; |
| 853 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 854 | powerRestorePolicyTimer.async_wait([](const boost::system::error_code ec) { |
| 855 | if (ec) |
| 856 | { |
| 857 | // operation_aborted is expected if timer is canceled before |
| 858 | // completion. |
| 859 | if (ec != boost::asio::error::operation_aborted) |
| 860 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 861 | std::string errMsg = |
| 862 | "power restore policy async_wait failed: " + ec.message(); |
| 863 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 864 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 865 | } |
| 866 | return; |
| 867 | } |
| 868 | // Get Power Restore Policy |
| 869 | // In case PowerRestorePolicy is not available, set a match for it |
| 870 | static std::unique_ptr<sdbusplus::bus::match::match> |
| 871 | powerRestorePolicyMatch = std::make_unique< |
| 872 | sdbusplus::bus::match::match>( |
| 873 | *conn, |
| 874 | "type='signal',interface='org.freedesktop.DBus.Properties'," |
| 875 | "member='PropertiesChanged',arg0namespace='xyz.openbmc_" |
| 876 | "project.Control.Power.RestorePolicy'", |
| 877 | [](sdbusplus::message::message& msg) { |
| 878 | std::string interfaceName; |
| 879 | boost::container::flat_map<std::string, |
| 880 | std::variant<std::string>> |
| 881 | propertiesChanged; |
| 882 | std::string policy; |
| 883 | try |
| 884 | { |
| 885 | msg.read(interfaceName, propertiesChanged); |
| 886 | policy = std::get<std::string>( |
| 887 | propertiesChanged.begin()->second); |
| 888 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 889 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 890 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 891 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 892 | "Unable to read power restore policy value"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 893 | powerRestorePolicyMatch.reset(); |
| 894 | return; |
| 895 | } |
| 896 | invokePowerRestorePolicy(policy); |
| 897 | powerRestorePolicyMatch.reset(); |
| 898 | }); |
| 899 | |
| 900 | // Check if it's already on DBus |
| 901 | conn->async_method_call( |
| 902 | [](boost::system::error_code ec, |
| 903 | const std::variant<std::string>& policyProperty) { |
| 904 | if (ec) |
| 905 | { |
| 906 | return; |
| 907 | } |
| 908 | powerRestorePolicyMatch.reset(); |
| 909 | const std::string* policy = |
| 910 | std::get_if<std::string>(&policyProperty); |
| 911 | if (policy == nullptr) |
| 912 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 913 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 914 | "Unable to read power restore policy value"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 915 | return; |
| 916 | } |
| 917 | invokePowerRestorePolicy(*policy); |
| 918 | }, |
| 919 | "xyz.openbmc_project.Settings", |
| 920 | "/xyz/openbmc_project/control/host0/power_restore_policy", |
| 921 | "org.freedesktop.DBus.Properties", "Get", |
| 922 | "xyz.openbmc_project.Control.Power.RestorePolicy", |
| 923 | "PowerRestorePolicy"); |
| 924 | }); |
| 925 | } |
| 926 | |
| 927 | static void powerRestorePolicyStart() |
| 928 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 929 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 930 | "Power restore policy started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 931 | powerRestorePolicyLog(); |
| 932 | |
| 933 | // Get the desired delay time |
| 934 | // In case PowerRestoreDelay is not available, set a match for it |
| 935 | static std::unique_ptr<sdbusplus::bus::match::match> |
| 936 | powerRestoreDelayMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 937 | *conn, |
| 938 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 939 | "PropertiesChanged',arg0namespace='xyz.openbmc_project.Control." |
| 940 | "Power.RestoreDelay'", |
| 941 | [](sdbusplus::message::message& msg) { |
| 942 | std::string interfaceName; |
| 943 | boost::container::flat_map<std::string, std::variant<uint16_t>> |
| 944 | propertiesChanged; |
| 945 | int delay = 0; |
| 946 | try |
| 947 | { |
| 948 | msg.read(interfaceName, propertiesChanged); |
| 949 | delay = |
| 950 | std::get<uint16_t>(propertiesChanged.begin()->second); |
| 951 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 952 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 953 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 954 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 955 | "Unable to read power restore delay value"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 956 | powerRestoreDelayMatch.reset(); |
| 957 | return; |
| 958 | } |
| 959 | powerRestorePolicyDelay(delay); |
| 960 | powerRestoreDelayMatch.reset(); |
| 961 | }); |
| 962 | |
| 963 | // Check if it's already on DBus |
| 964 | conn->async_method_call( |
| 965 | [](boost::system::error_code ec, |
| 966 | const std::variant<uint16_t>& delayProperty) { |
| 967 | if (ec) |
| 968 | { |
| 969 | return; |
| 970 | } |
| 971 | powerRestoreDelayMatch.reset(); |
| 972 | const uint16_t* delay = std::get_if<uint16_t>(&delayProperty); |
| 973 | if (delay == nullptr) |
| 974 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 975 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 976 | "Unable to read power restore delay value"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 977 | return; |
| 978 | } |
| 979 | powerRestorePolicyDelay(*delay); |
| 980 | }, |
| 981 | "xyz.openbmc_project.Settings", |
| 982 | "/xyz/openbmc_project/control/power_restore_delay", |
| 983 | "org.freedesktop.DBus.Properties", "Get", |
| 984 | "xyz.openbmc_project.Control.Power.RestoreDelay", "PowerRestoreDelay"); |
| 985 | } |
| 986 | |
| 987 | static void powerRestorePolicyCheck() |
| 988 | { |
| 989 | // In case ACBoot is not available, set a match for it |
| 990 | static std::unique_ptr<sdbusplus::bus::match::match> acBootMatch = |
| 991 | std::make_unique<sdbusplus::bus::match::match>( |
| 992 | *conn, |
| 993 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 994 | "PropertiesChanged',arg0namespace='xyz.openbmc_project.Common." |
| 995 | "ACBoot'", |
| 996 | [](sdbusplus::message::message& msg) { |
| 997 | std::string interfaceName; |
| 998 | boost::container::flat_map<std::string, |
| 999 | std::variant<std::string>> |
| 1000 | propertiesChanged; |
| 1001 | std::string acBoot; |
| 1002 | try |
| 1003 | { |
| 1004 | msg.read(interfaceName, propertiesChanged); |
| 1005 | acBoot = std::get<std::string>( |
| 1006 | propertiesChanged.begin()->second); |
| 1007 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 1008 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1009 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1010 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1011 | "Unable to read AC Boot status"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1012 | acBootMatch.reset(); |
| 1013 | return; |
| 1014 | } |
| 1015 | if (acBoot == "Unknown") |
| 1016 | { |
| 1017 | return; |
| 1018 | } |
| 1019 | if (acBoot == "True") |
| 1020 | { |
| 1021 | // Start the Power Restore policy |
| 1022 | powerRestorePolicyStart(); |
| 1023 | } |
| 1024 | acBootMatch.reset(); |
| 1025 | }); |
| 1026 | |
| 1027 | // Check if it's already on DBus |
| 1028 | conn->async_method_call( |
| 1029 | [](boost::system::error_code ec, |
| 1030 | const std::variant<std::string>& acBootProperty) { |
| 1031 | if (ec) |
| 1032 | { |
| 1033 | return; |
| 1034 | } |
| 1035 | const std::string* acBoot = |
| 1036 | std::get_if<std::string>(&acBootProperty); |
| 1037 | if (acBoot == nullptr) |
| 1038 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1039 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1040 | "Unable to read AC Boot status"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | if (*acBoot == "Unknown") |
| 1044 | { |
| 1045 | return; |
| 1046 | } |
| 1047 | if (*acBoot == "True") |
| 1048 | { |
| 1049 | // Start the Power Restore policy |
| 1050 | powerRestorePolicyStart(); |
| 1051 | } |
| 1052 | acBootMatch.reset(); |
| 1053 | }, |
| 1054 | "xyz.openbmc_project.Settings", |
| 1055 | "/xyz/openbmc_project/control/host0/ac_boot", |
| 1056 | "org.freedesktop.DBus.Properties", "Get", |
| 1057 | "xyz.openbmc_project.Common.ACBoot", "ACBoot"); |
| 1058 | } |
| 1059 | |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1060 | static void waitForGPIOEvent(const std::string& name, |
| 1061 | const std::function<void(bool)>& eventHandler, |
| 1062 | gpiod::line& line, |
| 1063 | boost::asio::posix::stream_descriptor& event) |
| 1064 | { |
| 1065 | event.async_wait( |
| 1066 | boost::asio::posix::stream_descriptor::wait_read, |
| 1067 | [&name, eventHandler, &line, |
| 1068 | &event](const boost::system::error_code ec) { |
| 1069 | if (ec) |
| 1070 | { |
| 1071 | std::string errMsg = |
| 1072 | name + " fd handler error: " + ec.message(); |
| 1073 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1074 | errMsg.c_str()); |
| 1075 | // TODO: throw here to force power-control to restart? |
| 1076 | return; |
| 1077 | } |
| 1078 | gpiod::line_event line_event = line.event_read(); |
| 1079 | eventHandler(line_event.event_type == |
| 1080 | gpiod::line_event::RISING_EDGE); |
| 1081 | waitForGPIOEvent(name, eventHandler, line, event); |
| 1082 | }); |
| 1083 | } |
| 1084 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1085 | static bool requestGPIOEvents( |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1086 | const std::string& name, const std::function<void(bool)>& handler, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1087 | gpiod::line& gpioLine, |
| 1088 | boost::asio::posix::stream_descriptor& gpioEventDescriptor) |
| 1089 | { |
| 1090 | // Find the GPIO line |
| 1091 | gpioLine = gpiod::find_line(name); |
| 1092 | if (!gpioLine) |
| 1093 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1094 | std::string errMsg = "Failed to find the " + name + " line"; |
| 1095 | phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1096 | return false; |
| 1097 | } |
| 1098 | |
| 1099 | try |
| 1100 | { |
| 1101 | gpioLine.request( |
Jason M. Bills | 9fd8ac2 | 2021-10-21 15:14:17 -0700 | [diff] [blame^] | 1102 | {"power-control", gpiod::line_request::EVENT_BOTH_EDGES, {}}); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1103 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 1104 | catch (const std::exception&) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1105 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1106 | std::string errMsg = "Failed to request events for " + name; |
| 1107 | phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | int gpioLineFd = gpioLine.event_get_fd(); |
| 1112 | if (gpioLineFd < 0) |
| 1113 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1114 | std::string errMsg = "Failed to name " + name + " fd"; |
| 1115 | phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | gpioEventDescriptor.assign(gpioLineFd); |
| 1120 | |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1121 | waitForGPIOEvent(name, handler, gpioLine, gpioEventDescriptor); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | static bool setGPIOOutput(const std::string& name, const int value, |
| 1126 | gpiod::line& gpioLine) |
| 1127 | { |
| 1128 | // Find the GPIO line |
| 1129 | gpioLine = gpiod::find_line(name); |
| 1130 | if (!gpioLine) |
| 1131 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1132 | std::string errMsg = "Failed to find the " + name + " line"; |
| 1133 | phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | // Request GPIO output to specified value |
| 1138 | try |
| 1139 | { |
Jason M. Bills | 9fd8ac2 | 2021-10-21 15:14:17 -0700 | [diff] [blame^] | 1140 | gpioLine.request( |
| 1141 | {__FUNCTION__, gpiod::line_request::DIRECTION_OUTPUT, {}}, value); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1142 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 1143 | catch (const std::exception&) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1144 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1145 | std::string errMsg = "Failed to request " + name + " output"; |
| 1146 | phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1147 | return false; |
| 1148 | } |
| 1149 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1150 | std::string logMsg = name + " set to " + std::to_string(value); |
| 1151 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1152 | return true; |
| 1153 | } |
| 1154 | |
| 1155 | static int setMaskedGPIOOutputForMs(gpiod::line& maskedGPIOLine, |
| 1156 | const std::string& name, const int value, |
| 1157 | const int durationMs) |
| 1158 | { |
| 1159 | // Set the masked GPIO line to the specified value |
| 1160 | maskedGPIOLine.set_value(value); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1161 | std::string logMsg = name + " set to " + std::to_string(value); |
| 1162 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1163 | gpioAssertTimer.expires_after(std::chrono::milliseconds(durationMs)); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1164 | gpioAssertTimer.async_wait([maskedGPIOLine, value, |
| 1165 | name](const boost::system::error_code ec) { |
| 1166 | // Set the masked GPIO line back to the opposite value |
| 1167 | maskedGPIOLine.set_value(!value); |
| 1168 | std::string logMsg = name + " released"; |
| 1169 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
| 1170 | if (ec) |
| 1171 | { |
| 1172 | // operation_aborted is expected if timer is canceled before |
| 1173 | // completion. |
| 1174 | if (ec != boost::asio::error::operation_aborted) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1175 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1176 | std::string errMsg = |
| 1177 | name + " async_wait failed: " + ec.message(); |
| 1178 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1179 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1180 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1181 | } |
| 1182 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1186 | static int setGPIOOutputForMs(const ConfigData& config, const int value, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1187 | const int durationMs) |
| 1188 | { |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1189 | // If the requested GPIO is masked, use the mask line to set the output |
| 1190 | if (powerButtonMask && config.lineName == powerOutConfig.lineName) |
| 1191 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1192 | return setMaskedGPIOOutputForMs(powerButtonMask, config.lineName, value, |
| 1193 | durationMs); |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1194 | } |
| 1195 | if (resetButtonMask && config.lineName == resetOutConfig.lineName) |
| 1196 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1197 | return setMaskedGPIOOutputForMs(resetButtonMask, config.lineName, value, |
| 1198 | durationMs); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | // No mask set, so request and set the GPIO normally |
| 1202 | gpiod::line gpioLine; |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1203 | if (!setGPIOOutput(config.lineName, value, gpioLine)) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1204 | { |
| 1205 | return -1; |
| 1206 | } |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1207 | const std::string name = config.lineName; |
Jean-Marie Verdun | 2c495cf | 2021-09-17 21:42:23 -0400 | [diff] [blame] | 1208 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1209 | gpioAssertTimer.expires_after(std::chrono::milliseconds(durationMs)); |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1210 | gpioAssertTimer.async_wait([gpioLine, value, |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1211 | name](const boost::system::error_code ec) { |
| 1212 | // Set the GPIO line back to the opposite value |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1213 | gpioLine.set_value(!value); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1214 | std::string logMsg = name + " released"; |
| 1215 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
| 1216 | if (ec) |
| 1217 | { |
| 1218 | // operation_aborted is expected if timer is canceled before |
| 1219 | // completion. |
| 1220 | if (ec != boost::asio::error::operation_aborted) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1221 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1222 | std::string errMsg = |
| 1223 | name + " async_wait failed: " + ec.message(); |
| 1224 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1225 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1226 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1227 | } |
| 1228 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1229 | return 0; |
| 1230 | } |
| 1231 | |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1232 | static int assertGPIOForMs(const ConfigData& config, const int durationMs) |
| 1233 | { |
| 1234 | return setGPIOOutputForMs(config, config.polarity, durationMs); |
| 1235 | } |
| 1236 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1237 | static void powerOn() |
| 1238 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1239 | assertGPIOForMs(powerOutConfig, TimerMap["PowerPulseMs"]); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1240 | } |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1241 | #ifdef CHASSIS_SYSTEM_RESET |
| 1242 | static int slotPowerOn() |
| 1243 | { |
| 1244 | if (power_control::slotPowerState != power_control::SlotPowerState::on) |
| 1245 | { |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1246 | |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1247 | slotPowerLine.set_value(1); |
| 1248 | |
| 1249 | if (slotPowerLine.get_value() > 0) |
| 1250 | { |
| 1251 | setSlotPowerState(SlotPowerState::on); |
| 1252 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1253 | "Slot Power is switched On\n"); |
| 1254 | } |
| 1255 | else |
| 1256 | { |
| 1257 | return -1; |
| 1258 | } |
| 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1263 | "Slot Power is already in 'On' state\n"); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | return 0; |
| 1267 | } |
| 1268 | static int slotPowerOff() |
| 1269 | { |
| 1270 | if (power_control::slotPowerState != power_control::SlotPowerState::off) |
| 1271 | { |
| 1272 | slotPowerLine.set_value(0); |
| 1273 | |
| 1274 | if (!(slotPowerLine.get_value() > 0)) |
| 1275 | { |
| 1276 | setSlotPowerState(SlotPowerState::off); |
| 1277 | setPowerState(PowerState::off); |
| 1278 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1279 | "Slot Power is switched Off\n"); |
| 1280 | } |
| 1281 | else |
| 1282 | { |
| 1283 | return -1; |
| 1284 | } |
| 1285 | } |
| 1286 | else |
| 1287 | { |
| 1288 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1289 | "Slot Power is already in 'Off' state\n"); |
| 1290 | return -1; |
| 1291 | } |
| 1292 | return 0; |
| 1293 | } |
| 1294 | static void slotPowerCycle() |
| 1295 | { |
| 1296 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1297 | "Slot Power Cycle started\n"); |
| 1298 | slotPowerOff(); |
| 1299 | slotPowerCycleTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1300 | std::chrono::milliseconds(TimerMap["SlotPowerCycleMs"])); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1301 | slotPowerCycleTimer.async_wait([](const boost::system::error_code ec) { |
| 1302 | if (ec) |
| 1303 | { |
| 1304 | if (ec != boost::asio::error::operation_aborted) |
| 1305 | { |
| 1306 | std::string errMsg = |
| 1307 | "Slot Power cycle timer async_wait failed: " + ec.message(); |
| 1308 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1309 | errMsg.c_str()); |
| 1310 | } |
| 1311 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1312 | "Slot Power cycle timer canceled\n"); |
| 1313 | return; |
| 1314 | } |
| 1315 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1316 | "Slot Power cycle timer completed\n"); |
| 1317 | slotPowerOn(); |
| 1318 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1319 | "Slot Power Cycle Completed\n"); |
| 1320 | }); |
| 1321 | } |
| 1322 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1323 | static void gracefulPowerOff() |
| 1324 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1325 | assertGPIOForMs(powerOutConfig, TimerMap["PowerPulseMs"]); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | static void forcePowerOff() |
| 1329 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1330 | if (assertGPIOForMs(powerOutConfig, TimerMap["ForceOffPulseMs"]) < 0) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1331 | { |
| 1332 | return; |
| 1333 | } |
| 1334 | |
Jason M. Bills | c6961b6 | 2021-10-21 14:08:02 -0700 | [diff] [blame] | 1335 | // If the force off timer expires, then the power-button override failed |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1336 | gpioAssertTimer.async_wait([](const boost::system::error_code ec) { |
| 1337 | if (ec) |
| 1338 | { |
| 1339 | // operation_aborted is expected if timer is canceled before |
| 1340 | // completion. |
| 1341 | if (ec != boost::asio::error::operation_aborted) |
| 1342 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1343 | std::string errMsg = |
| 1344 | "Force power off async_wait failed: " + ec.message(); |
| 1345 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1346 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1347 | } |
| 1348 | return; |
| 1349 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1350 | |
| 1351 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Jason M. Bills | c6961b6 | 2021-10-21 14:08:02 -0700 | [diff] [blame] | 1352 | "Power-button override failed. Not sure what to do now."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1353 | }); |
| 1354 | } |
| 1355 | |
| 1356 | static void reset() |
| 1357 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1358 | assertGPIOForMs(resetOutConfig, TimerMap["ResetPulseMs"]); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | static void gracefulPowerOffTimerStart() |
| 1362 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1363 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1364 | "Graceful power-off timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1365 | gracefulPowerOffTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1366 | std::chrono::seconds(TimerMap["GracefulPowerOffS"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1367 | gracefulPowerOffTimer.async_wait([](const boost::system::error_code ec) { |
| 1368 | if (ec) |
| 1369 | { |
| 1370 | // operation_aborted is expected if timer is canceled before |
| 1371 | // completion. |
| 1372 | if (ec != boost::asio::error::operation_aborted) |
| 1373 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1374 | std::string errMsg = |
| 1375 | "Graceful power-off async_wait failed: " + ec.message(); |
| 1376 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1377 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1378 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1379 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1380 | "Graceful power-off timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1381 | return; |
| 1382 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1383 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1384 | "Graceful power-off timer completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1385 | sendPowerControlEvent(Event::gracefulPowerOffTimerExpired); |
| 1386 | }); |
| 1387 | } |
| 1388 | |
| 1389 | static void powerCycleTimerStart() |
| 1390 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1391 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1392 | "Power-cycle timer started"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 1393 | powerCycleTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1394 | std::chrono::milliseconds(TimerMap["PowerCycleMs"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1395 | powerCycleTimer.async_wait([](const boost::system::error_code ec) { |
| 1396 | if (ec) |
| 1397 | { |
| 1398 | // operation_aborted is expected if timer is canceled before |
| 1399 | // completion. |
| 1400 | if (ec != boost::asio::error::operation_aborted) |
| 1401 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1402 | std::string errMsg = |
| 1403 | "Power-cycle async_wait failed: " + ec.message(); |
| 1404 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1405 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1406 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1407 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1408 | "Power-cycle timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1409 | return; |
| 1410 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1411 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1412 | "Power-cycle timer completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1413 | sendPowerControlEvent(Event::powerCycleTimerExpired); |
| 1414 | }); |
| 1415 | } |
| 1416 | |
| 1417 | static void psPowerOKWatchdogTimerStart() |
| 1418 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1419 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1420 | "power supply power OK watchdog timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1421 | psPowerOKWatchdogTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1422 | std::chrono::milliseconds(TimerMap["PsPowerOKWatchdogMs"])); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1423 | psPowerOKWatchdogTimer.async_wait([](const boost::system::error_code ec) { |
| 1424 | if (ec) |
| 1425 | { |
| 1426 | // operation_aborted is expected if timer is canceled before |
| 1427 | // completion. |
| 1428 | if (ec != boost::asio::error::operation_aborted) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1429 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1430 | std::string errMsg = |
| 1431 | "power supply power OK watchdog async_wait failed: " + |
| 1432 | ec.message(); |
| 1433 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1434 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1435 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1436 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1437 | "power supply power OK watchdog timer canceled"); |
| 1438 | return; |
| 1439 | } |
| 1440 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1441 | "power supply power OK watchdog timer expired"); |
| 1442 | sendPowerControlEvent(Event::psPowerOKWatchdogTimerExpired); |
| 1443 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1446 | static void warmResetCheckTimerStart() |
| 1447 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1448 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1449 | "Warm reset check timer started"); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1450 | warmResetCheckTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1451 | std::chrono::milliseconds(TimerMap["WarmResetCheckMs"])); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1452 | warmResetCheckTimer.async_wait([](const boost::system::error_code ec) { |
| 1453 | if (ec) |
| 1454 | { |
| 1455 | // operation_aborted is expected if timer is canceled before |
| 1456 | // completion. |
| 1457 | if (ec != boost::asio::error::operation_aborted) |
| 1458 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1459 | std::string errMsg = |
| 1460 | "Warm reset check async_wait failed: " + ec.message(); |
| 1461 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1462 | errMsg.c_str()); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1463 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1464 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1465 | "Warm reset check timer canceled"); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1466 | return; |
| 1467 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1468 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1469 | "Warm reset check timer completed"); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1470 | sendPowerControlEvent(Event::warmResetDetected); |
| 1471 | }); |
| 1472 | } |
| 1473 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1474 | static void pohCounterTimerStart() |
| 1475 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1476 | phosphor::logging::log<phosphor::logging::level::INFO>("POH timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1477 | // Set the time-out as 1 hour, to align with POH command in ipmid |
| 1478 | pohCounterTimer.expires_after(std::chrono::hours(1)); |
| 1479 | pohCounterTimer.async_wait([](const boost::system::error_code& ec) { |
| 1480 | if (ec) |
| 1481 | { |
| 1482 | // operation_aborted is expected if timer is canceled before |
| 1483 | // completion. |
| 1484 | if (ec != boost::asio::error::operation_aborted) |
| 1485 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1486 | std::string errMsg = |
| 1487 | "POH timer async_wait failed: " + ec.message(); |
| 1488 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1489 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1490 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1491 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1492 | "POH timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1493 | return; |
| 1494 | } |
| 1495 | |
| 1496 | if (getHostState(powerState) != |
| 1497 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1498 | { |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | conn->async_method_call( |
| 1503 | [](boost::system::error_code ec, |
| 1504 | const std::variant<uint32_t>& pohCounterProperty) { |
| 1505 | if (ec) |
| 1506 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1507 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1508 | "error to get poh counter"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1509 | return; |
| 1510 | } |
| 1511 | const uint32_t* pohCounter = |
| 1512 | std::get_if<uint32_t>(&pohCounterProperty); |
| 1513 | if (pohCounter == nullptr) |
| 1514 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1515 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1516 | "unable to read poh counter"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | conn->async_method_call( |
| 1521 | [](boost::system::error_code ec) { |
| 1522 | if (ec) |
| 1523 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1524 | phosphor::logging::log< |
| 1525 | phosphor::logging::level::INFO>( |
| 1526 | "failed to set poh counter"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1527 | } |
| 1528 | }, |
| 1529 | "xyz.openbmc_project.Settings", |
| 1530 | "/xyz/openbmc_project/state/chassis0", |
| 1531 | "org.freedesktop.DBus.Properties", "Set", |
| 1532 | "xyz.openbmc_project.State.PowerOnHours", "POHCounter", |
| 1533 | std::variant<uint32_t>(*pohCounter + 1)); |
| 1534 | }, |
| 1535 | "xyz.openbmc_project.Settings", |
| 1536 | "/xyz/openbmc_project/state/chassis0", |
| 1537 | "org.freedesktop.DBus.Properties", "Get", |
| 1538 | "xyz.openbmc_project.State.PowerOnHours", "POHCounter"); |
| 1539 | |
| 1540 | pohCounterTimerStart(); |
| 1541 | }); |
| 1542 | } |
| 1543 | |
| 1544 | static void currentHostStateMonitor() |
| 1545 | { |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 1546 | if (getHostState(powerState) == |
| 1547 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1548 | { |
| 1549 | pohCounterTimerStart(); |
| 1550 | // Clear the restart cause set for the next restart |
| 1551 | clearRestartCause(); |
| 1552 | } |
| 1553 | else |
| 1554 | { |
| 1555 | pohCounterTimer.cancel(); |
| 1556 | // Set the restart cause set for this restart |
| 1557 | setRestartCause(); |
| 1558 | } |
| 1559 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1560 | static auto match = sdbusplus::bus::match::match( |
| 1561 | *conn, |
| 1562 | "type='signal',member='PropertiesChanged', " |
| 1563 | "interface='org.freedesktop.DBus.Properties', " |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1564 | "arg0='xyz.openbmc_project.State.Host'", |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1565 | [](sdbusplus::message::message& message) { |
| 1566 | std::string intfName; |
| 1567 | std::map<std::string, std::variant<std::string>> properties; |
| 1568 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1569 | try |
| 1570 | { |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1571 | message.read(intfName, properties); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1572 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 1573 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1574 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1575 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1576 | "Unable to read host state"); |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1577 | return; |
| 1578 | } |
| 1579 | if (properties.empty()) |
| 1580 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1581 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1582 | "ERROR: Empty PropertiesChanged signal received"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1583 | return; |
| 1584 | } |
| 1585 | |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1586 | // We only want to check for CurrentHostState |
| 1587 | if (properties.begin()->first != "CurrentHostState") |
| 1588 | { |
| 1589 | return; |
| 1590 | } |
| 1591 | std::string* currentHostState = |
| 1592 | std::get_if<std::string>(&(properties.begin()->second)); |
| 1593 | if (currentHostState == nullptr) |
| 1594 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1595 | std::string errMsg = |
| 1596 | properties.begin()->first + " property invalid"; |
| 1597 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1598 | errMsg.c_str()); |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | if (*currentHostState == |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1603 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1604 | { |
| 1605 | pohCounterTimerStart(); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1606 | // Clear the restart cause set for the next restart |
| 1607 | clearRestartCause(); |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 1608 | sd_journal_send("MESSAGE=Host system DC power is on", |
| 1609 | "PRIORITY=%i", LOG_INFO, |
| 1610 | "REDFISH_MESSAGE_ID=%s", |
| 1611 | "OpenBMC.0.1.DCPowerOn", NULL); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1612 | } |
| 1613 | else |
| 1614 | { |
| 1615 | pohCounterTimer.cancel(); |
AppaRao Puli | 8f5cb6a | 2020-01-14 02:47:29 +0530 | [diff] [blame] | 1616 | // POST_COMPLETE GPIO event is not working in some platforms |
| 1617 | // when power state is changed to OFF. This resulted in |
| 1618 | // 'OperatingSystemState' to stay at 'Standby', even though |
| 1619 | // system is OFF. Set 'OperatingSystemState' to 'Inactive' |
| 1620 | // if HostState is trurned to OFF. |
| 1621 | osIface->set_property("OperatingSystemState", |
| 1622 | std::string("Inactive")); |
| 1623 | |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1624 | // Set the restart cause set for this restart |
| 1625 | setRestartCause(); |
Rashmi RV | 89f6131 | 2020-01-22 15:41:50 +0530 | [diff] [blame] | 1626 | resetACBootProperty(); |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 1627 | sd_journal_send("MESSAGE=Host system DC power is off", |
| 1628 | "PRIORITY=%i", LOG_INFO, |
| 1629 | "REDFISH_MESSAGE_ID=%s", |
| 1630 | "OpenBMC.0.1.DCPowerOff", NULL); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1631 | } |
| 1632 | }); |
| 1633 | } |
| 1634 | |
| 1635 | static void sioPowerGoodWatchdogTimerStart() |
| 1636 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1637 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1638 | "SIO power good watchdog timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1639 | sioPowerGoodWatchdogTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1640 | std::chrono::milliseconds(TimerMap["SioPowerGoodWatchdogMs"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1641 | sioPowerGoodWatchdogTimer.async_wait( |
| 1642 | [](const boost::system::error_code ec) { |
| 1643 | if (ec) |
| 1644 | { |
| 1645 | // operation_aborted is expected if timer is canceled before |
| 1646 | // completion. |
| 1647 | if (ec != boost::asio::error::operation_aborted) |
| 1648 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1649 | std::string errMsg = |
| 1650 | "SIO power good watchdog async_wait failed: " + |
| 1651 | ec.message(); |
| 1652 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 1653 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1654 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1655 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1656 | "SIO power good watchdog timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1657 | return; |
| 1658 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1659 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1660 | "SIO power good watchdog timer completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1661 | sendPowerControlEvent(Event::sioPowerGoodWatchdogTimerExpired); |
| 1662 | }); |
| 1663 | } |
| 1664 | |
| 1665 | static void powerStateOn(const Event event) |
| 1666 | { |
| 1667 | logEvent(__FUNCTION__, event); |
| 1668 | switch (event) |
| 1669 | { |
| 1670 | case Event::psPowerOKDeAssert: |
| 1671 | setPowerState(PowerState::off); |
| 1672 | // DC power is unexpectedly lost, beep |
| 1673 | beep(beepPowerFail); |
| 1674 | break; |
| 1675 | case Event::sioS5Assert: |
| 1676 | setPowerState(PowerState::transitionToOff); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1677 | addRestartCause(RestartCause::softReset); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1678 | break; |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 1679 | #if USE_PLT_RST |
| 1680 | case Event::pltRstAssert: |
| 1681 | #else |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1682 | case Event::postCompleteDeAssert: |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 1683 | #endif |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1684 | setPowerState(PowerState::checkForWarmReset); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1685 | addRestartCause(RestartCause::softReset); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1686 | warmResetCheckTimerStart(); |
| 1687 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1688 | case Event::powerButtonPressed: |
| 1689 | setPowerState(PowerState::gracefulTransitionToOff); |
| 1690 | gracefulPowerOffTimerStart(); |
| 1691 | break; |
| 1692 | case Event::powerOffRequest: |
| 1693 | setPowerState(PowerState::transitionToOff); |
| 1694 | forcePowerOff(); |
| 1695 | break; |
| 1696 | case Event::gracefulPowerOffRequest: |
| 1697 | setPowerState(PowerState::gracefulTransitionToOff); |
| 1698 | gracefulPowerOffTimerStart(); |
| 1699 | gracefulPowerOff(); |
| 1700 | break; |
| 1701 | case Event::powerCycleRequest: |
| 1702 | setPowerState(PowerState::transitionToCycleOff); |
| 1703 | forcePowerOff(); |
| 1704 | break; |
| 1705 | case Event::gracefulPowerCycleRequest: |
| 1706 | setPowerState(PowerState::gracefulTransitionToCycleOff); |
| 1707 | gracefulPowerOffTimerStart(); |
| 1708 | gracefulPowerOff(); |
| 1709 | break; |
| 1710 | case Event::resetRequest: |
| 1711 | reset(); |
| 1712 | break; |
| 1713 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1714 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1715 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1716 | break; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | static void powerStateWaitForPSPowerOK(const Event event) |
| 1721 | { |
| 1722 | logEvent(__FUNCTION__, event); |
| 1723 | switch (event) |
| 1724 | { |
| 1725 | case Event::psPowerOKAssert: |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1726 | { |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1727 | // Cancel any GPIO assertions held during the transition |
| 1728 | gpioAssertTimer.cancel(); |
| 1729 | psPowerOKWatchdogTimer.cancel(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1730 | if (sioEnabled == true) |
| 1731 | { |
| 1732 | sioPowerGoodWatchdogTimerStart(); |
| 1733 | setPowerState(PowerState::waitForSIOPowerGood); |
| 1734 | } |
| 1735 | else |
| 1736 | { |
| 1737 | setPowerState(PowerState::on); |
| 1738 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1739 | break; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1740 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1741 | case Event::psPowerOKWatchdogTimerExpired: |
Jason M. Bills | 273d789 | 2020-06-17 14:46:57 -0700 | [diff] [blame] | 1742 | setPowerState(PowerState::off); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 1743 | psPowerOKFailedLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1744 | break; |
Vijay Khemka | 0eef6b6 | 2019-10-22 12:22:52 -0700 | [diff] [blame] | 1745 | case Event::sioPowerGoodAssert: |
| 1746 | psPowerOKWatchdogTimer.cancel(); |
| 1747 | setPowerState(PowerState::on); |
| 1748 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1749 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1750 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1751 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | static void powerStateWaitForSIOPowerGood(const Event event) |
| 1757 | { |
| 1758 | logEvent(__FUNCTION__, event); |
| 1759 | switch (event) |
| 1760 | { |
| 1761 | case Event::sioPowerGoodAssert: |
| 1762 | sioPowerGoodWatchdogTimer.cancel(); |
| 1763 | setPowerState(PowerState::on); |
| 1764 | break; |
| 1765 | case Event::sioPowerGoodWatchdogTimerExpired: |
Jason M. Bills | 273d789 | 2020-06-17 14:46:57 -0700 | [diff] [blame] | 1766 | setPowerState(PowerState::off); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 1767 | systemPowerGoodFailedLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1768 | break; |
| 1769 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1770 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1771 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | static void powerStateOff(const Event event) |
| 1777 | { |
| 1778 | logEvent(__FUNCTION__, event); |
| 1779 | switch (event) |
| 1780 | { |
| 1781 | case Event::psPowerOKAssert: |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1782 | { |
| 1783 | if (sioEnabled == true) |
| 1784 | { |
Jason M. Bills | 7e27d3d | 2021-09-08 14:51:09 -0700 | [diff] [blame] | 1785 | sioPowerGoodWatchdogTimerStart(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1786 | setPowerState(PowerState::waitForSIOPowerGood); |
| 1787 | } |
| 1788 | else |
| 1789 | { |
| 1790 | setPowerState(PowerState::on); |
| 1791 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1792 | break; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1793 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1794 | case Event::sioS5DeAssert: |
| 1795 | setPowerState(PowerState::waitForPSPowerOK); |
| 1796 | break; |
Jason M. Bills | 273d789 | 2020-06-17 14:46:57 -0700 | [diff] [blame] | 1797 | case Event::sioPowerGoodAssert: |
| 1798 | setPowerState(PowerState::on); |
| 1799 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1800 | case Event::powerButtonPressed: |
| 1801 | psPowerOKWatchdogTimerStart(); |
| 1802 | setPowerState(PowerState::waitForPSPowerOK); |
| 1803 | break; |
| 1804 | case Event::powerOnRequest: |
| 1805 | psPowerOKWatchdogTimerStart(); |
| 1806 | setPowerState(PowerState::waitForPSPowerOK); |
| 1807 | powerOn(); |
| 1808 | break; |
| 1809 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1810 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1811 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1812 | break; |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | static void powerStateTransitionToOff(const Event event) |
| 1817 | { |
| 1818 | logEvent(__FUNCTION__, event); |
| 1819 | switch (event) |
| 1820 | { |
| 1821 | case Event::psPowerOKDeAssert: |
| 1822 | // Cancel any GPIO assertions held during the transition |
| 1823 | gpioAssertTimer.cancel(); |
| 1824 | setPowerState(PowerState::off); |
| 1825 | break; |
| 1826 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1827 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1828 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1829 | break; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | static void powerStateGracefulTransitionToOff(const Event event) |
| 1834 | { |
| 1835 | logEvent(__FUNCTION__, event); |
| 1836 | switch (event) |
| 1837 | { |
| 1838 | case Event::psPowerOKDeAssert: |
| 1839 | gracefulPowerOffTimer.cancel(); |
| 1840 | setPowerState(PowerState::off); |
| 1841 | break; |
| 1842 | case Event::gracefulPowerOffTimerExpired: |
| 1843 | setPowerState(PowerState::on); |
| 1844 | break; |
Jason M. Bills | 22e0bec | 2021-03-04 12:54:04 -0800 | [diff] [blame] | 1845 | case Event::powerOffRequest: |
| 1846 | gracefulPowerOffTimer.cancel(); |
| 1847 | setPowerState(PowerState::transitionToOff); |
| 1848 | forcePowerOff(); |
| 1849 | break; |
| 1850 | case Event::powerCycleRequest: |
| 1851 | gracefulPowerOffTimer.cancel(); |
| 1852 | setPowerState(PowerState::transitionToCycleOff); |
| 1853 | forcePowerOff(); |
| 1854 | break; |
| 1855 | case Event::resetRequest: |
| 1856 | gracefulPowerOffTimer.cancel(); |
| 1857 | setPowerState(PowerState::on); |
| 1858 | reset(); |
| 1859 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1860 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1861 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1862 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1863 | break; |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | static void powerStateCycleOff(const Event event) |
| 1868 | { |
| 1869 | logEvent(__FUNCTION__, event); |
| 1870 | switch (event) |
| 1871 | { |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1872 | case Event::psPowerOKAssert: |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1873 | { |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1874 | powerCycleTimer.cancel(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1875 | if (sioEnabled == true) |
| 1876 | { |
Jason M. Bills | 7e27d3d | 2021-09-08 14:51:09 -0700 | [diff] [blame] | 1877 | sioPowerGoodWatchdogTimerStart(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1878 | setPowerState(PowerState::waitForSIOPowerGood); |
| 1879 | } |
| 1880 | else |
| 1881 | { |
| 1882 | setPowerState(PowerState::on); |
| 1883 | } |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1884 | break; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1885 | } |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1886 | case Event::sioS5DeAssert: |
| 1887 | powerCycleTimer.cancel(); |
| 1888 | setPowerState(PowerState::waitForPSPowerOK); |
| 1889 | break; |
| 1890 | case Event::powerButtonPressed: |
| 1891 | powerCycleTimer.cancel(); |
| 1892 | psPowerOKWatchdogTimerStart(); |
| 1893 | setPowerState(PowerState::waitForPSPowerOK); |
| 1894 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1895 | case Event::powerCycleTimerExpired: |
| 1896 | psPowerOKWatchdogTimerStart(); |
| 1897 | setPowerState(PowerState::waitForPSPowerOK); |
| 1898 | powerOn(); |
| 1899 | break; |
| 1900 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1901 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1902 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1903 | break; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | static void powerStateTransitionToCycleOff(const Event event) |
| 1908 | { |
| 1909 | logEvent(__FUNCTION__, event); |
| 1910 | switch (event) |
| 1911 | { |
| 1912 | case Event::psPowerOKDeAssert: |
| 1913 | // Cancel any GPIO assertions held during the transition |
| 1914 | gpioAssertTimer.cancel(); |
| 1915 | setPowerState(PowerState::cycleOff); |
| 1916 | powerCycleTimerStart(); |
| 1917 | break; |
| 1918 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1919 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1920 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1921 | break; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | static void powerStateGracefulTransitionToCycleOff(const Event event) |
| 1926 | { |
| 1927 | logEvent(__FUNCTION__, event); |
| 1928 | switch (event) |
| 1929 | { |
| 1930 | case Event::psPowerOKDeAssert: |
| 1931 | gracefulPowerOffTimer.cancel(); |
| 1932 | setPowerState(PowerState::cycleOff); |
| 1933 | powerCycleTimerStart(); |
| 1934 | break; |
| 1935 | case Event::gracefulPowerOffTimerExpired: |
| 1936 | setPowerState(PowerState::on); |
| 1937 | break; |
Jason M. Bills | 22e0bec | 2021-03-04 12:54:04 -0800 | [diff] [blame] | 1938 | case Event::powerOffRequest: |
| 1939 | gracefulPowerOffTimer.cancel(); |
| 1940 | setPowerState(PowerState::transitionToOff); |
| 1941 | forcePowerOff(); |
| 1942 | break; |
| 1943 | case Event::powerCycleRequest: |
| 1944 | gracefulPowerOffTimer.cancel(); |
| 1945 | setPowerState(PowerState::transitionToCycleOff); |
| 1946 | forcePowerOff(); |
| 1947 | break; |
| 1948 | case Event::resetRequest: |
| 1949 | gracefulPowerOffTimer.cancel(); |
| 1950 | setPowerState(PowerState::on); |
| 1951 | reset(); |
| 1952 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1953 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1954 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1955 | "No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1956 | break; |
| 1957 | } |
| 1958 | } |
| 1959 | |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1960 | static void powerStateCheckForWarmReset(const Event event) |
| 1961 | { |
| 1962 | logEvent(__FUNCTION__, event); |
| 1963 | switch (event) |
| 1964 | { |
| 1965 | case Event::sioS5Assert: |
| 1966 | warmResetCheckTimer.cancel(); |
| 1967 | setPowerState(PowerState::transitionToOff); |
| 1968 | break; |
| 1969 | case Event::warmResetDetected: |
| 1970 | setPowerState(PowerState::on); |
| 1971 | break; |
P.K. Lee | 344dae8 | 2019-11-27 16:35:05 +0800 | [diff] [blame] | 1972 | case Event::psPowerOKDeAssert: |
| 1973 | warmResetCheckTimer.cancel(); |
| 1974 | setPowerState(PowerState::off); |
| 1975 | // DC power is unexpectedly lost, beep |
| 1976 | beep(beepPowerFail); |
| 1977 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1978 | default: |
Jason M. Bills | 95f631c | 2020-06-17 14:04:29 -0700 | [diff] [blame] | 1979 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 1980 | "No action taken."); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1981 | break; |
| 1982 | } |
| 1983 | } |
| 1984 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1985 | static void psPowerOKHandler(bool state) |
| 1986 | { |
| 1987 | Event powerControlEvent = |
| 1988 | state ? Event::psPowerOKAssert : Event::psPowerOKDeAssert; |
| 1989 | sendPowerControlEvent(powerControlEvent); |
| 1990 | } |
| 1991 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1992 | static void sioPowerGoodHandler(bool state) |
| 1993 | { |
| 1994 | Event powerControlEvent = |
| 1995 | state ? Event::sioPowerGoodAssert : Event::sioPowerGoodDeAssert; |
| 1996 | sendPowerControlEvent(powerControlEvent); |
| 1997 | } |
| 1998 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1999 | static void sioOnControlHandler(bool state) |
| 2000 | { |
| 2001 | std::string logMsg = |
| 2002 | "SIO_ONCONTROL value changed: " + std::to_string(state); |
| 2003 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
| 2004 | } |
| 2005 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2006 | static void sioS5Handler(bool state) |
| 2007 | { |
| 2008 | Event powerControlEvent = state ? Event::sioS5DeAssert : Event::sioS5Assert; |
| 2009 | sendPowerControlEvent(powerControlEvent); |
| 2010 | } |
| 2011 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2012 | static void powerButtonHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2013 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2014 | powerButtonIface->set_property("ButtonPressed", !state); |
| 2015 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2016 | { |
| 2017 | powerButtonPressLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2018 | if (!powerButtonMask) |
| 2019 | { |
| 2020 | sendPowerControlEvent(Event::powerButtonPressed); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 2021 | addRestartCause(RestartCause::powerButton); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2022 | } |
| 2023 | else |
| 2024 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2025 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2026 | "power button press masked"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2027 | } |
| 2028 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2029 | } |
| 2030 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2031 | static void resetButtonHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2032 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2033 | resetButtonIface->set_property("ButtonPressed", !state); |
| 2034 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2035 | { |
| 2036 | resetButtonPressLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2037 | if (!resetButtonMask) |
| 2038 | { |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 2039 | sendPowerControlEvent(Event::resetButtonPressed); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 2040 | addRestartCause(RestartCause::resetButton); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2041 | } |
| 2042 | else |
| 2043 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2044 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2045 | "reset button press masked"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2046 | } |
| 2047 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2048 | } |
| 2049 | |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 2050 | #ifdef CHASSIS_SYSTEM_RESET |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2051 | static constexpr auto systemdBusname = "org.freedesktop.systemd1"; |
| 2052 | static constexpr auto systemdPath = "/org/freedesktop/systemd1"; |
| 2053 | static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| 2054 | static constexpr auto systemTargetName = "chassis-system-reset.target"; |
| 2055 | |
| 2056 | void systemReset() |
| 2057 | { |
| 2058 | conn->async_method_call( |
| 2059 | [](boost::system::error_code ec) { |
| 2060 | if (ec) |
| 2061 | { |
| 2062 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2063 | "Failed to call chassis system reset", |
| 2064 | phosphor::logging::entry("ERR=%s", ec.message().c_str())); |
| 2065 | } |
| 2066 | }, |
| 2067 | systemdBusname, systemdPath, systemdInterface, "StartUnit", |
| 2068 | systemTargetName, "replace"); |
| 2069 | } |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 2070 | #endif |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2071 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2072 | static void nmiSetEnableProperty(bool value) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2073 | { |
| 2074 | conn->async_method_call( |
| 2075 | [](boost::system::error_code ec) { |
| 2076 | if (ec) |
| 2077 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2078 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2079 | "failed to set NMI source"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2080 | } |
| 2081 | }, |
Chen Yugang | 303bd58 | 2019-11-01 08:45:06 +0800 | [diff] [blame] | 2082 | "xyz.openbmc_project.Settings", |
| 2083 | "/xyz/openbmc_project/Chassis/Control/NMISource", |
| 2084 | "org.freedesktop.DBus.Properties", "Set", |
| 2085 | "xyz.openbmc_project.Chassis.Control.NMISource", "Enabled", |
| 2086 | std::variant<bool>{value}); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | static void nmiReset(void) |
| 2090 | { |
| 2091 | static constexpr const uint8_t value = 1; |
| 2092 | const static constexpr int nmiOutPulseTimeMs = 200; |
| 2093 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2094 | phosphor::logging::log<phosphor::logging::level::INFO>("NMI out action"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2095 | nmiOutLine.set_value(value); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2096 | std::string logMsg = |
| 2097 | nmiOutConfig.lineName + " set to " + std::to_string(value); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2098 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2099 | gpioAssertTimer.expires_after(std::chrono::milliseconds(nmiOutPulseTimeMs)); |
| 2100 | gpioAssertTimer.async_wait([](const boost::system::error_code ec) { |
| 2101 | // restore the NMI_OUT GPIO line back to the opposite value |
| 2102 | nmiOutLine.set_value(!value); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2103 | std::string logMsg = nmiOutConfig.lineName + " released"; |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2104 | phosphor::logging::log<phosphor::logging::level::INFO>(logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2105 | if (ec) |
| 2106 | { |
| 2107 | // operation_aborted is expected if timer is canceled before |
| 2108 | // completion. |
| 2109 | if (ec != boost::asio::error::operation_aborted) |
| 2110 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2111 | std::string errMsg = nmiOutConfig.lineName + |
| 2112 | " async_wait failed: " + ec.message(); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2113 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2114 | errMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | }); |
| 2118 | // log to redfish |
| 2119 | nmiDiagIntLog(); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2120 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2121 | "NMI out action completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2122 | // reset Enable Property |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2123 | nmiSetEnableProperty(false); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | static void nmiSourcePropertyMonitor(void) |
| 2127 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2128 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2129 | "NMI Source Property Monitor"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2130 | |
| 2131 | static std::unique_ptr<sdbusplus::bus::match::match> nmiSourceMatch = |
| 2132 | std::make_unique<sdbusplus::bus::match::match>( |
| 2133 | *conn, |
| 2134 | "type='signal',interface='org.freedesktop.DBus.Properties'," |
Chen Yugang | 303bd58 | 2019-11-01 08:45:06 +0800 | [diff] [blame] | 2135 | "member='PropertiesChanged',arg0namespace='xyz.openbmc_project." |
| 2136 | "Chassis.Control." |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2137 | "NMISource'", |
| 2138 | [](sdbusplus::message::message& msg) { |
| 2139 | std::string interfaceName; |
| 2140 | boost::container::flat_map<std::string, |
| 2141 | std::variant<bool, std::string>> |
| 2142 | propertiesChanged; |
| 2143 | std::string state; |
| 2144 | bool value = true; |
| 2145 | try |
| 2146 | { |
| 2147 | msg.read(interfaceName, propertiesChanged); |
| 2148 | if (propertiesChanged.begin()->first == "Enabled") |
| 2149 | { |
| 2150 | value = |
| 2151 | std::get<bool>(propertiesChanged.begin()->second); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2152 | std::string logMsg = |
| 2153 | " NMI Enabled propertiesChanged value: " + |
| 2154 | std::to_string(value); |
| 2155 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2156 | logMsg.c_str()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2157 | nmiEnabled = value; |
| 2158 | if (nmiEnabled) |
| 2159 | { |
| 2160 | nmiReset(); |
| 2161 | } |
| 2162 | } |
| 2163 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 2164 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2165 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2166 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2167 | "Unable to read NMI source"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2168 | return; |
| 2169 | } |
| 2170 | }); |
| 2171 | } |
| 2172 | |
| 2173 | static void setNmiSource() |
| 2174 | { |
| 2175 | conn->async_method_call( |
| 2176 | [](boost::system::error_code ec) { |
| 2177 | if (ec) |
| 2178 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2179 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2180 | "failed to set NMI source"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2181 | } |
| 2182 | }, |
Chen Yugang | 303bd58 | 2019-11-01 08:45:06 +0800 | [diff] [blame] | 2183 | "xyz.openbmc_project.Settings", |
| 2184 | "/xyz/openbmc_project/Chassis/Control/NMISource", |
| 2185 | "org.freedesktop.DBus.Properties", "Set", |
| 2186 | "xyz.openbmc_project.Chassis.Control.NMISource", "BMCSource", |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2187 | std::variant<std::string>{ |
| 2188 | "xyz.openbmc_project.Chassis.Control.NMISource.BMCSourceSignal.FpBtn"}); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2189 | // set Enable Property |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2190 | nmiSetEnableProperty(true); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2193 | static void nmiButtonHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2194 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2195 | nmiButtonIface->set_property("ButtonPressed", !state); |
| 2196 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2197 | { |
| 2198 | nmiButtonPressLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2199 | if (nmiButtonMasked) |
| 2200 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2201 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2202 | "NMI button press masked"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2203 | } |
| 2204 | else |
| 2205 | { |
| 2206 | setNmiSource(); |
| 2207 | } |
| 2208 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2209 | } |
| 2210 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2211 | static void idButtonHandler(bool state) |
| 2212 | { |
| 2213 | idButtonIface->set_property("ButtonPressed", !state); |
| 2214 | } |
| 2215 | |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2216 | static void pltRstHandler(bool pltRst) |
| 2217 | { |
| 2218 | if (pltRst) |
| 2219 | { |
| 2220 | sendPowerControlEvent(Event::pltRstDeAssert); |
| 2221 | } |
| 2222 | else |
| 2223 | { |
| 2224 | sendPowerControlEvent(Event::pltRstAssert); |
| 2225 | } |
| 2226 | } |
| 2227 | |
Jason M. Bills | 9fd8ac2 | 2021-10-21 15:14:17 -0700 | [diff] [blame^] | 2228 | [[maybe_unused]] static void hostMiscHandler(sdbusplus::message::message& msg) |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2229 | { |
| 2230 | std::string interfaceName; |
| 2231 | boost::container::flat_map<std::string, std::variant<bool>> |
| 2232 | propertiesChanged; |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2233 | try |
| 2234 | { |
| 2235 | msg.read(interfaceName, propertiesChanged); |
| 2236 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 2237 | catch (const std::exception& e) |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2238 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2239 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2240 | "Unable to read Host Misc status"); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2241 | return; |
| 2242 | } |
| 2243 | if (propertiesChanged.empty()) |
| 2244 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2245 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2246 | "ERROR: Empty Host.Misc PropertiesChanged signal received"); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2247 | return; |
| 2248 | } |
| 2249 | |
| 2250 | for (auto& [property, value] : propertiesChanged) |
| 2251 | { |
| 2252 | if (property == "ESpiPlatformReset") |
| 2253 | { |
| 2254 | bool* pltRst = std::get_if<bool>(&value); |
| 2255 | if (pltRst == nullptr) |
| 2256 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2257 | std::string errMsg = property + " property invalid"; |
| 2258 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2259 | errMsg.c_str()); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2260 | return; |
| 2261 | } |
| 2262 | pltRstHandler(*pltRst); |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2267 | static void postCompleteHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2268 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2269 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2270 | { |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 2271 | sendPowerControlEvent(Event::postCompleteAssert); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2272 | osIface->set_property("OperatingSystemState", std::string("Standby")); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2273 | } |
| 2274 | else |
| 2275 | { |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 2276 | sendPowerControlEvent(Event::postCompleteDeAssert); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2277 | osIface->set_property("OperatingSystemState", std::string("Inactive")); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2278 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2279 | } |
| 2280 | |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2281 | static int loadConfigValues() |
| 2282 | { |
| 2283 | const std::string configFilePath = |
| 2284 | "/usr/share/x86-power-control/power-config-host" + power_control::node + |
| 2285 | ".json"; |
| 2286 | std::ifstream configFile(configFilePath.c_str()); |
| 2287 | if (!configFile.is_open()) |
| 2288 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2289 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2290 | "loadConfigValues : Cannot open config path"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2291 | return -1; |
| 2292 | } |
Zev Weiss | 1aa08b2 | 2021-09-15 17:06:20 -0500 | [diff] [blame] | 2293 | auto jsonData = nlohmann::json::parse(configFile, nullptr, true, true); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2294 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2295 | if (jsonData.is_discarded()) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2296 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2297 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2298 | "Power config readings JSON parser failure"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2299 | return -1; |
| 2300 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2301 | auto gpios = jsonData["gpio_configs"]; |
| 2302 | auto timers = jsonData["timing_configs"]; |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2303 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2304 | ConfigData* tempGpioData; |
| 2305 | |
| 2306 | for (nlohmann::json& gpioConfig : gpios) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2307 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2308 | if (!gpioConfig.contains("Name")) |
| 2309 | { |
| 2310 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2311 | "The 'Name' field must be defined in Json file"); |
| 2312 | return -1; |
| 2313 | } |
| 2314 | |
| 2315 | // Iterate through the powersignal map to check if the gpio json config |
| 2316 | // entry is valid |
| 2317 | std::string gpioName = gpioConfig["Name"]; |
| 2318 | auto signalMapIter = powerSignalMap.find(gpioName); |
| 2319 | if (signalMapIter == powerSignalMap.end()) |
| 2320 | { |
| 2321 | std::string errMsg = "Undefined Name : " + gpioName; |
| 2322 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2323 | errMsg.c_str()); |
| 2324 | return -1; |
| 2325 | } |
| 2326 | |
| 2327 | // assign the power signal name to the corresponding structure reference |
| 2328 | // from map then fillup the structure with coressponding json config |
| 2329 | // value |
| 2330 | tempGpioData = signalMapIter->second; |
| 2331 | tempGpioData->name = gpioName; |
| 2332 | |
| 2333 | if (!gpioConfig.contains("Type")) |
| 2334 | { |
| 2335 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2336 | "The \'Type\' field must be defined in Json file"); |
| 2337 | return -1; |
| 2338 | } |
| 2339 | |
| 2340 | std::string signalType = gpioConfig["Type"]; |
| 2341 | if (signalType == "GPIO") |
| 2342 | { |
| 2343 | tempGpioData->type = ConfigType::GPIO; |
| 2344 | } |
| 2345 | else if (signalType == "DBUS") |
| 2346 | { |
| 2347 | tempGpioData->type = ConfigType::DBUS; |
| 2348 | } |
| 2349 | else |
| 2350 | { |
| 2351 | std::string errMsg = "Undefined Type : " + signalType; |
| 2352 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2353 | errMsg.c_str()); |
| 2354 | return -1; |
| 2355 | } |
| 2356 | |
| 2357 | if (tempGpioData->type == ConfigType::GPIO) |
| 2358 | { |
| 2359 | if (gpioConfig.contains("LineName")) |
| 2360 | { |
| 2361 | tempGpioData->lineName = gpioConfig["LineName"]; |
| 2362 | } |
| 2363 | else |
| 2364 | { |
| 2365 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2366 | "The \'LineName\' field must be defined for GPIO configuration"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2367 | return -1; |
| 2368 | } |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2369 | if (gpioConfig.contains("Polarity")) |
| 2370 | { |
| 2371 | std::string polarity = gpioConfig["Polarity"]; |
| 2372 | if (polarity == "ActiveLow") |
| 2373 | { |
| 2374 | tempGpioData->polarity = false; |
| 2375 | } |
| 2376 | else if (polarity == "ActiveHigh") |
| 2377 | { |
| 2378 | tempGpioData->polarity = true; |
| 2379 | } |
| 2380 | else |
| 2381 | { |
| 2382 | std::string errMsg = |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2383 | "Polarity defined but not properly setup. Please only ActiveHigh or ActiveLow. Currently set to " + |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2384 | polarity; |
| 2385 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2386 | errMsg.c_str()); |
| 2387 | return -1; |
| 2388 | } |
| 2389 | } |
| 2390 | else |
| 2391 | { |
| 2392 | std::string errMsg = |
| 2393 | "Polarity field not found for " + tempGpioData->lineName; |
| 2394 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2395 | errMsg.c_str()); |
| 2396 | return -1; |
| 2397 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2398 | } |
| 2399 | else |
| 2400 | { |
| 2401 | // if dbus based gpio config is defined read and update the dbus |
| 2402 | // params corresponding to the gpio config instance |
| 2403 | for (auto& [key, dbusParamName] : dbusParams) |
| 2404 | { |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2405 | if (!gpioConfig.contains(dbusParamName)) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2406 | { |
| 2407 | std::string errMsg = |
| 2408 | "The " + dbusParamName + |
| 2409 | "field must be defined for Dbus configuration "; |
| 2410 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2411 | errMsg.c_str()); |
| 2412 | return -1; |
| 2413 | } |
| 2414 | } |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2415 | tempGpioData->dbusName = |
| 2416 | gpioConfig[dbusParams[DbusConfigType::name]]; |
| 2417 | tempGpioData->path = gpioConfig[dbusParams[DbusConfigType::path]]; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2418 | tempGpioData->interface = |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2419 | gpioConfig[dbusParams[DbusConfigType::interface]]; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2420 | tempGpioData->lineName = |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2421 | gpioConfig[dbusParams[DbusConfigType::property]]; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2422 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2423 | } |
| 2424 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2425 | // read and store the timer values from json config to Timer Map |
| 2426 | for (auto& [key, timerValue] : TimerMap) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2427 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2428 | if (timers.contains(key.c_str())) |
| 2429 | { |
| 2430 | timerValue = timers[key.c_str()]; |
| 2431 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2432 | } |
| 2433 | |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2434 | return 0; |
| 2435 | } |
Zev Weiss | a8f116a | 2021-09-01 21:08:30 -0500 | [diff] [blame] | 2436 | |
| 2437 | static bool getDbusMsgGPIOState(sdbusplus::message::message& msg, |
| 2438 | const std::string& lineName, bool& value) |
| 2439 | { |
| 2440 | std::string thresholdInterface; |
| 2441 | std::string event; |
| 2442 | boost::container::flat_map<std::string, std::variant<bool>> |
| 2443 | propertiesChanged; |
| 2444 | try |
| 2445 | { |
| 2446 | msg.read(thresholdInterface, propertiesChanged); |
| 2447 | if (propertiesChanged.empty()) |
| 2448 | { |
| 2449 | return false; |
| 2450 | } |
| 2451 | |
| 2452 | event = propertiesChanged.begin()->first; |
| 2453 | if (event.empty() || event != lineName) |
| 2454 | { |
| 2455 | return false; |
| 2456 | } |
| 2457 | |
| 2458 | value = std::get<bool>(propertiesChanged.begin()->second); |
| 2459 | return true; |
| 2460 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 2461 | catch (const std::exception& e) |
Zev Weiss | a8f116a | 2021-09-01 21:08:30 -0500 | [diff] [blame] | 2462 | { |
| 2463 | std::string logmsg = |
| 2464 | "exception while reading dbus property: " + lineName; |
| 2465 | phosphor::logging::log<phosphor::logging::level::ERR>(logmsg.c_str()); |
| 2466 | return false; |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | static sdbusplus::bus::match::match |
| 2471 | dbusGPIOMatcher(const ConfigData& cfg, std::function<void(bool)> onMatch) |
| 2472 | { |
| 2473 | auto pulseEventMatcherCallback = |
| 2474 | [&cfg, onMatch](sdbusplus::message::message& msg) { |
| 2475 | bool value = false; |
| 2476 | if (!getDbusMsgGPIOState(msg, cfg.lineName, value)) |
| 2477 | { |
| 2478 | return; |
| 2479 | } |
| 2480 | onMatch(value); |
| 2481 | }; |
| 2482 | |
| 2483 | return sdbusplus::bus::match::match( |
| 2484 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 2485 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 2486 | "PropertiesChanged',arg0='" + |
| 2487 | cfg.dbusName + "'", |
| 2488 | std::move(pulseEventMatcherCallback)); |
| 2489 | } |
| 2490 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2491 | int getProperty(ConfigData& configData) |
| 2492 | { |
| 2493 | auto method = conn->new_method_call( |
| 2494 | configData.dbusName.c_str(), configData.path.c_str(), |
| 2495 | "org.freedesktop.DBus.Properties", "Get"); |
| 2496 | method.append(configData.interface.c_str(), configData.lineName.c_str()); |
| 2497 | |
| 2498 | auto reply = conn->call(method); |
| 2499 | if (reply.is_method_error()) |
| 2500 | { |
| 2501 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2502 | "Error reading from Bus"); |
| 2503 | return -1; |
| 2504 | } |
| 2505 | std::variant<int> resp; |
| 2506 | reply.read(resp); |
| 2507 | auto respValue = std::get_if<int>(&resp); |
| 2508 | if (!respValue) |
| 2509 | { |
| 2510 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2511 | "Error reading response"); |
| 2512 | return -1; |
| 2513 | } |
| 2514 | return (*respValue); |
| 2515 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2516 | } // namespace power_control |
| 2517 | |
| 2518 | int main(int argc, char* argv[]) |
| 2519 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2520 | using namespace power_control; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2521 | |
| 2522 | if (argc > 1) |
| 2523 | { |
| 2524 | node = argv[1]; |
| 2525 | } |
| 2526 | std::string infoMsg = |
| 2527 | "Start Chassis power control service for host : " + node; |
| 2528 | phosphor::logging::log<phosphor::logging::level::INFO>(infoMsg.c_str()); |
| 2529 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2530 | conn = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2531 | |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2532 | // Load GPIO's through json config file |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2533 | if (loadConfigValues() == -1) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2534 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2535 | std::string errMsg = "Host" + node + ": " + "Error in Parsing..."; |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2536 | phosphor::logging::log<phosphor::logging::level::ERR>(errMsg.c_str()); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2537 | } |
Naveen Moses | ec972d8 | 2021-07-16 21:19:23 +0530 | [diff] [blame] | 2538 | /* Currently for single host based systems additional busname is added |
| 2539 | with "0" at the end of the name ex : xyz.openbmc_project.State.Host0. |
| 2540 | Going forward for single hosts the old bus name without zero numbering |
| 2541 | will be removed when all other applications adapted to the |
| 2542 | bus name with zero numbering (xyz.openbmc_project.State.Host0). */ |
| 2543 | |
| 2544 | if (node == "0") |
| 2545 | { |
| 2546 | // Request all the dbus names |
| 2547 | conn->request_name(hostDbusName.c_str()); |
| 2548 | conn->request_name(chassisDbusName.c_str()); |
| 2549 | conn->request_name(osDbusName.c_str()); |
| 2550 | conn->request_name(buttonDbusName.c_str()); |
| 2551 | conn->request_name(nmiDbusName.c_str()); |
| 2552 | conn->request_name(rstCauseDbusName.c_str()); |
| 2553 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2554 | |
Zev Weiss | c4005bd | 2021-09-01 22:30:23 -0500 | [diff] [blame] | 2555 | hostDbusName += node; |
| 2556 | chassisDbusName += node; |
| 2557 | osDbusName += node; |
| 2558 | buttonDbusName += node; |
| 2559 | nmiDbusName += node; |
| 2560 | rstCauseDbusName += node; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2561 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2562 | // Request all the dbus names |
| 2563 | conn->request_name(hostDbusName.c_str()); |
| 2564 | conn->request_name(chassisDbusName.c_str()); |
| 2565 | conn->request_name(osDbusName.c_str()); |
| 2566 | conn->request_name(buttonDbusName.c_str()); |
| 2567 | conn->request_name(nmiDbusName.c_str()); |
| 2568 | conn->request_name(rstCauseDbusName.c_str()); |
| 2569 | |
| 2570 | if (sioPwrGoodConfig.lineName.empty() || |
| 2571 | sioOnControlConfig.lineName.empty() || sioS5Config.lineName.empty()) |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2572 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2573 | sioEnabled = false; |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2574 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2575 | "SIO control GPIOs not defined, disable SIO support."); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2576 | } |
| 2577 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2578 | // Request PS_PWROK GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2579 | if (powerOkConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2580 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2581 | if (!requestGPIOEvents(powerOkConfig.lineName, psPowerOKHandler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2582 | psPowerOKLine, psPowerOKEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2583 | { |
| 2584 | return -1; |
| 2585 | } |
| 2586 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2587 | else if (powerOkConfig.type == ConfigType::DBUS) |
| 2588 | { |
| 2589 | |
| 2590 | static sdbusplus::bus::match::match powerOkEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2591 | power_control::dbusGPIOMatcher(powerOkConfig, psPowerOKHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2592 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2593 | else |
| 2594 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2595 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2596 | "PowerOk name should be configured from json config file"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2597 | return -1; |
| 2598 | } |
| 2599 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2600 | if (sioEnabled == true) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2601 | { |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2602 | // Request SIO_POWER_GOOD GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2603 | if (sioPwrGoodConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2604 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2605 | if (!requestGPIOEvents(sioPwrGoodConfig.lineName, |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2606 | sioPowerGoodHandler, sioPowerGoodLine, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2607 | sioPowerGoodEvent)) |
| 2608 | { |
| 2609 | return -1; |
| 2610 | } |
| 2611 | } |
| 2612 | else if (sioPwrGoodConfig.type == ConfigType::DBUS) |
| 2613 | { |
| 2614 | static sdbusplus::bus::match::match sioPwrGoodEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2615 | power_control::dbusGPIOMatcher(sioPwrGoodConfig, |
| 2616 | sioPowerGoodHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2617 | } |
| 2618 | else |
| 2619 | { |
| 2620 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2621 | "sioPwrGood name should be configured from json config file"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2622 | return -1; |
| 2623 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2624 | |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2625 | // Request SIO_ONCONTROL GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2626 | if (sioOnControlConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2627 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2628 | if (!requestGPIOEvents(sioOnControlConfig.lineName, |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2629 | sioOnControlHandler, sioOnControlLine, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2630 | sioOnControlEvent)) |
| 2631 | { |
| 2632 | return -1; |
| 2633 | } |
| 2634 | } |
| 2635 | else if (sioOnControlConfig.type == ConfigType::DBUS) |
| 2636 | { |
| 2637 | static sdbusplus::bus::match::match sioOnControlEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2638 | power_control::dbusGPIOMatcher(sioOnControlConfig, |
| 2639 | sioOnControlHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2640 | } |
| 2641 | else |
| 2642 | { |
| 2643 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2644 | "sioOnControl name should be configured from jsonconfig file\n"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2645 | return -1; |
| 2646 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2647 | |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2648 | // Request SIO_S5 GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2649 | if (sioS5Config.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2650 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2651 | if (!requestGPIOEvents(sioS5Config.lineName, sioS5Handler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2652 | sioS5Line, sioS5Event)) |
| 2653 | { |
| 2654 | return -1; |
| 2655 | } |
| 2656 | } |
| 2657 | else if (sioS5Config.type == ConfigType::DBUS) |
| 2658 | { |
| 2659 | static sdbusplus::bus::match::match sioS5EventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2660 | power_control::dbusGPIOMatcher(sioS5Config, sioS5Handler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2661 | } |
| 2662 | else |
| 2663 | { |
| 2664 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2665 | "sioS5 name should be configured from json config file"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2666 | return -1; |
| 2667 | } |
| 2668 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2669 | |
| 2670 | // Request POWER_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2671 | if (powerButtonConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2672 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2673 | if (!requestGPIOEvents(powerButtonConfig.lineName, powerButtonHandler, |
| 2674 | powerButtonLine, powerButtonEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2675 | { |
| 2676 | return -1; |
| 2677 | } |
| 2678 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2679 | else if (powerButtonConfig.type == ConfigType::DBUS) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2680 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2681 | static sdbusplus::bus::match::match powerButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2682 | power_control::dbusGPIOMatcher(powerButtonConfig, |
| 2683 | powerButtonHandler); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | // Request RESET_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2687 | if (resetButtonConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2688 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2689 | if (!requestGPIOEvents(resetButtonConfig.lineName, resetButtonHandler, |
| 2690 | resetButtonLine, resetButtonEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2691 | { |
| 2692 | return -1; |
| 2693 | } |
| 2694 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2695 | else if (resetButtonConfig.type == ConfigType::DBUS) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2696 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2697 | static sdbusplus::bus::match::match resetButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2698 | power_control::dbusGPIOMatcher(resetButtonConfig, |
| 2699 | resetButtonHandler); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | // Request NMI_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2703 | if (nmiButtonConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2704 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2705 | if (!nmiButtonConfig.lineName.empty()) |
| 2706 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2707 | requestGPIOEvents(nmiButtonConfig.lineName, nmiButtonHandler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2708 | nmiButtonLine, nmiButtonEvent); |
| 2709 | } |
| 2710 | } |
| 2711 | else if (nmiButtonConfig.type == ConfigType::DBUS) |
| 2712 | { |
| 2713 | static sdbusplus::bus::match::match nmiButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2714 | power_control::dbusGPIOMatcher(nmiButtonConfig, nmiButtonHandler); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2715 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2716 | |
| 2717 | // Request ID_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2718 | if (idButtonConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2719 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2720 | if (!idButtonConfig.lineName.empty()) |
| 2721 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2722 | requestGPIOEvents(idButtonConfig.lineName, idButtonHandler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2723 | idButtonLine, idButtonEvent); |
| 2724 | } |
| 2725 | } |
| 2726 | else if (idButtonConfig.type == ConfigType::DBUS) |
| 2727 | { |
| 2728 | static sdbusplus::bus::match::match idButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2729 | power_control::dbusGPIOMatcher(idButtonConfig, idButtonHandler); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2730 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2731 | |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2732 | #ifdef USE_PLT_RST |
| 2733 | sdbusplus::bus::match::match pltRstMatch( |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2734 | *conn, |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2735 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 2736 | "PropertiesChanged',arg0='xyz.openbmc_project.State.Host.Misc'", |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2737 | hostMiscHandler); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2738 | #endif |
| 2739 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2740 | // Request POST_COMPLETE GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2741 | if (postCompleteConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2742 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2743 | if (!requestGPIOEvents(postCompleteConfig.lineName, postCompleteHandler, |
| 2744 | postCompleteLine, postCompleteEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2745 | { |
| 2746 | return -1; |
| 2747 | } |
| 2748 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2749 | else if (postCompleteConfig.type == ConfigType::DBUS) |
| 2750 | { |
| 2751 | static sdbusplus::bus::match::match postCompleteEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2752 | power_control::dbusGPIOMatcher(postCompleteConfig, |
| 2753 | postCompleteHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2754 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2755 | else |
| 2756 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2757 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2758 | "postComplete name should be configured from json config file"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2759 | return -1; |
| 2760 | } |
| 2761 | |
| 2762 | // initialize NMI_OUT GPIO. |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2763 | if (!nmiOutConfig.lineName.empty()) |
| 2764 | { |
| 2765 | setGPIOOutput(nmiOutConfig.lineName, 0, nmiOutLine); |
| 2766 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2767 | |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2768 | // Initialize POWER_OUT and RESET_OUT GPIO. |
| 2769 | gpiod::line line; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2770 | if (!powerOutConfig.lineName.empty()) |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2771 | { |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2772 | if (!setGPIOOutput(powerOutConfig.lineName, !powerOutConfig.polarity, |
| 2773 | line)) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2774 | { |
| 2775 | return -1; |
| 2776 | } |
| 2777 | } |
| 2778 | else |
| 2779 | { |
| 2780 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2781 | "powerOut name should be configured from json config file"); |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2782 | return -1; |
| 2783 | } |
| 2784 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2785 | if (!resetOutConfig.lineName.empty()) |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2786 | { |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2787 | if (!setGPIOOutput(resetOutConfig.lineName, !resetOutConfig.polarity, |
| 2788 | line)) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2789 | { |
| 2790 | return -1; |
| 2791 | } |
| 2792 | } |
| 2793 | else |
| 2794 | { |
| 2795 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2796 | "ResetOut name should be configured from json config file"); |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2797 | return -1; |
| 2798 | } |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2799 | // Release line |
| 2800 | line.reset(); |
| 2801 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2802 | // Initialize the power state |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2803 | powerState = PowerState::off; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2804 | // Check power good |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2805 | |
| 2806 | if (powerOkConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2807 | { |
Jean-Marie Verdun | 61b4a5b | 2021-09-19 08:53:28 -0400 | [diff] [blame] | 2808 | if (psPowerOKLine.get_value() > 0 || |
Lei YU | a37c247 | 2021-09-26 15:57:12 +0800 | [diff] [blame] | 2809 | (sioEnabled && |
| 2810 | (sioPowerGoodLine.get_value() == sioPwrGoodConfig.polarity))) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2811 | { |
| 2812 | powerState = PowerState::on; |
| 2813 | } |
| 2814 | } |
| 2815 | else |
| 2816 | { |
| 2817 | if (getProperty(powerOkConfig)) |
| 2818 | { |
| 2819 | powerState = PowerState::on; |
| 2820 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2821 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2822 | // Initialize the power state storage |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2823 | if (initializePowerStateStorage() < 0) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2824 | { |
| 2825 | return -1; |
| 2826 | } |
| 2827 | |
| 2828 | // Check if we need to start the Power Restore policy |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2829 | powerRestorePolicyCheck(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2830 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2831 | if (nmiOutLine) |
| 2832 | nmiSourcePropertyMonitor(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2833 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2834 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2835 | "Initializing power state. "); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2836 | logStateTransition(powerState); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2837 | |
| 2838 | // Power Control Service |
| 2839 | sdbusplus::asio::object_server hostServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2840 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2841 | |
| 2842 | // Power Control Interface |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2843 | hostIface = |
| 2844 | hostServer.add_interface("/xyz/openbmc_project/state/host" + node, |
| 2845 | "xyz.openbmc_project.State.Host"); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2846 | // Interface for IPMI/Redfish initiated host state transitions |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2847 | hostIface->register_property( |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2848 | "RequestedHostTransition", |
| 2849 | std::string("xyz.openbmc_project.State.Host.Transition.Off"), |
| 2850 | [](const std::string& requested, std::string& resp) { |
| 2851 | if (requested == "xyz.openbmc_project.State.Host.Transition.Off") |
| 2852 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2853 | // if power button is masked, ignore this |
| 2854 | if (!powerButtonMask) |
| 2855 | { |
| 2856 | sendPowerControlEvent(Event::gracefulPowerOffRequest); |
| 2857 | addRestartCause(RestartCause::command); |
| 2858 | } |
| 2859 | else |
| 2860 | { |
| 2861 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2862 | "Power Button Masked."); |
| 2863 | throw std::invalid_argument("Transition Request Masked"); |
| 2864 | return 0; |
| 2865 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2866 | } |
| 2867 | else if (requested == |
| 2868 | "xyz.openbmc_project.State.Host.Transition.On") |
| 2869 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2870 | // if power button is masked, ignore this |
| 2871 | if (!powerButtonMask) |
| 2872 | { |
| 2873 | sendPowerControlEvent(Event::powerOnRequest); |
| 2874 | addRestartCause(RestartCause::command); |
| 2875 | } |
| 2876 | else |
| 2877 | { |
| 2878 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2879 | "Power Button Masked."); |
| 2880 | throw std::invalid_argument("Transition Request Masked"); |
| 2881 | return 0; |
| 2882 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2883 | } |
| 2884 | else if (requested == |
| 2885 | "xyz.openbmc_project.State.Host.Transition.Reboot") |
| 2886 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2887 | // if power button is masked, ignore this |
| 2888 | if (!powerButtonMask) |
| 2889 | { |
| 2890 | sendPowerControlEvent(Event::powerCycleRequest); |
| 2891 | addRestartCause(RestartCause::command); |
| 2892 | } |
| 2893 | else |
| 2894 | { |
| 2895 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2896 | "Power Button Masked."); |
| 2897 | throw std::invalid_argument("Transition Request Masked"); |
| 2898 | return 0; |
| 2899 | } |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2900 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2901 | else if ( |
| 2902 | requested == |
| 2903 | "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot") |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2904 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2905 | // if reset button is masked, ignore this |
| 2906 | if (!resetButtonMask) |
| 2907 | { |
| 2908 | sendPowerControlEvent(Event::gracefulPowerCycleRequest); |
| 2909 | addRestartCause(RestartCause::command); |
| 2910 | } |
| 2911 | else |
| 2912 | { |
| 2913 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2914 | "Reset Button Masked."); |
| 2915 | throw std::invalid_argument("Transition Request Masked"); |
| 2916 | return 0; |
| 2917 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2918 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2919 | else if ( |
| 2920 | requested == |
| 2921 | "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot") |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2922 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2923 | // if reset button is masked, ignore this |
| 2924 | if (!resetButtonMask) |
| 2925 | { |
| 2926 | sendPowerControlEvent(Event::resetRequest); |
| 2927 | addRestartCause(RestartCause::command); |
| 2928 | } |
| 2929 | else |
| 2930 | { |
| 2931 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2932 | "Reset Button Masked."); |
| 2933 | throw std::invalid_argument("Transition Request Masked"); |
| 2934 | return 0; |
| 2935 | } |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2936 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2937 | else |
| 2938 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2939 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 2940 | "Unrecognized host state transition request."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2941 | throw std::invalid_argument("Unrecognized Transition Request"); |
| 2942 | return 0; |
| 2943 | } |
| 2944 | resp = requested; |
| 2945 | return 1; |
| 2946 | }); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2947 | hostIface->register_property("CurrentHostState", |
| 2948 | std::string(getHostState(powerState))); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2949 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2950 | hostIface->initialize(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2951 | |
| 2952 | // Chassis Control Service |
| 2953 | sdbusplus::asio::object_server chassisServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2954 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2955 | |
| 2956 | // Chassis Control Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2957 | chassisIface = |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2958 | chassisServer.add_interface("/xyz/openbmc_project/state/chassis" + node, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2959 | "xyz.openbmc_project.State.Chassis"); |
| 2960 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2961 | chassisIface->register_property( |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2962 | "RequestedPowerTransition", |
| 2963 | std::string("xyz.openbmc_project.State.Chassis.Transition.Off"), |
| 2964 | [](const std::string& requested, std::string& resp) { |
| 2965 | if (requested == "xyz.openbmc_project.State.Chassis.Transition.Off") |
| 2966 | { |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2967 | // if power button is masked, ignore this |
| 2968 | if (!powerButtonMask) |
| 2969 | { |
| 2970 | sendPowerControlEvent(Event::powerOffRequest); |
| 2971 | addRestartCause(RestartCause::command); |
| 2972 | } |
| 2973 | else |
| 2974 | { |
| 2975 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2976 | "Power Button Masked."); |
| 2977 | throw std::invalid_argument("Transition Request Masked"); |
| 2978 | return 0; |
| 2979 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2980 | } |
| 2981 | else if (requested == |
| 2982 | "xyz.openbmc_project.State.Chassis.Transition.On") |
| 2983 | { |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2984 | // if power button is masked, ignore this |
| 2985 | if (!powerButtonMask) |
| 2986 | { |
| 2987 | sendPowerControlEvent(Event::powerOnRequest); |
| 2988 | addRestartCause(RestartCause::command); |
| 2989 | } |
| 2990 | else |
| 2991 | { |
| 2992 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 2993 | "Power Button Masked."); |
| 2994 | throw std::invalid_argument("Transition Request Masked"); |
| 2995 | return 0; |
| 2996 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2997 | } |
| 2998 | else if (requested == |
| 2999 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle") |
| 3000 | { |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 3001 | // if power button is masked, ignore this |
| 3002 | if (!powerButtonMask) |
| 3003 | { |
| 3004 | sendPowerControlEvent(Event::powerCycleRequest); |
| 3005 | addRestartCause(RestartCause::command); |
| 3006 | } |
| 3007 | else |
| 3008 | { |
| 3009 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3010 | "Power Button Masked."); |
| 3011 | throw std::invalid_argument("Transition Request Masked"); |
| 3012 | return 0; |
| 3013 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3014 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3015 | else |
| 3016 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3017 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 3018 | "Unrecognized chassis state transition request."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3019 | throw std::invalid_argument("Unrecognized Transition Request"); |
| 3020 | return 0; |
| 3021 | } |
| 3022 | resp = requested; |
| 3023 | return 1; |
| 3024 | }); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3025 | chassisIface->register_property("CurrentPowerState", |
| 3026 | std::string(getChassisState(powerState))); |
| 3027 | chassisIface->register_property("LastStateChangeTime", getCurrentTimeMs()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3028 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3029 | chassisIface->initialize(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3030 | |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 3031 | #ifdef CHASSIS_SYSTEM_RESET |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3032 | // Chassis System Service |
| 3033 | sdbusplus::asio::object_server chassisSysServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3034 | sdbusplus::asio::object_server(conn); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3035 | |
| 3036 | // Chassis System Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3037 | chassisSysIface = chassisSysServer.add_interface( |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3038 | "/xyz/openbmc_project/state/chassis_system0", |
| 3039 | "xyz.openbmc_project.State.Chassis"); |
| 3040 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3041 | chassisSysIface->register_property( |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3042 | "RequestedPowerTransition", |
| 3043 | std::string("xyz.openbmc_project.State.Chassis.Transition.On"), |
| 3044 | [](const std::string& requested, std::string& resp) { |
| 3045 | if (requested == |
| 3046 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle") |
| 3047 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3048 | systemReset(); |
| 3049 | addRestartCause(RestartCause::command); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3050 | } |
| 3051 | else |
| 3052 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3053 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 3054 | "Unrecognized chassis system state transition request."); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3055 | throw std::invalid_argument("Unrecognized Transition Request"); |
| 3056 | return 0; |
| 3057 | } |
| 3058 | resp = requested; |
| 3059 | return 1; |
| 3060 | }); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3061 | chassisSysIface->register_property( |
| 3062 | "CurrentPowerState", std::string(getChassisState(powerState))); |
| 3063 | chassisSysIface->register_property("LastStateChangeTime", |
| 3064 | getCurrentTimeMs()); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3065 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3066 | chassisSysIface->initialize(); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 3067 | |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 3068 | if (!slotPowerConfig.lineName.empty()) |
| 3069 | { |
| 3070 | if (!setGPIOOutput(slotPowerConfig.lineName, 1, slotPowerLine)) |
| 3071 | { |
| 3072 | return -1; |
| 3073 | } |
| 3074 | |
| 3075 | slotPowerState = SlotPowerState::off; |
| 3076 | if (slotPowerLine.get_value() > 0) |
| 3077 | { |
| 3078 | slotPowerState = SlotPowerState::on; |
| 3079 | } |
| 3080 | |
| 3081 | chassisSlotIface = chassisSysServer.add_interface( |
| 3082 | "/xyz/openbmc_project/state/chassis_system" + node, |
| 3083 | "xyz.openbmc_project.State.Chassis"); |
| 3084 | chassisSlotIface->register_property( |
| 3085 | "RequestedPowerTransition", |
| 3086 | std::string("xyz.openbmc_project.State.Chassis.Transition.On"), |
| 3087 | [](const std::string& requested, std::string& resp) { |
| 3088 | if (requested == |
| 3089 | "xyz.openbmc_project.State.Chassis.Transition.On") |
| 3090 | { |
| 3091 | slotPowerOn(); |
| 3092 | } |
| 3093 | else if (requested == |
| 3094 | "xyz.openbmc_project.State.Chassis.Transition.Off") |
| 3095 | { |
| 3096 | slotPowerOff(); |
| 3097 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 3098 | else if ( |
| 3099 | requested == |
| 3100 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle") |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 3101 | { |
| 3102 | slotPowerCycle(); |
| 3103 | } |
| 3104 | else |
| 3105 | { |
| 3106 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 3107 | "Unrecognized chassis system state transition request.\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 3108 | throw std::invalid_argument( |
| 3109 | "Unrecognized Transition Request"); |
| 3110 | return 0; |
| 3111 | } |
| 3112 | resp = requested; |
| 3113 | return 1; |
| 3114 | }); |
| 3115 | chassisSlotIface->register_property( |
| 3116 | "CurrentPowerState", std::string(getSlotState(slotPowerState))); |
| 3117 | chassisSlotIface->register_property("LastStateChangeTime", |
| 3118 | getCurrentTimeMs()); |
| 3119 | chassisSlotIface->initialize(); |
| 3120 | } |
| 3121 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3122 | // Buttons Service |
| 3123 | sdbusplus::asio::object_server buttonsServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3124 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3125 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3126 | if (!powerButtonConfig.lineName.empty()) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3127 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3128 | // Power Button Interface |
| 3129 | power_control::powerButtonIface = buttonsServer.add_interface( |
| 3130 | "/xyz/openbmc_project/chassis/buttons/power", |
| 3131 | "xyz.openbmc_project.Chassis.Buttons"); |
| 3132 | |
| 3133 | powerButtonIface->register_property( |
| 3134 | "ButtonMasked", false, [](const bool requested, bool& current) { |
| 3135 | if (requested) |
| 3136 | { |
| 3137 | if (powerButtonMask) |
| 3138 | { |
| 3139 | return 1; |
| 3140 | } |
Jean-Marie Verdun | 2c495cf | 2021-09-17 21:42:23 -0400 | [diff] [blame] | 3141 | if (!setGPIOOutput(powerOutConfig.lineName, |
| 3142 | !powerOutConfig.polarity, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3143 | powerButtonMask)) |
| 3144 | { |
| 3145 | throw std::runtime_error("Failed to request GPIO"); |
| 3146 | return 0; |
| 3147 | } |
| 3148 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3149 | "Power Button Masked."); |
| 3150 | } |
| 3151 | else |
| 3152 | { |
| 3153 | if (!powerButtonMask) |
| 3154 | { |
| 3155 | return 1; |
| 3156 | } |
| 3157 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3158 | "Power Button Un-masked"); |
| 3159 | powerButtonMask.reset(); |
| 3160 | } |
| 3161 | // Update the mask setting |
| 3162 | current = requested; |
| 3163 | return 1; |
| 3164 | }); |
| 3165 | |
| 3166 | // Check power button state |
| 3167 | bool powerButtonPressed; |
| 3168 | if (powerButtonConfig.type == ConfigType::GPIO) |
| 3169 | { |
| 3170 | powerButtonPressed = powerButtonLine.get_value() == 0; |
| 3171 | } |
| 3172 | else |
| 3173 | { |
| 3174 | powerButtonPressed = getProperty(powerButtonConfig) == 0; |
| 3175 | } |
| 3176 | |
| 3177 | powerButtonIface->register_property("ButtonPressed", |
| 3178 | powerButtonPressed); |
| 3179 | |
| 3180 | powerButtonIface->initialize(); |
| 3181 | } |
| 3182 | |
| 3183 | if (!resetButtonConfig.lineName.empty()) |
| 3184 | { |
| 3185 | // Reset Button Interface |
| 3186 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3187 | resetButtonIface = buttonsServer.add_interface( |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3188 | "/xyz/openbmc_project/chassis/buttons/reset", |
| 3189 | "xyz.openbmc_project.Chassis.Buttons"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3190 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3191 | resetButtonIface->register_property( |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3192 | "ButtonMasked", false, [](const bool requested, bool& current) { |
| 3193 | if (requested) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3194 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3195 | if (resetButtonMask) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3196 | { |
| 3197 | return 1; |
| 3198 | } |
Jean-Marie Verdun | 2c495cf | 2021-09-17 21:42:23 -0400 | [diff] [blame] | 3199 | if (!setGPIOOutput(resetOutConfig.lineName, |
| 3200 | !resetOutConfig.polarity, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3201 | resetButtonMask)) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3202 | { |
| 3203 | throw std::runtime_error("Failed to request GPIO"); |
| 3204 | return 0; |
| 3205 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3206 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3207 | "Reset Button Masked."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3208 | } |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3209 | else |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3210 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3211 | if (!resetButtonMask) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3212 | { |
| 3213 | return 1; |
| 3214 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3215 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3216 | "Reset Button Un-masked"); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3217 | resetButtonMask.reset(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3218 | } |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3219 | // Update the mask setting |
| 3220 | current = requested; |
| 3221 | return 1; |
| 3222 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3223 | |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3224 | // Check reset button state |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3225 | bool resetButtonPressed; |
| 3226 | if (resetButtonConfig.type == ConfigType::GPIO) |
| 3227 | { |
| 3228 | resetButtonPressed = resetButtonLine.get_value() == 0; |
| 3229 | } |
| 3230 | else |
| 3231 | { |
| 3232 | resetButtonPressed = getProperty(resetButtonConfig) == 0; |
| 3233 | } |
| 3234 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3235 | resetButtonIface->register_property("ButtonPressed", |
| 3236 | resetButtonPressed); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3237 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3238 | resetButtonIface->initialize(); |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3239 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3240 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3241 | if (nmiButtonLine) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3242 | { |
| 3243 | // NMI Button Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3244 | nmiButtonIface = buttonsServer.add_interface( |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3245 | "/xyz/openbmc_project/chassis/buttons/nmi", |
| 3246 | "xyz.openbmc_project.Chassis.Buttons"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3247 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3248 | nmiButtonIface->register_property( |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3249 | "ButtonMasked", false, [](const bool requested, bool& current) { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3250 | if (nmiButtonMasked == requested) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3251 | { |
| 3252 | // NMI button mask is already set as requested, so no change |
| 3253 | return 1; |
| 3254 | } |
| 3255 | if (requested) |
| 3256 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3257 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3258 | "NMI Button Masked."); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3259 | nmiButtonMasked = true; |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3260 | } |
| 3261 | else |
| 3262 | { |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3263 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3264 | "NMI Button Un-masked."); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3265 | nmiButtonMasked = false; |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3266 | } |
| 3267 | // Update the mask setting |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3268 | current = nmiButtonMasked; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3269 | return 1; |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3270 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3271 | |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3272 | // Check NMI button state |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3273 | bool nmiButtonPressed; |
| 3274 | if (nmiButtonConfig.type == ConfigType::GPIO) |
| 3275 | { |
| 3276 | nmiButtonPressed = nmiButtonLine.get_value() == 0; |
| 3277 | } |
| 3278 | else |
| 3279 | { |
| 3280 | nmiButtonPressed = getProperty(nmiButtonConfig) == 0; |
| 3281 | } |
| 3282 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3283 | nmiButtonIface->register_property("ButtonPressed", nmiButtonPressed); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3284 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3285 | nmiButtonIface->initialize(); |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3286 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3287 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3288 | if (nmiOutLine) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3289 | { |
| 3290 | // NMI out Service |
| 3291 | sdbusplus::asio::object_server nmiOutServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3292 | sdbusplus::asio::object_server(conn); |
Chen Yugang | 174ec66 | 2019-08-19 19:58:49 +0800 | [diff] [blame] | 3293 | |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3294 | // NMI out Interface |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3295 | nmiOutIface = nmiOutServer.add_interface( |
| 3296 | "/xyz/openbmc_project/control/host" + node + "/nmi", |
| 3297 | "xyz.openbmc_project.Control.Host.NMI"); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3298 | nmiOutIface->register_method("NMI", nmiReset); |
| 3299 | nmiOutIface->initialize(); |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3300 | } |
Chen Yugang | 174ec66 | 2019-08-19 19:58:49 +0800 | [diff] [blame] | 3301 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3302 | if (idButtonLine) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3303 | { |
| 3304 | // ID Button Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3305 | idButtonIface = buttonsServer.add_interface( |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3306 | "/xyz/openbmc_project/chassis/buttons/id", |
| 3307 | "xyz.openbmc_project.Chassis.Buttons"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3308 | |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3309 | // Check ID button state |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3310 | bool idButtonPressed; |
| 3311 | if (idButtonConfig.type == ConfigType::GPIO) |
| 3312 | { |
| 3313 | idButtonPressed = idButtonLine.get_value() == 0; |
| 3314 | } |
| 3315 | else |
| 3316 | { |
| 3317 | idButtonPressed = getProperty(idButtonConfig) == 0; |
| 3318 | } |
| 3319 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3320 | idButtonIface->register_property("ButtonPressed", idButtonPressed); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3321 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3322 | idButtonIface->initialize(); |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3323 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3324 | |
| 3325 | // OS State Service |
| 3326 | sdbusplus::asio::object_server osServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3327 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3328 | |
| 3329 | // OS State Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3330 | osIface = osServer.add_interface( |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3331 | "/xyz/openbmc_project/state/os", |
| 3332 | "xyz.openbmc_project.State.OperatingSystem.Status"); |
| 3333 | |
| 3334 | // Get the initial OS state based on POST complete |
| 3335 | // 0: Asserted, OS state is "Standby" (ready to boot) |
| 3336 | // 1: De-Asserted, OS state is "Inactive" |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3337 | std::string osState; |
| 3338 | if (postCompleteConfig.type == ConfigType::GPIO) |
| 3339 | { |
| 3340 | osState = postCompleteLine.get_value() > 0 ? "Inactive" : "Standby"; |
| 3341 | } |
| 3342 | else |
| 3343 | { |
| 3344 | osState = getProperty(postCompleteConfig) > 0 ? "Inactive" : "Standby"; |
| 3345 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3346 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3347 | osIface->register_property("OperatingSystemState", std::string(osState)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3348 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3349 | osIface->initialize(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3350 | |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3351 | // Restart Cause Service |
| 3352 | sdbusplus::asio::object_server restartCauseServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3353 | sdbusplus::asio::object_server(conn); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3354 | |
| 3355 | // Restart Cause Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3356 | restartCauseIface = restartCauseServer.add_interface( |
Naveen Moses | ec972d8 | 2021-07-16 21:19:23 +0530 | [diff] [blame] | 3357 | "/xyz/openbmc_project/control/host" + node + "/restart_cause", |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3358 | "xyz.openbmc_project.Control.Host.RestartCause"); |
| 3359 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3360 | restartCauseIface->register_property( |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3361 | "RestartCause", |
| 3362 | std::string("xyz.openbmc_project.State.Host.RestartCause.Unknown")); |
| 3363 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3364 | restartCauseIface->register_property( |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3365 | "RequestedRestartCause", |
| 3366 | std::string("xyz.openbmc_project.State.Host.RestartCause.Unknown"), |
| 3367 | [](const std::string& requested, std::string& resp) { |
| 3368 | if (requested == |
| 3369 | "xyz.openbmc_project.State.Host.RestartCause.WatchdogTimer") |
| 3370 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3371 | addRestartCause(RestartCause::watchdog); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3372 | } |
| 3373 | else |
| 3374 | { |
| 3375 | throw std::invalid_argument( |
| 3376 | "Unrecognized RestartCause Request"); |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 3380 | std::string logMsg = "RestartCause requested: " + requested; |
| 3381 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 3382 | logMsg.c_str()); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3383 | resp = requested; |
| 3384 | return 1; |
| 3385 | }); |
| 3386 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3387 | restartCauseIface->initialize(); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3388 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3389 | currentHostStateMonitor(); |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 3390 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3391 | io.run(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3392 | |
| 3393 | return 0; |
| 3394 | } |