1 #pragma once
2 
3 #include "config.h"
4 
5 #ifdef POWER10
6 #include "occ_command.hpp"
7 
8 #include <cereal/archives/json.hpp>
9 #include <cereal/cereal.hpp>
10 #include <cereal/types/string.hpp>
11 #include <cereal/types/tuple.hpp>
12 #include <cereal/types/vector.hpp>
13 #include <sdbusplus/bus.hpp>
14 #include <sdbusplus/bus/match.hpp>
15 #include <xyz/openbmc_project/Control/Power/IdlePowerSaver/server.hpp>
16 #include <xyz/openbmc_project/Control/Power/Mode/server.hpp>
17 
18 #include <filesystem>
19 
20 namespace open_power
21 {
22 namespace occ
23 {
24 
25 class Manager;
26 
27 namespace powermode
28 {
29 namespace Base = sdbusplus::xyz::openbmc_project::Control::Power::server;
30 using ModeInterface = sdbusplus::server::object_t<Base::Mode>;
31 using IpsInterface = sdbusplus::server::object_t<Base::IdlePowerSaver>;
32 using namespace std::literals::string_literals;
33 
34 constexpr auto PMODE_PATH = "/xyz/openbmc_project/control/host0/power_mode";
35 constexpr auto PMODE_INTERFACE = "xyz.openbmc_project.Control.Power.Mode";
36 constexpr auto POWER_MODE_PROP = "PowerMode";
37 constexpr auto POWER_SAFE_MODE_PROP = "SafeMode";
38 
39 constexpr auto PIPS_PATH = "/xyz/openbmc_project/control/host0/power_ips";
40 constexpr auto PIPS_INTERFACE =
41     "xyz.openbmc_project.Control.Power.IdlePowerSaver";
42 constexpr auto IPS_ACTIVE_PROP = "Active";
43 constexpr auto IPS_ENABLED_PROP = "Enabled";
44 constexpr auto IPS_ENTER_UTIL = "EnterUtilizationPercent";
45 constexpr auto IPS_ENTER_TIME = "EnterDwellTime";
46 constexpr auto IPS_EXIT_UTIL = "ExitUtilizationPercent";
47 constexpr auto IPS_EXIT_TIME = "ExitDwellTime";
48 
49 const auto PMODE_DEFAULT_INTERFACE =
50     "xyz.openbmc_project.Configuration.PowerModeProperties"s;
51 
52 /** @brief Query the current Hypervisor target
53  * @return true if the current Hypervisor target is PowerVM
54  */
55 bool isPowerVM();
56 
57 /** @brief Convert power mode string to OCC SysPwrMode value
58  *
59  * @param[in] i_modeString - power mode string
60  *
61  * @return  SysPwrMode or SysPwrMode::NO_CHANGE if not found
62  */
63 SysPwrMode convertStringToMode(const std::string& i_modeString);
64 
65 struct PowerModeData
66 {
67     bool modeInitialized = false;
68     SysPwrMode mode = SysPwrMode::NO_CHANGE;
69     uint16_t oemModeData = 0x0000;
70     bool ipsInitialized = false;
71     bool ipsEnabled = true;
72     uint8_t ipsEnterUtil = 0;
73     uint16_t ipsEnterTime = 0;
74     uint8_t ipsExitUtil = 0;
75     uint16_t ipsExitTime = 0;
76     bool modeLocked = false;
77 
78     /** @brief Function specifying data to archive for cereal.
79      */
80     template <class Archive>
serializeopen_power::occ::powermode::PowerModeData81     void serialize(Archive& archive)
82     {
83         archive(modeInitialized, mode, oemModeData, ipsInitialized, ipsEnabled,
84                 ipsEnterUtil, ipsEnterTime, ipsExitUtil, ipsExitTime,
85                 modeLocked);
86     }
87 };
88 
89 /** @class OccPersistData
90  *  @brief Provides persistent container to store data for OCC
91  *
92  * Data is stored via cereal
93  */
94 class OccPersistData
95 {
96   public:
97     ~OccPersistData() = default;
98     OccPersistData(const OccPersistData&) = default;
99     OccPersistData& operator=(const OccPersistData&) = default;
100     OccPersistData(OccPersistData&&) = default;
101     OccPersistData& operator=(OccPersistData&&) = default;
102 
103     /** @brief Loads any saved power mode data */
OccPersistData()104     OccPersistData()
105     {
106         load();
107     }
108 
109     /** @brief Save Power Mode data to persistent file
110      *
111      *  @param[in] newMode - desired System Power Mode
112      *  @param[in] oemModeData - data required by some OEM Power Modes
113      */
updateMode(const SysPwrMode newMode,const uint16_t oemModeData)114     void updateMode(const SysPwrMode newMode, const uint16_t oemModeData)
115     {
116         modeData.mode = newMode;
117         modeData.oemModeData = oemModeData;
118         modeData.modeInitialized = true;
119         save();
120     }
121 
122     /** @brief Save Power Mode Lock value to persistent file
123      *
124      *  @param[in] modeLock - desired System Power Mode Lock
125      */
updateModeLock(const bool modeLock)126     void updateModeLock(const bool modeLock)
127     {
128         modeData.modeLocked = modeLock;
129         save();
130     }
131     /** @brief Write Idle Power Saver parameters to persistent file
132      *
133      *  @param[in] enabled - Idle Power Save status (true = enabled)
134      *  @param[in] enterUtil - IPS Enter Utilization (%)
135      *  @param[in] enterTime - IPS Enter Time (seconds)
136      *  @param[in] exitUtil - IPS Exit Utilization (%)
137      *  @param[in] exitTime - IPS Exit Time (seconds)
138      */
updateIPS(const bool enabled,const uint8_t enterUtil,const uint16_t enterTime,const uint8_t exitUtil,const uint16_t exitTime)139     void updateIPS(const bool enabled, const uint8_t enterUtil,
140                    const uint16_t enterTime, const uint8_t exitUtil,
141                    const uint16_t exitTime)
142     {
143         modeData.ipsEnabled = enabled;
144         modeData.ipsEnterUtil = enterUtil;
145         modeData.ipsEnterTime = enterTime;
146         modeData.ipsExitUtil = exitUtil;
147         modeData.ipsExitTime = exitTime;
148         modeData.ipsInitialized = true;
149         save();
150     }
151 
152     /** @brief Return the Power Mode and mode data
153      *
154      *  @param[out] mode - current system power mode
155      *  @param[out] oemModeData - frequency data for some OEM mode
156      *
157      *  @returns true if mode was available
158      */
getMode(SysPwrMode & mode,uint16_t & oemModeData) const159     bool getMode(SysPwrMode& mode, uint16_t& oemModeData) const
160     {
161         if (modeData.modeInitialized)
162         {
163             mode = modeData.mode;
164             oemModeData = modeData.oemModeData;
165         }
166         return modeData.modeInitialized;
167     }
168 
169     /** @brief Get the Idle Power Saver properties from DBus
170      *
171      *  @param[out] enabled - Idle Power Save status (true = enabled)
172      *  @param[out] enterUtil - IPS Enter Utilization (%)
173      *  @param[out] enterTime - IPS Enter Time (seconds)
174      *  @param[out] exitUtil - IPS Exit Utilization (%)
175      *  @param[out] exitTime - IPS Exit Time (seconds)
176      *
177      * @return true if parameters were read successfully
178      */
getIPS(bool & enabled,uint8_t & enterUtil,uint16_t & enterTime,uint8_t & exitUtil,uint16_t & exitTime)179     bool getIPS(bool& enabled, uint8_t& enterUtil, uint16_t& enterTime,
180                 uint8_t& exitUtil, uint16_t& exitTime)
181     {
182         if (!modeData.ipsInitialized)
183         {
184             return false;
185         }
186 
187         enabled = modeData.ipsEnabled;
188         enterUtil = modeData.ipsEnterUtil;
189         enterTime = modeData.ipsEnterTime;
190         exitUtil = modeData.ipsExitUtil;
191         exitTime = modeData.ipsExitTime;
192         return true;
193     }
194 
195     /** @brief Return persisted mode lock */
getModeLock()196     bool getModeLock()
197     {
198         return modeData.modeLocked;
199     }
200 
201     /** @brief Return true if the power mode is available */
modeAvailable()202     bool modeAvailable()
203     {
204         return (modeData.modeInitialized);
205     }
206 
207     /** @brief Return true if the IPS data is available */
ipsAvailable()208     bool ipsAvailable()
209     {
210         return (modeData.ipsInitialized);
211     }
212 
213     /** @brief Saves the Power Mode data in the filesystem using cereal. */
214     void save();
215 
216     /** @brief Trace the Power Mode and IPS parameters. */
217     void print();
218 
219     /** @brief Invalidate the persisted mode */
invalidateMode()220     void invalidateMode()
221     {
222         modeData.modeInitialized = false;
223     }
224 
225   private:
226     /** @brief Power Mode data filename to store persistent data */
227     static constexpr auto powerModeFilename = "powerModeData";
228 
229     /** @brief Power Mode data object to be persisted */
230     PowerModeData modeData;
231 
232     /** @brief Loads the OEM mode data in the filesystem using cereal. */
233     void load();
234 };
235 
236 /** @class PowerMode
237  *  @brief Monitors for changes to the power mode and notifies occ
238  *
239  *  The customer power mode is provided to the OCC by host TMGT when the occ
240  *  first goes active or is reset.  This code is responsible for sending
241  *  the power mode to the OCC if the mode is changed while the occ is active.
242  */
243 
244 class PowerMode : public ModeInterface, public IpsInterface
245 {
246   public:
247     /** @brief PowerMode object to inform occ of changes to mode
248      *
249      * This object will monitor for changes to the power mode setting.
250      * If a change is detected, and the occ is active, then this object will
251      * notify the OCC of the change.
252      *
253      * @param[in] managerRef - manager object reference
254      * @param[in] modePath - Power Mode dbus path
255      * @param[in] ipsPath - Idle Power Saver dbus path
256      */
257     explicit PowerMode(const Manager& managerRef, const char* modePath,
258                        const char* ipsPath, EventPtr& event);
259 
260     /** @brief Initialize the persistent data with default values
261      *
262      * @return true if initialization completed
263      */
264     bool initPersistentData();
265 
266     /** @brief Set the power mode lock (dbus method)
267      *
268      * @return true if successful
269      */
270     bool powerModeLock();
271 
272     /** @brief Get the power mode lock status (dbus method)
273      *
274      * @return true if locked
275      */
276     bool powerModeLockStatus();
277 
278     /** @brief Set the current power mode property
279      *
280      * @param[in] newMode     - desired system power mode
281      * @param[in] oemModeData - data required by some OEM Power Modes
282      *
283      * @return true if mode accepted
284      */
285     bool setMode(const SysPwrMode newMode, const uint16_t oemModeData);
286 
287     /** @brief Send mode change command to the master OCC
288      *  @return SUCCESS on success
289      */
290     CmdStatus sendModeChange();
291 
292     /** @brief Send Idle Power Saver config data to the master OCC
293      *  @return SUCCESS on success
294      */
295     CmdStatus sendIpsData();
296 
297     /** @brief Set the master OCC path
298      *
299      * @param[in]  occPath - hwmon path for master OCC
300      */
301     void setMasterOcc(const std::string& occPath);
302 
303     /** @brief Notify object of master OCC state.  If not acitve, no
304      * commands will be sent to the master OCC
305      *
306      * @param[in]  isActive - true when master OCC is active
307      */
setMasterActive(const bool isActive=true)308     void setMasterActive(const bool isActive = true)
309     {
310         masterActive = isActive;
311     };
312 
313     /** @brief Starts to monitor for IPS active state change conditions
314      *
315      *  @param[in] poll - Indicates whether or not the IPS state file should
316      *                    actually be read for changes.
317      */
318     void addIpsWatch(bool poll = true);
319 
320     /** @brief Removes IPS active watch */
321     void removeIpsWatch();
322 
323     /** @brief Set dbus property to SAFE Mode(true) or clear SAFE Mode(false)*/
324     void updateDbusSafeMode(const bool safeMode);
325 
326     /** @brief override the set/get MODE function
327      *
328      *  @param[in] value - Intended value
329      *
330      *  @return          - the value or Updated value of the property
331      */
332     Base::Mode::PowerMode powerMode(Base::Mode::PowerMode value) override;
333 
334     /** @brief Determine if the supplied mode is valid for the system
335      *
336      *  @param[in] mode  - potential mode
337      *
338      *  @return          - true if the mode is valid
339      */
340     bool isValidMode(const SysPwrMode mode);
341 
342   private:
343     /** @brief OCC manager object */
344     const Manager& manager;
345 
346     /** @brief Pass-through occ path on the bus */
347     std::string path;
348 
349     /** @brief OCC instance number */
350     int occInstance;
351 
352     /** @brief Object to send commands to the OCC */
353     std::unique_ptr<open_power::occ::OccCommand> occCmd;
354 
355     /** @brief Used to subscribe to dbus IPS property changes **/
356     sdbusplus::bus::match_t ipsMatch;
357 
358     /** @brief Used to subscribe to dbus defaults property changes **/
359     sdbusplus::bus::match_t defaultsUpdateMatch;
360 
361     OccPersistData persistedData;
362 
363     /** @brief True when the master OCC has been established **/
364     bool masterOccSet;
365 
366     /** @brief True when the master OCC is active **/
367     bool masterActive;
368 
369     /** @brief True when the ecoModes are supported for this system **/
370     bool ecoModeSupport = false;
371 
372     /** @brief List of customer supported power modes **/
373     std::set<SysPwrMode> customerModeList = {
374         SysPwrMode::STATIC, SysPwrMode::POWER_SAVING, SysPwrMode::MAX_PERF};
375 
376     /** @brief List of OEM supported power modes **/
377     std::set<SysPwrMode> oemModeList = {SysPwrMode::SFP, SysPwrMode::FFO,
378                                         SysPwrMode::MAX_FREQ};
379 
380     /** @brief IPS status data filename to read */
381     const fs::path ipsStatusFile = std::filesystem::path{OCC_HWMON_PATH} /
382                                    std::filesystem::path{OCC_MASTER_NAME} /
383                                    "occ_ips_status";
384 
385     /** @brief Current state of error watching */
386     bool watching = false;
387 
388     /** @brief register for the callback from the POLL IPS changed event */
389     void registerIpsStatusCallBack();
390 
391     /** @brief Get the current power mode property
392      *
393      * @param[out] currentMode - current system power mode
394      * @param[out] oemModeData - frequency data for some OEM mode
395      *
396      * @return true if data read successfully
397      */
398     bool getMode(SysPwrMode& currentMode, uint16_t& oemModeData);
399 
400     /** @brief Update the power mode property on DBus
401      *
402      * @param[in]  newMode - desired power mode
403      *
404      * @return true on success
405      */
406     bool updateDbusMode(const SysPwrMode newMode);
407 
408     /** @brief Callback for IPS setting changes
409      *
410      * Process change and inform OCC
411      *
412      * @param[in]  msg - Data associated with IPS change signal
413      *
414      */
415     void ipsChanged(sdbusplus::message_t& msg);
416 
417     /** @brief Get the Idle Power Saver properties
418      *
419      *  @param[out] enabled - Idle Power Save status (true = enabled)
420      *  @param[out] enterUtil - IPS Enter Utilization (%)
421      *  @param[out] enterTime - IPS Enter Time (seconds)
422      *  @param[out] exitUtil - IPS Exit Utilization (%)
423      *  @param[out] exitTime - IPS Exit Time (seconds)
424      *
425      * @return true if data read successfully
426      */
427     bool getIPSParms(bool& enabled, uint8_t& enterUtil, uint16_t& enterTime,
428                      uint8_t& exitUtil, uint16_t& exitTime);
429 
430     /** Update the Idle Power Saver data on DBus
431      *
432      *  @param[in] enabled - Idle Power Save status (true = enabled)
433      *  @param[in] enterUtil - IPS Enter Utilization (%)
434      *  @param[in] enterTime - IPS Enter Time (seconds)
435      *  @param[in] exitUtil - IPS Exit Utilization (%)
436      *  @param[in] exitTime - IPS Exit Time (seconds)
437      *
438      *  @return true if parameters were set successfully
439      */
440     bool updateDbusIPS(const bool enabled, const uint8_t enterUtil,
441                        const uint16_t enterTime, const uint8_t exitUtil,
442                        const uint16_t exitTime);
443 
444     /** @brief Callback for entity manager default changes
445      *
446      * Called when PowerModeProperties defaults are available
447      */
448     void defaultsReady(sdbusplus::message_t& msg);
449 
450     /** @brief Get the default power mode property for this system type
451      *
452      * @param[out] defaultMode - default system power mode
453      *
454      * @return true if data read successfully
455      */
456     bool getDefaultMode(SysPwrMode& defaultMode);
457 
458     /** @brief Get the default Idle Power Saver properties for this system type
459      *
460      *  @param[out] enabled - Idle Power Save status (true = enabled)
461      *  @param[out] enterUtil - IPS Enter Utilization (%)
462      *  @param[out] enterTime - IPS Enter Time (seconds)
463      *  @param[out] exitUtil - IPS Exit Utilization (%)
464      *  @param[out] exitTime - IPS Exit Time (seconds)
465      *
466      *  @return true if parameters were read successfully
467      */
468     bool getDefaultIPSParms(bool& enabled, uint8_t& enterUtil,
469                             uint16_t& enterTime, uint8_t& exitUtil,
470                             uint16_t& exitTime);
471 
472     /** @brief Read the default Idle Power Saver parameters and save them to the
473      * DBUS so they will get used
474      *
475      * @return true if restore was successful
476      */
477     bool useDefaultIPSParms();
478 
479     /** @brief Read the supported power modes from entity-manager and update
480      * AllowedPowerModes on dbus
481      *
482      * @return true if data was found/updated
483      */
484     bool getSupportedModes();
485 
486     /** @brief callback for the POLL IPS changed event
487      *
488      *  @param[in] es       - Populated event source
489      *  @param[in] fd       - Associated File descriptor
490      *  @param[in] revents  - Type of event
491      *  @param[in] userData - User data that was passed during registration
492      */
493     static int ipsStatusCallBack(sd_event_source* es, int fd, uint32_t revents,
494                                  void* userData);
495 
496     /** @brief Opens the IPS file and populates fd */
497     bool openIpsFile();
498 
499     /** @brief sd_event wrapped in unique_ptr */
500     EventPtr& event;
501 
502     /** @brief event source wrapped in unique_ptr */
503     EventSourcePtr eventSource;
504 
505     /** @brief When the ips status event is received, analyzes it */
506     virtual void analyzeIpsEvent();
507 
508   protected:
509     /** @brief File descriptor to watch for errors */
510     int fd = -1;
511 };
512 
513 } // namespace powermode
514 
515 } // namespace occ
516 
517 } // namespace open_power
518 #endif
519