Merge branch 'timers/core' into sched/idle
[linux-2.6-block.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>
288f023e 22#include <trace/events/power.h>
4f86d3a8
LB
23
24#include "cpuidle.h"
25
26DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
4c637b21 27DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
4f86d3a8
LB
28
29DEFINE_MUTEX(cpuidle_lock);
30LIST_HEAD(cpuidle_detected_devices);
4f86d3a8
LB
31
32static int enabled_devices;
62027aea 33static int off __read_mostly;
a0bfa137 34static int initialized __read_mostly;
62027aea
LB
35
36int cpuidle_disabled(void)
37{
38 return off;
39}
d91ee586
LB
40void disable_cpuidle(void)
41{
42 off = 1;
43}
4f86d3a8 44
1a022e3f
BO
45/**
46 * cpuidle_play_dead - cpu off-lining
47 *
ee01e663 48 * Returns in case of an error or no driver
1a022e3f
BO
49 */
50int cpuidle_play_dead(void)
51{
52 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 53 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
8aef33a7 54 int i;
1a022e3f 55
ee01e663
TK
56 if (!drv)
57 return -ENODEV;
58
1a022e3f 59 /* Find lowest-power state that supports long-term idle */
8aef33a7
DL
60 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
61 if (drv->states[i].enter_dead)
62 return drv->states[i].enter_dead(dev, i);
1a022e3f
BO
63
64 return -ENODEV;
65}
66
56cfbf74
CC
67/**
68 * cpuidle_enter_state - enter the state and update stats
69 * @dev: cpuidle device for this cpu
70 * @drv: cpuidle driver for this cpu
71 * @next_state: index into drv->states of the state to enter
72 */
73int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
554c06ba 74 int index)
56cfbf74
CC
75{
76 int entered_state;
77
554c06ba
DL
78 struct cpuidle_state *target_state = &drv->states[index];
79 ktime_t time_start, time_end;
80 s64 diff;
81
82 time_start = ktime_get();
83
84 entered_state = target_state->enter(dev, drv, index);
85
86 time_end = ktime_get();
87
88 local_irq_enable();
89
90 diff = ktime_to_us(ktime_sub(time_end, time_start));
91 if (diff > INT_MAX)
92 diff = INT_MAX;
93
94 dev->last_residency = (int) diff;
56cfbf74
CC
95
96 if (entered_state >= 0) {
97 /* Update cpuidle counters */
98 /* This can be moved to within driver enter routine
99 * but that results in multiple copies of same code.
100 */
a474a515 101 dev->states_usage[entered_state].time += dev->last_residency;
56cfbf74
CC
102 dev->states_usage[entered_state].usage++;
103 } else {
104 dev->last_residency = 0;
105 }
106
107 return entered_state;
108}
109
4f86d3a8
LB
110/**
111 * cpuidle_idle_call - the main idle loop
112 *
113 * NOTE: no locks or semaphores should be used here
a0bfa137 114 * return non-zero on failure
4f86d3a8 115 */
a0bfa137 116int cpuidle_idle_call(void)
4f86d3a8 117{
4a6f4fe8 118 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 119 struct cpuidle_driver *drv;
e978aa7d 120 int next_state, entered_state;
fb11c9c6 121 bool broadcast;
4f86d3a8 122
9b29a86f 123 if (off || !initialized)
a0bfa137
LB
124 return -ENODEV;
125
4f86d3a8 126 /* check if the device is ready */
a0bfa137
LB
127 if (!dev || !dev->enabled)
128 return -EBUSY;
4f86d3a8 129
bf4d1b5d
DL
130 drv = cpuidle_get_cpu_driver(dev);
131
4f86d3a8 132 /* ask the governor for the next state */
46bcfad7 133 next_state = cpuidle_curr_governor->select(drv, dev);
246eb7f0 134 if (need_resched()) {
d73d68dc
YS
135 dev->last_residency = 0;
136 /* give the governor an opportunity to reflect on the outcome */
137 if (cpuidle_curr_governor->reflect)
138 cpuidle_curr_governor->reflect(dev, next_state);
246eb7f0 139 local_irq_enable();
a0bfa137 140 return 0;
246eb7f0
KH
141 }
142
fb11c9c6
VK
143 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
144
ba8f20c2
PM
145 if (broadcast &&
146 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
147 return -EBUSY;
148
149
150 trace_cpu_idle_rcuidle(next_state, dev->cpu);
b60e6a0e 151
4126c019
CC
152 if (cpuidle_state_is_coupled(dev, drv, next_state))
153 entered_state = cpuidle_enter_state_coupled(dev, drv,
154 next_state);
155 else
156 entered_state = cpuidle_enter_state(dev, drv, next_state);
f77cfe4e 157
ba8f20c2
PM
158 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
159
fb11c9c6 160 if (broadcast)
6d281e97 161 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
b60e6a0e 162
4f86d3a8
LB
163 /* give the governor an opportunity to reflect on the outcome */
164 if (cpuidle_curr_governor->reflect)
e978aa7d 165 cpuidle_curr_governor->reflect(dev, entered_state);
a0bfa137
LB
166
167 return 0;
4f86d3a8
LB
168}
169
170/**
171 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
172 */
173void cpuidle_install_idle_handler(void)
174{
a0bfa137 175 if (enabled_devices) {
4f86d3a8
LB
176 /* Make sure all changes finished before we switch to new idle */
177 smp_wmb();
a0bfa137 178 initialized = 1;
4f86d3a8
LB
179 }
180}
181
182/**
183 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
184 */
185void cpuidle_uninstall_idle_handler(void)
186{
a0bfa137
LB
187 if (enabled_devices) {
188 initialized = 0;
4a162513 189 kick_all_cpus_sync();
4f86d3a8
LB
190 }
191}
192
193/**
194 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
195 */
196void cpuidle_pause_and_lock(void)
197{
198 mutex_lock(&cpuidle_lock);
199 cpuidle_uninstall_idle_handler();
200}
201
202EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
203
204/**
205 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
206 */
207void cpuidle_resume_and_unlock(void)
208{
209 cpuidle_install_idle_handler();
210 mutex_unlock(&cpuidle_lock);
211}
212
213EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
214
8651f97b
PM
215/* Currently used in suspend/resume path to suspend cpuidle */
216void cpuidle_pause(void)
217{
218 mutex_lock(&cpuidle_lock);
219 cpuidle_uninstall_idle_handler();
220 mutex_unlock(&cpuidle_lock);
221}
222
223/* Currently used in suspend/resume path to resume cpuidle */
224void cpuidle_resume(void)
225{
226 mutex_lock(&cpuidle_lock);
227 cpuidle_install_idle_handler();
228 mutex_unlock(&cpuidle_lock);
229}
230
4f86d3a8
LB
231/**
232 * cpuidle_enable_device - enables idle PM for a CPU
233 * @dev: the CPU
234 *
235 * This function must be called between cpuidle_pause_and_lock and
236 * cpuidle_resume_and_unlock when used externally.
237 */
238int cpuidle_enable_device(struct cpuidle_device *dev)
239{
5df0aa73 240 int ret;
bf4d1b5d 241 struct cpuidle_driver *drv;
4f86d3a8 242
1b0a0e9a
SB
243 if (!dev)
244 return -EINVAL;
245
4f86d3a8
LB
246 if (dev->enabled)
247 return 0;
bf4d1b5d
DL
248
249 drv = cpuidle_get_cpu_driver(dev);
250
e1689795 251 if (!drv || !cpuidle_curr_governor)
4f86d3a8 252 return -EIO;
bf4d1b5d 253
10b9d3f8
DL
254 if (!dev->registered)
255 return -EINVAL;
256
4f86d3a8 257 if (!dev->state_count)
fc850f39 258 dev->state_count = drv->state_count;
4f86d3a8 259
bf4d1b5d
DL
260 ret = cpuidle_add_device_sysfs(dev);
261 if (ret)
4f86d3a8
LB
262 return ret;
263
264 if (cpuidle_curr_governor->enable &&
e1689795 265 (ret = cpuidle_curr_governor->enable(drv, dev)))
4f86d3a8
LB
266 goto fail_sysfs;
267
4f86d3a8
LB
268 smp_wmb();
269
270 dev->enabled = 1;
271
272 enabled_devices++;
273 return 0;
274
275fail_sysfs:
bf4d1b5d 276 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
277
278 return ret;
279}
280
281EXPORT_SYMBOL_GPL(cpuidle_enable_device);
282
283/**
284 * cpuidle_disable_device - disables idle PM for a CPU
285 * @dev: the CPU
286 *
287 * This function must be called between cpuidle_pause_and_lock and
288 * cpuidle_resume_and_unlock when used externally.
289 */
290void cpuidle_disable_device(struct cpuidle_device *dev)
291{
bf4d1b5d
DL
292 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
293
cf31cd1a 294 if (!dev || !dev->enabled)
4f86d3a8 295 return;
bf4d1b5d
DL
296
297 if (!drv || !cpuidle_curr_governor)
4f86d3a8
LB
298 return;
299
300 dev->enabled = 0;
301
302 if (cpuidle_curr_governor->disable)
bf4d1b5d 303 cpuidle_curr_governor->disable(drv, dev);
4f86d3a8 304
bf4d1b5d 305 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
306 enabled_devices--;
307}
308
309EXPORT_SYMBOL_GPL(cpuidle_disable_device);
310
f6bb51a5
DL
311static void __cpuidle_unregister_device(struct cpuidle_device *dev)
312{
313 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
314
315 list_del(&dev->device_list);
316 per_cpu(cpuidle_devices, dev->cpu) = NULL;
317 module_put(drv->owner);
318}
319
267d4bf8 320static void __cpuidle_device_init(struct cpuidle_device *dev)
5df0aa73
DL
321{
322 memset(dev->states_usage, 0, sizeof(dev->states_usage));
323 dev->last_residency = 0;
5df0aa73
DL
324}
325
4f86d3a8 326/**
dcb84f33
VP
327 * __cpuidle_register_device - internal register function called before register
328 * and enable routines
4f86d3a8 329 * @dev: the cpu
dcb84f33
VP
330 *
331 * cpuidle_lock mutex must be held before this is called
4f86d3a8 332 */
dcb84f33 333static int __cpuidle_register_device(struct cpuidle_device *dev)
4f86d3a8
LB
334{
335 int ret;
bf4d1b5d 336 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4f86d3a8 337
bf4d1b5d 338 if (!try_module_get(drv->owner))
4f86d3a8
LB
339 return -EINVAL;
340
4f86d3a8
LB
341 per_cpu(cpuidle_devices, dev->cpu) = dev;
342 list_add(&dev->device_list, &cpuidle_detected_devices);
4f86d3a8 343
4126c019 344 ret = cpuidle_coupled_register_device(dev);
47182668 345 if (ret)
f6bb51a5 346 __cpuidle_unregister_device(dev);
47182668
VK
347 else
348 dev->registered = 1;
4f86d3a8 349
47182668 350 return ret;
dcb84f33
VP
351}
352
353/**
354 * cpuidle_register_device - registers a CPU's idle PM feature
355 * @dev: the cpu
356 */
357int cpuidle_register_device(struct cpuidle_device *dev)
358{
c878a52d 359 int ret = -EBUSY;
dcb84f33 360
1b0a0e9a
SB
361 if (!dev)
362 return -EINVAL;
363
dcb84f33
VP
364 mutex_lock(&cpuidle_lock);
365
c878a52d
DL
366 if (dev->registered)
367 goto out_unlock;
368
267d4bf8 369 __cpuidle_device_init(dev);
5df0aa73 370
f6bb51a5
DL
371 ret = __cpuidle_register_device(dev);
372 if (ret)
373 goto out_unlock;
374
375 ret = cpuidle_add_sysfs(dev);
376 if (ret)
377 goto out_unregister;
dcb84f33 378
10b9d3f8 379 ret = cpuidle_enable_device(dev);
f6bb51a5
DL
380 if (ret)
381 goto out_sysfs;
10b9d3f8 382
4f86d3a8
LB
383 cpuidle_install_idle_handler();
384
f6bb51a5 385out_unlock:
4f86d3a8
LB
386 mutex_unlock(&cpuidle_lock);
387
f6bb51a5
DL
388 return ret;
389
390out_sysfs:
391 cpuidle_remove_sysfs(dev);
392out_unregister:
393 __cpuidle_unregister_device(dev);
394 goto out_unlock;
4f86d3a8
LB
395}
396
397EXPORT_SYMBOL_GPL(cpuidle_register_device);
398
399/**
400 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
401 * @dev: the cpu
402 */
403void cpuidle_unregister_device(struct cpuidle_device *dev)
404{
813e8e3d 405 if (!dev || dev->registered == 0)
dcb84f33
VP
406 return;
407
4f86d3a8
LB
408 cpuidle_pause_and_lock();
409
410 cpuidle_disable_device(dev);
411
1aef40e2 412 cpuidle_remove_sysfs(dev);
f6bb51a5
DL
413
414 __cpuidle_unregister_device(dev);
4f86d3a8 415
4126c019
CC
416 cpuidle_coupled_unregister_device(dev);
417
4f86d3a8 418 cpuidle_resume_and_unlock();
4f86d3a8
LB
419}
420
421EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
422
1c192d04 423/**
4c637b21
DL
424 * cpuidle_unregister: unregister a driver and the devices. This function
425 * can be used only if the driver has been previously registered through
426 * the cpuidle_register function.
427 *
428 * @drv: a valid pointer to a struct cpuidle_driver
429 */
430void cpuidle_unregister(struct cpuidle_driver *drv)
431{
432 int cpu;
433 struct cpuidle_device *device;
434
82467a5a 435 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
436 device = &per_cpu(cpuidle_dev, cpu);
437 cpuidle_unregister_device(device);
438 }
439
440 cpuidle_unregister_driver(drv);
441}
442EXPORT_SYMBOL_GPL(cpuidle_unregister);
443
444/**
445 * cpuidle_register: registers the driver and the cpu devices with the
446 * coupled_cpus passed as parameter. This function is used for all common
447 * initialization pattern there are in the arch specific drivers. The
448 * devices is globally defined in this file.
449 *
450 * @drv : a valid pointer to a struct cpuidle_driver
451 * @coupled_cpus: a cpumask for the coupled states
452 *
453 * Returns 0 on success, < 0 otherwise
454 */
455int cpuidle_register(struct cpuidle_driver *drv,
456 const struct cpumask *const coupled_cpus)
457{
458 int ret, cpu;
459 struct cpuidle_device *device;
460
461 ret = cpuidle_register_driver(drv);
462 if (ret) {
463 pr_err("failed to register cpuidle driver\n");
464 return ret;
465 }
466
82467a5a 467 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
468 device = &per_cpu(cpuidle_dev, cpu);
469 device->cpu = cpu;
470
471#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
472 /*
caf4a36e 473 * On multiplatform for ARM, the coupled idle states could be
4c637b21
DL
474 * enabled in the kernel even if the cpuidle driver does not
475 * use it. Note, coupled_cpus is a struct copy.
476 */
477 if (coupled_cpus)
478 device->coupled_cpus = *coupled_cpus;
479#endif
480 ret = cpuidle_register_device(device);
481 if (!ret)
482 continue;
483
484 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
485
486 cpuidle_unregister(drv);
487 break;
488 }
489
490 return ret;
491}
492EXPORT_SYMBOL_GPL(cpuidle_register);
493
4f86d3a8
LB
494#ifdef CONFIG_SMP
495
496static void smp_callback(void *v)
497{
498 /* we already woke the CPU up, nothing more to do */
499}
500
501/*
502 * This function gets called when a part of the kernel has a new latency
503 * requirement. This means we need to get all processors out of their C-state,
504 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
505 * wakes them all right up.
506 */
507static int cpuidle_latency_notify(struct notifier_block *b,
508 unsigned long l, void *v)
509{
8691e5a8 510 smp_call_function(smp_callback, NULL, 1);
4f86d3a8
LB
511 return NOTIFY_OK;
512}
513
514static struct notifier_block cpuidle_latency_notifier = {
515 .notifier_call = cpuidle_latency_notify,
516};
517
d82b3518
MG
518static inline void latency_notifier_init(struct notifier_block *n)
519{
520 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
521}
4f86d3a8
LB
522
523#else /* CONFIG_SMP */
524
525#define latency_notifier_init(x) do { } while (0)
526
527#endif /* CONFIG_SMP */
528
529/**
530 * cpuidle_init - core initializer
531 */
532static int __init cpuidle_init(void)
533{
534 int ret;
535
62027aea
LB
536 if (cpuidle_disabled())
537 return -ENODEV;
538
8a25a2fd 539 ret = cpuidle_add_interface(cpu_subsys.dev_root);
4f86d3a8
LB
540 if (ret)
541 return ret;
542
543 latency_notifier_init(&cpuidle_latency_notifier);
544
545 return 0;
546}
547
62027aea 548module_param(off, int, 0444);
4f86d3a8 549core_initcall(cpuidle_init);