blob: bb1ae175fae136a57cd85e7566af617706e29232 [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
Joe Perches7a5bd122019-03-04 09:14:38 -08009#define pr_fmt(fmt) "PM: " fmt
10
Rafael J. Wysockic125e962010-07-05 22:43:53 +020011#include <linux/device.h>
12#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
Rafael J. Wysockic125e962010-07-05 22:43:53 +020014#include <linux/capability.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040015#include <linux/export.h>
Rafael J. Wysockic125e962010-07-05 22:43:53 +020016#include <linux/suspend.h>
Rafael J. Wysocki9c034392010-10-19 23:42:49 +020017#include <linux/seq_file.h>
18#include <linux/debugfs.h>
Tony Lindgren4990d4f2015-05-18 15:40:29 -070019#include <linux/pm_wakeirq.h>
Arve Hjønnevåg6791e362012-04-29 22:53:02 +020020#include <trace/events/power.h>
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020021
22#include "power.h"
23
Ulf Hansson0026cef2018-01-11 09:18:59 +010024#ifndef CONFIG_SUSPEND
25suspend_state_t pm_suspend_target_state;
26#define pm_suspend_target_state (PM_SUSPEND_ON)
27#endif
28
Rafael J. Wysockic125e962010-07-05 22:43:53 +020029/*
30 * If set, the suspend/hibernate code will abort transitions to a sleep state
31 * if wakeup events are registered during or immediately before the transition.
32 */
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +020033bool events_check_enabled __read_mostly;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020034
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -070035/* First wakeup IRQ seen by the kernel in the last cycle. */
36unsigned int pm_wakeup_irq __read_mostly;
37
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +020038/* If greater than 0 and the system is suspending, terminate the suspend. */
39static atomic_t pm_abort_suspend __read_mostly;
Rafael J. Wysocki068765b2014-09-01 13:47:49 +020040
Rafael J. Wysocki023d3772011-01-31 11:06:39 +010041/*
42 * Combined counters of registered wakeup events and wakeup events in progress.
43 * They need to be modified together atomically, so it's better to use one
44 * atomic variable to hold them both.
45 */
46static atomic_t combined_event_count = ATOMIC_INIT(0);
47
48#define IN_PROGRESS_BITS (sizeof(int) * 4)
49#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
50
51static void split_counters(unsigned int *cnt, unsigned int *inpr)
52{
53 unsigned int comb = atomic_read(&combined_event_count);
54
55 *cnt = (comb >> IN_PROGRESS_BITS);
56 *inpr = comb & MAX_IN_PROGRESS;
57}
58
59/* A preserved old value of the events counter. */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020060static unsigned int saved_count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020061
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +020062static DEFINE_RAW_SPINLOCK(events_lock);
Rafael J. Wysockic125e962010-07-05 22:43:53 +020063
Kees Cook96428e92017-10-16 16:20:55 -070064static void pm_wakeup_timer_fn(struct timer_list *t);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +020065
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020066static LIST_HEAD(wakeup_sources);
67
Rafael J. Wysocki60af1062012-04-29 22:52:34 +020068static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
69
Thomas Gleixnerea0212f2017-06-25 19:31:13 +020070DEFINE_STATIC_SRCU(wakeup_srcu);
71
Jin Qian7f436052015-05-15 18:10:37 -070072static struct wakeup_source deleted_ws = {
73 .name = "deleted",
74 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
75};
76
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020077/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010078 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
79 * @ws: Wakeup source to prepare.
80 * @name: Pointer to the name of the new wakeup source.
81 *
82 * Callers must ensure that the @name string won't be freed when @ws is still in
83 * use.
84 */
85void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
86{
87 if (ws) {
88 memset(ws, 0, sizeof(*ws));
89 ws->name = name;
90 }
91}
92EXPORT_SYMBOL_GPL(wakeup_source_prepare);
93
94/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020095 * wakeup_source_create - Create a struct wakeup_source object.
96 * @name: Name of the new wakeup source.
97 */
98struct wakeup_source *wakeup_source_create(const char *name)
99{
100 struct wakeup_source *ws;
101
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100102 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200103 if (!ws)
104 return NULL;
105
Rasmus Villemoes72362142015-09-09 23:42:06 +0200106 wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200107 return ws;
108}
109EXPORT_SYMBOL_GPL(wakeup_source_create);
110
Jin Qian7f436052015-05-15 18:10:37 -0700111/*
112 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
113 */
114static void wakeup_source_record(struct wakeup_source *ws)
115{
116 unsigned long flags;
117
118 spin_lock_irqsave(&deleted_ws.lock, flags);
119
120 if (ws->event_count) {
121 deleted_ws.total_time =
122 ktime_add(deleted_ws.total_time, ws->total_time);
123 deleted_ws.prevent_sleep_time =
124 ktime_add(deleted_ws.prevent_sleep_time,
125 ws->prevent_sleep_time);
126 deleted_ws.max_time =
127 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
128 deleted_ws.max_time : ws->max_time;
129 deleted_ws.event_count += ws->event_count;
130 deleted_ws.active_count += ws->active_count;
131 deleted_ws.relax_count += ws->relax_count;
132 deleted_ws.expire_count += ws->expire_count;
133 deleted_ws.wakeup_count += ws->wakeup_count;
134 }
135
136 spin_unlock_irqrestore(&deleted_ws.lock, flags);
137}
138
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100139/**
140 * wakeup_source_destroy - Destroy a struct wakeup_source object.
141 * @ws: Wakeup source to destroy.
142 *
143 * Use only for wakeup source objects created with wakeup_source_create().
144 */
145void wakeup_source_destroy(struct wakeup_source *ws)
146{
147 if (!ws)
148 return;
149
Rafael J. Wysocki623217a2019-03-11 12:53:59 +0100150 __pm_relax(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700151 wakeup_source_record(ws);
Rasmus Villemoes72362142015-09-09 23:42:06 +0200152 kfree_const(ws->name);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200153 kfree(ws);
154}
155EXPORT_SYMBOL_GPL(wakeup_source_destroy);
156
157/**
158 * wakeup_source_add - Add given object to the list of wakeup sources.
159 * @ws: Wakeup source object to add to the list.
160 */
161void wakeup_source_add(struct wakeup_source *ws)
162{
John Stultz49550702012-09-06 23:19:06 +0200163 unsigned long flags;
164
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200165 if (WARN_ON(!ws))
166 return;
167
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100168 spin_lock_init(&ws->lock);
Kees Cook96428e92017-10-16 16:20:55 -0700169 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200170 ws->active = false;
171
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200172 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200173 list_add_rcu(&ws->entry, &wakeup_sources);
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200174 raw_spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200175}
176EXPORT_SYMBOL_GPL(wakeup_source_add);
177
178/**
179 * wakeup_source_remove - Remove given object from the wakeup sources list.
180 * @ws: Wakeup source object to remove from the list.
181 */
182void wakeup_source_remove(struct wakeup_source *ws)
183{
John Stultz49550702012-09-06 23:19:06 +0200184 unsigned long flags;
185
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200186 if (WARN_ON(!ws))
187 return;
188
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200189 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200190 list_del_rcu(&ws->entry);
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200191 raw_spin_unlock_irqrestore(&events_lock, flags);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200192 synchronize_srcu(&wakeup_srcu);
Viresh Kumar1fad17f2019-03-08 15:23:11 +0530193
194 del_timer_sync(&ws->timer);
195 /*
196 * Clear timer.function to make wakeup_source_not_registered() treat
197 * this wakeup source as not registered.
198 */
199 ws->timer.function = NULL;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200200}
201EXPORT_SYMBOL_GPL(wakeup_source_remove);
202
203/**
204 * wakeup_source_register - Create wakeup source and add it to the list.
205 * @name: Name of the wakeup source to register.
206 */
207struct wakeup_source *wakeup_source_register(const char *name)
208{
209 struct wakeup_source *ws;
210
211 ws = wakeup_source_create(name);
212 if (ws)
213 wakeup_source_add(ws);
214
215 return ws;
216}
217EXPORT_SYMBOL_GPL(wakeup_source_register);
218
219/**
220 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
221 * @ws: Wakeup source object to unregister.
222 */
223void wakeup_source_unregister(struct wakeup_source *ws)
224{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100225 if (ws) {
226 wakeup_source_remove(ws);
227 wakeup_source_destroy(ws);
228 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200229}
230EXPORT_SYMBOL_GPL(wakeup_source_unregister);
231
232/**
233 * device_wakeup_attach - Attach a wakeup source object to a device object.
234 * @dev: Device to handle.
235 * @ws: Wakeup source object to attach to @dev.
236 *
237 * This causes @dev to be treated as a wakeup device.
238 */
239static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
240{
241 spin_lock_irq(&dev->power.lock);
242 if (dev->power.wakeup) {
243 spin_unlock_irq(&dev->power.lock);
244 return -EEXIST;
245 }
246 dev->power.wakeup = ws;
Strashko, Grygorii16669be2016-04-06 14:45:53 +0300247 if (dev->power.wakeirq)
248 device_wakeup_attach_irq(dev, dev->power.wakeirq);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200249 spin_unlock_irq(&dev->power.lock);
250 return 0;
251}
252
253/**
254 * device_wakeup_enable - Enable given device to be a wakeup source.
255 * @dev: Device to handle.
256 *
257 * Create a wakeup source object, register it and attach it to @dev.
258 */
259int device_wakeup_enable(struct device *dev)
260{
261 struct wakeup_source *ws;
262 int ret;
263
264 if (!dev || !dev->power.can_wakeup)
265 return -EINVAL;
266
Ulf Hansson0026cef2018-01-11 09:18:59 +0100267 if (pm_suspend_target_state != PM_SUSPEND_ON)
268 dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
269
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200270 ws = wakeup_source_register(dev_name(dev));
271 if (!ws)
272 return -ENOMEM;
273
274 ret = device_wakeup_attach(dev, ws);
275 if (ret)
276 wakeup_source_unregister(ws);
277
278 return ret;
279}
280EXPORT_SYMBOL_GPL(device_wakeup_enable);
281
282/**
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700283 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
284 * @dev: Device to handle
285 * @wakeirq: Device specific wakeirq entry
286 *
287 * Attach a device wakeirq to the wakeup source so the device
288 * wake IRQ can be configured automatically for suspend and
289 * resume.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200290 *
291 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700292 */
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100293void device_wakeup_attach_irq(struct device *dev,
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700294 struct wake_irq *wakeirq)
295{
296 struct wakeup_source *ws;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700297
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700298 ws = dev->power.wakeup;
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100299 if (!ws)
300 return;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700301
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200302 if (ws->wakeirq)
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100303 dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700304
305 ws->wakeirq = wakeirq;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700306}
307
308/**
309 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
310 * @dev: Device to handle
311 *
312 * Removes a device wakeirq from the wakeup source.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200313 *
314 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700315 */
316void device_wakeup_detach_irq(struct device *dev)
317{
318 struct wakeup_source *ws;
319
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700320 ws = dev->power.wakeup;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200321 if (ws)
322 ws->wakeirq = NULL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700323}
324
325/**
326 * device_wakeup_arm_wake_irqs(void)
327 *
328 * Itereates over the list of device wakeirqs to arm them.
329 */
330void device_wakeup_arm_wake_irqs(void)
331{
332 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200333 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700334
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200335 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200336 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
337 dev_pm_arm_wake_irq(ws->wakeirq);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200338 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700339}
340
341/**
342 * device_wakeup_disarm_wake_irqs(void)
343 *
344 * Itereates over the list of device wakeirqs to disarm them.
345 */
346void device_wakeup_disarm_wake_irqs(void)
347{
348 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200349 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700350
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200351 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200352 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
353 dev_pm_disarm_wake_irq(ws->wakeirq);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200354 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700355}
356
357/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200358 * device_wakeup_detach - Detach a device's wakeup source object from it.
359 * @dev: Device to detach the wakeup source object from.
360 *
361 * After it returns, @dev will not be treated as a wakeup device any more.
362 */
363static struct wakeup_source *device_wakeup_detach(struct device *dev)
364{
365 struct wakeup_source *ws;
366
367 spin_lock_irq(&dev->power.lock);
368 ws = dev->power.wakeup;
369 dev->power.wakeup = NULL;
370 spin_unlock_irq(&dev->power.lock);
371 return ws;
372}
373
374/**
375 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
376 * @dev: Device to handle.
377 *
378 * Detach the @dev's wakeup source object from it, unregister this wakeup source
379 * object and destroy it.
380 */
381int device_wakeup_disable(struct device *dev)
382{
383 struct wakeup_source *ws;
384
385 if (!dev || !dev->power.can_wakeup)
386 return -EINVAL;
387
388 ws = device_wakeup_detach(dev);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200389 wakeup_source_unregister(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200390 return 0;
391}
392EXPORT_SYMBOL_GPL(device_wakeup_disable);
393
394/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100395 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
396 * @dev: Device to handle.
397 * @capable: Whether or not @dev is capable of waking up the system from sleep.
398 *
399 * If @capable is set, set the @dev's power.can_wakeup flag and add its
400 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
401 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
402 *
403 * This function may sleep and it can't be called from any context where
404 * sleeping is not allowed.
405 */
406void device_set_wakeup_capable(struct device *dev, bool capable)
407{
408 if (!!dev->power.can_wakeup == !!capable)
409 return;
410
Rafael J. Wysocki820b9b02017-08-02 01:32:44 +0200411 dev->power.can_wakeup = capable;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200412 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100413 if (capable) {
Rafael J. Wysocki820b9b02017-08-02 01:32:44 +0200414 int ret = wakeup_sysfs_add(dev);
415
416 if (ret)
417 dev_info(dev, "Wakeup sysfs attributes not added\n");
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100418 } else {
419 wakeup_sysfs_remove(dev);
420 }
421 }
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100422}
423EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
424
425/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200426 * device_init_wakeup - Device wakeup initialization.
427 * @dev: Device to handle.
428 * @enable: Whether or not to enable @dev as a wakeup device.
429 *
430 * By default, most devices should leave wakeup disabled. The exceptions are
431 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200432 * possibly network interfaces, etc. Also, devices that don't generate their
433 * own wakeup requests but merely forward requests from one bus to another
434 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200435 */
436int device_init_wakeup(struct device *dev, bool enable)
437{
438 int ret = 0;
439
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800440 if (!dev)
441 return -EINVAL;
442
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200443 if (enable) {
444 device_set_wakeup_capable(dev, true);
445 ret = device_wakeup_enable(dev);
446 } else {
Rafael J. Wysocki9dbc64a52018-01-02 01:42:56 +0100447 device_wakeup_disable(dev);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200448 device_set_wakeup_capable(dev, false);
449 }
450
451 return ret;
452}
453EXPORT_SYMBOL_GPL(device_init_wakeup);
454
455/**
456 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
457 * @dev: Device to handle.
458 */
459int device_set_wakeup_enable(struct device *dev, bool enable)
460{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200461 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
462}
463EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200464
Jin Qianb6ec9452015-05-06 15:26:56 -0700465/**
466 * wakeup_source_not_registered - validate the given wakeup source.
467 * @ws: Wakeup source to be validated.
468 */
469static bool wakeup_source_not_registered(struct wakeup_source *ws)
470{
471 /*
472 * Use timer struct to check if the given source is initialized
473 * by wakeup_source_add.
474 */
Kees Cook841b86f2017-10-23 09:40:42 +0200475 return ws->timer.function != pm_wakeup_timer_fn;
Jin Qianb6ec9452015-05-06 15:26:56 -0700476}
477
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200478/*
479 * The functions below use the observation that each wakeup event starts a
480 * period in which the system should not be suspended. The moment this period
481 * will end depends on how the wakeup event is going to be processed after being
482 * detected and all of the possible cases can be divided into two distinct
483 * groups.
484 *
485 * First, a wakeup event may be detected by the same functional unit that will
486 * carry out the entire processing of it and possibly will pass it to user space
487 * for further processing. In that case the functional unit that has detected
488 * the event may later "close" the "no suspend" period associated with it
489 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
490 * pm_relax(), balanced with each other, is supposed to be used in such
491 * situations.
492 *
493 * Second, a wakeup event may be detected by one functional unit and processed
494 * by another one. In that case the unit that has detected it cannot really
495 * "close" the "no suspend" period associated with it, unless it knows in
496 * advance what's going to happen to the event during processing. This
497 * knowledge, however, may not be available to it, so it can simply specify time
498 * to wait before the system can be suspended and pass it as the second
499 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200500 *
501 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
502 * "no suspend" period will be ended either by the pm_relax(), or by the timer
503 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200504 */
505
506/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200507 * wakup_source_activate - Mark given wakeup source as active.
508 * @ws: Wakeup source to handle.
509 *
510 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
511 * core of the event by incrementing the counter of of wakeup events being
512 * processed.
513 */
Rafael J. Wysocki60d45532017-05-14 02:23:04 +0200514static void wakeup_source_activate(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200515{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200516 unsigned int cec;
517
Jin Qianb6ec9452015-05-06 15:26:56 -0700518 if (WARN_ONCE(wakeup_source_not_registered(ws),
519 "unregistered wakeup source\n"))
520 return;
521
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200522 ws->active = true;
523 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200524 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200525 if (ws->autosleep_enabled)
526 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200527
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100528 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200529 cec = atomic_inc_return(&combined_event_count);
530
531 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200532}
533
534/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200535 * wakeup_source_report_event - Report wakeup event using the given source.
536 * @ws: Wakeup source to report the event for.
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200537 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200538 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200539static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200540{
541 ws->event_count++;
542 /* This is racy, but the counter is approximate anyway. */
543 if (events_check_enabled)
544 ws->wakeup_count++;
545
546 if (!ws->active)
Rafael J. Wysocki60d45532017-05-14 02:23:04 +0200547 wakeup_source_activate(ws);
548
549 if (hard)
550 pm_system_wakeup();
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200551}
552
553/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200554 * __pm_stay_awake - Notify the PM core of a wakeup event.
555 * @ws: Wakeup source object associated with the source of the event.
556 *
557 * It is safe to call this function from interrupt context.
558 */
559void __pm_stay_awake(struct wakeup_source *ws)
560{
561 unsigned long flags;
562
563 if (!ws)
564 return;
565
566 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100567
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200568 wakeup_source_report_event(ws, false);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100569 del_timer(&ws->timer);
570 ws->timer_expires = 0;
571
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200572 spin_unlock_irqrestore(&ws->lock, flags);
573}
574EXPORT_SYMBOL_GPL(__pm_stay_awake);
575
576/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200577 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
578 * @dev: Device the wakeup event is related to.
579 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200580 * Notify the PM core of a wakeup event (signaled by @dev) by calling
581 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200582 *
583 * Call this function after detecting of a wakeup event if pm_relax() is going
584 * to be called directly after processing the event (and possibly passing it to
585 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 */
587void pm_stay_awake(struct device *dev)
588{
589 unsigned long flags;
590
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200591 if (!dev)
592 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200593
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200594 spin_lock_irqsave(&dev->power.lock, flags);
595 __pm_stay_awake(dev->power.wakeup);
596 spin_unlock_irqrestore(&dev->power.lock, flags);
597}
598EXPORT_SYMBOL_GPL(pm_stay_awake);
599
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200600#ifdef CONFIG_PM_AUTOSLEEP
601static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
602{
603 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
604 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
605}
606#else
607static inline void update_prevent_sleep_time(struct wakeup_source *ws,
608 ktime_t now) {}
609#endif
610
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200611/**
612 * wakup_source_deactivate - Mark given wakeup source as inactive.
613 * @ws: Wakeup source to handle.
614 *
615 * Update the @ws' statistics and notify the PM core that the wakeup source has
616 * become inactive by decrementing the counter of wakeup events being processed
617 * and incrementing the counter of registered wakeup events.
618 */
619static void wakeup_source_deactivate(struct wakeup_source *ws)
620{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200621 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200622 ktime_t duration;
623 ktime_t now;
624
625 ws->relax_count++;
626 /*
627 * __pm_relax() may be called directly or from a timer function.
628 * If it is called directly right after the timer function has been
629 * started, but before the timer function calls __pm_relax(), it is
630 * possible that __pm_stay_awake() will be called in the meantime and
631 * will set ws->active. Then, ws->active may be cleared immediately
632 * by the __pm_relax() called from the timer function, but in such a
633 * case ws->relax_count will be different from ws->active_count.
634 */
635 if (ws->relax_count != ws->active_count) {
636 ws->relax_count--;
637 return;
638 }
639
640 ws->active = false;
641
642 now = ktime_get();
643 duration = ktime_sub(now, ws->last_time);
644 ws->total_time = ktime_add(ws->total_time, duration);
645 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
646 ws->max_time = duration;
647
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200648 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200649 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100650 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200651
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200652 if (ws->autosleep_enabled)
653 update_prevent_sleep_time(ws, now);
654
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200655 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100656 * Increment the counter of registered wakeup events and decrement the
657 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200658 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200659 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
660 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200661
662 split_counters(&cnt, &inpr);
663 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
664 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200665}
666
667/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200668 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
669 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200670 *
671 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200672 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200673 *
674 * It is safe to call it from interrupt context.
675 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200676void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200677{
678 unsigned long flags;
679
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200680 if (!ws)
681 return;
682
683 spin_lock_irqsave(&ws->lock, flags);
684 if (ws->active)
685 wakeup_source_deactivate(ws);
686 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200687}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200688EXPORT_SYMBOL_GPL(__pm_relax);
689
690/**
691 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
692 * @dev: Device that signaled the event.
693 *
694 * Execute __pm_relax() for the @dev's wakeup source object.
695 */
696void pm_relax(struct device *dev)
697{
698 unsigned long flags;
699
700 if (!dev)
701 return;
702
703 spin_lock_irqsave(&dev->power.lock, flags);
704 __pm_relax(dev->power.wakeup);
705 spin_unlock_irqrestore(&dev->power.lock, flags);
706}
707EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200708
709/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200710 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200711 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200712 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100713 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
714 * in @data if it is currently active and its timer has not been canceled and
715 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200716 */
Kees Cook96428e92017-10-16 16:20:55 -0700717static void pm_wakeup_timer_fn(struct timer_list *t)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200718{
Kees Cook96428e92017-10-16 16:20:55 -0700719 struct wakeup_source *ws = from_timer(ws, t, timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100720 unsigned long flags;
721
722 spin_lock_irqsave(&ws->lock, flags);
723
724 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200725 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100726 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200727 ws->expire_count++;
728 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100729
730 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200731}
732
733/**
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200734 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200735 * @ws: Wakeup source object associated with the event source.
736 * @msec: Anticipated event processing time (in milliseconds).
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200737 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200738 *
739 * Notify the PM core of a wakeup event whose source is @ws that will take
740 * approximately @msec milliseconds to be processed by the kernel. If @ws is
741 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
742 * execute pm_wakeup_timer_fn() in future.
743 *
744 * It is safe to call this function from interrupt context.
745 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200746void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200747{
748 unsigned long flags;
749 unsigned long expires;
750
751 if (!ws)
752 return;
753
754 spin_lock_irqsave(&ws->lock, flags);
755
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200756 wakeup_source_report_event(ws, hard);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200757
758 if (!msec) {
759 wakeup_source_deactivate(ws);
760 goto unlock;
761 }
762
763 expires = jiffies + msecs_to_jiffies(msec);
764 if (!expires)
765 expires = 1;
766
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100767 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200768 mod_timer(&ws->timer, expires);
769 ws->timer_expires = expires;
770 }
771
772 unlock:
773 spin_unlock_irqrestore(&ws->lock, flags);
774}
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200775EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200776
777/**
Yangtao Lid1c6b412019-01-23 11:22:01 -0500778 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200779 * @dev: Device the wakeup event is related to.
780 * @msec: Anticipated event processing time (in milliseconds).
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200781 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200782 *
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200783 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200784 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200785void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200786{
787 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200788
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200789 if (!dev)
790 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200791
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200792 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200793 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200794 spin_unlock_irqrestore(&dev->power.lock, flags);
795}
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200796EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200797
Julius Wernerbb177fe2013-06-12 12:55:22 -0700798void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200799{
800 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200801 int srcuidx, active = 0;
Todd Poynora938da02012-08-12 00:17:02 +0200802 struct wakeup_source *last_activity_ws = NULL;
803
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200804 srcuidx = srcu_read_lock(&wakeup_srcu);
Todd Poynora938da02012-08-12 00:17:02 +0200805 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
806 if (ws->active) {
xing wei9320f952016-12-07 19:31:16 +0800807 pr_debug("active wakeup source: %s\n", ws->name);
Todd Poynora938da02012-08-12 00:17:02 +0200808 active = 1;
809 } else if (!active &&
810 (!last_activity_ws ||
811 ktime_to_ns(ws->last_time) >
812 ktime_to_ns(last_activity_ws->last_time))) {
813 last_activity_ws = ws;
814 }
815 }
816
817 if (!active && last_activity_ws)
xing wei9320f952016-12-07 19:31:16 +0800818 pr_debug("last active wakeup source: %s\n",
Todd Poynora938da02012-08-12 00:17:02 +0200819 last_activity_ws->name);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200820 srcu_read_unlock(&wakeup_srcu, srcuidx);
Todd Poynora938da02012-08-12 00:17:02 +0200821}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700822EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200823
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200824/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100825 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200826 *
827 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100828 * value from the past and return true if new wakeup events have been registered
829 * since the old value was stored. Also return true if the current number of
830 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200831 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100832bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200833{
834 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100835 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200836
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200837 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200838 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100839 unsigned int cnt, inpr;
840
841 split_counters(&cnt, &inpr);
842 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100843 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200844 }
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200845 raw_spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200846
Bernie Thompson9350de062013-06-01 00:47:43 +0000847 if (ret) {
Joe Perches7a5bd122019-03-04 09:14:38 -0800848 pr_debug("Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700849 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000850 }
Todd Poynora938da02012-08-12 00:17:02 +0200851
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200852 return ret || atomic_read(&pm_abort_suspend) > 0;
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200853}
854
855void pm_system_wakeup(void)
856{
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200857 atomic_inc(&pm_abort_suspend);
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200858 s2idle_wake();
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200859}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100860EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200861
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200862void pm_system_cancel_wakeup(void)
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200863{
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200864 atomic_dec(&pm_abort_suspend);
865}
866
867void pm_wakeup_clear(bool reset)
868{
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700869 pm_wakeup_irq = 0;
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200870 if (reset)
871 atomic_set(&pm_abort_suspend, 0);
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700872}
873
874void pm_system_irq_wakeup(unsigned int irq_number)
875{
876 if (pm_wakeup_irq == 0) {
877 pm_wakeup_irq = irq_number;
878 pm_system_wakeup();
879 }
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200880}
881
882/**
883 * pm_get_wakeup_count - Read the number of registered wakeup events.
884 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200885 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200886 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200887 * Store the number of registered wakeup events at the address in @count. If
888 * @block is set, block until the current number of wakeup events being
889 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200890 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200891 * Return 'false' if the current number of wakeup events being processed is
892 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200893 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200894bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200895{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100896 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200897
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200898 if (block) {
899 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200900
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200901 for (;;) {
902 prepare_to_wait(&wakeup_count_wait_queue, &wait,
903 TASK_INTERRUPTIBLE);
904 split_counters(&cnt, &inpr);
905 if (inpr == 0 || signal_pending(current))
906 break;
xing wei9320f952016-12-07 19:31:16 +0800907 pm_print_active_wakeup_sources();
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200908 schedule();
909 }
910 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200911 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200912
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100913 split_counters(&cnt, &inpr);
914 *count = cnt;
915 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200916}
917
918/**
919 * pm_save_wakeup_count - Save the current number of registered wakeup events.
920 * @count: Value to compare with the current number of registered wakeup events.
921 *
922 * If @count is equal to the current number of registered wakeup events and the
923 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100924 * old number of registered wakeup events for pm_check_wakeup_events(), enable
925 * wakeup events detection and return 'true'. Otherwise disable wakeup events
926 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200927 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200928bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200929{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100930 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200931 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200932
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100933 events_check_enabled = false;
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200934 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100935 split_counters(&cnt, &inpr);
936 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200937 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200938 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200939 }
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200940 raw_spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100941 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200942}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200943
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200944#ifdef CONFIG_PM_AUTOSLEEP
945/**
946 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
947 * @enabled: Whether to set or to clear the autosleep_enabled flags.
948 */
949void pm_wakep_autosleep_enabled(bool set)
950{
951 struct wakeup_source *ws;
952 ktime_t now = ktime_get();
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200953 int srcuidx;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200954
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200955 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200956 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
957 spin_lock_irq(&ws->lock);
958 if (ws->autosleep_enabled != set) {
959 ws->autosleep_enabled = set;
960 if (ws->active) {
961 if (set)
962 ws->start_prevent_time = now;
963 else
964 update_prevent_sleep_time(ws, now);
965 }
966 }
967 spin_unlock_irq(&ws->lock);
968 }
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200969 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200970}
971#endif /* CONFIG_PM_AUTOSLEEP */
972
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200973static struct dentry *wakeup_sources_stats_dentry;
974
975/**
976 * print_wakeup_source_stats - Print wakeup source statistics information.
977 * @m: seq_file to print the statistics into.
978 * @ws: Wakeup source object to print the statistics for.
979 */
980static int print_wakeup_source_stats(struct seq_file *m,
981 struct wakeup_source *ws)
982{
983 unsigned long flags;
984 ktime_t total_time;
985 ktime_t max_time;
986 unsigned long active_count;
987 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200988 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200989
990 spin_lock_irqsave(&ws->lock, flags);
991
992 total_time = ws->total_time;
993 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200994 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200995 active_count = ws->active_count;
996 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200997 ktime_t now = ktime_get();
998
999 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001000 total_time = ktime_add(total_time, active_time);
Thomas Gleixner2456e852016-12-25 11:38:40 +01001001 if (active_time > max_time)
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001002 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001003
1004 if (ws->autosleep_enabled)
1005 prevent_sleep_time = ktime_add(prevent_sleep_time,
1006 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001007 } else {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001008 active_time = 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001009 }
1010
Joe Perches9f6a2402015-04-15 16:17:48 -07001011 seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
1012 ws->name, active_count, ws->event_count,
1013 ws->wakeup_count, ws->expire_count,
1014 ktime_to_ms(active_time), ktime_to_ms(total_time),
1015 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1016 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001017
1018 spin_unlock_irqrestore(&ws->lock, flags);
1019
Joe Perches9f6a2402015-04-15 16:17:48 -07001020 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001021}
1022
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001023static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1024 loff_t *pos)
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001025{
1026 struct wakeup_source *ws;
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001027 loff_t n = *pos;
1028 int *srcuidx = m->private;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001029
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001030 if (n == 0) {
1031 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1032 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1033 "last_change\tprevent_suspend_time\n");
1034 }
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001035
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001036 *srcuidx = srcu_read_lock(&wakeup_srcu);
1037 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
1038 if (n-- <= 0)
1039 return ws;
1040 }
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001041
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001042 return NULL;
1043}
1044
1045static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1046 void *v, loff_t *pos)
1047{
1048 struct wakeup_source *ws = v;
1049 struct wakeup_source *next_ws = NULL;
1050
1051 ++(*pos);
1052
1053 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1054 next_ws = ws;
1055 break;
1056 }
1057
1058 return next_ws;
1059}
1060
1061static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1062{
1063 int *srcuidx = m->private;
1064
1065 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1066}
1067
1068/**
1069 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1070 * @m: seq_file to print the statistics into.
1071 * @v: wakeup_source of each iteration
1072 */
1073static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1074{
1075 struct wakeup_source *ws = v;
1076
1077 print_wakeup_source_stats(m, ws);
Jin Qian7f436052015-05-15 18:10:37 -07001078
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001079 return 0;
1080}
1081
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001082static const struct seq_operations wakeup_sources_stats_seq_ops = {
1083 .start = wakeup_sources_stats_seq_start,
1084 .next = wakeup_sources_stats_seq_next,
1085 .stop = wakeup_sources_stats_seq_stop,
1086 .show = wakeup_sources_stats_seq_show,
1087};
1088
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001089static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1090{
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001091 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001092}
1093
1094static const struct file_operations wakeup_sources_stats_fops = {
1095 .owner = THIS_MODULE,
1096 .open = wakeup_sources_stats_open,
1097 .read = seq_read,
1098 .llseek = seq_lseek,
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001099 .release = seq_release_private,
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001100};
1101
1102static int __init wakeup_sources_debugfs_init(void)
1103{
1104 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1105 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1106 return 0;
1107}
1108
1109postcore_initcall(wakeup_sources_debugfs_init);