blob: 6493040edd4b5e9062d7de2e59300d0ec4ad6134 [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>(
Chris Cainf0295f52024-09-12 15:41:14 -0500135 sdpEvent, std::bind(&Status::safeStateDelayExpired, this)))
Chris Caina7b74dc2021-11-10 17:03:43 -0600136#endif
Sheldon Bailey373af752022-02-21 15:14:00 -0600137
Tom Joseph00325232020-07-29 17:51:48 +0530138#ifdef PLDM
139 ,
140 resetCallBack(resetCallBack)
141#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500142 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500143 // Announce that we are ready
144 this->emit_object_added();
145 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530146
Gunnar Mills94df8c92018-09-14 14:50:03 -0500147 /** @brief Since we are overriding the setter-occActive but not the
148 * getter-occActive, we need to have this using in order to
149 * allow passthrough usage of the getter-occActive
150 */
151 using Base::Status::occActive;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530152
Gunnar Mills94df8c92018-09-14 14:50:03 -0500153 /** @brief SET OccActive to True or False
154 *
155 * @param[in] value - Intended value
156 *
157 * @return - Updated value of the property
158 */
159 bool occActive(bool value) override;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530160
Gunnar Mills94df8c92018-09-14 14:50:03 -0500161 /** @brief Starts OCC error detection */
162 inline void addErrorWatch()
163 {
164 return device.addErrorWatch();
165 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530166
Gunnar Mills94df8c92018-09-14 14:50:03 -0500167 /** @brief Stops OCC error detection */
168 inline void removeErrorWatch()
169 {
170 return device.removeErrorWatch();
171 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600172
Gunnar Mills94df8c92018-09-14 14:50:03 -0500173 /** @brief Starts to watch how many OCCs are present on the master */
174 inline void addPresenceWatchMaster()
175 {
176 return device.addPresenceWatchMaster();
177 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530178
Chicago Duanbb895cb2021-06-18 19:37:16 +0800179 /** @brief Gets the occ instance number */
180 unsigned int getOccInstanceID()
181 {
182 return instance;
183 }
184
185 /** @brief Is this OCC the master OCC */
186 bool isMasterOcc()
187 {
188 return device.master();
189 }
190
Chris Caina8857c52021-01-27 11:53:05 -0600191 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
192 void readOccState();
193
Eddie James9789e712022-05-25 15:43:40 -0500194 /** @brief Called when device errors are detected
195 *
196 * @param[in] d - description of the error that occurred
197 */
198 void deviceError(Error::Descriptor d = Error::Descriptor());
Eddie Jamescbad2192021-10-07 09:39:39 -0500199
Chris Cain78e86012021-03-04 16:15:31 -0600200#ifdef POWER10
201 /** @brief Handle additional tasks when the OCCs reach active state */
202 void occsWentActive();
203
Chris Cain17257672021-10-22 13:41:03 -0500204 /** @brief Send Ambient & Altitude data to OCC
205 *
206 * @param[in] ambient - temperature to send (0xFF will force read
207 * of current temperature and altitude)
208 * @param[in] altitude - altitude to send (0xFFFF = unavailable)
209 *
210 * @return SUCCESS on success
211 */
212 CmdStatus sendAmbient(const uint8_t ambient = 0xFF,
213 const uint16_t altitude = 0xFFFF);
Chris Cain733b2012022-05-04 11:54:06 -0500214
215 /** @brief Set flag indicating if PLDM sensor has been received
216 *
217 * @param[in] wasReceived - true if PLDM sensor was read
218 */
219 void setPldmSensorReceived(const bool wasReceived)
220 {
221 pldmSensorStateReceived = wasReceived;
222 }
223
224 /** @brief Read flag indicating if PLDM sensor has been read
225 *
226 * @return true if sensor has been read
227 */
228 bool getPldmSensorReceived()
229 {
230 return pldmSensorStateReceived;
231 }
Chris Cain78e86012021-03-04 16:15:31 -0600232#endif // POWER10
233
Chris Cain5d66a0a2022-02-09 08:52:10 -0600234 /** @brief Return the HWMON path for this OCC
235 *
236 * @return path or empty path if not found
237 */
Chris Caine2d0a432022-03-28 11:08:49 -0500238 fs::path getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -0600239
Chris Cainc86d80f2023-05-04 15:49:18 -0500240 /** @brief Update the processor path associated with this OCC
241 */
242 void updateProcAssociation()
243 {
244 readProcAssociation();
245 if (nullptr != throttleHandle)
246 {
247 throttleHandle.reset();
248 }
249 if (!procPath.empty())
250 {
251 throttleHandle = std::make_unique<ThrottleInterface>(
252 utils::getBus(), procPath.c_str());
253 }
254 }
255
256 /** @brief Update the processor throttle status on dbus
257 */
258 void updateThrottle(const bool isThrottled, const uint8_t reason);
259
Gunnar Mills94df8c92018-09-14 14:50:03 -0500260 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500261 /** @brief OCC dbus object path */
262 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530263
Chris Cainc86d80f2023-05-04 15:49:18 -0500264 /** @brief Processor path associated with this OCC */
265 std::string procPath;
266
Gunnar Mills94df8c92018-09-14 14:50:03 -0500267 /** @brief Callback handler to be invoked during property change.
268 * This is a handler in Manager class
269 */
Sheldon Bailey373af752022-02-21 15:14:00 -0600270 std::function<void(instanceID, bool)> managerCallBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530271
Gunnar Mills94df8c92018-09-14 14:50:03 -0500272 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600273 unsigned int instance;
274
275 /** @brief The last state read from the OCC */
276 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530277
Chris Cainf0295f52024-09-12 15:41:14 -0500278 /** @brief The last OCC read status (0 = no error) */
279 int lastOccReadStatus = 0;
280
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();
Chris Cain78e86012021-03-04 16:15:31 -0600356#endif // POWER10
Chris Cainf0295f52024-09-12 15:41:14 -0500357
Sheldon Bailey373af752022-02-21 15:14:00 -0600358 /** @brief Callback for timer that is started when OCC state
359 * was not able to be read. Called to attempt another read when needed.
360 */
361 void occReadStateNow();
Chris Cain78e86012021-03-04 16:15:31 -0600362
Alexander Filippov1d69e192019-03-21 18:12:07 +0300363 /** @brief Override the sensor name with name from the definition.
364 * @param[in] estimatedPath - Estimated OCC Dbus object path
365 * @return Fixed OCC DBus object path
366 */
367 static std::string getDbusPath(const std::string& estimatedPath)
368 {
369 if (!estimatedPath.empty())
370 {
371 auto it = sensorMap.find(getInstance(estimatedPath));
372 if (sensorMap.end() != it)
373 {
374 auto& name = std::get<1>(it->second);
375 if (!name.empty() && name != "None")
376 {
George Liuf3a4a692021-12-28 13:59:51 +0800377 auto objectPath = fs::path(estimatedPath);
378 objectPath.replace_filename(name);
379 return objectPath.string();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300380 }
381 }
382 }
383
384 return estimatedPath;
385 }
Tom Joseph00325232020-07-29 17:51:48 +0530386#ifdef PLDM
387 std::function<void(instanceID)> resetCallBack = nullptr;
388#endif
Chris Cainc86d80f2023-05-04 15:49:18 -0500389
390 /** @brief Current throttle reason(s) for this processor */
391 uint8_t throttleCause = THROTTLED_NONE;
392
393 /** @brief Throttle interface for the processor associated with this OCC */
394 std::unique_ptr<ThrottleInterface> throttleHandle;
395
396 /** @brief Read the processor path associated with this OCC */
397 void readProcAssociation();
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530398};
399
400} // namespace occ
401} // namespace open_power