Merge branch 'linus' into idle-test
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / cpuidle / cpuidle.c
bloba2d5b95a6d64f01d7b913ca74b83512c0c7c2a62
1 /*
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.
9 */
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>
22 #include "cpuidle.h"
24 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
26 DEFINE_MUTEX(cpuidle_lock);
27 LIST_HEAD(cpuidle_detected_devices);
28 static void (*pm_idle_old)(void);
30 static int enabled_devices;
32 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
33 static void cpuidle_kick_cpus(void)
35 cpu_idle_wait();
37 #elif defined(CONFIG_SMP)
38 # error "Arch needs cpu_idle_wait() equivalent here"
39 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
40 static void cpuidle_kick_cpus(void) {}
41 #endif
43 static int __cpuidle_register_device(struct cpuidle_device *dev);
45 /**
46 * cpuidle_idle_call - the main idle loop
48 * NOTE: no locks or semaphores should be used here
50 static void cpuidle_idle_call(void)
52 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
53 struct cpuidle_state *target_state;
54 int next_state;
56 /* check if the device is ready */
57 if (!dev || !dev->enabled) {
58 if (pm_idle_old)
59 pm_idle_old();
60 else
61 #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
62 default_idle();
63 #else
64 local_irq_enable();
65 #endif
66 return;
69 #if 0
70 /* shows regressions, re-enable for 2.6.29 */
72 * run any timers that can be run now, at this point
73 * before calculating the idle duration etc.
75 hrtimer_peek_ahead_timers();
76 #endif
79 * Call the device's prepare function before calling the
80 * governor's select function. ->prepare gives the device's
81 * cpuidle driver a chance to update any dynamic information
82 * of its cpuidle states for the current idle period, e.g.
83 * state availability, latencies, residencies, etc.
85 if (dev->prepare)
86 dev->prepare(dev);
88 /* ask the governor for the next state */
89 next_state = cpuidle_curr_governor->select(dev);
90 if (need_resched()) {
91 local_irq_enable();
92 return;
95 target_state = &dev->states[next_state];
97 /* enter the state and update stats */
98 dev->last_state = target_state;
99 dev->last_residency = target_state->enter(dev, target_state);
100 if (dev->last_state)
101 target_state = dev->last_state;
103 target_state->time += (unsigned long long)dev->last_residency;
104 target_state->usage++;
106 /* give the governor an opportunity to reflect on the outcome */
107 if (cpuidle_curr_governor->reflect)
108 cpuidle_curr_governor->reflect(dev);
109 trace_power_end(smp_processor_id());
110 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
114 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
116 void cpuidle_install_idle_handler(void)
118 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
119 /* Make sure all changes finished before we switch to new idle */
120 smp_wmb();
121 pm_idle = cpuidle_idle_call;
126 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
128 void cpuidle_uninstall_idle_handler(void)
130 if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
131 pm_idle = pm_idle_old;
132 cpuidle_kick_cpus();
137 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
139 void cpuidle_pause_and_lock(void)
141 mutex_lock(&cpuidle_lock);
142 cpuidle_uninstall_idle_handler();
145 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
148 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
150 void cpuidle_resume_and_unlock(void)
152 cpuidle_install_idle_handler();
153 mutex_unlock(&cpuidle_lock);
156 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
158 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
159 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
161 ktime_t t1, t2;
162 s64 diff;
163 int ret;
165 t1 = ktime_get();
166 local_irq_enable();
167 while (!need_resched())
168 cpu_relax();
170 t2 = ktime_get();
171 diff = ktime_to_us(ktime_sub(t2, t1));
172 if (diff > INT_MAX)
173 diff = INT_MAX;
175 ret = (int) diff;
176 return ret;
179 static void poll_idle_init(struct cpuidle_device *dev)
181 struct cpuidle_state *state = &dev->states[0];
183 cpuidle_set_statedata(state, NULL);
185 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
186 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
187 state->exit_latency = 0;
188 state->target_residency = 0;
189 state->power_usage = -1;
190 state->flags = 0;
191 state->enter = poll_idle;
193 #else
194 static void poll_idle_init(struct cpuidle_device *dev) {}
195 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
198 * cpuidle_enable_device - enables idle PM for a CPU
199 * @dev: the CPU
201 * This function must be called between cpuidle_pause_and_lock and
202 * cpuidle_resume_and_unlock when used externally.
204 int cpuidle_enable_device(struct cpuidle_device *dev)
206 int ret, i;
208 if (dev->enabled)
209 return 0;
210 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
211 return -EIO;
212 if (!dev->state_count)
213 return -EINVAL;
215 if (dev->registered == 0) {
216 ret = __cpuidle_register_device(dev);
217 if (ret)
218 return ret;
221 poll_idle_init(dev);
223 if ((ret = cpuidle_add_state_sysfs(dev)))
224 return ret;
226 if (cpuidle_curr_governor->enable &&
227 (ret = cpuidle_curr_governor->enable(dev)))
228 goto fail_sysfs;
230 for (i = 0; i < dev->state_count; i++) {
231 dev->states[i].usage = 0;
232 dev->states[i].time = 0;
234 dev->last_residency = 0;
235 dev->last_state = NULL;
237 smp_wmb();
239 dev->enabled = 1;
241 enabled_devices++;
242 return 0;
244 fail_sysfs:
245 cpuidle_remove_state_sysfs(dev);
247 return ret;
250 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
253 * cpuidle_disable_device - disables idle PM for a CPU
254 * @dev: the CPU
256 * This function must be called between cpuidle_pause_and_lock and
257 * cpuidle_resume_and_unlock when used externally.
259 void cpuidle_disable_device(struct cpuidle_device *dev)
261 if (!dev->enabled)
262 return;
263 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
264 return;
266 dev->enabled = 0;
268 if (cpuidle_curr_governor->disable)
269 cpuidle_curr_governor->disable(dev);
271 cpuidle_remove_state_sysfs(dev);
272 enabled_devices--;
275 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
278 * __cpuidle_register_device - internal register function called before register
279 * and enable routines
280 * @dev: the cpu
282 * cpuidle_lock mutex must be held before this is called
284 static int __cpuidle_register_device(struct cpuidle_device *dev)
286 int ret;
287 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
288 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
290 if (!sys_dev)
291 return -EINVAL;
292 if (!try_module_get(cpuidle_driver->owner))
293 return -EINVAL;
295 init_completion(&dev->kobj_unregister);
298 * cpuidle driver should set the dev->power_specified bit
299 * before registering the device if the driver provides
300 * power_usage numbers.
302 * For those devices whose ->power_specified is not set,
303 * we fill in power_usage with decreasing values as the
304 * cpuidle code has an implicit assumption that state Cn
305 * uses less power than C(n-1).
307 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
308 * an power value of -1. So we use -2, -3, etc, for other
309 * c-states.
311 if (!dev->power_specified) {
312 int i;
313 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
314 dev->states[i].power_usage = -1 - i;
317 per_cpu(cpuidle_devices, dev->cpu) = dev;
318 list_add(&dev->device_list, &cpuidle_detected_devices);
319 if ((ret = cpuidle_add_sysfs(sys_dev))) {
320 module_put(cpuidle_driver->owner);
321 return ret;
324 dev->registered = 1;
325 return 0;
329 * cpuidle_register_device - registers a CPU's idle PM feature
330 * @dev: the cpu
332 int cpuidle_register_device(struct cpuidle_device *dev)
334 int ret;
336 mutex_lock(&cpuidle_lock);
338 if ((ret = __cpuidle_register_device(dev))) {
339 mutex_unlock(&cpuidle_lock);
340 return ret;
343 cpuidle_enable_device(dev);
344 cpuidle_install_idle_handler();
346 mutex_unlock(&cpuidle_lock);
348 return 0;
352 EXPORT_SYMBOL_GPL(cpuidle_register_device);
355 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
356 * @dev: the cpu
358 void cpuidle_unregister_device(struct cpuidle_device *dev)
360 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
361 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
363 if (dev->registered == 0)
364 return;
366 cpuidle_pause_and_lock();
368 cpuidle_disable_device(dev);
370 cpuidle_remove_sysfs(sys_dev);
371 list_del(&dev->device_list);
372 wait_for_completion(&dev->kobj_unregister);
373 per_cpu(cpuidle_devices, dev->cpu) = NULL;
375 cpuidle_resume_and_unlock();
377 module_put(cpuidle_driver->owner);
380 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
382 #ifdef CONFIG_SMP
384 static void smp_callback(void *v)
386 /* we already woke the CPU up, nothing more to do */
390 * This function gets called when a part of the kernel has a new latency
391 * requirement. This means we need to get all processors out of their C-state,
392 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
393 * wakes them all right up.
395 static int cpuidle_latency_notify(struct notifier_block *b,
396 unsigned long l, void *v)
398 smp_call_function(smp_callback, NULL, 1);
399 return NOTIFY_OK;
402 static struct notifier_block cpuidle_latency_notifier = {
403 .notifier_call = cpuidle_latency_notify,
406 static inline void latency_notifier_init(struct notifier_block *n)
408 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
411 #else /* CONFIG_SMP */
413 #define latency_notifier_init(x) do { } while (0)
415 #endif /* CONFIG_SMP */
418 * cpuidle_init - core initializer
420 static int __init cpuidle_init(void)
422 int ret;
424 pm_idle_old = pm_idle;
426 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
427 if (ret)
428 return ret;
430 latency_notifier_init(&cpuidle_latency_notifier);
432 return 0;
435 core_initcall(cpuidle_init);