0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) "PM: " fmt
0008
0009 #include <linux/device.h>
0010 #include <linux/slab.h>
0011 #include <linux/sched/signal.h>
0012 #include <linux/capability.h>
0013 #include <linux/export.h>
0014 #include <linux/suspend.h>
0015 #include <linux/seq_file.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/pm_wakeirq.h>
0018 #include <trace/events/power.h>
0019
0020 #include "power.h"
0021
0022 #ifndef CONFIG_SUSPEND
0023 suspend_state_t pm_suspend_target_state;
0024 #define pm_suspend_target_state (PM_SUSPEND_ON)
0025 #endif
0026
0027 #define list_for_each_entry_rcu_locked(pos, head, member) \
0028 list_for_each_entry_rcu(pos, head, member, \
0029 srcu_read_lock_held(&wakeup_srcu))
0030
0031
0032
0033
0034 bool events_check_enabled __read_mostly;
0035
0036
0037 static unsigned int wakeup_irq[2] __read_mostly;
0038 static DEFINE_RAW_SPINLOCK(wakeup_irq_lock);
0039
0040
0041 static atomic_t pm_abort_suspend __read_mostly;
0042
0043
0044
0045
0046
0047
0048 static atomic_t combined_event_count = ATOMIC_INIT(0);
0049
0050 #define IN_PROGRESS_BITS (sizeof(int) * 4)
0051 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
0052
0053 static void split_counters(unsigned int *cnt, unsigned int *inpr)
0054 {
0055 unsigned int comb = atomic_read(&combined_event_count);
0056
0057 *cnt = (comb >> IN_PROGRESS_BITS);
0058 *inpr = comb & MAX_IN_PROGRESS;
0059 }
0060
0061
0062 static unsigned int saved_count;
0063
0064 static DEFINE_RAW_SPINLOCK(events_lock);
0065
0066 static void pm_wakeup_timer_fn(struct timer_list *t);
0067
0068 static LIST_HEAD(wakeup_sources);
0069
0070 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
0071
0072 DEFINE_STATIC_SRCU(wakeup_srcu);
0073
0074 static struct wakeup_source deleted_ws = {
0075 .name = "deleted",
0076 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
0077 };
0078
0079 static DEFINE_IDA(wakeup_ida);
0080
0081
0082
0083
0084
0085 struct wakeup_source *wakeup_source_create(const char *name)
0086 {
0087 struct wakeup_source *ws;
0088 const char *ws_name;
0089 int id;
0090
0091 ws = kzalloc(sizeof(*ws), GFP_KERNEL);
0092 if (!ws)
0093 goto err_ws;
0094
0095 ws_name = kstrdup_const(name, GFP_KERNEL);
0096 if (!ws_name)
0097 goto err_name;
0098 ws->name = ws_name;
0099
0100 id = ida_alloc(&wakeup_ida, GFP_KERNEL);
0101 if (id < 0)
0102 goto err_id;
0103 ws->id = id;
0104
0105 return ws;
0106
0107 err_id:
0108 kfree_const(ws->name);
0109 err_name:
0110 kfree(ws);
0111 err_ws:
0112 return NULL;
0113 }
0114 EXPORT_SYMBOL_GPL(wakeup_source_create);
0115
0116
0117
0118
0119 static void wakeup_source_record(struct wakeup_source *ws)
0120 {
0121 unsigned long flags;
0122
0123 spin_lock_irqsave(&deleted_ws.lock, flags);
0124
0125 if (ws->event_count) {
0126 deleted_ws.total_time =
0127 ktime_add(deleted_ws.total_time, ws->total_time);
0128 deleted_ws.prevent_sleep_time =
0129 ktime_add(deleted_ws.prevent_sleep_time,
0130 ws->prevent_sleep_time);
0131 deleted_ws.max_time =
0132 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
0133 deleted_ws.max_time : ws->max_time;
0134 deleted_ws.event_count += ws->event_count;
0135 deleted_ws.active_count += ws->active_count;
0136 deleted_ws.relax_count += ws->relax_count;
0137 deleted_ws.expire_count += ws->expire_count;
0138 deleted_ws.wakeup_count += ws->wakeup_count;
0139 }
0140
0141 spin_unlock_irqrestore(&deleted_ws.lock, flags);
0142 }
0143
0144 static void wakeup_source_free(struct wakeup_source *ws)
0145 {
0146 ida_free(&wakeup_ida, ws->id);
0147 kfree_const(ws->name);
0148 kfree(ws);
0149 }
0150
0151
0152
0153
0154
0155
0156
0157 void wakeup_source_destroy(struct wakeup_source *ws)
0158 {
0159 if (!ws)
0160 return;
0161
0162 __pm_relax(ws);
0163 wakeup_source_record(ws);
0164 wakeup_source_free(ws);
0165 }
0166 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
0167
0168
0169
0170
0171
0172 void wakeup_source_add(struct wakeup_source *ws)
0173 {
0174 unsigned long flags;
0175
0176 if (WARN_ON(!ws))
0177 return;
0178
0179 spin_lock_init(&ws->lock);
0180 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
0181 ws->active = false;
0182
0183 raw_spin_lock_irqsave(&events_lock, flags);
0184 list_add_rcu(&ws->entry, &wakeup_sources);
0185 raw_spin_unlock_irqrestore(&events_lock, flags);
0186 }
0187 EXPORT_SYMBOL_GPL(wakeup_source_add);
0188
0189
0190
0191
0192
0193 void wakeup_source_remove(struct wakeup_source *ws)
0194 {
0195 unsigned long flags;
0196
0197 if (WARN_ON(!ws))
0198 return;
0199
0200 raw_spin_lock_irqsave(&events_lock, flags);
0201 list_del_rcu(&ws->entry);
0202 raw_spin_unlock_irqrestore(&events_lock, flags);
0203 synchronize_srcu(&wakeup_srcu);
0204
0205 del_timer_sync(&ws->timer);
0206
0207
0208
0209
0210 ws->timer.function = NULL;
0211 }
0212 EXPORT_SYMBOL_GPL(wakeup_source_remove);
0213
0214
0215
0216
0217
0218
0219 struct wakeup_source *wakeup_source_register(struct device *dev,
0220 const char *name)
0221 {
0222 struct wakeup_source *ws;
0223 int ret;
0224
0225 ws = wakeup_source_create(name);
0226 if (ws) {
0227 if (!dev || device_is_registered(dev)) {
0228 ret = wakeup_source_sysfs_add(dev, ws);
0229 if (ret) {
0230 wakeup_source_free(ws);
0231 return NULL;
0232 }
0233 }
0234 wakeup_source_add(ws);
0235 }
0236 return ws;
0237 }
0238 EXPORT_SYMBOL_GPL(wakeup_source_register);
0239
0240
0241
0242
0243
0244 void wakeup_source_unregister(struct wakeup_source *ws)
0245 {
0246 if (ws) {
0247 wakeup_source_remove(ws);
0248 if (ws->dev)
0249 wakeup_source_sysfs_remove(ws);
0250
0251 wakeup_source_destroy(ws);
0252 }
0253 }
0254 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
0255
0256
0257
0258
0259
0260
0261
0262 int wakeup_sources_read_lock(void)
0263 {
0264 return srcu_read_lock(&wakeup_srcu);
0265 }
0266 EXPORT_SYMBOL_GPL(wakeup_sources_read_lock);
0267
0268
0269
0270
0271
0272 void wakeup_sources_read_unlock(int idx)
0273 {
0274 srcu_read_unlock(&wakeup_srcu, idx);
0275 }
0276 EXPORT_SYMBOL_GPL(wakeup_sources_read_unlock);
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286 struct wakeup_source *wakeup_sources_walk_start(void)
0287 {
0288 struct list_head *ws_head = &wakeup_sources;
0289
0290 return list_entry_rcu(ws_head->next, struct wakeup_source, entry);
0291 }
0292 EXPORT_SYMBOL_GPL(wakeup_sources_walk_start);
0293
0294
0295
0296
0297
0298
0299
0300
0301 struct wakeup_source *wakeup_sources_walk_next(struct wakeup_source *ws)
0302 {
0303 struct list_head *ws_head = &wakeup_sources;
0304
0305 return list_next_or_null_rcu(ws_head, &ws->entry,
0306 struct wakeup_source, entry);
0307 }
0308 EXPORT_SYMBOL_GPL(wakeup_sources_walk_next);
0309
0310
0311
0312
0313
0314
0315
0316
0317 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
0318 {
0319 spin_lock_irq(&dev->power.lock);
0320 if (dev->power.wakeup) {
0321 spin_unlock_irq(&dev->power.lock);
0322 return -EEXIST;
0323 }
0324 dev->power.wakeup = ws;
0325 if (dev->power.wakeirq)
0326 device_wakeup_attach_irq(dev, dev->power.wakeirq);
0327 spin_unlock_irq(&dev->power.lock);
0328 return 0;
0329 }
0330
0331
0332
0333
0334
0335
0336
0337 int device_wakeup_enable(struct device *dev)
0338 {
0339 struct wakeup_source *ws;
0340 int ret;
0341
0342 if (!dev || !dev->power.can_wakeup)
0343 return -EINVAL;
0344
0345 if (pm_suspend_target_state != PM_SUSPEND_ON)
0346 dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
0347
0348 ws = wakeup_source_register(dev, dev_name(dev));
0349 if (!ws)
0350 return -ENOMEM;
0351
0352 ret = device_wakeup_attach(dev, ws);
0353 if (ret)
0354 wakeup_source_unregister(ws);
0355
0356 return ret;
0357 }
0358 EXPORT_SYMBOL_GPL(device_wakeup_enable);
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371 void device_wakeup_attach_irq(struct device *dev,
0372 struct wake_irq *wakeirq)
0373 {
0374 struct wakeup_source *ws;
0375
0376 ws = dev->power.wakeup;
0377 if (!ws)
0378 return;
0379
0380 if (ws->wakeirq)
0381 dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
0382
0383 ws->wakeirq = wakeirq;
0384 }
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394 void device_wakeup_detach_irq(struct device *dev)
0395 {
0396 struct wakeup_source *ws;
0397
0398 ws = dev->power.wakeup;
0399 if (ws)
0400 ws->wakeirq = NULL;
0401 }
0402
0403
0404
0405
0406
0407
0408 void device_wakeup_arm_wake_irqs(void)
0409 {
0410 struct wakeup_source *ws;
0411 int srcuidx;
0412
0413 srcuidx = srcu_read_lock(&wakeup_srcu);
0414 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry)
0415 dev_pm_arm_wake_irq(ws->wakeirq);
0416 srcu_read_unlock(&wakeup_srcu, srcuidx);
0417 }
0418
0419
0420
0421
0422
0423
0424 void device_wakeup_disarm_wake_irqs(void)
0425 {
0426 struct wakeup_source *ws;
0427 int srcuidx;
0428
0429 srcuidx = srcu_read_lock(&wakeup_srcu);
0430 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry)
0431 dev_pm_disarm_wake_irq(ws->wakeirq);
0432 srcu_read_unlock(&wakeup_srcu, srcuidx);
0433 }
0434
0435
0436
0437
0438
0439
0440
0441 static struct wakeup_source *device_wakeup_detach(struct device *dev)
0442 {
0443 struct wakeup_source *ws;
0444
0445 spin_lock_irq(&dev->power.lock);
0446 ws = dev->power.wakeup;
0447 dev->power.wakeup = NULL;
0448 spin_unlock_irq(&dev->power.lock);
0449 return ws;
0450 }
0451
0452
0453
0454
0455
0456
0457
0458
0459 int device_wakeup_disable(struct device *dev)
0460 {
0461 struct wakeup_source *ws;
0462
0463 if (!dev || !dev->power.can_wakeup)
0464 return -EINVAL;
0465
0466 ws = device_wakeup_detach(dev);
0467 wakeup_source_unregister(ws);
0468 return 0;
0469 }
0470 EXPORT_SYMBOL_GPL(device_wakeup_disable);
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484 void device_set_wakeup_capable(struct device *dev, bool capable)
0485 {
0486 if (!!dev->power.can_wakeup == !!capable)
0487 return;
0488
0489 dev->power.can_wakeup = capable;
0490 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
0491 if (capable) {
0492 int ret = wakeup_sysfs_add(dev);
0493
0494 if (ret)
0495 dev_info(dev, "Wakeup sysfs attributes not added\n");
0496 } else {
0497 wakeup_sysfs_remove(dev);
0498 }
0499 }
0500 }
0501 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
0502
0503
0504
0505
0506
0507
0508 int device_set_wakeup_enable(struct device *dev, bool enable)
0509 {
0510 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
0511 }
0512 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
0513
0514
0515
0516
0517
0518 static bool wakeup_source_not_registered(struct wakeup_source *ws)
0519 {
0520
0521
0522
0523
0524 return ws->timer.function != pm_wakeup_timer_fn;
0525 }
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563 static void wakeup_source_activate(struct wakeup_source *ws)
0564 {
0565 unsigned int cec;
0566
0567 if (WARN_ONCE(wakeup_source_not_registered(ws),
0568 "unregistered wakeup source\n"))
0569 return;
0570
0571 ws->active = true;
0572 ws->active_count++;
0573 ws->last_time = ktime_get();
0574 if (ws->autosleep_enabled)
0575 ws->start_prevent_time = ws->last_time;
0576
0577
0578 cec = atomic_inc_return(&combined_event_count);
0579
0580 trace_wakeup_source_activate(ws->name, cec);
0581 }
0582
0583
0584
0585
0586
0587
0588 static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
0589 {
0590 ws->event_count++;
0591
0592 if (events_check_enabled)
0593 ws->wakeup_count++;
0594
0595 if (!ws->active)
0596 wakeup_source_activate(ws);
0597
0598 if (hard)
0599 pm_system_wakeup();
0600 }
0601
0602
0603
0604
0605
0606
0607
0608 void __pm_stay_awake(struct wakeup_source *ws)
0609 {
0610 unsigned long flags;
0611
0612 if (!ws)
0613 return;
0614
0615 spin_lock_irqsave(&ws->lock, flags);
0616
0617 wakeup_source_report_event(ws, false);
0618 del_timer(&ws->timer);
0619 ws->timer_expires = 0;
0620
0621 spin_unlock_irqrestore(&ws->lock, flags);
0622 }
0623 EXPORT_SYMBOL_GPL(__pm_stay_awake);
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636 void pm_stay_awake(struct device *dev)
0637 {
0638 unsigned long flags;
0639
0640 if (!dev)
0641 return;
0642
0643 spin_lock_irqsave(&dev->power.lock, flags);
0644 __pm_stay_awake(dev->power.wakeup);
0645 spin_unlock_irqrestore(&dev->power.lock, flags);
0646 }
0647 EXPORT_SYMBOL_GPL(pm_stay_awake);
0648
0649 #ifdef CONFIG_PM_AUTOSLEEP
0650 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
0651 {
0652 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
0653 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
0654 }
0655 #else
0656 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
0657 ktime_t now) {}
0658 #endif
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668 static void wakeup_source_deactivate(struct wakeup_source *ws)
0669 {
0670 unsigned int cnt, inpr, cec;
0671 ktime_t duration;
0672 ktime_t now;
0673
0674 ws->relax_count++;
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684 if (ws->relax_count != ws->active_count) {
0685 ws->relax_count--;
0686 return;
0687 }
0688
0689 ws->active = false;
0690
0691 now = ktime_get();
0692 duration = ktime_sub(now, ws->last_time);
0693 ws->total_time = ktime_add(ws->total_time, duration);
0694 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
0695 ws->max_time = duration;
0696
0697 ws->last_time = now;
0698 del_timer(&ws->timer);
0699 ws->timer_expires = 0;
0700
0701 if (ws->autosleep_enabled)
0702 update_prevent_sleep_time(ws, now);
0703
0704
0705
0706
0707
0708 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
0709 trace_wakeup_source_deactivate(ws->name, cec);
0710
0711 split_counters(&cnt, &inpr);
0712 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
0713 wake_up(&wakeup_count_wait_queue);
0714 }
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725 void __pm_relax(struct wakeup_source *ws)
0726 {
0727 unsigned long flags;
0728
0729 if (!ws)
0730 return;
0731
0732 spin_lock_irqsave(&ws->lock, flags);
0733 if (ws->active)
0734 wakeup_source_deactivate(ws);
0735 spin_unlock_irqrestore(&ws->lock, flags);
0736 }
0737 EXPORT_SYMBOL_GPL(__pm_relax);
0738
0739
0740
0741
0742
0743
0744
0745 void pm_relax(struct device *dev)
0746 {
0747 unsigned long flags;
0748
0749 if (!dev)
0750 return;
0751
0752 spin_lock_irqsave(&dev->power.lock, flags);
0753 __pm_relax(dev->power.wakeup);
0754 spin_unlock_irqrestore(&dev->power.lock, flags);
0755 }
0756 EXPORT_SYMBOL_GPL(pm_relax);
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766 static void pm_wakeup_timer_fn(struct timer_list *t)
0767 {
0768 struct wakeup_source *ws = from_timer(ws, t, timer);
0769 unsigned long flags;
0770
0771 spin_lock_irqsave(&ws->lock, flags);
0772
0773 if (ws->active && ws->timer_expires
0774 && time_after_eq(jiffies, ws->timer_expires)) {
0775 wakeup_source_deactivate(ws);
0776 ws->expire_count++;
0777 }
0778
0779 spin_unlock_irqrestore(&ws->lock, flags);
0780 }
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795 void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
0796 {
0797 unsigned long flags;
0798 unsigned long expires;
0799
0800 if (!ws)
0801 return;
0802
0803 spin_lock_irqsave(&ws->lock, flags);
0804
0805 wakeup_source_report_event(ws, hard);
0806
0807 if (!msec) {
0808 wakeup_source_deactivate(ws);
0809 goto unlock;
0810 }
0811
0812 expires = jiffies + msecs_to_jiffies(msec);
0813 if (!expires)
0814 expires = 1;
0815
0816 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
0817 mod_timer(&ws->timer, expires);
0818 ws->timer_expires = expires;
0819 }
0820
0821 unlock:
0822 spin_unlock_irqrestore(&ws->lock, flags);
0823 }
0824 EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834 void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
0835 {
0836 unsigned long flags;
0837
0838 if (!dev)
0839 return;
0840
0841 spin_lock_irqsave(&dev->power.lock, flags);
0842 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
0843 spin_unlock_irqrestore(&dev->power.lock, flags);
0844 }
0845 EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
0846
0847 void pm_print_active_wakeup_sources(void)
0848 {
0849 struct wakeup_source *ws;
0850 int srcuidx, active = 0;
0851 struct wakeup_source *last_activity_ws = NULL;
0852
0853 srcuidx = srcu_read_lock(&wakeup_srcu);
0854 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
0855 if (ws->active) {
0856 pm_pr_dbg("active wakeup source: %s\n", ws->name);
0857 active = 1;
0858 } else if (!active &&
0859 (!last_activity_ws ||
0860 ktime_to_ns(ws->last_time) >
0861 ktime_to_ns(last_activity_ws->last_time))) {
0862 last_activity_ws = ws;
0863 }
0864 }
0865
0866 if (!active && last_activity_ws)
0867 pm_pr_dbg("last active wakeup source: %s\n",
0868 last_activity_ws->name);
0869 srcu_read_unlock(&wakeup_srcu, srcuidx);
0870 }
0871 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881 bool pm_wakeup_pending(void)
0882 {
0883 unsigned long flags;
0884 bool ret = false;
0885
0886 raw_spin_lock_irqsave(&events_lock, flags);
0887 if (events_check_enabled) {
0888 unsigned int cnt, inpr;
0889
0890 split_counters(&cnt, &inpr);
0891 ret = (cnt != saved_count || inpr > 0);
0892 events_check_enabled = !ret;
0893 }
0894 raw_spin_unlock_irqrestore(&events_lock, flags);
0895
0896 if (ret) {
0897 pm_pr_dbg("Wakeup pending, aborting suspend\n");
0898 pm_print_active_wakeup_sources();
0899 }
0900
0901 return ret || atomic_read(&pm_abort_suspend) > 0;
0902 }
0903 EXPORT_SYMBOL_GPL(pm_wakeup_pending);
0904
0905 void pm_system_wakeup(void)
0906 {
0907 atomic_inc(&pm_abort_suspend);
0908 s2idle_wake();
0909 }
0910 EXPORT_SYMBOL_GPL(pm_system_wakeup);
0911
0912 void pm_system_cancel_wakeup(void)
0913 {
0914 atomic_dec_if_positive(&pm_abort_suspend);
0915 }
0916
0917 void pm_wakeup_clear(unsigned int irq_number)
0918 {
0919 raw_spin_lock_irq(&wakeup_irq_lock);
0920
0921 if (irq_number && wakeup_irq[0] == irq_number)
0922 wakeup_irq[0] = wakeup_irq[1];
0923 else
0924 wakeup_irq[0] = 0;
0925
0926 wakeup_irq[1] = 0;
0927
0928 raw_spin_unlock_irq(&wakeup_irq_lock);
0929
0930 if (!irq_number)
0931 atomic_set(&pm_abort_suspend, 0);
0932 }
0933
0934 void pm_system_irq_wakeup(unsigned int irq_number)
0935 {
0936 unsigned long flags;
0937
0938 raw_spin_lock_irqsave(&wakeup_irq_lock, flags);
0939
0940 if (wakeup_irq[0] == 0)
0941 wakeup_irq[0] = irq_number;
0942 else if (wakeup_irq[1] == 0)
0943 wakeup_irq[1] = irq_number;
0944 else
0945 irq_number = 0;
0946
0947 raw_spin_unlock_irqrestore(&wakeup_irq_lock, flags);
0948
0949 if (irq_number)
0950 pm_system_wakeup();
0951 }
0952
0953 unsigned int pm_wakeup_irq(void)
0954 {
0955 return wakeup_irq[0];
0956 }
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970 bool pm_get_wakeup_count(unsigned int *count, bool block)
0971 {
0972 unsigned int cnt, inpr;
0973
0974 if (block) {
0975 DEFINE_WAIT(wait);
0976
0977 for (;;) {
0978 prepare_to_wait(&wakeup_count_wait_queue, &wait,
0979 TASK_INTERRUPTIBLE);
0980 split_counters(&cnt, &inpr);
0981 if (inpr == 0 || signal_pending(current))
0982 break;
0983 pm_print_active_wakeup_sources();
0984 schedule();
0985 }
0986 finish_wait(&wakeup_count_wait_queue, &wait);
0987 }
0988
0989 split_counters(&cnt, &inpr);
0990 *count = cnt;
0991 return !inpr;
0992 }
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004 bool pm_save_wakeup_count(unsigned int count)
1005 {
1006 unsigned int cnt, inpr;
1007 unsigned long flags;
1008
1009 events_check_enabled = false;
1010 raw_spin_lock_irqsave(&events_lock, flags);
1011 split_counters(&cnt, &inpr);
1012 if (cnt == count && inpr == 0) {
1013 saved_count = count;
1014 events_check_enabled = true;
1015 }
1016 raw_spin_unlock_irqrestore(&events_lock, flags);
1017 return events_check_enabled;
1018 }
1019
1020 #ifdef CONFIG_PM_AUTOSLEEP
1021
1022
1023
1024
1025 void pm_wakep_autosleep_enabled(bool set)
1026 {
1027 struct wakeup_source *ws;
1028 ktime_t now = ktime_get();
1029 int srcuidx;
1030
1031 srcuidx = srcu_read_lock(&wakeup_srcu);
1032 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
1033 spin_lock_irq(&ws->lock);
1034 if (ws->autosleep_enabled != set) {
1035 ws->autosleep_enabled = set;
1036 if (ws->active) {
1037 if (set)
1038 ws->start_prevent_time = now;
1039 else
1040 update_prevent_sleep_time(ws, now);
1041 }
1042 }
1043 spin_unlock_irq(&ws->lock);
1044 }
1045 srcu_read_unlock(&wakeup_srcu, srcuidx);
1046 }
1047 #endif
1048
1049
1050
1051
1052
1053
1054 static int print_wakeup_source_stats(struct seq_file *m,
1055 struct wakeup_source *ws)
1056 {
1057 unsigned long flags;
1058 ktime_t total_time;
1059 ktime_t max_time;
1060 unsigned long active_count;
1061 ktime_t active_time;
1062 ktime_t prevent_sleep_time;
1063
1064 spin_lock_irqsave(&ws->lock, flags);
1065
1066 total_time = ws->total_time;
1067 max_time = ws->max_time;
1068 prevent_sleep_time = ws->prevent_sleep_time;
1069 active_count = ws->active_count;
1070 if (ws->active) {
1071 ktime_t now = ktime_get();
1072
1073 active_time = ktime_sub(now, ws->last_time);
1074 total_time = ktime_add(total_time, active_time);
1075 if (active_time > max_time)
1076 max_time = active_time;
1077
1078 if (ws->autosleep_enabled)
1079 prevent_sleep_time = ktime_add(prevent_sleep_time,
1080 ktime_sub(now, ws->start_prevent_time));
1081 } else {
1082 active_time = 0;
1083 }
1084
1085 seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
1086 ws->name, active_count, ws->event_count,
1087 ws->wakeup_count, ws->expire_count,
1088 ktime_to_ms(active_time), ktime_to_ms(total_time),
1089 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1090 ktime_to_ms(prevent_sleep_time));
1091
1092 spin_unlock_irqrestore(&ws->lock, flags);
1093
1094 return 0;
1095 }
1096
1097 static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1098 loff_t *pos)
1099 {
1100 struct wakeup_source *ws;
1101 loff_t n = *pos;
1102 int *srcuidx = m->private;
1103
1104 if (n == 0) {
1105 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1106 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1107 "last_change\tprevent_suspend_time\n");
1108 }
1109
1110 *srcuidx = srcu_read_lock(&wakeup_srcu);
1111 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
1112 if (n-- <= 0)
1113 return ws;
1114 }
1115
1116 return NULL;
1117 }
1118
1119 static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1120 void *v, loff_t *pos)
1121 {
1122 struct wakeup_source *ws = v;
1123 struct wakeup_source *next_ws = NULL;
1124
1125 ++(*pos);
1126
1127 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1128 next_ws = ws;
1129 break;
1130 }
1131
1132 if (!next_ws)
1133 print_wakeup_source_stats(m, &deleted_ws);
1134
1135 return next_ws;
1136 }
1137
1138 static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1139 {
1140 int *srcuidx = m->private;
1141
1142 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1143 }
1144
1145
1146
1147
1148
1149
1150 static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1151 {
1152 struct wakeup_source *ws = v;
1153
1154 print_wakeup_source_stats(m, ws);
1155
1156 return 0;
1157 }
1158
1159 static const struct seq_operations wakeup_sources_stats_seq_ops = {
1160 .start = wakeup_sources_stats_seq_start,
1161 .next = wakeup_sources_stats_seq_next,
1162 .stop = wakeup_sources_stats_seq_stop,
1163 .show = wakeup_sources_stats_seq_show,
1164 };
1165
1166 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1167 {
1168 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
1169 }
1170
1171 static const struct file_operations wakeup_sources_stats_fops = {
1172 .owner = THIS_MODULE,
1173 .open = wakeup_sources_stats_open,
1174 .read = seq_read,
1175 .llseek = seq_lseek,
1176 .release = seq_release_private,
1177 };
1178
1179 static int __init wakeup_sources_debugfs_init(void)
1180 {
1181 debugfs_create_file("wakeup_sources", 0444, NULL, NULL,
1182 &wakeup_sources_stats_fops);
1183 return 0;
1184 }
1185
1186 postcore_initcall(wakeup_sources_debugfs_init);