~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/skiboot/hw/occ.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2013-2016 IBM Corp.
 
2
 *
 
3
 * Licensed under the Apache License, Version 2.0 (the "License");
 
4
 * you may not use this file except in compliance with the License.
 
5
 * You may obtain a copy of the License at
 
6
 *
 
7
 *      http://www.apache.org/licenses/LICENSE-2.0
 
8
 *
 
9
 * Unless required by applicable law or agreed to in writing, software
 
10
 * distributed under the License is distributed on an "AS IS" BASIS,
 
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
12
 * implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include <skiboot.h>
 
18
#include <xscom.h>
 
19
#include <io.h>
 
20
#include <cpu.h>
 
21
#include <chip.h>
 
22
#include <mem_region.h>
 
23
#include <fsp.h>
 
24
#include <timebase.h>
 
25
#include <hostservices.h>
 
26
#include <errorlog.h>
 
27
#include <opal-api.h>
 
28
#include <opal-msg.h>
 
29
#include <timer.h>
 
30
 
 
31
/* OCC Communication Area for PStates */
 
32
 
 
33
#define P8_HOMER_SAPPHIRE_DATA_OFFSET   0x1F8000
 
34
 
 
35
#define MAX_PSTATES 256
 
36
 
 
37
#define chip_occ_data(chip) \
 
38
                ((struct occ_pstate_table *)(chip->homer_base + \
 
39
                                P8_HOMER_SAPPHIRE_DATA_OFFSET))
 
40
 
 
41
static bool occ_reset;
 
42
static struct lock occ_lock = LOCK_UNLOCKED;
 
43
 
 
44
struct occ_pstate_entry {
 
45
        s8 id;
 
46
        u8 flags;
 
47
        u8 vdd;
 
48
        u8 vcs;
 
49
        u32 freq_khz;
 
50
} __packed;
 
51
 
 
52
/*
 
53
 * OCC-OPAL Shared Memory Region Version 2
 
54
 * https://github.com/open-power/occ/blob/master/src/occ/proc/proc_pstate.h
 
55
 * Interface defined in 'sapphire_table_t'
 
56
 */
 
57
struct occ_pstate_table {
 
58
        u8 valid;
 
59
        u8 version;
 
60
        u8 throttle;
 
61
        s8 pstate_min;
 
62
        s8 pstate_nom;
 
63
        s8 pstate_turbo;
 
64
        s8 pstate_ultra_turbo;
 
65
        u8 spare;
 
66
        u64 reserved;
 
67
        struct occ_pstate_entry pstates[MAX_PSTATES];
 
68
        s8 core_max[16];
 
69
} __packed;
 
70
 
 
71
DEFINE_LOG_ENTRY(OPAL_RC_OCC_LOAD, OPAL_PLATFORM_ERR_EVT, OPAL_OCC,
 
72
                OPAL_CEC_HARDWARE, OPAL_PREDICTIVE_ERR_GENERAL,
 
73
                OPAL_NA);
 
74
 
 
75
DEFINE_LOG_ENTRY(OPAL_RC_OCC_RESET, OPAL_PLATFORM_ERR_EVT, OPAL_OCC,
 
76
                OPAL_CEC_HARDWARE, OPAL_PREDICTIVE_ERR_GENERAL,
 
77
                OPAL_NA);
 
78
 
 
79
DEFINE_LOG_ENTRY(OPAL_RC_OCC_PSTATE_INIT, OPAL_PLATFORM_ERR_EVT, OPAL_OCC,
 
80
                OPAL_CEC_HARDWARE, OPAL_INFO,
 
81
                OPAL_NA);
 
82
 
 
83
DEFINE_LOG_ENTRY(OPAL_RC_OCC_TIMEOUT, OPAL_PLATFORM_ERR_EVT, OPAL_OCC,
 
84
                OPAL_CEC_HARDWARE, OPAL_UNRECOVERABLE_ERR_GENERAL,
 
85
                OPAL_NA);
 
86
 
 
87
/* Check each chip's HOMER/Sapphire area for PState valid bit */
 
88
static bool wait_for_all_occ_init(void)
 
89
{
 
90
        struct proc_chip *chip;
 
91
        uint64_t occ_data_area;
 
92
        struct occ_pstate_table *occ_data;
 
93
        int tries;
 
94
        uint64_t start_time, end_time;
 
95
        uint32_t timeout = 0;
 
96
 
 
97
        if (platform.occ_timeout)
 
98
                timeout = platform.occ_timeout();
 
99
 
 
100
        start_time = mftb();
 
101
        for_each_chip(chip) {
 
102
                /* Check for valid homer address */
 
103
                if (!chip->homer_base) {
 
104
                        /**
 
105
                         * @fwts-label OCCInvalidHomerBase
 
106
                         * @fwts-advice The HOMER base address for a chip
 
107
                         * was not valid. This means that OCC (On Chip
 
108
                         * Controller) will be non-functional and CPU
 
109
                         * frequency scaling will not be functional. CPU may
 
110
                         * be set to a safe, low frequency. Power savings in
 
111
                         * CPU idle or CPU hotplug may be impacted.
 
112
                         */
 
113
                        prlog(PR_ERR,"OCC: Chip: %x homer_base is not valid\n",
 
114
                                chip->id);
 
115
                        return false;
 
116
                }
 
117
 
 
118
                if (!chip->occ_functional) {
 
119
                        prlog(PR_WARNING, "OCC: Chip: %x occ not functional\n",
 
120
                              chip->id);
 
121
                        continue;
 
122
                }
 
123
 
 
124
                /* Get PState table address */
 
125
                occ_data_area = chip->homer_base + P8_HOMER_SAPPHIRE_DATA_OFFSET;
 
126
                occ_data = (struct occ_pstate_table *)occ_data_area;
 
127
 
 
128
                /*
 
129
                 * Checking for occ_data->valid == 1 is ok because we clear all
 
130
                 * homer_base+size before passing memory to host services.
 
131
                 * This ensures occ_data->valid == 0 before OCC load
 
132
                 */
 
133
                tries = timeout * 10;
 
134
                while((occ_data->valid != 1) && tries--) {
 
135
                        time_wait_ms(100);
 
136
                }
 
137
                if (occ_data->valid != 1) {
 
138
                        /**
 
139
                         * @fwts-label OCCInvalidPStateTable
 
140
                         * @fwts-advice The pstate table for a chip
 
141
                         * was not valid. This means that OCC (On Chip
 
142
                         * Controller) will be non-functional and CPU
 
143
                         * frequency scaling will not be functional. CPU may
 
144
                         * be set to a low, safe frequency. This means
 
145
                         * that CPU idle states and CPU frequency scaling
 
146
                         * may not be functional.
 
147
                         */
 
148
                        prlog(PR_ERR, "OCC: Chip: %x PState table is not valid\n",
 
149
                                chip->id);
 
150
                        return false;
 
151
                }
 
152
                prlog(PR_DEBUG, "OCC: Chip %02x Data (%016llx) = %016llx\n",
 
153
                      chip->id, occ_data_area,
 
154
                      *(uint64_t *)occ_data_area);
 
155
        }
 
156
        end_time = mftb();
 
157
        prlog(PR_NOTICE, "OCC: All Chip Rdy after %lld ms\n",
 
158
              (end_time - start_time) / 512 / 1000);
 
159
        return true;
 
160
}
 
