blob: 3e9761f86351fa585b6fd628ca42cc9256f038d5 [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,
Sheldon Bailey16a5adb2025-06-10 14:10:06 -050050
Sheldon Baileyea2b22e2022-04-04 12:24:46 -050051 std::unique_ptr<powermode::PowerMode>& powerModeRef,
Sheldon Bailey16a5adb2025-06-10 14:10:06 -050052
Sheldon Baileyea2b22e2022-04-04 12:24:46 -050053 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",
Eddie Jamescbad2192021-10-07 09:39:39 -050063 std::bind(std::mem_fn(&Device::timeoutCallback), this,
Sheldon Bailey16a5adb2025-06-10 14:10:06 -050064 std::placeholders::_1)),
Eddie James2f9f9bb2021-09-20 14:26:31 -050065 ffdc(event, path / "ffdc", instance),
Eddie Jamescbad2192021-10-07 09:39:39 -050066 presence(event, path / "occs_present", manager,
67 std::bind(std::mem_fn(&Device::errorCallback), this,
68 std::placeholders::_1)),
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 throttleProcTemp(
Eddie James774f9af2019-03-19 20:58:53 +000070 event, path / "occ_dvfs_overtemp",
Gunnar Mills94df8c92018-09-14 14:50:03 -050071 std::bind(std::mem_fn(&Device::throttleProcTempCallback), this,
72 std::placeholders::_1)),
73 throttleProcPower(
Eddie James774f9af2019-03-19 20:58:53 +000074 event, path / "occ_dvfs_power",
Gunnar Mills94df8c92018-09-14 14:50:03 -050075 std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this,
76 std::placeholders::_1)),
Eddie James774f9af2019-03-19 20:58:53 +000077 throttleMemTemp(event, path / "occ_mem_throttle",
Gunnar Mills94df8c92018-09-14 14:50:03 -050078 std::bind(std::mem_fn(&Device::throttleMemTempCallback),
Sheldon Bailey16a5adb2025-06-10 14:10:06 -050079 this, std::placeholders::_1)),
Sheldon Baileyea2b22e2022-04-04 12:24:46 -050080 pmode(powerModeRef)
Gunnar Mills94df8c92018-09-14 14:50:03 -050081 {
82 // Nothing to do here
83 }
84
Eddie Jamesaced3092022-04-22 16:19:30 -050085 /** @brief Sets the device active or inactive
Gunnar Mills94df8c92018-09-14 14:50:03 -050086 *
Eddie Jamesaced3092022-04-22 16:19:30 -050087 * @param[in] active - Indicates whether or not to set the device active
Gunnar Mills94df8c92018-09-14 14:50:03 -050088 */
Eddie Jamesaced3092022-04-22 16:19:30 -050089 void setActive(bool active);
Gunnar Mills94df8c92018-09-14 14:50:03 -050090
Eddie James774f9af2019-03-19 20:58:53 +000091 /** @brief Starts to monitor for errors
92 *
93 * @param[in] poll - Indicates whether or not the error file should
94 * actually be polled for changes. Disabling polling is
95 * necessary for error files that don't support the poll
96 * file operation.
97 */
98 inline void addErrorWatch(bool poll = true)
Gunnar Mills94df8c92018-09-14 14:50:03 -050099 {
Chris Cainf0295f52024-09-12 15:41:14 -0500100 throttleProcTemp.addWatch(poll);
Eddie James774f9af2019-03-19 20:58:53 +0000101
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500102 if (master())
103 {
104 pmode->addIpsWatch(poll);
105 }
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500106
Eddie James774f9af2019-03-19 20:58:53 +0000107 throttleProcPower.addWatch(poll);
108 throttleMemTemp.addWatch(poll);
Eddie James2f9f9bb2021-09-20 14:26:31 -0500109
110 try
111 {
112 ffdc.addWatch(poll);
113 }
114 catch (const OpenFailure& e)
115 {
116 // nothing to do if there is no FFDC file
117 }
118
Eddie Jamescbad2192021-10-07 09:39:39 -0500119 try
120 {
121 timeout.addWatch(poll);
122 }
123 catch (const std::exception& e)
124 {
125 // nothing to do if there is no SBE timeout file
126 }
127
Eddie James774f9af2019-03-19 20:58:53 +0000128 error.addWatch(poll);
Gunnar Mills94df8c92018-09-14 14:50:03 -0500129 }
130
131 /** @brief stops monitoring for errors */
132 inline void removeErrorWatch()
133 {
134 // we can always safely remove watch even if we don't add it
135 presence.removeWatch();
Eddie James2f9f9bb2021-09-20 14:26:31 -0500136 ffdc.removeWatch();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500137 error.removeWatch();
Eddie Jamescbad2192021-10-07 09:39:39 -0500138 timeout.removeWatch();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500139 throttleMemTemp.removeWatch();
140 throttleProcPower.removeWatch();
141 throttleProcTemp.removeWatch();
Sheldon Bailey16a5adb2025-06-10 14:10:06 -0500142
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500143 if (master())
144 {
145 pmode->removeIpsWatch();
146 }
Gunnar Mills94df8c92018-09-14 14:50:03 -0500147 }
148
149 /** @brief Starts to watch how many OCCs are present on the master */
150 inline void addPresenceWatchMaster()
151 {
152 if (master())
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530153 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500154 presence.addWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530155 }
Gunnar Mills94df8c92018-09-14 14:50:03 -0500156 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530157
Eddie James774f9af2019-03-19 20:58:53 +0000158 /** @brief helper function to get the last part of the path
159 *
160 * @param[in] path - Path to parse
161 * @return - Last directory name in the path
162 */
163 static std::string getPathBack(const fs::path& path);
164
Eddie Jamesaced3092022-04-22 16:19:30 -0500165 /** @brief Returns true if the device is active */
166 bool active() const;
167
Chris Cain78e86012021-03-04 16:15:31 -0600168 /** @brief Returns true if device represents the master OCC */
169 bool master() const;
170
Gunnar Mills94df8c92018-09-14 14:50:03 -0500171 private:
Eddie James774f9af2019-03-19 20:58:53 +0000172 /** @brief This directory contains the error files */
173 const fs::path devPath;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530174
Eddie Jamescbad2192021-10-07 09:39:39 -0500175 /** @brief OCC instance ID */
176 const unsigned int instance;
177
Gunnar Mills94df8c92018-09-14 14:50:03 -0500178 /** Store the associated Status instance */
179 Status& statusObject;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530180
Eddie Jamescbad2192021-10-07 09:39:39 -0500181 /** Store the parent Manager instance */
182 Manager& managerObject;
183
Gunnar Mills94df8c92018-09-14 14:50:03 -0500184 /** Abstraction of error monitoring */
185 Error error;
Eddie Jamesdae2d942017-12-20 10:50:03 -0600186
Eddie Jamescbad2192021-10-07 09:39:39 -0500187 /** Abstraction of SBE timeout monitoring */
188 Error timeout;
189
Eddie James2f9f9bb2021-09-20 14:26:31 -0500190 /** SBE FFDC monitoring */
191 FFDC ffdc;
192
Gunnar Mills94df8c92018-09-14 14:50:03 -0500193 /** Abstraction of OCC presence monitoring */
194 Presence presence;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530195
Gunnar Mills94df8c92018-09-14 14:50:03 -0500196 /** Error instances for watching for throttling events */
197 Error throttleProcTemp;
198 Error throttleProcPower;
199 Error throttleMemTemp;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530200
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500201 /** @brief OCC PowerMode object */
202 std::unique_ptr<powermode::PowerMode>& pmode;
Sheldon Baileyea2b22e2022-04-04 12:24:46 -0500203
Eddie Jamesaced3092022-04-22 16:19:30 -0500204 /** @brief file reader to read a binary string ("1" or "0")
205 *
206 * @param[in] fileName - Name of file to be read
207 * @return - The value returned by reading the file
208 */
209 bool readBinary(const std::string& fileName) const;
210
Gunnar Mills94df8c92018-09-14 14:50:03 -0500211 /** @brief file writer to achieve bind and unbind
212 *
213 * @param[in] filename - Name of file to be written
214 * @param[in] data - Data to be written to
215 * @return - None
216 */
217 void write(const fs::path& fileName, const std::string& data)
218 {
219 // If there is an error, move the exception all the way up
220 std::ofstream file(fileName, std::ios::out);
221 file << data;
222 file.close();
223 return;
224 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530225
Eddie James9789e712022-05-25 15:43:40 -0500226 /** @brief callback for OCC error monitoring
Eddie Jamescbad2192021-10-07 09:39:39 -0500227 *
Eddie James9789e712022-05-25 15:43:40 -0500228 * @param[in] error - Errno stored in the error file, 0 if no error
Eddie Jamescbad2192021-10-07 09:39:39 -0500229 */
Eddie James9789e712022-05-25 15:43:40 -0500230 void errorCallback(int error);
231
232 /** @brief callback for OCC presence monitoring
233 *
234 * @param[in] occsPresent - The number of OCCs indicated in the poll
235 * response
236 */
237 void presenceCallback(int occsPresent);
Eddie Jamescbad2192021-10-07 09:39:39 -0500238
Eddie Jamescbad2192021-10-07 09:39:39 -0500239 /** @brief callback for SBE timeout monitoring
240 *
241 * @param[in] error - True if an error is reported, false otherwise
242 */
Eddie James9789e712022-05-25 15:43:40 -0500243 void timeoutCallback(int error);
Eddie Jamescbad2192021-10-07 09:39:39 -0500244
Gunnar Mills94df8c92018-09-14 14:50:03 -0500245 /** @brief callback for the proc temp throttle event
246 *
247 * @param[in] error - True if an error is reported, false otherwise
248 */
Eddie James9789e712022-05-25 15:43:40 -0500249 void throttleProcTempCallback(int error);
Eddie James482e31f2017-09-14 13:17:17 -0500250
Gunnar Mills94df8c92018-09-14 14:50:03 -0500251 /** @brief callback for the proc power throttle event
252 *
253 * @param[in] error - True if an error is reported, false otherwise
254 */
Eddie James9789e712022-05-25 15:43:40 -0500255 void throttleProcPowerCallback(int error);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530256
Gunnar Mills94df8c92018-09-14 14:50:03 -0500257 /** @brief callback for the proc temp throttle event
258 *
259 * @param[in] error - True if an error is reported, false otherwise
260 */
Eddie James9789e712022-05-25 15:43:40 -0500261 void throttleMemTempCallback(int error);
Chris Cain5d66a0a2022-02-09 08:52:10 -0600262
263 /** @brief Get the pathname for a file based on a regular expression
264 *
265 * @param[in] basePath - The path where the files will be checked
266 * @param[in] expr - Regular expression describing the target file
267 *
268 * @return path to the file or empty path if not found
269 */
Chris Caine2d0a432022-03-28 11:08:49 -0500270 fs::path getFilenameByRegex(fs::path basePath,
271 const std::regex& expr) const;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530272};
273
274} // namespace occ
275} // namespace open_power