2 * drivers/base/power/wakeup.c - System wakeup events framework
4 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6 * This file is released under the GPLv2.
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/capability.h>
13 #include <linux/export.h>
14 #include <linux/suspend.h>
15 #include <linux/seq_file.h>
16 #include <linux/debugfs.h>
17 #include <linux/pm_wakeirq.h>
18 #include <trace/events/power.h>
22 #ifndef CONFIG_SUSPEND
23 suspend_state_t pm_suspend_target_state
;
24 #define pm_suspend_target_state (PM_SUSPEND_ON)
28 * If set, the suspend/hibernate code will abort transitions to a sleep state
29 * if wakeup events are registered during or immediately before the transition.
31 bool events_check_enabled __read_mostly
;
33 /* First wakeup IRQ seen by the kernel in the last cycle. */
34 unsigned int pm_wakeup_irq __read_mostly
;
36 /* If greater than 0 and the system is suspending, terminate the suspend. */
37 static atomic_t pm_abort_suspend __read_mostly
;
40 * Combined counters of registered wakeup events and wakeup events in progress.
41 * They need to be modified together atomically, so it's better to use one
42 * atomic variable to hold them both.
44 static atomic_t combined_event_count
= ATOMIC_INIT(0);
46 #define IN_PROGRESS_BITS (sizeof(int) * 4)
47 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
49 static void split_counters(unsigned int *cnt
, unsigned int *inpr
)
51 unsigned int comb
= atomic_read(&combined_event_count
);
53 *cnt
= (comb
>> IN_PROGRESS_BITS
);
54 *inpr
= comb
& MAX_IN_PROGRESS
;
57 /* A preserved old value of the events counter. */
58 static unsigned int saved_count
;
60 static DEFINE_RAW_SPINLOCK(events_lock
);
62 static void pm_wakeup_timer_fn(struct timer_list
*t
);
64 static LIST_HEAD(wakeup_sources
);
66 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue
);
68 DEFINE_STATIC_SRCU(wakeup_srcu
);
70 static struct wakeup_source deleted_ws
= {
72 .lock
= __SPIN_LOCK_UNLOCKED(deleted_ws
.lock
),
76 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
77 * @ws: Wakeup source to prepare.
78 * @name: Pointer to the name of the new wakeup source.
80 * Callers must ensure that the @name string won't be freed when @ws is still in
83 void wakeup_source_prepare(struct wakeup_source
*ws
, const char *name
)
86 memset(ws
, 0, sizeof(*ws
));
90 EXPORT_SYMBOL_GPL(wakeup_source_prepare
);
93 * wakeup_source_create - Create a struct wakeup_source object.
94 * @name: Name of the new wakeup source.
96 struct wakeup_source
*wakeup_source_create(const char *name
)
98 struct wakeup_source
*ws
;
100 ws
= kmalloc(sizeof(*ws
), GFP_KERNEL
);
104 wakeup_source_prepare(ws
, name
? kstrdup_const(name
, GFP_KERNEL
) : NULL
);
107 EXPORT_SYMBOL_GPL(wakeup_source_create
);
110 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
111 * @ws: Wakeup source to prepare for destruction.
113 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
114 * be run in parallel with this function for the same wakeup source object.
116 void wakeup_source_drop(struct wakeup_source
*ws
)
123 EXPORT_SYMBOL_GPL(wakeup_source_drop
);
126 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
128 static void wakeup_source_record(struct wakeup_source
*ws
)
132 spin_lock_irqsave(&deleted_ws
.lock
, flags
);
134 if (ws
->event_count
) {
135 deleted_ws
.total_time
=
136 ktime_add(deleted_ws
.total_time
, ws
->total_time
);
137 deleted_ws
.prevent_sleep_time
=
138 ktime_add(deleted_ws
.prevent_sleep_time
,
139 ws
->prevent_sleep_time
);
140 deleted_ws
.max_time
=
141 ktime_compare(deleted_ws
.max_time
, ws
->max_time
) > 0 ?
142 deleted_ws
.max_time
: ws
->max_time
;
143 deleted_ws
.event_count
+= ws
->event_count
;
144 deleted_ws
.active_count
+= ws
->active_count
;
145 deleted_ws
.relax_count
+= ws
->relax_count
;
146 deleted_ws
.expire_count
+= ws
->expire_count
;
147 deleted_ws
.wakeup_count
+= ws
->wakeup_count
;
150 spin_unlock_irqrestore(&deleted_ws
.lock
, flags
);
154 * wakeup_source_destroy - Destroy a struct wakeup_source object.
155 * @ws: Wakeup source to destroy.
157 * Use only for wakeup source objects created with wakeup_source_create().
159 void wakeup_source_destroy(struct wakeup_source
*ws
)
164 wakeup_source_drop(ws
);
165 wakeup_source_record(ws
);
166 kfree_const(ws
->name
);
169 EXPORT_SYMBOL_GPL(wakeup_source_destroy
);
172 * wakeup_source_add - Add given object to the list of wakeup sources.
173 * @ws: Wakeup source object to add to the list.
175 void wakeup_source_add(struct wakeup_source
*ws
)
182 spin_lock_init(&ws
->lock
);
183 timer_setup(&ws
->timer
, pm_wakeup_timer_fn
, 0);
186 raw_spin_lock_irqsave(&events_lock
, flags
);
187 list_add_rcu(&ws
->entry
, &wakeup_sources
);
188 raw_spin_unlock_irqrestore(&events_lock
, flags
);
190 EXPORT_SYMBOL_GPL(wakeup_source_add
);
193 * wakeup_source_remove - Remove given object from the wakeup sources list.
194 * @ws: Wakeup source object to remove from the list.
196 void wakeup_source_remove(struct wakeup_source
*ws
)
203 raw_spin_lock_irqsave(&events_lock
, flags
);
204 list_del_rcu(&ws
->entry
);
205 raw_spin_unlock_irqrestore(&events_lock
, flags
);
206 synchronize_srcu(&wakeup_srcu
);
208 del_timer_sync(&ws
->timer
);
210 * Clear timer.function to make wakeup_source_not_registered() treat
211 * this wakeup source as not registered.
213 ws
->timer
.function
= NULL
;
215 EXPORT_SYMBOL_GPL(wakeup_source_remove
);
218 * wakeup_source_register - Create wakeup source and add it to the list.
219 * @name: Name of the wakeup source to register.
221 struct wakeup_source
*wakeup_source_register(const char *name
)
223 struct wakeup_source
*ws
;
225 ws
= wakeup_source_create(name
);
227 wakeup_source_add(ws
);
231 EXPORT_SYMBOL_GPL(wakeup_source_register
);
234 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
235 * @ws: Wakeup source object to unregister.
237 void wakeup_source_unregister(struct wakeup_source
*ws
)
240 wakeup_source_remove(ws
);
241 wakeup_source_destroy(ws
);
244 EXPORT_SYMBOL_GPL(wakeup_source_unregister
);
247 * device_wakeup_attach - Attach a wakeup source object to a device object.
248 * @dev: Device to handle.
249 * @ws: Wakeup source object to attach to @dev.
251 * This causes @dev to be treated as a wakeup device.
253 static int device_wakeup_attach(struct device
*dev
, struct wakeup_source
*ws
)
255 spin_lock_irq(&dev
->power
.lock
);
256 if (dev
->power
.wakeup
) {
257 spin_unlock_irq(&dev
->power
.lock
);
260 dev
->power
.wakeup
= ws
;
261 if (dev
->power
.wakeirq
)
262 device_wakeup_attach_irq(dev
, dev
->power
.wakeirq
);
263 spin_unlock_irq(&dev
->power
.lock
);
268 * device_wakeup_enable - Enable given device to be a wakeup source.
269 * @dev: Device to handle.
271 * Create a wakeup source object, register it and attach it to @dev.
273 int device_wakeup_enable(struct device
*dev
)
275 struct wakeup_source
*ws
;
278 if (!dev
|| !dev
->power
.can_wakeup
)
281 if (pm_suspend_target_state
!= PM_SUSPEND_ON
)
282 dev_dbg(dev
, "Suspicious %s() during system transition!\n", __func__
);
284 ws
= wakeup_source_register(dev_name(dev
));
288 ret
= device_wakeup_attach(dev
, ws
);
290 wakeup_source_unregister(ws
);
294 EXPORT_SYMBOL_GPL(device_wakeup_enable
);
297 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
298 * @dev: Device to handle
299 * @wakeirq: Device specific wakeirq entry
301 * Attach a device wakeirq to the wakeup source so the device
302 * wake IRQ can be configured automatically for suspend and
305 * Call under the device's power.lock lock.
307 void device_wakeup_attach_irq(struct device
*dev
,
308 struct wake_irq
*wakeirq
)
310 struct wakeup_source
*ws
;
312 ws
= dev
->power
.wakeup
;
317 dev_err(dev
, "Leftover wakeup IRQ found, overriding\n");
319 ws
->wakeirq
= wakeirq
;
323 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
324 * @dev: Device to handle
326 * Removes a device wakeirq from the wakeup source.
328 * Call under the device's power.lock lock.
330 void device_wakeup_detach_irq(struct device
*dev
)
332 struct wakeup_source
*ws
;
334 ws
= dev
->power
.wakeup
;
340 * device_wakeup_arm_wake_irqs(void)
342 * Itereates over the list of device wakeirqs to arm them.
344 void device_wakeup_arm_wake_irqs(void)
346 struct wakeup_source
*ws
;
349 srcuidx
= srcu_read_lock(&wakeup_srcu
);
350 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
)
351 dev_pm_arm_wake_irq(ws
->wakeirq
);
352 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
356 * device_wakeup_disarm_wake_irqs(void)
358 * Itereates over the list of device wakeirqs to disarm them.
360 void device_wakeup_disarm_wake_irqs(void)
362 struct wakeup_source
*ws
;
365 srcuidx
= srcu_read_lock(&wakeup_srcu
);
366 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
)
367 dev_pm_disarm_wake_irq(ws
->wakeirq
);
368 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
372 * device_wakeup_detach - Detach a device's wakeup source object from it.
373 * @dev: Device to detach the wakeup source object from.
375 * After it returns, @dev will not be treated as a wakeup device any more.
377 static struct wakeup_source
*device_wakeup_detach(struct device
*dev
)
379 struct wakeup_source
*ws
;
381 spin_lock_irq(&dev
->power
.lock
);
382 ws
= dev
->power
.wakeup
;
383 dev
->power
.wakeup
= NULL
;
384 spin_unlock_irq(&dev
->power
.lock
);
389 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
390 * @dev: Device to handle.
392 * Detach the @dev's wakeup source object from it, unregister this wakeup source
393 * object and destroy it.
395 int device_wakeup_disable(struct device
*dev
)
397 struct wakeup_source
*ws
;
399 if (!dev
|| !dev
->power
.can_wakeup
)
402 ws
= device_wakeup_detach(dev
);
403 wakeup_source_unregister(ws
);
406 EXPORT_SYMBOL_GPL(device_wakeup_disable
);
409 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
410 * @dev: Device to handle.
411 * @capable: Whether or not @dev is capable of waking up the system from sleep.
413 * If @capable is set, set the @dev's power.can_wakeup flag and add its
414 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
415 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
417 * This function may sleep and it can't be called from any context where
418 * sleeping is not allowed.
420 void device_set_wakeup_capable(struct device
*dev
, bool capable
)
422 if (!!dev
->power
.can_wakeup
== !!capable
)
425 dev
->power
.can_wakeup
= capable
;
426 if (device_is_registered(dev
) && !list_empty(&dev
->power
.entry
)) {
428 int ret
= wakeup_sysfs_add(dev
);
431 dev_info(dev
, "Wakeup sysfs attributes not added\n");
433 wakeup_sysfs_remove(dev
);
437 EXPORT_SYMBOL_GPL(device_set_wakeup_capable
);
440 * device_init_wakeup - Device wakeup initialization.
441 * @dev: Device to handle.
442 * @enable: Whether or not to enable @dev as a wakeup device.
444 * By default, most devices should leave wakeup disabled. The exceptions are
445 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
446 * possibly network interfaces, etc. Also, devices that don't generate their
447 * own wakeup requests but merely forward requests from one bus to another
448 * (like PCI bridges) should have wakeup enabled by default.
450 int device_init_wakeup(struct device
*dev
, bool enable
)
458 device_set_wakeup_capable(dev
, true);
459 ret
= device_wakeup_enable(dev
);
461 device_wakeup_disable(dev
);
462 device_set_wakeup_capable(dev
, false);
467 EXPORT_SYMBOL_GPL(device_init_wakeup
);
470 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
471 * @dev: Device to handle.
473 int device_set_wakeup_enable(struct device
*dev
, bool enable
)
475 return enable
? device_wakeup_enable(dev
) : device_wakeup_disable(dev
);
477 EXPORT_SYMBOL_GPL(device_set_wakeup_enable
);
480 * wakeup_source_not_registered - validate the given wakeup source.
481 * @ws: Wakeup source to be validated.
483 static bool wakeup_source_not_registered(struct wakeup_source
*ws
)
486 * Use timer struct to check if the given source is initialized
487 * by wakeup_source_add.
489 return ws
->timer
.function
!= pm_wakeup_timer_fn
;
493 * The functions below use the observation that each wakeup event starts a
494 * period in which the system should not be suspended. The moment this period
495 * will end depends on how the wakeup event is going to be processed after being
496 * detected and all of the possible cases can be divided into two distinct
499 * First, a wakeup event may be detected by the same functional unit that will
500 * carry out the entire processing of it and possibly will pass it to user space
501 * for further processing. In that case the functional unit that has detected
502 * the event may later "close" the "no suspend" period associated with it
503 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
504 * pm_relax(), balanced with each other, is supposed to be used in such
507 * Second, a wakeup event may be detected by one functional unit and processed
508 * by another one. In that case the unit that has detected it cannot really
509 * "close" the "no suspend" period associated with it, unless it knows in
510 * advance what's going to happen to the event during processing. This
511 * knowledge, however, may not be available to it, so it can simply specify time
512 * to wait before the system can be suspended and pass it as the second
513 * argument of pm_wakeup_event().
515 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
516 * "no suspend" period will be ended either by the pm_relax(), or by the timer
517 * function executed when the timer expires, whichever comes first.
521 * wakup_source_activate - Mark given wakeup source as active.
522 * @ws: Wakeup source to handle.
524 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
525 * core of the event by incrementing the counter of of wakeup events being
528 static void wakeup_source_activate(struct wakeup_source
*ws
)
532 if (WARN_ONCE(wakeup_source_not_registered(ws
),
533 "unregistered wakeup source\n"))
538 ws
->last_time
= ktime_get();
539 if (ws
->autosleep_enabled
)
540 ws
->start_prevent_time
= ws
->last_time
;
542 /* Increment the counter of events in progress. */
543 cec
= atomic_inc_return(&combined_event_count
);
545 trace_wakeup_source_activate(ws
->name
, cec
);
549 * wakeup_source_report_event - Report wakeup event using the given source.
550 * @ws: Wakeup source to report the event for.
551 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
553 static void wakeup_source_report_event(struct wakeup_source
*ws
, bool hard
)
556 /* This is racy, but the counter is approximate anyway. */
557 if (events_check_enabled
)
561 wakeup_source_activate(ws
);
568 * __pm_stay_awake - Notify the PM core of a wakeup event.
569 * @ws: Wakeup source object associated with the source of the event.
571 * It is safe to call this function from interrupt context.
573 void __pm_stay_awake(struct wakeup_source
*ws
)
580 spin_lock_irqsave(&ws
->lock
, flags
);
582 wakeup_source_report_event(ws
, false);
583 del_timer(&ws
->timer
);
584 ws
->timer_expires
= 0;
586 spin_unlock_irqrestore(&ws
->lock
, flags
);
588 EXPORT_SYMBOL_GPL(__pm_stay_awake
);
591 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
592 * @dev: Device the wakeup event is related to.
594 * Notify the PM core of a wakeup event (signaled by @dev) by calling
595 * __pm_stay_awake for the @dev's wakeup source object.
597 * Call this function after detecting of a wakeup event if pm_relax() is going
598 * to be called directly after processing the event (and possibly passing it to
599 * user space for further processing).
601 void pm_stay_awake(struct device
*dev
)
608 spin_lock_irqsave(&dev
->power
.lock
, flags
);
609 __pm_stay_awake(dev
->power
.wakeup
);
610 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
612 EXPORT_SYMBOL_GPL(pm_stay_awake
);
614 #ifdef CONFIG_PM_AUTOSLEEP
615 static void update_prevent_sleep_time(struct wakeup_source
*ws
, ktime_t now
)
617 ktime_t delta
= ktime_sub(now
, ws
->start_prevent_time
);
618 ws
->prevent_sleep_time
= ktime_add(ws
->prevent_sleep_time
, delta
);
621 static inline void update_prevent_sleep_time(struct wakeup_source
*ws
,
626 * wakup_source_deactivate - Mark given wakeup source as inactive.
627 * @ws: Wakeup source to handle.
629 * Update the @ws' statistics and notify the PM core that the wakeup source has
630 * become inactive by decrementing the counter of wakeup events being processed
631 * and incrementing the counter of registered wakeup events.
633 static void wakeup_source_deactivate(struct wakeup_source
*ws
)
635 unsigned int cnt
, inpr
, cec
;
641 * __pm_relax() may be called directly or from a timer function.
642 * If it is called directly right after the timer function has been
643 * started, but before the timer function calls __pm_relax(), it is
644 * possible that __pm_stay_awake() will be called in the meantime and
645 * will set ws->active. Then, ws->active may be cleared immediately
646 * by the __pm_relax() called from the timer function, but in such a
647 * case ws->relax_count will be different from ws->active_count.
649 if (ws
->relax_count
!= ws
->active_count
) {
657 duration
= ktime_sub(now
, ws
->last_time
);
658 ws
->total_time
= ktime_add(ws
->total_time
, duration
);
659 if (ktime_to_ns(duration
) > ktime_to_ns(ws
->max_time
))
660 ws
->max_time
= duration
;
663 del_timer(&ws
->timer
);
664 ws
->timer_expires
= 0;
666 if (ws
->autosleep_enabled
)
667 update_prevent_sleep_time(ws
, now
);
670 * Increment the counter of registered wakeup events and decrement the
671 * couter of wakeup events in progress simultaneously.
673 cec
= atomic_add_return(MAX_IN_PROGRESS
, &combined_event_count
);
674 trace_wakeup_source_deactivate(ws
->name
, cec
);
676 split_counters(&cnt
, &inpr
);
677 if (!inpr
&& waitqueue_active(&wakeup_count_wait_queue
))
678 wake_up(&wakeup_count_wait_queue
);
682 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
683 * @ws: Wakeup source object associated with the source of the event.
685 * Call this function for wakeup events whose processing started with calling
688 * It is safe to call it from interrupt context.
690 void __pm_relax(struct wakeup_source
*ws
)
697 spin_lock_irqsave(&ws
->lock
, flags
);
699 wakeup_source_deactivate(ws
);
700 spin_unlock_irqrestore(&ws
->lock
, flags
);
702 EXPORT_SYMBOL_GPL(__pm_relax
);
705 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
706 * @dev: Device that signaled the event.
708 * Execute __pm_relax() for the @dev's wakeup source object.
710 void pm_relax(struct device
*dev
)
717 spin_lock_irqsave(&dev
->power
.lock
, flags
);
718 __pm_relax(dev
->power
.wakeup
);
719 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
721 EXPORT_SYMBOL_GPL(pm_relax
);
724 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
725 * @data: Address of the wakeup source object associated with the event source.
727 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
728 * in @data if it is currently active and its timer has not been canceled and
729 * the expiration time of the timer is not in future.
731 static void pm_wakeup_timer_fn(struct timer_list
*t
)
733 struct wakeup_source
*ws
= from_timer(ws
, t
, timer
);
736 spin_lock_irqsave(&ws
->lock
, flags
);
738 if (ws
->active
&& ws
->timer_expires
739 && time_after_eq(jiffies
, ws
->timer_expires
)) {
740 wakeup_source_deactivate(ws
);
744 spin_unlock_irqrestore(&ws
->lock
, flags
);
748 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
749 * @ws: Wakeup source object associated with the event source.
750 * @msec: Anticipated event processing time (in milliseconds).
751 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
753 * Notify the PM core of a wakeup event whose source is @ws that will take
754 * approximately @msec milliseconds to be processed by the kernel. If @ws is
755 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
756 * execute pm_wakeup_timer_fn() in future.
758 * It is safe to call this function from interrupt context.
760 void pm_wakeup_ws_event(struct wakeup_source
*ws
, unsigned int msec
, bool hard
)
763 unsigned long expires
;
768 spin_lock_irqsave(&ws
->lock
, flags
);
770 wakeup_source_report_event(ws
, hard
);
773 wakeup_source_deactivate(ws
);
777 expires
= jiffies
+ msecs_to_jiffies(msec
);
781 if (!ws
->timer_expires
|| time_after(expires
, ws
->timer_expires
)) {
782 mod_timer(&ws
->timer
, expires
);
783 ws
->timer_expires
= expires
;
787 spin_unlock_irqrestore(&ws
->lock
, flags
);
789 EXPORT_SYMBOL_GPL(pm_wakeup_ws_event
);
792 * pm_wakeup_event - Notify the PM core of a wakeup event.
793 * @dev: Device the wakeup event is related to.
794 * @msec: Anticipated event processing time (in milliseconds).
795 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
797 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
799 void pm_wakeup_dev_event(struct device
*dev
, unsigned int msec
, bool hard
)
806 spin_lock_irqsave(&dev
->power
.lock
, flags
);
807 pm_wakeup_ws_event(dev
->power
.wakeup
, msec
, hard
);
808 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
810 EXPORT_SYMBOL_GPL(pm_wakeup_dev_event
);
812 void pm_print_active_wakeup_sources(void)
814 struct wakeup_source
*ws
;
815 int srcuidx
, active
= 0;
816 struct wakeup_source
*last_activity_ws
= NULL
;
818 srcuidx
= srcu_read_lock(&wakeup_srcu
);
819 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
821 pr_debug("active wakeup source: %s\n", ws
->name
);
823 } else if (!active
&&
824 (!last_activity_ws
||
825 ktime_to_ns(ws
->last_time
) >
826 ktime_to_ns(last_activity_ws
->last_time
))) {
827 last_activity_ws
= ws
;
831 if (!active
&& last_activity_ws
)
832 pr_debug("last active wakeup source: %s\n",
833 last_activity_ws
->name
);
834 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
836 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources
);
839 * pm_wakeup_pending - Check if power transition in progress should be aborted.
841 * Compare the current number of registered wakeup events with its preserved
842 * value from the past and return true if new wakeup events have been registered
843 * since the old value was stored. Also return true if the current number of
844 * wakeup events being processed is different from zero.
846 bool pm_wakeup_pending(void)
851 raw_spin_lock_irqsave(&events_lock
, flags
);
852 if (events_check_enabled
) {
853 unsigned int cnt
, inpr
;
855 split_counters(&cnt
, &inpr
);
856 ret
= (cnt
!= saved_count
|| inpr
> 0);
857 events_check_enabled
= !ret
;
859 raw_spin_unlock_irqrestore(&events_lock
, flags
);
862 pr_debug("PM: Wakeup pending, aborting suspend\n");
863 pm_print_active_wakeup_sources();
866 return ret
|| atomic_read(&pm_abort_suspend
) > 0;
869 void pm_system_wakeup(void)
871 atomic_inc(&pm_abort_suspend
);
874 EXPORT_SYMBOL_GPL(pm_system_wakeup
);
876 void pm_system_cancel_wakeup(void)
878 atomic_dec_if_positive(&pm_abort_suspend
);
881 void pm_wakeup_clear(bool reset
)
885 atomic_set(&pm_abort_suspend
, 0);
888 void pm_system_irq_wakeup(unsigned int irq_number
)
890 if (pm_wakeup_irq
== 0) {
891 pm_wakeup_irq
= irq_number
;
897 * pm_get_wakeup_count - Read the number of registered wakeup events.
898 * @count: Address to store the value at.
899 * @block: Whether or not to block.
901 * Store the number of registered wakeup events at the address in @count. If
902 * @block is set, block until the current number of wakeup events being
905 * Return 'false' if the current number of wakeup events being processed is
906 * nonzero. Otherwise return 'true'.
908 bool pm_get_wakeup_count(unsigned int *count
, bool block
)
910 unsigned int cnt
, inpr
;
916 prepare_to_wait(&wakeup_count_wait_queue
, &wait
,
918 split_counters(&cnt
, &inpr
);
919 if (inpr
== 0 || signal_pending(current
))
921 pm_print_active_wakeup_sources();
924 finish_wait(&wakeup_count_wait_queue
, &wait
);
927 split_counters(&cnt
, &inpr
);
933 * pm_save_wakeup_count - Save the current number of registered wakeup events.
934 * @count: Value to compare with the current number of registered wakeup events.
936 * If @count is equal to the current number of registered wakeup events and the
937 * current number of wakeup events being processed is zero, store @count as the
938 * old number of registered wakeup events for pm_check_wakeup_events(), enable
939 * wakeup events detection and return 'true'. Otherwise disable wakeup events
940 * detection and return 'false'.
942 bool pm_save_wakeup_count(unsigned int count
)
944 unsigned int cnt
, inpr
;
947 events_check_enabled
= false;
948 raw_spin_lock_irqsave(&events_lock
, flags
);
949 split_counters(&cnt
, &inpr
);
950 if (cnt
== count
&& inpr
== 0) {
952 events_check_enabled
= true;
954 raw_spin_unlock_irqrestore(&events_lock
, flags
);
955 return events_check_enabled
;
958 #ifdef CONFIG_PM_AUTOSLEEP
960 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
961 * @enabled: Whether to set or to clear the autosleep_enabled flags.
963 void pm_wakep_autosleep_enabled(bool set
)
965 struct wakeup_source
*ws
;
966 ktime_t now
= ktime_get();
969 srcuidx
= srcu_read_lock(&wakeup_srcu
);
970 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
971 spin_lock_irq(&ws
->lock
);
972 if (ws
->autosleep_enabled
!= set
) {
973 ws
->autosleep_enabled
= set
;
976 ws
->start_prevent_time
= now
;
978 update_prevent_sleep_time(ws
, now
);
981 spin_unlock_irq(&ws
->lock
);
983 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
985 #endif /* CONFIG_PM_AUTOSLEEP */
987 static struct dentry
*wakeup_sources_stats_dentry
;
990 * print_wakeup_source_stats - Print wakeup source statistics information.
991 * @m: seq_file to print the statistics into.
992 * @ws: Wakeup source object to print the statistics for.
994 static int print_wakeup_source_stats(struct seq_file
*m
,
995 struct wakeup_source
*ws
)
1000 unsigned long active_count
;
1001 ktime_t active_time
;
1002 ktime_t prevent_sleep_time
;
1004 spin_lock_irqsave(&ws
->lock
, flags
);
1006 total_time
= ws
->total_time
;
1007 max_time
= ws
->max_time
;
1008 prevent_sleep_time
= ws
->prevent_sleep_time
;
1009 active_count
= ws
->active_count
;
1011 ktime_t now
= ktime_get();
1013 active_time
= ktime_sub(now
, ws
->last_time
);
1014 total_time
= ktime_add(total_time
, active_time
);
1015 if (active_time
> max_time
)
1016 max_time
= active_time
;
1018 if (ws
->autosleep_enabled
)
1019 prevent_sleep_time
= ktime_add(prevent_sleep_time
,
1020 ktime_sub(now
, ws
->start_prevent_time
));
1025 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",
1026 ws
->name
, active_count
, ws
->event_count
,
1027 ws
->wakeup_count
, ws
->expire_count
,
1028 ktime_to_ms(active_time
), ktime_to_ms(total_time
),
1029 ktime_to_ms(max_time
), ktime_to_ms(ws
->last_time
),
1030 ktime_to_ms(prevent_sleep_time
));
1032 spin_unlock_irqrestore(&ws
->lock
, flags
);
1037 static void *wakeup_sources_stats_seq_start(struct seq_file
*m
,
1040 struct wakeup_source
*ws
;
1042 int *srcuidx
= m
->private;
1045 seq_puts(m
, "name\t\tactive_count\tevent_count\twakeup_count\t"
1046 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1047 "last_change\tprevent_suspend_time\n");
1050 *srcuidx
= srcu_read_lock(&wakeup_srcu
);
1051 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
1059 static void *wakeup_sources_stats_seq_next(struct seq_file
*m
,
1060 void *v
, loff_t
*pos
)
1062 struct wakeup_source
*ws
= v
;
1063 struct wakeup_source
*next_ws
= NULL
;
1067 list_for_each_entry_continue_rcu(ws
, &wakeup_sources
, entry
) {
1075 static void wakeup_sources_stats_seq_stop(struct seq_file
*m
, void *v
)
1077 int *srcuidx
= m
->private;
1079 srcu_read_unlock(&wakeup_srcu
, *srcuidx
);
1083 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1084 * @m: seq_file to print the statistics into.
1085 * @v: wakeup_source of each iteration
1087 static int wakeup_sources_stats_seq_show(struct seq_file
*m
, void *v
)
1089 struct wakeup_source
*ws
= v
;
1091 print_wakeup_source_stats(m
, ws
);
1096 static const struct seq_operations wakeup_sources_stats_seq_ops
= {
1097 .start
= wakeup_sources_stats_seq_start
,
1098 .next
= wakeup_sources_stats_seq_next
,
1099 .stop
= wakeup_sources_stats_seq_stop
,
1100 .show
= wakeup_sources_stats_seq_show
,
1103 static int wakeup_sources_stats_open(struct inode
*inode
, struct file
*file
)
1105 return seq_open_private(file
, &wakeup_sources_stats_seq_ops
, sizeof(int));
1108 static const struct file_operations wakeup_sources_stats_fops
= {
1109 .owner
= THIS_MODULE
,
1110 .open
= wakeup_sources_stats_open
,
1112 .llseek
= seq_lseek
,
1113 .release
= seq_release_private
,
1116 static int __init
wakeup_sources_debugfs_init(void)
1118 wakeup_sources_stats_dentry
= debugfs_create_file("wakeup_sources",
1119 S_IRUGO
, NULL
, NULL
, &wakeup_sources_stats_fops
);
1123 postcore_initcall(wakeup_sources_debugfs_init
);