161
 
 
162
/* Add device tree properties to describe pstates states */
 
163
/* Retrun nominal pstate to set in each core */
 
164
static bool add_cpu_pstate_properties(s8 *pstate_nom)
 
165
{
 
166
        struct proc_chip *chip;
 
167
        uint64_t occ_data_area;
 
168
        struct occ_pstate_table *occ_data;
 
169
        struct dt_node *power_mgt;
 
170
        u8 nr_pstates, nr_cores = 0;
 
171
        s8 pmax;
 
172
        /* Arrays for device tree */
 
173
        u32 *dt_id, *dt_freq;
 
174
        u8 *dt_vdd, *dt_vcs;
 
175
        s8 *dt_core_max = NULL;
 
176
        bool rc, ultra_turbo_en;
 
177
        int i, j;
 
178
 
 
179
        prlog(PR_DEBUG, "OCC: CPU pstate state device tree init\n");
 
180
 
 
181
        /* Find first chip and core */
 
182
        chip = next_chip(NULL);
 
183
 
 
184
        /* Extract PState information from OCC */
 
185
 
 
186
        /* Dump state table */
 
187
        occ_data_area = chip->homer_base + P8_HOMER_SAPPHIRE_DATA_OFFSET;
 
188
 
 
189
        prlog(PR_DEBUG, "OCC: Data (%16llx) = %16llx %16llx\n",
 
190
              occ_data_area,
 
191
              *(uint64_t *)occ_data_area,
 
192
              *(uint64_t *)(occ_data_area+8));
 
193
        
 
194
        occ_data = (struct occ_pstate_table *)occ_data_area;
 
195
 
 
196
        if (!occ_data->valid) {
 
197
                /**
 
198
                 * @fwts-label OCCInvalidPStateTableDT
 
199
                 * @fwts-advice The pstate table for the first chip
 
200
                 * was not valid. This means that OCC (On Chip
 
201
                 * Controller) will be non-functional. This means
 
202
                 * that CPU idle states and CPU frequency scaling
 
203
                 * will not be functional as OPAL doesn't populate
 
204
                 * the device tree with pstates in this case.
 
205
                 */
 
206
                prlog(PR_ERR, "OCC: PState table is not valid\n");
 
207
                return false;
 
208
        }
 
209
 
 
210
        if (occ_data->version > 1 &&
 
211
            occ_data->pstate_ultra_turbo > occ_data->pstate_turbo)
 
212
                ultra_turbo_en = true;
 
213
        else
 
214
                ultra_turbo_en = false;
 
215
 
 
216
        pmax = ultra_turbo_en ? occ_data->pstate_ultra_turbo :
 
217
                                occ_data->pstate_turbo;
 
218
        nr_pstates = pmax - occ_data->pstate_min + 1;
 
219
        prlog(PR_DEBUG, "OCC: Min %d Nom %d Max %d Nr States %d\n", 
 
220
              occ_data->pstate_min, occ_data->pstate_nom,
 
221
              pmax, nr_pstates);
 
222
 
 
223
        if (nr_pstates <= 1 || nr_pstates > 128) {
 
224
                /**
 
225
                 * @fwts-label OCCInvalidPStateRange
 
226
                 * @fwts-advice The number of pstates is outside the valid
 
227
                 * range (currently <=1 or > 128), so OPAL has not added
 
228
                 * pstates to the device tree. This means that OCC (On Chip
 
229
                 * Controller) will be non-functional. This means
 
230
                 * that CPU idle states and CPU frequency scaling
 
231
                 * will not be functional.
 
232
                 */
 
233
                prlog(PR_ERR, "OCC: OCC range is not valid\n");
 
234
                return false;
 
235
        }
 
236
 
 
237
        power_mgt = dt_find_by_path(dt_root, "/ibm,opal/power-mgt");
 
238
        if (!power_mgt) {
 
239
                /**
 
240
                 * @fwts-label OCCDTNodeNotFound
 
241
                 * @fwts-advice Device tree node /ibm,opal/power-mgt not
 
242
                 * found. OPAL didn't add pstate information to device tree.
 
243
                 * Probably a firmware bug.
 
244
                 */
 
245
                prlog(PR_ERR, "OCC: dt node /ibm,opal/power-mgt not found\n");
 
246
                return false;
 
247
        }
 
248
 
 
249
        rc = false;
 
250
 
 
251
        /* Setup arrays for device-tree */
 
252
        /* Allocate memory */
 
253
        dt_id = malloc(nr_pstates * sizeof(u32));
 
254
        if (!dt_id) {
 
255
                /**
 
256
                 * @fwts-label OCCdt_idENOMEM
 
257
                 * @fwts-advice Out of memory when allocating pstates array.
 
258
                 * No Pstates added to device tree, pstates not functional.
 
259
                 */
 
260
                prlog(PR_ERR, "OCC: dt_id array alloc failure\n");
 
261
                goto out;
 
262
        }
 
263
 
 
264
        dt_freq = malloc(nr_pstates * sizeof(u32));
 
265
        if (!dt_freq) {
 
266
                /**
 
267
                 * @fwts-label OCCdt_freqENOMEM
 
268
                 * @fwts-advice Out of memory when allocating pstates array.
 
269
                 * No Pstates added to device tree, pstates not functional.
 
270
                 */
 
271
                prlog(PR_ERR, "OCC: dt_freq array alloc failure\n");
 
272
                goto out_free_id;
 
273
        }
 
274
 
 
275
        dt_vdd = malloc(nr_pstates * sizeof(u8));
 
276
        if (!dt_vdd) {
 
277
                /**
 
278
                 * @fwts-label OCCdt_vddENOMEM
 
279
                 * @fwts-advice Out of memory when allocating pstates array.
 
280
                 * No Pstates added to device tree, pstates not functional.
 
281
                 */
 
282
                prlog(PR_ERR, "OCC: dt_vdd array alloc failure\n");
 
283
                goto out_free_freq;
 
284
        }
 
285
 
 
286
        dt_vcs = malloc(nr_pstates * sizeof(u8));
 
287
        if (!dt_vcs) {
 
288
                /**
 
289
                 * @fwts-label OCCdt_vcsENOMEM
 
290
                 * @fwts-advice Out of memory when allocating pstates array.
 
291
                 * No Pstates added to device tree, pstates not functional.
 
292
                 */
 
293
                prlog(PR_ERR, "OCC: dt_vcs array alloc failure\n");
 
294
                goto out_free_vdd;
 
295
        }
 
296
 
 
297
        if (ultra_turbo_en) {
 
298
                nr_cores = get_available_nr_cores_in_chip(chip->id);
 
299
                dt_core_max = malloc(nr_cores * sizeof(s8));
 
300
                if (!dt_core_max) {
 
301
                        /**
 
302
                         * @fwts-label OCCdt_core_maxENOMEM
 
303
                         * @fwts-advice Out of memory allocating dt_core_max
 
304
                         * array. No PStates in Device Tree: non-functional
 
305
                         * power/frequency management.
 
306
                         */
 
307
                        prlog(PR_ERR, "OCC: dt_core_max alloc failure\n");
 
308
                        goto out_free_vcs;
 
309
                }
 
310
 
 
311
                for (i = 0; i < nr_cores; i++)
 
312
                        dt_core_max[i] = occ_data->core_max[i];
 
313
        }
 
314
 
 
315
        for (i = 0, j = 0; i < MAX_PSTATES && j < nr_pstates; i++) {
 
316
                if (occ_data->pstates[i].id > pmax ||
 
317
                    occ_data->pstates[i].id < occ_data->pstate_min)
 
318
                        continue;
 
319
                dt_id[j] = occ_data->pstates[i].id;
 
320
                dt_freq[j] = occ_data->pstates[i].freq_khz / 1000;
 
321
                dt_vdd[j] = occ_data->pstates[i].vdd;
 
322
                dt_vcs[j] = occ_data->pstates[i].vcs;
 
323
                j++;
 
324
        }
 
325
 
 
326
        /* Add the device-tree entries */
 
327
        dt_add_property(power_mgt, "ibm,pstate-ids", dt_id,
 
328
                        nr_pstates * sizeof(u32));
 
329
        dt_add_property(power_mgt, "ibm,pstate-frequencies-mhz", dt_freq,
 
330
                        nr_pstates * sizeof(u32));
 
331
        dt_add_property(power_mgt, "ibm,pstate-vdds", dt_vdd, nr_pstates);
 
332
        dt_add_property(power_mgt, "ibm,pstate-vcss", dt_vcs, nr_pstates);
 
333
        dt_add_property_cells(power_mgt, "ibm,pstate-min", occ_data->pstate_min);
 
334
        dt_add_property_cells(power_mgt, "ibm,pstate-nominal", occ_data->pstate_nom);
 
335
        dt_add_property_cells(power_mgt, "ibm,pstate-max", pmax);
 
336
 
 
337
        if (ultra_turbo_en) {
 
338
                dt_add_property_cells(power_mgt, "ibm,pstate-turbo",
 
339
                                      occ_data->pstate_turbo);
 
340
                dt_add_property_cells(power_mgt, "ibm,pstate-ultra-turbo",
 
341
                                      occ_data->pstate_ultra_turbo);
 
342
                dt_add_property(power_mgt, "ibm,pstate-core-max", dt_core_max,
 
343
                                nr_cores);
 
344
                free(dt_core_max);
 
345
        }
 
346
 
 
347
        /* Return pstate to set for each core */
 
348
        *pstate_nom = occ_data->pstate_nom;
 
349
        rc = true;
 
350
 
 
351
out_free_vcs:
 
352
        free(dt_vcs);
 
353
out_free_vdd:
 
354
        free(dt_vdd);
 
355
out_free_id:
 
356
        free(dt_id);
 
357
out_free_freq:
 
358
        free(dt_freq);
 
359
out:
 
360
        return rc;
 
361
}
 
