blob: fd30641c317a21ae0d09779907e29e5f7b2dec5e [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#pragma once
2
3#include <fstream>
4#include <experimental/filesystem>
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +05305#include "occ_events.hpp"
6#include "occ_errors.hpp"
Edward A. James636577f2017-10-06 10:53:55 -05007#include "occ_presence.hpp"
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05308#include "config.h"
Lei YU0ab90ca2017-07-13 17:02:23 +08009
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053010namespace open_power
11{
12namespace occ
13{
14
Edward A. James636577f2017-10-06 10:53:55 -050015class Manager;
Eddie James482e31f2017-09-14 13:17:17 -050016class Status;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053017namespace fs = std::experimental::filesystem;
18
19/** @class Device
20 * @brief Binds and unbinds the OCC driver upon request
21 */
22class 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 Subbannaee4d83d2017-06-29 18:35:00 +053034 * @param[in] event - Unique ptr reference to sd_event
35 * @param[in] name - OCC instance name
Edward A. James636577f2017-10-06 10:53:55 -050036 * @param[in] manager - OCC manager instance
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053037 * @param[in] callback - Optional callback on errors
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053038 */
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053039 Device(EventPtr& event,
40 const std::string& name,
Edward A. James636577f2017-10-06 10:53:55 -050041 const Manager& manager,
Eddie James482e31f2017-09-14 13:17:17 -050042 Status& status,
43 std::function<void(bool)> callBack = nullptr) :
Lei YU0ab90ca2017-07-13 17:02:23 +080044#ifdef I2C_OCC
45 config(name),
46#else
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053047 config(name + '-' + "dev0"),
Lei YU0ab90ca2017-07-13 17:02:23 +080048#endif
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053049 errorFile(fs::path(config) / "occ_error"),
Eddie James482e31f2017-09-14 13:17:17 -050050 statusObject(status),
Edward A. James636577f2017-10-06 10:53:55 -050051 error(event, errorFile, callBack),
52 presence(event,
53 fs::path(config) / "occs_present",
54 manager,
Eddie James482e31f2017-09-14 13:17:17 -050055 callBack),
56 throttleProcTemp(
57 event,
58 fs::path(config) / "occ_dvfs_ot",
59 std::bind(std::mem_fn(&Device::throttleProcTempCallback),
60 this,
61 std::placeholders::_1)),
62 throttleProcPower(
63 event,
64 fs::path(config) / "occ_dvfs_power",
65 std::bind(std::mem_fn(&Device::throttleProcPowerCallback),
66 this,
67 std::placeholders::_1)),
68 throttleMemTemp(
69 event,
70 fs::path(config) / "occ_mem_throttle",
71 std::bind(std::mem_fn(&Device::throttleMemTempCallback),
72 this,
73 std::placeholders::_1))
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053074 {
75 // Nothing to do here
76 }
77
78 /** @brief Binds device to the OCC driver */
79 inline void bind()
80 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053081 // Bind the device
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053082 return write(bindPath, config);
83 }
84
85 /** @brief Un-binds device from the OCC driver */
86 inline void unBind()
87 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053088 // Unbind the device
89 return write(unBindPath, config);
90 }
91
Vishwanatha Subbannab57f1512017-09-04 15:06:20 +053092 /** @brief Returns if device is already bound.
93 *
94 * On device bind, a soft link by the name $config
95 * gets created in OCC_HWMON_PATH and gets removed
96 * on unbind
97 *
98 * @return true if bound, else false
99 */
100 inline bool bound() const
101 {
102 return fs::exists(OCC_HWMON_PATH + config);
103 }
104
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530105 /** @brief Starts to monitor for errors */
106 inline void addErrorWatch()
107 {
Edward A. James636577f2017-10-06 10:53:55 -0500108 if (master())
109 {
110 presence.addWatch();
111 }
112
Eddie James482e31f2017-09-14 13:17:17 -0500113 throttleProcTemp.addWatch();
114 throttleProcPower.addWatch();
115 throttleMemTemp.addWatch();
Edward A. James636577f2017-10-06 10:53:55 -0500116 error.addWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530117 }
118
119 /** @brief stops monitoring for errors */
120 inline void removeErrorWatch()
121 {
Edward A. James636577f2017-10-06 10:53:55 -0500122 // we can always safely remove watch even if we don't add it
123 presence.removeWatch();
124 error.removeWatch();
Eddie James482e31f2017-09-14 13:17:17 -0500125 error.removeWatch();
126 throttleMemTemp.removeWatch();
127 throttleProcPower.removeWatch();
128 throttleProcTemp.removeWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530129 }
130
131 private:
132 /** @brief Config value to be used to do bind and unbind */
133 const std::string config;
134
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530135 /** @brief This file contains 0 for success, non-zero for errors */
136 const fs::path errorFile;
137
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530138 /** @brief To bind the device to the OCC driver, do:
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530139 *
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530140 * 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 James482e31f2017-09-14 13:17:17 -0500149 /** Store the associated Status instance */
150 Status& statusObject;
151
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530152 /** Abstraction of error monitoring */
153 Error error;
154
Edward A. James636577f2017-10-06 10:53:55 -0500155 /** Abstraction of OCC presence monitoring */
156 Presence presence;
157
Eddie James482e31f2017-09-14 13:17:17 -0500158 /** Error instances for watching for throttling events */
159 Error throttleProcTemp;
160 Error throttleProcPower;
161 Error throttleMemTemp;
162
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530163 /** @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 }
Edward A. James636577f2017-10-06 10:53:55 -0500177
178 /** @brief Returns if device represents the master OCC */
179 bool master() const;
Eddie James482e31f2017-09-14 13:17:17 -0500180
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 Subbanna32e84e92017-06-28 19:17:28 +0530198};
199
200} // namespace occ
201} // namespace open_power