blob: c4218c9d923f7074febcdfae031db9597ce6b042 [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"
George Liuf3b75142021-06-10 11:22:50 +08009#include "utils.hpp"
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053010
Gunnar Mills94df8c92018-09-14 14:50:03 -050011#include <cstring>
12#include <functional>
13#include <sdbusplus/bus.hpp>
Chris Caina8857c52021-01-27 11:53:05 -060014#include <sdeventplus/event.hpp>
15#include <sdeventplus/utility/timer.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -050016#include <vector>
17
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053018namespace sdbusRule = sdbusplus::bus::match::rules;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053019namespace open_power
20{
21namespace occ
22{
23
Chris Caina8857c52021-01-27 11:53:05 -060024/** @brief Default time, in seconds, between OCC poll commands */
25constexpr unsigned int defaultPollingInterval = 10;
26
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053027/** @class Manager
28 * @brief Builds and manages OCC objects
29 */
30struct Manager
31{
Gunnar Mills94df8c92018-09-14 14:50:03 -050032 public:
33 Manager() = delete;
34 Manager(const Manager&) = delete;
35 Manager& operator=(const Manager&) = delete;
36 Manager(Manager&&) = delete;
37 Manager& operator=(Manager&&) = delete;
38 ~Manager() = default;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053039
Gunnar Mills94df8c92018-09-14 14:50:03 -050040 /** @brief Adds OCC pass-through and status objects on the bus
41 * when corresponding CPU inventory is created.
42 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050043 * @param[in] event - Unique ptr reference to sd_event
44 */
George Liuf3b75142021-06-10 11:22:50 +080045 Manager(EventPtr& event) :
46 event(event), pollInterval(defaultPollingInterval),
Chris Caina8857c52021-01-27 11:53:05 -060047 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>(
George Liuf3b75142021-06-10 11:22:50 +080055 std::bind(std::mem_fn(&Manager::updateOCCActive), this,
56 std::placeholders::_1, std::placeholders::_2)))
Tom Joseph815f9f52020-07-27 12:12:13 +053057#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 sd_event wrapped in unique_ptr */
111 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530112
Gunnar Mills94df8c92018-09-14 14:50:03 -0500113 /** @brief OCC pass-through objects */
114 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530115
Gunnar Mills94df8c92018-09-14 14:50:03 -0500116 /** @brief OCC Status objects */
117 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530118
Gunnar Mills94df8c92018-09-14 14:50:03 -0500119 /** @brief Power cap monitor and occ notification object */
120 std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500121
Gunnar Mills94df8c92018-09-14 14:50:03 -0500122 /** @brief sbdbusplus match objects */
123 std::vector<sdbusplus::bus::match_t> cpuMatches;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530124
Gunnar Mills94df8c92018-09-14 14:50:03 -0500125 /** @brief Number of OCCs that are bound */
126 uint8_t activeCount = 0;
Lei YU0ab90ca2017-07-13 17:02:23 +0800127
Chris Caina8857c52021-01-27 11:53:05 -0600128 /** @brief Number of seconds between poll commands */
129 uint8_t pollInterval;
130
131 /** @brief Poll timer event */
132 sdeventplus::Event sdpEvent;
133
134 /**
135 * @brief The timer to be used once the OCC goes active. When it expires,
136 * a POLL command will be sent to the OCC and then timer restarted.
137 */
138 std::unique_ptr<
139 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
140 _pollTimer;
141
Lei YU0ab90ca2017-07-13 17:02:23 +0800142#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -0500143 /** @brief Init Status objects for I2C OCC devices
144 *
145 * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices
146 * and creates status objects.
147 */
148 void initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +0800149#endif
Tom Joseph815f9f52020-07-27 12:12:13 +0530150
151#ifdef PLDM
152 /** @brief Callback handler invoked by the PLDM event handler when state of
153 * the OCC is toggled by the host. The caller passes the instance
154 * of the OCC and state of the OCC.
155 *
156 * @param[in] instance - instance of the OCC
157 * @param[in] status - true when the OCC goes active and false when the OCC
158 * goes inactive
159 *
160 * @return true if setting the state of OCC is successful and false if it
161 * fails.
162 */
163 bool updateOCCActive(instanceID instance, bool status);
164
165 std::unique_ptr<pldm::Interface> pldmHandle = nullptr;
166#endif
Chris Caina8857c52021-01-27 11:53:05 -0600167
168 /**
169 * @brief Called when poll timer expires and forces a POLL command to the
170 * OCC. The poll timer will then be restarted.
171 * */
172 void pollerTimerExpired();
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530173};
174
175} // namespace occ
176} // namespace open_power