summaryrefslogtreecommitdiffstats
path: root/src/usr/htmgt/htmgt_memthrottles.C
blob: e4c5593f10c7b1ca35b113ff303809cb9556a0c3 (plain)
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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/htmgt/htmgt_memthrottles.C $                          */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2017                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include "htmgt_memthrottles.H"
#include "htmgt_utility.H"
#include <htmgt/htmgt_reasoncodes.H>
#include <errl/errlmanager.H>
#include <targeting/common/commontargeting.H>
#include <targeting/common/utilFilter.H>
#include <targeting/common/attributes.H>
#include <targeting/common/targetservice.H>
#include <fapi2.H>
#include <plat_hwp_invoker.H>
#include "htmgt_cfgdata.H"


// Hardware Procedures:
#include <p9_mss_utils_to_throttle.H>
#include <p9_mss_bulk_pwr_throttles.H>

// See src/include/usr/targeting/common/utilFilter.H for handy target utilities

using namespace TARGETING;



//for unit testing
//#define TRACUCOMP(args...)  TMGT_INF(args)
#define TRACUCOMP(args...)

namespace HTMGT
{
    uint32_t G_mem_power_max_throttles = 0;
    uint32_t G_mem_power_min_throttles = 0;

/**
 * Run hardware procedure to determine throttle/numerator/number of commands
 * based on the specified utilization
 *
 * @param[in] i_utilization - Minimum utilization value required
 * @param[in] i_watt_target - power target required for bulk_pwr_throttles
 */
errlHndl_t call_utils_to_throttle(
         std::vector <fapi2::Target<fapi2::TARGET_TYPE_MCS>> i_fapi_target_list,
                                  const uint32_t i_util,
                                  const uint32_t i_watt_target = 0)
{
    errlHndl_t err = NULL;
    uint32_t utilization[TMGT_MAX_MCA_PER_MCS] = { i_util, i_util };
    uint32_t l_watt_targets[TMGT_MAX_MCA_PER_MCS][TMGT_MAX_DIMM_PER_MCA] =
        {i_watt_target, i_watt_target, i_watt_target, i_watt_target};
    TMGT_INF("call_utils_to_throttle: utilization: 0x%04X, watt_target: "
             "0x%04X", i_util, i_watt_target);

    // Update input attributes for specified targets
    for(const auto & l_fapi_target : i_fapi_target_list)
    {
        FAPI_ATTR_SET(fapi2::ATTR_MSS_DATABUS_UTIL, l_fapi_target, utilization);
        if (i_watt_target)
        {
            FAPI_ATTR_SET(fapi2::ATTR_MSS_MEM_WATT_TARGET,
                          l_fapi_target, l_watt_targets);
        }
    }

    // p9_mss_utils_to_throttle() - Sets number commands allowed within a
    //                              given port databus utilization
    // inputs: ATTR_MSS_DATABUS_UTIL
    // outputs: ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_SLOT,
    //          ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_PORT, and
    //          ATTR_MSS_PORT_MAXPOWER
    TMGT_INF("call_utils_to_throttle: Calling HWP:p9_mss_utils_to_throttle"
             "(POWER) for %d MCS Targets", i_fapi_target_list.size());
    {
        FAPI_INVOKE_HWP(err, p9_mss_utils_to_throttle, i_fapi_target_list);
    }

    if (NULL != err)
    {
        TMGT_ERR("call_utils_to_throttle: HWP:p9_mss_utils_to_throttle failed"
                 " with rc=0x%04X", err->reasonCode());
    }

    return err;

} // end call_utils_to_throttle()



/**
 * Calculate throttles for over-temperture
 *
 * @param[in] i_fapi_target_list - list of FAPI MCS targets
 * @param[in] i_utilization - Minimum utilization value required
 */
errlHndl_t memPowerThrottleOT(
       std::vector < fapi2::Target< fapi2::TARGET_TYPE_MCS>> i_fapi_target_list,
                                   const uint8_t i_utilization)
{
    errlHndl_t err = NULL;

    TMGT_INF("memPowerThrottleOT: Calculating throttles for util: %d",
             i_utilization);

    err = call_utils_to_throttle(i_fapi_target_list, i_utilization);
    if (NULL == err)
    {
        uint32_t ot_mem_power = 0;
        for(const auto & mcs_fapi_target : i_fapi_target_list)
        {
            // Read HWP outputs:
            ATTR_OT_MIN_N_PER_MBA_type l_slot = {0};
            ATTR_OT_MEM_POWER_type l_power = {0};
            FAPI_ATTR_GET(fapi2::ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_SLOT,
                          mcs_fapi_target, l_slot);
            FAPI_ATTR_GET(fapi2::ATTR_MSS_PORT_MAXPOWER,
                          mcs_fapi_target, l_power);
            ot_mem_power += l_power[0] + l_power[1];

            // Update MCS data (to be sent to OCC)
            TARGETING::Target * mcs_target =
                reinterpret_cast<TARGETING::Target *>(mcs_fapi_target.get());
            ConstTargetHandle_t proc_target = getParentChip(mcs_target);
            assert(proc_target != nullptr);
            const uint8_t occ_instance =
                proc_target->getAttr<TARGETING::ATTR_POSITION>();
            uint8_t mcs_unit = 0xFF;
            mcs_target->tryGetAttr<TARGETING::ATTR_CHIP_UNIT>(mcs_unit);
            mcs_target->setAttr<ATTR_OT_MIN_N_PER_MBA>(l_slot);
            mcs_target->setAttr<ATTR_OT_MEM_POWER>(l_power);
            TMGT_INF("memPowerThrottleOT: MIN: OCC%d/MCS%d - "
                     "N/slot: %d/%d, Power: %d/%dcW",
                     occ_instance, mcs_unit, l_slot[0], l_slot[1],
                     l_power[0], l_power[1]);
        }
        TMGT_INF("memPowerThrottleOT: Total Minimum Memory"
                 " Power: %dW", ot_mem_power/100);
    }
    else
    {
        TMGT_ERR("memPowerThrottleOT: Failed to calculate over-temp"
                 " memory throttles, rc=0x%04X",
                 err->reasonCode());
    }

    return err;

} // end memPowerThrottleOT()



/**
 * Run the p9_mss_bulk_pwr_throttles hardware procedure
 * to calculate memory throttling numerator values.
 *
 * @param[in] i_fapi_target_list - list of FAPI MCS targets
 * @param[in] i_wattTarget - the power target for the MCS
 * @param[in] i_utilization - the utilization desired
 */
errlHndl_t call_bulk_pwr_throttles(
      std::vector < fapi2::Target< fapi2::TARGET_TYPE_MCS>> i_fapi_target_list,
                       const uint32_t i_wattTarget,
                       const uint8_t i_utilization)
{
    errlHndl_t err = NULL;

    //Set the values the procedures need
    err = call_utils_to_throttle(i_fapi_target_list,
                                 i_utilization, i_wattTarget);
    if (NULL == err)
    {
        TMGT_INF("call_bulk_pwr_throttles: Calling HWP:p9_mss_bulk_pwr_"
                 "throttles(POWER) for MCS Targets");
        FAPI_INVOKE_HWP(err, p9_mss_bulk_pwr_throttles, i_fapi_target_list,
                        mss::throttle_type::POWER);
        if (NULL != err)
        {
            TMGT_ERR("call_bulk_pwr_throttles: p9_mss_bulk_pwr_throttles "
                     "failed with rc=0x%04X", err->reasonCode());
        }
    }
    else
    {
        TMGT_ERR("call_bulk_pwr_throttles: call_utils_to_throttle failed with"
                 " rc=0x%04X", err->reasonCode());
    }

    return err;

} // end call_bulk_pwr_throttles()


/**
 * Calculate throttles for when system has redundant power (N+1 mode)
 *
 * @param[in] i_fapi_target_list - list of FAPI MCS targets
 * @param[in] i_utilization - Minimum utilization value required
 * @param[in] i_efficiency - the regulator efficiency
 */
errlHndl_t memPowerThrottleRedPower(
         std::vector <fapi2::Target<fapi2::TARGET_TYPE_MCS>> i_fapi_target_list,
                              const uint8_t i_utilization,
                              const uint8_t i_efficiency)
{
    errlHndl_t err = NULL;
    Target* sys = NULL;
    uint32_t power = 0;
    uint32_t wattTarget = 0;
    targetService().getTopLevelTarget(sys);
    assert(sys != NULL);

    //Get the max redundant (N+1) power allocated to memory
    power = sys->getAttr<ATTR_OPEN_POWER_N_PLUS_ONE_MAX_MEM_POWER_WATTS>();
    power *= 100; // convert to centiWatts

    //Account for the regulator efficiency (percentage), if supplied
    if (i_efficiency != 0)
    {
        power = (power * i_efficiency) / 100;
    }

    //Find the Watt target for each MCS
    if (i_fapi_target_list.size())
    {
        wattTarget = power / i_fapi_target_list.size();
    }

    TMGT_INF("memPowerThrottleRedPower: N+1 power: %dW / %dcW per MCS",
             power/100, wattTarget);

    //Calculate the throttles
    err = call_bulk_pwr_throttles(i_fapi_target_list,
                                  wattTarget,
                                  i_utilization);
    if (NULL == err)
    {
        G_mem_power_min_throttles = 0;
        for(auto & mcs_fapi_target : i_fapi_target_list)
        {
            // Read HWP output parms:
            ATTR_N_PLUS_ONE_N_PER_MBA_type l_slot = {0};
            ATTR_N_PLUS_ONE_N_PER_CHIP_type l_port = {0};
            ATTR_N_PLUS_ONE_MEM_POWER_type l_power = {0};
            FAPI_ATTR_GET(fapi2::ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_SLOT,
                          mcs_fapi_target, l_slot);
            FAPI_ATTR_GET(fapi2::ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_PORT,
                          mcs_fapi_target, l_port);
            FAPI_ATTR_GET(fapi2::ATTR_MSS_PORT_MAXPOWER,
                          mcs_fapi_target, l_power);
            // Calculate memory power at min throttles (in Watts)
            G_mem_power_min_throttles += (l_power[0] + l_power[1])/100;

            // Update MCS data (to be sent to OCC)
            TARGETING::Target * mcs_target =
                reinterpret_cast<TARGETING::Target *>(mcs_fapi_target.get());
            ConstTargetHandle_t proc_target = getParentChip(mcs_target);
            assert(proc_target != nullptr);
            const uint8_t occ_instance =
                proc_target->getAttr<TARGETING::ATTR_POSITION>();
            uint8_t mcs_unit = 0xFF;
            mcs_target->tryGetAttr<TARGETING::ATTR_CHIP_UNIT>(mcs_unit);
            mcs_target->setAttr<ATTR_N_PLUS_ONE_N_PER_MBA>(l_slot);
            mcs_target->setAttr<ATTR_N_PLUS_ONE_N_PER_CHIP>(l_port);
            mcs_target->setAttr<ATTR_N_PLUS_ONE_MEM_POWER>(l_power);

            TMGT_INF("memPowerThrottleRedPower: NOMINAL: OCC%d/MCS%d - "
                     "N/slot: %d/%d, N/port: %d/%d, Power: %d/%dcW",
                     occ_instance, mcs_unit, l_slot[0], l_slot[1],
                     l_port[0], l_port[1], l_power[0], l_power[1]);
        }
        TMGT_INF("memPowerThrottleRedPower: Total Redundant Memory Power: %dW",
                 G_mem_power_min_throttles);
    }
    else
    {
        TMGT_ERR("memPowerThrottleRedPower: Failed to calculate redundant "
                 "power memory throttles, rc=0x%04X",
                 err->reasonCode());
    }

    return err;

} // end memPowerThrottleRedPower()



/**
 * Run hardware procedures to calculate the throttles for power capping
 *
 * @param[in] i_fapi_target_list - list of FAPI MCS targets
 * @param[in] i_utilization - Minimum utilization value required
 */
errlHndl_t memPowerThrottlePowercap(
       std::vector < fapi2::Target< fapi2::TARGET_TYPE_MCS>> i_fapi_target_list,
                                    const uint8_t i_utilization)
{
    errlHndl_t err = NULL;

    TMGT_INF("memPowerThrottlePowercap: Calculating throttles for util: %d",
             i_utilization);

    if (i_utilization != 0)
    {
        //Calculate the throttles
        err = call_utils_to_throttle(i_fapi_target_list, i_utilization);
    }
    if (NULL == err)
    {
        G_mem_power_max_throttles = 0;
        for(auto & mcs_fapi_target : i_fapi_target_list)
        {
            TARGETING::Target * mcs_target =
                reinterpret_cast<TARGETING::Target *>(mcs_fapi_target.get());
            ConstTargetHandle_t proc_target = getParentChip(mcs_target);
            assert(proc_target != nullptr);
            const uint8_t occ_instance =
                proc_target->getAttr<TARGETING::ATTR_POSITION>();
            uint8_t mcs_unit = 0xFF;
            mcs_target->tryGetAttr<TARGETING::ATTR_CHIP_UNIT>(mcs_unit);

            // Read HWP output parms (if the procedure was run):
            ATTR_POWERCAP_N_PER_MBA_type l_slot = {0xFF, 0xFF};
            ATTR_POWERCAP_N_PER_CHIP_type l_port = {0xFF, 0xFF};
            ATTR_POWERCAP_MEM_POWER_type l_power = {0};
            if (i_utilization != 0)
            {
                FAPI_ATTR_GET(fapi2::ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_SLOT,
                              mcs_fapi_target, l_slot);
                FAPI_ATTR_GET(fapi2::ATTR_MSS_MEM_THROTTLED_N_COMMANDS_PER_PORT,
                              mcs_fapi_target, l_port);
                FAPI_ATTR_GET(fapi2::ATTR_MSS_PORT_MAXPOWER,
                              mcs_fapi_target, l_power);
            }
            // else N values will be 0xFF and will be overwritten below

            // Validate pcap throttles are the lowest throttles
            ATTR_N_PLUS_ONE_N_PER_MBA_type l_slot_redun = {0};
            ATTR_N_PLUS_ONE_N_PER_CHIP_type l_port_redun = {0};
            ATTR_N_PLUS_ONE_MEM_POWER_type l_power_redun = {0};
            mcs_target->tryGetAttr<ATTR_N_PLUS_ONE_N_PER_MBA>(l_slot_redun);
            mcs_target->tryGetAttr<ATTR_N_PLUS_ONE_N_PER_CHIP>(l_port_redun);
            mcs_target->tryGetAttr<ATTR_N_PLUS_ONE_MEM_POWER>(l_power_redun);
            ATTR_OVERSUB_N_PER_MBA_type l_slot_oversub = {0};
            ATTR_OVERSUB_N_PER_CHIP_type l_port_oversub = {0};
            ATTR_OVERSUB_MEM_POWER_type l_power_oversub = {0};
            mcs_target->tryGetAttr<ATTR_OVERSUB_N_PER_MBA>(l_slot_oversub);
            mcs_target->tryGetAttr<ATTR_OVERSUB_N_PER_CHIP>(l_port_oversub);
            mcs_target->tryGetAttr<ATTR_OVERSUB_MEM_POWER>(l_power_oversub);
            unsigned int mca_index;
            for (mca_index = 0; mca_index < TMGT_MAX_MCA_PER_MCS; ++mca_index)
            {
                if (l_slot[mca_index] > l_slot_oversub[mca_index])
                {
                    TMGT_INF("memPowerThrottlePowercap: MCS%d/MCA%d"
                             " oversub throttle (%d) < pcap throttle (%d)",
                             mcs_unit, mca_index,
                             l_slot_oversub[mca_index], l_slot[mca_index]);
                    l_slot[mca_index] = l_slot_oversub[mca_index];
                    l_port[mca_index] = l_port_oversub[mca_index];
                    l_power[mca_index] = l_power_oversub[mca_index];
                }
                if (l_slot[mca_index] > l_slot_redun[mca_index])
                {
                    TMGT_INF("memPowerThrottlePowercap: MCS%d/MCA%d - "
                             " redun throttle (%d) < pcap throttle (%d)",
                             mcs_unit, mca_index,
                             l_slot_redun[mca_index], l_slot[mca_index]);
                    l_slot[mca_index] = l_slot_redun[mca_index];
                    l_port[mca_index] = l_port_redun[mca_index];
                    l_power[mca_index] = l_power_redun[mca_index];
                }
            }

            // Update MCS data (to be sent to OCC)
            mcs_target->setAttr<ATTR_POWERCAP_N_PER_MBA>(l_slot);
            mcs_target->setAttr<ATTR_POWERCAP_N_PER_CHIP>(l_port);
            mcs_target->setAttr<ATTR_POWERCAP_MEM_POWER>(l_power);
            // Calculate memory power at max throttles (in Watts)
            G_mem_power_max_throttles += (l_power[0] + l_power[1]) / 100;

            // Trace Results
            TMGT_INF("memPowerThrottlePowercap: PCAP: OCC%d/MCS%d - "
                     "N/slot: %d/%d, N/port: %d/%d, Power: %d/%dcW",
                     occ_instance, mcs_unit, l_slot[0], l_slot[1],
                     l_port[0], l_port[1], l_power[0], l_power[1]);
        }
        TMGT_INF("memPowerThrottlePowercap: Total PowerCap Memory"
                 " Power: %dW (@max throttles)", G_mem_power_max_throttles);
    }
    else
    {
        TMGT_ERR("memPowerThrottlePowercap: Failed to calculate powercap "
                 "memory throttles, rc=0x%04X",
                 err->reasonCode());
    }

    return err;

} // end memPowerThrottlePowercap()


void calculate_system_power()
{
    Target* sys = NULL;
    targetService().getTopLevelTarget(sys);
    assert(sys != NULL);

    TARGETING::TargetHandleList proc_list;
    // Get all processor chips (do not have to be functional)
    getAllChips(proc_list, TARGETING::TYPE_PROC, false);
    const uint8_t num_procs = proc_list.size();

    const uint16_t proc_socket_power =
        sys->getAttr<ATTR_PROC_SOCKET_POWER_WATTS>();
    TMGT_INF("calculate_system_power: proc socket power: %5dW (%d procs)",
             proc_socket_power, num_procs);
    const uint16_t misc_power =
        sys->getAttr<ATTR_MISC_SYSTEM_COMPONENTS_MAX_POWER_WATTS>();
    TMGT_INF("calculate_system_power: misc power: %5dW", misc_power);

    // Calculate Total non-GPU maximum power (Watts):
    //   Maximum system power excluding GPUs when CPUs are at maximum frequency
    //   (ultra turbo) and memory at maximum power (least throttled) plus
    //   everything else (fans...) excluding GPUs.
    uint32_t power_max = proc_socket_power * num_procs;
    TMGT_INF("calculate_system_power: power(max)  proc: %5dW, mem: %5dW",
                 power_max, G_mem_power_min_throttles);
    power_max += G_mem_power_min_throttles + misc_power;
    TMGT_INF("calculate_system_power: max proc/mem/misc power (no GPUs): %5dW",
             power_max);
    sys->setAttr<ATTR_CALCULATED_MAX_SYS_POWER_EXCLUDING_GPUS>(power_max);

    // Calculate Total Processor/Memory Power Drop (Watts):
    //   The max non-GPU power can be reduced (with proc/mem)
    //   calculates this as the CPU power at minimum frequency plus memory at
    //   minimum power (most throttled)
    const uint16_t freq_min = sys->getAttr<ATTR_MIN_FREQ_MHZ>();
    uint16_t freq_turbo, freq_ultra;
    check_wof_support(freq_turbo, freq_ultra);
    if (freq_turbo == 0)
    {
        freq_turbo = sys->getAttr<ATTR_FREQ_CORE_MAX>();
        if (freq_turbo == 0)
        {
            // If no turbo point, then use nominal...
            TMGT_ERR("calculate_system_power: No turbo frequency to calculate "
                     "power drop.  Using nominal");
            freq_turbo = sys->getAttr<ATTR_NOMINAL_FREQ_MHZ>();
        }
    }
    const uint16_t mhz_per_watt = sys->getAttr<ATTR_PROC_MHZ_PER_WATT>();
    // Drop always calculated from Turbo to Min (not ultra)
    uint32_t proc_drop = ((freq_turbo - freq_min) / mhz_per_watt);
    TMGT_INF("calculate_system_power: Processor Power Drop: %dMHz / %dMHz/W"
             " = %dW / proc",
             freq_turbo - freq_min, mhz_per_watt, proc_drop);
    proc_drop *= num_procs;
    const uint32_t memory_drop =
        G_mem_power_min_throttles - G_mem_power_max_throttles;
    TMGT_INF("calculate_system_power: Memory Power Drop: %d - %d = %dW",
             G_mem_power_min_throttles, G_mem_power_max_throttles,
             G_mem_power_min_throttles - G_mem_power_max_throttles);
    const uint32_t power_drop = proc_drop + memory_drop;
    TMGT_INF("calculate_system_power: Proc/Mem Power Drop: %d + %d = %dW",
             proc_drop, memory_drop, power_drop);
    sys->setAttr<ATTR_CALCULATED_PROC_MEMORY_POWER_DROP>(power_drop);

} // end calculate_system_power()


errlHndl_t calcMemThrottles()
{
    Target* sys = NULL;

    targetService().getTopLevelTarget(sys);
    assert(sys != NULL);

    uint8_t min_utilization =
        sys->getAttr<ATTR_OPEN_POWER_MIN_MEM_UTILIZATION_THROTTLING>();
    if (min_utilization == 0)
    {
        uint32_t l_safe_n_per_mba =
        sys->getAttr<ATTR_MSS_MRW_SAFEMODE_MEM_THROTTLED_N_COMMANDS_PER_PORT>();
        TMGT_INF("MIN_MEM_UTILIZATION_THROTTLING is 0, so reading "
                 "ATTR_MSS_MRW_SAFEMODE_MEM_THROTTLED_N_COMMANDS_PER_PORT:"
                 " %d", l_safe_n_per_mba);
        if (l_safe_n_per_mba == 0)
        {
            l_safe_n_per_mba = 10;
            TMGT_ERR("ATTR_MSS_MRW_SAFEMODE_MEM_THROTTLED_N_COMMANDS_PER_PORT"
                     " is 0!  Using %d", l_safe_n_per_mba);
        }
    }
    const uint8_t efficiency =
        sys->getAttr<ATTR_OPEN_POWER_REGULATOR_EFFICIENCY_FACTOR>();
    TMGT_INF("calcMemThrottles: Using min utilization=%d, efficiency=%d"
             " percent", min_utilization, efficiency);

    //Get all functional MCSs
    TargetHandleList mcs_list;
    getAllChiplets(mcs_list, TYPE_MCS, true);
    TMGT_INF("calcMemThrottles: found %d MCSs", mcs_list.size());

    // Create a FAPI Target list for HWP
    std::vector < fapi2::Target< fapi2::TARGET_TYPE_MCS>> l_fapi_target_list;
    for(const auto & mcs_target : mcs_list)
    {
        uint32_t mcs_huid = 0xFFFFFFFF;
        uint8_t mcs_unit = 0xFF;
        mcs_target->tryGetAttr<TARGETING::ATTR_HUID>(mcs_huid);
        mcs_target->tryGetAttr<TARGETING::ATTR_CHIP_UNIT>(mcs_unit);

        // Query the functional MCAs for this MCS
        TARGETING::TargetHandleList mca_list;
        getChildAffinityTargetsByState(mca_list, mcs_target, CLASS_UNIT,
                                       TYPE_MCA, UTIL_FILTER_FUNCTIONAL);
        uint8_t occ_instance = 0xFF;
        ConstTargetHandle_t proc_target = getParentChip(mcs_target);
        assert(proc_target != nullptr);
        occ_instance = proc_target->getAttr<TARGETING::ATTR_POSITION>();
        TMGT_INF("calcMemThrottles: OCC%d, MCS%d HUID:0x%08X has %d"
                 " functional MCSs",
                 occ_instance, mcs_unit, mcs_huid, mca_list.size());

        // Convert to FAPI target and add to list
        fapi2::Target<fapi2::TARGET_TYPE_MCS> l_fapiTarget(mcs_target);
        l_fapi_target_list.push_back(l_fapiTarget);
    }

    errlHndl_t err = NULL;
    do
    {
        //Calculate Throttle settings for Over Temperature
        err = memPowerThrottleOT(l_fapi_target_list, min_utilization);
        if (NULL != err) break;

        //Calculate Throttle settings for Nominal/Turbo
        err = memPowerThrottleRedPower(l_fapi_target_list,
                                       min_utilization, efficiency);
        if (NULL != err) break;

        //Calculate Throttle settings for Power Capping
        uint8_t pcap_min_utilization;
        if (!sys->tryGetAttr<ATTR_OPEN_POWER_MIN_MEM_UTILIZATION_POWER_CAP>
            (pcap_min_utilization))
        {
            pcap_min_utilization = 0;
        }
        err = memPowerThrottlePowercap(l_fapi_target_list,pcap_min_utilization);

    } while(0);

    calculate_system_power();

    if (err)
    {
        err->collectTrace(HTMGT_COMP_NAME);
    }

    return err;

}
}  // End namespace
OpenPOWER on IntegriCloud