blob: b24eb7594ef2af67f4ec52c318af057f0551310c [file] [log] [blame]
Arve Hjønnevågfe6cd632008-09-09 22:14:34 -07001/* kernel/power/wakelock.c
2 *
3 * Copyright (C) 2005-2008 Google, Inc.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/rtc.h>
19#include <linux/suspend.h>
20#include <linux/syscalls.h> /* sys_sync */
21#include <linux/wakelock.h>
22#ifdef CONFIG_WAKELOCK_STAT
23#include <linux/proc_fs.h>
24#endif
25#include "power.h"
26
27enum {
28 DEBUG_EXIT_SUSPEND = 1U << 0,
29 DEBUG_WAKEUP = 1U << 1,
30 DEBUG_SUSPEND = 1U << 2,
31 DEBUG_EXPIRE = 1U << 3,
32 DEBUG_WAKE_LOCK = 1U << 4,
33};
34static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP;
35module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
36
37#define WAKE_LOCK_TYPE_MASK (0x0f)
38#define WAKE_LOCK_INITIALIZED (1U << 8)
39#define WAKE_LOCK_ACTIVE (1U << 9)
40#define WAKE_LOCK_AUTO_EXPIRE (1U << 10)
41#define WAKE_LOCK_PREVENTING_SUSPEND (1U << 11)
42
43static DEFINE_SPINLOCK(list_lock);
44static LIST_HEAD(inactive_locks);
45static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT];
46static int current_event_num;
47struct workqueue_struct *suspend_work_queue;
48struct wake_lock main_wake_lock;
49suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
50static struct wake_lock unknown_wakeup;
51
52#ifdef CONFIG_WAKELOCK_STAT
53static struct wake_lock deleted_wake_locks;
54static ktime_t last_sleep_time_update;
55static int wait_for_wakeup;
56
57int get_expired_time(struct wake_lock *lock, ktime_t *expire_time)
58{
59 struct timespec ts;
60 struct timespec kt;
61 struct timespec tomono;
62 struct timespec delta;
63 unsigned long seq;
64 long timeout;
65
66 if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE))
67 return 0;
68 do {
69 seq = read_seqbegin(&xtime_lock);
70 timeout = lock->expires - jiffies;
71 if (timeout > 0)
72 return 0;
73 kt = current_kernel_time();
74 tomono = wall_to_monotonic;
75 } while (read_seqretry(&xtime_lock, seq));
76 jiffies_to_timespec(-timeout, &delta);
77 set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec,
78 kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec);
79 *expire_time = timespec_to_ktime(ts);
80 return 1;
81}
82
83
84static int print_lock_stat(char *buf, struct wake_lock *lock)
85{
86 int lock_count = lock->stat.count;
87 int expire_count = lock->stat.expire_count;
88 ktime_t active_time = ktime_set(0, 0);
89 ktime_t total_time = lock->stat.total_time;
90 ktime_t max_time = lock->stat.max_time;
91 ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time;
92 if (lock->flags & WAKE_LOCK_ACTIVE) {
93 ktime_t now, add_time;
94 int expired = get_expired_time(lock, &now);
95 if (!expired)
96 now = ktime_get();
97 add_time = ktime_sub(now, lock->stat.last_time);
98 lock_count++;
99 if (!expired)
100 active_time = add_time;
101 else
102 expire_count++;
103 total_time = ktime_add(total_time, add_time);
104 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND)
105 prevent_suspend_time = ktime_add(prevent_suspend_time,
106 ktime_sub(now, last_sleep_time_update));
107 if (add_time.tv64 > max_time.tv64)
108 max_time = add_time;
109 }
110
111 return sprintf(buf, "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t"
112 "%lld\n", lock->name, lock_count, expire_count,
113 lock->stat.wakeup_count, ktime_to_ns(active_time),
114 ktime_to_ns(total_time),
115 ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time),
116 ktime_to_ns(lock->stat.last_time));
117}
118
119
120static int wakelocks_read_proc(char *page, char **start, off_t off,
121 int count, int *eof, void *data)
122{
123 unsigned long irqflags;
124 struct wake_lock *lock;
125 int len = 0;
126 char *p = page;
127 int type;
128
129 spin_lock_irqsave(&list_lock, irqflags);
130
131 p += sprintf(p, "name\tcount\texpire_count\twake_count\tactive_since"
132 "\ttotal_time\tsleep_time\tmax_time\tlast_change\n");
133 list_for_each_entry(lock, &inactive_locks, link) {
134 p += print_lock_stat(p, lock);
135 }
136 for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) {
137 list_for_each_entry(lock, &active_wake_locks[type], link)
138 p += print_lock_stat(p, lock);
139 }
140 spin_unlock_irqrestore(&list_lock, irqflags);
141
142 *start = page + off;
143
144 len = p - page;
145 if (len > off)
146 len -= off;
147 else
148 len = 0;
149
150 return len < count ? len : count;
151}
152
153static void wake_unlock_stat_locked(struct wake_lock *lock, int expired)
154{
155 ktime_t duration;
156 ktime_t now;
157 if (!(lock->flags & WAKE_LOCK_ACTIVE))
158 return;
159 if (get_expired_time(lock, &now))
160 expired = 1;
161 else
162 now = ktime_get();
163 lock->stat.count++;
164 if (expired)
165 lock->stat.expire_count++;
166 duration = ktime_sub(now, lock->stat.last_time);
167 lock->stat.total_time = ktime_add(lock->stat.total_time, duration);
168 if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time))
169 lock->stat.max_time = duration;
170 lock->stat.last_time = ktime_get();
171 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
172 duration = ktime_sub(now, last_sleep_time_update);
173 lock->stat.prevent_suspend_time = ktime_add(
174 lock->stat.prevent_suspend_time, duration);
175 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
176 }
177}
178
179static void update_sleep_wait_stats_locked(int done)
180{
181 struct wake_lock *lock;
182 ktime_t now, etime, elapsed, add;
183 int expired;
184
185 now = ktime_get();
186 elapsed = ktime_sub(now, last_sleep_time_update);
187 list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) {
188 expired = get_expired_time(lock, &etime);
189 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
190 if (expired)
191 add = ktime_sub(etime, last_sleep_time_update);
192 else
193 add = elapsed;
194 lock->stat.prevent_suspend_time = ktime_add(
195 lock->stat.prevent_suspend_time, add);
196 }
197 if (done || expired)
198 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
199 else
200 lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND;
201 }
202 last_sleep_time_update = now;
203}
204#endif
205
206
207static void expire_wake_lock(struct wake_lock *lock)
208{
209#ifdef CONFIG_WAKELOCK_STAT
210 wake_unlock_stat_locked(lock, 1);
211#endif
212 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
213 list_del(&lock->link);
214 list_add(&lock->link, &inactive_locks);
215 if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE))
216 pr_info("expired wake lock %s\n", lock->name);
217}
218
219static void print_active_locks(int type)
220{
221 unsigned long irqflags;
222 struct wake_lock *lock;
223
224 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
225 spin_lock_irqsave(&list_lock, irqflags);
226 list_for_each_entry(lock, &active_wake_locks[type], link) {
227 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
228 long timeout = lock->expires - jiffies;
229 if (timeout <= 0)
230 pr_info("wake lock %s, expired\n", lock->name);
231 else
232 pr_info("active wake lock %s, time left %ld\n",
233 lock->name, timeout);
234 } else
235 pr_info("active wake lock %s\n", lock->name);
236 }
237 spin_unlock_irqrestore(&list_lock, irqflags);
238}
239
240static long has_wake_lock_locked(int type)
241{
242 struct wake_lock *lock, *n;
243 long max_timeout = 0;
244
245 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
246 list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
247 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
248 long timeout = lock->expires - jiffies;
249 if (timeout <= 0)
250 expire_wake_lock(lock);
251 else if (timeout > max_timeout)
252 max_timeout = timeout;
253 } else
254 return -1;
255 }
256 return max_timeout;
257}
258
259long has_wake_lock(int type)
260{
261 long ret;
262 unsigned long irqflags;
263 spin_lock_irqsave(&list_lock, irqflags);
264 ret = has_wake_lock_locked(type);
265 spin_unlock_irqrestore(&list_lock, irqflags);
266 return ret;
267}
268
269static void suspend(struct work_struct *work)
270{
271 int ret;
272 int entry_event_num;
273
274 if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
275 if (debug_mask & DEBUG_SUSPEND)
276 pr_info("suspend: abort suspend\n");
277 return;
278 }
279
280 entry_event_num = current_event_num;
281 sys_sync();
282 if (debug_mask & DEBUG_SUSPEND)
283 pr_info("suspend: enter suspend\n");
284 ret = pm_suspend(requested_suspend_state);
285 if (debug_mask & DEBUG_EXIT_SUSPEND) {
286 struct timespec ts;
287 struct rtc_time tm;
288 getnstimeofday(&ts);
289 rtc_time_to_tm(ts.tv_sec, &tm);
290 pr_info("suspend: exit suspend, ret = %d "
291 "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
292 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
293 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
294 }
295 if (current_event_num == entry_event_num) {
296 if (debug_mask & DEBUG_SUSPEND)
297 pr_info("suspend: pm_suspend returned with no event\n");
298 wake_lock_timeout(&unknown_wakeup, HZ / 2);
299 }
300}
301static DECLARE_WORK(suspend_work, suspend);
302
303static void expire_wake_locks(unsigned long data)
304{
305 long has_lock;
306 unsigned long irqflags;
307 if (debug_mask & DEBUG_EXPIRE)
308 pr_info("expire_wake_locks: start\n");
309 if (debug_mask & DEBUG_SUSPEND)
310 print_active_locks(WAKE_LOCK_SUSPEND);
311 spin_lock_irqsave(&list_lock, irqflags);
312 has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
313 if (debug_mask & DEBUG_EXPIRE)
314 pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
315 if (has_lock == 0)
316 queue_work(suspend_work_queue, &suspend_work);
317 spin_unlock_irqrestore(&list_lock, irqflags);
318}
319static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
320
321static int power_suspend_late(struct device *dev)
322{
323 int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
324#ifdef CONFIG_WAKELOCK_STAT
325 wait_for_wakeup = 1;
326#endif
327 if (debug_mask & DEBUG_SUSPEND)
328 pr_info("power_suspend_late return %d\n", ret);
329 return ret;
330}
331
332static struct dev_pm_ops power_driver_pm_ops = {
333 .suspend_noirq = power_suspend_late,
334};
335
336static struct platform_driver power_driver = {
337 .driver.name = "power",
338 .driver.pm = &power_driver_pm_ops,
339};
340static struct platform_device power_device = {
341 .name = "power",
342};
343
344void wake_lock_init(struct wake_lock *lock, int type, const char *name)
345{
346 unsigned long irqflags = 0;
347
348 if (name)
349 lock->name = name;
350 BUG_ON(!lock->name);
351
352 if (debug_mask & DEBUG_WAKE_LOCK)
353 pr_info("wake_lock_init name=%s\n", lock->name);
354#ifdef CONFIG_WAKELOCK_STAT
355 lock->stat.count = 0;
356 lock->stat.expire_count = 0;
357 lock->stat.wakeup_count = 0;
358 lock->stat.total_time = ktime_set(0, 0);
359 lock->stat.prevent_suspend_time = ktime_set(0, 0);
360 lock->stat.max_time = ktime_set(0, 0);
361 lock->stat.last_time = ktime_set(0, 0);
362#endif
363 lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
364
365 INIT_LIST_HEAD(&lock->link);
366 spin_lock_irqsave(&list_lock, irqflags);
367 list_add(&lock->link, &inactive_locks);
368 spin_unlock_irqrestore(&list_lock, irqflags);
369}
370EXPORT_SYMBOL(wake_lock_init);
371
372void wake_lock_destroy(struct wake_lock *lock)
373{
374 unsigned long irqflags;
375 if (debug_mask & DEBUG_WAKE_LOCK)
376 pr_info("wake_lock_destroy name=%s\n", lock->name);
377 spin_lock_irqsave(&list_lock, irqflags);
378 lock->flags &= ~WAKE_LOCK_INITIALIZED;
379#ifdef CONFIG_WAKELOCK_STAT
380 if (lock->stat.count) {
381 deleted_wake_locks.stat.count += lock->stat.count;
382 deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
383 deleted_wake_locks.stat.total_time =
384 ktime_add(deleted_wake_locks.stat.total_time,
385 lock->stat.total_time);
386 deleted_wake_locks.stat.prevent_suspend_time =
387 ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
388 lock->stat.prevent_suspend_time);
389 deleted_wake_locks.stat.max_time =
390 ktime_add(deleted_wake_locks.stat.max_time,
391 lock->stat.max_time);
392 }
393#endif
394 list_del(&lock->link);
395 spin_unlock_irqrestore(&list_lock, irqflags);
396}
397EXPORT_SYMBOL(wake_lock_destroy);
398
399static void wake_lock_internal(
400 struct wake_lock *lock, long timeout, int has_timeout)
401{
402 int type;
403 unsigned long irqflags;
404 long expire_in;
405
406 spin_lock_irqsave(&list_lock, irqflags);
407 type = lock->flags & WAKE_LOCK_TYPE_MASK;
408 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
409 BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
410#ifdef CONFIG_WAKELOCK_STAT
411 if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
412 if (debug_mask & DEBUG_WAKEUP)
413 pr_info("wakeup wake lock: %s\n", lock->name);
414 wait_for_wakeup = 0;
415 lock->stat.wakeup_count++;
416 }
417 if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
418 (long)(lock->expires - jiffies) <= 0) {
419 wake_unlock_stat_locked(lock, 0);
420 lock->stat.last_time = ktime_get();
421 }
422#endif
423 if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
424 lock->flags |= WAKE_LOCK_ACTIVE;
425#ifdef CONFIG_WAKELOCK_STAT
426 lock->stat.last_time = ktime_get();
427#endif
428 }
429 list_del(&lock->link);
430 if (has_timeout) {
431 if (debug_mask & DEBUG_WAKE_LOCK)
432 pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
433 lock->name, type, timeout / HZ,
434 (timeout % HZ) * MSEC_PER_SEC / HZ);
435 lock->expires = jiffies + timeout;
436 lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
437 list_add_tail(&lock->link, &active_wake_locks[type]);
438 } else {
439 if (debug_mask & DEBUG_WAKE_LOCK)
440 pr_info("wake_lock: %s, type %d\n", lock->name, type);
441 lock->expires = LONG_MAX;
442 lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
443 list_add(&lock->link, &active_wake_locks[type]);
444 }
445 if (type == WAKE_LOCK_SUSPEND) {
446 current_event_num++;
447#ifdef CONFIG_WAKELOCK_STAT
448 if (lock == &main_wake_lock)
449 update_sleep_wait_stats_locked(1);
450 else if (!wake_lock_active(&main_wake_lock))
451 update_sleep_wait_stats_locked(0);
452#endif
453 if (has_timeout)
454 expire_in = has_wake_lock_locked(type);
455 else
456 expire_in = -1;
457 if (expire_in > 0) {
458 if (debug_mask & DEBUG_EXPIRE)
459 pr_info("wake_lock: %s, start expire timer, "
460 "%ld\n", lock->name, expire_in);
461 mod_timer(&expire_timer, jiffies + expire_in);
462 } else {
463 if (del_timer(&expire_timer))
464 if (debug_mask & DEBUG_EXPIRE)
465 pr_info("wake_lock: %s, stop expire timer\n",
466 lock->name);
467 if (expire_in == 0)
468 queue_work(suspend_work_queue, &suspend_work);
469 }
470 }
471 spin_unlock_irqrestore(&list_lock, irqflags);
472}
473
474void wake_lock(struct wake_lock *lock)
475{
476 wake_lock_internal(lock, 0, 0);
477}
478EXPORT_SYMBOL(wake_lock);
479
480void wake_lock_timeout(struct wake_lock *lock, long timeout)
481{
482 wake_lock_internal(lock, timeout, 1);
483}
484EXPORT_SYMBOL(wake_lock_timeout);
485
486void wake_unlock(struct wake_lock *lock)
487{
488 int type;
489 unsigned long irqflags;
490 spin_lock_irqsave(&list_lock, irqflags);
491 type = lock->flags & WAKE_LOCK_TYPE_MASK;
492#ifdef CONFIG_WAKELOCK_STAT
493 wake_unlock_stat_locked(lock, 0);
494#endif
495 if (debug_mask & DEBUG_WAKE_LOCK)
496 pr_info("wake_unlock: %s\n", lock->name);
497 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
498 list_del(&lock->link);
499 list_add(&lock->link, &inactive_locks);
500 if (type == WAKE_LOCK_SUSPEND) {
501 long has_lock = has_wake_lock_locked(type);
502 if (has_lock > 0) {
503 if (debug_mask & DEBUG_EXPIRE)
504 pr_info("wake_unlock: %s, start expire timer, "
505 "%ld\n", lock->name, has_lock);
506 mod_timer(&expire_timer, jiffies + has_lock);
507 } else {
508 if (del_timer(&expire_timer))
509 if (debug_mask & DEBUG_EXPIRE)
510 pr_info("wake_unlock: %s, stop expire "
511 "timer\n", lock->name);
512 if (has_lock == 0)
513 queue_work(suspend_work_queue, &suspend_work);
514 }
515 if (lock == &main_wake_lock) {
516 if (debug_mask & DEBUG_SUSPEND)
517 print_active_locks(WAKE_LOCK_SUSPEND);
518#ifdef CONFIG_WAKELOCK_STAT
519 update_sleep_wait_stats_locked(0);
520#endif
521 }
522 }
523 spin_unlock_irqrestore(&list_lock, irqflags);
524}
525EXPORT_SYMBOL(wake_unlock);
526
527int wake_lock_active(struct wake_lock *lock)
528{
529 return !!(lock->flags & WAKE_LOCK_ACTIVE);
530}
531EXPORT_SYMBOL(wake_lock_active);
532
533static int __init wakelocks_init(void)
534{
535 int ret;
536 int i;
537
538 for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
539 INIT_LIST_HEAD(&active_wake_locks[i]);
540
541#ifdef CONFIG_WAKELOCK_STAT
542 wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
543 "deleted_wake_locks");
544#endif
545 wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
546 wake_lock(&main_wake_lock);
547 wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
548
549 ret = platform_device_register(&power_device);
550 if (ret) {
551 pr_err("wakelocks_init: platform_device_register failed\n");
552 goto err_platform_device_register;
553 }
554 ret = platform_driver_register(&power_driver);
555 if (ret) {
556 pr_err("wakelocks_init: platform_driver_register failed\n");
557 goto err_platform_driver_register;
558 }
559
560 suspend_work_queue = create_singlethread_workqueue("suspend");
561 if (suspend_work_queue == NULL) {
562 ret = -ENOMEM;
563 goto err_suspend_work_queue;
564 }
565
566#ifdef CONFIG_WAKELOCK_STAT
567 create_proc_read_entry("wakelocks", S_IRUGO, NULL,
568 wakelocks_read_proc, NULL);
569#endif
570
571 return 0;
572
573err_suspend_work_queue:
574 platform_driver_unregister(&power_driver);
575err_platform_driver_register:
576 platform_device_unregister(&power_device);
577err_platform_device_register:
578 wake_lock_destroy(&unknown_wakeup);
579 wake_lock_destroy(&main_wake_lock);
580#ifdef CONFIG_WAKELOCK_STAT
581 wake_lock_destroy(&deleted_wake_locks);
582#endif
583 return ret;
584}
585
586static void __exit wakelocks_exit(void)
587{
588#ifdef CONFIG_WAKELOCK_STAT
589 remove_proc_entry("wakelocks", NULL);
590#endif
591 destroy_workqueue(suspend_work_queue);
592 platform_driver_unregister(&power_driver);
593 platform_device_unregister(&power_device);
594 wake_lock_destroy(&unknown_wakeup);
595 wake_lock_destroy(&main_wake_lock);
596#ifdef CONFIG_WAKELOCK_STAT
597 wake_lock_destroy(&deleted_wake_locks);
598#endif
599}
600
601core_initcall(wakelocks_init);
602module_exit(wakelocks_exit);