PM / sleep: Make it possible to quiesce timers during suspend-to-idle
[deliverable/linux.git] / drivers / cpuidle / cpuidle.c
CommitLineData
4f86d3a8
LB
1/*
2 * cpuidle.c - core cpuidle infrastructure
3 *
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
7 *
8 * This code is licenced under the GPL.
9 */
10
b60e6a0e 11#include <linux/clockchips.h>
4f86d3a8
LB
12#include <linux/kernel.h>
13#include <linux/mutex.h>
14#include <linux/sched.h>
15#include <linux/notifier.h>
e8db0be1 16#include <linux/pm_qos.h>
4f86d3a8
LB
17#include <linux/cpu.h>
18#include <linux/cpuidle.h>
9a0b8415 19#include <linux/ktime.h>
2e94d1f7 20#include <linux/hrtimer.h>
884b17e1 21#include <linux/module.h>
38106313 22#include <linux/suspend.h>
124cf911 23#include <linux/tick.h>
288f023e 24#include <trace/events/power.h>
4f86d3a8
LB
25
26#include "cpuidle.h"
27
28DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
4c637b21 29DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
4f86d3a8
LB
30
31DEFINE_MUTEX(cpuidle_lock);
32LIST_HEAD(cpuidle_detected_devices);
4f86d3a8
LB
33
34static int enabled_devices;
62027aea 35static int off __read_mostly;
a0bfa137 36static int initialized __read_mostly;
62027aea
LB
37
38int cpuidle_disabled(void)
39{
40 return off;
41}
d91ee586
LB
42void disable_cpuidle(void)
43{
44 off = 1;
45}
4f86d3a8 46
1a022e3f
BO
47/**
48 * cpuidle_play_dead - cpu off-lining
49 *
ee01e663 50 * Returns in case of an error or no driver
1a022e3f
BO
51 */
52int cpuidle_play_dead(void)
53{
54 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 55 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
8aef33a7 56 int i;
1a022e3f 57
ee01e663
TK
58 if (!drv)
59 return -ENODEV;
60
1a022e3f 61 /* Find lowest-power state that supports long-term idle */
8aef33a7
DL
62 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
63 if (drv->states[i].enter_dead)
64 return drv->states[i].enter_dead(dev, i);
1a022e3f
BO
65
66 return -ENODEV;
67}
68
a6220fc1 69/**
38106313
RW
70 * cpuidle_find_deepest_state - Find deepest state meeting specific conditions.
71 * @drv: cpuidle driver for the given CPU.
72 * @dev: cpuidle device for the given CPU.
124cf911 73 * @freeze: Whether or not the state should be suitable for suspend-to-idle.
a6220fc1
RW
74 */
75static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
124cf911 76 struct cpuidle_device *dev, bool freeze)
a6220fc1
RW
77{
78 unsigned int latency_req = 0;
124cf911 79 int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1;
a6220fc1
RW
80
81 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
82 struct cpuidle_state *s = &drv->states[i];
83 struct cpuidle_state_usage *su = &dev->states_usage[i];
84
124cf911
RW
85 if (s->disabled || su->disable || s->exit_latency <= latency_req
86 || (freeze && !s->enter_freeze))
a6220fc1
RW
87 continue;
88
89 latency_req = s->exit_latency;
90 ret = i;
91 }
92 return ret;
93}
94
124cf911
RW
95static void enter_freeze_proper(struct cpuidle_driver *drv,
96 struct cpuidle_device *dev, int index)
97{
98 tick_freeze();
99 /*
100 * The state used here cannot be a "coupled" one, because the "coupled"
101 * cpuidle mechanism enables interrupts and doing that with timekeeping
102 * suspended is generally unsafe.
103 */
104 drv->states[index].enter_freeze(dev, drv, index);
105 WARN_ON(!irqs_disabled());
106 /*
107 * timekeeping_resume() that will be called by tick_unfreeze() for the
108 * last CPU executing it calls functions containing RCU read-side
109 * critical sections, so tell RCU about that.
110 */
111 RCU_NONIDLE(tick_unfreeze());
112}
113
38106313
RW
114/**
115 * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
116 *
124cf911
RW
117 * If there are states with the ->enter_freeze callback, find the deepest of
118 * them and enter it with frozen tick. Otherwise, find the deepest state
119 * available and enter it normally.
38106313
RW
120 */
121void cpuidle_enter_freeze(void)
122{
123 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
124 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
125 int index;
126
124cf911
RW
127 /*
128 * Find the deepest state with ->enter_freeze present, which guarantees
129 * that interrupts won't be enabled when it exits and allows the tick to
130 * be frozen safely.
131 */
132 index = cpuidle_find_deepest_state(drv, dev, true);
133 if (index >= 0) {
134 enter_freeze_proper(drv, dev, index);
135 return;
136 }
137
138 /*
139 * It is not safe to freeze the tick, find the deepest state available
140 * at all and try to enter it normally.
141 */
142 index = cpuidle_find_deepest_state(drv, dev, false);
38106313
RW
143 if (index >= 0)
144 cpuidle_enter(drv, dev, index);
145 else
146 arch_cpu_idle();
147
148 /* Interrupts are enabled again here. */
149 local_irq_disable();
150}
151
56cfbf74
CC
152/**
153 * cpuidle_enter_state - enter the state and update stats
154 * @dev: cpuidle device for this cpu
155 * @drv: cpuidle driver for this cpu
156 * @next_state: index into drv->states of the state to enter
157 */
158int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
554c06ba 159 int index)
56cfbf74
CC
160{
161 int entered_state;
162
554c06ba
DL
163 struct cpuidle_state *target_state = &drv->states[index];
164 ktime_t time_start, time_end;
165 s64 diff;
166
30fe6884 167 trace_cpu_idle_rcuidle(index, dev->cpu);
554c06ba
DL
168 time_start = ktime_get();
169
170 entered_state = target_state->enter(dev, drv, index);
171
172 time_end = ktime_get();
30fe6884 173 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
554c06ba 174
0b89e9aa
PB
175 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
176 local_irq_enable();
554c06ba
DL
177
178 diff = ktime_to_us(ktime_sub(time_end, time_start));
179 if (diff > INT_MAX)
180 diff = INT_MAX;
181
182 dev->last_residency = (int) diff;
56cfbf74
CC
183
184 if (entered_state >= 0) {
185 /* Update cpuidle counters */
186 /* This can be moved to within driver enter routine
187 * but that results in multiple copies of same code.
188 */
a474a515 189 dev->states_usage[entered_state].time += dev->last_residency;
56cfbf74
CC
190 dev->states_usage[entered_state].usage++;
191 } else {
192 dev->last_residency = 0;
193 }
194
195 return entered_state;
196}
197
4f86d3a8 198/**
907e30f1
DL
199 * cpuidle_select - ask the cpuidle framework to choose an idle state
200 *
201 * @drv: the cpuidle driver
202 * @dev: the cpuidle device
4f86d3a8 203 *
907e30f1 204 * Returns the index of the idle state.
4f86d3a8 205 */
907e30f1 206int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
4f86d3a8 207{
52c324f8
RW
208 if (off || !initialized)
209 return -ENODEV;
210
211 if (!drv || !dev || !dev->enabled)
212 return -EBUSY;
213
907e30f1
DL
214 return cpuidle_curr_governor->select(drv, dev);
215}
ba8f20c2 216
907e30f1
DL
217/**
218 * cpuidle_enter - enter into the specified idle state
219 *
220 * @drv: the cpuidle driver tied with the cpu
221 * @dev: the cpuidle device
222 * @index: the index in the idle state table
223 *
224 * Returns the index in the idle state, < 0 in case of error.
225 * The error code depends on the backend driver
226 */
227int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
228 int index)
229{
230 if (cpuidle_state_is_coupled(dev, drv, index))
231 return cpuidle_enter_state_coupled(dev, drv, index);
232 return cpuidle_enter_state(dev, drv, index);
233}
b60e6a0e 234
907e30f1
DL
235/**
236 * cpuidle_reflect - tell the underlying governor what was the state
237 * we were in
238 *
239 * @dev : the cpuidle device
240 * @index: the index in the idle state table
241 *
242 */
243void cpuidle_reflect(struct cpuidle_device *dev, int index)
244{
38106313 245 if (cpuidle_curr_governor->reflect)
907e30f1 246 cpuidle_curr_governor->reflect(dev, index);
4f86d3a8
LB
247}
248
249/**
250 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
251 */
252void cpuidle_install_idle_handler(void)
253{
a0bfa137 254 if (enabled_devices) {
4f86d3a8
LB
255 /* Make sure all changes finished before we switch to new idle */
256 smp_wmb();
a0bfa137 257 initialized = 1;
4f86d3a8
LB
258 }
259}
260
261/**
262 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
263 */
264void cpuidle_uninstall_idle_handler(void)
265{
a0bfa137
LB
266 if (enabled_devices) {
267 initialized = 0;
2ed903c5 268 wake_up_all_idle_cpus();
4f86d3a8 269 }
442bf3aa
DL
270
271 /*
272 * Make sure external observers (such as the scheduler)
273 * are done looking at pointed idle states.
274 */
275 synchronize_rcu();
4f86d3a8
LB
276}
277
278/**
279 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
280 */
281void cpuidle_pause_and_lock(void)
282{
283 mutex_lock(&cpuidle_lock);
284 cpuidle_uninstall_idle_handler();
285}
286
287EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
288
289/**
290 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
291 */
292void cpuidle_resume_and_unlock(void)
293{
294 cpuidle_install_idle_handler();
295 mutex_unlock(&cpuidle_lock);
296}
297
298EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
299
8651f97b
PM
300/* Currently used in suspend/resume path to suspend cpuidle */
301void cpuidle_pause(void)
302{
303 mutex_lock(&cpuidle_lock);
304 cpuidle_uninstall_idle_handler();
305 mutex_unlock(&cpuidle_lock);
306}
307
308/* Currently used in suspend/resume path to resume cpuidle */
309void cpuidle_resume(void)
310{
311 mutex_lock(&cpuidle_lock);
312 cpuidle_install_idle_handler();
313 mutex_unlock(&cpuidle_lock);
314}
315
4f86d3a8
LB
316/**
317 * cpuidle_enable_device - enables idle PM for a CPU
318 * @dev: the CPU
319 *
320 * This function must be called between cpuidle_pause_and_lock and
321 * cpuidle_resume_and_unlock when used externally.
322 */
323int cpuidle_enable_device(struct cpuidle_device *dev)
324{
5df0aa73 325 int ret;
bf4d1b5d 326 struct cpuidle_driver *drv;
4f86d3a8 327
1b0a0e9a
SB
328 if (!dev)
329 return -EINVAL;
330
4f86d3a8
LB
331 if (dev->enabled)
332 return 0;
bf4d1b5d
DL
333
334 drv = cpuidle_get_cpu_driver(dev);
335
e1689795 336 if (!drv || !cpuidle_curr_governor)
4f86d3a8 337 return -EIO;
bf4d1b5d 338
10b9d3f8
DL
339 if (!dev->registered)
340 return -EINVAL;
341
4f86d3a8 342 if (!dev->state_count)
fc850f39 343 dev->state_count = drv->state_count;
4f86d3a8 344
bf4d1b5d
DL
345 ret = cpuidle_add_device_sysfs(dev);
346 if (ret)
4f86d3a8
LB
347 return ret;
348
349 if (cpuidle_curr_governor->enable &&
e1689795 350 (ret = cpuidle_curr_governor->enable(drv, dev)))
4f86d3a8
LB
351 goto fail_sysfs;
352
4f86d3a8
LB
353 smp_wmb();
354
355 dev->enabled = 1;
356
357 enabled_devices++;
358 return 0;
359
360fail_sysfs:
bf4d1b5d 361 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
362
363 return ret;
364}
365
366EXPORT_SYMBOL_GPL(cpuidle_enable_device);
367
368/**
369 * cpuidle_disable_device - disables idle PM for a CPU
370 * @dev: the CPU
371 *
372 * This function must be called between cpuidle_pause_and_lock and
373 * cpuidle_resume_and_unlock when used externally.
374 */
375void cpuidle_disable_device(struct cpuidle_device *dev)
376{
bf4d1b5d
DL
377 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
378
cf31cd1a 379 if (!dev || !dev->enabled)
4f86d3a8 380 return;
bf4d1b5d
DL
381
382 if (!drv || !cpuidle_curr_governor)
4f86d3a8
LB
383 return;
384
385 dev->enabled = 0;
386
387 if (cpuidle_curr_governor->disable)
bf4d1b5d 388 cpuidle_curr_governor->disable(drv, dev);
4f86d3a8 389
bf4d1b5d 390 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
391 enabled_devices--;
392}
393
394EXPORT_SYMBOL_GPL(cpuidle_disable_device);
395
f6bb51a5
DL
396static void __cpuidle_unregister_device(struct cpuidle_device *dev)
397{
398 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
399
400 list_del(&dev->device_list);
401 per_cpu(cpuidle_devices, dev->cpu) = NULL;
402 module_put(drv->owner);
403}
404
267d4bf8 405static void __cpuidle_device_init(struct cpuidle_device *dev)
5df0aa73
DL
406{
407 memset(dev->states_usage, 0, sizeof(dev->states_usage));
408 dev->last_residency = 0;
5df0aa73
DL
409}
410
4f86d3a8 411/**
dcb84f33
VP
412 * __cpuidle_register_device - internal register function called before register
413 * and enable routines
4f86d3a8 414 * @dev: the cpu
dcb84f33
VP
415 *
416 * cpuidle_lock mutex must be held before this is called
4f86d3a8 417 */
dcb84f33 418static int __cpuidle_register_device(struct cpuidle_device *dev)
4f86d3a8
LB
419{
420 int ret;
bf4d1b5d 421 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4f86d3a8 422
bf4d1b5d 423 if (!try_module_get(drv->owner))
4f86d3a8
LB
424 return -EINVAL;
425
4f86d3a8
LB
426 per_cpu(cpuidle_devices, dev->cpu) = dev;
427 list_add(&dev->device_list, &cpuidle_detected_devices);
4f86d3a8 428
4126c019 429 ret = cpuidle_coupled_register_device(dev);
47182668 430 if (ret)
f6bb51a5 431 __cpuidle_unregister_device(dev);
47182668
VK
432 else
433 dev->registered = 1;
4f86d3a8 434
47182668 435 return ret;
dcb84f33
VP
436}
437
438/**
439 * cpuidle_register_device - registers a CPU's idle PM feature
440 * @dev: the cpu
441 */
442int cpuidle_register_device(struct cpuidle_device *dev)
443{
c878a52d 444 int ret = -EBUSY;
dcb84f33 445
1b0a0e9a
SB
446 if (!dev)
447 return -EINVAL;
448
dcb84f33
VP
449 mutex_lock(&cpuidle_lock);
450
c878a52d
DL
451 if (dev->registered)
452 goto out_unlock;
453
267d4bf8 454 __cpuidle_device_init(dev);
5df0aa73 455
f6bb51a5
DL
456 ret = __cpuidle_register_device(dev);
457 if (ret)
458 goto out_unlock;
459
460 ret = cpuidle_add_sysfs(dev);
461 if (ret)
462 goto out_unregister;
dcb84f33 463
10b9d3f8 464 ret = cpuidle_enable_device(dev);
f6bb51a5
DL
465 if (ret)
466 goto out_sysfs;
10b9d3f8 467
4f86d3a8
LB
468 cpuidle_install_idle_handler();
469
f6bb51a5 470out_unlock:
4f86d3a8
LB
471 mutex_unlock(&cpuidle_lock);
472
f6bb51a5
DL
473 return ret;
474
475out_sysfs:
476 cpuidle_remove_sysfs(dev);
477out_unregister:
478 __cpuidle_unregister_device(dev);
479 goto out_unlock;
4f86d3a8
LB
480}
481
482EXPORT_SYMBOL_GPL(cpuidle_register_device);
483
484/**
485 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
486 * @dev: the cpu
487 */
488void cpuidle_unregister_device(struct cpuidle_device *dev)
489{
813e8e3d 490 if (!dev || dev->registered == 0)
dcb84f33
VP
491 return;
492
4f86d3a8
LB
493 cpuidle_pause_and_lock();
494
495 cpuidle_disable_device(dev);
496
1aef40e2 497 cpuidle_remove_sysfs(dev);
f6bb51a5
DL
498
499 __cpuidle_unregister_device(dev);
4f86d3a8 500
4126c019
CC
501 cpuidle_coupled_unregister_device(dev);
502
4f86d3a8 503 cpuidle_resume_and_unlock();
4f86d3a8
LB
504}
505
506EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
507
1c192d04 508/**
4c637b21
DL
509 * cpuidle_unregister: unregister a driver and the devices. This function
510 * can be used only if the driver has been previously registered through
511 * the cpuidle_register function.
512 *
513 * @drv: a valid pointer to a struct cpuidle_driver
514 */
515void cpuidle_unregister(struct cpuidle_driver *drv)
516{
517 int cpu;
518 struct cpuidle_device *device;
519
82467a5a 520 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
521 device = &per_cpu(cpuidle_dev, cpu);
522 cpuidle_unregister_device(device);
523 }
524
525 cpuidle_unregister_driver(drv);
526}
527EXPORT_SYMBOL_GPL(cpuidle_unregister);
528
529/**
530 * cpuidle_register: registers the driver and the cpu devices with the
531 * coupled_cpus passed as parameter. This function is used for all common
532 * initialization pattern there are in the arch specific drivers. The
533 * devices is globally defined in this file.
534 *
535 * @drv : a valid pointer to a struct cpuidle_driver
536 * @coupled_cpus: a cpumask for the coupled states
537 *
538 * Returns 0 on success, < 0 otherwise
539 */
540int cpuidle_register(struct cpuidle_driver *drv,
541 const struct cpumask *const coupled_cpus)
542{
543 int ret, cpu;
544 struct cpuidle_device *device;
545
546 ret = cpuidle_register_driver(drv);
547 if (ret) {
548 pr_err("failed to register cpuidle driver\n");
549 return ret;
550 }
551
82467a5a 552 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
553 device = &per_cpu(cpuidle_dev, cpu);
554 device->cpu = cpu;
555
556#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
557 /*
caf4a36e 558 * On multiplatform for ARM, the coupled idle states could be
4c637b21
DL
559 * enabled in the kernel even if the cpuidle driver does not
560 * use it. Note, coupled_cpus is a struct copy.
561 */
562 if (coupled_cpus)
563 device->coupled_cpus = *coupled_cpus;
564#endif
565 ret = cpuidle_register_device(device);
566 if (!ret)
567 continue;
568
569 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
570
571 cpuidle_unregister(drv);
572 break;
573 }
574
575 return ret;
576}
577EXPORT_SYMBOL_GPL(cpuidle_register);
578
4f86d3a8
LB
579#ifdef CONFIG_SMP
580
4f86d3a8
LB
581/*
582 * This function gets called when a part of the kernel has a new latency
583 * requirement. This means we need to get all processors out of their C-state,
584 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
585 * wakes them all right up.
586 */
587static int cpuidle_latency_notify(struct notifier_block *b,
588 unsigned long l, void *v)
589{
2ed903c5 590 wake_up_all_idle_cpus();
4f86d3a8
LB
591 return NOTIFY_OK;
592}
593
594static struct notifier_block cpuidle_latency_notifier = {
595 .notifier_call = cpuidle_latency_notify,
596};
597
d82b3518
MG
598static inline void latency_notifier_init(struct notifier_block *n)
599{
600 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
601}
4f86d3a8
LB
602
603#else /* CONFIG_SMP */
604
605#define latency_notifier_init(x) do { } while (0)
606
607#endif /* CONFIG_SMP */
608
609/**
610 * cpuidle_init - core initializer
611 */
612static int __init cpuidle_init(void)
613{
614 int ret;
615
62027aea
LB
616 if (cpuidle_disabled())
617 return -ENODEV;
618
8a25a2fd 619 ret = cpuidle_add_interface(cpu_subsys.dev_root);
4f86d3a8
LB
620 if (ret)
621 return ret;
622
623 latency_notifier_init(&cpuidle_latency_notifier);
624
625 return 0;
626}
627
62027aea 628module_param(off, int, 0444);
4f86d3a8 629core_initcall(cpuidle_init);
This page took 0.538047 seconds and 5 git commands to generate.