blob: 4869b55002340adc430352e9aab9b756e5936162 [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);
Len Brown4f86d3a2007-10-03 18:58:00 -040026
27DEFINE_MUTEX(cpuidle_lock);
28LIST_HEAD(cpuidle_detected_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040029
30static int enabled_devices;
Len Brown62027ae2011-04-01 18:13:10 -040031static int off __read_mostly;
Len Browna0bfa132011-04-01 19:34:59 -040032static int initialized __read_mostly;
Len Brown62027ae2011-04-01 18:13:10 -040033
34int cpuidle_disabled(void)
35{
36 return off;
37}
Len Brownd91ee582011-04-01 18:28:35 -040038void disable_cpuidle(void)
39{
40 off = 1;
41}
Len Brown4f86d3a2007-10-03 18:58:00 -040042
Venki Pallipadia6869cc2008-02-08 17:05:44 -080043#if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
44static void cpuidle_kick_cpus(void)
45{
46 cpu_idle_wait();
47}
48#elif defined(CONFIG_SMP)
49# error "Arch needs cpu_idle_wait() equivalent here"
50#else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
51static void cpuidle_kick_cpus(void) {}
52#endif
53
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040054static int __cpuidle_register_device(struct cpuidle_device *dev);
55
Robert Leee1689792012-03-20 15:22:42 -050056static inline int cpuidle_enter(struct cpuidle_device *dev,
57 struct cpuidle_driver *drv, int index)
58{
59 struct cpuidle_state *target_state = &drv->states[index];
60 return target_state->enter(dev, drv, index);
61}
62
63static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
64 struct cpuidle_driver *drv, int index)
65{
66 return cpuidle_wrap_enter(dev, drv, index, cpuidle_enter);
67}
68
69typedef int (*cpuidle_enter_t)(struct cpuidle_device *dev,
70 struct cpuidle_driver *drv, int index);
71
72static cpuidle_enter_t cpuidle_enter_ops;
73
Len Brown4f86d3a2007-10-03 18:58:00 -040074/**
75 * cpuidle_idle_call - the main idle loop
76 *
77 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -040078 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -040079 */
Len Browna0bfa132011-04-01 19:34:59 -040080int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -040081{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -060082 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053083 struct cpuidle_driver *drv = cpuidle_get_driver();
Deepthi Dharware978aa72011-10-28 16:20:09 +053084 int next_state, entered_state;
Len Brown4f86d3a2007-10-03 18:58:00 -040085
Len Browna0bfa132011-04-01 19:34:59 -040086 if (off)
87 return -ENODEV;
88
89 if (!initialized)
90 return -ENODEV;
91
Len Brown4f86d3a2007-10-03 18:58:00 -040092 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -040093 if (!dev || !dev->enabled)
94 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -040095
Arjan van de Ven9a655832008-11-09 12:45:10 -080096#if 0
97 /* shows regressions, re-enable for 2.6.29 */
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070098 /*
99 * run any timers that can be run now, at this point
100 * before calculating the idle duration etc.
101 */
102 hrtimer_peek_ahead_timers();
Arjan van de Ven9a655832008-11-09 12:45:10 -0800103#endif
Ai Li71abbbf2010-08-09 17:20:13 -0700104
Len Brown4f86d3a2007-10-03 18:58:00 -0400105 /* ask the governor for the next state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530106 next_state = cpuidle_curr_governor->select(drv, dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700107 if (need_resched()) {
108 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -0400109 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -0700110 }
111
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100112 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
113 trace_cpu_idle(next_state, dev->cpu);
114
Robert Leee1689792012-03-20 15:22:42 -0500115 entered_state = cpuidle_enter_ops(dev, drv, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100116
117 trace_power_end(dev->cpu);
118 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
119
Deepthi Dharware978aa72011-10-28 16:20:09 +0530120 if (entered_state >= 0) {
121 /* Update cpuidle counters */
122 /* This can be moved to within driver enter routine
123 * but that results in multiple copies of same code.
124 */
Deepthi Dharwar42027352011-10-28 16:20:33 +0530125 dev->states_usage[entered_state].time +=
Deepthi Dharware978aa72011-10-28 16:20:09 +0530126 (unsigned long long)dev->last_residency;
Deepthi Dharwar42027352011-10-28 16:20:33 +0530127 dev->states_usage[entered_state].usage++;
Robert Leee1689792012-03-20 15:22:42 -0500128 } else {
129 dev->last_residency = 0;
Deepthi Dharware978aa72011-10-28 16:20:09 +0530130 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400131
132 /* give the governor an opportunity to reflect on the outcome */
133 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530134 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400135
136 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400137}
138
139/**
140 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
141 */
142void cpuidle_install_idle_handler(void)
143{
Len Browna0bfa132011-04-01 19:34:59 -0400144 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400145 /* Make sure all changes finished before we switch to new idle */
146 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400147 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400148 }
149}
150
151/**
152 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
153 */
154void cpuidle_uninstall_idle_handler(void)
155{
Len Browna0bfa132011-04-01 19:34:59 -0400156 if (enabled_devices) {
157 initialized = 0;
Venki Pallipadia6869cc2008-02-08 17:05:44 -0800158 cpuidle_kick_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400159 }
160}
161
162/**
163 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
164 */
165void cpuidle_pause_and_lock(void)
166{
167 mutex_lock(&cpuidle_lock);
168 cpuidle_uninstall_idle_handler();
169}
170
171EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
172
173/**
174 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
175 */
176void cpuidle_resume_and_unlock(void)
177{
178 cpuidle_install_idle_handler();
179 mutex_unlock(&cpuidle_lock);
180}
181
182EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
183
Robert Leee1689792012-03-20 15:22:42 -0500184/**
185 * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
186 * @dev: pointer to a valid cpuidle_device object
187 * @drv: pointer to a valid cpuidle_driver object
188 * @index: index of the target cpuidle state.
189 */
190int cpuidle_wrap_enter(struct cpuidle_device *dev,
191 struct cpuidle_driver *drv, int index,
192 int (*enter)(struct cpuidle_device *dev,
193 struct cpuidle_driver *drv, int index))
194{
195 ktime_t time_start, time_end;
196 s64 diff;
197
198 time_start = ktime_get();
199
200 index = enter(dev, drv, index);
201
202 time_end = ktime_get();
203
204 local_irq_enable();
205
206 diff = ktime_to_us(ktime_sub(time_end, time_start));
207 if (diff > INT_MAX)
208 diff = INT_MAX;
209
210 dev->last_residency = (int) diff;
211
212 return index;
213}
214
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100215#ifdef CONFIG_ARCH_HAS_CPU_RELAX
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530216static int poll_idle(struct cpuidle_device *dev,
217 struct cpuidle_driver *drv, int index)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100218{
219 ktime_t t1, t2;
220 s64 diff;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100221
222 t1 = ktime_get();
223 local_irq_enable();
224 while (!need_resched())
225 cpu_relax();
226
227 t2 = ktime_get();
228 diff = ktime_to_us(ktime_sub(t2, t1));
229 if (diff > INT_MAX)
230 diff = INT_MAX;
231
Deepthi Dharware978aa72011-10-28 16:20:09 +0530232 dev->last_residency = (int) diff;
233
234 return index;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100235}
236
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530237static void poll_idle_init(struct cpuidle_driver *drv)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100238{
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530239 struct cpuidle_state *state = &drv->states[0];
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100240
Thomas Renninger720f1c32011-01-07 11:29:43 +0100241 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100242 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
243 state->exit_latency = 0;
244 state->target_residency = 0;
245 state->power_usage = -1;
Len Brownd2476322011-01-12 02:34:59 -0500246 state->flags = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100247 state->enter = poll_idle;
248}
249#else
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530250static void poll_idle_init(struct cpuidle_driver *drv) {}
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100251#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
252
Len Brown4f86d3a2007-10-03 18:58:00 -0400253/**
254 * cpuidle_enable_device - enables idle PM for a CPU
255 * @dev: the CPU
256 *
257 * This function must be called between cpuidle_pause_and_lock and
258 * cpuidle_resume_and_unlock when used externally.
259 */
260int cpuidle_enable_device(struct cpuidle_device *dev)
261{
262 int ret, i;
Robert Leee1689792012-03-20 15:22:42 -0500263 struct cpuidle_driver *drv = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400264
265 if (dev->enabled)
266 return 0;
Robert Leee1689792012-03-20 15:22:42 -0500267 if (!drv || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400268 return -EIO;
269 if (!dev->state_count)
270 return -EINVAL;
271
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400272 if (dev->registered == 0) {
273 ret = __cpuidle_register_device(dev);
274 if (ret)
275 return ret;
276 }
277
Robert Leee1689792012-03-20 15:22:42 -0500278 cpuidle_enter_ops = drv->en_core_tk_irqen ?
279 cpuidle_enter_tk : cpuidle_enter;
280
281 poll_idle_init(drv);
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100282
Len Brown4f86d3a2007-10-03 18:58:00 -0400283 if ((ret = cpuidle_add_state_sysfs(dev)))
284 return ret;
285
286 if (cpuidle_curr_governor->enable &&
Robert Leee1689792012-03-20 15:22:42 -0500287 (ret = cpuidle_curr_governor->enable(drv, dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400288 goto fail_sysfs;
289
290 for (i = 0; i < dev->state_count; i++) {
Deepthi Dharwar42027352011-10-28 16:20:33 +0530291 dev->states_usage[i].usage = 0;
292 dev->states_usage[i].time = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400293 }
294 dev->last_residency = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400295
296 smp_wmb();
297
298 dev->enabled = 1;
299
300 enabled_devices++;
301 return 0;
302
303fail_sysfs:
304 cpuidle_remove_state_sysfs(dev);
305
306 return ret;
307}
308
309EXPORT_SYMBOL_GPL(cpuidle_enable_device);
310
311/**
312 * cpuidle_disable_device - disables idle PM for a CPU
313 * @dev: the CPU
314 *
315 * This function must be called between cpuidle_pause_and_lock and
316 * cpuidle_resume_and_unlock when used externally.
317 */
318void cpuidle_disable_device(struct cpuidle_device *dev)
319{
320 if (!dev->enabled)
321 return;
Len Brown752138d2010-05-22 16:57:26 -0400322 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400323 return;
324
325 dev->enabled = 0;
326
327 if (cpuidle_curr_governor->disable)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530328 cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400329
330 cpuidle_remove_state_sysfs(dev);
331 enabled_devices--;
332}
333
334EXPORT_SYMBOL_GPL(cpuidle_disable_device);
335
336/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400337 * __cpuidle_register_device - internal register function called before register
338 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400339 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400340 *
341 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400342 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400343static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400344{
345 int ret;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800346 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400347 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400348
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800349 if (!dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400350 return -EINVAL;
Len Brown752138d2010-05-22 16:57:26 -0400351 if (!try_module_get(cpuidle_driver->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400352 return -EINVAL;
353
354 init_completion(&dev->kobj_unregister);
355
Len Brown4f86d3a2007-10-03 18:58:00 -0400356 per_cpu(cpuidle_devices, dev->cpu) = dev;
357 list_add(&dev->device_list, &cpuidle_detected_devices);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800358 if ((ret = cpuidle_add_sysfs(cpu_dev))) {
Len Brown752138d2010-05-22 16:57:26 -0400359 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400360 return ret;
361 }
362
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400363 dev->registered = 1;
364 return 0;
365}
366
367/**
368 * cpuidle_register_device - registers a CPU's idle PM feature
369 * @dev: the cpu
370 */
371int cpuidle_register_device(struct cpuidle_device *dev)
372{
373 int ret;
374
375 mutex_lock(&cpuidle_lock);
376
377 if ((ret = __cpuidle_register_device(dev))) {
378 mutex_unlock(&cpuidle_lock);
379 return ret;
380 }
381
Len Brown4f86d3a2007-10-03 18:58:00 -0400382 cpuidle_enable_device(dev);
383 cpuidle_install_idle_handler();
384
385 mutex_unlock(&cpuidle_lock);
386
387 return 0;
388
389}
390
391EXPORT_SYMBOL_GPL(cpuidle_register_device);
392
393/**
394 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
395 * @dev: the cpu
396 */
397void cpuidle_unregister_device(struct cpuidle_device *dev)
398{
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
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400402 if (dev->registered == 0)
403 return;
404
Len Brown4f86d3a2007-10-03 18:58:00 -0400405 cpuidle_pause_and_lock();
406
407 cpuidle_disable_device(dev);
408
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800409 cpuidle_remove_sysfs(cpu_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400410 list_del(&dev->device_list);
411 wait_for_completion(&dev->kobj_unregister);
412 per_cpu(cpuidle_devices, dev->cpu) = NULL;
413
414 cpuidle_resume_and_unlock();
415
Len Brown752138d2010-05-22 16:57:26 -0400416 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400417}
418
419EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
420
421#ifdef CONFIG_SMP
422
423static void smp_callback(void *v)
424{
425 /* we already woke the CPU up, nothing more to do */
426}
427
428/*
429 * This function gets called when a part of the kernel has a new latency
430 * requirement. This means we need to get all processors out of their C-state,
431 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
432 * wakes them all right up.
433 */
434static int cpuidle_latency_notify(struct notifier_block *b,
435 unsigned long l, void *v)
436{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200437 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400438 return NOTIFY_OK;
439}
440
441static struct notifier_block cpuidle_latency_notifier = {
442 .notifier_call = cpuidle_latency_notify,
443};
444
Mark Grossd82b3512008-02-04 22:30:08 -0800445static inline void latency_notifier_init(struct notifier_block *n)
446{
447 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
448}
Len Brown4f86d3a2007-10-03 18:58:00 -0400449
450#else /* CONFIG_SMP */
451
452#define latency_notifier_init(x) do { } while (0)
453
454#endif /* CONFIG_SMP */
455
456/**
457 * cpuidle_init - core initializer
458 */
459static int __init cpuidle_init(void)
460{
461 int ret;
462
Len Brown62027ae2011-04-01 18:13:10 -0400463 if (cpuidle_disabled())
464 return -ENODEV;
465
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800466 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400467 if (ret)
468 return ret;
469
470 latency_notifier_init(&cpuidle_latency_notifier);
471
472 return 0;
473}
474
Len Brown62027ae2011-04-01 18:13:10 -0400475module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400476core_initcall(cpuidle_init);