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