-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpowermode.cpp
1466 lines (1320 loc) · 46.5 KB
/
powermode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "powermode.hpp"
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef POWERVM_CHECK
#include <com/ibm/Host/Target/server.hpp>
#endif
#include <org/open_power/OCC/Device/error.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/Control/Power/Mode/server.hpp>
#include <cassert>
#include <fstream>
#include <regex>
namespace open_power
{
namespace occ
{
namespace powermode
{
using namespace phosphor::logging;
using namespace std::literals::string_literals;
using namespace sdbusplus::org::open_power::OCC::Device::Error;
using Mode = sdbusplus::xyz::openbmc_project::Control::Power::server::Mode;
using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
// List of all Power Modes that are currently supported (and in Redfish)
#define VALID_POWER_MODE_SETTING(mode) \
((mode == SysPwrMode::STATIC) || (mode == SysPwrMode::POWER_SAVING) || \
(mode == SysPwrMode::BALANCED_PERF) || (mode == SysPwrMode::MAX_PERF) || \
(mode == SysPwrMode::EFF_FAVOR_POWER) || \
(mode == SysPwrMode::EFF_FAVOR_PERF))
// List of OEM Power Modes that are currently supported
#define VALID_OEM_POWER_MODE_SETTING(mode) \
((mode == SysPwrMode::SFP) || (mode == SysPwrMode::FFO) || \
(mode == SysPwrMode::MAX_FREQ) || \
(mode == SysPwrMode::NON_DETERMINISTIC))
// List of all Power Modes that disable IPS
#define IS_ECO_MODE(mode) \
((mode == SysPwrMode::EFF_FAVOR_POWER) || \
(mode == SysPwrMode::EFF_FAVOR_PERF))
// Constructor
PowerMode::PowerMode(const Manager& managerRef, const char* modePath,
const char* ipsPath, EventPtr& event) :
ModeInterface(utils::getBus(), modePath,
ModeInterface::action::emit_no_signals),
manager(managerRef),
ipsMatch(utils::getBus(),
sdbusplus::bus::match::rules::propertiesChanged(PIPS_PATH,
PIPS_INTERFACE),
[this](auto& msg) { this->ipsChanged(msg); }),
defaultsUpdateMatch(
utils::getBus(),
sdbusplus::bus::match::rules::propertiesChangedNamespace(
"/xyz/openbmc_project/inventory", PMODE_DEFAULT_INTERFACE),
[this](auto& msg) { this->defaultsReady(msg); }),
masterOccSet(false), masterActive(false), ipsObjectPath(ipsPath),
event(event)
{
// Get supported power modes from entity manager
if (false == getSupportedModes())
{
// Did not find them so use default customer modes
using Mode =
sdbusplus::xyz::openbmc_project::Control::Power::server::Mode;
// Update power modes that will be allowed by the Redfish interface
ModeInterface::allowedPowerModes(
{Mode::PowerMode::Static, Mode::PowerMode::MaximumPerformance,
Mode::PowerMode::PowerSaving});
}
SysPwrMode currentMode;
uint16_t oemModeData = 0;
// Read the persisted power mode
if (getMode(currentMode, oemModeData))
{
// Validate persisted mode is supported
if (isValidMode(currentMode))
{
// Update power mode on DBus and create IPS object if allowed
updateDbusMode(currentMode);
}
else
{
lg2::error("PowerMode: Persisted power mode ({MODE}/{DATA}) is not "
"valid. Reading system default mode",
"MODE", currentMode, "DATA", oemModeData);
persistedData.invalidateMode();
// Read default power mode
initPersistentData();
}
}
};
void PowerMode::createIpsObject()
{
if (!ipsObject)
{
lg2::info("createIpsObject: Creating IPS object");
ipsObject =
std::make_unique<IpsInterface>(utils::getBus(), ipsObjectPath);
uint8_t enterUtil, exitUtil;
uint16_t enterTime, exitTime;
bool ipsEnabled;
// Read the persisted Idle Power Saver parametres
if (getIPSParms(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime))
{
// Update Idle Power Saver parameters on DBus
updateDbusIPS(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime);
}
// Starts watching for IPS state changes.
addIpsWatch(true);
needToSendIpsData = true;
}
}
void PowerMode::removeIpsObject()
{
if (ipsObject)
{
// Stop watching for IPS state changes.
removeIpsWatch();
lg2::info("removeIpsObject: Deleting IPS object");
ipsObject.reset(nullptr);
}
needToSendIpsData = false;
}
// Set the Master OCC
void PowerMode::setMasterOcc(const std::string& masterOccPath)
{
if (masterOccSet)
{
if (masterOccPath != path)
{
lg2::error(
"PowerMode::setMasterOcc: Master changed (was OCC{INST}, {PATH})",
"INST", occInstance, "PATH", masterOccPath);
if (occCmd)
{
occCmd.reset();
}
}
}
path = masterOccPath;
occInstance = path.back() - '0';
lg2::debug("PowerMode::setMasterOcc(OCC{INST}, {PATH})", "INST",
occInstance, "PATH", path);
if (!occCmd)
{
occCmd = std::make_unique<open_power::occ::OccCommand>(
occInstance, path.c_str());
}
masterOccSet = true;
};
// Set the state of power mode lock. Writing persistent data via dbus method.
bool PowerMode::powerModeLock()
{
lg2::info("PowerMode::powerModeLock: locking mode change");
persistedData.updateModeLock(true); // write persistent data
return true;
}
// Get the state of power mode. Reading persistent data via dbus method.
bool PowerMode::powerModeLockStatus()
{
bool status = persistedData.getModeLock(); // read persistent data
lg2::info("PowerMode::powerModeLockStatus: {STATUS}", "STATUS",
status ? "locked" : "unlocked");
return status;
}
// Called from OCC PassThrough interface (via CE login / BMC command line)
bool PowerMode::setMode(const SysPwrMode newMode, const uint16_t oemModeData)
{
if (persistedData.getModeLock())
{
lg2::info("PowerMode::setMode: mode change blocked");
return false;
}
if (updateDbusMode(newMode) == false)
{
// Unsupported mode
return false;
}
// Save mode
persistedData.updateMode(newMode, oemModeData);
// Send mode change to OCC
if (sendModeChange() != CmdStatus::SUCCESS)
{
// Mode change failed
return false;
}
return true;
}
// Convert PowerMode value to occ-control internal SysPwrMode
// Returns SysPwrMode::NO_CHANGE if mode not valid
SysPwrMode getInternalMode(const Mode::PowerMode& mode)
{
if (mode == Mode::PowerMode::MaximumPerformance)
{
return SysPwrMode::MAX_PERF;
}
else if (mode == Mode::PowerMode::PowerSaving)
{
return SysPwrMode::POWER_SAVING;
}
else if (mode == Mode::PowerMode::Static)
{
return SysPwrMode::STATIC;
}
else if (mode == Mode::PowerMode::EfficiencyFavorPower)
{
return SysPwrMode::EFF_FAVOR_POWER;
}
else if (mode == Mode::PowerMode::EfficiencyFavorPerformance)
{
return SysPwrMode::EFF_FAVOR_PERF;
}
else if (mode == Mode::PowerMode::BalancedPerformance)
{
return SysPwrMode::BALANCED_PERF;
}
lg2::warning("getInternalMode: Invalid PowerMode specified");
return SysPwrMode::NO_CHANGE;
}
// Convert PowerMode string to OCC SysPwrMode
// Returns NO_CHANGE if OEM or unsupported mode
SysPwrMode convertStringToMode(const std::string& i_modeString)
{
SysPwrMode newMode = SysPwrMode::NO_CHANGE;
try
{
Mode::PowerMode newPMode =
Mode::convertPowerModeFromString(i_modeString);
newMode = getInternalMode(newPMode);
}
catch (const std::exception& e)
{
// Strip off prefix to to search OEM modes not part of Redfish
auto prefix = PMODE_INTERFACE + ".PowerMode."s;
std::string shortMode = i_modeString;
std::string::size_type index = i_modeString.find(prefix);
if (index != std::string::npos)
{
shortMode.erase(0, prefix.length());
}
if (shortMode == "FFO")
{
newMode = SysPwrMode::FFO;
}
else if (shortMode == "SFP")
{
newMode = SysPwrMode::SFP;
}
else if (shortMode == "MaxFrequency")
{
newMode = SysPwrMode::MAX_FREQ;
}
else if (shortMode == "NonDeterministic")
{
newMode = SysPwrMode::NON_DETERMINISTIC;
}
else
{
lg2::error(
"convertStringToMode: Invalid Power Mode: {MODE} ({DATA})",
"MODE", shortMode, "DATA", e.what());
}
}
return newMode;
}
// Check if Hypervisor target is PowerVM
bool isPowerVM()
{
bool powerVmTarget = true;
#ifdef POWERVM_CHECK
namespace Hyper = sdbusplus::com::ibm::Host::server;
constexpr auto HYPE_PATH = "/com/ibm/host0/hypervisor";
constexpr auto HYPE_INTERFACE = "com.ibm.Host.Target";
constexpr auto HYPE_PROP = "Target";
// This will throw exception on failure
auto& bus = utils::getBus();
auto service = utils::getService(HYPE_PATH, HYPE_INTERFACE);
auto method = bus.new_method_call(service.c_str(), HYPE_PATH,
"org.freedesktop.DBus.Properties", "Get");
method.append(HYPE_INTERFACE, HYPE_PROP);
auto reply = bus.call(method);
std::variant<std::string> hyperEntryValue;
reply.read(hyperEntryValue);
auto propVal = std::get<std::string>(hyperEntryValue);
if (Hyper::Target::convertHypervisorFromString(propVal) ==
Hyper::Target::Hypervisor::PowerVM)
{
powerVmTarget = true;
}
lg2::debug("isPowerVM returning {VAL}", "VAL", powerVmTarget);
#endif
return powerVmTarget;
}
// Initialize persistent data and return true if successful
bool PowerMode::initPersistentData()
{
if (!persistedData.modeAvailable())
{
// Read the default mode
SysPwrMode currentMode;
if (!getDefaultMode(currentMode))
{
// Unable to read defaults
return false;
}
lg2::info("PowerMode::initPersistentData: Using default mode: {MODE}",
"MODE", currentMode);
// Save default mode as current mode
persistedData.updateMode(currentMode, 0);
// Write default mode to DBus and create IPS object if allowed
updateDbusMode(currentMode);
}
if (!persistedData.ipsAvailable())
{
// Read the default IPS parameters, write persistent file and update
// DBus
return useDefaultIPSParms();
}
return true;
}
// Get the requested power mode and return true if successful
bool PowerMode::getMode(SysPwrMode& currentMode, uint16_t& oemModeData)
{
currentMode = SysPwrMode::NO_CHANGE;
oemModeData = 0;
if (!persistedData.getMode(currentMode, oemModeData))
{
// Persistent data not initialized, read defaults and update DBus
if (!initPersistentData())
{
// Unable to read defaults from entity manager yet
return false;
}
return persistedData.getMode(currentMode, oemModeData);
}
return true;
}
// Set the power mode on DBus and create IPS object if allowed/needed
bool PowerMode::updateDbusMode(const SysPwrMode newMode)
{
if (!isValidMode(newMode))
{
lg2::error(
"PowerMode::updateDbusMode - Requested power mode not supported: {MODE}",
"MODE", newMode);
return false;
}
ModeInterface::PowerMode dBusMode = Mode::PowerMode::OEM;
if (customerModeList.contains(newMode))
{
// Convert mode for DBus
switch (newMode)
{
case SysPwrMode::STATIC:
dBusMode = Mode::PowerMode::Static;
break;
case SysPwrMode::POWER_SAVING:
dBusMode = Mode::PowerMode::PowerSaving;
break;
case SysPwrMode::MAX_PERF:
dBusMode = Mode::PowerMode::MaximumPerformance;
break;
case SysPwrMode::EFF_FAVOR_POWER:
dBusMode = Mode::PowerMode::EfficiencyFavorPower;
break;
case SysPwrMode::EFF_FAVOR_PERF:
dBusMode = Mode::PowerMode::EfficiencyFavorPerformance;
break;
case SysPwrMode::BALANCED_PERF:
dBusMode = Mode::PowerMode::BalancedPerformance;
break;
default:
break;
}
}
// else return OEM mode
// Determine if IPS is allowed and create/remove as needed
if (IS_ECO_MODE(newMode))
{
removeIpsObject();
}
else
{
createIpsObject();
}
ModeInterface::powerMode(dBusMode);
return true;
}
// Send mode change request to the master OCC
CmdStatus PowerMode::sendModeChange()
{
CmdStatus status;
SysPwrMode newMode;
uint16_t oemModeData = 0;
getMode(newMode, oemModeData);
if (isValidMode(newMode))
{
if (IS_ECO_MODE(newMode))
{
// Customer no longer able to enable IPS
removeIpsObject();
}
else
{
// Customer now able to enable IPS
if (!ipsObject)
{
createIpsObject();
}
else
{
if (!watching)
{
// Starts watching for IPS state changes.
addIpsWatch(true);
}
}
}
if (!masterActive || !masterOccSet)
{
// Nothing to do until OCC goes active
lg2::debug("PowerMode::sendModeChange: OCC master not active");
return CmdStatus::SUCCESS;
}
if (!isPowerVM())
{
// Mode change is only supported on PowerVM systems
lg2::debug(
"PowerMode::sendModeChange: MODE CHANGE does not get sent on non-PowerVM systems");
return CmdStatus::SUCCESS;
}
std::vector<std::uint8_t> cmd, rsp;
cmd.reserve(9);
cmd.push_back(uint8_t(CmdType::SET_MODE_AND_STATE));
cmd.push_back(0x00); // Data Length (2 bytes)
cmd.push_back(0x06);
cmd.push_back(0x30); // Data (Version)
cmd.push_back(uint8_t(OccState::NO_CHANGE));
cmd.push_back(uint8_t(newMode));
cmd.push_back(oemModeData >> 8); // Mode Data (Freq Point)
cmd.push_back(oemModeData & 0xFF); //
cmd.push_back(0x00); // reserved
lg2::info(
"PowerMode::sendModeChange: SET_MODE({MODE},{DATA}) command to OCC{INST} ({LEN} bytes)",
"MODE", uint8_t(newMode), "DATA", oemModeData, "INST", occInstance,
"LEN", cmd.size());
status = occCmd->send(cmd, rsp);
if (status == CmdStatus::SUCCESS)
{
if (rsp.size() == 5)
{
if (RspStatus::SUCCESS == RspStatus(rsp[2]))
{
if (needToSendIpsData)
{
// Successful mode change and IPS is now allowed, so
// send IPS config
sendIpsData();
}
}
else
{
lg2::error(
"PowerMode::sendModeChange: SET MODE failed with status {STATUS}",
"STATUS", lg2::hex, rsp[2]);
dump_hex(rsp);
status = CmdStatus::FAILURE;
}
}
else
{
lg2::error(
"PowerMode::sendModeChange: INVALID SET MODE response");
dump_hex(rsp);
status = CmdStatus::FAILURE;
}
}
else
{
lg2::error(
"PowerMode::sendModeChange: SET_MODE FAILED with status={STATUS}",
"STATUS", lg2::hex, uint8_t(status));
}
}
else
{
lg2::error(
"PowerMode::sendModeChange: Unable to set power mode to {MODE}",
"MODE", newMode);
status = CmdStatus::FAILURE;
}
return status;
}
// Handle IPS changed event (from GUI/Redfish)
void PowerMode::ipsChanged(sdbusplus::message_t& msg)
{
bool parmsChanged = false;
std::string interface;
std::map<std::string, std::variant<bool, uint8_t, uint64_t>>
ipsProperties{};
msg.read(interface, ipsProperties);
// Read persisted values
bool ipsEnabled;
uint8_t enterUtil, exitUtil;
uint16_t enterTime, exitTime;
getIPSParms(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime);
if (!ipsObject)
{
lg2::warning(
"ipsChanged: Idle Power Saver can not be modified in an ECO power mode");
return;
}
// Check for any changed data
auto ipsEntry = ipsProperties.find(IPS_ENABLED_PROP);
if (ipsEntry != ipsProperties.end())
{
ipsEnabled = std::get<bool>(ipsEntry->second);
lg2::info("Idle Power Saver change: Enabled={STAT}", "STAT",
ipsEnabled);
parmsChanged = true;
}
ipsEntry = ipsProperties.find(IPS_ENTER_UTIL);
if (ipsEntry != ipsProperties.end())
{
enterUtil = std::get<uint8_t>(ipsEntry->second);
lg2::info("Idle Power Saver change: Enter Util={UTIL}%", "UTIL",
enterUtil);
parmsChanged = true;
}
ipsEntry = ipsProperties.find(IPS_ENTER_TIME);
if (ipsEntry != ipsProperties.end())
{
std::chrono::milliseconds ms(std::get<uint64_t>(ipsEntry->second));
enterTime =
std::chrono::duration_cast<std::chrono::seconds>(ms).count();
lg2::info("Idle Power Saver change: Enter Time={TIME}sec", "TIME",
enterTime);
parmsChanged = true;
}
ipsEntry = ipsProperties.find(IPS_EXIT_UTIL);
if (ipsEntry != ipsProperties.end())
{
exitUtil = std::get<uint8_t>(ipsEntry->second);
lg2::info("Idle Power Saver change: Exit Util={UTIL}%", "UTIL",
exitUtil);
parmsChanged = true;
}
ipsEntry = ipsProperties.find(IPS_EXIT_TIME);
if (ipsEntry != ipsProperties.end())
{
std::chrono::milliseconds ms(std::get<uint64_t>(ipsEntry->second));
exitTime = std::chrono::duration_cast<std::chrono::seconds>(ms).count();
lg2::info("Idle Power Saver change: Exit Time={TIME}sec", "TIME",
exitTime);
parmsChanged = true;
}
if (parmsChanged)
{
if (exitUtil == 0)
{
// Setting the exitUtil to 0 will force restoring the default IPS
// parmeters (0 is not valid exit utilization)
lg2::info(
"Idle Power Saver Exit Utilization is 0%. Restoring default parameters");
// Read the default IPS parameters, write persistent file and update
// DBus
useDefaultIPSParms();
}
else
{
// Update persistant data with new DBus values
persistedData.updateIPS(ipsEnabled, enterUtil, enterTime, exitUtil,
exitTime);
}
// Trigger IPS data to get sent to the OCC
sendIpsData();
}
return;
}
/** @brief Get the Idle Power Saver properties from persisted data
* @return true if IPS parameters were read
*/
bool PowerMode::getIPSParms(bool& ipsEnabled, uint8_t& enterUtil,
uint16_t& enterTime, uint8_t& exitUtil,
uint16_t& exitTime)
{
// Defaults:
ipsEnabled = true; // Enabled
enterUtil = 8; // Enter Utilization (8%)
enterTime = 240; // Enter Delay Time (240s)
exitUtil = 12; // Exit Utilization (12%)
exitTime = 10; // Exit Delay Time (10s)
if (!persistedData.getIPS(ipsEnabled, enterUtil, enterTime, exitUtil,
exitTime))
{
// Persistent data not initialized, read defaults and update DBus
if (!initPersistentData())
{
// Unable to read defaults from entity manager yet
return false;
}
persistedData.getIPS(ipsEnabled, enterUtil, enterTime, exitUtil,
exitTime);
}
if (enterUtil > exitUtil)
{
lg2::error(
"ERROR: Idle Power Saver Enter Utilization ({ENTER}%) is > Exit Utilization ({EXIT}%) - using Exit for both",
"ENTER", enterUtil, "EXIT", exitUtil);
enterUtil = exitUtil;
}
return true;
}
// Set the Idle Power Saver data on DBus
bool PowerMode::updateDbusIPS(const bool enabled, const uint8_t enterUtil,
const uint16_t enterTime, const uint8_t exitUtil,
const uint16_t exitTime)
{
if (ipsObject)
{
// true = skip update signal
ipsObject->setPropertyByName(IPS_ENABLED_PROP, enabled, true);
ipsObject->setPropertyByName(IPS_ENTER_UTIL, enterUtil, true);
// Convert time from seconds to ms
uint64_t msTime = enterTime * 1000;
ipsObject->setPropertyByName(IPS_ENTER_TIME, msTime, true);
ipsObject->setPropertyByName(IPS_EXIT_UTIL, exitUtil, true);
msTime = exitTime * 1000;
ipsObject->setPropertyByName(IPS_EXIT_TIME, msTime, true);
}
else
{
lg2::warning("updateDbusIPS: No IPS object was found");
}
return true;
}
// Send Idle Power Saver config data to the master OCC
CmdStatus PowerMode::sendIpsData()
{
if (!masterActive || !masterOccSet)
{
// Nothing to do
return CmdStatus::SUCCESS;
}
if (!isPowerVM())
{
// Idle Power Saver data is only supported on PowerVM systems
lg2::debug(
"PowerMode::sendIpsData: SET_CFG_DATA[IPS] does not get sent on non-PowerVM systems");
return CmdStatus::SUCCESS;
}
if (!ipsObject)
{
// Idle Power Saver data is not available in Eco Modes
lg2::info(
"PowerMode::sendIpsData: Skipping IPS data due to being in an ECO Power Mode");
return CmdStatus::SUCCESS;
}
bool ipsEnabled;
uint8_t enterUtil, exitUtil;
uint16_t enterTime, exitTime;
getIPSParms(ipsEnabled, enterUtil, enterTime, exitUtil, exitTime);
lg2::info(
"Idle Power Saver Parameters: enabled:{ENABLE}, enter:{EUTIL}%/{ETIME}s, exit:{XUTIL}%/{XTIME}s",
"ENABLE", ipsEnabled, "EUTIL", enterUtil, "ETIME", enterTime, "XUTIL",
exitUtil, "XTIME", exitTime);
std::vector<std::uint8_t> cmd, rsp;
cmd.reserve(12);
cmd.push_back(uint8_t(CmdType::SET_CONFIG_DATA));
cmd.push_back(0x00); // Data Length (2 bytes)
cmd.push_back(0x09); //
cmd.push_back(0x11); // Config Format: IPS Settings
cmd.push_back(0x00); // Version
cmd.push_back(ipsEnabled ? 1 : 0); // IPS Enable
cmd.push_back(enterTime >> 8); // Enter Delay Time
cmd.push_back(enterTime & 0xFF); //
cmd.push_back(enterUtil); // Enter Utilization
cmd.push_back(exitTime >> 8); // Exit Delay Time
cmd.push_back(exitTime & 0xFF); //
cmd.push_back(exitUtil); // Exit Utilization
lg2::info("PowerMode::sendIpsData: SET_CFG_DATA[IPS] "
"command to OCC{INST} ({LEN} bytes)",
"INST", occInstance, "LEN", cmd.size());
CmdStatus status = occCmd->send(cmd, rsp);
if (status == CmdStatus::SUCCESS)
{
if (rsp.size() == 5)
{
if (RspStatus::SUCCESS == RspStatus(rsp[2]))
{
needToSendIpsData = false;
}
else
{
lg2::error(
"PowerMode::sendIpsData: SET_CFG_DATA[IPS] failed with status {STATUS}",
"STATUS", lg2::hex, rsp[2]);
dump_hex(rsp);
status = CmdStatus::FAILURE;
}
}
else
{
lg2::error(
"PowerMode::sendIpsData: INVALID SET_CFG_DATA[IPS] response");
dump_hex(rsp);
status = CmdStatus::FAILURE;
}
}
else
{
lg2::error(
"PowerMode::sendIpsData: SET_CFG_DATA[IPS] with status={STATUS}",
"STATUS", lg2::hex, uint8_t(status));
}
return status;
}
// Print the current values
void OccPersistData::print()
{
if (modeData.modeInitialized)
{
lg2::info(
"OccPersistData: Mode: {MODE}, OEM Mode Data: {DATA} ({DATAHEX} Locked{LOCK})",
"MODE", lg2::hex, uint8_t(modeData.mode), "DATA",
modeData.oemModeData, "DATAHEX", lg2::hex, modeData.oemModeData,
"LOCK", modeData.modeLocked);
}
if (modeData.ipsInitialized)
{
lg2::info(
"OccPersistData: IPS enabled:{ENABLE}, enter:{EUTIL}%/{ETIME}s, exit:{XUTIL}%/{XTIME}s",
"ENABLE", modeData.ipsEnabled, "EUTIL", modeData.ipsEnterUtil,
"ETIME", modeData.ipsEnterTime, "XUTIL", modeData.ipsExitUtil,
"XTIME", modeData.ipsExitTime);
}
}
// Saves the OEM mode data in the filesystem using cereal.
void OccPersistData::save()
{
std::filesystem::path opath =
std::filesystem::path{OCC_CONTROL_PERSIST_PATH} / powerModeFilename;
if (!std::filesystem::exists(opath.parent_path()))
{
std::filesystem::create_directory(opath.parent_path());
}
lg2::debug(
"OccPersistData::save: Writing Power Mode persisted data to {FILE}",
"FILE", opath);
// print();
std::ofstream stream{opath.c_str()};
cereal::JSONOutputArchive oarchive{stream};
oarchive(modeData);
}
// Loads the OEM mode data in the filesystem using cereal.
void OccPersistData::load()
{
std::filesystem::path ipath =
std::filesystem::path{OCC_CONTROL_PERSIST_PATH} / powerModeFilename;
if (!std::filesystem::exists(ipath))
{
modeData.modeInitialized = false;
modeData.ipsInitialized = false;
return;
}
lg2::debug(
"OccPersistData::load: Reading Power Mode persisted data from {FILE}",
"FILE", ipath);
try
{
std::ifstream stream{ipath.c_str()};
cereal::JSONInputArchive iarchive(stream);
iarchive(modeData);
}
catch (const std::exception& e)
{
auto error = errno;
lg2::error("OccPersistData::load: failed to read {FILE}, errno={ERR}",
"FILE", ipath, "ERR", error);
modeData.modeInitialized = false;
modeData.ipsInitialized = false;
}
// print();
}
// Called when PowerModeProperties defaults are available on DBus
void PowerMode::defaultsReady(sdbusplus::message_t& msg)
{
std::map<std::string, std::variant<std::string>> properties{};
std::string interface;
msg.read(interface, properties);
if (persistedData.modeAvailable())
{
// Validate persisted mode is supported
SysPwrMode pMode = SysPwrMode::NO_CHANGE;
uint16_t oemModeData = 0;
persistedData.getMode(pMode, oemModeData);
if (!isValidMode(pMode))
{
lg2::error(
"defaultsReady: Persisted power mode ({MODE}/{DATA}) is not valid. Reading system default mode",
"MODE", pMode, "DATA", oemModeData);
persistedData.invalidateMode();
}
}
// If persistent data exists, then don't need to read defaults
if ((!persistedData.modeAvailable()) || (!persistedData.ipsAvailable()))
{
lg2::info(
"Default PowerModeProperties are now available (persistent modeAvail={MAVAIL}, ipsAvail={IAVAIL})",
"MAVAIL", persistedData.modeAvailable(), "IAVAIL",
persistedData.ipsAvailable());
// Read default power mode defaults and update DBus
initPersistentData();
}
}
// Get the default power mode from DBus and return true if success
bool PowerMode::getDefaultMode(SysPwrMode& defaultMode)
{
try
{
auto& bus = utils::getBus();
std::string path = "/";
std::string service =
utils::getServiceUsingSubTree(PMODE_DEFAULT_INTERFACE, path);
auto method =
bus.new_method_call(service.c_str(), path.c_str(),
"org.freedesktop.DBus.Properties", "Get");
method.append(PMODE_DEFAULT_INTERFACE, "PowerMode");
auto reply = bus.call(method);
std::variant<std::string> stateEntryValue;
reply.read(stateEntryValue);
auto propVal = std::get<std::string>(stateEntryValue);
const std::string fullModeString =
PMODE_INTERFACE + ".PowerMode."s + propVal;
defaultMode = powermode::convertStringToMode(fullModeString);
if (!VALID_POWER_MODE_SETTING(defaultMode))
{
lg2::error("PowerMode::getDefaultMode: Invalid "
"default power mode found: {MODE}",
"MODE", defaultMode);
// If default was read but not valid, use Max Performance
defaultMode = SysPwrMode::MAX_PERF;
return true;
}
}
catch (const sdbusplus::exception_t& e)
{
lg2::error("Unable to read Default Power Mode: {ERR}", "ERR", e.what());
return false;
}
return true;
}
/* Get the default Idle Power Saver properties and return true if successful */
bool PowerMode::getDefaultIPSParms(bool& ipsEnabled, uint8_t& enterUtil,
uint16_t& enterTime, uint8_t& exitUtil,
uint16_t& exitTime)
{
// Defaults:
ipsEnabled = true; // Enabled
enterUtil = 8; // Enter Utilization (8%)
enterTime = 240; // Enter Delay Time (240s)
exitUtil = 12; // Exit Utilization (12%)
exitTime = 10; // Exit Delay Time (10s)
std::map<std::string, std::variant<bool, uint8_t, uint16_t, uint64_t>>
ipsProperties{};
// Get all IPS properties from DBus
try
{
auto& bus = utils::getBus();
std::string path = "/";
std::string service =
utils::getServiceUsingSubTree(PMODE_DEFAULT_INTERFACE, path);
auto method =
bus.new_method_call(service.c_str(), path.c_str(),
"org.freedesktop.DBus.Properties", "GetAll");
method.append(PMODE_DEFAULT_INTERFACE);
auto reply = bus.call(method);
reply.read(ipsProperties);
}
catch (const sdbusplus::exception_t& e)
{
lg2::error(
"Unable to read Default Idle Power Saver parameters so it will be disabled: {ERR}",
"ERR", e.what());
return false;
}
auto ipsEntry = ipsProperties.find("IdlePowerSaverEnabled");
if (ipsEntry != ipsProperties.end())
{
ipsEnabled = std::get<bool>(ipsEntry->second);
}
else
{
lg2::error(
"PowerMode::getDefaultIPSParms could not find property: IdlePowerSaverEnabled");
}
ipsEntry = ipsProperties.find("EnterUtilizationPercent");
if (ipsEntry != ipsProperties.end())
{
enterUtil = std::get<uint64_t>(ipsEntry->second);
}
else
{
lg2::error(
"PowerMode::getDefaultIPSParms could not find property: EnterUtilizationPercent");