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