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)
45 static int __cpuidle_register_device(struct cpuidle_device *dev);
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_enter_state - enter the state and update stats
71 * @dev: cpuidle device for this cpu
72 * @drv: cpuidle driver for this cpu
73 * @next_state: index into drv->states of the state to enter
75 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
80 struct cpuidle_state *target_state = &drv->states[index];
81 ktime_t time_start, time_end;
84 time_start = ktime_get();
86 entered_state = target_state->enter(dev, drv, index);
88 time_end = ktime_get();
92 diff = ktime_to_us(ktime_sub(time_end, time_start));
96 dev->last_residency = (int) diff;
98 if (entered_state >= 0) {
99 /* Update cpuidle counters */
100 /* This can be moved to within driver enter routine
101 * but that results in multiple copies of same code.
103 dev->states_usage[entered_state].time += dev->last_residency;
104 dev->states_usage[entered_state].usage++;
106 dev->last_residency = 0;
109 return entered_state;
113 * cpuidle_idle_call - the main idle loop
115 * NOTE: no locks or semaphores should be used here
116 * return non-zero on failure
118 int cpuidle_idle_call(void)
120 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
121 struct cpuidle_driver *drv;
122 int next_state, entered_state;
130 /* check if the device is ready */
131 if (!dev || !dev->enabled)
134 drv = cpuidle_get_cpu_driver(dev);
136 /* ask the governor for the next state */
137 next_state = cpuidle_curr_governor->select(drv, dev);
138 if (need_resched()) {
139 dev->last_residency = 0;
140 /* give the governor an opportunity to reflect on the outcome */
141 if (cpuidle_curr_governor->reflect)
142 cpuidle_curr_governor->reflect(dev, next_state);
147 trace_cpu_idle_rcuidle(next_state, dev->cpu);
149 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
150 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
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 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
160 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
163 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
165 /* give the governor an opportunity to reflect on the outcome */
166 if (cpuidle_curr_governor->reflect)
167 cpuidle_curr_governor->reflect(dev, entered_state);
173 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
175 void cpuidle_install_idle_handler(void)
177 if (enabled_devices) {
178 /* Make sure all changes finished before we switch to new idle */
185 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
187 void cpuidle_uninstall_idle_handler(void)
189 if (enabled_devices) {
191 kick_all_cpus_sync();
196 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
198 void cpuidle_pause_and_lock(void)
200 mutex_lock(&cpuidle_lock);
201 cpuidle_uninstall_idle_handler();
204 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
207 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
209 void cpuidle_resume_and_unlock(void)
211 cpuidle_install_idle_handler();
212 mutex_unlock(&cpuidle_lock);
215 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
217 /* Currently used in suspend/resume path to suspend cpuidle */
218 void cpuidle_pause(void)
220 mutex_lock(&cpuidle_lock);
221 cpuidle_uninstall_idle_handler();
222 mutex_unlock(&cpuidle_lock);
225 /* Currently used in suspend/resume path to resume cpuidle */
226 void cpuidle_resume(void)
228 mutex_lock(&cpuidle_lock);
229 cpuidle_install_idle_handler();
230 mutex_unlock(&cpuidle_lock);
233 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
234 static int poll_idle(struct cpuidle_device *dev,
235 struct cpuidle_driver *drv, int index)
242 while (!need_resched())
246 diff = ktime_to_us(ktime_sub(t2, t1));
250 dev->last_residency = (int) diff;
255 static void poll_idle_init(struct cpuidle_driver *drv)
257 struct cpuidle_state *state = &drv->states[0];
259 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
260 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
261 state->exit_latency = 0;
262 state->target_residency = 0;
263 state->power_usage = -1;
265 state->enter = poll_idle;
266 state->disabled = false;
269 static void poll_idle_init(struct cpuidle_driver *drv) {}
270 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
273 * cpuidle_enable_device - enables idle PM for a CPU
276 * This function must be called between cpuidle_pause_and_lock and
277 * cpuidle_resume_and_unlock when used externally.
279 int cpuidle_enable_device(struct cpuidle_device *dev)
282 struct cpuidle_driver *drv;
290 drv = cpuidle_get_cpu_driver(dev);
292 if (!drv || !cpuidle_curr_governor)
295 if (!dev->registered)
298 if (!dev->state_count)
299 dev->state_count = drv->state_count;
303 ret = cpuidle_add_device_sysfs(dev);
307 if (cpuidle_curr_governor->enable &&
308 (ret = cpuidle_curr_governor->enable(drv, dev)))
311 for (i = 0; i < dev->state_count; i++) {
312 dev->states_usage[i].usage = 0;
313 dev->states_usage[i].time = 0;
315 dev->last_residency = 0;
325 cpuidle_remove_device_sysfs(dev);
330 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
333 * cpuidle_disable_device - disables idle PM for a CPU
336 * This function must be called between cpuidle_pause_and_lock and
337 * cpuidle_resume_and_unlock when used externally.
339 void cpuidle_disable_device(struct cpuidle_device *dev)
341 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
343 if (!dev || !dev->enabled)
346 if (!drv || !cpuidle_curr_governor)
351 if (cpuidle_curr_governor->disable)
352 cpuidle_curr_governor->disable(drv, dev);
354 cpuidle_remove_device_sysfs(dev);
358 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
361 * __cpuidle_register_device - internal register function called before register
362 * and enable routines
365 * cpuidle_lock mutex must be held before this is called
367 static int __cpuidle_register_device(struct cpuidle_device *dev)
370 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
372 if (!try_module_get(drv->owner))
375 per_cpu(cpuidle_devices, dev->cpu) = dev;
376 list_add(&dev->device_list, &cpuidle_detected_devices);
377 ret = cpuidle_add_sysfs(dev);
381 ret = cpuidle_coupled_register_device(dev);
389 cpuidle_remove_sysfs(dev);
391 list_del(&dev->device_list);
392 per_cpu(cpuidle_devices, dev->cpu) = NULL;
393 module_put(drv->owner);
398 * cpuidle_register_device - registers a CPU's idle PM feature
401 int cpuidle_register_device(struct cpuidle_device *dev)
408 mutex_lock(&cpuidle_lock);
410 if ((ret = __cpuidle_register_device(dev))) {
411 mutex_unlock(&cpuidle_lock);
415 ret = cpuidle_enable_device(dev);
417 mutex_unlock(&cpuidle_lock);
421 cpuidle_install_idle_handler();
423 mutex_unlock(&cpuidle_lock);
428 EXPORT_SYMBOL_GPL(cpuidle_register_device);
431 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
434 void cpuidle_unregister_device(struct cpuidle_device *dev)
436 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
438 if (dev->registered == 0)
441 cpuidle_pause_and_lock();
443 cpuidle_disable_device(dev);
445 cpuidle_remove_sysfs(dev);
446 list_del(&dev->device_list);
447 per_cpu(cpuidle_devices, dev->cpu) = NULL;
449 cpuidle_coupled_unregister_device(dev);
451 cpuidle_resume_and_unlock();
453 module_put(drv->owner);
456 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
459 * cpuidle_unregister: unregister a driver and the devices. This function
460 * can be used only if the driver has been previously registered through
461 * the cpuidle_register function.
463 * @drv: a valid pointer to a struct cpuidle_driver
465 void cpuidle_unregister(struct cpuidle_driver *drv)
468 struct cpuidle_device *device;
470 for_each_cpu(cpu, drv->cpumask) {
471 device = &per_cpu(cpuidle_dev, cpu);
472 cpuidle_unregister_device(device);
475 cpuidle_unregister_driver(drv);
477 EXPORT_SYMBOL_GPL(cpuidle_unregister);
480 * cpuidle_register: registers the driver and the cpu devices with the
481 * coupled_cpus passed as parameter. This function is used for all common
482 * initialization pattern there are in the arch specific drivers. The
483 * devices is globally defined in this file.
485 * @drv : a valid pointer to a struct cpuidle_driver
486 * @coupled_cpus: a cpumask for the coupled states
488 * Returns 0 on success, < 0 otherwise
490 int cpuidle_register(struct cpuidle_driver *drv,
491 const struct cpumask *const coupled_cpus)
494 struct cpuidle_device *device;
496 ret = cpuidle_register_driver(drv);
498 pr_err("failed to register cpuidle driver\n");
502 for_each_cpu(cpu, drv->cpumask) {
503 device = &per_cpu(cpuidle_dev, cpu);
506 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
508 * On multiplatform for ARM, the coupled idle states could
509 * enabled in the kernel even if the cpuidle driver does not
510 * use it. Note, coupled_cpus is a struct copy.
513 device->coupled_cpus = *coupled_cpus;
515 ret = cpuidle_register_device(device);
519 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
521 cpuidle_unregister(drv);
527 EXPORT_SYMBOL_GPL(cpuidle_register);
531 static void smp_callback(void *v)
533 /* we already woke the CPU up, nothing more to do */
537 * This function gets called when a part of the kernel has a new latency
538 * requirement. This means we need to get all processors out of their C-state,
539 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
540 * wakes them all right up.
542 static int cpuidle_latency_notify(struct notifier_block *b,
543 unsigned long l, void *v)
545 smp_call_function(smp_callback, NULL, 1);
549 static struct notifier_block cpuidle_latency_notifier = {
550 .notifier_call = cpuidle_latency_notify,
553 static inline void latency_notifier_init(struct notifier_block *n)
555 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
558 #else /* CONFIG_SMP */
560 #define latency_notifier_init(x) do { } while (0)
562 #endif /* CONFIG_SMP */
565 * cpuidle_init - core initializer
567 static int __init cpuidle_init(void)
571 if (cpuidle_disabled())
574 ret = cpuidle_add_interface(cpu_subsys.dev_root);
578 latency_notifier_init(&cpuidle_latency_notifier);
583 module_param(off, int, 0444);
584 core_initcall(cpuidle_init);