blob: 88bd121043962cad5b8f2a54b0ee22fd0b73a512 [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>
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070019#include <linux/hrtimer.h>
Arjan van de Ven288f0232009-09-19 13:35:33 +020020#include <trace/events/power.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040021
22#include "cpuidle.h"
23
24DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040025
26DEFINE_MUTEX(cpuidle_lock);
27LIST_HEAD(cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040028
29static int enabled_devices;
Len Brown62027ae2011-04-01 18:13:10 -040030static int off __read_mostly;
Len Browna0bfa132011-04-01 19:34:59 -040031static int initialized __read_mostly;
Len Brown62027ae2011-04-01 18:13:10 -040032
33int cpuidle_disabled(void)
34{
35 return off;
36}
Len Brownd91ee582011-04-01 18:28:35 -040037void disable_cpuidle(void)
38{
39 off = 1;
40}
Len Brown4f86d3a2007-10-03 18:58:00 -040041
Venki Pallipadia6869cc2008-02-08 17:05:44 -080042#if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
43static void cpuidle_kick_cpus(void)
44{
45 cpu_idle_wait();
46}
47#elif defined(CONFIG_SMP)
48# error "Arch needs cpu_idle_wait() equivalent here"
49#else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
50static void cpuidle_kick_cpus(void) {}
51#endif
52
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040053static int __cpuidle_register_device(struct cpuidle_device *dev);
54
Len Brown4f86d3a2007-10-03 18:58:00 -040055/**
56 * cpuidle_idle_call - the main idle loop
57 *
58 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -040059 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -040060 */
Len Browna0bfa132011-04-01 19:34:59 -040061int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -040062{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -060063 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040064 struct cpuidle_state *target_state;
Deepthi Dharware978aa72011-10-28 16:20:09 +053065 int next_state, entered_state;
Len Brown4f86d3a2007-10-03 18:58:00 -040066
Len Browna0bfa132011-04-01 19:34:59 -040067 if (off)
68 return -ENODEV;
69
70 if (!initialized)
71 return -ENODEV;
72
Len Brown4f86d3a2007-10-03 18:58:00 -040073 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -040074 if (!dev || !dev->enabled)
75 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -040076
Arjan van de Ven9a655832008-11-09 12:45:10 -080077#if 0
78 /* shows regressions, re-enable for 2.6.29 */
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070079 /*
80 * run any timers that can be run now, at this point
81 * before calculating the idle duration etc.
82 */
83 hrtimer_peek_ahead_timers();
Arjan van de Ven9a655832008-11-09 12:45:10 -080084#endif
Ai Li71abbbf2010-08-09 17:20:13 -070085
86 /*
87 * Call the device's prepare function before calling the
88 * governor's select function. ->prepare gives the device's
89 * cpuidle driver a chance to update any dynamic information
90 * of its cpuidle states for the current idle period, e.g.
91 * state availability, latencies, residencies, etc.
92 */
93 if (dev->prepare)
94 dev->prepare(dev);
95
Len Brown4f86d3a2007-10-03 18:58:00 -040096 /* ask the governor for the next state */
97 next_state = cpuidle_curr_governor->select(dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -070098 if (need_resched()) {
99 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400100 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700101 }
102
Len Brown4f86d3a2007-10-03 18:58:00 -0400103 target_state = &dev->states[next_state];
104
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100105 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
106 trace_cpu_idle(next_state, dev->cpu);
107
Deepthi Dharware978aa72011-10-28 16:20:09 +0530108 entered_state = target_state->enter(dev, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100109
110 trace_power_end(dev->cpu);
111 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
112
Deepthi Dharware978aa72011-10-28 16:20:09 +0530113 if (entered_state >= 0) {
114 /* Update cpuidle counters */
115 /* This can be moved to within driver enter routine
116 * but that results in multiple copies of same code.
117 */
118 dev->states[entered_state].time +=
119 (unsigned long long)dev->last_residency;
120 dev->states[entered_state].usage++;
121 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400122
123 /* give the governor an opportunity to reflect on the outcome */
124 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530125 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400126
127 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400128}
129
130/**
131 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
132 */
133void cpuidle_install_idle_handler(void)
134{
Len Browna0bfa132011-04-01 19:34:59 -0400135 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400136 /* Make sure all changes finished before we switch to new idle */
137 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400138 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400139 }
140}
141
142/**
143 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
144 */
145void cpuidle_uninstall_idle_handler(void)
146{
Len Browna0bfa132011-04-01 19:34:59 -0400147 if (enabled_devices) {
148 initialized = 0;
Venki Pallipadia6869cc2008-02-08 17:05:44 -0800149 cpuidle_kick_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400150 }
151}
152
153/**
154 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
155 */
156void cpuidle_pause_and_lock(void)
157{
158 mutex_lock(&cpuidle_lock);
159 cpuidle_uninstall_idle_handler();
160}
161
162EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
163
164/**
165 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
166 */
167void cpuidle_resume_and_unlock(void)
168{
169 cpuidle_install_idle_handler();
170 mutex_unlock(&cpuidle_lock);
171}
172
173EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
174
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100175#ifdef CONFIG_ARCH_HAS_CPU_RELAX
Deepthi Dharware978aa72011-10-28 16:20:09 +0530176static int poll_idle(struct cpuidle_device *dev, int index)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100177{
178 ktime_t t1, t2;
179 s64 diff;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100180
181 t1 = ktime_get();
182 local_irq_enable();
183 while (!need_resched())
184 cpu_relax();
185
186 t2 = ktime_get();
187 diff = ktime_to_us(ktime_sub(t2, t1));
188 if (diff > INT_MAX)
189 diff = INT_MAX;
190
Deepthi Dharware978aa72011-10-28 16:20:09 +0530191 dev->last_residency = (int) diff;
192
193 return index;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100194}
195
196static void poll_idle_init(struct cpuidle_device *dev)
197{
198 struct cpuidle_state *state = &dev->states[0];
199
200 cpuidle_set_statedata(state, NULL);
201
Thomas Renninger720f1c32011-01-07 11:29:43 +0100202 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100203 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
204 state->exit_latency = 0;
205 state->target_residency = 0;
206 state->power_usage = -1;
Len Brownd2476322011-01-12 02:34:59 -0500207 state->flags = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100208 state->enter = poll_idle;
209}
210#else
211static void poll_idle_init(struct cpuidle_device *dev) {}
212#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
213
Len Brown4f86d3a2007-10-03 18:58:00 -0400214/**
215 * cpuidle_enable_device - enables idle PM for a CPU
216 * @dev: the CPU
217 *
218 * This function must be called between cpuidle_pause_and_lock and
219 * cpuidle_resume_and_unlock when used externally.
220 */
221int cpuidle_enable_device(struct cpuidle_device *dev)
222{
223 int ret, i;
224
225 if (dev->enabled)
226 return 0;
Len Brown752138d2010-05-22 16:57:26 -0400227 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400228 return -EIO;
229 if (!dev->state_count)
230 return -EINVAL;
231
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400232 if (dev->registered == 0) {
233 ret = __cpuidle_register_device(dev);
234 if (ret)
235 return ret;
236 }
237
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100238 poll_idle_init(dev);
239
Len Brown4f86d3a2007-10-03 18:58:00 -0400240 if ((ret = cpuidle_add_state_sysfs(dev)))
241 return ret;
242
243 if (cpuidle_curr_governor->enable &&
244 (ret = cpuidle_curr_governor->enable(dev)))
245 goto fail_sysfs;
246
247 for (i = 0; i < dev->state_count; i++) {
248 dev->states[i].usage = 0;
249 dev->states[i].time = 0;
250 }
251 dev->last_residency = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400252
253 smp_wmb();
254
255 dev->enabled = 1;
256
257 enabled_devices++;
258 return 0;
259
260fail_sysfs:
261 cpuidle_remove_state_sysfs(dev);
262
263 return ret;
264}
265
266EXPORT_SYMBOL_GPL(cpuidle_enable_device);
267
268/**
269 * cpuidle_disable_device - disables idle PM for a CPU
270 * @dev: the CPU
271 *
272 * This function must be called between cpuidle_pause_and_lock and
273 * cpuidle_resume_and_unlock when used externally.
274 */
275void cpuidle_disable_device(struct cpuidle_device *dev)
276{
277 if (!dev->enabled)
278 return;
Len Brown752138d2010-05-22 16:57:26 -0400279 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400280 return;
281
282 dev->enabled = 0;
283
284 if (cpuidle_curr_governor->disable)
285 cpuidle_curr_governor->disable(dev);
286
287 cpuidle_remove_state_sysfs(dev);
288 enabled_devices--;
289}
290
291EXPORT_SYMBOL_GPL(cpuidle_disable_device);
292
293/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400294 * __cpuidle_register_device - internal register function called before register
295 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400296 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400297 *
298 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400299 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400300static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400301{
302 int ret;
303 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400304 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400305
306 if (!sys_dev)
307 return -EINVAL;
Len Brown752138d2010-05-22 16:57:26 -0400308 if (!try_module_get(cpuidle_driver->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400309 return -EINVAL;
310
311 init_completion(&dev->kobj_unregister);
312
Ai Li71abbbf2010-08-09 17:20:13 -0700313 /*
314 * cpuidle driver should set the dev->power_specified bit
315 * before registering the device if the driver provides
316 * power_usage numbers.
317 *
318 * For those devices whose ->power_specified is not set,
319 * we fill in power_usage with decreasing values as the
320 * cpuidle code has an implicit assumption that state Cn
321 * uses less power than C(n-1).
322 *
323 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
324 * an power value of -1. So we use -2, -3, etc, for other
325 * c-states.
326 */
327 if (!dev->power_specified) {
328 int i;
329 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
330 dev->states[i].power_usage = -1 - i;
331 }
332
Len Brown4f86d3a2007-10-03 18:58:00 -0400333 per_cpu(cpuidle_devices, dev->cpu) = dev;
334 list_add(&dev->device_list, &cpuidle_detected_devices);
335 if ((ret = cpuidle_add_sysfs(sys_dev))) {
Len Brown752138d2010-05-22 16:57:26 -0400336 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400337 return ret;
338 }
339
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400340 dev->registered = 1;
341 return 0;
342}
343
344/**
345 * cpuidle_register_device - registers a CPU's idle PM feature
346 * @dev: the cpu
347 */
348int cpuidle_register_device(struct cpuidle_device *dev)
349{
350 int ret;
351
352 mutex_lock(&cpuidle_lock);
353
354 if ((ret = __cpuidle_register_device(dev))) {
355 mutex_unlock(&cpuidle_lock);
356 return ret;
357 }
358
Len Brown4f86d3a2007-10-03 18:58:00 -0400359 cpuidle_enable_device(dev);
360 cpuidle_install_idle_handler();
361
362 mutex_unlock(&cpuidle_lock);
363
364 return 0;
365
366}
367
368EXPORT_SYMBOL_GPL(cpuidle_register_device);
369
370/**
371 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
372 * @dev: the cpu
373 */
374void cpuidle_unregister_device(struct cpuidle_device *dev)
375{
376 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400377 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400378
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400379 if (dev->registered == 0)
380 return;
381
Len Brown4f86d3a2007-10-03 18:58:00 -0400382 cpuidle_pause_and_lock();
383
384 cpuidle_disable_device(dev);
385
386 cpuidle_remove_sysfs(sys_dev);
387 list_del(&dev->device_list);
388 wait_for_completion(&dev->kobj_unregister);
389 per_cpu(cpuidle_devices, dev->cpu) = NULL;
390
391 cpuidle_resume_and_unlock();
392
Len Brown752138d2010-05-22 16:57:26 -0400393 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400394}
395
396EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
397
398#ifdef CONFIG_SMP
399
400static void smp_callback(void *v)
401{
402 /* we already woke the CPU up, nothing more to do */
403}
404
405/*
406 * This function gets called when a part of the kernel has a new latency
407 * requirement. This means we need to get all processors out of their C-state,
408 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
409 * wakes them all right up.
410 */
411static int cpuidle_latency_notify(struct notifier_block *b,
412 unsigned long l, void *v)
413{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200414 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400415 return NOTIFY_OK;
416}
417
418static struct notifier_block cpuidle_latency_notifier = {
419 .notifier_call = cpuidle_latency_notify,
420};
421
Mark Grossd82b3512008-02-04 22:30:08 -0800422static inline void latency_notifier_init(struct notifier_block *n)
423{
424 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
425}
Len Brown4f86d3a2007-10-03 18:58:00 -0400426
427#else /* CONFIG_SMP */
428
429#define latency_notifier_init(x) do { } while (0)
430
431#endif /* CONFIG_SMP */
432
433/**
434 * cpuidle_init - core initializer
435 */
436static int __init cpuidle_init(void)
437{
438 int ret;
439
Len Brown62027ae2011-04-01 18:13:10 -0400440 if (cpuidle_disabled())
441 return -ENODEV;
442
Len Brown4f86d3a2007-10-03 18:58:00 -0400443 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
444 if (ret)
445 return ret;
446
447 latency_notifier_init(&cpuidle_latency_notifier);
448
449 return 0;
450}
451
Len Brown62027ae2011-04-01 18:13:10 -0400452module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400453core_initcall(cpuidle_init);