udf: move calculating of nr_groups into helper function
[linux-2.6/x86.git] / drivers / cpuidle / cpuidle.c
blob2c4b2d47973e195cbb0422a9b539400a2ad5ef5e
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>
20 #include "cpuidle.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;
30 /**
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;
39 int next_state;
41 /* check if the device is ready */
42 if (!dev || !dev->enabled) {
43 if (pm_idle_old)
44 pm_idle_old();
45 else
46 local_irq_enable();
47 return;
50 /* ask the governor for the next state */
51 next_state = cpuidle_curr_governor->select(dev);
52 if (need_resched())
53 return;
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);
67 /**
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 */
74 smp_wmb();
75 pm_idle = cpuidle_idle_call;
79 /**
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;
86 cpu_idle_wait();
90 /**
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
114 * @dev: the 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)
121 int ret, i;
123 if (dev->enabled)
124 return 0;
125 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
126 return -EIO;
127 if (!dev->state_count)
128 return -EINVAL;
130 if ((ret = cpuidle_add_state_sysfs(dev)))
131 return ret;
133 if (cpuidle_curr_governor->enable &&
134 (ret = cpuidle_curr_governor->enable(dev)))
135 goto fail_sysfs;
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;
144 smp_wmb();
146 dev->enabled = 1;
148 enabled_devices++;
149 return 0;
151 fail_sysfs:
152 cpuidle_remove_state_sysfs(dev);
154 return ret;
157 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
160 * cpuidle_disable_device - disables idle PM for a CPU
161 * @dev: the 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)
168 if (!dev->enabled)
169 return;
170 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
171 return;
173 dev->enabled = 0;
175 if (cpuidle_curr_governor->disable)
176 cpuidle_curr_governor->disable(dev);
178 cpuidle_remove_state_sysfs(dev);
179 enabled_devices--;
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)
187 ktime_t t1, t2;
188 s64 diff;
189 int ret;
191 t1 = ktime_get();
192 local_irq_enable();
193 while (!need_resched())
194 cpu_relax();
196 t2 = ktime_get();
197 diff = ktime_to_us(ktime_sub(t2, t1));
198 if (diff > INT_MAX)
199 diff = INT_MAX;
201 ret = (int) diff;
202 return ret;
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;
218 #else
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
224 * @dev: the cpu
226 int cpuidle_register_device(struct cpuidle_device *dev)
228 int ret;
229 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
231 if (!sys_dev)
232 return -EINVAL;
233 if (!try_module_get(cpuidle_curr_driver->owner))
234 return -EINVAL;
236 init_completion(&dev->kobj_unregister);
238 mutex_lock(&cpuidle_lock);
240 poll_idle_init(dev);
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);
247 return ret;
250 cpuidle_enable_device(dev);
251 cpuidle_install_idle_handler();
253 mutex_unlock(&cpuidle_lock);
255 return 0;
259 EXPORT_SYMBOL_GPL(cpuidle_register_device);
262 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
263 * @dev: the cpu
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);
285 #ifdef CONFIG_SMP
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);
302 return NOTIFY_OK;
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)
325 int ret;
327 pm_idle_old = pm_idle;
329 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
330 if (ret)
331 return ret;
333 latency_notifier_init(&cpuidle_latency_notifier);
335 return 0;
338 core_initcall(cpuidle_init);