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 <trace/events/power.h>
27 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
28 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
30 DEFINE_MUTEX(cpuidle_lock);
31 LIST_HEAD(cpuidle_detected_devices);
33 static int enabled_devices;
34 static int off __read_mostly;
35 static int initialized __read_mostly;
37 int cpuidle_disabled(void)
41 void disable_cpuidle(void)
47 * cpuidle_play_dead - cpu off-lining
49 * Returns in case of an error or no driver
51 int cpuidle_play_dead(void)
53 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
54 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
60 /* Find lowest-power state that supports long-term idle */
61 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
62 if (drv->states[i].enter_dead)
63 return drv->states[i].enter_dead(dev, i);
69 * cpuidle_find_deepest_state - Find deepest state meeting specific conditions.
70 * @drv: cpuidle driver for the given CPU.
71 * @dev: cpuidle device for the given CPU.
73 static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
74 struct cpuidle_device *dev)
76 unsigned int latency_req = 0;
77 int i, ret = CPUIDLE_DRIVER_STATE_START - 1;
79 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
80 struct cpuidle_state *s = &drv->states[i];
81 struct cpuidle_state_usage *su = &dev->states_usage[i];
83 if (s->disabled || su->disable || s->exit_latency <= latency_req)
86 latency_req = s->exit_latency;
93 * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
95 * Find the deepest state available and enter it.
97 void cpuidle_enter_freeze(void)
99 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
100 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
103 index = cpuidle_find_deepest_state(drv, dev);
105 cpuidle_enter(drv, dev, index);
109 /* Interrupts are enabled again here. */
114 * cpuidle_enter_state - enter the state and update stats
115 * @dev: cpuidle device for this cpu
116 * @drv: cpuidle driver for this cpu
117 * @next_state: index into drv->states of the state to enter
119 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
124 struct cpuidle_state *target_state = &drv->states[index];
125 ktime_t time_start, time_end;
128 trace_cpu_idle_rcuidle(index, dev->cpu);
129 time_start = ktime_get();
131 entered_state = target_state->enter(dev, drv, index);
133 time_end = ktime_get();
134 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
136 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
139 diff = ktime_to_us(ktime_sub(time_end, time_start));
143 dev->last_residency = (int) diff;
145 if (entered_state >= 0) {
146 /* Update cpuidle counters */
147 /* This can be moved to within driver enter routine
148 * but that results in multiple copies of same code.
150 dev->states_usage[entered_state].time += dev->last_residency;
151 dev->states_usage[entered_state].usage++;
153 dev->last_residency = 0;
156 return entered_state;
160 * cpuidle_select - ask the cpuidle framework to choose an idle state
162 * @drv: the cpuidle driver
163 * @dev: the cpuidle device
165 * Returns the index of the idle state.
167 int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
169 if (off || !initialized)
172 if (!drv || !dev || !dev->enabled)
175 return cpuidle_curr_governor->select(drv, dev);
179 * cpuidle_enter - enter into the specified idle state
181 * @drv: the cpuidle driver tied with the cpu
182 * @dev: the cpuidle device
183 * @index: the index in the idle state table
185 * Returns the index in the idle state, < 0 in case of error.
186 * The error code depends on the backend driver
188 int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
191 if (cpuidle_state_is_coupled(dev, drv, index))
192 return cpuidle_enter_state_coupled(dev, drv, index);
193 return cpuidle_enter_state(dev, drv, index);
197 * cpuidle_reflect - tell the underlying governor what was the state
200 * @dev : the cpuidle device
201 * @index: the index in the idle state table
204 void cpuidle_reflect(struct cpuidle_device *dev, int index)
206 if (cpuidle_curr_governor->reflect)
207 cpuidle_curr_governor->reflect(dev, index);
211 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
213 void cpuidle_install_idle_handler(void)
215 if (enabled_devices) {
216 /* Make sure all changes finished before we switch to new idle */
223 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
225 void cpuidle_uninstall_idle_handler(void)
227 if (enabled_devices) {
229 wake_up_all_idle_cpus();
233 * Make sure external observers (such as the scheduler)
234 * are done looking at pointed idle states.
240 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
242 void cpuidle_pause_and_lock(void)
244 mutex_lock(&cpuidle_lock);
245 cpuidle_uninstall_idle_handler();
248 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
251 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
253 void cpuidle_resume_and_unlock(void)
255 cpuidle_install_idle_handler();
256 mutex_unlock(&cpuidle_lock);
259 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
261 /* Currently used in suspend/resume path to suspend cpuidle */
262 void cpuidle_pause(void)
264 mutex_lock(&cpuidle_lock);
265 cpuidle_uninstall_idle_handler();
266 mutex_unlock(&cpuidle_lock);
269 /* Currently used in suspend/resume path to resume cpuidle */
270 void cpuidle_resume(void)
272 mutex_lock(&cpuidle_lock);
273 cpuidle_install_idle_handler();
274 mutex_unlock(&cpuidle_lock);
278 * cpuidle_enable_device - enables idle PM for a CPU
281 * This function must be called between cpuidle_pause_and_lock and
282 * cpuidle_resume_and_unlock when used externally.
284 int cpuidle_enable_device(struct cpuidle_device *dev)
287 struct cpuidle_driver *drv;
295 drv = cpuidle_get_cpu_driver(dev);
297 if (!drv || !cpuidle_curr_governor)
300 if (!dev->registered)
303 if (!dev->state_count)
304 dev->state_count = drv->state_count;
306 ret = cpuidle_add_device_sysfs(dev);
310 if (cpuidle_curr_governor->enable &&
311 (ret = cpuidle_curr_governor->enable(drv, dev)))
322 cpuidle_remove_device_sysfs(dev);
327 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
330 * cpuidle_disable_device - disables idle PM for a CPU
333 * This function must be called between cpuidle_pause_and_lock and
334 * cpuidle_resume_and_unlock when used externally.
336 void cpuidle_disable_device(struct cpuidle_device *dev)
338 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
340 if (!dev || !dev->enabled)
343 if (!drv || !cpuidle_curr_governor)
348 if (cpuidle_curr_governor->disable)
349 cpuidle_curr_governor->disable(drv, dev);
351 cpuidle_remove_device_sysfs(dev);
355 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
357 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
359 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
361 list_del(&dev->device_list);
362 per_cpu(cpuidle_devices, dev->cpu) = NULL;
363 module_put(drv->owner);
366 static void __cpuidle_device_init(struct cpuidle_device *dev)
368 memset(dev->states_usage, 0, sizeof(dev->states_usage));
369 dev->last_residency = 0;
373 * __cpuidle_register_device - internal register function called before register
374 * and enable routines
377 * cpuidle_lock mutex must be held before this is called
379 static int __cpuidle_register_device(struct cpuidle_device *dev)
382 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
384 if (!try_module_get(drv->owner))
387 per_cpu(cpuidle_devices, dev->cpu) = dev;
388 list_add(&dev->device_list, &cpuidle_detected_devices);
390 ret = cpuidle_coupled_register_device(dev);
392 __cpuidle_unregister_device(dev);
400 * cpuidle_register_device - registers a CPU's idle PM feature
403 int cpuidle_register_device(struct cpuidle_device *dev)
410 mutex_lock(&cpuidle_lock);
415 __cpuidle_device_init(dev);
417 ret = __cpuidle_register_device(dev);
421 ret = cpuidle_add_sysfs(dev);
425 ret = cpuidle_enable_device(dev);
429 cpuidle_install_idle_handler();
432 mutex_unlock(&cpuidle_lock);
437 cpuidle_remove_sysfs(dev);
439 __cpuidle_unregister_device(dev);
443 EXPORT_SYMBOL_GPL(cpuidle_register_device);
446 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
449 void cpuidle_unregister_device(struct cpuidle_device *dev)
451 if (!dev || dev->registered == 0)
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)
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)
505 struct cpuidle_device *device;
507 ret = cpuidle_register_driver(drv);
509 pr_err("failed to register cpuidle driver\n");
513 for_each_cpu(cpu, drv->cpumask) {
514 device = &per_cpu(cpuidle_dev, cpu);
517 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
519 * On multiplatform for ARM, the coupled idle states could be
520 * enabled in the kernel even if the cpuidle driver does not
521 * use it. Note, coupled_cpus is a struct copy.
524 device->coupled_cpus = *coupled_cpus;
526 ret = cpuidle_register_device(device);
530 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
532 cpuidle_unregister(drv);
538 EXPORT_SYMBOL_GPL(cpuidle_register);
543 * This function gets called when a part of the kernel has a new latency
544 * requirement. This means we need to get all processors out of their C-state,
545 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
546 * wakes them all right up.
548 static int cpuidle_latency_notify(struct notifier_block *b,
549 unsigned long l, void *v)
551 wake_up_all_idle_cpus();
555 static struct notifier_block cpuidle_latency_notifier = {
556 .notifier_call = cpuidle_latency_notify,
559 static inline void latency_notifier_init(struct notifier_block *n)
561 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
564 #else /* CONFIG_SMP */
566 #define latency_notifier_init(x) do { } while (0)
568 #endif /* CONFIG_SMP */
571 * cpuidle_init - core initializer
573 static int __init cpuidle_init(void)
577 if (cpuidle_disabled())
580 ret = cpuidle_add_interface(cpu_subsys.dev_root);
584 latency_notifier_init(&cpuidle_latency_notifier);
589 module_param(off, int, 0444);
590 core_initcall(cpuidle_init);