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