1 // SPDX-License-Identifier: GPL-2.0
3 * cpuidle-powernv - idle state cpuidle driver.
4 * Adapted from drivers/cpuidle/cpuidle-pseries
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/moduleparam.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpu.h>
14 #include <linux/notifier.h>
15 #include <linux/clockchips.h>
17 #include <linux/slab.h>
19 #include <asm/machdep.h>
20 #include <asm/firmware.h>
22 #include <asm/runlatch.h>
23 #include <asm/cpuidle.h>
26 * Expose only those Hardware idle states via the cpuidle framework
27 * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
29 #define POWERNV_THRESHOLD_LATENCY_NS 200000
31 static struct cpuidle_driver powernv_idle_driver
= {
32 .name
= "powernv_idle",
36 static int max_idle_state __read_mostly
;
37 static struct cpuidle_state
*cpuidle_state_table __read_mostly
;
39 struct stop_psscr_table
{
44 static struct stop_psscr_table stop_psscr_table
[CPUIDLE_STATE_MAX
] __read_mostly
;
46 static u64 snooze_timeout __read_mostly
;
47 static bool snooze_timeout_en __read_mostly
;
49 static int snooze_loop(struct cpuidle_device
*dev
,
50 struct cpuidle_driver
*drv
,
55 set_thread_flag(TIF_POLLING_NRFLAG
);
59 snooze_exit_time
= get_tb() + snooze_timeout
;
62 while (!need_resched()) {
63 if (likely(snooze_timeout_en
) && get_tb() > snooze_exit_time
) {
65 * Task has not woken up but we are exiting the polling
66 * loop anyway. Require a barrier after polling is
67 * cleared to order subsequent test of need_resched().
69 clear_thread_flag(TIF_POLLING_NRFLAG
);
77 clear_thread_flag(TIF_POLLING_NRFLAG
);
84 static int nap_loop(struct cpuidle_device
*dev
,
85 struct cpuidle_driver
*drv
,
88 power7_idle_type(PNV_THREAD_NAP
);
93 /* Register for fastsleep only in oneshot mode of broadcast */
94 #ifdef CONFIG_TICK_ONESHOT
95 static int fastsleep_loop(struct cpuidle_device
*dev
,
96 struct cpuidle_driver
*drv
,
99 unsigned long old_lpcr
= mfspr(SPRN_LPCR
);
100 unsigned long new_lpcr
;
102 if (unlikely(system_state
< SYSTEM_RUNNING
))
106 /* Do not exit powersave upon decrementer as we've setup the timer
109 new_lpcr
&= ~LPCR_PECE1
;
111 mtspr(SPRN_LPCR
, new_lpcr
);
113 power7_idle_type(PNV_THREAD_SLEEP
);
115 mtspr(SPRN_LPCR
, old_lpcr
);
121 static int stop_loop(struct cpuidle_device
*dev
,
122 struct cpuidle_driver
*drv
,
125 power9_idle_type(stop_psscr_table
[index
].val
,
126 stop_psscr_table
[index
].mask
);
131 * States for dedicated partition case.
133 static struct cpuidle_state powernv_states
[CPUIDLE_STATE_MAX
] = {
138 .target_residency
= 0,
139 .enter
= snooze_loop
},
142 static int powernv_cpuidle_cpu_online(unsigned int cpu
)
144 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
146 if (dev
&& cpuidle_get_driver()) {
147 cpuidle_pause_and_lock();
148 cpuidle_enable_device(dev
);
149 cpuidle_resume_and_unlock();
154 static int powernv_cpuidle_cpu_dead(unsigned int cpu
)
156 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
158 if (dev
&& cpuidle_get_driver()) {
159 cpuidle_pause_and_lock();
160 cpuidle_disable_device(dev
);
161 cpuidle_resume_and_unlock();
167 * powernv_cpuidle_driver_init()
169 static int powernv_cpuidle_driver_init(void)
172 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
174 drv
->state_count
= 0;
176 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
177 /* Is the state not enabled? */
178 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
181 drv
->states
[drv
->state_count
] = /* structure copy */
182 cpuidle_state_table
[idle_state
];
184 drv
->state_count
+= 1;
188 * On the PowerNV platform cpu_present may be less than cpu_possible in
189 * cases when firmware detects the CPU, but it is not available to the
190 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
191 * run time and hence cpu_devices are not created for those CPUs by the
192 * generic topology_init().
194 * drv->cpumask defaults to cpu_possible_mask in
195 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
196 * cpu_devices are not created for CPUs in cpu_possible_mask that
197 * cannot be hot-added later at run time.
199 * Trying cpuidle_register_device() on a CPU without a cpu_device is
200 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
203 drv
->cpumask
= (struct cpumask
*)cpu_present_mask
;
208 static inline void add_powernv_state(int index
, const char *name
,
210 int (*idle_fn
)(struct cpuidle_device
*,
211 struct cpuidle_driver
*,
213 unsigned int target_residency
,
214 unsigned int exit_latency
,
215 u64 psscr_val
, u64 psscr_mask
)
217 strlcpy(powernv_states
[index
].name
, name
, CPUIDLE_NAME_LEN
);
218 strlcpy(powernv_states
[index
].desc
, name
, CPUIDLE_NAME_LEN
);
219 powernv_states
[index
].flags
= flags
;
220 powernv_states
[index
].target_residency
= target_residency
;
221 powernv_states
[index
].exit_latency
= exit_latency
;
222 powernv_states
[index
].enter
= idle_fn
;
223 stop_psscr_table
[index
].val
= psscr_val
;
224 stop_psscr_table
[index
].mask
= psscr_mask
;
228 * Returns 0 if prop1_len == prop2_len. Else returns -1
230 static inline int validate_dt_prop_sizes(const char *prop1
, int prop1_len
,
231 const char *prop2
, int prop2_len
)
233 if (prop1_len
== prop2_len
)
236 pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
241 extern u32
pnv_get_supported_cpuidle_states(void);
242 static int powernv_add_idle_states(void)
244 struct device_node
*power_mgt
;
245 int nr_idle_states
= 1; /* Snooze */
246 int dt_idle_states
, count
;
247 u32 latency_ns
[CPUIDLE_STATE_MAX
];
248 u32 residency_ns
[CPUIDLE_STATE_MAX
];
249 u32 flags
[CPUIDLE_STATE_MAX
];
250 u64 psscr_val
[CPUIDLE_STATE_MAX
];
251 u64 psscr_mask
[CPUIDLE_STATE_MAX
];
252 const char *names
[CPUIDLE_STATE_MAX
];
253 u32 has_stop_states
= 0;
255 u32 supported_flags
= pnv_get_supported_cpuidle_states();
258 /* Currently we have snooze statically defined */
260 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
262 pr_warn("opal: PowerMgmt Node not found\n");
266 /* Read values of any property to determine the num of idle states */
267 dt_idle_states
= of_property_count_u32_elems(power_mgt
, "ibm,cpu-idle-state-flags");
268 if (dt_idle_states
< 0) {
269 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
273 count
= of_property_count_u32_elems(power_mgt
,
274 "ibm,cpu-idle-state-latencies-ns");
276 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states
,
277 "ibm,cpu-idle-state-latencies-ns",
281 count
= of_property_count_strings(power_mgt
,
282 "ibm,cpu-idle-state-names");
283 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states
,
284 "ibm,cpu-idle-state-names",
289 * Since snooze is used as first idle state, max idle states allowed is
290 * CPUIDLE_STATE_MAX -1
292 if (dt_idle_states
> CPUIDLE_STATE_MAX
- 1) {
293 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
294 dt_idle_states
= CPUIDLE_STATE_MAX
- 1;
297 if (of_property_read_u32_array(power_mgt
,
298 "ibm,cpu-idle-state-flags", flags
, dt_idle_states
)) {
299 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
303 if (of_property_read_u32_array(power_mgt
,
304 "ibm,cpu-idle-state-latencies-ns", latency_ns
,
306 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
309 if (of_property_read_string_array(power_mgt
,
310 "ibm,cpu-idle-state-names", names
, dt_idle_states
) < 0) {
311 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
316 * If the idle states use stop instruction, probe for psscr values
317 * and psscr mask which are necessary to specify required stop level.
319 has_stop_states
= (flags
[0] &
320 (OPAL_PM_STOP_INST_FAST
| OPAL_PM_STOP_INST_DEEP
));
321 if (has_stop_states
) {
322 count
= of_property_count_u64_elems(power_mgt
,
323 "ibm,cpu-idle-state-psscr");
324 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
326 "ibm,cpu-idle-state-psscr",
330 count
= of_property_count_u64_elems(power_mgt
,
331 "ibm,cpu-idle-state-psscr-mask");
332 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
334 "ibm,cpu-idle-state-psscr-mask",
338 if (of_property_read_u64_array(power_mgt
,
339 "ibm,cpu-idle-state-psscr", psscr_val
, dt_idle_states
)) {
340 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
344 if (of_property_read_u64_array(power_mgt
,
345 "ibm,cpu-idle-state-psscr-mask",
346 psscr_mask
, dt_idle_states
)) {
347 pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
352 count
= of_property_count_u32_elems(power_mgt
,
353 "ibm,cpu-idle-state-residency-ns");
357 } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
359 "ibm,cpu-idle-state-residency-ns",
363 rc
= of_property_read_u32_array(power_mgt
,
364 "ibm,cpu-idle-state-residency-ns",
365 residency_ns
, dt_idle_states
);
368 for (i
= 0; i
< dt_idle_states
; i
++) {
369 unsigned int exit_latency
, target_residency
;
370 bool stops_timebase
= false;
373 * Skip the platform idle state whose flag isn't in
374 * the supported_cpuidle_states flag mask.
376 if ((flags
[i
] & supported_flags
) != flags
[i
])
379 * If an idle state has exit latency beyond
380 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
383 if (latency_ns
[i
] > POWERNV_THRESHOLD_LATENCY_NS
)
386 * Firmware passes residency and latency values in ns.
387 * cpuidle expects it in us.
389 exit_latency
= DIV_ROUND_UP(latency_ns
[i
], 1000);
391 target_residency
= DIV_ROUND_UP(residency_ns
[i
], 1000);
393 target_residency
= 0;
395 if (has_stop_states
) {
396 int err
= validate_psscr_val_mask(&psscr_val
[i
],
400 report_invalid_psscr_val(psscr_val
[i
], err
);
405 if (flags
[i
] & OPAL_PM_TIMEBASE_STOP
)
406 stops_timebase
= true;
409 * For nap and fastsleep, use default target_residency
410 * values if f/w does not expose it.
412 if (flags
[i
] & OPAL_PM_NAP_ENABLED
) {
414 target_residency
= 100;
416 add_powernv_state(nr_idle_states
, "Nap",
417 CPUIDLE_FLAG_NONE
, nap_loop
,
418 target_residency
, exit_latency
, 0, 0);
419 } else if (has_stop_states
&& !stops_timebase
) {
420 add_powernv_state(nr_idle_states
, names
[i
],
421 CPUIDLE_FLAG_NONE
, stop_loop
,
422 target_residency
, exit_latency
,
423 psscr_val
[i
], psscr_mask
[i
]);
427 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
428 * within this config dependency check.
430 #ifdef CONFIG_TICK_ONESHOT
431 else if (flags
[i
] & OPAL_PM_SLEEP_ENABLED
||
432 flags
[i
] & OPAL_PM_SLEEP_ENABLED_ER1
) {
434 target_residency
= 300000;
435 /* Add FASTSLEEP state */
436 add_powernv_state(nr_idle_states
, "FastSleep",
437 CPUIDLE_FLAG_TIMER_STOP
,
439 target_residency
, exit_latency
, 0, 0);
440 } else if (has_stop_states
&& stops_timebase
) {
441 add_powernv_state(nr_idle_states
, names
[i
],
442 CPUIDLE_FLAG_TIMER_STOP
, stop_loop
,
443 target_residency
, exit_latency
,
444 psscr_val
[i
], psscr_mask
[i
]);
452 return nr_idle_states
;
456 * powernv_idle_probe()
457 * Choose state table for shared versus dedicated partition
459 static int powernv_idle_probe(void)
461 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
464 if (firmware_has_feature(FW_FEATURE_OPAL
)) {
465 cpuidle_state_table
= powernv_states
;
466 /* Device tree can indicate more idle states */
467 max_idle_state
= powernv_add_idle_states();
468 if (max_idle_state
> 1) {
469 snooze_timeout_en
= true;
470 snooze_timeout
= powernv_states
[1].target_residency
*
479 static int __init
powernv_processor_idle_init(void)
483 retval
= powernv_idle_probe();
487 powernv_cpuidle_driver_init();
488 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
490 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
494 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
495 "cpuidle/powernv:online",
496 powernv_cpuidle_cpu_online
, NULL
);
498 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
499 "cpuidle/powernv:dead", NULL
,
500 powernv_cpuidle_cpu_dead
);
502 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
506 device_initcall(powernv_processor_idle_init
);