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