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