summaryrefslogtreecommitdiffstats
path: root/src/occ_405/amec/amec_controller.c
blob: 1f02edab165b88f523789628641f2277caf56837 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/occ_405/amec/amec_controller.c $                          */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2019                        */
/* [+] 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 <occ_common.h>
#include <sensor.h>
#include <amec_sys.h>
#include <memory.h>

//*************************************************************************
// Externs
//*************************************************************************
extern dimm_sensor_flags_t G_dimm_temp_expired_bitmap;
extern uint16_t G_cent_temp_expired_bitmap;
//*************************************************************************
// Macros
//*************************************************************************

//*************************************************************************
// Defines/Enums
//*************************************************************************

//*************************************************************************
// Structures
//*************************************************************************

//*************************************************************************
// Globals
//*************************************************************************

//*************************************************************************
// Function Prototypes
//*************************************************************************

//*************************************************************************
// Functions
//*************************************************************************
// Function Specification
//
// Name: amec_controller_proc_thermal
//
// Description: This function implements the Proportional Controller for the
//              processor thermal control. Although it doesn't return any
//              results, it populates the thermal vote in the field
//              g_amec->thermalproc.speed_request.
//
// Task Flags:
//
// End Function Specification
void amec_controller_proc_thermal()
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                      l_thermal_winner = 0;
    uint16_t                      l_residue = 0;
    uint16_t                      l_old_residue = 0;
    int16_t                       l_error = 0;
    int16_t                       l_cpu_speed = 0;
    int16_t                       l_throttle_chg = 0;
    int32_t                       l_throttle = 0;
    sensor_t                    * l_sensor = NULL;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/
    // Get TEMPPROCTHRM sensor, which is hottest core temperature in OCC
    // processor
    l_sensor = getSensorByGsid(TEMPPROCTHRM);

    // Use the highest temperature of all processors in 0.1 degrees C
    l_thermal_winner = l_sensor->sample * 10;

    // Check if there is an error
    if (g_amec->thermalproc.setpoint == l_thermal_winner)
        return;

    // Calculate the thermal control error
    l_error = g_amec->thermalproc.setpoint - l_thermal_winner;

    // Proportional Controller for the thermal control loop
    l_throttle = (int32_t) l_error * g_amec->thermalproc.Pgain;
    l_residue = (uint16_t) l_throttle;
    l_throttle_chg = (int16_t) (l_throttle >> 16);

    if ((int16_t) l_throttle_chg > (int16_t) g_amec->sys.speed_step_limit)
    {
        l_throttle_chg = g_amec->sys.speed_step_limit;
    }
    else
    {
        if ((int16_t) l_throttle_chg < ((int16_t) (-g_amec->sys.speed_step_limit)))
        {
            l_throttle_chg = (int16_t)(-g_amec->sys.speed_step_limit);
        }
    }

    // Calculate the new thermal CPU speed request
    l_cpu_speed = g_amec->thermalproc.speed_request +
        (int16_t) l_throttle_chg * g_amec->sys.speed_step;

    // Proceed with residue summation to correctly follow set-point
    l_old_residue = g_amec->thermalproc.total_res;
    g_amec->thermalproc.total_res += l_residue;
    if (g_amec->thermalproc.total_res < l_old_residue)
    {
        l_cpu_speed += g_amec->sys.speed_step;
    }

    // Enforce actuator saturation limits
    if (l_cpu_speed > g_amec->sys.max_speed)
        l_cpu_speed = g_amec->sys.max_speed;
    if (l_cpu_speed < g_amec->sys.min_speed)
        l_cpu_speed = g_amec->sys.min_speed;

    // Generate the new thermal speed request
    g_amec->thermalproc.speed_request = l_cpu_speed;
    // Calculate frequency request based on thermal speed request
    g_amec->thermalproc.freq_request = amec_controller_speed2freq(
            g_amec->thermalproc.speed_request,
            g_amec->sys.fmax);

    // Update the Processor OT Throttle Sensor
    if(g_amec->thermalproc.freq_request < g_amec->sys.fmax)
    {
       // frequency is less than max indicate throttle due to OT
       sensor_update(AMECSENSOR_PTR(PROCOTTHROT), 1);
    }
    else  // not currently throttled due to OT
    {
       sensor_update(AMECSENSOR_PTR(PROCOTTHROT), 0);
    }
}

// Function Specification
//
// Name: amec_controller_vrm_vdd_thermal
//
// Description: This function implements the Proportional Controller for the
//              VRM Vdd thermal control. Although it doesn't return any
//              results, it populates the thermal vote in the field
//              g_amec->thermalvdd.speed_request.
//
// Task Flags:
//
// End Function Specification
void amec_controller_vrm_vdd_thermal()
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                      l_vdd_temp_tenthsC = 0;
    uint16_t                      l_residue = 0;
    uint16_t                      l_old_residue = 0;
    int16_t                       l_thermal_diff = 0;
    int16_t                       l_cpu_speed = 0;
    int16_t                       l_throttle_chg = 0;
    int32_t                       l_throttle = 0;
    sensor_t                    * l_sensor = NULL;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/
    // Get VRM Vdd temperature sensor
    l_sensor = getSensorByGsid(TEMPVDD);

    // Convert current Vdd temperature to 0.1 degrees C
    l_vdd_temp_tenthsC = l_sensor->sample * 10;

    // Calculate the temperature difference from the DVFS setpoint
    l_thermal_diff = g_amec->thermalvdd.setpoint - l_vdd_temp_tenthsC;

    // Proportional Controller for the thermal control loop
    l_throttle = (int32_t) l_thermal_diff * g_amec->thermalvdd.Pgain;
    l_residue = (uint16_t) l_throttle;
    l_throttle_chg = (int16_t) (l_throttle >> 16);

    // don't allow a throttle change more than step limit
    if ((int16_t) l_throttle_chg > (int16_t) g_amec->sys.speed_step_limit)
    {
        l_throttle_chg = g_amec->sys.speed_step_limit;
    }
    else
    {
        if ((int16_t) l_throttle_chg < ((int16_t) (-g_amec->sys.speed_step_limit)))
        {
            l_throttle_chg = (int16_t)(-g_amec->sys.speed_step_limit);
        }
    }

    // Calculate the new thermal CPU speed request
    l_cpu_speed = g_amec->thermalvdd.speed_request +
        (int16_t)(l_throttle_chg * g_amec->sys.speed_step);

    // Proceed with residue summation to correctly follow set-point
    l_old_residue = g_amec->thermalvdd.total_res;
    g_amec->thermalvdd.total_res += l_residue;
    if (g_amec->thermalvdd.total_res < l_old_residue)
    {
        l_cpu_speed += g_amec->sys.speed_step;
    }

    // Enforce actuator saturation limits
    if (l_cpu_speed > g_amec->sys.max_speed)
        l_cpu_speed = g_amec->sys.max_speed;
    if (l_cpu_speed < g_amec->sys.min_speed)
        l_cpu_speed = g_amec->sys.min_speed;

    // Generate the new thermal speed request
    g_amec->thermalvdd.speed_request = l_cpu_speed;
    // Calculate frequency request based on thermal speed request
    g_amec->thermalvdd.freq_request = amec_controller_speed2freq(
            g_amec->thermalvdd.speed_request,
            g_amec->sys.fmax);
}

//*************************************************************************
// Function Specification
//
// Name: amec_controller_dimm_thermal
//
// Description: This function implements the Proportional Controller for the
//              DIMM thermal control. Although it doesn't return any
//              results, it populates the thermal vote in the field
//              g_amec->thermaldimm.speed_request.
//
// Task Flags:
//
// End Function Specification
void amec_controller_dimm_thermal()
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                      l_thermal_winner = 0;
    uint16_t                      l_residue = 0;
    uint16_t                      l_old_residue = 0;
    int16_t                       l_error = 0;
    int16_t                       l_mem_speed = 0;
    int16_t                       l_throttle_chg = 0;
    int32_t                       l_throttle = 0;
    sensor_t                    * l_sensor = NULL;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/
    // Get TEMPDIMMTHRM sensor value
    l_sensor = getSensorByGsid(TEMPDIMMTHRM);

    if(G_dimm_temp_expired_bitmap.dw[0] ||
       G_dimm_temp_expired_bitmap.dw[1])
    {
       //we were not able to read one or more dimm temperatures.
       //Assume temperature is at the setpoint plus 1 degree C.
       l_thermal_winner = g_amec->thermaldimm.setpoint + 10;
    }
    else
    {
        // Use the highest temperature of all DIMMs in 0.1 degrees C
        l_thermal_winner = l_sensor->sample * 10;
    }

    // Check if there is an error
    if (g_amec->thermaldimm.setpoint == l_thermal_winner)
        return;

    // Calculate the thermal control error
    l_error = g_amec->thermaldimm.setpoint - l_thermal_winner;

    // Proportional Controller for the thermal control loop based on DIMM
    // temperatures
    l_throttle = (int32_t) l_error * g_amec->thermaldimm.Pgain;
    l_residue = (uint16_t) l_throttle;
    l_throttle_chg = (int16_t) (l_throttle >> 16);

    if ((int16_t) l_throttle_chg > AMEC_MEMORY_SPEED_CHANGE_LIMIT)
    {
        l_throttle_chg = AMEC_MEMORY_SPEED_CHANGE_LIMIT;
    }
    else
    {
        if ((int16_t) l_throttle_chg < (-AMEC_MEMORY_SPEED_CHANGE_LIMIT))
        {
            l_throttle_chg = -AMEC_MEMORY_SPEED_CHANGE_LIMIT;
        }
    }

    // Calculate the new thermal speed request for DIMMs
    l_mem_speed = g_amec->thermaldimm.speed_request +
        (int16_t) l_throttle_chg * AMEC_MEMORY_STEP_SIZE;

    // Proceed with residue summation to correctly follow set-point
    l_old_residue = g_amec->thermaldimm.total_res;
    g_amec->thermaldimm.total_res += l_residue;
    if (g_amec->thermaldimm.total_res < l_old_residue)
    {
        l_mem_speed += AMEC_MEMORY_STEP_SIZE;
    }

    // Enforce actuator saturation limits
    if (l_mem_speed > AMEC_MEMORY_MAX_STEP)
        l_mem_speed = AMEC_MEMORY_MAX_STEP;
    if (l_mem_speed < AMEC_MEMORY_MIN_STEP)
        l_mem_speed = AMEC_MEMORY_MIN_STEP;

    // Generate the new thermal speed request
    g_amec->thermaldimm.speed_request = (uint16_t) l_mem_speed;

    // Update the Memory OT Throttle Sensor
    if(g_amec->thermaldimm.speed_request < AMEC_MEMORY_MAX_STEP)
    {
       // Memory speed is less than max indicate throttle due to OT
       sensor_update(AMECSENSOR_PTR(MEMOTTHROT), 1);
    }
    else  // not currently throttled due to OT
    {
       sensor_update(AMECSENSOR_PTR(MEMOTTHROT), 0);
    }

}


//*************************************************************************
// Function Specification
//
// Name: amec_controller_membuf_thermal
//
// Description: This function implements the Proportional Controller for the
//              membuf thermal control. Although it doesn't return any
//              results, it populates the thermal vote in the field
//              g_amec->thermalcent.speed_request.
//
// Task Flags:
//
// End Function Specification
void amec_controller_membuf_thermal()
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                      l_thermal_winner = 0;
    uint16_t                      l_residue = 0;
    uint16_t                      l_old_residue = 0;
    int16_t                       l_error = 0;
    int16_t                       l_mem_speed = 0;
    int16_t                       l_throttle_chg = 0;
    int32_t                       l_throttle = 0;
    sensor_t                    * l_sensor = NULL;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/
    // Get TEMPCENT sensor value
    l_sensor = getSensorByGsid(TEMPCENT);

    if(G_cent_temp_expired_bitmap)
    {
        //we were not able to get a valid temperature.  Assume it is 1 degree
        //over the setpoint.
        l_thermal_winner = g_amec->thermalcent.setpoint + 10;
    }
    else
    {
        // Use the highest temperature of all membuf in 0.1 degrees C
        l_thermal_winner = l_sensor->sample * 10;
    }

    // Check if there is an error
    if (g_amec->thermalcent.setpoint == l_thermal_winner)
        return;

    // Calculate the thermal control error
    l_error = g_amec->thermalcent.setpoint - l_thermal_winner;

    // Proportional Controller for the thermal control loop based on membuf
    // temperatures
    l_throttle = (int32_t) l_error * g_amec->thermalcent.Pgain;
    l_residue = (uint16_t) l_throttle;
    l_throttle_chg = (int16_t) (l_throttle >> 16);

    if ((int16_t) l_throttle_chg > AMEC_MEMORY_SPEED_CHANGE_LIMIT)
    {
        l_throttle_chg = AMEC_MEMORY_SPEED_CHANGE_LIMIT;
    }
    else
    {
        if ((int16_t) l_throttle_chg < (-AMEC_MEMORY_SPEED_CHANGE_LIMIT))
        {
            l_throttle_chg = -AMEC_MEMORY_SPEED_CHANGE_LIMIT;
        }
    }

    // Calculate the new thermal speed request for membufs
    l_mem_speed = g_amec->thermalcent.speed_request +
        (int16_t) l_throttle_chg * AMEC_MEMORY_STEP_SIZE;

    // Proceed with residue summation to correctly follow set-point
    l_old_residue = g_amec->thermalcent.total_res;
    g_amec->thermalcent.total_res += l_residue;
    if (g_amec->thermalcent.total_res < l_old_residue)
    {
        l_mem_speed += AMEC_MEMORY_STEP_SIZE;
    }

    // Enforce actuator saturation limits
    if (l_mem_speed > AMEC_MEMORY_MAX_STEP)
        l_mem_speed = AMEC_MEMORY_MAX_STEP;
    if (l_mem_speed < AMEC_MEMORY_MIN_STEP)
        l_mem_speed = AMEC_MEMORY_MIN_STEP;

    // Generate the new thermal speed request
    g_amec->thermalcent.speed_request = (uint16_t) l_mem_speed;
}

//*************************************************************************
// Function Specification
//
// Name: amec_controller_speed2freq
//
// Description: Helper function to convert speed to MHz.
//
// End Function Specification
uint16_t amec_controller_speed2freq (const uint16_t i_speed, const uint16_t i_fmax)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t                    l_tempreg = 0;
    uint16_t                    l_temp16 = 0;
    uint32_t                    l_temp32 = 0;
    uint16_t                    l_freq = 0;
    uint32_t                    l_divide32[2] = {0, 0};

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/
    // to handle max freq changing (i.e. mode change) between now and running amec_slv_proc_voting_box
    // if speed is unconstrained set freq to unconstrained so voting box will use
    // the most recent maximum frequency
    if(i_speed >= g_amec->sys.max_speed)
    {
        l_freq = 0xFFFF;
    }
    else
    {
        l_temp16 = i_fmax;
        l_tempreg = (uint16_t)i_speed;
        l_temp32 = ((uint32_t)l_tempreg)*((uint32_t)l_temp16);
        l_temp16 = (uint16_t)1000;
        l_divide32[1] = (uint32_t)l_temp16;
        l_divide32[0] = (uint32_t)l_temp32;
        l_divide32[0] /= l_divide32[1];
        l_temp32 = l_divide32[0];
        l_freq = (uint16_t)l_temp32; /* freq will always fit in 16 bits */
    }
    return l_freq;
}

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