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/latency.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
21 DEFINE_PER_CPU(struct cpuidle_device
*, cpuidle_devices
);
22 EXPORT_PER_CPU_SYMBOL_GPL(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
);
185 * cpuidle_register_device - registers a CPU's idle PM feature
188 int cpuidle_register_device(struct cpuidle_device
*dev
)
191 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
195 if (!try_module_get(cpuidle_curr_driver
->owner
))
198 init_completion(&dev
->kobj_unregister
);
200 mutex_lock(&cpuidle_lock
);
202 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
203 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
204 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
205 mutex_unlock(&cpuidle_lock
);
206 module_put(cpuidle_curr_driver
->owner
);
210 cpuidle_enable_device(dev
);
211 cpuidle_install_idle_handler();
213 mutex_unlock(&cpuidle_lock
);
219 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
222 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
225 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
227 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
229 cpuidle_pause_and_lock();
231 cpuidle_disable_device(dev
);
233 cpuidle_remove_sysfs(sys_dev
);
234 list_del(&dev
->device_list
);
235 wait_for_completion(&dev
->kobj_unregister
);
236 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
238 cpuidle_resume_and_unlock();
240 module_put(cpuidle_curr_driver
->owner
);
243 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
247 static void smp_callback(void *v
)
249 /* we already woke the CPU up, nothing more to do */
253 * This function gets called when a part of the kernel has a new latency
254 * requirement. This means we need to get all processors out of their C-state,
255 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
256 * wakes them all right up.
258 static int cpuidle_latency_notify(struct notifier_block
*b
,
259 unsigned long l
, void *v
)
261 smp_call_function(smp_callback
, NULL
, 0, 1);
265 static struct notifier_block cpuidle_latency_notifier
= {
266 .notifier_call
= cpuidle_latency_notify
,
269 #define latency_notifier_init(x) do { register_latency_notifier(x); } while (0)
271 #else /* CONFIG_SMP */
273 #define latency_notifier_init(x) do { } while (0)
275 #endif /* CONFIG_SMP */
278 * cpuidle_init - core initializer
280 static int __init
cpuidle_init(void)
284 pm_idle_old
= pm_idle
;
286 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
290 latency_notifier_init(&cpuidle_latency_notifier
);
295 core_initcall(cpuidle_init
);