0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "linux/percpu-defs.h"
0012 #include <linux/clockchips.h>
0013 #include <linux/kernel.h>
0014 #include <linux/mutex.h>
0015 #include <linux/sched.h>
0016 #include <linux/sched/clock.h>
0017 #include <linux/notifier.h>
0018 #include <linux/pm_qos.h>
0019 #include <linux/cpu.h>
0020 #include <linux/cpuidle.h>
0021 #include <linux/ktime.h>
0022 #include <linux/hrtimer.h>
0023 #include <linux/module.h>
0024 #include <linux/suspend.h>
0025 #include <linux/tick.h>
0026 #include <linux/mmu_context.h>
0027 #include <linux/context_tracking.h>
0028 #include <trace/events/power.h>
0029
0030 #include "cpuidle.h"
0031
0032 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
0033 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
0034
0035 DEFINE_MUTEX(cpuidle_lock);
0036 LIST_HEAD(cpuidle_detected_devices);
0037
0038 static int enabled_devices;
0039 static int off __read_mostly;
0040 static int initialized __read_mostly;
0041
0042 int cpuidle_disabled(void)
0043 {
0044 return off;
0045 }
0046 void disable_cpuidle(void)
0047 {
0048 off = 1;
0049 }
0050
0051 bool cpuidle_not_available(struct cpuidle_driver *drv,
0052 struct cpuidle_device *dev)
0053 {
0054 return off || !initialized || !drv || !dev || !dev->enabled;
0055 }
0056
0057
0058
0059
0060
0061
0062 int cpuidle_play_dead(void)
0063 {
0064 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
0065 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0066 int i;
0067
0068 if (!drv)
0069 return -ENODEV;
0070
0071
0072 for (i = drv->state_count - 1; i >= 0; i--)
0073 if (drv->states[i].enter_dead)
0074 return drv->states[i].enter_dead(dev, i);
0075
0076 return -ENODEV;
0077 }
0078
0079 static int find_deepest_state(struct cpuidle_driver *drv,
0080 struct cpuidle_device *dev,
0081 u64 max_latency_ns,
0082 unsigned int forbidden_flags,
0083 bool s2idle)
0084 {
0085 u64 latency_req = 0;
0086 int i, ret = 0;
0087
0088 for (i = 1; i < drv->state_count; i++) {
0089 struct cpuidle_state *s = &drv->states[i];
0090
0091 if (dev->states_usage[i].disable ||
0092 s->exit_latency_ns <= latency_req ||
0093 s->exit_latency_ns > max_latency_ns ||
0094 (s->flags & forbidden_flags) ||
0095 (s2idle && !s->enter_s2idle))
0096 continue;
0097
0098 latency_req = s->exit_latency_ns;
0099 ret = i;
0100 }
0101 return ret;
0102 }
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 void cpuidle_use_deepest_state(u64 latency_limit_ns)
0113 {
0114 struct cpuidle_device *dev;
0115
0116 preempt_disable();
0117 dev = cpuidle_get_device();
0118 if (dev)
0119 dev->forced_idle_latency_limit_ns = latency_limit_ns;
0120 preempt_enable();
0121 }
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131 int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
0132 struct cpuidle_device *dev,
0133 u64 latency_limit_ns)
0134 {
0135 return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
0136 }
0137
0138 #ifdef CONFIG_SUSPEND
0139 static void enter_s2idle_proper(struct cpuidle_driver *drv,
0140 struct cpuidle_device *dev, int index)
0141 {
0142 ktime_t time_start, time_end;
0143 struct cpuidle_state *target_state = &drv->states[index];
0144
0145 time_start = ns_to_ktime(local_clock());
0146
0147 tick_freeze();
0148
0149
0150
0151
0152
0153 stop_critical_timings();
0154 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0155 ct_idle_enter();
0156 target_state->enter_s2idle(dev, drv, index);
0157 if (WARN_ON_ONCE(!irqs_disabled()))
0158 local_irq_disable();
0159 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0160 ct_idle_exit();
0161 tick_unfreeze();
0162 start_critical_timings();
0163
0164 time_end = ns_to_ktime(local_clock());
0165
0166 dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
0167 dev->states_usage[index].s2idle_usage++;
0168 }
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
0179 {
0180 int index;
0181
0182
0183
0184
0185
0186
0187 index = find_deepest_state(drv, dev, U64_MAX, 0, true);
0188 if (index > 0) {
0189 enter_s2idle_proper(drv, dev, index);
0190 local_irq_enable();
0191 }
0192 return index;
0193 }
0194 #endif
0195
0196
0197
0198
0199
0200
0201
0202 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
0203 int index)
0204 {
0205 int entered_state;
0206
0207 struct cpuidle_state *target_state = &drv->states[index];
0208 bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
0209 ktime_t time_start, time_end;
0210
0211
0212
0213
0214
0215
0216 if (broadcast && tick_broadcast_enter()) {
0217 index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
0218 CPUIDLE_FLAG_TIMER_STOP, false);
0219 if (index < 0) {
0220 default_idle_call();
0221 return -EBUSY;
0222 }
0223 target_state = &drv->states[index];
0224 broadcast = false;
0225 }
0226
0227 if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
0228 leave_mm(dev->cpu);
0229
0230
0231 sched_idle_set_state(target_state);
0232
0233 trace_cpu_idle(index, dev->cpu);
0234 time_start = ns_to_ktime(local_clock());
0235
0236 stop_critical_timings();
0237 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0238 ct_idle_enter();
0239 entered_state = target_state->enter(dev, drv, index);
0240 if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0241 ct_idle_exit();
0242 start_critical_timings();
0243
0244 sched_clock_idle_wakeup_event();
0245 time_end = ns_to_ktime(local_clock());
0246 trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
0247
0248
0249 sched_idle_set_state(NULL);
0250
0251 if (broadcast) {
0252 if (WARN_ON_ONCE(!irqs_disabled()))
0253 local_irq_disable();
0254
0255 tick_broadcast_exit();
0256 }
0257
0258 if (!cpuidle_state_is_coupled(drv, index))
0259 local_irq_enable();
0260
0261 if (entered_state >= 0) {
0262 s64 diff, delay = drv->states[entered_state].exit_latency_ns;
0263 int i;
0264
0265
0266
0267
0268
0269
0270 diff = ktime_sub(time_end, time_start);
0271
0272 dev->last_residency_ns = diff;
0273 dev->states_usage[entered_state].time_ns += diff;
0274 dev->states_usage[entered_state].usage++;
0275
0276 if (diff < drv->states[entered_state].target_residency_ns) {
0277 for (i = entered_state - 1; i >= 0; i--) {
0278 if (dev->states_usage[i].disable)
0279 continue;
0280
0281
0282 dev->states_usage[entered_state].above++;
0283 trace_cpu_idle_miss(dev->cpu, entered_state, false);
0284 break;
0285 }
0286 } else if (diff > delay) {
0287 for (i = entered_state + 1; i < drv->state_count; i++) {
0288 if (dev->states_usage[i].disable)
0289 continue;
0290
0291
0292
0293
0294
0295 if (diff - delay >= drv->states[i].target_residency_ns) {
0296 dev->states_usage[entered_state].below++;
0297 trace_cpu_idle_miss(dev->cpu, entered_state, true);
0298 }
0299
0300 break;
0301 }
0302 }
0303 } else {
0304 dev->last_residency_ns = 0;
0305 dev->states_usage[index].rejected++;
0306 }
0307
0308 return entered_state;
0309 }
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
0325 bool *stop_tick)
0326 {
0327 return cpuidle_curr_governor->select(drv, dev, stop_tick);
0328 }
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340 int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
0341 int index)
0342 {
0343 int ret = 0;
0344
0345
0346
0347
0348
0349
0350
0351 WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
0352
0353 if (cpuidle_state_is_coupled(drv, index))
0354 ret = cpuidle_enter_state_coupled(dev, drv, index);
0355 else
0356 ret = cpuidle_enter_state(dev, drv, index);
0357
0358 WRITE_ONCE(dev->next_hrtimer, 0);
0359 return ret;
0360 }
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 void cpuidle_reflect(struct cpuidle_device *dev, int index)
0371 {
0372 if (cpuidle_curr_governor->reflect && index >= 0)
0373 cpuidle_curr_governor->reflect(dev, index);
0374 }
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386 #define CPUIDLE_POLL_MIN 10000
0387 #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397 u64 cpuidle_poll_time(struct cpuidle_driver *drv,
0398 struct cpuidle_device *dev)
0399 {
0400 int i;
0401 u64 limit_ns;
0402
0403 BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
0404
0405 if (dev->poll_limit_ns)
0406 return dev->poll_limit_ns;
0407
0408 limit_ns = CPUIDLE_POLL_MAX;
0409 for (i = 1; i < drv->state_count; i++) {
0410 u64 state_limit;
0411
0412 if (dev->states_usage[i].disable)
0413 continue;
0414
0415 state_limit = drv->states[i].target_residency_ns;
0416 if (state_limit < CPUIDLE_POLL_MIN)
0417 continue;
0418
0419 limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
0420 break;
0421 }
0422
0423 dev->poll_limit_ns = limit_ns;
0424
0425 return dev->poll_limit_ns;
0426 }
0427
0428
0429
0430
0431 void cpuidle_install_idle_handler(void)
0432 {
0433 if (enabled_devices) {
0434
0435 smp_wmb();
0436 initialized = 1;
0437 }
0438 }
0439
0440
0441
0442
0443 void cpuidle_uninstall_idle_handler(void)
0444 {
0445 if (enabled_devices) {
0446 initialized = 0;
0447 wake_up_all_idle_cpus();
0448 }
0449
0450
0451
0452
0453
0454 synchronize_rcu();
0455 }
0456
0457
0458
0459
0460 void cpuidle_pause_and_lock(void)
0461 {
0462 mutex_lock(&cpuidle_lock);
0463 cpuidle_uninstall_idle_handler();
0464 }
0465
0466 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
0467
0468
0469
0470
0471 void cpuidle_resume_and_unlock(void)
0472 {
0473 cpuidle_install_idle_handler();
0474 mutex_unlock(&cpuidle_lock);
0475 }
0476
0477 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
0478
0479
0480 void cpuidle_pause(void)
0481 {
0482 mutex_lock(&cpuidle_lock);
0483 cpuidle_uninstall_idle_handler();
0484 mutex_unlock(&cpuidle_lock);
0485 }
0486
0487
0488 void cpuidle_resume(void)
0489 {
0490 mutex_lock(&cpuidle_lock);
0491 cpuidle_install_idle_handler();
0492 mutex_unlock(&cpuidle_lock);
0493 }
0494
0495
0496
0497
0498
0499
0500
0501
0502 int cpuidle_enable_device(struct cpuidle_device *dev)
0503 {
0504 int ret;
0505 struct cpuidle_driver *drv;
0506
0507 if (!dev)
0508 return -EINVAL;
0509
0510 if (dev->enabled)
0511 return 0;
0512
0513 if (!cpuidle_curr_governor)
0514 return -EIO;
0515
0516 drv = cpuidle_get_cpu_driver(dev);
0517
0518 if (!drv)
0519 return -EIO;
0520
0521 if (!dev->registered)
0522 return -EINVAL;
0523
0524 ret = cpuidle_add_device_sysfs(dev);
0525 if (ret)
0526 return ret;
0527
0528 if (cpuidle_curr_governor->enable) {
0529 ret = cpuidle_curr_governor->enable(drv, dev);
0530 if (ret)
0531 goto fail_sysfs;
0532 }
0533
0534 smp_wmb();
0535
0536 dev->enabled = 1;
0537
0538 enabled_devices++;
0539 return 0;
0540
0541 fail_sysfs:
0542 cpuidle_remove_device_sysfs(dev);
0543
0544 return ret;
0545 }
0546
0547 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
0548
0549
0550
0551
0552
0553
0554
0555
0556 void cpuidle_disable_device(struct cpuidle_device *dev)
0557 {
0558 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0559
0560 if (!dev || !dev->enabled)
0561 return;
0562
0563 if (!drv || !cpuidle_curr_governor)
0564 return;
0565
0566 dev->enabled = 0;
0567
0568 if (cpuidle_curr_governor->disable)
0569 cpuidle_curr_governor->disable(drv, dev);
0570
0571 cpuidle_remove_device_sysfs(dev);
0572 enabled_devices--;
0573 }
0574
0575 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
0576
0577 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
0578 {
0579 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0580
0581 list_del(&dev->device_list);
0582 per_cpu(cpuidle_devices, dev->cpu) = NULL;
0583 module_put(drv->owner);
0584
0585 dev->registered = 0;
0586 }
0587
0588 static void __cpuidle_device_init(struct cpuidle_device *dev)
0589 {
0590 memset(dev->states_usage, 0, sizeof(dev->states_usage));
0591 dev->last_residency_ns = 0;
0592 dev->next_hrtimer = 0;
0593 }
0594
0595
0596
0597
0598
0599
0600
0601
0602 static int __cpuidle_register_device(struct cpuidle_device *dev)
0603 {
0604 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0605 int i, ret;
0606
0607 if (!try_module_get(drv->owner))
0608 return -EINVAL;
0609
0610 for (i = 0; i < drv->state_count; i++) {
0611 if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
0612 dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
0613
0614 if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
0615 dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
0616 }
0617
0618 per_cpu(cpuidle_devices, dev->cpu) = dev;
0619 list_add(&dev->device_list, &cpuidle_detected_devices);
0620
0621 ret = cpuidle_coupled_register_device(dev);
0622 if (ret)
0623 __cpuidle_unregister_device(dev);
0624 else
0625 dev->registered = 1;
0626
0627 return ret;
0628 }
0629
0630
0631
0632
0633
0634 int cpuidle_register_device(struct cpuidle_device *dev)
0635 {
0636 int ret = -EBUSY;
0637
0638 if (!dev)
0639 return -EINVAL;
0640
0641 mutex_lock(&cpuidle_lock);
0642
0643 if (dev->registered)
0644 goto out_unlock;
0645
0646 __cpuidle_device_init(dev);
0647
0648 ret = __cpuidle_register_device(dev);
0649 if (ret)
0650 goto out_unlock;
0651
0652 ret = cpuidle_add_sysfs(dev);
0653 if (ret)
0654 goto out_unregister;
0655
0656 ret = cpuidle_enable_device(dev);
0657 if (ret)
0658 goto out_sysfs;
0659
0660 cpuidle_install_idle_handler();
0661
0662 out_unlock:
0663 mutex_unlock(&cpuidle_lock);
0664
0665 return ret;
0666
0667 out_sysfs:
0668 cpuidle_remove_sysfs(dev);
0669 out_unregister:
0670 __cpuidle_unregister_device(dev);
0671 goto out_unlock;
0672 }
0673
0674 EXPORT_SYMBOL_GPL(cpuidle_register_device);
0675
0676
0677
0678
0679
0680 void cpuidle_unregister_device(struct cpuidle_device *dev)
0681 {
0682 if (!dev || dev->registered == 0)
0683 return;
0684
0685 cpuidle_pause_and_lock();
0686
0687 cpuidle_disable_device(dev);
0688
0689 cpuidle_remove_sysfs(dev);
0690
0691 __cpuidle_unregister_device(dev);
0692
0693 cpuidle_coupled_unregister_device(dev);
0694
0695 cpuidle_resume_and_unlock();
0696 }
0697
0698 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
0699
0700
0701
0702
0703
0704
0705
0706
0707 void cpuidle_unregister(struct cpuidle_driver *drv)
0708 {
0709 int cpu;
0710 struct cpuidle_device *device;
0711
0712 for_each_cpu(cpu, drv->cpumask) {
0713 device = &per_cpu(cpuidle_dev, cpu);
0714 cpuidle_unregister_device(device);
0715 }
0716
0717 cpuidle_unregister_driver(drv);
0718 }
0719 EXPORT_SYMBOL_GPL(cpuidle_unregister);
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732 int cpuidle_register(struct cpuidle_driver *drv,
0733 const struct cpumask *const coupled_cpus)
0734 {
0735 int ret, cpu;
0736 struct cpuidle_device *device;
0737
0738 ret = cpuidle_register_driver(drv);
0739 if (ret) {
0740 pr_err("failed to register cpuidle driver\n");
0741 return ret;
0742 }
0743
0744 for_each_cpu(cpu, drv->cpumask) {
0745 device = &per_cpu(cpuidle_dev, cpu);
0746 device->cpu = cpu;
0747
0748 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
0749
0750
0751
0752
0753
0754 if (coupled_cpus)
0755 device->coupled_cpus = *coupled_cpus;
0756 #endif
0757 ret = cpuidle_register_device(device);
0758 if (!ret)
0759 continue;
0760
0761 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
0762
0763 cpuidle_unregister(drv);
0764 break;
0765 }
0766
0767 return ret;
0768 }
0769 EXPORT_SYMBOL_GPL(cpuidle_register);
0770
0771
0772
0773
0774 static int __init cpuidle_init(void)
0775 {
0776 if (cpuidle_disabled())
0777 return -ENODEV;
0778
0779 return cpuidle_add_interface(cpu_subsys.dev_root);
0780 }
0781
0782 module_param(off, int, 0444);
0783 module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
0784 core_initcall(cpuidle_init);