blob: 386888f10df02c0147b1b3664d8b08e7ebe88aa4 [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);
28static void (*pm_idle_old)(void);
29
30static int enabled_devices;
31
Venki Pallipadia6869cc2008-02-08 17:05:44 -080032#if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
33static void cpuidle_kick_cpus(void)
34{
35 cpu_idle_wait();
36}
37#elif defined(CONFIG_SMP)
38# error "Arch needs cpu_idle_wait() equivalent here"
39#else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
40static void cpuidle_kick_cpus(void) {}
41#endif
42
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -040043static int __cpuidle_register_device(struct cpuidle_device *dev);
44
Len Brown4f86d3a2007-10-03 18:58:00 -040045/**
46 * cpuidle_idle_call - the main idle loop
47 *
48 * NOTE: no locks or semaphores should be used here
49 */
50static void cpuidle_idle_call(void)
51{
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -060052 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040053 struct cpuidle_state *target_state;
54 int next_state;
55
56 /* check if the device is ready */
57 if (!dev || !dev->enabled) {
58 if (pm_idle_old)
59 pm_idle_old();
60 else
Venkatesh Pallipadi89cedfe2008-10-16 19:00:08 -040061#if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
62 default_idle();
63#else
Len Brown4f86d3a2007-10-03 18:58:00 -040064 local_irq_enable();
Venkatesh Pallipadi89cedfe2008-10-16 19:00:08 -040065#endif
Len Brown4f86d3a2007-10-03 18:58:00 -040066 return;
67 }
68
Arjan van de Ven9a655832008-11-09 12:45:10 -080069#if 0
70 /* shows regressions, re-enable for 2.6.29 */
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -070071 /*
72 * run any timers that can be run now, at this point
73 * before calculating the idle duration etc.
74 */
75 hrtimer_peek_ahead_timers();
Arjan van de Ven9a655832008-11-09 12:45:10 -080076#endif
Ai Li71abbbf2010-08-09 17:20:13 -070077
78 /*
79 * Call the device's prepare function before calling the
80 * governor's select function. ->prepare gives the device's
81 * cpuidle driver a chance to update any dynamic information
82 * of its cpuidle states for the current idle period, e.g.
83 * state availability, latencies, residencies, etc.
84 */
85 if (dev->prepare)
86 dev->prepare(dev);
87
Len Brown4f86d3a2007-10-03 18:58:00 -040088 /* ask the governor for the next state */
89 next_state = cpuidle_curr_governor->select(dev);
Kevin Hilman246eb7f2009-10-26 16:50:18 -070090 if (need_resched()) {
91 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -040092 return;
Kevin Hilman246eb7f2009-10-26 16:50:18 -070093 }
94
Len Brown4f86d3a2007-10-03 18:58:00 -040095 target_state = &dev->states[next_state];
96
97 /* enter the state and update stats */
Len Brown4f86d3a2007-10-03 18:58:00 -040098 dev->last_state = target_state;
Venkatesh Pallipadi887e3012008-09-29 15:24:27 -070099 dev->last_residency = target_state->enter(dev, target_state);
100 if (dev->last_state)
101 target_state = dev->last_state;
102
Yi Yang8b78cf62008-02-25 08:46:12 +0800103 target_state->time += (unsigned long long)dev->last_residency;
Len Brown4f86d3a2007-10-03 18:58:00 -0400104 target_state->usage++;
105
106 /* give the governor an opportunity to reflect on the outcome */
107 if (cpuidle_curr_governor->reflect)
108 cpuidle_curr_governor->reflect(dev);
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200109 trace_power_end(smp_processor_id());
Thomas Renninger25e41932011-01-03 17:50:44 +0100110 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
Len Brown4f86d3a2007-10-03 18:58:00 -0400111}
112
113/**
114 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
115 */
116void cpuidle_install_idle_handler(void)
117{
118 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
119 /* Make sure all changes finished before we switch to new idle */
120 smp_wmb();
121 pm_idle = cpuidle_idle_call;
122 }
123}
124
125/**
126 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
127 */
128void cpuidle_uninstall_idle_handler(void)
129{
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +0200130 if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400131 pm_idle = pm_idle_old;
Venki Pallipadia6869cc2008-02-08 17:05:44 -0800132 cpuidle_kick_cpus();
Len Brown4f86d3a2007-10-03 18:58:00 -0400133 }
134}
135
136/**
137 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
138 */
139void cpuidle_pause_and_lock(void)
140{
141 mutex_lock(&cpuidle_lock);
142 cpuidle_uninstall_idle_handler();
143}
144
145EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
146
147/**
148 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
149 */
150void cpuidle_resume_and_unlock(void)
151{
152 cpuidle_install_idle_handler();
153 mutex_unlock(&cpuidle_lock);
154}
155
156EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
157
158/**
159 * cpuidle_enable_device - enables idle PM for a CPU
160 * @dev: the CPU
161 *
162 * This function must be called between cpuidle_pause_and_lock and
163 * cpuidle_resume_and_unlock when used externally.
164 */
165int cpuidle_enable_device(struct cpuidle_device *dev)
166{
167 int ret, i;
168
169 if (dev->enabled)
170 return 0;
Len Brown752138d2010-05-22 16:57:26 -0400171 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400172 return -EIO;
173 if (!dev->state_count)
174 return -EINVAL;
175
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400176 if (dev->registered == 0) {
177 ret = __cpuidle_register_device(dev);
178 if (ret)
179 return ret;
180 }
181
Len Brown4f86d3a2007-10-03 18:58:00 -0400182 if ((ret = cpuidle_add_state_sysfs(dev)))
183 return ret;
184
185 if (cpuidle_curr_governor->enable &&
186 (ret = cpuidle_curr_governor->enable(dev)))
187 goto fail_sysfs;
188
189 for (i = 0; i < dev->state_count; i++) {
190 dev->states[i].usage = 0;
191 dev->states[i].time = 0;
192 }
193 dev->last_residency = 0;
194 dev->last_state = NULL;
195
196 smp_wmb();
197
198 dev->enabled = 1;
199
200 enabled_devices++;
201 return 0;
202
203fail_sysfs:
204 cpuidle_remove_state_sysfs(dev);
205
206 return ret;
207}
208
209EXPORT_SYMBOL_GPL(cpuidle_enable_device);
210
211/**
212 * cpuidle_disable_device - disables idle PM for a CPU
213 * @dev: the CPU
214 *
215 * This function must be called between cpuidle_pause_and_lock and
216 * cpuidle_resume_and_unlock when used externally.
217 */
218void cpuidle_disable_device(struct cpuidle_device *dev)
219{
220 if (!dev->enabled)
221 return;
Len Brown752138d2010-05-22 16:57:26 -0400222 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
Len Brown4f86d3a2007-10-03 18:58:00 -0400223 return;
224
225 dev->enabled = 0;
226
227 if (cpuidle_curr_governor->disable)
228 cpuidle_curr_governor->disable(dev);
229
230 cpuidle_remove_state_sysfs(dev);
231 enabled_devices--;
232}
233
234EXPORT_SYMBOL_GPL(cpuidle_disable_device);
235
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800236#ifdef CONFIG_ARCH_HAS_CPU_RELAX
237static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
238{
239 ktime_t t1, t2;
240 s64 diff;
241 int ret;
242
243 t1 = ktime_get();
244 local_irq_enable();
245 while (!need_resched())
246 cpu_relax();
247
248 t2 = ktime_get();
249 diff = ktime_to_us(ktime_sub(t2, t1));
250 if (diff > INT_MAX)
251 diff = INT_MAX;
252
253 ret = (int) diff;
254 return ret;
255}
256
257static void poll_idle_init(struct cpuidle_device *dev)
258{
259 struct cpuidle_state *state = &dev->states[0];
260
261 cpuidle_set_statedata(state, NULL);
262
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800263 snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
264 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800265 state->exit_latency = 0;
266 state->target_residency = 0;
267 state->power_usage = -1;
Venki Pallipadi8e92b662008-02-29 10:24:32 -0800268 state->flags = CPUIDLE_FLAG_POLL;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800269 state->enter = poll_idle;
270}
271#else
272static void poll_idle_init(struct cpuidle_device *dev) {}
273#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
274
Len Brown4f86d3a2007-10-03 18:58:00 -0400275/**
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400276 * __cpuidle_register_device - internal register function called before register
277 * and enable routines
Len Brown4f86d3a2007-10-03 18:58:00 -0400278 * @dev: the cpu
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400279 *
280 * cpuidle_lock mutex must be held before this is called
Len Brown4f86d3a2007-10-03 18:58:00 -0400281 */
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400282static int __cpuidle_register_device(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400283{
284 int ret;
285 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400286 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400287
288 if (!sys_dev)
289 return -EINVAL;
Len Brown752138d2010-05-22 16:57:26 -0400290 if (!try_module_get(cpuidle_driver->owner))
Len Brown4f86d3a2007-10-03 18:58:00 -0400291 return -EINVAL;
292
293 init_completion(&dev->kobj_unregister);
294
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800295 poll_idle_init(dev);
296
Ai Li71abbbf2010-08-09 17:20:13 -0700297 /*
298 * cpuidle driver should set the dev->power_specified bit
299 * before registering the device if the driver provides
300 * power_usage numbers.
301 *
302 * For those devices whose ->power_specified is not set,
303 * we fill in power_usage with decreasing values as the
304 * cpuidle code has an implicit assumption that state Cn
305 * uses less power than C(n-1).
306 *
307 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
308 * an power value of -1. So we use -2, -3, etc, for other
309 * c-states.
310 */
311 if (!dev->power_specified) {
312 int i;
313 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
314 dev->states[i].power_usage = -1 - i;
315 }
316
Len Brown4f86d3a2007-10-03 18:58:00 -0400317 per_cpu(cpuidle_devices, dev->cpu) = dev;
318 list_add(&dev->device_list, &cpuidle_detected_devices);
319 if ((ret = cpuidle_add_sysfs(sys_dev))) {
Len Brown752138d2010-05-22 16:57:26 -0400320 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400321 return ret;
322 }
323
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400324 dev->registered = 1;
325 return 0;
326}
327
328/**
329 * cpuidle_register_device - registers a CPU's idle PM feature
330 * @dev: the cpu
331 */
332int cpuidle_register_device(struct cpuidle_device *dev)
333{
334 int ret;
335
336 mutex_lock(&cpuidle_lock);
337
338 if ((ret = __cpuidle_register_device(dev))) {
339 mutex_unlock(&cpuidle_lock);
340 return ret;
341 }
342
Len Brown4f86d3a2007-10-03 18:58:00 -0400343 cpuidle_enable_device(dev);
344 cpuidle_install_idle_handler();
345
346 mutex_unlock(&cpuidle_lock);
347
348 return 0;
349
350}
351
352EXPORT_SYMBOL_GPL(cpuidle_register_device);
353
354/**
355 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
356 * @dev: the cpu
357 */
358void cpuidle_unregister_device(struct cpuidle_device *dev)
359{
360 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
Len Brown752138d2010-05-22 16:57:26 -0400361 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -0400362
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400363 if (dev->registered == 0)
364 return;
365
Len Brown4f86d3a2007-10-03 18:58:00 -0400366 cpuidle_pause_and_lock();
367
368 cpuidle_disable_device(dev);
369
370 cpuidle_remove_sysfs(sys_dev);
371 list_del(&dev->device_list);
372 wait_for_completion(&dev->kobj_unregister);
373 per_cpu(cpuidle_devices, dev->cpu) = NULL;
374
375 cpuidle_resume_and_unlock();
376
Len Brown752138d2010-05-22 16:57:26 -0400377 module_put(cpuidle_driver->owner);
Len Brown4f86d3a2007-10-03 18:58:00 -0400378}
379
380EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
381
382#ifdef CONFIG_SMP
383
384static void smp_callback(void *v)
385{
386 /* we already woke the CPU up, nothing more to do */
387}
388
389/*
390 * This function gets called when a part of the kernel has a new latency
391 * requirement. This means we need to get all processors out of their C-state,
392 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
393 * wakes them all right up.
394 */
395static int cpuidle_latency_notify(struct notifier_block *b,
396 unsigned long l, void *v)
397{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200398 smp_call_function(smp_callback, NULL, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400399 return NOTIFY_OK;
400}
401
402static struct notifier_block cpuidle_latency_notifier = {
403 .notifier_call = cpuidle_latency_notify,
404};
405
Mark Grossd82b3512008-02-04 22:30:08 -0800406static inline void latency_notifier_init(struct notifier_block *n)
407{
408 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
409}
Len Brown4f86d3a2007-10-03 18:58:00 -0400410
411#else /* CONFIG_SMP */
412
413#define latency_notifier_init(x) do { } while (0)
414
415#endif /* CONFIG_SMP */
416
417/**
418 * cpuidle_init - core initializer
419 */
420static int __init cpuidle_init(void)
421{
422 int ret;
423
424 pm_idle_old = pm_idle;
425
426 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
427 if (ret)
428 return ret;
429
430 latency_notifier_init(&cpuidle_latency_notifier);
431
432 return 0;
433}
434
435core_initcall(cpuidle_init);