aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base')
-rw-r--r--drivers/base/power/main.c10
-rw-r--r--drivers/base/power/sysfs.c54
-rw-r--r--drivers/base/power/wakeup.c174
3 files changed, 179 insertions, 59 deletions
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index b462c0e341cb..e0fb5b0435a3 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -889,6 +889,11 @@ static int dpm_suspend_noirq(pm_message_t state)
889 if (!list_empty(&dev->power.entry)) 889 if (!list_empty(&dev->power.entry))
890 list_move(&dev->power.entry, &dpm_noirq_list); 890 list_move(&dev->power.entry, &dpm_noirq_list);
891 put_device(dev); 891 put_device(dev);
892
893 if (pm_wakeup_pending()) {
894 error = -EBUSY;
895 break;
896 }
892 } 897 }
893 mutex_unlock(&dpm_list_mtx); 898 mutex_unlock(&dpm_list_mtx);
894 if (error) 899 if (error)
@@ -962,6 +967,11 @@ static int dpm_suspend_late(pm_message_t state)
962 if (!list_empty(&dev->power.entry)) 967 if (!list_empty(&dev->power.entry))
963 list_move(&dev->power.entry, &dpm_late_early_list); 968 list_move(&dev->power.entry, &dpm_late_early_list);
964 put_device(dev); 969 put_device(dev);
970
971 if (pm_wakeup_pending()) {
972 error = -EBUSY;
973 break;
974 }
965 } 975 }
966 mutex_unlock(&dpm_list_mtx); 976 mutex_unlock(&dpm_list_mtx);
967 if (error) 977 if (error)
diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
index 95c12f6cb5b9..48be2ad4dd2c 100644
--- a/drivers/base/power/sysfs.c
+++ b/drivers/base/power/sysfs.c
@@ -314,22 +314,41 @@ static ssize_t wakeup_active_count_show(struct device *dev,
314 314
315static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL); 315static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
316 316
317static ssize_t wakeup_hit_count_show(struct device *dev, 317static ssize_t wakeup_abort_count_show(struct device *dev,
318 struct device_attribute *attr, char *buf) 318 struct device_attribute *attr,
319 char *buf)
320{
321 unsigned long count = 0;
322 bool enabled = false;
323
324 spin_lock_irq(&dev->power.lock);
325 if (dev->power.wakeup) {
326 count = dev->power.wakeup->wakeup_count;
327 enabled = true;
328 }
329 spin_unlock_irq(&dev->power.lock);
330 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
331}
332
333static DEVICE_ATTR(wakeup_abort_count, 0444, wakeup_abort_count_show, NULL);
334
335static ssize_t wakeup_expire_count_show(struct device *dev,
336 struct device_attribute *attr,
337 char *buf)
319{ 338{
320 unsigned long count = 0; 339 unsigned long count = 0;
321 bool enabled = false; 340 bool enabled = false;
322 341
323 spin_lock_irq(&dev->power.lock); 342 spin_lock_irq(&dev->power.lock);
324 if (dev->power.wakeup) { 343 if (dev->power.wakeup) {
325 count = dev->power.wakeup->hit_count; 344 count = dev->power.wakeup->expire_count;
326 enabled = true; 345 enabled = true;
327 } 346 }
328 spin_unlock_irq(&dev->power.lock); 347 spin_unlock_irq(&dev->power.lock);
329 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n"); 348 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
330} 349}
331 350
332static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL); 351static DEVICE_ATTR(wakeup_expire_count, 0444, wakeup_expire_count_show, NULL);
333 352
334static ssize_t wakeup_active_show(struct device *dev, 353static ssize_t wakeup_active_show(struct device *dev,
335 struct device_attribute *attr, char *buf) 354 struct device_attribute *attr, char *buf)
@@ -398,6 +417,27 @@ static ssize_t wakeup_last_time_show(struct device *dev,
398} 417}
399 418
400static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL); 419static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
420
421#ifdef CONFIG_PM_AUTOSLEEP
422static ssize_t wakeup_prevent_sleep_time_show(struct device *dev,
423 struct device_attribute *attr,
424 char *buf)
425{
426 s64 msec = 0;
427 bool enabled = false;
428
429 spin_lock_irq(&dev->power.lock);
430 if (dev->power.wakeup) {
431 msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
432 enabled = true;
433 }
434 spin_unlock_irq(&dev->power.lock);
435 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
436}
437
438static DEVICE_ATTR(wakeup_prevent_sleep_time_ms, 0444,
439 wakeup_prevent_sleep_time_show, NULL);
440#endif /* CONFIG_PM_AUTOSLEEP */
401#endif /* CONFIG_PM_SLEEP */ 441#endif /* CONFIG_PM_SLEEP */
402 442
403#ifdef CONFIG_PM_ADVANCED_DEBUG 443#ifdef CONFIG_PM_ADVANCED_DEBUG
@@ -486,11 +526,15 @@ static struct attribute *wakeup_attrs[] = {
486 &dev_attr_wakeup.attr, 526 &dev_attr_wakeup.attr,
487 &dev_attr_wakeup_count.attr, 527 &dev_attr_wakeup_count.attr,
488 &dev_attr_wakeup_active_count.attr, 528 &dev_attr_wakeup_active_count.attr,
489 &dev_attr_wakeup_hit_count.attr, 529 &dev_attr_wakeup_abort_count.attr,
530 &dev_attr_wakeup_expire_count.attr,
490 &dev_attr_wakeup_active.attr, 531 &dev_attr_wakeup_active.attr,
491 &dev_attr_wakeup_total_time_ms.attr, 532 &dev_attr_wakeup_total_time_ms.attr,
492 &dev_attr_wakeup_max_time_ms.attr, 533 &dev_attr_wakeup_max_time_ms.attr,
493 &dev_attr_wakeup_last_time_ms.attr, 534 &dev_attr_wakeup_last_time_ms.attr,
535#ifdef CONFIG_PM_AUTOSLEEP
536 &dev_attr_wakeup_prevent_sleep_time_ms.attr,
537#endif
494#endif 538#endif
495 NULL, 539 NULL,
496}; 540};
diff --git a/drivers/base/power/wakeup.c b/drivers/base/power/wakeup.c
index 2a3e581b8dcd..cbb463b3a750 100644
--- a/drivers/base/power/wakeup.c
+++ b/drivers/base/power/wakeup.c
@@ -14,16 +14,15 @@
14#include <linux/suspend.h> 14#include <linux/suspend.h>
15#include <linux/seq_file.h> 15#include <linux/seq_file.h>
16#include <linux/debugfs.h> 16#include <linux/debugfs.h>
17#include <trace/events/power.h>
17 18
18#include "power.h" 19#include "power.h"
19 20
20#define TIMEOUT 100
21
22/* 21/*
23 * If set, the suspend/hibernate code will abort transitions to a sleep state 22 * If set, the suspend/hibernate code will abort transitions to a sleep state
24 * if wakeup events are registered during or immediately before the transition. 23 * if wakeup events are registered during or immediately before the transition.
25 */ 24 */
26bool events_check_enabled; 25bool events_check_enabled __read_mostly;
27 26
28/* 27/*
29 * Combined counters of registered wakeup events and wakeup events in progress. 28 * Combined counters of registered wakeup events and wakeup events in progress.
@@ -52,6 +51,8 @@ static void pm_wakeup_timer_fn(unsigned long data);
52 51
53static LIST_HEAD(wakeup_sources); 52static LIST_HEAD(wakeup_sources);
54 53
54static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
55
55/** 56/**
56 * wakeup_source_prepare - Prepare a new wakeup source for initialization. 57 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
57 * @ws: Wakeup source to prepare. 58 * @ws: Wakeup source to prepare.
@@ -132,6 +133,7 @@ void wakeup_source_add(struct wakeup_source *ws)
132 spin_lock_init(&ws->lock); 133 spin_lock_init(&ws->lock);
133 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws); 134 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
134 ws->active = false; 135 ws->active = false;
136 ws->last_time = ktime_get();
135 137
136 spin_lock_irq(&events_lock); 138 spin_lock_irq(&events_lock);
137 list_add_rcu(&ws->entry, &wakeup_sources); 139 list_add_rcu(&ws->entry, &wakeup_sources);
@@ -374,12 +376,33 @@ EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
374 */ 376 */
375static void wakeup_source_activate(struct wakeup_source *ws) 377static void wakeup_source_activate(struct wakeup_source *ws)
376{ 378{
379 unsigned int cec;
380
377 ws->active = true; 381 ws->active = true;
378 ws->active_count++; 382 ws->active_count++;
379 ws->last_time = ktime_get(); 383 ws->last_time = ktime_get();
384 if (ws->autosleep_enabled)
385 ws->start_prevent_time = ws->last_time;
380 386
381 /* Increment the counter of events in progress. */ 387 /* Increment the counter of events in progress. */
382 atomic_inc(&combined_event_count); 388 cec = atomic_inc_return(&combined_event_count);
389
390 trace_wakeup_source_activate(ws->name, cec);
391}
392
393/**
394 * wakeup_source_report_event - Report wakeup event using the given source.
395 * @ws: Wakeup source to report the event for.
396 */
397static void wakeup_source_report_event(struct wakeup_source *ws)
398{
399 ws->event_count++;
400 /* This is racy, but the counter is approximate anyway. */
401 if (events_check_enabled)
402 ws->wakeup_count++;
403
404 if (!ws->active)
405 wakeup_source_activate(ws);
383} 406}
384 407
385/** 408/**
@@ -397,10 +420,7 @@ void __pm_stay_awake(struct wakeup_source *ws)
397 420
398 spin_lock_irqsave(&ws->lock, flags); 421 spin_lock_irqsave(&ws->lock, flags);
399 422
400 ws->event_count++; 423 wakeup_source_report_event(ws);
401 if (!ws->active)
402 wakeup_source_activate(ws);
403
404 del_timer(&ws->timer); 424 del_timer(&ws->timer);
405 ws->timer_expires = 0; 425 ws->timer_expires = 0;
406 426
@@ -432,6 +452,17 @@ void pm_stay_awake(struct device *dev)
432} 452}
433EXPORT_SYMBOL_GPL(pm_stay_awake); 453EXPORT_SYMBOL_GPL(pm_stay_awake);
434 454
455#ifdef CONFIG_PM_AUTOSLEEP
456static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
457{
458 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
459 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
460}
461#else
462static inline void update_prevent_sleep_time(struct wakeup_source *ws,
463 ktime_t now) {}
464#endif
465
435/** 466/**
436 * wakup_source_deactivate - Mark given wakeup source as inactive. 467 * wakup_source_deactivate - Mark given wakeup source as inactive.
437 * @ws: Wakeup source to handle. 468 * @ws: Wakeup source to handle.
@@ -442,6 +473,7 @@ EXPORT_SYMBOL_GPL(pm_stay_awake);
442 */ 473 */
443static void wakeup_source_deactivate(struct wakeup_source *ws) 474static void wakeup_source_deactivate(struct wakeup_source *ws)
444{ 475{
476 unsigned int cnt, inpr, cec;
445 ktime_t duration; 477 ktime_t duration;
446 ktime_t now; 478 ktime_t now;
447 479
@@ -468,14 +500,23 @@ static void wakeup_source_deactivate(struct wakeup_source *ws)
468 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) 500 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
469 ws->max_time = duration; 501 ws->max_time = duration;
470 502
503 ws->last_time = now;
471 del_timer(&ws->timer); 504 del_timer(&ws->timer);
472 ws->timer_expires = 0; 505 ws->timer_expires = 0;
473 506
507 if (ws->autosleep_enabled)
508 update_prevent_sleep_time(ws, now);
509
474 /* 510 /*
475 * Increment the counter of registered wakeup events and decrement the 511 * Increment the counter of registered wakeup events and decrement the
476 * couter of wakeup events in progress simultaneously. 512 * couter of wakeup events in progress simultaneously.
477 */ 513 */
478 atomic_add(MAX_IN_PROGRESS, &combined_event_count); 514 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
515 trace_wakeup_source_deactivate(ws->name, cec);
516
517 split_counters(&cnt, &inpr);
518 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
519 wake_up(&wakeup_count_wait_queue);
479} 520}
480 521
481/** 522/**
@@ -536,8 +577,10 @@ static void pm_wakeup_timer_fn(unsigned long data)
536 spin_lock_irqsave(&ws->lock, flags); 577 spin_lock_irqsave(&ws->lock, flags);
537 578
538 if (ws->active && ws->timer_expires 579 if (ws->active && ws->timer_expires
539 && time_after_eq(jiffies, ws->timer_expires)) 580 && time_after_eq(jiffies, ws->timer_expires)) {
540 wakeup_source_deactivate(ws); 581 wakeup_source_deactivate(ws);
582 ws->expire_count++;
583 }
541 584
542 spin_unlock_irqrestore(&ws->lock, flags); 585 spin_unlock_irqrestore(&ws->lock, flags);
543} 586}
@@ -564,9 +607,7 @@ void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
564 607
565 spin_lock_irqsave(&ws->lock, flags); 608 spin_lock_irqsave(&ws->lock, flags);
566 609
567 ws->event_count++; 610 wakeup_source_report_event(ws);
568 if (!ws->active)
569 wakeup_source_activate(ws);
570 611
571 if (!msec) { 612 if (!msec) {
572 wakeup_source_deactivate(ws); 613 wakeup_source_deactivate(ws);
@@ -609,24 +650,6 @@ void pm_wakeup_event(struct device *dev, unsigned int msec)
609EXPORT_SYMBOL_GPL(pm_wakeup_event); 650EXPORT_SYMBOL_GPL(pm_wakeup_event);
610 651
611/** 652/**
612 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
613 */
614static void pm_wakeup_update_hit_counts(void)
615{
616 unsigned long flags;
617 struct wakeup_source *ws;
618
619 rcu_read_lock();
620 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
621 spin_lock_irqsave(&ws->lock, flags);
622 if (ws->active)
623 ws->hit_count++;
624 spin_unlock_irqrestore(&ws->lock, flags);
625 }
626 rcu_read_unlock();
627}
628
629/**
630 * pm_wakeup_pending - Check if power transition in progress should be aborted. 653 * pm_wakeup_pending - Check if power transition in progress should be aborted.
631 * 654 *
632 * Compare the current number of registered wakeup events with its preserved 655 * Compare the current number of registered wakeup events with its preserved
@@ -648,32 +671,38 @@ bool pm_wakeup_pending(void)
648 events_check_enabled = !ret; 671 events_check_enabled = !ret;
649 } 672 }
650 spin_unlock_irqrestore(&events_lock, flags); 673 spin_unlock_irqrestore(&events_lock, flags);
651 if (ret)
652 pm_wakeup_update_hit_counts();
653 return ret; 674 return ret;
654} 675}
655 676
656/** 677/**
657 * pm_get_wakeup_count - Read the number of registered wakeup events. 678 * pm_get_wakeup_count - Read the number of registered wakeup events.
658 * @count: Address to store the value at. 679 * @count: Address to store the value at.
680 * @block: Whether or not to block.
659 * 681 *
660 * Store the number of registered wakeup events at the address in @count. Block 682 * Store the number of registered wakeup events at the address in @count. If
661 * if the current number of wakeup events being processed is nonzero. 683 * @block is set, block until the current number of wakeup events being
684 * processed is zero.
662 * 685 *
663 * Return 'false' if the wait for the number of wakeup events being processed to 686 * Return 'false' if the current number of wakeup events being processed is
664 * drop down to zero has been interrupted by a signal (and the current number 687 * nonzero. Otherwise return 'true'.
665 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
666 */ 688 */
667bool pm_get_wakeup_count(unsigned int *count) 689bool pm_get_wakeup_count(unsigned int *count, bool block)
668{ 690{
669 unsigned int cnt, inpr; 691 unsigned int cnt, inpr;
670 692
671 for (;;) { 693 if (block) {
672 split_counters(&cnt, &inpr); 694 DEFINE_WAIT(wait);
673 if (inpr == 0 || signal_pending(current)) 695
674 break; 696 for (;;) {
675 pm_wakeup_update_hit_counts(); 697 prepare_to_wait(&wakeup_count_wait_queue, &wait,
676 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT)); 698 TASK_INTERRUPTIBLE);
699 split_counters(&cnt, &inpr);
700 if (inpr == 0 || signal_pending(current))
701 break;
702
703 schedule();
704 }
705 finish_wait(&wakeup_count_wait_queue, &wait);
677 } 706 }
678 707
679 split_counters(&cnt, &inpr); 708 split_counters(&cnt, &inpr);
@@ -703,11 +732,37 @@ bool pm_save_wakeup_count(unsigned int count)
703 events_check_enabled = true; 732 events_check_enabled = true;
704 } 733 }
705 spin_unlock_irq(&events_lock); 734 spin_unlock_irq(&events_lock);
706 if (!events_check_enabled)
707 pm_wakeup_update_hit_counts();
708 return events_check_enabled; 735 return events_check_enabled;
709} 736}
710 737
738#ifdef CONFIG_PM_AUTOSLEEP
739/**
740 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
741 * @enabled: Whether to set or to clear the autosleep_enabled flags.
742 */
743void pm_wakep_autosleep_enabled(bool set)
744{
745 struct wakeup_source *ws;
746 ktime_t now = ktime_get();
747
748 rcu_read_lock();
749 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
750 spin_lock_irq(&ws->lock);
751 if (ws->autosleep_enabled != set) {
752 ws->autosleep_enabled = set;
753 if (ws->active) {
754 if (set)
755 ws->start_prevent_time = now;
756 else
757 update_prevent_sleep_time(ws, now);
758 }
759 }
760 spin_unlock_irq(&ws->lock);
761 }
762 rcu_read_unlock();
763}
764#endif /* CONFIG_PM_AUTOSLEEP */
765
711static struct dentry *wakeup_sources_stats_dentry; 766static struct dentry *wakeup_sources_stats_dentry;
712 767
713/** 768/**
@@ -723,27 +778,37 @@ static int print_wakeup_source_stats(struct seq_file *m,
723 ktime_t max_time; 778 ktime_t max_time;
724 unsigned long active_count; 779 unsigned long active_count;
725 ktime_t active_time; 780 ktime_t active_time;
781 ktime_t prevent_sleep_time;
726 int ret; 782 int ret;
727 783
728 spin_lock_irqsave(&ws->lock, flags); 784 spin_lock_irqsave(&ws->lock, flags);
729 785
730 total_time = ws->total_time; 786 total_time = ws->total_time;
731 max_time = ws->max_time; 787 max_time = ws->max_time;
788 prevent_sleep_time = ws->prevent_sleep_time;
732 active_count = ws->active_count; 789 active_count = ws->active_count;
733 if (ws->active) { 790 if (ws->active) {
734 active_time = ktime_sub(ktime_get(), ws->last_time); 791 ktime_t now = ktime_get();
792
793 active_time = ktime_sub(now, ws->last_time);
735 total_time = ktime_add(total_time, active_time); 794 total_time = ktime_add(total_time, active_time);
736 if (active_time.tv64 > max_time.tv64) 795 if (active_time.tv64 > max_time.tv64)
737 max_time = active_time; 796 max_time = active_time;
797
798 if (ws->autosleep_enabled)
799 prevent_sleep_time = ktime_add(prevent_sleep_time,
800 ktime_sub(now, ws->start_prevent_time));
738 } else { 801 } else {
739 active_time = ktime_set(0, 0); 802 active_time = ktime_set(0, 0);
740 } 803 }
741 804
742 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t" 805 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t"
743 "%lld\t\t%lld\t\t%lld\t\t%lld\n", 806 "%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
744 ws->name, active_count, ws->event_count, ws->hit_count, 807 ws->name, active_count, ws->event_count,
808 ws->wakeup_count, ws->expire_count,
745 ktime_to_ms(active_time), ktime_to_ms(total_time), 809 ktime_to_ms(active_time), ktime_to_ms(total_time),
746 ktime_to_ms(max_time), ktime_to_ms(ws->last_time)); 810 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
811 ktime_to_ms(prevent_sleep_time));
747 812
748 spin_unlock_irqrestore(&ws->lock, flags); 813 spin_unlock_irqrestore(&ws->lock, flags);
749 814
@@ -758,8 +823,9 @@ static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
758{ 823{
759 struct wakeup_source *ws; 824 struct wakeup_source *ws;
760 825
761 seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t" 826 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
762 "active_since\ttotal_time\tmax_time\tlast_change\n"); 827 "expire_count\tactive_since\ttotal_time\tmax_time\t"
828 "last_change\tprevent_suspend_time\n");
763 829
764 rcu_read_lock(); 830 rcu_read_lock();
765 list_for_each_entry_rcu(ws, &wakeup_sources, entry) 831 list_for_each_entry_rcu(ws, &wakeup_sources, entry)