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/kernel.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h>
14 #include <linux/notifier.h>
15 #include <linux/pm_qos.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/ktime.h>
19 #include <linux/hrtimer.h>
20 #include <linux/module.h>
21 #include <trace/events/power.h>
25 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
27 DEFINE_MUTEX(cpuidle_lock);
28 LIST_HEAD(cpuidle_detected_devices);
30 static int enabled_devices;
31 static int off __read_mostly;
32 static int initialized __read_mostly;
34 int cpuidle_disabled(void)
38 void disable_cpuidle(void)
43 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
44 static void cpuidle_kick_cpus(void)
48 #elif defined(CONFIG_SMP)
49 # error "Arch needs cpu_idle_wait() equivalent here"
50 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
51 static void cpuidle_kick_cpus(void) {}
54 static int __cpuidle_register_device(struct cpuidle_device *dev);
56 static inline int cpuidle_enter(struct cpuidle_device *dev,
57 struct cpuidle_driver *drv, int index)
59 struct cpuidle_state *target_state = &drv->states[index];
60 return target_state->enter(dev, drv, index);
63 static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
64 struct cpuidle_driver *drv, int index)
66 return cpuidle_wrap_enter(dev, drv, index, cpuidle_enter);
69 typedef int (*cpuidle_enter_t)(struct cpuidle_device *dev,
70 struct cpuidle_driver *drv, int index);
72 static cpuidle_enter_t cpuidle_enter_ops;
75 * cpuidle_idle_call - the main idle loop
77 * NOTE: no locks or semaphores should be used here
78 * return non-zero on failure
80 int cpuidle_idle_call(void)
82 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
83 struct cpuidle_driver *drv = cpuidle_get_driver();
84 int next_state, entered_state;
92 /* check if the device is ready */
93 if (!dev || !dev->enabled)
97 /* shows regressions, re-enable for 2.6.29 */
99 * run any timers that can be run now, at this point
100 * before calculating the idle duration etc.
102 hrtimer_peek_ahead_timers();
105 /* ask the governor for the next state */
106 next_state = cpuidle_curr_governor->select(drv, dev);
107 if (need_resched()) {
112 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
113 trace_cpu_idle(next_state, dev->cpu);
115 entered_state = cpuidle_enter_ops(dev, drv, next_state);
117 trace_power_end(dev->cpu);
118 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
120 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.
125 dev->states_usage[entered_state].time +=
126 (unsigned long long)dev->last_residency;
127 dev->states_usage[entered_state].usage++;
129 dev->last_residency = 0;
132 /* give the governor an opportunity to reflect on the outcome */
133 if (cpuidle_curr_governor->reflect)
134 cpuidle_curr_governor->reflect(dev, entered_state);
140 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
142 void cpuidle_install_idle_handler(void)
144 if (enabled_devices) {
145 /* Make sure all changes finished before we switch to new idle */
152 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
154 void cpuidle_uninstall_idle_handler(void)
156 if (enabled_devices) {
163 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
165 void cpuidle_pause_and_lock(void)
167 mutex_lock(&cpuidle_lock);
168 cpuidle_uninstall_idle_handler();
171 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
174 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
176 void cpuidle_resume_and_unlock(void)
178 cpuidle_install_idle_handler();
179 mutex_unlock(&cpuidle_lock);
182 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
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.
190 int 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))
195 ktime_t time_start, time_end;
198 time_start = ktime_get();
200 index = enter(dev, drv, index);
202 time_end = ktime_get();
206 diff = ktime_to_us(ktime_sub(time_end, time_start));
210 dev->last_residency = (int) diff;
215 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
216 static int poll_idle(struct cpuidle_device *dev,
217 struct cpuidle_driver *drv, int index)
224 while (!need_resched())
228 diff = ktime_to_us(ktime_sub(t2, t1));
232 dev->last_residency = (int) diff;
237 static void poll_idle_init(struct cpuidle_driver *drv)
239 struct cpuidle_state *state = &drv->states[0];
241 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
242 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;
247 state->enter = poll_idle;
251 static void poll_idle_init(struct cpuidle_driver *drv) {}
252 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
255 * cpuidle_enable_device - enables idle PM for a CPU
258 * This function must be called between cpuidle_pause_and_lock and
259 * cpuidle_resume_and_unlock when used externally.
261 int cpuidle_enable_device(struct cpuidle_device *dev)
264 struct cpuidle_driver *drv = cpuidle_get_driver();
268 if (!drv || !cpuidle_curr_governor)
270 if (!dev->state_count)
271 dev->state_count = drv->state_count;
273 if (dev->registered == 0) {
274 ret = __cpuidle_register_device(dev);
279 cpuidle_enter_ops = drv->en_core_tk_irqen ?
280 cpuidle_enter_tk : cpuidle_enter;
284 if ((ret = cpuidle_add_state_sysfs(dev)))
287 if (cpuidle_curr_governor->enable &&
288 (ret = cpuidle_curr_governor->enable(drv, dev)))
291 for (i = 0; i < dev->state_count; i++) {
292 dev->states_usage[i].usage = 0;
293 dev->states_usage[i].time = 0;
295 dev->last_residency = 0;
305 cpuidle_remove_state_sysfs(dev);
310 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
313 * cpuidle_disable_device - disables idle PM for a CPU
316 * This function must be called between cpuidle_pause_and_lock and
317 * cpuidle_resume_and_unlock when used externally.
319 void cpuidle_disable_device(struct cpuidle_device *dev)
323 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
328 if (cpuidle_curr_governor->disable)
329 cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
331 cpuidle_remove_state_sysfs(dev);
335 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
338 * __cpuidle_register_device - internal register function called before register
339 * and enable routines
342 * cpuidle_lock mutex must be held before this is called
344 static int __cpuidle_register_device(struct cpuidle_device *dev)
347 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
348 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
352 if (!try_module_get(cpuidle_driver->owner))
355 init_completion(&dev->kobj_unregister);
357 per_cpu(cpuidle_devices, dev->cpu) = dev;
358 list_add(&dev->device_list, &cpuidle_detected_devices);
359 if ((ret = cpuidle_add_sysfs(cpu_dev))) {
360 module_put(cpuidle_driver->owner);
369 * cpuidle_register_device - registers a CPU's idle PM feature
372 int cpuidle_register_device(struct cpuidle_device *dev)
376 mutex_lock(&cpuidle_lock);
378 if ((ret = __cpuidle_register_device(dev))) {
379 mutex_unlock(&cpuidle_lock);
383 cpuidle_enable_device(dev);
384 cpuidle_install_idle_handler();
386 mutex_unlock(&cpuidle_lock);
392 EXPORT_SYMBOL_GPL(cpuidle_register_device);
395 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
398 void cpuidle_unregister_device(struct cpuidle_device *dev)
400 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
401 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
403 if (dev->registered == 0)
406 cpuidle_pause_and_lock();
408 cpuidle_disable_device(dev);
410 cpuidle_remove_sysfs(cpu_dev);
411 list_del(&dev->device_list);
412 wait_for_completion(&dev->kobj_unregister);
413 per_cpu(cpuidle_devices, dev->cpu) = NULL;
415 cpuidle_resume_and_unlock();
417 module_put(cpuidle_driver->owner);
420 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
424 static void smp_callback(void *v)
426 /* we already woke the CPU up, nothing more to do */
430 * This function gets called when a part of the kernel has a new latency
431 * requirement. This means we need to get all processors out of their C-state,
432 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
433 * wakes them all right up.
435 static int cpuidle_latency_notify(struct notifier_block *b,
436 unsigned long l, void *v)
438 smp_call_function(smp_callback, NULL, 1);
442 static struct notifier_block cpuidle_latency_notifier = {
443 .notifier_call = cpuidle_latency_notify,
446 static inline void latency_notifier_init(struct notifier_block *n)
448 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
451 #else /* CONFIG_SMP */
453 #define latency_notifier_init(x) do { } while (0)
455 #endif /* CONFIG_SMP */
458 * cpuidle_init - core initializer
460 static int __init cpuidle_init(void)
464 if (cpuidle_disabled())
467 ret = cpuidle_add_interface(cpu_subsys.dev_root);
471 latency_notifier_init(&cpuidle_latency_notifier);
476 module_param(off, int, 0444);
477 core_initcall(cpuidle_init);