362
 
 
363
/*
 
364
 * Prepare chip for pstate transitions
 
365
 */
 
366
 
 
367
static bool cpu_pstates_prepare_core(struct proc_chip *chip, struct cpu_thread *c, s8 pstate_nom)
 
368
{
 
369
        uint32_t core = pir_to_core_id(c->pir);
 
370
        uint64_t tmp, pstate;
 
371
        int rc;
 
372
 
 
373
        /*
 
374
         * Currently Fastsleep init clears EX_PM_SPR_OVERRIDE_EN.
 
375
         * Need to ensure only relevant bits are inited
 
376
         */
 
377
 
 
378
        /* Init PM GP1 for SCOM based PSTATE control to set nominal freq
 
379
         *
 
380
         * Use the OR SCOM to set the required bits in PM_GP1 register
 
381
         * since the OCC might be mainpulating the PM_GP1 register as well.
 
382
         */ 
 
383
        rc = xscom_write(chip->id, XSCOM_ADDR_P8_EX_SLAVE(core, EX_PM_SET_GP1),
 
384
                         EX_PM_SETUP_GP1_PM_SPR_OVERRIDE_EN);
 
385
        if (rc) {
 
386
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
387
                        "OCC: Failed to write PM_GP1 in pstates init\n");
 
388
                return false;
 
389
        }
 
