2 * cpuidle.c - core cpuidle infrastructure
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
8 * This code is licenced under the GPL.
11 #include <linux/clockchips.h>
12 #include <linux/kernel.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/notifier.h>
16 #include <linux/pm_qos.h>
17 #include <linux/cpu.h>
18 #include <linux/cpuidle.h>
19 #include <linux/ktime.h>
20 #include <linux/hrtimer.h>
21 #include <linux/module.h>
22 #include <trace/events/power.h>
26 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
28 DEFINE_MUTEX(cpuidle_lock);
29 LIST_HEAD(cpuidle_detected_devices);
31 static int enabled_devices;
32 static int off __read_mostly;
33 static int initialized __read_mostly;
35 int cpuidle_disabled(void)
39 void disable_cpuidle(void)
44 static int __cpuidle_register_device(struct cpuidle_device *dev);
47 * cpuidle_play_dead - cpu off-lining
49 * Returns in case of an error or no driver
51 int cpuidle_play_dead(void)
53 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
54 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
60 /* Find lowest-power state that supports long-term idle */
61 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);
69 * 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
74 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
79 struct cpuidle_state *target_state = &drv->states[index];
80 ktime_t time_start, time_end;
83 time_start = ktime_get();
85 entered_state = target_state->enter(dev, drv, index);
87 time_end = ktime_get();
91 diff = ktime_to_us(ktime_sub(time_end, time_start));
95 dev->last_residency = (int) diff;
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.
102 dev->states_usage[entered_state].time += dev->last_residency;
103 dev->states_usage[entered_state].usage++;
105 dev->last_residency = 0;
108 return entered_state;
112 * cpuidle_idle_call - the main idle loop
114 * NOTE: no locks or semaphores should be used here
115 * return non-zero on failure
117 int cpuidle_idle_call(void)
119 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
120 struct cpuidle_driver *drv;
121 int next_state, entered_state;
129 /* check if the device is ready */
130 if (!dev || !dev->enabled)
133 drv = cpuidle_get_cpu_driver(dev);
135 /* ask the governor for the next state */
136 next_state = cpuidle_curr_governor->select(drv, dev);
137 if (need_resched()) {
138 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);
146 trace_cpu_idle_rcuidle(next_state, dev->cpu);
148 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
149 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
152 if (cpuidle_state_is_coupled(dev, drv, next_state))
153 entered_state = cpuidle_enter_state_coupled(dev, drv,
156 entered_state = cpuidle_enter_state(dev, drv, next_state);
158 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
159 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
162 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
164 /* give the governor an opportunity to reflect on the outcome */
165 if (cpuidle_curr_governor->reflect)
166 cpuidle_curr_governor->reflect(dev, entered_state);
172 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
174 void cpuidle_install_idle_handler(void)
176 if (enabled_devices) {
177 /* Make sure all changes finished before we switch to new idle */
184 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
186 void cpuidle_uninstall_idle_handler(void)
188 if (enabled_devices) {
190 kick_all_cpus_sync();
195 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
197 void cpuidle_pause_and_lock(void)
199 mutex_lock(&cpuidle_lock);
200 cpuidle_uninstall_idle_handler();
203 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
206 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
208 void cpuidle_resume_and_unlock(void)
210 cpuidle_install_idle_handler();
211 mutex_unlock(&cpuidle_lock);
214 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
216 /* Currently used in suspend/resume path to suspend cpuidle */
217 void cpuidle_pause(void)
219 mutex_lock(&cpuidle_lock);
220 cpuidle_uninstall_idle_handler();
221 mutex_unlock(&cpuidle_lock);
224 /* Currently used in suspend/resume path to resume cpuidle */
225 void cpuidle_resume(void)
227 mutex_lock(&cpuidle_lock);
228 cpuidle_install_idle_handler();
229 mutex_unlock(&cpuidle_lock);
232 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
233 static int poll_idle(struct cpuidle_device *dev,
234 struct cpuidle_driver *drv, int index)
241 while (!need_resched())
245 diff = ktime_to_us(ktime_sub(t2, t1));
249 dev->last_residency = (int) diff;
254 static void poll_idle_init(struct cpuidle_driver *drv)
256 struct cpuidle_state *state = &drv->states[0];
258 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
259 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;
264 state->enter = poll_idle;
265 state->disabled = false;
268 static void poll_idle_init(struct cpuidle_driver *drv) {}
269 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
272 * cpuidle_enable_device - enables idle PM for a CPU
275 * This function must be called between cpuidle_pause_and_lock and
276 * cpuidle_resume_and_unlock when used externally.
278 int cpuidle_enable_device(struct cpuidle_device *dev)
281 struct cpuidle_driver *drv;
289 drv = cpuidle_get_cpu_driver(dev);
291 if (!drv || !cpuidle_curr_governor)
294 if (!dev->state_count)
295 dev->state_count = drv->state_count;
297 if (dev->registered == 0) {
298 ret = __cpuidle_register_device(dev);
305 ret = cpuidle_add_device_sysfs(dev);
309 if (cpuidle_curr_governor->enable &&
310 (ret = cpuidle_curr_governor->enable(drv, dev)))
313 for (i = 0; i < dev->state_count; i++) {
314 dev->states_usage[i].usage = 0;
315 dev->states_usage[i].time = 0;
317 dev->last_residency = 0;
327 cpuidle_remove_device_sysfs(dev);
332 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
335 * cpuidle_disable_device - disables idle PM for a CPU
338 * This function must be called between cpuidle_pause_and_lock and
339 * cpuidle_resume_and_unlock when used externally.
341 void cpuidle_disable_device(struct cpuidle_device *dev)
343 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
345 if (!dev || !dev->enabled)
348 if (!drv || !cpuidle_curr_governor)
353 if (cpuidle_curr_governor->disable)
354 cpuidle_curr_governor->disable(drv, dev);
356 cpuidle_remove_device_sysfs(dev);
360 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
363 * __cpuidle_register_device - internal register function called before register
364 * and enable routines
367 * cpuidle_lock mutex must be held before this is called
369 static int __cpuidle_register_device(struct cpuidle_device *dev)
372 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
374 if (!try_module_get(drv->owner))
377 per_cpu(cpuidle_devices, dev->cpu) = dev;
378 list_add(&dev->device_list, &cpuidle_detected_devices);
379 ret = cpuidle_add_sysfs(dev);
383 ret = cpuidle_coupled_register_device(dev);
391 cpuidle_remove_sysfs(dev);
393 list_del(&dev->device_list);
394 per_cpu(cpuidle_devices, dev->cpu) = NULL;
395 module_put(drv->owner);
400 * cpuidle_register_device - registers a CPU's idle PM feature
403 int cpuidle_register_device(struct cpuidle_device *dev)
410 mutex_lock(&cpuidle_lock);
412 if ((ret = __cpuidle_register_device(dev))) {
413 mutex_unlock(&cpuidle_lock);
417 cpuidle_enable_device(dev);
418 cpuidle_install_idle_handler();
420 mutex_unlock(&cpuidle_lock);
426 EXPORT_SYMBOL_GPL(cpuidle_register_device);
429 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
432 void cpuidle_unregister_device(struct cpuidle_device *dev)
434 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
436 if (dev->registered == 0)
439 cpuidle_pause_and_lock();
441 cpuidle_disable_device(dev);
443 cpuidle_remove_sysfs(dev);
444 list_del(&dev->device_list);
445 per_cpu(cpuidle_devices, dev->cpu) = NULL;
447 cpuidle_coupled_unregister_device(dev);
449 cpuidle_resume_and_unlock();
451 module_put(drv->owner);
454 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
458 static void smp_callback(void *v)
460 /* we already woke the CPU up, nothing more to do */
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.
469 static int cpuidle_latency_notify(struct notifier_block *b,
470 unsigned long l, void *v)
472 smp_call_function(smp_callback, NULL, 1);
476 static struct notifier_block cpuidle_latency_notifier = {
477 .notifier_call = cpuidle_latency_notify,
480 static inline void latency_notifier_init(struct notifier_block *n)
482 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
485 #else /* CONFIG_SMP */
487 #define latency_notifier_init(x) do { } while (0)
489 #endif /* CONFIG_SMP */
492 * cpuidle_init - core initializer
494 static int __init cpuidle_init(void)
498 if (cpuidle_disabled())
501 ret = cpuidle_add_interface(cpu_subsys.dev_root);
505 latency_notifier_init(&cpuidle_latency_notifier);
510 module_param(off, int, 0444);
511 core_initcall(cpuidle_init);