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_params.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/ktime.h>
19 #include <linux/hrtimer.h>
20 #include <trace/events/power.h>
24 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
26 DEFINE_MUTEX(cpuidle_lock);
27 LIST_HEAD(cpuidle_detected_devices);
29 static int enabled_devices;
30 static int off __read_mostly;
31 static int initialized __read_mostly;
33 int cpuidle_disabled(void)
37 void disable_cpuidle(void)
42 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
43 static void cpuidle_kick_cpus(void)
47 #elif defined(CONFIG_SMP)
48 # error "Arch needs cpu_idle_wait() equivalent here"
49 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
50 static void cpuidle_kick_cpus(void) {}
53 static int __cpuidle_register_device(struct cpuidle_device *dev);
56 * cpuidle_idle_call - the main idle loop
58 * NOTE: no locks or semaphores should be used here
59 * return non-zero on failure
61 int cpuidle_idle_call(void)
63 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
64 struct cpuidle_driver *drv = cpuidle_get_driver();
65 struct cpuidle_state *target_state;
66 int next_state, entered_state;
74 /* check if the device is ready */
75 if (!dev || !dev->enabled)
79 /* shows regressions, re-enable for 2.6.29 */
81 * run any timers that can be run now, at this point
82 * before calculating the idle duration etc.
84 hrtimer_peek_ahead_timers();
87 /* ask the governor for the next state */
88 next_state = cpuidle_curr_governor->select(drv, dev);
94 target_state = &drv->states[next_state];
96 trace_power_start(POWER_CSTATE, next_state, dev->cpu);
97 trace_cpu_idle(next_state, dev->cpu);
99 entered_state = target_state->enter(dev, drv, next_state);
101 trace_power_end(dev->cpu);
102 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
104 if (entered_state >= 0) {
105 /* Update cpuidle counters */
106 /* This can be moved to within driver enter routine
107 * but that results in multiple copies of same code.
109 dev->states_usage[entered_state].time +=
110 (unsigned long long)dev->last_residency;
111 dev->states_usage[entered_state].usage++;
114 /* give the governor an opportunity to reflect on the outcome */
115 if (cpuidle_curr_governor->reflect)
116 cpuidle_curr_governor->reflect(dev, entered_state);
122 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
124 void cpuidle_install_idle_handler(void)
126 if (enabled_devices) {
127 /* Make sure all changes finished before we switch to new idle */
134 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
136 void cpuidle_uninstall_idle_handler(void)
138 if (enabled_devices) {
145 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
147 void cpuidle_pause_and_lock(void)
149 mutex_lock(&cpuidle_lock);
150 cpuidle_uninstall_idle_handler();
153 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
156 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
158 void cpuidle_resume_and_unlock(void)
160 cpuidle_install_idle_handler();
161 mutex_unlock(&cpuidle_lock);
164 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
166 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
167 static int poll_idle(struct cpuidle_device *dev,
168 struct cpuidle_driver *drv, int index)
175 while (!need_resched())
179 diff = ktime_to_us(ktime_sub(t2, t1));
183 dev->last_residency = (int) diff;
188 static void poll_idle_init(struct cpuidle_driver *drv)
190 struct cpuidle_state *state = &drv->states[0];
192 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
193 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
194 state->exit_latency = 0;
195 state->target_residency = 0;
196 state->power_usage = -1;
198 state->enter = poll_idle;
201 static void poll_idle_init(struct cpuidle_driver *drv) {}
202 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
205 * cpuidle_enable_device - enables idle PM for a CPU
208 * This function must be called between cpuidle_pause_and_lock and
209 * cpuidle_resume_and_unlock when used externally.
211 int cpuidle_enable_device(struct cpuidle_device *dev)
217 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
219 if (!dev->state_count)
222 if (dev->registered == 0) {
223 ret = __cpuidle_register_device(dev);
228 poll_idle_init(cpuidle_get_driver());
230 if ((ret = cpuidle_add_state_sysfs(dev)))
233 if (cpuidle_curr_governor->enable &&
234 (ret = cpuidle_curr_governor->enable(cpuidle_get_driver(), dev)))
237 for (i = 0; i < dev->state_count; i++) {
238 dev->states_usage[i].usage = 0;
239 dev->states_usage[i].time = 0;
241 dev->last_residency = 0;
251 cpuidle_remove_state_sysfs(dev);
256 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
259 * cpuidle_disable_device - disables idle PM for a CPU
262 * This function must be called between cpuidle_pause_and_lock and
263 * cpuidle_resume_and_unlock when used externally.
265 void cpuidle_disable_device(struct cpuidle_device *dev)
269 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
274 if (cpuidle_curr_governor->disable)
275 cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
277 cpuidle_remove_state_sysfs(dev);
281 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
284 * __cpuidle_register_device - internal register function called before register
285 * and enable routines
288 * cpuidle_lock mutex must be held before this is called
290 static int __cpuidle_register_device(struct cpuidle_device *dev)
293 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
294 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
298 if (!try_module_get(cpuidle_driver->owner))
301 init_completion(&dev->kobj_unregister);
303 per_cpu(cpuidle_devices, dev->cpu) = dev;
304 list_add(&dev->device_list, &cpuidle_detected_devices);
305 if ((ret = cpuidle_add_sysfs(sys_dev))) {
306 module_put(cpuidle_driver->owner);
315 * cpuidle_register_device - registers a CPU's idle PM feature
318 int cpuidle_register_device(struct cpuidle_device *dev)
322 mutex_lock(&cpuidle_lock);
324 if ((ret = __cpuidle_register_device(dev))) {
325 mutex_unlock(&cpuidle_lock);
329 cpuidle_enable_device(dev);
330 cpuidle_install_idle_handler();
332 mutex_unlock(&cpuidle_lock);
338 EXPORT_SYMBOL_GPL(cpuidle_register_device);
341 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
344 void cpuidle_unregister_device(struct cpuidle_device *dev)
346 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
347 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
349 if (dev->registered == 0)
352 cpuidle_pause_and_lock();
354 cpuidle_disable_device(dev);
356 cpuidle_remove_sysfs(sys_dev);
357 list_del(&dev->device_list);
358 wait_for_completion(&dev->kobj_unregister);
359 per_cpu(cpuidle_devices, dev->cpu) = NULL;
361 cpuidle_resume_and_unlock();
363 module_put(cpuidle_driver->owner);
366 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
370 static void smp_callback(void *v)
372 /* we already woke the CPU up, nothing more to do */
376 * This function gets called when a part of the kernel has a new latency
377 * requirement. This means we need to get all processors out of their C-state,
378 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
379 * wakes them all right up.
381 static int cpuidle_latency_notify(struct notifier_block *b,
382 unsigned long l, void *v)
384 smp_call_function(smp_callback, NULL, 1);
388 static struct notifier_block cpuidle_latency_notifier = {
389 .notifier_call = cpuidle_latency_notify,
392 static inline void latency_notifier_init(struct notifier_block *n)
394 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
397 #else /* CONFIG_SMP */
399 #define latency_notifier_init(x) do { } while (0)
401 #endif /* CONFIG_SMP */
404 * cpuidle_init - core initializer
406 static int __init cpuidle_init(void)
410 if (cpuidle_disabled())
413 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
417 latency_notifier_init(&cpuidle_latency_notifier);
422 module_param(off, int, 0444);
423 core_initcall(cpuidle_init);