xref: /openbmc/openpower-occ-control/occ_device.hpp (revision f0295f52536d9e508305a0e522184157966ee2f5)
132e84e98SVishwanatha Subbanna #pragma once
232e84e98SVishwanatha Subbanna 
332e84e98SVishwanatha Subbanna #include "config.h"
40ab90ca7SLei YU 
594df8c90SGunnar Mills #include "occ_errors.hpp"
694df8c90SGunnar Mills #include "occ_events.hpp"
72f9f9bbaSEddie James #include "occ_ffdc.hpp"
894df8c90SGunnar Mills #include "occ_presence.hpp"
9ea2b22ebSSheldon Bailey #include "powermode.hpp"
1094df8c90SGunnar Mills 
11b5ca1015SGeorge Liu #include <org/open_power/OCC/Device/error.hpp>
12b5ca1015SGeorge Liu 
13bcef3b48SGeorge Liu #include <filesystem>
1494df8c90SGunnar Mills #include <fstream>
155d66a0aaSChris Cain #include <regex>
1694df8c90SGunnar Mills 
1732e84e98SVishwanatha Subbanna namespace open_power
1832e84e98SVishwanatha Subbanna {
1932e84e98SVishwanatha Subbanna namespace occ
2032e84e98SVishwanatha Subbanna {
2132e84e98SVishwanatha Subbanna 
22636577f4SEdward A. James class Manager;
23482e31ffSEddie James class Status;
24bcef3b48SGeorge Liu namespace fs = std::filesystem;
25774f9af9SEddie James using namespace sdbusplus::org::open_power::OCC::Device::Error;
2632e84e98SVishwanatha Subbanna 
2732e84e98SVishwanatha Subbanna /** @class Device
2832e84e98SVishwanatha Subbanna  *  @brief Binds and unbinds the OCC driver upon request
2932e84e98SVishwanatha Subbanna  */
3032e84e98SVishwanatha Subbanna class Device
3132e84e98SVishwanatha Subbanna {
3232e84e98SVishwanatha Subbanna   public:
3332e84e98SVishwanatha Subbanna     Device() = delete;
3432e84e98SVishwanatha Subbanna     ~Device() = default;
3532e84e98SVishwanatha Subbanna     Device(const Device&) = delete;
3632e84e98SVishwanatha Subbanna     Device& operator=(const Device&) = delete;
3732e84e98SVishwanatha Subbanna     Device(Device&&) = default;
3832e84e98SVishwanatha Subbanna     Device& operator=(Device&&) = default;
3932e84e98SVishwanatha Subbanna 
4032e84e98SVishwanatha Subbanna     /** @brief Constructs the Device object
4132e84e98SVishwanatha Subbanna      *
42ee4d83dfSVishwanatha Subbanna      *  @param[in] event    - Unique ptr reference to sd_event
43774f9af9SEddie James      *  @param[in] path     - Path to the OCC instance
44636577f4SEdward A. James      *  @param[in] manager  - OCC manager instance
452f9f9bbaSEddie James      *  @param[in] status   - Status instance
462f9f9bbaSEddie James      *  @param[in] instance - OCC instance number
4732e84e98SVishwanatha Subbanna      */
Device(EventPtr & event,const fs::path & path,Manager & manager,Status & status,std::unique_ptr<powermode::PowerMode> & powerModeRef,unsigned int instance=0)48cbad219eSEddie James     Device(EventPtr& event, const fs::path& path, Manager& manager,
49ea2b22ebSSheldon Bailey            Status& status,
50ea2b22ebSSheldon Bailey #ifdef POWER10
51ea2b22ebSSheldon Bailey            std::unique_ptr<powermode::PowerMode>& powerModeRef,
52ea2b22ebSSheldon Bailey #endif
53ea2b22ebSSheldon Bailey            unsigned int instance = 0) :
54d7542c83SPatrick Williams         devPath(path), instance(instance), statusObject(status),
55d7542c83SPatrick Williams         managerObject(manager),
56cbad219eSEddie James         error(event, path / "occ_error",
57cbad219eSEddie James               std::bind(std::mem_fn(&Device::errorCallback), this,
58cbad219eSEddie James                         std::placeholders::_1)),
59cbad219eSEddie James         timeout(event,
60cbad219eSEddie James                 path /
61cbad219eSEddie James                     fs::path("../../sbefifo" + std::to_string(instance + 1)) /
62cbad219eSEddie James                     "timeout",
63cbad219eSEddie James #ifdef PLDM
64cbad219eSEddie James                 std::bind(std::mem_fn(&Device::timeoutCallback), this,
65cbad219eSEddie James                           std::placeholders::_1)
66cbad219eSEddie James #else
67cbad219eSEddie James                 nullptr
68cbad219eSEddie James #endif
69cbad219eSEddie James                     ),
702f9f9bbaSEddie James         ffdc(event, path / "ffdc", instance),
71cbad219eSEddie James         presence(event, path / "occs_present", manager,
72cbad219eSEddie James                  std::bind(std::mem_fn(&Device::errorCallback), this,
73cbad219eSEddie James                            std::placeholders::_1)),
74482e31ffSEddie James         throttleProcTemp(
75774f9af9SEddie James             event, path / "occ_dvfs_overtemp",
7694df8c90SGunnar Mills             std::bind(std::mem_fn(&Device::throttleProcTempCallback), this,
77482e31ffSEddie James                       std::placeholders::_1)),
78482e31ffSEddie James         throttleProcPower(
79774f9af9SEddie James             event, path / "occ_dvfs_power",
8094df8c90SGunnar Mills             std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this,
81482e31ffSEddie James                       std::placeholders::_1)),
82774f9af9SEddie James         throttleMemTemp(event, path / "occ_mem_throttle",
83482e31ffSEddie James                         std::bind(std::mem_fn(&Device::throttleMemTempCallback),
8494df8c90SGunnar Mills                                   this, std::placeholders::_1))
85ea2b22ebSSheldon Bailey #ifdef POWER10
86ea2b22ebSSheldon Bailey         ,
87ea2b22ebSSheldon Bailey         pmode(powerModeRef)
88ea2b22ebSSheldon Bailey #endif
8932e84e98SVishwanatha Subbanna     {
9032e84e98SVishwanatha Subbanna         // Nothing to do here
9132e84e98SVishwanatha Subbanna     }
9232e84e98SVishwanatha Subbanna 
93aced3098SEddie James     /** @brief Sets the device active or inactive
94b57f1510SVishwanatha Subbanna      *
95aced3098SEddie James      * @param[in] active - Indicates whether or not to set the device active
96b57f1510SVishwanatha Subbanna      */
97aced3098SEddie James     void setActive(bool active);
98b57f1510SVishwanatha Subbanna 
99774f9af9SEddie James     /** @brief Starts to monitor for errors
100774f9af9SEddie James      *
101774f9af9SEddie James      *  @param[in] poll - Indicates whether or not the error file should
102774f9af9SEddie James      *                    actually be polled for changes. Disabling polling is
103774f9af9SEddie James      *                    necessary for error files that don't support the poll
104774f9af9SEddie James      *                    file operation.
105774f9af9SEddie James      */
addErrorWatch(bool poll=true)106774f9af9SEddie James     inline void addErrorWatch(bool poll = true)
107ee4d83dfSVishwanatha Subbanna     {
108*f0295f52SChris Cain #ifdef POWER10
109*f0295f52SChris Cain         throttleProcTemp.addWatch(poll);
110*f0295f52SChris Cain #else
111774f9af9SEddie James         try
112774f9af9SEddie James         {
113774f9af9SEddie James             throttleProcTemp.addWatch(poll);
114774f9af9SEddie James         }
115774f9af9SEddie James         catch (const OpenFailure& e)
116774f9af9SEddie James         {
117774f9af9SEddie James             // try the old kernel version
118774f9af9SEddie James             throttleProcTemp.setFile(devPath / "occ_dvfs_ot");
119774f9af9SEddie James             throttleProcTemp.addWatch(poll);
120774f9af9SEddie James         }
121*f0295f52SChris Cain #endif
122774f9af9SEddie James 
123ea2b22ebSSheldon Bailey #ifdef POWER10
124ea2b22ebSSheldon Bailey         if (master())
125ea2b22ebSSheldon Bailey         {
126ea2b22ebSSheldon Bailey             pmode->addIpsWatch(poll);
127ea2b22ebSSheldon Bailey         }
128ea2b22ebSSheldon Bailey #endif
129ea2b22ebSSheldon Bailey 
130774f9af9SEddie James         throttleProcPower.addWatch(poll);
131774f9af9SEddie James         throttleMemTemp.addWatch(poll);
1322f9f9bbaSEddie James 
1332f9f9bbaSEddie James         try
1342f9f9bbaSEddie James         {
1352f9f9bbaSEddie James             ffdc.addWatch(poll);
1362f9f9bbaSEddie James         }
1372f9f9bbaSEddie James         catch (const OpenFailure& e)
1382f9f9bbaSEddie James         {
1392f9f9bbaSEddie James             // nothing to do if there is no FFDC file
1402f9f9bbaSEddie James         }
1412f9f9bbaSEddie James 
142cbad219eSEddie James         try
143cbad219eSEddie James         {
144cbad219eSEddie James             timeout.addWatch(poll);
145cbad219eSEddie James         }
146cbad219eSEddie James         catch (const std::exception& e)
147cbad219eSEddie James         {
148cbad219eSEddie James             // nothing to do if there is no SBE timeout file
149cbad219eSEddie James         }
150cbad219eSEddie James 
151774f9af9SEddie James         error.addWatch(poll);
152ee4d83dfSVishwanatha Subbanna     }
153ee4d83dfSVishwanatha Subbanna 
154ee4d83dfSVishwanatha Subbanna     /** @brief stops monitoring for errors */
removeErrorWatch()155ee4d83dfSVishwanatha Subbanna     inline void removeErrorWatch()
156ee4d83dfSVishwanatha Subbanna     {
157636577f4SEdward A. James         // we can always safely remove watch even if we don't add it
158636577f4SEdward A. James         presence.removeWatch();
1592f9f9bbaSEddie James         ffdc.removeWatch();
160636577f4SEdward A. James         error.removeWatch();
161cbad219eSEddie James         timeout.removeWatch();
162482e31ffSEddie James         throttleMemTemp.removeWatch();
163482e31ffSEddie James         throttleProcPower.removeWatch();
164482e31ffSEddie James         throttleProcTemp.removeWatch();
165ea2b22ebSSheldon Bailey #ifdef POWER10
166ea2b22ebSSheldon Bailey         if (master())
167ea2b22ebSSheldon Bailey         {
168ea2b22ebSSheldon Bailey             pmode->removeIpsWatch();
169ea2b22ebSSheldon Bailey         }
170ea2b22ebSSheldon Bailey #endif
171ee4d83dfSVishwanatha Subbanna     }
172ee4d83dfSVishwanatha Subbanna 
173dae2d940SEddie James     /** @brief Starts to watch how many OCCs are present on the master */
addPresenceWatchMaster()174dae2d940SEddie James     inline void addPresenceWatchMaster()
175dae2d940SEddie James     {
176dae2d940SEddie James         if (master())
177dae2d940SEddie James         {
178dae2d940SEddie James             presence.addWatch();
179dae2d940SEddie James         }
180dae2d940SEddie James     }
181dae2d940SEddie James 
182774f9af9SEddie James     /** @brief helper function to get the last part of the path
183774f9af9SEddie James      *
184774f9af9SEddie James      * @param[in] path - Path to parse
185774f9af9SEddie James      * @return         - Last directory name in the path
186774f9af9SEddie James      */
187774f9af9SEddie James     static std::string getPathBack(const fs::path& path);
188774f9af9SEddie James 
189aced3098SEddie James     /** @brief Returns true if the device is active */
190aced3098SEddie James     bool active() const;
191aced3098SEddie James 
19278e86012SChris Cain     /** @brief Returns true if device represents the master OCC */
19378e86012SChris Cain     bool master() const;
19478e86012SChris Cain 
19532e84e98SVishwanatha Subbanna   private:
196774f9af9SEddie James     /** @brief This directory contains the error files */
197774f9af9SEddie James     const fs::path devPath;
198ee4d83dfSVishwanatha Subbanna 
199cbad219eSEddie James     /** @brief OCC instance ID */
200cbad219eSEddie James     const unsigned int instance;
201cbad219eSEddie James 
202482e31ffSEddie James     /**  Store the associated Status instance */
203482e31ffSEddie James     Status& statusObject;
204482e31ffSEddie James 
205cbad219eSEddie James     /** Store the parent Manager instance */
206cbad219eSEddie James     Manager& managerObject;
207cbad219eSEddie James 
208ee4d83dfSVishwanatha Subbanna     /** Abstraction of error monitoring */
209ee4d83dfSVishwanatha Subbanna     Error error;
210ee4d83dfSVishwanatha Subbanna 
211cbad219eSEddie James     /** Abstraction of SBE timeout monitoring */
212cbad219eSEddie James     Error timeout;
213cbad219eSEddie James 
2142f9f9bbaSEddie James     /** SBE FFDC monitoring */
2152f9f9bbaSEddie James     FFDC ffdc;
2162f9f9bbaSEddie James 
217636577f4SEdward A. James     /** Abstraction of OCC presence monitoring */
218636577f4SEdward A. James     Presence presence;
219636577f4SEdward A. James 
220482e31ffSEddie James     /** Error instances for watching for throttling events */
221482e31ffSEddie James     Error throttleProcTemp;
222482e31ffSEddie James     Error throttleProcPower;
223482e31ffSEddie James     Error throttleMemTemp;
224482e31ffSEddie James 
225ea2b22ebSSheldon Bailey #ifdef POWER10
226ea2b22ebSSheldon Bailey     /** @brief OCC PowerMode object */
227ea2b22ebSSheldon Bailey     std::unique_ptr<powermode::PowerMode>& pmode;
228ea2b22ebSSheldon Bailey #endif
229ea2b22ebSSheldon Bailey 
230aced3098SEddie James     /** @brief file reader to read a binary string ("1" or "0")
231aced3098SEddie James      *
232aced3098SEddie James      * @param[in] fileName - Name of file to be read
233aced3098SEddie James      * @return             - The value returned by reading the file
234aced3098SEddie James      */
235aced3098SEddie James     bool readBinary(const std::string& fileName) const;
236aced3098SEddie James 
237f7d9e76dSEddie James     /** @brief file writer to achieve bind and unbind
238f7d9e76dSEddie James      *
239f7d9e76dSEddie James      *  @param[in] filename - Name of file to be written
240f7d9e76dSEddie James      *  @param[in] data     - Data to be written to
241f7d9e76dSEddie James      *  @return             - None
242f7d9e76dSEddie James      */
write(const fs::path & fileName,const std::string & data)243f7d9e76dSEddie James     void write(const fs::path& fileName, const std::string& data)
244f7d9e76dSEddie James     {
245f7d9e76dSEddie James         // If there is an error, move the exception all the way up
246f7d9e76dSEddie James         std::ofstream file(fileName, std::ios::out);
247f7d9e76dSEddie James         file << data;
248f7d9e76dSEddie James         file.close();
249f7d9e76dSEddie James         return;
250f7d9e76dSEddie James     }
251f7d9e76dSEddie James 
2529789e71fSEddie James     /** @brief callback for OCC error monitoring
253cbad219eSEddie James      *
2549789e71fSEddie James      * @param[in] error - Errno stored in the error file, 0 if no error
255cbad219eSEddie James      */
2569789e71fSEddie James     void errorCallback(int error);
2579789e71fSEddie James 
2589789e71fSEddie James     /** @brief callback for OCC presence monitoring
2599789e71fSEddie James      *
2609789e71fSEddie James      * @param[in] occsPresent - The number of OCCs indicated in the poll
2619789e71fSEddie James      * response
2629789e71fSEddie James      */
2639789e71fSEddie James     void presenceCallback(int occsPresent);
264cbad219eSEddie James 
265cbad219eSEddie James #ifdef PLDM
266cbad219eSEddie James     /** @brief callback for SBE timeout monitoring
267cbad219eSEddie James      *
268cbad219eSEddie James      * @param[in] error - True if an error is reported, false otherwise
269cbad219eSEddie James      */
2709789e71fSEddie James     void timeoutCallback(int error);
271cbad219eSEddie James #endif
272cbad219eSEddie James 
273482e31ffSEddie James     /** @brief callback for the proc temp throttle event
274482e31ffSEddie James      *
275482e31ffSEddie James      *  @param[in] error - True if an error is reported, false otherwise
276482e31ffSEddie James      */
2779789e71fSEddie James     void throttleProcTempCallback(int error);
278482e31ffSEddie James 
279482e31ffSEddie James     /** @brief callback for the proc power throttle event
280482e31ffSEddie James      *
281482e31ffSEddie James      *  @param[in] error - True if an error is reported, false otherwise
282482e31ffSEddie James      */
2839789e71fSEddie James     void throttleProcPowerCallback(int error);
284482e31ffSEddie James 
285482e31ffSEddie James     /** @brief callback for the proc temp throttle event
286482e31ffSEddie James      *
287482e31ffSEddie James      *  @param[in] error - True if an error is reported, false otherwise
288482e31ffSEddie James      */
2899789e71fSEddie James     void throttleMemTempCallback(int error);
2905d66a0aaSChris Cain 
2915d66a0aaSChris Cain     /** @brief Get the pathname for a file based on a regular expression
2925d66a0aaSChris Cain      *
2935d66a0aaSChris Cain      *  @param[in] basePath - The path where the files will be checked
2945d66a0aaSChris Cain      *  @param[in] expr - Regular expression describing the target file
2955d66a0aaSChris Cain      *
2965d66a0aaSChris Cain      *  @return path to the file or empty path if not found
2975d66a0aaSChris Cain      */
298e2d0a43cSChris Cain     fs::path getFilenameByRegex(fs::path basePath,
299e2d0a43cSChris Cain                                 const std::regex& expr) const;
30032e84e98SVishwanatha Subbanna };
30132e84e98SVishwanatha Subbanna 
30232e84e98SVishwanatha Subbanna } // namespace occ
30332e84e98SVishwanatha Subbanna } // namespace open_power
304