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) {}
42 * cpuidle_idle_call - the main idle loop
44 * NOTE: no locks or semaphores should be used here
46 static void cpuidle_idle_call(void)
48 struct cpuidle_device
*dev
= __get_cpu_var(cpuidle_devices
);
49 struct cpuidle_state
*target_state
;
52 /* check if the device is ready */
53 if (!dev
|| !dev
->enabled
) {
61 /* ask the governor for the next state */
62 next_state
= cpuidle_curr_governor
->select(dev
);
65 target_state
= &dev
->states
[next_state
];
67 /* enter the state and update stats */
68 dev
->last_residency
= target_state
->enter(dev
, target_state
);
69 dev
->last_state
= target_state
;
70 target_state
->time
+= dev
->last_residency
;
71 target_state
->usage
++;
73 /* give the governor an opportunity to reflect on the outcome */
74 if (cpuidle_curr_governor
->reflect
)
75 cpuidle_curr_governor
->reflect(dev
);
79 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
81 void cpuidle_install_idle_handler(void)
83 if (enabled_devices
&& (pm_idle
!= cpuidle_idle_call
)) {
84 /* Make sure all changes finished before we switch to new idle */
86 pm_idle
= cpuidle_idle_call
;
91 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
93 void cpuidle_uninstall_idle_handler(void)
95 if (enabled_devices
&& (pm_idle
!= pm_idle_old
)) {
96 pm_idle
= pm_idle_old
;
102 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
104 void cpuidle_pause_and_lock(void)
106 mutex_lock(&cpuidle_lock
);
107 cpuidle_uninstall_idle_handler();
110 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
113 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
115 void cpuidle_resume_and_unlock(void)
117 cpuidle_install_idle_handler();
118 mutex_unlock(&cpuidle_lock
);
121 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
124 * cpuidle_enable_device - enables idle PM for a CPU
127 * This function must be called between cpuidle_pause_and_lock and
128 * cpuidle_resume_and_unlock when used externally.
130 int cpuidle_enable_device(struct cpuidle_device
*dev
)
136 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
138 if (!dev
->state_count
)
141 if ((ret
= cpuidle_add_state_sysfs(dev
)))
144 if (cpuidle_curr_governor
->enable
&&
145 (ret
= cpuidle_curr_governor
->enable(dev
)))
148 for (i
= 0; i
< dev
->state_count
; i
++) {
149 dev
->states
[i
].usage
= 0;
150 dev
->states
[i
].time
= 0;
152 dev
->last_residency
= 0;
153 dev
->last_state
= NULL
;
163 cpuidle_remove_state_sysfs(dev
);
168 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
171 * cpuidle_disable_device - disables idle PM for a CPU
174 * This function must be called between cpuidle_pause_and_lock and
175 * cpuidle_resume_and_unlock when used externally.
177 void cpuidle_disable_device(struct cpuidle_device
*dev
)
181 if (!cpuidle_curr_driver
|| !cpuidle_curr_governor
)
186 if (cpuidle_curr_governor
->disable
)
187 cpuidle_curr_governor
->disable(dev
);
189 cpuidle_remove_state_sysfs(dev
);
193 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
195 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
196 static int poll_idle(struct cpuidle_device
*dev
, struct cpuidle_state
*st
)
204 while (!need_resched())
208 diff
= ktime_to_us(ktime_sub(t2
, t1
));
216 static void poll_idle_init(struct cpuidle_device
*dev
)
218 struct cpuidle_state
*state
= &dev
->states
[0];
220 cpuidle_set_statedata(state
, NULL
);
222 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "C0");
223 snprintf(state
->desc
, CPUIDLE_DESC_LEN
, "CPUIDLE CORE POLL IDLE");
224 state
->exit_latency
= 0;
225 state
->target_residency
= 0;
226 state
->power_usage
= -1;
227 state
->flags
= CPUIDLE_FLAG_POLL
| CPUIDLE_FLAG_TIME_VALID
;
228 state
->enter
= poll_idle
;
231 static void poll_idle_init(struct cpuidle_device
*dev
) {}
232 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
235 * cpuidle_register_device - registers a CPU's idle PM feature
238 int cpuidle_register_device(struct cpuidle_device
*dev
)
241 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
245 if (!try_module_get(cpuidle_curr_driver
->owner
))
248 init_completion(&dev
->kobj_unregister
);
250 mutex_lock(&cpuidle_lock
);
254 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
255 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
256 if ((ret
= cpuidle_add_sysfs(sys_dev
))) {
257 mutex_unlock(&cpuidle_lock
);
258 module_put(cpuidle_curr_driver
->owner
);
262 cpuidle_enable_device(dev
);
263 cpuidle_install_idle_handler();
265 mutex_unlock(&cpuidle_lock
);
271 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
274 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
277 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
279 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned long)dev
->cpu
);
281 cpuidle_pause_and_lock();
283 cpuidle_disable_device(dev
);
285 cpuidle_remove_sysfs(sys_dev
);
286 list_del(&dev
->device_list
);
287 wait_for_completion(&dev
->kobj_unregister
);
288 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
290 cpuidle_resume_and_unlock();
292 module_put(cpuidle_curr_driver
->owner
);
295 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
299 static void smp_callback(void *v
)
301 /* we already woke the CPU up, nothing more to do */
305 * This function gets called when a part of the kernel has a new latency
306 * requirement. This means we need to get all processors out of their C-state,
307 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
308 * wakes them all right up.
310 static int cpuidle_latency_notify(struct notifier_block
*b
,
311 unsigned long l
, void *v
)
313 smp_call_function(smp_callback
, NULL
, 0, 1);
317 static struct notifier_block cpuidle_latency_notifier
= {
318 .notifier_call
= cpuidle_latency_notify
,
321 static inline void latency_notifier_init(struct notifier_block
*n
)
323 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
326 #else /* CONFIG_SMP */
328 #define latency_notifier_init(x) do { } while (0)
330 #endif /* CONFIG_SMP */
333 * cpuidle_init - core initializer
335 static int __init
cpuidle_init(void)
339 pm_idle_old
= pm_idle
;
341 ret
= cpuidle_add_class_sysfs(&cpu_sysdev_class
);
345 latency_notifier_init(&cpuidle_latency_notifier
);
350 core_initcall(cpuidle_init
);