blob: 85abde6e31d6ea8c403b66c0faf836441c4fbdee [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#pragma once
2
3#include <fstream>
4#include <experimental/filesystem>
Eddie Jamesb5508d72018-05-02 15:57:23 -05005#include "occ_bus.hpp"
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +05306#include "occ_events.hpp"
7#include "occ_errors.hpp"
Edward A. James636577f2017-10-06 10:53:55 -05008#include "occ_presence.hpp"
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05309#include "config.h"
Lei YU0ab90ca2017-07-13 17:02:23 +080010
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053011namespace open_power
12{
13namespace occ
14{
15
Edward A. James636577f2017-10-06 10:53:55 -050016class Manager;
Eddie James482e31f2017-09-14 13:17:17 -050017class Status;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053018namespace fs = std::experimental::filesystem;
19
20/** @class Device
21 * @brief Binds and unbinds the OCC driver upon request
22 */
23class 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 Subbannaee4d83d2017-06-29 18:35:00 +053035 * @param[in] event - Unique ptr reference to sd_event
36 * @param[in] name - OCC instance name
Edward A. James636577f2017-10-06 10:53:55 -050037 * @param[in] manager - OCC manager instance
Eddie Jamesb5508d72018-05-02 15:57:23 -050038 * @param[in] status - OCC status instance
39 * @param[in] instance - OCC device index
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053040 * @param[in] callback - Optional callback on errors
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053041 */
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053042 Device(EventPtr& event,
43 const std::string& name,
Edward A. James636577f2017-10-06 10:53:55 -050044 const Manager& manager,
Eddie James482e31f2017-09-14 13:17:17 -050045 Status& status,
Eddie Jamesb5508d72018-05-02 15:57:23 -050046 int instance,
Eddie James482e31f2017-09-14 13:17:17 -050047 std::function<void(bool)> callBack = nullptr) :
Lei YU0ab90ca2017-07-13 17:02:23 +080048 config(name),
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053049 errorFile(fs::path(config) / "occ_error"),
Eddie James482e31f2017-09-14 13:17:17 -050050 statusObject(status),
Eddie Jamesb5508d72018-05-02 15:57:23 -050051 busObject(instance),
Edward A. James636577f2017-10-06 10:53:55 -050052 error(event, errorFile, callBack),
53 presence(event,
54 fs::path(config) / "occs_present",
55 manager,
Eddie James482e31f2017-09-14 13:17:17 -050056 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 Subbanna32e84e92017-06-28 19:17:28 +053075 {
76 // Nothing to do here
77 }
78
79 /** @brief Binds device to the OCC driver */
80 inline void bind()
81 {
Eddie Jamesb5508d72018-05-02 15:57:23 -050082 // Reset this OCC's bus driver
83 busObject.reset();
84
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053085 // Bind the device
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053086 return write(bindPath, config);
87 }
88
89 /** @brief Un-binds device from the OCC driver */
90 inline void unBind()
91 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053092 // Unbind the device
93 return write(unBindPath, config);
94 }
95
Vishwanatha Subbannab57f1512017-09-04 15:06:20 +053096 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +0530109 /** @brief Starts to monitor for errors */
110 inline void addErrorWatch()
111 {
Eddie James482e31f2017-09-14 13:17:17 -0500112 throttleProcTemp.addWatch();
113 throttleProcPower.addWatch();
114 throttleMemTemp.addWatch();
Edward A. James636577f2017-10-06 10:53:55 -0500115 error.addWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530116 }
117
118 /** @brief stops monitoring for errors */
119 inline void removeErrorWatch()
120 {
Edward A. James636577f2017-10-06 10:53:55 -0500121 // we can always safely remove watch even if we don't add it
122 presence.removeWatch();
123 error.removeWatch();
Eddie James482e31f2017-09-14 13:17:17 -0500124 error.removeWatch();
125 throttleMemTemp.removeWatch();
126 throttleProcPower.removeWatch();
127 throttleProcTemp.removeWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530128 }
129
Eddie Jamesdae2d942017-12-20 10:50:03 -0600130 /** @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 Jamesb5508d72018-05-02 15:57:23 -0500139 /** @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 Subbanna32e84e92017-06-28 19:17:28 +0530154 private:
155 /** @brief Config value to be used to do bind and unbind */
156 const std::string config;
157
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530158 /** @brief This file contains 0 for success, non-zero for errors */
159 const fs::path errorFile;
160
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530161 /** @brief To bind the device to the OCC driver, do:
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530162 *
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530163 * 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 James482e31f2017-09-14 13:17:17 -0500172 /** Store the associated Status instance */
173 Status& statusObject;
174
Eddie Jamesb5508d72018-05-02 15:57:23 -0500175 /** Store the associated Bus instance */
176 const Bus busObject;
177
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530178 /** Abstraction of error monitoring */
179 Error error;
180
Edward A. James636577f2017-10-06 10:53:55 -0500181 /** Abstraction of OCC presence monitoring */
182 Presence presence;
183
Eddie James482e31f2017-09-14 13:17:17 -0500184 /** Error instances for watching for throttling events */
185 Error throttleProcTemp;
186 Error throttleProcPower;
187 Error throttleMemTemp;
188
Edward A. James636577f2017-10-06 10:53:55 -0500189 /** @brief Returns if device represents the master OCC */
190 bool master() const;
Eddie James482e31f2017-09-14 13:17:17 -0500191
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 Subbanna32e84e92017-06-28 19:17:28 +0530209};
210
211} // namespace occ
212} // namespace open_power