blob: 59f4261c753a019a157256b081d905a9ba3e4e79 [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
Len Brown4f86d3a2007-10-03 18:58:00 -040056/**
57 * cpuidle_idle_call - the main idle loop
58 *
59 * NOTE: no locks or semaphores should be used here
Len Browna0bfa132011-04-01 19:34:59 -040060 * return non-zero on failure
Len Brown4f86d3a2007-10-03 18:58:00 -040061 */
Len Browna0bfa132011-04-01 19:34:59 -040062int cpuidle_idle_call(void)
Len Brown4f86d3a2007-10-03 18:58:00 -040063{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -060064 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053065 struct cpuidle_driver *drv = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -040066 struct cpuidle_state *target_state;
Deepthi Dharware978aa72011-10-28 16:20:09 +053067 int next_state, entered_state;
Len Brown4f86d3a2007-10-03 18:58:00 -040068
Len Browna0bfa132011-04-01 19:34:59 -040069 if (off)
70 return -ENODEV;
71
72 if (!initialized)
73 return -ENODEV;
74
Len Brown4f86d3a2007-10-03 18:58:00 -040075 /* check if the device is ready */
Len Browna0bfa132011-04-01 19:34:59 -040076 if (!dev || !dev->enabled)
77 return -EBUSY;
Len Brown4f86d3a2007-10-03 18:58:00 -040078
Arjan van de Ven9a655832008-11-09 12:45:10 -080079#if 0
80 /* shows regressions, re-enable for 2.6.29 */
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070081 /*
82 * run any timers that can be run now, at this point
83 * before calculating the idle duration etc.
84 */
85 hrtimer_peek_ahead_timers();
Arjan van de Ven9a655832008-11-09 12:45:10 -080086#endif
Ai Li71abbbf2010-08-09 17:20:13 -070087
Len Brown4f86d3a2007-10-03 18:58:00 -040088 /* ask the governor for the next state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053089 next_state = cpuidle_curr_governor->select(drv, dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -070090 if (need_resched()) {
91 local_irq_enable();
Len Browna0bfa132011-04-01 19:34:59 -040092 return 0;
Kevin Hilman246eb7f2009-10-26 16:50:18 -070093 }
94
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053095 target_state = &drv->states[next_state];
Thomas Renningerf77cfe42011-01-07 11:29:44 +010096
97 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
98 trace_cpu_idle(next_state, dev->cpu);
99
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530100 entered_state = target_state->enter(dev, drv, next_state);
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100101
102 trace_power_end(dev->cpu);
103 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
104
Deepthi Dharware978aa72011-10-28 16:20:09 +0530105 if (entered_state >= 0) {
106 /* Update cpuidle counters */
107 /* This can be moved to within driver enter routine
108 * but that results in multiple copies of same code.
109 */
Deepthi Dharwar42027352011-10-28 16:20:33 +0530110 dev->states_usage[entered_state].time +=
Deepthi Dharware978aa72011-10-28 16:20:09 +0530111 (unsigned long long)dev->last_residency;
Deepthi Dharwar42027352011-10-28 16:20:33 +0530112 dev->states_usage[entered_state].usage++;
Deepthi Dharware978aa72011-10-28 16:20:09 +0530113 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400114
115 /* give the governor an opportunity to reflect on the outcome */
116 if (cpuidle_curr_governor->reflect)
Deepthi Dharware978aa72011-10-28 16:20:09 +0530117 cpuidle_curr_governor->reflect(dev, entered_state);
Len Browna0bfa132011-04-01 19:34:59 -0400118
119 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400120}
121
122/**
123 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
124 */
125void cpuidle_install_idle_handler(void)
126{
Len Browna0bfa132011-04-01 19:34:59 -0400127 if (enabled_devices) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400128 /* Make sure all changes finished before we switch to new idle */
129 smp_wmb();
Len Browna0bfa132011-04-01 19:34:59 -0400130 initialized = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400131 }
132}
133
134/**
135 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
136 */
137void cpuidle_uninstall_idle_handler(void)
138{
Len Browna0bfa132011-04-01 19:34:59 -0400139 if (enabled_devices) {
140 initialized = 0;
Venki Pallipadia6869cc2008-02-08 17:05:44 -0800141 cpuidle_kick_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400142 }
143}
144
145/**
146 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
147 */
148void cpuidle_pause_and_lock(void)
149{
150 mutex_lock(&cpuidle_lock);
151 cpuidle_uninstall_idle_handler();
152}
153
154EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
155
156/**
157 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
158 */
159void cpuidle_resume_and_unlock(void)
160{
161 cpuidle_install_idle_handler();
162 mutex_unlock(&cpuidle_lock);
163}
164
165EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
166
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100167#ifdef CONFIG_ARCH_HAS_CPU_RELAX
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530168static int poll_idle(struct cpuidle_device *dev,
169 struct cpuidle_driver *drv, int index)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100170{
171 ktime_t t1, t2;
172 s64 diff;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100173
174 t1 = ktime_get();
175 local_irq_enable();
176 while (!need_resched())
177 cpu_relax();
178
179 t2 = ktime_get();
180 diff = ktime_to_us(ktime_sub(t2, t1));
181 if (diff > INT_MAX)
182 diff = INT_MAX;
183
Deepthi Dharware978aa72011-10-28 16:20:09 +0530184 dev->last_residency = (int) diff;
185
186 return index;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100187}
188
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530189static void poll_idle_init(struct cpuidle_driver *drv)
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100190{
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530191 struct cpuidle_state *state = &drv->states[0];
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100192
Thomas Renninger720f1c32011-01-07 11:29:43 +0100193 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100194 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
195 state->exit_latency = 0;
196 state->target_residency = 0;
197 state->power_usage = -1;
Len Brownd2476322011-01-12 02:34:59 -0500198 state->flags = 0;
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100199 state->enter = poll_idle;
200}
201#else
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530202static void poll_idle_init(struct cpuidle_driver *drv) {}
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100203#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
204
Len Brown4f86d3a2007-10-03 18:58:00 -0400205/**
206 * cpuidle_enable_device - enables idle PM for a CPU
207 * @dev: the CPU
208 *
209 * This function must be called between cpuidle_pause_and_lock and
210 * cpuidle_resume_and_unlock when used externally.
211 */
212int cpuidle_enable_device(struct cpuidle_device *dev)
213{
214 int ret, i;
215
216 if (dev->enabled)
217 return 0;
Len Brown752138d2010-05-22 16:57:26 -0400218 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400219 return -EIO;
220 if (!dev->state_count)
221 return -EINVAL;
222
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400223 if (dev->registered == 0) {
224 ret = __cpuidle_register_device(dev);
225 if (ret)
226 return ret;
227 }
228
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530229 poll_idle_init(cpuidle_get_driver());
Rafael J. Wysockid8c216c2011-01-08 00:29:20 +0100230
Len Brown4f86d3a2007-10-03 18:58:00 -0400231 if ((ret = cpuidle_add_state_sysfs(dev)))
232 return ret;
233
234 if (cpuidle_curr_governor->enable &&
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530235 (ret = cpuidle_curr_governor->enable(cpuidle_get_driver(), dev)))
Len Brown4f86d3a2007-10-03 18:58:00 -0400236 goto fail_sysfs;
237
238 for (i = 0; i < dev->state_count; i++) {
Deepthi Dharwar42027352011-10-28 16:20:33 +0530239 dev->states_usage[i].usage = 0;
240 dev->states_usage[i].time = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400241 }
242 dev->last_residency = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400243
244 smp_wmb();
245
246 dev->enabled = 1;
247
248 enabled_devices++;
249 return 0;
250
251fail_sysfs:
252 cpuidle_remove_state_sysfs(dev);
253
254 return ret;
255}
256
257EXPORT_SYMBOL_GPL(cpuidle_enable_device);
258
259/**
260 * cpuidle_disable_device - disables idle PM for a CPU
261 * @dev: the CPU
262 *
263 * This function must be called between cpuidle_pause_and_lock and
264 * cpuidle_resume_and_unlock when used externally.
265 */
266void cpuidle_disable_device(struct cpuidle_device *dev)
267{
268 if (!dev->enabled)
269 return;
Len Brown752138d2010-05-22 16:57:26 -0400270 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400271 return;
272
273 dev->enabled = 0;
274
275 if (cpuidle_curr_governor->disable)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530276 cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400277
278 cpuidle_remove_state_sysfs(dev);
279 enabled_devices--;
280}
281
282EXPORT_SYMBOL_GPL(cpuidle_disable_device);
283
284/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400285 * __cpuidle_register_device - internal register function called before register
286 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400287 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400288 *
289 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400290 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400291static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400292{
293 int ret;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800294 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400295 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400296
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800297 if (!dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400298 return -EINVAL;
Len Brown752138d2010-05-22 16:57:26 -0400299 if (!try_module_get(cpuidle_driver->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400300 return -EINVAL;
301
302 init_completion(&dev->kobj_unregister);
303
Len Brown4f86d3a2007-10-03 18:58:00 -0400304 per_cpu(cpuidle_devices, dev->cpu) = dev;
305 list_add(&dev->device_list, &cpuidle_detected_devices);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800306 if ((ret = cpuidle_add_sysfs(cpu_dev))) {
Len Brown752138d2010-05-22 16:57:26 -0400307 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400308 return ret;
309 }
310
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400311 dev->registered = 1;
312 return 0;
313}
314
315/**
316 * cpuidle_register_device - registers a CPU's idle PM feature
317 * @dev: the cpu
318 */
319int cpuidle_register_device(struct cpuidle_device *dev)
320{
321 int ret;
322
323 mutex_lock(&cpuidle_lock);
324
325 if ((ret = __cpuidle_register_device(dev))) {
326 mutex_unlock(&cpuidle_lock);
327 return ret;
328 }
329
Len Brown4f86d3a2007-10-03 18:58:00 -0400330 cpuidle_enable_device(dev);
331 cpuidle_install_idle_handler();
332
333 mutex_unlock(&cpuidle_lock);
334
335 return 0;
336
337}
338
339EXPORT_SYMBOL_GPL(cpuidle_register_device);
340
341/**
342 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
343 * @dev: the cpu
344 */
345void cpuidle_unregister_device(struct cpuidle_device *dev)
346{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800347 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400348 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400349
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400350 if (dev->registered == 0)
351 return;
352
Len Brown4f86d3a2007-10-03 18:58:00 -0400353 cpuidle_pause_and_lock();
354
355 cpuidle_disable_device(dev);
356
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800357 cpuidle_remove_sysfs(cpu_dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400358 list_del(&dev->device_list);
359 wait_for_completion(&dev->kobj_unregister);
360 per_cpu(cpuidle_devices, dev->cpu) = NULL;
361
362 cpuidle_resume_and_unlock();
363
Len Brown752138d2010-05-22 16:57:26 -0400364 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400365}
366
367EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
368
369#ifdef CONFIG_SMP
370
371static void smp_callback(void *v)
372{
373 /* we already woke the CPU up, nothing more to do */
374}
375
376/*
377 * This function gets called when a part of the kernel has a new latency
378 * requirement. This means we need to get all processors out of their C-state,
379 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
380 * wakes them all right up.
381 */
382static int cpuidle_latency_notify(struct notifier_block *b,
383 unsigned long l, void *v)
384{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200385 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400386 return NOTIFY_OK;
387}
388
389static struct notifier_block cpuidle_latency_notifier = {
390 .notifier_call = cpuidle_latency_notify,
391};
392
Mark Grossd82b3512008-02-04 22:30:08 -0800393static inline void latency_notifier_init(struct notifier_block *n)
394{
395 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
396}
Len Brown4f86d3a2007-10-03 18:58:00 -0400397
398#else /* CONFIG_SMP */
399
400#define latency_notifier_init(x) do { } while (0)
401
402#endif /* CONFIG_SMP */
403
404/**
405 * cpuidle_init - core initializer
406 */
407static int __init cpuidle_init(void)
408{
409 int ret;
410
Len Brown62027ae2011-04-01 18:13:10 -0400411 if (cpuidle_disabled())
412 return -ENODEV;
413
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800414 ret = cpuidle_add_interface(cpu_subsys.dev_root);
Len Brown4f86d3a2007-10-03 18:58:00 -0400415 if (ret)
416 return ret;
417
418 latency_notifier_init(&cpuidle_latency_notifier);
419
420 return 0;
421}
422
Len Brown62027ae2011-04-01 18:13:10 -0400423module_param(off, int, 0444);
Len Brown4f86d3a2007-10-03 18:58:00 -0400424core_initcall(cpuidle_init);