Linux 5.1.15
[linux/fpc-iii.git] / drivers / base / power / wakeup.c
blobbb1ae175fae136a57cd85e7566af617706e29232
1 /*
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.
7 */
9 #define pr_fmt(fmt) "PM: " fmt
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/capability.h>
15 #include <linux/export.h>
16 #include <linux/suspend.h>
17 #include <linux/seq_file.h>
18 #include <linux/debugfs.h>
19 #include <linux/pm_wakeirq.h>
20 #include <trace/events/power.h>
22 #include "power.h"
24 #ifndef CONFIG_SUSPEND
25 suspend_state_t pm_suspend_target_state;
26 #define pm_suspend_target_state (PM_SUSPEND_ON)
27 #endif
30 * If set, the suspend/hibernate code will abort transitions to a sleep state
31 * if wakeup events are registered during or immediately before the transition.
33 bool events_check_enabled __read_mostly;
35 /* First wakeup IRQ seen by the kernel in the last cycle. */
36 unsigned int pm_wakeup_irq __read_mostly;
38 /* If greater than 0 and the system is suspending, terminate the suspend. */
39 static atomic_t pm_abort_suspend __read_mostly;
42 * Combined counters of registered wakeup events and wakeup events in progress.
43 * They need to be modified together atomically, so it's better to use one
44 * atomic variable to hold them both.
46 static atomic_t combined_event_count = ATOMIC_INIT(0);
48 #define IN_PROGRESS_BITS (sizeof(int) * 4)
49 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
51 static void split_counters(unsigned int *cnt, unsigned int *inpr)
53 unsigned int comb = atomic_read(&combined_event_count);
55 *cnt = (comb >> IN_PROGRESS_BITS);
56 *inpr = comb & MAX_IN_PROGRESS;
59 /* A preserved old value of the events counter. */
60 static unsigned int saved_count;
62 static DEFINE_RAW_SPINLOCK(events_lock);
64 static void pm_wakeup_timer_fn(struct timer_list *t);
66 static LIST_HEAD(wakeup_sources);
68 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
70 DEFINE_STATIC_SRCU(wakeup_srcu);
72 static struct wakeup_source deleted_ws = {
73 .name = "deleted",
74 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
77 /**
78 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
79 * @ws: Wakeup source to prepare.
80 * @name: Pointer to the name of the new wakeup source.
82 * Callers must ensure that the @name string won't be freed when @ws is still in
83 * use.
85 void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
87 if (ws) {
88 memset(ws, 0, sizeof(*ws));
89 ws->name = name;
92 EXPORT_SYMBOL_GPL(wakeup_source_prepare);
94 /**
95 * wakeup_source_create - Create a struct wakeup_source object.
96 * @name: Name of the new wakeup source.
98 struct wakeup_source *wakeup_source_create(const char *name)
100 struct wakeup_source *ws;
102 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
103 if (!ws)
104 return NULL;
106 wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
107 return ws;
109 EXPORT_SYMBOL_GPL(wakeup_source_create);
112 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
114 static void wakeup_source_record(struct wakeup_source *ws)
116 unsigned long flags;
118 spin_lock_irqsave(&deleted_ws.lock, flags);
120 if (ws->event_count) {
121 deleted_ws.total_time =
122 ktime_add(deleted_ws.total_time, ws->total_time);
123 deleted_ws.prevent_sleep_time =
124 ktime_add(deleted_ws.prevent_sleep_time,
125 ws->prevent_sleep_time);
126 deleted_ws.max_time =
127 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
128 deleted_ws.max_time : ws->max_time;
129 deleted_ws.event_count += ws->event_count;
130 deleted_ws.active_count += ws->active_count;
131 deleted_ws.relax_count += ws->relax_count;
132 deleted_ws.expire_count += ws->expire_count;
133 deleted_ws.wakeup_count += ws->wakeup_count;
136 spin_unlock_irqrestore(&deleted_ws.lock, flags);
140 * wakeup_source_destroy - Destroy a struct wakeup_source object.
141 * @ws: Wakeup source to destroy.
143 * Use only for wakeup source objects created with wakeup_source_create().
145 void wakeup_source_destroy(struct wakeup_source *ws)
147 if (!ws)
148 return;
150 __pm_relax(ws);
151 wakeup_source_record(ws);
152 kfree_const(ws->name);
153 kfree(ws);
155 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
158 * wakeup_source_add - Add given object to the list of wakeup sources.
159 * @ws: Wakeup source object to add to the list.
161 void wakeup_source_add(struct wakeup_source *ws)
163 unsigned long flags;
165 if (WARN_ON(!ws))
166 return;
168 spin_lock_init(&ws->lock);
169 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
170 ws->active = false;
172 raw_spin_lock_irqsave(&events_lock, flags);
173 list_add_rcu(&ws->entry, &wakeup_sources);
174 raw_spin_unlock_irqrestore(&events_lock, flags);
176 EXPORT_SYMBOL_GPL(wakeup_source_add);
179 * wakeup_source_remove - Remove given object from the wakeup sources list.
180 * @ws: Wakeup source object to remove from the list.
182 void wakeup_source_remove(struct wakeup_source *ws)
184 unsigned long flags;
186 if (WARN_ON(!ws))
187 return;
189 raw_spin_lock_irqsave(&events_lock, flags);
190 list_del_rcu(&ws->entry);
191 raw_spin_unlock_irqrestore(&events_lock, flags);
192 synchronize_srcu(&wakeup_srcu);
194 del_timer_sync(&ws->timer);
196 * Clear timer.function to make wakeup_source_not_registered() treat
197 * this wakeup source as not registered.
199 ws->timer.function = NULL;
201 EXPORT_SYMBOL_GPL(wakeup_source_remove);
204 * wakeup_source_register - Create wakeup source and add it to the list.
205 * @name: Name of the wakeup source to register.
207 struct wakeup_source *wakeup_source_register(const char *name)
209 struct wakeup_source *ws;
211 ws = wakeup_source_create(name);
212 if (ws)
213 wakeup_source_add(ws);
215 return ws;
217 EXPORT_SYMBOL_GPL(wakeup_source_register);
220 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
221 * @ws: Wakeup source object to unregister.
223 void wakeup_source_unregister(struct wakeup_source *ws)
225 if (ws) {
226 wakeup_source_remove(ws);
227 wakeup_source_destroy(ws);
230 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
233 * device_wakeup_attach - Attach a wakeup source object to a device object.
234 * @dev: Device to handle.
235 * @ws: Wakeup source object to attach to @dev.
237 * This causes @dev to be treated as a wakeup device.
239 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
241 spin_lock_irq(&dev->power.lock);
242 if (dev->power.wakeup) {
243 spin_unlock_irq(&dev->power.lock);
244 return -EEXIST;
246 dev->power.wakeup = ws;
247 if (dev->power.wakeirq)
248 device_wakeup_attach_irq(dev, dev->power.wakeirq);
249 spin_unlock_irq(&dev->power.lock);
250 return 0;
254 * device_wakeup_enable - Enable given device to be a wakeup source.
255 * @dev: Device to handle.
257 * Create a wakeup source object, register it and attach it to @dev.
259 int device_wakeup_enable(struct device *dev)
261 struct wakeup_source *ws;
262 int ret;
264 if (!dev || !dev->power.can_wakeup)
265 return -EINVAL;
267 if (pm_suspend_target_state != PM_SUSPEND_ON)
268 dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
270 ws = wakeup_source_register(dev_name(dev));
271 if (!ws)
272 return -ENOMEM;
274 ret = device_wakeup_attach(dev, ws);
275 if (ret)
276 wakeup_source_unregister(ws);
278 return ret;
280 EXPORT_SYMBOL_GPL(device_wakeup_enable);
283 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
284 * @dev: Device to handle
285 * @wakeirq: Device specific wakeirq entry
287 * Attach a device wakeirq to the wakeup source so the device
288 * wake IRQ can be configured automatically for suspend and
289 * resume.
291 * Call under the device's power.lock lock.
293 void device_wakeup_attach_irq(struct device *dev,
294 struct wake_irq *wakeirq)
296 struct wakeup_source *ws;
298 ws = dev->power.wakeup;
299 if (!ws)
300 return;
302 if (ws->wakeirq)
303 dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
305 ws->wakeirq = wakeirq;
309 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
310 * @dev: Device to handle
312 * Removes a device wakeirq from the wakeup source.
314 * Call under the device's power.lock lock.
316 void device_wakeup_detach_irq(struct device *dev)
318 struct wakeup_source *ws;
320 ws = dev->power.wakeup;
321 if (ws)
322 ws->wakeirq = NULL;
326 * device_wakeup_arm_wake_irqs(void)
328 * Itereates over the list of device wakeirqs to arm them.
330 void device_wakeup_arm_wake_irqs(void)
332 struct wakeup_source *ws;
333 int srcuidx;
335 srcuidx = srcu_read_lock(&wakeup_srcu);
336 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
337 dev_pm_arm_wake_irq(ws->wakeirq);
338 srcu_read_unlock(&wakeup_srcu, srcuidx);
342 * device_wakeup_disarm_wake_irqs(void)
344 * Itereates over the list of device wakeirqs to disarm them.
346 void device_wakeup_disarm_wake_irqs(void)
348 struct wakeup_source *ws;
349 int srcuidx;
351 srcuidx = srcu_read_lock(&wakeup_srcu);
352 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
353 dev_pm_disarm_wake_irq(ws->wakeirq);
354 srcu_read_unlock(&wakeup_srcu, srcuidx);
358 * device_wakeup_detach - Detach a device's wakeup source object from it.
359 * @dev: Device to detach the wakeup source object from.
361 * After it returns, @dev will not be treated as a wakeup device any more.
363 static struct wakeup_source *device_wakeup_detach(struct device *dev)
365 struct wakeup_source *ws;
367 spin_lock_irq(&dev->power.lock);
368 ws = dev->power.wakeup;
369 dev->power.wakeup = NULL;
370 spin_unlock_irq(&dev->power.lock);
371 return ws;
375 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
376 * @dev: Device to handle.
378 * Detach the @dev's wakeup source object from it, unregister this wakeup source
379 * object and destroy it.
381 int device_wakeup_disable(struct device *dev)
383 struct wakeup_source *ws;
385 if (!dev || !dev->power.can_wakeup)
386 return -EINVAL;
388 ws = device_wakeup_detach(dev);
389 wakeup_source_unregister(ws);
390 return 0;
392 EXPORT_SYMBOL_GPL(device_wakeup_disable);
395 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
396 * @dev: Device to handle.
397 * @capable: Whether or not @dev is capable of waking up the system from sleep.
399 * If @capable is set, set the @dev's power.can_wakeup flag and add its
400 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
401 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
403 * This function may sleep and it can't be called from any context where
404 * sleeping is not allowed.
406 void device_set_wakeup_capable(struct device *dev, bool capable)
408 if (!!dev->power.can_wakeup == !!capable)
409 return;
411 dev->power.can_wakeup = capable;
412 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
413 if (capable) {
414 int ret = wakeup_sysfs_add(dev);
416 if (ret)
417 dev_info(dev, "Wakeup sysfs attributes not added\n");
418 } else {
419 wakeup_sysfs_remove(dev);
423 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
426 * device_init_wakeup - Device wakeup initialization.
427 * @dev: Device to handle.
428 * @enable: Whether or not to enable @dev as a wakeup device.
430 * By default, most devices should leave wakeup disabled. The exceptions are
431 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
432 * possibly network interfaces, etc. Also, devices that don't generate their
433 * own wakeup requests but merely forward requests from one bus to another
434 * (like PCI bridges) should have wakeup enabled by default.
436 int device_init_wakeup(struct device *dev, bool enable)
438 int ret = 0;
440 if (!dev)
441 return -EINVAL;
443 if (enable) {
444 device_set_wakeup_capable(dev, true);
445 ret = device_wakeup_enable(dev);
446 } else {
447 device_wakeup_disable(dev);
448 device_set_wakeup_capable(dev, false);
451 return ret;
453 EXPORT_SYMBOL_GPL(device_init_wakeup);
456 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
457 * @dev: Device to handle.
459 int device_set_wakeup_enable(struct device *dev, bool enable)
461 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
463 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
466 * wakeup_source_not_registered - validate the given wakeup source.
467 * @ws: Wakeup source to be validated.
469 static bool wakeup_source_not_registered(struct wakeup_source *ws)
472 * Use timer struct to check if the given source is initialized
473 * by wakeup_source_add.
475 return ws->timer.function != pm_wakeup_timer_fn;
479 * The functions below use the observation that each wakeup event starts a
480 * period in which the system should not be suspended. The moment this period
481 * will end depends on how the wakeup event is going to be processed after being
482 * detected and all of the possible cases can be divided into two distinct
483 * groups.
485 * First, a wakeup event may be detected by the same functional unit that will
486 * carry out the entire processing of it and possibly will pass it to user space
487 * for further processing. In that case the functional unit that has detected
488 * the event may later "close" the "no suspend" period associated with it
489 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
490 * pm_relax(), balanced with each other, is supposed to be used in such
491 * situations.
493 * Second, a wakeup event may be detected by one functional unit and processed
494 * by another one. In that case the unit that has detected it cannot really
495 * "close" the "no suspend" period associated with it, unless it knows in
496 * advance what's going to happen to the event during processing. This
497 * knowledge, however, may not be available to it, so it can simply specify time
498 * to wait before the system can be suspended and pass it as the second
499 * argument of pm_wakeup_event().
501 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
502 * "no suspend" period will be ended either by the pm_relax(), or by the timer
503 * function executed when the timer expires, whichever comes first.
507 * wakup_source_activate - Mark given wakeup source as active.
508 * @ws: Wakeup source to handle.
510 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
511 * core of the event by incrementing the counter of of wakeup events being
512 * processed.
514 static void wakeup_source_activate(struct wakeup_source *ws)
516 unsigned int cec;
518 if (WARN_ONCE(wakeup_source_not_registered(ws),
519 "unregistered wakeup source\n"))
520 return;
522 ws->active = true;
523 ws->active_count++;
524 ws->last_time = ktime_get();
525 if (ws->autosleep_enabled)
526 ws->start_prevent_time = ws->last_time;
528 /* Increment the counter of events in progress. */
529 cec = atomic_inc_return(&combined_event_count);
531 trace_wakeup_source_activate(ws->name, cec);
535 * wakeup_source_report_event - Report wakeup event using the given source.
536 * @ws: Wakeup source to report the event for.
537 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
539 static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
541 ws->event_count++;
542 /* This is racy, but the counter is approximate anyway. */
543 if (events_check_enabled)
544 ws->wakeup_count++;
546 if (!ws->active)
547 wakeup_source_activate(ws);
549 if (hard)
550 pm_system_wakeup();
554 * __pm_stay_awake - Notify the PM core of a wakeup event.
555 * @ws: Wakeup source object associated with the source of the event.
557 * It is safe to call this function from interrupt context.
559 void __pm_stay_awake(struct wakeup_source *ws)
561 unsigned long flags;
563 if (!ws)
564 return;
566 spin_lock_irqsave(&ws->lock, flags);
568 wakeup_source_report_event(ws, false);
569 del_timer(&ws->timer);
570 ws->timer_expires = 0;
572 spin_unlock_irqrestore(&ws->lock, flags);
574 EXPORT_SYMBOL_GPL(__pm_stay_awake);
577 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
578 * @dev: Device the wakeup event is related to.
580 * Notify the PM core of a wakeup event (signaled by @dev) by calling
581 * __pm_stay_awake for the @dev's wakeup source object.
583 * Call this function after detecting of a wakeup event if pm_relax() is going
584 * to be called directly after processing the event (and possibly passing it to
585 * user space for further processing).
587 void pm_stay_awake(struct device *dev)
589 unsigned long flags;
591 if (!dev)
592 return;
594 spin_lock_irqsave(&dev->power.lock, flags);
595 __pm_stay_awake(dev->power.wakeup);
596 spin_unlock_irqrestore(&dev->power.lock, flags);
598 EXPORT_SYMBOL_GPL(pm_stay_awake);
600 #ifdef CONFIG_PM_AUTOSLEEP
601 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
603 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
604 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
606 #else
607 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
608 ktime_t now) {}
609 #endif
612 * wakup_source_deactivate - Mark given wakeup source as inactive.
613 * @ws: Wakeup source to handle.
615 * Update the @ws' statistics and notify the PM core that the wakeup source has
616 * become inactive by decrementing the counter of wakeup events being processed
617 * and incrementing the counter of registered wakeup events.
619 static void wakeup_source_deactivate(struct wakeup_source *ws)
621 unsigned int cnt, inpr, cec;
622 ktime_t duration;
623 ktime_t now;
625 ws->relax_count++;
627 * __pm_relax() may be called directly or from a timer function.
628 * If it is called directly right after the timer function has been
629 * started, but before the timer function calls __pm_relax(), it is
630 * possible that __pm_stay_awake() will be called in the meantime and
631 * will set ws->active. Then, ws->active may be cleared immediately
632 * by the __pm_relax() called from the timer function, but in such a
633 * case ws->relax_count will be different from ws->active_count.
635 if (ws->relax_count != ws->active_count) {
636 ws->relax_count--;
637 return;
640 ws->active = false;
642 now = ktime_get();
643 duration = ktime_sub(now, ws->last_time);
644 ws->total_time = ktime_add(ws->total_time, duration);
645 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
646 ws->max_time = duration;
648 ws->last_time = now;
649 del_timer(&ws->timer);
650 ws->timer_expires = 0;
652 if (ws->autosleep_enabled)
653 update_prevent_sleep_time(ws, now);
656 * Increment the counter of registered wakeup events and decrement the
657 * couter of wakeup events in progress simultaneously.
659 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
660 trace_wakeup_source_deactivate(ws->name, cec);
662 split_counters(&cnt, &inpr);
663 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
664 wake_up(&wakeup_count_wait_queue);
668 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
669 * @ws: Wakeup source object associated with the source of the event.
671 * Call this function for wakeup events whose processing started with calling
672 * __pm_stay_awake().
674 * It is safe to call it from interrupt context.
676 void __pm_relax(struct wakeup_source *ws)
678 unsigned long flags;
680 if (!ws)
681 return;
683 spin_lock_irqsave(&ws->lock, flags);
684 if (ws->active)
685 wakeup_source_deactivate(ws);
686 spin_unlock_irqrestore(&ws->lock, flags);
688 EXPORT_SYMBOL_GPL(__pm_relax);
691 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
692 * @dev: Device that signaled the event.
694 * Execute __pm_relax() for the @dev's wakeup source object.
696 void pm_relax(struct device *dev)
698 unsigned long flags;
700 if (!dev)
701 return;
703 spin_lock_irqsave(&dev->power.lock, flags);
704 __pm_relax(dev->power.wakeup);
705 spin_unlock_irqrestore(&dev->power.lock, flags);
707 EXPORT_SYMBOL_GPL(pm_relax);
710 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
711 * @data: Address of the wakeup source object associated with the event source.
713 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
714 * in @data if it is currently active and its timer has not been canceled and
715 * the expiration time of the timer is not in future.
717 static void pm_wakeup_timer_fn(struct timer_list *t)
719 struct wakeup_source *ws = from_timer(ws, t, timer);
720 unsigned long flags;
722 spin_lock_irqsave(&ws->lock, flags);
724 if (ws->active && ws->timer_expires
725 && time_after_eq(jiffies, ws->timer_expires)) {
726 wakeup_source_deactivate(ws);
727 ws->expire_count++;
730 spin_unlock_irqrestore(&ws->lock, flags);
734 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
735 * @ws: Wakeup source object associated with the event source.
736 * @msec: Anticipated event processing time (in milliseconds).
737 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
739 * Notify the PM core of a wakeup event whose source is @ws that will take
740 * approximately @msec milliseconds to be processed by the kernel. If @ws is
741 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
742 * execute pm_wakeup_timer_fn() in future.
744 * It is safe to call this function from interrupt context.
746 void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
748 unsigned long flags;
749 unsigned long expires;
751 if (!ws)
752 return;
754 spin_lock_irqsave(&ws->lock, flags);
756 wakeup_source_report_event(ws, hard);
758 if (!msec) {
759 wakeup_source_deactivate(ws);
760 goto unlock;
763 expires = jiffies + msecs_to_jiffies(msec);
764 if (!expires)
765 expires = 1;
767 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
768 mod_timer(&ws->timer, expires);
769 ws->timer_expires = expires;
772 unlock:
773 spin_unlock_irqrestore(&ws->lock, flags);
775 EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
778 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
779 * @dev: Device the wakeup event is related to.
780 * @msec: Anticipated event processing time (in milliseconds).
781 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
783 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
785 void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
787 unsigned long flags;
789 if (!dev)
790 return;
792 spin_lock_irqsave(&dev->power.lock, flags);
793 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
794 spin_unlock_irqrestore(&dev->power.lock, flags);
796 EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
798 void pm_print_active_wakeup_sources(void)
800 struct wakeup_source *ws;
801 int srcuidx, active = 0;
802 struct wakeup_source *last_activity_ws = NULL;
804 srcuidx = srcu_read_lock(&wakeup_srcu);
805 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
806 if (ws->active) {
807 pr_debug("active wakeup source: %s\n", ws->name);
808 active = 1;
809 } else if (!active &&
810 (!last_activity_ws ||
811 ktime_to_ns(ws->last_time) >
812 ktime_to_ns(last_activity_ws->last_time))) {
813 last_activity_ws = ws;
817 if (!active && last_activity_ws)
818 pr_debug("last active wakeup source: %s\n",
819 last_activity_ws->name);
820 srcu_read_unlock(&wakeup_srcu, srcuidx);
822 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
825 * pm_wakeup_pending - Check if power transition in progress should be aborted.
827 * Compare the current number of registered wakeup events with its preserved
828 * value from the past and return true if new wakeup events have been registered
829 * since the old value was stored. Also return true if the current number of
830 * wakeup events being processed is different from zero.
832 bool pm_wakeup_pending(void)
834 unsigned long flags;
835 bool ret = false;
837 raw_spin_lock_irqsave(&events_lock, flags);
838 if (events_check_enabled) {
839 unsigned int cnt, inpr;
841 split_counters(&cnt, &inpr);
842 ret = (cnt != saved_count || inpr > 0);
843 events_check_enabled = !ret;
845 raw_spin_unlock_irqrestore(&events_lock, flags);
847 if (ret) {
848 pr_debug("Wakeup pending, aborting suspend\n");
849 pm_print_active_wakeup_sources();
852 return ret || atomic_read(&pm_abort_suspend) > 0;
855 void pm_system_wakeup(void)
857 atomic_inc(&pm_abort_suspend);
858 s2idle_wake();
860 EXPORT_SYMBOL_GPL(pm_system_wakeup);
862 void pm_system_cancel_wakeup(void)
864 atomic_dec(&pm_abort_suspend);
867 void pm_wakeup_clear(bool reset)
869 pm_wakeup_irq = 0;
870 if (reset)
871 atomic_set(&pm_abort_suspend, 0);
874 void pm_system_irq_wakeup(unsigned int irq_number)
876 if (pm_wakeup_irq == 0) {
877 pm_wakeup_irq = irq_number;
878 pm_system_wakeup();
883 * pm_get_wakeup_count - Read the number of registered wakeup events.
884 * @count: Address to store the value at.
885 * @block: Whether or not to block.
887 * Store the number of registered wakeup events at the address in @count. If
888 * @block is set, block until the current number of wakeup events being
889 * processed is zero.
891 * Return 'false' if the current number of wakeup events being processed is
892 * nonzero. Otherwise return 'true'.
894 bool pm_get_wakeup_count(unsigned int *count, bool block)
896 unsigned int cnt, inpr;
898 if (block) {
899 DEFINE_WAIT(wait);
901 for (;;) {
902 prepare_to_wait(&wakeup_count_wait_queue, &wait,
903 TASK_INTERRUPTIBLE);
904 split_counters(&cnt, &inpr);
905 if (inpr == 0 || signal_pending(current))
906 break;
907 pm_print_active_wakeup_sources();
908 schedule();
910 finish_wait(&wakeup_count_wait_queue, &wait);
913 split_counters(&cnt, &inpr);
914 *count = cnt;
915 return !inpr;
919 * pm_save_wakeup_count - Save the current number of registered wakeup events.
920 * @count: Value to compare with the current number of registered wakeup events.
922 * If @count is equal to the current number of registered wakeup events and the
923 * current number of wakeup events being processed is zero, store @count as the
924 * old number of registered wakeup events for pm_check_wakeup_events(), enable
925 * wakeup events detection and return 'true'. Otherwise disable wakeup events
926 * detection and return 'false'.
928 bool pm_save_wakeup_count(unsigned int count)
930 unsigned int cnt, inpr;
931 unsigned long flags;
933 events_check_enabled = false;
934 raw_spin_lock_irqsave(&events_lock, flags);
935 split_counters(&cnt, &inpr);
936 if (cnt == count && inpr == 0) {
937 saved_count = count;
938 events_check_enabled = true;
940 raw_spin_unlock_irqrestore(&events_lock, flags);
941 return events_check_enabled;
944 #ifdef CONFIG_PM_AUTOSLEEP
946 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
947 * @enabled: Whether to set or to clear the autosleep_enabled flags.
949 void pm_wakep_autosleep_enabled(bool set)
951 struct wakeup_source *ws;
952 ktime_t now = ktime_get();
953 int srcuidx;
955 srcuidx = srcu_read_lock(&wakeup_srcu);
956 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
957 spin_lock_irq(&ws->lock);
958 if (ws->autosleep_enabled != set) {
959 ws->autosleep_enabled = set;
960 if (ws->active) {
961 if (set)
962 ws->start_prevent_time = now;
963 else
964 update_prevent_sleep_time(ws, now);
967 spin_unlock_irq(&ws->lock);
969 srcu_read_unlock(&wakeup_srcu, srcuidx);
971 #endif /* CONFIG_PM_AUTOSLEEP */
973 static struct dentry *wakeup_sources_stats_dentry;
976 * print_wakeup_source_stats - Print wakeup source statistics information.
977 * @m: seq_file to print the statistics into.
978 * @ws: Wakeup source object to print the statistics for.
980 static int print_wakeup_source_stats(struct seq_file *m,
981 struct wakeup_source *ws)
983 unsigned long flags;
984 ktime_t total_time;
985 ktime_t max_time;
986 unsigned long active_count;
987 ktime_t active_time;
988 ktime_t prevent_sleep_time;
990 spin_lock_irqsave(&ws->lock, flags);
992 total_time = ws->total_time;
993 max_time = ws->max_time;
994 prevent_sleep_time = ws->prevent_sleep_time;
995 active_count = ws->active_count;
996 if (ws->active) {
997 ktime_t now = ktime_get();
999 active_time = ktime_sub(now, ws->last_time);
1000 total_time = ktime_add(total_time, active_time);
1001 if (active_time > max_time)
1002 max_time = active_time;
1004 if (ws->autosleep_enabled)
1005 prevent_sleep_time = ktime_add(prevent_sleep_time,
1006 ktime_sub(now, ws->start_prevent_time));
1007 } else {
1008 active_time = 0;
1011 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",
1012 ws->name, active_count, ws->event_count,
1013 ws->wakeup_count, ws->expire_count,
1014 ktime_to_ms(active_time), ktime_to_ms(total_time),
1015 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1016 ktime_to_ms(prevent_sleep_time));
1018 spin_unlock_irqrestore(&ws->lock, flags);
1020 return 0;
1023 static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1024 loff_t *pos)
1026 struct wakeup_source *ws;
1027 loff_t n = *pos;
1028 int *srcuidx = m->private;
1030 if (n == 0) {
1031 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1032 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1033 "last_change\tprevent_suspend_time\n");
1036 *srcuidx = srcu_read_lock(&wakeup_srcu);
1037 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
1038 if (n-- <= 0)
1039 return ws;
1042 return NULL;
1045 static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1046 void *v, loff_t *pos)
1048 struct wakeup_source *ws = v;
1049 struct wakeup_source *next_ws = NULL;
1051 ++(*pos);
1053 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1054 next_ws = ws;
1055 break;
1058 return next_ws;
1061 static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1063 int *srcuidx = m->private;
1065 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1069 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1070 * @m: seq_file to print the statistics into.
1071 * @v: wakeup_source of each iteration
1073 static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1075 struct wakeup_source *ws = v;
1077 print_wakeup_source_stats(m, ws);
1079 return 0;
1082 static const struct seq_operations wakeup_sources_stats_seq_ops = {
1083 .start = wakeup_sources_stats_seq_start,
1084 .next = wakeup_sources_stats_seq_next,
1085 .stop = wakeup_sources_stats_seq_stop,
1086 .show = wakeup_sources_stats_seq_show,
1089 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1091 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
1094 static const struct file_operations wakeup_sources_stats_fops = {
1095 .owner = THIS_MODULE,
1096 .open = wakeup_sources_stats_open,
1097 .read = seq_read,
1098 .llseek = seq_lseek,
1099 .release = seq_release_private,
1102 static int __init wakeup_sources_debugfs_init(void)
1104 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1105 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1106 return 0;
1109 postcore_initcall(wakeup_sources_debugfs_init);