blob: 34df45fbb16260a2d828e2c6f6d802b69ae81799 [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
Matt Spinlereaaf3b22019-07-16 10:29:27 -050096 /**
97 * @brief Returns the filename to use for the user power cap
98 *
99 * The file is of the form "powerX_cap_user", where X is any
100 * number.
101 *
Chris Cain5d66a0a2022-02-09 08:52:10 -0600102 * @param[in] expr - Regular expression of file to find
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500103 *
Chris Cain5d66a0a2022-02-09 08:52:10 -0600104 * @return full path/filename, or empty path if not found.
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500105 */
Chris Cain5d66a0a2022-02-09 08:52:10 -0600106 fs::path getPcapFilename(const std::regex& expr);
Lei YU41470e52017-11-30 16:03:50 +0800107
Andrew Geissler32016d12017-06-20 15:46:52 -0500108 /* @brief OCC Status object */
Gunnar Mills94df8c92018-09-14 14:50:03 -0500109 Status& occStatus;
Andrew Geissler32016d12017-06-20 15:46:52 -0500110
Gunnar Mills85e65202018-04-08 15:01:54 -0500111 /** @brief Used to subscribe to dbus pcap property changes **/
Andrew Geissler32016d12017-06-20 15:46:52 -0500112 sdbusplus::bus::match_t pcapMatch;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600113
114 /** @brief Path to the sysfs files holding the cap properties **/
115 fs::path pcapBasePathname;
116
117 /** @brief Update the power cap bounds on DBus
118 *
119 * @param[in] softMin - soft minimum power cap in Watts
120 * @param[in] hardMin - hard minimum power cap in Watts
121 * @param[in] pcapMax - maximum power cap in Watts
122 *
123 * @return true if all parms were written successfully
124 */
125 bool updateDbusPcap(uint32_t softMin, uint32_t hardMin, uint32_t pcapMax);
Gunnar Mills94df8c92018-09-14 14:50:03 -0500126};
Andrew Geissler32016d12017-06-20 15:46:52 -0500127
Gunnar Mills94df8c92018-09-14 14:50:03 -0500128} // namespace powercap
Andrew Geissler32016d12017-06-20 15:46:52 -0500129
130} // namespace occ
131
Gunnar Mills94df8c92018-09-14 14:50:03 -0500132} // namespace open_power