Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 5 | #include "utils.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 6 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/bus/match.hpp> |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 9 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 10 | #include <filesystem> |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 11 | #include <regex> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 12 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 13 | namespace open_power |
| 14 | { |
| 15 | namespace occ |
| 16 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 17 | class Status; |
| 18 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 19 | namespace powercap |
| 20 | { |
| 21 | |
| 22 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 24 | |
| 25 | /** @class PowerCap |
| 26 | * @brief Monitors for changes to the power cap and notifies occ |
| 27 | * |
| 28 | * The customer power cap is provided to the OCC by host TMGT when the occ |
| 29 | * first goes active or is reset. This code is responsible for sending |
| 30 | * the power cap to the OCC if the cap is changed while the occ is active. |
| 31 | */ |
| 32 | |
| 33 | class PowerCap |
| 34 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 35 | public: |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 36 | /** @brief PowerCap object to inform occ of changes to cap |
| 37 | * |
| 38 | * This object will monitor for changes to the power cap setting and |
| 39 | * power cap enable properties. If a change is detected, and the occ |
| 40 | * is active, then this object will notify the OCC of the change. |
| 41 | * |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 42 | * @param[in] occStatus - The occ status object |
| 43 | */ |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 44 | explicit PowerCap(Status& occStatus) : |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 45 | occStatus(occStatus), |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 46 | pcapMatch( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 47 | utils::getBus(), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 48 | sdbusRule::member("PropertiesChanged") + |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 49 | sdbusRule::path( |
| 50 | "/xyz/openbmc_project/control/host0/power_cap") + |
| 51 | sdbusRule::argN(0, "xyz.openbmc_project.Control.Power.Cap") + |
| 52 | sdbusRule::interface("org.freedesktop.DBus.Properties"), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 53 | std::bind(std::mem_fn(&PowerCap::pcapChanged), this, |
| 54 | std::placeholders::_1)){}; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 55 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 56 | /** @brief Return the appropriate value to write to the OCC |
| 57 | * |
| 58 | * @param[in] pcap - Current user power cap setting |
| 59 | * @param[in] pcapEnabled - Current power cap enable setting |
| 60 | * |
| 61 | * @return The value to write to the occ user pcap |
| 62 | */ |
| 63 | uint32_t getOccInput(uint32_t pcap, bool pcapEnabled); |
| 64 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 65 | /** @brief Read the power cap bounds from sysfs and update DBus */ |
| 66 | void updatePcapBounds(); |
| 67 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 68 | private: |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 69 | /** @brief Callback for pcap setting changes |
| 70 | * |
| 71 | * Process change and inform OCC |
| 72 | * |
| 73 | * @param[in] msg - Data associated with pcap change signal |
| 74 | * |
| 75 | */ |
| 76 | void pcapChanged(sdbusplus::message::message& msg); |
| 77 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 78 | /** @brief Get the power cap property |
| 79 | * |
| 80 | * @return Power cap, 0 on failure to indicate no pcap |
| 81 | */ |
| 82 | uint32_t getPcap(); |
| 83 | |
| 84 | /** @brief Get the power cap enable property |
| 85 | * |
| 86 | * @return Whether power cap enabled, will return false on error |
| 87 | */ |
| 88 | bool getPcapEnabled(); |
| 89 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 90 | /** @brief Write the input power cap to the occ hwmon entry |
| 91 | * |
| 92 | * @param[in] pcapValue - Power cap value to write to OCC |
| 93 | */ |
| 94 | void writeOcc(uint32_t pcapValue); |
| 95 | |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 96 | /** @brief Read the user power cap from sysfs |
| 97 | * |
| 98 | * @return User power cap value in Watts or 0 if disabled |
| 99 | */ |
| 100 | uint32_t readUserCapHwmon(); |
| 101 | |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 102 | /** |
| 103 | * @brief Returns the filename to use for the user power cap |
| 104 | * |
| 105 | * The file is of the form "powerX_cap_user", where X is any |
| 106 | * number. |
| 107 | * |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 108 | * @param[in] expr - Regular expression of file to find |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 109 | * |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 110 | * @return full path/filename, or empty path if not found. |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 111 | */ |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 112 | fs::path getPcapFilename(const std::regex& expr); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 113 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 114 | /* @brief OCC Status object */ |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 115 | Status& occStatus; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 116 | |
Gunnar Mills | 85e6520 | 2018-04-08 15:01:54 -0500 | [diff] [blame] | 117 | /** @brief Used to subscribe to dbus pcap property changes **/ |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 118 | sdbusplus::bus::match_t pcapMatch; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 119 | |
| 120 | /** @brief Path to the sysfs files holding the cap properties **/ |
| 121 | fs::path pcapBasePathname; |
| 122 | |
| 123 | /** @brief Update the power cap bounds on DBus |
| 124 | * |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 125 | * @param[in] hardMin - hard minimum power cap in Watts |
| 126 | * @param[in] pcapMax - maximum power cap in Watts |
| 127 | * |
| 128 | * @return true if all parms were written successfully |
| 129 | */ |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 130 | bool updateDbusPcap(uint32_t hardMin, uint32_t pcapMax); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 131 | }; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 132 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 133 | } // namespace powercap |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 134 | |
| 135 | } // namespace occ |
| 136 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 137 | } // namespace open_power |