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
;
36 static struct cpuidle_state
*cpuidle_state_table
;
38 struct stop_psscr_table
{
43 static struct stop_psscr_table stop_psscr_table
[CPUIDLE_STATE_MAX
];
45 static u64 snooze_timeout
;
46 static bool snooze_timeout_en
;
48 static int snooze_loop(struct cpuidle_device
*dev
,
49 struct cpuidle_driver
*drv
,
55 set_thread_flag(TIF_POLLING_NRFLAG
);
57 snooze_exit_time
= get_tb() + snooze_timeout
;
59 while (!need_resched()) {
62 if (snooze_timeout_en
&& get_tb() > snooze_exit_time
)
68 clear_thread_flag(TIF_POLLING_NRFLAG
);
73 static int nap_loop(struct cpuidle_device
*dev
,
74 struct cpuidle_driver
*drv
,
83 /* Register for fastsleep only in oneshot mode of broadcast */
84 #ifdef CONFIG_TICK_ONESHOT
85 static int fastsleep_loop(struct cpuidle_device
*dev
,
86 struct cpuidle_driver
*drv
,
89 unsigned long old_lpcr
= mfspr(SPRN_LPCR
);
90 unsigned long new_lpcr
;
92 if (unlikely(system_state
< SYSTEM_RUNNING
))
96 /* Do not exit powersave upon decrementer as we've setup the timer
99 new_lpcr
&= ~LPCR_PECE1
;
101 mtspr(SPRN_LPCR
, new_lpcr
);
104 mtspr(SPRN_LPCR
, old_lpcr
);
110 static int stop_loop(struct cpuidle_device
*dev
,
111 struct cpuidle_driver
*drv
,
114 ppc64_runlatch_off();
115 power9_idle_stop(stop_psscr_table
[index
].val
,
116 stop_psscr_table
[index
].mask
);
122 * States for dedicated partition case.
124 static struct cpuidle_state powernv_states
[CPUIDLE_STATE_MAX
] = {
129 .target_residency
= 0,
130 .enter
= snooze_loop
},
133 static int powernv_cpuidle_cpu_online(unsigned int cpu
)
135 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
137 if (dev
&& cpuidle_get_driver()) {
138 cpuidle_pause_and_lock();
139 cpuidle_enable_device(dev
);
140 cpuidle_resume_and_unlock();
145 static int powernv_cpuidle_cpu_dead(unsigned int cpu
)
147 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
149 if (dev
&& cpuidle_get_driver()) {
150 cpuidle_pause_and_lock();
151 cpuidle_disable_device(dev
);
152 cpuidle_resume_and_unlock();
158 * powernv_cpuidle_driver_init()
160 static int powernv_cpuidle_driver_init(void)
163 struct cpuidle_driver
*drv
= &powernv_idle_driver
;
165 drv
->state_count
= 0;
167 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
168 /* Is the state not enabled? */
169 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
172 drv
->states
[drv
->state_count
] = /* structure copy */
173 cpuidle_state_table
[idle_state
];
175 drv
->state_count
+= 1;
181 static inline void add_powernv_state(int index
, const char *name
,
183 int (*idle_fn
)(struct cpuidle_device
*,
184 struct cpuidle_driver
*,
186 unsigned int target_residency
,
187 unsigned int exit_latency
,
188 u64 psscr_val
, u64 psscr_mask
)
190 strlcpy(powernv_states
[index
].name
, name
, CPUIDLE_NAME_LEN
);
191 strlcpy(powernv_states
[index
].desc
, name
, CPUIDLE_NAME_LEN
);
192 powernv_states
[index
].flags
= flags
;
193 powernv_states
[index
].target_residency
= target_residency
;
194 powernv_states
[index
].exit_latency
= exit_latency
;
195 powernv_states
[index
].enter
= idle_fn
;
196 stop_psscr_table
[index
].val
= psscr_val
;
197 stop_psscr_table
[index
].mask
= psscr_mask
;
200 static int powernv_add_idle_states(void)
202 struct device_node
*power_mgt
;
203 int nr_idle_states
= 1; /* Snooze */
205 u32 latency_ns
[CPUIDLE_STATE_MAX
];
206 u32 residency_ns
[CPUIDLE_STATE_MAX
];
207 u32 flags
[CPUIDLE_STATE_MAX
];
208 u64 psscr_val
[CPUIDLE_STATE_MAX
];
209 u64 psscr_mask
[CPUIDLE_STATE_MAX
];
210 const char *names
[CPUIDLE_STATE_MAX
];
211 u32 has_stop_states
= 0;
214 /* Currently we have snooze statically defined */
216 power_mgt
= of_find_node_by_path("/ibm,opal/power-mgt");
218 pr_warn("opal: PowerMgmt Node not found\n");
222 /* Read values of any property to determine the num of idle states */
223 dt_idle_states
= of_property_count_u32_elems(power_mgt
, "ibm,cpu-idle-state-flags");
224 if (dt_idle_states
< 0) {
225 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
230 * Since snooze is used as first idle state, max idle states allowed is
231 * CPUIDLE_STATE_MAX -1
233 if (dt_idle_states
> CPUIDLE_STATE_MAX
- 1) {
234 pr_warn("cpuidle-powernv: discovered idle states more than allowed");
235 dt_idle_states
= CPUIDLE_STATE_MAX
- 1;
238 if (of_property_read_u32_array(power_mgt
,
239 "ibm,cpu-idle-state-flags", flags
, dt_idle_states
)) {
240 pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n");
244 if (of_property_read_u32_array(power_mgt
,
245 "ibm,cpu-idle-state-latencies-ns", latency_ns
,
247 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n");
250 if (of_property_read_string_array(power_mgt
,
251 "ibm,cpu-idle-state-names", names
, dt_idle_states
) < 0) {
252 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n");
257 * If the idle states use stop instruction, probe for psscr values
258 * and psscr mask which are necessary to specify required stop level.
260 has_stop_states
= (flags
[0] &
261 (OPAL_PM_STOP_INST_FAST
| OPAL_PM_STOP_INST_DEEP
));
262 if (has_stop_states
) {
263 if (of_property_read_u64_array(power_mgt
,
264 "ibm,cpu-idle-state-psscr", psscr_val
, dt_idle_states
)) {
265 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
269 if (of_property_read_u64_array(power_mgt
,
270 "ibm,cpu-idle-state-psscr-mask",
271 psscr_mask
, dt_idle_states
)) {
272 pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n");
277 rc
= of_property_read_u32_array(power_mgt
,
278 "ibm,cpu-idle-state-residency-ns", residency_ns
, dt_idle_states
);
280 for (i
= 0; i
< dt_idle_states
; i
++) {
281 unsigned int exit_latency
, target_residency
;
283 * If an idle state has exit latency beyond
284 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
287 if (latency_ns
[i
] > POWERNV_THRESHOLD_LATENCY_NS
)
290 * Firmware passes residency and latency values in ns.
291 * cpuidle expects it in us.
293 exit_latency
= latency_ns
[i
] / 1000;
295 target_residency
= residency_ns
[i
] / 1000;
297 target_residency
= 0;
299 if (has_stop_states
) {
300 int err
= validate_psscr_val_mask(&psscr_val
[i
],
304 report_invalid_psscr_val(psscr_val
[i
], err
);
310 * For nap and fastsleep, use default target_residency
311 * values if f/w does not expose it.
313 if (flags
[i
] & OPAL_PM_NAP_ENABLED
) {
315 target_residency
= 100;
317 add_powernv_state(nr_idle_states
, "Nap",
318 CPUIDLE_FLAG_NONE
, nap_loop
,
319 target_residency
, exit_latency
, 0, 0);
320 } else if ((flags
[i
] & OPAL_PM_STOP_INST_FAST
) &&
321 !(flags
[i
] & OPAL_PM_TIMEBASE_STOP
)) {
322 add_powernv_state(nr_idle_states
, names
[i
],
323 CPUIDLE_FLAG_NONE
, stop_loop
,
324 target_residency
, exit_latency
,
325 psscr_val
[i
], psscr_mask
[i
]);
329 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
330 * within this config dependency check.
332 #ifdef CONFIG_TICK_ONESHOT
333 if (flags
[i
] & OPAL_PM_SLEEP_ENABLED
||
334 flags
[i
] & OPAL_PM_SLEEP_ENABLED_ER1
) {
336 target_residency
= 300000;
337 /* Add FASTSLEEP state */
338 add_powernv_state(nr_idle_states
, "FastSleep",
339 CPUIDLE_FLAG_TIMER_STOP
,
341 target_residency
, exit_latency
, 0, 0);
342 } else if ((flags
[i
] & OPAL_PM_STOP_INST_DEEP
) &&
343 (flags
[i
] & OPAL_PM_TIMEBASE_STOP
)) {
344 add_powernv_state(nr_idle_states
, names
[i
],
345 CPUIDLE_FLAG_TIMER_STOP
, stop_loop
,
346 target_residency
, exit_latency
,
347 psscr_val
[i
], psscr_mask
[i
]);
353 return nr_idle_states
;
357 * powernv_idle_probe()
358 * Choose state table for shared versus dedicated partition
360 static int powernv_idle_probe(void)
362 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
365 if (firmware_has_feature(FW_FEATURE_OPAL
)) {
366 cpuidle_state_table
= powernv_states
;
367 /* Device tree can indicate more idle states */
368 max_idle_state
= powernv_add_idle_states();
369 if (max_idle_state
> 1) {
370 snooze_timeout_en
= true;
371 snooze_timeout
= powernv_states
[1].target_residency
*
380 static int __init
powernv_processor_idle_init(void)
384 retval
= powernv_idle_probe();
388 powernv_cpuidle_driver_init();
389 retval
= cpuidle_register(&powernv_idle_driver
, NULL
);
391 printk(KERN_DEBUG
"Registration of powernv driver failed.\n");
395 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
396 "cpuidle/powernv:online",
397 powernv_cpuidle_cpu_online
, NULL
);
399 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
400 "cpuidle/powernv:dead", NULL
,
401 powernv_cpuidle_cpu_dead
);
403 printk(KERN_DEBUG
"powernv_idle_driver registered\n");
407 device_initcall(powernv_processor_idle_init
);