]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/cpuidle/cpuidle.c
cpuidle: Add missing forward declarations of structures
[mirror_ubuntu-bionic-kernel.git] / drivers / cpuidle / cpuidle.c
CommitLineData
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
b60e6a0e 11#include <linux/clockchips.h>
4f86d3a8
LB
12#include <linux/kernel.h>
13#include <linux/mutex.h>
14#include <linux/sched.h>
15#include <linux/notifier.h>
e8db0be1 16#include <linux/pm_qos.h>
4f86d3a8
LB
17#include <linux/cpu.h>
18#include <linux/cpuidle.h>
9a0b8415 19#include <linux/ktime.h>
2e94d1f7 20#include <linux/hrtimer.h>
884b17e1 21#include <linux/module.h>
288f023e 22#include <trace/events/power.h>
4f86d3a8
LB
23
24#include "cpuidle.h"
25
26DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
4c637b21 27DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
4f86d3a8
LB
28
29DEFINE_MUTEX(cpuidle_lock);
30LIST_HEAD(cpuidle_detected_devices);
4f86d3a8
LB
31
32static int enabled_devices;
62027aea 33static int off __read_mostly;
a0bfa137 34static int initialized __read_mostly;
62027aea
LB
35
36int cpuidle_disabled(void)
37{
38 return off;
39}
d91ee586
LB
40void disable_cpuidle(void)
41{
42 off = 1;
43}
4f86d3a8 44
dcb84f33
VP
45static int __cpuidle_register_device(struct cpuidle_device *dev);
46
1a022e3f
BO
47/**
48 * cpuidle_play_dead - cpu off-lining
49 *
ee01e663 50 * Returns in case of an error or no driver
1a022e3f
BO
51 */
52int cpuidle_play_dead(void)
53{
54 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 55 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
8aef33a7 56 int i;
1a022e3f 57
ee01e663
TK
58 if (!drv)
59 return -ENODEV;
60
1a022e3f 61 /* Find lowest-power state that supports long-term idle */
8aef33a7
DL
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);
1a022e3f
BO
65
66 return -ENODEV;
67}
68
56cfbf74
CC
69/**
70 * cpuidle_enter_state - enter the state and update stats
71 * @dev: cpuidle device for this cpu
72 * @drv: cpuidle driver for this cpu
73 * @next_state: index into drv->states of the state to enter
74 */
75int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
554c06ba 76 int index)
56cfbf74
CC
77{
78 int entered_state;
79
554c06ba
DL
80 struct cpuidle_state *target_state = &drv->states[index];
81 ktime_t time_start, time_end;
82 s64 diff;
83
84 time_start = ktime_get();
85
86 entered_state = target_state->enter(dev, drv, index);
87
88 time_end = ktime_get();
89
90 local_irq_enable();
91
92 diff = ktime_to_us(ktime_sub(time_end, time_start));
93 if (diff > INT_MAX)
94 diff = INT_MAX;
95
96 dev->last_residency = (int) diff;
56cfbf74
CC
97
98 if (entered_state >= 0) {
99 /* Update cpuidle counters */
100 /* This can be moved to within driver enter routine
101 * but that results in multiple copies of same code.
102 */
a474a515 103 dev->states_usage[entered_state].time += dev->last_residency;
56cfbf74
CC
104 dev->states_usage[entered_state].usage++;
105 } else {
106 dev->last_residency = 0;
107 }
108
109 return entered_state;
110}
111
4f86d3a8
LB
112/**
113 * cpuidle_idle_call - the main idle loop
114 *
115 * NOTE: no locks or semaphores should be used here
a0bfa137 116 * return non-zero on failure
4f86d3a8 117 */
a0bfa137 118int cpuidle_idle_call(void)
4f86d3a8 119{
4a6f4fe8 120 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 121 struct cpuidle_driver *drv;
e978aa7d 122 int next_state, entered_state;
4f86d3a8 123
a0bfa137
LB
124 if (off)
125 return -ENODEV;
126
127 if (!initialized)
128 return -ENODEV;
129
4f86d3a8 130 /* check if the device is ready */
a0bfa137
LB
131 if (!dev || !dev->enabled)
132 return -EBUSY;
4f86d3a8 133
bf4d1b5d
DL
134 drv = cpuidle_get_cpu_driver(dev);
135
4f86d3a8 136 /* ask the governor for the next state */
46bcfad7 137 next_state = cpuidle_curr_governor->select(drv, dev);
246eb7f0 138 if (need_resched()) {
d73d68dc
YS
139 dev->last_residency = 0;
140 /* give the governor an opportunity to reflect on the outcome */
141 if (cpuidle_curr_governor->reflect)
142 cpuidle_curr_governor->reflect(dev, next_state);
246eb7f0 143 local_irq_enable();
a0bfa137 144 return 0;
246eb7f0
KH
145 }
146
76027ea8 147 trace_cpu_idle_rcuidle(next_state, dev->cpu);
f77cfe4e 148
b60e6a0e
DL
149 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
150 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
151 &dev->cpu);
152
4126c019
CC
153 if (cpuidle_state_is_coupled(dev, drv, next_state))
154 entered_state = cpuidle_enter_state_coupled(dev, drv,
155 next_state);
156 else
157 entered_state = cpuidle_enter_state(dev, drv, next_state);
f77cfe4e 158
b60e6a0e
DL
159 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
160 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
161 &dev->cpu);
162
76027ea8 163 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
f77cfe4e 164
4f86d3a8
LB
165 /* give the governor an opportunity to reflect on the outcome */
166 if (cpuidle_curr_governor->reflect)
e978aa7d 167 cpuidle_curr_governor->reflect(dev, entered_state);
a0bfa137
LB
168
169 return 0;
4f86d3a8
LB
170}
171
172/**
173 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
174 */
175void cpuidle_install_idle_handler(void)
176{
a0bfa137 177 if (enabled_devices) {
4f86d3a8
LB
178 /* Make sure all changes finished before we switch to new idle */
179 smp_wmb();
a0bfa137 180 initialized = 1;
4f86d3a8
LB
181 }
182}
183
184/**
185 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
186 */
187void cpuidle_uninstall_idle_handler(void)
188{
a0bfa137
LB
189 if (enabled_devices) {
190 initialized = 0;
4a162513 191 kick_all_cpus_sync();
4f86d3a8
LB
192 }
193}
194
195/**
196 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
197 */
198void cpuidle_pause_and_lock(void)
199{
200 mutex_lock(&cpuidle_lock);
201 cpuidle_uninstall_idle_handler();
202}
203
204EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
205
206/**
207 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
208 */
209void cpuidle_resume_and_unlock(void)
210{
211 cpuidle_install_idle_handler();
212 mutex_unlock(&cpuidle_lock);
213}
214
215EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
216
8651f97b
PM
217/* Currently used in suspend/resume path to suspend cpuidle */
218void cpuidle_pause(void)
219{
220 mutex_lock(&cpuidle_lock);
221 cpuidle_uninstall_idle_handler();
222 mutex_unlock(&cpuidle_lock);
223}
224
225/* Currently used in suspend/resume path to resume cpuidle */
226void cpuidle_resume(void)
227{
228 mutex_lock(&cpuidle_lock);
229 cpuidle_install_idle_handler();
230 mutex_unlock(&cpuidle_lock);
231}
232
d8c216cf 233#ifdef CONFIG_ARCH_HAS_CPU_RELAX
46bcfad7
DD
234static int poll_idle(struct cpuidle_device *dev,
235 struct cpuidle_driver *drv, int index)
d8c216cf
RW
236{
237 ktime_t t1, t2;
238 s64 diff;
d8c216cf
RW
239
240 t1 = ktime_get();
241 local_irq_enable();
242 while (!need_resched())
243 cpu_relax();
244
245 t2 = ktime_get();
246 diff = ktime_to_us(ktime_sub(t2, t1));
247 if (diff > INT_MAX)
248 diff = INT_MAX;
249
e978aa7d
DD
250 dev->last_residency = (int) diff;
251
252 return index;
d8c216cf
RW
253}
254
46bcfad7 255static void poll_idle_init(struct cpuidle_driver *drv)
d8c216cf 256{
46bcfad7 257 struct cpuidle_state *state = &drv->states[0];
d8c216cf 258
720f1c30 259 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
d8c216cf
RW
260 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
261 state->exit_latency = 0;
262 state->target_residency = 0;
263 state->power_usage = -1;
d247632c 264 state->flags = 0;
d8c216cf 265 state->enter = poll_idle;
cbc9ef02 266 state->disabled = false;
d8c216cf
RW
267}
268#else
46bcfad7 269static void poll_idle_init(struct cpuidle_driver *drv) {}
d8c216cf
RW
270#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
271
4f86d3a8
LB
272/**
273 * cpuidle_enable_device - enables idle PM for a CPU
274 * @dev: the CPU
275 *
276 * This function must be called between cpuidle_pause_and_lock and
277 * cpuidle_resume_and_unlock when used externally.
278 */
279int cpuidle_enable_device(struct cpuidle_device *dev)
280{
281 int ret, i;
bf4d1b5d 282 struct cpuidle_driver *drv;
4f86d3a8 283
1b0a0e9a
SB
284 if (!dev)
285 return -EINVAL;
286
4f86d3a8
LB
287 if (dev->enabled)
288 return 0;
bf4d1b5d
DL
289
290 drv = cpuidle_get_cpu_driver(dev);
291
e1689795 292 if (!drv || !cpuidle_curr_governor)
4f86d3a8 293 return -EIO;
bf4d1b5d 294
10b9d3f8
DL
295 if (!dev->registered)
296 return -EINVAL;
297
4f86d3a8 298 if (!dev->state_count)
fc850f39 299 dev->state_count = drv->state_count;
4f86d3a8 300
e1689795 301 poll_idle_init(drv);
d8c216cf 302
bf4d1b5d
DL
303 ret = cpuidle_add_device_sysfs(dev);
304 if (ret)
4f86d3a8
LB
305 return ret;
306
307 if (cpuidle_curr_governor->enable &&
e1689795 308 (ret = cpuidle_curr_governor->enable(drv, dev)))
4f86d3a8
LB
309 goto fail_sysfs;
310
311 for (i = 0; i < dev->state_count; i++) {
4202735e
DD
312 dev->states_usage[i].usage = 0;
313 dev->states_usage[i].time = 0;
4f86d3a8
LB
314 }
315 dev->last_residency = 0;
4f86d3a8
LB
316
317 smp_wmb();
318
319 dev->enabled = 1;
320
321 enabled_devices++;
322 return 0;
323
324fail_sysfs:
bf4d1b5d 325 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
326
327 return ret;
328}
329
330EXPORT_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 */
339void cpuidle_disable_device(struct cpuidle_device *dev)
340{
bf4d1b5d
DL
341 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
342
cf31cd1a 343 if (!dev || !dev->enabled)
4f86d3a8 344 return;
bf4d1b5d
DL
345
346 if (!drv || !cpuidle_curr_governor)
4f86d3a8
LB
347 return;
348
349 dev->enabled = 0;
350
351 if (cpuidle_curr_governor->disable)
bf4d1b5d 352 cpuidle_curr_governor->disable(drv, dev);
4f86d3a8 353
bf4d1b5d 354 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
355 enabled_devices--;
356}
357
358EXPORT_SYMBOL_GPL(cpuidle_disable_device);
359
360/**
dcb84f33
VP
361 * __cpuidle_register_device - internal register function called before register
362 * and enable routines
4f86d3a8 363 * @dev: the cpu
dcb84f33
VP
364 *
365 * cpuidle_lock mutex must be held before this is called
4f86d3a8 366 */
dcb84f33 367static int __cpuidle_register_device(struct cpuidle_device *dev)
4f86d3a8
LB
368{
369 int ret;
bf4d1b5d 370 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4f86d3a8 371
bf4d1b5d 372 if (!try_module_get(drv->owner))
4f86d3a8
LB
373 return -EINVAL;
374
4f86d3a8
LB
375 per_cpu(cpuidle_devices, dev->cpu) = dev;
376 list_add(&dev->device_list, &cpuidle_detected_devices);
1aef40e2 377 ret = cpuidle_add_sysfs(dev);
3af272ab
CC
378 if (ret)
379 goto err_sysfs;
4f86d3a8 380
4126c019
CC
381 ret = cpuidle_coupled_register_device(dev);
382 if (ret)
383 goto err_coupled;
4f86d3a8 384
dcb84f33
VP
385 dev->registered = 1;
386 return 0;
3af272ab 387
4126c019 388err_coupled:
1aef40e2 389 cpuidle_remove_sysfs(dev);
3af272ab
CC
390err_sysfs:
391 list_del(&dev->device_list);
392 per_cpu(cpuidle_devices, dev->cpu) = NULL;
bf4d1b5d 393 module_put(drv->owner);
3af272ab 394 return ret;
dcb84f33
VP
395}
396
397/**
398 * cpuidle_register_device - registers a CPU's idle PM feature
399 * @dev: the cpu
400 */
401int cpuidle_register_device(struct cpuidle_device *dev)
402{
403 int ret;
404
1b0a0e9a
SB
405 if (!dev)
406 return -EINVAL;
407
dcb84f33
VP
408 mutex_lock(&cpuidle_lock);
409
410 if ((ret = __cpuidle_register_device(dev))) {
411 mutex_unlock(&cpuidle_lock);
412 return ret;
413 }
414
10b9d3f8
DL
415 ret = cpuidle_enable_device(dev);
416 if (ret) {
417 mutex_unlock(&cpuidle_lock);
418 return ret;
419 }
420
4f86d3a8
LB
421 cpuidle_install_idle_handler();
422
423 mutex_unlock(&cpuidle_lock);
424
425 return 0;
4f86d3a8
LB
426}
427
428EXPORT_SYMBOL_GPL(cpuidle_register_device);
429
430/**
431 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
432 * @dev: the cpu
433 */
434void cpuidle_unregister_device(struct cpuidle_device *dev)
435{
bf4d1b5d 436 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4f86d3a8 437
dcb84f33
VP
438 if (dev->registered == 0)
439 return;
440
4f86d3a8
LB
441 cpuidle_pause_and_lock();
442
443 cpuidle_disable_device(dev);
444
1aef40e2 445 cpuidle_remove_sysfs(dev);
4f86d3a8 446 list_del(&dev->device_list);
4f86d3a8
LB
447 per_cpu(cpuidle_devices, dev->cpu) = NULL;
448
4126c019
CC
449 cpuidle_coupled_unregister_device(dev);
450
4f86d3a8
LB
451 cpuidle_resume_and_unlock();
452
bf4d1b5d 453 module_put(drv->owner);
4f86d3a8
LB
454}
455
456EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
457
1c192d04 458/**
4c637b21
DL
459 * cpuidle_unregister: unregister a driver and the devices. This function
460 * can be used only if the driver has been previously registered through
461 * the cpuidle_register function.
462 *
463 * @drv: a valid pointer to a struct cpuidle_driver
464 */
465void cpuidle_unregister(struct cpuidle_driver *drv)
466{
467 int cpu;
468 struct cpuidle_device *device;
469
82467a5a 470 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
471 device = &per_cpu(cpuidle_dev, cpu);
472 cpuidle_unregister_device(device);
473 }
474
475 cpuidle_unregister_driver(drv);
476}
477EXPORT_SYMBOL_GPL(cpuidle_unregister);
478
479/**
480 * cpuidle_register: registers the driver and the cpu devices with the
481 * coupled_cpus passed as parameter. This function is used for all common
482 * initialization pattern there are in the arch specific drivers. The
483 * devices is globally defined in this file.
484 *
485 * @drv : a valid pointer to a struct cpuidle_driver
486 * @coupled_cpus: a cpumask for the coupled states
487 *
488 * Returns 0 on success, < 0 otherwise
489 */
490int cpuidle_register(struct cpuidle_driver *drv,
491 const struct cpumask *const coupled_cpus)
492{
493 int ret, cpu;
494 struct cpuidle_device *device;
495
496 ret = cpuidle_register_driver(drv);
497 if (ret) {
498 pr_err("failed to register cpuidle driver\n");
499 return ret;
500 }
501
82467a5a 502 for_each_cpu(cpu, drv->cpumask) {
4c637b21
DL
503 device = &per_cpu(cpuidle_dev, cpu);
504 device->cpu = cpu;
505
506#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
507 /*
508 * On multiplatform for ARM, the coupled idle states could
509 * enabled in the kernel even if the cpuidle driver does not
510 * use it. Note, coupled_cpus is a struct copy.
511 */
512 if (coupled_cpus)
513 device->coupled_cpus = *coupled_cpus;
514#endif
515 ret = cpuidle_register_device(device);
516 if (!ret)
517 continue;
518
519 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
520
521 cpuidle_unregister(drv);
522 break;
523 }
524
525 return ret;
526}
527EXPORT_SYMBOL_GPL(cpuidle_register);
528
4f86d3a8
LB
529#ifdef CONFIG_SMP
530
531static void smp_callback(void *v)
532{
533 /* we already woke the CPU up, nothing more to do */
534}
535
536/*
537 * This function gets called when a part of the kernel has a new latency
538 * requirement. This means we need to get all processors out of their C-state,
539 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
540 * wakes them all right up.
541 */
542static int cpuidle_latency_notify(struct notifier_block *b,
543 unsigned long l, void *v)
544{
8691e5a8 545 smp_call_function(smp_callback, NULL, 1);
4f86d3a8
LB
546 return NOTIFY_OK;
547}
548
549static struct notifier_block cpuidle_latency_notifier = {
550 .notifier_call = cpuidle_latency_notify,
551};
552
d82b3518
MG
553static inline void latency_notifier_init(struct notifier_block *n)
554{
555 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
556}
4f86d3a8
LB
557
558#else /* CONFIG_SMP */
559
560#define latency_notifier_init(x) do { } while (0)
561
562#endif /* CONFIG_SMP */
563
564/**
565 * cpuidle_init - core initializer
566 */
567static int __init cpuidle_init(void)
568{
569 int ret;
570
62027aea
LB
571 if (cpuidle_disabled())
572 return -ENODEV;
573
8a25a2fd 574 ret = cpuidle_add_interface(cpu_subsys.dev_root);
4f86d3a8
LB
575 if (ret)
576 return ret;
577
578 latency_notifier_init(&cpuidle_latency_notifier);
579
580 return 0;
581}
582
62027aea 583module_param(off, int, 0444);
4f86d3a8 584core_initcall(cpuidle_init);