blob: 742c222ef95d5352220a1d07a6afa621cce2aaae [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"
Eddie Jamescbad2192021-10-07 09:39:39 -05007
8#include <libphal.H>
Tom Joseph815f9f52020-07-27 12:12:13 +05309#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053010#include "powercap.hpp"
George Liuf3b75142021-06-10 11:22:50 +080011#include "utils.hpp"
Chris Cain78e86012021-03-04 16:15:31 -060012#ifdef POWER10
13#include "powermode.hpp"
14#endif
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053015
Gunnar Mills94df8c92018-09-14 14:50:03 -050016#include <sdbusplus/bus.hpp>
Chris Caina8857c52021-01-27 11:53:05 -060017#include <sdeventplus/event.hpp>
18#include <sdeventplus/utility/timer.hpp>
George Liub5ca1012021-09-10 12:53:11 +080019
20#include <cstring>
21#include <functional>
Gunnar Mills94df8c92018-09-14 14:50:03 -050022#include <vector>
23
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053024namespace sdbusRule = sdbusplus::bus::match::rules;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053025namespace open_power
26{
27namespace occ
28{
29
Chicago Duanbb895cb2021-06-18 19:37:16 +080030#ifdef READ_OCC_SENSORS
31enum occFruType
32{
33 processorCore = 0,
34 internalMemCtlr = 1,
35 dimm = 2,
36 memCtrlAndDimm = 3,
37 VRMVdd = 6,
38 PMIC = 7,
Matt Spinlerace67d82021-10-18 13:41:57 -050039 memCtlrExSensor = 8,
40 processorIoRing = 9
Chicago Duanbb895cb2021-06-18 19:37:16 +080041};
42#endif
43
Chris Caina8857c52021-01-27 11:53:05 -060044/** @brief Default time, in seconds, between OCC poll commands */
Matt Spinler37923462021-09-24 11:38:05 -050045#ifndef POWER10
Chicago Duanbb895cb2021-06-18 19:37:16 +080046constexpr unsigned int defaultPollingInterval = 1;
Matt Spinler37923462021-09-24 11:38:05 -050047#else
48constexpr unsigned int defaultPollingInterval = 5;
49#endif
Chris Caina8857c52021-01-27 11:53:05 -060050
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053051/** @class Manager
52 * @brief Builds and manages OCC objects
53 */
54struct Manager
55{
Gunnar Mills94df8c92018-09-14 14:50:03 -050056 public:
57 Manager() = delete;
58 Manager(const Manager&) = delete;
59 Manager& operator=(const Manager&) = delete;
60 Manager(Manager&&) = delete;
61 Manager& operator=(Manager&&) = delete;
62 ~Manager() = default;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053063
Gunnar Mills94df8c92018-09-14 14:50:03 -050064 /** @brief Adds OCC pass-through and status objects on the bus
65 * when corresponding CPU inventory is created.
66 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050067 * @param[in] event - Unique ptr reference to sd_event
68 */
George Liuf3b75142021-06-10 11:22:50 +080069 Manager(EventPtr& event) :
70 event(event), pollInterval(defaultPollingInterval),
Chris Caina8857c52021-01-27 11:53:05 -060071 sdpEvent(sdeventplus::Event::get_default()),
72 _pollTimer(
73 std::make_unique<
74 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
75 sdpEvent, std::bind(&Manager::pollerTimerExpired, this)))
Tom Joseph815f9f52020-07-27 12:12:13 +053076#ifdef PLDM
77 ,
78 pldmHandle(std::make_unique<pldm::Interface>(
George Liuf3b75142021-06-10 11:22:50 +080079 std::bind(std::mem_fn(&Manager::updateOCCActive), this,
Eddie Jamescbad2192021-10-07 09:39:39 -050080 std::placeholders::_1, std::placeholders::_2),
81 std::bind(std::mem_fn(&Manager::sbeHRESETResult), this,
George Liuf3b75142021-06-10 11:22:50 +080082 std::placeholders::_1, std::placeholders::_2)))
Tom Joseph815f9f52020-07-27 12:12:13 +053083#endif
Matt Spinlerd267cec2021-09-01 14:49:19 -050084#ifdef POWER10
85 ,
86 discoverTimer(
87 std::make_unique<
88 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
89 sdpEvent, std::bind(&Manager::findAndCreateObjects, this)))
90#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050091 {
Lei YU0ab90ca2017-07-13 17:02:23 +080092#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -050093 // I2C OCC status objects are initialized directly
94 initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +080095#else
Gunnar Mills94df8c92018-09-14 14:50:03 -050096 findAndCreateObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +080097#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050098 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053099
Chris Caina8857c52021-01-27 11:53:05 -0600100 /** @brief Return the number of bound OCCs */
Gunnar Mills94df8c92018-09-14 14:50:03 -0500101 inline auto getNumOCCs() const
102 {
103 return activeCount;
104 }
Edward A. James636577f2017-10-06 10:53:55 -0500105
Eddie Jamescbad2192021-10-07 09:39:39 -0500106#ifdef PLDM
107 /** @brief Called by a Device to report that the SBE timed out
108 * and appropriate action should be taken
109 *
110 * @param[in] instance - the OCC instance id
111 */
112 void sbeTimeout(unsigned int instance);
113#endif
114
Gunnar Mills94df8c92018-09-14 14:50:03 -0500115 private:
Matt Spinlerd267cec2021-09-01 14:49:19 -0500116 /** @brief Creates the OCC D-Bus objects.
Gunnar Mills94df8c92018-09-14 14:50:03 -0500117 */
118 void findAndCreateObjects();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530119
Gunnar Mills94df8c92018-09-14 14:50:03 -0500120 /** @brief Callback that responds to cpu creation in the inventory -
121 * by creating the needed objects.
122 *
123 * @param[in] msg - bus message
124 *
125 * @returns 0 to indicate success
126 */
127 int cpuCreated(sdbusplus::message::message& msg);
Deepak Kodihalli5f031f32017-07-26 08:25:59 -0500128
Gunnar Mills94df8c92018-09-14 14:50:03 -0500129 /** @brief Create child OCC objects.
130 *
131 * @param[in] occ - the occ name, such as occ0.
132 */
133 void createObjects(const std::string& occ);
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530134
Gunnar Mills94df8c92018-09-14 14:50:03 -0500135 /** @brief Callback handler invoked by Status object when the OccActive
136 * property is changed. This is needed to make sure that the
137 * error detection is started only after all the OCCs are bound.
138 * Similarly, when one of the OCC gets its OccActive property
139 * un-set, then the OCC error detection needs to be stopped on
140 * all the OCCs
141 *
142 * @param[in] status - OccActive status
143 */
144 void statusCallBack(bool status);
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530145
Gunnar Mills94df8c92018-09-14 14:50:03 -0500146 /** @brief Sends a Heartbeat command to host control command handler */
147 void sendHeartBeat();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530148
Gunnar Mills94df8c92018-09-14 14:50:03 -0500149 /** @brief reference to sd_event wrapped in unique_ptr */
150 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530151
Gunnar Mills94df8c92018-09-14 14:50:03 -0500152 /** @brief OCC pass-through objects */
153 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530154
Gunnar Mills94df8c92018-09-14 14:50:03 -0500155 /** @brief OCC Status objects */
156 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530157
Gunnar Mills94df8c92018-09-14 14:50:03 -0500158 /** @brief Power cap monitor and occ notification object */
159 std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500160
Chris Cain78e86012021-03-04 16:15:31 -0600161#ifdef POWER10
162 /** @brief Power mode monitor and notification object */
163 std::unique_ptr<open_power::occ::powermode::PowerMode> pmode;
Chris Cain1d51da22021-09-21 14:13:41 -0500164
165 /** @brief Idle Power Saver monitor and notification object */
166 std::unique_ptr<open_power::occ::powermode::PowerIPS> pips;
Chris Cain78e86012021-03-04 16:15:31 -0600167#endif
168
Gunnar Mills94df8c92018-09-14 14:50:03 -0500169 /** @brief sbdbusplus match objects */
170 std::vector<sdbusplus::bus::match_t> cpuMatches;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530171
Gunnar Mills94df8c92018-09-14 14:50:03 -0500172 /** @brief Number of OCCs that are bound */
173 uint8_t activeCount = 0;
Lei YU0ab90ca2017-07-13 17:02:23 +0800174
Chris Caina8857c52021-01-27 11:53:05 -0600175 /** @brief Number of seconds between poll commands */
176 uint8_t pollInterval;
177
178 /** @brief Poll timer event */
179 sdeventplus::Event sdpEvent;
180
181 /**
182 * @brief The timer to be used once the OCC goes active. When it expires,
183 * a POLL command will be sent to the OCC and then timer restarted.
184 */
185 std::unique_ptr<
186 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
187 _pollTimer;
188
Lei YU0ab90ca2017-07-13 17:02:23 +0800189#ifdef I2C_OCC
Gunnar Mills94df8c92018-09-14 14:50:03 -0500190 /** @brief Init Status objects for I2C OCC devices
191 *
192 * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices
193 * and creates status objects.
194 */
195 void initStatusObjects();
Lei YU0ab90ca2017-07-13 17:02:23 +0800196#endif
Tom Joseph815f9f52020-07-27 12:12:13 +0530197
198#ifdef PLDM
199 /** @brief Callback handler invoked by the PLDM event handler when state of
200 * the OCC is toggled by the host. The caller passes the instance
201 * of the OCC and state of the OCC.
202 *
203 * @param[in] instance - instance of the OCC
204 * @param[in] status - true when the OCC goes active and false when the OCC
205 * goes inactive
206 *
207 * @return true if setting the state of OCC is successful and false if it
208 * fails.
209 */
210 bool updateOCCActive(instanceID instance, bool status);
211
Eddie Jamescbad2192021-10-07 09:39:39 -0500212 /** @brief Callback handler invoked by PLDM sensor change when
213 * the HRESET succeeds or fails.
214 *
215 * @param[in] instance - the SBE instance id
216 * @param[in] success - true if the HRESET succeeded, otherwise false
217 */
218 void sbeHRESETResult(instanceID instance, bool success);
219
220 /** @brief Helper function to check whether an SBE dump should be collected
221 * now.
222 *
223 * @param[in] instance - the SBE instance id
224 *
225 * @return true if an SBE dump should be collected and false if not
226 */
227 bool sbeCanDump(unsigned int instance);
228
229 /** @brief Helper function to set the SBE state through PDBG/PHAL
230 *
231 * @param[in] instance - instance of the SBE
232 * @param[in] state - the state to which the SBE should be set
233 *
234 */
235 void setSBEState(unsigned int instance, enum sbe_state state);
236
237 /** @brief Helper function to get the SBE instance PDBG processor target
238 *
239 * @param[in] instance - the SBE instance id
240 *
241 * @return a pointer to the PDBG target
242 */
243 struct pdbg_target* getPdbgTarget(unsigned int instance);
244
245 /** @brief Whether pdbg_targets_init has been called */
246 bool pdbgInitialized = false;
247
Tom Joseph815f9f52020-07-27 12:12:13 +0530248 std::unique_ptr<pldm::Interface> pldmHandle = nullptr;
249#endif
Chris Caina8857c52021-01-27 11:53:05 -0600250
Matt Spinlerd267cec2021-09-01 14:49:19 -0500251#ifdef POWER10
252 /**
253 * @brief Timer used when discovering OCCs in /dev.
254 */
255 std::unique_ptr<
256 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
257 discoverTimer;
258
259 /**
260 * @brief Used when discovering /dev/occ objects to know if
261 * any were added since the last check.
262 */
263 std::vector<int> prevOCCSearch;
264#endif
265
Chris Caina8857c52021-01-27 11:53:05 -0600266 /**
267 * @brief Called when poll timer expires and forces a POLL command to the
268 * OCC. The poll timer will then be restarted.
269 * */
270 void pollerTimerExpired();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800271
Matt Spinlerd267cec2021-09-01 14:49:19 -0500272 /**
273 * @brief Finds the OCC devices in /dev
274 *
275 * @return The IDs of the OCCs - 0, 1, etc.
276 */
277 std::vector<int> findOCCsInDev();
278
Chicago Duanbb895cb2021-06-18 19:37:16 +0800279#ifdef READ_OCC_SENSORS
280 /**
281 * @brief Gets the occ sensor values.
282 * @param[in] id - Id of the OCC.
283 * @param[in] masterOcc - Is this OCC the master OCC.
284 * */
285 void getSensorValues(uint32_t id, bool masterOcc);
286
287 /**
288 * @brief Trigger OCC driver to read the temperature sensors.
289 * @param[in] path - path of the OCC sensors.
290 * @param[in] id - Id of the OCC.
291 * */
292 void readTempSensors(const fs::path& path, uint32_t id);
293
294 /**
295 * @brief Trigger OCC driver to read the power sensors.
296 * @param[in] path - path of the OCC sensors.
297 * @param[in] id - Id of the OCC.
298 * */
299 void readPowerSensors(const fs::path& path, uint32_t id);
300
301 /**
302 * @brief Set all sensor values of this OCC to NaN.
303 * @param[in] id - Id of the OCC.
304 * */
305 void setSensorValueToNaN(uint32_t id);
306
307 /** @brief Store the existing OCC sensors on D-BUS */
308 std::map<std::string, uint32_t> existingSensors;
309
310 /** @brief Get FunctionID from the `powerX_label` file.
311 * @param[in] value - the value of the `powerX_label` file.
312 * @returns FunctionID of the power sensors.
313 */
314 std::optional<std::string>
315 getPowerLabelFunctionID(const std::string& value);
316
317 /** @brief The power sensor names map */
318 const std::map<std::string, std::string> powerSensorName = {
319 {"system", "total_power"}, {"1", "p0_mem_power"},
320 {"2", "p1_mem_power"}, {"3", "p2_mem_power"},
321 {"4", "p3_mem_power"}, {"5", "p0_power"},
322 {"6", "p1_power"}, {"7", "p2_power"},
323 {"8", "p3_power"}, {"9", "p0_cache_power"},
324 {"10", "p1_cache_power"}, {"11", "p2_cache_power"},
325 {"12", "p3_cache_power"}, {"13", "io_a_power"},
326 {"14", "io_b_power"}, {"15", "io_c_power"},
327 {"16", "fans_a_power"}, {"17", "fans_b_power"},
328 {"18", "storage_a_power"}, {"19", "storage_b_power"},
329 {"23", "mem_cache_power"}, {"25", "p0_mem_0_power"},
330 {"26", "p0_mem_1_power"}, {"27", "p0_mem_2_power"}};
331
332 /** @brief The dimm temperature sensor names map */
333 const std::map<uint32_t, std::string> dimmTempSensorName = {
334 {internalMemCtlr, "_intmb_temp"},
335 {dimm, "_dram_temp"},
336 {memCtrlAndDimm, "_dram_extmb_temp"},
337 {PMIC, "_pmic_temp"},
338 {memCtlrExSensor, "_extmb_temp"}};
339#endif
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530340};
341
342} // namespace occ
343} // namespace open_power