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