]>
Commit | Line | Data |
---|---|---|
4f86d3a8 LB |
1 | /* |
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> | |
d82b3518 | 15 | #include <linux/pm_qos_params.h> |
4f86d3a8 LB |
16 | #include <linux/cpu.h> |
17 | #include <linux/cpuidle.h> | |
9a0b8415 | 18 | #include <linux/ktime.h> |
2e94d1f7 | 19 | #include <linux/hrtimer.h> |
288f023e | 20 | #include <trace/events/power.h> |
4f86d3a8 LB |
21 | |
22 | #include "cpuidle.h" | |
23 | ||
24 | DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); | |
4f86d3a8 LB |
25 | |
26 | DEFINE_MUTEX(cpuidle_lock); | |
27 | LIST_HEAD(cpuidle_detected_devices); | |
4f86d3a8 LB |
28 | |
29 | static int enabled_devices; | |
62027aea | 30 | static int off __read_mostly; |
a0bfa137 | 31 | static int initialized __read_mostly; |
62027aea LB |
32 | |
33 | int cpuidle_disabled(void) | |
34 | { | |
35 | return off; | |
36 | } | |
d91ee586 LB |
37 | void disable_cpuidle(void) |
38 | { | |
39 | off = 1; | |
40 | } | |
4f86d3a8 | 41 | |
a6869cc4 VP |
42 | #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT) |
43 | static void cpuidle_kick_cpus(void) | |
44 | { | |
45 | cpu_idle_wait(); | |
46 | } | |
47 | #elif defined(CONFIG_SMP) | |
48 | # error "Arch needs cpu_idle_wait() equivalent here" | |
49 | #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */ | |
50 | static void cpuidle_kick_cpus(void) {} | |
51 | #endif | |
52 | ||
dcb84f33 VP |
53 | static int __cpuidle_register_device(struct cpuidle_device *dev); |
54 | ||
4f86d3a8 LB |
55 | /** |
56 | * cpuidle_idle_call - the main idle loop | |
57 | * | |
58 | * NOTE: no locks or semaphores should be used here | |
a0bfa137 | 59 | * return non-zero on failure |
4f86d3a8 | 60 | */ |
a0bfa137 | 61 | int cpuidle_idle_call(void) |
4f86d3a8 | 62 | { |
4a6f4fe8 | 63 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
4f86d3a8 LB |
64 | struct cpuidle_state *target_state; |
65 | int next_state; | |
66 | ||
a0bfa137 LB |
67 | if (off) |
68 | return -ENODEV; | |
69 | ||
70 | if (!initialized) | |
71 | return -ENODEV; | |
72 | ||
4f86d3a8 | 73 | /* check if the device is ready */ |
a0bfa137 LB |
74 | if (!dev || !dev->enabled) |
75 | return -EBUSY; | |
4f86d3a8 | 76 | |
9a655837 AV |
77 | #if 0 |
78 | /* shows regressions, re-enable for 2.6.29 */ | |
2e94d1f7 AV |
79 | /* |
80 | * run any timers that can be run now, at this point | |
81 | * before calculating the idle duration etc. | |
82 | */ | |
83 | hrtimer_peek_ahead_timers(); | |
9a655837 | 84 | #endif |
71abbbf8 AL |
85 | |
86 | /* | |
87 | * Call the device's prepare function before calling the | |
88 | * governor's select function. ->prepare gives the device's | |
89 | * cpuidle driver a chance to update any dynamic information | |
90 | * of its cpuidle states for the current idle period, e.g. | |
91 | * state availability, latencies, residencies, etc. | |
92 | */ | |
93 | if (dev->prepare) | |
94 | dev->prepare(dev); | |
95 | ||
4f86d3a8 LB |
96 | /* ask the governor for the next state */ |
97 | next_state = cpuidle_curr_governor->select(dev); | |
246eb7f0 KH |
98 | if (need_resched()) { |
99 | local_irq_enable(); | |
a0bfa137 | 100 | return 0; |
246eb7f0 KH |
101 | } |
102 | ||
4f86d3a8 LB |
103 | target_state = &dev->states[next_state]; |
104 | ||
105 | /* enter the state and update stats */ | |
4f86d3a8 | 106 | dev->last_state = target_state; |
f77cfe4e TR |
107 | |
108 | trace_power_start(POWER_CSTATE, next_state, dev->cpu); | |
109 | trace_cpu_idle(next_state, dev->cpu); | |
110 | ||
887e301a | 111 | dev->last_residency = target_state->enter(dev, target_state); |
f77cfe4e TR |
112 | |
113 | trace_power_end(dev->cpu); | |
114 | trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu); | |
115 | ||
887e301a VP |
116 | if (dev->last_state) |
117 | target_state = dev->last_state; | |
118 | ||
8b78cf60 | 119 | target_state->time += (unsigned long long)dev->last_residency; |
4f86d3a8 LB |
120 | target_state->usage++; |
121 | ||
122 | /* give the governor an opportunity to reflect on the outcome */ | |
123 | if (cpuidle_curr_governor->reflect) | |
124 | cpuidle_curr_governor->reflect(dev); | |
a0bfa137 LB |
125 | |
126 | return 0; | |
4f86d3a8 LB |
127 | } |
128 | ||
129 | /** | |
130 | * cpuidle_install_idle_handler - installs the cpuidle idle loop handler | |
131 | */ | |
132 | void cpuidle_install_idle_handler(void) | |
133 | { | |
a0bfa137 | 134 | if (enabled_devices) { |
4f86d3a8 LB |
135 | /* Make sure all changes finished before we switch to new idle */ |
136 | smp_wmb(); | |
a0bfa137 | 137 | initialized = 1; |
4f86d3a8 LB |
138 | } |
139 | } | |
140 | ||
141 | /** | |
142 | * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler | |
143 | */ | |
144 | void cpuidle_uninstall_idle_handler(void) | |
145 | { | |
a0bfa137 LB |
146 | if (enabled_devices) { |
147 | initialized = 0; | |
a6869cc4 | 148 | cpuidle_kick_cpus(); |
4f86d3a8 LB |
149 | } |
150 | } | |
151 | ||
152 | /** | |
153 | * cpuidle_pause_and_lock - temporarily disables CPUIDLE | |
154 | */ | |
155 | void cpuidle_pause_and_lock(void) | |
156 | { | |
157 | mutex_lock(&cpuidle_lock); | |
158 | cpuidle_uninstall_idle_handler(); | |
159 | } | |
160 | ||
161 | EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); | |
162 | ||
163 | /** | |
164 | * cpuidle_resume_and_unlock - resumes CPUIDLE operation | |
165 | */ | |
166 | void cpuidle_resume_and_unlock(void) | |
167 | { | |
168 | cpuidle_install_idle_handler(); | |
169 | mutex_unlock(&cpuidle_lock); | |
170 | } | |
171 | ||
172 | EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); | |
173 | ||
d8c216cf RW |
174 | #ifdef CONFIG_ARCH_HAS_CPU_RELAX |
175 | static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st) | |
176 | { | |
177 | ktime_t t1, t2; | |
178 | s64 diff; | |
179 | int ret; | |
180 | ||
181 | t1 = ktime_get(); | |
182 | local_irq_enable(); | |
183 | while (!need_resched()) | |
184 | cpu_relax(); | |
185 | ||
186 | t2 = ktime_get(); | |
187 | diff = ktime_to_us(ktime_sub(t2, t1)); | |
188 | if (diff > INT_MAX) | |
189 | diff = INT_MAX; | |
190 | ||
191 | ret = (int) diff; | |
192 | return ret; | |
193 | } | |
194 | ||
195 | static void poll_idle_init(struct cpuidle_device *dev) | |
196 | { | |
197 | struct cpuidle_state *state = &dev->states[0]; | |
198 | ||
199 | cpuidle_set_statedata(state, NULL); | |
200 | ||
720f1c30 | 201 | snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); |
d8c216cf RW |
202 | snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); |
203 | state->exit_latency = 0; | |
204 | state->target_residency = 0; | |
205 | state->power_usage = -1; | |
d247632c | 206 | state->flags = 0; |
d8c216cf RW |
207 | state->enter = poll_idle; |
208 | } | |
209 | #else | |
210 | static void poll_idle_init(struct cpuidle_device *dev) {} | |
211 | #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ | |
212 | ||
4f86d3a8 LB |
213 | /** |
214 | * cpuidle_enable_device - enables idle PM for a CPU | |
215 | * @dev: the CPU | |
216 | * | |
217 | * This function must be called between cpuidle_pause_and_lock and | |
218 | * cpuidle_resume_and_unlock when used externally. | |
219 | */ | |
220 | int cpuidle_enable_device(struct cpuidle_device *dev) | |
221 | { | |
222 | int ret, i; | |
223 | ||
224 | if (dev->enabled) | |
225 | return 0; | |
752138df | 226 | if (!cpuidle_get_driver() || !cpuidle_curr_governor) |
4f86d3a8 LB |
227 | return -EIO; |
228 | if (!dev->state_count) | |
229 | return -EINVAL; | |
230 | ||
dcb84f33 VP |
231 | if (dev->registered == 0) { |
232 | ret = __cpuidle_register_device(dev); | |
233 | if (ret) | |
234 | return ret; | |
235 | } | |
236 | ||
d8c216cf RW |
237 | poll_idle_init(dev); |
238 | ||
4f86d3a8 LB |
239 | if ((ret = cpuidle_add_state_sysfs(dev))) |
240 | return ret; | |
241 | ||
242 | if (cpuidle_curr_governor->enable && | |
243 | (ret = cpuidle_curr_governor->enable(dev))) | |
244 | goto fail_sysfs; | |
245 | ||
246 | for (i = 0; i < dev->state_count; i++) { | |
247 | dev->states[i].usage = 0; | |
248 | dev->states[i].time = 0; | |
249 | } | |
250 | dev->last_residency = 0; | |
251 | dev->last_state = NULL; | |
252 | ||
253 | smp_wmb(); | |
254 | ||
255 | dev->enabled = 1; | |
256 | ||
257 | enabled_devices++; | |
258 | return 0; | |
259 | ||
260 | fail_sysfs: | |
261 | cpuidle_remove_state_sysfs(dev); | |
262 | ||
263 | return ret; | |
264 | } | |
265 | ||
266 | EXPORT_SYMBOL_GPL(cpuidle_enable_device); | |
267 | ||
268 | /** | |
269 | * cpuidle_disable_device - disables idle PM for a CPU | |
270 | * @dev: the CPU | |
271 | * | |
272 | * This function must be called between cpuidle_pause_and_lock and | |
273 | * cpuidle_resume_and_unlock when used externally. | |
274 | */ | |
275 | void cpuidle_disable_device(struct cpuidle_device *dev) | |
276 | { | |
277 | if (!dev->enabled) | |
278 | return; | |
752138df | 279 | if (!cpuidle_get_driver() || !cpuidle_curr_governor) |
4f86d3a8 LB |
280 | return; |
281 | ||
282 | dev->enabled = 0; | |
283 | ||
284 | if (cpuidle_curr_governor->disable) | |
285 | cpuidle_curr_governor->disable(dev); | |
286 | ||
287 | cpuidle_remove_state_sysfs(dev); | |
288 | enabled_devices--; | |
289 | } | |
290 | ||
291 | EXPORT_SYMBOL_GPL(cpuidle_disable_device); | |
292 | ||
293 | /** | |
dcb84f33 VP |
294 | * __cpuidle_register_device - internal register function called before register |
295 | * and enable routines | |
4f86d3a8 | 296 | * @dev: the cpu |
dcb84f33 VP |
297 | * |
298 | * cpuidle_lock mutex must be held before this is called | |
4f86d3a8 | 299 | */ |
dcb84f33 | 300 | static int __cpuidle_register_device(struct cpuidle_device *dev) |
4f86d3a8 LB |
301 | { |
302 | int ret; | |
303 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); | |
752138df | 304 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
4f86d3a8 LB |
305 | |
306 | if (!sys_dev) | |
307 | return -EINVAL; | |
752138df | 308 | if (!try_module_get(cpuidle_driver->owner)) |
4f86d3a8 LB |
309 | return -EINVAL; |
310 | ||
311 | init_completion(&dev->kobj_unregister); | |
312 | ||
71abbbf8 AL |
313 | /* |
314 | * cpuidle driver should set the dev->power_specified bit | |
315 | * before registering the device if the driver provides | |
316 | * power_usage numbers. | |
317 | * | |
318 | * For those devices whose ->power_specified is not set, | |
319 | * we fill in power_usage with decreasing values as the | |
320 | * cpuidle code has an implicit assumption that state Cn | |
321 | * uses less power than C(n-1). | |
322 | * | |
323 | * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned | |
324 | * an power value of -1. So we use -2, -3, etc, for other | |
325 | * c-states. | |
326 | */ | |
327 | if (!dev->power_specified) { | |
328 | int i; | |
329 | for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++) | |
330 | dev->states[i].power_usage = -1 - i; | |
331 | } | |
332 | ||
4f86d3a8 LB |
333 | per_cpu(cpuidle_devices, dev->cpu) = dev; |
334 | list_add(&dev->device_list, &cpuidle_detected_devices); | |
335 | if ((ret = cpuidle_add_sysfs(sys_dev))) { | |
752138df | 336 | module_put(cpuidle_driver->owner); |
4f86d3a8 LB |
337 | return ret; |
338 | } | |
339 | ||
dcb84f33 VP |
340 | dev->registered = 1; |
341 | return 0; | |
342 | } | |
343 | ||
344 | /** | |
345 | * cpuidle_register_device - registers a CPU's idle PM feature | |
346 | * @dev: the cpu | |
347 | */ | |
348 | int cpuidle_register_device(struct cpuidle_device *dev) | |
349 | { | |
350 | int ret; | |
351 | ||
352 | mutex_lock(&cpuidle_lock); | |
353 | ||
354 | if ((ret = __cpuidle_register_device(dev))) { | |
355 | mutex_unlock(&cpuidle_lock); | |
356 | return ret; | |
357 | } | |
358 | ||
4f86d3a8 LB |
359 | cpuidle_enable_device(dev); |
360 | cpuidle_install_idle_handler(); | |
361 | ||
362 | mutex_unlock(&cpuidle_lock); | |
363 | ||
364 | return 0; | |
365 | ||
366 | } | |
367 | ||
368 | EXPORT_SYMBOL_GPL(cpuidle_register_device); | |
369 | ||
370 | /** | |
371 | * cpuidle_unregister_device - unregisters a CPU's idle PM feature | |
372 | * @dev: the cpu | |
373 | */ | |
374 | void cpuidle_unregister_device(struct cpuidle_device *dev) | |
375 | { | |
376 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); | |
752138df | 377 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
4f86d3a8 | 378 | |
dcb84f33 VP |
379 | if (dev->registered == 0) |
380 | return; | |
381 | ||
4f86d3a8 LB |
382 | cpuidle_pause_and_lock(); |
383 | ||
384 | cpuidle_disable_device(dev); | |
385 | ||
386 | cpuidle_remove_sysfs(sys_dev); | |
387 | list_del(&dev->device_list); | |
388 | wait_for_completion(&dev->kobj_unregister); | |
389 | per_cpu(cpuidle_devices, dev->cpu) = NULL; | |
390 | ||
391 | cpuidle_resume_and_unlock(); | |
392 | ||
752138df | 393 | module_put(cpuidle_driver->owner); |
4f86d3a8 LB |
394 | } |
395 | ||
396 | EXPORT_SYMBOL_GPL(cpuidle_unregister_device); | |
397 | ||
398 | #ifdef CONFIG_SMP | |
399 | ||
400 | static void smp_callback(void *v) | |
401 | { | |
402 | /* we already woke the CPU up, nothing more to do */ | |
403 | } | |
404 | ||
405 | /* | |
406 | * This function gets called when a part of the kernel has a new latency | |
407 | * requirement. This means we need to get all processors out of their C-state, | |
408 | * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that | |
409 | * wakes them all right up. | |
410 | */ | |
411 | static int cpuidle_latency_notify(struct notifier_block *b, | |
412 | unsigned long l, void *v) | |
413 | { | |
8691e5a8 | 414 | smp_call_function(smp_callback, NULL, 1); |
4f86d3a8 LB |
415 | return NOTIFY_OK; |
416 | } | |
417 | ||
418 | static struct notifier_block cpuidle_latency_notifier = { | |
419 | .notifier_call = cpuidle_latency_notify, | |
420 | }; | |
421 | ||
d82b3518 MG |
422 | static inline void latency_notifier_init(struct notifier_block *n) |
423 | { | |
424 | pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); | |
425 | } | |
4f86d3a8 LB |
426 | |
427 | #else /* CONFIG_SMP */ | |
428 | ||
429 | #define latency_notifier_init(x) do { } while (0) | |
430 | ||
431 | #endif /* CONFIG_SMP */ | |
432 | ||
433 | /** | |
434 | * cpuidle_init - core initializer | |
435 | */ | |
436 | static int __init cpuidle_init(void) | |
437 | { | |
438 | int ret; | |
439 | ||
62027aea LB |
440 | if (cpuidle_disabled()) |
441 | return -ENODEV; | |
442 | ||
4f86d3a8 LB |
443 | ret = cpuidle_add_class_sysfs(&cpu_sysdev_class); |
444 | if (ret) | |
445 | return ret; | |
446 | ||
447 | latency_notifier_init(&cpuidle_latency_notifier); | |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
62027aea | 452 | module_param(off, int, 0444); |
4f86d3a8 | 453 | core_initcall(cpuidle_init); |