]>
Commit | Line | Data |
---|---|---|
2c2e6ecf DD |
1 | /* |
2 | * cpuidle-powernv - idle state cpuidle driver. | |
3 | * Adapted from drivers/cpuidle/cpuidle-pseries | |
4 | * | |
5 | */ | |
6 | ||
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> | |
0d948730 | 14 | #include <linux/clockchips.h> |
0888839c | 15 | #include <linux/of.h> |
92c83ff5 | 16 | #include <linux/slab.h> |
2c2e6ecf DD |
17 | |
18 | #include <asm/machdep.h> | |
19 | #include <asm/firmware.h> | |
8eb8ac89 | 20 | #include <asm/opal.h> |
591ac0cb | 21 | #include <asm/runlatch.h> |
09206b60 | 22 | #include <asm/cpuidle.h> |
2c2e6ecf | 23 | |
9e9fc6f0 GS |
24 | /* |
25 | * Expose only those Hardware idle states via the cpuidle framework | |
26 | * that have latency value below POWERNV_THRESHOLD_LATENCY_NS. | |
27 | */ | |
3005c597 SP |
28 | #define POWERNV_THRESHOLD_LATENCY_NS 200000 |
29 | ||
ed61390b | 30 | static struct cpuidle_driver powernv_idle_driver = { |
2c2e6ecf DD |
31 | .name = "powernv_idle", |
32 | .owner = THIS_MODULE, | |
33 | }; | |
34 | ||
35 | static int max_idle_state; | |
36 | static struct cpuidle_state *cpuidle_state_table; | |
3005c597 | 37 | |
09206b60 GS |
38 | struct stop_psscr_table { |
39 | u64 val; | |
40 | u64 mask; | |
41 | }; | |
42 | ||
43 | static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX]; | |
3005c597 | 44 | |
78eaa10f SB |
45 | static u64 snooze_timeout; |
46 | static bool snooze_timeout_en; | |
2c2e6ecf DD |
47 | |
48 | static int snooze_loop(struct cpuidle_device *dev, | |
49 | struct cpuidle_driver *drv, | |
50 | int index) | |
51 | { | |
78eaa10f SB |
52 | u64 snooze_exit_time; |
53 | ||
2c2e6ecf DD |
54 | local_irq_enable(); |
55 | set_thread_flag(TIF_POLLING_NRFLAG); | |
56 | ||
78eaa10f | 57 | snooze_exit_time = get_tb() + snooze_timeout; |
591ac0cb | 58 | ppc64_runlatch_off(); |
2c2e6ecf | 59 | while (!need_resched()) { |
2c2e6ecf | 60 | HMT_very_low(); |
78eaa10f SB |
61 | if (snooze_timeout_en && get_tb() > snooze_exit_time) |
62 | break; | |
2c2e6ecf DD |
63 | } |
64 | ||
65 | HMT_medium(); | |
591ac0cb | 66 | ppc64_runlatch_on(); |
2c2e6ecf DD |
67 | clear_thread_flag(TIF_POLLING_NRFLAG); |
68 | smp_mb(); | |
69 | return index; | |
70 | } | |
71 | ||
72 | static int nap_loop(struct cpuidle_device *dev, | |
73 | struct cpuidle_driver *drv, | |
74 | int index) | |
75 | { | |
591ac0cb | 76 | ppc64_runlatch_off(); |
2c2e6ecf | 77 | power7_idle(); |
591ac0cb | 78 | ppc64_runlatch_on(); |
2c2e6ecf DD |
79 | return index; |
80 | } | |
81 | ||
cc5a2f7b | 82 | /* Register for fastsleep only in oneshot mode of broadcast */ |
83 | #ifdef CONFIG_TICK_ONESHOT | |
0d948730 PM |
84 | static int fastsleep_loop(struct cpuidle_device *dev, |
85 | struct cpuidle_driver *drv, | |
86 | int index) | |
87 | { | |
88 | unsigned long old_lpcr = mfspr(SPRN_LPCR); | |
89 | unsigned long new_lpcr; | |
90 | ||
91 | if (unlikely(system_state < SYSTEM_RUNNING)) | |
92 | return index; | |
93 | ||
94 | new_lpcr = old_lpcr; | |
9b6a68d9 MN |
95 | /* Do not exit powersave upon decrementer as we've setup the timer |
96 | * offload. | |
0d948730 | 97 | */ |
9b6a68d9 | 98 | new_lpcr &= ~LPCR_PECE1; |
0d948730 PM |
99 | |
100 | mtspr(SPRN_LPCR, new_lpcr); | |
101 | power7_sleep(); | |
102 | ||
103 | mtspr(SPRN_LPCR, old_lpcr); | |
104 | ||
105 | return index; | |
106 | } | |
cc5a2f7b | 107 | #endif |
3005c597 SP |
108 | |
109 | static int stop_loop(struct cpuidle_device *dev, | |
110 | struct cpuidle_driver *drv, | |
111 | int index) | |
112 | { | |
113 | ppc64_runlatch_off(); | |
09206b60 GS |
114 | power9_idle_stop(stop_psscr_table[index].val, |
115 | stop_psscr_table[index].mask); | |
3005c597 SP |
116 | ppc64_runlatch_on(); |
117 | return index; | |
118 | } | |
119 | ||
2c2e6ecf DD |
120 | /* |
121 | * States for dedicated partition case. | |
122 | */ | |
169f3fae | 123 | static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = { |
2c2e6ecf DD |
124 | { /* Snooze */ |
125 | .name = "snooze", | |
126 | .desc = "snooze", | |
2c2e6ecf DD |
127 | .exit_latency = 0, |
128 | .target_residency = 0, | |
957efced | 129 | .enter = snooze_loop }, |
2c2e6ecf DD |
130 | }; |
131 | ||
10fcca9d | 132 | static int powernv_cpuidle_cpu_online(unsigned int cpu) |
2c2e6ecf | 133 | { |
10fcca9d | 134 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
2c2e6ecf DD |
135 | |
136 | if (dev && cpuidle_get_driver()) { | |
10fcca9d SAS |
137 | cpuidle_pause_and_lock(); |
138 | cpuidle_enable_device(dev); | |
139 | cpuidle_resume_and_unlock(); | |
140 | } | |
141 | return 0; | |
142 | } | |
2c2e6ecf | 143 | |
10fcca9d SAS |
144 | static int powernv_cpuidle_cpu_dead(unsigned int cpu) |
145 | { | |
146 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); | |
2c2e6ecf | 147 | |
10fcca9d SAS |
148 | if (dev && cpuidle_get_driver()) { |
149 | cpuidle_pause_and_lock(); | |
150 | cpuidle_disable_device(dev); | |
151 | cpuidle_resume_and_unlock(); | |
2c2e6ecf | 152 | } |
10fcca9d | 153 | return 0; |
2c2e6ecf DD |
154 | } |
155 | ||
2c2e6ecf DD |
156 | /* |
157 | * powernv_cpuidle_driver_init() | |
158 | */ | |
159 | static int powernv_cpuidle_driver_init(void) | |
160 | { | |
161 | int idle_state; | |
162 | struct cpuidle_driver *drv = &powernv_idle_driver; | |
163 | ||
164 | drv->state_count = 0; | |
165 | ||
166 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { | |
167 | /* Is the state not enabled? */ | |
168 | if (cpuidle_state_table[idle_state].enter == NULL) | |
169 | continue; | |
170 | ||
171 | drv->states[drv->state_count] = /* structure copy */ | |
172 | cpuidle_state_table[idle_state]; | |
173 | ||
174 | drv->state_count += 1; | |
175 | } | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
9e9fc6f0 GS |
180 | static inline void add_powernv_state(int index, const char *name, |
181 | unsigned int flags, | |
182 | int (*idle_fn)(struct cpuidle_device *, | |
183 | struct cpuidle_driver *, | |
184 | int), | |
185 | unsigned int target_residency, | |
186 | unsigned int exit_latency, | |
09206b60 | 187 | u64 psscr_val, u64 psscr_mask) |
9e9fc6f0 GS |
188 | { |
189 | strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN); | |
190 | strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN); | |
191 | powernv_states[index].flags = flags; | |
192 | powernv_states[index].target_residency = target_residency; | |
193 | powernv_states[index].exit_latency = exit_latency; | |
194 | powernv_states[index].enter = idle_fn; | |
09206b60 GS |
195 | stop_psscr_table[index].val = psscr_val; |
196 | stop_psscr_table[index].mask = psscr_mask; | |
9e9fc6f0 GS |
197 | } |
198 | ||
ecad4502 GS |
199 | /* |
200 | * Returns 0 if prop1_len == prop2_len. Else returns -1 | |
201 | */ | |
202 | static inline int validate_dt_prop_sizes(const char *prop1, int prop1_len, | |
203 | const char *prop2, int prop2_len) | |
204 | { | |
205 | if (prop1_len == prop2_len) | |
206 | return 0; | |
207 | ||
208 | pr_warn("cpuidle-powernv: array sizes don't match for %s and %s\n", | |
209 | prop1, prop2); | |
210 | return -1; | |
211 | } | |
212 | ||
0888839c PM |
213 | static int powernv_add_idle_states(void) |
214 | { | |
215 | struct device_node *power_mgt; | |
0888839c | 216 | int nr_idle_states = 1; /* Snooze */ |
ecad4502 | 217 | int dt_idle_states, count; |
957efced SP |
218 | u32 latency_ns[CPUIDLE_STATE_MAX]; |
219 | u32 residency_ns[CPUIDLE_STATE_MAX]; | |
220 | u32 flags[CPUIDLE_STATE_MAX]; | |
3005c597 | 221 | u64 psscr_val[CPUIDLE_STATE_MAX]; |
09206b60 | 222 | u64 psscr_mask[CPUIDLE_STATE_MAX]; |
3005c597 | 223 | const char *names[CPUIDLE_STATE_MAX]; |
09206b60 | 224 | u32 has_stop_states = 0; |
92c83ff5 | 225 | int i, rc; |
0888839c PM |
226 | |
227 | /* Currently we have snooze statically defined */ | |
228 | ||
229 | power_mgt = of_find_node_by_path("/ibm,opal/power-mgt"); | |
230 | if (!power_mgt) { | |
231 | pr_warn("opal: PowerMgmt Node not found\n"); | |
92c83ff5 | 232 | goto out; |
0888839c PM |
233 | } |
234 | ||
70734a78 PM |
235 | /* Read values of any property to determine the num of idle states */ |
236 | dt_idle_states = of_property_count_u32_elems(power_mgt, "ibm,cpu-idle-state-flags"); | |
237 | if (dt_idle_states < 0) { | |
238 | pr_warn("cpuidle-powernv: no idle states found in the DT\n"); | |
92c83ff5 | 239 | goto out; |
0888839c PM |
240 | } |
241 | ||
ecad4502 GS |
242 | count = of_property_count_u32_elems(power_mgt, |
243 | "ibm,cpu-idle-state-latencies-ns"); | |
244 | ||
245 | if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states, | |
246 | "ibm,cpu-idle-state-latencies-ns", | |
247 | count) != 0) | |
248 | goto out; | |
249 | ||
250 | count = of_property_count_strings(power_mgt, | |
251 | "ibm,cpu-idle-state-names"); | |
252 | if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", dt_idle_states, | |
253 | "ibm,cpu-idle-state-names", | |
254 | count) != 0) | |
255 | goto out; | |
256 | ||
957efced SP |
257 | /* |
258 | * Since snooze is used as first idle state, max idle states allowed is | |
259 | * CPUIDLE_STATE_MAX -1 | |
260 | */ | |
261 | if (dt_idle_states > CPUIDLE_STATE_MAX - 1) { | |
262 | pr_warn("cpuidle-powernv: discovered idle states more than allowed"); | |
263 | dt_idle_states = CPUIDLE_STATE_MAX - 1; | |
264 | } | |
265 | ||
70734a78 PM |
266 | if (of_property_read_u32_array(power_mgt, |
267 | "ibm,cpu-idle-state-flags", flags, dt_idle_states)) { | |
268 | pr_warn("cpuidle-powernv : missing ibm,cpu-idle-state-flags in DT\n"); | |
957efced | 269 | goto out; |
70734a78 | 270 | } |
92c83ff5 | 271 | |
957efced SP |
272 | if (of_property_read_u32_array(power_mgt, |
273 | "ibm,cpu-idle-state-latencies-ns", latency_ns, | |
274 | dt_idle_states)) { | |
92c83ff5 | 275 | pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-latencies-ns in DT\n"); |
957efced | 276 | goto out; |
74aa51b5 | 277 | } |
3005c597 SP |
278 | if (of_property_read_string_array(power_mgt, |
279 | "ibm,cpu-idle-state-names", names, dt_idle_states) < 0) { | |
280 | pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-names in DT\n"); | |
281 | goto out; | |
282 | } | |
283 | ||
284 | /* | |
285 | * If the idle states use stop instruction, probe for psscr values | |
09206b60 | 286 | * and psscr mask which are necessary to specify required stop level. |
3005c597 | 287 | */ |
09206b60 GS |
288 | has_stop_states = (flags[0] & |
289 | (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP)); | |
290 | if (has_stop_states) { | |
ecad4502 GS |
291 | count = of_property_count_u64_elems(power_mgt, |
292 | "ibm,cpu-idle-state-psscr"); | |
293 | if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", | |
294 | dt_idle_states, | |
295 | "ibm,cpu-idle-state-psscr", | |
296 | count) != 0) | |
297 | goto out; | |
298 | ||
299 | count = of_property_count_u64_elems(power_mgt, | |
300 | "ibm,cpu-idle-state-psscr-mask"); | |
301 | if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", | |
302 | dt_idle_states, | |
303 | "ibm,cpu-idle-state-psscr-mask", | |
304 | count) != 0) | |
305 | goto out; | |
306 | ||
3005c597 SP |
307 | if (of_property_read_u64_array(power_mgt, |
308 | "ibm,cpu-idle-state-psscr", psscr_val, dt_idle_states)) { | |
09206b60 | 309 | pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n"); |
3005c597 SP |
310 | goto out; |
311 | } | |
74aa51b5 | 312 | |
09206b60 GS |
313 | if (of_property_read_u64_array(power_mgt, |
314 | "ibm,cpu-idle-state-psscr-mask", | |
315 | psscr_mask, dt_idle_states)) { | |
316 | pr_warn("cpuidle-powernv:Missing ibm,cpu-idle-state-psscr-mask in DT\n"); | |
317 | goto out; | |
318 | } | |
319 | } | |
320 | ||
ecad4502 GS |
321 | count = of_property_count_u32_elems(power_mgt, |
322 | "ibm,cpu-idle-state-residency-ns"); | |
323 | ||
324 | if (count < 0) { | |
325 | rc = count; | |
326 | } else if (validate_dt_prop_sizes("ibm,cpu-idle-state-flags", | |
327 | dt_idle_states, | |
328 | "ibm,cpu-idle-state-residency-ns", | |
329 | count) != 0) { | |
330 | goto out; | |
331 | } else { | |
332 | rc = of_property_read_u32_array(power_mgt, | |
333 | "ibm,cpu-idle-state-residency-ns", | |
334 | residency_ns, dt_idle_states); | |
335 | } | |
0888839c PM |
336 | |
337 | for (i = 0; i < dt_idle_states; i++) { | |
9e9fc6f0 | 338 | unsigned int exit_latency, target_residency; |
3005c597 SP |
339 | /* |
340 | * If an idle state has exit latency beyond | |
341 | * POWERNV_THRESHOLD_LATENCY_NS then don't use it | |
342 | * in cpu-idle. | |
343 | */ | |
344 | if (latency_ns[i] > POWERNV_THRESHOLD_LATENCY_NS) | |
345 | continue; | |
9e9fc6f0 GS |
346 | /* |
347 | * Firmware passes residency and latency values in ns. | |
348 | * cpuidle expects it in us. | |
349 | */ | |
350 | exit_latency = latency_ns[i] / 1000; | |
351 | if (!rc) | |
352 | target_residency = residency_ns[i] / 1000; | |
353 | else | |
354 | target_residency = 0; | |
0888839c | 355 | |
09206b60 GS |
356 | if (has_stop_states) { |
357 | int err = validate_psscr_val_mask(&psscr_val[i], | |
358 | &psscr_mask[i], | |
359 | flags[i]); | |
360 | if (err) { | |
361 | report_invalid_psscr_val(psscr_val[i], err); | |
362 | continue; | |
363 | } | |
364 | } | |
365 | ||
92c83ff5 | 366 | /* |
9e9fc6f0 GS |
367 | * For nap and fastsleep, use default target_residency |
368 | * values if f/w does not expose it. | |
74aa51b5 | 369 | */ |
70734a78 | 370 | if (flags[i] & OPAL_PM_NAP_ENABLED) { |
9e9fc6f0 GS |
371 | if (!rc) |
372 | target_residency = 100; | |
0888839c | 373 | /* Add NAP state */ |
9e9fc6f0 GS |
374 | add_powernv_state(nr_idle_states, "Nap", |
375 | CPUIDLE_FLAG_NONE, nap_loop, | |
09206b60 | 376 | target_residency, exit_latency, 0, 0); |
3005c597 SP |
377 | } else if ((flags[i] & OPAL_PM_STOP_INST_FAST) && |
378 | !(flags[i] & OPAL_PM_TIMEBASE_STOP)) { | |
9e9fc6f0 GS |
379 | add_powernv_state(nr_idle_states, names[i], |
380 | CPUIDLE_FLAG_NONE, stop_loop, | |
381 | target_residency, exit_latency, | |
09206b60 | 382 | psscr_val[i], psscr_mask[i]); |
cc5a2f7b | 383 | } |
384 | ||
385 | /* | |
386 | * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come | |
387 | * within this config dependency check. | |
388 | */ | |
389 | #ifdef CONFIG_TICK_ONESHOT | |
390 | if (flags[i] & OPAL_PM_SLEEP_ENABLED || | |
70734a78 | 391 | flags[i] & OPAL_PM_SLEEP_ENABLED_ER1) { |
9e9fc6f0 GS |
392 | if (!rc) |
393 | target_residency = 300000; | |
0888839c | 394 | /* Add FASTSLEEP state */ |
9e9fc6f0 GS |
395 | add_powernv_state(nr_idle_states, "FastSleep", |
396 | CPUIDLE_FLAG_TIMER_STOP, | |
397 | fastsleep_loop, | |
09206b60 | 398 | target_residency, exit_latency, 0, 0); |
3005c597 SP |
399 | } else if ((flags[i] & OPAL_PM_STOP_INST_DEEP) && |
400 | (flags[i] & OPAL_PM_TIMEBASE_STOP)) { | |
9e9fc6f0 GS |
401 | add_powernv_state(nr_idle_states, names[i], |
402 | CPUIDLE_FLAG_TIMER_STOP, stop_loop, | |
403 | target_residency, exit_latency, | |
09206b60 | 404 | psscr_val[i], psscr_mask[i]); |
0888839c | 405 | } |
cc5a2f7b | 406 | #endif |
92c83ff5 | 407 | nr_idle_states++; |
0888839c | 408 | } |
92c83ff5 | 409 | out: |
0888839c PM |
410 | return nr_idle_states; |
411 | } | |
412 | ||
2c2e6ecf DD |
413 | /* |
414 | * powernv_idle_probe() | |
415 | * Choose state table for shared versus dedicated partition | |
416 | */ | |
417 | static int powernv_idle_probe(void) | |
418 | { | |
2c2e6ecf DD |
419 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
420 | return -ENODEV; | |
421 | ||
e4d54f71 | 422 | if (firmware_has_feature(FW_FEATURE_OPAL)) { |
2c2e6ecf | 423 | cpuidle_state_table = powernv_states; |
0888839c PM |
424 | /* Device tree can indicate more idle states */ |
425 | max_idle_state = powernv_add_idle_states(); | |
78eaa10f SB |
426 | if (max_idle_state > 1) { |
427 | snooze_timeout_en = true; | |
428 | snooze_timeout = powernv_states[1].target_residency * | |
429 | tb_ticks_per_usec; | |
430 | } | |
2c2e6ecf DD |
431 | } else |
432 | return -ENODEV; | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
437 | static int __init powernv_processor_idle_init(void) | |
438 | { | |
439 | int retval; | |
440 | ||
441 | retval = powernv_idle_probe(); | |
442 | if (retval) | |
443 | return retval; | |
444 | ||
445 | powernv_cpuidle_driver_init(); | |
446 | retval = cpuidle_register(&powernv_idle_driver, NULL); | |
447 | if (retval) { | |
448 | printk(KERN_DEBUG "Registration of powernv driver failed.\n"); | |
449 | return retval; | |
450 | } | |
451 | ||
10fcca9d SAS |
452 | retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
453 | "cpuidle/powernv:online", | |
454 | powernv_cpuidle_cpu_online, NULL); | |
455 | WARN_ON(retval < 0); | |
456 | retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD, | |
457 | "cpuidle/powernv:dead", NULL, | |
458 | powernv_cpuidle_cpu_dead); | |
459 | WARN_ON(retval < 0); | |
2c2e6ecf DD |
460 | printk(KERN_DEBUG "powernv_idle_driver registered\n"); |
461 | return 0; | |
462 | } | |
463 | ||
464 | device_initcall(powernv_processor_idle_init); |