cpuidle: use last_state which can reflect the actual state entered
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / cpuidle / cpuidle.c
blobbb6e3b338043e13e8e87bc8fceaad67e8f6a9dcc
1 /*
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.
9 */
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>
20 #include "cpuidle.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)
33 cpu_idle_wait();
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) {}
39 #endif
41 static int __cpuidle_register_device(struct cpuidle_device *dev);
43 /**
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;
52 int next_state;
54 /* check if the device is ready */
55 if (!dev || !dev->enabled) {
56 if (pm_idle_old)
57 pm_idle_old();
58 else
59 #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
60 default_idle();
61 #else
62 local_irq_enable();
63 #endif
64 return;
67 /* ask the governor for the next state */
68 next_state = cpuidle_curr_governor->select(dev);
69 if (need_resched())
70 return;
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);
76 if (dev->last_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);
87 /**
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 */
94 smp_wmb();
95 pm_idle = cpuidle_idle_call;
99 /**
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;
106 cpuidle_kick_cpus();
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
134 * @dev: the 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)
141 int ret, i;
143 if (dev->enabled)
144 return 0;
145 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
146 return -EIO;
147 if (!dev->state_count)
148 return -EINVAL;
150 if (dev->registered == 0) {
151 ret = __cpuidle_register_device(dev);
152 if (ret)
153 return ret;
156 if ((ret = cpuidle_add_state_sysfs(dev)))
157 return ret;
159 if (cpuidle_curr_governor->enable &&
160 (ret = cpuidle_curr_governor->enable(dev)))
161 goto fail_sysfs;
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;
170 smp_wmb();
172 dev->enabled = 1;
174 enabled_devices++;
175 return 0;
177 fail_sysfs:
178 cpuidle_remove_state_sysfs(dev);
180 return ret;
183 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
186 * cpuidle_disable_device - disables idle PM for a CPU
187 * @dev: the 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)
194 if (!dev->enabled)
195 return;
196 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
197 return;
199 dev->enabled = 0;
201 if (cpuidle_curr_governor->disable)
202 cpuidle_curr_governor->disable(dev);
204 cpuidle_remove_state_sysfs(dev);
205 enabled_devices--;
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)
213 ktime_t t1, t2;
214 s64 diff;
215 int ret;
217 t1 = ktime_get();
218 local_irq_enable();
219 while (!need_resched())
220 cpu_relax();
222 t2 = ktime_get();
223 diff = ktime_to_us(ktime_sub(t2, t1));
224 if (diff > INT_MAX)
225 diff = INT_MAX;
227 ret = (int) diff;
228 return ret;
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;
245 #else
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
252 * @dev: the cpu
254 * cpuidle_lock mutex must be held before this is called
256 static int __cpuidle_register_device(struct cpuidle_device *dev)
258 int ret;
259 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
261 if (!sys_dev)
262 return -EINVAL;
263 if (!try_module_get(cpuidle_curr_driver->owner))
264 return -EINVAL;
266 init_completion(&dev->kobj_unregister);
268 poll_idle_init(dev);
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);
274 return ret;
277 dev->registered = 1;
278 return 0;
282 * cpuidle_register_device - registers a CPU's idle PM feature
283 * @dev: the cpu
285 int cpuidle_register_device(struct cpuidle_device *dev)
287 int ret;
289 mutex_lock(&cpuidle_lock);
291 if ((ret = __cpuidle_register_device(dev))) {
292 mutex_unlock(&cpuidle_lock);
293 return ret;
296 cpuidle_enable_device(dev);
297 cpuidle_install_idle_handler();
299 mutex_unlock(&cpuidle_lock);
301 return 0;
305 EXPORT_SYMBOL_GPL(cpuidle_register_device);
308 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
309 * @dev: the cpu
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)
316 return;
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);
334 #ifdef CONFIG_SMP
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);
351 return NOTIFY_OK;
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)
374 int ret;
376 pm_idle_old = pm_idle;
378 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
379 if (ret)
380 return ret;
382 latency_notifier_init(&cpuidle_latency_notifier);
384 return 0;
387 core_initcall(cpuidle_init);