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