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.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>
23 * 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.
26 bool events_check_enabled __read_mostly;
28 /* First wakeup IRQ seen by the kernel in the last cycle. */
29 unsigned int pm_wakeup_irq __read_mostly;
31 /* If set and the system is suspending, terminate the suspend. */
32 static bool pm_abort_suspend __read_mostly;
35 * Combined counters of registered wakeup events and wakeup events in progress.
36 * They need to be modified together atomically, so it's better to use one
37 * atomic variable to hold them both.
39 static atomic_t combined_event_count = ATOMIC_INIT(0);
41 #define IN_PROGRESS_BITS (sizeof(int) * 4)
42 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
44 static void split_counters(unsigned int *cnt, unsigned int *inpr)
46 unsigned int comb = atomic_read(&combined_event_count);
48 *cnt = (comb >> IN_PROGRESS_BITS);
49 *inpr = comb & MAX_IN_PROGRESS;
52 /* A preserved old value of the events counter. */
53 static unsigned int saved_count;
55 static DEFINE_SPINLOCK(events_lock);
57 static void pm_wakeup_timer_fn(unsigned long data);
59 static LIST_HEAD(wakeup_sources);
61 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
63 DEFINE_STATIC_SRCU(wakeup_srcu);
65 static struct wakeup_source deleted_ws = {
67 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
71 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
72 * @ws: Wakeup source to prepare.
73 * @name: Pointer to the name of the new wakeup source.
75 * Callers must ensure that the @name string won't be freed when @ws is still in
78 void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
81 memset(ws, 0, sizeof(*ws));
85 EXPORT_SYMBOL_GPL(wakeup_source_prepare);
88 * wakeup_source_create - Create a struct wakeup_source object.
89 * @name: Name of the new wakeup source.
91 struct wakeup_source *wakeup_source_create(const char *name)
93 struct wakeup_source *ws;
95 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
99 wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
102 EXPORT_SYMBOL_GPL(wakeup_source_create);
105 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
106 * @ws: Wakeup source to prepare for destruction.
108 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
109 * be run in parallel with this function for the same wakeup source object.
111 void wakeup_source_drop(struct wakeup_source *ws)
118 EXPORT_SYMBOL_GPL(wakeup_source_drop);
121 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
123 static void wakeup_source_record(struct wakeup_source *ws)
127 spin_lock_irqsave(&deleted_ws.lock, flags);
129 if (ws->event_count) {
130 deleted_ws.total_time =
131 ktime_add(deleted_ws.total_time, ws->total_time);
132 deleted_ws.prevent_sleep_time =
133 ktime_add(deleted_ws.prevent_sleep_time,
134 ws->prevent_sleep_time);
135 deleted_ws.max_time =
136 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
137 deleted_ws.max_time : ws->max_time;
138 deleted_ws.event_count += ws->event_count;
139 deleted_ws.active_count += ws->active_count;
140 deleted_ws.relax_count += ws->relax_count;
141 deleted_ws.expire_count += ws->expire_count;
142 deleted_ws.wakeup_count += ws->wakeup_count;
145 spin_unlock_irqrestore(&deleted_ws.lock, flags);
149 * wakeup_source_destroy - Destroy a struct wakeup_source object.
150 * @ws: Wakeup source to destroy.
152 * Use only for wakeup source objects created with wakeup_source_create().
154 void wakeup_source_destroy(struct wakeup_source *ws)
159 wakeup_source_drop(ws);
160 wakeup_source_record(ws);
161 kfree_const(ws->name);
164 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
167 * wakeup_source_add - Add given object to the list of wakeup sources.
168 * @ws: Wakeup source object to add to the list.
170 void wakeup_source_add(struct wakeup_source *ws)
177 spin_lock_init(&ws->lock);
178 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
180 ws->last_time = ktime_get();
182 spin_lock_irqsave(&events_lock, flags);
183 list_add_rcu(&ws->entry, &wakeup_sources);
184 spin_unlock_irqrestore(&events_lock, flags);
186 EXPORT_SYMBOL_GPL(wakeup_source_add);
189 * wakeup_source_remove - Remove given object from the wakeup sources list.
190 * @ws: Wakeup source object to remove from the list.
192 void wakeup_source_remove(struct wakeup_source *ws)
199 spin_lock_irqsave(&events_lock, flags);
200 list_del_rcu(&ws->entry);
201 spin_unlock_irqrestore(&events_lock, flags);
202 synchronize_srcu(&wakeup_srcu);
204 del_timer_sync(&ws->timer);
206 * Clear timer.function to make wakeup_source_not_registered() treat
207 * this wakeup source as not registered.
209 ws->timer.function = NULL;
211 EXPORT_SYMBOL_GPL(wakeup_source_remove);
214 * wakeup_source_register - Create wakeup source and add it to the list.
215 * @name: Name of the wakeup source to register.
217 struct wakeup_source *wakeup_source_register(const char *name)
219 struct wakeup_source *ws;
221 ws = wakeup_source_create(name);
223 wakeup_source_add(ws);
227 EXPORT_SYMBOL_GPL(wakeup_source_register);
230 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
231 * @ws: Wakeup source object to unregister.
233 void wakeup_source_unregister(struct wakeup_source *ws)
236 wakeup_source_remove(ws);
237 wakeup_source_destroy(ws);
240 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
243 * device_wakeup_attach - Attach a wakeup source object to a device object.
244 * @dev: Device to handle.
245 * @ws: Wakeup source object to attach to @dev.
247 * This causes @dev to be treated as a wakeup device.
249 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
251 spin_lock_irq(&dev->power.lock);
252 if (dev->power.wakeup) {
253 spin_unlock_irq(&dev->power.lock);
256 dev->power.wakeup = ws;
257 spin_unlock_irq(&dev->power.lock);
262 * device_wakeup_enable - Enable given device to be a wakeup source.
263 * @dev: Device to handle.
265 * Create a wakeup source object, register it and attach it to @dev.
267 int device_wakeup_enable(struct device *dev)
269 struct wakeup_source *ws;
272 if (!dev || !dev->power.can_wakeup)
275 ws = wakeup_source_register(dev_name(dev));
279 ret = device_wakeup_attach(dev, ws);
281 wakeup_source_unregister(ws);
285 EXPORT_SYMBOL_GPL(device_wakeup_enable);
288 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
289 * @dev: Device to handle
290 * @wakeirq: Device specific wakeirq entry
292 * Attach a device wakeirq to the wakeup source so the device
293 * wake IRQ can be configured automatically for suspend and
296 * Call under the device's power.lock lock.
298 int device_wakeup_attach_irq(struct device *dev,
299 struct wake_irq *wakeirq)
301 struct wakeup_source *ws;
303 ws = dev->power.wakeup;
305 dev_err(dev, "forgot to call call device_init_wakeup?\n");
312 ws->wakeirq = wakeirq;
317 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
318 * @dev: Device to handle
320 * Removes a device wakeirq from the wakeup source.
322 * Call under the device's power.lock lock.
324 void device_wakeup_detach_irq(struct device *dev)
326 struct wakeup_source *ws;
328 ws = dev->power.wakeup;
334 * device_wakeup_arm_wake_irqs(void)
336 * Itereates over the list of device wakeirqs to arm them.
338 void device_wakeup_arm_wake_irqs(void)
340 struct wakeup_source *ws;
343 srcuidx = srcu_read_lock(&wakeup_srcu);
344 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
346 dev_pm_arm_wake_irq(ws->wakeirq);
348 srcu_read_unlock(&wakeup_srcu, srcuidx);
352 * device_wakeup_disarm_wake_irqs(void)
354 * Itereates over the list of device wakeirqs to disarm them.
356 void device_wakeup_disarm_wake_irqs(void)
358 struct wakeup_source *ws;
361 srcuidx = srcu_read_lock(&wakeup_srcu);
362 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
364 dev_pm_disarm_wake_irq(ws->wakeirq);
366 srcu_read_unlock(&wakeup_srcu, srcuidx);
370 * device_wakeup_detach - Detach a device's wakeup source object from it.
371 * @dev: Device to detach the wakeup source object from.
373 * After it returns, @dev will not be treated as a wakeup device any more.
375 static struct wakeup_source *device_wakeup_detach(struct device *dev)
377 struct wakeup_source *ws;
379 spin_lock_irq(&dev->power.lock);
380 ws = dev->power.wakeup;
381 dev->power.wakeup = NULL;
382 spin_unlock_irq(&dev->power.lock);
387 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
388 * @dev: Device to handle.
390 * Detach the @dev's wakeup source object from it, unregister this wakeup source
391 * object and destroy it.
393 int device_wakeup_disable(struct device *dev)
395 struct wakeup_source *ws;
397 if (!dev || !dev->power.can_wakeup)
400 ws = device_wakeup_detach(dev);
402 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 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
427 if (wakeup_sysfs_add(dev))
430 wakeup_sysfs_remove(dev);
433 dev->power.can_wakeup = capable;
435 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
438 * device_init_wakeup - Device wakeup initialization.
439 * @dev: Device to handle.
440 * @enable: Whether or not to enable @dev as a wakeup device.
442 * By default, most devices should leave wakeup disabled. The exceptions are
443 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
444 * possibly network interfaces, etc. Also, devices that don't generate their
445 * own wakeup requests but merely forward requests from one bus to another
446 * (like PCI bridges) should have wakeup enabled by default.
448 int device_init_wakeup(struct device *dev, bool enable)
456 device_set_wakeup_capable(dev, true);
457 ret = device_wakeup_enable(dev);
459 if (dev->power.can_wakeup)
460 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 if (!dev || !dev->power.can_wakeup)
478 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
480 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
483 * wakeup_source_not_registered - validate the given wakeup source.
484 * @ws: Wakeup source to be validated.
486 static bool wakeup_source_not_registered(struct wakeup_source *ws)
489 * Use timer struct to check if the given source is initialized
490 * by wakeup_source_add.
492 return ws->timer.function != pm_wakeup_timer_fn ||
493 ws->timer.data != (unsigned long)ws;
497 * The functions below use the observation that each wakeup event starts a
498 * period in which the system should not be suspended. The moment this period
499 * will end depends on how the wakeup event is going to be processed after being
500 * detected and all of the possible cases can be divided into two distinct
503 * First, a wakeup event may be detected by the same functional unit that will
504 * carry out the entire processing of it and possibly will pass it to user space
505 * for further processing. In that case the functional unit that has detected
506 * the event may later "close" the "no suspend" period associated with it
507 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
508 * pm_relax(), balanced with each other, is supposed to be used in such
511 * Second, a wakeup event may be detected by one functional unit and processed
512 * by another one. In that case the unit that has detected it cannot really
513 * "close" the "no suspend" period associated with it, unless it knows in
514 * advance what's going to happen to the event during processing. This
515 * knowledge, however, may not be available to it, so it can simply specify time
516 * to wait before the system can be suspended and pass it as the second
517 * argument of pm_wakeup_event().
519 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
520 * "no suspend" period will be ended either by the pm_relax(), or by the timer
521 * function executed when the timer expires, whichever comes first.
525 * wakup_source_activate - Mark given wakeup source as active.
526 * @ws: Wakeup source to handle.
528 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
529 * core of the event by incrementing the counter of of wakeup events being
532 static void wakeup_source_activate(struct wakeup_source *ws)
536 if (WARN_ONCE(wakeup_source_not_registered(ws),
537 "unregistered wakeup source\n"))
541 * active wakeup source should bring the system
542 * out of PM_SUSPEND_FREEZE state
548 ws->last_time = ktime_get();
549 if (ws->autosleep_enabled)
550 ws->start_prevent_time = ws->last_time;
552 /* Increment the counter of events in progress. */
553 cec = atomic_inc_return(&combined_event_count);
555 trace_wakeup_source_activate(ws->name, cec);
559 * wakeup_source_report_event - Report wakeup event using the given source.
560 * @ws: Wakeup source to report the event for.
562 static void wakeup_source_report_event(struct wakeup_source *ws)
565 /* This is racy, but the counter is approximate anyway. */
566 if (events_check_enabled)
570 wakeup_source_activate(ws);
574 * __pm_stay_awake - Notify the PM core of a wakeup event.
575 * @ws: Wakeup source object associated with the source of the event.
577 * It is safe to call this function from interrupt context.
579 void __pm_stay_awake(struct wakeup_source *ws)
586 spin_lock_irqsave(&ws->lock, flags);
588 wakeup_source_report_event(ws);
589 del_timer(&ws->timer);
590 ws->timer_expires = 0;
592 spin_unlock_irqrestore(&ws->lock, flags);
594 EXPORT_SYMBOL_GPL(__pm_stay_awake);
597 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
598 * @dev: Device the wakeup event is related to.
600 * Notify the PM core of a wakeup event (signaled by @dev) by calling
601 * __pm_stay_awake for the @dev's wakeup source object.
603 * Call this function after detecting of a wakeup event if pm_relax() is going
604 * to be called directly after processing the event (and possibly passing it to
605 * user space for further processing).
607 void pm_stay_awake(struct device *dev)
614 spin_lock_irqsave(&dev->power.lock, flags);
615 __pm_stay_awake(dev->power.wakeup);
616 spin_unlock_irqrestore(&dev->power.lock, flags);
618 EXPORT_SYMBOL_GPL(pm_stay_awake);
620 #ifdef CONFIG_PM_AUTOSLEEP
621 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
623 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
624 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
627 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
632 * wakup_source_deactivate - Mark given wakeup source as inactive.
633 * @ws: Wakeup source to handle.
635 * Update the @ws' statistics and notify the PM core that the wakeup source has
636 * become inactive by decrementing the counter of wakeup events being processed
637 * and incrementing the counter of registered wakeup events.
639 static void wakeup_source_deactivate(struct wakeup_source *ws)
641 unsigned int cnt, inpr, cec;
647 * __pm_relax() may be called directly or from a timer function.
648 * If it is called directly right after the timer function has been
649 * started, but before the timer function calls __pm_relax(), it is
650 * possible that __pm_stay_awake() will be called in the meantime and
651 * will set ws->active. Then, ws->active may be cleared immediately
652 * by the __pm_relax() called from the timer function, but in such a
653 * case ws->relax_count will be different from ws->active_count.
655 if (ws->relax_count != ws->active_count) {
663 duration = ktime_sub(now, ws->last_time);
664 ws->total_time = ktime_add(ws->total_time, duration);
665 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
666 ws->max_time = duration;
669 del_timer(&ws->timer);
670 ws->timer_expires = 0;
672 if (ws->autosleep_enabled)
673 update_prevent_sleep_time(ws, now);
676 * Increment the counter of registered wakeup events and decrement the
677 * couter of wakeup events in progress simultaneously.
679 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
680 trace_wakeup_source_deactivate(ws->name, cec);
682 split_counters(&cnt, &inpr);
683 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
684 wake_up(&wakeup_count_wait_queue);
688 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
689 * @ws: Wakeup source object associated with the source of the event.
691 * Call this function for wakeup events whose processing started with calling
694 * It is safe to call it from interrupt context.
696 void __pm_relax(struct wakeup_source *ws)
703 spin_lock_irqsave(&ws->lock, flags);
705 wakeup_source_deactivate(ws);
706 spin_unlock_irqrestore(&ws->lock, flags);
708 EXPORT_SYMBOL_GPL(__pm_relax);
711 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
712 * @dev: Device that signaled the event.
714 * Execute __pm_relax() for the @dev's wakeup source object.
716 void pm_relax(struct device *dev)
723 spin_lock_irqsave(&dev->power.lock, flags);
724 __pm_relax(dev->power.wakeup);
725 spin_unlock_irqrestore(&dev->power.lock, flags);
727 EXPORT_SYMBOL_GPL(pm_relax);
730 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
731 * @data: Address of the wakeup source object associated with the event source.
733 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
734 * in @data if it is currently active and its timer has not been canceled and
735 * the expiration time of the timer is not in future.
737 static void pm_wakeup_timer_fn(unsigned long data)
739 struct wakeup_source *ws = (struct wakeup_source *)data;
742 spin_lock_irqsave(&ws->lock, flags);
744 if (ws->active && ws->timer_expires
745 && time_after_eq(jiffies, ws->timer_expires)) {
746 wakeup_source_deactivate(ws);
750 spin_unlock_irqrestore(&ws->lock, flags);
754 * __pm_wakeup_event - Notify the PM core of a wakeup event.
755 * @ws: Wakeup source object associated with the event source.
756 * @msec: Anticipated event processing time (in milliseconds).
758 * Notify the PM core of a wakeup event whose source is @ws that will take
759 * approximately @msec milliseconds to be processed by the kernel. If @ws is
760 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
761 * execute pm_wakeup_timer_fn() in future.
763 * It is safe to call this function from interrupt context.
765 void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
768 unsigned long expires;
773 spin_lock_irqsave(&ws->lock, flags);
775 wakeup_source_report_event(ws);
778 wakeup_source_deactivate(ws);
782 expires = jiffies + msecs_to_jiffies(msec);
786 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
787 mod_timer(&ws->timer, expires);
788 ws->timer_expires = expires;
792 spin_unlock_irqrestore(&ws->lock, flags);
794 EXPORT_SYMBOL_GPL(__pm_wakeup_event);
798 * pm_wakeup_event - Notify the PM core of a wakeup event.
799 * @dev: Device the wakeup event is related to.
800 * @msec: Anticipated event processing time (in milliseconds).
802 * Call __pm_wakeup_event() for the @dev's wakeup source object.
804 void pm_wakeup_event(struct device *dev, unsigned int msec)
811 spin_lock_irqsave(&dev->power.lock, flags);
812 __pm_wakeup_event(dev->power.wakeup, msec);
813 spin_unlock_irqrestore(&dev->power.lock, flags);
815 EXPORT_SYMBOL_GPL(pm_wakeup_event);
817 void pm_print_active_wakeup_sources(void)
819 struct wakeup_source *ws;
820 int srcuidx, active = 0;
821 struct wakeup_source *last_activity_ws = NULL;
823 srcuidx = srcu_read_lock(&wakeup_srcu);
824 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
826 pr_info("active wakeup source: %s\n", ws->name);
828 } else if (!active &&
829 (!last_activity_ws ||
830 ktime_to_ns(ws->last_time) >
831 ktime_to_ns(last_activity_ws->last_time))) {
832 last_activity_ws = ws;
836 if (!active && last_activity_ws)
837 pr_info("last active wakeup source: %s\n",
838 last_activity_ws->name);
839 srcu_read_unlock(&wakeup_srcu, srcuidx);
841 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
844 * pm_wakeup_pending - Check if power transition in progress should be aborted.
846 * Compare the current number of registered wakeup events with its preserved
847 * value from the past and return true if new wakeup events have been registered
848 * since the old value was stored. Also return true if the current number of
849 * wakeup events being processed is different from zero.
851 bool pm_wakeup_pending(void)
856 spin_lock_irqsave(&events_lock, flags);
857 if (events_check_enabled) {
858 unsigned int cnt, inpr;
860 split_counters(&cnt, &inpr);
861 ret = (cnt != saved_count || inpr > 0);
862 events_check_enabled = !ret;
864 spin_unlock_irqrestore(&events_lock, flags);
867 pr_info("PM: Wakeup pending, aborting suspend\n");
868 pm_print_active_wakeup_sources();
871 return ret || pm_abort_suspend;
874 void pm_system_wakeup(void)
876 pm_abort_suspend = true;
879 EXPORT_SYMBOL_GPL(pm_system_wakeup);
881 void pm_wakeup_clear(void)
883 pm_abort_suspend = false;
887 void pm_system_irq_wakeup(unsigned int irq_number)
889 if (pm_wakeup_irq == 0) {
890 pm_wakeup_irq = irq_number;
896 * pm_get_wakeup_count - Read the number of registered wakeup events.
897 * @count: Address to store the value at.
898 * @block: Whether or not to block.
900 * Store the number of registered wakeup events at the address in @count. If
901 * @block is set, block until the current number of wakeup events being
904 * Return 'false' if the current number of wakeup events being processed is
905 * nonzero. Otherwise return 'true'.
907 bool pm_get_wakeup_count(unsigned int *count, bool block)
909 unsigned int cnt, inpr;
915 prepare_to_wait(&wakeup_count_wait_queue, &wait,
917 split_counters(&cnt, &inpr);
918 if (inpr == 0 || signal_pending(current))
923 finish_wait(&wakeup_count_wait_queue, &wait);
926 split_counters(&cnt, &inpr);
932 * pm_save_wakeup_count - Save the current number of registered wakeup events.
933 * @count: Value to compare with the current number of registered wakeup events.
935 * If @count is equal to the current number of registered wakeup events and the
936 * current number of wakeup events being processed is zero, store @count as the
937 * old number of registered wakeup events for pm_check_wakeup_events(), enable
938 * wakeup events detection and return 'true'. Otherwise disable wakeup events
939 * detection and return 'false'.
941 bool pm_save_wakeup_count(unsigned int count)
943 unsigned int cnt, inpr;
946 events_check_enabled = false;
947 spin_lock_irqsave(&events_lock, flags);
948 split_counters(&cnt, &inpr);
949 if (cnt == count && inpr == 0) {
951 events_check_enabled = true;
953 spin_unlock_irqrestore(&events_lock, flags);
954 return events_check_enabled;
957 #ifdef CONFIG_PM_AUTOSLEEP
959 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
960 * @enabled: Whether to set or to clear the autosleep_enabled flags.
962 void pm_wakep_autosleep_enabled(bool set)
964 struct wakeup_source *ws;
965 ktime_t now = ktime_get();
968 srcuidx = srcu_read_lock(&wakeup_srcu);
969 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
970 spin_lock_irq(&ws->lock);
971 if (ws->autosleep_enabled != set) {
972 ws->autosleep_enabled = set;
975 ws->start_prevent_time = now;
977 update_prevent_sleep_time(ws, now);
980 spin_unlock_irq(&ws->lock);
982 srcu_read_unlock(&wakeup_srcu, srcuidx);
984 #endif /* CONFIG_PM_AUTOSLEEP */
986 static struct dentry *wakeup_sources_stats_dentry;
989 * print_wakeup_source_stats - Print wakeup source statistics information.
990 * @m: seq_file to print the statistics into.
991 * @ws: Wakeup source object to print the statistics for.
993 static int print_wakeup_source_stats(struct seq_file *m,
994 struct wakeup_source *ws)
999 unsigned long active_count;
1000 ktime_t active_time;
1001 ktime_t prevent_sleep_time;
1003 spin_lock_irqsave(&ws->lock, flags);
1005 total_time = ws->total_time;
1006 max_time = ws->max_time;
1007 prevent_sleep_time = ws->prevent_sleep_time;
1008 active_count = ws->active_count;
1010 ktime_t now = ktime_get();
1012 active_time = ktime_sub(now, ws->last_time);
1013 total_time = ktime_add(total_time, active_time);
1014 if (active_time.tv64 > max_time.tv64)
1015 max_time = active_time;
1017 if (ws->autosleep_enabled)
1018 prevent_sleep_time = ktime_add(prevent_sleep_time,
1019 ktime_sub(now, ws->start_prevent_time));
1021 active_time = ktime_set(0, 0);
1024 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",
1025 ws->name, active_count, ws->event_count,
1026 ws->wakeup_count, ws->expire_count,
1027 ktime_to_ms(active_time), ktime_to_ms(total_time),
1028 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1029 ktime_to_ms(prevent_sleep_time));
1031 spin_unlock_irqrestore(&ws->lock, flags);
1037 * wakeup_sources_stats_show - Print wakeup sources statistics information.
1038 * @m: seq_file to print the statistics into.
1040 static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
1042 struct wakeup_source *ws;
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");
1049 srcuidx = srcu_read_lock(&wakeup_srcu);
1050 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
1051 print_wakeup_source_stats(m, ws);
1052 srcu_read_unlock(&wakeup_srcu, srcuidx);
1054 print_wakeup_source_stats(m, &deleted_ws);
1059 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1061 return single_open(file, wakeup_sources_stats_show, NULL);
1064 static const struct file_operations wakeup_sources_stats_fops = {
1065 .owner = THIS_MODULE,
1066 .open = wakeup_sources_stats_open,
1068 .llseek = seq_lseek,
1069 .release = single_release,
1072 static int __init wakeup_sources_debugfs_init(void)
1074 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1075 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1079 postcore_initcall(wakeup_sources_debugfs_init);