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