Merge branches 'pm-sleep' and 'pm-runtime'
[linux-2.6-microblaze.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 <trace/events/power.h>
18
19 #include "power.h"
20
21 /*
22  * If set, the suspend/hibernate code will abort transitions to a sleep state
23  * if wakeup events are registered during or immediately before the transition.
24  */
25 bool events_check_enabled __read_mostly;
26
27 /* If set and the system is suspending, terminate the suspend. */
28 static bool pm_abort_suspend __read_mostly;
29
30 /*
31  * Combined counters of registered wakeup events and wakeup events in progress.
32  * They need to be modified together atomically, so it's better to use one
33  * atomic variable to hold them both.
34  */
35 static atomic_t combined_event_count = ATOMIC_INIT(0);
36
37 #define IN_PROGRESS_BITS        (sizeof(int) * 4)
38 #define MAX_IN_PROGRESS         ((1 << IN_PROGRESS_BITS) - 1)
39
40 static void split_counters(unsigned int *cnt, unsigned int *inpr)
41 {
42         unsigned int comb = atomic_read(&combined_event_count);
43
44         *cnt = (comb >> IN_PROGRESS_BITS);
45         *inpr = comb & MAX_IN_PROGRESS;
46 }
47
48 /* A preserved old value of the events counter. */
49 static unsigned int saved_count;
50
51 static DEFINE_SPINLOCK(events_lock);
52
53 static void pm_wakeup_timer_fn(unsigned long data);
54
55 static LIST_HEAD(wakeup_sources);
56
57 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
58
59 static struct wakeup_source deleted_ws = {
60         .name = "deleted",
61         .lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
62 };
63
64 /**
65  * wakeup_source_prepare - Prepare a new wakeup source for initialization.
66  * @ws: Wakeup source to prepare.
67  * @name: Pointer to the name of the new wakeup source.
68  *
69  * Callers must ensure that the @name string won't be freed when @ws is still in
70  * use.
71  */
72 void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
73 {
74         if (ws) {
75                 memset(ws, 0, sizeof(*ws));
76                 ws->name = name;
77         }
78 }
79 EXPORT_SYMBOL_GPL(wakeup_source_prepare);
80
81 /**
82  * wakeup_source_create - Create a struct wakeup_source object.
83  * @name: Name of the new wakeup source.
84  */
85 struct wakeup_source *wakeup_source_create(const char *name)
86 {
87         struct wakeup_source *ws;
88
89         ws = kmalloc(sizeof(*ws), GFP_KERNEL);
90         if (!ws)
91                 return NULL;
92
93         wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL);
94         return ws;
95 }
96 EXPORT_SYMBOL_GPL(wakeup_source_create);
97
98 /**
99  * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
100  * @ws: Wakeup source to prepare for destruction.
101  *
102  * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
103  * be run in parallel with this function for the same wakeup source object.
104  */
105 void wakeup_source_drop(struct wakeup_source *ws)
106 {
107         if (!ws)
108                 return;
109
110         del_timer_sync(&ws->timer);
111         __pm_relax(ws);
112 }
113 EXPORT_SYMBOL_GPL(wakeup_source_drop);
114
115 /*
116  * Record wakeup_source statistics being deleted into a dummy wakeup_source.
117  */
118 static void wakeup_source_record(struct wakeup_source *ws)
119 {
120         unsigned long flags;
121
122         spin_lock_irqsave(&deleted_ws.lock, flags);
123
124         if (ws->event_count) {
125                 deleted_ws.total_time =
126                         ktime_add(deleted_ws.total_time, ws->total_time);
127                 deleted_ws.prevent_sleep_time =
128                         ktime_add(deleted_ws.prevent_sleep_time,
129                                   ws->prevent_sleep_time);
130                 deleted_ws.max_time =
131                         ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
132                                 deleted_ws.max_time : ws->max_time;
133                 deleted_ws.event_count += ws->event_count;
134                 deleted_ws.active_count += ws->active_count;
135                 deleted_ws.relax_count += ws->relax_count;
136                 deleted_ws.expire_count += ws->expire_count;
137                 deleted_ws.wakeup_count += ws->wakeup_count;
138         }
139
140         spin_unlock_irqrestore(&deleted_ws.lock, flags);
141 }
142
143 /**
144  * wakeup_source_destroy - Destroy a struct wakeup_source object.
145  * @ws: Wakeup source to destroy.
146  *
147  * Use only for wakeup source objects created with wakeup_source_create().
148  */
149 void wakeup_source_destroy(struct wakeup_source *ws)
150 {
151         if (!ws)
152                 return;
153
154         wakeup_source_drop(ws);
155         wakeup_source_record(ws);
156         kfree(ws->name);
157         kfree(ws);
158 }
159 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
160
161 /**
162  * wakeup_source_add - Add given object to the list of wakeup sources.
163  * @ws: Wakeup source object to add to the list.
164  */
165 void wakeup_source_add(struct wakeup_source *ws)
166 {
167         unsigned long flags;
168
169         if (WARN_ON(!ws))
170                 return;
171
172         spin_lock_init(&ws->lock);
173         setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
174         ws->active = false;
175         ws->last_time = ktime_get();
176
177         spin_lock_irqsave(&events_lock, flags);
178         list_add_rcu(&ws->entry, &wakeup_sources);
179         spin_unlock_irqrestore(&events_lock, flags);
180 }
181 EXPORT_SYMBOL_GPL(wakeup_source_add);
182
183 /**
184  * wakeup_source_remove - Remove given object from the wakeup sources list.
185  * @ws: Wakeup source object to remove from the list.
186  */
187 void wakeup_source_remove(struct wakeup_source *ws)
188 {
189         unsigned long flags;
190
191         if (WARN_ON(!ws))
192                 return;
193
194         spin_lock_irqsave(&events_lock, flags);
195         list_del_rcu(&ws->entry);
196         spin_unlock_irqrestore(&events_lock, flags);
197         synchronize_rcu();
198 }
199 EXPORT_SYMBOL_GPL(wakeup_source_remove);
200
201 /**
202  * wakeup_source_register - Create wakeup source and add it to the list.
203  * @name: Name of the wakeup source to register.
204  */
205 struct wakeup_source *wakeup_source_register(const char *name)
206 {
207         struct wakeup_source *ws;
208
209         ws = wakeup_source_create(name);
210         if (ws)
211                 wakeup_source_add(ws);
212
213         return ws;
214 }
215 EXPORT_SYMBOL_GPL(wakeup_source_register);
216
217 /**
218  * wakeup_source_unregister - Remove wakeup source from the list and remove it.
219  * @ws: Wakeup source object to unregister.
220  */
221 void wakeup_source_unregister(struct wakeup_source *ws)
222 {
223         if (ws) {
224                 wakeup_source_remove(ws);
225                 wakeup_source_destroy(ws);
226         }
227 }
228 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
229
230 /**
231  * device_wakeup_attach - Attach a wakeup source object to a device object.
232  * @dev: Device to handle.
233  * @ws: Wakeup source object to attach to @dev.
234  *
235  * This causes @dev to be treated as a wakeup device.
236  */
237 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
238 {
239         spin_lock_irq(&dev->power.lock);
240         if (dev->power.wakeup) {
241                 spin_unlock_irq(&dev->power.lock);
242                 return -EEXIST;
243         }
244         dev->power.wakeup = ws;
245         spin_unlock_irq(&dev->power.lock);
246         return 0;
247 }
248
249 /**
250  * device_wakeup_enable - Enable given device to be a wakeup source.
251  * @dev: Device to handle.
252  *
253  * Create a wakeup source object, register it and attach it to @dev.
254  */
255 int device_wakeup_enable(struct device *dev)
256 {
257         struct wakeup_source *ws;
258         int ret;
259
260         if (!dev || !dev->power.can_wakeup)
261                 return -EINVAL;
262
263         ws = wakeup_source_register(dev_name(dev));
264         if (!ws)
265                 return -ENOMEM;
266
267         ret = device_wakeup_attach(dev, ws);
268         if (ret)
269                 wakeup_source_unregister(ws);
270
271         return ret;
272 }
273 EXPORT_SYMBOL_GPL(device_wakeup_enable);
274
275 /**
276  * device_wakeup_detach - Detach a device's wakeup source object from it.
277  * @dev: Device to detach the wakeup source object from.
278  *
279  * After it returns, @dev will not be treated as a wakeup device any more.
280  */
281 static struct wakeup_source *device_wakeup_detach(struct device *dev)
282 {
283         struct wakeup_source *ws;
284
285         spin_lock_irq(&dev->power.lock);
286         ws = dev->power.wakeup;
287         dev->power.wakeup = NULL;
288         spin_unlock_irq(&dev->power.lock);
289         return ws;
290 }
291
292 /**
293  * device_wakeup_disable - Do not regard a device as a wakeup source any more.
294  * @dev: Device to handle.
295  *
296  * Detach the @dev's wakeup source object from it, unregister this wakeup source
297  * object and destroy it.
298  */
299 int device_wakeup_disable(struct device *dev)
300 {
301         struct wakeup_source *ws;
302
303         if (!dev || !dev->power.can_wakeup)
304                 return -EINVAL;
305
306         ws = device_wakeup_detach(dev);
307         if (ws)
308                 wakeup_source_unregister(ws);
309
310         return 0;
311 }
312 EXPORT_SYMBOL_GPL(device_wakeup_disable);
313
314 /**
315  * device_set_wakeup_capable - Set/reset device wakeup capability flag.
316  * @dev: Device to handle.
317  * @capable: Whether or not @dev is capable of waking up the system from sleep.
318  *
319  * If @capable is set, set the @dev's power.can_wakeup flag and add its
320  * wakeup-related attributes to sysfs.  Otherwise, unset the @dev's
321  * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
322  *
323  * This function may sleep and it can't be called from any context where
324  * sleeping is not allowed.
325  */
326 void device_set_wakeup_capable(struct device *dev, bool capable)
327 {
328         if (!!dev->power.can_wakeup == !!capable)
329                 return;
330
331         if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
332                 if (capable) {
333                         if (wakeup_sysfs_add(dev))
334                                 return;
335                 } else {
336                         wakeup_sysfs_remove(dev);
337                 }
338         }
339         dev->power.can_wakeup = capable;
340 }
341 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
342
343 /**
344  * device_init_wakeup - Device wakeup initialization.
345  * @dev: Device to handle.
346  * @enable: Whether or not to enable @dev as a wakeup device.
347  *
348  * By default, most devices should leave wakeup disabled.  The exceptions are
349  * devices that everyone expects to be wakeup sources: keyboards, power buttons,
350  * possibly network interfaces, etc.  Also, devices that don't generate their
351  * own wakeup requests but merely forward requests from one bus to another
352  * (like PCI bridges) should have wakeup enabled by default.
353  */
354 int device_init_wakeup(struct device *dev, bool enable)
355 {
356         int ret = 0;
357
358         if (!dev)
359                 return -EINVAL;
360
361         if (enable) {
362                 device_set_wakeup_capable(dev, true);
363                 ret = device_wakeup_enable(dev);
364         } else {
365                 if (dev->power.can_wakeup)
366                         device_wakeup_disable(dev);
367
368                 device_set_wakeup_capable(dev, false);
369         }
370
371         return ret;
372 }
373 EXPORT_SYMBOL_GPL(device_init_wakeup);
374
375 /**
376  * device_set_wakeup_enable - Enable or disable a device to wake up the system.
377  * @dev: Device to handle.
378  */
379 int device_set_wakeup_enable(struct device *dev, bool enable)
380 {
381         if (!dev || !dev->power.can_wakeup)
382                 return -EINVAL;
383
384         return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
385 }
386 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
387
388 /**
389  * wakeup_source_not_registered - validate the given wakeup source.
390  * @ws: Wakeup source to be validated.
391  */
392 static bool wakeup_source_not_registered(struct wakeup_source *ws)
393 {
394         /*
395          * Use timer struct to check if the given source is initialized
396          * by wakeup_source_add.
397          */
398         return ws->timer.function != pm_wakeup_timer_fn ||
399                    ws->timer.data != (unsigned long)ws;
400 }
401
402 /*
403  * The functions below use the observation that each wakeup event starts a
404  * period in which the system should not be suspended.  The moment this period
405  * will end depends on how the wakeup event is going to be processed after being
406  * detected and all of the possible cases can be divided into two distinct
407  * groups.
408  *
409  * First, a wakeup event may be detected by the same functional unit that will
410  * carry out the entire processing of it and possibly will pass it to user space
411  * for further processing.  In that case the functional unit that has detected
412  * the event may later "close" the "no suspend" period associated with it
413  * directly as soon as it has been dealt with.  The pair of pm_stay_awake() and
414  * pm_relax(), balanced with each other, is supposed to be used in such
415  * situations.
416  *
417  * Second, a wakeup event may be detected by one functional unit and processed
418  * by another one.  In that case the unit that has detected it cannot really
419  * "close" the "no suspend" period associated with it, unless it knows in
420  * advance what's going to happen to the event during processing.  This
421  * knowledge, however, may not be available to it, so it can simply specify time
422  * to wait before the system can be suspended and pass it as the second
423  * argument of pm_wakeup_event().
424  *
425  * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
426  * "no suspend" period will be ended either by the pm_relax(), or by the timer
427  * function executed when the timer expires, whichever comes first.
428  */
429
430 /**
431  * wakup_source_activate - Mark given wakeup source as active.
432  * @ws: Wakeup source to handle.
433  *
434  * Update the @ws' statistics and, if @ws has just been activated, notify the PM
435  * core of the event by incrementing the counter of of wakeup events being
436  * processed.
437  */
438 static void wakeup_source_activate(struct wakeup_source *ws)
439 {
440         unsigned int cec;
441
442         if (WARN_ONCE(wakeup_source_not_registered(ws),
443                         "unregistered wakeup source\n"))
444                 return;
445
446         /*
447          * active wakeup source should bring the system
448          * out of PM_SUSPEND_FREEZE state
449          */
450         freeze_wake();
451
452         ws->active = true;
453         ws->active_count++;
454         ws->last_time = ktime_get();
455         if (ws->autosleep_enabled)
456                 ws->start_prevent_time = ws->last_time;
457
458         /* Increment the counter of events in progress. */
459         cec = atomic_inc_return(&combined_event_count);
460
461         trace_wakeup_source_activate(ws->name, cec);
462 }
463
464 /**
465  * wakeup_source_report_event - Report wakeup event using the given source.
466  * @ws: Wakeup source to report the event for.
467  */
468 static void wakeup_source_report_event(struct wakeup_source *ws)
469 {
470         ws->event_count++;
471         /* This is racy, but the counter is approximate anyway. */
472         if (events_check_enabled)
473                 ws->wakeup_count++;
474
475         if (!ws->active)
476                 wakeup_source_activate(ws);
477 }
478
479 /**
480  * __pm_stay_awake - Notify the PM core of a wakeup event.
481  * @ws: Wakeup source object associated with the source of the event.
482  *
483  * It is safe to call this function from interrupt context.
484  */
485 void __pm_stay_awake(struct wakeup_source *ws)
486 {
487         unsigned long flags;
488
489         if (!ws)
490                 return;
491
492         spin_lock_irqsave(&ws->lock, flags);
493
494         wakeup_source_report_event(ws);
495         del_timer(&ws->timer);
496         ws->timer_expires = 0;
497
498         spin_unlock_irqrestore(&ws->lock, flags);
499 }
500 EXPORT_SYMBOL_GPL(__pm_stay_awake);
501
502 /**
503  * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
504  * @dev: Device the wakeup event is related to.
505  *
506  * Notify the PM core of a wakeup event (signaled by @dev) by calling
507  * __pm_stay_awake for the @dev's wakeup source object.
508  *
509  * Call this function after detecting of a wakeup event if pm_relax() is going
510  * to be called directly after processing the event (and possibly passing it to
511  * user space for further processing).
512  */
513 void pm_stay_awake(struct device *dev)
514 {
515         unsigned long flags;
516
517         if (!dev)
518                 return;
519
520         spin_lock_irqsave(&dev->power.lock, flags);
521         __pm_stay_awake(dev->power.wakeup);
522         spin_unlock_irqrestore(&dev->power.lock, flags);
523 }
524 EXPORT_SYMBOL_GPL(pm_stay_awake);
525
526 #ifdef CONFIG_PM_AUTOSLEEP
527 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
528 {
529         ktime_t delta = ktime_sub(now, ws->start_prevent_time);
530         ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
531 }
532 #else
533 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
534                                              ktime_t now) {}
535 #endif
536
537 /**
538  * wakup_source_deactivate - Mark given wakeup source as inactive.
539  * @ws: Wakeup source to handle.
540  *
541  * Update the @ws' statistics and notify the PM core that the wakeup source has
542  * become inactive by decrementing the counter of wakeup events being processed
543  * and incrementing the counter of registered wakeup events.
544  */
545 static void wakeup_source_deactivate(struct wakeup_source *ws)
546 {
547         unsigned int cnt, inpr, cec;
548         ktime_t duration;
549         ktime_t now;
550
551         ws->relax_count++;
552         /*
553          * __pm_relax() may be called directly or from a timer function.
554          * If it is called directly right after the timer function has been
555          * started, but before the timer function calls __pm_relax(), it is
556          * possible that __pm_stay_awake() will be called in the meantime and
557          * will set ws->active.  Then, ws->active may be cleared immediately
558          * by the __pm_relax() called from the timer function, but in such a
559          * case ws->relax_count will be different from ws->active_count.
560          */
561         if (ws->relax_count != ws->active_count) {
562                 ws->relax_count--;
563                 return;
564         }
565
566         ws->active = false;
567
568         now = ktime_get();
569         duration = ktime_sub(now, ws->last_time);
570         ws->total_time = ktime_add(ws->total_time, duration);
571         if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
572                 ws->max_time = duration;
573
574         ws->last_time = now;
575         del_timer(&ws->timer);
576         ws->timer_expires = 0;
577
578         if (ws->autosleep_enabled)
579                 update_prevent_sleep_time(ws, now);
580
581         /*
582          * Increment the counter of registered wakeup events and decrement the
583          * couter of wakeup events in progress simultaneously.
584          */
585         cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
586         trace_wakeup_source_deactivate(ws->name, cec);
587
588         split_counters(&cnt, &inpr);
589         if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
590                 wake_up(&wakeup_count_wait_queue);
591 }
592
593 /**
594  * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
595  * @ws: Wakeup source object associated with the source of the event.
596  *
597  * Call this function for wakeup events whose processing started with calling
598  * __pm_stay_awake().
599  *
600  * It is safe to call it from interrupt context.
601  */
602 void __pm_relax(struct wakeup_source *ws)
603 {
604         unsigned long flags;
605
606         if (!ws)
607                 return;
608
609         spin_lock_irqsave(&ws->lock, flags);
610         if (ws->active)
611                 wakeup_source_deactivate(ws);
612         spin_unlock_irqrestore(&ws->lock, flags);
613 }
614 EXPORT_SYMBOL_GPL(__pm_relax);
615
616 /**
617  * pm_relax - Notify the PM core that processing of a wakeup event has ended.
618  * @dev: Device that signaled the event.
619  *
620  * Execute __pm_relax() for the @dev's wakeup source object.
621  */
622 void pm_relax(struct device *dev)
623 {
624         unsigned long flags;
625
626         if (!dev)
627                 return;
628
629         spin_lock_irqsave(&dev->power.lock, flags);
630         __pm_relax(dev->power.wakeup);
631         spin_unlock_irqrestore(&dev->power.lock, flags);
632 }
633 EXPORT_SYMBOL_GPL(pm_relax);
634
635 /**
636  * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
637  * @data: Address of the wakeup source object associated with the event source.
638  *
639  * Call wakeup_source_deactivate() for the wakeup source whose address is stored
640  * in @data if it is currently active and its timer has not been canceled and
641  * the expiration time of the timer is not in future.
642  */
643 static void pm_wakeup_timer_fn(unsigned long data)
644 {
645         struct wakeup_source *ws = (struct wakeup_source *)data;
646         unsigned long flags;
647
648         spin_lock_irqsave(&ws->lock, flags);
649
650         if (ws->active && ws->timer_expires
651             && time_after_eq(jiffies, ws->timer_expires)) {
652                 wakeup_source_deactivate(ws);
653                 ws->expire_count++;
654         }
655
656         spin_unlock_irqrestore(&ws->lock, flags);
657 }
658
659 /**
660  * __pm_wakeup_event - Notify the PM core of a wakeup event.
661  * @ws: Wakeup source object associated with the event source.
662  * @msec: Anticipated event processing time (in milliseconds).
663  *
664  * Notify the PM core of a wakeup event whose source is @ws that will take
665  * approximately @msec milliseconds to be processed by the kernel.  If @ws is
666  * not active, activate it.  If @msec is nonzero, set up the @ws' timer to
667  * execute pm_wakeup_timer_fn() in future.
668  *
669  * It is safe to call this function from interrupt context.
670  */
671 void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
672 {
673         unsigned long flags;
674         unsigned long expires;
675
676         if (!ws)
677                 return;
678
679         spin_lock_irqsave(&ws->lock, flags);
680
681         wakeup_source_report_event(ws);
682
683         if (!msec) {
684                 wakeup_source_deactivate(ws);
685                 goto unlock;
686         }
687
688         expires = jiffies + msecs_to_jiffies(msec);
689         if (!expires)
690                 expires = 1;
691
692         if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
693                 mod_timer(&ws->timer, expires);
694                 ws->timer_expires = expires;
695         }
696
697  unlock:
698         spin_unlock_irqrestore(&ws->lock, flags);
699 }
700 EXPORT_SYMBOL_GPL(__pm_wakeup_event);
701
702
703 /**
704  * pm_wakeup_event - Notify the PM core of a wakeup event.
705  * @dev: Device the wakeup event is related to.
706  * @msec: Anticipated event processing time (in milliseconds).
707  *
708  * Call __pm_wakeup_event() for the @dev's wakeup source object.
709  */
710 void pm_wakeup_event(struct device *dev, unsigned int msec)
711 {
712         unsigned long flags;
713
714         if (!dev)
715                 return;
716
717         spin_lock_irqsave(&dev->power.lock, flags);
718         __pm_wakeup_event(dev->power.wakeup, msec);
719         spin_unlock_irqrestore(&dev->power.lock, flags);
720 }
721 EXPORT_SYMBOL_GPL(pm_wakeup_event);
722
723 void pm_print_active_wakeup_sources(void)
724 {
725         struct wakeup_source *ws;
726         int active = 0;
727         struct wakeup_source *last_activity_ws = NULL;
728
729         rcu_read_lock();
730         list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
731                 if (ws->active) {
732                         pr_info("active wakeup source: %s\n", ws->name);
733                         active = 1;
734                 } else if (!active &&
735                            (!last_activity_ws ||
736                             ktime_to_ns(ws->last_time) >
737                             ktime_to_ns(last_activity_ws->last_time))) {
738                         last_activity_ws = ws;
739                 }
740         }
741
742         if (!active && last_activity_ws)
743                 pr_info("last active wakeup source: %s\n",
744                         last_activity_ws->name);
745         rcu_read_unlock();
746 }
747 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
748
749 /**
750  * pm_wakeup_pending - Check if power transition in progress should be aborted.
751  *
752  * Compare the current number of registered wakeup events with its preserved
753  * value from the past and return true if new wakeup events have been registered
754  * since the old value was stored.  Also return true if the current number of
755  * wakeup events being processed is different from zero.
756  */
757 bool pm_wakeup_pending(void)
758 {
759         unsigned long flags;
760         bool ret = false;
761
762         spin_lock_irqsave(&events_lock, flags);
763         if (events_check_enabled) {
764                 unsigned int cnt, inpr;
765
766                 split_counters(&cnt, &inpr);
767                 ret = (cnt != saved_count || inpr > 0);
768                 events_check_enabled = !ret;
769         }
770         spin_unlock_irqrestore(&events_lock, flags);
771
772         if (ret) {
773                 pr_info("PM: Wakeup pending, aborting suspend\n");
774                 pm_print_active_wakeup_sources();
775         }
776
777         return ret || pm_abort_suspend;
778 }
779
780 void pm_system_wakeup(void)
781 {
782         pm_abort_suspend = true;
783         freeze_wake();
784 }
785 EXPORT_SYMBOL_GPL(pm_system_wakeup);
786
787 void pm_wakeup_clear(void)
788 {
789         pm_abort_suspend = false;
790 }
791
792 /**
793  * pm_get_wakeup_count - Read the number of registered wakeup events.
794  * @count: Address to store the value at.
795  * @block: Whether or not to block.
796  *
797  * Store the number of registered wakeup events at the address in @count.  If
798  * @block is set, block until the current number of wakeup events being
799  * processed is zero.
800  *
801  * Return 'false' if the current number of wakeup events being processed is
802  * nonzero.  Otherwise return 'true'.
803  */
804 bool pm_get_wakeup_count(unsigned int *count, bool block)
805 {
806         unsigned int cnt, inpr;
807
808         if (block) {
809                 DEFINE_WAIT(wait);
810
811                 for (;;) {
812                         prepare_to_wait(&wakeup_count_wait_queue, &wait,
813                                         TASK_INTERRUPTIBLE);
814                         split_counters(&cnt, &inpr);
815                         if (inpr == 0 || signal_pending(current))
816                                 break;
817
818                         schedule();
819                 }
820                 finish_wait(&wakeup_count_wait_queue, &wait);
821         }
822
823         split_counters(&cnt, &inpr);
824         *count = cnt;
825         return !inpr;
826 }
827
828 /**
829  * pm_save_wakeup_count - Save the current number of registered wakeup events.
830  * @count: Value to compare with the current number of registered wakeup events.
831  *
832  * If @count is equal to the current number of registered wakeup events and the
833  * current number of wakeup events being processed is zero, store @count as the
834  * old number of registered wakeup events for pm_check_wakeup_events(), enable
835  * wakeup events detection and return 'true'.  Otherwise disable wakeup events
836  * detection and return 'false'.
837  */
838 bool pm_save_wakeup_count(unsigned int count)
839 {
840         unsigned int cnt, inpr;
841         unsigned long flags;
842
843         events_check_enabled = false;
844         spin_lock_irqsave(&events_lock, flags);
845         split_counters(&cnt, &inpr);
846         if (cnt == count && inpr == 0) {
847                 saved_count = count;
848                 events_check_enabled = true;
849         }
850         spin_unlock_irqrestore(&events_lock, flags);
851         return events_check_enabled;
852 }
853
854 #ifdef CONFIG_PM_AUTOSLEEP
855 /**
856  * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
857  * @enabled: Whether to set or to clear the autosleep_enabled flags.
858  */
859 void pm_wakep_autosleep_enabled(bool set)
860 {
861         struct wakeup_source *ws;
862         ktime_t now = ktime_get();
863
864         rcu_read_lock();
865         list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
866                 spin_lock_irq(&ws->lock);
867                 if (ws->autosleep_enabled != set) {
868                         ws->autosleep_enabled = set;
869                         if (ws->active) {
870                                 if (set)
871                                         ws->start_prevent_time = now;
872                                 else
873                                         update_prevent_sleep_time(ws, now);
874                         }
875                 }
876                 spin_unlock_irq(&ws->lock);
877         }
878         rcu_read_unlock();
879 }
880 #endif /* CONFIG_PM_AUTOSLEEP */
881
882 static struct dentry *wakeup_sources_stats_dentry;
883
884 /**
885  * print_wakeup_source_stats - Print wakeup source statistics information.
886  * @m: seq_file to print the statistics into.
887  * @ws: Wakeup source object to print the statistics for.
888  */
889 static int print_wakeup_source_stats(struct seq_file *m,
890                                      struct wakeup_source *ws)
891 {
892         unsigned long flags;
893         ktime_t total_time;
894         ktime_t max_time;
895         unsigned long active_count;
896         ktime_t active_time;
897         ktime_t prevent_sleep_time;
898
899         spin_lock_irqsave(&ws->lock, flags);
900
901         total_time = ws->total_time;
902         max_time = ws->max_time;
903         prevent_sleep_time = ws->prevent_sleep_time;
904         active_count = ws->active_count;
905         if (ws->active) {
906                 ktime_t now = ktime_get();
907
908                 active_time = ktime_sub(now, ws->last_time);
909                 total_time = ktime_add(total_time, active_time);
910                 if (active_time.tv64 > max_time.tv64)
911                         max_time = active_time;
912
913                 if (ws->autosleep_enabled)
914                         prevent_sleep_time = ktime_add(prevent_sleep_time,
915                                 ktime_sub(now, ws->start_prevent_time));
916         } else {
917                 active_time = ktime_set(0, 0);
918         }
919
920         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",
921                    ws->name, active_count, ws->event_count,
922                    ws->wakeup_count, ws->expire_count,
923                    ktime_to_ms(active_time), ktime_to_ms(total_time),
924                    ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
925                    ktime_to_ms(prevent_sleep_time));
926
927         spin_unlock_irqrestore(&ws->lock, flags);
928
929         return 0;
930 }
931
932 /**
933  * wakeup_sources_stats_show - Print wakeup sources statistics information.
934  * @m: seq_file to print the statistics into.
935  */
936 static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
937 {
938         struct wakeup_source *ws;
939
940         seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
941                 "expire_count\tactive_since\ttotal_time\tmax_time\t"
942                 "last_change\tprevent_suspend_time\n");
943
944         rcu_read_lock();
945         list_for_each_entry_rcu(ws, &wakeup_sources, entry)
946                 print_wakeup_source_stats(m, ws);
947         rcu_read_unlock();
948
949         print_wakeup_source_stats(m, &deleted_ws);
950
951         return 0;
952 }
953
954 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
955 {
956         return single_open(file, wakeup_sources_stats_show, NULL);
957 }
958
959 static const struct file_operations wakeup_sources_stats_fops = {
960         .owner = THIS_MODULE,
961         .open = wakeup_sources_stats_open,
962         .read = seq_read,
963         .llseek = seq_lseek,
964         .release = single_release,
965 };
966
967 static int __init wakeup_sources_debugfs_init(void)
968 {
969         wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
970                         S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
971         return 0;
972 }
973
974 postcore_initcall(wakeup_sources_debugfs_init);