GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / cpuidle / cpuidle.c
blob822f4546f17d26bbc053d6de7c5598f0faff5e2b
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 = __get_cpu_var(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;
71 * Call the device's prepare function before calling the
72 * governor's select function. ->prepare gives the device's
73 * cpuidle driver a chance to update any dynamic information
74 * of its cpuidle states for the current idle period, e.g.
75 * state availability, latencies, residencies, etc.
77 if (dev->prepare)
78 dev->prepare(dev);
80 /* ask the governor for the next state */
81 next_state = cpuidle_curr_governor->select(dev);
82 if (need_resched()) {
83 local_irq_enable();
84 return;
87 target_state = &dev->states[next_state];
89 /* enter the state and update stats */
90 dev->last_state = target_state;
91 dev->last_residency = target_state->enter(dev, target_state);
92 if (dev->last_state)
93 target_state = dev->last_state;
95 target_state->time += (unsigned long long)dev->last_residency;
96 target_state->usage++;
98 /* give the governor an opportunity to reflect on the outcome */
99 if (cpuidle_curr_governor->reflect)
100 cpuidle_curr_governor->reflect(dev);
101 trace_power_end(smp_processor_id());
105 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
107 void cpuidle_install_idle_handler(void)
109 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
110 /* Make sure all changes finished before we switch to new idle */
111 smp_wmb();
112 pm_idle = cpuidle_idle_call;
117 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
119 void cpuidle_uninstall_idle_handler(void)
121 if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
122 pm_idle = pm_idle_old;
123 cpuidle_kick_cpus();
128 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
130 void cpuidle_pause_and_lock(void)
132 mutex_lock(&cpuidle_lock);
133 cpuidle_uninstall_idle_handler();
136 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
139 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
141 void cpuidle_resume_and_unlock(void)
143 cpuidle_install_idle_handler();
144 mutex_unlock(&cpuidle_lock);
147 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
149 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
150 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
152 ktime_t t1, t2;
153 s64 diff;
154 int ret;
156 t1 = ktime_get();
157 local_irq_enable();
158 while (!need_resched())
159 cpu_relax();
161 t2 = ktime_get();
162 diff = ktime_to_us(ktime_sub(t2, t1));
163 if (diff > INT_MAX)
164 diff = INT_MAX;
166 ret = (int) diff;
167 return ret;
170 static void poll_idle_init(struct cpuidle_device *dev)
172 struct cpuidle_state *state = &dev->states[0];
174 cpuidle_set_statedata(state, NULL);
176 snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
177 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
178 state->exit_latency = 0;
179 state->target_residency = 0;
180 state->power_usage = -1;
181 state->flags = CPUIDLE_FLAG_POLL;
182 state->enter = poll_idle;
184 #else
185 static void poll_idle_init(struct cpuidle_device *dev) {}
186 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
189 * cpuidle_enable_device - enables idle PM for a CPU
190 * @dev: the CPU
192 * This function must be called between cpuidle_pause_and_lock and
193 * cpuidle_resume_and_unlock when used externally.
195 int cpuidle_enable_device(struct cpuidle_device *dev)
197 int ret, i;
199 if (dev->enabled)
200 return 0;
201 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
202 return -EIO;
203 if (!dev->state_count)
204 return -EINVAL;
206 if (dev->registered == 0) {
207 ret = __cpuidle_register_device(dev);
208 if (ret)
209 return ret;
212 poll_idle_init(dev);
214 if ((ret = cpuidle_add_state_sysfs(dev)))
215 return ret;
217 if (cpuidle_curr_governor->enable &&
218 (ret = cpuidle_curr_governor->enable(dev)))
219 goto fail_sysfs;
221 for (i = 0; i < dev->state_count; i++) {
222 dev->states[i].usage = 0;
223 dev->states[i].time = 0;
225 dev->last_residency = 0;
226 dev->last_state = NULL;
228 smp_wmb();
230 dev->enabled = 1;
232 enabled_devices++;
233 return 0;
235 fail_sysfs:
236 cpuidle_remove_state_sysfs(dev);
238 return ret;
241 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
244 * cpuidle_disable_device - disables idle PM for a CPU
245 * @dev: the CPU
247 * This function must be called between cpuidle_pause_and_lock and
248 * cpuidle_resume_and_unlock when used externally.
250 void cpuidle_disable_device(struct cpuidle_device *dev)
252 if (!dev->enabled)
253 return;
254 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
255 return;
257 dev->enabled = 0;
259 if (cpuidle_curr_governor->disable)
260 cpuidle_curr_governor->disable(dev);
262 cpuidle_remove_state_sysfs(dev);
263 enabled_devices--;
266 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
269 * __cpuidle_register_device - internal register function called before register
270 * and enable routines
271 * @dev: the cpu
273 * cpuidle_lock mutex must be held before this is called
275 static int __cpuidle_register_device(struct cpuidle_device *dev)
277 int ret;
278 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
279 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
281 if (!sys_dev)
282 return -EINVAL;
283 if (!try_module_get(cpuidle_driver->owner))
284 return -EINVAL;
286 init_completion(&dev->kobj_unregister);
289 * cpuidle driver should set the dev->power_specified bit
290 * before registering the device if the driver provides
291 * power_usage numbers.
293 * For those devices whose ->power_specified is not set,
294 * we fill in power_usage with decreasing values as the
295 * cpuidle code has an implicit assumption that state Cn
296 * uses less power than C(n-1).
298 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
299 * an power value of -1. So we use -2, -3, etc, for other
300 * c-states.
302 if (!dev->power_specified) {
303 int i;
304 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
305 dev->states[i].power_usage = -1 - i;
308 per_cpu(cpuidle_devices, dev->cpu) = dev;
309 list_add(&dev->device_list, &cpuidle_detected_devices);
310 if ((ret = cpuidle_add_sysfs(sys_dev))) {
311 module_put(cpuidle_driver->owner);
312 return ret;
315 dev->registered = 1;
316 return 0;
320 * cpuidle_register_device - registers a CPU's idle PM feature
321 * @dev: the cpu
323 int cpuidle_register_device(struct cpuidle_device *dev)
325 int ret;
327 mutex_lock(&cpuidle_lock);
329 if ((ret = __cpuidle_register_device(dev))) {
330 mutex_unlock(&cpuidle_lock);
331 return ret;
334 cpuidle_enable_device(dev);
335 cpuidle_install_idle_handler();
337 mutex_unlock(&cpuidle_lock);
339 return 0;
343 EXPORT_SYMBOL_GPL(cpuidle_register_device);
346 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
347 * @dev: the cpu
349 void cpuidle_unregister_device(struct cpuidle_device *dev)
351 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
352 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
354 if (dev->registered == 0)
355 return;
357 cpuidle_pause_and_lock();
359 cpuidle_disable_device(dev);
361 cpuidle_remove_sysfs(sys_dev);
362 list_del(&dev->device_list);
363 wait_for_completion(&dev->kobj_unregister);
364 per_cpu(cpuidle_devices, dev->cpu) = NULL;
366 cpuidle_resume_and_unlock();
368 module_put(cpuidle_driver->owner);
371 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
373 #ifdef CONFIG_SMP
375 static void smp_callback(void *v)
377 /* we already woke the CPU up, nothing more to do */
381 * This function gets called when a part of the kernel has a new latency
382 * requirement. This means we need to get all processors out of their C-state,
383 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
384 * wakes them all right up.
386 static int cpuidle_latency_notify(struct notifier_block *b,
387 unsigned long l, void *v)
389 smp_call_function(smp_callback, NULL, 1);
390 return NOTIFY_OK;
393 static struct notifier_block cpuidle_latency_notifier = {
394 .notifier_call = cpuidle_latency_notify,
397 static inline void latency_notifier_init(struct notifier_block *n)
399 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
402 #else /* CONFIG_SMP */
404 #define latency_notifier_init(x) do { } while (0)
406 #endif /* CONFIG_SMP */
409 * cpuidle_init - core initializer
411 static int __init cpuidle_init(void)
413 int ret;
415 pm_idle_old = pm_idle;
417 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
418 if (ret)
419 return ret;
421 latency_notifier_init(&cpuidle_latency_notifier);
423 return 0;
426 core_initcall(cpuidle_init);