blob: 58070ba957c635e5b4a1c212702e368a9c8c52fe [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
Andrew Geissler32016d12017-06-20 15:46:52 -05008#include <sdbusplus/bus.hpp>
9#include <sdbusplus/bus/match.hpp>
Andrew Geissler32016d12017-06-20 15:46:52 -050010
George Liubcef3b42021-09-10 12:39:02 +080011#include <filesystem>
George Liub5ca1012021-09-10 12:53:11 +080012
Andrew Geissler32016d12017-06-20 15:46:52 -050013namespace open_power
14{
15namespace occ
16{
17namespace powercap
18{
19
20namespace sdbusRule = sdbusplus::bus::match::rules;
21
22/** @class PowerCap
23 * @brief Monitors for changes to the power cap and notifies occ
24 *
25 * The customer power cap is provided to the OCC by host TMGT when the occ
26 * first goes active or is reset. This code is responsible for sending
27 * the power cap to the OCC if the cap is changed while the occ is active.
28 */
29
30class PowerCap
31{
Gunnar Mills94df8c92018-09-14 14:50:03 -050032 public:
Andrew Geissler32016d12017-06-20 15:46:52 -050033 /** @brief PowerCap object to inform occ of changes to cap
34 *
35 * This object will monitor for changes to the power cap setting and
36 * power cap enable properties. If a change is detected, and the occ
37 * is active, then this object will notify the OCC of the change.
38 *
Andrew Geissler32016d12017-06-20 15:46:52 -050039 * @param[in] occStatus - The occ status object
40 */
George Liuf3b75142021-06-10 11:22:50 +080041 PowerCap(Status& occStatus,
Lei YU41470e52017-11-30 16:03:50 +080042 const std::string& occMasterName = OCC_MASTER_NAME) :
George Liuf3b75142021-06-10 11:22:50 +080043 occMasterName(occMasterName),
44 occStatus(occStatus),
Andrew Geissler32016d12017-06-20 15:46:52 -050045 pcapMatch(
George Liuf3b75142021-06-10 11:22:50 +080046 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050047 sdbusRule::member("PropertiesChanged") +
Andrew Geissler32016d12017-06-20 15:46:52 -050048 sdbusRule::path(
49 "/xyz/openbmc_project/control/host0/power_cap") +
50 sdbusRule::argN(0, "xyz.openbmc_project.Control.Power.Cap") +
51 sdbusRule::interface("org.freedesktop.DBus.Properties"),
Gunnar Mills94df8c92018-09-14 14:50:03 -050052 std::bind(std::mem_fn(&PowerCap::pcapChanged), this,
53 std::placeholders::_1)){};
Andrew Geissler32016d12017-06-20 15:46:52 -050054
Andrew Geissler4cea4d22017-07-10 15:13:33 -050055 /** @brief Return the appropriate value to write to the OCC
56 *
57 * @param[in] pcap - Current user power cap setting
58 * @param[in] pcapEnabled - Current power cap enable setting
59 *
60 * @return The value to write to the occ user pcap
61 */
62 uint32_t getOccInput(uint32_t pcap, bool pcapEnabled);
63
Gunnar Mills94df8c92018-09-14 14:50:03 -050064 private:
Andrew Geissler32016d12017-06-20 15:46:52 -050065 /** @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
Andrew Geissler52cf26a2017-07-06 12:56:32 -050074 /** @brief Get the power cap property
75 *
76 * @return Power cap, 0 on failure to indicate no pcap
77 */
78 uint32_t getPcap();
79
80 /** @brief Get the power cap enable property
81 *
82 * @return Whether power cap enabled, will return false on error
83 */
84 bool getPcapEnabled();
85
Andrew Geissler6ac874e2017-07-10 15:54:58 -050086 /** @brief Write the input power cap to the occ hwmon entry
87 *
88 * @param[in] pcapValue - Power cap value to write to OCC
89 */
90 void writeOcc(uint32_t pcapValue);
91
Matt Spinlereaaf3b22019-07-16 10:29:27 -050092 /**
93 * @brief Returns the filename to use for the user power cap
94 *
95 * The file is of the form "powerX_cap_user", where X is any
96 * number.
97 *
98 * @param[in] path - The directory to look for the file in
99 *
100 * @return std::string - The filename, or empty string if not found.
101 */
George Liubcef3b42021-09-10 12:39:02 +0800102 std::string getPcapFilename(const std::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