390
 
 
391
        /* Set new pstate to core */
 
392
        rc = xscom_read(chip->id, XSCOM_ADDR_P8_EX_SLAVE(core, EX_PM_PPMCR), &tmp);
 
393
        if (rc) {
 
394
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
395
                        "OCC: Failed to read from OCC in pstates init\n");
 
396
                return false;
 
397
        }
 
398
        tmp = tmp & ~0xFFFF000000000000ULL;
 
399
        pstate = ((uint64_t) pstate_nom) & 0xFF;
 
400
        tmp = tmp | (pstate << 56) | (pstate << 48);
 
401
        rc = xscom_write(chip->id, XSCOM_ADDR_P8_EX_SLAVE(core, EX_PM_PPMCR), tmp);
 
402
        if (rc) {
 
403
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
404
                        "OCC: Failed to write PM_GP1 in pstates init\n");
 
405
                return false;
 
406
        }
 
407
        time_wait_ms(1); /* Wait for PState to change */
 
408
        /*
 
409
         * Init PM GP1 for SPR based PSTATE control.
 
410
         * Once OCC is active EX_PM_SETUP_GP1_DPLL_FREQ_OVERRIDE_EN will be
 
411
         * cleared by OCC.  Sapphire need not clear.
 
412
         * However wait for DVFS state machine to become idle after min->nominal
 
413
         * transition initiated above.  If not switch over to SPR control could fail.
 
414
         *
 
415
         * Use the AND SCOM to clear the required bits in PM_GP1 register
 
416
         * since the OCC might be mainpulating the PM_GP1 register as well.
 
417
         */
 
418
        tmp = ~EX_PM_SETUP_GP1_PM_SPR_OVERRIDE_EN;
 
419
        rc = xscom_write(chip->id, XSCOM_ADDR_P8_EX_SLAVE(core, EX_PM_CLEAR_GP1),
 
420
                        tmp);
 
421
        if (rc) {
 
422
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
423
                        "OCC: Failed to write PM_GP1 in pstates init\n");
 
424
                return false;
 
425
        }
 
426
 
 
427
        /* Just debug */
 
428
        rc = xscom_read(chip->id, XSCOM_ADDR_P8_EX_SLAVE(core, EX_PM_PPMSR), &tmp);
 
429
        if (rc) {
 
430
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
431
                        "OCC: Failed to read back setting from OCC"
 
432
                                 "in pstates init\n");
 
433
                return false;
 
434
        }
 
435
        prlog(PR_DEBUG, "OCC: Chip %x Core %x PPMSR %016llx\n",
 
436
              chip->id, core, tmp);
 
437
 
 
438
        /*
 
439
         * If PMSR is still in transition at this point due to PState change
 
440
         * initiated above, then the switchover to SPR may not work.
 
441
         * ToDo: Check for DVFS state machine idle before change.
 
442
         */
 
443
 
 
444
        return true;
 
445
}
 
446
 
 
447
static bool occ_opal_msg_outstanding = false;
 
448
static void occ_msg_consumed(void *data __unused)
 
449
{
 
450
        lock(&occ_lock);
 
451
        occ_opal_msg_outstanding = false;
 
452
        unlock(&occ_lock);
 
453
}
 
454
 
 
455
static void occ_throttle_poll(void *data __unused)
 
456
{
 
457
        struct proc_chip *chip;
 
458
        struct occ_pstate_table *occ_data;
 
459
        struct opal_occ_msg occ_msg;
 
460
        int rc;
 
461
 
 
462
        if (!try_lock(&occ_lock))
 
463
                return;
 
464
        if (occ_reset) {
 
465
                int inactive = 0;
 
466
 
 
467
                for_each_chip(chip) {
 
468
                        occ_data = chip_occ_data(chip);
 
469
                        if (occ_data->valid != 1) {
 
470
                                inactive = 1;
 
471
                                break;
 
472
                        }
 
473
                }
 
474
                if (!inactive) {
 
475
                        /*
 
476
                         * Queue OCC_THROTTLE with throttle status as 0 to
 
477
                         * indicate all OCCs are active after a reset.
 
478
                         */
 
479
                        occ_msg.type = cpu_to_be64(OCC_THROTTLE);
 
480
                        occ_msg.chip = 0;
 
481
                        occ_msg.throttle_status = 0;
 
482
                        rc = _opal_queue_msg(OPAL_MSG_OCC, NULL, NULL, 3,
 
483
                                             (uint64_t *)&occ_msg);
 
484
                        if (!rc)
 
485
                                occ_reset = false;
 
486
                }
 
487
        } else {
 
488
                if (occ_opal_msg_outstanding)
 
489
                        goto done;
 
490
                for_each_chip(chip) {
 
491
                        occ_data = chip_occ_data(chip);
 
492
                        if ((occ_data->valid == 1) &&
 
493
                            (chip->throttle != occ_data->throttle) &&
 
494
                            (occ_data->throttle <= OCC_MAX_THROTTLE_STATUS)) {
 
495
                                occ_msg.type = cpu_to_be64(OCC_THROTTLE);
 
496
                                occ_msg.chip = cpu_to_be64(chip->id);
 
497
                                occ_msg.throttle_status = cpu_to_be64(occ_data->throttle);
 
498
                                rc = _opal_queue_msg(OPAL_MSG_OCC, NULL,
 
499
                                                     occ_msg_consumed,
 
500
                                                     3, (uint64_t *)&occ_msg);
 
501
                                if (!rc) {
 
502
                                        chip->throttle = occ_data->throttle;
 
503
                                        occ_opal_msg_outstanding = true;
 
504
                                        break;
 
505
                                }
 
506
                        }
 
507
                }
 
508
        }
 
509
done:
 
510
        unlock(&occ_lock);
 
511
}
 
