blob: d44409b4160cfb9c51f33f6c620f0794e6d42886 [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#pragma once
2
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05303#include "config.h"
Lei YU0ab90ca2017-07-13 17:02:23 +08004
Gunnar Mills94df8c92018-09-14 14:50:03 -05005#include "occ_errors.hpp"
6#include "occ_events.hpp"
Eddie James2f9f9bb2021-09-20 14:26:31 -05007#include "occ_ffdc.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05008#include "occ_presence.hpp"
Sheldon Baileyea2b22e2022-04-04 12:24:46 -05009#include "powermode.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -050010
George Liub5ca1012021-09-10 12:53:11 +080011#include <org/open_power/OCC/Device/error.hpp>
12
George Liubcef3b42021-09-10 12:39:02 +080013#include <filesystem>
Gunnar Mills94df8c92018-09-14 14:50:03 -050014#include <fstream>
Chris Cain5d66a0a2022-02-09 08:52:10 -060015#include <regex>
Gunnar Mills94df8c92018-09-14 14:50:03 -050016
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053017namespace open_power
18{
19namespace occ
20{
21
Edward A. James636577f2017-10-06 10:53:55 -050022class Manager;
Eddie James482e31f2017-09-14 13:17:17 -050023class Status;
George Liubcef3b42021-09-10 12:39:02 +080024namespace fs = std::filesystem;
Eddie James774f9af2019-03-19 20:58:53 +000025using namespace sdbusplus::org::open_power::OCC::Device::Error;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053026
27/** @class Device
28 * @brief Binds and unbinds the OCC driver upon request
29 */
30class Device
31{
Gunnar Mills94df8c92018-09-14 14:50:03 -050032 public:
33 Device() = delete;
34 ~Device() = default;
35 Device(const Device&) = delete;
36 Device& operator=(const Device&) = delete;
37 Device(Device&&) = default;
38 Device& operator=(Device&&) = default;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053039
Gunnar Mills94df8c92018-09-14 14:50:03 -050040 /** @brief Constructs the Device object
41 *
42 * @param[in] event - Unique ptr reference to sd_event
Eddie James774f9af2019-03-19 20:58:53 +000043 * @param[in] path - Path to the OCC instance
Gunnar Mills94df8c92018-09-14 14:50:03 -050044 * @param[in] manager - OCC manager instance
Eddie James2f9f9bb2021-09-20 14:26:31 -050045 * @param[in] status - Status instance
46 * @param[in] instance - OCC instance number
Gunnar Mills94df8c92018-09-14 14:50:03 -050047 */
Eddie Jamescbad2192021-10-07 09:39:39 -050048 Device(EventPtr& event, const fs::path& path, Manager& manager,
Sheldon Baileyea2b22e2022-04-04 12:24:46 -050049 Status& status,
50#ifdef POWER10
51 std::unique_ptr<powermode::PowerMode>& powerModeRef,
52#endif
53 unsigned int instance = 0) :
Patrick Williamsd7542c82024-08-16 15:20:28 -040054 devPath(path), instance(instance), statusObject(status),
55 managerObject(manager),
Eddie Jamescbad2192021-10-07 09:39:39 -050056 error(event, path / "occ_error",
57 std::bind(std::mem_fn(&Device::errorCallback), this,
58 std::placeholders::_1)),
59 timeout(event,
60 path /
61 fs::path("../../sbefifo" + std::to_string(instance + 1)) /
62 "timeout",
63#ifdef PLDM
64 std::bind(std::mem_fn(&Device::timeoutCallback), this,
65 std::placeholders::_1)
66#else
67 nullptr
68#endif
69 ),
Eddie James2f9f9bb2021-09-20 14:26:31 -050070 ffdc(event, path / "ffdc", instance),
Eddie Jamescbad2192021-10-07 09:39:39 -050071 presence(event, path / "occs_present", manager,
72 std::bind(std::mem_fn(&Device::errorCallback), this,
73 std::placeholders::_1)),
Gunnar Mills94df8c92018-09-14 14:50:03 -050074 throttleProcTemp(
Eddie James774f9af2019-03-19 20:58:53 +000075 event, path / "occ_dvfs_overtemp",
Gunnar Mills94df8c92018-09-14 14:50:03 -050076 std::bind(std::mem_fn(&Device::throttleProcTempCallback), this,
77 std::placeholders::_1)),
78 throttleProcPower(
Eddie James774f9af2019-03-19 20:58:53 +000079 event, path / "occ_dvfs_power",
Gunnar Mills94df8c92018-09-14 14:50:03 -050080 std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this,
81 std::placeholders::_1)),
Eddie James774f9af2019-03-19 20:58:53 +000082 throttleMemTemp(event, path / "occ_mem_throttle",
Gunnar Mills94df8c92018-09-14 14:50:03 -050083 std::bind(std::mem_fn(&Device::throttleMemTempCallback),
84 this, std::placeholders::_1))
Sheldon Baileyea2b22e2022-04-04 12:24:46 -050085#ifdef POWER10
86 ,
87 pmode(powerModeRef)
88#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050089 {
90 // Nothing to do here
91 }
92
Eddie Jamesaced3092022-04-22 16:19:30 -050093 /** @brief Sets the device active or inactive
Gunnar Mills94df8c92018-09-14 14:50:03 -050094 *
Eddie Jamesaced3092022-04-22 16:19:30 -050095 * @param[in] active - Indicates whether or not to set the device active
Gunnar Mills94df8c92018-09-14 14:50:03 -050096 */
Eddie Jamesaced3092022-04-22 16:19:30 -050097 void setActive(bool active);
Gunnar Mills94df8c92018-09-14 14:50:03 -050098
Eddie James774f9af2019-03-19 20:58:53 +000099 /** @brief Starts to monitor for errors
100 *
101 * @param[in] poll - Indicates whether or not the error file should
102 * actually be polled for changes. Disabling polling is
103 * necessary for error files that don't support the poll
104 * file operation.
105 */
106 inline void addErrorWatch(bool poll = true)
Gunnar Mills94df8c92018-09-14 14:50:03 -0500107 {
Chris Cainf0295f52024-09-12 15:41:14 -0500108#ifdef POWER10
109 throttleProcTemp.addWatch(poll);
110#else
Eddie James774f9af2019-03-19 20:58:53 +0000111 try
112 {
113 throttleProcTemp.addWatch(poll);
114 }
115 catch (const OpenFailure& e)
116 {
117 // try the old kernel version
118 throttleProcTemp.setFile(devPath / "occ_dvfs_ot");
119 throttleProcTemp.addWatch(poll);
120 }
Chris Cainf0295f52024-09-12 15:41:14 -0500121#endif
Eddie James774f9af2019-03-19 20:58:53 +0000122
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500123#ifdef POWER10
124 if (master())
125 {
126 pmode->addIpsWatch(poll);
127 }
128#endif
129
Eddie James774f9af2019-03-19 20:58:53 +0000130 throttleProcPower.addWatch(poll);
131 throttleMemTemp.addWatch(poll);
Eddie James2f9f9bb2021-09-20 14:26:31 -0500132
133 try
134 {
135 ffdc.addWatch(poll);
136 }
137 catch (const OpenFailure& e)
138 {
139 // nothing to do if there is no FFDC file
140 }
141
Eddie Jamescbad2192021-10-07 09:39:39 -0500142 try
143 {
144 timeout.addWatch(poll);
145 }
146 catch (const std::exception& e)
147 {
148 // nothing to do if there is no SBE timeout file
149 }
150
Eddie James774f9af2019-03-19 20:58:53 +0000151 error.addWatch(poll);
Gunnar Mills94df8c92018-09-14 14:50:03 -0500152 }
153
154 /** @brief stops monitoring for errors */
155 inline void removeErrorWatch()
156 {
157 // we can always safely remove watch even if we don't add it
158 presence.removeWatch();
Eddie James2f9f9bb2021-09-20 14:26:31 -0500159 ffdc.removeWatch();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500160 error.removeWatch();
Eddie Jamescbad2192021-10-07 09:39:39 -0500161 timeout.removeWatch();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500162 throttleMemTemp.removeWatch();
163 throttleProcPower.removeWatch();
164 throttleProcTemp.removeWatch();
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500165#ifdef POWER10
166 if (master())
167 {
168 pmode->removeIpsWatch();
169 }
170#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500171 }
172
173 /** @brief Starts to watch how many OCCs are present on the master */
174 inline void addPresenceWatchMaster()
175 {
176 if (master())
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530177 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500178 presence.addWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530179 }
Gunnar Mills94df8c92018-09-14 14:50:03 -0500180 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530181
Eddie James774f9af2019-03-19 20:58:53 +0000182 /** @brief helper function to get the last part of the path
183 *
184 * @param[in] path - Path to parse
185 * @return - Last directory name in the path
186 */
187 static std::string getPathBack(const fs::path& path);
188
Eddie Jamesaced3092022-04-22 16:19:30 -0500189 /** @brief Returns true if the device is active */
190 bool active() const;
191
Chris Cain78e86012021-03-04 16:15:31 -0600192 /** @brief Returns true if device represents the master OCC */
193 bool master() const;
194
Gunnar Mills94df8c92018-09-14 14:50:03 -0500195 private:
Eddie James774f9af2019-03-19 20:58:53 +0000196 /** @brief This directory contains the error files */
197 const fs::path devPath;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530198
Eddie Jamescbad2192021-10-07 09:39:39 -0500199 /** @brief OCC instance ID */
200 const unsigned int instance;
201
Gunnar Mills94df8c92018-09-14 14:50:03 -0500202 /** Store the associated Status instance */
203 Status& statusObject;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530204
Eddie Jamescbad2192021-10-07 09:39:39 -0500205 /** Store the parent Manager instance */
206 Manager& managerObject;
207
Gunnar Mills94df8c92018-09-14 14:50:03 -0500208 /** Abstraction of error monitoring */
209 Error error;
Eddie Jamesdae2d942017-12-20 10:50:03 -0600210
Eddie Jamescbad2192021-10-07 09:39:39 -0500211 /** Abstraction of SBE timeout monitoring */
212 Error timeout;
213
Eddie James2f9f9bb2021-09-20 14:26:31 -0500214 /** SBE FFDC monitoring */
215 FFDC ffdc;
216
Gunnar Mills94df8c92018-09-14 14:50:03 -0500217 /** Abstraction of OCC presence monitoring */
218 Presence presence;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530219
Gunnar Mills94df8c92018-09-14 14:50:03 -0500220 /** Error instances for watching for throttling events */
221 Error throttleProcTemp;
222 Error throttleProcPower;
223 Error throttleMemTemp;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530224
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500225#ifdef POWER10
226 /** @brief OCC PowerMode object */
227 std::unique_ptr<powermode::PowerMode>& pmode;
228#endif
229
Eddie Jamesaced3092022-04-22 16:19:30 -0500230 /** @brief file reader to read a binary string ("1" or "0")
231 *
232 * @param[in] fileName - Name of file to be read
233 * @return - The value returned by reading the file
234 */
235 bool readBinary(const std::string& fileName) const;
236
Gunnar Mills94df8c92018-09-14 14:50:03 -0500237 /** @brief file writer to achieve bind and unbind
238 *
239 * @param[in] filename - Name of file to be written
240 * @param[in] data - Data to be written to
241 * @return - None
242 */
243 void write(const fs::path& fileName, const std::string& data)
244 {
245 // If there is an error, move the exception all the way up
246 std::ofstream file(fileName, std::ios::out);
247 file << data;
248 file.close();
249 return;
250 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530251
Eddie James9789e712022-05-25 15:43:40 -0500252 /** @brief callback for OCC error monitoring
Eddie Jamescbad2192021-10-07 09:39:39 -0500253 *
Eddie James9789e712022-05-25 15:43:40 -0500254 * @param[in] error - Errno stored in the error file, 0 if no error
Eddie Jamescbad2192021-10-07 09:39:39 -0500255 */
Eddie James9789e712022-05-25 15:43:40 -0500256 void errorCallback(int error);
257
258 /** @brief callback for OCC presence monitoring
259 *
260 * @param[in] occsPresent - The number of OCCs indicated in the poll
261 * response
262 */
263 void presenceCallback(int occsPresent);
Eddie Jamescbad2192021-10-07 09:39:39 -0500264
265#ifdef PLDM
266 /** @brief callback for SBE timeout monitoring
267 *
268 * @param[in] error - True if an error is reported, false otherwise
269 */
Eddie James9789e712022-05-25 15:43:40 -0500270 void timeoutCallback(int error);
Eddie Jamescbad2192021-10-07 09:39:39 -0500271#endif
272
Gunnar Mills94df8c92018-09-14 14:50:03 -0500273 /** @brief callback for the proc temp throttle event
274 *
275 * @param[in] error - True if an error is reported, false otherwise
276 */
Eddie James9789e712022-05-25 15:43:40 -0500277 void throttleProcTempCallback(int error);
Eddie James482e31f2017-09-14 13:17:17 -0500278
Gunnar Mills94df8c92018-09-14 14:50:03 -0500279 /** @brief callback for the proc power throttle event
280 *
281 * @param[in] error - True if an error is reported, false otherwise
282 */
Eddie James9789e712022-05-25 15:43:40 -0500283 void throttleProcPowerCallback(int error);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530284
Gunnar Mills94df8c92018-09-14 14:50:03 -0500285 /** @brief callback for the proc temp throttle event
286 *
287 * @param[in] error - True if an error is reported, false otherwise
288 */
Eddie James9789e712022-05-25 15:43:40 -0500289 void throttleMemTempCallback(int error);
Chris Cain5d66a0a2022-02-09 08:52:10 -0600290
291 /** @brief Get the pathname for a file based on a regular expression
292 *
293 * @param[in] basePath - The path where the files will be checked
294 * @param[in] expr - Regular expression describing the target file
295 *
296 * @return path to the file or empty path if not found
297 */
Chris Caine2d0a432022-03-28 11:08:49 -0500298 fs::path getFilenameByRegex(fs::path basePath,
299 const std::regex& expr) const;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530300};
301
302} // namespace occ
303} // namespace open_power