GNU Linux-libre 4.4.296-gnu1
[releases.git] / drivers / base / power / wakeup.c
1 /*
2  * drivers/base/power/wakeup.c - System wakeup events framework
3  *
4  * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5  *
6  * This file is released under the GPLv2.
7  */
8
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>
19
20 #include "power.h"
21
22 /*
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.
25  */
26 bool events_check_enabled __read_mostly;
27
28 /* First wakeup IRQ seen by the kernel in the last cycle. */
29 unsigned int pm_wakeup_irq __read_mostly;
30
31 /* If set and the system is suspending, terminate the suspend. */
32 static bool pm_abort_suspend __read_mostly;
33
34 /*
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.
38  */
39 static atomic_t combined_event_count = ATOMIC_INIT(0);
40
41 #define IN_PROGRESS_BITS        (sizeof(int) * 4)
42 #define MAX_IN_PROGRESS         ((1 << IN_PROGRESS_BITS) - 1)
43
44 static void split_counters(unsigned int *cnt, unsigned int *inpr)
45 {
46         unsigned int comb = atomic_read(&combined_event_count);
47
48         *cnt = (comb >> IN_PROGRESS_BITS);
49         *inpr = comb & MAX_IN_PROGRESS;
50 }
51
52 /* A preserved old value of the events counter. */
53 static unsigned int saved_count;
54
55 static DEFINE_SPINLOCK(events_lock);
56
57 static void pm_wakeup_timer_fn(unsigned long data);
58
59 static LIST_HEAD(wakeup_sources);
60
61 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
62
63 DEFINE_STATIC_SRCU(wakeup_srcu);
64
65 static struct wakeup_source deleted_ws = {
66         .name = "deleted",
67         .lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
68 };
69
70 /**
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.
74  *
75  * Callers must ensure that the @name string won't be freed when @ws is still in
76  * use.
77  */
78 void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
79 {
80         if (ws) {
81                 memset(ws, 0, sizeof(*ws));
82                 ws->name = name;
83         }
84 }
85 EXPORT_SYMBOL_GPL(wakeup_source_prepare);
86
87 /**
88  * wakeup_source_create - Create a struct wakeup_source object.
89  * @name: Name of the new wakeup source.
90  */
91 struct wakeup_source *wakeup_source_create(const char *name)
92 {
93         struct wakeup_source *ws;
94
95         ws = kmalloc(sizeof(*ws), GFP_KERNEL);
96         if (!ws)
97                 return NULL;
98
99         wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
100         return ws;
101 }
102 EXPORT_SYMBOL_GPL(wakeup_source_create);
103
104 /**
105  * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
106  * @ws: Wakeup source to prepare for destruction.
107  *
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.
110  */
111 void wakeup_source_drop(struct wakeup_source *ws)
112 {
113         if (!ws)
114                 return;
115
116         __pm_relax(ws);
117 }
118 EXPORT_SYMBOL_GPL(wakeup_source_drop);
119
120 /*
121  * Record wakeup_source statistics being deleted into a dummy wakeup_source.
122  */
123 static void wakeup_source_record(struct wakeup_source *ws)
124 {
125         unsigned long flags;
126
127         spin_lock_irqsave(&deleted_ws.lock, flags);
128
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;
143         }
144
145         spin_unlock_irqrestore(&deleted_ws.lock, flags);
146 }
147
148 /**
149  * wakeup_source_destroy - Destroy a struct wakeup_source object.
150  * @ws: Wakeup source to destroy.
151  *
152  * Use only for wakeup source objects created with wakeup_source_create().
153  */
154 void wakeup_source_destroy(struct wakeup_source *ws)
155 {
156         if (!ws)
157                 return;
158
159         wakeup_source_drop(ws);
160         wakeup_source_record(ws);
161         kfree_const(ws->name);
162         kfree(ws);
163 }
164 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
165
166 /**
167  * wakeup_source_add - Add given object to the list of wakeup sources.
168  * @ws: Wakeup source object to add to the list.
169  */
170 void wakeup_source_add(struct wakeup_source *ws)
171 {
172         unsigned long flags;
173
174         if (WARN_ON(!ws))
175                 return;
176
177         spin_lock_init(&ws->lock);
178         setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
179         ws->active = false;
180         ws->last_time = ktime_get();
181
182         spin_lock_irqsave(&events_lock, flags);
183         list_add_rcu(&ws->entry, &wakeup_sources);
184         spin_unlock_irqrestore(&events_lock, flags);
185 }
186 EXPORT_SYMBOL_GPL(wakeup_source_add);
187
188 /**
189  * wakeup_source_remove - Remove given object from the wakeup sources list.
190  * @ws: Wakeup source object to remove from the list.
191  */
192 void wakeup_source_remove(struct wakeup_source *ws)
193 {
194         unsigned long flags;
195
196         if (WARN_ON(!ws))
197                 return;
198
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);
203
204         del_timer_sync(&ws->timer);
205         /*
206          * Clear timer.function to make wakeup_source_not_registered() treat
207          * this wakeup source as not registered.
208          */
209         ws->timer.function = NULL;
210 }
211 EXPORT_SYMBOL_GPL(wakeup_source_remove);
212
213 /**
214  * wakeup_source_register - Create wakeup source and add it to the list.
215  * @name: Name of the wakeup source to register.
216  */
217 struct wakeup_source *wakeup_source_register(const char *name)
218 {
219         struct wakeup_source *ws;
220
221         ws = wakeup_source_create(name);
222         if (ws)
223                 wakeup_source_add(ws);
224
225         return ws;
226 }
227 EXPORT_SYMBOL_GPL(wakeup_source_register);
228
229 /**
230  * wakeup_source_unregister - Remove wakeup source from the list and remove it.
231  * @ws: Wakeup source object to unregister.
232  */
233 void wakeup_source_unregister(struct wakeup_source *ws)
234 {
235         if (ws) {
236                 wakeup_source_remove(ws);
237                 wakeup_source_destroy(ws);
238         }
239 }
240 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
241
242 /**
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.
246  *
247  * This causes @dev to be treated as a wakeup device.
248  */
249 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
250 {
251         spin_lock_irq(&dev->power.lock);
252         if (dev->power.wakeup) {
253                 spin_unlock_irq(&dev->power.lock);
254                 return -EEXIST;
255         }
256         dev->power.wakeup = ws;
257         spin_unlock_irq(&dev->power.lock);
258         return 0;
259 }
260
261 /**
262  * device_wakeup_enable - Enable given device to be a wakeup source.
263  * @dev: Device to handle.
264  *
265  * Create a wakeup source object, register it and attach it to @dev.
266  */
267 int device_wakeup_enable(struct device *dev)
268 {
269         struct wakeup_source *ws;
270         int ret;
271
272         if (!dev || !dev->power.can_wakeup)
273                 return -EINVAL;
274
275         ws = wakeup_source_register(dev_name(dev));
276         if (!ws)
277                 return -ENOMEM;
278
279         ret = device_wakeup_attach(dev, ws);
280         if (ret)
281                 wakeup_source_unregister(ws);
282
283         return ret;
284 }
285 EXPORT_SYMBOL_GPL(device_wakeup_enable);
286
287 /**
288  * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
289  * @dev: Device to handle
290  * @wakeirq: Device specific wakeirq entry
291  *
292  * Attach a device wakeirq to the wakeup source so the device
293  * wake IRQ can be configured automatically for suspend and
294  * resume.
295  *
296  * Call under the device's power.lock lock.
297  */
298 int device_wakeup_attach_irq(struct device *dev,
299                              struct wake_irq *wakeirq)
300 {
301         struct wakeup_source *ws;
302
303         ws = dev->power.wakeup;
304         if (!ws) {
305                 dev_err(dev, "forgot to call call device_init_wakeup?\n");
306                 return -EINVAL;
307         }
308
309         if (ws->wakeirq)
310                 return -EEXIST;
311
312         ws->wakeirq = wakeirq;
313         return 0;
314 }
315
316 /**
317  * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
318  * @dev: Device to handle
319  *
320  * Removes a device wakeirq from the wakeup source.
321  *
322  * Call under the device's power.lock lock.
323  */
324 void device_wakeup_detach_irq(struct device *dev)
325 {
326         struct wakeup_source *ws;
327
328         ws = dev->power.wakeup;
329         if (ws)
330                 ws->wakeirq = NULL;
331 }
332
333 /**
334  * device_wakeup_arm_wake_irqs(void)
335  *
336  * Itereates over the list of device wakeirqs to arm them.
337  */
338 void device_wakeup_arm_wake_irqs(void)
339 {
340         struct wakeup_source *ws;
341         int srcuidx;
342
343         srcuidx = srcu_read_lock(&wakeup_srcu);
344         list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
345                 if (ws->wakeirq)
346                         dev_pm_arm_wake_irq(ws->wakeirq);
347         }
348         srcu_read_unlock(&wakeup_srcu, srcuidx);
349 }
350
351 /**
352  * device_wakeup_disarm_wake_irqs(void)
353  *
354  * Itereates over the list of device wakeirqs to disarm them.
355  */
356 void device_wakeup_disarm_wake_irqs(void)
357 {
358         struct wakeup_source *ws;
359         int srcuidx;
360
361         srcuidx = srcu_read_lock(&wakeup_srcu);
362         list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
363                 if (ws->wakeirq)
364                         dev_pm_disarm_wake_irq(ws->wakeirq);
365         }
366         srcu_read_unlock(&wakeup_srcu, srcuidx);
367 }
368
369 /**
370  * device_wakeup_detach - Detach a device's wakeup source object from it.
371  * @dev: Device to detach the wakeup source object from.
372  *
373  * After it returns, @dev will not be treated as a wakeup device any more.
374  */
375 static struct wakeup_source *device_wakeup_detach(struct device *dev)
376 {
377         struct wakeup_source *ws;
378
379         spin_lock_irq(&dev->power.lock);
380         ws = dev->power.wakeup;
381         dev->power.wakeup = NULL;
382         spin_unlock_irq(&dev->power.lock);
383         return ws;
384 }
385
386 /**
387  * device_wakeup_disable - Do not regard a device as a wakeup source any more.
388  * @dev: Device to handle.
389  *
390  * Detach the @dev's wakeup source object from it, unregister this wakeup source
391  * object and destroy it.
392  */
393 int device_wakeup_disable(struct device *dev)
394 {
395         struct wakeup_source *ws;
396
397         if (!dev || !dev->power.can_wakeup)
398                 return -EINVAL;
399
400         ws = device_wakeup_detach(dev);
401         if (ws)
402                 wakeup_source_unregister(ws);
403
404         return 0;
405 }
406 EXPORT_SYMBOL_GPL(device_wakeup_disable);
407
408 /**
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.
412  *
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.
416  *
417  * This function may sleep and it can't be called from any context where
418  * sleeping is not allowed.
419  */
420 void device_set_wakeup_capable(struct device *dev, bool capable)
421 {
422         if (!!dev->power.can_wakeup == !!capable)
423                 return;
424
425         if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
426                 if (capable) {
427                         if (wakeup_sysfs_add(dev))
428                                 return;
429                 } else {
430                         wakeup_sysfs_remove(dev);
431                 }
432         }
433         dev->power.can_wakeup = capable;
434 }
435 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
436
437 /**
438  * device_init_wakeup - Device wakeup initialization.
439  * @dev: Device to handle.
440  * @enable: Whether or not to enable @dev as a wakeup device.
441  *
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.
447  */
448 int device_init_wakeup(struct device *dev, bool enable)
449 {
450         int ret = 0;
451
452         if (!dev)
453                 return -EINVAL;
454
455         if (enable) {
456                 device_set_wakeup_capable(dev, true);
457                 ret = device_wakeup_enable(dev);
458         } else {
459                 if (dev->power.can_wakeup)
460                         device_wakeup_disable(dev);
461
462                 device_set_wakeup_capable(dev, false);
463         }
464
465         return ret;
466 }
467 EXPORT_SYMBOL_GPL(device_init_wakeup);
468
469 /**
470  * device_set_wakeup_enable - Enable or disable a device to wake up the system.
471  * @dev: Device to handle.
472  */
473 int device_set_wakeup_enable(struct device *dev, bool enable)
474 {
475         if (!dev || !dev->power.can_wakeup)
476                 return -EINVAL;
477
478         return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
479 }
480 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
481
482 /**
483  * wakeup_source_not_registered - validate the given wakeup source.
484  * @ws: Wakeup source to be validated.
485  */
486 static bool wakeup_source_not_registered(struct wakeup_source *ws)
487 {
488         /*
489          * Use timer struct to check if the given source is initialized
490          * by wakeup_source_add.
491          */
492         return ws->timer.function != pm_wakeup_timer_fn ||
493                    ws->timer.data != (unsigned long)ws;
494 }
495
496 /*
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
501  * groups.
502  *
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
509  * situations.
510  *
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().
518  *
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.
522  */
523
524 /**
525  * wakup_source_activate - Mark given wakeup source as active.
526  * @ws: Wakeup source to handle.
527  *
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
530  * processed.
531  */
532 static void wakeup_source_activate(struct wakeup_source *ws)
533 {
534         unsigned int cec;
535
536         if (WARN_ONCE(wakeup_source_not_registered(ws),
537                         "unregistered wakeup source\n"))
538                 return;
539
540         /*
541          * active wakeup source should bring the system
542          * out of PM_SUSPEND_FREEZE state
543          */
544         freeze_wake();
545
546         ws->active = true;
547         ws->active_count++;
548         ws->last_time = ktime_get();
549         if (ws->autosleep_enabled)
550                 ws->start_prevent_time = ws->last_time;
551
552         /* Increment the counter of events in progress. */
553         cec = atomic_inc_return(&combined_event_count);
554
555         trace_wakeup_source_activate(ws->name, cec);
556 }
557
558 /**
559  * wakeup_source_report_event - Report wakeup event using the given source.
560  * @ws: Wakeup source to report the event for.
561  */
562 static void wakeup_source_report_event(struct wakeup_source *ws)
563 {
564         ws->event_count++;
565         /* This is racy, but the counter is approximate anyway. */
566         if (events_check_enabled)
567                 ws->wakeup_count++;
568
569         if (!ws->active)
570                 wakeup_source_activate(ws);
571 }
572
573 /**
574  * __pm_stay_awake - Notify the PM core of a wakeup event.
575  * @ws: Wakeup source object associated with the source of the event.
576  *
577  * It is safe to call this function from interrupt context.
578  */
579 void __pm_stay_awake(struct wakeup_source *ws)
580 {
581         unsigned long flags;
582
583         if (!ws)
584                 return;
585
586         spin_lock_irqsave(&ws->lock, flags);
587
588         wakeup_source_report_event(ws);
589         del_timer(&ws->timer);
590         ws->timer_expires = 0;
591
592         spin_unlock_irqrestore(&ws->lock, flags);
593 }
594 EXPORT_SYMBOL_GPL(__pm_stay_awake);
595
596 /**
597  * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
598  * @dev: Device the wakeup event is related to.
599  *
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.
602  *
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).
606  */
607 void pm_stay_awake(struct device *dev)
608 {
609         unsigned long flags;
610
611         if (!dev)
612                 return;
613
614         spin_lock_irqsave(&dev->power.lock, flags);
615         __pm_stay_awake(dev->power.wakeup);
616         spin_unlock_irqrestore(&dev->power.lock, flags);
617 }
618 EXPORT_SYMBOL_GPL(pm_stay_awake);
619
620 #ifdef CONFIG_PM_AUTOSLEEP
621 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
622 {
623         ktime_t delta = ktime_sub(now, ws->start_prevent_time);
624         ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
625 }
626 #else
627 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
628                                              ktime_t now) {}
629 #endif
630
631 /**
632  * wakup_source_deactivate - Mark given wakeup source as inactive.
633  * @ws: Wakeup source to handle.
634  *
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.
638  */
639 static void wakeup_source_deactivate(struct wakeup_source *ws)
640 {
641         unsigned int cnt, inpr, cec;
642         ktime_t duration;
643         ktime_t now;
644
645         ws->relax_count++;
646         /*
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.
654          */
655         if (ws->relax_count != ws->active_count) {
656                 ws->relax_count--;
657                 return;
658         }
659
660         ws->active = false;
661
662         now = ktime_get();
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;
667
668         ws->last_time = now;
669         del_timer(&ws->timer);
670         ws->timer_expires = 0;
671
672         if (ws->autosleep_enabled)
673                 update_prevent_sleep_time(ws, now);
674
675         /*
676          * Increment the counter of registered wakeup events and decrement the
677          * couter of wakeup events in progress simultaneously.
678          */
679         cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
680         trace_wakeup_source_deactivate(ws->name, cec);
681
682         split_counters(&cnt, &inpr);
683         if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
684                 wake_up(&wakeup_count_wait_queue);
685 }
686
687 /**
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.
690  *
691  * Call this function for wakeup events whose processing started with calling
692  * __pm_stay_awake().
693  *
694  * It is safe to call it from interrupt context.
695  */
696 void __pm_relax(struct wakeup_source *ws)
697 {
698         unsigned long flags;
699
700         if (!ws)
701                 return;
702
703         spin_lock_irqsave(&ws->lock, flags);
704         if (ws->active)
705                 wakeup_source_deactivate(ws);
706         spin_unlock_irqrestore(&ws->lock, flags);
707 }
708 EXPORT_SYMBOL_GPL(__pm_relax);
709
710 /**
711  * pm_relax - Notify the PM core that processing of a wakeup event has ended.
712  * @dev: Device that signaled the event.
713  *
714  * Execute __pm_relax() for the @dev's wakeup source object.
715  */
716 void pm_relax(struct device *dev)
717 {
718         unsigned long flags;
719
720         if (!dev)
721                 return;
722
723         spin_lock_irqsave(&dev->power.lock, flags);
724         __pm_relax(dev->power.wakeup);
725         spin_unlock_irqrestore(&dev->power.lock, flags);
726 }
727 EXPORT_SYMBOL_GPL(pm_relax);
728
729 /**
730  * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
731  * @data: Address of the wakeup source object associated with the event source.
732  *
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.
736  */
737 static void pm_wakeup_timer_fn(unsigned long data)
738 {
739         struct wakeup_source *ws = (struct wakeup_source *)data;
740         unsigned long flags;
741
742         spin_lock_irqsave(&ws->lock, flags);
743
744         if (ws->active && ws->timer_expires
745             && time_after_eq(jiffies, ws->timer_expires)) {
746                 wakeup_source_deactivate(ws);
747                 ws->expire_count++;
748         }
749
750         spin_unlock_irqrestore(&ws->lock, flags);
751 }
752
753 /**
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).
757  *
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.
762  *
763  * It is safe to call this function from interrupt context.
764  */
765 void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
766 {
767         unsigned long flags;
768         unsigned long expires;
769
770         if (!ws)
771                 return;
772
773         spin_lock_irqsave(&ws->lock, flags);
774
775         wakeup_source_report_event(ws);
776
777         if (!msec) {
778                 wakeup_source_deactivate(ws);
779                 goto unlock;
780         }
781
782         expires = jiffies + msecs_to_jiffies(msec);
783         if (!expires)
784                 expires = 1;
785
786         if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
787                 mod_timer(&ws->timer, expires);
788                 ws->timer_expires = expires;
789         }
790
791  unlock:
792         spin_unlock_irqrestore(&ws->lock, flags);
793 }
794 EXPORT_SYMBOL_GPL(__pm_wakeup_event);
795
796
797 /**
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).
801  *
802  * Call __pm_wakeup_event() for the @dev's wakeup source object.
803  */
804 void pm_wakeup_event(struct device *dev, unsigned int msec)
805 {
806         unsigned long flags;
807
808         if (!dev)
809                 return;
810
811         spin_lock_irqsave(&dev->power.lock, flags);
812         __pm_wakeup_event(dev->power.wakeup, msec);
813         spin_unlock_irqrestore(&dev->power.lock, flags);
814 }
815 EXPORT_SYMBOL_GPL(pm_wakeup_event);
816
817 void pm_print_active_wakeup_sources(void)
818 {
819         struct wakeup_source *ws;
820         int srcuidx, active = 0;
821         struct wakeup_source *last_activity_ws = NULL;
822
823         srcuidx = srcu_read_lock(&wakeup_srcu);
824         list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
825                 if (ws->active) {
826                         pr_info("active wakeup source: %s\n", ws->name);
827                         active = 1;
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;
833                 }
834         }
835
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);
840 }
841 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
842
843 /**
844  * pm_wakeup_pending - Check if power transition in progress should be aborted.
845  *
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.
850  */
851 bool pm_wakeup_pending(void)
852 {
853         unsigned long flags;
854         bool ret = false;
855
856         spin_lock_irqsave(&events_lock, flags);
857         if (events_check_enabled) {
858                 unsigned int cnt, inpr;
859
860                 split_counters(&cnt, &inpr);
861                 ret = (cnt != saved_count || inpr > 0);
862                 events_check_enabled = !ret;
863         }
864         spin_unlock_irqrestore(&events_lock, flags);
865
866         if (ret) {
867                 pr_info("PM: Wakeup pending, aborting suspend\n");
868                 pm_print_active_wakeup_sources();
869         }
870
871         return ret || pm_abort_suspend;
872 }
873
874 void pm_system_wakeup(void)
875 {
876         pm_abort_suspend = true;
877         freeze_wake();
878 }
879 EXPORT_SYMBOL_GPL(pm_system_wakeup);
880
881 void pm_wakeup_clear(void)
882 {
883         pm_abort_suspend = false;
884         pm_wakeup_irq = 0;
885 }
886
887 void pm_system_irq_wakeup(unsigned int irq_number)
888 {
889         if (pm_wakeup_irq == 0) {
890                 pm_wakeup_irq = irq_number;
891                 pm_system_wakeup();
892         }
893 }
894
895 /**
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.
899  *
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
902  * processed is zero.
903  *
904  * Return 'false' if the current number of wakeup events being processed is
905  * nonzero.  Otherwise return 'true'.
906  */
907 bool pm_get_wakeup_count(unsigned int *count, bool block)
908 {
909         unsigned int cnt, inpr;
910
911         if (block) {
912                 DEFINE_WAIT(wait);
913
914                 for (;;) {
915                         prepare_to_wait(&wakeup_count_wait_queue, &wait,
916                                         TASK_INTERRUPTIBLE);
917                         split_counters(&cnt, &inpr);
918                         if (inpr == 0 || signal_pending(current))
919                                 break;
920
921                         schedule();
922                 }
923                 finish_wait(&wakeup_count_wait_queue, &wait);
924         }
925
926         split_counters(&cnt, &inpr);
927         *count = cnt;
928         return !inpr;
929 }
930
931 /**
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.
934  *
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'.
940  */
941 bool pm_save_wakeup_count(unsigned int count)
942 {
943         unsigned int cnt, inpr;
944         unsigned long flags;
945
946         events_check_enabled = false;
947         spin_lock_irqsave(&events_lock, flags);
948         split_counters(&cnt, &inpr);
949         if (cnt == count && inpr == 0) {
950                 saved_count = count;
951                 events_check_enabled = true;
952         }
953         spin_unlock_irqrestore(&events_lock, flags);
954         return events_check_enabled;
955 }
956
957 #ifdef CONFIG_PM_AUTOSLEEP
958 /**
959  * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
960  * @enabled: Whether to set or to clear the autosleep_enabled flags.
961  */
962 void pm_wakep_autosleep_enabled(bool set)
963 {
964         struct wakeup_source *ws;
965         ktime_t now = ktime_get();
966         int srcuidx;
967
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;
973                         if (ws->active) {
974                                 if (set)
975                                         ws->start_prevent_time = now;
976                                 else
977                                         update_prevent_sleep_time(ws, now);
978                         }
979                 }
980                 spin_unlock_irq(&ws->lock);
981         }
982         srcu_read_unlock(&wakeup_srcu, srcuidx);
983 }
984 #endif /* CONFIG_PM_AUTOSLEEP */
985
986 static struct dentry *wakeup_sources_stats_dentry;
987
988 /**
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.
992  */
993 static int print_wakeup_source_stats(struct seq_file *m,
994                                      struct wakeup_source *ws)
995 {
996         unsigned long flags;
997         ktime_t total_time;
998         ktime_t max_time;
999         unsigned long active_count;
1000         ktime_t active_time;
1001         ktime_t prevent_sleep_time;
1002
1003         spin_lock_irqsave(&ws->lock, flags);
1004
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;
1009         if (ws->active) {
1010                 ktime_t now = ktime_get();
1011
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;
1016
1017                 if (ws->autosleep_enabled)
1018                         prevent_sleep_time = ktime_add(prevent_sleep_time,
1019                                 ktime_sub(now, ws->start_prevent_time));
1020         } else {
1021                 active_time = ktime_set(0, 0);
1022         }
1023
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));
1030
1031         spin_unlock_irqrestore(&ws->lock, flags);
1032
1033         return 0;
1034 }
1035
1036 /**
1037  * wakeup_sources_stats_show - Print wakeup sources statistics information.
1038  * @m: seq_file to print the statistics into.
1039  */
1040 static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
1041 {
1042         struct wakeup_source *ws;
1043         int srcuidx;
1044
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");
1048
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);
1053
1054         print_wakeup_source_stats(m, &deleted_ws);
1055
1056         return 0;
1057 }
1058
1059 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1060 {
1061         return single_open(file, wakeup_sources_stats_show, NULL);
1062 }
1063
1064 static const struct file_operations wakeup_sources_stats_fops = {
1065         .owner = THIS_MODULE,
1066         .open = wakeup_sources_stats_open,
1067         .read = seq_read,
1068         .llseek = seq_lseek,
1069         .release = single_release,
1070 };
1071
1072 static int __init wakeup_sources_debugfs_init(void)
1073 {
1074         wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1075                         S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1076         return 0;
1077 }
1078
1079 postcore_initcall(wakeup_sources_debugfs_init);