512
 
 
513
/* CPU-OCC PState init */
 
514
/* Called after OCC init on P8 */
 
515
void occ_pstates_init(void)
 
516
{
 
517
        struct proc_chip *chip;
 
518
        struct cpu_thread *c;
 
519
        s8 pstate_nom;
 
520
 
 
521
        /* OCC is P8 only */
 
522
        if (proc_gen != proc_gen_p8)
 
523
                return;
 
524
 
 
525
        chip = next_chip(NULL);
 
526
        if (!chip->homer_base) {
 
527
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
528
                        "OCC: No HOMER detected, assuming no pstates\n");
 
529
                return;
 
530
        }
 
531
 
 
532
        /* Wait for all OCC to boot up */
 
533
        if(!wait_for_all_occ_init()) {
 
534
                log_simple_error(&e_info(OPAL_RC_OCC_TIMEOUT),
 
535
                         "OCC: Initialization on all chips did not complete"
 
536
                         "(timed out)\n");
 
537
                return;
 
538
        }
 
539
 
 
540
        /*
 
541
         * Check boundary conditions and add device tree nodes
 
542
         * and return nominal pstate to set for the core
 
543
         */
 
544
        if (!add_cpu_pstate_properties(&pstate_nom)) {
 
545
                log_simple_error(&e_info(OPAL_RC_OCC_PSTATE_INIT),
 
546
                        "Skiping core cpufreq init due to OCC error\n");
 
547
                return;
 
548
        }
 
549
 
 
550
        /* Setup host based pstates and set nominal frequency */
 
551
        for_each_chip(chip) {
 
552
                for_each_available_core_in_chip(c, chip->id) {
 
553
                        cpu_pstates_prepare_core(chip, c, pstate_nom);
 
554
                }
 
555
        }
 
556
 
 
557
        /* Add opal_poller to poll OCC throttle status of each chip */
 
558
        for_each_chip(chip)
 
559
                chip->throttle = 0;
 
560
        opal_add_poller(occ_throttle_poll, NULL);
 
561
}
 
562
 
 
563
struct occ_load_req {
 
564
        u8 scope;
 
565
        u32 dbob_id;
 
566
        u32 seq_id;
 
567
        struct list_node link;
 
568
};
 
569
static LIST_HEAD(occ_load_req_list);
 
570
 
 
571
int find_master_and_slave_occ(uint64_t **master, uint64_t **slave,
 
572
                              int *nr_masters, int *nr_slaves)
 
573
{
 
574
        struct proc_chip *chip;
 
575
        int nr_chips = 0, i;
 
576
        uint64_t chipids[MAX_CHIPS];
 
577
 
 
578
        for_each_chip(chip) {
 
579
                chipids[nr_chips++] = chip->id;
 
580
        }
 
581
 
 
582
        chip = next_chip(NULL);
 
583
        /*
 
584
         * Proc0 is the master OCC for Tuleta/Alpine boxes.
 
585
         * Hostboot expects the pair of chips for MURANO, so pass the sibling
 
586
         * chip id along with proc0 to hostboot.
 
587
         */
 
588
        *nr_masters = (chip->type == PROC_CHIP_P8_MURANO) ? 2 : 1;
 
589
        *master = (uint64_t *)malloc(*nr_masters * sizeof(uint64_t));
 
590
 
 
591
        if (!*master) {
 
592
                printf("OCC: master array alloc failure\n");
 
593
                return -ENOMEM;
 
594
        }
 
595
 
 
596
        if (nr_chips - *nr_masters > 0) {
 
597
                *nr_slaves = nr_chips - *nr_masters;
 
598
                *slave = (uint64_t *)malloc(*nr_slaves * sizeof(uint64_t));
 
599
                if (!*slave) {
 
600
                        printf("OCC: slave array alloc failure\n");
 
601
                        return -ENOMEM;
 
602
                }
 
603
        }
 
604
 
 
605
        for (i = 0; i < nr_chips; i++) {
 
606
                if (i < *nr_masters) {
 
607
                        *(*master + i) = chipids[i];
 
608
                        continue;
 
609
                }
 
610
                *(*slave + i - *nr_masters) = chipids[i];
 
611
        }
 
612
        return 0;
 
613
}
 
614
 
 
615
static void occ_queue_load(u8 scope, u32 dbob_id, u32 seq_id)
 
616
{
 
617
        struct occ_load_req *occ_req;
 
618
 
 
619
        occ_req = zalloc(sizeof(struct occ_load_req));
 
620
        if (!occ_req) {
 
621
                /**
 
622
                 * @fwts-label OCCload_reqENOMEM
 
623
                 * @fwts-advice ENOMEM while allocating OCC load message.
 
624
                 * OCCs not started, consequently no power/frequency scaling
 
625
                 * will be functional.
 
626
                 */
 
627
                prlog(PR_ERR, "OCC: Could not allocate occ_load_req\n");
 
628
                return;
 
629
        }
 
630
 
 
631
        occ_req->scope = scope;
 
632
        occ_req->dbob_id = dbob_id;
 
633
        occ_req->seq_id = seq_id;
 
634
        list_add_tail(&occ_load_req_list, &occ_req->link);
 
635
}
 
