blob: 3ef09c94f078fe7445f267b42733b1fc2566a321 [file] [log] [blame]
Andrew Geissler32016d12017-06-20 15:46:52 -05001#pragma once
2
Gunnar Mills94df8c92018-09-14 14:50:03 -05003#include "config.h"
4
George Liuf3b75142021-06-10 11:22:50 +08005#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05006
Andrew Geissler32016d12017-06-20 15:46:52 -05007#include <sdbusplus/bus.hpp>
8#include <sdbusplus/bus/match.hpp>
Andrew Geissler32016d12017-06-20 15:46:52 -05009
George Liubcef3b42021-09-10 12:39:02 +080010#include <filesystem>
Chris Cain5d66a0a2022-02-09 08:52:10 -060011#include <regex>
George Liub5ca1012021-09-10 12:53:11 +080012
Andrew Geissler32016d12017-06-20 15:46:52 -050013namespace open_power
14{
15namespace occ
16{
Chris Cain5d66a0a2022-02-09 08:52:10 -060017class Status;
18
Andrew Geissler32016d12017-06-20 15:46:52 -050019namespace powercap
20{
21
22namespace sdbusRule = sdbusplus::bus::match::rules;
Chris Cain5d66a0a2022-02-09 08:52:10 -060023namespace fs = std::filesystem;
Andrew Geissler32016d12017-06-20 15:46:52 -050024
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
33class PowerCap
34{
Gunnar Mills94df8c92018-09-14 14:50:03 -050035 public:
Andrew Geissler32016d12017-06-20 15:46:52 -050036 /** @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 Geissler32016d12017-06-20 15:46:52 -050042 * @param[in] occStatus - The occ status object
43 */
Chris Cain5d66a0a2022-02-09 08:52:10 -060044 explicit PowerCap(Status& occStatus) :
George Liuf3b75142021-06-10 11:22:50 +080045 occStatus(occStatus),
Andrew Geissler32016d12017-06-20 15:46:52 -050046 pcapMatch(
George Liuf3b75142021-06-10 11:22:50 +080047 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050048 sdbusRule::member("PropertiesChanged") +
Andrew Geissler32016d12017-06-20 15:46:52 -050049 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 Mills94df8c92018-09-14 14:50:03 -050053 std::bind(std::mem_fn(&PowerCap::pcapChanged), this,
54 std::placeholders::_1)){};
Andrew Geissler32016d12017-06-20 15:46:52 -050055
Chris Cain81c83432022-06-27 08:21:52 -050056 /** @brief Return the appropriate value to write to the OCC (output/DC
57 * power)
Andrew Geissler4cea4d22017-07-10 15:13:33 -050058 *
Chris Cain81c83432022-06-27 08:21:52 -050059 * @param[in] pcap - Current user power cap setting (input/AC power)
Andrew Geissler4cea4d22017-07-10 15:13:33 -050060 * @param[in] pcapEnabled - Current power cap enable setting
61 *
62 * @return The value to write to the occ user pcap
63 */
64 uint32_t getOccInput(uint32_t pcap, bool pcapEnabled);
65
Chris Cain5d66a0a2022-02-09 08:52:10 -060066 /** @brief Read the power cap bounds from sysfs and update DBus */
67 void updatePcapBounds();
68
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 private:
Andrew Geissler32016d12017-06-20 15:46:52 -050070 /** @brief Callback for pcap setting changes
71 *
72 * Process change and inform OCC
73 *
74 * @param[in] msg - Data associated with pcap change signal
75 *
76 */
Patrick Williamsaf408082022-07-22 19:26:54 -050077 void pcapChanged(sdbusplus::message_t& msg);
Andrew Geissler32016d12017-06-20 15:46:52 -050078
Andrew Geissler52cf26a2017-07-06 12:56:32 -050079 /** @brief Get the power cap property
80 *
81 * @return Power cap, 0 on failure to indicate no pcap
82 */
83 uint32_t getPcap();
84
85 /** @brief Get the power cap enable property
86 *
87 * @return Whether power cap enabled, will return false on error
88 */
89 bool getPcapEnabled();
90
Chris Cain81c83432022-06-27 08:21:52 -050091 /** @brief Write the output/DC power cap to the occ hwmon entry
Andrew Geissler6ac874e2017-07-10 15:54:58 -050092 *
93 * @param[in] pcapValue - Power cap value to write to OCC
94 */
95 void writeOcc(uint32_t pcapValue);
96
Chris Cain40501a22022-03-14 17:33:27 -050097 /** @brief Read the user power cap from sysfs
98 *
99 * @return User power cap value in Watts or 0 if disabled
100 */
101 uint32_t readUserCapHwmon();
102
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500103 /**
104 * @brief Returns the filename to use for the user power cap
105 *
106 * The file is of the form "powerX_cap_user", where X is any
107 * number.
108 *
Chris Cain5d66a0a2022-02-09 08:52:10 -0600109 * @param[in] expr - Regular expression of file to find
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500110 *
Chris Cain5d66a0a2022-02-09 08:52:10 -0600111 * @return full path/filename, or empty path if not found.
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500112 */
Chris Cain5d66a0a2022-02-09 08:52:10 -0600113 fs::path getPcapFilename(const std::regex& expr);
Lei YU41470e52017-11-30 16:03:50 +0800114
Andrew Geissler32016d12017-06-20 15:46:52 -0500115 /* @brief OCC Status object */
Gunnar Mills94df8c92018-09-14 14:50:03 -0500116 Status& occStatus;
Andrew Geissler32016d12017-06-20 15:46:52 -0500117
Gunnar Mills85e65202018-04-08 15:01:54 -0500118 /** @brief Used to subscribe to dbus pcap property changes **/
Andrew Geissler32016d12017-06-20 15:46:52 -0500119 sdbusplus::bus::match_t pcapMatch;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600120
121 /** @brief Path to the sysfs files holding the cap properties **/
122 fs::path pcapBasePathname;
123
124 /** @brief Update the power cap bounds on DBus
125 *
Chris Cain613dc902022-04-08 09:56:22 -0500126 * @param[in] softMin - soft minimum power cap in Watts
Chris Cain5d66a0a2022-02-09 08:52:10 -0600127 * @param[in] hardMin - hard minimum power cap in Watts
128 * @param[in] pcapMax - maximum power cap in Watts
129 *
130 * @return true if all parms were written successfully
131 */
Chris Cain81c83432022-06-27 08:21:52 -0500132 bool updateDbusPcapLimits(uint32_t softMin, uint32_t hardMin,
133 uint32_t pcapMax);
134
135 /** @brief Read the power cap bounds from DBus
136 *
137 * @param[out] softMin - soft minimum power cap in Watts
138 * @param[out] hardMin - hard minimum power cap in Watts
139 * @param[out] pcapMax - maximum power cap in Watts
140 *
141 * @return true if all parms were read successfully
142 * If a parm is not successfully read, it will default to 0 for the
143 * Min parameter and INT_MAX for the Max parameter
144 */
145 bool readDbusPcapLimits(uint32_t& softMin, uint32_t& hardMin,
146 uint32_t& max);
Gunnar Mills94df8c92018-09-14 14:50:03 -0500147};
Andrew Geissler32016d12017-06-20 15:46:52 -0500148
Gunnar Mills94df8c92018-09-14 14:50:03 -0500149} // namespace powercap
Andrew Geissler32016d12017-06-20 15:46:52 -0500150
151} // namespace occ
152
Gunnar Mills94df8c92018-09-14 14:50:03 -0500153} // namespace open_power