xref: /openbmc/linux/drivers/base/power/wakeup.c (revision 1ab142d4)
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.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 
18 #include "power.h"
19 
20 #define TIMEOUT		100
21 
22 /*
23  * If set, the suspend/hibernate code will abort transitions to a sleep state
24  * if wakeup events are registered during or immediately before the transition.
25  */
26 bool events_check_enabled;
27 
28 /*
29  * Combined counters of registered wakeup events and wakeup events in progress.
30  * They need to be modified together atomically, so it's better to use one
31  * atomic variable to hold them both.
32  */
33 static atomic_t combined_event_count = ATOMIC_INIT(0);
34 
35 #define IN_PROGRESS_BITS	(sizeof(int) * 4)
36 #define MAX_IN_PROGRESS		((1 << IN_PROGRESS_BITS) - 1)
37 
38 static void split_counters(unsigned int *cnt, unsigned int *inpr)
39 {
40 	unsigned int comb = atomic_read(&combined_event_count);
41 
42 	*cnt = (comb >> IN_PROGRESS_BITS);
43 	*inpr = comb & MAX_IN_PROGRESS;
44 }
45 
46 /* A preserved old value of the events counter. */
47 static unsigned int saved_count;
48 
49 static DEFINE_SPINLOCK(events_lock);
50 
51 static void pm_wakeup_timer_fn(unsigned long data);
52 
53 static LIST_HEAD(wakeup_sources);
54 
55 /**
56  * wakeup_source_prepare - Prepare a new wakeup source for initialization.
57  * @ws: Wakeup source to prepare.
58  * @name: Pointer to the name of the new wakeup source.
59  *
60  * Callers must ensure that the @name string won't be freed when @ws is still in
61  * use.
62  */
63 void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
64 {
65 	if (ws) {
66 		memset(ws, 0, sizeof(*ws));
67 		ws->name = name;
68 	}
69 }
70 EXPORT_SYMBOL_GPL(wakeup_source_prepare);
71 
72 /**
73  * wakeup_source_create - Create a struct wakeup_source object.
74  * @name: Name of the new wakeup source.
75  */
76 struct wakeup_source *wakeup_source_create(const char *name)
77 {
78 	struct wakeup_source *ws;
79 
80 	ws = kmalloc(sizeof(*ws), GFP_KERNEL);
81 	if (!ws)
82 		return NULL;
83 
84 	wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL);
85 	return ws;
86 }
87 EXPORT_SYMBOL_GPL(wakeup_source_create);
88 
89 /**
90  * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
91  * @ws: Wakeup source to prepare for destruction.
92  *
93  * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
94  * be run in parallel with this function for the same wakeup source object.
95  */
96 void wakeup_source_drop(struct wakeup_source *ws)
97 {
98 	if (!ws)
99 		return;
100 
101 	del_timer_sync(&ws->timer);
102 	__pm_relax(ws);
103 }
104 EXPORT_SYMBOL_GPL(wakeup_source_drop);
105 
106 /**
107  * wakeup_source_destroy - Destroy a struct wakeup_source object.
108  * @ws: Wakeup source to destroy.
109  *
110  * Use only for wakeup source objects created with wakeup_source_create().
111  */
112 void wakeup_source_destroy(struct wakeup_source *ws)
113 {
114 	if (!ws)
115 		return;
116 
117 	wakeup_source_drop(ws);
118 	kfree(ws->name);
119 	kfree(ws);
120 }
121 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
122 
123 /**
124  * wakeup_source_add - Add given object to the list of wakeup sources.
125  * @ws: Wakeup source object to add to the list.
126  */
127 void wakeup_source_add(struct wakeup_source *ws)
128 {
129 	if (WARN_ON(!ws))
130 		return;
131 
132 	spin_lock_init(&ws->lock);
133 	setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
134 	ws->active = false;
135 
136 	spin_lock_irq(&events_lock);
137 	list_add_rcu(&ws->entry, &wakeup_sources);
138 	spin_unlock_irq(&events_lock);
139 }
140 EXPORT_SYMBOL_GPL(wakeup_source_add);
141 
142 /**
143  * wakeup_source_remove - Remove given object from the wakeup sources list.
144  * @ws: Wakeup source object to remove from the list.
145  */
146 void wakeup_source_remove(struct wakeup_source *ws)
147 {
148 	if (WARN_ON(!ws))
149 		return;
150 
151 	spin_lock_irq(&events_lock);
152 	list_del_rcu(&ws->entry);
153 	spin_unlock_irq(&events_lock);
154 	synchronize_rcu();
155 }
156 EXPORT_SYMBOL_GPL(wakeup_source_remove);
157 
158 /**
159  * wakeup_source_register - Create wakeup source and add it to the list.
160  * @name: Name of the wakeup source to register.
161  */
162 struct wakeup_source *wakeup_source_register(const char *name)
163 {
164 	struct wakeup_source *ws;
165 
166 	ws = wakeup_source_create(name);
167 	if (ws)
168 		wakeup_source_add(ws);
169 
170 	return ws;
171 }
172 EXPORT_SYMBOL_GPL(wakeup_source_register);
173 
174 /**
175  * wakeup_source_unregister - Remove wakeup source from the list and remove it.
176  * @ws: Wakeup source object to unregister.
177  */
178 void wakeup_source_unregister(struct wakeup_source *ws)
179 {
180 	if (ws) {
181 		wakeup_source_remove(ws);
182 		wakeup_source_destroy(ws);
183 	}
184 }
185 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
186 
187 /**
188  * device_wakeup_attach - Attach a wakeup source object to a device object.
189  * @dev: Device to handle.
190  * @ws: Wakeup source object to attach to @dev.
191  *
192  * This causes @dev to be treated as a wakeup device.
193  */
194 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
195 {
196 	spin_lock_irq(&dev->power.lock);
197 	if (dev->power.wakeup) {
198 		spin_unlock_irq(&dev->power.lock);
199 		return -EEXIST;
200 	}
201 	dev->power.wakeup = ws;
202 	spin_unlock_irq(&dev->power.lock);
203 	return 0;
204 }
205 
206 /**
207  * device_wakeup_enable - Enable given device to be a wakeup source.
208  * @dev: Device to handle.
209  *
210  * Create a wakeup source object, register it and attach it to @dev.
211  */
212 int device_wakeup_enable(struct device *dev)
213 {
214 	struct wakeup_source *ws;
215 	int ret;
216 
217 	if (!dev || !dev->power.can_wakeup)
218 		return -EINVAL;
219 
220 	ws = wakeup_source_register(dev_name(dev));
221 	if (!ws)
222 		return -ENOMEM;
223 
224 	ret = device_wakeup_attach(dev, ws);
225 	if (ret)
226 		wakeup_source_unregister(ws);
227 
228 	return ret;
229 }
230 EXPORT_SYMBOL_GPL(device_wakeup_enable);
231 
232 /**
233  * device_wakeup_detach - Detach a device's wakeup source object from it.
234  * @dev: Device to detach the wakeup source object from.
235  *
236  * After it returns, @dev will not be treated as a wakeup device any more.
237  */
238 static struct wakeup_source *device_wakeup_detach(struct device *dev)
239 {
240 	struct wakeup_source *ws;
241 
242 	spin_lock_irq(&dev->power.lock);
243 	ws = dev->power.wakeup;
244 	dev->power.wakeup = NULL;
245 	spin_unlock_irq(&dev->power.lock);
246 	return ws;
247 }
248 
249 /**
250  * device_wakeup_disable - Do not regard a device as a wakeup source any more.
251  * @dev: Device to handle.
252  *
253  * Detach the @dev's wakeup source object from it, unregister this wakeup source
254  * object and destroy it.
255  */
256 int device_wakeup_disable(struct device *dev)
257 {
258 	struct wakeup_source *ws;
259 
260 	if (!dev || !dev->power.can_wakeup)
261 		return -EINVAL;
262 
263 	ws = device_wakeup_detach(dev);
264 	if (ws)
265 		wakeup_source_unregister(ws);
266 
267 	return 0;
268 }
269 EXPORT_SYMBOL_GPL(device_wakeup_disable);
270 
271 /**
272  * device_set_wakeup_capable - Set/reset device wakeup capability flag.
273  * @dev: Device to handle.
274  * @capable: Whether or not @dev is capable of waking up the system from sleep.
275  *
276  * If @capable is set, set the @dev's power.can_wakeup flag and add its
277  * wakeup-related attributes to sysfs.  Otherwise, unset the @dev's
278  * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
279  *
280  * This function may sleep and it can't be called from any context where
281  * sleeping is not allowed.
282  */
283 void device_set_wakeup_capable(struct device *dev, bool capable)
284 {
285 	if (!!dev->power.can_wakeup == !!capable)
286 		return;
287 
288 	if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
289 		if (capable) {
290 			if (wakeup_sysfs_add(dev))
291 				return;
292 		} else {
293 			wakeup_sysfs_remove(dev);
294 		}
295 	}
296 	dev->power.can_wakeup = capable;
297 }
298 EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
299 
300 /**
301  * device_init_wakeup - Device wakeup initialization.
302  * @dev: Device to handle.
303  * @enable: Whether or not to enable @dev as a wakeup device.
304  *
305  * By default, most devices should leave wakeup disabled.  The exceptions are
306  * devices that everyone expects to be wakeup sources: keyboards, power buttons,
307  * possibly network interfaces, etc.  Also, devices that don't generate their
308  * own wakeup requests but merely forward requests from one bus to another
309  * (like PCI bridges) should have wakeup enabled by default.
310  */
311 int device_init_wakeup(struct device *dev, bool enable)
312 {
313 	int ret = 0;
314 
315 	if (enable) {
316 		device_set_wakeup_capable(dev, true);
317 		ret = device_wakeup_enable(dev);
318 	} else {
319 		device_set_wakeup_capable(dev, false);
320 	}
321 
322 	return ret;
323 }
324 EXPORT_SYMBOL_GPL(device_init_wakeup);
325 
326 /**
327  * device_set_wakeup_enable - Enable or disable a device to wake up the system.
328  * @dev: Device to handle.
329  */
330 int device_set_wakeup_enable(struct device *dev, bool enable)
331 {
332 	if (!dev || !dev->power.can_wakeup)
333 		return -EINVAL;
334 
335 	return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
336 }
337 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
338 
339 /*
340  * The functions below use the observation that each wakeup event starts a
341  * period in which the system should not be suspended.  The moment this period
342  * will end depends on how the wakeup event is going to be processed after being
343  * detected and all of the possible cases can be divided into two distinct
344  * groups.
345  *
346  * First, a wakeup event may be detected by the same functional unit that will
347  * carry out the entire processing of it and possibly will pass it to user space
348  * for further processing.  In that case the functional unit that has detected
349  * the event may later "close" the "no suspend" period associated with it
350  * directly as soon as it has been dealt with.  The pair of pm_stay_awake() and
351  * pm_relax(), balanced with each other, is supposed to be used in such
352  * situations.
353  *
354  * Second, a wakeup event may be detected by one functional unit and processed
355  * by another one.  In that case the unit that has detected it cannot really
356  * "close" the "no suspend" period associated with it, unless it knows in
357  * advance what's going to happen to the event during processing.  This
358  * knowledge, however, may not be available to it, so it can simply specify time
359  * to wait before the system can be suspended and pass it as the second
360  * argument of pm_wakeup_event().
361  *
362  * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
363  * "no suspend" period will be ended either by the pm_relax(), or by the timer
364  * function executed when the timer expires, whichever comes first.
365  */
366 
367 /**
368  * wakup_source_activate - Mark given wakeup source as active.
369  * @ws: Wakeup source to handle.
370  *
371  * Update the @ws' statistics and, if @ws has just been activated, notify the PM
372  * core of the event by incrementing the counter of of wakeup events being
373  * processed.
374  */
375 static void wakeup_source_activate(struct wakeup_source *ws)
376 {
377 	ws->active = true;
378 	ws->active_count++;
379 	ws->last_time = ktime_get();
380 
381 	/* Increment the counter of events in progress. */
382 	atomic_inc(&combined_event_count);
383 }
384 
385 /**
386  * __pm_stay_awake - Notify the PM core of a wakeup event.
387  * @ws: Wakeup source object associated with the source of the event.
388  *
389  * It is safe to call this function from interrupt context.
390  */
391 void __pm_stay_awake(struct wakeup_source *ws)
392 {
393 	unsigned long flags;
394 
395 	if (!ws)
396 		return;
397 
398 	spin_lock_irqsave(&ws->lock, flags);
399 
400 	ws->event_count++;
401 	if (!ws->active)
402 		wakeup_source_activate(ws);
403 
404 	del_timer(&ws->timer);
405 	ws->timer_expires = 0;
406 
407 	spin_unlock_irqrestore(&ws->lock, flags);
408 }
409 EXPORT_SYMBOL_GPL(__pm_stay_awake);
410 
411 /**
412  * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
413  * @dev: Device the wakeup event is related to.
414  *
415  * Notify the PM core of a wakeup event (signaled by @dev) by calling
416  * __pm_stay_awake for the @dev's wakeup source object.
417  *
418  * Call this function after detecting of a wakeup event if pm_relax() is going
419  * to be called directly after processing the event (and possibly passing it to
420  * user space for further processing).
421  */
422 void pm_stay_awake(struct device *dev)
423 {
424 	unsigned long flags;
425 
426 	if (!dev)
427 		return;
428 
429 	spin_lock_irqsave(&dev->power.lock, flags);
430 	__pm_stay_awake(dev->power.wakeup);
431 	spin_unlock_irqrestore(&dev->power.lock, flags);
432 }
433 EXPORT_SYMBOL_GPL(pm_stay_awake);
434 
435 /**
436  * wakup_source_deactivate - Mark given wakeup source as inactive.
437  * @ws: Wakeup source to handle.
438  *
439  * Update the @ws' statistics and notify the PM core that the wakeup source has
440  * become inactive by decrementing the counter of wakeup events being processed
441  * and incrementing the counter of registered wakeup events.
442  */
443 static void wakeup_source_deactivate(struct wakeup_source *ws)
444 {
445 	ktime_t duration;
446 	ktime_t now;
447 
448 	ws->relax_count++;
449 	/*
450 	 * __pm_relax() may be called directly or from a timer function.
451 	 * If it is called directly right after the timer function has been
452 	 * started, but before the timer function calls __pm_relax(), it is
453 	 * possible that __pm_stay_awake() will be called in the meantime and
454 	 * will set ws->active.  Then, ws->active may be cleared immediately
455 	 * by the __pm_relax() called from the timer function, but in such a
456 	 * case ws->relax_count will be different from ws->active_count.
457 	 */
458 	if (ws->relax_count != ws->active_count) {
459 		ws->relax_count--;
460 		return;
461 	}
462 
463 	ws->active = false;
464 
465 	now = ktime_get();
466 	duration = ktime_sub(now, ws->last_time);
467 	ws->total_time = ktime_add(ws->total_time, duration);
468 	if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
469 		ws->max_time = duration;
470 
471 	del_timer(&ws->timer);
472 	ws->timer_expires = 0;
473 
474 	/*
475 	 * Increment the counter of registered wakeup events and decrement the
476 	 * couter of wakeup events in progress simultaneously.
477 	 */
478 	atomic_add(MAX_IN_PROGRESS, &combined_event_count);
479 }
480 
481 /**
482  * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
483  * @ws: Wakeup source object associated with the source of the event.
484  *
485  * Call this function for wakeup events whose processing started with calling
486  * __pm_stay_awake().
487  *
488  * It is safe to call it from interrupt context.
489  */
490 void __pm_relax(struct wakeup_source *ws)
491 {
492 	unsigned long flags;
493 
494 	if (!ws)
495 		return;
496 
497 	spin_lock_irqsave(&ws->lock, flags);
498 	if (ws->active)
499 		wakeup_source_deactivate(ws);
500 	spin_unlock_irqrestore(&ws->lock, flags);
501 }
502 EXPORT_SYMBOL_GPL(__pm_relax);
503 
504 /**
505  * pm_relax - Notify the PM core that processing of a wakeup event has ended.
506  * @dev: Device that signaled the event.
507  *
508  * Execute __pm_relax() for the @dev's wakeup source object.
509  */
510 void pm_relax(struct device *dev)
511 {
512 	unsigned long flags;
513 
514 	if (!dev)
515 		return;
516 
517 	spin_lock_irqsave(&dev->power.lock, flags);
518 	__pm_relax(dev->power.wakeup);
519 	spin_unlock_irqrestore(&dev->power.lock, flags);
520 }
521 EXPORT_SYMBOL_GPL(pm_relax);
522 
523 /**
524  * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
525  * @data: Address of the wakeup source object associated with the event source.
526  *
527  * Call wakeup_source_deactivate() for the wakeup source whose address is stored
528  * in @data if it is currently active and its timer has not been canceled and
529  * the expiration time of the timer is not in future.
530  */
531 static void pm_wakeup_timer_fn(unsigned long data)
532 {
533 	struct wakeup_source *ws = (struct wakeup_source *)data;
534 	unsigned long flags;
535 
536 	spin_lock_irqsave(&ws->lock, flags);
537 
538 	if (ws->active && ws->timer_expires
539 	    && time_after_eq(jiffies, ws->timer_expires))
540 		wakeup_source_deactivate(ws);
541 
542 	spin_unlock_irqrestore(&ws->lock, flags);
543 }
544 
545 /**
546  * __pm_wakeup_event - Notify the PM core of a wakeup event.
547  * @ws: Wakeup source object associated with the event source.
548  * @msec: Anticipated event processing time (in milliseconds).
549  *
550  * Notify the PM core of a wakeup event whose source is @ws that will take
551  * approximately @msec milliseconds to be processed by the kernel.  If @ws is
552  * not active, activate it.  If @msec is nonzero, set up the @ws' timer to
553  * execute pm_wakeup_timer_fn() in future.
554  *
555  * It is safe to call this function from interrupt context.
556  */
557 void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
558 {
559 	unsigned long flags;
560 	unsigned long expires;
561 
562 	if (!ws)
563 		return;
564 
565 	spin_lock_irqsave(&ws->lock, flags);
566 
567 	ws->event_count++;
568 	if (!ws->active)
569 		wakeup_source_activate(ws);
570 
571 	if (!msec) {
572 		wakeup_source_deactivate(ws);
573 		goto unlock;
574 	}
575 
576 	expires = jiffies + msecs_to_jiffies(msec);
577 	if (!expires)
578 		expires = 1;
579 
580 	if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
581 		mod_timer(&ws->timer, expires);
582 		ws->timer_expires = expires;
583 	}
584 
585  unlock:
586 	spin_unlock_irqrestore(&ws->lock, flags);
587 }
588 EXPORT_SYMBOL_GPL(__pm_wakeup_event);
589 
590 
591 /**
592  * pm_wakeup_event - Notify the PM core of a wakeup event.
593  * @dev: Device the wakeup event is related to.
594  * @msec: Anticipated event processing time (in milliseconds).
595  *
596  * Call __pm_wakeup_event() for the @dev's wakeup source object.
597  */
598 void pm_wakeup_event(struct device *dev, unsigned int msec)
599 {
600 	unsigned long flags;
601 
602 	if (!dev)
603 		return;
604 
605 	spin_lock_irqsave(&dev->power.lock, flags);
606 	__pm_wakeup_event(dev->power.wakeup, msec);
607 	spin_unlock_irqrestore(&dev->power.lock, flags);
608 }
609 EXPORT_SYMBOL_GPL(pm_wakeup_event);
610 
611 /**
612  * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
613  */
614 static void pm_wakeup_update_hit_counts(void)
615 {
616 	unsigned long flags;
617 	struct wakeup_source *ws;
618 
619 	rcu_read_lock();
620 	list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
621 		spin_lock_irqsave(&ws->lock, flags);
622 		if (ws->active)
623 			ws->hit_count++;
624 		spin_unlock_irqrestore(&ws->lock, flags);
625 	}
626 	rcu_read_unlock();
627 }
628 
629 /**
630  * pm_wakeup_pending - Check if power transition in progress should be aborted.
631  *
632  * Compare the current number of registered wakeup events with its preserved
633  * value from the past and return true if new wakeup events have been registered
634  * since the old value was stored.  Also return true if the current number of
635  * wakeup events being processed is different from zero.
636  */
637 bool pm_wakeup_pending(void)
638 {
639 	unsigned long flags;
640 	bool ret = false;
641 
642 	spin_lock_irqsave(&events_lock, flags);
643 	if (events_check_enabled) {
644 		unsigned int cnt, inpr;
645 
646 		split_counters(&cnt, &inpr);
647 		ret = (cnt != saved_count || inpr > 0);
648 		events_check_enabled = !ret;
649 	}
650 	spin_unlock_irqrestore(&events_lock, flags);
651 	if (ret)
652 		pm_wakeup_update_hit_counts();
653 	return ret;
654 }
655 
656 /**
657  * pm_get_wakeup_count - Read the number of registered wakeup events.
658  * @count: Address to store the value at.
659  *
660  * Store the number of registered wakeup events at the address in @count.  Block
661  * if the current number of wakeup events being processed is nonzero.
662  *
663  * Return 'false' if the wait for the number of wakeup events being processed to
664  * drop down to zero has been interrupted by a signal (and the current number
665  * of wakeup events being processed is still nonzero).  Otherwise return 'true'.
666  */
667 bool pm_get_wakeup_count(unsigned int *count)
668 {
669 	unsigned int cnt, inpr;
670 
671 	for (;;) {
672 		split_counters(&cnt, &inpr);
673 		if (inpr == 0 || signal_pending(current))
674 			break;
675 		pm_wakeup_update_hit_counts();
676 		schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
677 	}
678 
679 	split_counters(&cnt, &inpr);
680 	*count = cnt;
681 	return !inpr;
682 }
683 
684 /**
685  * pm_save_wakeup_count - Save the current number of registered wakeup events.
686  * @count: Value to compare with the current number of registered wakeup events.
687  *
688  * If @count is equal to the current number of registered wakeup events and the
689  * current number of wakeup events being processed is zero, store @count as the
690  * old number of registered wakeup events for pm_check_wakeup_events(), enable
691  * wakeup events detection and return 'true'.  Otherwise disable wakeup events
692  * detection and return 'false'.
693  */
694 bool pm_save_wakeup_count(unsigned int count)
695 {
696 	unsigned int cnt, inpr;
697 
698 	events_check_enabled = false;
699 	spin_lock_irq(&events_lock);
700 	split_counters(&cnt, &inpr);
701 	if (cnt == count && inpr == 0) {
702 		saved_count = count;
703 		events_check_enabled = true;
704 	}
705 	spin_unlock_irq(&events_lock);
706 	if (!events_check_enabled)
707 		pm_wakeup_update_hit_counts();
708 	return events_check_enabled;
709 }
710 
711 static struct dentry *wakeup_sources_stats_dentry;
712 
713 /**
714  * print_wakeup_source_stats - Print wakeup source statistics information.
715  * @m: seq_file to print the statistics into.
716  * @ws: Wakeup source object to print the statistics for.
717  */
718 static int print_wakeup_source_stats(struct seq_file *m,
719 				     struct wakeup_source *ws)
720 {
721 	unsigned long flags;
722 	ktime_t total_time;
723 	ktime_t max_time;
724 	unsigned long active_count;
725 	ktime_t active_time;
726 	int ret;
727 
728 	spin_lock_irqsave(&ws->lock, flags);
729 
730 	total_time = ws->total_time;
731 	max_time = ws->max_time;
732 	active_count = ws->active_count;
733 	if (ws->active) {
734 		active_time = ktime_sub(ktime_get(), ws->last_time);
735 		total_time = ktime_add(total_time, active_time);
736 		if (active_time.tv64 > max_time.tv64)
737 			max_time = active_time;
738 	} else {
739 		active_time = ktime_set(0, 0);
740 	}
741 
742 	ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
743 			"%lld\t\t%lld\t\t%lld\t\t%lld\n",
744 			ws->name, active_count, ws->event_count, ws->hit_count,
745 			ktime_to_ms(active_time), ktime_to_ms(total_time),
746 			ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
747 
748 	spin_unlock_irqrestore(&ws->lock, flags);
749 
750 	return ret;
751 }
752 
753 /**
754  * wakeup_sources_stats_show - Print wakeup sources statistics information.
755  * @m: seq_file to print the statistics into.
756  */
757 static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
758 {
759 	struct wakeup_source *ws;
760 
761 	seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
762 		"active_since\ttotal_time\tmax_time\tlast_change\n");
763 
764 	rcu_read_lock();
765 	list_for_each_entry_rcu(ws, &wakeup_sources, entry)
766 		print_wakeup_source_stats(m, ws);
767 	rcu_read_unlock();
768 
769 	return 0;
770 }
771 
772 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
773 {
774 	return single_open(file, wakeup_sources_stats_show, NULL);
775 }
776 
777 static const struct file_operations wakeup_sources_stats_fops = {
778 	.owner = THIS_MODULE,
779 	.open = wakeup_sources_stats_open,
780 	.read = seq_read,
781 	.llseek = seq_lseek,
782 	.release = single_release,
783 };
784 
785 static int __init wakeup_sources_debugfs_init(void)
786 {
787 	wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
788 			S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
789 	return 0;
790 }
791 
792 postcore_initcall(wakeup_sources_debugfs_init);
793