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_params.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/ktime.h>
19 #include <linux/hrtimer.h>
20 #include <trace/events/power.h>
24 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
26 DEFINE_MUTEX(cpuidle_lock);
27 LIST_HEAD(cpuidle_detected_devices);
29 static int enabled_devices;
30 static int off __read_mostly;
31 static int initialized __read_mostly;
33 int cpuidle_disabled(void)
37 void disable_cpuidle(void)
42 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
43 static void cpuidle_kick_cpus(void)
47 #elif defined(CONFIG_SMP)
48 # error "Arch needs cpu_idle_wait() equivalent here"
49 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
50 static void cpuidle_kick_cpus(void) {}
53 static int __cpuidle_register_device(struct cpuidle_device *dev);
56 * cpuidle_idle_call - the main idle loop
58 * NOTE: no locks or semaphores should be used here
59 * return non-zero on failure
61 int cpuidle_idle_call(void)
63 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
64 struct cpuidle_state *target_state;
65 int next_state, entered_state;
73 /* check if the device is ready */
74 if (!dev || !dev->enabled)
78 /* shows regressions, re-enable for 2.6.29 */
80 * run any timers that can be run now, at this point
81 * before calculating the idle duration etc.
83 hrtimer_peek_ahead_timers();
86 /* ask the governor for the next state */
87 next_state = cpuidle_curr_governor->select(dev);
93 target_state = &dev->states[next_state];
95 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
96 trace_cpu_idle(next_state, dev->cpu);
98 entered_state = target_state->enter(dev, next_state);
100 trace_power_end(dev->cpu);
101 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
103 if (entered_state >= 0) {
104 /* Update cpuidle counters */
105 /* This can be moved to within driver enter routine
106 * but that results in multiple copies of same code.
108 dev->states_usage[entered_state].time +=
109 (unsigned long long)dev->last_residency;
110 dev->states_usage[entered_state].usage++;
113 /* give the governor an opportunity to reflect on the outcome */
114 if (cpuidle_curr_governor->reflect)
115 cpuidle_curr_governor->reflect(dev, entered_state);
121 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
123 void cpuidle_install_idle_handler(void)
125 if (enabled_devices) {
126 /* Make sure all changes finished before we switch to new idle */
133 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
135 void cpuidle_uninstall_idle_handler(void)
137 if (enabled_devices) {
144 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
146 void cpuidle_pause_and_lock(void)
148 mutex_lock(&cpuidle_lock);
149 cpuidle_uninstall_idle_handler();
152 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
155 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
157 void cpuidle_resume_and_unlock(void)
159 cpuidle_install_idle_handler();
160 mutex_unlock(&cpuidle_lock);
163 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
165 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
166 static int poll_idle(struct cpuidle_device *dev, int index)
173 while (!need_resched())
177 diff = ktime_to_us(ktime_sub(t2, t1));
181 dev->last_residency = (int) diff;
186 static void poll_idle_init(struct cpuidle_device *dev)
188 struct cpuidle_state *state = &dev->states[0];
189 struct cpuidle_state_usage *state_usage = &dev->states_usage[0];
191 cpuidle_set_statedata(state_usage, NULL);
193 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
194 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
195 state->exit_latency = 0;
196 state->target_residency = 0;
197 state->power_usage = -1;
199 state->enter = poll_idle;
202 static void poll_idle_init(struct cpuidle_device *dev) {}
203 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
206 * cpuidle_enable_device - enables idle PM for a CPU
209 * This function must be called between cpuidle_pause_and_lock and
210 * cpuidle_resume_and_unlock when used externally.
212 int cpuidle_enable_device(struct cpuidle_device *dev)
218 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
220 if (!dev->state_count)
223 if (dev->registered == 0) {
224 ret = __cpuidle_register_device(dev);
231 if ((ret = cpuidle_add_state_sysfs(dev)))
234 if (cpuidle_curr_governor->enable &&
235 (ret = cpuidle_curr_governor->enable(dev)))
238 for (i = 0; i < dev->state_count; i++) {
239 dev->states_usage[i].usage = 0;
240 dev->states_usage[i].time = 0;
242 dev->last_residency = 0;
252 cpuidle_remove_state_sysfs(dev);
257 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
260 * cpuidle_disable_device - disables idle PM for a CPU
263 * This function must be called between cpuidle_pause_and_lock and
264 * cpuidle_resume_and_unlock when used externally.
266 void cpuidle_disable_device(struct cpuidle_device *dev)
270 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
275 if (cpuidle_curr_governor->disable)
276 cpuidle_curr_governor->disable(dev);
278 cpuidle_remove_state_sysfs(dev);
282 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
285 * __cpuidle_register_device - internal register function called before register
286 * and enable routines
289 * cpuidle_lock mutex must be held before this is called
291 static int __cpuidle_register_device(struct cpuidle_device *dev)
294 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
295 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
299 if (!try_module_get(cpuidle_driver->owner))
302 init_completion(&dev->kobj_unregister);
305 * cpuidle driver should set the dev->power_specified bit
306 * before registering the device if the driver provides
307 * power_usage numbers.
309 * For those devices whose ->power_specified is not set,
310 * we fill in power_usage with decreasing values as the
311 * cpuidle code has an implicit assumption that state Cn
312 * uses less power than C(n-1).
314 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
315 * an power value of -1. So we use -2, -3, etc, for other
318 if (!dev->power_specified) {
320 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
321 dev->states[i].power_usage = -1 - i;
324 per_cpu(cpuidle_devices, dev->cpu) = dev;
325 list_add(&dev->device_list, &cpuidle_detected_devices);
326 if ((ret = cpuidle_add_sysfs(sys_dev))) {
327 module_put(cpuidle_driver->owner);
336 * cpuidle_register_device - registers a CPU's idle PM feature
339 int cpuidle_register_device(struct cpuidle_device *dev)
343 mutex_lock(&cpuidle_lock);
345 if ((ret = __cpuidle_register_device(dev))) {
346 mutex_unlock(&cpuidle_lock);
350 cpuidle_enable_device(dev);
351 cpuidle_install_idle_handler();
353 mutex_unlock(&cpuidle_lock);
359 EXPORT_SYMBOL_GPL(cpuidle_register_device);
362 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
365 void cpuidle_unregister_device(struct cpuidle_device *dev)
367 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
368 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
370 if (dev->registered == 0)
373 cpuidle_pause_and_lock();
375 cpuidle_disable_device(dev);
377 cpuidle_remove_sysfs(sys_dev);
378 list_del(&dev->device_list);
379 wait_for_completion(&dev->kobj_unregister);
380 per_cpu(cpuidle_devices, dev->cpu) = NULL;
382 cpuidle_resume_and_unlock();
384 module_put(cpuidle_driver->owner);
387 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
391 static void smp_callback(void *v)
393 /* we already woke the CPU up, nothing more to do */
397 * This function gets called when a part of the kernel has a new latency
398 * requirement. This means we need to get all processors out of their C-state,
399 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
400 * wakes them all right up.
402 static int cpuidle_latency_notify(struct notifier_block *b,
403 unsigned long l, void *v)
405 smp_call_function(smp_callback, NULL, 1);
409 static struct notifier_block cpuidle_latency_notifier = {
410 .notifier_call = cpuidle_latency_notify,
413 static inline void latency_notifier_init(struct notifier_block *n)
415 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
418 #else /* CONFIG_SMP */
420 #define latency_notifier_init(x) do { } while (0)
422 #endif /* CONFIG_SMP */
425 * cpuidle_init - core initializer
427 static int __init cpuidle_init(void)
431 if (cpuidle_disabled())
434 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
438 latency_notifier_init(&cpuidle_latency_notifier);
443 module_param(off, int, 0444);
444 core_initcall(cpuidle_init);