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) : |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 54 | config(getPathBack(path)), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 55 | devPath(path), instance(instance), statusObject(status), |
| 56 | managerObject(manager), |
| 57 | error(event, path / "occ_error", |
| 58 | std::bind(std::mem_fn(&Device::errorCallback), this, |
| 59 | std::placeholders::_1)), |
| 60 | timeout(event, |
| 61 | path / |
| 62 | fs::path("../../sbefifo" + std::to_string(instance + 1)) / |
| 63 | "timeout", |
| 64 | #ifdef PLDM |
| 65 | std::bind(std::mem_fn(&Device::timeoutCallback), this, |
| 66 | std::placeholders::_1) |
| 67 | #else |
| 68 | nullptr |
| 69 | #endif |
| 70 | ), |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 71 | ffdc(event, path / "ffdc", instance), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 72 | presence(event, path / "occs_present", manager, |
| 73 | std::bind(std::mem_fn(&Device::errorCallback), this, |
| 74 | std::placeholders::_1)), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 75 | throttleProcTemp( |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 76 | event, path / "occ_dvfs_overtemp", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 77 | std::bind(std::mem_fn(&Device::throttleProcTempCallback), this, |
| 78 | std::placeholders::_1)), |
| 79 | throttleProcPower( |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 80 | event, path / "occ_dvfs_power", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 81 | std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this, |
| 82 | std::placeholders::_1)), |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 83 | throttleMemTemp(event, path / "occ_mem_throttle", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 84 | std::bind(std::mem_fn(&Device::throttleMemTempCallback), |
| 85 | this, std::placeholders::_1)) |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 86 | #ifdef POWER10 |
| 87 | , |
| 88 | pmode(powerModeRef) |
| 89 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 90 | { |
| 91 | // Nothing to do here |
| 92 | } |
| 93 | |
| 94 | /** @brief Binds device to the OCC driver */ |
| 95 | inline void bind() |
| 96 | { |
| 97 | // Bind the device |
| 98 | return write(bindPath, config); |
| 99 | } |
| 100 | |
| 101 | /** @brief Un-binds device from the OCC driver */ |
| 102 | inline void unBind() |
| 103 | { |
| 104 | // Unbind the device |
| 105 | return write(unBindPath, config); |
| 106 | } |
| 107 | |
| 108 | /** @brief Returns if device is already bound. |
| 109 | * |
| 110 | * On device bind, a soft link by the name $config |
| 111 | * gets created in OCC_HWMON_PATH and gets removed |
| 112 | * on unbind |
| 113 | * |
| 114 | * @return true if bound, else false |
| 115 | */ |
| 116 | inline bool bound() const |
| 117 | { |
| 118 | return fs::exists(OCC_HWMON_PATH + config); |
| 119 | } |
| 120 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 121 | /** @brief Starts to monitor for errors |
| 122 | * |
| 123 | * @param[in] poll - Indicates whether or not the error file should |
| 124 | * actually be polled for changes. Disabling polling is |
| 125 | * necessary for error files that don't support the poll |
| 126 | * file operation. |
| 127 | */ |
| 128 | inline void addErrorWatch(bool poll = true) |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 129 | { |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 130 | try |
| 131 | { |
| 132 | throttleProcTemp.addWatch(poll); |
| 133 | } |
| 134 | catch (const OpenFailure& e) |
| 135 | { |
| 136 | // try the old kernel version |
| 137 | throttleProcTemp.setFile(devPath / "occ_dvfs_ot"); |
| 138 | throttleProcTemp.addWatch(poll); |
| 139 | } |
| 140 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 141 | #ifdef POWER10 |
| 142 | if (master()) |
| 143 | { |
| 144 | pmode->addIpsWatch(poll); |
| 145 | } |
| 146 | #endif |
| 147 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 148 | throttleProcPower.addWatch(poll); |
| 149 | throttleMemTemp.addWatch(poll); |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 150 | |
| 151 | try |
| 152 | { |
| 153 | ffdc.addWatch(poll); |
| 154 | } |
| 155 | catch (const OpenFailure& e) |
| 156 | { |
| 157 | // nothing to do if there is no FFDC file |
| 158 | } |
| 159 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 160 | try |
| 161 | { |
| 162 | timeout.addWatch(poll); |
| 163 | } |
| 164 | catch (const std::exception& e) |
| 165 | { |
| 166 | // nothing to do if there is no SBE timeout file |
| 167 | } |
| 168 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 169 | error.addWatch(poll); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** @brief stops monitoring for errors */ |
| 173 | inline void removeErrorWatch() |
| 174 | { |
| 175 | // we can always safely remove watch even if we don't add it |
| 176 | presence.removeWatch(); |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 177 | ffdc.removeWatch(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 178 | error.removeWatch(); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 179 | timeout.removeWatch(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 180 | throttleMemTemp.removeWatch(); |
| 181 | throttleProcPower.removeWatch(); |
| 182 | throttleProcTemp.removeWatch(); |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 183 | #ifdef POWER10 |
| 184 | if (master()) |
| 185 | { |
| 186 | pmode->removeIpsWatch(); |
| 187 | } |
| 188 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** @brief Starts to watch how many OCCs are present on the master */ |
| 192 | inline void addPresenceWatchMaster() |
| 193 | { |
| 194 | if (master()) |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 195 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 196 | presence.addWatch(); |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 197 | } |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 198 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 199 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 200 | /** @brief helper function to get the last part of the path |
| 201 | * |
| 202 | * @param[in] path - Path to parse |
| 203 | * @return - Last directory name in the path |
| 204 | */ |
| 205 | static std::string getPathBack(const fs::path& path); |
| 206 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 207 | /** @brief Returns true if device represents the master OCC */ |
| 208 | bool master() const; |
| 209 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 210 | private: |
| 211 | /** @brief Config value to be used to do bind and unbind */ |
| 212 | const std::string config; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 213 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 214 | /** @brief This directory contains the error files */ |
| 215 | const fs::path devPath; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 216 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 217 | /** @brief OCC instance ID */ |
| 218 | const unsigned int instance; |
| 219 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 220 | /** @brief To bind the device to the OCC driver, do: |
| 221 | * |
| 222 | * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/bind |
| 223 | */ |
| 224 | static fs::path bindPath; |
Vishwanatha Subbanna | b57f151 | 2017-09-04 15:06:20 +0530 | [diff] [blame] | 225 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 226 | /** @brief To un-bind the device from the OCC driver, do: |
| 227 | * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/unbind |
| 228 | */ |
| 229 | static fs::path unBindPath; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 230 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 231 | /** Store the associated Status instance */ |
| 232 | Status& statusObject; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 233 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 234 | /** Store the parent Manager instance */ |
| 235 | Manager& managerObject; |
| 236 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 237 | /** Abstraction of error monitoring */ |
| 238 | Error error; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 239 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 240 | /** Abstraction of SBE timeout monitoring */ |
| 241 | Error timeout; |
| 242 | |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 243 | /** SBE FFDC monitoring */ |
| 244 | FFDC ffdc; |
| 245 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 246 | /** Abstraction of OCC presence monitoring */ |
| 247 | Presence presence; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 248 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 249 | /** Error instances for watching for throttling events */ |
| 250 | Error throttleProcTemp; |
| 251 | Error throttleProcPower; |
| 252 | Error throttleMemTemp; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 253 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 254 | #ifdef POWER10 |
| 255 | /** @brief OCC PowerMode object */ |
| 256 | std::unique_ptr<powermode::PowerMode>& pmode; |
| 257 | #endif |
| 258 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 259 | /** @brief file writer to achieve bind and unbind |
| 260 | * |
| 261 | * @param[in] filename - Name of file to be written |
| 262 | * @param[in] data - Data to be written to |
| 263 | * @return - None |
| 264 | */ |
| 265 | void write(const fs::path& fileName, const std::string& data) |
| 266 | { |
| 267 | // If there is an error, move the exception all the way up |
| 268 | std::ofstream file(fileName, std::ios::out); |
| 269 | file << data; |
| 270 | file.close(); |
| 271 | return; |
| 272 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 273 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 274 | /** @brief callback for OCC error and presence monitoring |
| 275 | * |
| 276 | * @param[in] error - True if an error is reported, false otherwise |
| 277 | */ |
| 278 | void errorCallback(bool error); |
| 279 | |
| 280 | #ifdef PLDM |
| 281 | /** @brief callback for SBE timeout monitoring |
| 282 | * |
| 283 | * @param[in] error - True if an error is reported, false otherwise |
| 284 | */ |
| 285 | void timeoutCallback(bool error); |
| 286 | #endif |
| 287 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 288 | /** @brief callback for the proc temp throttle event |
| 289 | * |
| 290 | * @param[in] error - True if an error is reported, false otherwise |
| 291 | */ |
| 292 | void throttleProcTempCallback(bool error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 293 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 294 | /** @brief callback for the proc power throttle event |
| 295 | * |
| 296 | * @param[in] error - True if an error is reported, false otherwise |
| 297 | */ |
| 298 | void throttleProcPowerCallback(bool error); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 299 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 300 | /** @brief callback for the proc temp throttle event |
| 301 | * |
| 302 | * @param[in] error - True if an error is reported, false otherwise |
| 303 | */ |
| 304 | void throttleMemTempCallback(bool error); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 305 | |
| 306 | /** @brief Get the pathname for a file based on a regular expression |
| 307 | * |
| 308 | * @param[in] basePath - The path where the files will be checked |
| 309 | * @param[in] expr - Regular expression describing the target file |
| 310 | * |
| 311 | * @return path to the file or empty path if not found |
| 312 | */ |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 313 | fs::path getFilenameByRegex(fs::path basePath, |
| 314 | const std::regex& expr) const; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | } // namespace occ |
| 318 | } // namespace open_power |