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_params.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/ktime.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;
31 * cpuidle_idle_call - the main idle loop
33 * NOTE: no locks or semaphores should be used here
35 static void cpuidle_idle_call(void)
37 struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
38 struct cpuidle_state *target_state;
41 /* check if the device is ready */
42 if (!dev || !dev->enabled) {
50 /* ask the governor for the next state */
51 next_state = cpuidle_curr_governor->select(dev);
54 target_state = &dev->states[next_state];
56 /* enter the state and update stats */
57 dev->last_residency = target_state->enter(dev, target_state);
58 dev->last_state = target_state;
59 target_state->time += dev->last_residency;
60 target_state->usage++;
62 /* give the governor an opportunity to reflect on the outcome */
63 if (cpuidle_curr_governor->reflect)
64 cpuidle_curr_governor->reflect(dev);
68 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
70 void cpuidle_install_idle_handler(void)
72 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
73 /* Make sure all changes finished before we switch to new idle */
75 pm_idle = cpuidle_idle_call;
80 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
82 void cpuidle_uninstall_idle_handler(void)
84 if (enabled_devices && (pm_idle != pm_idle_old)) {
85 pm_idle = pm_idle_old;
91 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
93 void cpuidle_pause_and_lock(void)
95 mutex_lock(&cpuidle_lock);
96 cpuidle_uninstall_idle_handler();
99 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
102 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
104 void cpuidle_resume_and_unlock(void)
106 cpuidle_install_idle_handler();
107 mutex_unlock(&cpuidle_lock);
110 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
113 * cpuidle_enable_device - enables idle PM for a CPU
116 * This function must be called between cpuidle_pause_and_lock and
117 * cpuidle_resume_and_unlock when used externally.
119 int cpuidle_enable_device(struct cpuidle_device *dev)
125 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
127 if (!dev->state_count)
130 if ((ret = cpuidle_add_state_sysfs(dev)))
133 if (cpuidle_curr_governor->enable &&
134 (ret = cpuidle_curr_governor->enable(dev)))
137 for (i = 0; i < dev->state_count; i++) {
138 dev->states[i].usage = 0;
139 dev->states[i].time = 0;
141 dev->last_residency = 0;
142 dev->last_state = NULL;
152 cpuidle_remove_state_sysfs(dev);
157 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
160 * cpuidle_disable_device - disables idle PM for a CPU
163 * This function must be called between cpuidle_pause_and_lock and
164 * cpuidle_resume_and_unlock when used externally.
166 void cpuidle_disable_device(struct cpuidle_device *dev)
170 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
175 if (cpuidle_curr_governor->disable)
176 cpuidle_curr_governor->disable(dev);
178 cpuidle_remove_state_sysfs(dev);
182 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
184 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
185 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
193 while (!need_resched())
197 diff = ktime_to_us(ktime_sub(t2, t1));
205 static void poll_idle_init(struct cpuidle_device *dev)
207 struct cpuidle_state *state = &dev->states[0];
209 cpuidle_set_statedata(state, NULL);
211 snprintf(state->name, CPUIDLE_NAME_LEN, "C0 (poll idle)");
212 state->exit_latency = 0;
213 state->target_residency = 0;
214 state->power_usage = -1;
215 state->flags = CPUIDLE_FLAG_POLL | CPUIDLE_FLAG_TIME_VALID;
216 state->enter = poll_idle;
219 static void poll_idle_init(struct cpuidle_device *dev) {}
220 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
223 * cpuidle_register_device - registers a CPU's idle PM feature
226 int cpuidle_register_device(struct cpuidle_device *dev)
229 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
233 if (!try_module_get(cpuidle_curr_driver->owner))
236 init_completion(&dev->kobj_unregister);
238 mutex_lock(&cpuidle_lock);
242 per_cpu(cpuidle_devices, dev->cpu) = dev;
243 list_add(&dev->device_list, &cpuidle_detected_devices);
244 if ((ret = cpuidle_add_sysfs(sys_dev))) {
245 mutex_unlock(&cpuidle_lock);
246 module_put(cpuidle_curr_driver->owner);
250 cpuidle_enable_device(dev);
251 cpuidle_install_idle_handler();
253 mutex_unlock(&cpuidle_lock);
259 EXPORT_SYMBOL_GPL(cpuidle_register_device);
262 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
265 void cpuidle_unregister_device(struct cpuidle_device *dev)
267 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
269 cpuidle_pause_and_lock();
271 cpuidle_disable_device(dev);
273 cpuidle_remove_sysfs(sys_dev);
274 list_del(&dev->device_list);
275 wait_for_completion(&dev->kobj_unregister);
276 per_cpu(cpuidle_devices, dev->cpu) = NULL;
278 cpuidle_resume_and_unlock();
280 module_put(cpuidle_curr_driver->owner);
283 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
287 static void smp_callback(void *v)
289 /* we already woke the CPU up, nothing more to do */
293 * This function gets called when a part of the kernel has a new latency
294 * requirement. This means we need to get all processors out of their C-state,
295 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
296 * wakes them all right up.
298 static int cpuidle_latency_notify(struct notifier_block *b,
299 unsigned long l, void *v)
301 smp_call_function(smp_callback, NULL, 0, 1);
305 static struct notifier_block cpuidle_latency_notifier = {
306 .notifier_call = cpuidle_latency_notify,
309 static inline void latency_notifier_init(struct notifier_block *n)
311 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
314 #else /* CONFIG_SMP */
316 #define latency_notifier_init(x) do { } while (0)
318 #endif /* CONFIG_SMP */
321 * cpuidle_init - core initializer
323 static int __init cpuidle_init(void)
327 pm_idle_old = pm_idle;
329 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
333 latency_notifier_init(&cpuidle_latency_notifier);
338 core_initcall(cpuidle_init);