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