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