blob: efa46dccfbfdd33264997e1503149ba97288a446 [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 config(name),
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053045 errorFile(fs::path(config) / "occ_error"),
Eddie James482e31f2017-09-14 13:17:17 -050046 statusObject(status),
Edward A. James636577f2017-10-06 10:53:55 -050047 error(event, errorFile, callBack),
48 presence(event,
49 fs::path(config) / "occs_present",
50 manager,
Eddie James482e31f2017-09-14 13:17:17 -050051 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 Subbanna32e84e92017-06-28 19:17:28 +053070 {
71 // Nothing to do here
72 }
73
74 /** @brief Binds device to the OCC driver */
75 inline void bind()
76 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053077 // Bind the device
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053078 return write(bindPath, config);
79 }
80
81 /** @brief Un-binds device from the OCC driver */
82 inline void unBind()
83 {
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053084 // Unbind the device
85 return write(unBindPath, config);
86 }
87
Vishwanatha Subbannab57f1512017-09-04 15:06:20 +053088 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +0530101 /** @brief Starts to monitor for errors */
102 inline void addErrorWatch()
103 {
Eddie James482e31f2017-09-14 13:17:17 -0500104 throttleProcTemp.addWatch();
105 throttleProcPower.addWatch();
106 throttleMemTemp.addWatch();
Edward A. James636577f2017-10-06 10:53:55 -0500107 error.addWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530108 }
109
110 /** @brief stops monitoring for errors */
111 inline void removeErrorWatch()
112 {
Edward A. James636577f2017-10-06 10:53:55 -0500113 // we can always safely remove watch even if we don't add it
114 presence.removeWatch();
115 error.removeWatch();
Eddie James482e31f2017-09-14 13:17:17 -0500116 error.removeWatch();
117 throttleMemTemp.removeWatch();
118 throttleProcPower.removeWatch();
119 throttleProcTemp.removeWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530120 }
121
Eddie Jamesdae2d942017-12-20 10:50:03 -0600122 /** @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 Subbanna32e84e92017-06-28 19:17:28 +0530131 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
Eddie Jamesf7d9e762018-06-28 09:06:16 -0500163 /** @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. James636577f2017-10-06 10:53:55 -0500178 /** @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