Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 1 | #include "occ_status.hpp" |
| 2 | |
Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 3 | #include <fmt/core.h> |
| 4 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 6 | #include <powercap.hpp> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 7 | |
| 8 | #include <cassert> |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 9 | #include <filesystem> |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 10 | |
| 11 | namespace open_power |
| 12 | { |
| 13 | namespace occ |
| 14 | { |
| 15 | namespace powercap |
| 16 | { |
| 17 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 18 | constexpr auto PCAP_PATH = "/xyz/openbmc_project/control/host0/power_cap"; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 19 | constexpr auto PCAP_INTERFACE = "xyz.openbmc_project.Control.Power.Cap"; |
| 20 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 21 | constexpr auto POWER_CAP_PROP = "PowerCap"; |
| 22 | constexpr auto POWER_CAP_ENABLE_PROP = "PowerCapEnable"; |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 23 | constexpr auto POWER_CAP_SOFT_MIN = "MinSoftPowerCapValue"; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 24 | constexpr auto POWER_CAP_HARD_MIN = "MinPowerCapValue"; |
| 25 | constexpr auto POWER_CAP_MAX = "MaxPowerCapValue"; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 26 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 27 | using namespace phosphor::logging; |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 28 | namespace fs = std::filesystem; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 29 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 30 | void PowerCap::updatePcapBounds() |
| 31 | { |
| 32 | // Build the hwmon string to write the power cap bounds |
| 33 | fs::path minName = getPcapFilename(std::regex{"power\\d+_cap_min$"}); |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 34 | fs::path softMinName = |
| 35 | getPcapFilename(std::regex{"power\\d+_cap_min_soft$"}); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 36 | fs::path maxName = getPcapFilename(std::regex{"power\\d+_cap_max$"}); |
| 37 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 38 | // Read the current cap bounds from dbus |
| 39 | uint32_t capSoftMin, capHardMin, capMax; |
| 40 | readDbusPcapLimits(capSoftMin, capHardMin, capMax); |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 41 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 42 | // Read the power cap bounds from sysfs files (from OCC) |
| 43 | uint64_t cap; |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 44 | std::ifstream softMinFile(softMinName, std::ios::in); |
| 45 | if (softMinFile) |
| 46 | { |
| 47 | softMinFile >> cap; |
| 48 | softMinFile.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 49 | // Convert to input/AC Power in Watts (round up) |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 50 | capSoftMin = ((cap / (PS_DERATING_FACTOR / 100.0) / 1000000) + 0.9); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | log<level::ERR>( |
| 55 | fmt::format( |
| 56 | "updatePcapBounds: unable to find pcap_min_soft file: {} (errno={})", |
| 57 | pcapBasePathname.c_str(), errno) |
| 58 | .c_str()); |
| 59 | } |
| 60 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 61 | std::ifstream minFile(minName, std::ios::in); |
| 62 | if (minFile) |
| 63 | { |
| 64 | minFile >> cap; |
| 65 | minFile.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 66 | // Convert to input/AC Power in Watts (round up) |
Chris Cain | 8676e03 | 2022-03-24 15:06:23 -0500 | [diff] [blame] | 67 | capHardMin = ((cap / (PS_DERATING_FACTOR / 100.0) / 1000000) + 0.9); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 68 | } |
| 69 | else |
| 70 | { |
| 71 | log<level::ERR>( |
| 72 | fmt::format( |
| 73 | "updatePcapBounds: unable to find cap_min file: {} (errno={})", |
| 74 | pcapBasePathname.c_str(), errno) |
| 75 | .c_str()); |
| 76 | } |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 77 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 78 | std::ifstream maxFile(maxName, std::ios::in); |
| 79 | if (maxFile) |
| 80 | { |
| 81 | maxFile >> cap; |
| 82 | maxFile.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 83 | // Convert to input/AC Power in Watts (truncate remainder) |
Chris Cain | 8676e03 | 2022-03-24 15:06:23 -0500 | [diff] [blame] | 84 | capMax = cap / (PS_DERATING_FACTOR / 100.0) / 1000000; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 85 | } |
| 86 | else |
| 87 | { |
| 88 | log<level::ERR>( |
| 89 | fmt::format( |
| 90 | "updatePcapBounds: unable to find cap_max file: {} (errno={})", |
| 91 | pcapBasePathname.c_str(), errno) |
| 92 | .c_str()); |
| 93 | } |
| 94 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 95 | // Save the power cap bounds to dbus |
| 96 | updateDbusPcapLimits(capSoftMin, capHardMin, capMax); |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 97 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 98 | // Validate user power cap (if enabled) is within the bounds |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 99 | const uint32_t dbusUserCap = getPcap(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 100 | const bool pcapEnabled = getPcapEnabled(); |
| 101 | if (pcapEnabled && (dbusUserCap != 0)) |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 102 | { |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 103 | const uint32_t hwmonUserCap = readUserCapHwmon(); |
| 104 | if ((dbusUserCap >= capSoftMin) && (dbusUserCap <= capMax)) |
| 105 | { |
| 106 | // Validate dbus and hwmon user caps match |
| 107 | if ((hwmonUserCap != 0) && (dbusUserCap != hwmonUserCap)) |
| 108 | { |
| 109 | // User power cap is enabled, but does not match dbus |
| 110 | log<level::ERR>( |
| 111 | fmt::format( |
| 112 | "updatePcapBounds: user powercap mismatch (hwmon:{}W, bdus:{}W) - using dbus", |
| 113 | hwmonUserCap, dbusUserCap) |
| 114 | .c_str()); |
| 115 | auto occInput = getOccInput(dbusUserCap, pcapEnabled); |
| 116 | writeOcc(occInput); |
| 117 | } |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | // User power cap is outside of current bounds |
| 122 | uint32_t newCap = capMax; |
| 123 | if (dbusUserCap < capSoftMin) |
| 124 | { |
| 125 | newCap = capSoftMin; |
| 126 | } |
| 127 | log<level::ERR>( |
| 128 | fmt::format( |
| 129 | "updatePcapBounds: user powercap {}W is outside bounds " |
| 130 | "(soft min:{}, min:{}, max:{})", |
| 131 | dbusUserCap, capSoftMin, capHardMin, capMax) |
| 132 | .c_str()); |
| 133 | try |
| 134 | { |
| 135 | log<level::INFO>( |
| 136 | fmt::format( |
| 137 | "updatePcapBounds: Updating user powercap from {} to {}W", |
| 138 | hwmonUserCap, newCap) |
| 139 | .c_str()); |
| 140 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, |
| 141 | newCap); |
| 142 | auto occInput = getOccInput(newCap, pcapEnabled); |
| 143 | writeOcc(occInput); |
| 144 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 145 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 146 | { |
| 147 | log<level::ERR>( |
| 148 | fmt::format( |
| 149 | "updatePcapBounds: Failed to update user powercap due to ", |
| 150 | e.what()) |
| 151 | .c_str()); |
| 152 | } |
| 153 | } |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 154 | } |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 157 | // Get value of power cap to send to the OCC (output/DC power) |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 158 | uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled) |
| 159 | { |
| 160 | if (!pcapEnabled) |
| 161 | { |
| 162 | // Pcap disabled, return 0 to indicate disabled |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | // If pcap is not disabled then just return the pcap with the derating |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 167 | // factor applied (output/DC power). |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 168 | return ((static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) / 100); |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 171 | uint32_t PowerCap::getPcap() |
| 172 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 173 | utils::PropertyValue pcap{}; |
| 174 | try |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 175 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 176 | pcap = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP); |
| 177 | |
| 178 | return std::get<uint32_t>(pcap); |
| 179 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 180 | catch (const sdbusplus::exception_t& e) |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 181 | { |
| 182 | log<level::ERR>("Failed to get PowerCap property", |
| 183 | entry("ERROR=%s", e.what()), |
| 184 | entry("PATH=%s", PCAP_PATH)); |
| 185 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 186 | return 0; |
| 187 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool PowerCap::getPcapEnabled() |
| 191 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 192 | utils::PropertyValue pcapEnabled{}; |
| 193 | try |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 194 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 195 | pcapEnabled = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, |
| 196 | POWER_CAP_ENABLE_PROP); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 197 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 198 | return std::get<bool>(pcapEnabled); |
| 199 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 200 | catch (const sdbusplus::exception_t& e) |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 201 | { |
| 202 | log<level::ERR>("Failed to get PowerCapEnable property", |
| 203 | entry("ERROR=%s", e.what()), |
| 204 | entry("PATH=%s", PCAP_PATH)); |
| 205 | |
| 206 | return false; |
| 207 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 208 | } |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 209 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 210 | fs::path PowerCap::getPcapFilename(const std::regex& expr) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 211 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 212 | if (pcapBasePathname.empty()) |
| 213 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 214 | pcapBasePathname = occStatus.getHwmonPath(); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 215 | } |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 216 | |
| 217 | if (fs::exists(pcapBasePathname)) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 218 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 219 | // Search for pcap file based on the supplied expr |
| 220 | for (auto& file : fs::directory_iterator(pcapBasePathname)) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 221 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 222 | if (std::regex_search(file.path().string(), expr)) |
| 223 | { |
| 224 | // Found match |
| 225 | return file; |
| 226 | } |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 227 | } |
| 228 | } |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 229 | else |
| 230 | { |
| 231 | log<level::ERR>(fmt::format("Power Cap base filename not found: {}", |
| 232 | pcapBasePathname.c_str()) |
| 233 | .c_str()); |
| 234 | } |
| 235 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 236 | // return empty path |
| 237 | return fs::path{}; |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 240 | // Write the user power cap to sysfs (output/DC power) |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 241 | // This will trigger the driver to send the cap to the OCC |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 242 | void PowerCap::writeOcc(uint32_t pcapValue) |
| 243 | { |
Chris Cain | d4c19a0 | 2022-04-22 15:46:13 -0500 | [diff] [blame] | 244 | if (!occStatus.occActive()) |
| 245 | { |
| 246 | // OCC not running, skip update |
| 247 | return; |
| 248 | } |
| 249 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 250 | // Build the hwmon string to write the user power cap |
| 251 | fs::path fileName = getPcapFilename(std::regex{"power\\d+_cap_user$"}); |
| 252 | if (fileName.empty()) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 253 | { |
Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 254 | log<level::ERR>( |
| 255 | fmt::format("Could not find a power cap file to write to: {})", |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 256 | pcapBasePathname.c_str()) |
Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 257 | .c_str()); |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 258 | return; |
| 259 | } |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 260 | |
| 261 | uint64_t microWatts = pcapValue * 1000000ull; |
| 262 | |
| 263 | auto pcapString{std::to_string(microWatts)}; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 264 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 265 | // Open the hwmon file and write the power cap |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 266 | std::ofstream file(fileName, std::ios::out); |
| 267 | if (file) |
| 268 | { |
| 269 | log<level::INFO>(fmt::format("Writing {}uW to {}", pcapString.c_str(), |
| 270 | fileName.c_str()) |
| 271 | .c_str()); |
| 272 | file << pcapString; |
| 273 | file.close(); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | log<level::ERR>(fmt::format("Failed writing {}uW to {} (errno={})", |
| 278 | microWatts, fileName.c_str(), errno) |
| 279 | .c_str()); |
| 280 | } |
| 281 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 285 | // Read the current user power cap from sysfs as input/AC power |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 286 | uint32_t PowerCap::readUserCapHwmon() |
| 287 | { |
| 288 | uint32_t userCap = 0; |
| 289 | |
| 290 | // Get the sysfs filename for the user power cap |
| 291 | fs::path userCapName = getPcapFilename(std::regex{"power\\d+_cap_user$"}); |
| 292 | if (userCapName.empty()) |
| 293 | { |
| 294 | log<level::ERR>( |
| 295 | fmt::format( |
| 296 | "readUserCapHwmon: Could not find a power cap file to read: {})", |
| 297 | pcapBasePathname.c_str()) |
| 298 | .c_str()); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | // Open the sysfs file and read the power cap |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 303 | std::ifstream file(userCapName, std::ios::in); |
| 304 | if (file) |
| 305 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 306 | uint64_t cap; |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 307 | file >> cap; |
| 308 | file.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 309 | // Convert to input/AC Power in Watts |
| 310 | userCap = (cap / (PS_DERATING_FACTOR / 100.0) / 1000000); |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 311 | } |
| 312 | else |
| 313 | { |
| 314 | log<level::ERR>( |
| 315 | fmt::format("readUserCapHwmon: Failed reading {} (errno={})", |
| 316 | userCapName.c_str(), errno) |
| 317 | .c_str()); |
| 318 | } |
| 319 | |
| 320 | return userCap; |
| 321 | } |
| 322 | |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 323 | void PowerCap::pcapChanged(sdbusplus::message_t& msg) |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 324 | { |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 325 | if (!occStatus.occActive()) |
| 326 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 327 | // Nothing to do |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 328 | return; |
| 329 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 330 | |
| 331 | uint32_t pcap = 0; |
| 332 | bool pcapEnabled = false; |
| 333 | |
| 334 | std::string msgSensor; |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 335 | std::map<std::string, std::variant<uint32_t, bool>> msgData; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 336 | msg.read(msgSensor, msgData); |
| 337 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 338 | bool changeFound = false; |
| 339 | for (const auto& [prop, value] : msgData) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 340 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 341 | if (prop == POWER_CAP_PROP) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 342 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 343 | pcap = std::get<uint32_t>(value); |
| 344 | pcapEnabled = getPcapEnabled(); |
| 345 | changeFound = true; |
| 346 | } |
| 347 | else if (prop == POWER_CAP_ENABLE_PROP) |
| 348 | { |
| 349 | pcapEnabled = std::get<bool>(value); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 350 | pcap = getPcap(); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 351 | changeFound = true; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 355 | // Ignore other properties |
| 356 | log<level::DEBUG>( |
| 357 | fmt::format( |
| 358 | "pcapChanged: Unknown power cap property changed {} to {}", |
| 359 | prop.c_str(), std::get<uint32_t>(value)) |
| 360 | .c_str()); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 364 | // Validate the cap is within supported range |
| 365 | uint32_t capSoftMin, capHardMin, capMax; |
| 366 | readDbusPcapLimits(capSoftMin, capHardMin, capMax); |
| 367 | if (((pcap > 0) && (pcap < capSoftMin)) || ((pcap == 0) && (pcapEnabled))) |
| 368 | { |
| 369 | log<level::ERR>( |
| 370 | fmt::format( |
| 371 | "pcapChanged: Power cap of {}W is lower than allowed (soft min:{}, min:{}) - using soft min", |
| 372 | pcap, capSoftMin, capHardMin) |
| 373 | .c_str()); |
| 374 | pcap = capSoftMin; |
| 375 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, pcap); |
| 376 | } |
| 377 | else if (pcap > capMax) |
| 378 | { |
| 379 | log<level::ERR>( |
| 380 | fmt::format( |
| 381 | "pcapChanged: Power cap of {}W is higher than allowed (max:{}) - using max", |
| 382 | pcap, capSoftMin, capHardMin) |
| 383 | .c_str()); |
| 384 | pcap = capMax; |
| 385 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, pcap); |
| 386 | } |
| 387 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 388 | if (changeFound) |
| 389 | { |
| 390 | log<level::INFO>( |
| 391 | fmt::format( |
| 392 | "Power Cap Property Change (cap={}W (input), enabled={})", pcap, |
| 393 | pcapEnabled ? 'y' : 'n') |
| 394 | .c_str()); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 395 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 396 | // Determine desired action to write to occ |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 397 | auto occInput = getOccInput(pcap, pcapEnabled); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 398 | // Write action to occ |
| 399 | writeOcc(occInput); |
| 400 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 401 | |
| 402 | return; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 405 | // Update the Power Cap bounds on DBus |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 406 | bool PowerCap::updateDbusPcapLimits(uint32_t softMin, uint32_t hardMin, |
| 407 | uint32_t max) |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 408 | { |
| 409 | bool complete = true; |
| 410 | |
| 411 | try |
| 412 | { |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 413 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_SOFT_MIN, |
| 414 | softMin); |
| 415 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 416 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 417 | { |
| 418 | log<level::ERR>( |
| 419 | fmt::format( |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 420 | "updateDbusPcapLimits: Failed to set SOFT PCAP to {}W due to {}", |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 421 | softMin, e.what()) |
| 422 | .c_str()); |
| 423 | complete = false; |
| 424 | } |
| 425 | |
| 426 | try |
| 427 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 428 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_HARD_MIN, |
| 429 | hardMin); |
| 430 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 431 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 432 | { |
| 433 | log<level::ERR>( |
| 434 | fmt::format( |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 435 | "updateDbusPcapLimits: Failed to set HARD PCAP to {}W due to {}", |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 436 | hardMin, e.what()) |
| 437 | .c_str()); |
| 438 | complete = false; |
| 439 | } |
| 440 | |
| 441 | try |
| 442 | { |
| 443 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_MAX, max); |
| 444 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 445 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 446 | { |
| 447 | log<level::ERR>( |
| 448 | fmt::format( |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 449 | "updateDbusPcapLimits: Failed to set MAX PCAP to {}W due to {}", |
| 450 | max, e.what()) |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 451 | .c_str()); |
| 452 | complete = false; |
| 453 | } |
| 454 | |
| 455 | return complete; |
| 456 | } |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 457 | |
| 458 | // Read the Power Cap bounds from DBus |
| 459 | bool PowerCap::readDbusPcapLimits(uint32_t& softMin, uint32_t& hardMin, |
| 460 | uint32_t& max) |
| 461 | { |
| 462 | bool complete = true; |
| 463 | utils::PropertyValue prop{}; |
| 464 | |
| 465 | try |
| 466 | { |
| 467 | prop = |
| 468 | utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_SOFT_MIN); |
| 469 | softMin = std::get<uint32_t>(prop); |
| 470 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 471 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 472 | { |
| 473 | log<level::ERR>( |
| 474 | fmt::format("readDbusPcapLimits: Failed to get SOFT PCAP due to {}", |
| 475 | e.what()) |
| 476 | .c_str()); |
| 477 | softMin = 0; |
| 478 | complete = false; |
| 479 | } |
| 480 | |
| 481 | try |
| 482 | { |
| 483 | prop = |
| 484 | utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_HARD_MIN); |
| 485 | hardMin = std::get<uint32_t>(prop); |
| 486 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 487 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 488 | { |
| 489 | log<level::ERR>( |
| 490 | fmt::format("readDbusPcapLimits: Failed to get HARD PCAP due to {}", |
| 491 | e.what()) |
| 492 | .c_str()); |
| 493 | hardMin = 0; |
| 494 | complete = false; |
| 495 | } |
| 496 | |
| 497 | try |
| 498 | { |
| 499 | prop = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_MAX); |
| 500 | max = std::get<uint32_t>(prop); |
| 501 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 502 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 503 | { |
| 504 | log<level::ERR>( |
| 505 | fmt::format("readDbusPcapLimits: Failed to get MAX PCAP due to {}", |
| 506 | e.what()) |
| 507 | .c_str()); |
| 508 | max = INT_MAX; |
| 509 | complete = false; |
| 510 | } |
| 511 | |
| 512 | return complete; |
| 513 | } |
| 514 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 515 | } // namespace powercap |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 516 | |
| 517 | } // namespace occ |
| 518 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 519 | } // namespace open_power |