2 * cpuidle-powernv - idle state cpuidle driver.
3 * Adapted from drivers/cpuidle/cpuidle-pseries
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/moduleparam.h>
11 #include <linux/cpuidle.h>
12 #include <linux/cpu.h>
13 #include <linux/notifier.h>
14 #include <linux/clockchips.h>
16 #include <linux/slab.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
21 #include <asm/runlatch.h>
22 #include <asm/cpuidle.h>
25 * Expose only those Hardware idle states via the cpuidle framework
26 * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
28 #define POWERNV_THRESHOLD_LATENCY_NS 200000
30 static struct cpuidle_driver powernv_idle_driver
= {
31 .name
= "powernv_idle",
35 static int max_idle_state __read_mostly
;
36 static struct cpuidle_state
*cpuidle_state_table __read_mostly
;
38 struct stop_psscr_table
{
43 static struct stop_psscr_table stop_psscr_table
[CPUIDLE_STATE_MAX
] __read_mostly
;
45 static u64 snooze_timeout __read_mostly
;
46 static bool snooze_timeout_en __read_mostly
;
48 static int snooze_loop(struct cpuidle_device
*dev
,
49 struct cpuidle_driver
*drv
,
54 set_thread_flag(TIF_POLLING_NRFLAG
);
58 snooze_exit_time
= get_tb() + snooze_timeout
;
61 while (!need_resched()) {
62 if (likely(snooze_timeout_en
) && get_tb() > snooze_exit_time
) {
64 * Task has not woken up but we are exiting the polling
65 * loop anyway. Require a barrier after polling is
66 * cleared to order subsequent test of need_resched().
68 clear_thread_flag(TIF_POLLING_NRFLAG
);
76 clear_thread_flag(TIF_POLLING_NRFLAG
);
81 static int nap_loop(struct cpuidle_device
*dev
,
82 struct cpuidle_driver
*drv
,
85 power7_idle_type(PNV_THREAD_NAP
);
90 /* Register for fastsleep only in oneshot mode of broadcast */
91 #ifdef CONFIG_TICK_ONESHOT
92 static int fastsleep_loop(struct cpuidle_device
*dev
,
93 struct cpuidle_driver
*drv
,
96 unsigned long old_lpcr
= mfspr(SPRN_LPCR
);
97 unsigned long new_lpcr
;
99 if (unlikely(system_state
< SYSTEM_RUNNING
))
103 /* Do not exit powersave upon decrementer as we've setup the timer
106 new_lpcr
&= ~LPCR_PECE1
;
108 mtspr(SPRN_LPCR
, new_lpcr
);
110 power7_idle_type(PNV_THREAD_SLEEP
);
112 mtspr(SPRN_LPCR
, old_lpcr
);
118 static int stop_loop(struct cpuidle_device
*dev
,
119 struct cpuidle_driver
*drv
,
122 power9_idle_type(stop_psscr_table
[index
].val
,
123 stop_psscr_table
[index
].mask
);
128 * States for dedicated partition case.
130 static struct cpuidle_state powernv_states
[CPUIDLE_STATE_MAX
] = {
135 .target_residency
= 0,
136 .enter
= snooze_loop
},
139 static int powernv_cpuidle_cpu_online(unsigned int cpu
)
141 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
143 if (dev
&& cpuidle_get_driver()) {
144 cpuidle_pause_and_lock();
145 cpuidle_enable_device(dev
);
146 cpuidle_resume_and_unlock();
151 static int powernv_cpuidle_cpu_dead(unsigned int cpu
)
153 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
155 if (dev
&& cpuidle_get_driver()) {
156 cpuidle_pause_and_lock();
157 cpuidle_disable_device(dev
);
158 cpuidle_resume_and_unlock();
164 * powernv_cpuidle_driver_init()
166 static int powernv_cpuidle_driver_init(void)
169 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
171 drv
->state_count
= 0;
173 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
174 /* Is the state not enabled? */
175 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
178 drv
->states
[drv
->state_count
] = /* structure copy */
179 cpuidle_state_table
[idle_state
];
181 drv
->state_count
+= 1;
185 * On the PowerNV platform cpu_present may be less than cpu_possible in
186 * cases when firmware detects the CPU, but it is not available to the
187 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
188 * run time and hence cpu_devices are not created for those CPUs by the
189 * generic topology_init().
191 * drv->cpumask defaults to cpu_possible_mask in
192 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
193 * cpu_devices are not created for CPUs in cpu_possible_mask that
194 * cannot be hot-added later at run time.
196 * Trying cpuidle_register_device() on a CPU without a cpu_device is
197 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
200 drv
->cpumask
= (struct cpumask
*)cpu_present_mask
;
205 static inline void add_powernv_state(int index
, const char *name
,
207 int (*idle_fn
)(struct cpuidle_device
*,
208 struct cpuidle_driver
*,
210 unsigned int target_residency
,
211 unsigned int exit_latency
,
212 u64 psscr_val
, u64 psscr_mask
)
214 strlcpy(powernv_states
[index
].name
, name
, CPUIDLE_NAME_LEN
);
215 strlcpy(powernv_states
[index
].desc
, name
, CPUIDLE_NAME_LEN
);
216 powernv_states
[index
].flags
= flags
;
217 powernv_states
[index
].target_residency
= target_residency
;
218 powernv_states
[index
].exit_latency
= exit_latency
;
219 powernv_states
[index
].enter
= idle_fn
;
220 stop_psscr_table
[index
].val
= psscr_val
;
221 stop_psscr_table
[index
].mask
= psscr_mask
;
225 * Returns 0 if prop1_len == prop2_len. Else returns -1
227 static inline int validate_dt_prop_sizes(const char *prop1
, int prop1_len
,
228 const char *prop2
, int prop2_len
)
230 if (prop1_len
== prop2_len
)
233 pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
238 extern u32
pnv_get_supported_cpuidle_states(void);
239 static int powernv_add_idle_states(void)
241 struct device_node
*power_mgt
;
242 int nr_idle_states
= 1; /* Snooze */
243 int dt_idle_states
, count
;
244 u32 latency_ns
[CPUIDLE_STATE_MAX
];
245 u32 residency_ns
[CPUIDLE_STATE_MAX
];
246 u32 flags
[CPUIDLE_STATE_MAX
];
247 u64 psscr_val
[CPUIDLE_STATE_MAX
];
248 u64 psscr_mask
[CPUIDLE_STATE_MAX
];
249 const char *names
[CPUIDLE_STATE_MAX
];
250 u32 has_stop_states
= 0;
252 u32 supported_flags
= pnv_get_supported_cpuidle_states();
255 /* Currently we have snooze statically defined */
257 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
259 pr_warn("opal: PowerMgmt Node not found\n");
263 /* Read values of any property to determine the num of idle states */
264 dt_idle_states
= of_property_count_u32_elems(power_mgt
, "ibm,cpu-idle-state-flags");
265 if (dt_idle_states
< 0) {
266 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
270 count
= of_property_count_u32_elems(power_mgt
,
271 "ibm,cpu-idle-state-latencies-ns");
273 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states
,
274 "ibm,cpu-idle-state-latencies-ns",
278 count
= of_property_count_strings(power_mgt
,
279 "ibm,cpu-idle-state-names");
280 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states
,
281 "ibm,cpu-idle-state-names",
286 * Since snooze is used as first idle state, max idle states allowed is
287 * CPUIDLE_STATE_MAX -1
289 if (dt_idle_states
> CPUIDLE_STATE_MAX
- 1) {
290 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
291 dt_idle_states
= CPUIDLE_STATE_MAX
- 1;
294 if (of_property_read_u32_array(power_mgt
,
295 "ibm,cpu-idle-state-flags", flags
, dt_idle_states
)) {
296 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
300 if (of_property_read_u32_array(power_mgt
,
301 "ibm,cpu-idle-state-latencies-ns", latency_ns
,
303 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
306 if (of_property_read_string_array(power_mgt
,
307 "ibm,cpu-idle-state-names", names
, dt_idle_states
) < 0) {
308 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
313 * If the idle states use stop instruction, probe for psscr values
314 * and psscr mask which are necessary to specify required stop level.
316 has_stop_states
= (flags
[0] &
317 (OPAL_PM_STOP_INST_FAST
| OPAL_PM_STOP_INST_DEEP
));
318 if (has_stop_states
) {
319 count
= of_property_count_u64_elems(power_mgt
,
320 "ibm,cpu-idle-state-psscr");
321 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
323 "ibm,cpu-idle-state-psscr",
327 count
= of_property_count_u64_elems(power_mgt
,
328 "ibm,cpu-idle-state-psscr-mask");
329 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
331 "ibm,cpu-idle-state-psscr-mask",
335 if (of_property_read_u64_array(power_mgt
,
336 "ibm,cpu-idle-state-psscr", psscr_val
, dt_idle_states
)) {
337 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
341 if (of_property_read_u64_array(power_mgt
,
342 "ibm,cpu-idle-state-psscr-mask",
343 psscr_mask
, dt_idle_states
)) {
344 pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
349 count
= of_property_count_u32_elems(power_mgt
,
350 "ibm,cpu-idle-state-residency-ns");
354 } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
356 "ibm,cpu-idle-state-residency-ns",
360 rc
= of_property_read_u32_array(power_mgt
,
361 "ibm,cpu-idle-state-residency-ns",
362 residency_ns
, dt_idle_states
);
365 for (i
= 0; i
< dt_idle_states
; i
++) {
366 unsigned int exit_latency
, target_residency
;
367 bool stops_timebase
= false;
370 * Skip the platform idle state whose flag isn't in
371 * the supported_cpuidle_states flag mask.
373 if ((flags
[i
] & supported_flags
) != flags
[i
])
376 * If an idle state has exit latency beyond
377 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
380 if (latency_ns
[i
] > POWERNV_THRESHOLD_LATENCY_NS
)
383 * Firmware passes residency and latency values in ns.
384 * cpuidle expects it in us.
386 exit_latency
= latency_ns
[i
] / 1000;
388 target_residency
= residency_ns
[i
] / 1000;
390 target_residency
= 0;
392 if (has_stop_states
) {
393 int err
= validate_psscr_val_mask(&psscr_val
[i
],
397 report_invalid_psscr_val(psscr_val
[i
], err
);
402 if (flags
[i
] & OPAL_PM_TIMEBASE_STOP
)
403 stops_timebase
= true;
406 * For nap and fastsleep, use default target_residency
407 * values if f/w does not expose it.
409 if (flags
[i
] & OPAL_PM_NAP_ENABLED
) {
411 target_residency
= 100;
413 add_powernv_state(nr_idle_states
, "Nap",
414 CPUIDLE_FLAG_NONE
, nap_loop
,
415 target_residency
, exit_latency
, 0, 0);
416 } else if (has_stop_states
&& !stops_timebase
) {
417 add_powernv_state(nr_idle_states
, names
[i
],
418 CPUIDLE_FLAG_NONE
, stop_loop
,
419 target_residency
, exit_latency
,
420 psscr_val
[i
], psscr_mask
[i
]);
424 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
425 * within this config dependency check.
427 #ifdef CONFIG_TICK_ONESHOT
428 else if (flags
[i
] & OPAL_PM_SLEEP_ENABLED
||
429 flags
[i
] & OPAL_PM_SLEEP_ENABLED_ER1
) {
431 target_residency
= 300000;
432 /* Add FASTSLEEP state */
433 add_powernv_state(nr_idle_states
, "FastSleep",
434 CPUIDLE_FLAG_TIMER_STOP
,
436 target_residency
, exit_latency
, 0, 0);
437 } else if (has_stop_states
&& stops_timebase
) {
438 add_powernv_state(nr_idle_states
, names
[i
],
439 CPUIDLE_FLAG_TIMER_STOP
, stop_loop
,
440 target_residency
, exit_latency
,
441 psscr_val
[i
], psscr_mask
[i
]);
449 return nr_idle_states
;
453 * powernv_idle_probe()
454 * Choose state table for shared versus dedicated partition
456 static int powernv_idle_probe(void)
458 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
461 if (firmware_has_feature(FW_FEATURE_OPAL
)) {
462 cpuidle_state_table
= powernv_states
;
463 /* Device tree can indicate more idle states */
464 max_idle_state
= powernv_add_idle_states();
465 if (max_idle_state
> 1) {
466 snooze_timeout_en
= true;
467 snooze_timeout
= powernv_states
[1].target_residency
*
476 static int __init
powernv_processor_idle_init(void)
480 retval
= powernv_idle_probe();
484 powernv_cpuidle_driver_init();
485 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
487 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
491 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
492 "cpuidle/powernv:online",
493 powernv_cpuidle_cpu_online
, NULL
);
495 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
496 "cpuidle/powernv:dead", NULL
,
497 powernv_cpuidle_cpu_dead
);
499 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
503 device_initcall(powernv_processor_idle_init
);