Linux 3.12.39
[linux/fpc-iii.git] / drivers / cpuidle / cpuidle.c
blob22c07fb6ab7856b57ca64c75ac29d314767834c5
1 /*
2 * cpuidle.c - core cpuidle infrastructure
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
8 * This code is licenced under the GPL.
9 */
11 #include <linux/clockchips.h>
12 #include <linux/kernel.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/notifier.h>
16 #include <linux/pm_qos.h>
17 #include <linux/cpu.h>
18 #include <linux/cpuidle.h>
19 #include <linux/ktime.h>
20 #include <linux/hrtimer.h>
21 #include <linux/module.h>
22 #include <trace/events/power.h>
24 #include "cpuidle.h"
26 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
27 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
29 DEFINE_MUTEX(cpuidle_lock);
30 LIST_HEAD(cpuidle_detected_devices);
32 static int enabled_devices;
33 static int off __read_mostly;
34 static int initialized __read_mostly;
36 int cpuidle_disabled(void)
38 return off;
40 void disable_cpuidle(void)
42 off = 1;
45 /**
46 * cpuidle_play_dead - cpu off-lining
48 * Returns in case of an error or no driver
50 int cpuidle_play_dead(void)
52 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
53 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
54 int i;
56 if (!drv)
57 return -ENODEV;
59 /* Find lowest-power state that supports long-term idle */
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);
64 return -ENODEV;
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
73 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
74 int index)
76 int entered_state;
78 struct cpuidle_state *target_state = &drv->states[index];
79 ktime_t time_start, time_end;
80 s64 diff;
82 time_start = ktime_get();
84 entered_state = target_state->enter(dev, drv, index);
86 time_end = ktime_get();
88 local_irq_enable();
90 diff = ktime_to_us(ktime_sub(time_end, time_start));
91 if (diff > INT_MAX)
92 diff = INT_MAX;
94 dev->last_residency = (int) diff;
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.
101 dev->states_usage[entered_state].time += dev->last_residency;
102 dev->states_usage[entered_state].usage++;
103 } else {
104 dev->last_residency = 0;
107 return entered_state;
111 * cpuidle_idle_call - the main idle loop
113 * NOTE: no locks or semaphores should be used here
114 * return non-zero on failure
116 int cpuidle_idle_call(void)
118 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
119 struct cpuidle_driver *drv;
120 int next_state, entered_state;
122 if (off)
123 return -ENODEV;
125 if (!initialized)
126 return -ENODEV;
128 /* check if the device is ready */
129 if (!dev || !dev->enabled)
130 return -EBUSY;
132 drv = cpuidle_get_cpu_driver(dev);
134 /* ask the governor for the next state */
135 next_state = cpuidle_curr_governor->select(drv, dev);
136 if (need_resched()) {
137 dev->last_residency = 0;
138 /* give the governor an opportunity to reflect on the outcome */
139 if (cpuidle_curr_governor->reflect)
140 cpuidle_curr_governor->reflect(dev, next_state);
141 local_irq_enable();
142 return 0;
145 trace_cpu_idle_rcuidle(next_state, dev->cpu);
147 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
148 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
149 &dev->cpu);
151 if (cpuidle_state_is_coupled(dev, drv, next_state))
152 entered_state = cpuidle_enter_state_coupled(dev, drv,
153 next_state);
154 else
155 entered_state = cpuidle_enter_state(dev, drv, next_state);
157 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
158 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
159 &dev->cpu);
161 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
163 /* give the governor an opportunity to reflect on the outcome */
164 if (cpuidle_curr_governor->reflect)
165 cpuidle_curr_governor->reflect(dev, entered_state);
167 return 0;
171 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
173 void cpuidle_install_idle_handler(void)
175 if (enabled_devices) {
176 /* Make sure all changes finished before we switch to new idle */
177 smp_wmb();
178 initialized = 1;
183 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
185 void cpuidle_uninstall_idle_handler(void)
187 if (enabled_devices) {
188 initialized = 0;
189 kick_all_cpus_sync();
194 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
196 void cpuidle_pause_and_lock(void)
198 mutex_lock(&cpuidle_lock);
199 cpuidle_uninstall_idle_handler();
202 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
205 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
207 void cpuidle_resume_and_unlock(void)
209 cpuidle_install_idle_handler();
210 mutex_unlock(&cpuidle_lock);
213 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
215 /* Currently used in suspend/resume path to suspend cpuidle */
216 void cpuidle_pause(void)
218 mutex_lock(&cpuidle_lock);
219 cpuidle_uninstall_idle_handler();
220 mutex_unlock(&cpuidle_lock);
223 /* Currently used in suspend/resume path to resume cpuidle */
224 void cpuidle_resume(void)
226 mutex_lock(&cpuidle_lock);
227 cpuidle_install_idle_handler();
228 mutex_unlock(&cpuidle_lock);
231 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
232 static int poll_idle(struct cpuidle_device *dev,
233 struct cpuidle_driver *drv, int index)
235 ktime_t t1, t2;
236 s64 diff;
238 t1 = ktime_get();
239 local_irq_enable();
240 while (!need_resched())
241 cpu_relax();
243 t2 = ktime_get();
244 diff = ktime_to_us(ktime_sub(t2, t1));
245 if (diff > INT_MAX)
246 diff = INT_MAX;
248 dev->last_residency = (int) diff;
250 return index;
253 static void poll_idle_init(struct cpuidle_driver *drv)
255 struct cpuidle_state *state = &drv->states[0];
257 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
258 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
259 state->exit_latency = 0;
260 state->target_residency = 0;
261 state->power_usage = -1;
262 state->flags = 0;
263 state->enter = poll_idle;
264 state->disabled = false;
266 #else
267 static void poll_idle_init(struct cpuidle_driver *drv) {}
268 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
271 * cpuidle_enable_device - enables idle PM for a CPU
272 * @dev: the CPU
274 * This function must be called between cpuidle_pause_and_lock and
275 * cpuidle_resume_and_unlock when used externally.
277 int cpuidle_enable_device(struct cpuidle_device *dev)
279 int ret;
280 struct cpuidle_driver *drv;
282 if (!dev)
283 return -EINVAL;
285 if (dev->enabled)
286 return 0;
288 drv = cpuidle_get_cpu_driver(dev);
290 if (!drv || !cpuidle_curr_governor)
291 return -EIO;
293 if (!dev->registered)
294 return -EINVAL;
296 if (!dev->state_count)
297 dev->state_count = drv->state_count;
299 poll_idle_init(drv);
301 ret = cpuidle_add_device_sysfs(dev);
302 if (ret)
303 return ret;
305 if (cpuidle_curr_governor->enable &&
306 (ret = cpuidle_curr_governor->enable(drv, dev)))
307 goto fail_sysfs;
309 smp_wmb();
311 dev->enabled = 1;
313 enabled_devices++;
314 return 0;
316 fail_sysfs:
317 cpuidle_remove_device_sysfs(dev);
319 return ret;
322 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
325 * cpuidle_disable_device - disables idle PM for a CPU
326 * @dev: the CPU
328 * This function must be called between cpuidle_pause_and_lock and
329 * cpuidle_resume_and_unlock when used externally.
331 void cpuidle_disable_device(struct cpuidle_device *dev)
333 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
335 if (!dev || !dev->enabled)
336 return;
338 if (!drv || !cpuidle_curr_governor)
339 return;
341 dev->enabled = 0;
343 if (cpuidle_curr_governor->disable)
344 cpuidle_curr_governor->disable(drv, dev);
346 cpuidle_remove_device_sysfs(dev);
347 enabled_devices--;
350 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
352 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
354 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
356 list_del(&dev->device_list);
357 per_cpu(cpuidle_devices, dev->cpu) = NULL;
358 module_put(drv->owner);
361 static int __cpuidle_device_init(struct cpuidle_device *dev)
363 memset(dev->states_usage, 0, sizeof(dev->states_usage));
364 dev->last_residency = 0;
366 return 0;
370 * __cpuidle_register_device - internal register function called before register
371 * and enable routines
372 * @dev: the cpu
374 * cpuidle_lock mutex must be held before this is called
376 static int __cpuidle_register_device(struct cpuidle_device *dev)
378 int ret;
379 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
381 if (!try_module_get(drv->owner))
382 return -EINVAL;
384 per_cpu(cpuidle_devices, dev->cpu) = dev;
385 list_add(&dev->device_list, &cpuidle_detected_devices);
387 ret = cpuidle_coupled_register_device(dev);
388 if (ret) {
389 __cpuidle_unregister_device(dev);
390 return ret;
393 dev->registered = 1;
394 return 0;
398 * cpuidle_register_device - registers a CPU's idle PM feature
399 * @dev: the cpu
401 int cpuidle_register_device(struct cpuidle_device *dev)
403 int ret = -EBUSY;
405 if (!dev)
406 return -EINVAL;
408 mutex_lock(&cpuidle_lock);
410 if (dev->registered)
411 goto out_unlock;
413 ret = __cpuidle_device_init(dev);
414 if (ret)
415 goto out_unlock;
417 ret = __cpuidle_register_device(dev);
418 if (ret)
419 goto out_unlock;
421 ret = cpuidle_add_sysfs(dev);
422 if (ret)
423 goto out_unregister;
425 ret = cpuidle_enable_device(dev);
426 if (ret)
427 goto out_sysfs;
429 cpuidle_install_idle_handler();
431 out_unlock:
432 mutex_unlock(&cpuidle_lock);
434 return ret;
436 out_sysfs:
437 cpuidle_remove_sysfs(dev);
438 out_unregister:
439 __cpuidle_unregister_device(dev);
440 goto out_unlock;
443 EXPORT_SYMBOL_GPL(cpuidle_register_device);
446 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
447 * @dev: the cpu
449 void cpuidle_unregister_device(struct cpuidle_device *dev)
451 if (!dev || dev->registered == 0)
452 return;
454 cpuidle_pause_and_lock();
456 cpuidle_disable_device(dev);
458 cpuidle_remove_sysfs(dev);
460 __cpuidle_unregister_device(dev);
462 cpuidle_coupled_unregister_device(dev);
464 cpuidle_resume_and_unlock();
467 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
470 * cpuidle_unregister: unregister a driver and the devices. This function
471 * can be used only if the driver has been previously registered through
472 * the cpuidle_register function.
474 * @drv: a valid pointer to a struct cpuidle_driver
476 void cpuidle_unregister(struct cpuidle_driver *drv)
478 int cpu;
479 struct cpuidle_device *device;
481 for_each_cpu(cpu, drv->cpumask) {
482 device = &per_cpu(cpuidle_dev, cpu);
483 cpuidle_unregister_device(device);
486 cpuidle_unregister_driver(drv);
488 EXPORT_SYMBOL_GPL(cpuidle_unregister);
491 * cpuidle_register: registers the driver and the cpu devices with the
492 * coupled_cpus passed as parameter. This function is used for all common
493 * initialization pattern there are in the arch specific drivers. The
494 * devices is globally defined in this file.
496 * @drv : a valid pointer to a struct cpuidle_driver
497 * @coupled_cpus: a cpumask for the coupled states
499 * Returns 0 on success, < 0 otherwise
501 int cpuidle_register(struct cpuidle_driver *drv,
502 const struct cpumask *const coupled_cpus)
504 int ret, cpu;
505 struct cpuidle_device *device;
507 ret = cpuidle_register_driver(drv);
508 if (ret) {
509 pr_err("failed to register cpuidle driver\n");
510 return ret;
513 for_each_cpu(cpu, drv->cpumask) {
514 device = &per_cpu(cpuidle_dev, cpu);
515 device->cpu = cpu;
517 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
519 * On multiplatform for ARM, the coupled idle states could
520 * enabled in the kernel even if the cpuidle driver does not
521 * use it. Note, coupled_cpus is a struct copy.
523 if (coupled_cpus)
524 device->coupled_cpus = *coupled_cpus;
525 #endif
526 ret = cpuidle_register_device(device);
527 if (!ret)
528 continue;
530 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
532 cpuidle_unregister(drv);
533 break;
536 return ret;
538 EXPORT_SYMBOL_GPL(cpuidle_register);
540 #ifdef CONFIG_SMP
542 static void smp_callback(void *v)
544 /* we already woke the CPU up, nothing more to do */
548 * This function gets called when a part of the kernel has a new latency
549 * requirement. This means we need to get all processors out of their C-state,
550 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
551 * wakes them all right up.
553 static int cpuidle_latency_notify(struct notifier_block *b,
554 unsigned long l, void *v)
556 smp_call_function(smp_callback, NULL, 1);
557 return NOTIFY_OK;
560 static struct notifier_block cpuidle_latency_notifier = {
561 .notifier_call = cpuidle_latency_notify,
564 static inline void latency_notifier_init(struct notifier_block *n)
566 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
569 #else /* CONFIG_SMP */
571 #define latency_notifier_init(x) do { } while (0)
573 #endif /* CONFIG_SMP */
576 * cpuidle_init - core initializer
578 static int __init cpuidle_init(void)
580 int ret;
582 if (cpuidle_disabled())
583 return -ENODEV;
585 ret = cpuidle_add_interface(cpu_subsys.dev_root);
586 if (ret)
587 return ret;
589 latency_notifier_init(&cpuidle_latency_notifier);
591 return 0;
594 module_param(off, int, 0444);
595 core_initcall(cpuidle_init);