blob: fd1b5c819af8f0ddee0e96900e9f3abbf152c709 [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 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100118}
119EXPORT_SYMBOL_GPL(wakeup_source_drop);
120
Jin Qian7f436052015-05-15 18:10:37 -0700121/*
122 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
123 */
124static void wakeup_source_record(struct wakeup_source *ws)
125{
126 unsigned long flags;
127
128 spin_lock_irqsave(&deleted_ws.lock, flags);
129
130 if (ws->event_count) {
131 deleted_ws.total_time =
132 ktime_add(deleted_ws.total_time, ws->total_time);
133 deleted_ws.prevent_sleep_time =
134 ktime_add(deleted_ws.prevent_sleep_time,
135 ws->prevent_sleep_time);
136 deleted_ws.max_time =
137 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
138 deleted_ws.max_time : ws->max_time;
139 deleted_ws.event_count += ws->event_count;
140 deleted_ws.active_count += ws->active_count;
141 deleted_ws.relax_count += ws->relax_count;
142 deleted_ws.expire_count += ws->expire_count;
143 deleted_ws.wakeup_count += ws->wakeup_count;
144 }
145
146 spin_unlock_irqrestore(&deleted_ws.lock, flags);
147}
148
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100149/**
150 * wakeup_source_destroy - Destroy a struct wakeup_source object.
151 * @ws: Wakeup source to destroy.
152 *
153 * Use only for wakeup source objects created with wakeup_source_create().
154 */
155void wakeup_source_destroy(struct wakeup_source *ws)
156{
157 if (!ws)
158 return;
159
160 wakeup_source_drop(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700161 wakeup_source_record(ws);
Rasmus Villemoes72362142015-09-09 23:42:06 +0200162 kfree_const(ws->name);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200163 kfree(ws);
164}
165EXPORT_SYMBOL_GPL(wakeup_source_destroy);
166
167/**
168 * wakeup_source_add - Add given object to the list of wakeup sources.
169 * @ws: Wakeup source object to add to the list.
170 */
171void wakeup_source_add(struct wakeup_source *ws)
172{
John Stultz49550702012-09-06 23:19:06 +0200173 unsigned long flags;
174
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200175 if (WARN_ON(!ws))
176 return;
177
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100178 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200179 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
180 ws->active = false;
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200181 ws->last_time = ktime_get();
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200182
John Stultz49550702012-09-06 23:19:06 +0200183 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200184 list_add_rcu(&ws->entry, &wakeup_sources);
John Stultz49550702012-09-06 23:19:06 +0200185 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200186}
187EXPORT_SYMBOL_GPL(wakeup_source_add);
188
189/**
190 * wakeup_source_remove - Remove given object from the wakeup sources list.
191 * @ws: Wakeup source object to remove from the list.
192 */
193void wakeup_source_remove(struct wakeup_source *ws)
194{
John Stultz49550702012-09-06 23:19:06 +0200195 unsigned long flags;
196
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200197 if (WARN_ON(!ws))
198 return;
199
John Stultz49550702012-09-06 23:19:06 +0200200 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200201 list_del_rcu(&ws->entry);
John Stultz49550702012-09-06 23:19:06 +0200202 spin_unlock_irqrestore(&events_lock, flags);
Thomas Gleixner54804372017-06-25 19:31:13 +0200203 synchronize_srcu(&wakeup_srcu);
Viresh Kumar6f76eec2019-03-08 15:23:11 +0530204
205 del_timer_sync(&ws->timer);
206 /*
207 * Clear timer.function to make wakeup_source_not_registered() treat
208 * this wakeup source as not registered.
209 */
210 ws->timer.function = NULL;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200211}
212EXPORT_SYMBOL_GPL(wakeup_source_remove);
213
214/**
215 * wakeup_source_register - Create wakeup source and add it to the list.
216 * @name: Name of the wakeup source to register.
217 */
218struct wakeup_source *wakeup_source_register(const char *name)
219{
220 struct wakeup_source *ws;
221
222 ws = wakeup_source_create(name);
223 if (ws)
224 wakeup_source_add(ws);
225
226 return ws;
227}
228EXPORT_SYMBOL_GPL(wakeup_source_register);
229
230/**
231 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
232 * @ws: Wakeup source object to unregister.
233 */
234void wakeup_source_unregister(struct wakeup_source *ws)
235{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100236 if (ws) {
237 wakeup_source_remove(ws);
238 wakeup_source_destroy(ws);
239 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200240}
241EXPORT_SYMBOL_GPL(wakeup_source_unregister);
242
243/**
244 * device_wakeup_attach - Attach a wakeup source object to a device object.
245 * @dev: Device to handle.
246 * @ws: Wakeup source object to attach to @dev.
247 *
248 * This causes @dev to be treated as a wakeup device.
249 */
250static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
251{
252 spin_lock_irq(&dev->power.lock);
253 if (dev->power.wakeup) {
254 spin_unlock_irq(&dev->power.lock);
255 return -EEXIST;
256 }
257 dev->power.wakeup = ws;
Strashko, Grygorii16669be2016-04-06 14:45:53 +0300258 if (dev->power.wakeirq)
259 device_wakeup_attach_irq(dev, dev->power.wakeirq);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200260 spin_unlock_irq(&dev->power.lock);
261 return 0;
262}
263
264/**
265 * device_wakeup_enable - Enable given device to be a wakeup source.
266 * @dev: Device to handle.
267 *
268 * Create a wakeup source object, register it and attach it to @dev.
269 */
270int device_wakeup_enable(struct device *dev)
271{
272 struct wakeup_source *ws;
273 int ret;
274
275 if (!dev || !dev->power.can_wakeup)
276 return -EINVAL;
277
278 ws = wakeup_source_register(dev_name(dev));
279 if (!ws)
280 return -ENOMEM;
281
282 ret = device_wakeup_attach(dev, ws);
283 if (ret)
284 wakeup_source_unregister(ws);
285
286 return ret;
287}
288EXPORT_SYMBOL_GPL(device_wakeup_enable);
289
290/**
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700291 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
292 * @dev: Device to handle
293 * @wakeirq: Device specific wakeirq entry
294 *
295 * Attach a device wakeirq to the wakeup source so the device
296 * wake IRQ can be configured automatically for suspend and
297 * resume.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200298 *
299 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700300 */
301int device_wakeup_attach_irq(struct device *dev,
302 struct wake_irq *wakeirq)
303{
304 struct wakeup_source *ws;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700305
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700306 ws = dev->power.wakeup;
307 if (!ws) {
308 dev_err(dev, "forgot to call call device_init_wakeup?\n");
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200309 return -EINVAL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700310 }
311
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200312 if (ws->wakeirq)
313 return -EEXIST;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700314
315 ws->wakeirq = wakeirq;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200316 return 0;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700317}
318
319/**
320 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
321 * @dev: Device to handle
322 *
323 * Removes a device wakeirq from the wakeup source.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200324 *
325 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700326 */
327void device_wakeup_detach_irq(struct device *dev)
328{
329 struct wakeup_source *ws;
330
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700331 ws = dev->power.wakeup;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200332 if (ws)
333 ws->wakeirq = NULL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700334}
335
336/**
337 * device_wakeup_arm_wake_irqs(void)
338 *
339 * Itereates over the list of device wakeirqs to arm them.
340 */
341void device_wakeup_arm_wake_irqs(void)
342{
343 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +0200344 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700345
Thomas Gleixner54804372017-06-25 19:31:13 +0200346 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200347 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
348 dev_pm_arm_wake_irq(ws->wakeirq);
Thomas Gleixner54804372017-06-25 19:31:13 +0200349 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700350}
351
352/**
353 * device_wakeup_disarm_wake_irqs(void)
354 *
355 * Itereates over the list of device wakeirqs to disarm them.
356 */
357void device_wakeup_disarm_wake_irqs(void)
358{
359 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +0200360 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700361
Thomas Gleixner54804372017-06-25 19:31:13 +0200362 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200363 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
364 dev_pm_disarm_wake_irq(ws->wakeirq);
Thomas Gleixner54804372017-06-25 19:31:13 +0200365 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700366}
367
368/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200369 * device_wakeup_detach - Detach a device's wakeup source object from it.
370 * @dev: Device to detach the wakeup source object from.
371 *
372 * After it returns, @dev will not be treated as a wakeup device any more.
373 */
374static struct wakeup_source *device_wakeup_detach(struct device *dev)
375{
376 struct wakeup_source *ws;
377
378 spin_lock_irq(&dev->power.lock);
379 ws = dev->power.wakeup;
380 dev->power.wakeup = NULL;
381 spin_unlock_irq(&dev->power.lock);
382 return ws;
383}
384
385/**
386 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
387 * @dev: Device to handle.
388 *
389 * Detach the @dev's wakeup source object from it, unregister this wakeup source
390 * object and destroy it.
391 */
392int device_wakeup_disable(struct device *dev)
393{
394 struct wakeup_source *ws;
395
396 if (!dev || !dev->power.can_wakeup)
397 return -EINVAL;
398
399 ws = device_wakeup_detach(dev);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200400 wakeup_source_unregister(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200401 return 0;
402}
403EXPORT_SYMBOL_GPL(device_wakeup_disable);
404
405/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100406 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
407 * @dev: Device to handle.
408 * @capable: Whether or not @dev is capable of waking up the system from sleep.
409 *
410 * If @capable is set, set the @dev's power.can_wakeup flag and add its
411 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
412 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
413 *
414 * This function may sleep and it can't be called from any context where
415 * sleeping is not allowed.
416 */
417void device_set_wakeup_capable(struct device *dev, bool capable)
418{
419 if (!!dev->power.can_wakeup == !!capable)
420 return;
421
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200422 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100423 if (capable) {
424 if (wakeup_sysfs_add(dev))
425 return;
426 } else {
427 wakeup_sysfs_remove(dev);
428 }
429 }
430 dev->power.can_wakeup = capable;
431}
432EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
433
434/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200435 * device_init_wakeup - Device wakeup initialization.
436 * @dev: Device to handle.
437 * @enable: Whether or not to enable @dev as a wakeup device.
438 *
439 * By default, most devices should leave wakeup disabled. The exceptions are
440 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200441 * possibly network interfaces, etc. Also, devices that don't generate their
442 * own wakeup requests but merely forward requests from one bus to another
443 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200444 */
445int device_init_wakeup(struct device *dev, bool enable)
446{
447 int ret = 0;
448
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800449 if (!dev)
450 return -EINVAL;
451
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200452 if (enable) {
453 device_set_wakeup_capable(dev, true);
454 ret = device_wakeup_enable(dev);
455 } else {
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800456 if (dev->power.can_wakeup)
457 device_wakeup_disable(dev);
458
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200459 device_set_wakeup_capable(dev, false);
460 }
461
462 return ret;
463}
464EXPORT_SYMBOL_GPL(device_init_wakeup);
465
466/**
467 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
468 * @dev: Device to handle.
469 */
470int device_set_wakeup_enable(struct device *dev, bool enable)
471{
472 if (!dev || !dev->power.can_wakeup)
473 return -EINVAL;
474
475 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
476}
477EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200478
Jin Qianb6ec9452015-05-06 15:26:56 -0700479/**
480 * wakeup_source_not_registered - validate the given wakeup source.
481 * @ws: Wakeup source to be validated.
482 */
483static bool wakeup_source_not_registered(struct wakeup_source *ws)
484{
485 /*
486 * Use timer struct to check if the given source is initialized
487 * by wakeup_source_add.
488 */
489 return ws->timer.function != pm_wakeup_timer_fn ||
490 ws->timer.data != (unsigned long)ws;
491}
492
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200493/*
494 * The functions below use the observation that each wakeup event starts a
495 * period in which the system should not be suspended. The moment this period
496 * will end depends on how the wakeup event is going to be processed after being
497 * detected and all of the possible cases can be divided into two distinct
498 * groups.
499 *
500 * First, a wakeup event may be detected by the same functional unit that will
501 * carry out the entire processing of it and possibly will pass it to user space
502 * for further processing. In that case the functional unit that has detected
503 * the event may later "close" the "no suspend" period associated with it
504 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
505 * pm_relax(), balanced with each other, is supposed to be used in such
506 * situations.
507 *
508 * Second, a wakeup event may be detected by one functional unit and processed
509 * by another one. In that case the unit that has detected it cannot really
510 * "close" the "no suspend" period associated with it, unless it knows in
511 * advance what's going to happen to the event during processing. This
512 * knowledge, however, may not be available to it, so it can simply specify time
513 * to wait before the system can be suspended and pass it as the second
514 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200515 *
516 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
517 * "no suspend" period will be ended either by the pm_relax(), or by the timer
518 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200519 */
520
521/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200522 * wakup_source_activate - Mark given wakeup source as active.
523 * @ws: Wakeup source to handle.
524 *
525 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
526 * core of the event by incrementing the counter of of wakeup events being
527 * processed.
528 */
529static void wakeup_source_activate(struct wakeup_source *ws)
530{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200531 unsigned int cec;
532
Jin Qianb6ec9452015-05-06 15:26:56 -0700533 if (WARN_ONCE(wakeup_source_not_registered(ws),
534 "unregistered wakeup source\n"))
535 return;
536
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100537 /*
538 * active wakeup source should bring the system
539 * out of PM_SUSPEND_FREEZE state
540 */
541 freeze_wake();
542
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200543 ws->active = true;
544 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200545 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200546 if (ws->autosleep_enabled)
547 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200548
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100549 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200550 cec = atomic_inc_return(&combined_event_count);
551
552 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200553}
554
555/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200556 * wakeup_source_report_event - Report wakeup event using the given source.
557 * @ws: Wakeup source to report the event for.
558 */
559static void wakeup_source_report_event(struct wakeup_source *ws)
560{
561 ws->event_count++;
562 /* This is racy, but the counter is approximate anyway. */
563 if (events_check_enabled)
564 ws->wakeup_count++;
565
566 if (!ws->active)
567 wakeup_source_activate(ws);
568}
569
570/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200571 * __pm_stay_awake - Notify the PM core of a wakeup event.
572 * @ws: Wakeup source object associated with the source of the event.
573 *
574 * It is safe to call this function from interrupt context.
575 */
576void __pm_stay_awake(struct wakeup_source *ws)
577{
578 unsigned long flags;
579
580 if (!ws)
581 return;
582
583 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100584
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200585 wakeup_source_report_event(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100586 del_timer(&ws->timer);
587 ws->timer_expires = 0;
588
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200589 spin_unlock_irqrestore(&ws->lock, flags);
590}
591EXPORT_SYMBOL_GPL(__pm_stay_awake);
592
593/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200594 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
595 * @dev: Device the wakeup event is related to.
596 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200597 * Notify the PM core of a wakeup event (signaled by @dev) by calling
598 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200599 *
600 * Call this function after detecting of a wakeup event if pm_relax() is going
601 * to be called directly after processing the event (and possibly passing it to
602 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200603 */
604void pm_stay_awake(struct device *dev)
605{
606 unsigned long flags;
607
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200608 if (!dev)
609 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200610
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200611 spin_lock_irqsave(&dev->power.lock, flags);
612 __pm_stay_awake(dev->power.wakeup);
613 spin_unlock_irqrestore(&dev->power.lock, flags);
614}
615EXPORT_SYMBOL_GPL(pm_stay_awake);
616
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200617#ifdef CONFIG_PM_AUTOSLEEP
618static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
619{
620 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
621 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
622}
623#else
624static inline void update_prevent_sleep_time(struct wakeup_source *ws,
625 ktime_t now) {}
626#endif
627
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200628/**
629 * wakup_source_deactivate - Mark given wakeup source as inactive.
630 * @ws: Wakeup source to handle.
631 *
632 * Update the @ws' statistics and notify the PM core that the wakeup source has
633 * become inactive by decrementing the counter of wakeup events being processed
634 * and incrementing the counter of registered wakeup events.
635 */
636static void wakeup_source_deactivate(struct wakeup_source *ws)
637{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200638 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200639 ktime_t duration;
640 ktime_t now;
641
642 ws->relax_count++;
643 /*
644 * __pm_relax() may be called directly or from a timer function.
645 * If it is called directly right after the timer function has been
646 * started, but before the timer function calls __pm_relax(), it is
647 * possible that __pm_stay_awake() will be called in the meantime and
648 * will set ws->active. Then, ws->active may be cleared immediately
649 * by the __pm_relax() called from the timer function, but in such a
650 * case ws->relax_count will be different from ws->active_count.
651 */
652 if (ws->relax_count != ws->active_count) {
653 ws->relax_count--;
654 return;
655 }
656
657 ws->active = false;
658
659 now = ktime_get();
660 duration = ktime_sub(now, ws->last_time);
661 ws->total_time = ktime_add(ws->total_time, duration);
662 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
663 ws->max_time = duration;
664
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200665 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200666 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100667 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200668
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200669 if (ws->autosleep_enabled)
670 update_prevent_sleep_time(ws, now);
671
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200672 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100673 * Increment the counter of registered wakeup events and decrement the
674 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200675 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200676 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
677 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200678
679 split_counters(&cnt, &inpr);
680 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
681 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200682}
683
684/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200685 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
686 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200687 *
688 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200689 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200690 *
691 * It is safe to call it from interrupt context.
692 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200693void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200694{
695 unsigned long flags;
696
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200697 if (!ws)
698 return;
699
700 spin_lock_irqsave(&ws->lock, flags);
701 if (ws->active)
702 wakeup_source_deactivate(ws);
703 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200704}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200705EXPORT_SYMBOL_GPL(__pm_relax);
706
707/**
708 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
709 * @dev: Device that signaled the event.
710 *
711 * Execute __pm_relax() for the @dev's wakeup source object.
712 */
713void pm_relax(struct device *dev)
714{
715 unsigned long flags;
716
717 if (!dev)
718 return;
719
720 spin_lock_irqsave(&dev->power.lock, flags);
721 __pm_relax(dev->power.wakeup);
722 spin_unlock_irqrestore(&dev->power.lock, flags);
723}
724EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200725
726/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200727 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200728 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200729 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100730 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
731 * in @data if it is currently active and its timer has not been canceled and
732 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200733 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200734static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200735{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100736 struct wakeup_source *ws = (struct wakeup_source *)data;
737 unsigned long flags;
738
739 spin_lock_irqsave(&ws->lock, flags);
740
741 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200742 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100743 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200744 ws->expire_count++;
745 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100746
747 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200748}
749
750/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200751 * __pm_wakeup_event - Notify the PM core of a wakeup event.
752 * @ws: Wakeup source object associated with the event source.
753 * @msec: Anticipated event processing time (in milliseconds).
754 *
755 * Notify the PM core of a wakeup event whose source is @ws that will take
756 * approximately @msec milliseconds to be processed by the kernel. If @ws is
757 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
758 * execute pm_wakeup_timer_fn() in future.
759 *
760 * It is safe to call this function from interrupt context.
761 */
762void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
763{
764 unsigned long flags;
765 unsigned long expires;
766
767 if (!ws)
768 return;
769
770 spin_lock_irqsave(&ws->lock, flags);
771
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200772 wakeup_source_report_event(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200773
774 if (!msec) {
775 wakeup_source_deactivate(ws);
776 goto unlock;
777 }
778
779 expires = jiffies + msecs_to_jiffies(msec);
780 if (!expires)
781 expires = 1;
782
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100783 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200784 mod_timer(&ws->timer, expires);
785 ws->timer_expires = expires;
786 }
787
788 unlock:
789 spin_unlock_irqrestore(&ws->lock, flags);
790}
791EXPORT_SYMBOL_GPL(__pm_wakeup_event);
792
793
794/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200795 * pm_wakeup_event - Notify the PM core of a wakeup event.
796 * @dev: Device the wakeup event is related to.
797 * @msec: Anticipated event processing time (in milliseconds).
798 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200799 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200800 */
801void pm_wakeup_event(struct device *dev, unsigned int msec)
802{
803 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200804
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200805 if (!dev)
806 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200807
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200808 spin_lock_irqsave(&dev->power.lock, flags);
809 __pm_wakeup_event(dev->power.wakeup, msec);
810 spin_unlock_irqrestore(&dev->power.lock, flags);
811}
812EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200813
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700814void pm_get_active_wakeup_sources(char *pending_wakeup_source, size_t max)
815{
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700816 struct wakeup_source *ws, *last_active_ws = NULL;
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700817 int len = 0;
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700818 bool active = false;
819
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700820 rcu_read_lock();
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700821 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
Ruchi Kandoi78ab6c42015-11-10 10:53:55 -0800822 if (ws->active && len < max) {
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700823 if (!active)
824 len += scnprintf(pending_wakeup_source, max,
825 "Pending Wakeup Sources: ");
826 len += scnprintf(pending_wakeup_source + len, max - len,
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700827 "%s ", ws->name);
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700828 active = true;
829 } else if (!active &&
830 (!last_active_ws ||
831 ktime_to_ns(ws->last_time) >
832 ktime_to_ns(last_active_ws->last_time))) {
833 last_active_ws = ws;
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700834 }
835 }
Ruchi Kandoi68b62542015-04-08 15:42:29 -0700836 if (!active && last_active_ws) {
837 scnprintf(pending_wakeup_source, max,
838 "Last active Wakeup Source: %s",
839 last_active_ws->name);
840 }
Ruchi Kandoi6118cb42014-10-29 10:36:27 -0700841 rcu_read_unlock();
842}
843EXPORT_SYMBOL_GPL(pm_get_active_wakeup_sources);
844
Julius Wernerbb177fe2013-06-12 12:55:22 -0700845void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200846{
847 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +0200848 int srcuidx, active = 0;
Todd Poynora938da02012-08-12 00:17:02 +0200849 struct wakeup_source *last_activity_ws = NULL;
850
Thomas Gleixner54804372017-06-25 19:31:13 +0200851 srcuidx = srcu_read_lock(&wakeup_srcu);
Todd Poynora938da02012-08-12 00:17:02 +0200852 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
853 if (ws->active) {
854 pr_info("active wakeup source: %s\n", ws->name);
855 active = 1;
856 } else if (!active &&
857 (!last_activity_ws ||
858 ktime_to_ns(ws->last_time) >
859 ktime_to_ns(last_activity_ws->last_time))) {
860 last_activity_ws = ws;
861 }
862 }
863
864 if (!active && last_activity_ws)
865 pr_info("last active wakeup source: %s\n",
866 last_activity_ws->name);
Thomas Gleixner54804372017-06-25 19:31:13 +0200867 srcu_read_unlock(&wakeup_srcu, srcuidx);
Todd Poynora938da02012-08-12 00:17:02 +0200868}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700869EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200870
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200871/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100872 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200873 *
874 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100875 * value from the past and return true if new wakeup events have been registered
876 * since the old value was stored. Also return true if the current number of
877 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200878 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100879bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200880{
881 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100882 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200883
884 spin_lock_irqsave(&events_lock, flags);
885 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100886 unsigned int cnt, inpr;
887
888 split_counters(&cnt, &inpr);
889 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100890 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200891 }
892 spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200893
Bernie Thompson9350de062013-06-01 00:47:43 +0000894 if (ret) {
895 pr_info("PM: Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700896 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000897 }
Todd Poynora938da02012-08-12 00:17:02 +0200898
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200899 return ret || pm_abort_suspend;
900}
901
902void pm_system_wakeup(void)
903{
904 pm_abort_suspend = true;
905 freeze_wake();
906}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100907EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200908
909void pm_wakeup_clear(void)
910{
911 pm_abort_suspend = false;
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700912 pm_wakeup_irq = 0;
913}
914
915void pm_system_irq_wakeup(unsigned int irq_number)
916{
917 if (pm_wakeup_irq == 0) {
918 pm_wakeup_irq = irq_number;
919 pm_system_wakeup();
920 }
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200921}
922
923/**
924 * pm_get_wakeup_count - Read the number of registered wakeup events.
925 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200926 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200927 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200928 * Store the number of registered wakeup events at the address in @count. If
929 * @block is set, block until the current number of wakeup events being
930 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200931 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200932 * Return 'false' if the current number of wakeup events being processed is
933 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200934 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200935bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200936{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100937 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200938
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200939 if (block) {
940 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200941
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200942 for (;;) {
943 prepare_to_wait(&wakeup_count_wait_queue, &wait,
944 TASK_INTERRUPTIBLE);
945 split_counters(&cnt, &inpr);
946 if (inpr == 0 || signal_pending(current))
947 break;
948
949 schedule();
950 }
951 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200952 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200953
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100954 split_counters(&cnt, &inpr);
955 *count = cnt;
956 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200957}
958
959/**
960 * pm_save_wakeup_count - Save the current number of registered wakeup events.
961 * @count: Value to compare with the current number of registered wakeup events.
962 *
963 * If @count is equal to the current number of registered wakeup events and the
964 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100965 * old number of registered wakeup events for pm_check_wakeup_events(), enable
966 * wakeup events detection and return 'true'. Otherwise disable wakeup events
967 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200968 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200969bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200970{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100971 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200972 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200973
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100974 events_check_enabled = false;
John Stultz49550702012-09-06 23:19:06 +0200975 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100976 split_counters(&cnt, &inpr);
977 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200978 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200979 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200980 }
John Stultz49550702012-09-06 23:19:06 +0200981 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100982 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200983}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200984
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200985#ifdef CONFIG_PM_AUTOSLEEP
986/**
987 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
988 * @enabled: Whether to set or to clear the autosleep_enabled flags.
989 */
990void pm_wakep_autosleep_enabled(bool set)
991{
992 struct wakeup_source *ws;
993 ktime_t now = ktime_get();
Thomas Gleixner54804372017-06-25 19:31:13 +0200994 int srcuidx;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200995
Thomas Gleixner54804372017-06-25 19:31:13 +0200996 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200997 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
998 spin_lock_irq(&ws->lock);
999 if (ws->autosleep_enabled != set) {
1000 ws->autosleep_enabled = set;
1001 if (ws->active) {
1002 if (set)
1003 ws->start_prevent_time = now;
1004 else
1005 update_prevent_sleep_time(ws, now);
1006 }
1007 }
1008 spin_unlock_irq(&ws->lock);
1009 }
Thomas Gleixner54804372017-06-25 19:31:13 +02001010 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001011}
1012#endif /* CONFIG_PM_AUTOSLEEP */
1013
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001014static struct dentry *wakeup_sources_stats_dentry;
1015
1016/**
1017 * print_wakeup_source_stats - Print wakeup source statistics information.
1018 * @m: seq_file to print the statistics into.
1019 * @ws: Wakeup source object to print the statistics for.
1020 */
1021static int print_wakeup_source_stats(struct seq_file *m,
1022 struct wakeup_source *ws)
1023{
1024 unsigned long flags;
1025 ktime_t total_time;
1026 ktime_t max_time;
1027 unsigned long active_count;
1028 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001029 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001030
1031 spin_lock_irqsave(&ws->lock, flags);
1032
1033 total_time = ws->total_time;
1034 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001035 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001036 active_count = ws->active_count;
1037 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001038 ktime_t now = ktime_get();
1039
1040 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001041 total_time = ktime_add(total_time, active_time);
1042 if (active_time.tv64 > max_time.tv64)
1043 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001044
1045 if (ws->autosleep_enabled)
1046 prevent_sleep_time = ktime_add(prevent_sleep_time,
1047 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001048 } else {
1049 active_time = ktime_set(0, 0);
1050 }
1051
yangdongdong481aeb22015-08-08 11:59:59 +08001052 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 -07001053 ws->name, active_count, ws->event_count,
1054 ws->wakeup_count, ws->expire_count,
1055 ktime_to_ms(active_time), ktime_to_ms(total_time),
1056 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1057 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001058
1059 spin_unlock_irqrestore(&ws->lock, flags);
1060
Joe Perches9f6a2402015-04-15 16:17:48 -07001061 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001062}
1063
1064/**
1065 * wakeup_sources_stats_show - Print wakeup sources statistics information.
1066 * @m: seq_file to print the statistics into.
1067 */
1068static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
1069{
1070 struct wakeup_source *ws;
Thomas Gleixner54804372017-06-25 19:31:13 +02001071 int srcuidx;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001072
yangdongdong481aeb22015-08-08 11:59:59 +08001073 seq_puts(m, "name\t\t\t\t\tactive_count\tevent_count\twakeup_count\t"
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +02001074 "expire_count\tactive_since\ttotal_time\tmax_time\t"
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001075 "last_change\tprevent_suspend_time\n");
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001076
Thomas Gleixner54804372017-06-25 19:31:13 +02001077 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001078 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
1079 print_wakeup_source_stats(m, ws);
Thomas Gleixner54804372017-06-25 19:31:13 +02001080 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001081
Jin Qian7f436052015-05-15 18:10:37 -07001082 print_wakeup_source_stats(m, &deleted_ws);
1083
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001084 return 0;
1085}
1086
1087static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1088{
1089 return single_open(file, wakeup_sources_stats_show, NULL);
1090}
1091
1092static const struct file_operations wakeup_sources_stats_fops = {
1093 .owner = THIS_MODULE,
1094 .open = wakeup_sources_stats_open,
1095 .read = seq_read,
1096 .llseek = seq_lseek,
1097 .release = single_release,
1098};
1099
1100static int __init wakeup_sources_debugfs_init(void)
1101{
1102 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1103 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1104 return 0;
1105}
1106
1107postcore_initcall(wakeup_sources_debugfs_init);