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