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 broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
147 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
151 trace_cpu_idle_rcuidle(next_state, dev->cpu);
153 if (cpuidle_state_is_coupled(dev, drv, next_state))
154 entered_state = cpuidle_enter_state_coupled(dev, drv,
157 entered_state = cpuidle_enter_state(dev, drv, next_state);
159 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
162 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
164 /* give the governor an opportunity to reflect on the outcome */
165 if (cpuidle_curr_governor->reflect)
166 cpuidle_curr_governor->reflect(dev, entered_state);
172 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
174 void cpuidle_install_idle_handler(void)
176 if (enabled_devices) {
177 /* Make sure all changes finished before we switch to new idle */
184 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
186 void cpuidle_uninstall_idle_handler(void)
188 if (enabled_devices) {
190 kick_all_cpus_sync();
195 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
197 void cpuidle_pause_and_lock(void)
199 mutex_lock(&cpuidle_lock);
200 cpuidle_uninstall_idle_handler();
203 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
206 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
208 void cpuidle_resume_and_unlock(void)
210 cpuidle_install_idle_handler();
211 mutex_unlock(&cpuidle_lock);
214 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
216 /* Currently used in suspend/resume path to suspend cpuidle */
217 void cpuidle_pause(void)
219 mutex_lock(&cpuidle_lock);
220 cpuidle_uninstall_idle_handler();
221 mutex_unlock(&cpuidle_lock);
224 /* Currently used in suspend/resume path to resume cpuidle */
225 void cpuidle_resume(void)
227 mutex_lock(&cpuidle_lock);
228 cpuidle_install_idle_handler();
229 mutex_unlock(&cpuidle_lock);
233 * cpuidle_enable_device - enables idle PM for a CPU
236 * This function must be called between cpuidle_pause_and_lock and
237 * cpuidle_resume_and_unlock when used externally.
239 int cpuidle_enable_device(struct cpuidle_device *dev)
242 struct cpuidle_driver *drv;
250 drv = cpuidle_get_cpu_driver(dev);
252 if (!drv || !cpuidle_curr_governor)
255 if (!dev->registered)
258 if (!dev->state_count)
259 dev->state_count = drv->state_count;
261 ret = cpuidle_add_device_sysfs(dev);
265 if (cpuidle_curr_governor->enable &&
266 (ret = cpuidle_curr_governor->enable(drv, dev)))
277 cpuidle_remove_device_sysfs(dev);
282 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
285 * cpuidle_disable_device - disables idle PM for a CPU
288 * This function must be called between cpuidle_pause_and_lock and
289 * cpuidle_resume_and_unlock when used externally.
291 void cpuidle_disable_device(struct cpuidle_device *dev)
293 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
295 if (!dev || !dev->enabled)
298 if (!drv || !cpuidle_curr_governor)
303 if (cpuidle_curr_governor->disable)
304 cpuidle_curr_governor->disable(drv, dev);
306 cpuidle_remove_device_sysfs(dev);
310 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
312 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
314 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
316 list_del(&dev->device_list);
317 per_cpu(cpuidle_devices, dev->cpu) = NULL;
318 module_put(drv->owner);
321 static void __cpuidle_device_init(struct cpuidle_device *dev)
323 memset(dev->states_usage, 0, sizeof(dev->states_usage));
324 dev->last_residency = 0;
328 * __cpuidle_register_device - internal register function called before register
329 * and enable routines
332 * cpuidle_lock mutex must be held before this is called
334 static int __cpuidle_register_device(struct cpuidle_device *dev)
337 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
339 if (!try_module_get(drv->owner))
342 per_cpu(cpuidle_devices, dev->cpu) = dev;
343 list_add(&dev->device_list, &cpuidle_detected_devices);
345 ret = cpuidle_coupled_register_device(dev);
347 __cpuidle_unregister_device(dev);
355 * cpuidle_register_device - registers a CPU's idle PM feature
358 int cpuidle_register_device(struct cpuidle_device *dev)
365 mutex_lock(&cpuidle_lock);
370 __cpuidle_device_init(dev);
372 ret = __cpuidle_register_device(dev);
376 ret = cpuidle_add_sysfs(dev);
380 ret = cpuidle_enable_device(dev);
384 cpuidle_install_idle_handler();
387 mutex_unlock(&cpuidle_lock);
392 cpuidle_remove_sysfs(dev);
394 __cpuidle_unregister_device(dev);
398 EXPORT_SYMBOL_GPL(cpuidle_register_device);
401 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
404 void cpuidle_unregister_device(struct cpuidle_device *dev)
406 if (!dev || dev->registered == 0)
409 cpuidle_pause_and_lock();
411 cpuidle_disable_device(dev);
413 cpuidle_remove_sysfs(dev);
415 __cpuidle_unregister_device(dev);
417 cpuidle_coupled_unregister_device(dev);
419 cpuidle_resume_and_unlock();
422 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
425 * cpuidle_unregister: unregister a driver and the devices. This function
426 * can be used only if the driver has been previously registered through
427 * the cpuidle_register function.
429 * @drv: a valid pointer to a struct cpuidle_driver
431 void cpuidle_unregister(struct cpuidle_driver *drv)
434 struct cpuidle_device *device;
436 for_each_cpu(cpu, drv->cpumask) {
437 device = &per_cpu(cpuidle_dev, cpu);
438 cpuidle_unregister_device(device);
441 cpuidle_unregister_driver(drv);
443 EXPORT_SYMBOL_GPL(cpuidle_unregister);
446 * cpuidle_register: registers the driver and the cpu devices with the
447 * coupled_cpus passed as parameter. This function is used for all common
448 * initialization pattern there are in the arch specific drivers. The
449 * devices is globally defined in this file.
451 * @drv : a valid pointer to a struct cpuidle_driver
452 * @coupled_cpus: a cpumask for the coupled states
454 * Returns 0 on success, < 0 otherwise
456 int cpuidle_register(struct cpuidle_driver *drv,
457 const struct cpumask *const coupled_cpus)
460 struct cpuidle_device *device;
462 ret = cpuidle_register_driver(drv);
464 pr_err("failed to register cpuidle driver\n");
468 for_each_cpu(cpu, drv->cpumask) {
469 device = &per_cpu(cpuidle_dev, cpu);
472 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
474 * On multiplatform for ARM, the coupled idle states could be
475 * enabled in the kernel even if the cpuidle driver does not
476 * use it. Note, coupled_cpus is a struct copy.
479 device->coupled_cpus = *coupled_cpus;
481 ret = cpuidle_register_device(device);
485 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
487 cpuidle_unregister(drv);
493 EXPORT_SYMBOL_GPL(cpuidle_register);
497 static void smp_callback(void *v)
499 /* we already woke the CPU up, nothing more to do */
503 * This function gets called when a part of the kernel has a new latency
504 * requirement. This means we need to get all processors out of their C-state,
505 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
506 * wakes them all right up.
508 static int cpuidle_latency_notify(struct notifier_block *b,
509 unsigned long l, void *v)
511 smp_call_function(smp_callback, NULL, 1);
515 static struct notifier_block cpuidle_latency_notifier = {
516 .notifier_call = cpuidle_latency_notify,
519 static inline void latency_notifier_init(struct notifier_block *n)
521 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
524 #else /* CONFIG_SMP */
526 #define latency_notifier_init(x) do { } while (0)
528 #endif /* CONFIG_SMP */
531 * cpuidle_init - core initializer
533 static int __init cpuidle_init(void)
537 if (cpuidle_disabled())
540 ret = cpuidle_add_interface(cpu_subsys.dev_root);
544 latency_notifier_init(&cpuidle_latency_notifier);
549 module_param(off, int, 0444);
550 core_initcall(cpuidle_init);