blob: 270cdd40edb4201bd847b9945138ac753e792a42 [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>
Tony Lindgren4990d4f2015-05-18 15:40:29 -070017#include <linux/pm_wakeirq.h>
Ruchi Kandoi68b62542015-04-08 15:42:29 -070018#include <linux/types.h>
Arve Hjønnevåg6791e362012-04-29 22:53:02 +020019#include <trace/events/power.h>
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020020
21#include "power.h"
22
Rafael J. Wysockic125e962010-07-05 22:43:53 +020023/*
24 * If set, the suspend/hibernate code will abort transitions to a sleep state
25 * if wakeup events are registered during or immediately before the transition.
26 */
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +020027bool events_check_enabled __read_mostly;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020028
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -070029/* First wakeup IRQ seen by the kernel in the last cycle. */
30unsigned int pm_wakeup_irq __read_mostly;
31
Rafael J. Wysocki068765b2014-09-01 13:47:49 +020032/* If set and the system is suspending, terminate the suspend. */
33static bool pm_abort_suspend __read_mostly;
34
Rafael J. Wysocki023d3772011-01-31 11:06:39 +010035/*
36 * Combined counters of registered wakeup events and wakeup events in progress.
37 * They need to be modified together atomically, so it's better to use one
38 * atomic variable to hold them both.
39 */
40static atomic_t combined_event_count = ATOMIC_INIT(0);
41
42#define IN_PROGRESS_BITS (sizeof(int) * 4)
43#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
44
45static void split_counters(unsigned int *cnt, unsigned int *inpr)
46{
47 unsigned int comb = atomic_read(&combined_event_count);
48
49 *cnt = (comb >> IN_PROGRESS_BITS);
50 *inpr = comb & MAX_IN_PROGRESS;
51}
52
53/* A preserved old value of the events counter. */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020054static unsigned int saved_count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020055
56static DEFINE_SPINLOCK(events_lock);
57
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +020058static void pm_wakeup_timer_fn(unsigned long data);
59
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020060static LIST_HEAD(wakeup_sources);
61
Rafael J. Wysocki60af1062012-04-29 22:52:34 +020062static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
63
Jin Qian7f436052015-05-15 18:10:37 -070064static struct wakeup_source deleted_ws = {
65 .name = "deleted",
66 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
67};
68
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020069/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010070 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
71 * @ws: Wakeup source to prepare.
72 * @name: Pointer to the name of the new wakeup source.
73 *
74 * Callers must ensure that the @name string won't be freed when @ws is still in
75 * use.
76 */
77void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
78{
79 if (ws) {
80 memset(ws, 0, sizeof(*ws));
81 ws->name = name;
82 }
83}
84EXPORT_SYMBOL_GPL(wakeup_source_prepare);
85
86/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020087 * wakeup_source_create - Create a struct wakeup_source object.
88 * @name: Name of the new wakeup source.
89 */
90struct wakeup_source *wakeup_source_create(const char *name)
91{
92 struct wakeup_source *ws;
93
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010094 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020095 if (!ws)
96 return NULL;
97
Rasmus Villemoes72362142015-09-09 23:42:06 +020098 wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020099 return ws;
100}
101EXPORT_SYMBOL_GPL(wakeup_source_create);
102
103/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100104 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
105 * @ws: Wakeup source to prepare for destruction.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100106 *
107 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
108 * be run in parallel with this function for the same wakeup source object.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200109 */
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100110void wakeup_source_drop(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200111{
112 if (!ws)
113 return;
114
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100115 del_timer_sync(&ws->timer);
116 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100117}
118EXPORT_SYMBOL_GPL(wakeup_source_drop);
119
Jin Qian7f436052015-05-15 18:10:37 -0700120/*
121 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
122 */
123static void wakeup_source_record(struct wakeup_source *ws)
124{
125 unsigned long flags;
126
127 spin_lock_irqsave(&deleted_ws.lock, flags);
128
129 if (ws->event_count) {
130 deleted_ws.total_time =
131 ktime_add(deleted_ws.total_time, ws->total_time);
132 deleted_ws.prevent_sleep_time =
133 ktime_add(deleted_ws.prevent_sleep_time,
134 ws->prevent_sleep_time);
135 deleted_ws.max_time =
136 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
137 deleted_ws.max_time : ws->max_time;
138 deleted_ws.event_count += ws->event_count;
139 deleted_ws.active_count += ws->active_count;
140 deleted_ws.relax_count += ws->relax_count;
141 deleted_ws.expire_count += ws->expire_count;
142 deleted_ws.wakeup_count += ws->wakeup_count;
143 }
144
145 spin_unlock_irqrestore(&deleted_ws.lock, flags);
146}
147
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100148/**
149 * wakeup_source_destroy - Destroy a struct wakeup_source object.
150 * @ws: Wakeup source to destroy.
151 *
152 * Use only for wakeup source objects created with wakeup_source_create().
153 */
154void wakeup_source_destroy(struct wakeup_source *ws)
155{
156 if (!ws)
157 return;
158
159 wakeup_source_drop(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700160 wakeup_source_record(ws);
Rasmus Villemoes72362142015-09-09 23:42:06 +0200161 kfree_const(ws->name);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200162 kfree(ws);
163}
164EXPORT_SYMBOL_GPL(wakeup_source_destroy);
165
166/**
167 * wakeup_source_add - Add given object to the list of wakeup sources.
168 * @ws: Wakeup source object to add to the list.
169 */
170void wakeup_source_add(struct wakeup_source *ws)
171{
John Stultz49550702012-09-06 23:19:06 +0200172 unsigned long flags;
173
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200174 if (WARN_ON(!ws))
175 return;
176
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100177 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200178 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
179 ws->active = false;
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200180 ws->last_time = ktime_get();
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200181
John Stultz49550702012-09-06 23:19:06 +0200182 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200183 list_add_rcu(&ws->entry, &wakeup_sources);
John Stultz49550702012-09-06 23:19:06 +0200184 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200185}
186EXPORT_SYMBOL_GPL(wakeup_source_add);
187
188/**
189 * wakeup_source_remove - Remove given object from the wakeup sources list.
190 * @ws: Wakeup source object to remove from the list.
191 */
192void wakeup_source_remove(struct wakeup_source *ws)
193{
John Stultz49550702012-09-06 23:19:06 +0200194 unsigned long flags;
195
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200196 if (WARN_ON(!ws))
197 return;
198
John Stultz49550702012-09-06 23:19:06 +0200199 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200200 list_del_rcu(&ws->entry);
John Stultz49550702012-09-06 23:19:06 +0200201 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200202 synchronize_rcu();
203}
204EXPORT_SYMBOL_GPL(wakeup_source_remove);
205
206/**
207 * wakeup_source_register - Create wakeup source and add it to the list.
208 * @name: Name of the wakeup source to register.
209 */
210struct wakeup_source *wakeup_source_register(const char *name)
211{
212 struct wakeup_source *ws;
213
214 ws = wakeup_source_create(name);
215 if (ws)
216 wakeup_source_add(ws);
217
218 return ws;
219}
220EXPORT_SYMBOL_GPL(wakeup_source_register);
221
222/**
223 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
224 * @ws: Wakeup source object to unregister.
225 */
226void wakeup_source_unregister(struct wakeup_source *ws)
227{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100228 if (ws) {
229 wakeup_source_remove(ws);
230 wakeup_source_destroy(ws);
231 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200232}
233EXPORT_SYMBOL_GPL(wakeup_source_unregister);
234
235/**
236 * device_wakeup_attach - Attach a wakeup source object to a device object.
237 * @dev: Device to handle.
238 * @ws: Wakeup source object to attach to @dev.
239 *
240 * This causes @dev to be treated as a wakeup device.
241 */
242static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
243{
244 spin_lock_irq(&dev->power.lock);
245 if (dev->power.wakeup) {
246 spin_unlock_irq(&dev->power.lock);
247 return -EEXIST;
248 }
249 dev->power.wakeup = ws;
Strashko, Grygorii16669be2016-04-06 14:45:53 +0300250 if (dev->power.wakeirq)
251 device_wakeup_attach_irq(dev, dev->power.wakeirq);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200252 spin_unlock_irq(&dev->power.lock);
253 return 0;
254}
255
256/**
257 * device_wakeup_enable - Enable given device to be a wakeup source.
258 * @dev: Device to handle.
259 *
260 * Create a wakeup source object, register it and attach it to @dev.
261 */
262int device_wakeup_enable(struct device *dev)
263{
264 struct wakeup_source *ws;
265 int ret;
266
267 if (!dev || !dev->power.can_wakeup)
268 return -EINVAL;
269
270 ws = wakeup_source_register(dev_name(dev));
271 if (!ws)
272 return -ENOMEM;
273
274 ret = device_wakeup_attach(dev, ws);
275 if (ret)
276 wakeup_source_unregister(ws);
277
278 return ret;
279}
280EXPORT_SYMBOL_GPL(device_wakeup_enable);
281
282/**
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700283 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
284 * @dev: Device to handle
285 * @wakeirq: Device specific wakeirq entry
286 *
287 * Attach a device wakeirq to the wakeup source so the device
288 * wake IRQ can be configured automatically for suspend and
289 * resume.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200290 *
291 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700292 */
293int device_wakeup_attach_irq(struct device *dev,
294 struct wake_irq *wakeirq)
295{
296 struct wakeup_source *ws;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700297
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700298 ws = dev->power.wakeup;
299 if (!ws) {
300 dev_err(dev, "forgot to call call device_init_wakeup?\n");
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200301 return -EINVAL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700302 }
303
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200304 if (ws->wakeirq)
305 return -EEXIST;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700306
307 ws->wakeirq = wakeirq;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200308 return 0;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700309}
310
311/**
312 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
313 * @dev: Device to handle
314 *
315 * Removes a device wakeirq from the wakeup source.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200316 *
317 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700318 */
319void device_wakeup_detach_irq(struct device *dev)
320{
321 struct wakeup_source *ws;
322
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700323 ws = dev->power.wakeup;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200324 if (ws)
325 ws->wakeirq = NULL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700326}
327
328/**
329 * device_wakeup_arm_wake_irqs(void)
330 *
331 * Itereates over the list of device wakeirqs to arm them.
332 */
333void device_wakeup_arm_wake_irqs(void)
334{
335 struct wakeup_source *ws;
336
337 rcu_read_lock();
Markus Elfring4f48ec82016-07-23 17:04:00 +0200338 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
339 dev_pm_arm_wake_irq(ws->wakeirq);
340
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700341 rcu_read_unlock();
342}
343
344/**
345 * device_wakeup_disarm_wake_irqs(void)
346 *
347 * Itereates over the list of device wakeirqs to disarm them.
348 */
349void device_wakeup_disarm_wake_irqs(void)
350{
351 struct wakeup_source *ws;
352
353 rcu_read_lock();
Markus Elfring4f48ec82016-07-23 17:04:00 +0200354 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
355 dev_pm_disarm_wake_irq(ws->wakeirq);
356
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700357 rcu_read_unlock();
358}
359
360/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200361 * device_wakeup_detach - Detach a device's wakeup source object from it.
362 * @dev: Device to detach the wakeup source object from.
363 *
364 * After it returns, @dev will not be treated as a wakeup device any more.
365 */
366static struct wakeup_source *device_wakeup_detach(struct device *dev)
367{
368 struct wakeup_source *ws;
369
370 spin_lock_irq(&dev->power.lock);
371 ws = dev->power.wakeup;
372 dev->power.wakeup = NULL;
373 spin_unlock_irq(&dev->power.lock);
374 return ws;
375}
376
377/**
378 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
379 * @dev: Device to handle.
380 *
381 * Detach the @dev's wakeup source object from it, unregister this wakeup source
382 * object and destroy it.
383 */
384int device_wakeup_disable(struct device *dev)
385{
386 struct wakeup_source *ws;
387
388 if (!dev || !dev->power.can_wakeup)
389 return -EINVAL;
390
391 ws = device_wakeup_detach(dev);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200392 wakeup_source_unregister(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200393 return 0;
394}
395EXPORT_SYMBOL_GPL(device_wakeup_disable);
396
397/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100398 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
399 * @dev: Device to handle.
400 * @capable: Whether or not @dev is capable of waking up the system from sleep.
401 *
402 * If @capable is set, set the @dev's power.can_wakeup flag and add its
403 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
404 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
405 *
406 * This function may sleep and it can't be called from any context where
407 * sleeping is not allowed.
408 */
409void device_set_wakeup_capable(struct device *dev, bool capable)
410{
411 if (!!dev->power.can_wakeup == !!capable)
412 return;
413
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200414 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100415 if (capable) {
416 if (wakeup_sysfs_add(dev))
417 return;
418 } else {
419 wakeup_sysfs_remove(dev);
420 }
421 }
422 dev->power.can_wakeup = capable;
423}
424EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
425
426/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200427 * device_init_wakeup - Device wakeup initialization.
428 * @dev: Device to handle.
429 * @enable: Whether or not to enable @dev as a wakeup device.
430 *
431 * By default, most devices should leave wakeup disabled. The exceptions are
432 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200433 * possibly network interfaces, etc. Also, devices that don't generate their
434 * own wakeup requests but merely forward requests from one bus to another
435 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200436 */
437int device_init_wakeup(struct device *dev, bool enable)
438{
439 int ret = 0;
440
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800441 if (!dev)
442 return -EINVAL;
443
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200444 if (enable) {
445 device_set_wakeup_capable(dev, true);
446 ret = device_wakeup_enable(dev);
447 } else {
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800448 if (dev->power.can_wakeup)
449 device_wakeup_disable(dev);
450
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200451 device_set_wakeup_capable(dev, false);
452 }
453
454 return ret;
455}
456EXPORT_SYMBOL_GPL(device_init_wakeup);
457
458/**
459 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
460 * @dev: Device to handle.
461 */
462int device_set_wakeup_enable(struct device *dev, bool enable)
463{
464 if (!dev || !dev->power.can_wakeup)
465 return -EINVAL;
466
467 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
468}
469EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200470
Jin Qianb6ec9452015-05-06 15:26:56 -0700471/**
472 * wakeup_source_not_registered - validate the given wakeup source.
473 * @ws: Wakeup source to be validated.
474 */
475static bool wakeup_source_not_registered(struct wakeup_source *ws)
476{
477 /*
478 * Use timer struct to check if the given source is initialized
479 * by wakeup_source_add.
480 */
481 return ws->timer.function != pm_wakeup_timer_fn ||
482 ws->timer.data != (unsigned long)ws;
483}
484
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200485/*
486 * The functions below use the observation that each wakeup event starts a
487 * period in which the system should not be suspended. The moment this period
488 * will end depends on how the wakeup event is going to be processed after being
489 * detected and all of the possible cases can be divided into two distinct
490 * groups.
491 *
492 * First, a wakeup event may be detected by the same functional unit that will
493 * carry out the entire processing of it and possibly will pass it to user space
494 * for further processing. In that case the functional unit that has detected
495 * the event may later "close" the "no suspend" period associated with it
496 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
497 * pm_relax(), balanced with each other, is supposed to be used in such
498 * situations.
499 *
500 * Second, a wakeup event may be detected by one functional unit and processed
501 * by another one. In that case the unit that has detected it cannot really
502 * "close" the "no suspend" period associated with it, unless it knows in
503 * advance what's going to happen to the event during processing. This
504 * knowledge, however, may not be available to it, so it can simply specify time
505 * to wait before the system can be suspended and pass it as the second
506 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200507 *
508 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
509 * "no suspend" period will be ended either by the pm_relax(), or by the timer
510 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200511 */
512
513/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200514 * wakup_source_activate - Mark given wakeup source as active.
515 * @ws: Wakeup source to handle.
516 *
517 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
518 * core of the event by incrementing the counter of of wakeup events being
519 * processed.
520 */
521static void wakeup_source_activate(struct wakeup_source *ws)
522{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200523 unsigned int cec;
524
Jin Qianb6ec9452015-05-06 15:26:56 -0700525 if (WARN_ONCE(wakeup_source_not_registered(ws),
526 "unregistered wakeup source\n"))
527 return;
528
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100529 /*
530 * active wakeup source should bring the system
531 * out of PM_SUSPEND_FREEZE state
532 */
533 freeze_wake();
534
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200535 ws->active = true;
536 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200537 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200538 if (ws->autosleep_enabled)
539 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200540
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100541 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200542 cec = atomic_inc_return(&combined_event_count);
543
544 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200545}
546
547/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200548 * wakeup_source_report_event - Report wakeup event using the given source.
549 * @ws: Wakeup source to report the event for.
550 */
551static void wakeup_source_report_event(struct wakeup_source *ws)
552{
553 ws->event_count++;
554 /* This is racy, but the counter is approximate anyway. */
555 if (events_check_enabled)
556 ws->wakeup_count++;
557
558 if (!ws->active)
559 wakeup_source_activate(ws);
560}
561
562/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200563 * __pm_stay_awake - Notify the PM core of a wakeup event.
564 * @ws: Wakeup source object associated with the source of the event.
565 *
566 * It is safe to call this function from interrupt context.
567 */
568void __pm_stay_awake(struct wakeup_source *ws)
569{
570 unsigned long flags;
571
572 if (!ws)
573 return;
574
575 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100576
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200577 wakeup_source_report_event(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100578 del_timer(&ws->timer);
579 ws->timer_expires = 0;
580
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200581 spin_unlock_irqrestore(&ws->lock, flags);
582}
583EXPORT_SYMBOL_GPL(__pm_stay_awake);
584
585/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
587 * @dev: Device the wakeup event is related to.
588 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200589 * Notify the PM core of a wakeup event (signaled by @dev) by calling
590 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591 *
592 * Call this function after detecting of a wakeup event if pm_relax() is going
593 * to be called directly after processing the event (and possibly passing it to
594 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200595 */
596void pm_stay_awake(struct device *dev)
597{
598 unsigned long flags;
599
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200600 if (!dev)
601 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200602
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200603 spin_lock_irqsave(&dev->power.lock, flags);
604 __pm_stay_awake(dev->power.wakeup);
605 spin_unlock_irqrestore(&dev->power.lock, flags);
606}
607EXPORT_SYMBOL_GPL(pm_stay_awake);
608
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200609#ifdef CONFIG_PM_AUTOSLEEP
610static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
611{
612 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
613 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
614}
615#else
616static inline void update_prevent_sleep_time(struct wakeup_source *ws,
617 ktime_t now) {}
618#endif
619
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200620/**
621 * wakup_source_deactivate - Mark given wakeup source as inactive.
622 * @ws: Wakeup source to handle.
623 *
624 * Update the @ws' statistics and notify the PM core that the wakeup source has
625 * become inactive by decrementing the counter of wakeup events being processed
626 * and incrementing the counter of registered wakeup events.
627 */
628static void wakeup_source_deactivate(struct wakeup_source *ws)
629{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200630 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200631 ktime_t duration;
632 ktime_t now;
633
634 ws->relax_count++;
635 /*
636 * __pm_relax() may be called directly or from a timer function.
637 * If it is called directly right after the timer function has been
638 * started, but before the timer function calls __pm_relax(), it is
639 * possible that __pm_stay_awake() will be called in the meantime and
640 * will set ws->active. Then, ws->active may be cleared immediately
641 * by the __pm_relax() called from the timer function, but in such a
642 * case ws->relax_count will be different from ws->active_count.
643 */
644 if (ws->relax_count != ws->active_count) {
645 ws->relax_count--;
646 return;
647 }
648
649 ws->active = false;
650
651 now = ktime_get();
652 duration = ktime_sub(now, ws->last_time);
653 ws->total_time = ktime_add(ws->total_time, duration);
654 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
655 ws->max_time = duration;
656
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200657 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200658 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100659 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200660
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200661 if (ws->autosleep_enabled)
662 update_prevent_sleep_time(ws, now);
663
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200664 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100665 * Increment the counter of registered wakeup events and decrement the
666 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200667 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200668 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
669 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200670
671 split_counters(&cnt, &inpr);
672 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
673 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200674}
675
676/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200677 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
678 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200679 *
680 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200681 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200682 *
683 * It is safe to call it from interrupt context.
684 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200685void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200686{
687 unsigned long flags;
688
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200689 if (!ws)
690 return;
691
692 spin_lock_irqsave(&ws->lock, flags);
693 if (ws->active)
694 wakeup_source_deactivate(ws);
695 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200696}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200697EXPORT_SYMBOL_GPL(__pm_relax);
698
699/**
700 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
701 * @dev: Device that signaled the event.
702 *
703 * Execute __pm_relax() for the @dev's wakeup source object.
704 */
705void pm_relax(struct device *dev)
706{
707 unsigned long flags;
708
709 if (!dev)
710 return;
711
712 spin_lock_irqsave(&dev->power.lock, flags);
713 __pm_relax(dev->power.wakeup);
714 spin_unlock_irqrestore(&dev->power.lock, flags);
715}
716EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200717
718/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200719 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200720 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200721 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100722 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
723 * in @data if it is currently active and its timer has not been canceled and
724 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200725 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200726static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200727{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100728 struct wakeup_source *ws = (struct wakeup_source *)data;
729 unsigned long flags;
730
731 spin_lock_irqsave(&ws->lock, flags);
732
733 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200734 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100735 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200736 ws->expire_count++;
737 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100738
739 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200740}
741
742/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200743 * __pm_wakeup_event - Notify the PM core of a wakeup event.
744 * @ws: Wakeup source object associated with the event source.
745 * @msec: Anticipated event processing time (in milliseconds).
746 *
747 * Notify the PM core of a wakeup event whose source is @ws that will take
748 * approximately @msec milliseconds to be processed by the kernel. If @ws is
749 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
750 * execute pm_wakeup_timer_fn() in future.
751 *
752 * It is safe to call this function from interrupt context.
753 */
754void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
755{
756 unsigned long flags;
757 unsigned long expires;
758
759 if (!ws)
760 return;
761
762 spin_lock_irqsave(&ws->lock, flags);
763
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200764 wakeup_source_report_event(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200765
766 if (!msec) {
767 wakeup_source_deactivate(ws);
768 goto unlock;
769 }
770
771 expires = jiffies + msecs_to_jiffies(msec);
772 if (!expires)
773 expires = 1;
774
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100775 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200776 mod_timer(&ws->timer, expires);
777 ws->timer_expires = expires;
778 }
779
780 unlock:
781 spin_unlock_irqrestore(&ws->lock, flags);
782}
783EXPORT_SYMBOL_GPL(__pm_wakeup_event);
784
785
786/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200787 * pm_wakeup_event - Notify the PM core of a wakeup event.
788 * @dev: Device the wakeup event is related to.
789 * @msec: Anticipated event processing time (in milliseconds).
790 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200791 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200792 */
793void pm_wakeup_event(struct device *dev, unsigned int msec)
794{
795 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200796
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200797 if (!dev)
798 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200799
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200800 spin_lock_irqsave(&dev->power.lock, flags);
801 __pm_wakeup_event(dev->power.wakeup, msec);
802 spin_unlock_irqrestore(&dev->power.lock, flags);
803}
804EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200805
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700806void pm_get_active_wakeup_sources(char *pending_wakeup_source, size_t max)
807{
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700808 struct wakeup_source *ws, *last_active_ws = NULL;
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700809 int len = 0;
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700810 bool active = false;
811
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700812 rcu_read_lock();
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700813 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
Ruchi Kandoi78ab6c42015-11-10 10:53:55 -0800814 if (ws->active && len < max) {
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700815 if (!active)
816 len += scnprintf(pending_wakeup_source, max,
817 "Pending Wakeup Sources: ");
818 len += scnprintf(pending_wakeup_source + len, max - len,
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700819 "%s ", ws->name);
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700820 active = true;
821 } else if (!active &&
822 (!last_active_ws ||
823 ktime_to_ns(ws->last_time) >
824 ktime_to_ns(last_active_ws->last_time))) {
825 last_active_ws = ws;
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700826 }
827 }
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700828 if (!active && last_active_ws) {
829 scnprintf(pending_wakeup_source, max,
830 "Last active Wakeup Source: %s",
831 last_active_ws->name);
832 }
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700833 rcu_read_unlock();
834}
835EXPORT_SYMBOL_GPL(pm_get_active_wakeup_sources);
836
Julius Wernerbb177fe2013-06-12 12:55:22 -0700837void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200838{
839 struct wakeup_source *ws;
840 int active = 0;
841 struct wakeup_source *last_activity_ws = NULL;
842
843 rcu_read_lock();
844 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
845 if (ws->active) {
846 pr_info("active wakeup source: %s\n", ws->name);
847 active = 1;
848 } else if (!active &&
849 (!last_activity_ws ||
850 ktime_to_ns(ws->last_time) >
851 ktime_to_ns(last_activity_ws->last_time))) {
852 last_activity_ws = ws;
853 }
854 }
855
856 if (!active && last_activity_ws)
857 pr_info("last active wakeup source: %s\n",
858 last_activity_ws->name);
859 rcu_read_unlock();
860}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700861EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200862
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200863/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100864 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200865 *
866 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100867 * value from the past and return true if new wakeup events have been registered
868 * since the old value was stored. Also return true if the current number of
869 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200870 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100871bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200872{
873 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100874 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200875
876 spin_lock_irqsave(&events_lock, flags);
877 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100878 unsigned int cnt, inpr;
879
880 split_counters(&cnt, &inpr);
881 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100882 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200883 }
884 spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200885
Bernie Thompson9350de062013-06-01 00:47:43 +0000886 if (ret) {
887 pr_info("PM: Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700888 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000889 }
Todd Poynora938da02012-08-12 00:17:02 +0200890
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200891 return ret || pm_abort_suspend;
892}
893
894void pm_system_wakeup(void)
895{
896 pm_abort_suspend = true;
897 freeze_wake();
898}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100899EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200900
901void pm_wakeup_clear(void)
902{
903 pm_abort_suspend = false;
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700904 pm_wakeup_irq = 0;
905}
906
907void pm_system_irq_wakeup(unsigned int irq_number)
908{
909 if (pm_wakeup_irq == 0) {
910 pm_wakeup_irq = irq_number;
911 pm_system_wakeup();
912 }
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200913}
914
915/**
916 * pm_get_wakeup_count - Read the number of registered wakeup events.
917 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200918 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200919 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200920 * Store the number of registered wakeup events at the address in @count. If
921 * @block is set, block until the current number of wakeup events being
922 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200923 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200924 * Return 'false' if the current number of wakeup events being processed is
925 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200926 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200927bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200928{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100929 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200930
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200931 if (block) {
932 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200933
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200934 for (;;) {
935 prepare_to_wait(&wakeup_count_wait_queue, &wait,
936 TASK_INTERRUPTIBLE);
937 split_counters(&cnt, &inpr);
938 if (inpr == 0 || signal_pending(current))
939 break;
940
941 schedule();
942 }
943 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200944 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200945
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100946 split_counters(&cnt, &inpr);
947 *count = cnt;
948 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200949}
950
951/**
952 * pm_save_wakeup_count - Save the current number of registered wakeup events.
953 * @count: Value to compare with the current number of registered wakeup events.
954 *
955 * If @count is equal to the current number of registered wakeup events and the
956 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100957 * old number of registered wakeup events for pm_check_wakeup_events(), enable
958 * wakeup events detection and return 'true'. Otherwise disable wakeup events
959 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200960 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200961bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200962{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100963 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200964 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200965
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100966 events_check_enabled = false;
John Stultz49550702012-09-06 23:19:06 +0200967 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100968 split_counters(&cnt, &inpr);
969 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200970 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200971 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200972 }
John Stultz49550702012-09-06 23:19:06 +0200973 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100974 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200975}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200976
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200977#ifdef CONFIG_PM_AUTOSLEEP
978/**
979 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
980 * @enabled: Whether to set or to clear the autosleep_enabled flags.
981 */
982void pm_wakep_autosleep_enabled(bool set)
983{
984 struct wakeup_source *ws;
985 ktime_t now = ktime_get();
986
987 rcu_read_lock();
988 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
989 spin_lock_irq(&ws->lock);
990 if (ws->autosleep_enabled != set) {
991 ws->autosleep_enabled = set;
992 if (ws->active) {
993 if (set)
994 ws->start_prevent_time = now;
995 else
996 update_prevent_sleep_time(ws, now);
997 }
998 }
999 spin_unlock_irq(&ws->lock);
1000 }
1001 rcu_read_unlock();
1002}
1003#endif /* CONFIG_PM_AUTOSLEEP */
1004
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001005static struct dentry *wakeup_sources_stats_dentry;
1006
1007/**
1008 * print_wakeup_source_stats - Print wakeup source statistics information.
1009 * @m: seq_file to print the statistics into.
1010 * @ws: Wakeup source object to print the statistics for.
1011 */
1012static int print_wakeup_source_stats(struct seq_file *m,
1013 struct wakeup_source *ws)
1014{
1015 unsigned long flags;
1016 ktime_t total_time;
1017 ktime_t max_time;
1018 unsigned long active_count;
1019 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001020 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001021
1022 spin_lock_irqsave(&ws->lock, flags);
1023
1024 total_time = ws->total_time;
1025 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001026 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001027 active_count = ws->active_count;
1028 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001029 ktime_t now = ktime_get();
1030
1031 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001032 total_time = ktime_add(total_time, active_time);
1033 if (active_time.tv64 > max_time.tv64)
1034 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001035
1036 if (ws->autosleep_enabled)
1037 prevent_sleep_time = ktime_add(prevent_sleep_time,
1038 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001039 } else {
1040 active_time = ktime_set(0, 0);
1041 }
1042
yangdongdong481aeb22015-08-08 11:59:59 +08001043 seq_printf(m, "%-32s\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",
Joe Perches9f6a2402015-04-15 16:17:48 -07001044 ws->name, active_count, ws->event_count,
1045 ws->wakeup_count, ws->expire_count,
1046 ktime_to_ms(active_time), ktime_to_ms(total_time),
1047 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1048 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001049
1050 spin_unlock_irqrestore(&ws->lock, flags);
1051
Joe Perches9f6a2402015-04-15 16:17:48 -07001052 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001053}
1054
1055/**
1056 * wakeup_sources_stats_show - Print wakeup sources statistics information.
1057 * @m: seq_file to print the statistics into.
1058 */
1059static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
1060{
1061 struct wakeup_source *ws;
1062
yangdongdong481aeb22015-08-08 11:59:59 +08001063 seq_puts(m, "name\t\t\t\t\tactive_count\tevent_count\twakeup_count\t"
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +02001064 "expire_count\tactive_since\ttotal_time\tmax_time\t"
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001065 "last_change\tprevent_suspend_time\n");
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001066
1067 rcu_read_lock();
1068 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
1069 print_wakeup_source_stats(m, ws);
1070 rcu_read_unlock();
1071
Jin Qian7f436052015-05-15 18:10:37 -07001072 print_wakeup_source_stats(m, &deleted_ws);
1073
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001074 return 0;
1075}
1076
1077static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1078{
1079 return single_open(file, wakeup_sources_stats_show, NULL);
1080}
1081
1082static const struct file_operations wakeup_sources_stats_fops = {
1083 .owner = THIS_MODULE,
1084 .open = wakeup_sources_stats_open,
1085 .read = seq_read,
1086 .llseek = seq_lseek,
1087 .release = single_release,
1088};
1089
1090static int __init wakeup_sources_debugfs_init(void)
1091{
1092 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1093 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1094 return 0;
1095}
1096
1097postcore_initcall(wakeup_sources_debugfs_init);