blob: 2c4b2d47973e195cbb0422a9b539400a2ad5ef5e [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * cpuidle.c - core cpuidle infrastructure
3 *
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
7 *
8 * This code is licenced under the GPL.
9 */
10
11#include <linux/kernel.h>
12#include <linux/mutex.h>
13#include <linux/sched.h>
14#include <linux/notifier.h>
Mark Grossd82b3512008-02-04 22:30:08 -080015#include <linux/pm_qos_params.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040016#include <linux/cpu.h>
17#include <linux/cpuidle.h>
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -080018#include <linux/ktime.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040019
20#include "cpuidle.h"
21
22DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -040023
24DEFINE_MUTEX(cpuidle_lock);
25LIST_HEAD(cpuidle_detected_devices);
26static void (*pm_idle_old)(void);
27
28static int enabled_devices;
29
30/**
31 * cpuidle_idle_call - the main idle loop
32 *
33 * NOTE: no locks or semaphores should be used here
34 */
35static void cpuidle_idle_call(void)
36{
37 struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
38 struct cpuidle_state *target_state;
39 int next_state;
40
41 /* check if the device is ready */
42 if (!dev || !dev->enabled) {
43 if (pm_idle_old)
44 pm_idle_old();
45 else
46 local_irq_enable();
47 return;
48 }
49
50 /* ask the governor for the next state */
51 next_state = cpuidle_curr_governor->select(dev);
52 if (need_resched())
53 return;
54 target_state = &dev->states[next_state];
55
56 /* enter the state and update stats */
57 dev->last_residency = target_state->enter(dev, target_state);
58 dev->last_state = target_state;
59 target_state->time += dev->last_residency;
60 target_state->usage++;
61
62 /* give the governor an opportunity to reflect on the outcome */
63 if (cpuidle_curr_governor->reflect)
64 cpuidle_curr_governor->reflect(dev);
65}
66
67/**
68 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
69 */
70void cpuidle_install_idle_handler(void)
71{
72 if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
73 /* Make sure all changes finished before we switch to new idle */
74 smp_wmb();
75 pm_idle = cpuidle_idle_call;
76 }
77}
78
79/**
80 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
81 */
82void cpuidle_uninstall_idle_handler(void)
83{
84 if (enabled_devices && (pm_idle != pm_idle_old)) {
85 pm_idle = pm_idle_old;
86 cpu_idle_wait();
87 }
88}
89
90/**
91 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
92 */
93void cpuidle_pause_and_lock(void)
94{
95 mutex_lock(&cpuidle_lock);
96 cpuidle_uninstall_idle_handler();
97}
98
99EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
100
101/**
102 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
103 */
104void cpuidle_resume_and_unlock(void)
105{
106 cpuidle_install_idle_handler();
107 mutex_unlock(&cpuidle_lock);
108}
109
110EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
111
112/**
113 * cpuidle_enable_device - enables idle PM for a CPU
114 * @dev: the CPU
115 *
116 * This function must be called between cpuidle_pause_and_lock and
117 * cpuidle_resume_and_unlock when used externally.
118 */
119int cpuidle_enable_device(struct cpuidle_device *dev)
120{
121 int ret, i;
122
123 if (dev->enabled)
124 return 0;
125 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
126 return -EIO;
127 if (!dev->state_count)
128 return -EINVAL;
129
130 if ((ret = cpuidle_add_state_sysfs(dev)))
131 return ret;
132
133 if (cpuidle_curr_governor->enable &&
134 (ret = cpuidle_curr_governor->enable(dev)))
135 goto fail_sysfs;
136
137 for (i = 0; i < dev->state_count; i++) {
138 dev->states[i].usage = 0;
139 dev->states[i].time = 0;
140 }
141 dev->last_residency = 0;
142 dev->last_state = NULL;
143
144 smp_wmb();
145
146 dev->enabled = 1;
147
148 enabled_devices++;
149 return 0;
150
151fail_sysfs:
152 cpuidle_remove_state_sysfs(dev);
153
154 return ret;
155}
156
157EXPORT_SYMBOL_GPL(cpuidle_enable_device);
158
159/**
160 * cpuidle_disable_device - disables idle PM for a CPU
161 * @dev: the CPU
162 *
163 * This function must be called between cpuidle_pause_and_lock and
164 * cpuidle_resume_and_unlock when used externally.
165 */
166void cpuidle_disable_device(struct cpuidle_device *dev)
167{
168 if (!dev->enabled)
169 return;
170 if (!cpuidle_curr_driver || !cpuidle_curr_governor)
171 return;
172
173 dev->enabled = 0;
174
175 if (cpuidle_curr_governor->disable)
176 cpuidle_curr_governor->disable(dev);
177
178 cpuidle_remove_state_sysfs(dev);
179 enabled_devices--;
180}
181
182EXPORT_SYMBOL_GPL(cpuidle_disable_device);
183
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800184#ifdef CONFIG_ARCH_HAS_CPU_RELAX
185static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
186{
187 ktime_t t1, t2;
188 s64 diff;
189 int ret;
190
191 t1 = ktime_get();
192 local_irq_enable();
193 while (!need_resched())
194 cpu_relax();
195
196 t2 = ktime_get();
197 diff = ktime_to_us(ktime_sub(t2, t1));
198 if (diff > INT_MAX)
199 diff = INT_MAX;
200
201 ret = (int) diff;
202 return ret;
203}
204
205static void poll_idle_init(struct cpuidle_device *dev)
206{
207 struct cpuidle_state *state = &dev->states[0];
208
209 cpuidle_set_statedata(state, NULL);
210
211 snprintf(state->name, CPUIDLE_NAME_LEN, "C0 (poll idle)");
212 state->exit_latency = 0;
213 state->target_residency = 0;
214 state->power_usage = -1;
215 state->flags = CPUIDLE_FLAG_POLL | CPUIDLE_FLAG_TIME_VALID;
216 state->enter = poll_idle;
217}
218#else
219static void poll_idle_init(struct cpuidle_device *dev) {}
220#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
221
Len Brown4f86d3a2007-10-03 18:58:00 -0400222/**
223 * cpuidle_register_device - registers a CPU's idle PM feature
224 * @dev: the cpu
225 */
226int cpuidle_register_device(struct cpuidle_device *dev)
227{
228 int ret;
229 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
230
231 if (!sys_dev)
232 return -EINVAL;
233 if (!try_module_get(cpuidle_curr_driver->owner))
234 return -EINVAL;
235
236 init_completion(&dev->kobj_unregister);
237
238 mutex_lock(&cpuidle_lock);
239
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800240 poll_idle_init(dev);
241
Len Brown4f86d3a2007-10-03 18:58:00 -0400242 per_cpu(cpuidle_devices, dev->cpu) = dev;
243 list_add(&dev->device_list, &cpuidle_detected_devices);
244 if ((ret = cpuidle_add_sysfs(sys_dev))) {
245 mutex_unlock(&cpuidle_lock);
246 module_put(cpuidle_curr_driver->owner);
247 return ret;
248 }
249
250 cpuidle_enable_device(dev);
251 cpuidle_install_idle_handler();
252
253 mutex_unlock(&cpuidle_lock);
254
255 return 0;
256
257}
258
259EXPORT_SYMBOL_GPL(cpuidle_register_device);
260
261/**
262 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
263 * @dev: the cpu
264 */
265void cpuidle_unregister_device(struct cpuidle_device *dev)
266{
267 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
268
269 cpuidle_pause_and_lock();
270
271 cpuidle_disable_device(dev);
272
273 cpuidle_remove_sysfs(sys_dev);
274 list_del(&dev->device_list);
275 wait_for_completion(&dev->kobj_unregister);
276 per_cpu(cpuidle_devices, dev->cpu) = NULL;
277
278 cpuidle_resume_and_unlock();
279
280 module_put(cpuidle_curr_driver->owner);
281}
282
283EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
284
285#ifdef CONFIG_SMP
286
287static void smp_callback(void *v)
288{
289 /* we already woke the CPU up, nothing more to do */
290}
291
292/*
293 * This function gets called when a part of the kernel has a new latency
294 * requirement. This means we need to get all processors out of their C-state,
295 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
296 * wakes them all right up.
297 */
298static int cpuidle_latency_notify(struct notifier_block *b,
299 unsigned long l, void *v)
300{
301 smp_call_function(smp_callback, NULL, 0, 1);
302 return NOTIFY_OK;
303}
304
305static struct notifier_block cpuidle_latency_notifier = {
306 .notifier_call = cpuidle_latency_notify,
307};
308
Mark Grossd82b3512008-02-04 22:30:08 -0800309static inline void latency_notifier_init(struct notifier_block *n)
310{
311 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
312}
Len Brown4f86d3a2007-10-03 18:58:00 -0400313
314#else /* CONFIG_SMP */
315
316#define latency_notifier_init(x) do { } while (0)
317
318#endif /* CONFIG_SMP */
319
320/**
321 * cpuidle_init - core initializer
322 */
323static int __init cpuidle_init(void)
324{
325 int ret;
326
327 pm_idle_old = pm_idle;
328
329 ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
330 if (ret)
331 return ret;
332
333 latency_notifier_init(&cpuidle_latency_notifier);
334
335 return 0;
336}
337
338core_initcall(cpuidle_init);