Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upstream-linus
[deliverable/linux.git] / drivers / cpuidle / cpuidle.c
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/clockchips.h>
12 #include <linux/kernel.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/notifier.h>
16 #include <linux/pm_qos.h>
17 #include <linux/cpu.h>
18 #include <linux/cpuidle.h>
19 #include <linux/ktime.h>
20 #include <linux/hrtimer.h>
21 #include <linux/module.h>
22 #include <linux/suspend.h>
23 #include <linux/tick.h>
24 #include <trace/events/power.h>
25
26 #include "cpuidle.h"
27
28 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
29 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
30
31 DEFINE_MUTEX(cpuidle_lock);
32 LIST_HEAD(cpuidle_detected_devices);
33
34 static int enabled_devices;
35 static int off __read_mostly;
36 static int initialized __read_mostly;
37
38 int cpuidle_disabled(void)
39 {
40 return off;
41 }
42 void disable_cpuidle(void)
43 {
44 off = 1;
45 }
46
47 /**
48 * cpuidle_play_dead - cpu off-lining
49 *
50 * Returns in case of an error or no driver
51 */
52 int cpuidle_play_dead(void)
53 {
54 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
55 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
56 int i;
57
58 if (!drv)
59 return -ENODEV;
60
61 /* Find lowest-power state that supports long-term idle */
62 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
63 if (drv->states[i].enter_dead)
64 return drv->states[i].enter_dead(dev, i);
65
66 return -ENODEV;
67 }
68
69 /**
70 * cpuidle_find_deepest_state - Find deepest state meeting specific conditions.
71 * @drv: cpuidle driver for the given CPU.
72 * @dev: cpuidle device for the given CPU.
73 * @freeze: Whether or not the state should be suitable for suspend-to-idle.
74 */
75 static int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
76 struct cpuidle_device *dev, bool freeze)
77 {
78 unsigned int latency_req = 0;
79 int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1;
80
81 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
82 struct cpuidle_state *s = &drv->states[i];
83 struct cpuidle_state_usage *su = &dev->states_usage[i];
84
85 if (s->disabled || su->disable || s->exit_latency <= latency_req
86 || (freeze && !s->enter_freeze))
87 continue;
88
89 latency_req = s->exit_latency;
90 ret = i;
91 }
92 return ret;
93 }
94
95 static void enter_freeze_proper(struct cpuidle_driver *drv,
96 struct cpuidle_device *dev, int index)
97 {
98 tick_freeze();
99 /*
100 * The state used here cannot be a "coupled" one, because the "coupled"
101 * cpuidle mechanism enables interrupts and doing that with timekeeping
102 * suspended is generally unsafe.
103 */
104 drv->states[index].enter_freeze(dev, drv, index);
105 WARN_ON(!irqs_disabled());
106 /*
107 * timekeeping_resume() that will be called by tick_unfreeze() for the
108 * last CPU executing it calls functions containing RCU read-side
109 * critical sections, so tell RCU about that.
110 */
111 RCU_NONIDLE(tick_unfreeze());
112 }
113
114 /**
115 * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle.
116 *
117 * If there are states with the ->enter_freeze callback, find the deepest of
118 * them and enter it with frozen tick. Otherwise, find the deepest state
119 * available and enter it normally.
120 */
121 void cpuidle_enter_freeze(void)
122 {
123 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
124 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
125 int index;
126
127 /*
128 * Find the deepest state with ->enter_freeze present, which guarantees
129 * that interrupts won't be enabled when it exits and allows the tick to
130 * be frozen safely.
131 */
132 index = cpuidle_find_deepest_state(drv, dev, true);
133 if (index >= 0) {
134 enter_freeze_proper(drv, dev, index);
135 return;
136 }
137
138 /*
139 * It is not safe to freeze the tick, find the deepest state available
140 * at all and try to enter it normally.
141 */
142 index = cpuidle_find_deepest_state(drv, dev, false);
143 if (index >= 0)
144 cpuidle_enter(drv, dev, index);
145 else
146 arch_cpu_idle();
147
148 /* Interrupts are enabled again here. */
149 local_irq_disable();
150 }
151
152 /**
153 * cpuidle_enter_state - enter the state and update stats
154 * @dev: cpuidle device for this cpu
155 * @drv: cpuidle driver for this cpu
156 * @next_state: index into drv->states of the state to enter
157 */
158 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
159 int index)
160 {
161 int entered_state;
162
163 struct cpuidle_state *target_state = &drv->states[index];
164 ktime_t time_start, time_end;
165 s64 diff;
166
167 trace_cpu_idle_rcuidle(index, dev->cpu);
168 time_start = ktime_get();
169
170 entered_state = target_state->enter(dev, drv, index);
171
172 time_end = ktime_get();
173 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
174
175 if (!cpuidle_state_is_coupled(dev, drv, entered_state))
176 local_irq_enable();
177
178 diff = ktime_to_us(ktime_sub(time_end, time_start));
179 if (diff > INT_MAX)
180 diff = INT_MAX;
181
182 dev->last_residency = (int) diff;
183
184 if (entered_state >= 0) {
185 /* Update cpuidle counters */
186 /* This can be moved to within driver enter routine
187 * but that results in multiple copies of same code.
188 */
189 dev->states_usage[entered_state].time += dev->last_residency;
190 dev->states_usage[entered_state].usage++;
191 } else {
192 dev->last_residency = 0;
193 }
194
195 return entered_state;
196 }
197
198 /**
199 * cpuidle_select - ask the cpuidle framework to choose an idle state
200 *
201 * @drv: the cpuidle driver
202 * @dev: the cpuidle device
203 *
204 * Returns the index of the idle state.
205 */
206 int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
207 {
208 if (off || !initialized)
209 return -ENODEV;
210
211 if (!drv || !dev || !dev->enabled)
212 return -EBUSY;
213
214 return cpuidle_curr_governor->select(drv, dev);
215 }
216
217 /**
218 * cpuidle_enter - enter into the specified idle state
219 *
220 * @drv: the cpuidle driver tied with the cpu
221 * @dev: the cpuidle device
222 * @index: the index in the idle state table
223 *
224 * Returns the index in the idle state, < 0 in case of error.
225 * The error code depends on the backend driver
226 */
227 int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
228 int index)
229 {
230 if (cpuidle_state_is_coupled(dev, drv, index))
231 return cpuidle_enter_state_coupled(dev, drv, index);
232 return cpuidle_enter_state(dev, drv, index);
233 }
234
235 /**
236 * cpuidle_reflect - tell the underlying governor what was the state
237 * we were in
238 *
239 * @dev : the cpuidle device
240 * @index: the index in the idle state table
241 *
242 */
243 void cpuidle_reflect(struct cpuidle_device *dev, int index)
244 {
245 if (cpuidle_curr_governor->reflect)
246 cpuidle_curr_governor->reflect(dev, index);
247 }
248
249 /**
250 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
251 */
252 void cpuidle_install_idle_handler(void)
253 {
254 if (enabled_devices) {
255 /* Make sure all changes finished before we switch to new idle */
256 smp_wmb();
257 initialized = 1;
258 }
259 }
260
261 /**
262 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
263 */
264 void cpuidle_uninstall_idle_handler(void)
265 {
266 if (enabled_devices) {
267 initialized = 0;
268 wake_up_all_idle_cpus();
269 }
270
271 /*
272 * Make sure external observers (such as the scheduler)
273 * are done looking at pointed idle states.
274 */
275 synchronize_rcu();
276 }
277
278 /**
279 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
280 */
281 void cpuidle_pause_and_lock(void)
282 {
283 mutex_lock(&cpuidle_lock);
284 cpuidle_uninstall_idle_handler();
285 }
286
287 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
288
289 /**
290 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
291 */
292 void cpuidle_resume_and_unlock(void)
293 {
294 cpuidle_install_idle_handler();
295 mutex_unlock(&cpuidle_lock);
296 }
297
298 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
299
300 /* Currently used in suspend/resume path to suspend cpuidle */
301 void cpuidle_pause(void)
302 {
303 mutex_lock(&cpuidle_lock);
304 cpuidle_uninstall_idle_handler();
305 mutex_unlock(&cpuidle_lock);
306 }
307
308 /* Currently used in suspend/resume path to resume cpuidle */
309 void cpuidle_resume(void)
310 {
311 mutex_lock(&cpuidle_lock);
312 cpuidle_install_idle_handler();
313 mutex_unlock(&cpuidle_lock);
314 }
315
316 /**
317 * cpuidle_enable_device - enables idle PM for a CPU
318 * @dev: the CPU
319 *
320 * This function must be called between cpuidle_pause_and_lock and
321 * cpuidle_resume_and_unlock when used externally.
322 */
323 int cpuidle_enable_device(struct cpuidle_device *dev)
324 {
325 int ret;
326 struct cpuidle_driver *drv;
327
328 if (!dev)
329 return -EINVAL;
330
331 if (dev->enabled)
332 return 0;
333
334 drv = cpuidle_get_cpu_driver(dev);
335
336 if (!drv || !cpuidle_curr_governor)
337 return -EIO;
338
339 if (!dev->registered)
340 return -EINVAL;
341
342 if (!dev->state_count)
343 dev->state_count = drv->state_count;
344
345 ret = cpuidle_add_device_sysfs(dev);
346 if (ret)
347 return ret;
348
349 if (cpuidle_curr_governor->enable &&
350 (ret = cpuidle_curr_governor->enable(drv, dev)))
351 goto fail_sysfs;
352
353 smp_wmb();
354
355 dev->enabled = 1;
356
357 enabled_devices++;
358 return 0;
359
360 fail_sysfs:
361 cpuidle_remove_device_sysfs(dev);
362
363 return ret;
364 }
365
366 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
367
368 /**
369 * cpuidle_disable_device - disables idle PM for a CPU
370 * @dev: the CPU
371 *
372 * This function must be called between cpuidle_pause_and_lock and
373 * cpuidle_resume_and_unlock when used externally.
374 */
375 void cpuidle_disable_device(struct cpuidle_device *dev)
376 {
377 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
378
379 if (!dev || !dev->enabled)
380 return;
381
382 if (!drv || !cpuidle_curr_governor)
383 return;
384
385 dev->enabled = 0;
386
387 if (cpuidle_curr_governor->disable)
388 cpuidle_curr_governor->disable(drv, dev);
389
390 cpuidle_remove_device_sysfs(dev);
391 enabled_devices--;
392 }
393
394 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
395
396 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
397 {
398 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
399
400 list_del(&dev->device_list);
401 per_cpu(cpuidle_devices, dev->cpu) = NULL;
402 module_put(drv->owner);
403 }
404
405 static void __cpuidle_device_init(struct cpuidle_device *dev)
406 {
407 memset(dev->states_usage, 0, sizeof(dev->states_usage));
408 dev->last_residency = 0;
409 }
410
411 /**
412 * __cpuidle_register_device - internal register function called before register
413 * and enable routines
414 * @dev: the cpu
415 *
416 * cpuidle_lock mutex must be held before this is called
417 */
418 static int __cpuidle_register_device(struct cpuidle_device *dev)
419 {
420 int ret;
421 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
422
423 if (!try_module_get(drv->owner))
424 return -EINVAL;
425
426 per_cpu(cpuidle_devices, dev->cpu) = dev;
427 list_add(&dev->device_list, &cpuidle_detected_devices);
428
429 ret = cpuidle_coupled_register_device(dev);
430 if (ret)
431 __cpuidle_unregister_device(dev);
432 else
433 dev->registered = 1;
434
435 return ret;
436 }
437
438 /**
439 * cpuidle_register_device - registers a CPU's idle PM feature
440 * @dev: the cpu
441 */
442 int cpuidle_register_device(struct cpuidle_device *dev)
443 {
444 int ret = -EBUSY;
445
446 if (!dev)
447 return -EINVAL;
448
449 mutex_lock(&cpuidle_lock);
450
451 if (dev->registered)
452 goto out_unlock;
453
454 __cpuidle_device_init(dev);
455
456 ret = __cpuidle_register_device(dev);
457 if (ret)
458 goto out_unlock;
459
460 ret = cpuidle_add_sysfs(dev);
461 if (ret)
462 goto out_unregister;
463
464 ret = cpuidle_enable_device(dev);
465 if (ret)
466 goto out_sysfs;
467
468 cpuidle_install_idle_handler();
469
470 out_unlock:
471 mutex_unlock(&cpuidle_lock);
472
473 return ret;
474
475 out_sysfs:
476 cpuidle_remove_sysfs(dev);
477 out_unregister:
478 __cpuidle_unregister_device(dev);
479 goto out_unlock;
480 }
481
482 EXPORT_SYMBOL_GPL(cpuidle_register_device);
483
484 /**
485 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
486 * @dev: the cpu
487 */
488 void cpuidle_unregister_device(struct cpuidle_device *dev)
489 {
490 if (!dev || dev->registered == 0)
491 return;
492
493 cpuidle_pause_and_lock();
494
495 cpuidle_disable_device(dev);
496
497 cpuidle_remove_sysfs(dev);
498
499 __cpuidle_unregister_device(dev);
500
501 cpuidle_coupled_unregister_device(dev);
502
503 cpuidle_resume_and_unlock();
504 }
505
506 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
507
508 /**
509 * cpuidle_unregister: unregister a driver and the devices. This function
510 * can be used only if the driver has been previously registered through
511 * the cpuidle_register function.
512 *
513 * @drv: a valid pointer to a struct cpuidle_driver
514 */
515 void cpuidle_unregister(struct cpuidle_driver *drv)
516 {
517 int cpu;
518 struct cpuidle_device *device;
519
520 for_each_cpu(cpu, drv->cpumask) {
521 device = &per_cpu(cpuidle_dev, cpu);
522 cpuidle_unregister_device(device);
523 }
524
525 cpuidle_unregister_driver(drv);
526 }
527 EXPORT_SYMBOL_GPL(cpuidle_unregister);
528
529 /**
530 * cpuidle_register: registers the driver and the cpu devices with the
531 * coupled_cpus passed as parameter. This function is used for all common
532 * initialization pattern there are in the arch specific drivers. The
533 * devices is globally defined in this file.
534 *
535 * @drv : a valid pointer to a struct cpuidle_driver
536 * @coupled_cpus: a cpumask for the coupled states
537 *
538 * Returns 0 on success, < 0 otherwise
539 */
540 int cpuidle_register(struct cpuidle_driver *drv,
541 const struct cpumask *const coupled_cpus)
542 {
543 int ret, cpu;
544 struct cpuidle_device *device;
545
546 ret = cpuidle_register_driver(drv);
547 if (ret) {
548 pr_err("failed to register cpuidle driver\n");
549 return ret;
550 }
551
552 for_each_cpu(cpu, drv->cpumask) {
553 device = &per_cpu(cpuidle_dev, cpu);
554 device->cpu = cpu;
555
556 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
557 /*
558 * On multiplatform for ARM, the coupled idle states could be
559 * enabled in the kernel even if the cpuidle driver does not
560 * use it. Note, coupled_cpus is a struct copy.
561 */
562 if (coupled_cpus)
563 device->coupled_cpus = *coupled_cpus;
564 #endif
565 ret = cpuidle_register_device(device);
566 if (!ret)
567 continue;
568
569 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
570
571 cpuidle_unregister(drv);
572 break;
573 }
574
575 return ret;
576 }
577 EXPORT_SYMBOL_GPL(cpuidle_register);
578
579 #ifdef CONFIG_SMP
580
581 /*
582 * This function gets called when a part of the kernel has a new latency
583 * requirement. This means we need to get all processors out of their C-state,
584 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
585 * wakes them all right up.
586 */
587 static int cpuidle_latency_notify(struct notifier_block *b,
588 unsigned long l, void *v)
589 {
590 wake_up_all_idle_cpus();
591 return NOTIFY_OK;
592 }
593
594 static struct notifier_block cpuidle_latency_notifier = {
595 .notifier_call = cpuidle_latency_notify,
596 };
597
598 static inline void latency_notifier_init(struct notifier_block *n)
599 {
600 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
601 }
602
603 #else /* CONFIG_SMP */
604
605 #define latency_notifier_init(x) do { } while (0)
606
607 #endif /* CONFIG_SMP */
608
609 /**
610 * cpuidle_init - core initializer
611 */
612 static int __init cpuidle_init(void)
613 {
614 int ret;
615
616 if (cpuidle_disabled())
617 return -ENODEV;
618
619 ret = cpuidle_add_interface(cpu_subsys.dev_root);
620 if (ret)
621 return ret;
622
623 latency_notifier_init(&cpuidle_latency_notifier);
624
625 return 0;
626 }
627
628 module_param(off, int, 0444);
629 core_initcall(cpuidle_init);
This page took 0.042151 seconds and 6 git commands to generate.