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>
22 DEFINE_PER_CPU(struct cpuidle_device
*, cpuidle_devices
);
24 DEFINE_MUTEX(cpuidle_lock
);
25 LIST_HEAD(cpuidle_detected_devices
);
26 static void (*pm_idle_old
)(void);
28 static int enabled_devices
;
30 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
31 static void cpuidle_kick_cpus(void)
35 #elif defined(CONFIG_SMP)
36 # error "Arch needs cpu_idle_wait() equivalent here"
37 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
38 static void cpuidle_kick_cpus(void) {}
41 static int __cpuidle_register_device(struct cpuidle_device
*dev
);
44 * cpuidle_idle_call - the main idle loop
46 * NOTE: no locks or semaphores should be used here
48 static void cpuidle_idle_call(void)
50 struct cpuidle_device
*dev
= __get_cpu_var(cpuidle_devices
);
51 struct cpuidle_state
*target_state
;
54 /* check if the device is ready */
55 if (!dev
|| !dev
->enabled
) {
63 /* ask the governor for the next state */
64 next_state
= cpuidle_curr_governor
->select(dev
);
67 target_state
= &dev
->states
[next_state
];
69 /* enter the state and update stats */
70 dev
->last_residency
= target_state
->enter(dev
, target_state
);
71 dev
->last_state
= target_state
;
72 target_state
->time
+= (unsigned long long)dev
->last_residency
;
73 target_state
->usage
++;
75 /* give the governor an opportunity to reflect on the outcome */
76 if (cpuidle_curr_governor
->reflect
)
77 cpuidle_curr_governor
->reflect(dev
);
81 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
83 void cpuidle_install_idle_handler(void)
85 if (enabled_devices
&& (pm_idle
!= cpuidle_idle_call
)) {
86 /* Make sure all changes finished before we switch to new idle */
88 pm_idle
= cpuidle_idle_call
;
93 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
95 void cpuidle_uninstall_idle_handler(void)
97 if (enabled_devices
&& pm_idle_old
&& (pm_idle
!= pm_idle_old
)) {
98 pm_idle
= pm_idle_old
;
104 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
106 void cpuidle_pause_and_lock(void)
108 mutex_lock(&cpuidle_lock
);
109 cpuidle_uninstall_idle_handler();
112 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
115 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
117 void cpuidle_resume_and_unlock(void)
119 cpuidle_install_idle_handler();
120 mutex_unlock(&cpuidle_lock
);
123 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
126 * cpuidle_enable_device - enables idle PM for a CPU
129 * This function must be called between cpuidle_pause_and_lock and
130 * cpuidle_resume_and_unlock when used externally.
132 int cpuidle_enable_device(struct cpuidle_device
*dev
)
138 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
140 if (!dev
->state_count
)
143 if (dev
->registered
== 0) {
144 ret
= __cpuidle_register_device(dev
);
149 if ((ret
= cpuidle_add_state_sysfs(dev
)))
152 if (cpuidle_curr_governor
->enable
&&
153 (ret
= cpuidle_curr_governor
->enable(dev
)))
156 for (i
= 0; i
< dev
->state_count
; i
++) {
157 dev
->states
[i
].usage
= 0;
158 dev
->states
[i
].time
= 0;
160 dev
->last_residency
= 0;
161 dev
->last_state
= NULL
;
171 cpuidle_remove_state_sysfs(dev
);
176 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
179 * cpuidle_disable_device - disables idle PM for a CPU
182 * This function must be called between cpuidle_pause_and_lock and
183 * cpuidle_resume_and_unlock when used externally.
185 void cpuidle_disable_device(struct cpuidle_device
*dev
)
189 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
194 if (cpuidle_curr_governor
->disable
)
195 cpuidle_curr_governor
->disable(dev
);
197 cpuidle_remove_state_sysfs(dev
);
201 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
203 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
204 static int poll_idle(struct cpuidle_device
*dev
, struct cpuidle_state
*st
)
212 while (!need_resched())
216 diff
= ktime_to_us(ktime_sub(t2
, t1
));
224 static void poll_idle_init(struct cpuidle_device
*dev
)
226 struct cpuidle_state
*state
= &dev
->states
[0];
228 cpuidle_set_statedata(state
, NULL
);
230 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "C0");
231 snprintf(state
->desc
, CPUIDLE_DESC_LEN
, "CPUIDLE CORE POLL IDLE");
232 state
->exit_latency
= 0;
233 state
->target_residency
= 0;
234 state
->power_usage
= -1;
235 state
->flags
= CPUIDLE_FLAG_POLL
;
236 state
->enter
= poll_idle
;
239 static void poll_idle_init(struct cpuidle_device
*dev
) {}
240 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
243 * __cpuidle_register_device - internal register function called before register
244 * and enable routines
247 * cpuidle_lock mutex must be held before this is called
249 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
252 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
256 if (!try_module_get(cpuidle_curr_driver
->owner
))
259 init_completion(&dev
->kobj_unregister
);
263 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
264 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
265 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
266 module_put(cpuidle_curr_driver
->owner
);
275 * cpuidle_register_device - registers a CPU's idle PM feature
278 int cpuidle_register_device(struct cpuidle_device
*dev
)
282 mutex_lock(&cpuidle_lock
);
284 if ((ret
= __cpuidle_register_device(dev
))) {
285 mutex_unlock(&cpuidle_lock
);
289 cpuidle_enable_device(dev
);
290 cpuidle_install_idle_handler();
292 mutex_unlock(&cpuidle_lock
);
298 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
301 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
304 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
306 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
308 if (dev
->registered
== 0)
311 cpuidle_pause_and_lock();
313 cpuidle_disable_device(dev
);
315 cpuidle_remove_sysfs(sys_dev
);
316 list_del(&dev
->device_list
);
317 wait_for_completion(&dev
->kobj_unregister
);
318 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
320 cpuidle_resume_and_unlock();
322 module_put(cpuidle_curr_driver
->owner
);
325 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
329 static void smp_callback(void *v
)
331 /* we already woke the CPU up, nothing more to do */
335 * This function gets called when a part of the kernel has a new latency
336 * requirement. This means we need to get all processors out of their C-state,
337 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
338 * wakes them all right up.
340 static int cpuidle_latency_notify(struct notifier_block
*b
,
341 unsigned long l
, void *v
)
343 smp_call_function(smp_callback
, NULL
, 1);
347 static struct notifier_block cpuidle_latency_notifier
= {
348 .notifier_call
= cpuidle_latency_notify
,
351 static inline void latency_notifier_init(struct notifier_block
*n
)
353 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
356 #else /* CONFIG_SMP */
358 #define latency_notifier_init(x) do { } while (0)
360 #endif /* CONFIG_SMP */
363 * cpuidle_init - core initializer
365 static int __init
cpuidle_init(void)
369 pm_idle_old
= pm_idle
;
371 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
375 latency_notifier_init(&cpuidle_latency_notifier
);
380 core_initcall(cpuidle_init
);