Merge branch 'fadt-test' into test
[linux-2.6] / drivers / cpuidle / cpuidle.c
1 /*
2  * cpuidle.c - core cpuidle infrastructure
3  *
4  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5  *               Shaohua Li <shaohua.li@intel.com>
6  *               Adam Belay <abelay@novell.com>
7  *
8  * This code is licenced under the GPL.
9  */
10
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
20 #include "cpuidle.h"
21
22 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
23
24 DEFINE_MUTEX(cpuidle_lock);
25 LIST_HEAD(cpuidle_detected_devices);
26 static void (*pm_idle_old)(void);
27
28 static int enabled_devices;
29
30 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
31 static void cpuidle_kick_cpus(void)
32 {
33         cpu_idle_wait();
34 }
35 #elif defined(CONFIG_SMP)
36 # error "Arch needs cpu_idle_wait() equivalent here"
37 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
38 static void cpuidle_kick_cpus(void) {}
39 #endif
40
41 static int __cpuidle_register_device(struct cpuidle_device *dev);
42
43 /**
44  * cpuidle_idle_call - the main idle loop
45  *
46  * NOTE: no locks or semaphores should be used here
47  */
48 static void cpuidle_idle_call(void)
49 {
50         struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
51         struct cpuidle_state *target_state;
52         int next_state;
53
54         /* check if the device is ready */
55         if (!dev || !dev->enabled) {
56                 if (pm_idle_old)
57                         pm_idle_old();
58                 else
59 #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
60                         default_idle();
61 #else
62                         local_irq_enable();
63 #endif
64                 return;
65         }
66
67         /* ask the governor for the next state */
68         next_state = cpuidle_curr_governor->select(dev);
69         if (need_resched())
70                 return;
71         target_state = &dev->states[next_state];
72
73         /* enter the state and update stats */
74         dev->last_state = target_state;
75         dev->last_residency = target_state->enter(dev, target_state);
76         if (dev->last_state)
77                 target_state = dev->last_state;
78
79         target_state->time += (unsigned long long)dev->last_residency;
80         target_state->usage++;
81
82         /* give the governor an opportunity to reflect on the outcome */
83         if (cpuidle_curr_governor->reflect)
84                 cpuidle_curr_governor->reflect(dev);
85 }
86
87 /**
88  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
89  */
90 void cpuidle_install_idle_handler(void)
91 {
92         if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
93                 /* Make sure all changes finished before we switch to new idle */
94                 smp_wmb();
95                 pm_idle = cpuidle_idle_call;
96         }
97 }
98
99 /**
100  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
101  */
102 void cpuidle_uninstall_idle_handler(void)
103 {
104         if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
105                 pm_idle = pm_idle_old;
106                 cpuidle_kick_cpus();
107         }
108 }
109
110 /**
111  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
112  */
113 void cpuidle_pause_and_lock(void)
114 {
115         mutex_lock(&cpuidle_lock);
116         cpuidle_uninstall_idle_handler();
117 }
118
119 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
120
121 /**
122  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
123  */
124 void cpuidle_resume_and_unlock(void)
125 {
126         cpuidle_install_idle_handler();
127         mutex_unlock(&cpuidle_lock);
128 }
129
130 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
131
132 /**
133  * cpuidle_enable_device - enables idle PM for a CPU
134  * @dev: the CPU
135  *
136  * This function must be called between cpuidle_pause_and_lock and
137  * cpuidle_resume_and_unlock when used externally.
138  */
139 int cpuidle_enable_device(struct cpuidle_device *dev)
140 {
141         int ret, i;
142
143         if (dev->enabled)
144                 return 0;
145         if (!cpuidle_curr_driver || !cpuidle_curr_governor)
146                 return -EIO;
147         if (!dev->state_count)
148                 return -EINVAL;
149
150         if (dev->registered == 0) {
151                 ret = __cpuidle_register_device(dev);
152                 if (ret)
153                         return ret;
154         }
155
156         if ((ret = cpuidle_add_state_sysfs(dev)))
157                 return ret;
158
159         if (cpuidle_curr_governor->enable &&
160             (ret = cpuidle_curr_governor->enable(dev)))
161                 goto fail_sysfs;
162
163         for (i = 0; i < dev->state_count; i++) {
164                 dev->states[i].usage = 0;
165                 dev->states[i].time = 0;
166         }
167         dev->last_residency = 0;
168         dev->last_state = NULL;
169
170         smp_wmb();
171
172         dev->enabled = 1;
173
174         enabled_devices++;
175         return 0;
176
177 fail_sysfs:
178         cpuidle_remove_state_sysfs(dev);
179
180         return ret;
181 }
182
183 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
184
185 /**
186  * cpuidle_disable_device - disables idle PM for a CPU
187  * @dev: the CPU
188  *
189  * This function must be called between cpuidle_pause_and_lock and
190  * cpuidle_resume_and_unlock when used externally.
191  */
192 void cpuidle_disable_device(struct cpuidle_device *dev)
193 {
194         if (!dev->enabled)
195                 return;
196         if (!cpuidle_curr_driver || !cpuidle_curr_governor)
197                 return;
198
199         dev->enabled = 0;
200
201         if (cpuidle_curr_governor->disable)
202                 cpuidle_curr_governor->disable(dev);
203
204         cpuidle_remove_state_sysfs(dev);
205         enabled_devices--;
206 }
207
208 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
209
210 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
211 static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
212 {
213         ktime_t t1, t2;
214         s64 diff;
215         int ret;
216
217         t1 = ktime_get();
218         local_irq_enable();
219         while (!need_resched())
220                 cpu_relax();
221
222         t2 = ktime_get();
223         diff = ktime_to_us(ktime_sub(t2, t1));
224         if (diff > INT_MAX)
225                 diff = INT_MAX;
226
227         ret = (int) diff;
228         return ret;
229 }
230
231 static void poll_idle_init(struct cpuidle_device *dev)
232 {
233         struct cpuidle_state *state = &dev->states[0];
234
235         cpuidle_set_statedata(state, NULL);
236
237         snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
238         snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
239         state->exit_latency = 0;
240         state->target_residency = 0;
241         state->power_usage = -1;
242         state->flags = CPUIDLE_FLAG_POLL;
243         state->enter = poll_idle;
244 }
245 #else
246 static void poll_idle_init(struct cpuidle_device *dev) {}
247 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
248
249 /**
250  * __cpuidle_register_device - internal register function called before register
251  * and enable routines
252  * @dev: the cpu
253  *
254  * cpuidle_lock mutex must be held before this is called
255  */
256 static int __cpuidle_register_device(struct cpuidle_device *dev)
257 {
258         int ret;
259         struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
260
261         if (!sys_dev)
262                 return -EINVAL;
263         if (!try_module_get(cpuidle_curr_driver->owner))
264                 return -EINVAL;
265
266         init_completion(&dev->kobj_unregister);
267
268         poll_idle_init(dev);
269
270         per_cpu(cpuidle_devices, dev->cpu) = dev;
271         list_add(&dev->device_list, &cpuidle_detected_devices);
272         if ((ret = cpuidle_add_sysfs(sys_dev))) {
273                 module_put(cpuidle_curr_driver->owner);
274                 return ret;
275         }
276
277         dev->registered = 1;
278         return 0;
279 }
280
281 /**
282  * cpuidle_register_device - registers a CPU's idle PM feature
283  * @dev: the cpu
284  */
285 int cpuidle_register_device(struct cpuidle_device *dev)
286 {
287         int ret;
288
289         mutex_lock(&cpuidle_lock);
290
291         if ((ret = __cpuidle_register_device(dev))) {
292                 mutex_unlock(&cpuidle_lock);
293                 return ret;
294         }
295
296         cpuidle_enable_device(dev);
297         cpuidle_install_idle_handler();
298
299         mutex_unlock(&cpuidle_lock);
300
301         return 0;
302
303 }
304
305 EXPORT_SYMBOL_GPL(cpuidle_register_device);
306
307 /**
308  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
309  * @dev: the cpu
310  */
311 void cpuidle_unregister_device(struct cpuidle_device *dev)
312 {
313         struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
314
315         if (dev->registered == 0)
316                 return;
317
318         cpuidle_pause_and_lock();
319
320         cpuidle_disable_device(dev);
321
322         cpuidle_remove_sysfs(sys_dev);
323         list_del(&dev->device_list);
324         wait_for_completion(&dev->kobj_unregister);
325         per_cpu(cpuidle_devices, dev->cpu) = NULL;
326
327         cpuidle_resume_and_unlock();
328
329         module_put(cpuidle_curr_driver->owner);
330 }
331
332 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
333
334 #ifdef CONFIG_SMP
335
336 static void smp_callback(void *v)
337 {
338         /* we already woke the CPU up, nothing more to do */
339 }
340
341 /*
342  * This function gets called when a part of the kernel has a new latency
343  * requirement.  This means we need to get all processors out of their C-state,
344  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
345  * wakes them all right up.
346  */
347 static int cpuidle_latency_notify(struct notifier_block *b,
348                 unsigned long l, void *v)
349 {
350         smp_call_function(smp_callback, NULL, 1);
351         return NOTIFY_OK;
352 }
353
354 static struct notifier_block cpuidle_latency_notifier = {
355         .notifier_call = cpuidle_latency_notify,
356 };
357
358 static inline void latency_notifier_init(struct notifier_block *n)
359 {
360         pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
361 }
362
363 #else /* CONFIG_SMP */
364
365 #define latency_notifier_init(x) do { } while (0)
366
367 #endif /* CONFIG_SMP */
368
369 /**
370  * cpuidle_init - core initializer
371  */
372 static int __init cpuidle_init(void)
373 {
374         int ret;
375
376         pm_idle_old = pm_idle;
377
378         ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
379         if (ret)
380                 return ret;
381
382         latency_notifier_init(&cpuidle_latency_notifier);
383
384         return 0;
385 }
386
387 core_initcall(cpuidle_init);