sched: wakeup preempt when small overlap
[linux-2.6/mini2440.git] / drivers / cpuidle / cpuidle.c
blob5ce07b517c5875def9106c5041402032c3f70fc1
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>
20 #include "cpuidle.h"
22 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
24 DEFINE_MUTEX(cpuidle_lock);
25 LIST_HEAD(cpuidle_detected_devices);
26 static void (*pm_idle_old)(void);
28 static int enabled_devices;
30 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
31 static void cpuidle_kick_cpus(void)
33 cpu_idle_wait();
35 #elif defined(CONFIG_SMP)
36 # error "Arch needs cpu_idle_wait() equivalent here"
37 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
38 static void cpuidle_kick_cpus(void) {}
39 #endif
41 static int __cpuidle_register_device(struct cpuidle_device *dev);
43 /**
44 * cpuidle_idle_call - the main idle loop
46 * NOTE: no locks or semaphores should be used here
48 static void cpuidle_idle_call(void)
50 struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
51 struct cpuidle_state *target_state;
52 int next_state;
54 /* check if the device is ready */
55 if (!dev || !dev->enabled) {
56 if (pm_idle_old)
57 pm_idle_old();
58 else
59 local_irq_enable();
60 return;
63 /* ask the governor for the next state */
64 next_state = cpuidle_curr_governor->select(dev);
65 if (need_resched())
66 return;
67 target_state = &dev->states[next_state];
69 /* enter the state and update stats */
70 dev->last_residency = target_state->enter(dev, target_state);
71 dev->last_state = target_state;
72 target_state->time += (unsigned long long)dev->last_residency;
73 target_state->usage++;
75 /* give the governor an opportunity to reflect on the outcome */
76 if (cpuidle_curr_governor->reflect)
77 cpuidle_curr_governor->reflect(dev);
80 /**
81 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
83 void cpuidle_install_idle_handler(void)
85 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
86 /* Make sure all changes finished before we switch to new idle */
87 smp_wmb();
88 pm_idle = cpuidle_idle_call;
92 /**
93 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
95 void cpuidle_uninstall_idle_handler(void)
97 if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
98 pm_idle = pm_idle_old;
99 cpuidle_kick_cpus();
104 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
106 void cpuidle_pause_and_lock(void)
108 mutex_lock(&cpuidle_lock);
109 cpuidle_uninstall_idle_handler();
112 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
115 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
117 void cpuidle_resume_and_unlock(void)
119 cpuidle_install_idle_handler();
120 mutex_unlock(&cpuidle_lock);
123 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
126 * cpuidle_enable_device - enables idle PM for a CPU
127 * @dev: the CPU
129 * This function must be called between cpuidle_pause_and_lock and
130 * cpuidle_resume_and_unlock when used externally.
132 int cpuidle_enable_device(struct cpuidle_device *dev)
134 int ret, i;
136 if (dev->enabled)
137 return 0;
138 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
139 return -EIO;
140 if (!dev->state_count)
141 return -EINVAL;
143 if (dev->registered == 0) {
144 ret = __cpuidle_register_device(dev);
145 if (ret)
146 return ret;
149 if ((ret = cpuidle_add_state_sysfs(dev)))
150 return ret;
152 if (cpuidle_curr_governor->enable &&
153 (ret = cpuidle_curr_governor->enable(dev)))
154 goto fail_sysfs;
156 for (i = 0; i < dev->state_count; i++) {
157 dev->states[i].usage = 0;
158 dev->states[i].time = 0;
160 dev->last_residency = 0;
161 dev->last_state = NULL;
163 smp_wmb();
165 dev->enabled = 1;
167 enabled_devices++;
168 return 0;
170 fail_sysfs:
171 cpuidle_remove_state_sysfs(dev);
173 return ret;
176 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
179 * cpuidle_disable_device - disables idle PM for a CPU
180 * @dev: the CPU
182 * This function must be called between cpuidle_pause_and_lock and
183 * cpuidle_resume_and_unlock when used externally.
185 void cpuidle_disable_device(struct cpuidle_device *dev)
187 if (!dev->enabled)
188 return;
189 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
190 return;
192 dev->enabled = 0;
194 if (cpuidle_curr_governor->disable)
195 cpuidle_curr_governor->disable(dev);
197 cpuidle_remove_state_sysfs(dev);
198 enabled_devices--;
201 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
203 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
204 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
206 ktime_t t1, t2;
207 s64 diff;
208 int ret;
210 t1 = ktime_get();
211 local_irq_enable();
212 while (!need_resched())
213 cpu_relax();
215 t2 = ktime_get();
216 diff = ktime_to_us(ktime_sub(t2, t1));
217 if (diff > INT_MAX)
218 diff = INT_MAX;
220 ret = (int) diff;
221 return ret;
224 static void poll_idle_init(struct cpuidle_device *dev)
226 struct cpuidle_state *state = &dev->states[0];
228 cpuidle_set_statedata(state, NULL);
230 snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
231 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
232 state->exit_latency = 0;
233 state->target_residency = 0;
234 state->power_usage = -1;
235 state->flags = CPUIDLE_FLAG_POLL;
236 state->enter = poll_idle;
238 #else
239 static void poll_idle_init(struct cpuidle_device *dev) {}
240 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
243 * __cpuidle_register_device - internal register function called before register
244 * and enable routines
245 * @dev: the cpu
247 * cpuidle_lock mutex must be held before this is called
249 static int __cpuidle_register_device(struct cpuidle_device *dev)
251 int ret;
252 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
254 if (!sys_dev)
255 return -EINVAL;
256 if (!try_module_get(cpuidle_curr_driver->owner))
257 return -EINVAL;
259 init_completion(&dev->kobj_unregister);
261 poll_idle_init(dev);
263 per_cpu(cpuidle_devices, dev->cpu) = dev;
264 list_add(&dev->device_list, &cpuidle_detected_devices);
265 if ((ret = cpuidle_add_sysfs(sys_dev))) {
266 module_put(cpuidle_curr_driver->owner);
267 return ret;
270 dev->registered = 1;
271 return 0;
275 * cpuidle_register_device - registers a CPU's idle PM feature
276 * @dev: the cpu
278 int cpuidle_register_device(struct cpuidle_device *dev)
280 int ret;
282 mutex_lock(&cpuidle_lock);
284 if ((ret = __cpuidle_register_device(dev))) {
285 mutex_unlock(&cpuidle_lock);
286 return ret;
289 cpuidle_enable_device(dev);
290 cpuidle_install_idle_handler();
292 mutex_unlock(&cpuidle_lock);
294 return 0;
298 EXPORT_SYMBOL_GPL(cpuidle_register_device);
301 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
302 * @dev: the cpu
304 void cpuidle_unregister_device(struct cpuidle_device *dev)
306 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
308 if (dev->registered == 0)
309 return;
311 cpuidle_pause_and_lock();
313 cpuidle_disable_device(dev);
315 cpuidle_remove_sysfs(sys_dev);
316 list_del(&dev->device_list);
317 wait_for_completion(&dev->kobj_unregister);
318 per_cpu(cpuidle_devices, dev->cpu) = NULL;
320 cpuidle_resume_and_unlock();
322 module_put(cpuidle_curr_driver->owner);
325 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
327 #ifdef CONFIG_SMP
329 static void smp_callback(void *v)
331 /* we already woke the CPU up, nothing more to do */
335 * This function gets called when a part of the kernel has a new latency
336 * requirement. This means we need to get all processors out of their C-state,
337 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
338 * wakes them all right up.
340 static int cpuidle_latency_notify(struct notifier_block *b,
341 unsigned long l, void *v)
343 smp_call_function(smp_callback, NULL, 1);
344 return NOTIFY_OK;
347 static struct notifier_block cpuidle_latency_notifier = {
348 .notifier_call = cpuidle_latency_notify,
351 static inline void latency_notifier_init(struct notifier_block *n)
353 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
356 #else /* CONFIG_SMP */
358 #define latency_notifier_init(x) do { } while (0)
360 #endif /* CONFIG_SMP */
363 * cpuidle_init - core initializer
365 static int __init cpuidle_init(void)
367 int ret;
369 pm_idle_old = pm_idle;
371 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
372 if (ret)
373 return ret;
375 latency_notifier_init(&cpuidle_latency_notifier);
377 return 0;
380 core_initcall(cpuidle_init);