blob: c7861a421c98182c7fed56d16478b7ee790ee88e [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"
6
Matt Spinler00a64782019-07-24 14:29:24 -05007#include <experimental/filesystem>
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
11namespace open_power
12{
13namespace occ
14{
15namespace powercap
16{
17
18namespace sdbusRule = sdbusplus::bus::match::rules;
19
20/** @class PowerCap
21 * @brief Monitors for changes to the power cap and notifies occ
22 *
23 * The customer power cap is provided to the OCC by host TMGT when the occ
24 * first goes active or is reset. This code is responsible for sending
25 * the power cap to the OCC if the cap is changed while the occ is active.
26 */
27
28class PowerCap
29{
Gunnar Mills94df8c92018-09-14 14:50:03 -050030 public:
Andrew Geissler32016d12017-06-20 15:46:52 -050031 /** @brief PowerCap object to inform occ of changes to cap
32 *
33 * This object will monitor for changes to the power cap setting and
34 * power cap enable properties. If a change is detected, and the occ
35 * is active, then this object will notify the OCC of the change.
36 *
37 * @param[in] bus - The Dbus bus object
38 * @param[in] occStatus - The occ status object
39 */
Gunnar Mills94df8c92018-09-14 14:50:03 -050040 PowerCap(sdbusplus::bus::bus& bus, Status& occStatus,
Lei YU41470e52017-11-30 16:03:50 +080041 const std::string& occMasterName = OCC_MASTER_NAME) :
Andrew Geissler32016d12017-06-20 15:46:52 -050042 bus(bus),
Gunnar Mills94df8c92018-09-14 14:50:03 -050043 occMasterName(occMasterName), occStatus(occStatus),
Andrew Geissler32016d12017-06-20 15:46:52 -050044 pcapMatch(
Gunnar Mills94df8c92018-09-14 14:50:03 -050045 bus,
46 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 Look up DBUS service for input path/interface
74 *
75 * @param[in] path - DBUS path
76 * @param[in] path - DBUS interface
77 *
78 * @return Distinct service name for input path/interface
79 */
Gunnar Mills94df8c92018-09-14 14:50:03 -050080 std::string getService(std::string path, std::string interface);
Andrew Geissler52cf26a2017-07-06 12:56:32 -050081
82 /** @brief Get the power cap property
83 *
84 * @return Power cap, 0 on failure to indicate no pcap
85 */
86 uint32_t getPcap();
87
88 /** @brief Get the power cap enable property
89 *
90 * @return Whether power cap enabled, will return false on error
91 */
92 bool getPcapEnabled();
93
Andrew Geissler6ac874e2017-07-10 15:54:58 -050094 /** @brief Write the input power cap to the occ hwmon entry
95 *
96 * @param[in] pcapValue - Power cap value to write to OCC
97 */
98 void writeOcc(uint32_t pcapValue);
99
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500100 /**
101 * @brief Returns the filename to use for the user power cap
102 *
103 * The file is of the form "powerX_cap_user", where X is any
104 * number.
105 *
106 * @param[in] path - The directory to look for the file in
107 *
108 * @return std::string - The filename, or empty string if not found.
109 */
Matt Spinler00a64782019-07-24 14:29:24 -0500110 std::string
111 getPcapFilename(const std::experimental::filesystem::path& path);
Matt Spinlereaaf3b22019-07-16 10:29:27 -0500112
Andrew Geissler32016d12017-06-20 15:46:52 -0500113 /** @brief Reference to sdbus **/
114 sdbusplus::bus::bus& bus;
115
Lei YU41470e52017-11-30 16:03:50 +0800116 /** @brief The master occ name */
117 std::string occMasterName;
118
Andrew Geissler32016d12017-06-20 15:46:52 -0500119 /* @brief OCC Status object */
Gunnar Mills94df8c92018-09-14 14:50:03 -0500120 Status& occStatus;
Andrew Geissler32016d12017-06-20 15:46:52 -0500121
Gunnar Mills85e65202018-04-08 15:01:54 -0500122 /** @brief Used to subscribe to dbus pcap property changes **/
Andrew Geissler32016d12017-06-20 15:46:52 -0500123 sdbusplus::bus::match_t pcapMatch;
Gunnar Mills94df8c92018-09-14 14:50:03 -0500124};
Andrew Geissler32016d12017-06-20 15:46:52 -0500125
Gunnar Mills94df8c92018-09-14 14:50:03 -0500126} // namespace powercap
Andrew Geissler32016d12017-06-20 15:46:52 -0500127
128} // namespace occ
129
Gunnar Mills94df8c92018-09-14 14:50:03 -0500130} // namespace open_power