636
 
 
637
static void __occ_do_load(u8 scope, u32 dbob_id __unused, u32 seq_id)
 
638
{
 
639
        struct fsp_msg *stat;
 
640
        int rc = -ENOMEM;
 
641
        int status_word = 0;
 
642
        struct proc_chip *chip = next_chip(NULL);
 
643
 
 
644
        /* Call HBRT... */
 
645
        rc = host_services_occ_load();
 
646
 
 
647
        /* Handle fallback to preload */
 
648
        if (rc == -ENOENT && chip->homer_base) {
 
649
                prlog(PR_INFO, "OCC: Load: Fallback to preloaded image\n");
 
650
                rc = 0;
 
651
        } else if (!rc) {
 
652
                struct opal_occ_msg occ_msg = { CPU_TO_BE64(OCC_LOAD), 0, 0 };
 
653
 
 
654
                rc = _opal_queue_msg(OPAL_MSG_OCC, NULL, NULL, 3,
 
655
                                     (uint64_t *)&occ_msg);
 
656
                if (rc)
 
657
                        prlog(PR_INFO, "OCC: Failed to queue message %d\n",
 
658
                              OCC_LOAD);
 
659
 
 
660
                /* Success, start OCC */
 
661
                rc = host_services_occ_start();
 
662
        }
 
663
        if (rc) {
 
664
                /* If either of hostservices call fail, send fail to FSP */
 
665
                /* Find a chip ID to send failure */
 
666
                for_each_chip(chip) {
 
667
                        if (scope == 0x01 && dbob_id != chip->dbob_id)
 
668
                                continue;
 
669
                        status_word = 0xB500 | (chip->pcid & 0xff);
 
670
                        break;
 
671
                }
 
672
                log_simple_error(&e_info(OPAL_RC_OCC_LOAD),
 
673
                        "OCC: Error %d in load/start OCC\n", rc);
 
674
        }
 
675
 
 
676
        /* Send a single response for all chips */
 
677
        stat = fsp_mkmsg(FSP_CMD_LOAD_OCC_STAT, 2, status_word, seq_id);
 
678
        if (stat)
 
679
                rc = fsp_queue_msg(stat, fsp_freemsg);
 
680
        if (rc) {
 
681
                log_simple_error(&e_info(OPAL_RC_OCC_LOAD),
 
682
                        "OCC: Error %d queueing FSP OCC LOAD STATUS msg", rc);
 
683
                fsp_freemsg(stat);
 
684
        }
 
685
}
 
686
 
 
687
void occ_poke_load_queue(void)
 
688
{
 
689
        struct occ_load_req *occ_req, *next;
 
690
 
 
691
        if (list_empty(&occ_load_req_list))
 
692
                return;
 
693
 
 
694
        list_for_each_safe(&occ_load_req_list, occ_req, next, link) {
 
695
                __occ_do_load(occ_req->scope, occ_req->dbob_id,
 
696
                                occ_req->seq_id);
 
697
                list_del(&occ_req->link);
 
698
                free(occ_req);
 
699
        }
 
700
}
 
701
 
 
702
static void occ_do_load(u8 scope, u32 dbob_id __unused, u32 seq_id)
 
703
{
 
704
        struct fsp_msg *rsp;
 
705
        int rc = -ENOMEM;
 
706
        u8 err = 0;
 
707
 
 
708
        if (scope != 0x01 && scope != 0x02) {
 
709
                /**
 
710
                 * @fwts-label OCCLoadInvalidScope
 
711
                 * @fwts-advice Invalid request for loading OCCs. Power and
 
712
                 * frequency management not functional
 
713
                 */
 
714
                prlog(PR_ERR, "OCC: Load message with invalid scope 0x%x\n",
 
715
                      scope);
 
716
                err = 0x22;
 
717
        }
 
718
 
 
719
        /* First queue up an OK response to the load message itself */
 
720
        rsp = fsp_mkmsg(FSP_RSP_LOAD_OCC | err, 0);
 
721
        if (rsp)
 
722
                rc = fsp_queue_msg(rsp, fsp_freemsg);
 
723
        if (rc) {
 
724
                log_simple_error(&e_info(OPAL_RC_OCC_LOAD),
 
725
                        "OCC: Error %d queueing FSP OCC LOAD reply\n", rc);
 
726
                fsp_freemsg(rsp);
 
727
                return;
 
728
        }
 
729
 
 
730
        if (err)
 
731
                return;
 
732
 
 
733
        /*
 
734
         * Check if hostservices lid caching is complete. If not, queue
 
735
         * the load request.
 
736
         */
 
737
        if (!hservices_lid_preload_complete()) {
 
738
                occ_queue_load(scope, dbob_id, seq_id);
 
739
                return;
 
740
        }
 
741
 
 
742
        __occ_do_load(scope, dbob_id, seq_id);
 
743
}
 
744
 
 
745
int occ_msg_queue_occ_reset(void)
 
746
{
 
747
        struct opal_occ_msg occ_msg = { OCC_RESET, 0, 0 };
 
748
        struct proc_chip *chip;
 
749
        int rc;
 
750
 
 
751
        lock(&occ_lock);
 
752
        rc = _opal_queue_msg(OPAL_MSG_OCC, NULL, NULL, 3,
 
753
                             (uint64_t *)&occ_msg);
 
754
        if (rc) {
 
755
                prlog(PR_INFO, "OCC: Failed to queue OCC_RESET message\n");
 
756
                goto out;
 
757
        }
 
758
        /*
 
759
         * Set 'valid' byte of chip_occ_data to 0 since OCC
 
760
         * may not clear this byte on a reset.
 
761
         * OCC will set the 'valid' byte to 1 when it becomes
 
762
         * active again.
 
763
         */
 
764
        for_each_chip(chip) {
 
765
                struct occ_pstate_table *occ_data;
 
766
 
 
767
                occ_data = chip_occ_data(chip);
 
768
                occ_data->valid = 0;
 
769
                chip->throttle = 0;
 
770
        }
 
771
        occ_reset = true;
 
772
out:
 
773
        unlock(&occ_lock);
 
774
        return rc;
 
775
}
 
