blob: 87c2603b9327b21bc40392aab350e131ef6f7467 [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
Jin Qian7f436052015-05-15 18:10:37 -070059static struct wakeup_source deleted_ws = {
60 .name = "deleted",
61 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
62};
63
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020064/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010065 * 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 */
72void 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}
79EXPORT_SYMBOL_GPL(wakeup_source_prepare);
80
81/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020082 * wakeup_source_create - Create a struct wakeup_source object.
83 * @name: Name of the new wakeup source.
84 */
85struct wakeup_source *wakeup_source_create(const char *name)
86{
87 struct wakeup_source *ws;
88
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010089 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020090 if (!ws)
91 return NULL;
92
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010093 wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020094 return ws;
95}
96EXPORT_SYMBOL_GPL(wakeup_source_create);
97
98/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010099 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
100 * @ws: Wakeup source to prepare for destruction.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100101 *
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.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200104 */
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100105void wakeup_source_drop(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200106{
107 if (!ws)
108 return;
109
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100110 del_timer_sync(&ws->timer);
111 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100112}
113EXPORT_SYMBOL_GPL(wakeup_source_drop);
114
Jin Qian7f436052015-05-15 18:10:37 -0700115/*
116 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
117 */
118static 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
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100143/**
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 */
149void wakeup_source_destroy(struct wakeup_source *ws)
150{
151 if (!ws)
152 return;
153
154 wakeup_source_drop(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700155 wakeup_source_record(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200156 kfree(ws->name);
157 kfree(ws);
158}
159EXPORT_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 */
165void wakeup_source_add(struct wakeup_source *ws)
166{
John Stultz49550702012-09-06 23:19:06 +0200167 unsigned long flags;
168
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200169 if (WARN_ON(!ws))
170 return;
171
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100172 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200173 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
174 ws->active = false;
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200175 ws->last_time = ktime_get();
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200176
John Stultz49550702012-09-06 23:19:06 +0200177 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200178 list_add_rcu(&ws->entry, &wakeup_sources);
John Stultz49550702012-09-06 23:19:06 +0200179 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200180}
181EXPORT_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 */
187void wakeup_source_remove(struct wakeup_source *ws)
188{
John Stultz49550702012-09-06 23:19:06 +0200189 unsigned long flags;
190
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200191 if (WARN_ON(!ws))
192 return;
193
John Stultz49550702012-09-06 23:19:06 +0200194 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200195 list_del_rcu(&ws->entry);
John Stultz49550702012-09-06 23:19:06 +0200196 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200197 synchronize_rcu();
198}
199EXPORT_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 */
205struct 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}
215EXPORT_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 */
221void wakeup_source_unregister(struct wakeup_source *ws)
222{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100223 if (ws) {
224 wakeup_source_remove(ws);
225 wakeup_source_destroy(ws);
226 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200227}
228EXPORT_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 */
237static 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 */
255int 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}
273EXPORT_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 */
281static 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 */
299int 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}
312EXPORT_SYMBOL_GPL(device_wakeup_disable);
313
314/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100315 * 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 */
326void device_set_wakeup_capable(struct device *dev, bool capable)
327{
328 if (!!dev->power.can_wakeup == !!capable)
329 return;
330
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200331 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100332 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}
341EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
342
343/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200344 * 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,
Alan Stern8f888932011-09-26 17:38:50 +0200350 * 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.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200353 */
354int device_init_wakeup(struct device *dev, bool enable)
355{
356 int ret = 0;
357
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800358 if (!dev)
359 return -EINVAL;
360
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200361 if (enable) {
362 device_set_wakeup_capable(dev, true);
363 ret = device_wakeup_enable(dev);
364 } else {
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800365 if (dev->power.can_wakeup)
366 device_wakeup_disable(dev);
367
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200368 device_set_wakeup_capable(dev, false);
369 }
370
371 return ret;
372}
373EXPORT_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 */
379int 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}
386EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200387
Jin Qianb6ec9452015-05-06 15:26:56 -0700388/**
389 * wakeup_source_not_registered - validate the given wakeup source.
390 * @ws: Wakeup source to be validated.
391 */
392static 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
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200402/*
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().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200424 *
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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200428 */
429
430/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200431 * 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 */
438static void wakeup_source_activate(struct wakeup_source *ws)
439{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200440 unsigned int cec;
441
Jin Qianb6ec9452015-05-06 15:26:56 -0700442 if (WARN_ONCE(wakeup_source_not_registered(ws),
443 "unregistered wakeup source\n"))
444 return;
445
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100446 /*
447 * active wakeup source should bring the system
448 * out of PM_SUSPEND_FREEZE state
449 */
450 freeze_wake();
451
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200452 ws->active = true;
453 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200454 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200455 if (ws->autosleep_enabled)
456 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200457
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100458 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200459 cec = atomic_inc_return(&combined_event_count);
460
461 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200462}
463
464/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200465 * wakeup_source_report_event - Report wakeup event using the given source.
466 * @ws: Wakeup source to report the event for.
467 */
468static 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/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200480 * __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 */
485void __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);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100493
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200494 wakeup_source_report_event(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100495 del_timer(&ws->timer);
496 ws->timer_expires = 0;
497
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200498 spin_unlock_irqrestore(&ws->lock, flags);
499}
500EXPORT_SYMBOL_GPL(__pm_stay_awake);
501
502/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200503 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
504 * @dev: Device the wakeup event is related to.
505 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200506 * Notify the PM core of a wakeup event (signaled by @dev) by calling
507 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200508 *
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).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200512 */
513void pm_stay_awake(struct device *dev)
514{
515 unsigned long flags;
516
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200517 if (!dev)
518 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200519
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200520 spin_lock_irqsave(&dev->power.lock, flags);
521 __pm_stay_awake(dev->power.wakeup);
522 spin_unlock_irqrestore(&dev->power.lock, flags);
523}
524EXPORT_SYMBOL_GPL(pm_stay_awake);
525
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200526#ifdef CONFIG_PM_AUTOSLEEP
527static 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
533static inline void update_prevent_sleep_time(struct wakeup_source *ws,
534 ktime_t now) {}
535#endif
536
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200537/**
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 */
545static void wakeup_source_deactivate(struct wakeup_source *ws)
546{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200547 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200548 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
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200574 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200575 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100576 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200577
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200578 if (ws->autosleep_enabled)
579 update_prevent_sleep_time(ws, now);
580
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200581 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100582 * Increment the counter of registered wakeup events and decrement the
583 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200584 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200585 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
586 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200587
588 split_counters(&cnt, &inpr);
589 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
590 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591}
592
593/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200594 * __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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200596 *
597 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200598 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200599 *
600 * It is safe to call it from interrupt context.
601 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200602void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200603{
604 unsigned long flags;
605
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200606 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);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200613}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200614EXPORT_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 */
622void 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}
633EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200634
635/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200636 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200637 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200638 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100639 * 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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200642 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200643static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200644{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100645 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
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200651 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100652 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200653 ws->expire_count++;
654 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100655
656 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200657}
658
659/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200660 * __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 */
671void __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
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200681 wakeup_source_report_event(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200682
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
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100692 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200693 mod_timer(&ws->timer, expires);
694 ws->timer_expires = expires;
695 }
696
697 unlock:
698 spin_unlock_irqrestore(&ws->lock, flags);
699}
700EXPORT_SYMBOL_GPL(__pm_wakeup_event);
701
702
703/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200704 * 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 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200708 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200709 */
710void pm_wakeup_event(struct device *dev, unsigned int msec)
711{
712 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200713
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200714 if (!dev)
715 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200716
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200717 spin_lock_irqsave(&dev->power.lock, flags);
718 __pm_wakeup_event(dev->power.wakeup, msec);
719 spin_unlock_irqrestore(&dev->power.lock, flags);
720}
721EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200722
Julius Wernerbb177fe2013-06-12 12:55:22 -0700723void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200724{
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}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700747EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200748
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200749/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100750 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200751 *
752 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100753 * 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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200756 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100757bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200758{
759 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100760 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200761
762 spin_lock_irqsave(&events_lock, flags);
763 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100764 unsigned int cnt, inpr;
765
766 split_counters(&cnt, &inpr);
767 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100768 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200769 }
770 spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200771
Bernie Thompson9350de062013-06-01 00:47:43 +0000772 if (ret) {
773 pr_info("PM: Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700774 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000775 }
Todd Poynora938da02012-08-12 00:17:02 +0200776
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200777 return ret || pm_abort_suspend;
778}
779
780void pm_system_wakeup(void)
781{
782 pm_abort_suspend = true;
783 freeze_wake();
784}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100785EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200786
787void pm_wakeup_clear(void)
788{
789 pm_abort_suspend = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200790}
791
792/**
793 * pm_get_wakeup_count - Read the number of registered wakeup events.
794 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200795 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200796 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200797 * 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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200800 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200801 * Return 'false' if the current number of wakeup events being processed is
802 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200803 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200804bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200805{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100806 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200807
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200808 if (block) {
809 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200810
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200811 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);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200821 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200822
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100823 split_counters(&cnt, &inpr);
824 *count = cnt;
825 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200826}
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
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100834 * 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'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200837 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200838bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200839{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100840 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200841 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200842
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100843 events_check_enabled = false;
John Stultz49550702012-09-06 23:19:06 +0200844 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100845 split_counters(&cnt, &inpr);
846 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200847 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200848 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200849 }
John Stultz49550702012-09-06 23:19:06 +0200850 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100851 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200852}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200853
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200854#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 */
859void 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
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200882static 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 */
889static 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;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200897 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200898
899 spin_lock_irqsave(&ws->lock, flags);
900
901 total_time = ws->total_time;
902 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200903 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200904 active_count = ws->active_count;
905 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200906 ktime_t now = ktime_get();
907
908 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200909 total_time = ktime_add(total_time, active_time);
910 if (active_time.tv64 > max_time.tv64)
911 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200912
913 if (ws->autosleep_enabled)
914 prevent_sleep_time = ktime_add(prevent_sleep_time,
915 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200916 } else {
917 active_time = ktime_set(0, 0);
918 }
919
Joe Perches9f6a2402015-04-15 16:17:48 -0700920 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));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200926
927 spin_unlock_irqrestore(&ws->lock, flags);
928
Joe Perches9f6a2402015-04-15 16:17:48 -0700929 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200930}
931
932/**
933 * wakeup_sources_stats_show - Print wakeup sources statistics information.
934 * @m: seq_file to print the statistics into.
935 */
936static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
937{
938 struct wakeup_source *ws;
939
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200940 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
941 "expire_count\tactive_since\ttotal_time\tmax_time\t"
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200942 "last_change\tprevent_suspend_time\n");
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200943
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
Jin Qian7f436052015-05-15 18:10:37 -0700949 print_wakeup_source_stats(m, &deleted_ws);
950
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200951 return 0;
952}
953
954static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
955{
956 return single_open(file, wakeup_sources_stats_show, NULL);
957}
958
959static 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
967static 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
974postcore_initcall(wakeup_sources_debugfs_init);