blob: ece7fb7ab2d9a0231f5e392c375fce9a2da423aa [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"
Chris Cain78e86012021-03-04 16:15:31 -060010#ifdef POWER10
11#include "powermode.hpp"
12#endif
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053013
Gunnar Mills94df8c92018-09-14 14:50:03 -050014#include <cstring>
15#include <functional>
16#include <sdbusplus/bus.hpp>
Chris Caina8857c52021-01-27 11:53:05 -060017#include <sdeventplus/event.hpp>
18#include <sdeventplus/utility/timer.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -050019#include <vector>
20
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053021namespace sdbusRule = sdbusplus::bus::match::rules;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053022namespace open_power
23{
24namespace occ
25{
26
Chris Caina8857c52021-01-27 11:53:05 -060027/** @brief Default time, in seconds, between OCC poll commands */
28constexpr unsigned int defaultPollingInterval = 10;
29
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053030/** @class Manager
31 * @brief Builds and manages OCC objects
32 */
33struct Manager
34{
Gunnar Mills94df8c92018-09-14 14:50:03 -050035 public:
36 Manager() = delete;
37 Manager(const Manager&) = delete;
38 Manager& operator=(const Manager&) = delete;
39 Manager(Manager&&) = delete;
40 Manager& operator=(Manager&&) = delete;
41 ~Manager() = default;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053042
Gunnar Mills94df8c92018-09-14 14:50:03 -050043 /** @brief Adds OCC pass-through and status objects on the bus
44 * when corresponding CPU inventory is created.
45 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050046 * @param[in] event - Unique ptr reference to sd_event
47 */
George Liuf3b75142021-06-10 11:22:50 +080048 Manager(EventPtr& event) :
49 event(event), pollInterval(defaultPollingInterval),
Chris Caina8857c52021-01-27 11:53:05 -060050 sdpEvent(sdeventplus::Event::get_default()),
51 _pollTimer(
52 std::make_unique<
53 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
54 sdpEvent, std::bind(&Manager::pollerTimerExpired, this)))
Tom Joseph815f9f52020-07-27 12:12:13 +053055#ifdef PLDM
56 ,
57 pldmHandle(std::make_unique<pldm::Interface>(
George Liuf3b75142021-06-10 11:22:50 +080058 std::bind(std::mem_fn(&Manager::updateOCCActive), this,
59 std::placeholders::_1, std::placeholders::_2)))
Tom Joseph815f9f52020-07-27 12:12:13 +053060#endif
61
Gunnar Mills94df8c92018-09-14 14:50:03 -050062 {
Lei YU0ab90ca2017-07-13 17:02:23 +080063#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -050064 // I2C OCC status objects are initialized directly
65 initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +080066#else
Gunnar Mills94df8c92018-09-14 14:50:03 -050067 findAndCreateObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +080068#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053070
Chris Caina8857c52021-01-27 11:53:05 -060071 /** @brief Return the number of bound OCCs */
Gunnar Mills94df8c92018-09-14 14:50:03 -050072 inline auto getNumOCCs() const
73 {
74 return activeCount;
75 }
Edward A. James636577f2017-10-06 10:53:55 -050076
Gunnar Mills94df8c92018-09-14 14:50:03 -050077 private:
78 /** @brief Checks if the CPU inventory is present and if so, creates
79 * the occ D-Bus objects. Else, registers a handler to be
80 * called when inventory is created.
81 */
82 void findAndCreateObjects();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053083
Gunnar Mills94df8c92018-09-14 14:50:03 -050084 /** @brief Callback that responds to cpu creation in the inventory -
85 * by creating the needed objects.
86 *
87 * @param[in] msg - bus message
88 *
89 * @returns 0 to indicate success
90 */
91 int cpuCreated(sdbusplus::message::message& msg);
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050092
Gunnar Mills94df8c92018-09-14 14:50:03 -050093 /** @brief Create child OCC objects.
94 *
95 * @param[in] occ - the occ name, such as occ0.
96 */
97 void createObjects(const std::string& occ);
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053098
Gunnar Mills94df8c92018-09-14 14:50:03 -050099 /** @brief Callback handler invoked by Status object when the OccActive
100 * property is changed. This is needed to make sure that the
101 * error detection is started only after all the OCCs are bound.
102 * Similarly, when one of the OCC gets its OccActive property
103 * un-set, then the OCC error detection needs to be stopped on
104 * all the OCCs
105 *
106 * @param[in] status - OccActive status
107 */
108 void statusCallBack(bool status);
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530109
Gunnar Mills94df8c92018-09-14 14:50:03 -0500110 /** @brief Sends a Heartbeat command to host control command handler */
111 void sendHeartBeat();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +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
Chris Cain78e86012021-03-04 16:15:31 -0600125#ifdef POWER10
126 /** @brief Power mode monitor and notification object */
127 std::unique_ptr<open_power::occ::powermode::PowerMode> pmode;
128#endif
129
Gunnar Mills94df8c92018-09-14 14:50:03 -0500130 /** @brief sbdbusplus match objects */
131 std::vector<sdbusplus::bus::match_t> cpuMatches;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530132
Gunnar Mills94df8c92018-09-14 14:50:03 -0500133 /** @brief Number of OCCs that are bound */
134 uint8_t activeCount = 0;
Lei YU0ab90ca2017-07-13 17:02:23 +0800135
Chris Caina8857c52021-01-27 11:53:05 -0600136 /** @brief Number of seconds between poll commands */
137 uint8_t pollInterval;
138
139 /** @brief Poll timer event */
140 sdeventplus::Event sdpEvent;
141
142 /**
143 * @brief The timer to be used once the OCC goes active. When it expires,
144 * a POLL command will be sent to the OCC and then timer restarted.
145 */
146 std::unique_ptr<
147 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
148 _pollTimer;
149
Lei YU0ab90ca2017-07-13 17:02:23 +0800150#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -0500151 /** @brief Init Status objects for I2C OCC devices
152 *
153 * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices
154 * and creates status objects.
155 */
156 void initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +0800157#endif
Tom Joseph815f9f52020-07-27 12:12:13 +0530158
159#ifdef PLDM
160 /** @brief Callback handler invoked by the PLDM event handler when state of
161 * the OCC is toggled by the host. The caller passes the instance
162 * of the OCC and state of the OCC.
163 *
164 * @param[in] instance - instance of the OCC
165 * @param[in] status - true when the OCC goes active and false when the OCC
166 * goes inactive
167 *
168 * @return true if setting the state of OCC is successful and false if it
169 * fails.
170 */
171 bool updateOCCActive(instanceID instance, bool status);
172
173 std::unique_ptr<pldm::Interface> pldmHandle = nullptr;
174#endif
Chris Caina8857c52021-01-27 11:53:05 -0600175
176 /**
177 * @brief Called when poll timer expires and forces a POLL command to the
178 * OCC. The poll timer will then be restarted.
179 * */
180 void pollerTimerExpired();
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530181};
182
183} // namespace occ
184} // namespace open_power