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