blob: 0da795b9dbbf19b083130b8ea776f420ad4f51c5 [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
Daniel Lezcanob60e6a02013-03-21 12:21:31 +000011#include <linux/clockchips.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040012#include <linux/kernel.h>
13#include <linux/mutex.h>
14#include <linux/sched.h>
15#include <linux/notifier.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020016#include <linux/pm_qos.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040017#include <linux/cpu.h>
18#include <linux/cpuidle.h>
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -080019#include <linux/ktime.h>
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070020#include <linux/hrtimer.h>
Paul Gortmaker884b17e2011-08-29 17:52:39 -040021#include <linux/module.h>
Arjan van de Ven288f0232009-09-19 13:35:33 +020022#include <trace/events/power.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040023
24#include "cpuidle.h"
25
26DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
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
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040044static int __cpuidle_register_device(struct cpuidle_device *dev);
45
Len Brown4f86d3a2007-10-03 18:58:00 -040046/**
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010047 * cpuidle_play_dead - cpu off-lining
48 *
Toshi Kaniee01e662012-03-31 21:37:02 -060049 * Returns in case of an error or no driver
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010050 */
51int cpuidle_play_dead(void)
52{
53 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +000054 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010055 int i;
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010056
Toshi Kaniee01e662012-03-31 21:37:02 -060057 if (!drv)
58 return -ENODEV;
59
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010060 /* Find lowest-power state that supports long-term idle */
Daniel Lezcano8aef33a2013-01-15 14:18:04 +010061 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
62 if (drv->states[i].enter_dead)
63 return drv->states[i].enter_dead(dev, i);
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010064
65 return -ENODEV;
66}
67
68/**
Colin Cross56cfbf72012-05-07 17:57:39 -070069 * cpuidle_enter_state - enter the state and update stats
70 * @dev: cpuidle device for this cpu
71 * @drv: cpuidle driver for this cpu
72 * @next_state: index into drv->states of the state to enter
73 */
74int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
Daniel Lezcano554c06b2013-04-23 08:54:31 +000075 int index)
Colin Cross56cfbf72012-05-07 17:57:39 -070076{
77 int entered_state;
78
Daniel Lezcano554c06b2013-04-23 08:54:31 +000079 struct cpuidle_state *target_state = &drv->states[index];
80 ktime_t time_start, time_end;
81 s64 diff;
82
83 time_start = ktime_get();
84
85 entered_state = target_state->enter(dev, drv, index);
86
87 time_end = ktime_get();
88
89 local_irq_enable();
90
91 diff = ktime_to_us(ktime_sub(time_end, time_start));
92 if (diff > INT_MAX)
93 diff = INT_MAX;
94
95 dev->last_residency = (int) diff;
Colin Cross56cfbf72012-05-07 17:57:39 -070096
97 if (entered_state >= 0) {
98 /* Update cpuidle counters */
99 /* This can be moved to within driver enter routine
100 * but that results in multiple copies of same code.
101 */
Julius Wernera474a512012-11-27 14:17:58 +0100102 dev->states_usage[entered_state].time += dev->last_residency;
Colin Cross56cfbf72012-05-07 17:57:39 -0700103 dev->states_usage[entered_state].usage++;
104 } else {
105 dev->last_residency = 0;
106 }
107
108 return entered_state;
109}
110
111/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400112 * cpuidle_idle_call - the main idle loop
113 *
114 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -0400115 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -0400116 */
Len Browna0bfa132011-04-01 19:34:59 -0400117int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -0400118{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600119 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000120 struct cpuidle_driver *drv;
Deepthi Dharware978aa72011-10-28 16:20:09 +0530121 int next_state, entered_state;
Len Brown4f86d3a2007-10-03 18:58:00 -0400122
Len Browna0bfa132011-04-01 19:34:59 -0400123 if (off)
124 return -ENODEV;
125
126 if (!initialized)
127 return -ENODEV;
128
Len Brown4f86d3a2007-10-03 18:58:00 -0400129 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -0400130 if (!dev || !dev->enabled)
131 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -0400132
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000133 drv = cpuidle_get_cpu_driver(dev);
134
Len Brown4f86d3a2007-10-03 18:58:00 -0400135 /* ask the governor for the next state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530136 next_state = cpuidle_curr_governor->select(drv, dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700137 if (need_resched()) {
Youquan Songd73d68d2012-10-26 12:26:59 +0200138 dev->last_residency = 0;
139 /* give the governor an opportunity to reflect on the outcome */
140 if (cpuidle_curr_governor->reflect)
141 cpuidle_curr_governor->reflect(dev, next_state);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700142 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400143 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700144 }
145
Steven Rostedt76027ea2012-02-07 09:46:01 -0500146 trace_cpu_idle_rcuidle(next_state, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100147
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000148 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
149 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
150 &dev->cpu);
151
Colin Cross4126c012012-05-07 17:57:41 -0700152 if (cpuidle_state_is_coupled(dev, drv, next_state))
153 entered_state = cpuidle_enter_state_coupled(dev, drv,
154 next_state);
155 else
156 entered_state = cpuidle_enter_state(dev, drv, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100157
Daniel Lezcanob60e6a02013-03-21 12:21:31 +0000158 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
159 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
160 &dev->cpu);
161
Steven Rostedt76027ea2012-02-07 09:46:01 -0500162 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100163
Len Brown4f86d3a2007-10-03 18:58:00 -0400164 /* give the governor an opportunity to reflect on the outcome */
165 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530166 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400167
168 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400169}
170
171/**
172 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
173 */
174void cpuidle_install_idle_handler(void)
175{
Len Browna0bfa132011-04-01 19:34:59 -0400176 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400177 /* Make sure all changes finished before we switch to new idle */
178 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400179 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400180 }
181}
182
183/**
184 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
185 */
186void cpuidle_uninstall_idle_handler(void)
187{
Len Browna0bfa132011-04-01 19:34:59 -0400188 if (enabled_devices) {
189 initialized = 0;
Thomas Gleixner4a162512012-05-07 17:59:48 +0000190 kick_all_cpus_sync();
Len Brown4f86d3a2007-10-03 18:58:00 -0400191 }
192}
193
194/**
195 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
196 */
197void cpuidle_pause_and_lock(void)
198{
199 mutex_lock(&cpuidle_lock);
200 cpuidle_uninstall_idle_handler();
201}
202
203EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
204
205/**
206 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
207 */
208void cpuidle_resume_and_unlock(void)
209{
210 cpuidle_install_idle_handler();
211 mutex_unlock(&cpuidle_lock);
212}
213
214EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
215
Preeti U Murthy8651f972012-07-09 10:12:56 +0200216/* Currently used in suspend/resume path to suspend cpuidle */
217void cpuidle_pause(void)
218{
219 mutex_lock(&cpuidle_lock);
220 cpuidle_uninstall_idle_handler();
221 mutex_unlock(&cpuidle_lock);
222}
223
224/* Currently used in suspend/resume path to resume cpuidle */
225void cpuidle_resume(void)
226{
227 mutex_lock(&cpuidle_lock);
228 cpuidle_install_idle_handler();
229 mutex_unlock(&cpuidle_lock);
230}
231
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100232#ifdef CONFIG_ARCH_HAS_CPU_RELAX
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530233static int poll_idle(struct cpuidle_device *dev,
234 struct cpuidle_driver *drv, int index)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100235{
236 ktime_t t1, t2;
237 s64 diff;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100238
239 t1 = ktime_get();
240 local_irq_enable();
241 while (!need_resched())
242 cpu_relax();
243
244 t2 = ktime_get();
245 diff = ktime_to_us(ktime_sub(t2, t1));
246 if (diff > INT_MAX)
247 diff = INT_MAX;
248
Deepthi Dharware978aa72011-10-28 16:20:09 +0530249 dev->last_residency = (int) diff;
250
251 return index;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100252}
253
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530254static void poll_idle_init(struct cpuidle_driver *drv)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100255{
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530256 struct cpuidle_state *state = &drv->states[0];
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100257
Thomas Renninger720f1c32011-01-07 11:29:43 +0100258 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100259 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
260 state->exit_latency = 0;
261 state->target_residency = 0;
262 state->power_usage = -1;
Len Brownd2476322011-01-12 02:34:59 -0500263 state->flags = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100264 state->enter = poll_idle;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200265 state->disabled = false;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100266}
267#else
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530268static void poll_idle_init(struct cpuidle_driver *drv) {}
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100269#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
270
Len Brown4f86d3a2007-10-03 18:58:00 -0400271/**
272 * cpuidle_enable_device - enables idle PM for a CPU
273 * @dev: the CPU
274 *
275 * This function must be called between cpuidle_pause_and_lock and
276 * cpuidle_resume_and_unlock when used externally.
277 */
278int cpuidle_enable_device(struct cpuidle_device *dev)
279{
280 int ret, i;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000281 struct cpuidle_driver *drv;
Len Brown4f86d3a2007-10-03 18:58:00 -0400282
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700283 if (!dev)
284 return -EINVAL;
285
Len Brown4f86d3a2007-10-03 18:58:00 -0400286 if (dev->enabled)
287 return 0;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000288
289 drv = cpuidle_get_cpu_driver(dev);
290
Robert Leee1689792012-03-20 15:22:42 -0500291 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400292 return -EIO;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000293
Len Brown4f86d3a2007-10-03 18:58:00 -0400294 if (!dev->state_count)
Daniel Lezcanofc850f32012-03-26 14:51:26 +0200295 dev->state_count = drv->state_count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400296
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400297 if (dev->registered == 0) {
298 ret = __cpuidle_register_device(dev);
299 if (ret)
300 return ret;
301 }
302
Robert Leee1689792012-03-20 15:22:42 -0500303 poll_idle_init(drv);
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100304
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000305 ret = cpuidle_add_device_sysfs(dev);
306 if (ret)
Len Brown4f86d3a2007-10-03 18:58:00 -0400307 return ret;
308
309 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500310 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400311 goto fail_sysfs;
312
313 for (i = 0; i < dev->state_count; i++) {
Deepthi Dharwar42027352011-10-28 16:20:33 +0530314 dev->states_usage[i].usage = 0;
315 dev->states_usage[i].time = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400316 }
317 dev->last_residency = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400318
319 smp_wmb();
320
321 dev->enabled = 1;
322
323 enabled_devices++;
324 return 0;
325
326fail_sysfs:
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000327 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400328
329 return ret;
330}
331
332EXPORT_SYMBOL_GPL(cpuidle_enable_device);
333
334/**
335 * cpuidle_disable_device - disables idle PM for a CPU
336 * @dev: the CPU
337 *
338 * This function must be called between cpuidle_pause_and_lock and
339 * cpuidle_resume_and_unlock when used externally.
340 */
341void cpuidle_disable_device(struct cpuidle_device *dev)
342{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000343 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
344
Srivatsa S. Bhatcf31cd12012-10-08 13:43:08 +0530345 if (!dev || !dev->enabled)
Len Brown4f86d3a2007-10-03 18:58:00 -0400346 return;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000347
348 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400349 return;
350
351 dev->enabled = 0;
352
353 if (cpuidle_curr_governor->disable)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000354 cpuidle_curr_governor->disable(drv, dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400355
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000356 cpuidle_remove_device_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400357 enabled_devices--;
358}
359
360EXPORT_SYMBOL_GPL(cpuidle_disable_device);
361
362/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400363 * __cpuidle_register_device - internal register function called before register
364 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400365 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400366 *
367 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400368 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400369static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400370{
371 int ret;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000372 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400373
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000374 if (!try_module_get(drv->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400375 return -EINVAL;
376
Len Brown4f86d3a2007-10-03 18:58:00 -0400377 per_cpu(cpuidle_devices, dev->cpu) = dev;
378 list_add(&dev->device_list, &cpuidle_detected_devices);
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200379 ret = cpuidle_add_sysfs(dev);
Colin Cross3af272a2012-05-07 17:57:40 -0700380 if (ret)
381 goto err_sysfs;
Len Brown4f86d3a2007-10-03 18:58:00 -0400382
Colin Cross4126c012012-05-07 17:57:41 -0700383 ret = cpuidle_coupled_register_device(dev);
384 if (ret)
385 goto err_coupled;
Len Brown4f86d3a2007-10-03 18:58:00 -0400386
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400387 dev->registered = 1;
388 return 0;
Colin Cross3af272a2012-05-07 17:57:40 -0700389
Colin Cross4126c012012-05-07 17:57:41 -0700390err_coupled:
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200391 cpuidle_remove_sysfs(dev);
Colin Cross3af272a2012-05-07 17:57:40 -0700392err_sysfs:
393 list_del(&dev->device_list);
394 per_cpu(cpuidle_devices, dev->cpu) = NULL;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000395 module_put(drv->owner);
Colin Cross3af272a2012-05-07 17:57:40 -0700396 return ret;
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400397}
398
399/**
400 * cpuidle_register_device - registers a CPU's idle PM feature
401 * @dev: the cpu
402 */
403int cpuidle_register_device(struct cpuidle_device *dev)
404{
405 int ret;
406
Srivatsa S. Bhat1b0a0e92012-05-04 14:06:02 -0700407 if (!dev)
408 return -EINVAL;
409
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400410 mutex_lock(&cpuidle_lock);
411
412 if ((ret = __cpuidle_register_device(dev))) {
413 mutex_unlock(&cpuidle_lock);
414 return ret;
415 }
416
Len Brown4f86d3a2007-10-03 18:58:00 -0400417 cpuidle_enable_device(dev);
418 cpuidle_install_idle_handler();
419
420 mutex_unlock(&cpuidle_lock);
421
422 return 0;
423
424}
425
426EXPORT_SYMBOL_GPL(cpuidle_register_device);
427
428/**
429 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
430 * @dev: the cpu
431 */
432void cpuidle_unregister_device(struct cpuidle_device *dev)
433{
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000434 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400435
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400436 if (dev->registered == 0)
437 return;
438
Len Brown4f86d3a2007-10-03 18:58:00 -0400439 cpuidle_pause_and_lock();
440
441 cpuidle_disable_device(dev);
442
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200443 cpuidle_remove_sysfs(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400444 list_del(&dev->device_list);
Len Brown4f86d3a2007-10-03 18:58:00 -0400445 per_cpu(cpuidle_devices, dev->cpu) = NULL;
446
Colin Cross4126c012012-05-07 17:57:41 -0700447 cpuidle_coupled_unregister_device(dev);
448
Len Brown4f86d3a2007-10-03 18:58:00 -0400449 cpuidle_resume_and_unlock();
450
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000451 module_put(drv->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400452}
453
454EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
455
456#ifdef CONFIG_SMP
457
458static void smp_callback(void *v)
459{
460 /* we already woke the CPU up, nothing more to do */
461}
462
463/*
464 * This function gets called when a part of the kernel has a new latency
465 * requirement. This means we need to get all processors out of their C-state,
466 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
467 * wakes them all right up.
468 */
469static int cpuidle_latency_notify(struct notifier_block *b,
470 unsigned long l, void *v)
471{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200472 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400473 return NOTIFY_OK;
474}
475
476static struct notifier_block cpuidle_latency_notifier = {
477 .notifier_call = cpuidle_latency_notify,
478};
479
Mark Grossd82b3512008-02-04 22:30:08 -0800480static inline void latency_notifier_init(struct notifier_block *n)
481{
482 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
483}
Len Brown4f86d3a2007-10-03 18:58:00 -0400484
485#else /* CONFIG_SMP */
486
487#define latency_notifier_init(x) do { } while (0)
488
489#endif /* CONFIG_SMP */
490
491/**
492 * cpuidle_init - core initializer
493 */
494static int __init cpuidle_init(void)
495{
496 int ret;
497
Len Brown62027ae2011-04-01 18:13:10 -0400498 if (cpuidle_disabled())
499 return -ENODEV;
500
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800501 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400502 if (ret)
503 return ret;
504
505 latency_notifier_init(&cpuidle_latency_notifier);
506
507 return 0;
508}
509
Len Brown62027ae2011-04-01 18:13:10 -0400510module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400511core_initcall(cpuidle_init);