blob: 2abee070373fb3a8b757b8d3cb269e5d0b89dff6 [file] [log] [blame]
Shreyas B. Prabhud405a982015-04-20 10:32:57 +05301/*
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>
Shreyas B. Prabhu5703d2f2015-04-20 10:32:58 +053016#include <linux/device.h>
17#include <linux/cpu.h>
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053018
19#include <asm/firmware.h>
Michael Ellerman4bece972015-06-15 15:01:32 +100020#include <asm/machdep.h>
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053021#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>
Nicholas Piggin2201f992017-06-13 23:05:45 +100026#include <asm/runlatch.h>
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053027
28#include "powernv.h"
29#include "subcore.h"
30
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +053031/* Power ISA 3.0 allows for stop states 0x0 - 0xF */
32#define MAX_STOP_STATE 0xF
33
Akshay Adiga1e1601b2017-05-16 14:19:46 +053034#define P9_STOP_SPR_MSR 2000
35#define P9_STOP_SPR_PSSCR 855
36
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053037static u32 supported_cpuidle_states;
38
Akshay Adiga1e1601b2017-05-16 14:19:46 +053039/*
40 * The default stop state that will be used by ppc_md.power_save
41 * function on platforms that support stop instruction.
42 */
43static u64 pnv_default_stop_val;
44static u64 pnv_default_stop_mask;
45static bool default_stop_found;
46
47/*
48 * First deep stop state. Used to figure out when to save/restore
49 * hypervisor context.
50 */
51u64 pnv_first_deep_stop_state = MAX_STOP_STATE;
52
53/*
54 * psscr value and mask of the deepest stop idle state.
55 * Used when a cpu is offlined.
56 */
57static u64 pnv_deepest_stop_psscr_val;
58static u64 pnv_deepest_stop_psscr_mask;
59static bool deepest_stop_found;
60
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +053061static int pnv_save_sprs_for_deep_states(void)
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053062{
63 int cpu;
64 int rc;
65
66 /*
Adam Buchbinder446957b2016-02-24 10:51:11 -080067 * hid0, hid1, hid4, hid5, hmeer and lpcr values are symmetric across
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053068 * all cpus at boot. Get these reg values of current cpu and use the
Adam Buchbinder446957b2016-02-24 10:51:11 -080069 * same across all cpus.
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053070 */
71 uint64_t lpcr_val = mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1;
72 uint64_t hid0_val = mfspr(SPRN_HID0);
73 uint64_t hid1_val = mfspr(SPRN_HID1);
74 uint64_t hid4_val = mfspr(SPRN_HID4);
75 uint64_t hid5_val = mfspr(SPRN_HID5);
76 uint64_t hmeer_val = mfspr(SPRN_HMEER);
Akshay Adiga1e1601b2017-05-16 14:19:46 +053077 uint64_t msr_val = MSR_IDLE;
78 uint64_t psscr_val = pnv_deepest_stop_psscr_val;
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053079
80 for_each_possible_cpu(cpu) {
81 uint64_t pir = get_hard_smp_processor_id(cpu);
82 uint64_t hsprg0_val = (uint64_t)&paca[cpu];
83
Shreyas B. Prabhud405a982015-04-20 10:32:57 +053084 rc = opal_slw_set_reg(pir, SPRN_HSPRG0, hsprg0_val);
85 if (rc != 0)
86 return rc;
87
88 rc = opal_slw_set_reg(pir, SPRN_LPCR, lpcr_val);
89 if (rc != 0)
90 return rc;
91
Akshay Adiga1e1601b2017-05-16 14:19:46 +053092 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
93 rc = opal_slw_set_reg(pir, P9_STOP_SPR_MSR, msr_val);
94 if (rc)
95 return rc;
96
97 rc = opal_slw_set_reg(pir,
98 P9_STOP_SPR_PSSCR, psscr_val);
99
100 if (rc)
101 return rc;
102 }
103
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530104 /* HIDs are per core registers */
105 if (cpu_thread_in_core(cpu) == 0) {
106
107 rc = opal_slw_set_reg(pir, SPRN_HMEER, hmeer_val);
108 if (rc != 0)
109 return rc;
110
111 rc = opal_slw_set_reg(pir, SPRN_HID0, hid0_val);
112 if (rc != 0)
113 return rc;
114
Akshay Adiga1e1601b2017-05-16 14:19:46 +0530115 /* Only p8 needs to set extra HID regiters */
116 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530117
Akshay Adiga1e1601b2017-05-16 14:19:46 +0530118 rc = opal_slw_set_reg(pir, SPRN_HID1, hid1_val);
119 if (rc != 0)
120 return rc;
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530121
Akshay Adiga1e1601b2017-05-16 14:19:46 +0530122 rc = opal_slw_set_reg(pir, SPRN_HID4, hid4_val);
123 if (rc != 0)
124 return rc;
125
126 rc = opal_slw_set_reg(pir, SPRN_HID5, hid5_val);
127 if (rc != 0)
128 return rc;
129 }
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530130 }
131 }
132
133 return 0;
134}
135
136static void pnv_alloc_idle_core_states(void)
137{
138 int i, j;
139 int nr_cores = cpu_nr_cores();
140 u32 *core_idle_state;
141
142 /*
Gautham R. Shenoy5f221c3c2017-05-16 14:19:43 +0530143 * core_idle_state - The lower 8 bits track the idle state of
144 * each thread of the core.
145 *
146 * The most significant bit is the lock bit.
147 *
148 * Initially all the bits corresponding to threads_per_core
149 * are set. They are cleared when the thread enters deep idle
150 * state like sleep and winkle/stop.
151 *
152 * Initially the lock bit is cleared. The lock bit has 2
153 * purposes:
154 * a. While the first thread in the core waking up from
155 * idle is restoring core state, it prevents other
156 * threads in the core from switching to process
157 * context.
158 * b. While the last thread in the core is saving the
159 * core state, it prevents a different thread from
160 * waking up.
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530161 */
162 for (i = 0; i < nr_cores; i++) {
163 int first_cpu = i * threads_per_core;
164 int node = cpu_to_node(first_cpu);
Gautham R. Shenoy17ed4c82017-03-22 20:34:17 +0530165 size_t paca_ptr_array_size;
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530166
167 core_idle_state = kmalloc_node(sizeof(u32), GFP_KERNEL, node);
Gautham R. Shenoy5f221c3c2017-05-16 14:19:43 +0530168 *core_idle_state = (1 << threads_per_core) - 1;
Gautham R. Shenoy17ed4c82017-03-22 20:34:17 +0530169 paca_ptr_array_size = (threads_per_core *
170 sizeof(struct paca_struct *));
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530171
172 for (j = 0; j < threads_per_core; j++) {
173 int cpu = first_cpu + j;
174
175 paca[cpu].core_idle_state_ptr = core_idle_state;
176 paca[cpu].thread_idle_state = PNV_THREAD_RUNNING;
177 paca[cpu].thread_mask = 1 << j;
Gautham R. Shenoy17ed4c82017-03-22 20:34:17 +0530178 if (!cpu_has_feature(CPU_FTR_POWER9_DD1))
179 continue;
180 paca[cpu].thread_sibling_pacas =
181 kmalloc_node(paca_ptr_array_size,
182 GFP_KERNEL, node);
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530183 }
184 }
185
186 update_subcore_sibling_mask();
187
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530188 if (supported_cpuidle_states & OPAL_PM_LOSE_FULL_CONTEXT)
189 pnv_save_sprs_for_deep_states();
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530190}
191
192u32 pnv_get_supported_cpuidle_states(void)
193{
194 return supported_cpuidle_states;
195}
196EXPORT_SYMBOL_GPL(pnv_get_supported_cpuidle_states);
197
Shreyas B. Prabhu5703d2f2015-04-20 10:32:58 +0530198static void pnv_fastsleep_workaround_apply(void *info)
199
200{
201 int rc;
202 int *err = info;
203
204 rc = opal_config_cpu_idle_state(OPAL_CONFIG_IDLE_FASTSLEEP,
205 OPAL_CONFIG_IDLE_APPLY);
206 if (rc)
207 *err = 1;
208}
209
210/*
211 * Used to store fastsleep workaround state
212 * 0 - Workaround applied/undone at fastsleep entry/exit path (Default)
213 * 1 - Workaround applied once, never undone.
214 */
215static u8 fastsleep_workaround_applyonce;
216
217static ssize_t show_fastsleep_workaround_applyonce(struct device *dev,
218 struct device_attribute *attr, char *buf)
219{
220 return sprintf(buf, "%u\n", fastsleep_workaround_applyonce);
221}
222
223static ssize_t store_fastsleep_workaround_applyonce(struct device *dev,
224 struct device_attribute *attr, const char *buf,
225 size_t count)
226{
227 cpumask_t primary_thread_mask;
228 int err;
229 u8 val;
230
231 if (kstrtou8(buf, 0, &val) || val != 1)
232 return -EINVAL;
233
234 if (fastsleep_workaround_applyonce == 1)
235 return count;
236
237 /*
238 * fastsleep_workaround_applyonce = 1 implies
239 * fastsleep workaround needs to be left in 'applied' state on all
240 * the cores. Do this by-
241 * 1. Patching out the call to 'undo' workaround in fastsleep exit path
Adam Buchbinder446957b2016-02-24 10:51:11 -0800242 * 2. Sending ipi to all the cores which have at least one online thread
Shreyas B. Prabhu5703d2f2015-04-20 10:32:58 +0530243 * 3. Patching out the call to 'apply' workaround in fastsleep entry
244 * path
245 * There is no need to send ipi to cores which have all threads
246 * offlined, as last thread of the core entering fastsleep or deeper
247 * state would have applied workaround.
248 */
249 err = patch_instruction(
250 (unsigned int *)pnv_fastsleep_workaround_at_exit,
251 PPC_INST_NOP);
252 if (err) {
253 pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_exit");
254 goto fail;
255 }
256
257 get_online_cpus();
258 primary_thread_mask = cpu_online_cores_map();
259 on_each_cpu_mask(&primary_thread_mask,
260 pnv_fastsleep_workaround_apply,
261 &err, 1);
262 put_online_cpus();
263 if (err) {
264 pr_err("fastsleep_workaround_applyonce change failed while running pnv_fastsleep_workaround_apply");
265 goto fail;
266 }
267
268 err = patch_instruction(
269 (unsigned int *)pnv_fastsleep_workaround_at_entry,
270 PPC_INST_NOP);
271 if (err) {
272 pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_entry");
273 goto fail;
274 }
275
276 fastsleep_workaround_applyonce = 1;
277
278 return count;
279fail:
280 return -EIO;
281}
282
283static DEVICE_ATTR(fastsleep_workaround_applyonce, 0600,
284 show_fastsleep_workaround_applyonce,
285 store_fastsleep_workaround_applyonce);
286
Nicholas Piggin2201f992017-06-13 23:05:45 +1000287static unsigned long __power7_idle_type(unsigned long type)
288{
289 unsigned long srr1;
290
291 if (!prep_irq_for_idle_irqsoff())
292 return 0;
293
Nicholas Piggin40d24342017-06-13 23:05:57 +1000294 __ppc64_runlatch_off();
Nicholas Piggin2201f992017-06-13 23:05:45 +1000295 srr1 = power7_idle_insn(type);
Nicholas Piggin40d24342017-06-13 23:05:57 +1000296 __ppc64_runlatch_on();
Nicholas Piggin2201f992017-06-13 23:05:45 +1000297
298 fini_irq_for_idle_irqsoff();
299
300 return srr1;
301}
302
303void power7_idle_type(unsigned long type)
304{
Nicholas Piggin771d4302017-06-13 23:05:47 +1000305 unsigned long srr1;
306
307 srr1 = __power7_idle_type(type);
308 irq_set_pending_from_srr1(srr1);
Nicholas Piggin2201f992017-06-13 23:05:45 +1000309}
310
311void power7_idle(void)
312{
313 if (!powersave_nap)
314 return;
315
316 power7_idle_type(PNV_THREAD_NAP);
317}
318
319static unsigned long __power9_idle_type(unsigned long stop_psscr_val,
320 unsigned long stop_psscr_mask)
321{
322 unsigned long psscr;
323 unsigned long srr1;
324
325 if (!prep_irq_for_idle_irqsoff())
326 return 0;
327
328 psscr = mfspr(SPRN_PSSCR);
329 psscr = (psscr & ~stop_psscr_mask) | stop_psscr_val;
330
Nicholas Piggin40d24342017-06-13 23:05:57 +1000331 __ppc64_runlatch_off();
Nicholas Piggin2201f992017-06-13 23:05:45 +1000332 srr1 = power9_idle_stop(psscr);
Nicholas Piggin40d24342017-06-13 23:05:57 +1000333 __ppc64_runlatch_on();
Nicholas Piggin2201f992017-06-13 23:05:45 +1000334
335 fini_irq_for_idle_irqsoff();
336
337 return srr1;
338}
339
340void power9_idle_type(unsigned long stop_psscr_val,
341 unsigned long stop_psscr_mask)
342{
Nicholas Piggin771d4302017-06-13 23:05:47 +1000343 unsigned long srr1;
344
345 srr1 = __power9_idle_type(stop_psscr_val, stop_psscr_mask);
346 irq_set_pending_from_srr1(srr1);
Nicholas Piggin2201f992017-06-13 23:05:45 +1000347}
348
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530349/*
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530350 * Used for ppc_md.power_save which needs a function with no parameters
351 */
Nicholas Piggin2201f992017-06-13 23:05:45 +1000352void power9_idle(void)
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530353{
Nicholas Piggin2201f992017-06-13 23:05:45 +1000354 power9_idle_type(pnv_default_stop_val, pnv_default_stop_mask);
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530355}
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530356
Nicholas Piggin67d20412017-05-12 01:15:20 +1000357#ifdef CONFIG_HOTPLUG_CPU
Shreyas B. Prabhuc0691f92016-07-08 11:50:53 +0530358/*
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530359 * pnv_cpu_offline: A function that puts the CPU into the deepest
360 * available platform idle state on a CPU-Offline.
Nicholas Piggin2525db02017-06-13 23:05:46 +1000361 * interrupts hard disabled and no lazy irq pending.
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530362 */
363unsigned long pnv_cpu_offline(unsigned int cpu)
364{
365 unsigned long srr1;
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530366 u32 idle_states = pnv_get_supported_cpuidle_states();
367
Nicholas Piggin40d24342017-06-13 23:05:57 +1000368 __ppc64_runlatch_off();
Nicholas Piggin2525db02017-06-13 23:05:46 +1000369
Gautham R. Shenoyf3b3f282017-03-22 20:34:16 +0530370 if (cpu_has_feature(CPU_FTR_ARCH_300) && deepest_stop_found) {
Nicholas Piggin2525db02017-06-13 23:05:46 +1000371 unsigned long psscr;
372
373 psscr = mfspr(SPRN_PSSCR);
374 psscr = (psscr & ~pnv_deepest_stop_psscr_mask) |
375 pnv_deepest_stop_psscr_val;
376 srr1 = power9_idle_stop(psscr);
377
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530378 } else if (idle_states & OPAL_PM_WINKLE_ENABLED) {
Nicholas Piggin2525db02017-06-13 23:05:46 +1000379 srr1 = power7_idle_insn(PNV_THREAD_WINKLE);
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530380 } else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
381 (idle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
Nicholas Piggin2525db02017-06-13 23:05:46 +1000382 srr1 = power7_idle_insn(PNV_THREAD_SLEEP);
Gautham R. Shenoy90061232017-03-22 20:34:15 +0530383 } else if (idle_states & OPAL_PM_NAP_ENABLED) {
Nicholas Piggin2525db02017-06-13 23:05:46 +1000384 srr1 = power7_idle_insn(PNV_THREAD_NAP);
Gautham R. Shenoy90061232017-03-22 20:34:15 +0530385 } else {
386 /* This is the fallback method. We emulate snooze */
387 while (!generic_check_cpu_restart(cpu)) {
388 HMT_low();
389 HMT_very_low();
390 }
391 srr1 = 0;
392 HMT_medium();
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530393 }
394
Nicholas Piggin40d24342017-06-13 23:05:57 +1000395 __ppc64_runlatch_on();
Nicholas Piggin2525db02017-06-13 23:05:46 +1000396
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530397 return srr1;
398}
Nicholas Piggin67d20412017-05-12 01:15:20 +1000399#endif
Gautham R. Shenoya7cd88d2017-03-22 20:34:14 +0530400
401/*
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530402 * Power ISA 3.0 idle initialization.
403 *
404 * POWER ISA 3.0 defines a new SPR Processor stop Status and Control
405 * Register (PSSCR) to control idle behavior.
406 *
407 * PSSCR layout:
408 * ----------------------------------------------------------
409 * | PLS | /// | SD | ESL | EC | PSLL | /// | TR | MTL | RL |
410 * ----------------------------------------------------------
411 * 0 4 41 42 43 44 48 54 56 60
412 *
413 * PSSCR key fields:
414 * Bits 0:3 - Power-Saving Level Status (PLS). This field indicates the
415 * lowest power-saving state the thread entered since stop instruction was
416 * last executed.
417 *
418 * Bit 41 - Status Disable(SD)
419 * 0 - Shows PLS entries
420 * 1 - PLS entries are all 0
421 *
422 * Bit 42 - Enable State Loss
423 * 0 - No state is lost irrespective of other fields
424 * 1 - Allows state loss
425 *
426 * Bit 43 - Exit Criterion
427 * 0 - Exit from power-save mode on any interrupt
428 * 1 - Exit from power-save mode controlled by LPCR's PECE bits
429 *
430 * Bits 44:47 - Power-Saving Level Limit
431 * This limits the power-saving level that can be entered into.
432 *
433 * Bits 60:63 - Requested Level
434 * Used to specify which power-saving level must be entered on executing
435 * stop instruction
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530436 */
437
438int validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags)
439{
440 int err = 0;
441
442 /*
443 * psscr_mask == 0xf indicates an older firmware.
444 * Set remaining fields of psscr to the default values.
445 * See NOTE above definition of PSSCR_HV_DEFAULT_VAL
446 */
447 if (*psscr_mask == 0xf) {
448 *psscr_val = *psscr_val | PSSCR_HV_DEFAULT_VAL;
449 *psscr_mask = PSSCR_HV_DEFAULT_MASK;
450 return err;
451 }
452
453 /*
454 * New firmware is expected to set the psscr_val bits correctly.
455 * Validate that the following invariants are correctly maintained by
456 * the new firmware.
457 * - ESL bit value matches the EC bit value.
458 * - ESL bit is set for all the deep stop states.
459 */
460 if (GET_PSSCR_ESL(*psscr_val) != GET_PSSCR_EC(*psscr_val)) {
461 err = ERR_EC_ESL_MISMATCH;
462 } else if ((flags & OPAL_PM_LOSE_FULL_CONTEXT) &&
463 GET_PSSCR_ESL(*psscr_val) == 0) {
464 err = ERR_DEEP_STATE_ESL_MISMATCH;
465 }
466
467 return err;
468}
469
470/*
471 * pnv_arch300_idle_init: Initializes the default idle state, first
472 * deep idle state and deepest idle state on
473 * ISA 3.0 CPUs.
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530474 *
475 * @np: /ibm,opal/power-mgt device node
476 * @flags: cpu-idle-state-flags array
477 * @dt_idle_states: Number of idle state entries
478 * Returns 0 on success
479 */
Gautham R. Shenoydd34c742017-01-25 14:06:26 +0530480static int __init pnv_power9_idle_init(struct device_node *np, u32 *flags,
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530481 int dt_idle_states)
482{
483 u64 *psscr_val = NULL;
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530484 u64 *psscr_mask = NULL;
485 u32 *residency_ns = NULL;
486 u64 max_residency_ns = 0;
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530487 int rc = 0, i;
488
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530489 psscr_val = kcalloc(dt_idle_states, sizeof(*psscr_val), GFP_KERNEL);
490 psscr_mask = kcalloc(dt_idle_states, sizeof(*psscr_mask), GFP_KERNEL);
491 residency_ns = kcalloc(dt_idle_states, sizeof(*residency_ns),
492 GFP_KERNEL);
493
494 if (!psscr_val || !psscr_mask || !residency_ns) {
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530495 rc = -1;
496 goto out;
497 }
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530498
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530499 if (of_property_read_u64_array(np,
500 "ibm,cpu-idle-state-psscr",
501 psscr_val, dt_idle_states)) {
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530502 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
503 rc = -1;
504 goto out;
505 }
506
507 if (of_property_read_u64_array(np,
508 "ibm,cpu-idle-state-psscr-mask",
509 psscr_mask, dt_idle_states)) {
510 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr-mask in DT\n");
511 rc = -1;
512 goto out;
513 }
514
515 if (of_property_read_u32_array(np,
516 "ibm,cpu-idle-state-residency-ns",
517 residency_ns, dt_idle_states)) {
518 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-residency-ns in DT\n");
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530519 rc = -1;
520 goto out;
521 }
522
523 /*
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530524 * Set pnv_first_deep_stop_state, pnv_deepest_stop_psscr_{val,mask},
525 * and the pnv_default_stop_{val,mask}.
526 *
Shreyas B. Prabhuc0691f92016-07-08 11:50:53 +0530527 * pnv_first_deep_stop_state should be set to the first stop
528 * level to cause hypervisor state loss.
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530529 *
530 * pnv_deepest_stop_{val,mask} should be set to values corresponding to
531 * the deepest stop state.
532 *
533 * pnv_default_stop_{val,mask} should be set to values corresponding to
534 * the shallowest (OPAL_PM_STOP_INST_FAST) loss-less stop state.
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530535 */
536 pnv_first_deep_stop_state = MAX_STOP_STATE;
537 for (i = 0; i < dt_idle_states; i++) {
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530538 int err;
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530539 u64 psscr_rl = psscr_val[i] & PSSCR_RL_MASK;
540
541 if ((flags[i] & OPAL_PM_LOSE_FULL_CONTEXT) &&
542 (pnv_first_deep_stop_state > psscr_rl))
543 pnv_first_deep_stop_state = psscr_rl;
Shreyas B. Prabhuc0691f92016-07-08 11:50:53 +0530544
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530545 err = validate_psscr_val_mask(&psscr_val[i], &psscr_mask[i],
546 flags[i]);
547 if (err) {
548 report_invalid_psscr_val(psscr_val[i], err);
549 continue;
550 }
551
552 if (max_residency_ns < residency_ns[i]) {
553 max_residency_ns = residency_ns[i];
554 pnv_deepest_stop_psscr_val = psscr_val[i];
555 pnv_deepest_stop_psscr_mask = psscr_mask[i];
556 deepest_stop_found = true;
557 }
558
559 if (!default_stop_found &&
560 (flags[i] & OPAL_PM_STOP_INST_FAST)) {
561 pnv_default_stop_val = psscr_val[i];
562 pnv_default_stop_mask = psscr_mask[i];
563 default_stop_found = true;
564 }
565 }
566
Gautham R. Shenoyf3b3f282017-03-22 20:34:16 +0530567 if (unlikely(!default_stop_found)) {
568 pr_warn("cpuidle-powernv: No suitable default stop state found. Disabling platform idle.\n");
569 } else {
570 ppc_md.power_save = power9_idle;
571 pr_info("cpuidle-powernv: Default stop: psscr = 0x%016llx,mask=0x%016llx\n",
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530572 pnv_default_stop_val, pnv_default_stop_mask);
573 }
574
Gautham R. Shenoyf3b3f282017-03-22 20:34:16 +0530575 if (unlikely(!deepest_stop_found)) {
576 pr_warn("cpuidle-powernv: No suitable stop state for CPU-Hotplug. Offlined CPUs will busy wait");
577 } else {
578 pr_info("cpuidle-powernv: Deepest stop: psscr = 0x%016llx,mask=0x%016llx\n",
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530579 pnv_deepest_stop_psscr_val,
580 pnv_deepest_stop_psscr_mask);
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530581 }
582
Gautham R. Shenoyf3b3f282017-03-22 20:34:16 +0530583 pr_info("cpuidle-powernv: Requested Level (RL) value of first deep stop = 0x%llx\n",
584 pnv_first_deep_stop_state);
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530585out:
586 kfree(psscr_val);
Gautham R. Shenoy09206b62017-01-25 14:06:28 +0530587 kfree(psscr_mask);
588 kfree(residency_ns);
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530589 return rc;
590}
591
592/*
593 * Probe device tree for supported idle states
594 */
595static void __init pnv_probe_idle_states(void)
596{
597 struct device_node *np;
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530598 int dt_idle_states;
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530599 u32 *flags = NULL;
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530600 int i;
601
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530602 np = of_find_node_by_path("/ibm,opal/power-mgt");
603 if (!np) {
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530604 pr_warn("opal: PowerMgmt Node not found\n");
605 goto out;
606 }
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530607 dt_idle_states = of_property_count_u32_elems(np,
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530608 "ibm,cpu-idle-state-flags");
609 if (dt_idle_states < 0) {
610 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
611 goto out;
612 }
613
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530614 flags = kcalloc(dt_idle_states, sizeof(*flags), GFP_KERNEL);
615
616 if (of_property_read_u32_array(np,
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530617 "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
618 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n");
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530619 goto out;
620 }
621
622 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Gautham R. Shenoydd34c742017-01-25 14:06:26 +0530623 if (pnv_power9_idle_init(np, flags, dt_idle_states))
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530624 goto out;
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530625 }
626
627 for (i = 0; i < dt_idle_states; i++)
628 supported_cpuidle_states |= flags[i];
629
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530630out:
631 kfree(flags);
632}
633static int __init pnv_init_idle_states(void)
634{
635
636 supported_cpuidle_states = 0;
637
638 if (cpuidle_disable != IDLE_NO_OVERRIDE)
639 goto out;
640
641 pnv_probe_idle_states();
642
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530643 if (!(supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
644 patch_instruction(
645 (unsigned int *)pnv_fastsleep_workaround_at_entry,
646 PPC_INST_NOP);
647 patch_instruction(
648 (unsigned int *)pnv_fastsleep_workaround_at_exit,
649 PPC_INST_NOP);
Shreyas B. Prabhu5703d2f2015-04-20 10:32:58 +0530650 } else {
651 /*
652 * OPAL_PM_SLEEP_ENABLED_ER1 is set. It indicates that
653 * workaround is needed to use fastsleep. Provide sysfs
654 * control to choose how this workaround has to be applied.
655 */
656 device_create_file(cpu_subsys.dev_root,
657 &dev_attr_fastsleep_workaround_applyonce);
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530658 }
Shreyas B. Prabhu5703d2f2015-04-20 10:32:58 +0530659
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530660 pnv_alloc_idle_core_states();
Shreyas B. Prabhu5593e302016-06-08 11:54:27 -0500661
Gautham R. Shenoy17ed4c82017-03-22 20:34:17 +0530662 /*
663 * For each CPU, record its PACA address in each of it's
664 * sibling thread's PACA at the slot corresponding to this
665 * CPU's index in the core.
666 */
667 if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
668 int cpu;
669
670 pr_info("powernv: idle: Saving PACA pointers of all CPUs in their thread sibling PACA\n");
671 for_each_possible_cpu(cpu) {
672 int base_cpu = cpu_first_thread_sibling(cpu);
673 int idx = cpu_thread_in_core(cpu);
674 int i;
675
676 for (i = 0; i < threads_per_core; i++) {
677 int j = base_cpu + i;
678
679 paca[j].thread_sibling_pacas[idx] = &paca[cpu];
680 }
681 }
682 }
683
Shreyas B. Prabhu5593e302016-06-08 11:54:27 -0500684 if (supported_cpuidle_states & OPAL_PM_NAP_ENABLED)
685 ppc_md.power_save = power7_idle;
Shreyas B. Prabhubcef83a2016-07-08 11:50:49 +0530686
Shreyas B. Prabhud405a982015-04-20 10:32:57 +0530687out:
688 return 0;
689}
Michael Ellerman4bece972015-06-15 15:01:32 +1000690machine_subsys_initcall(powernv, pnv_init_idle_states);