blob: a07c2726d07b2a5a05db29883ae8b36a7e4a410d [file] [log] [blame]
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05301#pragma once
George Liubddcf852021-09-08 08:46:22 +08002#include "config.h"
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05303
Gunnar Mills94df8c92018-09-14 14:50:03 -05004#include "i2c_occ.hpp"
Chris Caina8857c52021-01-27 11:53:05 -06005#include "occ_command.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05006#include "occ_device.hpp"
7#include "occ_events.hpp"
Chris Cain5d66a0a2022-02-09 08:52:10 -06008#include "powercap.hpp"
Chris Cain36f9cde2021-11-22 11:18:21 -06009#include "powermode.hpp"
George Liuf3b75142021-06-10 11:22:50 +080010#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -050011
Gunnar Mills94df8c92018-09-14 14:50:03 -050012#include <org/open_power/Control/Host/server.hpp>
13#include <org/open_power/OCC/Status/server.hpp>
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053014#include <sdbusplus/bus.hpp>
15#include <sdbusplus/server/object.hpp>
Chris Caina7b74dc2021-11-10 17:03:43 -060016#ifdef POWER10
17#include <sdeventplus/event.hpp>
18#include <sdeventplus/utility/timer.hpp>
19#endif
Chris Cainc86d80f2023-05-04 15:49:18 -050020#include <xyz/openbmc_project/Control/Power/Throttle/server.hpp>
Lei YU0ab90ca2017-07-13 17:02:23 +080021
George Liub5ca1012021-09-10 12:53:11 +080022#include <functional>
23
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053024namespace open_power
25{
26namespace occ
27{
28
Edward A. James636577f2017-10-06 10:53:55 -050029class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053030namespace Base = sdbusplus::org::open_power::OCC::server;
Patrick Williamsaf408082022-07-22 19:26:54 -050031using Interface = sdbusplus::server::object_t<Base::Status>;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053032
Chris Cainc86d80f2023-05-04 15:49:18 -050033namespace xyzBase = sdbusplus::xyz::openbmc_project::Control::Power::server;
34using ThrottleInterface = sdbusplus::server::object_t<xyzBase::Throttle>;
35
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053036// IPMID's host control application
37namespace Control = sdbusplus::org::open_power::Control::server;
38
39// For waiting on signals
40namespace sdbusRule = sdbusplus::bus::match::rules;
41
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053042// OCC status instance. Ex. for "occ0", the instance is 0
Chris Cain7e374fb2022-04-07 09:47:23 -050043using instanceID = unsigned int;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053044
45// IPMI sensor ID for a given OCC instance
46using sensorID = uint8_t;
47
Alexander Filippov1d69e192019-03-21 18:12:07 +030048// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
49using sensorName = std::string;
50
51// OCC sensors definitions in the map
52using sensorDefs = std::tuple<sensorID, sensorName>;
53
Eddie Jamese7d976b2018-02-26 13:42:45 -060054// OCC sysfs name prefix
55const std::string sysfsName = "occ-hwmon";
56
Chris Cainc86d80f2023-05-04 15:49:18 -050057const uint8_t THROTTLED_NONE = 0x00;
58const uint8_t THROTTLED_POWER = 0x01;
59const uint8_t THROTTLED_THERMAL = 0x02;
60const uint8_t THROTTLED_SAFE = 0x04;
61const uint8_t THROTTLED_ALL = 0xFF;
62
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053063/** @class Status
64 * @brief Implementation of OCC Active Status
65 */
66class Status : public Interface
67{
Gunnar Mills94df8c92018-09-14 14:50:03 -050068 public:
69 Status() = delete;
70 ~Status() = default;
71 Status(const Status&) = delete;
72 Status& operator=(const Status&) = delete;
73 Status(Status&&) = default;
74 Status& operator=(Status&&) = default;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053075
Gunnar Mills94df8c92018-09-14 14:50:03 -050076 /** @brief Constructs the Status object and
77 * the underlying device object
78 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050079 * @param[in] event - sd_event unique pointer reference
80 * @param[in] path - DBus object path
81 * @param[in] manager - OCC manager instance
82 * @param[in] callBack - Callback handler to invoke during
83 * property change
Tom Joseph00325232020-07-29 17:51:48 +053084 * @param[in] resetCallBack - callback handler to invoke for resetting the
85 * OCC if PLDM is the host communication
86 * protocol
Gunnar Mills94df8c92018-09-14 14:50:03 -050087 */
Chris Cain17257672021-10-22 13:41:03 -050088 Status(EventPtr& event, const char* path, Manager& managerRef,
Chris Cain36f9cde2021-11-22 11:18:21 -060089#ifdef POWER10
Chris Cain5d66a0a2022-02-09 08:52:10 -060090 std::unique_ptr<powermode::PowerMode>& powerModeRef,
Chris Cain36f9cde2021-11-22 11:18:21 -060091#endif
Sheldon Bailey373af752022-02-21 15:14:00 -060092 std::function<void(instanceID, bool)> callBack = nullptr
Tom Joseph00325232020-07-29 17:51:48 +053093#ifdef PLDM
94 ,
95 std::function<void(instanceID)> resetCallBack = nullptr
96#endif
97 ) :
98
Patrick Williams1923a752022-04-05 21:35:45 -050099 Interface(utils::getBus(), getDbusPath(path).c_str(),
100 Interface::action::defer_emit),
Chris Cain1be43372021-12-09 19:29:37 -0600101 path(path), managerCallBack(callBack), instance(getInstance(path)),
Chris Cain17257672021-10-22 13:41:03 -0500102 manager(managerRef),
Chris Cain36f9cde2021-11-22 11:18:21 -0600103#ifdef POWER10
104 pmode(powerModeRef),
105#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500106 device(event,
Lei YU0ab90ca2017-07-13 17:02:23 +0800107#ifdef I2C_OCC
Eddie James774f9af2019-03-19 20:58:53 +0000108 fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
Lei YU0ab90ca2017-07-13 17:02:23 +0800109#else
Eddie James774f9af2019-03-19 20:58:53 +0000110 fs::path(DEV_PATH) /
111 fs::path(sysfsName + "." + std::to_string(instance + 1)),
Lei YU0ab90ca2017-07-13 17:02:23 +0800112#endif
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500113 managerRef, *this,
114#ifdef POWER10
115 powerModeRef,
116#endif
117 instance),
Gunnar Mills94df8c92018-09-14 14:50:03 -0500118 hostControlSignal(
George Liuf3b75142021-06-10 11:22:50 +0800119 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -0500120 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
121 sdbusRule::path("/org/open_power/control/host0") +
122 sdbusRule::interface("org.open_power.Control.Host") +
123 sdbusRule::argN(0, Control::convertForMessage(
124 Control::Host::Command::OCCReset)),
125 std::bind(std::mem_fn(&Status::hostControlEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -0600126 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +0800127 occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
128 (std::string(OCC_NAME) + std::to_string(instance)))
129 .c_str())
Chris Caina7b74dc2021-11-10 17:03:43 -0600130#ifdef POWER10
131 ,
132 sdpEvent(sdeventplus::Event::get_default()),
133 safeStateDelayTimer(
134 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
Sheldon Bailey373af752022-02-21 15:14:00 -0600135 sdpEvent, std::bind(&Status::safeStateDelayExpired, this))),
136 occReadStateFailTimer(
137 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
138 sdpEvent, std::bind(&Status::occReadStateNow, this)))
Chris Caina7b74dc2021-11-10 17:03:43 -0600139#endif
Sheldon Bailey373af752022-02-21 15:14:00 -0600140
Tom Joseph00325232020-07-29 17:51:48 +0530141#ifdef PLDM
142 ,
143 resetCallBack(resetCallBack)
144#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500145 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500146 // Announce that we are ready
147 this->emit_object_added();
148 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530149
Gunnar Mills94df8c92018-09-14 14:50:03 -0500150 /** @brief Since we are overriding the setter-occActive but not the
151 * getter-occActive, we need to have this using in order to
152 * allow passthrough usage of the getter-occActive
153 */
154 using Base::Status::occActive;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530155
Gunnar Mills94df8c92018-09-14 14:50:03 -0500156 /** @brief SET OccActive to True or False
157 *
158 * @param[in] value - Intended value
159 *
160 * @return - Updated value of the property
161 */
162 bool occActive(bool value) override;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530163
Gunnar Mills94df8c92018-09-14 14:50:03 -0500164 /** @brief Starts OCC error detection */
165 inline void addErrorWatch()
166 {
167 return device.addErrorWatch();
168 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530169
Gunnar Mills94df8c92018-09-14 14:50:03 -0500170 /** @brief Stops OCC error detection */
171 inline void removeErrorWatch()
172 {
173 return device.removeErrorWatch();
174 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600175
Gunnar Mills94df8c92018-09-14 14:50:03 -0500176 /** @brief Starts to watch how many OCCs are present on the master */
177 inline void addPresenceWatchMaster()
178 {
179 return device.addPresenceWatchMaster();
180 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530181
Chicago Duanbb895cb2021-06-18 19:37:16 +0800182 /** @brief Gets the occ instance number */
183 unsigned int getOccInstanceID()
184 {
185 return instance;
186 }
187
188 /** @brief Is this OCC the master OCC */
189 bool isMasterOcc()
190 {
191 return device.master();
192 }
193
Chris Caina8857c52021-01-27 11:53:05 -0600194 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
195 void readOccState();
196
Eddie James9789e712022-05-25 15:43:40 -0500197 /** @brief Called when device errors are detected
198 *
199 * @param[in] d - description of the error that occurred
200 */
201 void deviceError(Error::Descriptor d = Error::Descriptor());
Eddie Jamescbad2192021-10-07 09:39:39 -0500202
Chris Cain78e86012021-03-04 16:15:31 -0600203#ifdef POWER10
204 /** @brief Handle additional tasks when the OCCs reach active state */
205 void occsWentActive();
206
Chris Cain17257672021-10-22 13:41:03 -0500207 /** @brief Send Ambient & Altitude data to OCC
208 *
209 * @param[in] ambient - temperature to send (0xFF will force read
210 * of current temperature and altitude)
211 * @param[in] altitude - altitude to send (0xFFFF = unavailable)
212 *
213 * @return SUCCESS on success
214 */
215 CmdStatus sendAmbient(const uint8_t ambient = 0xFF,
216 const uint16_t altitude = 0xFFFF);
Chris Cain733b2012022-05-04 11:54:06 -0500217
218 /** @brief Set flag indicating if PLDM sensor has been received
219 *
220 * @param[in] wasReceived - true if PLDM sensor was read
221 */
222 void setPldmSensorReceived(const bool wasReceived)
223 {
224 pldmSensorStateReceived = wasReceived;
225 }
226
227 /** @brief Read flag indicating if PLDM sensor has been read
228 *
229 * @return true if sensor has been read
230 */
231 bool getPldmSensorReceived()
232 {
233 return pldmSensorStateReceived;
234 }
Chris Cain78e86012021-03-04 16:15:31 -0600235#endif // POWER10
236
Chris Cain5d66a0a2022-02-09 08:52:10 -0600237 /** @brief Return the HWMON path for this OCC
238 *
239 * @return path or empty path if not found
240 */
Chris Caine2d0a432022-03-28 11:08:49 -0500241 fs::path getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -0600242
Chris Cainc86d80f2023-05-04 15:49:18 -0500243 /** @brief Update the processor path associated with this OCC
244 */
245 void updateProcAssociation()
246 {
247 readProcAssociation();
248 if (nullptr != throttleHandle)
249 {
250 throttleHandle.reset();
251 }
252 if (!procPath.empty())
253 {
254 throttleHandle = std::make_unique<ThrottleInterface>(
255 utils::getBus(), procPath.c_str());
256 }
257 }
258
259 /** @brief Update the processor throttle status on dbus
260 */
261 void updateThrottle(const bool isThrottled, const uint8_t reason);
262
Gunnar Mills94df8c92018-09-14 14:50:03 -0500263 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500264 /** @brief OCC dbus object path */
265 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530266
Chris Cainc86d80f2023-05-04 15:49:18 -0500267 /** @brief Processor path associated with this OCC */
268 std::string procPath;
269
Gunnar Mills94df8c92018-09-14 14:50:03 -0500270 /** @brief Callback handler to be invoked during property change.
271 * This is a handler in Manager class
272 */
Sheldon Bailey373af752022-02-21 15:14:00 -0600273 std::function<void(instanceID, bool)> managerCallBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530274
Gunnar Mills94df8c92018-09-14 14:50:03 -0500275 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600276 unsigned int instance;
277
278 /** @brief The last state read from the OCC */
279 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530280
Sheldon Bailey373af752022-02-21 15:14:00 -0600281 /** @brief Number of retry attempts to open file and update state. */
282 const unsigned int occReadRetries = 1;
283
284 /** @brief Current number of retries attempted towards occReadRetries. */
285 size_t currentOccReadRetriesCount = 0;
286
287 /** @brief The Trigger to indicate OCC State is valid or not. */
288 bool stateValid = false;
289
Alexander Filippov1d69e192019-03-21 18:12:07 +0300290 /** @brief OCC instance to Sensor definitions mapping */
291 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530292
Chris Cain17257672021-10-22 13:41:03 -0500293 /** @brief OCC manager object */
294 const Manager& manager;
295
Chris Cain36f9cde2021-11-22 11:18:21 -0600296#ifdef POWER10
297 /** @brief OCC PowerMode object */
Chris Cain5d66a0a2022-02-09 08:52:10 -0600298 std::unique_ptr<powermode::PowerMode>& pmode;
Chris Cain36f9cde2021-11-22 11:18:21 -0600299#endif
300
Gunnar Mills94df8c92018-09-14 14:50:03 -0500301 /** @brief OCC device object to do bind and unbind */
302 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530303
Gunnar Mills94df8c92018-09-14 14:50:03 -0500304 /** @brief Subscribe to host control signal
305 *
306 * Once the OCC reset is requested, BMC sends that message to host.
307 * If the host does not ack the message, then there would be a timeout
308 * and we need to catch that to log an error
309 **/
310 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530311
Chris Caina8857c52021-01-27 11:53:05 -0600312 /** @brief Command object to send commands to the OCC */
313 OccCommand occCmd;
314
Chris Caina7b74dc2021-11-10 17:03:43 -0600315#ifdef POWER10
316 /** @brief timer event */
317 sdeventplus::Event sdpEvent;
318#endif
319
Chris Caine2d0a432022-03-28 11:08:49 -0500320 /** @brief hwmon path for this OCC */
321 fs::path hwmonPath;
322
Chris Cain733b2012022-05-04 11:54:06 -0500323 /** @brief flag indicating if the OCC sensor has been received */
324 bool pldmSensorStateReceived = false;
325
Gunnar Mills94df8c92018-09-14 14:50:03 -0500326 /** @brief Callback function on host control signals
327 *
328 * @param[in] msg - Data associated with subscribed signal
329 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500330 void hostControlEvent(sdbusplus::message_t& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530331
Gunnar Mills94df8c92018-09-14 14:50:03 -0500332 /** @brief Sends a message to host control command handler to reset OCC
333 */
334 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300335
336 /** @brief Determines the instance ID by specified object path.
337 * @param[in] path Estimated OCC Dbus object path
338 * @return Instance number
339 */
340 static int getInstance(const std::string& path)
341 {
342 return (path.empty() ? 0 : path.back() - '0');
343 }
344
Chris Cain78e86012021-03-04 16:15:31 -0600345#ifdef POWER10
Chris Caina7b74dc2021-11-10 17:03:43 -0600346 /**
347 * @brief Timer that is started when OCC is detected to be in safe mode
348 */
349 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
350 safeStateDelayTimer;
351
352 /** @brief Callback for timer that is started when OCC was detected to be in
353 * safe mode. Called to verify and then disable and reset the OCCs.
354 */
355 void safeStateDelayExpired();
Sheldon Bailey373af752022-02-21 15:14:00 -0600356
357 /**
358 * @brief Timer that is started when OCC read Valid state failed.
359 */
360 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
361 occReadStateFailTimer;
362
Chris Cain78e86012021-03-04 16:15:31 -0600363#endif // POWER10
Sheldon Bailey373af752022-02-21 15:14:00 -0600364 /** @brief Callback for timer that is started when OCC state
365 * was not able to be read. Called to attempt another read when needed.
366 */
367 void occReadStateNow();
Chris Cain78e86012021-03-04 16:15:31 -0600368
Alexander Filippov1d69e192019-03-21 18:12:07 +0300369 /** @brief Override the sensor name with name from the definition.
370 * @param[in] estimatedPath - Estimated OCC Dbus object path
371 * @return Fixed OCC DBus object path
372 */
373 static std::string getDbusPath(const std::string& estimatedPath)
374 {
375 if (!estimatedPath.empty())
376 {
377 auto it = sensorMap.find(getInstance(estimatedPath));
378 if (sensorMap.end() != it)
379 {
380 auto& name = std::get<1>(it->second);
381 if (!name.empty() && name != "None")
382 {
George Liuf3a4a692021-12-28 13:59:51 +0800383 auto objectPath = fs::path(estimatedPath);
384 objectPath.replace_filename(name);
385 return objectPath.string();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300386 }
387 }
388 }
389
390 return estimatedPath;
391 }
Tom Joseph00325232020-07-29 17:51:48 +0530392#ifdef PLDM
393 std::function<void(instanceID)> resetCallBack = nullptr;
394#endif
Chris Cainc86d80f2023-05-04 15:49:18 -0500395
396 /** @brief Current throttle reason(s) for this processor */
397 uint8_t throttleCause = THROTTLED_NONE;
398
399 /** @brief Throttle interface for the processor associated with this OCC */
400 std::unique_ptr<ThrottleInterface> throttleHandle;
401
402 /** @brief Read the processor path associated with this OCC */
403 void readProcAssociation();
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530404};
405
406} // namespace occ
407} // namespace open_power