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
);
57 * cpuidle_idle_call - the main idle loop
59 * NOTE: no locks or semaphores should be used here
60 * return non-zero on failure
62 int cpuidle_idle_call(void)
64 struct cpuidle_device
*dev
= __this_cpu_read(cpuidle_devices
);
65 struct cpuidle_driver
*drv
= cpuidle_get_driver();
66 struct cpuidle_state
*target_state
;
67 int next_state
, entered_state
;
75 /* check if the device is ready */
76 if (!dev
|| !dev
->enabled
)
80 /* shows regressions, re-enable for 2.6.29 */
82 * run any timers that can be run now, at this point
83 * before calculating the idle duration etc.
85 hrtimer_peek_ahead_timers();
88 /* ask the governor for the next state */
89 next_state
= cpuidle_curr_governor
->select(drv
, dev
);
95 target_state
= &drv
->states
[next_state
];
97 trace_power_start(POWER_CSTATE
, next_state
, dev
->cpu
);
98 trace_cpu_idle(next_state
, dev
->cpu
);
100 entered_state
= target_state
->enter(dev
, drv
, next_state
);
102 trace_power_end(dev
->cpu
);
103 trace_cpu_idle(PWR_EVENT_EXIT
, dev
->cpu
);
105 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.
110 dev
->states_usage
[entered_state
].time
+=
111 (unsigned long long)dev
->last_residency
;
112 dev
->states_usage
[entered_state
].usage
++;
115 /* give the governor an opportunity to reflect on the outcome */
116 if (cpuidle_curr_governor
->reflect
)
117 cpuidle_curr_governor
->reflect(dev
, entered_state
);
123 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
125 void cpuidle_install_idle_handler(void)
127 if (enabled_devices
) {
128 /* Make sure all changes finished before we switch to new idle */
135 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
137 void cpuidle_uninstall_idle_handler(void)
139 if (enabled_devices
) {
146 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
148 void cpuidle_pause_and_lock(void)
150 mutex_lock(&cpuidle_lock
);
151 cpuidle_uninstall_idle_handler();
154 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
157 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
159 void cpuidle_resume_and_unlock(void)
161 cpuidle_install_idle_handler();
162 mutex_unlock(&cpuidle_lock
);
165 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
167 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
168 static int poll_idle(struct cpuidle_device
*dev
,
169 struct cpuidle_driver
*drv
, int index
)
176 while (!need_resched())
180 diff
= ktime_to_us(ktime_sub(t2
, t1
));
184 dev
->last_residency
= (int) diff
;
189 static void poll_idle_init(struct cpuidle_driver
*drv
)
191 struct cpuidle_state
*state
= &drv
->states
[0];
193 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "POLL");
194 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;
199 state
->enter
= poll_idle
;
202 static void poll_idle_init(struct cpuidle_driver
*drv
) {}
203 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
206 * cpuidle_enable_device - enables idle PM for a CPU
209 * This function must be called between cpuidle_pause_and_lock and
210 * cpuidle_resume_and_unlock when used externally.
212 int cpuidle_enable_device(struct cpuidle_device
*dev
)
218 if (!cpuidle_get_driver() || !cpuidle_curr_governor
)
220 if (!dev
->state_count
)
223 if (dev
->registered
== 0) {
224 ret
= __cpuidle_register_device(dev
);
229 poll_idle_init(cpuidle_get_driver());
231 if ((ret
= cpuidle_add_state_sysfs(dev
)))
234 if (cpuidle_curr_governor
->enable
&&
235 (ret
= cpuidle_curr_governor
->enable(cpuidle_get_driver(), dev
)))
238 for (i
= 0; i
< dev
->state_count
; i
++) {
239 dev
->states_usage
[i
].usage
= 0;
240 dev
->states_usage
[i
].time
= 0;
242 dev
->last_residency
= 0;
252 cpuidle_remove_state_sysfs(dev
);
257 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
260 * cpuidle_disable_device - disables idle PM for a CPU
263 * This function must be called between cpuidle_pause_and_lock and
264 * cpuidle_resume_and_unlock when used externally.
266 void cpuidle_disable_device(struct cpuidle_device
*dev
)
270 if (!cpuidle_get_driver() || !cpuidle_curr_governor
)
275 if (cpuidle_curr_governor
->disable
)
276 cpuidle_curr_governor
->disable(cpuidle_get_driver(), dev
);
278 cpuidle_remove_state_sysfs(dev
);
282 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
285 * __cpuidle_register_device - internal register function called before register
286 * and enable routines
289 * cpuidle_lock mutex must be held before this is called
291 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
294 struct device
*cpu_dev
= get_cpu_device((unsigned long)dev
->cpu
);
295 struct cpuidle_driver
*cpuidle_driver
= cpuidle_get_driver();
299 if (!try_module_get(cpuidle_driver
->owner
))
302 init_completion(&dev
->kobj_unregister
);
304 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
305 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
306 if ((ret
= cpuidle_add_sysfs(cpu_dev
))) {
307 module_put(cpuidle_driver
->owner
);
316 * cpuidle_register_device - registers a CPU's idle PM feature
319 int cpuidle_register_device(struct cpuidle_device
*dev
)
323 mutex_lock(&cpuidle_lock
);
325 if ((ret
= __cpuidle_register_device(dev
))) {
326 mutex_unlock(&cpuidle_lock
);
330 cpuidle_enable_device(dev
);
331 cpuidle_install_idle_handler();
333 mutex_unlock(&cpuidle_lock
);
339 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
342 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
345 void cpuidle_unregister_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();
350 if (dev
->registered
== 0)
353 cpuidle_pause_and_lock();
355 cpuidle_disable_device(dev
);
357 cpuidle_remove_sysfs(cpu_dev
);
358 list_del(&dev
->device_list
);
359 wait_for_completion(&dev
->kobj_unregister
);
360 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
362 cpuidle_resume_and_unlock();
364 module_put(cpuidle_driver
->owner
);
367 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
371 static void smp_callback(void *v
)
373 /* we already woke the CPU up, nothing more to do */
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.
382 static int cpuidle_latency_notify(struct notifier_block
*b
,
383 unsigned long l
, void *v
)
385 smp_call_function(smp_callback
, NULL
, 1);
389 static struct notifier_block cpuidle_latency_notifier
= {
390 .notifier_call
= cpuidle_latency_notify
,
393 static inline void latency_notifier_init(struct notifier_block
*n
)
395 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
398 #else /* CONFIG_SMP */
400 #define latency_notifier_init(x) do { } while (0)
402 #endif /* CONFIG_SMP */
405 * cpuidle_init - core initializer
407 static int __init
cpuidle_init(void)
411 if (cpuidle_disabled())
414 ret
= cpuidle_add_interface(cpu_subsys
.dev_root
);
418 latency_notifier_init(&cpuidle_latency_notifier
);
423 module_param(off
, int, 0444);
424 core_initcall(cpuidle_init
);