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
);
23 DEFINE_MUTEX(cpuidle_lock
);
24 LIST_HEAD(cpuidle_detected_devices
);
25 static void (*pm_idle_old
)(void);
27 static int enabled_devices
;
30 * cpuidle_idle_call - the main idle loop
32 * NOTE: no locks or semaphores should be used here
34 static void cpuidle_idle_call(void)
36 struct cpuidle_device
*dev
= __get_cpu_var(cpuidle_devices
);
37 struct cpuidle_state
*target_state
;
40 /* check if the device is ready */
41 if (!dev
|| !dev
->enabled
) {
49 /* ask the governor for the next state */
50 next_state
= cpuidle_curr_governor
->select(dev
);
53 target_state
= &dev
->states
[next_state
];
55 /* enter the state and update stats */
56 dev
->last_residency
= target_state
->enter(dev
, target_state
);
57 dev
->last_state
= target_state
;
58 target_state
->time
+= dev
->last_residency
;
59 target_state
->usage
++;
61 /* give the governor an opportunity to reflect on the outcome */
62 if (cpuidle_curr_governor
->reflect
)
63 cpuidle_curr_governor
->reflect(dev
);
67 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
69 void cpuidle_install_idle_handler(void)
71 if (enabled_devices
&& (pm_idle
!= cpuidle_idle_call
)) {
72 /* Make sure all changes finished before we switch to new idle */
74 pm_idle
= cpuidle_idle_call
;
79 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
81 void cpuidle_uninstall_idle_handler(void)
83 if (enabled_devices
&& (pm_idle
!= pm_idle_old
)) {
84 pm_idle
= pm_idle_old
;
90 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
92 void cpuidle_pause_and_lock(void)
94 mutex_lock(&cpuidle_lock
);
95 cpuidle_uninstall_idle_handler();
98 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
101 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
103 void cpuidle_resume_and_unlock(void)
105 cpuidle_install_idle_handler();
106 mutex_unlock(&cpuidle_lock
);
109 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
112 * cpuidle_enable_device - enables idle PM for a CPU
115 * This function must be called between cpuidle_pause_and_lock and
116 * cpuidle_resume_and_unlock when used externally.
118 int cpuidle_enable_device(struct cpuidle_device
*dev
)
124 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
126 if (!dev
->state_count
)
129 if ((ret
= cpuidle_add_state_sysfs(dev
)))
132 if (cpuidle_curr_governor
->enable
&&
133 (ret
= cpuidle_curr_governor
->enable(dev
)))
136 for (i
= 0; i
< dev
->state_count
; i
++) {
137 dev
->states
[i
].usage
= 0;
138 dev
->states
[i
].time
= 0;
140 dev
->last_residency
= 0;
141 dev
->last_state
= NULL
;
151 cpuidle_remove_state_sysfs(dev
);
156 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
159 * cpuidle_disable_device - disables idle PM for a CPU
162 * This function must be called between cpuidle_pause_and_lock and
163 * cpuidle_resume_and_unlock when used externally.
165 void cpuidle_disable_device(struct cpuidle_device
*dev
)
169 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
174 if (cpuidle_curr_governor
->disable
)
175 cpuidle_curr_governor
->disable(dev
);
177 cpuidle_remove_state_sysfs(dev
);
181 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
184 * cpuidle_register_device - registers a CPU's idle PM feature
187 int cpuidle_register_device(struct cpuidle_device
*dev
)
190 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
194 if (!try_module_get(cpuidle_curr_driver
->owner
))
197 init_completion(&dev
->kobj_unregister
);
199 mutex_lock(&cpuidle_lock
);
201 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
202 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
203 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
204 mutex_unlock(&cpuidle_lock
);
205 module_put(cpuidle_curr_driver
->owner
);
209 cpuidle_enable_device(dev
);
210 cpuidle_install_idle_handler();
212 mutex_unlock(&cpuidle_lock
);
218 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
221 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
224 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
226 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
228 cpuidle_pause_and_lock();
230 cpuidle_disable_device(dev
);
232 cpuidle_remove_sysfs(sys_dev
);
233 list_del(&dev
->device_list
);
234 wait_for_completion(&dev
->kobj_unregister
);
235 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
237 cpuidle_resume_and_unlock();
239 module_put(cpuidle_curr_driver
->owner
);
242 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
246 static void smp_callback(void *v
)
248 /* we already woke the CPU up, nothing more to do */
252 * This function gets called when a part of the kernel has a new latency
253 * requirement. This means we need to get all processors out of their C-state,
254 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
255 * wakes them all right up.
257 static int cpuidle_latency_notify(struct notifier_block
*b
,
258 unsigned long l
, void *v
)
260 smp_call_function(smp_callback
, NULL
, 0, 1);
264 static struct notifier_block cpuidle_latency_notifier
= {
265 .notifier_call
= cpuidle_latency_notify
,
268 #define latency_notifier_init(x) do { register_latency_notifier(x); } while (0)
270 #else /* CONFIG_SMP */
272 #define latency_notifier_init(x) do { } while (0)
274 #endif /* CONFIG_SMP */
277 * cpuidle_init - core initializer
279 static int __init
cpuidle_init(void)
283 pm_idle_old
= pm_idle
;
285 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
289 latency_notifier_init(&cpuidle_latency_notifier
);
294 core_initcall(cpuidle_init
);