summaryrefslogtreecommitdiffstats
path: root/src/occ_405/amec/amec_dps.c
blob: e41783a08bc450dda0664636e543a294e9cacfe8 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/occ_405/amec/amec_dps.c $                                 */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2015                        */
/* [+] 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                                                     */

/*----------------------------------------------------------------------------*/
/* Includes                                                                   */
/*----------------------------------------------------------------------------*/
#include <common_types.h>
#include <occ_sys_config.h>
#include <proc_data.h>
#include <amec_sys.h>
#include <amec_perfcount.h>
#include <amec_part.h>
#include <amec_dps.h>
#include <sensor.h>
#include <amec_controller.h>

/*----------------------------------------------------------------------------*/
/* Constants                                                                  */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/* Globals                                                                    */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/* Typedef / Enum                                                             */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/* Internal Function Prototypes                                               */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/* Code                                                                       */
/*----------------------------------------------------------------------------*/

// Function Specification
//
// Name: amec_dps_update_core_util
//
// Description: Update per-core utilization variables.
//
// End Function Specification
void amec_dps_update_core_util(void)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                    l_temp16 = 0;
    uint8_t                     l_tempreg = 0;
    uint8_t                     l_idx = 0;
    amec_core_perf_counter_t*   l_perf = NULL;
    amec_part_t                 *l_part = NULL;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    // If g_amec->fw.dps_no_update_flag=1, no updating is allowed
    if (!g_amec->fw.dps_no_update_flag)
    {
        // Update moving average of util_slack and util_active for all cores
        for(l_idx=0; l_idx<MAX_NUM_CORES; l_idx++)
        {
            if (!CORE_PRESENT(l_idx) || CORE_OFFLINE(l_idx))
            {
                continue; //nothing to do if the core's disabled
            }

            l_part = amec_part_find_by_core(&g_amec->part_config, l_idx);

            // If this core doesn't belong to a core group, then do nothing
            if (l_part == NULL)
            {
                continue;
            }

            // Get pointer to the perf struc of this core
            l_perf = &g_amec->proc[0].core[l_idx].core_perf;

            l_perf->ptr_putUtilslack--; // Decrement put pointer
            if (l_perf->ptr_putUtilslack > TWO_TO_THE_POWER_OF_FIFTEEN)
            {
                // Wrap circular pointer. WARNING -> buffer size must be a
                // power of 2
                l_perf->ptr_putUtilslack &= (MAX_UTIL_SLACK_AVG_LEN-1);
            }

            // Locate oldest sample associated with 32-bit moving average
            // WARNING -> we need to read this sample first in case entire
            // buffer is used which would result in the write overwriting the
            // oldest sample we need to read.
            l_temp16=(uint16_t)(l_perf->ptr_putUtilslack+l_part->dpsalg.sample_count_util) & (MAX_UTIL_SLACK_AVG_LEN-1);
            l_tempreg=l_perf->ptr_util_slack_avg_buffer[l_temp16];
            // Bleed off oldest sample from moving average
            l_perf->util_slack_accumulator = l_perf->util_slack_accumulator-(uint32_t)l_tempreg;
            l_tempreg=l_perf->ptr_util_active_avg_buffer[l_temp16];
            // Bleed off oldest sample from moving average
            l_perf->util_active_accumulator = l_perf->util_active_accumulator-(uint32_t)l_tempreg;
            // Add in newest sample into moving average
            l_tempreg = l_perf->util_slack_core_counter;
            l_perf->util_slack_accumulator = l_perf->util_slack_accumulator+(uint32_t)l_tempreg;
            // Write new sample into buffer.
            l_perf->ptr_util_slack_avg_buffer[l_perf->ptr_putUtilslack]=l_tempreg;
            // Add in newest sample into moving average
            l_tempreg = l_perf->util_active_core_counter;
            l_perf->util_active_accumulator = l_perf->util_active_accumulator+(uint32_t)l_tempreg;
            // Write new sample into buffer.
            l_perf->ptr_util_active_avg_buffer[l_perf->ptr_putUtilslack]=l_tempreg;

            // Reset counters every 2msec
            l_perf->util_active_core_counter=0;
            l_perf->util_slack_core_counter=0;
        }
    }
}

