1 /* kernel/power/wakelock.c
3 * Copyright (C) 2005-2008 Google, Inc.
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.
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.
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>
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,
34 static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP;
35 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
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)
43 static DEFINE_SPINLOCK(list_lock);
44 static LIST_HEAD(inactive_locks);
45 static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT];
46 static int current_event_num;
47 struct workqueue_struct *suspend_work_queue;
48 struct wake_lock main_wake_lock;
49 suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
50 static struct wake_lock unknown_wakeup;
52 #ifdef CONFIG_WAKELOCK_STAT
53 static struct wake_lock deleted_wake_locks;
54 static ktime_t last_sleep_time_update;
55 static int wait_for_wakeup;
57 int get_expired_time(struct wake_lock *lock, ktime_t *expire_time)
61 struct timespec tomono;
62 struct timespec delta;
66 if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE))
69 seq = read_seqbegin(&xtime_lock);
70 timeout = lock->expires - jiffies;
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);
84 static int print_lock_stat(struct seq_file *m, struct wake_lock *lock)
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;
92 ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time;
93 if (lock->flags & WAKE_LOCK_ACTIVE) {
94 ktime_t now, add_time;
95 int expired = get_expired_time(lock, &now);
98 add_time = ktime_sub(now, lock->stat.last_time);
101 active_time = add_time;
104 total_time = ktime_add(total_time, add_time);
105 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND)
106 prevent_suspend_time = ktime_add(prevent_suspend_time,
107 ktime_sub(now, last_sleep_time_update));
108 if (add_time.tv64 > max_time.tv64)
113 "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t%lld\n",
114 lock->name, lock_count, expire_count,
115 lock->stat.wakeup_count, ktime_to_ns(active_time),
116 ktime_to_ns(total_time),
117 ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time),
118 ktime_to_ns(lock->stat.last_time));
121 static int wakelock_stats_show(struct seq_file *m, void *unused)
123 unsigned long irqflags;
124 struct wake_lock *lock;
128 spin_lock_irqsave(&list_lock, irqflags);
130 ret = seq_puts(m, "name\tcount\texpire_count\twake_count\tactive_since"
131 "\ttotal_time\tsleep_time\tmax_time\tlast_change\n");
132 list_for_each_entry(lock, &inactive_locks, link)
133 ret = print_lock_stat(m, lock);
134 for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) {
135 list_for_each_entry(lock, &active_wake_locks[type], link)
136 ret = print_lock_stat(m, lock);
138 spin_unlock_irqrestore(&list_lock, irqflags);
142 static void wake_unlock_stat_locked(struct wake_lock *lock, int expired)
146 if (!(lock->flags & WAKE_LOCK_ACTIVE))
148 if (get_expired_time(lock, &now))
154 lock->stat.expire_count++;
155 duration = ktime_sub(now, lock->stat.last_time);
156 lock->stat.total_time = ktime_add(lock->stat.total_time, duration);
157 if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time))
158 lock->stat.max_time = duration;
159 lock->stat.last_time = ktime_get();
160 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
161 duration = ktime_sub(now, last_sleep_time_update);
162 lock->stat.prevent_suspend_time = ktime_add(
163 lock->stat.prevent_suspend_time, duration);
164 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
168 static void update_sleep_wait_stats_locked(int done)
170 struct wake_lock *lock;
171 ktime_t now, etime, elapsed, add;
175 elapsed = ktime_sub(now, last_sleep_time_update);
176 list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) {
177 expired = get_expired_time(lock, &etime);
178 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
180 add = ktime_sub(etime, last_sleep_time_update);
183 lock->stat.prevent_suspend_time = ktime_add(
184 lock->stat.prevent_suspend_time, add);
187 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
189 lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND;
191 last_sleep_time_update = now;
196 static void expire_wake_lock(struct wake_lock *lock)
198 #ifdef CONFIG_WAKELOCK_STAT
199 wake_unlock_stat_locked(lock, 1);
201 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
202 list_del(&lock->link);
203 list_add(&lock->link, &inactive_locks);
204 if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE))
205 pr_info("expired wake lock %s\n", lock->name);
208 /* Caller must acquire the list_lock spinlock */
209 static void print_active_locks(int type)
211 struct wake_lock *lock;
212 bool print_expired = true;
214 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
215 list_for_each_entry(lock, &active_wake_locks[type], link) {
216 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
217 long timeout = lock->expires - jiffies;
219 pr_info("active wake lock %s, time left %ld\n",
220 lock->name, timeout);
221 else if (print_expired)
222 pr_info("wake lock %s, expired\n", lock->name);
224 pr_info("active wake lock %s\n", lock->name);
225 if (!debug_mask & DEBUG_EXPIRE)
226 print_expired = false;
231 static long has_wake_lock_locked(int type)
233 struct wake_lock *lock, *n;
234 long max_timeout = 0;
236 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
237 list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
238 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
239 long timeout = lock->expires - jiffies;
241 expire_wake_lock(lock);
242 else if (timeout > max_timeout)
243 max_timeout = timeout;
250 long has_wake_lock(int type)
253 unsigned long irqflags;
254 spin_lock_irqsave(&list_lock, irqflags);
255 ret = has_wake_lock_locked(type);
256 if (ret && (debug_mask & DEBUG_SUSPEND) && type == WAKE_LOCK_SUSPEND)
257 print_active_locks(type);
258 spin_unlock_irqrestore(&list_lock, irqflags);
262 static void suspend(struct work_struct *work)
267 if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
268 if (debug_mask & DEBUG_SUSPEND)
269 pr_info("suspend: abort suspend\n");
273 entry_event_num = current_event_num;
275 if (debug_mask & DEBUG_SUSPEND)
276 pr_info("suspend: enter suspend\n");
277 ret = pm_suspend(requested_suspend_state);
278 if (debug_mask & DEBUG_EXIT_SUSPEND) {
282 rtc_time_to_tm(ts.tv_sec, &tm);
283 pr_info("suspend: exit suspend, ret = %d "
284 "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
285 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
286 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
288 if (current_event_num == entry_event_num) {
289 if (debug_mask & DEBUG_SUSPEND)
290 pr_info("suspend: pm_suspend returned with no event\n");
291 wake_lock_timeout(&unknown_wakeup, HZ / 2);
294 static DECLARE_WORK(suspend_work, suspend);
296 static void expire_wake_locks(unsigned long data)
299 unsigned long irqflags;
300 if (debug_mask & DEBUG_EXPIRE)
301 pr_info("expire_wake_locks: start\n");
302 spin_lock_irqsave(&list_lock, irqflags);
303 if (debug_mask & DEBUG_SUSPEND)
304 print_active_locks(WAKE_LOCK_SUSPEND);
305 has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
306 if (debug_mask & DEBUG_EXPIRE)
307 pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
309 queue_work(suspend_work_queue, &suspend_work);
310 spin_unlock_irqrestore(&list_lock, irqflags);
312 static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
314 static int power_suspend_late(struct device *dev)
316 int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
317 #ifdef CONFIG_WAKELOCK_STAT
320 if (debug_mask & DEBUG_SUSPEND)
321 pr_info("power_suspend_late return %d\n", ret);
325 static struct dev_pm_ops power_driver_pm_ops = {
326 .suspend_noirq = power_suspend_late,
329 static struct platform_driver power_driver = {
330 .driver.name = "power",
331 .driver.pm = &power_driver_pm_ops,
333 static struct platform_device power_device = {
337 void wake_lock_init(struct wake_lock *lock, int type, const char *name)
339 unsigned long irqflags = 0;
345 if (debug_mask & DEBUG_WAKE_LOCK)
346 pr_info("wake_lock_init name=%s\n", lock->name);
347 #ifdef CONFIG_WAKELOCK_STAT
348 lock->stat.count = 0;
349 lock->stat.expire_count = 0;
350 lock->stat.wakeup_count = 0;
351 lock->stat.total_time = ktime_set(0, 0);
352 lock->stat.prevent_suspend_time = ktime_set(0, 0);
353 lock->stat.max_time = ktime_set(0, 0);
354 lock->stat.last_time = ktime_set(0, 0);
356 lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
358 INIT_LIST_HEAD(&lock->link);
359 spin_lock_irqsave(&list_lock, irqflags);
360 list_add(&lock->link, &inactive_locks);
361 spin_unlock_irqrestore(&list_lock, irqflags);
363 EXPORT_SYMBOL(wake_lock_init);
365 void wake_lock_destroy(struct wake_lock *lock)
367 unsigned long irqflags;
368 if (debug_mask & DEBUG_WAKE_LOCK)
369 pr_info("wake_lock_destroy name=%s\n", lock->name);
370 spin_lock_irqsave(&list_lock, irqflags);
371 lock->flags &= ~WAKE_LOCK_INITIALIZED;
372 #ifdef CONFIG_WAKELOCK_STAT
373 if (lock->stat.count) {
374 deleted_wake_locks.stat.count += lock->stat.count;
375 deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
376 deleted_wake_locks.stat.total_time =
377 ktime_add(deleted_wake_locks.stat.total_time,
378 lock->stat.total_time);
379 deleted_wake_locks.stat.prevent_suspend_time =
380 ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
381 lock->stat.prevent_suspend_time);
382 deleted_wake_locks.stat.max_time =
383 ktime_add(deleted_wake_locks.stat.max_time,
384 lock->stat.max_time);
387 list_del(&lock->link);
388 spin_unlock_irqrestore(&list_lock, irqflags);
390 EXPORT_SYMBOL(wake_lock_destroy);
392 static void wake_lock_internal(
393 struct wake_lock *lock, long timeout, int has_timeout)
396 unsigned long irqflags;
399 spin_lock_irqsave(&list_lock, irqflags);
400 type = lock->flags & WAKE_LOCK_TYPE_MASK;
401 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
402 BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
403 #ifdef CONFIG_WAKELOCK_STAT
404 if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
405 if (debug_mask & DEBUG_WAKEUP)
406 pr_info("wakeup wake lock: %s\n", lock->name);
408 lock->stat.wakeup_count++;
410 if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
411 (long)(lock->expires - jiffies) <= 0) {
412 wake_unlock_stat_locked(lock, 0);
413 lock->stat.last_time = ktime_get();
416 if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
417 lock->flags |= WAKE_LOCK_ACTIVE;
418 #ifdef CONFIG_WAKELOCK_STAT
419 lock->stat.last_time = ktime_get();
422 list_del(&lock->link);
424 if (debug_mask & DEBUG_WAKE_LOCK)
425 pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
426 lock->name, type, timeout / HZ,
427 (timeout % HZ) * MSEC_PER_SEC / HZ);
428 lock->expires = jiffies + timeout;
429 lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
430 list_add_tail(&lock->link, &active_wake_locks[type]);
432 if (debug_mask & DEBUG_WAKE_LOCK)
433 pr_info("wake_lock: %s, type %d\n", lock->name, type);
434 lock->expires = LONG_MAX;
435 lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
436 list_add(&lock->link, &active_wake_locks[type]);
438 if (type == WAKE_LOCK_SUSPEND) {
440 #ifdef CONFIG_WAKELOCK_STAT
441 if (lock == &main_wake_lock)
442 update_sleep_wait_stats_locked(1);
443 else if (!wake_lock_active(&main_wake_lock))
444 update_sleep_wait_stats_locked(0);
447 expire_in = has_wake_lock_locked(type);
451 if (debug_mask & DEBUG_EXPIRE)
452 pr_info("wake_lock: %s, start expire timer, "
453 "%ld\n", lock->name, expire_in);
454 mod_timer(&expire_timer, jiffies + expire_in);
456 if (del_timer(&expire_timer))
457 if (debug_mask & DEBUG_EXPIRE)
458 pr_info("wake_lock: %s, stop expire timer\n",
461 queue_work(suspend_work_queue, &suspend_work);
464 spin_unlock_irqrestore(&list_lock, irqflags);
467 void wake_lock(struct wake_lock *lock)
469 wake_lock_internal(lock, 0, 0);
471 EXPORT_SYMBOL(wake_lock);
473 void wake_lock_timeout(struct wake_lock *lock, long timeout)
475 wake_lock_internal(lock, timeout, 1);
477 EXPORT_SYMBOL(wake_lock_timeout);
479 void wake_unlock(struct wake_lock *lock)
482 unsigned long irqflags;
483 spin_lock_irqsave(&list_lock, irqflags);
484 type = lock->flags & WAKE_LOCK_TYPE_MASK;
485 #ifdef CONFIG_WAKELOCK_STAT
486 wake_unlock_stat_locked(lock, 0);
488 if (debug_mask & DEBUG_WAKE_LOCK)
489 pr_info("wake_unlock: %s\n", lock->name);
490 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
491 list_del(&lock->link);
492 list_add(&lock->link, &inactive_locks);
493 if (type == WAKE_LOCK_SUSPEND) {
494 long has_lock = has_wake_lock_locked(type);
496 if (debug_mask & DEBUG_EXPIRE)
497 pr_info("wake_unlock: %s, start expire timer, "
498 "%ld\n", lock->name, has_lock);
499 mod_timer(&expire_timer, jiffies + has_lock);
501 if (del_timer(&expire_timer))
502 if (debug_mask & DEBUG_EXPIRE)
503 pr_info("wake_unlock: %s, stop expire "
504 "timer\n", lock->name);
506 queue_work(suspend_work_queue, &suspend_work);
508 if (lock == &main_wake_lock) {
509 if (debug_mask & DEBUG_SUSPEND)
510 print_active_locks(WAKE_LOCK_SUSPEND);
511 #ifdef CONFIG_WAKELOCK_STAT
512 update_sleep_wait_stats_locked(0);
516 spin_unlock_irqrestore(&list_lock, irqflags);
518 EXPORT_SYMBOL(wake_unlock);
520 int wake_lock_active(struct wake_lock *lock)
522 return !!(lock->flags & WAKE_LOCK_ACTIVE);
524 EXPORT_SYMBOL(wake_lock_active);
526 static int wakelock_stats_open(struct inode *inode, struct file *file)
528 return single_open(file, wakelock_stats_show, NULL);
531 static const struct file_operations wakelock_stats_fops = {
532 .owner = THIS_MODULE,
533 .open = wakelock_stats_open,
536 .release = single_release,
539 static int __init wakelocks_init(void)
544 for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
545 INIT_LIST_HEAD(&active_wake_locks[i]);
547 #ifdef CONFIG_WAKELOCK_STAT
548 wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
549 "deleted_wake_locks");
551 wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
552 wake_lock(&main_wake_lock);
553 wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
555 ret = platform_device_register(&power_device);
557 pr_err("wakelocks_init: platform_device_register failed\n");
558 goto err_platform_device_register;
560 ret = platform_driver_register(&power_driver);
562 pr_err("wakelocks_init: platform_driver_register failed\n");
563 goto err_platform_driver_register;
566 suspend_work_queue = create_singlethread_workqueue("suspend");
567 if (suspend_work_queue == NULL) {
569 goto err_suspend_work_queue;
572 #ifdef CONFIG_WAKELOCK_STAT
573 proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops);
578 err_suspend_work_queue:
579 platform_driver_unregister(&power_driver);
580 err_platform_driver_register:
581 platform_device_unregister(&power_device);
582 err_platform_device_register:
583 wake_lock_destroy(&unknown_wakeup);
584 wake_lock_destroy(&main_wake_lock);
585 #ifdef CONFIG_WAKELOCK_STAT
586 wake_lock_destroy(&deleted_wake_locks);
591 static void __exit wakelocks_exit(void)
593 #ifdef CONFIG_WAKELOCK_STAT
594 remove_proc_entry("wakelocks", NULL);
596 destroy_workqueue(suspend_work_queue);
597 platform_driver_unregister(&power_driver);
598 platform_device_unregister(&power_device);
599 wake_lock_destroy(&unknown_wakeup);
600 wake_lock_destroy(&main_wake_lock);
601 #ifdef CONFIG_WAKELOCK_STAT
602 wake_lock_destroy(&deleted_wake_locks);
606 core_initcall(wakelocks_init);
607 module_exit(wakelocks_exit);