blob: 92f0960e9014568dbdbf38f798874c10a764c1f6 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * drivers/base/power/wakeup.c - System wakeup events framework
4 *
5 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
David Brazdil0f672f62019-12-10 10:32:29 +00007#define pr_fmt(fmt) "PM: " fmt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008
9#include <linux/device.h>
10#include <linux/slab.h>
11#include <linux/sched/signal.h>
12#include <linux/capability.h>
13#include <linux/export.h>
14#include <linux/suspend.h>
15#include <linux/seq_file.h>
16#include <linux/debugfs.h>
17#include <linux/pm_wakeirq.h>
18#include <trace/events/power.h>
19
20#include "power.h"
21
22#ifndef CONFIG_SUSPEND
23suspend_state_t pm_suspend_target_state;
24#define pm_suspend_target_state (PM_SUSPEND_ON)
25#endif
26
27/*
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 */
31bool events_check_enabled __read_mostly;
32
33/* First wakeup IRQ seen by the kernel in the last cycle. */
34unsigned int pm_wakeup_irq __read_mostly;
35
36/* If greater than 0 and the system is suspending, terminate the suspend. */
37static atomic_t pm_abort_suspend __read_mostly;
38
39/*
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. */
58static unsigned int saved_count;
59
60static DEFINE_RAW_SPINLOCK(events_lock);
61
62static void pm_wakeup_timer_fn(struct timer_list *t);
63
64static LIST_HEAD(wakeup_sources);
65
66static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
67
68DEFINE_STATIC_SRCU(wakeup_srcu);
69
70static struct wakeup_source deleted_ws = {
71 .name = "deleted",
72 .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
73};
74
David Brazdil0f672f62019-12-10 10:32:29 +000075static DEFINE_IDA(wakeup_ida);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076
77/**
78 * wakeup_source_create - Create a struct wakeup_source object.
79 * @name: Name of the new wakeup source.
80 */
81struct wakeup_source *wakeup_source_create(const char *name)
82{
83 struct wakeup_source *ws;
David Brazdil0f672f62019-12-10 10:32:29 +000084 const char *ws_name;
85 int id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086
David Brazdil0f672f62019-12-10 10:32:29 +000087 ws = kzalloc(sizeof(*ws), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088 if (!ws)
David Brazdil0f672f62019-12-10 10:32:29 +000089 goto err_ws;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090
David Brazdil0f672f62019-12-10 10:32:29 +000091 ws_name = kstrdup_const(name, GFP_KERNEL);
92 if (!ws_name)
93 goto err_name;
94 ws->name = ws_name;
95
96 id = ida_alloc(&wakeup_ida, GFP_KERNEL);
97 if (id < 0)
98 goto err_id;
99 ws->id = id;
100
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101 return ws;
David Brazdil0f672f62019-12-10 10:32:29 +0000102
103err_id:
104 kfree_const(ws->name);
105err_name:
106 kfree(ws);
107err_ws:
108 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109}
110EXPORT_SYMBOL_GPL(wakeup_source_create);
111
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112/*
113 * Record wakeup_source statistics being deleted into a dummy wakeup_source.
114 */
115static void wakeup_source_record(struct wakeup_source *ws)
116{
117 unsigned long flags;
118
119 spin_lock_irqsave(&deleted_ws.lock, flags);
120
121 if (ws->event_count) {
122 deleted_ws.total_time =
123 ktime_add(deleted_ws.total_time, ws->total_time);
124 deleted_ws.prevent_sleep_time =
125 ktime_add(deleted_ws.prevent_sleep_time,
126 ws->prevent_sleep_time);
127 deleted_ws.max_time =
128 ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
129 deleted_ws.max_time : ws->max_time;
130 deleted_ws.event_count += ws->event_count;
131 deleted_ws.active_count += ws->active_count;
132 deleted_ws.relax_count += ws->relax_count;
133 deleted_ws.expire_count += ws->expire_count;
134 deleted_ws.wakeup_count += ws->wakeup_count;
135 }
136
137 spin_unlock_irqrestore(&deleted_ws.lock, flags);
138}
139
David Brazdil0f672f62019-12-10 10:32:29 +0000140static void wakeup_source_free(struct wakeup_source *ws)
141{
142 ida_free(&wakeup_ida, ws->id);
143 kfree_const(ws->name);
144 kfree(ws);
145}
146
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147/**
148 * wakeup_source_destroy - Destroy a struct wakeup_source object.
149 * @ws: Wakeup source to destroy.
150 *
151 * Use only for wakeup source objects created with wakeup_source_create().
152 */
153void wakeup_source_destroy(struct wakeup_source *ws)
154{
155 if (!ws)
156 return;
157
David Brazdil0f672f62019-12-10 10:32:29 +0000158 __pm_relax(ws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159 wakeup_source_record(ws);
David Brazdil0f672f62019-12-10 10:32:29 +0000160 wakeup_source_free(ws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161}
162EXPORT_SYMBOL_GPL(wakeup_source_destroy);
163
164/**
165 * wakeup_source_add - Add given object to the list of wakeup sources.
166 * @ws: Wakeup source object to add to the list.
167 */
168void wakeup_source_add(struct wakeup_source *ws)
169{
170 unsigned long flags;
171
172 if (WARN_ON(!ws))
173 return;
174
175 spin_lock_init(&ws->lock);
176 timer_setup(&ws->timer, pm_wakeup_timer_fn, 0);
177 ws->active = false;
178
179 raw_spin_lock_irqsave(&events_lock, flags);
180 list_add_rcu(&ws->entry, &wakeup_sources);
181 raw_spin_unlock_irqrestore(&events_lock, flags);
182}
183EXPORT_SYMBOL_GPL(wakeup_source_add);
184
185/**
186 * wakeup_source_remove - Remove given object from the wakeup sources list.
187 * @ws: Wakeup source object to remove from the list.
188 */
189void wakeup_source_remove(struct wakeup_source *ws)
190{
191 unsigned long flags;
192
193 if (WARN_ON(!ws))
194 return;
195
196 raw_spin_lock_irqsave(&events_lock, flags);
197 list_del_rcu(&ws->entry);
198 raw_spin_unlock_irqrestore(&events_lock, flags);
199 synchronize_srcu(&wakeup_srcu);
David Brazdil0f672f62019-12-10 10:32:29 +0000200
201 del_timer_sync(&ws->timer);
202 /*
203 * Clear timer.function to make wakeup_source_not_registered() treat
204 * this wakeup source as not registered.
205 */
206 ws->timer.function = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207}
208EXPORT_SYMBOL_GPL(wakeup_source_remove);
209
210/**
211 * wakeup_source_register - Create wakeup source and add it to the list.
David Brazdil0f672f62019-12-10 10:32:29 +0000212 * @dev: Device this wakeup source is associated with (or NULL if virtual).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213 * @name: Name of the wakeup source to register.
214 */
David Brazdil0f672f62019-12-10 10:32:29 +0000215struct wakeup_source *wakeup_source_register(struct device *dev,
216 const char *name)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217{
218 struct wakeup_source *ws;
David Brazdil0f672f62019-12-10 10:32:29 +0000219 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220
221 ws = wakeup_source_create(name);
David Brazdil0f672f62019-12-10 10:32:29 +0000222 if (ws) {
223 if (!dev || device_is_registered(dev)) {
224 ret = wakeup_source_sysfs_add(dev, ws);
225 if (ret) {
226 wakeup_source_free(ws);
227 return NULL;
228 }
229 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000230 wakeup_source_add(ws);
David Brazdil0f672f62019-12-10 10:32:29 +0000231 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 return ws;
233}
234EXPORT_SYMBOL_GPL(wakeup_source_register);
235
236/**
237 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
238 * @ws: Wakeup source object to unregister.
239 */
240void wakeup_source_unregister(struct wakeup_source *ws)
241{
242 if (ws) {
243 wakeup_source_remove(ws);
Olivier Deprez0e641232021-09-23 10:07:05 +0200244 if (ws->dev)
245 wakeup_source_sysfs_remove(ws);
246
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 wakeup_source_destroy(ws);
248 }
249}
250EXPORT_SYMBOL_GPL(wakeup_source_unregister);
251
252/**
253 * device_wakeup_attach - Attach a wakeup source object to a device object.
254 * @dev: Device to handle.
255 * @ws: Wakeup source object to attach to @dev.
256 *
257 * This causes @dev to be treated as a wakeup device.
258 */
259static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
260{
261 spin_lock_irq(&dev->power.lock);
262 if (dev->power.wakeup) {
263 spin_unlock_irq(&dev->power.lock);
264 return -EEXIST;
265 }
266 dev->power.wakeup = ws;
267 if (dev->power.wakeirq)
268 device_wakeup_attach_irq(dev, dev->power.wakeirq);
269 spin_unlock_irq(&dev->power.lock);
270 return 0;
271}
272
273/**
274 * device_wakeup_enable - Enable given device to be a wakeup source.
275 * @dev: Device to handle.
276 *
277 * Create a wakeup source object, register it and attach it to @dev.
278 */
279int device_wakeup_enable(struct device *dev)
280{
281 struct wakeup_source *ws;
282 int ret;
283
284 if (!dev || !dev->power.can_wakeup)
285 return -EINVAL;
286
287 if (pm_suspend_target_state != PM_SUSPEND_ON)
288 dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__);
289
David Brazdil0f672f62019-12-10 10:32:29 +0000290 ws = wakeup_source_register(dev, dev_name(dev));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291 if (!ws)
292 return -ENOMEM;
293
294 ret = device_wakeup_attach(dev, ws);
295 if (ret)
296 wakeup_source_unregister(ws);
297
298 return ret;
299}
300EXPORT_SYMBOL_GPL(device_wakeup_enable);
301
302/**
303 * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source
304 * @dev: Device to handle
305 * @wakeirq: Device specific wakeirq entry
306 *
307 * Attach a device wakeirq to the wakeup source so the device
308 * wake IRQ can be configured automatically for suspend and
309 * resume.
310 *
311 * Call under the device's power.lock lock.
312 */
313void device_wakeup_attach_irq(struct device *dev,
314 struct wake_irq *wakeirq)
315{
316 struct wakeup_source *ws;
317
318 ws = dev->power.wakeup;
319 if (!ws)
320 return;
321
322 if (ws->wakeirq)
323 dev_err(dev, "Leftover wakeup IRQ found, overriding\n");
324
325 ws->wakeirq = wakeirq;
326}
327
328/**
329 * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source
330 * @dev: Device to handle
331 *
332 * Removes a device wakeirq from the wakeup source.
333 *
334 * Call under the device's power.lock lock.
335 */
336void device_wakeup_detach_irq(struct device *dev)
337{
338 struct wakeup_source *ws;
339
340 ws = dev->power.wakeup;
341 if (ws)
342 ws->wakeirq = NULL;
343}
344
345/**
346 * device_wakeup_arm_wake_irqs(void)
347 *
348 * Itereates over the list of device wakeirqs to arm them.
349 */
350void device_wakeup_arm_wake_irqs(void)
351{
352 struct wakeup_source *ws;
353 int srcuidx;
354
355 srcuidx = srcu_read_lock(&wakeup_srcu);
356 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
357 dev_pm_arm_wake_irq(ws->wakeirq);
358 srcu_read_unlock(&wakeup_srcu, srcuidx);
359}
360
361/**
362 * device_wakeup_disarm_wake_irqs(void)
363 *
364 * Itereates over the list of device wakeirqs to disarm them.
365 */
366void device_wakeup_disarm_wake_irqs(void)
367{
368 struct wakeup_source *ws;
369 int srcuidx;
370
371 srcuidx = srcu_read_lock(&wakeup_srcu);
372 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
373 dev_pm_disarm_wake_irq(ws->wakeirq);
374 srcu_read_unlock(&wakeup_srcu, srcuidx);
375}
376
377/**
378 * device_wakeup_detach - Detach a device's wakeup source object from it.
379 * @dev: Device to detach the wakeup source object from.
380 *
381 * After it returns, @dev will not be treated as a wakeup device any more.
382 */
383static struct wakeup_source *device_wakeup_detach(struct device *dev)
384{
385 struct wakeup_source *ws;
386
387 spin_lock_irq(&dev->power.lock);
388 ws = dev->power.wakeup;
389 dev->power.wakeup = NULL;
390 spin_unlock_irq(&dev->power.lock);
391 return ws;
392}
393
394/**
395 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
396 * @dev: Device to handle.
397 *
398 * Detach the @dev's wakeup source object from it, unregister this wakeup source
399 * object and destroy it.
400 */
401int device_wakeup_disable(struct device *dev)
402{
403 struct wakeup_source *ws;
404
405 if (!dev || !dev->power.can_wakeup)
406 return -EINVAL;
407
408 ws = device_wakeup_detach(dev);
409 wakeup_source_unregister(ws);
410 return 0;
411}
412EXPORT_SYMBOL_GPL(device_wakeup_disable);
413
414/**
415 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
416 * @dev: Device to handle.
417 * @capable: Whether or not @dev is capable of waking up the system from sleep.
418 *
419 * If @capable is set, set the @dev's power.can_wakeup flag and add its
420 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
421 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
422 *
423 * This function may sleep and it can't be called from any context where
424 * sleeping is not allowed.
425 */
426void device_set_wakeup_capable(struct device *dev, bool capable)
427{
428 if (!!dev->power.can_wakeup == !!capable)
429 return;
430
431 dev->power.can_wakeup = capable;
432 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
433 if (capable) {
434 int ret = wakeup_sysfs_add(dev);
435
436 if (ret)
437 dev_info(dev, "Wakeup sysfs attributes not added\n");
438 } else {
439 wakeup_sysfs_remove(dev);
440 }
441 }
442}
443EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
444
445/**
446 * device_init_wakeup - Device wakeup initialization.
447 * @dev: Device to handle.
448 * @enable: Whether or not to enable @dev as a wakeup device.
449 *
450 * By default, most devices should leave wakeup disabled. The exceptions are
451 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
452 * possibly network interfaces, etc. Also, devices that don't generate their
453 * own wakeup requests but merely forward requests from one bus to another
454 * (like PCI bridges) should have wakeup enabled by default.
455 */
456int device_init_wakeup(struct device *dev, bool enable)
457{
458 int ret = 0;
459
460 if (!dev)
461 return -EINVAL;
462
463 if (enable) {
464 device_set_wakeup_capable(dev, true);
465 ret = device_wakeup_enable(dev);
466 } else {
467 device_wakeup_disable(dev);
468 device_set_wakeup_capable(dev, false);
469 }
470
471 return ret;
472}
473EXPORT_SYMBOL_GPL(device_init_wakeup);
474
475/**
476 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
477 * @dev: Device to handle.
478 */
479int device_set_wakeup_enable(struct device *dev, bool enable)
480{
481 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
482}
483EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
484
485/**
486 * wakeup_source_not_registered - validate the given wakeup source.
487 * @ws: Wakeup source to be validated.
488 */
489static bool wakeup_source_not_registered(struct wakeup_source *ws)
490{
491 /*
492 * Use timer struct to check if the given source is initialized
493 * by wakeup_source_add.
494 */
495 return ws->timer.function != pm_wakeup_timer_fn;
496}
497
498/*
499 * The functions below use the observation that each wakeup event starts a
500 * period in which the system should not be suspended. The moment this period
501 * will end depends on how the wakeup event is going to be processed after being
502 * detected and all of the possible cases can be divided into two distinct
503 * groups.
504 *
505 * First, a wakeup event may be detected by the same functional unit that will
506 * carry out the entire processing of it and possibly will pass it to user space
507 * for further processing. In that case the functional unit that has detected
508 * the event may later "close" the "no suspend" period associated with it
509 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
510 * pm_relax(), balanced with each other, is supposed to be used in such
511 * situations.
512 *
513 * Second, a wakeup event may be detected by one functional unit and processed
514 * by another one. In that case the unit that has detected it cannot really
515 * "close" the "no suspend" period associated with it, unless it knows in
516 * advance what's going to happen to the event during processing. This
517 * knowledge, however, may not be available to it, so it can simply specify time
518 * to wait before the system can be suspended and pass it as the second
519 * argument of pm_wakeup_event().
520 *
521 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
522 * "no suspend" period will be ended either by the pm_relax(), or by the timer
523 * function executed when the timer expires, whichever comes first.
524 */
525
526/**
527 * wakup_source_activate - Mark given wakeup source as active.
528 * @ws: Wakeup source to handle.
529 *
530 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
531 * core of the event by incrementing the counter of of wakeup events being
532 * processed.
533 */
534static void wakeup_source_activate(struct wakeup_source *ws)
535{
536 unsigned int cec;
537
538 if (WARN_ONCE(wakeup_source_not_registered(ws),
539 "unregistered wakeup source\n"))
540 return;
541
542 ws->active = true;
543 ws->active_count++;
544 ws->last_time = ktime_get();
545 if (ws->autosleep_enabled)
546 ws->start_prevent_time = ws->last_time;
547
548 /* Increment the counter of events in progress. */
549 cec = atomic_inc_return(&combined_event_count);
550
551 trace_wakeup_source_activate(ws->name, cec);
552}
553
554/**
555 * wakeup_source_report_event - Report wakeup event using the given source.
556 * @ws: Wakeup source to report the event for.
557 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
558 */
559static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
560{
561 ws->event_count++;
562 /* This is racy, but the counter is approximate anyway. */
563 if (events_check_enabled)
564 ws->wakeup_count++;
565
566 if (!ws->active)
567 wakeup_source_activate(ws);
568
569 if (hard)
570 pm_system_wakeup();
571}
572
573/**
574 * __pm_stay_awake - Notify the PM core of a wakeup event.
575 * @ws: Wakeup source object associated with the source of the event.
576 *
577 * It is safe to call this function from interrupt context.
578 */
579void __pm_stay_awake(struct wakeup_source *ws)
580{
581 unsigned long flags;
582
583 if (!ws)
584 return;
585
586 spin_lock_irqsave(&ws->lock, flags);
587
588 wakeup_source_report_event(ws, false);
589 del_timer(&ws->timer);
590 ws->timer_expires = 0;
591
592 spin_unlock_irqrestore(&ws->lock, flags);
593}
594EXPORT_SYMBOL_GPL(__pm_stay_awake);
595
596/**
597 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
598 * @dev: Device the wakeup event is related to.
599 *
600 * Notify the PM core of a wakeup event (signaled by @dev) by calling
601 * __pm_stay_awake for the @dev's wakeup source object.
602 *
603 * Call this function after detecting of a wakeup event if pm_relax() is going
604 * to be called directly after processing the event (and possibly passing it to
605 * user space for further processing).
606 */
607void pm_stay_awake(struct device *dev)
608{
609 unsigned long flags;
610
611 if (!dev)
612 return;
613
614 spin_lock_irqsave(&dev->power.lock, flags);
615 __pm_stay_awake(dev->power.wakeup);
616 spin_unlock_irqrestore(&dev->power.lock, flags);
617}
618EXPORT_SYMBOL_GPL(pm_stay_awake);
619
620#ifdef CONFIG_PM_AUTOSLEEP
621static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
622{
623 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
624 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
625}
626#else
627static inline void update_prevent_sleep_time(struct wakeup_source *ws,
628 ktime_t now) {}
629#endif
630
631/**
632 * wakup_source_deactivate - Mark given wakeup source as inactive.
633 * @ws: Wakeup source to handle.
634 *
635 * Update the @ws' statistics and notify the PM core that the wakeup source has
636 * become inactive by decrementing the counter of wakeup events being processed
637 * and incrementing the counter of registered wakeup events.
638 */
639static void wakeup_source_deactivate(struct wakeup_source *ws)
640{
641 unsigned int cnt, inpr, cec;
642 ktime_t duration;
643 ktime_t now;
644
645 ws->relax_count++;
646 /*
647 * __pm_relax() may be called directly or from a timer function.
648 * If it is called directly right after the timer function has been
649 * started, but before the timer function calls __pm_relax(), it is
650 * possible that __pm_stay_awake() will be called in the meantime and
651 * will set ws->active. Then, ws->active may be cleared immediately
652 * by the __pm_relax() called from the timer function, but in such a
653 * case ws->relax_count will be different from ws->active_count.
654 */
655 if (ws->relax_count != ws->active_count) {
656 ws->relax_count--;
657 return;
658 }
659
660 ws->active = false;
661
662 now = ktime_get();
663 duration = ktime_sub(now, ws->last_time);
664 ws->total_time = ktime_add(ws->total_time, duration);
665 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
666 ws->max_time = duration;
667
668 ws->last_time = now;
669 del_timer(&ws->timer);
670 ws->timer_expires = 0;
671
672 if (ws->autosleep_enabled)
673 update_prevent_sleep_time(ws, now);
674
675 /*
676 * Increment the counter of registered wakeup events and decrement the
677 * couter of wakeup events in progress simultaneously.
678 */
679 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
680 trace_wakeup_source_deactivate(ws->name, cec);
681
682 split_counters(&cnt, &inpr);
683 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
684 wake_up(&wakeup_count_wait_queue);
685}
686
687/**
688 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
689 * @ws: Wakeup source object associated with the source of the event.
690 *
691 * Call this function for wakeup events whose processing started with calling
692 * __pm_stay_awake().
693 *
694 * It is safe to call it from interrupt context.
695 */
696void __pm_relax(struct wakeup_source *ws)
697{
698 unsigned long flags;
699
700 if (!ws)
701 return;
702
703 spin_lock_irqsave(&ws->lock, flags);
704 if (ws->active)
705 wakeup_source_deactivate(ws);
706 spin_unlock_irqrestore(&ws->lock, flags);
707}
708EXPORT_SYMBOL_GPL(__pm_relax);
709
710/**
711 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
712 * @dev: Device that signaled the event.
713 *
714 * Execute __pm_relax() for the @dev's wakeup source object.
715 */
716void pm_relax(struct device *dev)
717{
718 unsigned long flags;
719
720 if (!dev)
721 return;
722
723 spin_lock_irqsave(&dev->power.lock, flags);
724 __pm_relax(dev->power.wakeup);
725 spin_unlock_irqrestore(&dev->power.lock, flags);
726}
727EXPORT_SYMBOL_GPL(pm_relax);
728
729/**
730 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
731 * @data: Address of the wakeup source object associated with the event source.
732 *
733 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
734 * in @data if it is currently active and its timer has not been canceled and
735 * the expiration time of the timer is not in future.
736 */
737static void pm_wakeup_timer_fn(struct timer_list *t)
738{
739 struct wakeup_source *ws = from_timer(ws, t, timer);
740 unsigned long flags;
741
742 spin_lock_irqsave(&ws->lock, flags);
743
744 if (ws->active && ws->timer_expires
745 && time_after_eq(jiffies, ws->timer_expires)) {
746 wakeup_source_deactivate(ws);
747 ws->expire_count++;
748 }
749
750 spin_unlock_irqrestore(&ws->lock, flags);
751}
752
753/**
754 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
755 * @ws: Wakeup source object associated with the event source.
756 * @msec: Anticipated event processing time (in milliseconds).
757 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
758 *
759 * Notify the PM core of a wakeup event whose source is @ws that will take
760 * approximately @msec milliseconds to be processed by the kernel. If @ws is
761 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
762 * execute pm_wakeup_timer_fn() in future.
763 *
764 * It is safe to call this function from interrupt context.
765 */
766void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
767{
768 unsigned long flags;
769 unsigned long expires;
770
771 if (!ws)
772 return;
773
774 spin_lock_irqsave(&ws->lock, flags);
775
776 wakeup_source_report_event(ws, hard);
777
778 if (!msec) {
779 wakeup_source_deactivate(ws);
780 goto unlock;
781 }
782
783 expires = jiffies + msecs_to_jiffies(msec);
784 if (!expires)
785 expires = 1;
786
787 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
788 mod_timer(&ws->timer, expires);
789 ws->timer_expires = expires;
790 }
791
792 unlock:
793 spin_unlock_irqrestore(&ws->lock, flags);
794}
795EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
796
797/**
David Brazdil0f672f62019-12-10 10:32:29 +0000798 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000799 * @dev: Device the wakeup event is related to.
800 * @msec: Anticipated event processing time (in milliseconds).
801 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
802 *
803 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
804 */
805void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
806{
807 unsigned long flags;
808
809 if (!dev)
810 return;
811
812 spin_lock_irqsave(&dev->power.lock, flags);
813 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
814 spin_unlock_irqrestore(&dev->power.lock, flags);
815}
816EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
817
818void pm_print_active_wakeup_sources(void)
819{
820 struct wakeup_source *ws;
821 int srcuidx, active = 0;
822 struct wakeup_source *last_activity_ws = NULL;
823
824 srcuidx = srcu_read_lock(&wakeup_srcu);
825 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
826 if (ws->active) {
David Brazdil0f672f62019-12-10 10:32:29 +0000827 pm_pr_dbg("active wakeup source: %s\n", ws->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828 active = 1;
829 } else if (!active &&
830 (!last_activity_ws ||
831 ktime_to_ns(ws->last_time) >
832 ktime_to_ns(last_activity_ws->last_time))) {
833 last_activity_ws = ws;
834 }
835 }
836
837 if (!active && last_activity_ws)
David Brazdil0f672f62019-12-10 10:32:29 +0000838 pm_pr_dbg("last active wakeup source: %s\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000839 last_activity_ws->name);
840 srcu_read_unlock(&wakeup_srcu, srcuidx);
841}
842EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
843
844/**
845 * pm_wakeup_pending - Check if power transition in progress should be aborted.
846 *
847 * Compare the current number of registered wakeup events with its preserved
848 * value from the past and return true if new wakeup events have been registered
849 * since the old value was stored. Also return true if the current number of
850 * wakeup events being processed is different from zero.
851 */
852bool pm_wakeup_pending(void)
853{
854 unsigned long flags;
855 bool ret = false;
856
857 raw_spin_lock_irqsave(&events_lock, flags);
858 if (events_check_enabled) {
859 unsigned int cnt, inpr;
860
861 split_counters(&cnt, &inpr);
862 ret = (cnt != saved_count || inpr > 0);
863 events_check_enabled = !ret;
864 }
865 raw_spin_unlock_irqrestore(&events_lock, flags);
866
867 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +0000868 pm_pr_dbg("Wakeup pending, aborting suspend\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869 pm_print_active_wakeup_sources();
870 }
871
872 return ret || atomic_read(&pm_abort_suspend) > 0;
873}
874
875void pm_system_wakeup(void)
876{
877 atomic_inc(&pm_abort_suspend);
878 s2idle_wake();
879}
880EXPORT_SYMBOL_GPL(pm_system_wakeup);
881
882void pm_system_cancel_wakeup(void)
883{
David Brazdil0f672f62019-12-10 10:32:29 +0000884 atomic_dec_if_positive(&pm_abort_suspend);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885}
886
887void pm_wakeup_clear(bool reset)
888{
889 pm_wakeup_irq = 0;
890 if (reset)
891 atomic_set(&pm_abort_suspend, 0);
892}
893
894void pm_system_irq_wakeup(unsigned int irq_number)
895{
896 if (pm_wakeup_irq == 0) {
897 pm_wakeup_irq = irq_number;
898 pm_system_wakeup();
899 }
900}
901
902/**
903 * pm_get_wakeup_count - Read the number of registered wakeup events.
904 * @count: Address to store the value at.
905 * @block: Whether or not to block.
906 *
907 * Store the number of registered wakeup events at the address in @count. If
908 * @block is set, block until the current number of wakeup events being
909 * processed is zero.
910 *
911 * Return 'false' if the current number of wakeup events being processed is
912 * nonzero. Otherwise return 'true'.
913 */
914bool pm_get_wakeup_count(unsigned int *count, bool block)
915{
916 unsigned int cnt, inpr;
917
918 if (block) {
919 DEFINE_WAIT(wait);
920
921 for (;;) {
922 prepare_to_wait(&wakeup_count_wait_queue, &wait,
923 TASK_INTERRUPTIBLE);
924 split_counters(&cnt, &inpr);
925 if (inpr == 0 || signal_pending(current))
926 break;
927 pm_print_active_wakeup_sources();
928 schedule();
929 }
930 finish_wait(&wakeup_count_wait_queue, &wait);
931 }
932
933 split_counters(&cnt, &inpr);
934 *count = cnt;
935 return !inpr;
936}
937
938/**
939 * pm_save_wakeup_count - Save the current number of registered wakeup events.
940 * @count: Value to compare with the current number of registered wakeup events.
941 *
942 * If @count is equal to the current number of registered wakeup events and the
943 * current number of wakeup events being processed is zero, store @count as the
944 * old number of registered wakeup events for pm_check_wakeup_events(), enable
945 * wakeup events detection and return 'true'. Otherwise disable wakeup events
946 * detection and return 'false'.
947 */
948bool pm_save_wakeup_count(unsigned int count)
949{
950 unsigned int cnt, inpr;
951 unsigned long flags;
952
953 events_check_enabled = false;
954 raw_spin_lock_irqsave(&events_lock, flags);
955 split_counters(&cnt, &inpr);
956 if (cnt == count && inpr == 0) {
957 saved_count = count;
958 events_check_enabled = true;
959 }
960 raw_spin_unlock_irqrestore(&events_lock, flags);
961 return events_check_enabled;
962}
963
964#ifdef CONFIG_PM_AUTOSLEEP
965/**
966 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
967 * @enabled: Whether to set or to clear the autosleep_enabled flags.
968 */
969void pm_wakep_autosleep_enabled(bool set)
970{
971 struct wakeup_source *ws;
972 ktime_t now = ktime_get();
973 int srcuidx;
974
975 srcuidx = srcu_read_lock(&wakeup_srcu);
976 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
977 spin_lock_irq(&ws->lock);
978 if (ws->autosleep_enabled != set) {
979 ws->autosleep_enabled = set;
980 if (ws->active) {
981 if (set)
982 ws->start_prevent_time = now;
983 else
984 update_prevent_sleep_time(ws, now);
985 }
986 }
987 spin_unlock_irq(&ws->lock);
988 }
989 srcu_read_unlock(&wakeup_srcu, srcuidx);
990}
991#endif /* CONFIG_PM_AUTOSLEEP */
992
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000993/**
994 * print_wakeup_source_stats - Print wakeup source statistics information.
995 * @m: seq_file to print the statistics into.
996 * @ws: Wakeup source object to print the statistics for.
997 */
998static int print_wakeup_source_stats(struct seq_file *m,
999 struct wakeup_source *ws)
1000{
1001 unsigned long flags;
1002 ktime_t total_time;
1003 ktime_t max_time;
1004 unsigned long active_count;
1005 ktime_t active_time;
1006 ktime_t prevent_sleep_time;
1007
1008 spin_lock_irqsave(&ws->lock, flags);
1009
1010 total_time = ws->total_time;
1011 max_time = ws->max_time;
1012 prevent_sleep_time = ws->prevent_sleep_time;
1013 active_count = ws->active_count;
1014 if (ws->active) {
1015 ktime_t now = ktime_get();
1016
1017 active_time = ktime_sub(now, ws->last_time);
1018 total_time = ktime_add(total_time, active_time);
1019 if (active_time > max_time)
1020 max_time = active_time;
1021
1022 if (ws->autosleep_enabled)
1023 prevent_sleep_time = ktime_add(prevent_sleep_time,
1024 ktime_sub(now, ws->start_prevent_time));
1025 } else {
1026 active_time = 0;
1027 }
1028
1029 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",
1030 ws->name, active_count, ws->event_count,
1031 ws->wakeup_count, ws->expire_count,
1032 ktime_to_ms(active_time), ktime_to_ms(total_time),
1033 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1034 ktime_to_ms(prevent_sleep_time));
1035
1036 spin_unlock_irqrestore(&ws->lock, flags);
1037
1038 return 0;
1039}
1040
1041static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1042 loff_t *pos)
1043{
1044 struct wakeup_source *ws;
1045 loff_t n = *pos;
1046 int *srcuidx = m->private;
1047
1048 if (n == 0) {
1049 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1050 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1051 "last_change\tprevent_suspend_time\n");
1052 }
1053
1054 *srcuidx = srcu_read_lock(&wakeup_srcu);
1055 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
1056 if (n-- <= 0)
1057 return ws;
1058 }
1059
1060 return NULL;
1061}
1062
1063static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1064 void *v, loff_t *pos)
1065{
1066 struct wakeup_source *ws = v;
1067 struct wakeup_source *next_ws = NULL;
1068
1069 ++(*pos);
1070
1071 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1072 next_ws = ws;
1073 break;
1074 }
1075
Olivier Deprez0e641232021-09-23 10:07:05 +02001076 if (!next_ws)
1077 print_wakeup_source_stats(m, &deleted_ws);
1078
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001079 return next_ws;
1080}
1081
1082static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1083{
1084 int *srcuidx = m->private;
1085
1086 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1087}
1088
1089/**
1090 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1091 * @m: seq_file to print the statistics into.
1092 * @v: wakeup_source of each iteration
1093 */
1094static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1095{
1096 struct wakeup_source *ws = v;
1097
1098 print_wakeup_source_stats(m, ws);
1099
1100 return 0;
1101}
1102
1103static const struct seq_operations wakeup_sources_stats_seq_ops = {
1104 .start = wakeup_sources_stats_seq_start,
1105 .next = wakeup_sources_stats_seq_next,
1106 .stop = wakeup_sources_stats_seq_stop,
1107 .show = wakeup_sources_stats_seq_show,
1108};
1109
1110static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1111{
1112 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
1113}
1114
1115static const struct file_operations wakeup_sources_stats_fops = {
1116 .owner = THIS_MODULE,
1117 .open = wakeup_sources_stats_open,
1118 .read = seq_read,
1119 .llseek = seq_lseek,
1120 .release = seq_release_private,
1121};
1122
1123static int __init wakeup_sources_debugfs_init(void)
1124{
David Brazdil0f672f62019-12-10 10:32:29 +00001125 debugfs_create_file("wakeup_sources", S_IRUGO, NULL, NULL,
1126 &wakeup_sources_stats_fops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001127 return 0;
1128}
1129
1130postcore_initcall(wakeup_sources_debugfs_init);