ALSA: usbaudio Mbox support, output only
[firewire-audio.git] / drivers / cpuidle / cpuidle.c
blob12fdd3987a36c2b09ab65b9d598f276e51fbf5b7
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;
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
77 /* ask the governor for the next state */
78 next_state = cpuidle_curr_governor->select(dev);
79 if (need_resched()) {
80 local_irq_enable();
81 return;
84 target_state = &dev->states[next_state];
86 /* enter the state and update stats */
87 dev->last_state = target_state;
88 dev->last_residency = target_state->enter(dev, target_state);
89 if (dev->last_state)
90 target_state = dev->last_state;
92 target_state->time += (unsigned long long)dev->last_residency;
93 target_state->usage++;
95 /* give the governor an opportunity to reflect on the outcome */
96 if (cpuidle_curr_governor->reflect)
97 cpuidle_curr_governor->reflect(dev);
98 trace_power_end(0);
102 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
104 void cpuidle_install_idle_handler(void)
106 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
107 /* Make sure all changes finished before we switch to new idle */
108 smp_wmb();
109 pm_idle = cpuidle_idle_call;
114 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
116 void cpuidle_uninstall_idle_handler(void)
118 if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
119 pm_idle = pm_idle_old;
120 cpuidle_kick_cpus();
125 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
127 void cpuidle_pause_and_lock(void)
129 mutex_lock(&cpuidle_lock);
130 cpuidle_uninstall_idle_handler();
133 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
136 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
138 void cpuidle_resume_and_unlock(void)
140 cpuidle_install_idle_handler();
141 mutex_unlock(&cpuidle_lock);
144 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
147 * cpuidle_enable_device - enables idle PM for a CPU
148 * @dev: the CPU
150 * This function must be called between cpuidle_pause_and_lock and
151 * cpuidle_resume_and_unlock when used externally.
153 int cpuidle_enable_device(struct cpuidle_device *dev)
155 int ret, i;
157 if (dev->enabled)
158 return 0;
159 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
160 return -EIO;
161 if (!dev->state_count)
162 return -EINVAL;
164 if (dev->registered == 0) {
165 ret = __cpuidle_register_device(dev);
166 if (ret)
167 return ret;
170 if ((ret = cpuidle_add_state_sysfs(dev)))
171 return ret;
173 if (cpuidle_curr_governor->enable &&
174 (ret = cpuidle_curr_governor->enable(dev)))
175 goto fail_sysfs;
177 for (i = 0; i < dev->state_count; i++) {
178 dev->states[i].usage = 0;
179 dev->states[i].time = 0;
181 dev->last_residency = 0;
182 dev->last_state = NULL;
184 smp_wmb();
186 dev->enabled = 1;
188 enabled_devices++;
189 return 0;
191 fail_sysfs:
192 cpuidle_remove_state_sysfs(dev);
194 return ret;
197 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
200 * cpuidle_disable_device - disables idle PM for a CPU
201 * @dev: the CPU
203 * This function must be called between cpuidle_pause_and_lock and
204 * cpuidle_resume_and_unlock when used externally.
206 void cpuidle_disable_device(struct cpuidle_device *dev)
208 if (!dev->enabled)
209 return;
210 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
211 return;
213 dev->enabled = 0;
215 if (cpuidle_curr_governor->disable)
216 cpuidle_curr_governor->disable(dev);
218 cpuidle_remove_state_sysfs(dev);
219 enabled_devices--;
222 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
224 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
225 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
227 ktime_t t1, t2;
228 s64 diff;
229 int ret;
231 t1 = ktime_get();
232 local_irq_enable();
233 while (!need_resched())
234 cpu_relax();
236 t2 = ktime_get();
237 diff = ktime_to_us(ktime_sub(t2, t1));
238 if (diff > INT_MAX)
239 diff = INT_MAX;
241 ret = (int) diff;
242 return ret;
245 static void poll_idle_init(struct cpuidle_device *dev)
247 struct cpuidle_state *state = &dev->states[0];
249 cpuidle_set_statedata(state, NULL);
251 snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
252 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
253 state->exit_latency = 0;
254 state->target_residency = 0;
255 state->power_usage = -1;
256 state->flags = CPUIDLE_FLAG_POLL;
257 state->enter = poll_idle;
259 #else
260 static void poll_idle_init(struct cpuidle_device *dev) {}
261 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
264 * __cpuidle_register_device - internal register function called before register
265 * and enable routines
266 * @dev: the cpu
268 * cpuidle_lock mutex must be held before this is called
270 static int __cpuidle_register_device(struct cpuidle_device *dev)
272 int ret;
273 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
275 if (!sys_dev)
276 return -EINVAL;
277 if (!try_module_get(cpuidle_curr_driver->owner))
278 return -EINVAL;
280 init_completion(&dev->kobj_unregister);
282 poll_idle_init(dev);
284 per_cpu(cpuidle_devices, dev->cpu) = dev;
285 list_add(&dev->device_list, &cpuidle_detected_devices);
286 if ((ret = cpuidle_add_sysfs(sys_dev))) {
287 module_put(cpuidle_curr_driver->owner);
288 return ret;
291 dev->registered = 1;
292 return 0;
296 * cpuidle_register_device - registers a CPU's idle PM feature
297 * @dev: the cpu
299 int cpuidle_register_device(struct cpuidle_device *dev)
301 int ret;
303 mutex_lock(&cpuidle_lock);
305 if ((ret = __cpuidle_register_device(dev))) {
306 mutex_unlock(&cpuidle_lock);
307 return ret;
310 cpuidle_enable_device(dev);
311 cpuidle_install_idle_handler();
313 mutex_unlock(&cpuidle_lock);
315 return 0;
319 EXPORT_SYMBOL_GPL(cpuidle_register_device);
322 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
323 * @dev: the cpu
325 void cpuidle_unregister_device(struct cpuidle_device *dev)
327 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
329 if (dev->registered == 0)
330 return;
332 cpuidle_pause_and_lock();
334 cpuidle_disable_device(dev);
336 cpuidle_remove_sysfs(sys_dev);
337 list_del(&dev->device_list);
338 wait_for_completion(&dev->kobj_unregister);
339 per_cpu(cpuidle_devices, dev->cpu) = NULL;
341 cpuidle_resume_and_unlock();
343 module_put(cpuidle_curr_driver->owner);
346 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
348 #ifdef CONFIG_SMP
350 static void smp_callback(void *v)
352 /* we already woke the CPU up, nothing more to do */
356 * This function gets called when a part of the kernel has a new latency
357 * requirement. This means we need to get all processors out of their C-state,
358 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
359 * wakes them all right up.
361 static int cpuidle_latency_notify(struct notifier_block *b,
362 unsigned long l, void *v)
364 smp_call_function(smp_callback, NULL, 1);
365 return NOTIFY_OK;
368 static struct notifier_block cpuidle_latency_notifier = {
369 .notifier_call = cpuidle_latency_notify,
372 static inline void latency_notifier_init(struct notifier_block *n)
374 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
377 #else /* CONFIG_SMP */
379 #define latency_notifier_init(x) do { } while (0)
381 #endif /* CONFIG_SMP */
384 * cpuidle_init - core initializer
386 static int __init cpuidle_init(void)
388 int ret;
390 pm_idle_old = pm_idle;
392 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
393 if (ret)
394 return ret;
396 latency_notifier_init(&cpuidle_latency_notifier);
398 return 0;
401 core_initcall(cpuidle_init);