blob: ef2fb06bfe81548205f3651c85c8c61a46d54cbb [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#pragma once
2
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05303#include "config.h"
Lei YU0ab90ca2017-07-13 17:02:23 +08004
Gunnar Mills94df8c92018-09-14 14:50:03 -05005#include "occ_errors.hpp"
6#include "occ_events.hpp"
Eddie James2f9f9bb2021-09-20 14:26:31 -05007#include "occ_ffdc.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05008#include "occ_presence.hpp"
9
George Liub5ca1012021-09-10 12:53:11 +080010#include <org/open_power/OCC/Device/error.hpp>
11
George Liubcef3b42021-09-10 12:39:02 +080012#include <filesystem>
Gunnar Mills94df8c92018-09-14 14:50:03 -050013#include <fstream>
14
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053015namespace open_power
16{
17namespace occ
18{
19
Edward A. James636577f2017-10-06 10:53:55 -050020class Manager;
Eddie James482e31f2017-09-14 13:17:17 -050021class Status;
George Liubcef3b42021-09-10 12:39:02 +080022namespace fs = std::filesystem;
Eddie James774f9af2019-03-19 20:58:53 +000023using namespace sdbusplus::org::open_power::OCC::Device::Error;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053024
25/** @class Device
26 * @brief Binds and unbinds the OCC driver upon request
27 */
28class Device
29{
Gunnar Mills94df8c92018-09-14 14:50:03 -050030 public:
31 Device() = delete;
32 ~Device() = default;
33 Device(const Device&) = delete;
34 Device& operator=(const Device&) = delete;
35 Device(Device&&) = default;
36 Device& operator=(Device&&) = default;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053037
Gunnar Mills94df8c92018-09-14 14:50:03 -050038 /** @brief Constructs the Device object
39 *
40 * @param[in] event - Unique ptr reference to sd_event
Eddie James774f9af2019-03-19 20:58:53 +000041 * @param[in] path - Path to the OCC instance
Gunnar Mills94df8c92018-09-14 14:50:03 -050042 * @param[in] manager - OCC manager instance
Eddie James2f9f9bb2021-09-20 14:26:31 -050043 * @param[in] status - Status instance
44 * @param[in] instance - OCC instance number
Gunnar Mills94df8c92018-09-14 14:50:03 -050045 * @param[in] callback - Optional callback on errors
46 */
Eddie James774f9af2019-03-19 20:58:53 +000047 Device(EventPtr& event, const fs::path& path, const Manager& manager,
Eddie James2f9f9bb2021-09-20 14:26:31 -050048 Status& status, unsigned int instance = 0,
49 std::function<void(bool)> callBack = nullptr) :
Eddie James774f9af2019-03-19 20:58:53 +000050 config(getPathBack(path)),
51 devPath(path), statusObject(status),
52 error(event, path / "occ_error", callBack),
Eddie James2f9f9bb2021-09-20 14:26:31 -050053 ffdc(event, path / "ffdc", instance),
Eddie James774f9af2019-03-19 20:58:53 +000054 presence(event, path / "occs_present", manager, callBack),
Gunnar Mills94df8c92018-09-14 14:50:03 -050055 throttleProcTemp(
Eddie James774f9af2019-03-19 20:58:53 +000056 event, path / "occ_dvfs_overtemp",
Gunnar Mills94df8c92018-09-14 14:50:03 -050057 std::bind(std::mem_fn(&Device::throttleProcTempCallback), this,
58 std::placeholders::_1)),
59 throttleProcPower(
Eddie James774f9af2019-03-19 20:58:53 +000060 event, path / "occ_dvfs_power",
Gunnar Mills94df8c92018-09-14 14:50:03 -050061 std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this,
62 std::placeholders::_1)),
Eddie James774f9af2019-03-19 20:58:53 +000063 throttleMemTemp(event, path / "occ_mem_throttle",
Gunnar Mills94df8c92018-09-14 14:50:03 -050064 std::bind(std::mem_fn(&Device::throttleMemTempCallback),
65 this, std::placeholders::_1))
66 {
67 // Nothing to do here
68 }
69
70 /** @brief Binds device to the OCC driver */
71 inline void bind()
72 {
73 // Bind the device
74 return write(bindPath, config);
75 }
76
77 /** @brief Un-binds device from the OCC driver */
78 inline void unBind()
79 {
80 // Unbind the device
81 return write(unBindPath, config);
82 }
83
84 /** @brief Returns if device is already bound.
85 *
86 * On device bind, a soft link by the name $config
87 * gets created in OCC_HWMON_PATH and gets removed
88 * on unbind
89 *
90 * @return true if bound, else false
91 */
92 inline bool bound() const
93 {
94 return fs::exists(OCC_HWMON_PATH + config);
95 }
96
Eddie James774f9af2019-03-19 20:58:53 +000097 /** @brief Starts to monitor for errors
98 *
99 * @param[in] poll - Indicates whether or not the error file should
100 * actually be polled for changes. Disabling polling is
101 * necessary for error files that don't support the poll
102 * file operation.
103 */
104 inline void addErrorWatch(bool poll = true)
Gunnar Mills94df8c92018-09-14 14:50:03 -0500105 {
Eddie James774f9af2019-03-19 20:58:53 +0000106 try
107 {
108 throttleProcTemp.addWatch(poll);
109 }
110 catch (const OpenFailure& e)
111 {
112 // try the old kernel version
113 throttleProcTemp.setFile(devPath / "occ_dvfs_ot");
114 throttleProcTemp.addWatch(poll);
115 }
116
117 throttleProcPower.addWatch(poll);
118 throttleMemTemp.addWatch(poll);
Eddie James2f9f9bb2021-09-20 14:26:31 -0500119
120 try
121 {
122 ffdc.addWatch(poll);
123 }
124 catch (const OpenFailure& e)
125 {
126 // nothing to do if there is no FFDC file
127 }
128
Eddie James774f9af2019-03-19 20:58:53 +0000129 error.addWatch(poll);
Gunnar Mills94df8c92018-09-14 14:50:03 -0500130 }
131
132 /** @brief stops monitoring for errors */
133 inline void removeErrorWatch()
134 {
135 // we can always safely remove watch even if we don't add it
136 presence.removeWatch();
Eddie James2f9f9bb2021-09-20 14:26:31 -0500137 ffdc.removeWatch();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500138 error.removeWatch();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500139 throttleMemTemp.removeWatch();
140 throttleProcPower.removeWatch();
141 throttleProcTemp.removeWatch();
142 }
143
144 /** @brief Starts to watch how many OCCs are present on the master */
145 inline void addPresenceWatchMaster()
146 {
147 if (master())
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530148 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500149 presence.addWatch();
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530150 }
Gunnar Mills94df8c92018-09-14 14:50:03 -0500151 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530152
Eddie James774f9af2019-03-19 20:58:53 +0000153 /** @brief helper function to get the last part of the path
154 *
155 * @param[in] path - Path to parse
156 * @return - Last directory name in the path
157 */
158 static std::string getPathBack(const fs::path& path);
159
Chris Cain78e86012021-03-04 16:15:31 -0600160 /** @brief Returns true if device represents the master OCC */
161 bool master() const;
162
Gunnar Mills94df8c92018-09-14 14:50:03 -0500163 private:
164 /** @brief Config value to be used to do bind and unbind */
165 const std::string config;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530166
Eddie James774f9af2019-03-19 20:58:53 +0000167 /** @brief This directory contains the error files */
168 const fs::path devPath;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530169
Gunnar Mills94df8c92018-09-14 14:50:03 -0500170 /** @brief To bind the device to the OCC driver, do:
171 *
172 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/bind
173 */
174 static fs::path bindPath;
Vishwanatha Subbannab57f1512017-09-04 15:06:20 +0530175
Gunnar Mills94df8c92018-09-14 14:50:03 -0500176 /** @brief To un-bind the device from the OCC driver, do:
177 * Write occ<#>-dev0 to: /sys/bus/platform/drivers/occ-hwmon/unbind
178 */
179 static fs::path unBindPath;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530180
Gunnar Mills94df8c92018-09-14 14:50:03 -0500181 /** Store the associated Status instance */
182 Status& statusObject;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530183
Gunnar Mills94df8c92018-09-14 14:50:03 -0500184 /** Abstraction of error monitoring */
185 Error error;
Eddie Jamesdae2d942017-12-20 10:50:03 -0600186
Eddie James2f9f9bb2021-09-20 14:26:31 -0500187 /** SBE FFDC monitoring */
188 FFDC ffdc;
189
Gunnar Mills94df8c92018-09-14 14:50:03 -0500190 /** Abstraction of OCC presence monitoring */
191 Presence presence;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530192
Gunnar Mills94df8c92018-09-14 14:50:03 -0500193 /** Error instances for watching for throttling events */
194 Error throttleProcTemp;
195 Error throttleProcPower;
196 Error throttleMemTemp;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530197
Gunnar Mills94df8c92018-09-14 14:50:03 -0500198 /** @brief file writer to achieve bind and unbind
199 *
200 * @param[in] filename - Name of file to be written
201 * @param[in] data - Data to be written to
202 * @return - None
203 */
204 void write(const fs::path& fileName, const std::string& data)
205 {
206 // If there is an error, move the exception all the way up
207 std::ofstream file(fileName, std::ios::out);
208 file << data;
209 file.close();
210 return;
211 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530212
Gunnar Mills94df8c92018-09-14 14:50:03 -0500213 /** @brief callback for the proc temp throttle event
214 *
215 * @param[in] error - True if an error is reported, false otherwise
216 */
217 void throttleProcTempCallback(bool error);
Eddie James482e31f2017-09-14 13:17:17 -0500218
Gunnar Mills94df8c92018-09-14 14:50:03 -0500219 /** @brief callback for the proc power throttle event
220 *
221 * @param[in] error - True if an error is reported, false otherwise
222 */
223 void throttleProcPowerCallback(bool error);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530224
Gunnar Mills94df8c92018-09-14 14:50:03 -0500225 /** @brief callback for the proc temp throttle event
226 *
227 * @param[in] error - True if an error is reported, false otherwise
228 */
229 void throttleMemTempCallback(bool error);
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530230};
231
232} // namespace occ
233} // namespace open_power