blob: 6d611db717f60408ea3c27da1ea0c3c9bc809a8d [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
Andrew Geissler4cea4d22017-07-10 15:13:33 -050056 /** @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 Cain5d66a0a2022-02-09 08:52:10 -060065 /** @brief Read the power cap bounds from sysfs and update DBus */
66 void updatePcapBounds();
67
Gunnar Mills94df8c92018-09-14 14:50:03 -050068 private:
Andrew Geissler32016d12017-06-20 15:46:52 -050069 /** @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 Geissler52cf26a2017-07-06 12:56:32 -050078 /** @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 Geissler6ac874e2017-07-10 15:54:58 -050090 /** @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 Cain40501a22022-03-14 17:33:27 -050096 /** @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 Spinlereaaf3b22019-07-16 10:29:27 -0500102 /**
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 Cain5d66a0a2022-02-09 08:52:10 -0600108 * @param[in] expr - Regular expression of file to find
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500109 *
Chris Cain5d66a0a2022-02-09 08:52:10 -0600110 * @return full path/filename, or empty path if not found.
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500111 */
Chris Cain5d66a0a2022-02-09 08:52:10 -0600112 fs::path getPcapFilename(const std::regex& expr);
Lei YU41470e52017-11-30 16:03:50 +0800113
Andrew Geissler32016d12017-06-20 15:46:52 -0500114 /* @brief OCC Status object */
Gunnar Mills94df8c92018-09-14 14:50:03 -0500115 Status& occStatus;
Andrew Geissler32016d12017-06-20 15:46:52 -0500116
Gunnar Mills85e65202018-04-08 15:01:54 -0500117 /** @brief Used to subscribe to dbus pcap property changes **/
Andrew Geissler32016d12017-06-20 15:46:52 -0500118 sdbusplus::bus::match_t pcapMatch;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600119
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 Cain5d66a0a2022-02-09 08:52:10 -0600125 * @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 Cain40501a22022-03-14 17:33:27 -0500130 bool updateDbusPcap(uint32_t hardMin, uint32_t pcapMax);
Gunnar Mills94df8c92018-09-14 14:50:03 -0500131};
Andrew Geissler32016d12017-06-20 15:46:52 -0500132
Gunnar Mills94df8c92018-09-14 14:50:03 -0500133} // namespace powercap
Andrew Geissler32016d12017-06-20 15:46:52 -0500134
135} // namespace occ
136
Gunnar Mills94df8c92018-09-14 14:50:03 -0500137} // namespace open_power