Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 1 | #include "powermode.hpp" |
| 2 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame^] | 3 | #include "elog-errors.hpp" |
| 4 | |
| 5 | #include <fcntl.h> |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 6 | #include <fmt/core.h> |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame^] | 7 | #include <sys/ioctl.h> |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 8 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 9 | #include <com/ibm/Host/Target/server.hpp> |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame^] | 10 | #include <org/open_power/OCC/Device/error.hpp> |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame^] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Control/Power/Mode/server.hpp> |
| 14 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 15 | #include <cassert> |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 16 | #include <fstream> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 17 | #include <regex> |
| 18 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 19 | namespace open_power |
| 20 | { |
| 21 | namespace occ |
| 22 | { |
| 23 | namespace powermode |
| 24 | { |
| 25 | |
| 26 | using namespace phosphor::logging; |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 27 | using namespace std::literals::string_literals; |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame^] | 28 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 29 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 30 | using Mode = sdbusplus::xyz::openbmc_project::Control::Power::server::Mode; |
| 31 | |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 32 | // Set the Master OCC |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 33 | void PowerMode::setMasterOcc(const std::string& masterOccPath) |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 34 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 35 | if (masterOccSet) |
| 36 | { |
| 37 | if (masterOccPath != path) |
| 38 | { |
| 39 | log<level::ERR>( |
| 40 | fmt::format( |
| 41 | "PowerMode::setMasterOcc: Master changed (was OCC{}, {})", |
| 42 | occInstance, masterOccPath) |
| 43 | .c_str()); |
| 44 | if (occCmd) |
| 45 | { |
| 46 | occCmd.reset(); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | path = masterOccPath; |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 51 | occInstance = path.back() - '0'; |
| 52 | log<level::DEBUG>(fmt::format("PowerMode::setMasterOcc(OCC{}, {})", |
| 53 | occInstance, path.c_str()) |
| 54 | .c_str()); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 55 | if (!occCmd) |
| 56 | { |
| 57 | occCmd = std::make_unique<open_power::occ::OccCommand>(occInstance, |
| 58 | path.c_str()); |
| 59 | } |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 60 | masterOccSet = true; |
| 61 | }; |
| 62 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 63 | // Called when DBus power mode gets changed |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 64 | void PowerMode::modeChanged(sdbusplus::message::message& msg) |
| 65 | { |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 66 | std::map<std::string, std::variant<std::string>> properties{}; |
| 67 | std::string interface; |
| 68 | std::string propVal; |
| 69 | msg.read(interface, properties); |
| 70 | const auto modeEntry = properties.find(POWER_MODE_PROP); |
| 71 | if (modeEntry != properties.end()) |
| 72 | { |
| 73 | auto modeEntryValue = modeEntry->second; |
| 74 | propVal = std::get<std::string>(modeEntryValue); |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 75 | SysPwrMode newMode = convertStringToMode(propVal); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 76 | if (newMode != SysPwrMode::NO_CHANGE) |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 77 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 78 | // Update persisted data with new mode |
| 79 | persistedData.updateMode(newMode, 0); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 80 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 81 | log<level::INFO>( |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 82 | fmt::format("DBus Power Mode Changed: {}", propVal).c_str()); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 83 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 84 | // Send mode change to OCC |
| 85 | sendModeChange(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Called from OCC PassThrough interface (via CE login / BMC command line) |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 91 | bool PowerMode::setMode(const SysPwrMode newMode, const uint16_t oemModeData) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 92 | { |
| 93 | if (updateDbusMode(newMode) == false) |
| 94 | { |
| 95 | // Unsupported mode |
| 96 | return false; |
| 97 | } |
| 98 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 99 | // Save mode |
| 100 | persistedData.updateMode(newMode, oemModeData); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 101 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 102 | // Send mode change to OCC |
| 103 | if (sendModeChange() != CmdStatus::SUCCESS) |
| 104 | { |
| 105 | // Mode change failed |
| 106 | return false; |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 107 | } |
| 108 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 109 | return true; |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Convert PowerMode string to OCC SysPwrMode |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 113 | // Returns NO_CHANGE if OEM or unsupported mode |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 114 | SysPwrMode convertStringToMode(const std::string& i_modeString) |
| 115 | { |
| 116 | SysPwrMode pmode = SysPwrMode::NO_CHANGE; |
| 117 | |
| 118 | Mode::PowerMode mode = Mode::convertPowerModeFromString(i_modeString); |
| 119 | if (mode == Mode::PowerMode::MaximumPerformance) |
| 120 | { |
| 121 | pmode = SysPwrMode::MAX_PERF; |
| 122 | } |
| 123 | else if (mode == Mode::PowerMode::PowerSaving) |
| 124 | { |
| 125 | pmode = SysPwrMode::POWER_SAVING; |
| 126 | } |
| 127 | else if (mode == Mode::PowerMode::Static) |
| 128 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 129 | pmode = SysPwrMode::STATIC; |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 133 | if (mode != Mode::PowerMode::OEM) |
| 134 | { |
| 135 | log<level::ERR>( |
| 136 | fmt::format( |
| 137 | "convertStringToMode: Invalid Power Mode specified: {}", |
| 138 | i_modeString) |
| 139 | .c_str()); |
| 140 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return pmode; |
| 144 | } |
| 145 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 146 | // Check if Hypervisor target is PowerVM |
| 147 | bool isPowerVM() |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 148 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 149 | namespace Hyper = sdbusplus::com::ibm::Host::server; |
| 150 | constexpr auto HYPE_PATH = "/com/ibm/host0/hypervisor"; |
| 151 | constexpr auto HYPE_INTERFACE = "com.ibm.Host.Target"; |
| 152 | constexpr auto HYPE_PROP = "Target"; |
| 153 | |
| 154 | bool powerVmTarget = false; |
| 155 | |
| 156 | // This will throw exception on failure |
| 157 | auto& bus = utils::getBus(); |
| 158 | auto service = utils::getService(HYPE_PATH, HYPE_INTERFACE); |
| 159 | auto method = bus.new_method_call(service.c_str(), HYPE_PATH, |
| 160 | "org.freedesktop.DBus.Properties", "Get"); |
| 161 | method.append(HYPE_INTERFACE, HYPE_PROP); |
| 162 | auto reply = bus.call(method); |
| 163 | |
| 164 | std::variant<std::string> hyperEntryValue; |
| 165 | reply.read(hyperEntryValue); |
| 166 | auto propVal = std::get<std::string>(hyperEntryValue); |
| 167 | if (Hyper::Target::convertHypervisorFromString(propVal) == |
| 168 | Hyper::Target::Hypervisor::PowerVM) |
| 169 | { |
| 170 | powerVmTarget = true; |
| 171 | } |
| 172 | |
| 173 | log<level::DEBUG>( |
| 174 | fmt::format("isPowerVM returning {}", powerVmTarget).c_str()); |
| 175 | |
| 176 | return powerVmTarget; |
| 177 | } |
| 178 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 179 | // Initialize persistent data and return true if successful |
| 180 | bool PowerMode::initPersistentData() |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 181 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 182 | if (!persistedData.modeAvailable()) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 183 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 184 | // Read the default mode |
| 185 | SysPwrMode currentMode; |
| 186 | if (!getDefaultMode(currentMode)) |
| 187 | { |
| 188 | // Unable to read defaults |
| 189 | return false; |
| 190 | } |
| 191 | log<level::INFO>( |
| 192 | fmt::format("PowerMode::initPersistentData: Using default mode: {}", |
| 193 | currentMode) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 194 | .c_str()); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 195 | |
| 196 | // Save default mode as current mode |
| 197 | persistedData.updateMode(currentMode, 0); |
| 198 | |
| 199 | // Write default mode to DBus |
| 200 | updateDbusMode(currentMode); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 203 | if (!persistedData.ipsAvailable()) |
| 204 | { |
Chris Cain | cde7bea | 2022-01-28 15:54:24 -0600 | [diff] [blame] | 205 | // Read the default IPS parameters, write persistent file and update |
| 206 | // DBus |
| 207 | return useDefaultIPSParms(); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | // Get the requested power mode and return true if successful |
| 213 | bool PowerMode::getMode(SysPwrMode& currentMode, uint16_t& oemModeData) |
| 214 | { |
| 215 | currentMode = SysPwrMode::NO_CHANGE; |
| 216 | oemModeData = 0; |
| 217 | |
| 218 | if (!persistedData.getMode(currentMode, oemModeData)) |
| 219 | { |
| 220 | // Persistent data not initialized, read defaults and update DBus |
| 221 | if (!initPersistentData()) |
| 222 | { |
| 223 | // Unable to read defaults from entity manager yet |
| 224 | return false; |
| 225 | } |
| 226 | return persistedData.getMode(currentMode, oemModeData); |
| 227 | } |
| 228 | |
| 229 | return true; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Set the power mode on DBus |
| 233 | bool PowerMode::updateDbusMode(const SysPwrMode newMode) |
| 234 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 235 | if (!VALID_POWER_MODE_SETTING(newMode) && |
| 236 | !VALID_OEM_POWER_MODE_SETTING(newMode)) |
| 237 | { |
| 238 | log<level::ERR>( |
| 239 | fmt::format( |
| 240 | "PowerMode::updateDbusMode - Requested power mode not supported: {}", |
| 241 | newMode) |
| 242 | .c_str()); |
| 243 | return false; |
| 244 | } |
| 245 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 246 | // Convert mode for DBus |
| 247 | ModeInterface::PowerMode dBusMode; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 248 | switch (newMode) |
| 249 | { |
| 250 | case SysPwrMode::STATIC: |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 251 | dBusMode = Mode::PowerMode::Static; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 252 | break; |
| 253 | case SysPwrMode::POWER_SAVING: |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 254 | dBusMode = Mode::PowerMode::PowerSaving; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 255 | break; |
| 256 | case SysPwrMode::MAX_PERF: |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 257 | dBusMode = Mode::PowerMode::MaximumPerformance; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 258 | break; |
| 259 | default: |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 260 | dBusMode = Mode::PowerMode::OEM; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 261 | } |
| 262 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 263 | // true = skip update signal |
| 264 | ModeInterface::setPropertyByName(POWER_MODE_PROP, dBusMode, true); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 265 | |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | // Send mode change request to the master OCC |
| 270 | CmdStatus PowerMode::sendModeChange() |
| 271 | { |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 272 | CmdStatus status; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 273 | |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 274 | if (!masterActive || !masterOccSet) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 275 | { |
| 276 | // Nothing to do |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 277 | log<level::DEBUG>("PowerMode::sendModeChange: OCC master not active"); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 278 | return CmdStatus::SUCCESS; |
| 279 | } |
| 280 | |
| 281 | if (!isPowerVM()) |
| 282 | { |
| 283 | // Mode change is only supported on PowerVM systems |
| 284 | log<level::DEBUG>( |
| 285 | "PowerMode::sendModeChange: MODE CHANGE does not get sent on non-PowerVM systems"); |
| 286 | return CmdStatus::SUCCESS; |
| 287 | } |
| 288 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 289 | SysPwrMode newMode; |
| 290 | uint16_t oemModeData = 0; |
| 291 | getMode(newMode, oemModeData); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 292 | |
| 293 | if (VALID_POWER_MODE_SETTING(newMode) || |
| 294 | VALID_OEM_POWER_MODE_SETTING(newMode)) |
| 295 | { |
| 296 | std::vector<std::uint8_t> cmd, rsp; |
| 297 | cmd.reserve(9); |
| 298 | cmd.push_back(uint8_t(CmdType::SET_MODE_AND_STATE)); |
| 299 | cmd.push_back(0x00); // Data Length (2 bytes) |
| 300 | cmd.push_back(0x06); |
| 301 | cmd.push_back(0x30); // Data (Version) |
| 302 | cmd.push_back(uint8_t(OccState::NO_CHANGE)); |
| 303 | cmd.push_back(uint8_t(newMode)); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 304 | cmd.push_back(oemModeData >> 8); // Mode Data (Freq Point) |
| 305 | cmd.push_back(oemModeData & 0xFF); // |
| 306 | cmd.push_back(0x00); // reserved |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 307 | log<level::INFO>( |
| 308 | fmt::format( |
| 309 | "PowerMode::sendModeChange: SET_MODE({},{}) command to OCC{} ({} bytes)", |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 310 | newMode, oemModeData, occInstance, cmd.size()) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 311 | .c_str()); |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 312 | status = occCmd->send(cmd, rsp); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 313 | if (status == CmdStatus::SUCCESS) |
| 314 | { |
| 315 | if (rsp.size() == 5) |
| 316 | { |
| 317 | if (RspStatus::SUCCESS != RspStatus(rsp[2])) |
| 318 | { |
| 319 | log<level::ERR>( |
| 320 | fmt::format( |
| 321 | "PowerMode::sendModeChange: SET MODE failed with status 0x{:02X}", |
| 322 | rsp[2]) |
| 323 | .c_str()); |
| 324 | dump_hex(rsp); |
| 325 | status = CmdStatus::FAILURE; |
| 326 | } |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | log<level::ERR>( |
| 331 | "PowerMode::sendModeChange: INVALID SET MODE response"); |
| 332 | dump_hex(rsp); |
| 333 | status = CmdStatus::FAILURE; |
| 334 | } |
| 335 | } |
| 336 | else |
| 337 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 338 | log<level::ERR>( |
| 339 | fmt::format( |
| 340 | "PowerMode::sendModeChange: SET_MODE FAILED with status={}", |
| 341 | status) |
| 342 | .c_str()); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | log<level::ERR>( |
| 348 | fmt::format( |
| 349 | "PowerMode::sendModeChange: Unable to set power mode to {}", |
| 350 | newMode) |
| 351 | .c_str()); |
| 352 | status = CmdStatus::FAILURE; |
| 353 | } |
| 354 | |
| 355 | return status; |
| 356 | } |
| 357 | |
| 358 | void PowerMode::ipsChanged(sdbusplus::message::message& msg) |
| 359 | { |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 360 | bool parmsChanged = false; |
| 361 | std::string interface; |
| 362 | std::map<std::string, std::variant<bool, uint8_t, uint64_t>> |
| 363 | ipsProperties{}; |
| 364 | msg.read(interface, ipsProperties); |
| 365 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 366 | // Read persisted values |
| 367 | bool ipsEnabled; |
| 368 | uint8_t enterUtil, exitUtil; |
| 369 | uint16_t enterTime, exitTime; |
| 370 | getIPSParms(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime); |
| 371 | |
| 372 | // Check for any changed data |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 373 | auto ipsEntry = ipsProperties.find(IPS_ENABLED_PROP); |
| 374 | if (ipsEntry != ipsProperties.end()) |
| 375 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 376 | ipsEnabled = std::get<bool>(ipsEntry->second); |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 377 | log<level::INFO>( |
| 378 | fmt::format("Idle Power Saver change: Enabled={}", ipsEnabled) |
| 379 | .c_str()); |
| 380 | parmsChanged = true; |
| 381 | } |
| 382 | ipsEntry = ipsProperties.find(IPS_ENTER_UTIL); |
| 383 | if (ipsEntry != ipsProperties.end()) |
| 384 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 385 | enterUtil = std::get<uint8_t>(ipsEntry->second); |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 386 | log<level::INFO>( |
| 387 | fmt::format("Idle Power Saver change: Enter Util={}%", enterUtil) |
| 388 | .c_str()); |
| 389 | parmsChanged = true; |
| 390 | } |
| 391 | ipsEntry = ipsProperties.find(IPS_ENTER_TIME); |
| 392 | if (ipsEntry != ipsProperties.end()) |
| 393 | { |
| 394 | std::chrono::milliseconds ms(std::get<uint64_t>(ipsEntry->second)); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 395 | enterTime = |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 396 | std::chrono::duration_cast<std::chrono::seconds>(ms).count(); |
| 397 | log<level::INFO>( |
| 398 | fmt::format("Idle Power Saver change: Enter Time={}sec", enterTime) |
| 399 | .c_str()); |
| 400 | parmsChanged = true; |
| 401 | } |
| 402 | ipsEntry = ipsProperties.find(IPS_EXIT_UTIL); |
| 403 | if (ipsEntry != ipsProperties.end()) |
| 404 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 405 | exitUtil = std::get<uint8_t>(ipsEntry->second); |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 406 | log<level::INFO>( |
| 407 | fmt::format("Idle Power Saver change: Exit Util={}%", exitUtil) |
| 408 | .c_str()); |
| 409 | parmsChanged = true; |
| 410 | } |
| 411 | ipsEntry = ipsProperties.find(IPS_EXIT_TIME); |
| 412 | if (ipsEntry != ipsProperties.end()) |
| 413 | { |
| 414 | std::chrono::milliseconds ms(std::get<uint64_t>(ipsEntry->second)); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 415 | exitTime = std::chrono::duration_cast<std::chrono::seconds>(ms).count(); |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 416 | log<level::INFO>( |
| 417 | fmt::format("Idle Power Saver change: Exit Time={}sec", exitTime) |
| 418 | .c_str()); |
| 419 | parmsChanged = true; |
| 420 | } |
| 421 | |
| 422 | if (parmsChanged) |
| 423 | { |
Chris Cain | cde7bea | 2022-01-28 15:54:24 -0600 | [diff] [blame] | 424 | if (exitUtil == 0) |
| 425 | { |
| 426 | // Setting the exitUtil to 0 will force restoring the default IPS |
| 427 | // parmeters (0 is not valid exit utilization) |
| 428 | log<level::INFO>( |
| 429 | "Idle Power Saver Exit Utilization is 0%. Restoring default parameters"); |
| 430 | // Read the default IPS parameters, write persistent file and update |
| 431 | // DBus |
| 432 | useDefaultIPSParms(); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | // Update persistant data with new DBus values |
| 437 | persistedData.updateIPS(ipsEnabled, enterUtil, enterTime, exitUtil, |
| 438 | exitTime); |
| 439 | } |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 440 | |
| 441 | // Trigger IPS data to get sent to the OCC |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 442 | sendIpsData(); |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return; |
| 446 | } |
| 447 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 448 | /** @brief Get the Idle Power Saver properties from persisted data |
| 449 | * @return true if IPS parameters were read |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 450 | */ |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 451 | bool PowerMode::getIPSParms(bool& ipsEnabled, uint8_t& enterUtil, |
| 452 | uint16_t& enterTime, uint8_t& exitUtil, |
| 453 | uint16_t& exitTime) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 454 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 455 | // Defaults: |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 456 | ipsEnabled = true; // Enabled |
| 457 | enterUtil = 8; // Enter Utilization (8%) |
| 458 | enterTime = 240; // Enter Delay Time (240s) |
| 459 | exitUtil = 12; // Exit Utilization (12%) |
| 460 | exitTime = 10; // Exit Delay Time (10s) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 461 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 462 | if (!persistedData.getIPS(ipsEnabled, enterUtil, enterTime, exitUtil, |
| 463 | exitTime)) |
| 464 | { |
| 465 | // Persistent data not initialized, read defaults and update DBus |
| 466 | if (!initPersistentData()) |
| 467 | { |
| 468 | // Unable to read defaults from entity manager yet |
| 469 | return false; |
| 470 | } |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 471 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 472 | persistedData.getIPS(ipsEnabled, enterUtil, enterTime, exitUtil, |
| 473 | exitTime); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (enterUtil > exitUtil) |
| 477 | { |
| 478 | log<level::ERR>( |
| 479 | fmt::format( |
| 480 | "ERROR: Idle Power Saver Enter Utilization ({}%) is > Exit Utilization ({}%) - using Exit for both", |
| 481 | enterUtil, exitUtil) |
| 482 | .c_str()); |
| 483 | enterUtil = exitUtil; |
| 484 | } |
| 485 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 486 | return true; |
| 487 | } |
| 488 | |
| 489 | // Set the Idle Power Saver data on DBus |
| 490 | bool PowerMode::updateDbusIPS(const bool enabled, const uint8_t enterUtil, |
| 491 | const uint16_t enterTime, const uint8_t exitUtil, |
| 492 | const uint16_t exitTime) |
| 493 | { |
| 494 | // true = skip update signal |
| 495 | IpsInterface::setPropertyByName(IPS_ENABLED_PROP, enabled, true); |
| 496 | IpsInterface::setPropertyByName(IPS_ENTER_UTIL, enterUtil, true); |
| 497 | // Convert time from seconds to ms |
| 498 | uint64_t msTime = enterTime * 1000; |
| 499 | IpsInterface::setPropertyByName(IPS_ENTER_TIME, msTime, true); |
| 500 | IpsInterface::setPropertyByName(IPS_EXIT_UTIL, exitUtil, true); |
| 501 | msTime = exitTime * 1000; |
| 502 | IpsInterface::setPropertyByName(IPS_EXIT_TIME, msTime, true); |
| 503 | |
| 504 | return true; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Send Idle Power Saver config data to the master OCC |
| 508 | CmdStatus PowerMode::sendIpsData() |
| 509 | { |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 510 | if (!masterActive || !masterOccSet) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 511 | { |
| 512 | // Nothing to do |
| 513 | return CmdStatus::SUCCESS; |
| 514 | } |
| 515 | |
| 516 | if (!isPowerVM()) |
| 517 | { |
| 518 | // Idle Power Saver data is only supported on PowerVM systems |
| 519 | log<level::DEBUG>( |
| 520 | "PowerMode::sendIpsData: SET_CFG_DATA[IPS] does not get sent on non-PowerVM systems"); |
| 521 | return CmdStatus::SUCCESS; |
| 522 | } |
| 523 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 524 | bool ipsEnabled; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 525 | uint8_t enterUtil, exitUtil; |
| 526 | uint16_t enterTime, exitTime; |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 527 | getIPSParms(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 528 | |
| 529 | log<level::INFO>( |
| 530 | fmt::format( |
| 531 | "Idle Power Saver Parameters: enabled:{}, enter:{}%/{}s, exit:{}%/{}s", |
| 532 | ipsEnabled, enterUtil, enterTime, exitUtil, exitTime) |
| 533 | .c_str()); |
| 534 | |
| 535 | std::vector<std::uint8_t> cmd, rsp; |
| 536 | cmd.reserve(12); |
| 537 | cmd.push_back(uint8_t(CmdType::SET_CONFIG_DATA)); |
| 538 | cmd.push_back(0x00); // Data Length (2 bytes) |
| 539 | cmd.push_back(0x09); // |
| 540 | cmd.push_back(0x11); // Config Format: IPS Settings |
| 541 | cmd.push_back(0x00); // Version |
| 542 | cmd.push_back(ipsEnabled ? 1 : 0); // IPS Enable |
| 543 | cmd.push_back(enterTime >> 8); // Enter Delay Time |
| 544 | cmd.push_back(enterTime & 0xFF); // |
| 545 | cmd.push_back(enterUtil); // Enter Utilization |
| 546 | cmd.push_back(exitTime >> 8); // Exit Delay Time |
| 547 | cmd.push_back(exitTime & 0xFF); // |
| 548 | cmd.push_back(exitUtil); // Exit Utilization |
| 549 | log<level::INFO>(fmt::format("PowerMode::sendIpsData: SET_CFG_DATA[IPS] " |
| 550 | "command to OCC{} ({} bytes)", |
| 551 | occInstance, cmd.size()) |
| 552 | .c_str()); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 553 | CmdStatus status = occCmd->send(cmd, rsp); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 554 | if (status == CmdStatus::SUCCESS) |
| 555 | { |
| 556 | if (rsp.size() == 5) |
| 557 | { |
| 558 | if (RspStatus::SUCCESS != RspStatus(rsp[2])) |
| 559 | { |
| 560 | log<level::ERR>( |
| 561 | fmt::format( |
| 562 | "PowerMode::sendIpsData: SET_CFG_DATA[IPS] failed with status 0x{:02X}", |
| 563 | rsp[2]) |
| 564 | .c_str()); |
| 565 | dump_hex(rsp); |
| 566 | status = CmdStatus::FAILURE; |
| 567 | } |
| 568 | } |
| 569 | else |
| 570 | { |
| 571 | log<level::ERR>( |
| 572 | "PowerMode::sendIpsData: INVALID SET_CFG_DATA[IPS] response"); |
| 573 | dump_hex(rsp); |
| 574 | status = CmdStatus::FAILURE; |
| 575 | } |
| 576 | } |
| 577 | else |
| 578 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 579 | log<level::ERR>( |
| 580 | fmt::format( |
| 581 | "PowerMode::sendIpsData: SET_CFG_DATA[IPS] with status={}", |
| 582 | status) |
| 583 | .c_str()); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | return status; |
| 587 | } |
| 588 | |
Chris Cain | cde7bea | 2022-01-28 15:54:24 -0600 | [diff] [blame] | 589 | // Print the current values |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 590 | void OccPersistData::print() |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 591 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 592 | if (modeData.modeInitialized) |
| 593 | { |
| 594 | log<level::INFO>( |
| 595 | fmt::format( |
| 596 | "OccPersistData: Mode: 0x{:02X}, OEM Mode Data: {} (0x{:04X})", |
| 597 | modeData.mode, modeData.oemModeData, modeData.oemModeData) |
| 598 | .c_str()); |
| 599 | } |
| 600 | if (modeData.ipsInitialized) |
| 601 | { |
| 602 | log<level::INFO>( |
| 603 | fmt::format( |
| 604 | "OccPersistData: IPS enabled:{}, enter:{}%/{}s, exit:{}%/{}s", |
| 605 | modeData.ipsEnabled, modeData.ipsEnterUtil, |
| 606 | modeData.ipsEnterTime, modeData.ipsExitUtil, |
| 607 | modeData.ipsExitTime) |
| 608 | .c_str()); |
| 609 | } |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | // Saves the OEM mode data in the filesystem using cereal. |
| 613 | void OccPersistData::save() |
| 614 | { |
| 615 | std::filesystem::path opath = |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 616 | std::filesystem::path{OCC_CONTROL_PERSIST_PATH} / powerModeFilename; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 617 | |
| 618 | if (!std::filesystem::exists(opath.parent_path())) |
| 619 | { |
| 620 | std::filesystem::create_directory(opath.parent_path()); |
| 621 | } |
| 622 | |
| 623 | log<level::DEBUG>( |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 624 | fmt::format( |
| 625 | "OccPersistData::save: Writing Power Mode persisted data to {}", |
| 626 | opath.c_str()) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 627 | .c_str()); |
Chris Cain | cde7bea | 2022-01-28 15:54:24 -0600 | [diff] [blame] | 628 | // print(); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 629 | |
| 630 | std::ofstream stream{opath.c_str()}; |
| 631 | cereal::JSONOutputArchive oarchive{stream}; |
| 632 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 633 | oarchive(modeData); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // Loads the OEM mode data in the filesystem using cereal. |
| 637 | void OccPersistData::load() |
| 638 | { |
| 639 | |
| 640 | std::filesystem::path ipath = |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 641 | std::filesystem::path{OCC_CONTROL_PERSIST_PATH} / powerModeFilename; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 642 | |
| 643 | if (!std::filesystem::exists(ipath)) |
| 644 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 645 | modeData.modeInitialized = false; |
| 646 | modeData.ipsInitialized = false; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | |
| 650 | log<level::DEBUG>( |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 651 | fmt::format( |
| 652 | "OccPersistData::load: Reading Power Mode persisted data from {}", |
| 653 | ipath.c_str()) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 654 | .c_str()); |
| 655 | try |
| 656 | { |
| 657 | std::ifstream stream{ipath.c_str()}; |
| 658 | cereal::JSONInputArchive iarchive(stream); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 659 | iarchive(modeData); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 660 | } |
| 661 | catch (const std::exception& e) |
| 662 | { |
| 663 | auto error = errno; |
| 664 | log<level::ERR>( |
| 665 | fmt::format("OccPersistData::load: failed to read {}, errno={}", |
| 666 | ipath.c_str(), error) |
| 667 | .c_str()); |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 668 | modeData.modeInitialized = false; |
| 669 | modeData.ipsInitialized = false; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 670 | } |
| 671 | |
Chris Cain | cde7bea | 2022-01-28 15:54:24 -0600 | [diff] [blame] | 672 | // print(); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 673 | } |
| 674 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 675 | // Called when PowerModeProperties defaults are available on DBus |
| 676 | void PowerMode::defaultsReady(sdbusplus::message::message& msg) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 677 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 678 | std::map<std::string, std::variant<std::string>> properties{}; |
| 679 | std::string interface; |
| 680 | msg.read(interface, properties); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 681 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 682 | // If persistent data exists, then don't need to read defaults |
| 683 | if ((!persistedData.modeAvailable()) || (!persistedData.ipsAvailable())) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 684 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 685 | log<level::INFO>( |
| 686 | fmt::format( |
| 687 | "Default PowerModeProperties are now available (persistent modeAvail={}, ipsAvail={})", |
| 688 | persistedData.modeAvailable() ? 'y' : 'n', |
| 689 | persistedData.modeAvailable() ? 'y' : 'n') |
| 690 | .c_str()); |
| 691 | |
| 692 | // Read default power mode defaults and update DBus |
| 693 | initPersistentData(); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | // Get the default power mode from DBus and return true if success |
| 698 | bool PowerMode::getDefaultMode(SysPwrMode& defaultMode) |
| 699 | { |
| 700 | try |
| 701 | { |
| 702 | auto& bus = utils::getBus(); |
| 703 | std::string path = "/"; |
| 704 | std::string service = |
| 705 | utils::getServiceUsingSubTree(PMODE_DEFAULT_INTERFACE, path); |
| 706 | auto method = |
| 707 | bus.new_method_call(service.c_str(), path.c_str(), |
| 708 | "org.freedesktop.DBus.Properties", "Get"); |
| 709 | method.append(PMODE_DEFAULT_INTERFACE, "PowerMode"); |
| 710 | auto reply = bus.call(method); |
| 711 | |
| 712 | std::variant<std::string> stateEntryValue; |
| 713 | reply.read(stateEntryValue); |
| 714 | auto propVal = std::get<std::string>(stateEntryValue); |
| 715 | |
| 716 | const std::string fullModeString = |
| 717 | PMODE_INTERFACE + ".PowerMode."s + propVal; |
| 718 | defaultMode = powermode::convertStringToMode(fullModeString); |
| 719 | if (!VALID_POWER_MODE_SETTING(defaultMode)) |
| 720 | { |
| 721 | log<level::ERR>( |
| 722 | fmt::format( |
| 723 | "PowerMode::getDefaultMode: Invalid default power mode found: {}", |
| 724 | defaultMode) |
| 725 | .c_str()); |
| 726 | // If default was read but not valid, use Max Performance |
| 727 | defaultMode = SysPwrMode::MAX_PERF; |
| 728 | return true; |
| 729 | } |
| 730 | } |
| 731 | catch (const sdbusplus::exception::exception& e) |
| 732 | { |
| 733 | log<level::ERR>( |
| 734 | fmt::format("Unable to read Default Power Mode: {}", e.what()) |
| 735 | .c_str()); |
| 736 | return false; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 737 | } |
| 738 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 739 | return true; |
| 740 | } |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 741 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 742 | /* Get the default Idle Power Saver properties and return true if successful */ |
| 743 | bool PowerMode::getDefaultIPSParms(bool& ipsEnabled, uint8_t& enterUtil, |
| 744 | uint16_t& enterTime, uint8_t& exitUtil, |
| 745 | uint16_t& exitTime) |
| 746 | { |
| 747 | // Defaults: |
| 748 | ipsEnabled = true; // Enabled |
| 749 | enterUtil = 8; // Enter Utilization (8%) |
| 750 | enterTime = 240; // Enter Delay Time (240s) |
| 751 | exitUtil = 12; // Exit Utilization (12%) |
| 752 | exitTime = 10; // Exit Delay Time (10s) |
| 753 | |
| 754 | std::map<std::string, std::variant<bool, uint8_t, uint16_t, uint64_t>> |
| 755 | ipsProperties{}; |
| 756 | |
| 757 | // Get all IPS properties from DBus |
| 758 | try |
| 759 | { |
| 760 | auto& bus = utils::getBus(); |
| 761 | std::string path = "/"; |
| 762 | std::string service = |
| 763 | utils::getServiceUsingSubTree(PMODE_DEFAULT_INTERFACE, path); |
| 764 | auto method = |
| 765 | bus.new_method_call(service.c_str(), path.c_str(), |
| 766 | "org.freedesktop.DBus.Properties", "GetAll"); |
| 767 | method.append(PMODE_DEFAULT_INTERFACE); |
| 768 | auto reply = bus.call(method); |
| 769 | reply.read(ipsProperties); |
| 770 | } |
| 771 | catch (const sdbusplus::exception::exception& e) |
| 772 | { |
| 773 | log<level::ERR>( |
| 774 | fmt::format( |
| 775 | "Unable to read Default Idle Power Saver parameters so it will be disabled: {}", |
| 776 | e.what()) |
| 777 | .c_str()); |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | auto ipsEntry = ipsProperties.find("IdlePowerSaverEnabled"); |
| 782 | if (ipsEntry != ipsProperties.end()) |
| 783 | { |
| 784 | ipsEnabled = std::get<bool>(ipsEntry->second); |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | log<level::ERR>( |
| 789 | "PowerMode::getDefaultIPSParms could not find property: IdlePowerSaverEnabled"); |
| 790 | } |
| 791 | |
| 792 | ipsEntry = ipsProperties.find("EnterUtilizationPercent"); |
| 793 | if (ipsEntry != ipsProperties.end()) |
| 794 | { |
| 795 | enterUtil = std::get<uint64_t>(ipsEntry->second); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | log<level::ERR>( |
| 800 | "PowerMode::getDefaultIPSParms could not find property: EnterUtilizationPercent"); |
| 801 | } |
| 802 | |
| 803 | ipsEntry = ipsProperties.find("EnterUtilizationDwellTime"); |
| 804 | if (ipsEntry != ipsProperties.end()) |
| 805 | { |
| 806 | enterTime = std::get<uint64_t>(ipsEntry->second); |
| 807 | } |
| 808 | else |
| 809 | { |
| 810 | log<level::ERR>( |
| 811 | "PowerMode::getDefaultIPSParms could not find property: EnterUtilizationDwellTime"); |
| 812 | } |
| 813 | |
| 814 | ipsEntry = ipsProperties.find("ExitUtilizationPercent"); |
| 815 | if (ipsEntry != ipsProperties.end()) |
| 816 | { |
| 817 | exitUtil = std::get<uint64_t>(ipsEntry->second); |
| 818 | } |
| 819 | else |
| 820 | { |
| 821 | log<level::ERR>( |
| 822 | "PowerMode::getDefaultIPSParms could not find property: ExitUtilizationPercent"); |
| 823 | } |
| 824 | |
| 825 | ipsEntry = ipsProperties.find("ExitUtilizationDwellTime"); |
| 826 | if (ipsEntry != ipsProperties.end()) |
| 827 | { |
| 828 | exitTime = std::get<uint64_t>(ipsEntry->second); |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | log<level::ERR>( |
| 833 | "PowerMode::getDefaultIPSParms could not find property: ExitUtilizationDwellTime"); |
| 834 | } |
| 835 | |
| 836 | if (enterUtil > exitUtil) |
| 837 | { |
| 838 | log<level::ERR>( |
| 839 | fmt::format( |
| 840 | "ERROR: Default Idle Power Saver Enter Utilization ({}%) is > Exit Utilization ({}%) - using Exit for both", |
| 841 | enterUtil, exitUtil) |
| 842 | .c_str()); |
| 843 | enterUtil = exitUtil; |
| 844 | } |
| 845 | |
| 846 | return true; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 847 | } |
| 848 | |
Chris Cain | cde7bea | 2022-01-28 15:54:24 -0600 | [diff] [blame] | 849 | /* Read default IPS parameters, save them to the persistent file and update |
| 850 | DBus. Return true if successful */ |
| 851 | bool PowerMode::useDefaultIPSParms() |
| 852 | { |
| 853 | // Read the default IPS parameters |
| 854 | bool ipsEnabled; |
| 855 | uint8_t enterUtil, exitUtil; |
| 856 | uint16_t enterTime, exitTime; |
| 857 | if (!getDefaultIPSParms(ipsEnabled, enterUtil, enterTime, exitUtil, |
| 858 | exitTime)) |
| 859 | { |
| 860 | // Unable to read defaults |
| 861 | return false; |
| 862 | } |
| 863 | log<level::INFO>( |
| 864 | fmt::format( |
| 865 | "PowerMode::useDefaultIPSParms: Using default IPS parms: Enabled: {}, EnterUtil: {}%, EnterTime: {}s, ExitUtil: {}%, ExitTime: {}s", |
| 866 | ipsEnabled, enterUtil, enterTime, exitUtil, exitTime) |
| 867 | .c_str()); |
| 868 | |
| 869 | // Save IPS parms to the persistent file |
| 870 | persistedData.updateIPS(ipsEnabled, enterUtil, enterTime, exitUtil, |
| 871 | exitTime); |
| 872 | |
| 873 | // Write IPS parms to DBus |
| 874 | return updateDbusIPS(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime); |
| 875 | } |
| 876 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame^] | 877 | #ifdef POWER10 |
| 878 | |
| 879 | // Starts to watch for IPS active state changes. |
| 880 | bool PowerMode::openIpsFile() |
| 881 | { |
| 882 | bool rc = true; |
| 883 | fd = open(ipsStatusFile.c_str(), O_RDONLY | O_NONBLOCK); |
| 884 | const int open_errno = errno; |
| 885 | if (fd < 0) |
| 886 | { |
| 887 | log<level::ERR>(fmt::format("openIpsFile Error({})={} : File={}", |
| 888 | open_errno, strerror(open_errno), |
| 889 | ipsStatusFile.c_str()) |
| 890 | .c_str()); |
| 891 | |
| 892 | close(fd); |
| 893 | |
| 894 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
| 895 | report<OpenFailure>( |
| 896 | phosphor::logging::org::open_power::OCC::Device::OpenFailure:: |
| 897 | CALLOUT_ERRNO(open_errno), |
| 898 | phosphor::logging::org::open_power::OCC::Device::OpenFailure:: |
| 899 | CALLOUT_DEVICE_PATH(ipsStatusFile.c_str())); |
| 900 | |
| 901 | // We are no longer watching the error |
| 902 | active(false); |
| 903 | |
| 904 | watching = false; |
| 905 | rc = false; |
| 906 | // NOTE: this will leave the system not reporting IPS active state to |
| 907 | // Fan Controls, Until an APP reload, or IPL and we will attempt again. |
| 908 | } |
| 909 | return rc; |
| 910 | } |
| 911 | |
| 912 | // Starts to watch for IPS active state changes. |
| 913 | void PowerMode::addIpsWatch(bool poll) |
| 914 | { |
| 915 | // open file and register callback on file if we are not currently watching, |
| 916 | // and if poll=true, and if we are the master. |
| 917 | if ((!watching) && poll) |
| 918 | { |
| 919 | // Open the file |
| 920 | if (openIpsFile()) |
| 921 | { |
| 922 | // register the callback handler which sets 'watching' |
| 923 | registerIpsStatusCallBack(); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // Stops watching for IPS active state changes. |
| 929 | void PowerMode::removeIpsWatch() |
| 930 | { |
| 931 | // NOTE: we want to remove event, close file, and IPS active false no |
| 932 | // matter what the 'watching' flags is set to. |
| 933 | |
| 934 | // We are no longer watching the error |
| 935 | active(false); |
| 936 | |
| 937 | watching = false; |
| 938 | |
| 939 | // Close file |
| 940 | close(fd); |
| 941 | |
| 942 | // clears sourcePtr in the event source. |
| 943 | eventSource.reset(); |
| 944 | } |
| 945 | |
| 946 | // Attaches the FD to event loop and registers the callback handler |
| 947 | void PowerMode::registerIpsStatusCallBack() |
| 948 | { |
| 949 | decltype(eventSource.get()) sourcePtr = nullptr; |
| 950 | |
| 951 | auto r = sd_event_add_io(event.get(), &sourcePtr, fd, EPOLLPRI | EPOLLERR, |
| 952 | ipsStatusCallBack, this); |
| 953 | if (r < 0) |
| 954 | { |
| 955 | log<level::ERR>(fmt::format("sd_event_add_io: Error({})={} : File={}", |
| 956 | r, strerror(-r), ipsStatusFile.c_str()) |
| 957 | .c_str()); |
| 958 | |
| 959 | using InternalFailure = |
| 960 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 961 | report<InternalFailure>(); |
| 962 | |
| 963 | removeIpsWatch(); |
| 964 | // NOTE: this will leave the system not reporting IPS active state to |
| 965 | // Fan Controls, Until an APP reload, or IPL and we will attempt again. |
| 966 | } |
| 967 | else |
| 968 | { |
| 969 | // puts sourcePtr in the event source. |
| 970 | eventSource.reset(sourcePtr); |
| 971 | // Set we are watching the error |
| 972 | watching = true; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | // Static function to redirect to non static analyze event function to be |
| 977 | // able to read file and push onto dBus. |
| 978 | int PowerMode::ipsStatusCallBack(sd_event_source* /*es*/, int /*fd*/, |
| 979 | uint32_t /*revents*/, void* userData) |
| 980 | { |
| 981 | auto pmode = static_cast<PowerMode*>(userData); |
| 982 | pmode->analyzeIpsEvent(); |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | // Function to Read SysFs file change on IPS state and push on dBus. |
| 987 | void PowerMode::analyzeIpsEvent() |
| 988 | { |
| 989 | // Need to seek to START, else the poll returns immediately telling |
| 990 | // there is data to be read. if not done this floods the system. |
| 991 | auto r = lseek(fd, 0, SEEK_SET); |
| 992 | const int open_errno = errno; |
| 993 | if (r < 0) |
| 994 | { |
| 995 | // NOTE: upon file access error we can not just re-open file, we have to |
| 996 | // remove and add to watch. |
| 997 | removeIpsWatch(); |
| 998 | addIpsWatch(true); |
| 999 | } |
| 1000 | |
| 1001 | // if we are 'watching' that is the file seek, or the re-open passed.. we |
| 1002 | // can read the data |
| 1003 | if (watching) |
| 1004 | { |
| 1005 | // This file gets created when polling OCCs. A value or length of 0 is |
| 1006 | // deemed success. That means we would disable IPS active on dbus. |
| 1007 | char data; |
| 1008 | bool ipsState = false; |
| 1009 | const auto len = read(fd, &data, sizeof(data)); |
| 1010 | const int readErrno = errno; |
| 1011 | if (len <= 0) |
| 1012 | { |
| 1013 | removeIpsWatch(); |
| 1014 | |
| 1015 | log<level::ERR>( |
| 1016 | fmt::format("IPS state Read Error({})={} : File={} : len={}", |
| 1017 | readErrno, strerror(readErrno), |
| 1018 | ipsStatusFile.c_str(), len) |
| 1019 | .c_str()); |
| 1020 | |
| 1021 | report<ReadFailure>( |
| 1022 | phosphor::logging::org::open_power::OCC::Device::ReadFailure:: |
| 1023 | CALLOUT_ERRNO(readErrno), |
| 1024 | phosphor::logging::org::open_power::OCC::Device::ReadFailure:: |
| 1025 | CALLOUT_DEVICE_PATH(ipsStatusFile.c_str())); |
| 1026 | |
| 1027 | // NOTE: this will leave the system not reporting IPS active state |
| 1028 | // to Fan Controls, Until an APP reload, or IPL and we will attempt |
| 1029 | // again. |
| 1030 | } |
| 1031 | else |
| 1032 | { |
| 1033 | // Data returned in ASCII. |
| 1034 | // convert to integer. atoi() |
| 1035 | // from OCC_P10_FW_Interfaces spec |
| 1036 | // Bit 6: IPS active 1 indicates enabled. |
| 1037 | // Bit 7: IPS enabled. 1 indicates enabled. |
| 1038 | // mask off bit 6 --> & 0x02 |
| 1039 | // Shift left one bit and store as bool. >> 1 |
| 1040 | ipsState = static_cast<bool>(((atoi(&data)) & 0x2) >> 1); |
| 1041 | } |
| 1042 | |
| 1043 | // This will only set IPS active dbus if different than current. |
| 1044 | active(ipsState); |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | removeIpsWatch(); |
| 1049 | |
| 1050 | // If the Retry did not get to "watching = true" we already have an |
| 1051 | // error log, just post trace. |
| 1052 | log<level::ERR>(fmt::format("Retry on File seek Error({})={} : File={}", |
| 1053 | open_errno, strerror(open_errno), |
| 1054 | ipsStatusFile.c_str()) |
| 1055 | .c_str()); |
| 1056 | |
| 1057 | // NOTE: this will leave the system not reporting IPS active state to |
| 1058 | // Fan Controls, Until an APP reload, or IPL and we will attempt again. |
| 1059 | } |
| 1060 | |
| 1061 | return; |
| 1062 | } |
| 1063 | #endif |
| 1064 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 1065 | } // namespace powermode |
| 1066 | |
| 1067 | } // namespace occ |
| 1068 | |
| 1069 | } // namespace open_power |