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