blob: bb6e3b338043e13e8e87bc8fceaad67e8f6a9dcc [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
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>
Mark Grossd82b3512008-02-04 22:30:08 -080015#include <linux/pm_qos_params.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040016#include <linux/cpu.h>
17#include <linux/cpuidle.h>
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -080018#include <linux/ktime.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040019
20#include "cpuidle.h"
21
22DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040023
24DEFINE_MUTEX(cpuidle_lock);
25LIST_HEAD(cpuidle_detected_devices);
26static void (*pm_idle_old)(void);
27
28static int enabled_devices;
29
Venki Pallipadia6869cc2008-02-08 17:05:44 -080030#if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
31static 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 */
38static void cpuidle_kick_cpus(void) {}
39#endif
40
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040041static int __cpuidle_register_device(struct cpuidle_device *dev);
42
Len Brown4f86d3a2007-10-03 18:58:00 -040043/**
44 * cpuidle_idle_call - the main idle loop
45 *
46 * NOTE: no locks or semaphores should be used here
47 */
48static 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
Venkatesh Pallipadi89cedfe2008-10-16 19:00:08 -040059#if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
60 default_idle();
61#else
Len Brown4f86d3a2007-10-03 18:58:00 -040062 local_irq_enable();
Venkatesh Pallipadi89cedfe2008-10-16 19:00:08 -040063#endif
Len Brown4f86d3a2007-10-03 18:58:00 -040064 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 */
Len Brown4f86d3a2007-10-03 18:58:00 -040074 dev->last_state = target_state;
Venkatesh Pallipadi887e3012008-09-29 15:24:27 -070075 dev->last_residency = target_state->enter(dev, target_state);
76 if (dev->last_state)
77 target_state = dev->last_state;
78
Yi Yang8b78cf62008-02-25 08:46:12 +080079 target_state->time += (unsigned long long)dev->last_residency;
Len Brown4f86d3a2007-10-03 18:58:00 -040080 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 */
90void 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 */
102void cpuidle_uninstall_idle_handler(void)
103{
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +0200104 if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400105 pm_idle = pm_idle_old;
Venki Pallipadia6869cc2008-02-08 17:05:44 -0800106 cpuidle_kick_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400107 }
108}
109
110/**
111 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
112 */
113void cpuidle_pause_and_lock(void)
114{
115 mutex_lock(&cpuidle_lock);
116 cpuidle_uninstall_idle_handler();
117}
118
119EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
120
121/**
122 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
123 */
124void cpuidle_resume_and_unlock(void)
125{
126 cpuidle_install_idle_handler();
127 mutex_unlock(&cpuidle_lock);
128}
129
130EXPORT_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 */
139int 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
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400150 if (dev->registered == 0) {
151 ret = __cpuidle_register_device(dev);
152 if (ret)
153 return ret;
154 }
155
Len Brown4f86d3a2007-10-03 18:58:00 -0400156 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
177fail_sysfs:
178 cpuidle_remove_state_sysfs(dev);
179
180 return ret;
181}
182
183EXPORT_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 */
192void 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
208EXPORT_SYMBOL_GPL(cpuidle_disable_device);
209
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800210#ifdef CONFIG_ARCH_HAS_CPU_RELAX
211static 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
231static 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
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800237 snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
238 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800239 state->exit_latency = 0;
240 state->target_residency = 0;
241 state->power_usage = -1;
Venki Pallipadi8e92b662008-02-29 10:24:32 -0800242 state->flags = CPUIDLE_FLAG_POLL;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800243 state->enter = poll_idle;
244}
245#else
246static void poll_idle_init(struct cpuidle_device *dev) {}
247#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
248
Len Brown4f86d3a2007-10-03 18:58:00 -0400249/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400250 * __cpuidle_register_device - internal register function called before register
251 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400252 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400253 *
254 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400255 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400256static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400257{
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
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800268 poll_idle_init(dev);
269
Len Brown4f86d3a2007-10-03 18:58:00 -0400270 per_cpu(cpuidle_devices, dev->cpu) = dev;
271 list_add(&dev->device_list, &cpuidle_detected_devices);
272 if ((ret = cpuidle_add_sysfs(sys_dev))) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400273 module_put(cpuidle_curr_driver->owner);
274 return ret;
275 }
276
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400277 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 */
285int 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
Len Brown4f86d3a2007-10-03 18:58:00 -0400296 cpuidle_enable_device(dev);
297 cpuidle_install_idle_handler();
298
299 mutex_unlock(&cpuidle_lock);
300
301 return 0;
302
303}
304
305EXPORT_SYMBOL_GPL(cpuidle_register_device);
306
307/**
308 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
309 * @dev: the cpu
310 */
311void cpuidle_unregister_device(struct cpuidle_device *dev)
312{
313 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
314
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400315 if (dev->registered == 0)
316 return;
317
Len Brown4f86d3a2007-10-03 18:58:00 -0400318 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
332EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
333
334#ifdef CONFIG_SMP
335
336static 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 */
347static int cpuidle_latency_notify(struct notifier_block *b,
348 unsigned long l, void *v)
349{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200350 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400351 return NOTIFY_OK;
352}
353
354static struct notifier_block cpuidle_latency_notifier = {
355 .notifier_call = cpuidle_latency_notify,
356};
357
Mark Grossd82b3512008-02-04 22:30:08 -0800358static inline void latency_notifier_init(struct notifier_block *n)
359{
360 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
361}
Len Brown4f86d3a2007-10-03 18:58:00 -0400362
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 */
372static 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
387core_initcall(cpuidle_init);