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/kernel.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h>
14 #include <linux/notifier.h>
15 #include <linux/pm_qos.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/ktime.h>
19 #include <linux/hrtimer.h>
20 #include <linux/module.h>
21 #include <trace/events/power.h>
25 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
27 DEFINE_MUTEX(cpuidle_lock);
28 LIST_HEAD(cpuidle_detected_devices);
30 static int enabled_devices;
31 static int off __read_mostly;
32 static int initialized __read_mostly;
34 int cpuidle_disabled(void)
38 void disable_cpuidle(void)
43 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
44 static void cpuidle_kick_cpus(void)
48 #elif defined(CONFIG_SMP)
49 # error "Arch needs cpu_idle_wait() equivalent here"
50 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
51 static void cpuidle_kick_cpus(void) {}
54 static int __cpuidle_register_device(struct cpuidle_device *dev);
57 * cpuidle_idle_call - the main idle loop
59 * NOTE: no locks or semaphores should be used here
60 * return non-zero on failure
62 int cpuidle_idle_call(void)
64 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
65 struct cpuidle_state *target_state;
74 /* check if the device is ready */
75 if (!dev || !dev->enabled)
79 /* shows regressions, re-enable for 2.6.29 */
81 * run any timers that can be run now, at this point
82 * before calculating the idle duration etc.
84 hrtimer_peek_ahead_timers();
88 * Call the device's prepare function before calling the
89 * governor's select function. ->prepare gives the device's
90 * cpuidle driver a chance to update any dynamic information
91 * of its cpuidle states for the current idle period, e.g.
92 * state availability, latencies, residencies, etc.
97 /* ask the governor for the next state */
98 next_state = cpuidle_curr_governor->select(dev);
104 target_state = &dev->states[next_state];
106 /* enter the state and update stats */
107 dev->last_state = target_state;
109 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
110 trace_cpu_idle(next_state, dev->cpu);
112 dev->last_residency = target_state->enter(dev, target_state);
114 trace_power_end(dev->cpu);
115 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
118 target_state = dev->last_state;
120 target_state->time += (unsigned long long)dev->last_residency;
121 target_state->usage++;
123 /* give the governor an opportunity to reflect on the outcome */
124 if (cpuidle_curr_governor->reflect)
125 cpuidle_curr_governor->reflect(dev);
131 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
133 void cpuidle_install_idle_handler(void)
135 if (enabled_devices) {
136 /* Make sure all changes finished before we switch to new idle */
143 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
145 void cpuidle_uninstall_idle_handler(void)
147 if (enabled_devices) {
154 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
156 void cpuidle_pause_and_lock(void)
158 mutex_lock(&cpuidle_lock);
159 cpuidle_uninstall_idle_handler();
162 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
165 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
167 void cpuidle_resume_and_unlock(void)
169 cpuidle_install_idle_handler();
170 mutex_unlock(&cpuidle_lock);
173 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
175 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
176 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
184 while (!need_resched())
188 diff = ktime_to_us(ktime_sub(t2, t1));
196 static void poll_idle_init(struct cpuidle_device *dev)
198 struct cpuidle_state *state = &dev->states[0];
200 cpuidle_set_statedata(state, NULL);
202 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
203 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
204 state->exit_latency = 0;
205 state->target_residency = 0;
206 state->power_usage = -1;
208 state->enter = poll_idle;
211 static void poll_idle_init(struct cpuidle_device *dev) {}
212 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
215 * cpuidle_enable_device - enables idle PM for a CPU
218 * This function must be called between cpuidle_pause_and_lock and
219 * cpuidle_resume_and_unlock when used externally.
221 int cpuidle_enable_device(struct cpuidle_device *dev)
227 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
229 if (!dev->state_count)
232 if (dev->registered == 0) {
233 ret = __cpuidle_register_device(dev);
240 if ((ret = cpuidle_add_state_sysfs(dev)))
243 if (cpuidle_curr_governor->enable &&
244 (ret = cpuidle_curr_governor->enable(dev)))
247 for (i = 0; i < dev->state_count; i++) {
248 dev->states[i].usage = 0;
249 dev->states[i].time = 0;
251 dev->last_residency = 0;
252 dev->last_state = NULL;
262 cpuidle_remove_state_sysfs(dev);
267 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
270 * cpuidle_disable_device - disables idle PM for a CPU
273 * This function must be called between cpuidle_pause_and_lock and
274 * cpuidle_resume_and_unlock when used externally.
276 void cpuidle_disable_device(struct cpuidle_device *dev)
280 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
285 if (cpuidle_curr_governor->disable)
286 cpuidle_curr_governor->disable(dev);
288 cpuidle_remove_state_sysfs(dev);
292 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
295 * __cpuidle_register_device - internal register function called before register
296 * and enable routines
299 * cpuidle_lock mutex must be held before this is called
301 static int __cpuidle_register_device(struct cpuidle_device *dev)
304 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
305 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
309 if (!try_module_get(cpuidle_driver->owner))
312 init_completion(&dev->kobj_unregister);
315 * cpuidle driver should set the dev->power_specified bit
316 * before registering the device if the driver provides
317 * power_usage numbers.
319 * For those devices whose ->power_specified is not set,
320 * we fill in power_usage with decreasing values as the
321 * cpuidle code has an implicit assumption that state Cn
322 * uses less power than C(n-1).
324 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
325 * an power value of -1. So we use -2, -3, etc, for other
328 if (!dev->power_specified) {
330 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
331 dev->states[i].power_usage = -1 - i;
334 per_cpu(cpuidle_devices, dev->cpu) = dev;
335 list_add(&dev->device_list, &cpuidle_detected_devices);
336 if ((ret = cpuidle_add_sysfs(sys_dev))) {
337 module_put(cpuidle_driver->owner);
346 * cpuidle_register_device - registers a CPU's idle PM feature
349 int cpuidle_register_device(struct cpuidle_device *dev)
353 mutex_lock(&cpuidle_lock);
355 if ((ret = __cpuidle_register_device(dev))) {
356 mutex_unlock(&cpuidle_lock);
360 cpuidle_enable_device(dev);
361 cpuidle_install_idle_handler();
363 mutex_unlock(&cpuidle_lock);
369 EXPORT_SYMBOL_GPL(cpuidle_register_device);
372 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
375 void cpuidle_unregister_device(struct cpuidle_device *dev)
377 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
378 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
380 if (dev->registered == 0)
383 cpuidle_pause_and_lock();
385 cpuidle_disable_device(dev);
387 cpuidle_remove_sysfs(sys_dev);
388 list_del(&dev->device_list);
389 wait_for_completion(&dev->kobj_unregister);
390 per_cpu(cpuidle_devices, dev->cpu) = NULL;
392 cpuidle_resume_and_unlock();
394 module_put(cpuidle_driver->owner);
397 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
401 static void smp_callback(void *v)
403 /* we already woke the CPU up, nothing more to do */
407 * This function gets called when a part of the kernel has a new latency
408 * requirement. This means we need to get all processors out of their C-state,
409 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
410 * wakes them all right up.
412 static int cpuidle_latency_notify(struct notifier_block *b,
413 unsigned long l, void *v)
415 smp_call_function(smp_callback, NULL, 1);
419 static struct notifier_block cpuidle_latency_notifier = {
420 .notifier_call = cpuidle_latency_notify,
423 static inline void latency_notifier_init(struct notifier_block *n)
425 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
428 #else /* CONFIG_SMP */
430 #define latency_notifier_init(x) do { } while (0)
432 #endif /* CONFIG_SMP */
435 * cpuidle_init - core initializer
437 static int __init cpuidle_init(void)
441 if (cpuidle_disabled())
444 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
448 latency_notifier_init(&cpuidle_latency_notifier);
453 module_param(off, int, 0444);
454 core_initcall(cpuidle_init);