Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 3 | #include "config.h" |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 4 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 5 | #include "occ_errors.hpp" |
| 6 | #include "occ_events.hpp" |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 7 | #include "occ_ffdc.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 8 | #include "occ_presence.hpp" |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 9 | #include "powermode.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 10 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 11 | #include <org/open_power/OCC/Device/error.hpp> |
| 12 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 13 | #include <filesystem> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 14 | #include <fstream> |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 15 | #include <regex> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 16 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 17 | namespace open_power |
| 18 | { |
| 19 | namespace occ |
| 20 | { |
| 21 | |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 22 | class Manager; |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 23 | class Status; |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 25 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 26 | |
| 27 | /** @class Device |
| 28 | * @brief Binds and unbinds the OCC driver upon request |
| 29 | */ |
| 30 | class Device |
| 31 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 32 | 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 Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 39 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 40 | /** @brief Constructs the Device object |
| 41 | * |
| 42 | * @param[in] event - Unique ptr reference to sd_event |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 43 | * @param[in] path - Path to the OCC instance |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 44 | * @param[in] manager - OCC manager instance |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 45 | * @param[in] status - Status instance |
| 46 | * @param[in] instance - OCC instance number |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 47 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 48 | Device(EventPtr& event, const fs::path& path, Manager& manager, |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 49 | Status& status, |
| 50 | #ifdef POWER10 |
| 51 | std::unique_ptr<powermode::PowerMode>& powerModeRef, |
| 52 | #endif |
| 53 | unsigned int instance = 0) : |
Patrick Williams | d7542c8 | 2024-08-16 15:20:28 -0400 | [diff] [blame] | 54 | devPath(path), instance(instance), statusObject(status), |
| 55 | managerObject(manager), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 56 | 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 James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 70 | ffdc(event, path / "ffdc", instance), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 71 | presence(event, path / "occs_present", manager, |
| 72 | std::bind(std::mem_fn(&Device::errorCallback), this, |
| 73 | std::placeholders::_1)), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 74 | throttleProcTemp( |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 75 | event, path / "occ_dvfs_overtemp", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 76 | std::bind(std::mem_fn(&Device::throttleProcTempCallback), this, |
| 77 | std::placeholders::_1)), |
| 78 | throttleProcPower( |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 79 | event, path / "occ_dvfs_power", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 80 | std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this, |
| 81 | std::placeholders::_1)), |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 82 | throttleMemTemp(event, path / "occ_mem_throttle", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 83 | std::bind(std::mem_fn(&Device::throttleMemTempCallback), |
| 84 | this, std::placeholders::_1)) |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 85 | #ifdef POWER10 |
| 86 | , |
| 87 | pmode(powerModeRef) |
| 88 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 89 | { |
| 90 | // Nothing to do here |
| 91 | } |
| 92 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 93 | /** @brief Sets the device active or inactive |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 94 | * |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 95 | * @param[in] active - Indicates whether or not to set the device active |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 96 | */ |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 97 | void setActive(bool active); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 98 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 99 | /** @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 Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 107 | { |
Chris Cain | f0295f5 | 2024-09-12 15:41:14 -0500 | [diff] [blame] | 108 | #ifdef POWER10 |
| 109 | throttleProcTemp.addWatch(poll); |
| 110 | #else |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 111 | 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 Cain | f0295f5 | 2024-09-12 15:41:14 -0500 | [diff] [blame] | 121 | #endif |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 122 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 123 | #ifdef POWER10 |
| 124 | if (master()) |
| 125 | { |
| 126 | pmode->addIpsWatch(poll); |
| 127 | } |
| 128 | #endif |
| 129 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 130 | throttleProcPower.addWatch(poll); |
| 131 | throttleMemTemp.addWatch(poll); |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 132 | |
| 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 James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 142 | 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 James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 151 | error.addWatch(poll); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 152 | } |
| 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 James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 159 | ffdc.removeWatch(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 160 | error.removeWatch(); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 161 | timeout.removeWatch(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 162 | throttleMemTemp.removeWatch(); |
| 163 | throttleProcPower.removeWatch(); |
| 164 | throttleProcTemp.removeWatch(); |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 165 | #ifdef POWER10 |
| 166 | if (master()) |
| 167 | { |
| 168 | pmode->removeIpsWatch(); |
| 169 | } |
| 170 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** @brief Starts to watch how many OCCs are present on the master */ |
| 174 | inline void addPresenceWatchMaster() |
| 175 | { |
| 176 | if (master()) |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 177 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 178 | presence.addWatch(); |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 179 | } |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 180 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 181 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 182 | /** @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 James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 189 | /** @brief Returns true if the device is active */ |
| 190 | bool active() const; |
| 191 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 192 | /** @brief Returns true if device represents the master OCC */ |
| 193 | bool master() const; |
| 194 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 195 | private: |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 196 | /** @brief This directory contains the error files */ |
| 197 | const fs::path devPath; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 198 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 199 | /** @brief OCC instance ID */ |
| 200 | const unsigned int instance; |
| 201 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 202 | /** Store the associated Status instance */ |
| 203 | Status& statusObject; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 204 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 205 | /** Store the parent Manager instance */ |
| 206 | Manager& managerObject; |
| 207 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 208 | /** Abstraction of error monitoring */ |
| 209 | Error error; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 210 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 211 | /** Abstraction of SBE timeout monitoring */ |
| 212 | Error timeout; |
| 213 | |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 214 | /** SBE FFDC monitoring */ |
| 215 | FFDC ffdc; |
| 216 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 217 | /** Abstraction of OCC presence monitoring */ |
| 218 | Presence presence; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 219 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 220 | /** Error instances for watching for throttling events */ |
| 221 | Error throttleProcTemp; |
| 222 | Error throttleProcPower; |
| 223 | Error throttleMemTemp; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 224 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 225 | #ifdef POWER10 |
| 226 | /** @brief OCC PowerMode object */ |
| 227 | std::unique_ptr<powermode::PowerMode>& pmode; |
| 228 | #endif |
| 229 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 230 | /** @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 Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 237 | /** @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 Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 251 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 252 | /** @brief callback for OCC error monitoring |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 253 | * |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 254 | * @param[in] error - Errno stored in the error file, 0 if no error |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 255 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 256 | 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 James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 264 | |
| 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 James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 270 | void timeoutCallback(int error); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 271 | #endif |
| 272 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 273 | /** @brief callback for the proc temp throttle event |
| 274 | * |
| 275 | * @param[in] error - True if an error is reported, false otherwise |
| 276 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 277 | void throttleProcTempCallback(int error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 278 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 279 | /** @brief callback for the proc power throttle event |
| 280 | * |
| 281 | * @param[in] error - True if an error is reported, false otherwise |
| 282 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 283 | void throttleProcPowerCallback(int error); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 284 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 285 | /** @brief callback for the proc temp throttle event |
| 286 | * |
| 287 | * @param[in] error - True if an error is reported, false otherwise |
| 288 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 289 | void throttleMemTempCallback(int error); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 290 | |
| 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 Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 298 | fs::path getFilenameByRegex(fs::path basePath, |
| 299 | const std::regex& expr) const; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | } // namespace occ |
| 303 | } // namespace open_power |