Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / cpuidle / cpuidle.c
blob2a991e468f78190e1dfab933225d33cfef69d710
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;
121 bool broadcast;
123 if (off || !initialized)
124 return -ENODEV;
126 /* check if the device is ready */
127 if (!dev || !dev->enabled)
128 return -EBUSY;
130 drv = cpuidle_get_cpu_driver(dev);
132 /* ask the governor for the next state */
133 next_state = cpuidle_curr_governor->select(drv, dev);
134 if (need_resched()) {
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);
139 local_irq_enable();
140 return 0;
143 trace_cpu_idle_rcuidle(next_state, dev->cpu);
145 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
147 if (broadcast)
148 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
150 if (cpuidle_state_is_coupled(dev, drv, next_state))
151 entered_state = cpuidle_enter_state_coupled(dev, drv,
152 next_state);
153 else
154 entered_state = cpuidle_enter_state(dev, drv, next_state);
156 if (broadcast)
157 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
159 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
161 /* give the governor an opportunity to reflect on the outcome */
162 if (cpuidle_curr_governor->reflect)
163 cpuidle_curr_governor->reflect(dev, entered_state);
165 return 0;
169 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
171 void cpuidle_install_idle_handler(void)
173 if (enabled_devices) {
174 /* Make sure all changes finished before we switch to new idle */
175 smp_wmb();
176 initialized = 1;
181 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
183 void cpuidle_uninstall_idle_handler(void)
185 if (enabled_devices) {
186 initialized = 0;
187 kick_all_cpus_sync();
192 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
194 void cpuidle_pause_and_lock(void)
196 mutex_lock(&cpuidle_lock);
197 cpuidle_uninstall_idle_handler();
200 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
203 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
205 void cpuidle_resume_and_unlock(void)
207 cpuidle_install_idle_handler();
208 mutex_unlock(&cpuidle_lock);
211 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
213 /* Currently used in suspend/resume path to suspend cpuidle */
214 void cpuidle_pause(void)
216 mutex_lock(&cpuidle_lock);
217 cpuidle_uninstall_idle_handler();
218 mutex_unlock(&cpuidle_lock);
221 /* Currently used in suspend/resume path to resume cpuidle */
222 void cpuidle_resume(void)
224 mutex_lock(&cpuidle_lock);
225 cpuidle_install_idle_handler();
226 mutex_unlock(&cpuidle_lock);
230 * cpuidle_enable_device - enables idle PM for a CPU
231 * @dev: the CPU
233 * This function must be called between cpuidle_pause_and_lock and
234 * cpuidle_resume_and_unlock when used externally.
236 int cpuidle_enable_device(struct cpuidle_device *dev)
238 int ret;
239 struct cpuidle_driver *drv;
241 if (!dev)
242 return -EINVAL;
244 if (dev->enabled)
245 return 0;
247 drv = cpuidle_get_cpu_driver(dev);
249 if (!drv || !cpuidle_curr_governor)
250 return -EIO;
252 if (!dev->registered)
253 return -EINVAL;
255 if (!dev->state_count)
256 dev->state_count = drv->state_count;
258 ret = cpuidle_add_device_sysfs(dev);
259 if (ret)
260 return ret;
262 if (cpuidle_curr_governor->enable &&
263 (ret = cpuidle_curr_governor->enable(drv, dev)))
264 goto fail_sysfs;
266 smp_wmb();
268 dev->enabled = 1;
270 enabled_devices++;
271 return 0;
273 fail_sysfs:
274 cpuidle_remove_device_sysfs(dev);
276 return ret;
279 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
282 * cpuidle_disable_device - disables idle PM for a CPU
283 * @dev: the CPU
285 * This function must be called between cpuidle_pause_and_lock and
286 * cpuidle_resume_and_unlock when used externally.
288 void cpuidle_disable_device(struct cpuidle_device *dev)
290 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
292 if (!dev || !dev->enabled)
293 return;
295 if (!drv || !cpuidle_curr_governor)
296 return;
298 dev->enabled = 0;
300 if (cpuidle_curr_governor->disable)
301 cpuidle_curr_governor->disable(drv, dev);
303 cpuidle_remove_device_sysfs(dev);
304 enabled_devices--;
307 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
309 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
311 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
313 list_del(&dev->device_list);
314 per_cpu(cpuidle_devices, dev->cpu) = NULL;
315 module_put(drv->owner);
318 static void __cpuidle_device_init(struct cpuidle_device *dev)
320 memset(dev->states_usage, 0, sizeof(dev->states_usage));
321 dev->last_residency = 0;
325 * __cpuidle_register_device - internal register function called before register
326 * and enable routines
327 * @dev: the cpu
329 * cpuidle_lock mutex must be held before this is called
331 static int __cpuidle_register_device(struct cpuidle_device *dev)
333 int ret;
334 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
336 if (!try_module_get(drv->owner))
337 return -EINVAL;
339 per_cpu(cpuidle_devices, dev->cpu) = dev;
340 list_add(&dev->device_list, &cpuidle_detected_devices);
342 ret = cpuidle_coupled_register_device(dev);
343 if (ret)
344 __cpuidle_unregister_device(dev);
345 else
346 dev->registered = 1;
348 return ret;
352 * cpuidle_register_device - registers a CPU's idle PM feature
353 * @dev: the cpu
355 int cpuidle_register_device(struct cpuidle_device *dev)
357 int ret = -EBUSY;
359 if (!dev)
360 return -EINVAL;
362 mutex_lock(&cpuidle_lock);
364 if (dev->registered)
365 goto out_unlock;
367 __cpuidle_device_init(dev);
369 ret = __cpuidle_register_device(dev);
370 if (ret)
371 goto out_unlock;
373 ret = cpuidle_add_sysfs(dev);
374 if (ret)
375 goto out_unregister;
377 ret = cpuidle_enable_device(dev);
378 if (ret)
379 goto out_sysfs;
381 cpuidle_install_idle_handler();
383 out_unlock:
384 mutex_unlock(&cpuidle_lock);
386 return ret;
388 out_sysfs:
389 cpuidle_remove_sysfs(dev);
390 out_unregister:
391 __cpuidle_unregister_device(dev);
392 goto out_unlock;
395 EXPORT_SYMBOL_GPL(cpuidle_register_device);
398 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
399 * @dev: the cpu
401 void cpuidle_unregister_device(struct cpuidle_device *dev)
403 if (dev->registered == 0)
404 return;
406 cpuidle_pause_and_lock();
408 cpuidle_disable_device(dev);
410 cpuidle_remove_sysfs(dev);
412 __cpuidle_unregister_device(dev);
414 cpuidle_coupled_unregister_device(dev);
416 cpuidle_resume_and_unlock();
419 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
422 * cpuidle_unregister: unregister a driver and the devices. This function
423 * can be used only if the driver has been previously registered through
424 * the cpuidle_register function.
426 * @drv: a valid pointer to a struct cpuidle_driver
428 void cpuidle_unregister(struct cpuidle_driver *drv)
430 int cpu;
431 struct cpuidle_device *device;
433 for_each_cpu(cpu, drv->cpumask) {
434 device = &per_cpu(cpuidle_dev, cpu);
435 cpuidle_unregister_device(device);
438 cpuidle_unregister_driver(drv);
440 EXPORT_SYMBOL_GPL(cpuidle_unregister);
443 * cpuidle_register: registers the driver and the cpu devices with the
444 * coupled_cpus passed as parameter. This function is used for all common
445 * initialization pattern there are in the arch specific drivers. The
446 * devices is globally defined in this file.
448 * @drv : a valid pointer to a struct cpuidle_driver
449 * @coupled_cpus: a cpumask for the coupled states
451 * Returns 0 on success, < 0 otherwise
453 int cpuidle_register(struct cpuidle_driver *drv,
454 const struct cpumask *const coupled_cpus)
456 int ret, cpu;
457 struct cpuidle_device *device;
459 ret = cpuidle_register_driver(drv);
460 if (ret) {
461 pr_err("failed to register cpuidle driver\n");
462 return ret;
465 for_each_cpu(cpu, drv->cpumask) {
466 device = &per_cpu(cpuidle_dev, cpu);
467 device->cpu = cpu;
469 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
471 * On multiplatform for ARM, the coupled idle states could be
472 * enabled in the kernel even if the cpuidle driver does not
473 * use it. Note, coupled_cpus is a struct copy.
475 if (coupled_cpus)
476 device->coupled_cpus = *coupled_cpus;
477 #endif
478 ret = cpuidle_register_device(device);
479 if (!ret)
480 continue;
482 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
484 cpuidle_unregister(drv);
485 break;
488 return ret;
490 EXPORT_SYMBOL_GPL(cpuidle_register);
492 #ifdef CONFIG_SMP
494 static void smp_callback(void *v)
496 /* we already woke the CPU up, nothing more to do */
500 * This function gets called when a part of the kernel has a new latency
501 * requirement. This means we need to get all processors out of their C-state,
502 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
503 * wakes them all right up.
505 static int cpuidle_latency_notify(struct notifier_block *b,
506 unsigned long l, void *v)
508 smp_call_function(smp_callback, NULL, 1);
509 return NOTIFY_OK;
512 static struct notifier_block cpuidle_latency_notifier = {
513 .notifier_call = cpuidle_latency_notify,
516 static inline void latency_notifier_init(struct notifier_block *n)
518 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
521 #else /* CONFIG_SMP */
523 #define latency_notifier_init(x) do { } while (0)
525 #endif /* CONFIG_SMP */
528 * cpuidle_init - core initializer
530 static int __init cpuidle_init(void)
532 int ret;
534 if (cpuidle_disabled())
535 return -ENODEV;
537 ret = cpuidle_add_interface(cpu_subsys.dev_root);
538 if (ret)
539 return ret;
541 latency_notifier_init(&cpuidle_latency_notifier);
543 return 0;
546 module_param(off, int, 0444);
547 core_initcall(cpuidle_init);