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