xref: /openbmc/openpower-occ-control/occ_manager.hpp (revision d2b044f935e069f6d30d9a25419efb71b9a73edf)
1 #pragma once
2 
3 #include "occ_pass_through.hpp"
4 #include "occ_status.hpp"
5 #ifdef PLDM
6 #include "pldm.hpp"
7 
8 #ifdef PHAL_SUPPORT
9 #include <libphal.H>
10 #endif
11 #endif
12 #include "powercap.hpp"
13 #include "utils.hpp"
14 #ifdef POWER10
15 #include "powermode.hpp"
16 #endif
17 
18 #include <sdbusplus/bus.hpp>
19 #include <sdeventplus/event.hpp>
20 #include <sdeventplus/utility/timer.hpp>
21 
22 #include <cstring>
23 #include <functional>
24 #include <vector>
25 
26 namespace sdbusRule = sdbusplus::bus::match::rules;
27 namespace open_power
28 {
29 namespace occ
30 {
31 
32 #ifdef READ_OCC_SENSORS
33 enum occFruType
34 {
35     processorCore = 0,
36     internalMemCtlr = 1,
37     dimm = 2,
38     memCtrlAndDimm = 3,
39     VRMVdd = 6,
40     PMIC = 7,
41     memCtlrExSensor = 8,
42     processorIoRing = 9
43 };
44 #endif
45 
46 /** @brief Default time, in seconds, between OCC poll commands */
47 #ifndef POWER10
48 constexpr unsigned int defaultPollingInterval = 1;
49 #else
50 constexpr unsigned int defaultPollingInterval = 5;
51 #endif
52 
53 constexpr auto AMBIENT_PATH =
54     "/xyz/openbmc_project/sensors/temperature/Ambient_Virtual_Temp";
55 constexpr auto AMBIENT_INTERFACE = "xyz.openbmc_project.Sensor.Value";
56 constexpr auto AMBIENT_PROP = "Value";
57 constexpr auto ALTITUDE_PATH = "/xyz/openbmc_project/sensors/altitude/Altitude";
58 constexpr auto ALTITUDE_INTERFACE = "xyz.openbmc_project.Sensor.Value";
59 constexpr auto ALTITUDE_PROP = "Value";
60 
61 constexpr auto EXTN_LABEL_PWRM_MEMORY_POWER = "5057524d";
62 constexpr auto EXTN_LABEL_PWRP_PROCESSOR_POWER = "50575250";
63 
64 /** @class Manager
65  *  @brief Builds and manages OCC objects
66  */
67 struct Manager
68 {
69   public:
70     Manager() = delete;
71     Manager(const Manager&) = delete;
72     Manager& operator=(const Manager&) = delete;
73     Manager(Manager&&) = delete;
74     Manager& operator=(Manager&&) = delete;
75     ~Manager() = default;
76 
77     /** @brief Adds OCC pass-through and status objects on the bus
78      *         when corresponding CPU inventory is created.
79      *
80      *  @param[in] event - Unique ptr reference to sd_event
81      */
Manageropen_power::occ::Manager82     explicit Manager(EventPtr& event) :
83         event(event), pollInterval(defaultPollingInterval),
84         sdpEvent(sdeventplus::Event::get_default()),
85         _pollTimer(
86             std::make_unique<
87                 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
88                 sdpEvent, std::bind(&Manager::pollerTimerExpired, this))),
89         ambientPropChanged(
90             utils::getBus(),
91             sdbusRule::member("PropertiesChanged") +
92                 sdbusRule::path(AMBIENT_PATH) +
93                 sdbusRule::argN(0, AMBIENT_INTERFACE) +
94                 sdbusRule::interface("org.freedesktop.DBus.Properties"),
95             std::bind(&Manager::ambientCallback, this, std::placeholders::_1))
96 #ifdef POWER10
97         ,
98         discoverTimer(
99             std::make_unique<
100                 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
101                 sdpEvent, std::bind(&Manager::findAndCreateObjects, this))),
102         waitForAllOccsTimer(
103             std::make_unique<
104                 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
105                 sdpEvent, std::bind(&Manager::occsNotAllRunning, this)))
106 #ifdef PLDM
107         ,
108         throttlePldmTraceTimer(
109             std::make_unique<
110                 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
111                 sdpEvent, std::bind(&Manager::throttlePldmTraceExpired, this)))
112 #endif
113 #endif // POWER10
114     {
115 #ifdef I2C_OCC
116         // I2C OCC status objects are initialized directly
117         initStatusObjects();
118 #else
119         findAndCreateObjects();
120 #endif
121         readAltitude();
122     }
123 
124     void createPldmHandle();
125 
126     /** @brief Return the number of bound OCCs */
getNumOCCsopen_power::occ::Manager127     inline auto getNumOCCs() const
128     {
129         return activeCount;
130     }
131 
132 #ifdef PLDM
133     /** @brief Called by a Device to report that the SBE timed out
134      *         and appropriate action should be taken
135      *
136      * @param[in] instance - the OCC instance id
137      */
138     void sbeTimeout(unsigned int instance);
139 #endif
140 
141     /** @brief Return the latest ambient and altitude readings
142      *
143      *  @param[out] ambientValid - true if ambientTemp is valid
144      *  @param[out] ambient - ambient temperature in degrees C
145      *  @param[out] altitude - altitude in meters
146      */
147     void getAmbientData(bool& ambientValid, uint8_t& ambientTemp,
148                         uint16_t& altitude) const;
149 
150     /** @brief Notify pcap object to update bounds */
151     void updatePcapBounds() const;
152 
153     /**
154      * @brief Set all sensor values of this OCC to NaN.
155      * @param[in] id - Id of the OCC.
156      * */
157     void setSensorValueToNaN(uint32_t id) const;
158 
159     /** @brief Set all sensor values of this OCC to NaN and non functional.
160      *
161      *  @param[in] id - Id of the OCC.
162      */
163     void setSensorValueToNonFunctional(uint32_t id) const;
164 
165   private:
166     /** @brief Creates the OCC D-Bus objects.
167      */
168     void findAndCreateObjects();
169 
170     /** @brief Callback that responds to cpu creation in the inventory -
171      *         by creating the needed objects.
172      *
173      *  @param[in] msg - bus message
174      *
175      *  @returns 0 to indicate success
176      */
177     int cpuCreated(sdbusplus::message_t& msg);
178 
179     /** @brief Create child OCC objects.
180      *
181      *  @param[in] occ - the occ name, such as occ0.
182      */
183     void createObjects(const std::string& occ);
184 
185     /** @brief Callback handler invoked by Status object when the OccActive
186      *         property is changed. This is needed to make sure that the
187      *         error detection is started only after all the OCCs are bound.
188      *         Similarly, when one of the OCC gets its OccActive property
189      *         un-set, then the OCC error detection needs to be stopped on
190      *         all the OCCs
191      *
192      *  @param[in] status - OccActive status
193      */
194     void statusCallBack(instanceID instance, bool status);
195 
196     /** @brief Set flag that a PM Complex reset is needed (to be initiated
197      * later) */
198     void resetOccRequest(instanceID instance);
199 
200     /** @brief Initiate the request to reset the PM Complex (PLDM -> HBRT) */
201     void initiateOccRequest(instanceID instance);
202 
203     /** @brief Sends a Heartbeat command to host control command handler */
204     void sendHeartBeat();
205 
206     /** @brief reference to sd_event wrapped in unique_ptr */
207     EventPtr& event;
208 
209     /** @brief OCC pass-through objects */
210     std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
211 
212     /** @brief OCC Status objects */
213     std::vector<std::unique_ptr<Status>> statusObjects;
214 
215     /** @brief Power cap monitor and occ notification object */
216     std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
217 
218 #ifdef POWER10
219     /** @brief Power mode monitor and notification object */
220     std::unique_ptr<open_power::occ::powermode::PowerMode> pmode;
221 #endif
222 
223     /** @brief sbdbusplus match objects */
224     std::vector<sdbusplus::bus::match_t> cpuMatches;
225 
226     /** @brief Number of OCCs that are bound */
227     uint8_t activeCount = 0;
228 
229     /** @brief Number of seconds between poll commands */
230     uint8_t pollInterval;
231 
232     /** @brief Ambient temperature of the system in degrees C */
233     uint8_t ambient = 0xFF; // default: not available
234 
235     /** @brief Altitude of the system in meters */
236     uint16_t altitude = 0xFFFF; // default: not available
237 
238     /** @brief Poll timer event */
239     sdeventplus::Event sdpEvent;
240 
241     /** @brief Flags to indicate if waiting for all of the OCC active sensors to
242      * come online */
243     bool waitingForAllOccActiveSensors = false;
244 
245     /** @brief Set containing intance numbers of any OCCs that became active
246      *         while waiting for status objects to be created */
247     std::set<uint8_t> queuedActiveState;
248 
249     /**
250      * @brief The timer to be used once the OCC goes active.  When it expires,
251      *        a POLL command will be sent to the OCC and then timer restarted.
252      */
253     std::unique_ptr<
254         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
255         _pollTimer;
256 
257     /** @brief Subscribe to ambient temperature changed events */
258     sdbusplus::bus::match_t ambientPropChanged;
259 
260     /** @brief Flag to indicate that a PM complex reset needs to happen */
261     bool resetRequired = false;
262     /** @brief Instance number of the OCC/processor that triggered the reset */
263     uint8_t resetInstance = 255;
264     /** @brief Set when a PM complex reset has been issued (to prevent multiple
265      * requests) */
266     bool resetInProgress = false;
267 
268 #ifdef I2C_OCC
269     /** @brief Init Status objects for I2C OCC devices
270      *
271      * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices
272      * and creates status objects.
273      */
274     void initStatusObjects();
275 #endif
276 
277 #ifdef PLDM
278     /** @brief Callback handler invoked by the PLDM event handler when state of
279      *         the OCC is toggled by the host. The caller passes the instance
280      *         of the OCC and state of the OCC.
281      *
282      *  @param[in] instance - instance of the OCC
283      *  @param[in] status - true when the OCC goes active and false when the OCC
284      *                      goes inactive
285      *
286      *  @return true if setting the state of OCC is successful and false if it
287      *          fails.
288      */
289     bool updateOCCActive(instanceID instance, bool status);
290 
291     /** @brief Callback handler invoked by the PLDM event handler when mode of
292      *         the OCC SAFE MODE is inacted or cleared.
293      */
294     void updateOccSafeMode(bool safeState);
295 
296     /** @brief Callback handler invoked by PLDM sensor change when
297      *         the HRESET succeeds or fails.
298      *
299      *  @param[in] instance - the SBE instance id
300      *  @param[in] success - true if the HRESET succeeded, otherwise false
301      */
302     void sbeHRESETResult(instanceID instance, bool success);
303 
304 #ifdef PHAL_SUPPORT
305     /** @brief Helper function to check whether an SBE dump should be collected
306      *         now.
307      *
308      *  @param[in] instance - the SBE instance id
309      *
310      *  @return true if an SBE dump should be collected and false if not
311      */
312     bool sbeCanDump(unsigned int instance);
313 
314     /** @brief Helper function to set the SBE state through PDBG/PHAL
315      *
316      * @param[in] instance - instance of the SBE
317      * @param[in] state - the state to which the SBE should be set
318      *
319      */
320     void setSBEState(unsigned int instance, enum sbe_state state);
321 
322     /** @brief Helper function to get the SBE instance PDBG processor target
323      *
324      * @param[in] instance - the SBE instance id
325      *
326      * @return a pointer to the PDBG target
327      */
328     struct pdbg_target* getPdbgTarget(unsigned int instance);
329 
330     /** @brief Whether pdbg_targets_init has been called */
331     bool pdbgInitialized = false;
332 #endif
333 
334     std::unique_ptr<pldm::Interface> pldmHandle = nullptr;
335 #endif
336 
337 #ifdef POWER10
338     /**
339      * @brief Timer used when discovering OCCs in /dev.
340      */
341     std::unique_ptr<
342         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
343         discoverTimer;
344 
345     /**
346      * @brief Used when discovering /dev/occ objects to know if
347      *        any were added since the last check.
348      */
349     std::vector<int> prevOCCSearch;
350 
351     /**
352      * @brief Timer used when waiting for OCCs to go active.
353      */
354     std::unique_ptr<
355         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
356         waitForAllOccsTimer;
357 
358 #ifdef PLDM
359     /**
360      * @brief Timer used to throttle PLDM traces when there are problems
361               determining the OCC status via pldm. Used to prevent excessive
362               journal traces.
363      */
364     std::unique_ptr<
365         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
366         throttlePldmTraceTimer;
367     /**
368      * @brief onPldmTimeoutCreatePel flag will be used to indicate if
369      *        a PEL should get created when the throttlePldmTraceTimer expires.
370      *        The first time the throttlePldmTraceTimer expires, the traces
371      *        will be throttled and then the timer gets restarted. The
372      *        next time the timer expires, a PEL will get created.
373      */
374     bool onPldmTimeoutCreatePel = false;
375 
376     /** @brief Check if all of the OCC Active sensors are available and if not
377      * restart the discoverTimer
378      */
379     void throttlePldmTraceExpired();
380 
381     /** @brief Create a PEL when the code is not able to obtain the OCC PDRs
382      * via PLDM. This is called when the throttlePldmTraceTimer expires.
383      */
384     void createPldmSensorPEL();
385 #endif
386 
387     /** @brief Called when code times out waiting for all OCCs to be running or
388      *         after the app is restarted (Status does not callback into
389      * Manager).
390      */
391     void occsNotAllRunning();
392 
393     /** @brief Check if all of the OCC Active sensors are available and if not
394      * restart the discoverTimer
395      */
396     void checkAllActiveSensors();
397 #endif // POWER10
398 
399     /**
400      * @brief Called when poll timer expires and forces a POLL command to the
401      * OCC. The poll timer will then be restarted.
402      * */
403     void pollerTimerExpired();
404 
405     /**
406      * @brief Finds the OCC devices in /dev
407      *
408      * @return The IDs of the OCCs - 0, 1, etc.
409      */
410     std::vector<int> findOCCsInDev();
411 
412 #ifdef READ_OCC_SENSORS
413     /**
414      * @brief Gets the occ sensor values.
415      * @param[in] occ - pointer to OCCs Status object
416      * */
417     void getSensorValues(std::unique_ptr<Status>& occ);
418 
419     /**
420      * @brief Trigger OCC driver to read the temperature sensors.
421      * @param[in] path - path of the OCC sensors.
422      * @param[in] id - Id of the OCC.
423      * */
424     void readTempSensors(const fs::path& path, uint32_t id);
425 
426     /**
427      * @brief Trigger OCC driver to read the extended sensors.
428      * @param[in] path - path of the OCC sensors.
429      * @param[in] id - Id of the OCC.
430      * */
431     void readExtnSensors(const fs::path& path, uint32_t id);
432 
433     /**
434      * @brief Trigger OCC driver to read the power sensors.
435      * @param[in] path - path of the OCC sensors.
436      * @param[in] id - Id of the OCC.
437      * */
438     void readPowerSensors(const fs::path& path, uint32_t id);
439 
440     /** @brief Store the existing OCC sensors on D-BUS */
441     std::map<std::string, uint32_t> existingSensors;
442 
443     /** @brief Get FunctionID from the `powerX_label` file.
444      *  @param[in] value - the value of the `powerX_label` file.
445      *  @returns FunctionID of the power sensors.
446      */
447     std::optional<std::string> getPowerLabelFunctionID(
448         const std::string& value);
449 
450     /** @brief The power sensor names map */
451     const std::map<std::string, std::string> powerSensorName = {
452         {"system", "total_power"}, {"1", "p0_mem_power"},
453         {"2", "p1_mem_power"},     {"3", "p2_mem_power"},
454         {"4", "p3_mem_power"},     {"5", "p0_power"},
455         {"6", "p1_power"},         {"7", "p2_power"},
456         {"8", "p3_power"},         {"9", "p0_cache_power"},
457         {"10", "p1_cache_power"},  {"11", "p2_cache_power"},
458         {"12", "p3_cache_power"},  {"13", "io_a_power"},
459         {"14", "io_b_power"},      {"15", "io_c_power"},
460         {"16", "fans_a_power"},    {"17", "fans_b_power"},
461         {"18", "storage_a_power"}, {"19", "storage_b_power"},
462         {"23", "mem_cache_power"}, {"25", "p0_mem_0_power"},
463         {"26", "p0_mem_1_power"},  {"27", "p0_mem_2_power"},
464         {"35", "pcie_dcm0_power"}, {"36", "pcie_dcm1_power"},
465         {"37", "pcie_dcm2_power"}, {"38", "pcie_dcm3_power"},
466         {"39", "io_dcm0_power"},   {"40", "io_dcm1_power"},
467         {"41", "io_dcm2_power"},   {"42", "io_dcm3_power"},
468         {"43", "avdd_total_power"}};
469 
470     /** @brief The dimm temperature sensor names map  */
471     const std::map<uint32_t, std::string> dimmTempSensorName = {
472         {internalMemCtlr, "_intmb_temp"},
473         {dimm, "_dram_temp"},
474         {memCtrlAndDimm, "_dram_extmb_temp"},
475         {PMIC, "_pmic_temp"},
476         {memCtlrExSensor, "_extmb_temp"}};
477 
478     /** @brief The dimm DVFS temperature sensor names map  */
479     const std::map<uint32_t, std::string> dimmDVFSSensorName = {
480         {internalMemCtlr, "dimm_intmb_dvfs_temp"},
481         {dimm, "dimm_dram_dvfs_temp"},
482         {memCtrlAndDimm, "dimm_dram_extmb_dvfs_temp"},
483         {PMIC, "dimm_pmic_dvfs_temp"},
484         {memCtlrExSensor, "dimm_extmb_dvfs_temp"}};
485 #endif
486 
487     /** @brief Read the altitude from DBus */
488     void readAltitude();
489 
490     /** @brief Callback function when ambient temperature changes
491      *
492      *  @param[in]  msg - Data associated with subscribed signal
493      */
494     void ambientCallback(sdbusplus::message_t& msg);
495 
496     /** @brief Confirm that a single OCC master was found and start presence
497      * monitoring
498      */
499     void validateOccMaster();
500 };
501 
502 } // namespace occ
503 } // namespace open_power
504