blob: 90c16d8952aa5b98e7e1afcffc0023a32ad32df3 [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
Thomas Gleixner54804372017-06-25 19:31:13 +020064DEFINE_STATIC_SRCU(wakeup_srcu);
65
Jin Qian7f436052015-05-15 18:10:37 -070066static struct wakeup_source deleted_ws = {
67 .name = "deleted",
68 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
69};
70
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020071/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010072 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
73 * @ws: Wakeup source to prepare.
74 * @name: Pointer to the name of the new wakeup source.
75 *
76 * Callers must ensure that the @name string won't be freed when @ws is still in
77 * use.
78 */
79void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
80{
81 if (ws) {
82 memset(ws, 0, sizeof(*ws));
83 ws->name = name;
84 }
85}
86EXPORT_SYMBOL_GPL(wakeup_source_prepare);
87
88/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020089 * wakeup_source_create - Create a struct wakeup_source object.
90 * @name: Name of the new wakeup source.
91 */
92struct wakeup_source *wakeup_source_create(const char *name)
93{
94 struct wakeup_source *ws;
95
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010096 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020097 if (!ws)
98 return NULL;
99
Rasmus Villemoes72362142015-09-09 23:42:06 +0200100 wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200101 return ws;
102}
103EXPORT_SYMBOL_GPL(wakeup_source_create);
104
105/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100106 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
107 * @ws: Wakeup source to prepare for destruction.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100108 *
109 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
110 * be run in parallel with this function for the same wakeup source object.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200111 */
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100112void wakeup_source_drop(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200113{
114 if (!ws)
115 return;
116
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100117 del_timer_sync(&ws->timer);
118 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100119}
120EXPORT_SYMBOL_GPL(wakeup_source_drop);
121
Jin Qian7f436052015-05-15 18:10:37 -0700122/*
123 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
124 */
125static void wakeup_source_record(struct wakeup_source *ws)
126{
127 unsigned long flags;
128
129 spin_lock_irqsave(&deleted_ws.lock, flags);
130
131 if (ws->event_count) {
132 deleted_ws.total_time =
133 ktime_add(deleted_ws.total_time, ws->total_time);
134 deleted_ws.prevent_sleep_time =
135 ktime_add(deleted_ws.prevent_sleep_time,
136 ws->prevent_sleep_time);
137 deleted_ws.max_time =
138 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
139 deleted_ws.max_time : ws->max_time;
140 deleted_ws.event_count += ws->event_count;
141 deleted_ws.active_count += ws->active_count;
142 deleted_ws.relax_count += ws->relax_count;
143 deleted_ws.expire_count += ws->expire_count;
144 deleted_ws.wakeup_count += ws->wakeup_count;
145 }
146
147 spin_unlock_irqrestore(&deleted_ws.lock, flags);
148}
149
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100150/**
151 * wakeup_source_destroy - Destroy a struct wakeup_source object.
152 * @ws: Wakeup source to destroy.
153 *
154 * Use only for wakeup source objects created with wakeup_source_create().
155 */
156void wakeup_source_destroy(struct wakeup_source *ws)
157{
158 if (!ws)
159 return;
160
161 wakeup_source_drop(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700162 wakeup_source_record(ws);
Rasmus Villemoes72362142015-09-09 23:42:06 +0200163 kfree_const(ws->name);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200164 kfree(ws);
165}
166EXPORT_SYMBOL_GPL(wakeup_source_destroy);
167
168/**
169 * wakeup_source_add - Add given object to the list of wakeup sources.
170 * @ws: Wakeup source object to add to the list.
171 */
172void wakeup_source_add(struct wakeup_source *ws)
173{
John Stultz49550702012-09-06 23:19:06 +0200174 unsigned long flags;
175
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200176 if (WARN_ON(!ws))
177 return;
178
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100179 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200180 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
181 ws->active = false;
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200182 ws->last_time = ktime_get();
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200183
John Stultz49550702012-09-06 23:19:06 +0200184 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200185 list_add_rcu(&ws->entry, &wakeup_sources);
John Stultz49550702012-09-06 23:19:06 +0200186 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200187}
188EXPORT_SYMBOL_GPL(wakeup_source_add);
189
190/**
191 * wakeup_source_remove - Remove given object from the wakeup sources list.
192 * @ws: Wakeup source object to remove from the list.
193 */
194void wakeup_source_remove(struct wakeup_source *ws)
195{
John Stultz49550702012-09-06 23:19:06 +0200196 unsigned long flags;
197
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200198 if (WARN_ON(!ws))
199 return;
200
John Stultz49550702012-09-06 23:19:06 +0200201 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200202 list_del_rcu(&ws->entry);
John Stultz49550702012-09-06 23:19:06 +0200203 spin_unlock_irqrestore(&events_lock, flags);
Thomas Gleixner54804372017-06-25 19:31:13 +0200204 synchronize_srcu(&wakeup_srcu);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200205}
206EXPORT_SYMBOL_GPL(wakeup_source_remove);
207
208/**
209 * wakeup_source_register - Create wakeup source and add it to the list.
210 * @name: Name of the wakeup source to register.
211 */
212struct wakeup_source *wakeup_source_register(const char *name)
213{
214 struct wakeup_source *ws;
215
216 ws = wakeup_source_create(name);
217 if (ws)
218 wakeup_source_add(ws);
219
220 return ws;
221}
222EXPORT_SYMBOL_GPL(wakeup_source_register);
223
224/**
225 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
226 * @ws: Wakeup source object to unregister.
227 */
228void wakeup_source_unregister(struct wakeup_source *ws)
229{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100230 if (ws) {
231 wakeup_source_remove(ws);
232 wakeup_source_destroy(ws);
233 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200234}
235EXPORT_SYMBOL_GPL(wakeup_source_unregister);
236
237/**
238 * device_wakeup_attach - Attach a wakeup source object to a device object.
239 * @dev: Device to handle.
240 * @ws: Wakeup source object to attach to @dev.
241 *
242 * This causes @dev to be treated as a wakeup device.
243 */
244static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
245{
246 spin_lock_irq(&dev->power.lock);
247 if (dev->power.wakeup) {
248 spin_unlock_irq(&dev->power.lock);
249 return -EEXIST;
250 }
251 dev->power.wakeup = ws;
Strashko, Grygorii16669be2016-04-06 14:45:53 +0300252 if (dev->power.wakeirq)
253 device_wakeup_attach_irq(dev, dev->power.wakeirq);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200254 spin_unlock_irq(&dev->power.lock);
255 return 0;
256}
257
258/**
259 * device_wakeup_enable - Enable given device to be a wakeup source.
260 * @dev: Device to handle.
261 *
262 * Create a wakeup source object, register it and attach it to @dev.
263 */
264int device_wakeup_enable(struct device *dev)
265{
266 struct wakeup_source *ws;
267 int ret;
268
269 if (!dev || !dev->power.can_wakeup)
270 return -EINVAL;
271
272 ws = wakeup_source_register(dev_name(dev));
273 if (!ws)
274 return -ENOMEM;
275
276 ret = device_wakeup_attach(dev, ws);
277 if (ret)
278 wakeup_source_unregister(ws);
279
280 return ret;
281}
282EXPORT_SYMBOL_GPL(device_wakeup_enable);
283
284/**
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700285 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
286 * @dev: Device to handle
287 * @wakeirq: Device specific wakeirq entry
288 *
289 * Attach a device wakeirq to the wakeup source so the device
290 * wake IRQ can be configured automatically for suspend and
291 * resume.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200292 *
293 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700294 */
295int device_wakeup_attach_irq(struct device *dev,
296 struct wake_irq *wakeirq)
297{
298 struct wakeup_source *ws;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700299
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700300 ws = dev->power.wakeup;
301 if (!ws) {
302 dev_err(dev, "forgot to call call device_init_wakeup?\n");
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200303 return -EINVAL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700304 }
305
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200306 if (ws->wakeirq)
307 return -EEXIST;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700308
309 ws->wakeirq = wakeirq;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200310 return 0;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700311}
312
313/**
314 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
315 * @dev: Device to handle
316 *
317 * Removes a device wakeirq from the wakeup source.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200318 *
319 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700320 */
321void device_wakeup_detach_irq(struct device *dev)
322{
323 struct wakeup_source *ws;
324
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700325 ws = dev->power.wakeup;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200326 if (ws)
327 ws->wakeirq = NULL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700328}
329
330/**
331 * device_wakeup_arm_wake_irqs(void)
332 *
333 * Itereates over the list of device wakeirqs to arm them.
334 */
335void device_wakeup_arm_wake_irqs(void)
336{
337 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +0200338 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700339
Thomas Gleixner54804372017-06-25 19:31:13 +0200340 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200341 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
342 dev_pm_arm_wake_irq(ws->wakeirq);
Thomas Gleixner54804372017-06-25 19:31:13 +0200343 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700344}
345
346/**
347 * device_wakeup_disarm_wake_irqs(void)
348 *
349 * Itereates over the list of device wakeirqs to disarm them.
350 */
351void device_wakeup_disarm_wake_irqs(void)
352{
353 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +0200354 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700355
Thomas Gleixner54804372017-06-25 19:31:13 +0200356 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200357 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
358 dev_pm_disarm_wake_irq(ws->wakeirq);
Thomas Gleixner54804372017-06-25 19:31:13 +0200359 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700360}
361
362/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200363 * device_wakeup_detach - Detach a device's wakeup source object from it.
364 * @dev: Device to detach the wakeup source object from.
365 *
366 * After it returns, @dev will not be treated as a wakeup device any more.
367 */
368static struct wakeup_source *device_wakeup_detach(struct device *dev)
369{
370 struct wakeup_source *ws;
371
372 spin_lock_irq(&dev->power.lock);
373 ws = dev->power.wakeup;
374 dev->power.wakeup = NULL;
375 spin_unlock_irq(&dev->power.lock);
376 return ws;
377}
378
379/**
380 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
381 * @dev: Device to handle.
382 *
383 * Detach the @dev's wakeup source object from it, unregister this wakeup source
384 * object and destroy it.
385 */
386int device_wakeup_disable(struct device *dev)
387{
388 struct wakeup_source *ws;
389
390 if (!dev || !dev->power.can_wakeup)
391 return -EINVAL;
392
393 ws = device_wakeup_detach(dev);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200394 wakeup_source_unregister(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200395 return 0;
396}
397EXPORT_SYMBOL_GPL(device_wakeup_disable);
398
399/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100400 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
401 * @dev: Device to handle.
402 * @capable: Whether or not @dev is capable of waking up the system from sleep.
403 *
404 * If @capable is set, set the @dev's power.can_wakeup flag and add its
405 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
406 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
407 *
408 * This function may sleep and it can't be called from any context where
409 * sleeping is not allowed.
410 */
411void device_set_wakeup_capable(struct device *dev, bool capable)
412{
413 if (!!dev->power.can_wakeup == !!capable)
414 return;
415
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200416 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100417 if (capable) {
418 if (wakeup_sysfs_add(dev))
419 return;
420 } else {
421 wakeup_sysfs_remove(dev);
422 }
423 }
424 dev->power.can_wakeup = capable;
425}
426EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
427
428/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200429 * device_init_wakeup - Device wakeup initialization.
430 * @dev: Device to handle.
431 * @enable: Whether or not to enable @dev as a wakeup device.
432 *
433 * By default, most devices should leave wakeup disabled. The exceptions are
434 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200435 * possibly network interfaces, etc. Also, devices that don't generate their
436 * own wakeup requests but merely forward requests from one bus to another
437 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200438 */
439int device_init_wakeup(struct device *dev, bool enable)
440{
441 int ret = 0;
442
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800443 if (!dev)
444 return -EINVAL;
445
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200446 if (enable) {
447 device_set_wakeup_capable(dev, true);
448 ret = device_wakeup_enable(dev);
449 } else {
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800450 if (dev->power.can_wakeup)
451 device_wakeup_disable(dev);
452
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200453 device_set_wakeup_capable(dev, false);
454 }
455
456 return ret;
457}
458EXPORT_SYMBOL_GPL(device_init_wakeup);
459
460/**
461 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
462 * @dev: Device to handle.
463 */
464int device_set_wakeup_enable(struct device *dev, bool enable)
465{
466 if (!dev || !dev->power.can_wakeup)
467 return -EINVAL;
468
469 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
470}
471EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200472
Jin Qianb6ec9452015-05-06 15:26:56 -0700473/**
474 * wakeup_source_not_registered - validate the given wakeup source.
475 * @ws: Wakeup source to be validated.
476 */
477static bool wakeup_source_not_registered(struct wakeup_source *ws)
478{
479 /*
480 * Use timer struct to check if the given source is initialized
481 * by wakeup_source_add.
482 */
483 return ws->timer.function != pm_wakeup_timer_fn ||
484 ws->timer.data != (unsigned long)ws;
485}
486
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200487/*
488 * The functions below use the observation that each wakeup event starts a
489 * period in which the system should not be suspended. The moment this period
490 * will end depends on how the wakeup event is going to be processed after being
491 * detected and all of the possible cases can be divided into two distinct
492 * groups.
493 *
494 * First, a wakeup event may be detected by the same functional unit that will
495 * carry out the entire processing of it and possibly will pass it to user space
496 * for further processing. In that case the functional unit that has detected
497 * the event may later "close" the "no suspend" period associated with it
498 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
499 * pm_relax(), balanced with each other, is supposed to be used in such
500 * situations.
501 *
502 * Second, a wakeup event may be detected by one functional unit and processed
503 * by another one. In that case the unit that has detected it cannot really
504 * "close" the "no suspend" period associated with it, unless it knows in
505 * advance what's going to happen to the event during processing. This
506 * knowledge, however, may not be available to it, so it can simply specify time
507 * to wait before the system can be suspended and pass it as the second
508 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200509 *
510 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
511 * "no suspend" period will be ended either by the pm_relax(), or by the timer
512 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200513 */
514
515/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200516 * wakup_source_activate - Mark given wakeup source as active.
517 * @ws: Wakeup source to handle.
518 *
519 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
520 * core of the event by incrementing the counter of of wakeup events being
521 * processed.
522 */
523static void wakeup_source_activate(struct wakeup_source *ws)
524{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200525 unsigned int cec;
526
Jin Qianb6ec9452015-05-06 15:26:56 -0700527 if (WARN_ONCE(wakeup_source_not_registered(ws),
528 "unregistered wakeup source\n"))
529 return;
530
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100531 /*
532 * active wakeup source should bring the system
533 * out of PM_SUSPEND_FREEZE state
534 */
535 freeze_wake();
536
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200537 ws->active = true;
538 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200539 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200540 if (ws->autosleep_enabled)
541 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200542
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100543 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200544 cec = atomic_inc_return(&combined_event_count);
545
546 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200547}
548
549/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200550 * wakeup_source_report_event - Report wakeup event using the given source.
551 * @ws: Wakeup source to report the event for.
552 */
553static void wakeup_source_report_event(struct wakeup_source *ws)
554{
555 ws->event_count++;
556 /* This is racy, but the counter is approximate anyway. */
557 if (events_check_enabled)
558 ws->wakeup_count++;
559
560 if (!ws->active)
561 wakeup_source_activate(ws);
562}
563
564/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200565 * __pm_stay_awake - Notify the PM core of a wakeup event.
566 * @ws: Wakeup source object associated with the source of the event.
567 *
568 * It is safe to call this function from interrupt context.
569 */
570void __pm_stay_awake(struct wakeup_source *ws)
571{
572 unsigned long flags;
573
574 if (!ws)
575 return;
576
577 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100578
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200579 wakeup_source_report_event(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100580 del_timer(&ws->timer);
581 ws->timer_expires = 0;
582
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200583 spin_unlock_irqrestore(&ws->lock, flags);
584}
585EXPORT_SYMBOL_GPL(__pm_stay_awake);
586
587/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200588 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
589 * @dev: Device the wakeup event is related to.
590 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200591 * Notify the PM core of a wakeup event (signaled by @dev) by calling
592 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200593 *
594 * Call this function after detecting of a wakeup event if pm_relax() is going
595 * to be called directly after processing the event (and possibly passing it to
596 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200597 */
598void pm_stay_awake(struct device *dev)
599{
600 unsigned long flags;
601
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200602 if (!dev)
603 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200604
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200605 spin_lock_irqsave(&dev->power.lock, flags);
606 __pm_stay_awake(dev->power.wakeup);
607 spin_unlock_irqrestore(&dev->power.lock, flags);
608}
609EXPORT_SYMBOL_GPL(pm_stay_awake);
610
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200611#ifdef CONFIG_PM_AUTOSLEEP
612static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
613{
614 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
615 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
616}
617#else
618static inline void update_prevent_sleep_time(struct wakeup_source *ws,
619 ktime_t now) {}
620#endif
621
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200622/**
623 * wakup_source_deactivate - Mark given wakeup source as inactive.
624 * @ws: Wakeup source to handle.
625 *
626 * Update the @ws' statistics and notify the PM core that the wakeup source has
627 * become inactive by decrementing the counter of wakeup events being processed
628 * and incrementing the counter of registered wakeup events.
629 */
630static void wakeup_source_deactivate(struct wakeup_source *ws)
631{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200632 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200633 ktime_t duration;
634 ktime_t now;
635
636 ws->relax_count++;
637 /*
638 * __pm_relax() may be called directly or from a timer function.
639 * If it is called directly right after the timer function has been
640 * started, but before the timer function calls __pm_relax(), it is
641 * possible that __pm_stay_awake() will be called in the meantime and
642 * will set ws->active. Then, ws->active may be cleared immediately
643 * by the __pm_relax() called from the timer function, but in such a
644 * case ws->relax_count will be different from ws->active_count.
645 */
646 if (ws->relax_count != ws->active_count) {
647 ws->relax_count--;
648 return;
649 }
650
651 ws->active = false;
652
653 now = ktime_get();
654 duration = ktime_sub(now, ws->last_time);
655 ws->total_time = ktime_add(ws->total_time, duration);
656 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
657 ws->max_time = duration;
658
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200659 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200660 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100661 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200662
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200663 if (ws->autosleep_enabled)
664 update_prevent_sleep_time(ws, now);
665
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200666 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100667 * Increment the counter of registered wakeup events and decrement the
668 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200669 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200670 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
671 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200672
673 split_counters(&cnt, &inpr);
674 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
675 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200676}
677
678/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200679 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
680 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200681 *
682 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200683 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200684 *
685 * It is safe to call it from interrupt context.
686 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200687void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200688{
689 unsigned long flags;
690
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200691 if (!ws)
692 return;
693
694 spin_lock_irqsave(&ws->lock, flags);
695 if (ws->active)
696 wakeup_source_deactivate(ws);
697 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200698}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200699EXPORT_SYMBOL_GPL(__pm_relax);
700
701/**
702 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
703 * @dev: Device that signaled the event.
704 *
705 * Execute __pm_relax() for the @dev's wakeup source object.
706 */
707void pm_relax(struct device *dev)
708{
709 unsigned long flags;
710
711 if (!dev)
712 return;
713
714 spin_lock_irqsave(&dev->power.lock, flags);
715 __pm_relax(dev->power.wakeup);
716 spin_unlock_irqrestore(&dev->power.lock, flags);
717}
718EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200719
720/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200721 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200722 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200723 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100724 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
725 * in @data if it is currently active and its timer has not been canceled and
726 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200727 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200728static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200729{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100730 struct wakeup_source *ws = (struct wakeup_source *)data;
731 unsigned long flags;
732
733 spin_lock_irqsave(&ws->lock, flags);
734
735 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200736 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100737 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200738 ws->expire_count++;
739 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100740
741 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200742}
743
744/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200745 * __pm_wakeup_event - Notify the PM core of a wakeup event.
746 * @ws: Wakeup source object associated with the event source.
747 * @msec: Anticipated event processing time (in milliseconds).
748 *
749 * Notify the PM core of a wakeup event whose source is @ws that will take
750 * approximately @msec milliseconds to be processed by the kernel. If @ws is
751 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
752 * execute pm_wakeup_timer_fn() in future.
753 *
754 * It is safe to call this function from interrupt context.
755 */
756void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
757{
758 unsigned long flags;
759 unsigned long expires;
760
761 if (!ws)
762 return;
763
764 spin_lock_irqsave(&ws->lock, flags);
765
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200766 wakeup_source_report_event(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200767
768 if (!msec) {
769 wakeup_source_deactivate(ws);
770 goto unlock;
771 }
772
773 expires = jiffies + msecs_to_jiffies(msec);
774 if (!expires)
775 expires = 1;
776
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100777 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200778 mod_timer(&ws->timer, expires);
779 ws->timer_expires = expires;
780 }
781
782 unlock:
783 spin_unlock_irqrestore(&ws->lock, flags);
784}
785EXPORT_SYMBOL_GPL(__pm_wakeup_event);
786
787
788/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200789 * pm_wakeup_event - Notify the PM core of a wakeup event.
790 * @dev: Device the wakeup event is related to.
791 * @msec: Anticipated event processing time (in milliseconds).
792 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200793 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200794 */
795void pm_wakeup_event(struct device *dev, unsigned int msec)
796{
797 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200798
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200799 if (!dev)
800 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200801
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200802 spin_lock_irqsave(&dev->power.lock, flags);
803 __pm_wakeup_event(dev->power.wakeup, msec);
804 spin_unlock_irqrestore(&dev->power.lock, flags);
805}
806EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200807
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700808void pm_get_active_wakeup_sources(char *pending_wakeup_source, size_t max)
809{
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700810 struct wakeup_source *ws, *last_active_ws = NULL;
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700811 int len = 0;
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700812 bool active = false;
813
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700814 rcu_read_lock();
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700815 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
Ruchi Kandoi78ab6c42015-11-10 10:53:55 -0800816 if (ws->active && len < max) {
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700817 if (!active)
818 len += scnprintf(pending_wakeup_source, max,
819 "Pending Wakeup Sources: ");
820 len += scnprintf(pending_wakeup_source + len, max - len,
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700821 "%s ", ws->name);
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700822 active = true;
823 } else if (!active &&
824 (!last_active_ws ||
825 ktime_to_ns(ws->last_time) >
826 ktime_to_ns(last_active_ws->last_time))) {
827 last_active_ws = ws;
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700828 }
829 }
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700830 if (!active && last_active_ws) {
831 scnprintf(pending_wakeup_source, max,
832 "Last active Wakeup Source: %s",
833 last_active_ws->name);
834 }
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700835 rcu_read_unlock();
836}
837EXPORT_SYMBOL_GPL(pm_get_active_wakeup_sources);
838
Julius Wernerbb177fe2013-06-12 12:55:22 -0700839void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200840{
841 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +0200842 int srcuidx, active = 0;
Todd Poynora938da02012-08-12 00:17:02 +0200843 struct wakeup_source *last_activity_ws = NULL;
844
Thomas Gleixner54804372017-06-25 19:31:13 +0200845 srcuidx = srcu_read_lock(&wakeup_srcu);
Todd Poynora938da02012-08-12 00:17:02 +0200846 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
847 if (ws->active) {
848 pr_info("active wakeup source: %s\n", ws->name);
849 active = 1;
850 } else if (!active &&
851 (!last_activity_ws ||
852 ktime_to_ns(ws->last_time) >
853 ktime_to_ns(last_activity_ws->last_time))) {
854 last_activity_ws = ws;
855 }
856 }
857
858 if (!active && last_activity_ws)
859 pr_info("last active wakeup source: %s\n",
860 last_activity_ws->name);
Thomas Gleixner54804372017-06-25 19:31:13 +0200861 srcu_read_unlock(&wakeup_srcu, srcuidx);
Todd Poynora938da02012-08-12 00:17:02 +0200862}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700863EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200864
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200865/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100866 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200867 *
868 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100869 * value from the past and return true if new wakeup events have been registered
870 * since the old value was stored. Also return true if the current number of
871 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200872 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100873bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200874{
875 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100876 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200877
878 spin_lock_irqsave(&events_lock, flags);
879 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100880 unsigned int cnt, inpr;
881
882 split_counters(&cnt, &inpr);
883 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100884 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200885 }
886 spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200887
Bernie Thompson9350de062013-06-01 00:47:43 +0000888 if (ret) {
889 pr_info("PM: Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700890 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000891 }
Todd Poynora938da02012-08-12 00:17:02 +0200892
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200893 return ret || pm_abort_suspend;
894}
895
896void pm_system_wakeup(void)
897{
898 pm_abort_suspend = true;
899 freeze_wake();
900}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100901EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200902
903void pm_wakeup_clear(void)
904{
905 pm_abort_suspend = false;
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700906 pm_wakeup_irq = 0;
907}
908
909void pm_system_irq_wakeup(unsigned int irq_number)
910{
911 if (pm_wakeup_irq == 0) {
912 pm_wakeup_irq = irq_number;
913 pm_system_wakeup();
914 }
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200915}
916
917/**
918 * pm_get_wakeup_count - Read the number of registered wakeup events.
919 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200920 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200921 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200922 * Store the number of registered wakeup events at the address in @count. If
923 * @block is set, block until the current number of wakeup events being
924 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200925 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200926 * Return 'false' if the current number of wakeup events being processed is
927 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200928 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200929bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200930{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100931 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200932
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200933 if (block) {
934 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200935
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200936 for (;;) {
937 prepare_to_wait(&wakeup_count_wait_queue, &wait,
938 TASK_INTERRUPTIBLE);
939 split_counters(&cnt, &inpr);
940 if (inpr == 0 || signal_pending(current))
941 break;
942
943 schedule();
944 }
945 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200946 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200947
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100948 split_counters(&cnt, &inpr);
949 *count = cnt;
950 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200951}
952
953/**
954 * pm_save_wakeup_count - Save the current number of registered wakeup events.
955 * @count: Value to compare with the current number of registered wakeup events.
956 *
957 * If @count is equal to the current number of registered wakeup events and the
958 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100959 * old number of registered wakeup events for pm_check_wakeup_events(), enable
960 * wakeup events detection and return 'true'. Otherwise disable wakeup events
961 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200962 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200963bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200964{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100965 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200966 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200967
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100968 events_check_enabled = false;
John Stultz49550702012-09-06 23:19:06 +0200969 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100970 split_counters(&cnt, &inpr);
971 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200972 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200973 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200974 }
John Stultz49550702012-09-06 23:19:06 +0200975 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100976 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200977}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200978
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200979#ifdef CONFIG_PM_AUTOSLEEP
980/**
981 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
982 * @enabled: Whether to set or to clear the autosleep_enabled flags.
983 */
984void pm_wakep_autosleep_enabled(bool set)
985{
986 struct wakeup_source *ws;
987 ktime_t now = ktime_get();
Thomas Gleixner54804372017-06-25 19:31:13 +0200988 int srcuidx;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200989
Thomas Gleixner54804372017-06-25 19:31:13 +0200990 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200991 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
992 spin_lock_irq(&ws->lock);
993 if (ws->autosleep_enabled != set) {
994 ws->autosleep_enabled = set;
995 if (ws->active) {
996 if (set)
997 ws->start_prevent_time = now;
998 else
999 update_prevent_sleep_time(ws, now);
1000 }
1001 }
1002 spin_unlock_irq(&ws->lock);
1003 }
Thomas Gleixner54804372017-06-25 19:31:13 +02001004 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001005}
1006#endif /* CONFIG_PM_AUTOSLEEP */
1007
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001008static struct dentry *wakeup_sources_stats_dentry;
1009
1010/**
1011 * print_wakeup_source_stats - Print wakeup source statistics information.
1012 * @m: seq_file to print the statistics into.
1013 * @ws: Wakeup source object to print the statistics for.
1014 */
1015static int print_wakeup_source_stats(struct seq_file *m,
1016 struct wakeup_source *ws)
1017{
1018 unsigned long flags;
1019 ktime_t total_time;
1020 ktime_t max_time;
1021 unsigned long active_count;
1022 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001023 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001024
1025 spin_lock_irqsave(&ws->lock, flags);
1026
1027 total_time = ws->total_time;
1028 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001029 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001030 active_count = ws->active_count;
1031 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001032 ktime_t now = ktime_get();
1033
1034 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001035 total_time = ktime_add(total_time, active_time);
1036 if (active_time.tv64 > max_time.tv64)
1037 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001038
1039 if (ws->autosleep_enabled)
1040 prevent_sleep_time = ktime_add(prevent_sleep_time,
1041 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001042 } else {
1043 active_time = ktime_set(0, 0);
1044 }
1045
yangdongdong481aeb22015-08-08 11:59:59 +08001046 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 -07001047 ws->name, active_count, ws->event_count,
1048 ws->wakeup_count, ws->expire_count,
1049 ktime_to_ms(active_time), ktime_to_ms(total_time),
1050 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1051 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001052
1053 spin_unlock_irqrestore(&ws->lock, flags);
1054
Joe Perches9f6a2402015-04-15 16:17:48 -07001055 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001056}
1057
1058/**
1059 * wakeup_sources_stats_show - Print wakeup sources statistics information.
1060 * @m: seq_file to print the statistics into.
1061 */
1062static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
1063{
1064 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +02001065 int srcuidx;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001066
yangdongdong481aeb22015-08-08 11:59:59 +08001067 seq_puts(m, "name\t\t\t\t\tactive_count\tevent_count\twakeup_count\t"
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +02001068 "expire_count\tactive_since\ttotal_time\tmax_time\t"
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001069 "last_change\tprevent_suspend_time\n");
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001070
Thomas Gleixner54804372017-06-25 19:31:13 +02001071 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001072 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
1073 print_wakeup_source_stats(m, ws);
Thomas Gleixner54804372017-06-25 19:31:13 +02001074 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001075
Jin Qian7f436052015-05-15 18:10:37 -07001076 print_wakeup_source_stats(m, &deleted_ws);
1077
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001078 return 0;
1079}
1080
1081static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1082{
1083 return single_open(file, wakeup_sources_stats_show, NULL);
1084}
1085
1086static const struct file_operations wakeup_sources_stats_fops = {
1087 .owner = THIS_MODULE,
1088 .open = wakeup_sources_stats_open,
1089 .read = seq_read,
1090 .llseek = seq_lseek,
1091 .release = single_release,
1092};
1093
1094static int __init wakeup_sources_debugfs_init(void)
1095{
1096 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1097 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1098 return 0;
1099}
1100
1101postcore_initcall(wakeup_sources_debugfs_init);