blob: bb4dc8b2a42d4f0f7ac6ea830a243629d01d7601 [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
5#include "occ_status.hpp"
George Liuf3b75142021-06-10 11:22:50 +08006#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05007
Matt Spinler00a64782019-07-24 14:29:24 -05008#include <experimental/filesystem>
Andrew Geissler32016d12017-06-20 15:46:52 -05009#include <sdbusplus/bus.hpp>
10#include <sdbusplus/bus/match.hpp>
Andrew Geissler32016d12017-06-20 15:46:52 -050011
12namespace open_power
13{
14namespace occ
15{
16namespace powercap
17{
18
19namespace sdbusRule = sdbusplus::bus::match::rules;
20
21/** @class PowerCap
22 * @brief Monitors for changes to the power cap and notifies occ
23 *
24 * The customer power cap is provided to the OCC by host TMGT when the occ
25 * first goes active or is reset. This code is responsible for sending
26 * the power cap to the OCC if the cap is changed while the occ is active.
27 */
28
29class PowerCap
30{
Gunnar Mills94df8c92018-09-14 14:50:03 -050031 public:
Andrew Geissler32016d12017-06-20 15:46:52 -050032 /** @brief PowerCap object to inform occ of changes to cap
33 *
34 * This object will monitor for changes to the power cap setting and
35 * power cap enable properties. If a change is detected, and the occ
36 * is active, then this object will notify the OCC of the change.
37 *
Andrew Geissler32016d12017-06-20 15:46:52 -050038 * @param[in] occStatus - The occ status object
39 */
George Liuf3b75142021-06-10 11:22:50 +080040 PowerCap(Status& occStatus,
Lei YU41470e52017-11-30 16:03:50 +080041 const std::string& occMasterName = OCC_MASTER_NAME) :
George Liuf3b75142021-06-10 11:22:50 +080042 occMasterName(occMasterName),
43 occStatus(occStatus),
Andrew Geissler32016d12017-06-20 15:46:52 -050044 pcapMatch(
George Liuf3b75142021-06-10 11:22:50 +080045 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050046 sdbusRule::member("PropertiesChanged") +
Andrew Geissler32016d12017-06-20 15:46:52 -050047 sdbusRule::path(
48 "/xyz/openbmc_project/control/host0/power_cap") +
49 sdbusRule::argN(0, "xyz.openbmc_project.Control.Power.Cap") +
50 sdbusRule::interface("org.freedesktop.DBus.Properties"),
Gunnar Mills94df8c92018-09-14 14:50:03 -050051 std::bind(std::mem_fn(&PowerCap::pcapChanged), this,
52 std::placeholders::_1)){};
Andrew Geissler32016d12017-06-20 15:46:52 -050053
Andrew Geissler4cea4d22017-07-10 15:13:33 -050054 /** @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
Gunnar Mills94df8c92018-09-14 14:50:03 -050063 private:
Andrew Geissler32016d12017-06-20 15:46:52 -050064 /** @brief Callback for pcap setting changes
65 *
66 * Process change and inform OCC
67 *
68 * @param[in] msg - Data associated with pcap change signal
69 *
70 */
71 void pcapChanged(sdbusplus::message::message& msg);
72
Andrew Geissler52cf26a2017-07-06 12:56:32 -050073 /** @brief Get the power cap property
74 *
75 * @return Power cap, 0 on failure to indicate no pcap
76 */
77 uint32_t getPcap();
78
79 /** @brief Get the power cap enable property
80 *
81 * @return Whether power cap enabled, will return false on error
82 */
83 bool getPcapEnabled();
84
Andrew Geissler6ac874e2017-07-10 15:54:58 -050085 /** @brief Write the input power cap to the occ hwmon entry
86 *
87 * @param[in] pcapValue - Power cap value to write to OCC
88 */
89 void writeOcc(uint32_t pcapValue);
90
Matt Spinlereaaf3b22019-07-16 10:29:27 -050091 /**
92 * @brief Returns the filename to use for the user power cap
93 *
94 * The file is of the form "powerX_cap_user", where X is any
95 * number.
96 *
97 * @param[in] path - The directory to look for the file in
98 *
99 * @return std::string - The filename, or empty string if not found.
100 */
Matt Spinler00a64782019-07-24 14:29:24 -0500101 std::string
102 getPcapFilename(const std::experimental::filesystem::path& path);
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500103
Lei YU41470e52017-11-30 16:03:50 +0800104 /** @brief The master occ name */
105 std::string occMasterName;
106
Andrew Geissler32016d12017-06-20 15:46:52 -0500107 /* @brief OCC Status object */
Gunnar Mills94df8c92018-09-14 14:50:03 -0500108 Status& occStatus;
Andrew Geissler32016d12017-06-20 15:46:52 -0500109
Gunnar Mills85e65202018-04-08 15:01:54 -0500110 /** @brief Used to subscribe to dbus pcap property changes **/
Andrew Geissler32016d12017-06-20 15:46:52 -0500111 sdbusplus::bus::match_t pcapMatch;
Gunnar Mills94df8c92018-09-14 14:50:03 -0500112};
Andrew Geissler32016d12017-06-20 15:46:52 -0500113
Gunnar Mills94df8c92018-09-14 14:50:03 -0500114} // namespace powercap
Andrew Geissler32016d12017-06-20 15:46:52 -0500115
116} // namespace occ
117
Gunnar Mills94df8c92018-09-14 14:50:03 -0500118} // namespace open_power