// Function Specification
//
// Name: amec_dps_partition_update_sensors
//
// Description: Update utilization sensors for a core group.
//
// End Function Specification
void amec_dps_partition_update_sensors(const uint16_t i_part_id)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    amec_core_perf_counter_t*   l_perf = NULL;
    uint16_t                    l_core_index = 0;
    uint8_t                     l_idx = 0;
    uint32_t                    l_cg_active_accumulator = 0;
    uint32_t                    l_cg_slack_accumulator = 0;
    uint16_t                    l_cg_util_slack_perc = 0;
    uint32_t                    l_divide32[2] = {0, 0};

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/
    for (l_idx=0; l_idx<g_amec->part_config.part_list[i_part_id].ncores; l_idx++)
    {
        l_core_index = g_amec->part_config.part_list[i_part_id].core_list[l_idx];
        l_perf = &g_amec->proc[0].core[l_core_index % MAX_NUM_CORES].core_perf;

        // Sum accumulators for core group
        l_cg_active_accumulator += l_perf->util_active_accumulator;
        l_cg_slack_accumulator += l_perf->util_slack_accumulator;
    }

    if (l_cg_active_accumulator == 0)
    {
        // Check for divide by zero
        l_cg_util_slack_perc=16384;  // if no active cores, set slack=100%
    }
    else
    {
        l_divide32[1]=(uint32_t)l_cg_active_accumulator;
        l_divide32[0]=(uint32_t)l_cg_slack_accumulator<<14;  // *16384 because 16384=1.0
        l_divide32[0] /= l_divide32[1];
        l_cg_util_slack_perc=(uint16_t)(l_divide32[0]);
    }

    // Update the sensor of the utilization slack
    sensor_update(&g_amec->part_config.part_list[i_part_id].util2msslack, l_cg_util_slack_perc);
}

// Function Specification
//
// Name: amec_dps_partition_alg
//
// Description: DPS algorithm function.
//
// End Function Specification
void amec_dps_partition_alg(const uint16_t i_part_id)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    amec_core_perf_counter_t*   l_perf = NULL;
    uint16_t                    l_freq = 0;
    uint16_t                    l_core_index = 0;
    uint16_t                    l_idx = 0;
    uint16_t                    l_tempreg = 0;
    uint16_t                    l_temp16 = 0;
    uint32_t                    l_divide32[2] = {0, 0};
    OCC_INTERNAL_MODE           l_part_policy = 0xFF;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    // Switch on dps_type of the core group
    switch (g_amec->part_config.part_list[i_part_id].dpsalg.type)
    {
    case 0x00:
        // Type 0 algorithm: no dynamic power savings enabled
        for (l_idx=0; l_idx<g_amec->part_config.part_list[i_part_id].ncores; l_idx++)
        {
            l_core_index = g_amec->part_config.part_list[i_part_id].core_list[l_idx];
            l_perf = &g_amec->proc[0].core[l_core_index % MAX_NUM_CORES].core_perf;
            l_perf->dps_freq_request = UINT16_MAX;
        }
        break;

    case 41:
        // Type 41 algorithm
        // l_tempreg=measure of slack over gamma time interval on a per core basis
        l_tempreg = (uint16_t)g_amec->part_config.part_list[i_part_id].util2msslack.sample;
        // Convert to 10000 is equivalent to 100.00% utilization.
        l_divide32[1]=(uint32_t)53687;
        l_divide32[0]=(uint32_t)l_tempreg<<15;
        l_divide32[0] /= l_divide32[1];
        // l_tempreg is level of utilization using per core info
        l_tempreg=10000-(uint16_t)(l_divide32[0]);

        l_temp16 = g_amec->part_config.part_list[i_part_id].dpsalg.util_speed_request;
        if (l_tempreg > g_amec->part_config.part_list[i_part_id].dpsalg.alpha_up)
        {
            l_temp16 = g_amec->part_config.part_list[i_part_id].dpsalg.util_speed_request +
                g_amec->part_config.part_list[i_part_id].dpsalg.step_up;
        }
        else
        {
            if (l_tempreg < g_amec->part_config.part_list[i_part_id].dpsalg.alpha_down)
            {
                l_temp16 = g_amec->part_config.part_list[i_part_id].dpsalg.util_speed_request -
                    g_amec->part_config.part_list[i_part_id].dpsalg.step_down;
            }
        }

        // Now limit final speed in l_temp16 to be between allowed speeds.
        if (l_temp16 > g_amec->sys.max_speed)
        {
            l_temp16 = g_amec->sys.max_speed;
        }
        // Use the lower boundary based on the power policy of the core group
        l_part_policy = g_amec->part_config.part_list[i_part_id].es_policy;
        if (l_temp16 < g_amec->part_mode_freq[l_part_policy].min_speed)
        {
            l_temp16 = g_amec->part_mode_freq[l_part_policy].min_speed;
        }

        // Generate vote for utilization
        g_amec->part_config.part_list[i_part_id].dpsalg.util_speed_request = l_temp16;
        l_freq = amec_controller_speed2freq(l_temp16, g_amec->part_mode_freq[l_part_policy].fmax);
        g_amec->part_config.part_list[i_part_id].dpsalg.freq_request = l_freq;

        for (l_idx=0; l_idx<g_amec->part_config.part_list[i_part_id].ncores; l_idx++)
        {
            l_core_index = g_amec->part_config.part_list[i_part_id].core_list[l_idx];
            l_perf = &g_amec->proc[0].core[l_core_index % MAX_NUM_CORES].core_perf;
            l_perf->dps_freq_request = l_freq;
        }
        // End algorithm type 41
        break;

    default:
        break;
    }
}

