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 <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;
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();
87 * Call the device's prepare function before calling the
88 * governor's select function. ->prepare gives the device's
89 * cpuidle driver a chance to update any dynamic information
90 * of its cpuidle states for the current idle period, e.g.
91 * state availability, latencies, residencies, etc.
96 /* ask the governor for the next state */
97 next_state = cpuidle_curr_governor->select(dev);
103 target_state = &dev->states[next_state];
105 /* enter the state and update stats */
106 dev->last_state = target_state;
108 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
109 trace_cpu_idle(next_state, dev->cpu);
111 dev->last_residency = target_state->enter(dev, target_state);
113 trace_power_end(dev->cpu);
114 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
117 target_state = dev->last_state;
119 target_state->time += (unsigned long long)dev->last_residency;
120 target_state->usage++;
122 /* give the governor an opportunity to reflect on the outcome */
123 if (cpuidle_curr_governor->reflect)
124 cpuidle_curr_governor->reflect(dev);
130 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
132 void cpuidle_install_idle_handler(void)
134 if (enabled_devices) {
135 /* Make sure all changes finished before we switch to new idle */
142 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
144 void cpuidle_uninstall_idle_handler(void)
146 if (enabled_devices) {
153 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
155 void cpuidle_pause_and_lock(void)
157 mutex_lock(&cpuidle_lock);
158 cpuidle_uninstall_idle_handler();
161 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
164 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
166 void cpuidle_resume_and_unlock(void)
168 cpuidle_install_idle_handler();
169 mutex_unlock(&cpuidle_lock);
172 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
174 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
175 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
183 while (!need_resched())
187 diff = ktime_to_us(ktime_sub(t2, t1));
195 static void poll_idle_init(struct cpuidle_device *dev)
197 struct cpuidle_state *state = &dev->states[0];
199 cpuidle_set_statedata(state, NULL);
201 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
202 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
203 state->exit_latency = 0;
204 state->target_residency = 0;
205 state->power_usage = -1;
207 state->enter = poll_idle;
210 static void poll_idle_init(struct cpuidle_device *dev) {}
211 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
214 * cpuidle_enable_device - enables idle PM for a CPU
217 * This function must be called between cpuidle_pause_and_lock and
218 * cpuidle_resume_and_unlock when used externally.
220 int cpuidle_enable_device(struct cpuidle_device *dev)
226 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
228 if (!dev->state_count)
231 if (dev->registered == 0) {
232 ret = __cpuidle_register_device(dev);
239 if ((ret = cpuidle_add_state_sysfs(dev)))
242 if (cpuidle_curr_governor->enable &&
243 (ret = cpuidle_curr_governor->enable(dev)))
246 for (i = 0; i < dev->state_count; i++) {
247 dev->states[i].usage = 0;
248 dev->states[i].time = 0;
250 dev->last_residency = 0;
251 dev->last_state = NULL;
261 cpuidle_remove_state_sysfs(dev);
266 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
269 * cpuidle_disable_device - disables idle PM for a CPU
272 * This function must be called between cpuidle_pause_and_lock and
273 * cpuidle_resume_and_unlock when used externally.
275 void cpuidle_disable_device(struct cpuidle_device *dev)
279 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
284 if (cpuidle_curr_governor->disable)
285 cpuidle_curr_governor->disable(dev);
287 cpuidle_remove_state_sysfs(dev);
291 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
294 * __cpuidle_register_device - internal register function called before register
295 * and enable routines
298 * cpuidle_lock mutex must be held before this is called
300 static int __cpuidle_register_device(struct cpuidle_device *dev)
303 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
304 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
308 if (!try_module_get(cpuidle_driver->owner))
311 init_completion(&dev->kobj_unregister);
314 * cpuidle driver should set the dev->power_specified bit
315 * before registering the device if the driver provides
316 * power_usage numbers.
318 * For those devices whose ->power_specified is not set,
319 * we fill in power_usage with decreasing values as the
320 * cpuidle code has an implicit assumption that state Cn
321 * uses less power than C(n-1).
323 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
324 * an power value of -1. So we use -2, -3, etc, for other
327 if (!dev->power_specified) {
329 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
330 dev->states[i].power_usage = -1 - i;
333 per_cpu(cpuidle_devices, dev->cpu) = dev;
334 list_add(&dev->device_list, &cpuidle_detected_devices);
335 if ((ret = cpuidle_add_sysfs(sys_dev))) {
336 module_put(cpuidle_driver->owner);
345 * cpuidle_register_device - registers a CPU's idle PM feature
348 int cpuidle_register_device(struct cpuidle_device *dev)
352 mutex_lock(&cpuidle_lock);
354 if ((ret = __cpuidle_register_device(dev))) {
355 mutex_unlock(&cpuidle_lock);
359 cpuidle_enable_device(dev);
360 cpuidle_install_idle_handler();
362 mutex_unlock(&cpuidle_lock);
368 EXPORT_SYMBOL_GPL(cpuidle_register_device);
371 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
374 void cpuidle_unregister_device(struct cpuidle_device *dev)
376 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
377 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
379 if (dev->registered == 0)
382 cpuidle_pause_and_lock();
384 cpuidle_disable_device(dev);
386 cpuidle_remove_sysfs(sys_dev);
387 list_del(&dev->device_list);
388 wait_for_completion(&dev->kobj_unregister);
389 per_cpu(cpuidle_devices, dev->cpu) = NULL;
391 cpuidle_resume_and_unlock();
393 module_put(cpuidle_driver->owner);
396 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
400 static void smp_callback(void *v)
402 /* we already woke the CPU up, nothing more to do */
406 * This function gets called when a part of the kernel has a new latency
407 * requirement. This means we need to get all processors out of their C-state,
408 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
409 * wakes them all right up.
411 static int cpuidle_latency_notify(struct notifier_block *b,
412 unsigned long l, void *v)
414 smp_call_function(smp_callback, NULL, 1);
418 static struct notifier_block cpuidle_latency_notifier = {
419 .notifier_call = cpuidle_latency_notify,
422 static inline void latency_notifier_init(struct notifier_block *n)
424 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
427 #else /* CONFIG_SMP */
429 #define latency_notifier_init(x) do { } while (0)
431 #endif /* CONFIG_SMP */
434 * cpuidle_init - core initializer
436 static int __init cpuidle_init(void)
440 if (cpuidle_disabled())
443 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
447 latency_notifier_init(&cpuidle_latency_notifier);
452 module_param(off, int, 0444);
453 core_initcall(cpuidle_init);