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>
23 DEFINE_PER_CPU(struct cpuidle_device
*, cpuidle_devices
);
25 DEFINE_MUTEX(cpuidle_lock
);
26 LIST_HEAD(cpuidle_detected_devices
);
27 static void (*pm_idle_old
)(void);
29 static int enabled_devices
;
31 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
32 static void cpuidle_kick_cpus(void)
36 #elif defined(CONFIG_SMP)
37 # error "Arch needs cpu_idle_wait() equivalent here"
38 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
39 static void cpuidle_kick_cpus(void) {}
42 static int __cpuidle_register_device(struct cpuidle_device
*dev
);
45 * cpuidle_idle_call - the main idle loop
47 * NOTE: no locks or semaphores should be used here
49 static void cpuidle_idle_call(void)
51 struct cpuidle_device
*dev
= __get_cpu_var(cpuidle_devices
);
52 struct cpuidle_state
*target_state
;
55 /* check if the device is ready */
56 if (!dev
|| !dev
->enabled
) {
60 #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
69 /* shows regressions, re-enable for 2.6.29 */
71 * run any timers that can be run now, at this point
72 * before calculating the idle duration etc.
74 hrtimer_peek_ahead_timers();
76 /* ask the governor for the next state */
77 next_state
= cpuidle_curr_governor
->select(dev
);
80 target_state
= &dev
->states
[next_state
];
82 /* enter the state and update stats */
83 dev
->last_state
= target_state
;
84 dev
->last_residency
= target_state
->enter(dev
, target_state
);
86 target_state
= dev
->last_state
;
88 target_state
->time
+= (unsigned long long)dev
->last_residency
;
89 target_state
->usage
++;
91 /* give the governor an opportunity to reflect on the outcome */
92 if (cpuidle_curr_governor
->reflect
)
93 cpuidle_curr_governor
->reflect(dev
);
97 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
99 void cpuidle_install_idle_handler(void)
101 if (enabled_devices
&& (pm_idle
!= cpuidle_idle_call
)) {
102 /* Make sure all changes finished before we switch to new idle */
104 pm_idle
= cpuidle_idle_call
;
109 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
111 void cpuidle_uninstall_idle_handler(void)
113 if (enabled_devices
&& pm_idle_old
&& (pm_idle
!= pm_idle_old
)) {
114 pm_idle
= pm_idle_old
;
120 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
122 void cpuidle_pause_and_lock(void)
124 mutex_lock(&cpuidle_lock
);
125 cpuidle_uninstall_idle_handler();
128 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
131 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
133 void cpuidle_resume_and_unlock(void)
135 cpuidle_install_idle_handler();
136 mutex_unlock(&cpuidle_lock
);
139 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
142 * cpuidle_enable_device - enables idle PM for a CPU
145 * This function must be called between cpuidle_pause_and_lock and
146 * cpuidle_resume_and_unlock when used externally.
148 int cpuidle_enable_device(struct cpuidle_device
*dev
)
154 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
156 if (!dev
->state_count
)
159 if (dev
->registered
== 0) {
160 ret
= __cpuidle_register_device(dev
);
165 if ((ret
= cpuidle_add_state_sysfs(dev
)))
168 if (cpuidle_curr_governor
->enable
&&
169 (ret
= cpuidle_curr_governor
->enable(dev
)))
172 for (i
= 0; i
< dev
->state_count
; i
++) {
173 dev
->states
[i
].usage
= 0;
174 dev
->states
[i
].time
= 0;
176 dev
->last_residency
= 0;
177 dev
->last_state
= NULL
;
187 cpuidle_remove_state_sysfs(dev
);
192 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
195 * cpuidle_disable_device - disables idle PM for a CPU
198 * This function must be called between cpuidle_pause_and_lock and
199 * cpuidle_resume_and_unlock when used externally.
201 void cpuidle_disable_device(struct cpuidle_device
*dev
)
205 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
210 if (cpuidle_curr_governor
->disable
)
211 cpuidle_curr_governor
->disable(dev
);
213 cpuidle_remove_state_sysfs(dev
);
217 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
219 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
220 static int poll_idle(struct cpuidle_device
*dev
, struct cpuidle_state
*st
)
228 while (!need_resched())
232 diff
= ktime_to_us(ktime_sub(t2
, t1
));
240 static void poll_idle_init(struct cpuidle_device
*dev
)
242 struct cpuidle_state
*state
= &dev
->states
[0];
244 cpuidle_set_statedata(state
, NULL
);
246 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "C0");
247 snprintf(state
->desc
, CPUIDLE_DESC_LEN
, "CPUIDLE CORE POLL IDLE");
248 state
->exit_latency
= 0;
249 state
->target_residency
= 0;
250 state
->power_usage
= -1;
251 state
->flags
= CPUIDLE_FLAG_POLL
;
252 state
->enter
= poll_idle
;
255 static void poll_idle_init(struct cpuidle_device
*dev
) {}
256 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
259 * __cpuidle_register_device - internal register function called before register
260 * and enable routines
263 * cpuidle_lock mutex must be held before this is called
265 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
268 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
272 if (!try_module_get(cpuidle_curr_driver
->owner
))
275 init_completion(&dev
->kobj_unregister
);
279 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
280 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
281 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
282 module_put(cpuidle_curr_driver
->owner
);
291 * cpuidle_register_device - registers a CPU's idle PM feature
294 int cpuidle_register_device(struct cpuidle_device
*dev
)
298 mutex_lock(&cpuidle_lock
);
300 if ((ret
= __cpuidle_register_device(dev
))) {
301 mutex_unlock(&cpuidle_lock
);
305 cpuidle_enable_device(dev
);
306 cpuidle_install_idle_handler();
308 mutex_unlock(&cpuidle_lock
);
314 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
317 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
320 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
322 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
324 if (dev
->registered
== 0)
327 cpuidle_pause_and_lock();
329 cpuidle_disable_device(dev
);
331 cpuidle_remove_sysfs(sys_dev
);
332 list_del(&dev
->device_list
);
333 wait_for_completion(&dev
->kobj_unregister
);
334 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
336 cpuidle_resume_and_unlock();
338 module_put(cpuidle_curr_driver
->owner
);
341 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
345 static void smp_callback(void *v
)
347 /* we already woke the CPU up, nothing more to do */
351 * This function gets called when a part of the kernel has a new latency
352 * requirement. This means we need to get all processors out of their C-state,
353 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
354 * wakes them all right up.
356 static int cpuidle_latency_notify(struct notifier_block
*b
,
357 unsigned long l
, void *v
)
359 smp_call_function(smp_callback
, NULL
, 1);
363 static struct notifier_block cpuidle_latency_notifier
= {
364 .notifier_call
= cpuidle_latency_notify
,
367 static inline void latency_notifier_init(struct notifier_block
*n
)
369 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
372 #else /* CONFIG_SMP */
374 #define latency_notifier_init(x) do { } while (0)
376 #endif /* CONFIG_SMP */
379 * cpuidle_init - core initializer
381 static int __init
cpuidle_init(void)
385 pm_idle_old
= pm_idle
;
387 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
391 latency_notifier_init(&cpuidle_latency_notifier
);
396 core_initcall(cpuidle_init
);