blob: c2744b30d5d92e9dde512e492cf9fdf44f21b5ef [file] [log] [blame]
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001/*
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>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040013#include <linux/export.h>
Rafael J. Wysockic125e962010-07-05 22:43:53 +020014#include <linux/suspend.h>
Rafael J. Wysocki9c034392010-10-19 23:42:49 +020015#include <linux/seq_file.h>
16#include <linux/debugfs.h>
Arve Hjønnevåg6791e362012-04-29 22:53:02 +020017#include <trace/events/power.h>
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020018
19#include "power.h"
20
Rafael J. Wysockic125e962010-07-05 22:43:53 +020021/*
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 */
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +020025bool events_check_enabled __read_mostly;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020026
Rafael J. Wysocki068765b2014-09-01 13:47:49 +020027/* If set and the system is suspending, terminate the suspend. */
28static bool pm_abort_suspend __read_mostly;
29
Rafael J. Wysocki023d3772011-01-31 11:06:39 +010030/*
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 */
35static 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
40static 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. */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020049static unsigned int saved_count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020050
51static DEFINE_SPINLOCK(events_lock);
52
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +020053static void pm_wakeup_timer_fn(unsigned long data);
54
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020055static LIST_HEAD(wakeup_sources);
56
Rafael J. Wysocki60af1062012-04-29 22:52:34 +020057static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
58
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020059/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010060 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
61 * @ws: Wakeup source to prepare.
62 * @name: Pointer to the name of the new wakeup source.
63 *
64 * Callers must ensure that the @name string won't be freed when @ws is still in
65 * use.
66 */
67void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
68{
69 if (ws) {
70 memset(ws, 0, sizeof(*ws));
71 ws->name = name;
72 }
73}
74EXPORT_SYMBOL_GPL(wakeup_source_prepare);
75
76/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020077 * wakeup_source_create - Create a struct wakeup_source object.
78 * @name: Name of the new wakeup source.
79 */
80struct wakeup_source *wakeup_source_create(const char *name)
81{
82 struct wakeup_source *ws;
83
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010084 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020085 if (!ws)
86 return NULL;
87
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010088 wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020089 return ws;
90}
91EXPORT_SYMBOL_GPL(wakeup_source_create);
92
93/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010094 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
95 * @ws: Wakeup source to prepare for destruction.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +010096 *
97 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
98 * be run in parallel with this function for the same wakeup source object.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020099 */
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100100void wakeup_source_drop(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200101{
102 if (!ws)
103 return;
104
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100105 del_timer_sync(&ws->timer);
106 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100107}
108EXPORT_SYMBOL_GPL(wakeup_source_drop);
109
110/**
111 * wakeup_source_destroy - Destroy a struct wakeup_source object.
112 * @ws: Wakeup source to destroy.
113 *
114 * Use only for wakeup source objects created with wakeup_source_create().
115 */
116void wakeup_source_destroy(struct wakeup_source *ws)
117{
118 if (!ws)
119 return;
120
121 wakeup_source_drop(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200122 kfree(ws->name);
123 kfree(ws);
124}
125EXPORT_SYMBOL_GPL(wakeup_source_destroy);
126
127/**
128 * wakeup_source_add - Add given object to the list of wakeup sources.
129 * @ws: Wakeup source object to add to the list.
130 */
131void wakeup_source_add(struct wakeup_source *ws)
132{
John Stultz49550702012-09-06 23:19:06 +0200133 unsigned long flags;
134
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200135 if (WARN_ON(!ws))
136 return;
137
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100138 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200139 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
140 ws->active = false;
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200141 ws->last_time = ktime_get();
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200142
John Stultz49550702012-09-06 23:19:06 +0200143 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200144 list_add_rcu(&ws->entry, &wakeup_sources);
John Stultz49550702012-09-06 23:19:06 +0200145 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200146}
147EXPORT_SYMBOL_GPL(wakeup_source_add);
148
149/**
150 * wakeup_source_remove - Remove given object from the wakeup sources list.
151 * @ws: Wakeup source object to remove from the list.
152 */
153void wakeup_source_remove(struct wakeup_source *ws)
154{
John Stultz49550702012-09-06 23:19:06 +0200155 unsigned long flags;
156
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200157 if (WARN_ON(!ws))
158 return;
159
John Stultz49550702012-09-06 23:19:06 +0200160 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200161 list_del_rcu(&ws->entry);
John Stultz49550702012-09-06 23:19:06 +0200162 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200163 synchronize_rcu();
164}
165EXPORT_SYMBOL_GPL(wakeup_source_remove);
166
167/**
168 * wakeup_source_register - Create wakeup source and add it to the list.
169 * @name: Name of the wakeup source to register.
170 */
171struct wakeup_source *wakeup_source_register(const char *name)
172{
173 struct wakeup_source *ws;
174
175 ws = wakeup_source_create(name);
176 if (ws)
177 wakeup_source_add(ws);
178
179 return ws;
180}
181EXPORT_SYMBOL_GPL(wakeup_source_register);
182
183/**
184 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
185 * @ws: Wakeup source object to unregister.
186 */
187void wakeup_source_unregister(struct wakeup_source *ws)
188{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100189 if (ws) {
190 wakeup_source_remove(ws);
191 wakeup_source_destroy(ws);
192 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200193}
194EXPORT_SYMBOL_GPL(wakeup_source_unregister);
195
196/**
197 * device_wakeup_attach - Attach a wakeup source object to a device object.
198 * @dev: Device to handle.
199 * @ws: Wakeup source object to attach to @dev.
200 *
201 * This causes @dev to be treated as a wakeup device.
202 */
203static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
204{
205 spin_lock_irq(&dev->power.lock);
206 if (dev->power.wakeup) {
207 spin_unlock_irq(&dev->power.lock);
208 return -EEXIST;
209 }
210 dev->power.wakeup = ws;
211 spin_unlock_irq(&dev->power.lock);
212 return 0;
213}
214
215/**
216 * device_wakeup_enable - Enable given device to be a wakeup source.
217 * @dev: Device to handle.
218 *
219 * Create a wakeup source object, register it and attach it to @dev.
220 */
221int device_wakeup_enable(struct device *dev)
222{
223 struct wakeup_source *ws;
224 int ret;
225
226 if (!dev || !dev->power.can_wakeup)
227 return -EINVAL;
228
229 ws = wakeup_source_register(dev_name(dev));
230 if (!ws)
231 return -ENOMEM;
232
233 ret = device_wakeup_attach(dev, ws);
234 if (ret)
235 wakeup_source_unregister(ws);
236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(device_wakeup_enable);
240
241/**
242 * device_wakeup_detach - Detach a device's wakeup source object from it.
243 * @dev: Device to detach the wakeup source object from.
244 *
245 * After it returns, @dev will not be treated as a wakeup device any more.
246 */
247static struct wakeup_source *device_wakeup_detach(struct device *dev)
248{
249 struct wakeup_source *ws;
250
251 spin_lock_irq(&dev->power.lock);
252 ws = dev->power.wakeup;
253 dev->power.wakeup = NULL;
254 spin_unlock_irq(&dev->power.lock);
255 return ws;
256}
257
258/**
259 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
260 * @dev: Device to handle.
261 *
262 * Detach the @dev's wakeup source object from it, unregister this wakeup source
263 * object and destroy it.
264 */
265int device_wakeup_disable(struct device *dev)
266{
267 struct wakeup_source *ws;
268
269 if (!dev || !dev->power.can_wakeup)
270 return -EINVAL;
271
272 ws = device_wakeup_detach(dev);
273 if (ws)
274 wakeup_source_unregister(ws);
275
276 return 0;
277}
278EXPORT_SYMBOL_GPL(device_wakeup_disable);
279
280/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100281 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
282 * @dev: Device to handle.
283 * @capable: Whether or not @dev is capable of waking up the system from sleep.
284 *
285 * If @capable is set, set the @dev's power.can_wakeup flag and add its
286 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
287 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
288 *
289 * This function may sleep and it can't be called from any context where
290 * sleeping is not allowed.
291 */
292void device_set_wakeup_capable(struct device *dev, bool capable)
293{
294 if (!!dev->power.can_wakeup == !!capable)
295 return;
296
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200297 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100298 if (capable) {
299 if (wakeup_sysfs_add(dev))
300 return;
301 } else {
302 wakeup_sysfs_remove(dev);
303 }
304 }
305 dev->power.can_wakeup = capable;
306}
307EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
308
309/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200310 * device_init_wakeup - Device wakeup initialization.
311 * @dev: Device to handle.
312 * @enable: Whether or not to enable @dev as a wakeup device.
313 *
314 * By default, most devices should leave wakeup disabled. The exceptions are
315 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200316 * possibly network interfaces, etc. Also, devices that don't generate their
317 * own wakeup requests but merely forward requests from one bus to another
318 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200319 */
320int device_init_wakeup(struct device *dev, bool enable)
321{
322 int ret = 0;
323
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800324 if (!dev)
325 return -EINVAL;
326
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200327 if (enable) {
328 device_set_wakeup_capable(dev, true);
329 ret = device_wakeup_enable(dev);
330 } else {
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800331 if (dev->power.can_wakeup)
332 device_wakeup_disable(dev);
333
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200334 device_set_wakeup_capable(dev, false);
335 }
336
337 return ret;
338}
339EXPORT_SYMBOL_GPL(device_init_wakeup);
340
341/**
342 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
343 * @dev: Device to handle.
344 */
345int device_set_wakeup_enable(struct device *dev, bool enable)
346{
347 if (!dev || !dev->power.can_wakeup)
348 return -EINVAL;
349
350 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
351}
352EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200353
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200354/*
355 * The functions below use the observation that each wakeup event starts a
356 * period in which the system should not be suspended. The moment this period
357 * will end depends on how the wakeup event is going to be processed after being
358 * detected and all of the possible cases can be divided into two distinct
359 * groups.
360 *
361 * First, a wakeup event may be detected by the same functional unit that will
362 * carry out the entire processing of it and possibly will pass it to user space
363 * for further processing. In that case the functional unit that has detected
364 * the event may later "close" the "no suspend" period associated with it
365 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
366 * pm_relax(), balanced with each other, is supposed to be used in such
367 * situations.
368 *
369 * Second, a wakeup event may be detected by one functional unit and processed
370 * by another one. In that case the unit that has detected it cannot really
371 * "close" the "no suspend" period associated with it, unless it knows in
372 * advance what's going to happen to the event during processing. This
373 * knowledge, however, may not be available to it, so it can simply specify time
374 * to wait before the system can be suspended and pass it as the second
375 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200376 *
377 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
378 * "no suspend" period will be ended either by the pm_relax(), or by the timer
379 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200380 */
381
382/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200383 * wakup_source_activate - Mark given wakeup source as active.
384 * @ws: Wakeup source to handle.
385 *
386 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
387 * core of the event by incrementing the counter of of wakeup events being
388 * processed.
389 */
390static void wakeup_source_activate(struct wakeup_source *ws)
391{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200392 unsigned int cec;
393
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100394 /*
395 * active wakeup source should bring the system
396 * out of PM_SUSPEND_FREEZE state
397 */
398 freeze_wake();
399
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200400 ws->active = true;
401 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200402 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200403 if (ws->autosleep_enabled)
404 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200405
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100406 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200407 cec = atomic_inc_return(&combined_event_count);
408
409 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200410}
411
412/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200413 * wakeup_source_report_event - Report wakeup event using the given source.
414 * @ws: Wakeup source to report the event for.
415 */
416static void wakeup_source_report_event(struct wakeup_source *ws)
417{
418 ws->event_count++;
419 /* This is racy, but the counter is approximate anyway. */
420 if (events_check_enabled)
421 ws->wakeup_count++;
422
423 if (!ws->active)
424 wakeup_source_activate(ws);
425}
426
427/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200428 * __pm_stay_awake - Notify the PM core of a wakeup event.
429 * @ws: Wakeup source object associated with the source of the event.
430 *
431 * It is safe to call this function from interrupt context.
432 */
433void __pm_stay_awake(struct wakeup_source *ws)
434{
435 unsigned long flags;
436
437 if (!ws)
438 return;
439
440 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100441
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200442 wakeup_source_report_event(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100443 del_timer(&ws->timer);
444 ws->timer_expires = 0;
445
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200446 spin_unlock_irqrestore(&ws->lock, flags);
447}
448EXPORT_SYMBOL_GPL(__pm_stay_awake);
449
450/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200451 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
452 * @dev: Device the wakeup event is related to.
453 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200454 * Notify the PM core of a wakeup event (signaled by @dev) by calling
455 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200456 *
457 * Call this function after detecting of a wakeup event if pm_relax() is going
458 * to be called directly after processing the event (and possibly passing it to
459 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200460 */
461void pm_stay_awake(struct device *dev)
462{
463 unsigned long flags;
464
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200465 if (!dev)
466 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200467
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200468 spin_lock_irqsave(&dev->power.lock, flags);
469 __pm_stay_awake(dev->power.wakeup);
470 spin_unlock_irqrestore(&dev->power.lock, flags);
471}
472EXPORT_SYMBOL_GPL(pm_stay_awake);
473
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200474#ifdef CONFIG_PM_AUTOSLEEP
475static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
476{
477 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
478 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
479}
480#else
481static inline void update_prevent_sleep_time(struct wakeup_source *ws,
482 ktime_t now) {}
483#endif
484
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200485/**
486 * wakup_source_deactivate - Mark given wakeup source as inactive.
487 * @ws: Wakeup source to handle.
488 *
489 * Update the @ws' statistics and notify the PM core that the wakeup source has
490 * become inactive by decrementing the counter of wakeup events being processed
491 * and incrementing the counter of registered wakeup events.
492 */
493static void wakeup_source_deactivate(struct wakeup_source *ws)
494{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200495 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200496 ktime_t duration;
497 ktime_t now;
498
499 ws->relax_count++;
500 /*
501 * __pm_relax() may be called directly or from a timer function.
502 * If it is called directly right after the timer function has been
503 * started, but before the timer function calls __pm_relax(), it is
504 * possible that __pm_stay_awake() will be called in the meantime and
505 * will set ws->active. Then, ws->active may be cleared immediately
506 * by the __pm_relax() called from the timer function, but in such a
507 * case ws->relax_count will be different from ws->active_count.
508 */
509 if (ws->relax_count != ws->active_count) {
510 ws->relax_count--;
511 return;
512 }
513
514 ws->active = false;
515
516 now = ktime_get();
517 duration = ktime_sub(now, ws->last_time);
518 ws->total_time = ktime_add(ws->total_time, duration);
519 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
520 ws->max_time = duration;
521
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200522 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200523 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100524 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200525
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200526 if (ws->autosleep_enabled)
527 update_prevent_sleep_time(ws, now);
528
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200529 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100530 * Increment the counter of registered wakeup events and decrement the
531 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200532 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200533 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
534 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200535
536 split_counters(&cnt, &inpr);
537 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
538 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200539}
540
541/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200542 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
543 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200544 *
545 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200546 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200547 *
548 * It is safe to call it from interrupt context.
549 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200550void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200551{
552 unsigned long flags;
553
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200554 if (!ws)
555 return;
556
557 spin_lock_irqsave(&ws->lock, flags);
558 if (ws->active)
559 wakeup_source_deactivate(ws);
560 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200561}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200562EXPORT_SYMBOL_GPL(__pm_relax);
563
564/**
565 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
566 * @dev: Device that signaled the event.
567 *
568 * Execute __pm_relax() for the @dev's wakeup source object.
569 */
570void pm_relax(struct device *dev)
571{
572 unsigned long flags;
573
574 if (!dev)
575 return;
576
577 spin_lock_irqsave(&dev->power.lock, flags);
578 __pm_relax(dev->power.wakeup);
579 spin_unlock_irqrestore(&dev->power.lock, flags);
580}
581EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200582
583/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200584 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200585 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100587 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
588 * in @data if it is currently active and its timer has not been canceled and
589 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200590 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200591static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200592{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100593 struct wakeup_source *ws = (struct wakeup_source *)data;
594 unsigned long flags;
595
596 spin_lock_irqsave(&ws->lock, flags);
597
598 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200599 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100600 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200601 ws->expire_count++;
602 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100603
604 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200605}
606
607/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200608 * __pm_wakeup_event - Notify the PM core of a wakeup event.
609 * @ws: Wakeup source object associated with the event source.
610 * @msec: Anticipated event processing time (in milliseconds).
611 *
612 * Notify the PM core of a wakeup event whose source is @ws that will take
613 * approximately @msec milliseconds to be processed by the kernel. If @ws is
614 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
615 * execute pm_wakeup_timer_fn() in future.
616 *
617 * It is safe to call this function from interrupt context.
618 */
619void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
620{
621 unsigned long flags;
622 unsigned long expires;
623
624 if (!ws)
625 return;
626
627 spin_lock_irqsave(&ws->lock, flags);
628
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200629 wakeup_source_report_event(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200630
631 if (!msec) {
632 wakeup_source_deactivate(ws);
633 goto unlock;
634 }
635
636 expires = jiffies + msecs_to_jiffies(msec);
637 if (!expires)
638 expires = 1;
639
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100640 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200641 mod_timer(&ws->timer, expires);
642 ws->timer_expires = expires;
643 }
644
645 unlock:
646 spin_unlock_irqrestore(&ws->lock, flags);
647}
648EXPORT_SYMBOL_GPL(__pm_wakeup_event);
649
650
651/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200652 * pm_wakeup_event - Notify the PM core of a wakeup event.
653 * @dev: Device the wakeup event is related to.
654 * @msec: Anticipated event processing time (in milliseconds).
655 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200656 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200657 */
658void pm_wakeup_event(struct device *dev, unsigned int msec)
659{
660 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200661
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200662 if (!dev)
663 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200664
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200665 spin_lock_irqsave(&dev->power.lock, flags);
666 __pm_wakeup_event(dev->power.wakeup, msec);
667 spin_unlock_irqrestore(&dev->power.lock, flags);
668}
669EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200670
Julius Wernerbb177fe2013-06-12 12:55:22 -0700671void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200672{
673 struct wakeup_source *ws;
674 int active = 0;
675 struct wakeup_source *last_activity_ws = NULL;
676
677 rcu_read_lock();
678 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
679 if (ws->active) {
680 pr_info("active wakeup source: %s\n", ws->name);
681 active = 1;
682 } else if (!active &&
683 (!last_activity_ws ||
684 ktime_to_ns(ws->last_time) >
685 ktime_to_ns(last_activity_ws->last_time))) {
686 last_activity_ws = ws;
687 }
688 }
689
690 if (!active && last_activity_ws)
691 pr_info("last active wakeup source: %s\n",
692 last_activity_ws->name);
693 rcu_read_unlock();
694}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700695EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200696
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200697/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100698 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200699 *
700 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100701 * value from the past and return true if new wakeup events have been registered
702 * since the old value was stored. Also return true if the current number of
703 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200704 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100705bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200706{
707 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100708 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200709
710 spin_lock_irqsave(&events_lock, flags);
711 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100712 unsigned int cnt, inpr;
713
714 split_counters(&cnt, &inpr);
715 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100716 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200717 }
718 spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200719
Bernie Thompson9350de062013-06-01 00:47:43 +0000720 if (ret) {
721 pr_info("PM: Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700722 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000723 }
Todd Poynora938da02012-08-12 00:17:02 +0200724
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200725 return ret || pm_abort_suspend;
726}
727
728void pm_system_wakeup(void)
729{
730 pm_abort_suspend = true;
731 freeze_wake();
732}
733
734void pm_wakeup_clear(void)
735{
736 pm_abort_suspend = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200737}
738
739/**
740 * pm_get_wakeup_count - Read the number of registered wakeup events.
741 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200742 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200743 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200744 * Store the number of registered wakeup events at the address in @count. If
745 * @block is set, block until the current number of wakeup events being
746 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200747 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200748 * Return 'false' if the current number of wakeup events being processed is
749 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200750 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200751bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200752{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100753 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200754
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200755 if (block) {
756 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200757
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200758 for (;;) {
759 prepare_to_wait(&wakeup_count_wait_queue, &wait,
760 TASK_INTERRUPTIBLE);
761 split_counters(&cnt, &inpr);
762 if (inpr == 0 || signal_pending(current))
763 break;
764
765 schedule();
766 }
767 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200768 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200769
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100770 split_counters(&cnt, &inpr);
771 *count = cnt;
772 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200773}
774
775/**
776 * pm_save_wakeup_count - Save the current number of registered wakeup events.
777 * @count: Value to compare with the current number of registered wakeup events.
778 *
779 * If @count is equal to the current number of registered wakeup events and the
780 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100781 * old number of registered wakeup events for pm_check_wakeup_events(), enable
782 * wakeup events detection and return 'true'. Otherwise disable wakeup events
783 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200784 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200785bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200786{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100787 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200788 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200789
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100790 events_check_enabled = false;
John Stultz49550702012-09-06 23:19:06 +0200791 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100792 split_counters(&cnt, &inpr);
793 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200794 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200795 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200796 }
John Stultz49550702012-09-06 23:19:06 +0200797 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100798 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200799}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200800
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200801#ifdef CONFIG_PM_AUTOSLEEP
802/**
803 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
804 * @enabled: Whether to set or to clear the autosleep_enabled flags.
805 */
806void pm_wakep_autosleep_enabled(bool set)
807{
808 struct wakeup_source *ws;
809 ktime_t now = ktime_get();
810
811 rcu_read_lock();
812 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
813 spin_lock_irq(&ws->lock);
814 if (ws->autosleep_enabled != set) {
815 ws->autosleep_enabled = set;
816 if (ws->active) {
817 if (set)
818 ws->start_prevent_time = now;
819 else
820 update_prevent_sleep_time(ws, now);
821 }
822 }
823 spin_unlock_irq(&ws->lock);
824 }
825 rcu_read_unlock();
826}
827#endif /* CONFIG_PM_AUTOSLEEP */
828
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200829static struct dentry *wakeup_sources_stats_dentry;
830
831/**
832 * print_wakeup_source_stats - Print wakeup source statistics information.
833 * @m: seq_file to print the statistics into.
834 * @ws: Wakeup source object to print the statistics for.
835 */
836static int print_wakeup_source_stats(struct seq_file *m,
837 struct wakeup_source *ws)
838{
839 unsigned long flags;
840 ktime_t total_time;
841 ktime_t max_time;
842 unsigned long active_count;
843 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200844 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200845 int ret;
846
847 spin_lock_irqsave(&ws->lock, flags);
848
849 total_time = ws->total_time;
850 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200851 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200852 active_count = ws->active_count;
853 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200854 ktime_t now = ktime_get();
855
856 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200857 total_time = ktime_add(total_time, active_time);
858 if (active_time.tv64 > max_time.tv64)
859 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200860
861 if (ws->autosleep_enabled)
862 prevent_sleep_time = ktime_add(prevent_sleep_time,
863 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200864 } else {
865 active_time = ktime_set(0, 0);
866 }
867
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200868 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t"
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200869 "%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200870 ws->name, active_count, ws->event_count,
871 ws->wakeup_count, ws->expire_count,
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200872 ktime_to_ms(active_time), ktime_to_ms(total_time),
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200873 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
874 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200875
876 spin_unlock_irqrestore(&ws->lock, flags);
877
878 return ret;
879}
880
881/**
882 * wakeup_sources_stats_show - Print wakeup sources statistics information.
883 * @m: seq_file to print the statistics into.
884 */
885static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
886{
887 struct wakeup_source *ws;
888
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200889 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
890 "expire_count\tactive_since\ttotal_time\tmax_time\t"
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200891 "last_change\tprevent_suspend_time\n");
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200892
893 rcu_read_lock();
894 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
895 print_wakeup_source_stats(m, ws);
896 rcu_read_unlock();
897
898 return 0;
899}
900
901static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
902{
903 return single_open(file, wakeup_sources_stats_show, NULL);
904}
905
906static const struct file_operations wakeup_sources_stats_fops = {
907 .owner = THIS_MODULE,
908 .open = wakeup_sources_stats_open,
909 .read = seq_read,
910 .llseek = seq_lseek,
911 .release = single_release,
912};
913
914static int __init wakeup_sources_debugfs_init(void)
915{
916 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
917 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
918 return 0;
919}
920
921postcore_initcall(wakeup_sources_debugfs_init);