]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/powerpc/platforms/powernv/idle.c
powerpc/powernv: Move CPU-Offline idle state invocation from smp.c to idle.c
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / platforms / powernv / idle.c
CommitLineData
d405a98c
SP
1/*
2 * PowerNV cpuidle code
3 *
4 * Copyright 2015 IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/types.h>
13#include <linux/mm.h>
14#include <linux/slab.h>
15#include <linux/of.h>
5703d2f4
SP
16#include <linux/device.h>
17#include <linux/cpu.h>
d405a98c
SP
18
19#include <asm/firmware.h>
4bece972 20#include <asm/machdep.h>
d405a98c
SP
21#include <asm/opal.h>
22#include <asm/cputhreads.h>
23#include <asm/cpuidle.h>
24#include <asm/code-patching.h>
25#include <asm/smp.h>
26
27#include "powernv.h"
28#include "subcore.h"
29
bcef83a0
SP
30/* Power ISA 3.0 allows for stop states 0x0 - 0xF */
31#define MAX_STOP_STATE 0xF
32
d405a98c
SP
33static u32 supported_cpuidle_states;
34
bcef83a0 35static int pnv_save_sprs_for_deep_states(void)
d405a98c
SP
36{
37 int cpu;
38 int rc;
39
40 /*
446957ba 41 * hid0, hid1, hid4, hid5, hmeer and lpcr values are symmetric across
d405a98c 42 * all cpus at boot. Get these reg values of current cpu and use the
446957ba 43 * same across all cpus.
d405a98c
SP
44 */
45 uint64_t lpcr_val = mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1;
46 uint64_t hid0_val = mfspr(SPRN_HID0);
47 uint64_t hid1_val = mfspr(SPRN_HID1);
48 uint64_t hid4_val = mfspr(SPRN_HID4);
49 uint64_t hid5_val = mfspr(SPRN_HID5);
50 uint64_t hmeer_val = mfspr(SPRN_HMEER);
51
52 for_each_possible_cpu(cpu) {
53 uint64_t pir = get_hard_smp_processor_id(cpu);
54 uint64_t hsprg0_val = (uint64_t)&paca[cpu];
55
bcef83a0
SP
56 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
57 /*
58 * HSPRG0 is used to store the cpu's pointer to paca.
59 * Hence last 3 bits are guaranteed to be 0. Program
60 * slw to restore HSPRG0 with 63rd bit set, so that
61 * when a thread wakes up at 0x100 we can use this bit
62 * to distinguish between fastsleep and deep winkle.
63 * This is not necessary with stop/psscr since PLS
64 * field of psscr indicates which state we are waking
65 * up from.
66 */
67 hsprg0_val |= 1;
68 }
d405a98c
SP
69 rc = opal_slw_set_reg(pir, SPRN_HSPRG0, hsprg0_val);
70 if (rc != 0)
71 return rc;
72
73 rc = opal_slw_set_reg(pir, SPRN_LPCR, lpcr_val);
74 if (rc != 0)
75 return rc;
76
77 /* HIDs are per core registers */
78 if (cpu_thread_in_core(cpu) == 0) {
79
80 rc = opal_slw_set_reg(pir, SPRN_HMEER, hmeer_val);
81 if (rc != 0)
82 return rc;
83
84 rc = opal_slw_set_reg(pir, SPRN_HID0, hid0_val);
85 if (rc != 0)
86 return rc;
87
88 rc = opal_slw_set_reg(pir, SPRN_HID1, hid1_val);
89 if (rc != 0)
90 return rc;
91
92 rc = opal_slw_set_reg(pir, SPRN_HID4, hid4_val);
93 if (rc != 0)
94 return rc;
95
96 rc = opal_slw_set_reg(pir, SPRN_HID5, hid5_val);
97 if (rc != 0)
98 return rc;
99 }
100 }
101
102 return 0;
103}
104
105static void pnv_alloc_idle_core_states(void)
106{
107 int i, j;
108 int nr_cores = cpu_nr_cores();
109 u32 *core_idle_state;
110
111 /*
112 * core_idle_state - First 8 bits track the idle state of each thread
113 * of the core. The 8th bit is the lock bit. Initially all thread bits
114 * are set. They are cleared when the thread enters deep idle state
115 * like sleep and winkle. Initially the lock bit is cleared.
116 * The lock bit has 2 purposes
117 * a. While the first thread is restoring core state, it prevents
118 * other threads in the core from switching to process context.
119 * b. While the last thread in the core is saving the core state, it
120 * prevents a different thread from waking up.
121 */
122 for (i = 0; i < nr_cores; i++) {
123 int first_cpu = i * threads_per_core;
124 int node = cpu_to_node(first_cpu);
125
126 core_idle_state = kmalloc_node(sizeof(u32), GFP_KERNEL, node);
127 *core_idle_state = PNV_CORE_IDLE_THREAD_BITS;
128
129 for (j = 0; j < threads_per_core; j++) {
130 int cpu = first_cpu + j;
131
132 paca[cpu].core_idle_state_ptr = core_idle_state;
133 paca[cpu].thread_idle_state = PNV_THREAD_RUNNING;
134 paca[cpu].thread_mask = 1 << j;
135 }
136 }
137
138 update_subcore_sibling_mask();
139
bcef83a0
SP
140 if (supported_cpuidle_states & OPAL_PM_LOSE_FULL_CONTEXT)
141 pnv_save_sprs_for_deep_states();
d405a98c
SP
142}
143
144u32 pnv_get_supported_cpuidle_states(void)
145{
146 return supported_cpuidle_states;
147}
148EXPORT_SYMBOL_GPL(pnv_get_supported_cpuidle_states);
149
5703d2f4
SP
150
151static void pnv_fastsleep_workaround_apply(void *info)
152
153{
154 int rc;
155 int *err = info;
156
157 rc = opal_config_cpu_idle_state(OPAL_CONFIG_IDLE_FASTSLEEP,
158 OPAL_CONFIG_IDLE_APPLY);
159 if (rc)
160 *err = 1;
161}
162
163/*
164 * Used to store fastsleep workaround state
165 * 0 - Workaround applied/undone at fastsleep entry/exit path (Default)
166 * 1 - Workaround applied once, never undone.
167 */
168static u8 fastsleep_workaround_applyonce;
169
170static ssize_t show_fastsleep_workaround_applyonce(struct device *dev,
171 struct device_attribute *attr, char *buf)
172{
173 return sprintf(buf, "%u\n", fastsleep_workaround_applyonce);
174}
175
176static ssize_t store_fastsleep_workaround_applyonce(struct device *dev,
177 struct device_attribute *attr, const char *buf,
178 size_t count)
179{
180 cpumask_t primary_thread_mask;
181 int err;
182 u8 val;
183
184 if (kstrtou8(buf, 0, &val) || val != 1)
185 return -EINVAL;
186
187 if (fastsleep_workaround_applyonce == 1)
188 return count;
189
190 /*
191 * fastsleep_workaround_applyonce = 1 implies
192 * fastsleep workaround needs to be left in 'applied' state on all
193 * the cores. Do this by-
194 * 1. Patching out the call to 'undo' workaround in fastsleep exit path
446957ba 195 * 2. Sending ipi to all the cores which have at least one online thread
5703d2f4
SP
196 * 3. Patching out the call to 'apply' workaround in fastsleep entry
197 * path
198 * There is no need to send ipi to cores which have all threads
199 * offlined, as last thread of the core entering fastsleep or deeper
200 * state would have applied workaround.
201 */
202 err = patch_instruction(
203 (unsigned int *)pnv_fastsleep_workaround_at_exit,
204 PPC_INST_NOP);
205 if (err) {
206 pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_exit");
207 goto fail;
208 }
209
210 get_online_cpus();
211 primary_thread_mask = cpu_online_cores_map();
212 on_each_cpu_mask(&primary_thread_mask,
213 pnv_fastsleep_workaround_apply,
214 &err, 1);
215 put_online_cpus();
216 if (err) {
217 pr_err("fastsleep_workaround_applyonce change failed while running pnv_fastsleep_workaround_apply");
218 goto fail;
219 }
220
221 err = patch_instruction(
222 (unsigned int *)pnv_fastsleep_workaround_at_entry,
223 PPC_INST_NOP);
224 if (err) {
225 pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_entry");
226 goto fail;
227 }
228
229 fastsleep_workaround_applyonce = 1;
230
231 return count;
232fail:
233 return -EIO;
234}
235
236static DEVICE_ATTR(fastsleep_workaround_applyonce, 0600,
237 show_fastsleep_workaround_applyonce,
238 store_fastsleep_workaround_applyonce);
239
09206b60
GS
240/*
241 * The default stop state that will be used by ppc_md.power_save
242 * function on platforms that support stop instruction.
243 */
244u64 pnv_default_stop_val;
245u64 pnv_default_stop_mask;
bcef83a0
SP
246
247/*
248 * Used for ppc_md.power_save which needs a function with no parameters
249 */
250static void power9_idle(void)
d405a98c 251{
09206b60 252 power9_idle_stop(pnv_default_stop_val, pnv_default_stop_mask);
bcef83a0 253}
09206b60 254
bcef83a0
SP
255/*
256 * First deep stop state. Used to figure out when to save/restore
257 * hypervisor context.
258 */
259u64 pnv_first_deep_stop_state = MAX_STOP_STATE;
d405a98c 260
c0691f9d 261/*
09206b60
GS
262 * psscr value and mask of the deepest stop idle state.
263 * Used when a cpu is offlined.
c0691f9d 264 */
09206b60
GS
265u64 pnv_deepest_stop_psscr_val;
266u64 pnv_deepest_stop_psscr_mask;
c0691f9d 267
a7cd88da
GS
268/*
269 * pnv_cpu_offline: A function that puts the CPU into the deepest
270 * available platform idle state on a CPU-Offline.
271 */
272unsigned long pnv_cpu_offline(unsigned int cpu)
273{
274 unsigned long srr1;
275
276 u32 idle_states = pnv_get_supported_cpuidle_states();
277
278 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
279 srr1 = power9_idle_stop(pnv_deepest_stop_psscr_val,
280 pnv_deepest_stop_psscr_mask);
281 } else if (idle_states & OPAL_PM_WINKLE_ENABLED) {
282 srr1 = power7_winkle();
283 } else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
284 (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
285 srr1 = power7_sleep();
286 } else {
287 srr1 = power7_nap(1);
288 }
289
290 return srr1;
291}
292
bcef83a0
SP
293/*
294 * Power ISA 3.0 idle initialization.
295 *
296 * POWER ISA 3.0 defines a new SPR Processor stop Status and Control
297 * Register (PSSCR) to control idle behavior.
298 *
299 * PSSCR layout:
300 * ----------------------------------------------------------
301 * | PLS | /// | SD | ESL | EC | PSLL | /// | TR | MTL | RL |
302 * ----------------------------------------------------------
303 * 0 4 41 42 43 44 48 54 56 60
304 *
305 * PSSCR key fields:
306 * Bits 0:3 - Power-Saving Level Status (PLS). This field indicates the
307 * lowest power-saving state the thread entered since stop instruction was
308 * last executed.
309 *
310 * Bit 41 - Status Disable(SD)
311 * 0 - Shows PLS entries
312 * 1 - PLS entries are all 0
313 *
314 * Bit 42 - Enable State Loss
315 * 0 - No state is lost irrespective of other fields
316 * 1 - Allows state loss
317 *
318 * Bit 43 - Exit Criterion
319 * 0 - Exit from power-save mode on any interrupt
320 * 1 - Exit from power-save mode controlled by LPCR's PECE bits
321 *
322 * Bits 44:47 - Power-Saving Level Limit
323 * This limits the power-saving level that can be entered into.
324 *
325 * Bits 60:63 - Requested Level
326 * Used to specify which power-saving level must be entered on executing
327 * stop instruction
09206b60
GS
328 */
329
330int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags)
331{
332 int err = 0;
333
334 /*
335 * psscr_mask == 0xf indicates an older firmware.
336 * Set remaining fields of psscr to the default values.
337 * See NOTE above definition of PSSCR_HV_DEFAULT_VAL
338 */
339 if (*psscr_mask == 0xf) {
340 *psscr_val = *psscr_val | PSSCR_HV_DEFAULT_VAL;
341 *psscr_mask = PSSCR_HV_DEFAULT_MASK;
342 return err;
343 }
344
345 /*
346 * New firmware is expected to set the psscr_val bits correctly.
347 * Validate that the following invariants are correctly maintained by
348 * the new firmware.
349 * - ESL bit value matches the EC bit value.
350 * - ESL bit is set for all the deep stop states.
351 */
352 if (GET_PSSCR_ESL(*psscr_val) != GET_PSSCR_EC(*psscr_val)) {
353 err = ERR_EC_ESL_MISMATCH;
354 } else if ((flags & OPAL_PM_LOSE_FULL_CONTEXT) &&
355 GET_PSSCR_ESL(*psscr_val) == 0) {
356 err = ERR_DEEP_STATE_ESL_MISMATCH;
357 }
358
359 return err;
360}
361
362/*
363 * pnv_arch300_idle_init: Initializes the default idle state, first
364 * deep idle state and deepest idle state on
365 * ISA 3.0 CPUs.
bcef83a0
SP
366 *
367 * @np: /ibm,opal/power-mgt device node
368 * @flags: cpu-idle-state-flags array
369 * @dt_idle_states: Number of idle state entries
370 * Returns 0 on success
371 */
dd34c74c 372static int __init pnv_power9_idle_init(struct device_node *np, u32 *flags,
bcef83a0
SP
373 int dt_idle_states)
374{
375 u64 *psscr_val = NULL;
09206b60
GS
376 u64 *psscr_mask = NULL;
377 u32 *residency_ns = NULL;
378 u64 max_residency_ns = 0;
bcef83a0 379 int rc = 0, i;
09206b60 380 bool default_stop_found = false, deepest_stop_found = false;
d405a98c 381
09206b60
GS
382 psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val), GFP_KERNEL);
383 psscr_mask = kcalloc(dt_idle_states, sizeof(*psscr_mask), GFP_KERNEL);
384 residency_ns = kcalloc(dt_idle_states, sizeof(*residency_ns),
385 GFP_KERNEL);
386
387 if (!psscr_val || !psscr_mask || !residency_ns) {
bcef83a0 388 rc = -1;
d405a98c 389 goto out;
bcef83a0 390 }
09206b60 391
bcef83a0
SP
392 if (of_property_read_u64_array(np,
393 "ibm,cpu-idle-state-psscr",
394 psscr_val, dt_idle_states)) {
09206b60
GS
395 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
396 rc = -1;
397 goto out;
398 }
399
400 if (of_property_read_u64_array(np,
401 "ibm,cpu-idle-state-psscr-mask",
402 psscr_mask, dt_idle_states)) {
403 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr-mask in DT\n");
404 rc = -1;
405 goto out;
406 }
407
408 if (of_property_read_u32_array(np,
409 "ibm,cpu-idle-state-residency-ns",
410 residency_ns, dt_idle_states)) {
411 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-residency-ns in DT\n");
bcef83a0 412 rc = -1;
d405a98c 413 goto out;
bcef83a0 414 }
d405a98c 415
bcef83a0 416 /*
09206b60
GS
417 * Set pnv_first_deep_stop_state, pnv_deepest_stop_psscr_{val,mask},
418 * and the pnv_default_stop_{val,mask}.
419 *
c0691f9d
SP
420 * pnv_first_deep_stop_state should be set to the first stop
421 * level to cause hypervisor state loss.
09206b60
GS
422 *
423 * pnv_deepest_stop_{val,mask} should be set to values corresponding to
424 * the deepest stop state.
425 *
426 * pnv_default_stop_{val,mask} should be set to values corresponding to
427 * the shallowest (OPAL_PM_STOP_INST_FAST) loss-less stop state.
bcef83a0
SP
428 */
429 pnv_first_deep_stop_state = MAX_STOP_STATE;
430 for (i = 0; i < dt_idle_states; i++) {
09206b60 431 int err;
bcef83a0
SP
432 u64 psscr_rl = psscr_val[i] & PSSCR_RL_MASK;
433
434 if ((flags[i] & OPAL_PM_LOSE_FULL_CONTEXT) &&
435 (pnv_first_deep_stop_state > psscr_rl))
436 pnv_first_deep_stop_state = psscr_rl;
c0691f9d 437
09206b60
GS
438 err = validate_psscr_val_mask(&psscr_val[i], &psscr_mask[i],
439 flags[i]);
440 if (err) {
441 report_invalid_psscr_val(psscr_val[i], err);
442 continue;
443 }
444
445 if (max_residency_ns < residency_ns[i]) {
446 max_residency_ns = residency_ns[i];
447 pnv_deepest_stop_psscr_val = psscr_val[i];
448 pnv_deepest_stop_psscr_mask = psscr_mask[i];
449 deepest_stop_found = true;
450 }
451
452 if (!default_stop_found &&
453 (flags[i] & OPAL_PM_STOP_INST_FAST)) {
454 pnv_default_stop_val = psscr_val[i];
455 pnv_default_stop_mask = psscr_mask[i];
456 default_stop_found = true;
457 }
458 }
459
460 if (!default_stop_found) {
461 pnv_default_stop_val = PSSCR_HV_DEFAULT_VAL;
462 pnv_default_stop_mask = PSSCR_HV_DEFAULT_MASK;
463 pr_warn("Setting default stop psscr val=0x%016llx,mask=0x%016llx\n",
464 pnv_default_stop_val, pnv_default_stop_mask);
465 }
466
467 if (!deepest_stop_found) {
468 pnv_deepest_stop_psscr_val = PSSCR_HV_DEFAULT_VAL;
469 pnv_deepest_stop_psscr_mask = PSSCR_HV_DEFAULT_MASK;
470 pr_warn("Setting default stop psscr val=0x%016llx,mask=0x%016llx\n",
471 pnv_deepest_stop_psscr_val,
472 pnv_deepest_stop_psscr_mask);
bcef83a0
SP
473 }
474
475out:
476 kfree(psscr_val);
09206b60
GS
477 kfree(psscr_mask);
478 kfree(residency_ns);
bcef83a0
SP
479 return rc;
480}
481
482/*
483 * Probe device tree for supported idle states
484 */
485static void __init pnv_probe_idle_states(void)
486{
487 struct device_node *np;
488 int dt_idle_states;
489 u32 *flags = NULL;
490 int i;
491
492 np = of_find_node_by_path("/ibm,opal/power-mgt");
493 if (!np) {
d405a98c
SP
494 pr_warn("opal: PowerMgmt Node not found\n");
495 goto out;
496 }
bcef83a0 497 dt_idle_states = of_property_count_u32_elems(np,
d405a98c
SP
498 "ibm,cpu-idle-state-flags");
499 if (dt_idle_states < 0) {
500 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
501 goto out;
502 }
503
bcef83a0
SP
504 flags = kcalloc(dt_idle_states, sizeof(*flags), GFP_KERNEL);
505
506 if (of_property_read_u32_array(np,
d405a98c
SP
507 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
508 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n");
bcef83a0
SP
509 goto out;
510 }
511
512 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
dd34c74c 513 if (pnv_power9_idle_init(np, flags, dt_idle_states))
bcef83a0 514 goto out;
d405a98c
SP
515 }
516
517 for (i = 0; i < dt_idle_states; i++)
518 supported_cpuidle_states |= flags[i];
519
bcef83a0
SP
520out:
521 kfree(flags);
522}
523static int __init pnv_init_idle_states(void)
524{
525
526 supported_cpuidle_states = 0;
527
528 if (cpuidle_disable != IDLE_NO_OVERRIDE)
529 goto out;
530
531 pnv_probe_idle_states();
532
d405a98c
SP
533 if (!(supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
534 patch_instruction(
535 (unsigned int *)pnv_fastsleep_workaround_at_entry,
536 PPC_INST_NOP);
537 patch_instruction(
538 (unsigned int *)pnv_fastsleep_workaround_at_exit,
539 PPC_INST_NOP);
5703d2f4
SP
540 } else {
541 /*
542 * OPAL_PM_SLEEP_ENABLED_ER1 is set. It indicates that
543 * workaround is needed to use fastsleep. Provide sysfs
544 * control to choose how this workaround has to be applied.
545 */
546 device_create_file(cpu_subsys.dev_root,
547 &dev_attr_fastsleep_workaround_applyonce);
d405a98c 548 }
5703d2f4 549
d405a98c 550 pnv_alloc_idle_core_states();
5593e303
SP
551
552 if (supported_cpuidle_states & OPAL_PM_NAP_ENABLED)
553 ppc_md.power_save = power7_idle;
bcef83a0
SP
554 else if (supported_cpuidle_states & OPAL_PM_STOP_INST_FAST)
555 ppc_md.power_save = power9_idle;
556
d405a98c
SP
557out:
558 return 0;
559}
4bece972 560machine_subsys_initcall(powernv, pnv_init_idle_states);