// Function Specification
//
// Name: amec_dps_main
//
// Description: Main DPS function.
//
// End Function Specification
void amec_dps_main(void)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                    l_idx = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    // First, update the utilization variables for all cores
    amec_dps_update_core_util();

    // Loop through all core groups and apply energy-savings policy
    for (l_idx=0; l_idx<AMEC_PART_MAX_PART; l_idx++)
    {
        if (!g_amec->part_config.part_list[l_idx].valid)
        {
            continue;
        }

        if (g_amec->part_config.part_list[l_idx].ncores == 0)
        {
            continue;
        }

        switch (g_amec->part_config.part_list[l_idx].es_policy)
        {
            case OCC_INTERNAL_MODE_DPS:
            case OCC_INTERNAL_MODE_DPS_MP:
                amec_dps_partition_update_sensors(l_idx);
                amec_dps_partition_alg(l_idx);
                break;

            default:
                // No energy-savings policy: DPS vote is already disabled when
                // policy first selected for partition or core ownership
                // changes (amec_part.c)
                break;
        }
    }

    // For GA1, we need to send the Fwish to the Master OCC
    if ((g_amec->part_config.part_list[0].es_policy == OCC_INTERNAL_MODE_DPS) ||
        (g_amec->part_config.part_list[0].es_policy == OCC_INTERNAL_MODE_DPS_MP))
    {
        // If this core group policy is one of the DPS modes, then send the
        // frequency request from the DPS algorithm
        G_dcom_slv_outbox_tx.fwish =
            g_amec->part_config.part_list[0].dpsalg.freq_request;
    }
    else
    {
        // Else, send the nominal frequency of the system
        G_dcom_slv_outbox_tx.fwish =
            g_amec->part_mode_freq[OCC_INTERNAL_MODE_NOM].fmax;
    }
    // We also need to send the Factual to the Master OCC
    for (l_idx=0; l_idx<MAX_NUM_CORES; l_idx++)
    {
        // Find the first valid core and send its frequency
        if (CORE_PRESENT(l_idx) && !CORE_OFFLINE(l_idx))
        {
            G_dcom_slv_outbox_tx.factual =
                AMECSENSOR_ARRAY_PTR(FREQREQC0,l_idx)->sample;
            break;
        }
    }
}

/*----------------------------------------------------------------------------*/
/* End                                                                        */
/*----------------------------------------------------------------------------*/
OpenPOWER on IntegriCloud