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
) {
59 #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
67 /* ask the governor for the next state */
68 next_state
= cpuidle_curr_governor
->select(dev
);
71 target_state
= &dev
->states
[next_state
];
73 /* enter the state and update stats */
74 dev
->last_state
= target_state
;
75 dev
->last_residency
= target_state
->enter(dev
, target_state
);
77 target_state
= dev
->last_state
;
79 target_state
->time
+= (unsigned long long)dev
->last_residency
;
80 target_state
->usage
++;
82 /* give the governor an opportunity to reflect on the outcome */
83 if (cpuidle_curr_governor
->reflect
)
84 cpuidle_curr_governor
->reflect(dev
);
88 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
90 void cpuidle_install_idle_handler(void)
92 if (enabled_devices
&& (pm_idle
!= cpuidle_idle_call
)) {
93 /* Make sure all changes finished before we switch to new idle */
95 pm_idle
= cpuidle_idle_call
;
100 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
102 void cpuidle_uninstall_idle_handler(void)
104 if (enabled_devices
&& pm_idle_old
&& (pm_idle
!= pm_idle_old
)) {
105 pm_idle
= pm_idle_old
;
111 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
113 void cpuidle_pause_and_lock(void)
115 mutex_lock(&cpuidle_lock
);
116 cpuidle_uninstall_idle_handler();
119 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
122 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
124 void cpuidle_resume_and_unlock(void)
126 cpuidle_install_idle_handler();
127 mutex_unlock(&cpuidle_lock
);
130 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
133 * cpuidle_enable_device - enables idle PM for a CPU
136 * This function must be called between cpuidle_pause_and_lock and
137 * cpuidle_resume_and_unlock when used externally.
139 int cpuidle_enable_device(struct cpuidle_device
*dev
)
145 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
147 if (!dev
->state_count
)
150 if (dev
->registered
== 0) {
151 ret
= __cpuidle_register_device(dev
);
156 if ((ret
= cpuidle_add_state_sysfs(dev
)))
159 if (cpuidle_curr_governor
->enable
&&
160 (ret
= cpuidle_curr_governor
->enable(dev
)))
163 for (i
= 0; i
< dev
->state_count
; i
++) {
164 dev
->states
[i
].usage
= 0;
165 dev
->states
[i
].time
= 0;
167 dev
->last_residency
= 0;
168 dev
->last_state
= NULL
;
178 cpuidle_remove_state_sysfs(dev
);
183 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
186 * cpuidle_disable_device - disables idle PM for a CPU
189 * This function must be called between cpuidle_pause_and_lock and
190 * cpuidle_resume_and_unlock when used externally.
192 void cpuidle_disable_device(struct cpuidle_device
*dev
)
196 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
201 if (cpuidle_curr_governor
->disable
)
202 cpuidle_curr_governor
->disable(dev
);
204 cpuidle_remove_state_sysfs(dev
);
208 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
210 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
211 static int poll_idle(struct cpuidle_device
*dev
, struct cpuidle_state
*st
)
219 while (!need_resched())
223 diff
= ktime_to_us(ktime_sub(t2
, t1
));
231 static void poll_idle_init(struct cpuidle_device
*dev
)
233 struct cpuidle_state
*state
= &dev
->states
[0];
235 cpuidle_set_statedata(state
, NULL
);
237 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "C0");
238 snprintf(state
->desc
, CPUIDLE_DESC_LEN
, "CPUIDLE CORE POLL IDLE");
239 state
->exit_latency
= 0;
240 state
->target_residency
= 0;
241 state
->power_usage
= -1;
242 state
->flags
= CPUIDLE_FLAG_POLL
;
243 state
->enter
= poll_idle
;
246 static void poll_idle_init(struct cpuidle_device
*dev
) {}
247 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
250 * __cpuidle_register_device - internal register function called before register
251 * and enable routines
254 * cpuidle_lock mutex must be held before this is called
256 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
259 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
263 if (!try_module_get(cpuidle_curr_driver
->owner
))
266 init_completion(&dev
->kobj_unregister
);
270 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
271 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
272 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
273 module_put(cpuidle_curr_driver
->owner
);
282 * cpuidle_register_device - registers a CPU's idle PM feature
285 int cpuidle_register_device(struct cpuidle_device
*dev
)
289 mutex_lock(&cpuidle_lock
);
291 if ((ret
= __cpuidle_register_device(dev
))) {
292 mutex_unlock(&cpuidle_lock
);
296 cpuidle_enable_device(dev
);
297 cpuidle_install_idle_handler();
299 mutex_unlock(&cpuidle_lock
);
305 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
308 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
311 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
313 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
315 if (dev
->registered
== 0)
318 cpuidle_pause_and_lock();
320 cpuidle_disable_device(dev
);
322 cpuidle_remove_sysfs(sys_dev
);
323 list_del(&dev
->device_list
);
324 wait_for_completion(&dev
->kobj_unregister
);
325 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
327 cpuidle_resume_and_unlock();
329 module_put(cpuidle_curr_driver
->owner
);
332 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
336 static void smp_callback(void *v
)
338 /* we already woke the CPU up, nothing more to do */
342 * This function gets called when a part of the kernel has a new latency
343 * requirement. This means we need to get all processors out of their C-state,
344 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
345 * wakes them all right up.
347 static int cpuidle_latency_notify(struct notifier_block
*b
,
348 unsigned long l
, void *v
)
350 smp_call_function(smp_callback
, NULL
, 1);
354 static struct notifier_block cpuidle_latency_notifier
= {
355 .notifier_call
= cpuidle_latency_notify
,
358 static inline void latency_notifier_init(struct notifier_block
*n
)
360 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
363 #else /* CONFIG_SMP */
365 #define latency_notifier_init(x) do { } while (0)
367 #endif /* CONFIG_SMP */
370 * cpuidle_init - core initializer
372 static int __init
cpuidle_init(void)
376 pm_idle_old
= pm_idle
;
378 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
382 latency_notifier_init(&cpuidle_latency_notifier
);
387 core_initcall(cpuidle_init
);