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