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 int device_wakeup_disable(struct device *dev)
458 {
459 struct wakeup_source *ws;
460
461 if (!dev || !dev->power.can_wakeup)
462 return -EINVAL;
463
464 ws = device_wakeup_detach(dev);
465 wakeup_source_unregister(ws);
466 return 0;
467 }
468 EXPORT_SYMBOL_GPL(device_wakeup_disable);
469
470 /**
471 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
472 * @dev: Device to handle.
473 * @capable: Whether or not @dev is capable of waking up the system from sleep.
474 *
475 * If @capable is set, set the @dev's power.can_wakeup flag and add its
476 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
477 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
478 *
479 * This function may sleep and it can't be called from any context where
480 * sleeping is not allowed.
481 */
device_set_wakeup_capable(struct device * dev,bool capable)482 void device_set_wakeup_capable(struct device *dev, bool capable)
483 {
484 if (!!dev->power.can_wakeup == !!capable)
485 return;
486
487 dev->power.can_wakeup = capable;
488 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
489 if (capable) {
490 int ret = wakeup_sysfs_add(dev);
491
492 if (ret)
493 dev_info(dev, "Wakeup sysfs attributes not added\n");
494 } else {
495 wakeup_sysfs_remove(dev);
496 }
497 }
498 }
499 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
500
501 /**
502 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
503 * @dev: Device to handle.
504 * @enable: enable/disable flag
505 */
device_set_wakeup_enable(struct device * dev,bool enable)506 int device_set_wakeup_enable(struct device *dev, bool enable)
507 {
508 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
509 }
510 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
511
512 /**
513 * wakeup_source_not_registered - validate the given wakeup source.
514 * @ws: Wakeup source to be validated.
515 */
wakeup_source_not_registered(struct wakeup_source * ws)516 static bool wakeup_source_not_registered(struct wakeup_source *ws)
517 {
518 /*
519 * Use timer struct to check if the given source is initialized
520 * by wakeup_source_add.
521 */
522 return ws->timer.function != pm_wakeup_timer_fn;
523 }
524
525 /*
526 * The functions below use the observation that each wakeup event starts a
527 * period in which the system should not be suspended. The moment this period
528 * will end depends on how the wakeup event is going to be processed after being
529 * detected and all of the possible cases can be divided into two distinct
530 * groups.
531 *
532 * First, a wakeup event may be detected by the same functional unit that will
533 * carry out the entire processing of it and possibly will pass it to user space
534 * for further processing. In that case the functional unit that has detected
535 * the event may later "close" the "no suspend" period associated with it
536 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
537 * pm_relax(), balanced with each other, is supposed to be used in such
538 * situations.
539 *
540 * Second, a wakeup event may be detected by one functional unit and processed
541 * by another one. In that case the unit that has detected it cannot really
542 * "close" the "no suspend" period associated with it, unless it knows in
543 * advance what's going to happen to the event during processing. This
544 * knowledge, however, may not be available to it, so it can simply specify time
545 * to wait before the system can be suspended and pass it as the second
546 * argument of pm_wakeup_event().
547 *
548 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
549 * "no suspend" period will be ended either by the pm_relax(), or by the timer
550 * function executed when the timer expires, whichever comes first.
551 */
552
553 /**
554 * wakeup_source_activate - Mark given wakeup source as active.
555 * @ws: Wakeup source to handle.
556 *
557 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
558 * core of the event by incrementing the counter of the wakeup events being
559 * processed.
560 */
wakeup_source_activate(struct wakeup_source * ws)561 static void wakeup_source_activate(struct wakeup_source *ws)
562 {
563 unsigned int cec;
564
565 if (WARN_ONCE(wakeup_source_not_registered(ws),
566 "unregistered wakeup source\n"))
567 return;
568
569 ws->active = true;
570 ws->active_count++;
571 ws->last_time = ktime_get();
572 if (ws->autosleep_enabled)
573 ws->start_prevent_time = ws->last_time;
574
575 /* Increment the counter of events in progress. */
576 cec = atomic_inc_return(&combined_event_count);
577
578 trace_wakeup_source_activate(ws->name, cec);
579 }
580
581 /**
582 * wakeup_source_report_event - Report wakeup event using the given source.
583 * @ws: Wakeup source to report the event for.
584 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
585 */
wakeup_source_report_event(struct wakeup_source * ws,bool hard)586 static void wakeup_source_report_event(struct wakeup_source *ws, bool hard)
587 {
588 ws->event_count++;
589 /* This is racy, but the counter is approximate anyway. */
590 if (events_check_enabled)
591 ws->wakeup_count++;
592
593 if (!ws->active)
594 wakeup_source_activate(ws);
595
596 if (hard)
597 pm_system_wakeup();
598 }
599
600 /**
601 * __pm_stay_awake - Notify the PM core of a wakeup event.
602 * @ws: Wakeup source object associated with the source of the event.
603 *
604 * It is safe to call this function from interrupt context.
605 */
__pm_stay_awake(struct wakeup_source * ws)606 void __pm_stay_awake(struct wakeup_source *ws)
607 {
608 unsigned long flags;
609
610 if (!ws)
611 return;
612
613 spin_lock_irqsave(&ws->lock, flags);
614
615 wakeup_source_report_event(ws, false);
616 del_timer(&ws->timer);
617 ws->timer_expires = 0;
618
619 spin_unlock_irqrestore(&ws->lock, flags);
620 }
621 EXPORT_SYMBOL_GPL(__pm_stay_awake);
622
623 /**
624 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
625 * @dev: Device the wakeup event is related to.
626 *
627 * Notify the PM core of a wakeup event (signaled by @dev) by calling
628 * __pm_stay_awake for the @dev's wakeup source object.
629 *
630 * Call this function after detecting of a wakeup event if pm_relax() is going
631 * to be called directly after processing the event (and possibly passing it to
632 * user space for further processing).
633 */
pm_stay_awake(struct device * dev)634 void pm_stay_awake(struct device *dev)
635 {
636 unsigned long flags;
637
638 if (!dev)
639 return;
640
641 spin_lock_irqsave(&dev->power.lock, flags);
642 __pm_stay_awake(dev->power.wakeup);
643 spin_unlock_irqrestore(&dev->power.lock, flags);
644 }
645 EXPORT_SYMBOL_GPL(pm_stay_awake);
646
647 #ifdef CONFIG_PM_AUTOSLEEP
update_prevent_sleep_time(struct wakeup_source * ws,ktime_t now)648 static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now)
649 {
650 ktime_t delta = ktime_sub(now, ws->start_prevent_time);
651 ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta);
652 }
653 #else
update_prevent_sleep_time(struct wakeup_source * ws,ktime_t now)654 static inline void update_prevent_sleep_time(struct wakeup_source *ws,
655 ktime_t now) {}
656 #endif
657
658 /**
659 * wakeup_source_deactivate - Mark given wakeup source as inactive.
660 * @ws: Wakeup source to handle.
661 *
662 * Update the @ws' statistics and notify the PM core that the wakeup source has
663 * become inactive by decrementing the counter of wakeup events being processed
664 * and incrementing the counter of registered wakeup events.
665 */
wakeup_source_deactivate(struct wakeup_source * ws)666 static void wakeup_source_deactivate(struct wakeup_source *ws)
667 {
668 unsigned int cnt, inpr, cec;
669 ktime_t duration;
670 ktime_t now;
671
672 ws->relax_count++;
673 /*
674 * __pm_relax() may be called directly or from a timer function.
675 * If it is called directly right after the timer function has been
676 * started, but before the timer function calls __pm_relax(), it is
677 * possible that __pm_stay_awake() will be called in the meantime and
678 * will set ws->active. Then, ws->active may be cleared immediately
679 * by the __pm_relax() called from the timer function, but in such a
680 * case ws->relax_count will be different from ws->active_count.
681 */
682 if (ws->relax_count != ws->active_count) {
683 ws->relax_count--;
684 return;
685 }
686
687 ws->active = false;
688
689 now = ktime_get();
690 duration = ktime_sub(now, ws->last_time);
691 ws->total_time = ktime_add(ws->total_time, duration);
692 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
693 ws->max_time = duration;
694
695 ws->last_time = now;
696 del_timer(&ws->timer);
697 ws->timer_expires = 0;
698
699 if (ws->autosleep_enabled)
700 update_prevent_sleep_time(ws, now);
701
702 /*
703 * Increment the counter of registered wakeup events and decrement the
704 * counter of wakeup events in progress simultaneously.
705 */
706 cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);
707 trace_wakeup_source_deactivate(ws->name, cec);
708
709 split_counters(&cnt, &inpr);
710 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
711 wake_up(&wakeup_count_wait_queue);
712 }
713
714 /**
715 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
716 * @ws: Wakeup source object associated with the source of the event.
717 *
718 * Call this function for wakeup events whose processing started with calling
719 * __pm_stay_awake().
720 *
721 * It is safe to call it from interrupt context.
722 */
__pm_relax(struct wakeup_source * ws)723 void __pm_relax(struct wakeup_source *ws)
724 {
725 unsigned long flags;
726
727 if (!ws)
728 return;
729
730 spin_lock_irqsave(&ws->lock, flags);
731 if (ws->active)
732 wakeup_source_deactivate(ws);
733 spin_unlock_irqrestore(&ws->lock, flags);
734 }
735 EXPORT_SYMBOL_GPL(__pm_relax);
736
737 /**
738 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
739 * @dev: Device that signaled the event.
740 *
741 * Execute __pm_relax() for the @dev's wakeup source object.
742 */
pm_relax(struct device * dev)743 void pm_relax(struct device *dev)
744 {
745 unsigned long flags;
746
747 if (!dev)
748 return;
749
750 spin_lock_irqsave(&dev->power.lock, flags);
751 __pm_relax(dev->power.wakeup);
752 spin_unlock_irqrestore(&dev->power.lock, flags);
753 }
754 EXPORT_SYMBOL_GPL(pm_relax);
755
756 /**
757 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
758 * @t: timer list
759 *
760 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
761 * in @data if it is currently active and its timer has not been canceled and
762 * the expiration time of the timer is not in future.
763 */
pm_wakeup_timer_fn(struct timer_list * t)764 static void pm_wakeup_timer_fn(struct timer_list *t)
765 {
766 struct wakeup_source *ws = from_timer(ws, t, timer);
767 unsigned long flags;
768
769 spin_lock_irqsave(&ws->lock, flags);
770
771 if (ws->active && ws->timer_expires
772 && time_after_eq(jiffies, ws->timer_expires)) {
773 wakeup_source_deactivate(ws);
774 ws->expire_count++;
775 }
776
777 spin_unlock_irqrestore(&ws->lock, flags);
778 }
779
780 /**
781 * pm_wakeup_ws_event - Notify the PM core of a wakeup event.
782 * @ws: Wakeup source object associated with the event source.
783 * @msec: Anticipated event processing time (in milliseconds).
784 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
785 *
786 * Notify the PM core of a wakeup event whose source is @ws that will take
787 * approximately @msec milliseconds to be processed by the kernel. If @ws is
788 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
789 * execute pm_wakeup_timer_fn() in future.
790 *
791 * It is safe to call this function from interrupt context.
792 */
pm_wakeup_ws_event(struct wakeup_source * ws,unsigned int msec,bool hard)793 void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard)
794 {
795 unsigned long flags;
796 unsigned long expires;
797
798 if (!ws)
799 return;
800
801 spin_lock_irqsave(&ws->lock, flags);
802
803 wakeup_source_report_event(ws, hard);
804
805 if (!msec) {
806 wakeup_source_deactivate(ws);
807 goto unlock;
808 }
809
810 expires = jiffies + msecs_to_jiffies(msec);
811 if (!expires)
812 expires = 1;
813
814 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
815 mod_timer(&ws->timer, expires);
816 ws->timer_expires = expires;
817 }
818
819 unlock:
820 spin_unlock_irqrestore(&ws->lock, flags);
821 }
822 EXPORT_SYMBOL_GPL(pm_wakeup_ws_event);
823
824 /**
825 * pm_wakeup_dev_event - Notify the PM core of a wakeup event.
826 * @dev: Device the wakeup event is related to.
827 * @msec: Anticipated event processing time (in milliseconds).
828 * @hard: If set, abort suspends in progress and wake up from suspend-to-idle.
829 *
830 * Call pm_wakeup_ws_event() for the @dev's wakeup source object.
831 */
pm_wakeup_dev_event(struct device * dev,unsigned int msec,bool hard)832 void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard)
833 {
834 unsigned long flags;
835
836 if (!dev)
837 return;
838
839 spin_lock_irqsave(&dev->power.lock, flags);
840 pm_wakeup_ws_event(dev->power.wakeup, msec, hard);
841 spin_unlock_irqrestore(&dev->power.lock, flags);
842 }
843 EXPORT_SYMBOL_GPL(pm_wakeup_dev_event);
844
pm_get_active_wakeup_sources(char * pending_wakeup_source,size_t max)845 void pm_get_active_wakeup_sources(char *pending_wakeup_source, size_t max)
846 {
847 struct wakeup_source *ws, *last_active_ws = NULL;
848 int len = 0;
849 bool active = false;
850
851 rcu_read_lock();
852 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
853 if (ws->active && len < max) {
854 if (!active)
855 len += scnprintf(pending_wakeup_source, max,
856 "Pending Wakeup Sources: ");
857 len += scnprintf(pending_wakeup_source + len, max - len,
858 "%s ", ws->name);
859 active = true;
860 } else if (!active &&
861 (!last_active_ws ||
862 ktime_to_ns(ws->last_time) >
863 ktime_to_ns(last_active_ws->last_time))) {
864 last_active_ws = ws;
865 }
866 }
867 if (!active && last_active_ws) {
868 scnprintf(pending_wakeup_source, max,
869 "Last active Wakeup Source: %s",
870 last_active_ws->name);
871 }
872 rcu_read_unlock();
873 }
874 EXPORT_SYMBOL_GPL(pm_get_active_wakeup_sources);
875
pm_print_active_wakeup_sources(void)876 void pm_print_active_wakeup_sources(void)
877 {
878 struct wakeup_source *ws;
879 int srcuidx, active = 0;
880 struct wakeup_source *last_activity_ws = NULL;
881
882 srcuidx = srcu_read_lock(&wakeup_srcu);
883 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
884 if (ws->active) {
885 pm_pr_dbg("active wakeup source: %s\n", ws->name);
886 active = 1;
887 } else if (!active &&
888 (!last_activity_ws ||
889 ktime_to_ns(ws->last_time) >
890 ktime_to_ns(last_activity_ws->last_time))) {
891 last_activity_ws = ws;
892 }
893 }
894
895 if (!active && last_activity_ws)
896 pm_pr_dbg("last active wakeup source: %s\n",
897 last_activity_ws->name);
898 srcu_read_unlock(&wakeup_srcu, srcuidx);
899 }
900 EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources);
901
902 /**
903 * pm_wakeup_pending - Check if power transition in progress should be aborted.
904 *
905 * Compare the current number of registered wakeup events with its preserved
906 * value from the past and return true if new wakeup events have been registered
907 * since the old value was stored. Also return true if the current number of
908 * wakeup events being processed is different from zero.
909 */
pm_wakeup_pending(void)910 bool pm_wakeup_pending(void)
911 {
912 unsigned long flags;
913 bool ret = false;
914 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
915
916 raw_spin_lock_irqsave(&events_lock, flags);
917 if (events_check_enabled) {
918 unsigned int cnt, inpr;
919
920 split_counters(&cnt, &inpr);
921 ret = (cnt != saved_count || inpr > 0);
922 events_check_enabled = !ret;
923 }
924 raw_spin_unlock_irqrestore(&events_lock, flags);
925
926 if (ret) {
927 pm_pr_dbg("Wakeup pending, aborting suspend\n");
928 pm_print_active_wakeup_sources();
929 pm_get_active_wakeup_sources(suspend_abort,
930 MAX_SUSPEND_ABORT_LEN);
931 log_suspend_abort_reason(suspend_abort);
932 pr_info("PM: %s\n", suspend_abort);
933 }
934
935 return ret || atomic_read(&pm_abort_suspend) > 0;
936 }
937 EXPORT_SYMBOL_GPL(pm_wakeup_pending);
938
pm_system_wakeup(void)939 void pm_system_wakeup(void)
940 {
941 atomic_inc(&pm_abort_suspend);
942 s2idle_wake();
943 }
944 EXPORT_SYMBOL_GPL(pm_system_wakeup);
945
pm_system_cancel_wakeup(void)946 void pm_system_cancel_wakeup(void)
947 {
948 atomic_dec_if_positive(&pm_abort_suspend);
949 }
950
pm_wakeup_clear(unsigned int irq_number)951 void pm_wakeup_clear(unsigned int irq_number)
952 {
953 raw_spin_lock_irq(&wakeup_irq_lock);
954
955 if (irq_number && wakeup_irq[0] == irq_number)
956 wakeup_irq[0] = wakeup_irq[1];
957 else
958 wakeup_irq[0] = 0;
959
960 wakeup_irq[1] = 0;
961
962 raw_spin_unlock_irq(&wakeup_irq_lock);
963
964 if (!irq_number)
965 atomic_set(&pm_abort_suspend, 0);
966 }
967
pm_system_irq_wakeup(unsigned int irq_number)968 void pm_system_irq_wakeup(unsigned int irq_number)
969 {
970 unsigned long flags;
971
972 raw_spin_lock_irqsave(&wakeup_irq_lock, flags);
973
974 if (wakeup_irq[0] == 0)
975 wakeup_irq[0] = irq_number;
976 else if (wakeup_irq[1] == 0)
977 wakeup_irq[1] = irq_number;
978 else
979 irq_number = 0;
980
981 pm_pr_dbg("Triggering wakeup from IRQ %d\n", irq_number);
982
983 raw_spin_unlock_irqrestore(&wakeup_irq_lock, flags);
984
985 if (irq_number) {
986 struct irq_desc *desc;
987 const char *name = "null";
988
989 desc = irq_to_desc(irq_number);
990 if (desc == NULL)
991 name = "stray irq";
992 else if (desc->action && desc->action->name)
993 name = desc->action->name;
994
995 log_irq_wakeup_reason(irq_number);
996 pr_warn("%s: %d triggered %s\n", __func__, irq_number, name);
997
998 pm_system_wakeup();
999 }
1000 }
1001
pm_wakeup_irq(void)1002 unsigned int pm_wakeup_irq(void)
1003 {
1004 return wakeup_irq[0];
1005 }
1006 EXPORT_SYMBOL_GPL(pm_wakeup_irq);
1007
1008 /**
1009 * pm_get_wakeup_count - Read the number of registered wakeup events.
1010 * @count: Address to store the value at.
1011 * @block: Whether or not to block.
1012 *
1013 * Store the number of registered wakeup events at the address in @count. If
1014 * @block is set, block until the current number of wakeup events being
1015 * processed is zero.
1016 *
1017 * Return 'false' if the current number of wakeup events being processed is
1018 * nonzero. Otherwise return 'true'.
1019 */
pm_get_wakeup_count(unsigned int * count,bool block)1020 bool pm_get_wakeup_count(unsigned int *count, bool block)
1021 {
1022 unsigned int cnt, inpr;
1023
1024 if (block) {
1025 DEFINE_WAIT(wait);
1026
1027 for (;;) {
1028 prepare_to_wait(&wakeup_count_wait_queue, &wait,
1029 TASK_INTERRUPTIBLE);
1030 split_counters(&cnt, &inpr);
1031 if (inpr == 0 || signal_pending(current))
1032 break;
1033 pm_print_active_wakeup_sources();
1034 schedule();
1035 }
1036 finish_wait(&wakeup_count_wait_queue, &wait);
1037 }
1038
1039 split_counters(&cnt, &inpr);
1040 *count = cnt;
1041 return !inpr;
1042 }
1043
1044 /**
1045 * pm_save_wakeup_count - Save the current number of registered wakeup events.
1046 * @count: Value to compare with the current number of registered wakeup events.
1047 *
1048 * If @count is equal to the current number of registered wakeup events and the
1049 * current number of wakeup events being processed is zero, store @count as the
1050 * old number of registered wakeup events for pm_check_wakeup_events(), enable
1051 * wakeup events detection and return 'true'. Otherwise disable wakeup events
1052 * detection and return 'false'.
1053 */
pm_save_wakeup_count(unsigned int count)1054 bool pm_save_wakeup_count(unsigned int count)
1055 {
1056 unsigned int cnt, inpr;
1057 unsigned long flags;
1058
1059 events_check_enabled = false;
1060 raw_spin_lock_irqsave(&events_lock, flags);
1061 split_counters(&cnt, &inpr);
1062 if (cnt == count && inpr == 0) {
1063 saved_count = count;
1064 events_check_enabled = true;
1065 }
1066 raw_spin_unlock_irqrestore(&events_lock, flags);
1067 return events_check_enabled;
1068 }
1069
1070 #ifdef CONFIG_PM_AUTOSLEEP
1071 /**
1072 * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources.
1073 * @set: Whether to set or to clear the autosleep_enabled flags.
1074 */
pm_wakep_autosleep_enabled(bool set)1075 void pm_wakep_autosleep_enabled(bool set)
1076 {
1077 struct wakeup_source *ws;
1078 ktime_t now = ktime_get();
1079 int srcuidx;
1080
1081 srcuidx = srcu_read_lock(&wakeup_srcu);
1082 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
1083 spin_lock_irq(&ws->lock);
1084 if (ws->autosleep_enabled != set) {
1085 ws->autosleep_enabled = set;
1086 if (ws->active) {
1087 if (set)
1088 ws->start_prevent_time = now;
1089 else
1090 update_prevent_sleep_time(ws, now);
1091 }
1092 }
1093 spin_unlock_irq(&ws->lock);
1094 }
1095 srcu_read_unlock(&wakeup_srcu, srcuidx);
1096 }
1097 #endif /* CONFIG_PM_AUTOSLEEP */
1098
1099 /**
1100 * print_wakeup_source_stats - Print wakeup source statistics information.
1101 * @m: seq_file to print the statistics into.
1102 * @ws: Wakeup source object to print the statistics for.
1103 */
print_wakeup_source_stats(struct seq_file * m,struct wakeup_source * ws)1104 static int print_wakeup_source_stats(struct seq_file *m,
1105 struct wakeup_source *ws)
1106 {
1107 unsigned long flags;
1108 ktime_t total_time;
1109 ktime_t max_time;
1110 unsigned long active_count;
1111 ktime_t active_time;
1112 ktime_t prevent_sleep_time;
1113
1114 spin_lock_irqsave(&ws->lock, flags);
1115
1116 total_time = ws->total_time;
1117 max_time = ws->max_time;
1118 prevent_sleep_time = ws->prevent_sleep_time;
1119 active_count = ws->active_count;
1120 if (ws->active) {
1121 ktime_t now = ktime_get();
1122
1123 active_time = ktime_sub(now, ws->last_time);
1124 total_time = ktime_add(total_time, active_time);
1125 if (active_time > max_time)
1126 max_time = active_time;
1127
1128 if (ws->autosleep_enabled)
1129 prevent_sleep_time = ktime_add(prevent_sleep_time,
1130 ktime_sub(now, ws->start_prevent_time));
1131 } else {
1132 active_time = 0;
1133 }
1134
1135 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",
1136 ws->name, active_count, ws->event_count,
1137 ws->wakeup_count, ws->expire_count,
1138 ktime_to_ms(active_time), ktime_to_ms(total_time),
1139 ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
1140 ktime_to_ms(prevent_sleep_time));
1141
1142 spin_unlock_irqrestore(&ws->lock, flags);
1143
1144 return 0;
1145 }
1146
wakeup_sources_stats_seq_start(struct seq_file * m,loff_t * pos)1147 static void *wakeup_sources_stats_seq_start(struct seq_file *m,
1148 loff_t *pos)
1149 {
1150 struct wakeup_source *ws;
1151 loff_t n = *pos;
1152 int *srcuidx = m->private;
1153
1154 if (n == 0) {
1155 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
1156 "expire_count\tactive_since\ttotal_time\tmax_time\t"
1157 "last_change\tprevent_suspend_time\n");
1158 }
1159
1160 *srcuidx = srcu_read_lock(&wakeup_srcu);
1161 list_for_each_entry_rcu_locked(ws, &wakeup_sources, entry) {
1162 if (n-- <= 0)
1163 return ws;
1164 }
1165
1166 return NULL;
1167 }
1168
wakeup_sources_stats_seq_next(struct seq_file * m,void * v,loff_t * pos)1169 static void *wakeup_sources_stats_seq_next(struct seq_file *m,
1170 void *v, loff_t *pos)
1171 {
1172 struct wakeup_source *ws = v;
1173 struct wakeup_source *next_ws = NULL;
1174
1175 ++(*pos);
1176
1177 list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) {
1178 next_ws = ws;
1179 break;
1180 }
1181
1182 if (!next_ws)
1183 print_wakeup_source_stats(m, &deleted_ws);
1184
1185 return next_ws;
1186 }
1187
wakeup_sources_stats_seq_stop(struct seq_file * m,void * v)1188 static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v)
1189 {
1190 int *srcuidx = m->private;
1191
1192 srcu_read_unlock(&wakeup_srcu, *srcuidx);
1193 }
1194
1195 /**
1196 * wakeup_sources_stats_seq_show - Print wakeup sources statistics information.
1197 * @m: seq_file to print the statistics into.
1198 * @v: wakeup_source of each iteration
1199 */
wakeup_sources_stats_seq_show(struct seq_file * m,void * v)1200 static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v)
1201 {
1202 struct wakeup_source *ws = v;
1203
1204 print_wakeup_source_stats(m, ws);
1205
1206 return 0;
1207 }
1208
1209 static const struct seq_operations wakeup_sources_stats_seq_ops = {
1210 .start = wakeup_sources_stats_seq_start,
1211 .next = wakeup_sources_stats_seq_next,
1212 .stop = wakeup_sources_stats_seq_stop,
1213 .show = wakeup_sources_stats_seq_show,
1214 };
1215
wakeup_sources_stats_open(struct inode * inode,struct file * file)1216 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
1217 {
1218 return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int));
1219 }
1220
1221 static const struct file_operations wakeup_sources_stats_fops = {
1222 .owner = THIS_MODULE,
1223 .open = wakeup_sources_stats_open,
1224 .read = seq_read,
1225 .llseek = seq_lseek,
1226 .release = seq_release_private,
1227 };
1228
wakeup_sources_debugfs_init(void)1229 static int __init wakeup_sources_debugfs_init(void)
1230 {
1231 debugfs_create_file("wakeup_sources", 0444, NULL, NULL,
1232 &wakeup_sources_stats_fops);
1233 return 0;
1234 }
1235
1236 postcore_initcall(wakeup_sources_debugfs_init);
1237