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.
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 <linux/suspend.h>
23 #include <linux/tick.h>
24 #include <trace/events/power.h>
28 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
29 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
31 DEFINE_MUTEX(cpuidle_lock);
32 LIST_HEAD(cpuidle_detected_devices);
34 static int enabled_devices;
35 static int off __read_mostly;
36 static int initialized __read_mostly;
38 int cpuidle_disabled(void)
42 void disable_cpuidle(void)
48 * cpuidle_play_dead - cpu off-lining
50 * Returns in case of an error or no driver
52 int cpuidle_play_dead(void)
54 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
55 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
61 /* Find lowest-power state that supports long-term idle */
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);
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.
73 * @freeze: Whether or not the state should be suitable for suspend-to-idle.
75 static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
76 struct cpuidle_device *dev, bool freeze)
78 unsigned int latency_req = 0;
79 int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1;
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];
85 if (s->disabled || su->disable || s->exit_latency <= latency_req
86 || (freeze && !s->enter_freeze))
89 latency_req = s->exit_latency;
95 static void enter_freeze_proper(struct cpuidle_driver *drv,
96 struct cpuidle_device *dev, int index)
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.
104 drv->states[index].enter_freeze(dev, drv, index);
105 WARN_ON(!irqs_disabled());
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.
111 RCU_NONIDLE(tick_unfreeze());
115 * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
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.
121 void cpuidle_enter_freeze(void)
123 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
124 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
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
132 index = cpuidle_find_deepest_state(drv, dev, true);
134 enter_freeze_proper(drv, dev, index);
139 * It is not safe to freeze the tick, find the deepest state available
140 * at all and try to enter it normally.
142 index = cpuidle_find_deepest_state(drv, dev, false);
144 cpuidle_enter(drv, dev, index);
148 /* Interrupts are enabled again here. */
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
158 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
163 struct cpuidle_state *target_state = &drv->states[index];
164 ktime_t time_start, time_end;
167 trace_cpu_idle_rcuidle(index, dev->cpu);
168 time_start = ktime_get();
170 entered_state = target_state->enter(dev, drv, index);
172 time_end = ktime_get();
173 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
175 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
178 diff = ktime_to_us(ktime_sub(time_end, time_start));
182 dev->last_residency = (int) diff;
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.
189 dev->states_usage[entered_state].time += dev->last_residency;
190 dev->states_usage[entered_state].usage++;
192 dev->last_residency = 0;
195 return entered_state;
199 * cpuidle_select - ask the cpuidle framework to choose an idle state
201 * @drv: the cpuidle driver
202 * @dev: the cpuidle device
204 * Returns the index of the idle state.
206 int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
208 if (off || !initialized)
211 if (!drv || !dev || !dev->enabled)
214 return cpuidle_curr_governor->select(drv, dev);
218 * cpuidle_enter - enter into the specified idle state
220 * @drv: the cpuidle driver tied with the cpu
221 * @dev: the cpuidle device
222 * @index: the index in the idle state table
224 * Returns the index in the idle state, < 0 in case of error.
225 * The error code depends on the backend driver
227 int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
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);
236 * cpuidle_reflect - tell the underlying governor what was the state
239 * @dev : the cpuidle device
240 * @index: the index in the idle state table
243 void cpuidle_reflect(struct cpuidle_device *dev, int index)
245 if (cpuidle_curr_governor->reflect)
246 cpuidle_curr_governor->reflect(dev, index);
250 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
252 void cpuidle_install_idle_handler(void)
254 if (enabled_devices) {
255 /* Make sure all changes finished before we switch to new idle */
262 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
264 void cpuidle_uninstall_idle_handler(void)
266 if (enabled_devices) {
268 wake_up_all_idle_cpus();
272 * Make sure external observers (such as the scheduler)
273 * are done looking at pointed idle states.
279 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
281 void cpuidle_pause_and_lock(void)
283 mutex_lock(&cpuidle_lock);
284 cpuidle_uninstall_idle_handler();
287 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
290 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
292 void cpuidle_resume_and_unlock(void)
294 cpuidle_install_idle_handler();
295 mutex_unlock(&cpuidle_lock);
298 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
300 /* Currently used in suspend/resume path to suspend cpuidle */
301 void cpuidle_pause(void)
303 mutex_lock(&cpuidle_lock);
304 cpuidle_uninstall_idle_handler();
305 mutex_unlock(&cpuidle_lock);
308 /* Currently used in suspend/resume path to resume cpuidle */
309 void cpuidle_resume(void)
311 mutex_lock(&cpuidle_lock);
312 cpuidle_install_idle_handler();
313 mutex_unlock(&cpuidle_lock);
317 * cpuidle_enable_device - enables idle PM for a CPU
320 * This function must be called between cpuidle_pause_and_lock and
321 * cpuidle_resume_and_unlock when used externally.
323 int cpuidle_enable_device(struct cpuidle_device *dev)
326 struct cpuidle_driver *drv;
334 drv = cpuidle_get_cpu_driver(dev);
336 if (!drv || !cpuidle_curr_governor)
339 if (!dev->registered)
342 if (!dev->state_count)
343 dev->state_count = drv->state_count;
345 ret = cpuidle_add_device_sysfs(dev);
349 if (cpuidle_curr_governor->enable &&
350 (ret = cpuidle_curr_governor->enable(drv, dev)))
361 cpuidle_remove_device_sysfs(dev);
366 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
369 * cpuidle_disable_device - disables idle PM for a CPU
372 * This function must be called between cpuidle_pause_and_lock and
373 * cpuidle_resume_and_unlock when used externally.
375 void cpuidle_disable_device(struct cpuidle_device *dev)
377 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
379 if (!dev || !dev->enabled)
382 if (!drv || !cpuidle_curr_governor)
387 if (cpuidle_curr_governor->disable)
388 cpuidle_curr_governor->disable(drv, dev);
390 cpuidle_remove_device_sysfs(dev);
394 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
396 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
398 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
400 list_del(&dev->device_list);
401 per_cpu(cpuidle_devices, dev->cpu) = NULL;
402 module_put(drv->owner);
405 static void __cpuidle_device_init(struct cpuidle_device *dev)
407 memset(dev->states_usage, 0, sizeof(dev->states_usage));
408 dev->last_residency = 0;
412 * __cpuidle_register_device - internal register function called before register
413 * and enable routines
416 * cpuidle_lock mutex must be held before this is called
418 static int __cpuidle_register_device(struct cpuidle_device *dev)
421 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
423 if (!try_module_get(drv->owner))
426 per_cpu(cpuidle_devices, dev->cpu) = dev;
427 list_add(&dev->device_list, &cpuidle_detected_devices);
429 ret = cpuidle_coupled_register_device(dev);
431 __cpuidle_unregister_device(dev);
439 * cpuidle_register_device - registers a CPU's idle PM feature
442 int cpuidle_register_device(struct cpuidle_device *dev)
449 mutex_lock(&cpuidle_lock);
454 __cpuidle_device_init(dev);
456 ret = __cpuidle_register_device(dev);
460 ret = cpuidle_add_sysfs(dev);
464 ret = cpuidle_enable_device(dev);
468 cpuidle_install_idle_handler();
471 mutex_unlock(&cpuidle_lock);
476 cpuidle_remove_sysfs(dev);
478 __cpuidle_unregister_device(dev);
482 EXPORT_SYMBOL_GPL(cpuidle_register_device);
485 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
488 void cpuidle_unregister_device(struct cpuidle_device *dev)
490 if (!dev || dev->registered == 0)
493 cpuidle_pause_and_lock();
495 cpuidle_disable_device(dev);
497 cpuidle_remove_sysfs(dev);
499 __cpuidle_unregister_device(dev);
501 cpuidle_coupled_unregister_device(dev);
503 cpuidle_resume_and_unlock();
506 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
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.
513 * @drv: a valid pointer to a struct cpuidle_driver
515 void cpuidle_unregister(struct cpuidle_driver *drv)
518 struct cpuidle_device *device;
520 for_each_cpu(cpu, drv->cpumask) {
521 device = &per_cpu(cpuidle_dev, cpu);
522 cpuidle_unregister_device(device);
525 cpuidle_unregister_driver(drv);
527 EXPORT_SYMBOL_GPL(cpuidle_unregister);
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.
535 * @drv : a valid pointer to a struct cpuidle_driver
536 * @coupled_cpus: a cpumask for the coupled states
538 * Returns 0 on success, < 0 otherwise
540 int cpuidle_register(struct cpuidle_driver *drv,
541 const struct cpumask *const coupled_cpus)
544 struct cpuidle_device *device;
546 ret = cpuidle_register_driver(drv);
548 pr_err("failed to register cpuidle driver\n");
552 for_each_cpu(cpu, drv->cpumask) {
553 device = &per_cpu(cpuidle_dev, cpu);
556 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
558 * On multiplatform for ARM, the coupled idle states could be
559 * enabled in the kernel even if the cpuidle driver does not
560 * use it. Note, coupled_cpus is a struct copy.
563 device->coupled_cpus = *coupled_cpus;
565 ret = cpuidle_register_device(device);
569 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
571 cpuidle_unregister(drv);
577 EXPORT_SYMBOL_GPL(cpuidle_register);
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.
587 static int cpuidle_latency_notify(struct notifier_block *b,
588 unsigned long l, void *v)
590 wake_up_all_idle_cpus();
594 static struct notifier_block cpuidle_latency_notifier = {
595 .notifier_call = cpuidle_latency_notify,
598 static inline void latency_notifier_init(struct notifier_block *n)
600 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
603 #else /* CONFIG_SMP */
605 #define latency_notifier_init(x) do { } while (0)
607 #endif /* CONFIG_SMP */
610 * cpuidle_init - core initializer
612 static int __init cpuidle_init(void)
616 if (cpuidle_disabled())
619 ret = cpuidle_add_interface(cpu_subsys.dev_root);
623 latency_notifier_init(&cpuidle_latency_notifier);
628 module_param(off, int, 0444);
629 core_initcall(cpuidle_init);