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
);
28 static void (*pm_idle_old
)(void);
30 static int enabled_devices
;
32 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
33 static void cpuidle_kick_cpus(void)
37 #elif defined(CONFIG_SMP)
38 # error "Arch needs cpu_idle_wait() equivalent here"
39 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
40 static void cpuidle_kick_cpus(void) {}
43 static int __cpuidle_register_device(struct cpuidle_device
*dev
);
46 * cpuidle_idle_call - the main idle loop
48 * NOTE: no locks or semaphores should be used here
50 static void cpuidle_idle_call(void)
52 struct cpuidle_device
*dev
= __get_cpu_var(cpuidle_devices
);
53 struct cpuidle_state
*target_state
;
56 /* check if the device is ready */
57 if (!dev
|| !dev
->enabled
) {
61 #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
70 /* shows regressions, re-enable for 2.6.29 */
72 * run any timers that can be run now, at this point
73 * before calculating the idle duration etc.
75 hrtimer_peek_ahead_timers();
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.
88 /* ask the governor for the next state */
89 next_state
= cpuidle_curr_governor
->select(dev
);
95 target_state
= &dev
->states
[next_state
];
97 /* enter the state and update stats */
98 dev
->last_state
= target_state
;
99 dev
->last_residency
= target_state
->enter(dev
, target_state
);
101 target_state
= dev
->last_state
;
103 target_state
->time
+= (unsigned long long)dev
->last_residency
;
104 target_state
->usage
++;
106 /* give the governor an opportunity to reflect on the outcome */
107 if (cpuidle_curr_governor
->reflect
)
108 cpuidle_curr_governor
->reflect(dev
);
109 trace_power_end(smp_processor_id());
113 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
115 void cpuidle_install_idle_handler(void)
117 if (enabled_devices
&& (pm_idle
!= cpuidle_idle_call
)) {
118 /* Make sure all changes finished before we switch to new idle */
120 pm_idle
= cpuidle_idle_call
;
125 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
127 void cpuidle_uninstall_idle_handler(void)
129 if (enabled_devices
&& pm_idle_old
&& (pm_idle
!= pm_idle_old
)) {
130 pm_idle
= pm_idle_old
;
136 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
138 void cpuidle_pause_and_lock(void)
140 mutex_lock(&cpuidle_lock
);
141 cpuidle_uninstall_idle_handler();
144 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
147 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
149 void cpuidle_resume_and_unlock(void)
151 cpuidle_install_idle_handler();
152 mutex_unlock(&cpuidle_lock
);
155 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
158 * cpuidle_enable_device - enables idle PM for a CPU
161 * This function must be called between cpuidle_pause_and_lock and
162 * cpuidle_resume_and_unlock when used externally.
164 int cpuidle_enable_device(struct cpuidle_device
*dev
)
170 if (!cpuidle_get_driver() || !cpuidle_curr_governor
)
172 if (!dev
->state_count
)
175 if (dev
->registered
== 0) {
176 ret
= __cpuidle_register_device(dev
);
181 if ((ret
= cpuidle_add_state_sysfs(dev
)))
184 if (cpuidle_curr_governor
->enable
&&
185 (ret
= cpuidle_curr_governor
->enable(dev
)))
188 for (i
= 0; i
< dev
->state_count
; i
++) {
189 dev
->states
[i
].usage
= 0;
190 dev
->states
[i
].time
= 0;
192 dev
->last_residency
= 0;
193 dev
->last_state
= NULL
;
203 cpuidle_remove_state_sysfs(dev
);
208 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
211 * cpuidle_disable_device - disables idle PM for a CPU
214 * This function must be called between cpuidle_pause_and_lock and
215 * cpuidle_resume_and_unlock when used externally.
217 void cpuidle_disable_device(struct cpuidle_device
*dev
)
221 if (!cpuidle_get_driver() || !cpuidle_curr_governor
)
226 if (cpuidle_curr_governor
->disable
)
227 cpuidle_curr_governor
->disable(dev
);
229 cpuidle_remove_state_sysfs(dev
);
233 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
235 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
236 static int poll_idle(struct cpuidle_device
*dev
, struct cpuidle_state
*st
)
244 while (!need_resched())
248 diff
= ktime_to_us(ktime_sub(t2
, t1
));
256 static void poll_idle_init(struct cpuidle_device
*dev
)
258 struct cpuidle_state
*state
= &dev
->states
[0];
260 cpuidle_set_statedata(state
, NULL
);
262 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "C0");
263 snprintf(state
->desc
, CPUIDLE_DESC_LEN
, "CPUIDLE CORE POLL IDLE");
264 state
->exit_latency
= 0;
265 state
->target_residency
= 0;
266 state
->power_usage
= -1;
267 state
->flags
= CPUIDLE_FLAG_POLL
;
268 state
->enter
= poll_idle
;
271 static void poll_idle_init(struct cpuidle_device
*dev
) {}
272 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
275 * __cpuidle_register_device - internal register function called before register
276 * and enable routines
279 * cpuidle_lock mutex must be held before this is called
281 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
284 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
285 struct cpuidle_driver
*cpuidle_driver
= cpuidle_get_driver();
289 if (!try_module_get(cpuidle_driver
->owner
))
292 init_completion(&dev
->kobj_unregister
);
297 * cpuidle driver should set the dev->power_specified bit
298 * before registering the device if the driver provides
299 * power_usage numbers.
301 * For those devices whose ->power_specified is not set,
302 * we fill in power_usage with decreasing values as the
303 * cpuidle code has an implicit assumption that state Cn
304 * uses less power than C(n-1).
306 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
307 * an power value of -1. So we use -2, -3, etc, for other
310 if (!dev
->power_specified
) {
312 for (i
= CPUIDLE_DRIVER_STATE_START
; i
< dev
->state_count
; i
++)
313 dev
->states
[i
].power_usage
= -1 - i
;
316 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
317 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
318 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
319 module_put(cpuidle_driver
->owner
);
328 * cpuidle_register_device - registers a CPU's idle PM feature
331 int cpuidle_register_device(struct cpuidle_device
*dev
)
335 mutex_lock(&cpuidle_lock
);
337 if ((ret
= __cpuidle_register_device(dev
))) {
338 mutex_unlock(&cpuidle_lock
);
342 cpuidle_enable_device(dev
);
343 cpuidle_install_idle_handler();
345 mutex_unlock(&cpuidle_lock
);
351 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
354 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
357 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
359 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
360 struct cpuidle_driver
*cpuidle_driver
= cpuidle_get_driver();
362 if (dev
->registered
== 0)
365 cpuidle_pause_and_lock();
367 cpuidle_disable_device(dev
);
369 cpuidle_remove_sysfs(sys_dev
);
370 list_del(&dev
->device_list
);
371 wait_for_completion(&dev
->kobj_unregister
);
372 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
374 cpuidle_resume_and_unlock();
376 module_put(cpuidle_driver
->owner
);
379 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
383 static void smp_callback(void *v
)
385 /* we already woke the CPU up, nothing more to do */
389 * This function gets called when a part of the kernel has a new latency
390 * requirement. This means we need to get all processors out of their C-state,
391 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
392 * wakes them all right up.
394 static int cpuidle_latency_notify(struct notifier_block
*b
,
395 unsigned long l
, void *v
)
397 smp_call_function(smp_callback
, NULL
, 1);
401 static struct notifier_block cpuidle_latency_notifier
= {
402 .notifier_call
= cpuidle_latency_notify
,
405 static inline void latency_notifier_init(struct notifier_block
*n
)
407 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
410 #else /* CONFIG_SMP */
412 #define latency_notifier_init(x) do { } while (0)
414 #endif /* CONFIG_SMP */
417 * cpuidle_init - core initializer
419 static int __init
cpuidle_init(void)
423 pm_idle_old
= pm_idle
;
425 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
429 latency_notifier_init(&cpuidle_latency_notifier
);
434 core_initcall(cpuidle_init
);