1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/wakeup.c - System wakeup events framework
5 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
7 #define pr_fmt(fmt) "PM: " fmt
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 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
112 static void wakeup_source_record(struct wakeup_source
*ws
)
116 spin_lock_irqsave(&deleted_ws
.lock
, flags
);
118 if (ws
->event_count
) {
119 deleted_ws
.total_time
=
120 ktime_add(deleted_ws
.total_time
, ws
->total_time
);
121 deleted_ws
.prevent_sleep_time
=
122 ktime_add(deleted_ws
.prevent_sleep_time
,
123 ws
->prevent_sleep_time
);
124 deleted_ws
.max_time
=
125 ktime_compare(deleted_ws
.max_time
, ws
->max_time
) > 0 ?
126 deleted_ws
.max_time
: ws
->max_time
;
127 deleted_ws
.event_count
+= ws
->event_count
;
128 deleted_ws
.active_count
+= ws
->active_count
;
129 deleted_ws
.relax_count
+= ws
->relax_count
;
130 deleted_ws
.expire_count
+= ws
->expire_count
;
131 deleted_ws
.wakeup_count
+= ws
->wakeup_count
;
134 spin_unlock_irqrestore(&deleted_ws
.lock
, flags
);
138 * wakeup_source_destroy - Destroy a struct wakeup_source object.
139 * @ws: Wakeup source to destroy.
141 * Use only for wakeup source objects created with wakeup_source_create().
143 void wakeup_source_destroy(struct wakeup_source
*ws
)
149 wakeup_source_record(ws
);
150 kfree_const(ws
->name
);
153 EXPORT_SYMBOL_GPL(wakeup_source_destroy
);
156 * wakeup_source_add - Add given object to the list of wakeup sources.
157 * @ws: Wakeup source object to add to the list.
159 void wakeup_source_add(struct wakeup_source
*ws
)
166 spin_lock_init(&ws
->lock
);
167 timer_setup(&ws
->timer
, pm_wakeup_timer_fn
, 0);
170 raw_spin_lock_irqsave(&events_lock
, flags
);
171 list_add_rcu(&ws
->entry
, &wakeup_sources
);
172 raw_spin_unlock_irqrestore(&events_lock
, flags
);
174 EXPORT_SYMBOL_GPL(wakeup_source_add
);
177 * wakeup_source_remove - Remove given object from the wakeup sources list.
178 * @ws: Wakeup source object to remove from the list.
180 void wakeup_source_remove(struct wakeup_source
*ws
)
187 raw_spin_lock_irqsave(&events_lock
, flags
);
188 list_del_rcu(&ws
->entry
);
189 raw_spin_unlock_irqrestore(&events_lock
, flags
);
190 synchronize_srcu(&wakeup_srcu
);
192 del_timer_sync(&ws
->timer
);
194 * Clear timer.function to make wakeup_source_not_registered() treat
195 * this wakeup source as not registered.
197 ws
->timer
.function
= NULL
;
199 EXPORT_SYMBOL_GPL(wakeup_source_remove
);
202 * wakeup_source_register - Create wakeup source and add it to the list.
203 * @name: Name of the wakeup source to register.
205 struct wakeup_source
*wakeup_source_register(const char *name
)
207 struct wakeup_source
*ws
;
209 ws
= wakeup_source_create(name
);
211 wakeup_source_add(ws
);
215 EXPORT_SYMBOL_GPL(wakeup_source_register
);
218 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
219 * @ws: Wakeup source object to unregister.
221 void wakeup_source_unregister(struct wakeup_source
*ws
)
224 wakeup_source_remove(ws
);
225 wakeup_source_destroy(ws
);
228 EXPORT_SYMBOL_GPL(wakeup_source_unregister
);
231 * device_wakeup_attach - Attach a wakeup source object to a device object.
232 * @dev: Device to handle.
233 * @ws: Wakeup source object to attach to @dev.
235 * This causes @dev to be treated as a wakeup device.
237 static int device_wakeup_attach(struct device
*dev
, struct wakeup_source
*ws
)
239 spin_lock_irq(&dev
->power
.lock
);
240 if (dev
->power
.wakeup
) {
241 spin_unlock_irq(&dev
->power
.lock
);
244 dev
->power
.wakeup
= ws
;
245 if (dev
->power
.wakeirq
)
246 device_wakeup_attach_irq(dev
, dev
->power
.wakeirq
);
247 spin_unlock_irq(&dev
->power
.lock
);
252 * device_wakeup_enable - Enable given device to be a wakeup source.
253 * @dev: Device to handle.
255 * Create a wakeup source object, register it and attach it to @dev.
257 int device_wakeup_enable(struct device
*dev
)
259 struct wakeup_source
*ws
;
262 if (!dev
|| !dev
->power
.can_wakeup
)
265 if (pm_suspend_target_state
!= PM_SUSPEND_ON
)
266 dev_dbg(dev
, "Suspicious %s() during system transition!\n", __func__
);
268 ws
= wakeup_source_register(dev_name(dev
));
272 ret
= device_wakeup_attach(dev
, ws
);
274 wakeup_source_unregister(ws
);
278 EXPORT_SYMBOL_GPL(device_wakeup_enable
);
281 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
282 * @dev: Device to handle
283 * @wakeirq: Device specific wakeirq entry
285 * Attach a device wakeirq to the wakeup source so the device
286 * wake IRQ can be configured automatically for suspend and
289 * Call under the device's power.lock lock.
291 void device_wakeup_attach_irq(struct device
*dev
,
292 struct wake_irq
*wakeirq
)
294 struct wakeup_source
*ws
;
296 ws
= dev
->power
.wakeup
;
301 dev_err(dev
, "Leftover wakeup IRQ found, overriding\n");
303 ws
->wakeirq
= wakeirq
;
307 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
308 * @dev: Device to handle
310 * Removes a device wakeirq from the wakeup source.
312 * Call under the device's power.lock lock.
314 void device_wakeup_detach_irq(struct device
*dev
)
316 struct wakeup_source
*ws
;
318 ws
= dev
->power
.wakeup
;
324 * device_wakeup_arm_wake_irqs(void)
326 * Itereates over the list of device wakeirqs to arm them.
328 void device_wakeup_arm_wake_irqs(void)
330 struct wakeup_source
*ws
;
333 srcuidx
= srcu_read_lock(&wakeup_srcu
);
334 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
)
335 dev_pm_arm_wake_irq(ws
->wakeirq
);
336 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
340 * device_wakeup_disarm_wake_irqs(void)
342 * Itereates over the list of device wakeirqs to disarm them.
344 void device_wakeup_disarm_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_disarm_wake_irq(ws
->wakeirq
);
352 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
356 * device_wakeup_detach - Detach a device's wakeup source object from it.
357 * @dev: Device to detach the wakeup source object from.
359 * After it returns, @dev will not be treated as a wakeup device any more.
361 static struct wakeup_source
*device_wakeup_detach(struct device
*dev
)
363 struct wakeup_source
*ws
;
365 spin_lock_irq(&dev
->power
.lock
);
366 ws
= dev
->power
.wakeup
;
367 dev
->power
.wakeup
= NULL
;
368 spin_unlock_irq(&dev
->power
.lock
);
373 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
374 * @dev: Device to handle.
376 * Detach the @dev's wakeup source object from it, unregister this wakeup source
377 * object and destroy it.
379 int device_wakeup_disable(struct device
*dev
)
381 struct wakeup_source
*ws
;
383 if (!dev
|| !dev
->power
.can_wakeup
)
386 ws
= device_wakeup_detach(dev
);
387 wakeup_source_unregister(ws
);
390 EXPORT_SYMBOL_GPL(device_wakeup_disable
);
393 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
394 * @dev: Device to handle.
395 * @capable: Whether or not @dev is capable of waking up the system from sleep.
397 * If @capable is set, set the @dev's power.can_wakeup flag and add its
398 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
399 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
401 * This function may sleep and it can't be called from any context where
402 * sleeping is not allowed.
404 void device_set_wakeup_capable(struct device
*dev
, bool capable
)
406 if (!!dev
->power
.can_wakeup
== !!capable
)
409 dev
->power
.can_wakeup
= capable
;
410 if (device_is_registered(dev
) && !list_empty(&dev
->power
.entry
)) {
412 int ret
= wakeup_sysfs_add(dev
);
415 dev_info(dev
, "Wakeup sysfs attributes not added\n");
417 wakeup_sysfs_remove(dev
);
421 EXPORT_SYMBOL_GPL(device_set_wakeup_capable
);
424 * device_init_wakeup - Device wakeup initialization.
425 * @dev: Device to handle.
426 * @enable: Whether or not to enable @dev as a wakeup device.
428 * By default, most devices should leave wakeup disabled. The exceptions are
429 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
430 * possibly network interfaces, etc. Also, devices that don't generate their
431 * own wakeup requests but merely forward requests from one bus to another
432 * (like PCI bridges) should have wakeup enabled by default.
434 int device_init_wakeup(struct device
*dev
, bool enable
)
442 device_set_wakeup_capable(dev
, true);
443 ret
= device_wakeup_enable(dev
);
445 device_wakeup_disable(dev
);
446 device_set_wakeup_capable(dev
, false);
451 EXPORT_SYMBOL_GPL(device_init_wakeup
);
454 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
455 * @dev: Device to handle.
457 int device_set_wakeup_enable(struct device
*dev
, bool enable
)
459 return enable
? device_wakeup_enable(dev
) : device_wakeup_disable(dev
);
461 EXPORT_SYMBOL_GPL(device_set_wakeup_enable
);
464 * wakeup_source_not_registered - validate the given wakeup source.
465 * @ws: Wakeup source to be validated.
467 static bool wakeup_source_not_registered(struct wakeup_source
*ws
)
470 * Use timer struct to check if the given source is initialized
471 * by wakeup_source_add.
473 return ws
->timer
.function
!= pm_wakeup_timer_fn
;
477 * The functions below use the observation that each wakeup event starts a
478 * period in which the system should not be suspended. The moment this period
479 * will end depends on how the wakeup event is going to be processed after being
480 * detected and all of the possible cases can be divided into two distinct
483 * First, a wakeup event may be detected by the same functional unit that will
484 * carry out the entire processing of it and possibly will pass it to user space
485 * for further processing. In that case the functional unit that has detected
486 * the event may later "close" the "no suspend" period associated with it
487 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
488 * pm_relax(), balanced with each other, is supposed to be used in such
491 * Second, a wakeup event may be detected by one functional unit and processed
492 * by another one. In that case the unit that has detected it cannot really
493 * "close" the "no suspend" period associated with it, unless it knows in
494 * advance what's going to happen to the event during processing. This
495 * knowledge, however, may not be available to it, so it can simply specify time
496 * to wait before the system can be suspended and pass it as the second
497 * argument of pm_wakeup_event().
499 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
500 * "no suspend" period will be ended either by the pm_relax(), or by the timer
501 * function executed when the timer expires, whichever comes first.
505 * wakup_source_activate - Mark given wakeup source as active.
506 * @ws: Wakeup source to handle.
508 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
509 * core of the event by incrementing the counter of of wakeup events being
512 static void wakeup_source_activate(struct wakeup_source
*ws
)
516 if (WARN_ONCE(wakeup_source_not_registered(ws
),
517 "unregistered wakeup source\n"))
522 ws
->last_time
= ktime_get();
523 if (ws
->autosleep_enabled
)
524 ws
->start_prevent_time
= ws
->last_time
;
526 /* Increment the counter of events in progress. */
527 cec
= atomic_inc_return(&combined_event_count
);
529 trace_wakeup_source_activate(ws
->name
, cec
);
533 * wakeup_source_report_event - Report wakeup event using the given source.
534 * @ws: Wakeup source to report the event for.
535 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
537 static void wakeup_source_report_event(struct wakeup_source
*ws
, bool hard
)
540 /* This is racy, but the counter is approximate anyway. */
541 if (events_check_enabled
)
545 wakeup_source_activate(ws
);
552 * __pm_stay_awake - Notify the PM core of a wakeup event.
553 * @ws: Wakeup source object associated with the source of the event.
555 * It is safe to call this function from interrupt context.
557 void __pm_stay_awake(struct wakeup_source
*ws
)
564 spin_lock_irqsave(&ws
->lock
, flags
);
566 wakeup_source_report_event(ws
, false);
567 del_timer(&ws
->timer
);
568 ws
->timer_expires
= 0;
570 spin_unlock_irqrestore(&ws
->lock
, flags
);
572 EXPORT_SYMBOL_GPL(__pm_stay_awake
);
575 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
576 * @dev: Device the wakeup event is related to.
578 * Notify the PM core of a wakeup event (signaled by @dev) by calling
579 * __pm_stay_awake for the @dev's wakeup source object.
581 * Call this function after detecting of a wakeup event if pm_relax() is going
582 * to be called directly after processing the event (and possibly passing it to
583 * user space for further processing).
585 void pm_stay_awake(struct device
*dev
)
592 spin_lock_irqsave(&dev
->power
.lock
, flags
);
593 __pm_stay_awake(dev
->power
.wakeup
);
594 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
596 EXPORT_SYMBOL_GPL(pm_stay_awake
);
598 #ifdef CONFIG_PM_AUTOSLEEP
599 static void update_prevent_sleep_time(struct wakeup_source
*ws
, ktime_t now
)
601 ktime_t delta
= ktime_sub(now
, ws
->start_prevent_time
);
602 ws
->prevent_sleep_time
= ktime_add(ws
->prevent_sleep_time
, delta
);
605 static inline void update_prevent_sleep_time(struct wakeup_source
*ws
,
610 * wakup_source_deactivate - Mark given wakeup source as inactive.
611 * @ws: Wakeup source to handle.
613 * Update the @ws' statistics and notify the PM core that the wakeup source has
614 * become inactive by decrementing the counter of wakeup events being processed
615 * and incrementing the counter of registered wakeup events.
617 static void wakeup_source_deactivate(struct wakeup_source
*ws
)
619 unsigned int cnt
, inpr
, cec
;
625 * __pm_relax() may be called directly or from a timer function.
626 * If it is called directly right after the timer function has been
627 * started, but before the timer function calls __pm_relax(), it is
628 * possible that __pm_stay_awake() will be called in the meantime and
629 * will set ws->active. Then, ws->active may be cleared immediately
630 * by the __pm_relax() called from the timer function, but in such a
631 * case ws->relax_count will be different from ws->active_count.
633 if (ws
->relax_count
!= ws
->active_count
) {
641 duration
= ktime_sub(now
, ws
->last_time
);
642 ws
->total_time
= ktime_add(ws
->total_time
, duration
);
643 if (ktime_to_ns(duration
) > ktime_to_ns(ws
->max_time
))
644 ws
->max_time
= duration
;
647 del_timer(&ws
->timer
);
648 ws
->timer_expires
= 0;
650 if (ws
->autosleep_enabled
)
651 update_prevent_sleep_time(ws
, now
);
654 * Increment the counter of registered wakeup events and decrement the
655 * couter of wakeup events in progress simultaneously.
657 cec
= atomic_add_return(MAX_IN_PROGRESS
, &combined_event_count
);
658 trace_wakeup_source_deactivate(ws
->name
, cec
);
660 split_counters(&cnt
, &inpr
);
661 if (!inpr
&& waitqueue_active(&wakeup_count_wait_queue
))
662 wake_up(&wakeup_count_wait_queue
);
666 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
667 * @ws: Wakeup source object associated with the source of the event.
669 * Call this function for wakeup events whose processing started with calling
672 * It is safe to call it from interrupt context.
674 void __pm_relax(struct wakeup_source
*ws
)
681 spin_lock_irqsave(&ws
->lock
, flags
);
683 wakeup_source_deactivate(ws
);
684 spin_unlock_irqrestore(&ws
->lock
, flags
);
686 EXPORT_SYMBOL_GPL(__pm_relax
);
689 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
690 * @dev: Device that signaled the event.
692 * Execute __pm_relax() for the @dev's wakeup source object.
694 void pm_relax(struct device
*dev
)
701 spin_lock_irqsave(&dev
->power
.lock
, flags
);
702 __pm_relax(dev
->power
.wakeup
);
703 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
705 EXPORT_SYMBOL_GPL(pm_relax
);
708 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
709 * @data: Address of the wakeup source object associated with the event source.
711 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
712 * in @data if it is currently active and its timer has not been canceled and
713 * the expiration time of the timer is not in future.
715 static void pm_wakeup_timer_fn(struct timer_list
*t
)
717 struct wakeup_source
*ws
= from_timer(ws
, t
, timer
);
720 spin_lock_irqsave(&ws
->lock
, flags
);
722 if (ws
->active
&& ws
->timer_expires
723 && time_after_eq(jiffies
, ws
->timer_expires
)) {
724 wakeup_source_deactivate(ws
);
728 spin_unlock_irqrestore(&ws
->lock
, flags
);
732 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
733 * @ws: Wakeup source object associated with the event source.
734 * @msec: Anticipated event processing time (in milliseconds).
735 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
737 * Notify the PM core of a wakeup event whose source is @ws that will take
738 * approximately @msec milliseconds to be processed by the kernel. If @ws is
739 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
740 * execute pm_wakeup_timer_fn() in future.
742 * It is safe to call this function from interrupt context.
744 void pm_wakeup_ws_event(struct wakeup_source
*ws
, unsigned int msec
, bool hard
)
747 unsigned long expires
;
752 spin_lock_irqsave(&ws
->lock
, flags
);
754 wakeup_source_report_event(ws
, hard
);
757 wakeup_source_deactivate(ws
);
761 expires
= jiffies
+ msecs_to_jiffies(msec
);
765 if (!ws
->timer_expires
|| time_after(expires
, ws
->timer_expires
)) {
766 mod_timer(&ws
->timer
, expires
);
767 ws
->timer_expires
= expires
;
771 spin_unlock_irqrestore(&ws
->lock
, flags
);
773 EXPORT_SYMBOL_GPL(pm_wakeup_ws_event
);
776 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
777 * @dev: Device the wakeup event is related to.
778 * @msec: Anticipated event processing time (in milliseconds).
779 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
781 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
783 void pm_wakeup_dev_event(struct device
*dev
, unsigned int msec
, bool hard
)
790 spin_lock_irqsave(&dev
->power
.lock
, flags
);
791 pm_wakeup_ws_event(dev
->power
.wakeup
, msec
, hard
);
792 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
794 EXPORT_SYMBOL_GPL(pm_wakeup_dev_event
);
796 void pm_print_active_wakeup_sources(void)
798 struct wakeup_source
*ws
;
799 int srcuidx
, active
= 0;
800 struct wakeup_source
*last_activity_ws
= NULL
;
802 srcuidx
= srcu_read_lock(&wakeup_srcu
);
803 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
805 pm_pr_dbg("active wakeup source: %s\n", ws
->name
);
807 } else if (!active
&&
808 (!last_activity_ws
||
809 ktime_to_ns(ws
->last_time
) >
810 ktime_to_ns(last_activity_ws
->last_time
))) {
811 last_activity_ws
= ws
;
815 if (!active
&& last_activity_ws
)
816 pm_pr_dbg("last active wakeup source: %s\n",
817 last_activity_ws
->name
);
818 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
820 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources
);
823 * pm_wakeup_pending - Check if power transition in progress should be aborted.
825 * Compare the current number of registered wakeup events with its preserved
826 * value from the past and return true if new wakeup events have been registered
827 * since the old value was stored. Also return true if the current number of
828 * wakeup events being processed is different from zero.
830 bool pm_wakeup_pending(void)
835 raw_spin_lock_irqsave(&events_lock
, flags
);
836 if (events_check_enabled
) {
837 unsigned int cnt
, inpr
;
839 split_counters(&cnt
, &inpr
);
840 ret
= (cnt
!= saved_count
|| inpr
> 0);
841 events_check_enabled
= !ret
;
843 raw_spin_unlock_irqrestore(&events_lock
, flags
);
846 pm_pr_dbg("Wakeup pending, aborting suspend\n");
847 pm_print_active_wakeup_sources();
850 return ret
|| atomic_read(&pm_abort_suspend
) > 0;
853 void pm_system_wakeup(void)
855 atomic_inc(&pm_abort_suspend
);
858 EXPORT_SYMBOL_GPL(pm_system_wakeup
);
860 void pm_system_cancel_wakeup(void)
862 atomic_dec(&pm_abort_suspend
);
865 void pm_wakeup_clear(bool reset
)
869 atomic_set(&pm_abort_suspend
, 0);
872 void pm_system_irq_wakeup(unsigned int irq_number
)
874 if (pm_wakeup_irq
== 0) {
875 pm_wakeup_irq
= irq_number
;
881 * pm_get_wakeup_count - Read the number of registered wakeup events.
882 * @count: Address to store the value at.
883 * @block: Whether or not to block.
885 * Store the number of registered wakeup events at the address in @count. If
886 * @block is set, block until the current number of wakeup events being
889 * Return 'false' if the current number of wakeup events being processed is
890 * nonzero. Otherwise return 'true'.
892 bool pm_get_wakeup_count(unsigned int *count
, bool block
)
894 unsigned int cnt
, inpr
;
900 prepare_to_wait(&wakeup_count_wait_queue
, &wait
,
902 split_counters(&cnt
, &inpr
);
903 if (inpr
== 0 || signal_pending(current
))
905 pm_print_active_wakeup_sources();
908 finish_wait(&wakeup_count_wait_queue
, &wait
);
911 split_counters(&cnt
, &inpr
);
917 * pm_save_wakeup_count - Save the current number of registered wakeup events.
918 * @count: Value to compare with the current number of registered wakeup events.
920 * If @count is equal to the current number of registered wakeup events and the
921 * current number of wakeup events being processed is zero, store @count as the
922 * old number of registered wakeup events for pm_check_wakeup_events(), enable
923 * wakeup events detection and return 'true'. Otherwise disable wakeup events
924 * detection and return 'false'.
926 bool pm_save_wakeup_count(unsigned int count
)
928 unsigned int cnt
, inpr
;
931 events_check_enabled
= false;
932 raw_spin_lock_irqsave(&events_lock
, flags
);
933 split_counters(&cnt
, &inpr
);
934 if (cnt
== count
&& inpr
== 0) {
936 events_check_enabled
= true;
938 raw_spin_unlock_irqrestore(&events_lock
, flags
);
939 return events_check_enabled
;
942 #ifdef CONFIG_PM_AUTOSLEEP
944 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
945 * @enabled: Whether to set or to clear the autosleep_enabled flags.
947 void pm_wakep_autosleep_enabled(bool set
)
949 struct wakeup_source
*ws
;
950 ktime_t now
= ktime_get();
953 srcuidx
= srcu_read_lock(&wakeup_srcu
);
954 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
955 spin_lock_irq(&ws
->lock
);
956 if (ws
->autosleep_enabled
!= set
) {
957 ws
->autosleep_enabled
= set
;
960 ws
->start_prevent_time
= now
;
962 update_prevent_sleep_time(ws
, now
);
965 spin_unlock_irq(&ws
->lock
);
967 srcu_read_unlock(&wakeup_srcu
, srcuidx
);
969 #endif /* CONFIG_PM_AUTOSLEEP */
971 static struct dentry
*wakeup_sources_stats_dentry
;
974 * print_wakeup_source_stats - Print wakeup source statistics information.
975 * @m: seq_file to print the statistics into.
976 * @ws: Wakeup source object to print the statistics for.
978 static int print_wakeup_source_stats(struct seq_file
*m
,
979 struct wakeup_source
*ws
)
984 unsigned long active_count
;
986 ktime_t prevent_sleep_time
;
988 spin_lock_irqsave(&ws
->lock
, flags
);
990 total_time
= ws
->total_time
;
991 max_time
= ws
->max_time
;
992 prevent_sleep_time
= ws
->prevent_sleep_time
;
993 active_count
= ws
->active_count
;
995 ktime_t now
= ktime_get();
997 active_time
= ktime_sub(now
, ws
->last_time
);
998 total_time
= ktime_add(total_time
, active_time
);
999 if (active_time
> max_time
)
1000 max_time
= active_time
;
1002 if (ws
->autosleep_enabled
)
1003 prevent_sleep_time
= ktime_add(prevent_sleep_time
,
1004 ktime_sub(now
, ws
->start_prevent_time
));
1009 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",
1010 ws
->name
, active_count
, ws
->event_count
,
1011 ws
->wakeup_count
, ws
->expire_count
,
1012 ktime_to_ms(active_time
), ktime_to_ms(total_time
),
1013 ktime_to_ms(max_time
), ktime_to_ms(ws
->last_time
),
1014 ktime_to_ms(prevent_sleep_time
));
1016 spin_unlock_irqrestore(&ws
->lock
, flags
);
1021 static void *wakeup_sources_stats_seq_start(struct seq_file
*m
,
1024 struct wakeup_source
*ws
;
1026 int *srcuidx
= m
->private;
1029 seq_puts(m
, "name\t\tactive_count\tevent_count\twakeup_count\t"
1030 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1031 "last_change\tprevent_suspend_time\n");
1034 *srcuidx
= srcu_read_lock(&wakeup_srcu
);
1035 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
1043 static void *wakeup_sources_stats_seq_next(struct seq_file
*m
,
1044 void *v
, loff_t
*pos
)
1046 struct wakeup_source
*ws
= v
;
1047 struct wakeup_source
*next_ws
= NULL
;
1051 list_for_each_entry_continue_rcu(ws
, &wakeup_sources
, entry
) {
1059 static void wakeup_sources_stats_seq_stop(struct seq_file
*m
, void *v
)
1061 int *srcuidx
= m
->private;
1063 srcu_read_unlock(&wakeup_srcu
, *srcuidx
);
1067 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1068 * @m: seq_file to print the statistics into.
1069 * @v: wakeup_source of each iteration
1071 static int wakeup_sources_stats_seq_show(struct seq_file
*m
, void *v
)
1073 struct wakeup_source
*ws
= v
;
1075 print_wakeup_source_stats(m
, ws
);
1080 static const struct seq_operations wakeup_sources_stats_seq_ops
= {
1081 .start
= wakeup_sources_stats_seq_start
,
1082 .next
= wakeup_sources_stats_seq_next
,
1083 .stop
= wakeup_sources_stats_seq_stop
,
1084 .show
= wakeup_sources_stats_seq_show
,
1087 static int wakeup_sources_stats_open(struct inode
*inode
, struct file
*file
)
1089 return seq_open_private(file
, &wakeup_sources_stats_seq_ops
, sizeof(int));
1092 static const struct file_operations wakeup_sources_stats_fops
= {
1093 .owner
= THIS_MODULE
,
1094 .open
= wakeup_sources_stats_open
,
1096 .llseek
= seq_lseek
,
1097 .release
= seq_release_private
,
1100 static int __init
wakeup_sources_debugfs_init(void)
1102 wakeup_sources_stats_dentry
= debugfs_create_file("wakeup_sources",
1103 S_IRUGO
, NULL
, NULL
, &wakeup_sources_stats_fops
);
1107 postcore_initcall(wakeup_sources_debugfs_init
);