blob: 4db762c06ff28813fb6ba50eff2d1bcaecb7d9a3 [file] [log] [blame]
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +05301#pragma once
2
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +05303#include "occ_pass_through.hpp"
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05304#include "occ_status.hpp"
Tom Joseph815f9f52020-07-27 12:12:13 +05305#ifdef PLDM
6#include "pldm.hpp"
7#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05308#include "powercap.hpp"
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +05309
Gunnar Mills94df8c92018-09-14 14:50:03 -050010#include <cstring>
11#include <functional>
12#include <sdbusplus/bus.hpp>
Chris Caina8857c52021-01-27 11:53:05 -060013#include <sdeventplus/event.hpp>
14#include <sdeventplus/utility/timer.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -050015#include <vector>
16
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053017namespace sdbusRule = sdbusplus::bus::match::rules;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053018namespace open_power
19{
20namespace occ
21{
22
Chris Caina8857c52021-01-27 11:53:05 -060023/** @brief Default time, in seconds, between OCC poll commands */
24constexpr unsigned int defaultPollingInterval = 10;
25
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053026/** @class Manager
27 * @brief Builds and manages OCC objects
28 */
29struct Manager
30{
Gunnar Mills94df8c92018-09-14 14:50:03 -050031 public:
32 Manager() = delete;
33 Manager(const Manager&) = delete;
34 Manager& operator=(const Manager&) = delete;
35 Manager(Manager&&) = delete;
36 Manager& operator=(Manager&&) = delete;
37 ~Manager() = default;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053038
Gunnar Mills94df8c92018-09-14 14:50:03 -050039 /** @brief Adds OCC pass-through and status objects on the bus
40 * when corresponding CPU inventory is created.
41 *
42 * @param[in] bus - handle to the bus
43 * @param[in] event - Unique ptr reference to sd_event
44 */
Tom Joseph815f9f52020-07-27 12:12:13 +053045 Manager(sdbusplus::bus::bus& bus, EventPtr& event) :
Chris Caina8857c52021-01-27 11:53:05 -060046 bus(bus), event(event), pollInterval(defaultPollingInterval),
47 sdpEvent(sdeventplus::Event::get_default()),
48 _pollTimer(
49 std::make_unique<
50 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
51 sdpEvent, std::bind(&Manager::pollerTimerExpired, this)))
Tom Joseph815f9f52020-07-27 12:12:13 +053052#ifdef PLDM
53 ,
54 pldmHandle(std::make_unique<pldm::Interface>(
55 bus, std::bind(std::mem_fn(&Manager::updateOCCActive), this,
56 std::placeholders::_1, std::placeholders::_2)))
57#endif
58
Gunnar Mills94df8c92018-09-14 14:50:03 -050059 {
Lei YU0ab90ca2017-07-13 17:02:23 +080060#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -050061 // I2C OCC status objects are initialized directly
62 initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +080063#else
Gunnar Mills94df8c92018-09-14 14:50:03 -050064 findAndCreateObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +080065#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050066 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053067
Chris Caina8857c52021-01-27 11:53:05 -060068 /** @brief Return the number of bound OCCs */
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 inline auto getNumOCCs() const
70 {
71 return activeCount;
72 }
Edward A. James636577f2017-10-06 10:53:55 -050073
Gunnar Mills94df8c92018-09-14 14:50:03 -050074 private:
75 /** @brief Checks if the CPU inventory is present and if so, creates
76 * the occ D-Bus objects. Else, registers a handler to be
77 * called when inventory is created.
78 */
79 void findAndCreateObjects();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053080
Gunnar Mills94df8c92018-09-14 14:50:03 -050081 /** @brief Callback that responds to cpu creation in the inventory -
82 * by creating the needed objects.
83 *
84 * @param[in] msg - bus message
85 *
86 * @returns 0 to indicate success
87 */
88 int cpuCreated(sdbusplus::message::message& msg);
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050089
Gunnar Mills94df8c92018-09-14 14:50:03 -050090 /** @brief Create child OCC objects.
91 *
92 * @param[in] occ - the occ name, such as occ0.
93 */
94 void createObjects(const std::string& occ);
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053095
Gunnar Mills94df8c92018-09-14 14:50:03 -050096 /** @brief Callback handler invoked by Status object when the OccActive
97 * property is changed. This is needed to make sure that the
98 * error detection is started only after all the OCCs are bound.
99 * Similarly, when one of the OCC gets its OccActive property
100 * un-set, then the OCC error detection needs to be stopped on
101 * all the OCCs
102 *
103 * @param[in] status - OccActive status
104 */
105 void statusCallBack(bool status);
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530106
Gunnar Mills94df8c92018-09-14 14:50:03 -0500107 /** @brief Sends a Heartbeat command to host control command handler */
108 void sendHeartBeat();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530109
Gunnar Mills94df8c92018-09-14 14:50:03 -0500110 /** @brief reference to the bus */
111 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530112
Gunnar Mills94df8c92018-09-14 14:50:03 -0500113 /** @brief reference to sd_event wrapped in unique_ptr */
114 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530115
Gunnar Mills94df8c92018-09-14 14:50:03 -0500116 /** @brief OCC pass-through objects */
117 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530118
Gunnar Mills94df8c92018-09-14 14:50:03 -0500119 /** @brief OCC Status objects */
120 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530121
Gunnar Mills94df8c92018-09-14 14:50:03 -0500122 /** @brief Power cap monitor and occ notification object */
123 std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500124
Gunnar Mills94df8c92018-09-14 14:50:03 -0500125 /** @brief sbdbusplus match objects */
126 std::vector<sdbusplus::bus::match_t> cpuMatches;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530127
Gunnar Mills94df8c92018-09-14 14:50:03 -0500128 /** @brief Number of OCCs that are bound */
129 uint8_t activeCount = 0;
Lei YU0ab90ca2017-07-13 17:02:23 +0800130
Chris Caina8857c52021-01-27 11:53:05 -0600131 /** @brief Number of seconds between poll commands */
132 uint8_t pollInterval;
133
134 /** @brief Poll timer event */
135 sdeventplus::Event sdpEvent;
136
137 /**
138 * @brief The timer to be used once the OCC goes active. When it expires,
139 * a POLL command will be sent to the OCC and then timer restarted.
140 */
141 std::unique_ptr<
142 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
143 _pollTimer;
144
Lei YU0ab90ca2017-07-13 17:02:23 +0800145#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -0500146 /** @brief Init Status objects for I2C OCC devices
147 *
148 * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices
149 * and creates status objects.
150 */
151 void initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +0800152#endif
Tom Joseph815f9f52020-07-27 12:12:13 +0530153
154#ifdef PLDM
155 /** @brief Callback handler invoked by the PLDM event handler when state of
156 * the OCC is toggled by the host. The caller passes the instance
157 * of the OCC and state of the OCC.
158 *
159 * @param[in] instance - instance of the OCC
160 * @param[in] status - true when the OCC goes active and false when the OCC
161 * goes inactive
162 *
163 * @return true if setting the state of OCC is successful and false if it
164 * fails.
165 */
166 bool updateOCCActive(instanceID instance, bool status);
167
168 std::unique_ptr<pldm::Interface> pldmHandle = nullptr;
169#endif
Chris Caina8857c52021-01-27 11:53:05 -0600170
171 /**
172 * @brief Called when poll timer expires and forces a POLL command to the
173 * OCC. The poll timer will then be restarted.
174 * */
175 void pollerTimerExpired();
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530176};
177
178} // namespace occ
179} // namespace open_power