blob: 5872705432f8abda47243cf70b8af4d7c7281ebd [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010011#include <linux/sched/signal.h>
Rafael J. Wysockic125e962010-07-05 22:43:53 +020012#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>
Arve Hjønnevåg6791e362012-04-29 22:53:02 +020018#include <trace/events/power.h>
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020019
20#include "power.h"
21
Ulf Hansson0026cef2018-01-11 09:18:59 +010022#ifndef CONFIG_SUSPEND
23suspend_state_t pm_suspend_target_state;
24#define pm_suspend_target_state (PM_SUSPEND_ON)
25#endif
26
Rafael J. Wysockic125e962010-07-05 22:43:53 +020027/*
28 * If set, the suspend/hibernate code will abort transitions to a sleep state
29 * if wakeup events are registered during or immediately before the transition.
30 */
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +020031bool events_check_enabled __read_mostly;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020032
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -070033/* First wakeup IRQ seen by the kernel in the last cycle. */
34unsigned int pm_wakeup_irq __read_mostly;
35
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +020036/* If greater than 0 and the system is suspending, terminate the suspend. */
37static atomic_t pm_abort_suspend __read_mostly;
Rafael J. Wysocki068765b2014-09-01 13:47:49 +020038
Rafael J. Wysocki023d3772011-01-31 11:06:39 +010039/*
40 * Combined counters of registered wakeup events and wakeup events in progress.
41 * They need to be modified together atomically, so it's better to use one
42 * atomic variable to hold them both.
43 */
44static atomic_t combined_event_count = ATOMIC_INIT(0);
45
46#define IN_PROGRESS_BITS (sizeof(int) * 4)
47#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
48
49static void split_counters(unsigned int *cnt, unsigned int *inpr)
50{
51 unsigned int comb = atomic_read(&combined_event_count);
52
53 *cnt = (comb >> IN_PROGRESS_BITS);
54 *inpr = comb & MAX_IN_PROGRESS;
55}
56
57/* A preserved old value of the events counter. */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020058static unsigned int saved_count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020059
60static DEFINE_SPINLOCK(events_lock);
61
Kees Cook96428e92017-10-16 16:20:55 -070062static void pm_wakeup_timer_fn(struct timer_list *t);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +020063
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020064static LIST_HEAD(wakeup_sources);
65
Rafael J. Wysocki60af1062012-04-29 22:52:34 +020066static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
67
Thomas Gleixnerea0212f2017-06-25 19:31:13 +020068DEFINE_STATIC_SRCU(wakeup_srcu);
69
Jin Qian7f436052015-05-15 18:10:37 -070070static struct wakeup_source deleted_ws = {
71 .name = "deleted",
72 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
73};
74
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020075/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010076 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
77 * @ws: Wakeup source to prepare.
78 * @name: Pointer to the name of the new wakeup source.
79 *
80 * Callers must ensure that the @name string won't be freed when @ws is still in
81 * use.
82 */
83void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
84{
85 if (ws) {
86 memset(ws, 0, sizeof(*ws));
87 ws->name = name;
88 }
89}
90EXPORT_SYMBOL_GPL(wakeup_source_prepare);
91
92/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020093 * wakeup_source_create - Create a struct wakeup_source object.
94 * @name: Name of the new wakeup source.
95 */
96struct wakeup_source *wakeup_source_create(const char *name)
97{
98 struct wakeup_source *ws;
99
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100100 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200101 if (!ws)
102 return NULL;
103
Rasmus Villemoes72362142015-09-09 23:42:06 +0200104 wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200105 return ws;
106}
107EXPORT_SYMBOL_GPL(wakeup_source_create);
108
109/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100110 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
111 * @ws: Wakeup source to prepare for destruction.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100112 *
113 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
114 * be run in parallel with this function for the same wakeup source object.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200115 */
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100116void wakeup_source_drop(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200117{
118 if (!ws)
119 return;
120
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100121 del_timer_sync(&ws->timer);
122 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100123}
124EXPORT_SYMBOL_GPL(wakeup_source_drop);
125
Jin Qian7f436052015-05-15 18:10:37 -0700126/*
127 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
128 */
129static void wakeup_source_record(struct wakeup_source *ws)
130{
131 unsigned long flags;
132
133 spin_lock_irqsave(&deleted_ws.lock, flags);
134
135 if (ws->event_count) {
136 deleted_ws.total_time =
137 ktime_add(deleted_ws.total_time, ws->total_time);
138 deleted_ws.prevent_sleep_time =
139 ktime_add(deleted_ws.prevent_sleep_time,
140 ws->prevent_sleep_time);
141 deleted_ws.max_time =
142 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
143 deleted_ws.max_time : ws->max_time;
144 deleted_ws.event_count += ws->event_count;
145 deleted_ws.active_count += ws->active_count;
146 deleted_ws.relax_count += ws->relax_count;
147 deleted_ws.expire_count += ws->expire_count;
148 deleted_ws.wakeup_count += ws->wakeup_count;
149 }
150
151 spin_unlock_irqrestore(&deleted_ws.lock, flags);
152}
153
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100154/**
155 * wakeup_source_destroy - Destroy a struct wakeup_source object.
156 * @ws: Wakeup source to destroy.
157 *
158 * Use only for wakeup source objects created with wakeup_source_create().
159 */
160void wakeup_source_destroy(struct wakeup_source *ws)
161{
162 if (!ws)
163 return;
164
165 wakeup_source_drop(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700166 wakeup_source_record(ws);
Rasmus Villemoes72362142015-09-09 23:42:06 +0200167 kfree_const(ws->name);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200168 kfree(ws);
169}
170EXPORT_SYMBOL_GPL(wakeup_source_destroy);
171
172/**
173 * wakeup_source_add - Add given object to the list of wakeup sources.
174 * @ws: Wakeup source object to add to the list.
175 */
176void wakeup_source_add(struct wakeup_source *ws)
177{
John Stultz49550702012-09-06 23:19:06 +0200178 unsigned long flags;
179
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200180 if (WARN_ON(!ws))
181 return;
182
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100183 spin_lock_init(&ws->lock);
Kees Cook96428e92017-10-16 16:20:55 -0700184 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200185 ws->active = false;
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200186 ws->last_time = ktime_get();
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200187
John Stultz49550702012-09-06 23:19:06 +0200188 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200189 list_add_rcu(&ws->entry, &wakeup_sources);
John Stultz49550702012-09-06 23:19:06 +0200190 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200191}
192EXPORT_SYMBOL_GPL(wakeup_source_add);
193
194/**
195 * wakeup_source_remove - Remove given object from the wakeup sources list.
196 * @ws: Wakeup source object to remove from the list.
197 */
198void wakeup_source_remove(struct wakeup_source *ws)
199{
John Stultz49550702012-09-06 23:19:06 +0200200 unsigned long flags;
201
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200202 if (WARN_ON(!ws))
203 return;
204
John Stultz49550702012-09-06 23:19:06 +0200205 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200206 list_del_rcu(&ws->entry);
John Stultz49550702012-09-06 23:19:06 +0200207 spin_unlock_irqrestore(&events_lock, flags);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200208 synchronize_srcu(&wakeup_srcu);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200209}
210EXPORT_SYMBOL_GPL(wakeup_source_remove);
211
212/**
213 * wakeup_source_register - Create wakeup source and add it to the list.
214 * @name: Name of the wakeup source to register.
215 */
216struct wakeup_source *wakeup_source_register(const char *name)
217{
218 struct wakeup_source *ws;
219
220 ws = wakeup_source_create(name);
221 if (ws)
222 wakeup_source_add(ws);
223
224 return ws;
225}
226EXPORT_SYMBOL_GPL(wakeup_source_register);
227
228/**
229 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
230 * @ws: Wakeup source object to unregister.
231 */
232void wakeup_source_unregister(struct wakeup_source *ws)
233{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100234 if (ws) {
235 wakeup_source_remove(ws);
236 wakeup_source_destroy(ws);
237 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200238}
239EXPORT_SYMBOL_GPL(wakeup_source_unregister);
240
241/**
242 * device_wakeup_attach - Attach a wakeup source object to a device object.
243 * @dev: Device to handle.
244 * @ws: Wakeup source object to attach to @dev.
245 *
246 * This causes @dev to be treated as a wakeup device.
247 */
248static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
249{
250 spin_lock_irq(&dev->power.lock);
251 if (dev->power.wakeup) {
252 spin_unlock_irq(&dev->power.lock);
253 return -EEXIST;
254 }
255 dev->power.wakeup = ws;
Strashko, Grygorii16669be2016-04-06 14:45:53 +0300256 if (dev->power.wakeirq)
257 device_wakeup_attach_irq(dev, dev->power.wakeirq);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200258 spin_unlock_irq(&dev->power.lock);
259 return 0;
260}
261
262/**
263 * device_wakeup_enable - Enable given device to be a wakeup source.
264 * @dev: Device to handle.
265 *
266 * Create a wakeup source object, register it and attach it to @dev.
267 */
268int device_wakeup_enable(struct device *dev)
269{
270 struct wakeup_source *ws;
271 int ret;
272
273 if (!dev || !dev->power.can_wakeup)
274 return -EINVAL;
275
Ulf Hansson0026cef2018-01-11 09:18:59 +0100276 if (pm_suspend_target_state != PM_SUSPEND_ON)
277 dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
278
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200279 ws = wakeup_source_register(dev_name(dev));
280 if (!ws)
281 return -ENOMEM;
282
283 ret = device_wakeup_attach(dev, ws);
284 if (ret)
285 wakeup_source_unregister(ws);
286
287 return ret;
288}
289EXPORT_SYMBOL_GPL(device_wakeup_enable);
290
291/**
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700292 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
293 * @dev: Device to handle
294 * @wakeirq: Device specific wakeirq entry
295 *
296 * Attach a device wakeirq to the wakeup source so the device
297 * wake IRQ can be configured automatically for suspend and
298 * resume.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200299 *
300 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700301 */
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100302void device_wakeup_attach_irq(struct device *dev,
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700303 struct wake_irq *wakeirq)
304{
305 struct wakeup_source *ws;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700306
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700307 ws = dev->power.wakeup;
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100308 if (!ws)
309 return;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700310
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200311 if (ws->wakeirq)
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100312 dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700313
314 ws->wakeirq = wakeirq;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700315}
316
317/**
318 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
319 * @dev: Device to handle
320 *
321 * Removes a device wakeirq from the wakeup source.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200322 *
323 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700324 */
325void device_wakeup_detach_irq(struct device *dev)
326{
327 struct wakeup_source *ws;
328
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700329 ws = dev->power.wakeup;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200330 if (ws)
331 ws->wakeirq = NULL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700332}
333
334/**
335 * device_wakeup_arm_wake_irqs(void)
336 *
337 * Itereates over the list of device wakeirqs to arm them.
338 */
339void device_wakeup_arm_wake_irqs(void)
340{
341 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200342 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700343
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200344 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200345 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
346 dev_pm_arm_wake_irq(ws->wakeirq);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200347 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700348}
349
350/**
351 * device_wakeup_disarm_wake_irqs(void)
352 *
353 * Itereates over the list of device wakeirqs to disarm them.
354 */
355void device_wakeup_disarm_wake_irqs(void)
356{
357 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200358 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700359
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200360 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200361 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
362 dev_pm_disarm_wake_irq(ws->wakeirq);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200363 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700364}
365
366/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200367 * device_wakeup_detach - Detach a device's wakeup source object from it.
368 * @dev: Device to detach the wakeup source object from.
369 *
370 * After it returns, @dev will not be treated as a wakeup device any more.
371 */
372static struct wakeup_source *device_wakeup_detach(struct device *dev)
373{
374 struct wakeup_source *ws;
375
376 spin_lock_irq(&dev->power.lock);
377 ws = dev->power.wakeup;
378 dev->power.wakeup = NULL;
379 spin_unlock_irq(&dev->power.lock);
380 return ws;
381}
382
383/**
384 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
385 * @dev: Device to handle.
386 *
387 * Detach the @dev's wakeup source object from it, unregister this wakeup source
388 * object and destroy it.
389 */
390int device_wakeup_disable(struct device *dev)
391{
392 struct wakeup_source *ws;
393
394 if (!dev || !dev->power.can_wakeup)
395 return -EINVAL;
396
397 ws = device_wakeup_detach(dev);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200398 wakeup_source_unregister(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200399 return 0;
400}
401EXPORT_SYMBOL_GPL(device_wakeup_disable);
402
403/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100404 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
405 * @dev: Device to handle.
406 * @capable: Whether or not @dev is capable of waking up the system from sleep.
407 *
408 * If @capable is set, set the @dev's power.can_wakeup flag and add its
409 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
410 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
411 *
412 * This function may sleep and it can't be called from any context where
413 * sleeping is not allowed.
414 */
415void device_set_wakeup_capable(struct device *dev, bool capable)
416{
417 if (!!dev->power.can_wakeup == !!capable)
418 return;
419
Rafael J. Wysocki820b9b02017-08-02 01:32:44 +0200420 dev->power.can_wakeup = capable;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200421 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100422 if (capable) {
Rafael J. Wysocki820b9b02017-08-02 01:32:44 +0200423 int ret = wakeup_sysfs_add(dev);
424
425 if (ret)
426 dev_info(dev, "Wakeup sysfs attributes not added\n");
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100427 } else {
428 wakeup_sysfs_remove(dev);
429 }
430 }
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100431}
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 {
Rafael J. Wysocki9dbc64a52018-01-02 01:42:56 +0100456 device_wakeup_disable(dev);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200457 device_set_wakeup_capable(dev, false);
458 }
459
460 return ret;
461}
462EXPORT_SYMBOL_GPL(device_init_wakeup);
463
464/**
465 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
466 * @dev: Device to handle.
467 */
468int device_set_wakeup_enable(struct device *dev, bool enable)
469{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200470 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
471}
472EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200473
Jin Qianb6ec9452015-05-06 15:26:56 -0700474/**
475 * wakeup_source_not_registered - validate the given wakeup source.
476 * @ws: Wakeup source to be validated.
477 */
478static bool wakeup_source_not_registered(struct wakeup_source *ws)
479{
480 /*
481 * Use timer struct to check if the given source is initialized
482 * by wakeup_source_add.
483 */
Kees Cook841b86f2017-10-23 09:40:42 +0200484 return ws->timer.function != pm_wakeup_timer_fn;
Jin Qianb6ec9452015-05-06 15:26:56 -0700485}
486
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200487/*
488 * The functions below use the observation that each wakeup event starts a
489 * period in which the system should not be suspended. The moment this period
490 * will end depends on how the wakeup event is going to be processed after being
491 * detected and all of the possible cases can be divided into two distinct
492 * groups.
493 *
494 * First, a wakeup event may be detected by the same functional unit that will
495 * carry out the entire processing of it and possibly will pass it to user space
496 * for further processing. In that case the functional unit that has detected
497 * the event may later "close" the "no suspend" period associated with it
498 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
499 * pm_relax(), balanced with each other, is supposed to be used in such
500 * situations.
501 *
502 * Second, a wakeup event may be detected by one functional unit and processed
503 * by another one. In that case the unit that has detected it cannot really
504 * "close" the "no suspend" period associated with it, unless it knows in
505 * advance what's going to happen to the event during processing. This
506 * knowledge, however, may not be available to it, so it can simply specify time
507 * to wait before the system can be suspended and pass it as the second
508 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200509 *
510 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
511 * "no suspend" period will be ended either by the pm_relax(), or by the timer
512 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200513 */
514
515/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200516 * wakup_source_activate - Mark given wakeup source as active.
517 * @ws: Wakeup source to handle.
518 *
519 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
520 * core of the event by incrementing the counter of of wakeup events being
521 * processed.
522 */
Rafael J. Wysocki60d45532017-05-14 02:23:04 +0200523static void wakeup_source_activate(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200524{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200525 unsigned int cec;
526
Jin Qianb6ec9452015-05-06 15:26:56 -0700527 if (WARN_ONCE(wakeup_source_not_registered(ws),
528 "unregistered wakeup source\n"))
529 return;
530
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200531 ws->active = true;
532 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200533 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200534 if (ws->autosleep_enabled)
535 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200536
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100537 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200538 cec = atomic_inc_return(&combined_event_count);
539
540 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200541}
542
543/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200544 * wakeup_source_report_event - Report wakeup event using the given source.
545 * @ws: Wakeup source to report the event for.
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200546 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200547 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200548static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200549{
550 ws->event_count++;
551 /* This is racy, but the counter is approximate anyway. */
552 if (events_check_enabled)
553 ws->wakeup_count++;
554
555 if (!ws->active)
Rafael J. Wysocki60d45532017-05-14 02:23:04 +0200556 wakeup_source_activate(ws);
557
558 if (hard)
559 pm_system_wakeup();
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200560}
561
562/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200563 * __pm_stay_awake - Notify the PM core of a wakeup event.
564 * @ws: Wakeup source object associated with the source of the event.
565 *
566 * It is safe to call this function from interrupt context.
567 */
568void __pm_stay_awake(struct wakeup_source *ws)
569{
570 unsigned long flags;
571
572 if (!ws)
573 return;
574
575 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100576
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200577 wakeup_source_report_event(ws, false);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100578 del_timer(&ws->timer);
579 ws->timer_expires = 0;
580
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200581 spin_unlock_irqrestore(&ws->lock, flags);
582}
583EXPORT_SYMBOL_GPL(__pm_stay_awake);
584
585/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
587 * @dev: Device the wakeup event is related to.
588 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200589 * Notify the PM core of a wakeup event (signaled by @dev) by calling
590 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591 *
592 * Call this function after detecting of a wakeup event if pm_relax() is going
593 * to be called directly after processing the event (and possibly passing it to
594 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200595 */
596void pm_stay_awake(struct device *dev)
597{
598 unsigned long flags;
599
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200600 if (!dev)
601 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200602
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200603 spin_lock_irqsave(&dev->power.lock, flags);
604 __pm_stay_awake(dev->power.wakeup);
605 spin_unlock_irqrestore(&dev->power.lock, flags);
606}
607EXPORT_SYMBOL_GPL(pm_stay_awake);
608
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200609#ifdef CONFIG_PM_AUTOSLEEP
610static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
611{
612 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
613 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
614}
615#else
616static inline void update_prevent_sleep_time(struct wakeup_source *ws,
617 ktime_t now) {}
618#endif
619
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200620/**
621 * wakup_source_deactivate - Mark given wakeup source as inactive.
622 * @ws: Wakeup source to handle.
623 *
624 * Update the @ws' statistics and notify the PM core that the wakeup source has
625 * become inactive by decrementing the counter of wakeup events being processed
626 * and incrementing the counter of registered wakeup events.
627 */
628static void wakeup_source_deactivate(struct wakeup_source *ws)
629{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200630 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200631 ktime_t duration;
632 ktime_t now;
633
634 ws->relax_count++;
635 /*
636 * __pm_relax() may be called directly or from a timer function.
637 * If it is called directly right after the timer function has been
638 * started, but before the timer function calls __pm_relax(), it is
639 * possible that __pm_stay_awake() will be called in the meantime and
640 * will set ws->active. Then, ws->active may be cleared immediately
641 * by the __pm_relax() called from the timer function, but in such a
642 * case ws->relax_count will be different from ws->active_count.
643 */
644 if (ws->relax_count != ws->active_count) {
645 ws->relax_count--;
646 return;
647 }
648
649 ws->active = false;
650
651 now = ktime_get();
652 duration = ktime_sub(now, ws->last_time);
653 ws->total_time = ktime_add(ws->total_time, duration);
654 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
655 ws->max_time = duration;
656
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200657 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200658 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100659 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200660
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200661 if (ws->autosleep_enabled)
662 update_prevent_sleep_time(ws, now);
663
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200664 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100665 * Increment the counter of registered wakeup events and decrement the
666 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200667 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200668 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
669 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200670
671 split_counters(&cnt, &inpr);
672 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
673 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200674}
675
676/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200677 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
678 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200679 *
680 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200681 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200682 *
683 * It is safe to call it from interrupt context.
684 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200685void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200686{
687 unsigned long flags;
688
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200689 if (!ws)
690 return;
691
692 spin_lock_irqsave(&ws->lock, flags);
693 if (ws->active)
694 wakeup_source_deactivate(ws);
695 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200696}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200697EXPORT_SYMBOL_GPL(__pm_relax);
698
699/**
700 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
701 * @dev: Device that signaled the event.
702 *
703 * Execute __pm_relax() for the @dev's wakeup source object.
704 */
705void pm_relax(struct device *dev)
706{
707 unsigned long flags;
708
709 if (!dev)
710 return;
711
712 spin_lock_irqsave(&dev->power.lock, flags);
713 __pm_relax(dev->power.wakeup);
714 spin_unlock_irqrestore(&dev->power.lock, flags);
715}
716EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200717
718/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200719 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200720 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200721 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100722 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
723 * in @data if it is currently active and its timer has not been canceled and
724 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200725 */
Kees Cook96428e92017-10-16 16:20:55 -0700726static void pm_wakeup_timer_fn(struct timer_list *t)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200727{
Kees Cook96428e92017-10-16 16:20:55 -0700728 struct wakeup_source *ws = from_timer(ws, t, timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100729 unsigned long flags;
730
731 spin_lock_irqsave(&ws->lock, flags);
732
733 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200734 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100735 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200736 ws->expire_count++;
737 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100738
739 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200740}
741
742/**
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200743 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200744 * @ws: Wakeup source object associated with the event source.
745 * @msec: Anticipated event processing time (in milliseconds).
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200746 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200747 *
748 * Notify the PM core of a wakeup event whose source is @ws that will take
749 * approximately @msec milliseconds to be processed by the kernel. If @ws is
750 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
751 * execute pm_wakeup_timer_fn() in future.
752 *
753 * It is safe to call this function from interrupt context.
754 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200755void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200756{
757 unsigned long flags;
758 unsigned long expires;
759
760 if (!ws)
761 return;
762
763 spin_lock_irqsave(&ws->lock, flags);
764
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200765 wakeup_source_report_event(ws, hard);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200766
767 if (!msec) {
768 wakeup_source_deactivate(ws);
769 goto unlock;
770 }
771
772 expires = jiffies + msecs_to_jiffies(msec);
773 if (!expires)
774 expires = 1;
775
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100776 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200777 mod_timer(&ws->timer, expires);
778 ws->timer_expires = expires;
779 }
780
781 unlock:
782 spin_unlock_irqrestore(&ws->lock, flags);
783}
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200784EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200785
786/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200787 * pm_wakeup_event - Notify the PM core of a wakeup event.
788 * @dev: Device the wakeup event is related to.
789 * @msec: Anticipated event processing time (in milliseconds).
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200790 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200791 *
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200792 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200793 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200794void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200795{
796 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200797
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200798 if (!dev)
799 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200800
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200801 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200802 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200803 spin_unlock_irqrestore(&dev->power.lock, flags);
804}
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200805EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200806
Julius Wernerbb177fe2013-06-12 12:55:22 -0700807void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200808{
809 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200810 int srcuidx, active = 0;
Todd Poynora938da02012-08-12 00:17:02 +0200811 struct wakeup_source *last_activity_ws = NULL;
812
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200813 srcuidx = srcu_read_lock(&wakeup_srcu);
Todd Poynora938da02012-08-12 00:17:02 +0200814 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
815 if (ws->active) {
xing wei9320f952016-12-07 19:31:16 +0800816 pr_debug("active wakeup source: %s\n", ws->name);
Todd Poynora938da02012-08-12 00:17:02 +0200817 active = 1;
818 } else if (!active &&
819 (!last_activity_ws ||
820 ktime_to_ns(ws->last_time) >
821 ktime_to_ns(last_activity_ws->last_time))) {
822 last_activity_ws = ws;
823 }
824 }
825
826 if (!active && last_activity_ws)
xing wei9320f952016-12-07 19:31:16 +0800827 pr_debug("last active wakeup source: %s\n",
Todd Poynora938da02012-08-12 00:17:02 +0200828 last_activity_ws->name);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200829 srcu_read_unlock(&wakeup_srcu, srcuidx);
Todd Poynora938da02012-08-12 00:17:02 +0200830}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700831EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200832
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200833/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100834 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200835 *
836 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100837 * value from the past and return true if new wakeup events have been registered
838 * since the old value was stored. Also return true if the current number of
839 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200840 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100841bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200842{
843 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100844 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200845
846 spin_lock_irqsave(&events_lock, flags);
847 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100848 unsigned int cnt, inpr;
849
850 split_counters(&cnt, &inpr);
851 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100852 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200853 }
854 spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200855
Bernie Thompson9350de062013-06-01 00:47:43 +0000856 if (ret) {
857 pr_info("PM: Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700858 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000859 }
Todd Poynora938da02012-08-12 00:17:02 +0200860
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200861 return ret || atomic_read(&pm_abort_suspend) > 0;
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200862}
863
864void pm_system_wakeup(void)
865{
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200866 atomic_inc(&pm_abort_suspend);
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200867 s2idle_wake();
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200868}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100869EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200870
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200871void pm_system_cancel_wakeup(void)
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200872{
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200873 atomic_dec(&pm_abort_suspend);
874}
875
876void pm_wakeup_clear(bool reset)
877{
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700878 pm_wakeup_irq = 0;
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200879 if (reset)
880 atomic_set(&pm_abort_suspend, 0);
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700881}
882
883void pm_system_irq_wakeup(unsigned int irq_number)
884{
885 if (pm_wakeup_irq == 0) {
886 pm_wakeup_irq = irq_number;
887 pm_system_wakeup();
888 }
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200889}
890
891/**
892 * pm_get_wakeup_count - Read the number of registered wakeup events.
893 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200894 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200895 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200896 * Store the number of registered wakeup events at the address in @count. If
897 * @block is set, block until the current number of wakeup events being
898 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200899 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200900 * Return 'false' if the current number of wakeup events being processed is
901 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200902 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200903bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200904{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100905 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200906
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200907 if (block) {
908 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200909
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200910 for (;;) {
911 prepare_to_wait(&wakeup_count_wait_queue, &wait,
912 TASK_INTERRUPTIBLE);
913 split_counters(&cnt, &inpr);
914 if (inpr == 0 || signal_pending(current))
915 break;
xing wei9320f952016-12-07 19:31:16 +0800916 pm_print_active_wakeup_sources();
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200917 schedule();
918 }
919 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200920 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200921
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100922 split_counters(&cnt, &inpr);
923 *count = cnt;
924 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200925}
926
927/**
928 * pm_save_wakeup_count - Save the current number of registered wakeup events.
929 * @count: Value to compare with the current number of registered wakeup events.
930 *
931 * If @count is equal to the current number of registered wakeup events and the
932 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100933 * old number of registered wakeup events for pm_check_wakeup_events(), enable
934 * wakeup events detection and return 'true'. Otherwise disable wakeup events
935 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200936 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200937bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200938{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100939 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200940 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200941
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100942 events_check_enabled = false;
John Stultz49550702012-09-06 23:19:06 +0200943 spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100944 split_counters(&cnt, &inpr);
945 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200946 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200947 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200948 }
John Stultz49550702012-09-06 23:19:06 +0200949 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100950 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200951}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200952
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200953#ifdef CONFIG_PM_AUTOSLEEP
954/**
955 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
956 * @enabled: Whether to set or to clear the autosleep_enabled flags.
957 */
958void pm_wakep_autosleep_enabled(bool set)
959{
960 struct wakeup_source *ws;
961 ktime_t now = ktime_get();
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200962 int srcuidx;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200963
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200964 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200965 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
966 spin_lock_irq(&ws->lock);
967 if (ws->autosleep_enabled != set) {
968 ws->autosleep_enabled = set;
969 if (ws->active) {
970 if (set)
971 ws->start_prevent_time = now;
972 else
973 update_prevent_sleep_time(ws, now);
974 }
975 }
976 spin_unlock_irq(&ws->lock);
977 }
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200978 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200979}
980#endif /* CONFIG_PM_AUTOSLEEP */
981
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200982static struct dentry *wakeup_sources_stats_dentry;
983
984/**
985 * print_wakeup_source_stats - Print wakeup source statistics information.
986 * @m: seq_file to print the statistics into.
987 * @ws: Wakeup source object to print the statistics for.
988 */
989static int print_wakeup_source_stats(struct seq_file *m,
990 struct wakeup_source *ws)
991{
992 unsigned long flags;
993 ktime_t total_time;
994 ktime_t max_time;
995 unsigned long active_count;
996 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200997 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200998
999 spin_lock_irqsave(&ws->lock, flags);
1000
1001 total_time = ws->total_time;
1002 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001003 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001004 active_count = ws->active_count;
1005 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001006 ktime_t now = ktime_get();
1007
1008 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001009 total_time = ktime_add(total_time, active_time);
Thomas Gleixner2456e852016-12-25 11:38:40 +01001010 if (active_time > max_time)
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001011 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001012
1013 if (ws->autosleep_enabled)
1014 prevent_sleep_time = ktime_add(prevent_sleep_time,
1015 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001016 } else {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001017 active_time = 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001018 }
1019
Joe Perches9f6a2402015-04-15 16:17:48 -07001020 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",
1021 ws->name, active_count, ws->event_count,
1022 ws->wakeup_count, ws->expire_count,
1023 ktime_to_ms(active_time), ktime_to_ms(total_time),
1024 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1025 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001026
1027 spin_unlock_irqrestore(&ws->lock, flags);
1028
Joe Perches9f6a2402015-04-15 16:17:48 -07001029 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001030}
1031
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001032static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1033 loff_t *pos)
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001034{
1035 struct wakeup_source *ws;
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001036 loff_t n = *pos;
1037 int *srcuidx = m->private;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001038
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001039 if (n == 0) {
1040 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1041 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1042 "last_change\tprevent_suspend_time\n");
1043 }
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001044
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001045 *srcuidx = srcu_read_lock(&wakeup_srcu);
1046 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
1047 if (n-- <= 0)
1048 return ws;
1049 }
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001050
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001051 return NULL;
1052}
1053
1054static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1055 void *v, loff_t *pos)
1056{
1057 struct wakeup_source *ws = v;
1058 struct wakeup_source *next_ws = NULL;
1059
1060 ++(*pos);
1061
1062 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1063 next_ws = ws;
1064 break;
1065 }
1066
1067 return next_ws;
1068}
1069
1070static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1071{
1072 int *srcuidx = m->private;
1073
1074 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1075}
1076
1077/**
1078 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1079 * @m: seq_file to print the statistics into.
1080 * @v: wakeup_source of each iteration
1081 */
1082static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1083{
1084 struct wakeup_source *ws = v;
1085
1086 print_wakeup_source_stats(m, ws);
Jin Qian7f436052015-05-15 18:10:37 -07001087
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001088 return 0;
1089}
1090
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001091static const struct seq_operations wakeup_sources_stats_seq_ops = {
1092 .start = wakeup_sources_stats_seq_start,
1093 .next = wakeup_sources_stats_seq_next,
1094 .stop = wakeup_sources_stats_seq_stop,
1095 .show = wakeup_sources_stats_seq_show,
1096};
1097
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001098static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1099{
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001100 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001101}
1102
1103static const struct file_operations wakeup_sources_stats_fops = {
1104 .owner = THIS_MODULE,
1105 .open = wakeup_sources_stats_open,
1106 .read = seq_read,
1107 .llseek = seq_lseek,
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001108 .release = seq_release_private,
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001109};
1110
1111static int __init wakeup_sources_debugfs_init(void)
1112{
1113 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1114 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1115 return 0;
1116}
1117
1118postcore_initcall(wakeup_sources_debugfs_init);