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 <trace/events/power.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)
40 void disable_cpuidle(void)
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);
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);
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,
78 struct cpuidle_state *target_state = &drv->states[index];
79 ktime_t time_start, time_end;
82 time_start = ktime_get();
84 entered_state = target_state->enter(dev, drv, index);
86 time_end = ktime_get();
88 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
91 diff = ktime_to_us(ktime_sub(time_end, time_start));
95 dev->last_residency = (int) diff;
97 if (entered_state >= 0) {
98 /* Update cpuidle counters */
99 /* This can be moved to within driver enter routine
100 * but that results in multiple copies of same code.
102 dev->states_usage[entered_state].time += dev->last_residency;
103 dev->states_usage[entered_state].usage++;
105 dev->last_residency = 0;
108 return entered_state;
112 * cpuidle_idle_call - the main idle loop
114 * NOTE: no locks or semaphores should be used here
115 * return non-zero on failure
117 int cpuidle_idle_call(void)
119 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
120 struct cpuidle_driver *drv;
121 int next_state, entered_state;
124 if (off || !initialized)
127 /* check if the device is ready */
128 if (!dev || !dev->enabled)
131 drv = cpuidle_get_cpu_driver(dev);
133 /* ask the governor for the next state */
134 next_state = cpuidle_curr_governor->select(drv, dev);
135 if (need_resched()) {
136 dev->last_residency = 0;
137 /* give the governor an opportunity to reflect on the outcome */
138 if (cpuidle_curr_governor->reflect)
139 cpuidle_curr_governor->reflect(dev, next_state);
144 trace_cpu_idle_rcuidle(next_state, dev->cpu);
146 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
149 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
151 if (cpuidle_state_is_coupled(dev, drv, next_state))
152 entered_state = cpuidle_enter_state_coupled(dev, drv,
155 entered_state = cpuidle_enter_state(dev, drv, next_state);
158 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
160 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
162 /* give the governor an opportunity to reflect on the outcome */
163 if (cpuidle_curr_governor->reflect)
164 cpuidle_curr_governor->reflect(dev, entered_state);
170 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
172 void cpuidle_install_idle_handler(void)
174 if (enabled_devices) {
175 /* Make sure all changes finished before we switch to new idle */
182 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
184 void cpuidle_uninstall_idle_handler(void)
186 if (enabled_devices) {
188 kick_all_cpus_sync();
193 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
195 void cpuidle_pause_and_lock(void)
197 mutex_lock(&cpuidle_lock);
198 cpuidle_uninstall_idle_handler();
201 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
204 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
206 void cpuidle_resume_and_unlock(void)
208 cpuidle_install_idle_handler();
209 mutex_unlock(&cpuidle_lock);
212 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
214 /* Currently used in suspend/resume path to suspend cpuidle */
215 void cpuidle_pause(void)
217 mutex_lock(&cpuidle_lock);
218 cpuidle_uninstall_idle_handler();
219 mutex_unlock(&cpuidle_lock);
222 /* Currently used in suspend/resume path to resume cpuidle */
223 void cpuidle_resume(void)
225 mutex_lock(&cpuidle_lock);
226 cpuidle_install_idle_handler();
227 mutex_unlock(&cpuidle_lock);
231 * cpuidle_enable_device - enables idle PM for a CPU
234 * This function must be called between cpuidle_pause_and_lock and
235 * cpuidle_resume_and_unlock when used externally.
237 int cpuidle_enable_device(struct cpuidle_device *dev)
240 struct cpuidle_driver *drv;
248 drv = cpuidle_get_cpu_driver(dev);
250 if (!drv || !cpuidle_curr_governor)
253 if (!dev->registered)
256 if (!dev->state_count)
257 dev->state_count = drv->state_count;
259 ret = cpuidle_add_device_sysfs(dev);
263 if (cpuidle_curr_governor->enable &&
264 (ret = cpuidle_curr_governor->enable(drv, dev)))
275 cpuidle_remove_device_sysfs(dev);
280 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
283 * cpuidle_disable_device - disables idle PM for a CPU
286 * This function must be called between cpuidle_pause_and_lock and
287 * cpuidle_resume_and_unlock when used externally.
289 void cpuidle_disable_device(struct cpuidle_device *dev)
291 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
293 if (!dev || !dev->enabled)
296 if (!drv || !cpuidle_curr_governor)
301 if (cpuidle_curr_governor->disable)
302 cpuidle_curr_governor->disable(drv, dev);
304 cpuidle_remove_device_sysfs(dev);
308 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
310 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
312 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
314 list_del(&dev->device_list);
315 per_cpu(cpuidle_devices, dev->cpu) = NULL;
316 module_put(drv->owner);
319 static void __cpuidle_device_init(struct cpuidle_device *dev)
321 memset(dev->states_usage, 0, sizeof(dev->states_usage));
322 dev->last_residency = 0;
326 * __cpuidle_register_device - internal register function called before register
327 * and enable routines
330 * cpuidle_lock mutex must be held before this is called
332 static int __cpuidle_register_device(struct cpuidle_device *dev)
335 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
337 if (!try_module_get(drv->owner))
340 per_cpu(cpuidle_devices, dev->cpu) = dev;
341 list_add(&dev->device_list, &cpuidle_detected_devices);
343 ret = cpuidle_coupled_register_device(dev);
345 __cpuidle_unregister_device(dev);
353 * cpuidle_register_device - registers a CPU's idle PM feature
356 int cpuidle_register_device(struct cpuidle_device *dev)
363 mutex_lock(&cpuidle_lock);
368 __cpuidle_device_init(dev);
370 ret = __cpuidle_register_device(dev);
374 ret = cpuidle_add_sysfs(dev);
378 ret = cpuidle_enable_device(dev);
382 cpuidle_install_idle_handler();
385 mutex_unlock(&cpuidle_lock);
390 cpuidle_remove_sysfs(dev);
392 __cpuidle_unregister_device(dev);
396 EXPORT_SYMBOL_GPL(cpuidle_register_device);
399 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
402 void cpuidle_unregister_device(struct cpuidle_device *dev)
404 if (!dev || dev->registered == 0)
407 cpuidle_pause_and_lock();
409 cpuidle_disable_device(dev);
411 cpuidle_remove_sysfs(dev);
413 __cpuidle_unregister_device(dev);
415 cpuidle_coupled_unregister_device(dev);
417 cpuidle_resume_and_unlock();
420 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
423 * cpuidle_unregister: unregister a driver and the devices. This function
424 * can be used only if the driver has been previously registered through
425 * the cpuidle_register function.
427 * @drv: a valid pointer to a struct cpuidle_driver
429 void cpuidle_unregister(struct cpuidle_driver *drv)
432 struct cpuidle_device *device;
434 for_each_cpu(cpu, drv->cpumask) {
435 device = &per_cpu(cpuidle_dev, cpu);
436 cpuidle_unregister_device(device);
439 cpuidle_unregister_driver(drv);
441 EXPORT_SYMBOL_GPL(cpuidle_unregister);
444 * cpuidle_register: registers the driver and the cpu devices with the
445 * coupled_cpus passed as parameter. This function is used for all common
446 * initialization pattern there are in the arch specific drivers. The
447 * devices is globally defined in this file.
449 * @drv : a valid pointer to a struct cpuidle_driver
450 * @coupled_cpus: a cpumask for the coupled states
452 * Returns 0 on success, < 0 otherwise
454 int cpuidle_register(struct cpuidle_driver *drv,
455 const struct cpumask *const coupled_cpus)
458 struct cpuidle_device *device;
460 ret = cpuidle_register_driver(drv);
462 pr_err("failed to register cpuidle driver\n");
466 for_each_cpu(cpu, drv->cpumask) {
467 device = &per_cpu(cpuidle_dev, cpu);
470 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
472 * On multiplatform for ARM, the coupled idle states could be
473 * enabled in the kernel even if the cpuidle driver does not
474 * use it. Note, coupled_cpus is a struct copy.
477 device->coupled_cpus = *coupled_cpus;
479 ret = cpuidle_register_device(device);
483 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
485 cpuidle_unregister(drv);
491 EXPORT_SYMBOL_GPL(cpuidle_register);
495 static void smp_callback(void *v)
497 /* we already woke the CPU up, nothing more to do */
501 * This function gets called when a part of the kernel has a new latency
502 * requirement. This means we need to get all processors out of their C-state,
503 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
504 * wakes them all right up.
506 static int cpuidle_latency_notify(struct notifier_block *b,
507 unsigned long l, void *v)
509 smp_call_function(smp_callback, NULL, 1);
513 static struct notifier_block cpuidle_latency_notifier = {
514 .notifier_call = cpuidle_latency_notify,
517 static inline void latency_notifier_init(struct notifier_block *n)
519 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
522 #else /* CONFIG_SMP */
524 #define latency_notifier_init(x) do { } while (0)
526 #endif /* CONFIG_SMP */
529 * cpuidle_init - core initializer
531 static int __init cpuidle_init(void)
535 if (cpuidle_disabled())
538 ret = cpuidle_add_interface(cpu_subsys.dev_root);
542 latency_notifier_init(&cpuidle_latency_notifier);
547 module_param(off, int, 0444);
548 core_initcall(cpuidle_init);