blob: 7ccc6d948d5681617da2a07027e3bae1387cae2f [file] [log] [blame]
Chris Cain5d66a0a2022-02-09 08:52:10 -06001#include "occ_status.hpp"
2
Chris Cain37abe9b2024-10-31 17:20:31 -05003#include <phosphor-logging/lg2.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -05004#include <powercap.hpp>
George Liub5ca1012021-09-10 12:53:11 +08005
6#include <cassert>
Chris Caine2d0a432022-03-28 11:08:49 -05007#include <filesystem>
Andrew Geissler32016d12017-06-20 15:46:52 -05008
9namespace open_power
10{
11namespace occ
12{
13namespace powercap
14{
15
Gunnar Mills94df8c92018-09-14 14:50:03 -050016constexpr auto PCAP_PATH = "/xyz/openbmc_project/control/host0/power_cap";
Andrew Geissler52cf26a2017-07-06 12:56:32 -050017constexpr auto PCAP_INTERFACE = "xyz.openbmc_project.Control.Power.Cap";
18
Andrew Geissler52cf26a2017-07-06 12:56:32 -050019constexpr auto POWER_CAP_PROP = "PowerCap";
20constexpr auto POWER_CAP_ENABLE_PROP = "PowerCapEnable";
Chris Cain613dc902022-04-08 09:56:22 -050021constexpr auto POWER_CAP_SOFT_MIN = "MinSoftPowerCapValue";
Chris Cain5d66a0a2022-02-09 08:52:10 -060022constexpr auto POWER_CAP_HARD_MIN = "MinPowerCapValue";
23constexpr auto POWER_CAP_MAX = "MaxPowerCapValue";
Andrew Geissler52cf26a2017-07-06 12:56:32 -050024
George Liubcef3b42021-09-10 12:39:02 +080025namespace fs = std::filesystem;
Andrew Geissler32016d12017-06-20 15:46:52 -050026
Chris Cain5d66a0a2022-02-09 08:52:10 -060027void 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 Cain613dc902022-04-08 09:56:22 -050031 fs::path softMinName =
32 getPcapFilename(std::regex{"power\\d+_cap_min_soft$"});
Chris Cain5d66a0a2022-02-09 08:52:10 -060033 fs::path maxName = getPcapFilename(std::regex{"power\\d+_cap_max$"});
34
Chris Cain81c83432022-06-27 08:21:52 -050035 // Read the current cap bounds from dbus
36 uint32_t capSoftMin, capHardMin, capMax;
37 readDbusPcapLimits(capSoftMin, capHardMin, capMax);
Chris Cain613dc902022-04-08 09:56:22 -050038
Chris Cain81c83432022-06-27 08:21:52 -050039 // Read the power cap bounds from sysfs files (from OCC)
40 uint64_t cap;
Chris Cain613dc902022-04-08 09:56:22 -050041 std::ifstream softMinFile(softMinName, std::ios::in);
42 if (softMinFile)
43 {
44 softMinFile >> cap;
45 softMinFile.close();
Chris Cain81c83432022-06-27 08:21:52 -050046 // Convert to input/AC Power in Watts (round up)
Chris Cain613dc902022-04-08 09:56:22 -050047 capSoftMin = ((cap / (PS_DERATING_FACTOR / 100.0) / 1000000) + 0.9);
48 }
49 else
50 {
Chris Cain37abe9b2024-10-31 17:20:31 -050051 lg2::error(
52 "updatePcapBounds: unable to find pcap_min_soft file: {FILE} (errno={ERR})",
53 "FILE", pcapBasePathname, "ERR", errno);
Chris Cain613dc902022-04-08 09:56:22 -050054 }
55
Chris Cain5d66a0a2022-02-09 08:52:10 -060056 std::ifstream minFile(minName, std::ios::in);
57 if (minFile)
58 {
59 minFile >> cap;
60 minFile.close();
Chris Cain81c83432022-06-27 08:21:52 -050061 // Convert to input/AC Power in Watts (round up)
Chris Cain8676e032022-03-24 15:06:23 -050062 capHardMin = ((cap / (PS_DERATING_FACTOR / 100.0) / 1000000) + 0.9);
Chris Cain5d66a0a2022-02-09 08:52:10 -060063 }
64 else
65 {
Chris Cain37abe9b2024-10-31 17:20:31 -050066 lg2::error(
67 "updatePcapBounds: unable to find cap_min file: {FILE} (errno={ERR})",
68 "FILE", pcapBasePathname, "ERR", errno);
Chris Cain5d66a0a2022-02-09 08:52:10 -060069 }
Chris Cain613dc902022-04-08 09:56:22 -050070
Chris Cain5d66a0a2022-02-09 08:52:10 -060071 std::ifstream maxFile(maxName, std::ios::in);
72 if (maxFile)
73 {
74 maxFile >> cap;
75 maxFile.close();
Chris Cain81c83432022-06-27 08:21:52 -050076 // Convert to input/AC Power in Watts (truncate remainder)
Chris Cain8676e032022-03-24 15:06:23 -050077 capMax = cap / (PS_DERATING_FACTOR / 100.0) / 1000000;
Chris Cain5d66a0a2022-02-09 08:52:10 -060078 }
79 else
80 {
Chris Cain37abe9b2024-10-31 17:20:31 -050081 lg2::error(
82 "updatePcapBounds: unable to find cap_max file: {FILE} (errno={ERR})",
83 "FILE", pcapBasePathname, "ERR", errno);
Chris Cain5d66a0a2022-02-09 08:52:10 -060084 }
85
Chris Cain81c83432022-06-27 08:21:52 -050086 // Save the power cap bounds to dbus
87 updateDbusPcapLimits(capSoftMin, capHardMin, capMax);
Chris Cain40501a22022-03-14 17:33:27 -050088
Chris Cain81c83432022-06-27 08:21:52 -050089 // Validate user power cap (if enabled) is within the bounds
Chris Cain40501a22022-03-14 17:33:27 -050090 const uint32_t dbusUserCap = getPcap();
Chris Cain81c83432022-06-27 08:21:52 -050091 const bool pcapEnabled = getPcapEnabled();
92 if (pcapEnabled && (dbusUserCap != 0))
Chris Cain40501a22022-03-14 17:33:27 -050093 {
Chris Cain81c83432022-06-27 08:21:52 -050094 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 Cain37abe9b2024-10-31 17:20:31 -0500101 lg2::error(
102 "updatePcapBounds: user powercap mismatch (hwmon:{HCAP}W, bdus:{DCAP}W) - using dbus",
103 "HCAP", hwmonUserCap, "DCAP", dbusUserCap);
Chris Cain81c83432022-06-27 08:21:52 -0500104 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 Cain37abe9b2024-10-31 17:20:31 -0500116 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 Cain81c83432022-06-27 08:21:52 -0500121 try
122 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500123 lg2::info(
124 "updatePcapBounds: Updating user powercap from {OLD} to {NEW}W",
125 "OLD", hwmonUserCap, "NEW", newCap);
Chris Cain81c83432022-06-27 08:21:52 -0500126 utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP,
127 newCap);
128 auto occInput = getOccInput(newCap, pcapEnabled);
129 writeOcc(occInput);
130 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500131 catch (const sdbusplus::exception_t& e)
Chris Cain81c83432022-06-27 08:21:52 -0500132 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500133 lg2::error(
134 "updatePcapBounds: Failed to update user powercap due to {ERR}",
135 "ERR", e.what());
Chris Cain81c83432022-06-27 08:21:52 -0500136 }
137 }
Chris Cain40501a22022-03-14 17:33:27 -0500138 }
Chris Cain5d66a0a2022-02-09 08:52:10 -0600139}
140
Chris Cain81c83432022-06-27 08:21:52 -0500141// Get value of power cap to send to the OCC (output/DC power)
Andrew Geissler4cea4d22017-07-10 15:13:33 -0500142uint32_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 Cain81c83432022-06-27 08:21:52 -0500151 // factor applied (output/DC power).
Gunnar Mills94df8c92018-09-14 14:50:03 -0500152 return ((static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) / 100);
Andrew Geissler4cea4d22017-07-10 15:13:33 -0500153}
154
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500155uint32_t PowerCap::getPcap()
156{
George Liuf3b75142021-06-10 11:22:50 +0800157 utils::PropertyValue pcap{};
158 try
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500159 {
George Liuf3b75142021-06-10 11:22:50 +0800160 pcap = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP);
161
162 return std::get<uint32_t>(pcap);
163 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500164 catch (const sdbusplus::exception_t& e)
George Liuf3b75142021-06-10 11:22:50 +0800165 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500166 lg2::error("Failed to get PowerCap property: path={PATH}: {ERR}",
167 "PATH", PCAP_PATH, "ERR", e.what());
George Liuf3b75142021-06-10 11:22:50 +0800168
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500169 return 0;
170 }
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500171}
172
173bool PowerCap::getPcapEnabled()
174{
George Liuf3b75142021-06-10 11:22:50 +0800175 utils::PropertyValue pcapEnabled{};
176 try
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500177 {
George Liuf3b75142021-06-10 11:22:50 +0800178 pcapEnabled = utils::getProperty(PCAP_PATH, PCAP_INTERFACE,
179 POWER_CAP_ENABLE_PROP);
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500180
George Liuf3b75142021-06-10 11:22:50 +0800181 return std::get<bool>(pcapEnabled);
182 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500183 catch (const sdbusplus::exception_t& e)
George Liuf3b75142021-06-10 11:22:50 +0800184 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500185 lg2::error("Failed to get PowerCapEnable property: path={PATH}: {ERR}",
186 "PATH", PCAP_PATH, "ERR", e.what());
George Liuf3b75142021-06-10 11:22:50 +0800187
188 return false;
189 }
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500190}
Andrew Geissler32016d12017-06-20 15:46:52 -0500191
Chris Cain5d66a0a2022-02-09 08:52:10 -0600192fs::path PowerCap::getPcapFilename(const std::regex& expr)
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500193{
Chris Cain5d66a0a2022-02-09 08:52:10 -0600194 if (pcapBasePathname.empty())
195 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600196 pcapBasePathname = occStatus.getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -0600197 }
Chris Caine2d0a432022-03-28 11:08:49 -0500198
199 if (fs::exists(pcapBasePathname))
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500200 {
Chris Caine2d0a432022-03-28 11:08:49 -0500201 // Search for pcap file based on the supplied expr
202 for (auto& file : fs::directory_iterator(pcapBasePathname))
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500203 {
Chris Caine2d0a432022-03-28 11:08:49 -0500204 if (std::regex_search(file.path().string(), expr))
205 {
206 // Found match
207 return file;
208 }
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500209 }
210 }
Chris Caine2d0a432022-03-28 11:08:49 -0500211 else
212 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500213 lg2::error("Power Cap base filename not found: {FILE}", "FILE",
214 pcapBasePathname);
Chris Caine2d0a432022-03-28 11:08:49 -0500215 }
216
Chris Cain5d66a0a2022-02-09 08:52:10 -0600217 // return empty path
218 return fs::path{};
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500219}
220
Chris Cain81c83432022-06-27 08:21:52 -0500221// Write the user power cap to sysfs (output/DC power)
Chris Cain40501a22022-03-14 17:33:27 -0500222// This will trigger the driver to send the cap to the OCC
Andrew Geissler6ac874e2017-07-10 15:54:58 -0500223void PowerCap::writeOcc(uint32_t pcapValue)
224{
Chris Caind4c19a02022-04-22 15:46:13 -0500225 if (!occStatus.occActive())
226 {
227 // OCC not running, skip update
228 return;
229 }
230
Chris Cain5d66a0a2022-02-09 08:52:10 -0600231 // 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 Spinlereaaf3b22019-07-16 10:29:27 -0500234 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500235 lg2::error("Could not find a power cap file to write to: {FILE})",
236 "FILE", pcapBasePathname);
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500237 return;
238 }
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500239
240 uint64_t microWatts = pcapValue * 1000000ull;
241
242 auto pcapString{std::to_string(microWatts)};
Andrew Geissler6ac874e2017-07-10 15:54:58 -0500243
Andrew Geissler6ac874e2017-07-10 15:54:58 -0500244 // Open the hwmon file and write the power cap
Chris Cain5d66a0a2022-02-09 08:52:10 -0600245 std::ofstream file(fileName, std::ios::out);
246 if (file)
247 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500248 lg2::info("Writing {CAP}uW to {FILE}", "CAP", pcapString, "FILE",
249 fileName);
Chris Cain5d66a0a2022-02-09 08:52:10 -0600250 file << pcapString;
251 file.close();
252 }
253 else
254 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500255 lg2::error("Failed writing {CAP}uW to {FILE} (errno={ERR})", "CAP",
256 microWatts, "FILE", fileName, "ERR", errno);
Chris Cain5d66a0a2022-02-09 08:52:10 -0600257 }
258
Andrew Geissler6ac874e2017-07-10 15:54:58 -0500259 return;
260}
261
Chris Cain81c83432022-06-27 08:21:52 -0500262// Read the current user power cap from sysfs as input/AC power
Chris Cain40501a22022-03-14 17:33:27 -0500263uint32_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 Cain37abe9b2024-10-31 17:20:31 -0500271 lg2::error(
272 "readUserCapHwmon: Could not find a power cap file to read: {FILE})",
273 "FILE", pcapBasePathname);
Chris Cain40501a22022-03-14 17:33:27 -0500274 return 0;
275 }
276
277 // Open the sysfs file and read the power cap
Chris Cain40501a22022-03-14 17:33:27 -0500278 std::ifstream file(userCapName, std::ios::in);
279 if (file)
280 {
Chris Caine2d0a432022-03-28 11:08:49 -0500281 uint64_t cap;
Chris Cain40501a22022-03-14 17:33:27 -0500282 file >> cap;
283 file.close();
Chris Cain81c83432022-06-27 08:21:52 -0500284 // Convert to input/AC Power in Watts
285 userCap = (cap / (PS_DERATING_FACTOR / 100.0) / 1000000);
Chris Cain40501a22022-03-14 17:33:27 -0500286 }
287 else
288 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500289 lg2::error("readUserCapHwmon: Failed reading {FILE} (errno={ERR})",
290 "FILE", userCapName, "ERR", errno);
Chris Cain40501a22022-03-14 17:33:27 -0500291 }
292
293 return userCap;
294}
295
Patrick Williamsaf408082022-07-22 19:26:54 -0500296void PowerCap::pcapChanged(sdbusplus::message_t& msg)
Andrew Geissler32016d12017-06-20 15:46:52 -0500297{
Andrew Geissler32016d12017-06-20 15:46:52 -0500298 if (!occStatus.occActive())
299 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600300 // Nothing to do
Andrew Geissler32016d12017-06-20 15:46:52 -0500301 return;
302 }
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500303
304 uint32_t pcap = 0;
305 bool pcapEnabled = false;
306
307 std::string msgSensor;
Patrick Williamse0962702020-05-13 17:50:22 -0500308 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500309 msg.read(msgSensor, msgData);
310
Chris Cain5d66a0a2022-02-09 08:52:10 -0600311 bool changeFound = false;
312 for (const auto& [prop, value] : msgData)
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500313 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600314 if (prop == POWER_CAP_PROP)
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500315 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600316 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 Geissler52cf26a2017-07-06 12:56:32 -0500323 pcap = getPcap();
Chris Cain5d66a0a2022-02-09 08:52:10 -0600324 changeFound = true;
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500325 }
326 else
327 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600328 // Ignore other properties
Chris Cain37abe9b2024-10-31 17:20:31 -0500329 lg2::debug(
330 "pcapChanged: Unknown power cap property changed {PROP} to {VAL}",
331 "PROP", prop, "VAL", std::get<uint32_t>(value));
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500332 }
333 }
334
Chris Cain81c83432022-06-27 08:21:52 -0500335 // 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 Cain37abe9b2024-10-31 17:20:31 -0500340 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 Cain81c83432022-06-27 08:21:52 -0500343 pcap = capSoftMin;
344 utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, pcap);
345 }
346 else if (pcap > capMax)
347 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500348 lg2::error(
349 "pcapChanged: Power cap of {CAP}W is higher than allowed (max:{MAX}) - using max",
350 "CAP", pcap, "MAX", capMax);
Chris Cain81c83432022-06-27 08:21:52 -0500351 pcap = capMax;
352 utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, pcap);
353 }
354
Chris Cain5d66a0a2022-02-09 08:52:10 -0600355 if (changeFound)
356 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500357 lg2::info(
358 "Power Cap Property Change (cap={CAP}W (input), enabled={ENABLE})",
359 "CAP", pcap, "ENABLE", pcapEnabled);
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500360
Chris Cain5d66a0a2022-02-09 08:52:10 -0600361 // Determine desired action to write to occ
Chris Cain5d66a0a2022-02-09 08:52:10 -0600362 auto occInput = getOccInput(pcap, pcapEnabled);
Chris Cain5d66a0a2022-02-09 08:52:10 -0600363 // Write action to occ
364 writeOcc(occInput);
365 }
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500366
367 return;
Andrew Geissler32016d12017-06-20 15:46:52 -0500368}
369
Chris Cain5d66a0a2022-02-09 08:52:10 -0600370// Update the Power Cap bounds on DBus
Chris Cain81c83432022-06-27 08:21:52 -0500371bool PowerCap::updateDbusPcapLimits(uint32_t softMin, uint32_t hardMin,
372 uint32_t max)
Chris Cain5d66a0a2022-02-09 08:52:10 -0600373{
374 bool complete = true;
375
376 try
377 {
Chris Cain613dc902022-04-08 09:56:22 -0500378 utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_SOFT_MIN,
379 softMin);
380 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500381 catch (const sdbusplus::exception_t& e)
Chris Cain613dc902022-04-08 09:56:22 -0500382 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500383 lg2::error(
384 "updateDbusPcapLimits: Failed to set SOFT PCAP to {MIN}W due to {ERR}",
385 "MIN", softMin, "ERR", e.what());
Chris Cain613dc902022-04-08 09:56:22 -0500386 complete = false;
387 }
388
389 try
390 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600391 utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_HARD_MIN,
392 hardMin);
393 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500394 catch (const sdbusplus::exception_t& e)
Chris Cain5d66a0a2022-02-09 08:52:10 -0600395 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500396 lg2::error(
397 "updateDbusPcapLimits: Failed to set HARD PCAP to {MIN}W due to {ERR}",
398 "MIN", hardMin, "ERR", e.what());
Chris Cain5d66a0a2022-02-09 08:52:10 -0600399 complete = false;
400 }
401
402 try
403 {
404 utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_MAX, max);
405 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500406 catch (const sdbusplus::exception_t& e)
Chris Cain5d66a0a2022-02-09 08:52:10 -0600407 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500408 lg2::error(
409 "updateDbusPcapLimits: Failed to set MAX PCAP to {MAX}W due to {ERR}",
410 "MAX", max, "ERR", e.what());
Chris Cain5d66a0a2022-02-09 08:52:10 -0600411 complete = false;
412 }
413
414 return complete;
415}
Chris Cain81c83432022-06-27 08:21:52 -0500416
417// Read the Power Cap bounds from DBus
418bool 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 Williamsd7542c82024-08-16 15:20:28 -0400426 prop =
427 utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_SOFT_MIN);
Chris Cain81c83432022-06-27 08:21:52 -0500428 softMin = std::get<uint32_t>(prop);
429 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500430 catch (const sdbusplus::exception_t& e)
Chris Cain81c83432022-06-27 08:21:52 -0500431 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500432 lg2::error("readDbusPcapLimits: Failed to get SOFT PCAP due to {ERR}",
433 "ERR", e.what());
Chris Cain81c83432022-06-27 08:21:52 -0500434 softMin = 0;
435 complete = false;
436 }
437
438 try
439 {
Patrick Williamsd7542c82024-08-16 15:20:28 -0400440 prop =
441 utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_HARD_MIN);
Chris Cain81c83432022-06-27 08:21:52 -0500442 hardMin = std::get<uint32_t>(prop);
443 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500444 catch (const sdbusplus::exception_t& e)
Chris Cain81c83432022-06-27 08:21:52 -0500445 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500446 lg2::error("readDbusPcapLimits: Failed to get HARD PCAP due to {ERR}",
447 "ERR", e.what());
Chris Cain81c83432022-06-27 08:21:52 -0500448 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 Williamsaf408082022-07-22 19:26:54 -0500457 catch (const sdbusplus::exception_t& e)
Chris Cain81c83432022-06-27 08:21:52 -0500458 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500459 lg2::error("readDbusPcapLimits: Failed to get MAX PCAP due to {ERR}",
460 "ERR", e.what());
Chris Cain81c83432022-06-27 08:21:52 -0500461 max = INT_MAX;
462 complete = false;
463 }
464
465 return complete;
466}
467
Gunnar Mills94df8c92018-09-14 14:50:03 -0500468} // namespace powercap
Andrew Geissler32016d12017-06-20 15:46:52 -0500469
470} // namespace occ
471
Gunnar Mills94df8c92018-09-14 14:50:03 -0500472} // namespace open_power