xref: /openbmc/openpower-occ-control/powercap.hpp (revision 41470e56e5dce5d9d138fd2a136f0010571bc613)
1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/bus/match.hpp>
5 #include "occ_status.hpp"
6 #include "config.h"
7 
8 namespace open_power
9 {
10 namespace occ
11 {
12 namespace powercap
13 {
14 
15 namespace sdbusRule = sdbusplus::bus::match::rules;
16 
17 /** @class PowerCap
18  *  @brief Monitors for changes to the power cap and notifies occ
19  *
20  *  The customer power cap is provided to the OCC by host TMGT when the occ
21  *  first goes active or is reset.  This code is responsible for sending
22  *  the power cap to the OCC if the cap is changed while the occ is active.
23  */
24 
25 class PowerCap
26 {
27 public:
28     /** @brief PowerCap object to inform occ of changes to cap
29      *
30      * This object will monitor for changes to the power cap setting and
31      * power cap enable properties.  If a change is detected, and the occ
32      * is active, then this object will notify the OCC of the change.
33      *
34      * @param[in] bus       - The Dbus bus object
35      * @param[in] occStatus - The occ status object
36      */
37     PowerCap(sdbusplus::bus::bus &bus,
38              Status &occStatus,
39              const std::string& occMasterName = OCC_MASTER_NAME) :
40         bus(bus),
41         occMasterName(occMasterName),
42         occStatus(occStatus),
43         pcapMatch(
44                 bus,
45                 sdbusRule::member("PropertiesChanged") +
46                 sdbusRule::path(
47                     "/xyz/openbmc_project/control/host0/power_cap") +
48                 sdbusRule::argN(0, "xyz.openbmc_project.Control.Power.Cap") +
49                 sdbusRule::interface("org.freedesktop.DBus.Properties"),
50                 std::bind(std::mem_fn(&PowerCap::pcapChanged),
51                           this, std::placeholders::_1))
52     {};
53 
54     /** @brief Return the appropriate value to write to the OCC
55      *
56      * @param[in]  pcap        - Current user power cap setting
57      * @param[in]  pcapEnabled - Current power cap enable setting
58      *
59      * @return The value to write to the occ user pcap
60      */
61     uint32_t getOccInput(uint32_t pcap, bool pcapEnabled);
62 
63 private:
64 
65     /** @brief Callback for pcap setting changes
66      *
67      * Process change and inform OCC
68      *
69      * @param[in]  msg       - Data associated with pcap change signal
70      *
71      */
72     void pcapChanged(sdbusplus::message::message& msg);
73 
74     /** @brief Look up DBUS service for input path/interface
75      *
76      * @param[in]  path       - DBUS path
77      * @param[in]  path       - DBUS interface
78      *
79      * @return Distinct service name for input path/interface
80      */
81     std::string getService(std::string path,
82                            std::string interface);
83 
84     /** @brief Get the power cap property
85      *
86      * @return Power cap, 0 on failure to indicate no pcap
87      */
88     uint32_t getPcap();
89 
90     /** @brief Get the power cap enable property
91      *
92      * @return Whether power cap enabled, will return false on error
93      */
94     bool getPcapEnabled();
95 
96     /** @brief Write the input power cap to the occ hwmon entry
97      *
98      * @param[in]  pcapValue - Power cap value to write to OCC
99      */
100     void writeOcc(uint32_t pcapValue);
101 
102     /** @brief Reference to sdbus **/
103     sdbusplus::bus::bus& bus;
104 
105     /** @brief The master occ name */
106     std::string occMasterName;
107 
108     /* @brief OCC Status object */
109     Status &occStatus;
110 
111     /** @brief Used to subscribe to dbus pcap propety changes **/
112     sdbusplus::bus::match_t pcapMatch;
113 
114  };
115 
116 } // namespace open_power
117 
118 } // namespace occ
119 
120 }// namespace powercap
121