blob: 9066b2dfc3e163d0fbc46266de2734cdb272f967 [file] [log] [blame]
Greg Kroah-Hartman5de363b2019-04-02 15:32:01 +02001// SPDX-License-Identifier: GPL-2.0
Rafael J. Wysockic125e962010-07-05 22:43:53 +02002/*
3 * drivers/base/power/wakeup.c - System wakeup events framework
4 *
5 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Rafael J. Wysockic125e962010-07-05 22:43:53 +02006 */
Joe Perches7a5bd122019-03-04 09:14:38 -08007#define pr_fmt(fmt) "PM: " fmt
8
Rafael J. Wysockic125e962010-07-05 22:43:53 +02009#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
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +020060static DEFINE_RAW_SPINLOCK(events_lock);
Rafael J. Wysockic125e962010-07-05 22:43:53 +020061
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
Jin Qian7f436052015-05-15 18:10:37 -0700109/*
110 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
111 */
112static void wakeup_source_record(struct wakeup_source *ws)
113{
114 unsigned long flags;
115
116 spin_lock_irqsave(&deleted_ws.lock, flags);
117
118 if (ws->event_count) {
119 deleted_ws.total_time =
120 ktime_add(deleted_ws.total_time, ws->total_time);
121 deleted_ws.prevent_sleep_time =
122 ktime_add(deleted_ws.prevent_sleep_time,
123 ws->prevent_sleep_time);
124 deleted_ws.max_time =
125 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
126 deleted_ws.max_time : ws->max_time;
127 deleted_ws.event_count += ws->event_count;
128 deleted_ws.active_count += ws->active_count;
129 deleted_ws.relax_count += ws->relax_count;
130 deleted_ws.expire_count += ws->expire_count;
131 deleted_ws.wakeup_count += ws->wakeup_count;
132 }
133
134 spin_unlock_irqrestore(&deleted_ws.lock, flags);
135}
136
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100137/**
138 * wakeup_source_destroy - Destroy a struct wakeup_source object.
139 * @ws: Wakeup source to destroy.
140 *
141 * Use only for wakeup source objects created with wakeup_source_create().
142 */
143void wakeup_source_destroy(struct wakeup_source *ws)
144{
145 if (!ws)
146 return;
147
Rafael J. Wysocki623217a2019-03-11 12:53:59 +0100148 __pm_relax(ws);
Jin Qian7f436052015-05-15 18:10:37 -0700149 wakeup_source_record(ws);
Rasmus Villemoes72362142015-09-09 23:42:06 +0200150 kfree_const(ws->name);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200151 kfree(ws);
152}
153EXPORT_SYMBOL_GPL(wakeup_source_destroy);
154
155/**
156 * wakeup_source_add - Add given object to the list of wakeup sources.
157 * @ws: Wakeup source object to add to the list.
158 */
159void wakeup_source_add(struct wakeup_source *ws)
160{
John Stultz49550702012-09-06 23:19:06 +0200161 unsigned long flags;
162
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200163 if (WARN_ON(!ws))
164 return;
165
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100166 spin_lock_init(&ws->lock);
Kees Cook96428e92017-10-16 16:20:55 -0700167 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200168 ws->active = false;
169
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200170 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200171 list_add_rcu(&ws->entry, &wakeup_sources);
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200172 raw_spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200173}
174EXPORT_SYMBOL_GPL(wakeup_source_add);
175
176/**
177 * wakeup_source_remove - Remove given object from the wakeup sources list.
178 * @ws: Wakeup source object to remove from the list.
179 */
180void wakeup_source_remove(struct wakeup_source *ws)
181{
John Stultz49550702012-09-06 23:19:06 +0200182 unsigned long flags;
183
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200184 if (WARN_ON(!ws))
185 return;
186
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200187 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200188 list_del_rcu(&ws->entry);
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200189 raw_spin_unlock_irqrestore(&events_lock, flags);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200190 synchronize_srcu(&wakeup_srcu);
Viresh Kumar1fad17f2019-03-08 15:23:11 +0530191
192 del_timer_sync(&ws->timer);
193 /*
194 * Clear timer.function to make wakeup_source_not_registered() treat
195 * this wakeup source as not registered.
196 */
197 ws->timer.function = NULL;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200198}
199EXPORT_SYMBOL_GPL(wakeup_source_remove);
200
201/**
202 * wakeup_source_register - Create wakeup source and add it to the list.
203 * @name: Name of the wakeup source to register.
204 */
205struct wakeup_source *wakeup_source_register(const char *name)
206{
207 struct wakeup_source *ws;
208
209 ws = wakeup_source_create(name);
210 if (ws)
211 wakeup_source_add(ws);
212
213 return ws;
214}
215EXPORT_SYMBOL_GPL(wakeup_source_register);
216
217/**
218 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
219 * @ws: Wakeup source object to unregister.
220 */
221void wakeup_source_unregister(struct wakeup_source *ws)
222{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100223 if (ws) {
224 wakeup_source_remove(ws);
225 wakeup_source_destroy(ws);
226 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200227}
228EXPORT_SYMBOL_GPL(wakeup_source_unregister);
229
230/**
231 * device_wakeup_attach - Attach a wakeup source object to a device object.
232 * @dev: Device to handle.
233 * @ws: Wakeup source object to attach to @dev.
234 *
235 * This causes @dev to be treated as a wakeup device.
236 */
237static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
238{
239 spin_lock_irq(&dev->power.lock);
240 if (dev->power.wakeup) {
241 spin_unlock_irq(&dev->power.lock);
242 return -EEXIST;
243 }
244 dev->power.wakeup = ws;
Strashko, Grygorii16669be2016-04-06 14:45:53 +0300245 if (dev->power.wakeirq)
246 device_wakeup_attach_irq(dev, dev->power.wakeirq);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200247 spin_unlock_irq(&dev->power.lock);
248 return 0;
249}
250
251/**
252 * device_wakeup_enable - Enable given device to be a wakeup source.
253 * @dev: Device to handle.
254 *
255 * Create a wakeup source object, register it and attach it to @dev.
256 */
257int device_wakeup_enable(struct device *dev)
258{
259 struct wakeup_source *ws;
260 int ret;
261
262 if (!dev || !dev->power.can_wakeup)
263 return -EINVAL;
264
Ulf Hansson0026cef2018-01-11 09:18:59 +0100265 if (pm_suspend_target_state != PM_SUSPEND_ON)
266 dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
267
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200268 ws = wakeup_source_register(dev_name(dev));
269 if (!ws)
270 return -ENOMEM;
271
272 ret = device_wakeup_attach(dev, ws);
273 if (ret)
274 wakeup_source_unregister(ws);
275
276 return ret;
277}
278EXPORT_SYMBOL_GPL(device_wakeup_enable);
279
280/**
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700281 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
282 * @dev: Device to handle
283 * @wakeirq: Device specific wakeirq entry
284 *
285 * Attach a device wakeirq to the wakeup source so the device
286 * wake IRQ can be configured automatically for suspend and
287 * resume.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200288 *
289 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700290 */
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100291void device_wakeup_attach_irq(struct device *dev,
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700292 struct wake_irq *wakeirq)
293{
294 struct wakeup_source *ws;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700295
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700296 ws = dev->power.wakeup;
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100297 if (!ws)
298 return;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700299
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200300 if (ws->wakeirq)
Rafael J. Wysocki7bf4e592018-01-05 02:18:42 +0100301 dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700302
303 ws->wakeirq = wakeirq;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700304}
305
306/**
307 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
308 * @dev: Device to handle
309 *
310 * Removes a device wakeirq from the wakeup source.
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200311 *
312 * Call under the device's power.lock lock.
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700313 */
314void device_wakeup_detach_irq(struct device *dev)
315{
316 struct wakeup_source *ws;
317
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700318 ws = dev->power.wakeup;
Rafael J. Wysocki6d3dab72015-07-07 13:08:39 +0200319 if (ws)
320 ws->wakeirq = NULL;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700321}
322
323/**
324 * device_wakeup_arm_wake_irqs(void)
325 *
326 * Itereates over the list of device wakeirqs to arm them.
327 */
328void device_wakeup_arm_wake_irqs(void)
329{
330 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200331 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700332
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200333 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200334 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
335 dev_pm_arm_wake_irq(ws->wakeirq);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200336 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700337}
338
339/**
340 * device_wakeup_disarm_wake_irqs(void)
341 *
342 * Itereates over the list of device wakeirqs to disarm them.
343 */
344void device_wakeup_disarm_wake_irqs(void)
345{
346 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200347 int srcuidx;
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700348
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200349 srcuidx = srcu_read_lock(&wakeup_srcu);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200350 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
351 dev_pm_disarm_wake_irq(ws->wakeirq);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200352 srcu_read_unlock(&wakeup_srcu, srcuidx);
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700353}
354
355/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200356 * device_wakeup_detach - Detach a device's wakeup source object from it.
357 * @dev: Device to detach the wakeup source object from.
358 *
359 * After it returns, @dev will not be treated as a wakeup device any more.
360 */
361static struct wakeup_source *device_wakeup_detach(struct device *dev)
362{
363 struct wakeup_source *ws;
364
365 spin_lock_irq(&dev->power.lock);
366 ws = dev->power.wakeup;
367 dev->power.wakeup = NULL;
368 spin_unlock_irq(&dev->power.lock);
369 return ws;
370}
371
372/**
373 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
374 * @dev: Device to handle.
375 *
376 * Detach the @dev's wakeup source object from it, unregister this wakeup source
377 * object and destroy it.
378 */
379int device_wakeup_disable(struct device *dev)
380{
381 struct wakeup_source *ws;
382
383 if (!dev || !dev->power.can_wakeup)
384 return -EINVAL;
385
386 ws = device_wakeup_detach(dev);
Markus Elfring4f48ec82016-07-23 17:04:00 +0200387 wakeup_source_unregister(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200388 return 0;
389}
390EXPORT_SYMBOL_GPL(device_wakeup_disable);
391
392/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100393 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
394 * @dev: Device to handle.
395 * @capable: Whether or not @dev is capable of waking up the system from sleep.
396 *
397 * If @capable is set, set the @dev's power.can_wakeup flag and add its
398 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
399 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
400 *
401 * This function may sleep and it can't be called from any context where
402 * sleeping is not allowed.
403 */
404void device_set_wakeup_capable(struct device *dev, bool capable)
405{
406 if (!!dev->power.can_wakeup == !!capable)
407 return;
408
Rafael J. Wysocki820b9b02017-08-02 01:32:44 +0200409 dev->power.can_wakeup = capable;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200410 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100411 if (capable) {
Rafael J. Wysocki820b9b02017-08-02 01:32:44 +0200412 int ret = wakeup_sysfs_add(dev);
413
414 if (ret)
415 dev_info(dev, "Wakeup sysfs attributes not added\n");
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100416 } else {
417 wakeup_sysfs_remove(dev);
418 }
419 }
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100420}
421EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
422
423/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200424 * device_init_wakeup - Device wakeup initialization.
425 * @dev: Device to handle.
426 * @enable: Whether or not to enable @dev as a wakeup device.
427 *
428 * By default, most devices should leave wakeup disabled. The exceptions are
429 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200430 * possibly network interfaces, etc. Also, devices that don't generate their
431 * own wakeup requests but merely forward requests from one bus to another
432 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200433 */
434int device_init_wakeup(struct device *dev, bool enable)
435{
436 int ret = 0;
437
Zhang Rui0c5ff0e2014-05-28 15:23:35 +0800438 if (!dev)
439 return -EINVAL;
440
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200441 if (enable) {
442 device_set_wakeup_capable(dev, true);
443 ret = device_wakeup_enable(dev);
444 } else {
Rafael J. Wysocki9dbc64a52018-01-02 01:42:56 +0100445 device_wakeup_disable(dev);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200446 device_set_wakeup_capable(dev, false);
447 }
448
449 return ret;
450}
451EXPORT_SYMBOL_GPL(device_init_wakeup);
452
453/**
454 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
455 * @dev: Device to handle.
456 */
457int device_set_wakeup_enable(struct device *dev, bool enable)
458{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200459 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
460}
461EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200462
Jin Qianb6ec9452015-05-06 15:26:56 -0700463/**
464 * wakeup_source_not_registered - validate the given wakeup source.
465 * @ws: Wakeup source to be validated.
466 */
467static bool wakeup_source_not_registered(struct wakeup_source *ws)
468{
469 /*
470 * Use timer struct to check if the given source is initialized
471 * by wakeup_source_add.
472 */
Kees Cook841b86f2017-10-23 09:40:42 +0200473 return ws->timer.function != pm_wakeup_timer_fn;
Jin Qianb6ec9452015-05-06 15:26:56 -0700474}
475
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200476/*
477 * The functions below use the observation that each wakeup event starts a
478 * period in which the system should not be suspended. The moment this period
479 * will end depends on how the wakeup event is going to be processed after being
480 * detected and all of the possible cases can be divided into two distinct
481 * groups.
482 *
483 * First, a wakeup event may be detected by the same functional unit that will
484 * carry out the entire processing of it and possibly will pass it to user space
485 * for further processing. In that case the functional unit that has detected
486 * the event may later "close" the "no suspend" period associated with it
487 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
488 * pm_relax(), balanced with each other, is supposed to be used in such
489 * situations.
490 *
491 * Second, a wakeup event may be detected by one functional unit and processed
492 * by another one. In that case the unit that has detected it cannot really
493 * "close" the "no suspend" period associated with it, unless it knows in
494 * advance what's going to happen to the event during processing. This
495 * knowledge, however, may not be available to it, so it can simply specify time
496 * to wait before the system can be suspended and pass it as the second
497 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200498 *
499 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
500 * "no suspend" period will be ended either by the pm_relax(), or by the timer
501 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200502 */
503
504/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200505 * wakup_source_activate - Mark given wakeup source as active.
506 * @ws: Wakeup source to handle.
507 *
508 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
509 * core of the event by incrementing the counter of of wakeup events being
510 * processed.
511 */
Rafael J. Wysocki60d45532017-05-14 02:23:04 +0200512static void wakeup_source_activate(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200513{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200514 unsigned int cec;
515
Jin Qianb6ec9452015-05-06 15:26:56 -0700516 if (WARN_ONCE(wakeup_source_not_registered(ws),
517 "unregistered wakeup source\n"))
518 return;
519
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200520 ws->active = true;
521 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200522 ws->last_time = ktime_get();
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200523 if (ws->autosleep_enabled)
524 ws->start_prevent_time = ws->last_time;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200525
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100526 /* Increment the counter of events in progress. */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200527 cec = atomic_inc_return(&combined_event_count);
528
529 trace_wakeup_source_activate(ws->name, cec);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200530}
531
532/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200533 * wakeup_source_report_event - Report wakeup event using the given source.
534 * @ws: Wakeup source to report the event for.
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200535 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200536 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200537static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200538{
539 ws->event_count++;
540 /* This is racy, but the counter is approximate anyway. */
541 if (events_check_enabled)
542 ws->wakeup_count++;
543
544 if (!ws->active)
Rafael J. Wysocki60d45532017-05-14 02:23:04 +0200545 wakeup_source_activate(ws);
546
547 if (hard)
548 pm_system_wakeup();
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200549}
550
551/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200552 * __pm_stay_awake - Notify the PM core of a wakeup event.
553 * @ws: Wakeup source object associated with the source of the event.
554 *
555 * It is safe to call this function from interrupt context.
556 */
557void __pm_stay_awake(struct wakeup_source *ws)
558{
559 unsigned long flags;
560
561 if (!ws)
562 return;
563
564 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100565
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200566 wakeup_source_report_event(ws, false);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100567 del_timer(&ws->timer);
568 ws->timer_expires = 0;
569
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200570 spin_unlock_irqrestore(&ws->lock, flags);
571}
572EXPORT_SYMBOL_GPL(__pm_stay_awake);
573
574/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200575 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
576 * @dev: Device the wakeup event is related to.
577 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200578 * Notify the PM core of a wakeup event (signaled by @dev) by calling
579 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200580 *
581 * Call this function after detecting of a wakeup event if pm_relax() is going
582 * to be called directly after processing the event (and possibly passing it to
583 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200584 */
585void pm_stay_awake(struct device *dev)
586{
587 unsigned long flags;
588
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200589 if (!dev)
590 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200592 spin_lock_irqsave(&dev->power.lock, flags);
593 __pm_stay_awake(dev->power.wakeup);
594 spin_unlock_irqrestore(&dev->power.lock, flags);
595}
596EXPORT_SYMBOL_GPL(pm_stay_awake);
597
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200598#ifdef CONFIG_PM_AUTOSLEEP
599static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
600{
601 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
602 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
603}
604#else
605static inline void update_prevent_sleep_time(struct wakeup_source *ws,
606 ktime_t now) {}
607#endif
608
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200609/**
610 * wakup_source_deactivate - Mark given wakeup source as inactive.
611 * @ws: Wakeup source to handle.
612 *
613 * Update the @ws' statistics and notify the PM core that the wakeup source has
614 * become inactive by decrementing the counter of wakeup events being processed
615 * and incrementing the counter of registered wakeup events.
616 */
617static void wakeup_source_deactivate(struct wakeup_source *ws)
618{
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200619 unsigned int cnt, inpr, cec;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200620 ktime_t duration;
621 ktime_t now;
622
623 ws->relax_count++;
624 /*
625 * __pm_relax() may be called directly or from a timer function.
626 * If it is called directly right after the timer function has been
627 * started, but before the timer function calls __pm_relax(), it is
628 * possible that __pm_stay_awake() will be called in the meantime and
629 * will set ws->active. Then, ws->active may be cleared immediately
630 * by the __pm_relax() called from the timer function, but in such a
631 * case ws->relax_count will be different from ws->active_count.
632 */
633 if (ws->relax_count != ws->active_count) {
634 ws->relax_count--;
635 return;
636 }
637
638 ws->active = false;
639
640 now = ktime_get();
641 duration = ktime_sub(now, ws->last_time);
642 ws->total_time = ktime_add(ws->total_time, duration);
643 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
644 ws->max_time = duration;
645
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200646 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200647 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100648 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200649
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200650 if (ws->autosleep_enabled)
651 update_prevent_sleep_time(ws, now);
652
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200653 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100654 * Increment the counter of registered wakeup events and decrement the
655 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200656 */
Arve Hjønnevåg6791e362012-04-29 22:53:02 +0200657 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
658 trace_wakeup_source_deactivate(ws->name, cec);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200659
660 split_counters(&cnt, &inpr);
661 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
662 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200663}
664
665/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200666 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
667 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200668 *
669 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200670 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200671 *
672 * It is safe to call it from interrupt context.
673 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200674void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200675{
676 unsigned long flags;
677
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200678 if (!ws)
679 return;
680
681 spin_lock_irqsave(&ws->lock, flags);
682 if (ws->active)
683 wakeup_source_deactivate(ws);
684 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200685}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200686EXPORT_SYMBOL_GPL(__pm_relax);
687
688/**
689 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
690 * @dev: Device that signaled the event.
691 *
692 * Execute __pm_relax() for the @dev's wakeup source object.
693 */
694void pm_relax(struct device *dev)
695{
696 unsigned long flags;
697
698 if (!dev)
699 return;
700
701 spin_lock_irqsave(&dev->power.lock, flags);
702 __pm_relax(dev->power.wakeup);
703 spin_unlock_irqrestore(&dev->power.lock, flags);
704}
705EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200706
707/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200708 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200709 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200710 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100711 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
712 * in @data if it is currently active and its timer has not been canceled and
713 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200714 */
Kees Cook96428e92017-10-16 16:20:55 -0700715static void pm_wakeup_timer_fn(struct timer_list *t)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200716{
Kees Cook96428e92017-10-16 16:20:55 -0700717 struct wakeup_source *ws = from_timer(ws, t, timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100718 unsigned long flags;
719
720 spin_lock_irqsave(&ws->lock, flags);
721
722 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200723 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100724 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200725 ws->expire_count++;
726 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100727
728 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200729}
730
731/**
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200732 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200733 * @ws: Wakeup source object associated with the event source.
734 * @msec: Anticipated event processing time (in milliseconds).
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200735 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200736 *
737 * Notify the PM core of a wakeup event whose source is @ws that will take
738 * approximately @msec milliseconds to be processed by the kernel. If @ws is
739 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
740 * execute pm_wakeup_timer_fn() in future.
741 *
742 * It is safe to call this function from interrupt context.
743 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200744void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200745{
746 unsigned long flags;
747 unsigned long expires;
748
749 if (!ws)
750 return;
751
752 spin_lock_irqsave(&ws->lock, flags);
753
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200754 wakeup_source_report_event(ws, hard);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200755
756 if (!msec) {
757 wakeup_source_deactivate(ws);
758 goto unlock;
759 }
760
761 expires = jiffies + msecs_to_jiffies(msec);
762 if (!expires)
763 expires = 1;
764
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100765 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200766 mod_timer(&ws->timer, expires);
767 ws->timer_expires = expires;
768 }
769
770 unlock:
771 spin_unlock_irqrestore(&ws->lock, flags);
772}
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200773EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200774
775/**
Yangtao Lid1c6b412019-01-23 11:22:01 -0500776 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200777 * @dev: Device the wakeup event is related to.
778 * @msec: Anticipated event processing time (in milliseconds).
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200779 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200780 *
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200781 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200782 */
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200783void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200784{
785 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200786
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200787 if (!dev)
788 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200789
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200790 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200791 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200792 spin_unlock_irqrestore(&dev->power.lock, flags);
793}
Rafael J. Wysocki8a537ec2017-04-26 23:22:09 +0200794EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200795
Julius Wernerbb177fe2013-06-12 12:55:22 -0700796void pm_print_active_wakeup_sources(void)
Todd Poynora938da02012-08-12 00:17:02 +0200797{
798 struct wakeup_source *ws;
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200799 int srcuidx, active = 0;
Todd Poynora938da02012-08-12 00:17:02 +0200800 struct wakeup_source *last_activity_ws = NULL;
801
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200802 srcuidx = srcu_read_lock(&wakeup_srcu);
Todd Poynora938da02012-08-12 00:17:02 +0200803 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
804 if (ws->active) {
xing wei9320f952016-12-07 19:31:16 +0800805 pr_debug("active wakeup source: %s\n", ws->name);
Todd Poynora938da02012-08-12 00:17:02 +0200806 active = 1;
807 } else if (!active &&
808 (!last_activity_ws ||
809 ktime_to_ns(ws->last_time) >
810 ktime_to_ns(last_activity_ws->last_time))) {
811 last_activity_ws = ws;
812 }
813 }
814
815 if (!active && last_activity_ws)
xing wei9320f952016-12-07 19:31:16 +0800816 pr_debug("last active wakeup source: %s\n",
Todd Poynora938da02012-08-12 00:17:02 +0200817 last_activity_ws->name);
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200818 srcu_read_unlock(&wakeup_srcu, srcuidx);
Todd Poynora938da02012-08-12 00:17:02 +0200819}
Julius Wernerbb177fe2013-06-12 12:55:22 -0700820EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
Todd Poynora938da02012-08-12 00:17:02 +0200821
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200822/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100823 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200824 *
825 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100826 * value from the past and return true if new wakeup events have been registered
827 * since the old value was stored. Also return true if the current number of
828 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200829 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100830bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200831{
832 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100833 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200834
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200835 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200836 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100837 unsigned int cnt, inpr;
838
839 split_counters(&cnt, &inpr);
840 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100841 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200842 }
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200843 raw_spin_unlock_irqrestore(&events_lock, flags);
Todd Poynora938da02012-08-12 00:17:02 +0200844
Bernie Thompson9350de062013-06-01 00:47:43 +0000845 if (ret) {
Joe Perches7a5bd122019-03-04 09:14:38 -0800846 pr_debug("Wakeup pending, aborting suspend\n");
Julius Wernerbb177fe2013-06-12 12:55:22 -0700847 pm_print_active_wakeup_sources();
Bernie Thompson9350de062013-06-01 00:47:43 +0000848 }
Todd Poynora938da02012-08-12 00:17:02 +0200849
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200850 return ret || atomic_read(&pm_abort_suspend) > 0;
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200851}
852
853void pm_system_wakeup(void)
854{
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200855 atomic_inc(&pm_abort_suspend);
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200856 s2idle_wake();
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200857}
Boris BREZILLON432ec922015-03-02 10:18:13 +0100858EXPORT_SYMBOL_GPL(pm_system_wakeup);
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200859
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200860void pm_system_cancel_wakeup(void)
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200861{
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200862 atomic_dec(&pm_abort_suspend);
863}
864
865void pm_wakeup_clear(bool reset)
866{
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700867 pm_wakeup_irq = 0;
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200868 if (reset)
869 atomic_set(&pm_abort_suspend, 0);
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700870}
871
872void pm_system_irq_wakeup(unsigned int irq_number)
873{
874 if (pm_wakeup_irq == 0) {
875 pm_wakeup_irq = irq_number;
876 pm_system_wakeup();
877 }
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200878}
879
880/**
881 * pm_get_wakeup_count - Read the number of registered wakeup events.
882 * @count: Address to store the value at.
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200883 * @block: Whether or not to block.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200884 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200885 * Store the number of registered wakeup events at the address in @count. If
886 * @block is set, block until the current number of wakeup events being
887 * processed is zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200888 *
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200889 * Return 'false' if the current number of wakeup events being processed is
890 * nonzero. Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200891 */
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200892bool pm_get_wakeup_count(unsigned int *count, bool block)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200893{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100894 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200895
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200896 if (block) {
897 DEFINE_WAIT(wait);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200898
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200899 for (;;) {
900 prepare_to_wait(&wakeup_count_wait_queue, &wait,
901 TASK_INTERRUPTIBLE);
902 split_counters(&cnt, &inpr);
903 if (inpr == 0 || signal_pending(current))
904 break;
xing wei9320f952016-12-07 19:31:16 +0800905 pm_print_active_wakeup_sources();
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200906 schedule();
907 }
908 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200909 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200910
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100911 split_counters(&cnt, &inpr);
912 *count = cnt;
913 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200914}
915
916/**
917 * pm_save_wakeup_count - Save the current number of registered wakeup events.
918 * @count: Value to compare with the current number of registered wakeup events.
919 *
920 * If @count is equal to the current number of registered wakeup events and the
921 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100922 * old number of registered wakeup events for pm_check_wakeup_events(), enable
923 * wakeup events detection and return 'true'. Otherwise disable wakeup events
924 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200925 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200926bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200927{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100928 unsigned int cnt, inpr;
John Stultz49550702012-09-06 23:19:06 +0200929 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200930
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100931 events_check_enabled = false;
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200932 raw_spin_lock_irqsave(&events_lock, flags);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100933 split_counters(&cnt, &inpr);
934 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200935 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200936 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200937 }
Sebastian Andrzej Siewiorbccaada2018-05-25 11:46:46 +0200938 raw_spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100939 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200940}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200941
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200942#ifdef CONFIG_PM_AUTOSLEEP
943/**
944 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
945 * @enabled: Whether to set or to clear the autosleep_enabled flags.
946 */
947void pm_wakep_autosleep_enabled(bool set)
948{
949 struct wakeup_source *ws;
950 ktime_t now = ktime_get();
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200951 int srcuidx;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200952
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200953 srcuidx = srcu_read_lock(&wakeup_srcu);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200954 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
955 spin_lock_irq(&ws->lock);
956 if (ws->autosleep_enabled != set) {
957 ws->autosleep_enabled = set;
958 if (ws->active) {
959 if (set)
960 ws->start_prevent_time = now;
961 else
962 update_prevent_sleep_time(ws, now);
963 }
964 }
965 spin_unlock_irq(&ws->lock);
966 }
Thomas Gleixnerea0212f2017-06-25 19:31:13 +0200967 srcu_read_unlock(&wakeup_srcu, srcuidx);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200968}
969#endif /* CONFIG_PM_AUTOSLEEP */
970
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200971static struct dentry *wakeup_sources_stats_dentry;
972
973/**
974 * print_wakeup_source_stats - Print wakeup source statistics information.
975 * @m: seq_file to print the statistics into.
976 * @ws: Wakeup source object to print the statistics for.
977 */
978static int print_wakeup_source_stats(struct seq_file *m,
979 struct wakeup_source *ws)
980{
981 unsigned long flags;
982 ktime_t total_time;
983 ktime_t max_time;
984 unsigned long active_count;
985 ktime_t active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200986 ktime_t prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200987
988 spin_lock_irqsave(&ws->lock, flags);
989
990 total_time = ws->total_time;
991 max_time = ws->max_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200992 prevent_sleep_time = ws->prevent_sleep_time;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200993 active_count = ws->active_count;
994 if (ws->active) {
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200995 ktime_t now = ktime_get();
996
997 active_time = ktime_sub(now, ws->last_time);
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200998 total_time = ktime_add(total_time, active_time);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100999 if (active_time > max_time)
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001000 max_time = active_time;
Rafael J. Wysocki55850942012-04-29 22:53:32 +02001001
1002 if (ws->autosleep_enabled)
1003 prevent_sleep_time = ktime_add(prevent_sleep_time,
1004 ktime_sub(now, ws->start_prevent_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001005 } else {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001006 active_time = 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001007 }
1008
Joe Perches9f6a2402015-04-15 16:17:48 -07001009 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",
1010 ws->name, active_count, ws->event_count,
1011 ws->wakeup_count, ws->expire_count,
1012 ktime_to_ms(active_time), ktime_to_ms(total_time),
1013 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1014 ktime_to_ms(prevent_sleep_time));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001015
1016 spin_unlock_irqrestore(&ws->lock, flags);
1017
Joe Perches9f6a2402015-04-15 16:17:48 -07001018 return 0;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001019}
1020
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001021static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1022 loff_t *pos)
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001023{
1024 struct wakeup_source *ws;
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001025 loff_t n = *pos;
1026 int *srcuidx = m->private;
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001027
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001028 if (n == 0) {
1029 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1030 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1031 "last_change\tprevent_suspend_time\n");
1032 }
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001033
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001034 *srcuidx = srcu_read_lock(&wakeup_srcu);
1035 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
1036 if (n-- <= 0)
1037 return ws;
1038 }
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001039
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001040 return NULL;
1041}
1042
1043static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1044 void *v, loff_t *pos)
1045{
1046 struct wakeup_source *ws = v;
1047 struct wakeup_source *next_ws = NULL;
1048
1049 ++(*pos);
1050
1051 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1052 next_ws = ws;
1053 break;
1054 }
1055
1056 return next_ws;
1057}
1058
1059static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1060{
1061 int *srcuidx = m->private;
1062
1063 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1064}
1065
1066/**
1067 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1068 * @m: seq_file to print the statistics into.
1069 * @v: wakeup_source of each iteration
1070 */
1071static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1072{
1073 struct wakeup_source *ws = v;
1074
1075 print_wakeup_source_stats(m, ws);
Jin Qian7f436052015-05-15 18:10:37 -07001076
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001077 return 0;
1078}
1079
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001080static const struct seq_operations wakeup_sources_stats_seq_ops = {
1081 .start = wakeup_sources_stats_seq_start,
1082 .next = wakeup_sources_stats_seq_next,
1083 .stop = wakeup_sources_stats_seq_stop,
1084 .show = wakeup_sources_stats_seq_show,
1085};
1086
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001087static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1088{
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001089 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001090}
1091
1092static const struct file_operations wakeup_sources_stats_fops = {
1093 .owner = THIS_MODULE,
1094 .open = wakeup_sources_stats_open,
1095 .read = seq_read,
1096 .llseek = seq_lseek,
Mahendran Ganesh00ee22c2018-04-25 18:59:31 +08001097 .release = seq_release_private,
Rafael J. Wysocki9c034392010-10-19 23:42:49 +02001098};
1099
1100static int __init wakeup_sources_debugfs_init(void)
1101{
1102 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
1103 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
1104 return 0;
1105}
1106
1107postcore_initcall(wakeup_sources_debugfs_init);