776
 
 
777
static void occ_do_reset(u8 scope, u32 dbob_id, u32 seq_id)
 
778
{
 
779
        struct fsp_msg *rsp, *stat;
 
780
        struct proc_chip *chip = next_chip(NULL);
 
781
        int rc = -ENOMEM;
 
782
        u8 err = 0;
 
783
 
 
784
        /* Check arguments */
 
785
        if (scope != 0x01 && scope != 0x02) {
 
786
                /**
 
787
                 * @fwts-label OCCResetInvalidScope
 
788
                 * @fwts-advice Invalid request for resetting OCCs. Power and
 
789
                 * frequency management not functional
 
790
                 */
 
791
                prlog(PR_ERR, "OCC: Reset message with invalid scope 0x%x\n",
 
792
                      scope);
 
793
                err = 0x22;
 
794
        }
 
795
 
 
796
        /* First queue up an OK response to the reset message itself */
 
797
        rsp = fsp_mkmsg(FSP_RSP_RESET_OCC | err, 0);
 
798
        if (rsp)
 
799
                rc = fsp_queue_msg(rsp, fsp_freemsg);
 
800
        if (rc) {
 
801
                fsp_freemsg(rsp);
 
802
                log_simple_error(&e_info(OPAL_RC_OCC_RESET),
 
803
                        "OCC: Error %d queueing FSP OCC RESET reply\n", rc);
 
804
                return;
 
805
        }
 
806
 
 
807
        /* If we had an error, return */
 
808
        if (err)
 
809
                return;
 
810
 
 
811
        /*
 
812
         * Call HBRT to stop OCC and leave it stopped.  FSP will send load/start
 
813
         * request subsequently.  Also after few runtime restarts (currently 3),
 
814
         * FSP will request OCC to left in stopped state.
 
815
         */
 
816
 
 
817
        rc = host_services_occ_stop();
 
818
 
 
819
        /* Handle fallback to preload */
 
820
        if (rc == -ENOENT && chip->homer_base) {
 
821
                prlog(PR_INFO, "OCC: Reset: Fallback to preloaded image\n");
 
822
                rc = 0;
 
823
        }
 
824
        if (!rc) {
 
825
                /* Send a single success response for all chips */
 
826
                stat = fsp_mkmsg(FSP_CMD_RESET_OCC_STAT, 2, 0, seq_id);
 
827
                if (stat)
 
828
                        rc = fsp_queue_msg(stat, fsp_freemsg);
 
829
                if (rc) {
 
830
                        fsp_freemsg(stat);
 
831
                        log_simple_error(&e_info(OPAL_RC_OCC_RESET),
 
832
                                "OCC: Error %d queueing FSP OCC RESET"
 
833
                                        " STATUS message\n", rc);
 
834
                }
 
835
                occ_msg_queue_occ_reset();
 
836
        } else {
 
837
 
 
838
                /*
 
839
                 * Then send a matching OCC Reset Status message with an 0xFE
 
840
                 * (fail) response code as well to the first matching chip
 
841
                 */
 
842
                for_each_chip(chip) {
 
843
                        if (scope == 0x01 && dbob_id != chip->dbob_id)
 
844
                                continue;
 
845
                        rc = -ENOMEM;
 
846
                        stat = fsp_mkmsg(FSP_CMD_RESET_OCC_STAT, 2,
 
847
                                         0xfe00 | (chip->pcid & 0xff), seq_id);
 
848
                        if (stat)
 
849
                                rc = fsp_queue_msg(stat, fsp_freemsg);
 
850
                        if (rc) {
 
851
                                fsp_freemsg(stat);
 
852
                                log_simple_error(&e_info(OPAL_RC_OCC_RESET),
 
853
                                        "OCC: Error %d queueing FSP OCC RESET"
 
854
                                                " STATUS message\n", rc);
 
855
                        }
 
856
                        break;
 
857
                }
 
858
        }
 
859
}
 
860
 
 
861
#define PV_OCC_GP0              0x01000000
 
862
#define PV_OCC_GP0_AND          0x01000004
 
863
#define PV_OCC_GP0_OR           0x01000005
 
864
#define PV_OCC_GP0_PNOR_OWNER   PPC_BIT(18) /* 1 = OCC / Host, 0 = BMC */
 
865
 
 
866
static void occ_pnor_set_one_owner(uint32_t chip_id, enum pnor_owner owner)
 
867
{
 
868
        uint64_t reg, mask;
 
869
 
 
870
        if (owner == PNOR_OWNER_HOST) {
 
871
                reg = PV_OCC_GP0_OR;
 
872
                mask = PV_OCC_GP0_PNOR_OWNER;
 
873
        } else {
 
874
                reg = PV_OCC_GP0_AND;
 
875
                mask = ~PV_OCC_GP0_PNOR_OWNER;
 
876
        }
 
877
 
 
878
        xscom_write(chip_id, reg, mask);
 
879
}
 
880
 
 
881
void occ_pnor_set_owner(enum pnor_owner owner)
 
882
{
 
883
        struct proc_chip *chip;
 
884
 
 
885
        for_each_chip(chip)
 
886
                occ_pnor_set_one_owner(chip->id, owner);
 
887
}
 
888
 
 
889
static bool fsp_occ_msg(u32 cmd_sub_mod, struct fsp_msg *msg)
 
