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
);
82 static int nap_loop(struct cpuidle_device
*dev
,
83 struct cpuidle_driver
*drv
,
86 power7_idle_type(PNV_THREAD_NAP
);
91 /* Register for fastsleep only in oneshot mode of broadcast */
92 #ifdef CONFIG_TICK_ONESHOT
93 static int fastsleep_loop(struct cpuidle_device
*dev
,
94 struct cpuidle_driver
*drv
,
97 unsigned long old_lpcr
= mfspr(SPRN_LPCR
);
98 unsigned long new_lpcr
;
100 if (unlikely(system_state
< SYSTEM_RUNNING
))
104 /* Do not exit powersave upon decrementer as we've setup the timer
107 new_lpcr
&= ~LPCR_PECE1
;
109 mtspr(SPRN_LPCR
, new_lpcr
);
111 power7_idle_type(PNV_THREAD_SLEEP
);
113 mtspr(SPRN_LPCR
, old_lpcr
);
119 static int stop_loop(struct cpuidle_device
*dev
,
120 struct cpuidle_driver
*drv
,
123 power9_idle_type(stop_psscr_table
[index
].val
,
124 stop_psscr_table
[index
].mask
);
129 * States for dedicated partition case.
131 static struct cpuidle_state powernv_states
[CPUIDLE_STATE_MAX
] = {
136 .target_residency
= 0,
137 .enter
= snooze_loop
},
140 static int powernv_cpuidle_cpu_online(unsigned int cpu
)
142 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
144 if (dev
&& cpuidle_get_driver()) {
145 cpuidle_pause_and_lock();
146 cpuidle_enable_device(dev
);
147 cpuidle_resume_and_unlock();
152 static int powernv_cpuidle_cpu_dead(unsigned int cpu
)
154 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
156 if (dev
&& cpuidle_get_driver()) {
157 cpuidle_pause_and_lock();
158 cpuidle_disable_device(dev
);
159 cpuidle_resume_and_unlock();
165 * powernv_cpuidle_driver_init()
167 static int powernv_cpuidle_driver_init(void)
170 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
172 drv
->state_count
= 0;
174 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
175 /* Is the state not enabled? */
176 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
179 drv
->states
[drv
->state_count
] = /* structure copy */
180 cpuidle_state_table
[idle_state
];
182 drv
->state_count
+= 1;
186 * On the PowerNV platform cpu_present may be less than cpu_possible in
187 * cases when firmware detects the CPU, but it is not available to the
188 * OS. If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
189 * run time and hence cpu_devices are not created for those CPUs by the
190 * generic topology_init().
192 * drv->cpumask defaults to cpu_possible_mask in
193 * __cpuidle_driver_init(). This breaks cpuidle on PowerNV where
194 * cpu_devices are not created for CPUs in cpu_possible_mask that
195 * cannot be hot-added later at run time.
197 * Trying cpuidle_register_device() on a CPU without a cpu_device is
198 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
201 drv
->cpumask
= (struct cpumask
*)cpu_present_mask
;
206 static inline void add_powernv_state(int index
, const char *name
,
208 int (*idle_fn
)(struct cpuidle_device
*,
209 struct cpuidle_driver
*,
211 unsigned int target_residency
,
212 unsigned int exit_latency
,
213 u64 psscr_val
, u64 psscr_mask
)
215 strlcpy(powernv_states
[index
].name
, name
, CPUIDLE_NAME_LEN
);
216 strlcpy(powernv_states
[index
].desc
, name
, CPUIDLE_NAME_LEN
);
217 powernv_states
[index
].flags
= flags
;
218 powernv_states
[index
].target_residency
= target_residency
;
219 powernv_states
[index
].exit_latency
= exit_latency
;
220 powernv_states
[index
].enter
= idle_fn
;
221 stop_psscr_table
[index
].val
= psscr_val
;
222 stop_psscr_table
[index
].mask
= psscr_mask
;
226 * Returns 0 if prop1_len == prop2_len. Else returns -1
228 static inline int validate_dt_prop_sizes(const char *prop1
, int prop1_len
,
229 const char *prop2
, int prop2_len
)
231 if (prop1_len
== prop2_len
)
234 pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n",
239 extern u32
pnv_get_supported_cpuidle_states(void);
240 static int powernv_add_idle_states(void)
242 struct device_node
*power_mgt
;
243 int nr_idle_states
= 1; /* Snooze */
244 int dt_idle_states
, count
;
245 u32 latency_ns
[CPUIDLE_STATE_MAX
];
246 u32 residency_ns
[CPUIDLE_STATE_MAX
];
247 u32 flags
[CPUIDLE_STATE_MAX
];
248 u64 psscr_val
[CPUIDLE_STATE_MAX
];
249 u64 psscr_mask
[CPUIDLE_STATE_MAX
];
250 const char *names
[CPUIDLE_STATE_MAX
];
251 u32 has_stop_states
= 0;
253 u32 supported_flags
= pnv_get_supported_cpuidle_states();
256 /* Currently we have snooze statically defined */
258 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
260 pr_warn("opal: PowerMgmt Node not found\n");
264 /* Read values of any property to determine the num of idle states */
265 dt_idle_states
= of_property_count_u32_elems(power_mgt
, "ibm,cpu-idle-state-flags");
266 if (dt_idle_states
< 0) {
267 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
271 count
= of_property_count_u32_elems(power_mgt
,
272 "ibm,cpu-idle-state-latencies-ns");
274 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states
,
275 "ibm,cpu-idle-state-latencies-ns",
279 count
= of_property_count_strings(power_mgt
,
280 "ibm,cpu-idle-state-names");
281 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states
,
282 "ibm,cpu-idle-state-names",
287 * Since snooze is used as first idle state, max idle states allowed is
288 * CPUIDLE_STATE_MAX -1
290 if (dt_idle_states
> CPUIDLE_STATE_MAX
- 1) {
291 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
292 dt_idle_states
= CPUIDLE_STATE_MAX
- 1;
295 if (of_property_read_u32_array(power_mgt
,
296 "ibm,cpu-idle-state-flags", flags
, dt_idle_states
)) {
297 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
301 if (of_property_read_u32_array(power_mgt
,
302 "ibm,cpu-idle-state-latencies-ns", latency_ns
,
304 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
307 if (of_property_read_string_array(power_mgt
,
308 "ibm,cpu-idle-state-names", names
, dt_idle_states
) < 0) {
309 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
314 * If the idle states use stop instruction, probe for psscr values
315 * and psscr mask which are necessary to specify required stop level.
317 has_stop_states
= (flags
[0] &
318 (OPAL_PM_STOP_INST_FAST
| OPAL_PM_STOP_INST_DEEP
));
319 if (has_stop_states
) {
320 count
= of_property_count_u64_elems(power_mgt
,
321 "ibm,cpu-idle-state-psscr");
322 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
324 "ibm,cpu-idle-state-psscr",
328 count
= of_property_count_u64_elems(power_mgt
,
329 "ibm,cpu-idle-state-psscr-mask");
330 if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
332 "ibm,cpu-idle-state-psscr-mask",
336 if (of_property_read_u64_array(power_mgt
,
337 "ibm,cpu-idle-state-psscr", psscr_val
, dt_idle_states
)) {
338 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
342 if (of_property_read_u64_array(power_mgt
,
343 "ibm,cpu-idle-state-psscr-mask",
344 psscr_mask
, dt_idle_states
)) {
345 pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
350 count
= of_property_count_u32_elems(power_mgt
,
351 "ibm,cpu-idle-state-residency-ns");
355 } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags",
357 "ibm,cpu-idle-state-residency-ns",
361 rc
= of_property_read_u32_array(power_mgt
,
362 "ibm,cpu-idle-state-residency-ns",
363 residency_ns
, dt_idle_states
);
366 for (i
= 0; i
< dt_idle_states
; i
++) {
367 unsigned int exit_latency
, target_residency
;
368 bool stops_timebase
= false;
371 * Skip the platform idle state whose flag isn't in
372 * the supported_cpuidle_states flag mask.
374 if ((flags
[i
] & supported_flags
) != flags
[i
])
377 * If an idle state has exit latency beyond
378 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
381 if (latency_ns
[i
] > POWERNV_THRESHOLD_LATENCY_NS
)
384 * Firmware passes residency and latency values in ns.
385 * cpuidle expects it in us.
387 exit_latency
= DIV_ROUND_UP(latency_ns
[i
], 1000);
389 target_residency
= DIV_ROUND_UP(residency_ns
[i
], 1000);
391 target_residency
= 0;
393 if (has_stop_states
) {
394 int err
= validate_psscr_val_mask(&psscr_val
[i
],
398 report_invalid_psscr_val(psscr_val
[i
], err
);
403 if (flags
[i
] & OPAL_PM_TIMEBASE_STOP
)
404 stops_timebase
= true;
407 * For nap and fastsleep, use default target_residency
408 * values if f/w does not expose it.
410 if (flags
[i
] & OPAL_PM_NAP_ENABLED
) {
412 target_residency
= 100;
414 add_powernv_state(nr_idle_states
, "Nap",
415 CPUIDLE_FLAG_NONE
, nap_loop
,
416 target_residency
, exit_latency
, 0, 0);
417 } else if (has_stop_states
&& !stops_timebase
) {
418 add_powernv_state(nr_idle_states
, names
[i
],
419 CPUIDLE_FLAG_NONE
, stop_loop
,
420 target_residency
, exit_latency
,
421 psscr_val
[i
], psscr_mask
[i
]);
425 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
426 * within this config dependency check.
428 #ifdef CONFIG_TICK_ONESHOT
429 else if (flags
[i
] & OPAL_PM_SLEEP_ENABLED
||
430 flags
[i
] & OPAL_PM_SLEEP_ENABLED_ER1
) {
432 target_residency
= 300000;
433 /* Add FASTSLEEP state */
434 add_powernv_state(nr_idle_states
, "FastSleep",
435 CPUIDLE_FLAG_TIMER_STOP
,
437 target_residency
, exit_latency
, 0, 0);
438 } else if (has_stop_states
&& stops_timebase
) {
439 add_powernv_state(nr_idle_states
, names
[i
],
440 CPUIDLE_FLAG_TIMER_STOP
, stop_loop
,
441 target_residency
, exit_latency
,
442 psscr_val
[i
], psscr_mask
[i
]);
450 return nr_idle_states
;
454 * powernv_idle_probe()
455 * Choose state table for shared versus dedicated partition
457 static int powernv_idle_probe(void)
459 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
462 if (firmware_has_feature(FW_FEATURE_OPAL
)) {
463 cpuidle_state_table
= powernv_states
;
464 /* Device tree can indicate more idle states */
465 max_idle_state
= powernv_add_idle_states();
466 if (max_idle_state
> 1) {
467 snooze_timeout_en
= true;
468 snooze_timeout
= powernv_states
[1].target_residency
*
477 static int __init
powernv_processor_idle_init(void)
481 retval
= powernv_idle_probe();
485 powernv_cpuidle_driver_init();
486 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
488 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
492 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
493 "cpuidle/powernv:online",
494 powernv_cpuidle_cpu_online
, NULL
);
496 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
497 "cpuidle/powernv:dead", NULL
,
498 powernv_cpuidle_cpu_dead
);
500 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
504 device_initcall(powernv_processor_idle_init
);