x86 setup: correct the base in the GDT_ENTRY() macro
[linux-2.6/linux-loongson.git] / drivers / cpuidle / cpuidle.c
blobd2fabe7863a96345e096f5a644c111436a492819
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/latency.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
19 #include "cpuidle.h"
21 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
23 DEFINE_MUTEX(cpuidle_lock);
24 LIST_HEAD(cpuidle_detected_devices);
25 static void (*pm_idle_old)(void);
27 static int enabled_devices;
29 /**
30 * cpuidle_idle_call - the main idle loop
32 * NOTE: no locks or semaphores should be used here
34 static void cpuidle_idle_call(void)
36 struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
37 struct cpuidle_state *target_state;
38 int next_state;
40 /* check if the device is ready */
41 if (!dev || !dev->enabled) {
42 if (pm_idle_old)
43 pm_idle_old();
44 else
45 local_irq_enable();
46 return;
49 /* ask the governor for the next state */
50 next_state = cpuidle_curr_governor->select(dev);
51 if (need_resched())
52 return;
53 target_state = &dev->states[next_state];
55 /* enter the state and update stats */
56 dev->last_residency = target_state->enter(dev, target_state);
57 dev->last_state = target_state;
58 target_state->time += dev->last_residency;
59 target_state->usage++;
61 /* give the governor an opportunity to reflect on the outcome */
62 if (cpuidle_curr_governor->reflect)
63 cpuidle_curr_governor->reflect(dev);
66 /**
67 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
69 void cpuidle_install_idle_handler(void)
71 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
72 /* Make sure all changes finished before we switch to new idle */
73 smp_wmb();
74 pm_idle = cpuidle_idle_call;
78 /**
79 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
81 void cpuidle_uninstall_idle_handler(void)
83 if (enabled_devices && (pm_idle != pm_idle_old)) {
84 pm_idle = pm_idle_old;
85 cpu_idle_wait();
89 /**
90 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
92 void cpuidle_pause_and_lock(void)
94 mutex_lock(&cpuidle_lock);
95 cpuidle_uninstall_idle_handler();
98 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
101 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
103 void cpuidle_resume_and_unlock(void)
105 cpuidle_install_idle_handler();
106 mutex_unlock(&cpuidle_lock);
109 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
112 * cpuidle_enable_device - enables idle PM for a CPU
113 * @dev: the CPU
115 * This function must be called between cpuidle_pause_and_lock and
116 * cpuidle_resume_and_unlock when used externally.
118 int cpuidle_enable_device(struct cpuidle_device *dev)
120 int ret, i;
122 if (dev->enabled)
123 return 0;
124 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
125 return -EIO;
126 if (!dev->state_count)
127 return -EINVAL;
129 if ((ret = cpuidle_add_state_sysfs(dev)))
130 return ret;
132 if (cpuidle_curr_governor->enable &&
133 (ret = cpuidle_curr_governor->enable(dev)))
134 goto fail_sysfs;
136 for (i = 0; i < dev->state_count; i++) {
137 dev->states[i].usage = 0;
138 dev->states[i].time = 0;
140 dev->last_residency = 0;
141 dev->last_state = NULL;
143 smp_wmb();
145 dev->enabled = 1;
147 enabled_devices++;
148 return 0;
150 fail_sysfs:
151 cpuidle_remove_state_sysfs(dev);
153 return ret;
156 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
159 * cpuidle_disable_device - disables idle PM for a CPU
160 * @dev: the CPU
162 * This function must be called between cpuidle_pause_and_lock and
163 * cpuidle_resume_and_unlock when used externally.
165 void cpuidle_disable_device(struct cpuidle_device *dev)
167 if (!dev->enabled)
168 return;
169 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
170 return;
172 dev->enabled = 0;
174 if (cpuidle_curr_governor->disable)
175 cpuidle_curr_governor->disable(dev);
177 cpuidle_remove_state_sysfs(dev);
178 enabled_devices--;
181 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
184 * cpuidle_register_device - registers a CPU's idle PM feature
185 * @dev: the cpu
187 int cpuidle_register_device(struct cpuidle_device *dev)
189 int ret;
190 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
192 if (!sys_dev)
193 return -EINVAL;
194 if (!try_module_get(cpuidle_curr_driver->owner))
195 return -EINVAL;
197 init_completion(&dev->kobj_unregister);
199 mutex_lock(&cpuidle_lock);
201 per_cpu(cpuidle_devices, dev->cpu) = dev;
202 list_add(&dev->device_list, &cpuidle_detected_devices);
203 if ((ret = cpuidle_add_sysfs(sys_dev))) {
204 mutex_unlock(&cpuidle_lock);
205 module_put(cpuidle_curr_driver->owner);
206 return ret;
209 cpuidle_enable_device(dev);
210 cpuidle_install_idle_handler();
212 mutex_unlock(&cpuidle_lock);
214 return 0;
218 EXPORT_SYMBOL_GPL(cpuidle_register_device);
221 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
222 * @dev: the cpu
224 void cpuidle_unregister_device(struct cpuidle_device *dev)
226 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
228 cpuidle_pause_and_lock();
230 cpuidle_disable_device(dev);
232 cpuidle_remove_sysfs(sys_dev);
233 list_del(&dev->device_list);
234 wait_for_completion(&dev->kobj_unregister);
235 per_cpu(cpuidle_devices, dev->cpu) = NULL;
237 cpuidle_resume_and_unlock();
239 module_put(cpuidle_curr_driver->owner);
242 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
244 #ifdef CONFIG_SMP
246 static void smp_callback(void *v)
248 /* we already woke the CPU up, nothing more to do */
252 * This function gets called when a part of the kernel has a new latency
253 * requirement. This means we need to get all processors out of their C-state,
254 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
255 * wakes them all right up.
257 static int cpuidle_latency_notify(struct notifier_block *b,
258 unsigned long l, void *v)
260 smp_call_function(smp_callback, NULL, 0, 1);
261 return NOTIFY_OK;
264 static struct notifier_block cpuidle_latency_notifier = {
265 .notifier_call = cpuidle_latency_notify,
268 #define latency_notifier_init(x) do { register_latency_notifier(x); } while (0)
270 #else /* CONFIG_SMP */
272 #define latency_notifier_init(x) do { } while (0)
274 #endif /* CONFIG_SMP */
277 * cpuidle_init - core initializer
279 static int __init cpuidle_init(void)
281 int ret;
283 pm_idle_old = pm_idle;
285 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
286 if (ret)
287 return ret;
289 latency_notifier_init(&cpuidle_latency_notifier);
291 return 0;
294 core_initcall(cpuidle_init);