890
{
 
891
        u32 dbob_id, seq_id;
 
892
        u8 scope;
 
893
 
 
894
        switch (cmd_sub_mod) {
 
895
        case FSP_CMD_LOAD_OCC:
 
896
                /*
 
897
                 * We get the "Load OCC" command at boot. We don't currently
 
898
                 * support loading it ourselves (we don't have the procedures,
 
899
                 * they will come with Host Services). For now HostBoot will
 
900
                 * have loaded a OCC firmware for us, but we still need to
 
901
                 * be nice and respond to OCC.
 
902
                 */
 
903
                scope = msg->data.bytes[3];
 
904
                dbob_id = msg->data.words[1];
 
905
                seq_id = msg->data.words[2];
 
906
                prlog(PR_INFO, "OCC: Got OCC Load message, scope=0x%x"
 
907
                      " dbob=0x%x seq=0x%x\n", scope, dbob_id, seq_id);
 
908
                occ_do_load(scope, dbob_id, seq_id);
 
909
                return true;
 
910
 
 
911
        case FSP_CMD_RESET_OCC:
 
912
                /*
 
913
                 * We shouldn't be getting this one, but if we do, we have
 
914
                 * to reply something sensible or the FSP will get upset
 
915
                 */
 
916
                scope = msg->data.bytes[3];
 
917
                dbob_id = msg->data.words[1];
 
918
                seq_id = msg->data.words[2];
 
919
                prlog(PR_INFO, "OCC: Got OCC Reset message, scope=0x%x"
 
920
                      " dbob=0x%x seq=0x%x\n", scope, dbob_id, seq_id);
 
921
                occ_do_reset(scope, dbob_id, seq_id);
 
922
                return true;
 
923
        }
 
924
        return false;
 
925
}
 
926
 
 
927
static struct fsp_client fsp_occ_client = {
 
928
        .message = fsp_occ_msg,
 
929
};
 
930
 
 
931
#define OCB_OCI_OCCMISC         0x6a020
 
932
#define OCB_OCI_OCCMISC_AND     0x6a021
 
933
#define OCB_OCI_OCCMISC_OR      0x6a022
 
934
#define OCB_OCI_OCIMISC_IRQ             PPC_BIT(0)
 
935
#define OCB_OCI_OCIMISC_IRQ_TMGT        PPC_BIT(1)
 
936
#define OCB_OCI_OCIMISC_IRQ_SLW_TMR     PPC_BIT(14)
 
937
#define OCB_OCI_OCIMISC_IRQ_OPAL_DUMMY  PPC_BIT(15)
 
938
#define OCB_OCI_OCIMISC_MASK            (OCB_OCI_OCIMISC_IRQ_TMGT | \
 
939
                                         OCB_OCI_OCIMISC_IRQ_OPAL_DUMMY | \
 
940
                                         OCB_OCI_OCIMISC_IRQ_SLW_TMR)
 
941
 
 
942
void occ_send_dummy_interrupt(void)
 
943
{
 
944
        struct psi *psi;
 
945
        struct proc_chip *chip = get_chip(this_cpu()->chip_id);
 
946
 
 
947
        /* Emulators and P7 doesn't do this */
 
948
        if (proc_gen != proc_gen_p8 || chip_quirk(QUIRK_NO_OCC_IRQ))
 
949
                return;
 
950
 
 
951
        /* Find a functional PSI. This ensures an interrupt even if
 
952
         * the psihb on the current chip is not configured */
 
953
        if (chip->psi)
 
954
                psi = chip->psi;
 
955
        else
 
956
                psi = psi_find_functional_chip();
 
957
 
 
958
        if (!psi) {
 
959
                prlog_once(PR_WARNING, "PSI: no functional PSI HB found, "
 
960
                                       "no self interrupts delivered\n");
 
961
                return;
 
962
        }
 
963
 
 
964
        xscom_write(psi->chip_id, OCB_OCI_OCCMISC_OR,
 
965
                    OCB_OCI_OCIMISC_IRQ | OCB_OCI_OCIMISC_IRQ_OPAL_DUMMY);
 
966
}
 
967
 
 
968
void occ_interrupt(uint32_t chip_id)
 
969
{
 
970
        uint64_t ireg;
 
971
        int64_t rc;
 
972
 
 
973
        /* The OCC interrupt is used to mux up to 15 different sources */
 
974
        rc = xscom_read(chip_id, OCB_OCI_OCCMISC, &ireg);
 
975
        if (rc) {
 
976
                prerror("OCC: Failed to read interrupt status !\n");
 
977
                /* Should we mask it in the XIVR ? */
 
978
                return;
 
979
        }
 
980
        prlog(PR_TRACE, "OCC: IRQ received: %04llx\n", ireg >> 48);
 
981
 
 
982
        /* Clear the bits */
 
983
        xscom_write(chip_id, OCB_OCI_OCCMISC_AND, ~ireg);
 
984
 
 
985
        /* Dispatch */
 
986
        if (ireg & OCB_OCI_OCIMISC_IRQ_TMGT)
 
987
                prd_tmgt_interrupt(chip_id);
 
988
        if (ireg & OCB_OCI_OCIMISC_IRQ_SLW_TMR)
 
989
                check_timers(true);
 
990
 
 
991
        /* We may have masked-out OCB_OCI_OCIMISC_IRQ in the previous
 
992
         * OCCMISC_AND write. Check if there are any new source bits set,
 
993
         * and trigger another interrupt if so.
 
994
         */
 
995
        rc = xscom_read(chip_id, OCB_OCI_OCCMISC, &ireg);
 
996
        if (!rc && (ireg & OCB_OCI_OCIMISC_MASK))
 
997
                xscom_write(chip_id, OCB_OCI_OCCMISC_OR, OCB_OCI_OCIMISC_IRQ);
 
998
}
 
999
 
 
1000
void occ_fsp_init(void)
 
1001
{
 
1002
        /* OCC is P8 only */
 
1003
        if (proc_gen != proc_gen_p8)
 
1004
                return;
 
1005
 
 
1006
        /* If we have an FSP, register for notifications */
 
1007
        if (fsp_present())
 
1008
                fsp_register_client(&fsp_occ_client, FSP_MCLASS_OCC);
 
1009
}
 
1010
 
 
1011