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