blob: d048a910b4072cbb3d1416bbca5a6931b4c7a6bd [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>
Jean Pihete8db0be2011-08-25 15:35:03 +020015#include <linux/pm_qos.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>
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070019#include <linux/hrtimer.h>
Paul Gortmaker884b17e2011-08-29 17:52:39 -040020#include <linux/module.h>
Arjan van de Ven288f0232009-09-19 13:35:33 +020021#include <trace/events/power.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040022
23#include "cpuidle.h"
24
25DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Daniel Lezcano72f25652013-04-23 08:54:33 +000026DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -040027
28DEFINE_MUTEX(cpuidle_lock);
29LIST_HEAD(cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040030
31static int enabled_devices;
Len Brown62027ae2011-04-01 18:13:10 -040032static int off __read_mostly;
Len Browna0bfa132011-04-01 19:34:59 -040033static int initialized __read_mostly;
Len Brown62027ae2011-04-01 18:13:10 -040034
35int cpuidle_disabled(void)
36{
37 return off;
38}
Len Brownd91ee582011-04-01 18:28:35 -040039void disable_cpuidle(void)
40{
41 off = 1;
42}
Len Brown4f86d3a2007-10-03 18:58:00 -040043
Venki Pallipadia6869cc2008-02-08 17:05:44 -080044#if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
45static void cpuidle_kick_cpus(void)
46{
47 cpu_idle_wait();
48}
49#elif defined(CONFIG_SMP)
50# error "Arch needs cpu_idle_wait() equivalent here"
51#else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
52static void cpuidle_kick_cpus(void) {}
53#endif
54
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040055static int __cpuidle_register_device(struct cpuidle_device *dev);
56
Robert Leee1689792012-03-20 15:22:42 -050057static inline int cpuidle_enter(struct cpuidle_device *dev,
58 struct cpuidle_driver *drv, int index)
59{
60 struct cpuidle_state *target_state = &drv->states[index];
61 return target_state->enter(dev, drv, index);
62}
63
64static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
65 struct cpuidle_driver *drv, int index)
66{
67 return cpuidle_wrap_enter(dev, drv, index, cpuidle_enter);
68}
69
70typedef int (*cpuidle_enter_t)(struct cpuidle_device *dev,
71 struct cpuidle_driver *drv, int index);
72
73static cpuidle_enter_t cpuidle_enter_ops;
74
Len Brown4f86d3a2007-10-03 18:58:00 -040075/**
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010076 * cpuidle_play_dead - cpu off-lining
77 *
Toshi Kaniee01e662012-03-31 21:37:02 -060078 * Returns in case of an error or no driver
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010079 */
80int cpuidle_play_dead(void)
81{
82 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
83 struct cpuidle_driver *drv = cpuidle_get_driver();
84 int i, dead_state = -1;
85 int power_usage = -1;
86
Toshi Kaniee01e662012-03-31 21:37:02 -060087 if (!drv)
88 return -ENODEV;
89
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010090 /* Find lowest-power state that supports long-term idle */
91 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
92 struct cpuidle_state *s = &drv->states[i];
93
94 if (s->power_usage < power_usage && s->enter_dead) {
95 power_usage = s->power_usage;
96 dead_state = i;
97 }
98 }
99
100 if (dead_state != -1)
101 return drv->states[dead_state].enter_dead(dev, dead_state);
102
103 return -ENODEV;
104}
105
106/**
Colin Cross3ccae8e2012-05-07 17:57:39 -0700107 * cpuidle_enter_state - enter the state and update stats
108 * @dev: cpuidle device for this cpu
109 * @drv: cpuidle driver for this cpu
110 * @next_state: index into drv->states of the state to enter
111 */
112int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
113 int next_state)
114{
115 int entered_state;
116
117 entered_state = cpuidle_enter_ops(dev, drv, next_state);
118
119 if (entered_state >= 0) {
120 /* Update cpuidle counters */
121 /* This can be moved to within driver enter routine
122 * but that results in multiple copies of same code.
123 */
124 dev->states_usage[entered_state].time +=
125 (unsigned long long)dev->last_residency;
126 dev->states_usage[entered_state].usage++;
127 } else {
128 dev->last_residency = 0;
129 }
130
131 return entered_state;
132}
133
134/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400135 * cpuidle_idle_call - the main idle loop
136 *
137 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -0400138 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -0400139 */
Len Browna0bfa132011-04-01 19:34:59 -0400140int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -0400141{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600142 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530143 struct cpuidle_driver *drv = cpuidle_get_driver();
Deepthi Dharware978aa72011-10-28 16:20:09 +0530144 int next_state, entered_state;
Len Brown4f86d3a2007-10-03 18:58:00 -0400145
Len Browna0bfa132011-04-01 19:34:59 -0400146 if (off)
147 return -ENODEV;
148
149 if (!initialized)
150 return -ENODEV;
151
Len Brown4f86d3a2007-10-03 18:58:00 -0400152 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -0400153 if (!dev || !dev->enabled)
154 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -0400155
Arjan van de Ven9a655832008-11-09 12:45:10 -0800156#if 0
157 /* shows regressions, re-enable for 2.6.29 */
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -0700158 /*
159 * run any timers that can be run now, at this point
160 * before calculating the idle duration etc.
161 */
162 hrtimer_peek_ahead_timers();
Arjan van de Ven9a655832008-11-09 12:45:10 -0800163#endif
Ai Li71abbbf2010-08-09 17:20:13 -0700164
Len Brown4f86d3a2007-10-03 18:58:00 -0400165 /* ask the governor for the next state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530166 next_state = cpuidle_curr_governor->select(drv, dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700167 if (need_resched()) {
168 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400169 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700170 }
171
Steven Rostedt76027ea2012-02-07 09:46:01 -0500172 trace_power_start_rcuidle(POWER_CSTATE, next_state, dev->cpu);
173 trace_cpu_idle_rcuidle(next_state, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100174
Colin Cross546d40c2012-05-07 17:57:41 -0700175 if (cpuidle_state_is_coupled(dev, drv, next_state))
176 entered_state = cpuidle_enter_state_coupled(dev, drv,
177 next_state);
178 else
179 entered_state = cpuidle_enter_state(dev, drv, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100180
Steven Rostedt76027ea2012-02-07 09:46:01 -0500181 trace_power_end_rcuidle(dev->cpu);
182 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100183
Len Brown4f86d3a2007-10-03 18:58:00 -0400184 /* give the governor an opportunity to reflect on the outcome */
185 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530186 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400187
188 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400189}
190
191/**
192 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
193 */
194void cpuidle_install_idle_handler(void)
195{
Len Browna0bfa132011-04-01 19:34:59 -0400196 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400197 /* Make sure all changes finished before we switch to new idle */
198 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400199 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400200 }
201}
202
203/**
204 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
205 */
206void cpuidle_uninstall_idle_handler(void)
207{
Len Browna0bfa132011-04-01 19:34:59 -0400208 if (enabled_devices) {
209 initialized = 0;
Venki Pallipadia6869cc2008-02-08 17:05:44 -0800210 cpuidle_kick_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400211 }
212}
213
214/**
215 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
216 */
217void cpuidle_pause_and_lock(void)
218{
219 mutex_lock(&cpuidle_lock);
220 cpuidle_uninstall_idle_handler();
221}
222
223EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
224
225/**
226 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
227 */
228void cpuidle_resume_and_unlock(void)
229{
230 cpuidle_install_idle_handler();
231 mutex_unlock(&cpuidle_lock);
232}
233
234EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
235
Robert Leee1689792012-03-20 15:22:42 -0500236/**
237 * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
238 * @dev: pointer to a valid cpuidle_device object
239 * @drv: pointer to a valid cpuidle_driver object
240 * @index: index of the target cpuidle state.
241 */
242int cpuidle_wrap_enter(struct cpuidle_device *dev,
243 struct cpuidle_driver *drv, int index,
244 int (*enter)(struct cpuidle_device *dev,
245 struct cpuidle_driver *drv, int index))
246{
247 ktime_t time_start, time_end;
248 s64 diff;
249
250 time_start = ktime_get();
251
252 index = enter(dev, drv, index);
253
254 time_end = ktime_get();
255
256 local_irq_enable();
257
258 diff = ktime_to_us(ktime_sub(time_end, time_start));
259 if (diff > INT_MAX)
260 diff = INT_MAX;
261
262 dev->last_residency = (int) diff;
263
264 return index;
265}
266
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100267#ifdef CONFIG_ARCH_HAS_CPU_RELAX
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530268static int poll_idle(struct cpuidle_device *dev,
269 struct cpuidle_driver *drv, int index)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100270{
271 ktime_t t1, t2;
272 s64 diff;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100273
274 t1 = ktime_get();
275 local_irq_enable();
276 while (!need_resched())
277 cpu_relax();
278
279 t2 = ktime_get();
280 diff = ktime_to_us(ktime_sub(t2, t1));
281 if (diff > INT_MAX)
282 diff = INT_MAX;
283
Deepthi Dharware978aa72011-10-28 16:20:09 +0530284 dev->last_residency = (int) diff;
285
286 return index;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100287}
288
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530289static void poll_idle_init(struct cpuidle_driver *drv)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100290{
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530291 struct cpuidle_state *state = &drv->states[0];
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100292
Thomas Renninger720f1c32011-01-07 11:29:43 +0100293 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100294 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
295 state->exit_latency = 0;
296 state->target_residency = 0;
297 state->power_usage = -1;
Len Brownd2476322011-01-12 02:34:59 -0500298 state->flags = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100299 state->enter = poll_idle;
ShuoX Liu3a533962012-03-28 15:19:11 -0700300 state->disable = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100301}
302#else
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530303static void poll_idle_init(struct cpuidle_driver *drv) {}
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100304#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
305
Len Brown4f86d3a2007-10-03 18:58:00 -0400306/**
307 * cpuidle_enable_device - enables idle PM for a CPU
308 * @dev: the CPU
309 *
310 * This function must be called between cpuidle_pause_and_lock and
311 * cpuidle_resume_and_unlock when used externally.
312 */
313int cpuidle_enable_device(struct cpuidle_device *dev)
314{
315 int ret, i;
Robert Leee1689792012-03-20 15:22:42 -0500316 struct cpuidle_driver *drv = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400317
318 if (dev->enabled)
319 return 0;
Robert Leee1689792012-03-20 15:22:42 -0500320 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400321 return -EIO;
322 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200323 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400324
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400325 if (dev->registered == 0) {
326 ret = __cpuidle_register_device(dev);
327 if (ret)
328 return ret;
329 }
330
Robert Leee1689792012-03-20 15:22:42 -0500331 cpuidle_enter_ops = drv->en_core_tk_irqen ?
332 cpuidle_enter_tk : cpuidle_enter;
333
334 poll_idle_init(drv);
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100335
Len Brown4f86d3a2007-10-03 18:58:00 -0400336 if ((ret = cpuidle_add_state_sysfs(dev)))
337 return ret;
338
339 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500340 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400341 goto fail_sysfs;
342
343 for (i = 0; i < dev->state_count; i++) {
Deepthi Dharwar42027352011-10-28 16:20:33 +0530344 dev->states_usage[i].usage = 0;
345 dev->states_usage[i].time = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400346 }
347 dev->last_residency = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400348
349 smp_wmb();
350
351 dev->enabled = 1;
352
353 enabled_devices++;
354 return 0;
355
356fail_sysfs:
357 cpuidle_remove_state_sysfs(dev);
358
359 return ret;
360}
361
362EXPORT_SYMBOL_GPL(cpuidle_enable_device);
363
364/**
365 * cpuidle_disable_device - disables idle PM for a CPU
366 * @dev: the CPU
367 *
368 * This function must be called between cpuidle_pause_and_lock and
369 * cpuidle_resume_and_unlock when used externally.
370 */
371void cpuidle_disable_device(struct cpuidle_device *dev)
372{
373 if (!dev->enabled)
374 return;
Len Brown752138d2010-05-22 16:57:26 -0400375 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400376 return;
377
378 dev->enabled = 0;
379
380 if (cpuidle_curr_governor->disable)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530381 cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400382
383 cpuidle_remove_state_sysfs(dev);
384 enabled_devices--;
385}
386
387EXPORT_SYMBOL_GPL(cpuidle_disable_device);
388
389/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400390 * __cpuidle_register_device - internal register function called before register
391 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400392 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400393 *
394 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400395 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400396static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400397{
398 int ret;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800399 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400400 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400401
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800402 if (!dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400403 return -EINVAL;
Len Brown752138d2010-05-22 16:57:26 -0400404 if (!try_module_get(cpuidle_driver->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400405 return -EINVAL;
406
407 init_completion(&dev->kobj_unregister);
408
Len Brown4f86d3a2007-10-03 18:58:00 -0400409 per_cpu(cpuidle_devices, dev->cpu) = dev;
410 list_add(&dev->device_list, &cpuidle_detected_devices);
Colin Cross69003bf2012-05-07 17:57:40 -0700411 ret = cpuidle_add_sysfs(cpu_dev);
412 if (ret)
413 goto err_sysfs;
Len Brown4f86d3a2007-10-03 18:58:00 -0400414
Colin Cross546d40c2012-05-07 17:57:41 -0700415 ret = cpuidle_coupled_register_device(dev);
416 if (ret)
417 goto err_coupled;
418
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400419 dev->registered = 1;
420 return 0;
Colin Cross69003bf2012-05-07 17:57:40 -0700421
Colin Cross546d40c2012-05-07 17:57:41 -0700422err_coupled:
423 cpuidle_remove_sysfs(cpu_dev);
424 wait_for_completion(&dev->kobj_unregister);
Colin Cross69003bf2012-05-07 17:57:40 -0700425err_sysfs:
426 list_del(&dev->device_list);
427 per_cpu(cpuidle_devices, dev->cpu) = NULL;
428 module_put(cpuidle_driver->owner);
429 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400430}
431
432/**
433 * cpuidle_register_device - registers a CPU's idle PM feature
434 * @dev: the cpu
435 */
436int cpuidle_register_device(struct cpuidle_device *dev)
437{
438 int ret;
439
440 mutex_lock(&cpuidle_lock);
441
442 if ((ret = __cpuidle_register_device(dev))) {
443 mutex_unlock(&cpuidle_lock);
444 return ret;
445 }
446
Len Brown4f86d3a2007-10-03 18:58:00 -0400447 cpuidle_enable_device(dev);
448 cpuidle_install_idle_handler();
449
450 mutex_unlock(&cpuidle_lock);
451
452 return 0;
453
454}
455
456EXPORT_SYMBOL_GPL(cpuidle_register_device);
457
458/**
459 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
460 * @dev: the cpu
461 */
462void cpuidle_unregister_device(struct cpuidle_device *dev)
463{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800464 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400465 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400466
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400467 if (dev->registered == 0)
468 return;
469
Len Brown4f86d3a2007-10-03 18:58:00 -0400470 cpuidle_pause_and_lock();
471
472 cpuidle_disable_device(dev);
473
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800474 cpuidle_remove_sysfs(cpu_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400475 list_del(&dev->device_list);
476 wait_for_completion(&dev->kobj_unregister);
477 per_cpu(cpuidle_devices, dev->cpu) = NULL;
478
Colin Cross546d40c2012-05-07 17:57:41 -0700479 cpuidle_coupled_unregister_device(dev);
480
Len Brown4f86d3a2007-10-03 18:58:00 -0400481 cpuidle_resume_and_unlock();
482
Len Brown752138d2010-05-22 16:57:26 -0400483 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400484}
485
486EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
487
Daniel Lezcano72f25652013-04-23 08:54:33 +0000488/*
489 * cpuidle_unregister: unregister a driver and the devices. This function
490 * can be used only if the driver has been previously registered through
491 * the cpuidle_register function.
492 *
493 * @drv: a valid pointer to a struct cpuidle_driver
494 */
495void cpuidle_unregister(struct cpuidle_driver *drv)
496{
497 int cpu;
498 struct cpuidle_device *device;
499
500 for_each_possible_cpu(cpu) {
501 device = &per_cpu(cpuidle_dev, cpu);
502 cpuidle_unregister_device(device);
503 }
504
505 cpuidle_unregister_driver(drv);
506}
507EXPORT_SYMBOL_GPL(cpuidle_unregister);
508
509/**
510 * cpuidle_register: registers the driver and the cpu devices with the
511 * coupled_cpus passed as parameter. This function is used for all common
512 * initialization pattern there are in the arch specific drivers. The
513 * devices is globally defined in this file.
514 *
515 * @drv : a valid pointer to a struct cpuidle_driver
516 * @coupled_cpus: a cpumask for the coupled states
517 *
518 * Returns 0 on success, < 0 otherwise
519 */
520int cpuidle_register(struct cpuidle_driver *drv,
521 const struct cpumask *const coupled_cpus)
522{
523 int ret, cpu;
524 struct cpuidle_device *device;
525
526 ret = cpuidle_register_driver(drv);
527 if (ret) {
528 pr_err("failed to register cpuidle driver\n");
529 return ret;
530 }
531
532 for_each_possible_cpu(cpu) {
533 device = &per_cpu(cpuidle_dev, cpu);
534 device->cpu = cpu;
535
536#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
537 /*
538 * On multiplatform for ARM, the coupled idle states could
539 * enabled in the kernel even if the cpuidle driver does not
540 * use it. Note, coupled_cpus is a struct copy.
541 */
542 if (coupled_cpus)
543 device->coupled_cpus = *coupled_cpus;
544#endif
545 ret = cpuidle_register_device(device);
546 if (!ret)
547 continue;
548
549 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
550
551 cpuidle_unregister(drv);
552 break;
553 }
554
555 return ret;
556}
557EXPORT_SYMBOL_GPL(cpuidle_register);
558
Len Brown4f86d3a2007-10-03 18:58:00 -0400559#ifdef CONFIG_SMP
560
561static void smp_callback(void *v)
562{
563 /* we already woke the CPU up, nothing more to do */
564}
565
566/*
567 * This function gets called when a part of the kernel has a new latency
568 * requirement. This means we need to get all processors out of their C-state,
569 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
570 * wakes them all right up.
571 */
572static int cpuidle_latency_notify(struct notifier_block *b,
573 unsigned long l, void *v)
574{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200575 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400576 return NOTIFY_OK;
577}
578
579static struct notifier_block cpuidle_latency_notifier = {
580 .notifier_call = cpuidle_latency_notify,
581};
582
Mark Grossd82b3512008-02-04 22:30:08 -0800583static inline void latency_notifier_init(struct notifier_block *n)
584{
585 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
586}
Len Brown4f86d3a2007-10-03 18:58:00 -0400587
588#else /* CONFIG_SMP */
589
590#define latency_notifier_init(x) do { } while (0)
591
592#endif /* CONFIG_SMP */
593
594/**
595 * cpuidle_init - core initializer
596 */
597static int __init cpuidle_init(void)
598{
599 int ret;
600
Len Brown62027ae2011-04-01 18:13:10 -0400601 if (cpuidle_disabled())
602 return -ENODEV;
603
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800604 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400605 if (ret)
606 return ret;
607
608 latency_notifier_init(&cpuidle_latency_notifier);
609
610 return 0;
611}
612
Len Brown62027ae2011-04-01 18:13:10 -0400613module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400614core_initcall(cpuidle_init);