2 * kernel/power/wakelock.c
4 * User space wakeup sources support.
6 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
8 * This code is based on the analogous interface allowing user space to
9 * manipulate wakelocks on Android.
12 #include <linux/ctype.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/hrtimer.h>
16 #include <linux/list.h>
17 #include <linux/rbtree.h>
18 #include <linux/slab.h>
20 static DEFINE_MUTEX(wakelocks_lock
);
25 struct wakeup_source ws
;
26 #ifdef CONFIG_PM_WAKELOCKS_GC
31 static struct rb_root wakelocks_tree
= RB_ROOT
;
33 ssize_t
pm_show_wakelocks(char *buf
, bool show_active
)
38 char *end
= buf
+ PAGE_SIZE
;
40 mutex_lock(&wakelocks_lock
);
42 for (node
= rb_first(&wakelocks_tree
); node
; node
= rb_next(node
)) {
43 wl
= rb_entry(node
, struct wakelock
, node
);
44 if (wl
->ws
.active
== show_active
)
45 str
+= scnprintf(str
, end
- str
, "%s ", wl
->name
);
50 str
+= scnprintf(str
, end
- str
, "\n");
52 mutex_unlock(&wakelocks_lock
);
56 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
57 static unsigned int number_of_wakelocks
;
59 static inline bool wakelocks_limit_exceeded(void)
61 return number_of_wakelocks
> CONFIG_PM_WAKELOCKS_LIMIT
;
64 static inline void increment_wakelocks_number(void)
66 number_of_wakelocks
++;
69 static inline void decrement_wakelocks_number(void)
71 number_of_wakelocks
--;
73 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
74 static inline bool wakelocks_limit_exceeded(void) { return false; }
75 static inline void increment_wakelocks_number(void) {}
76 static inline void decrement_wakelocks_number(void) {}
77 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
79 #ifdef CONFIG_PM_WAKELOCKS_GC
80 #define WL_GC_COUNT_MAX 100
81 #define WL_GC_TIME_SEC 300
83 static LIST_HEAD(wakelocks_lru_list
);
84 static unsigned int wakelocks_gc_count
;
86 static inline void wakelocks_lru_add(struct wakelock
*wl
)
88 list_add(&wl
->lru
, &wakelocks_lru_list
);
91 static inline void wakelocks_lru_most_recent(struct wakelock
*wl
)
93 list_move(&wl
->lru
, &wakelocks_lru_list
);
96 static void wakelocks_gc(void)
98 struct wakelock
*wl
, *aux
;
101 if (++wakelocks_gc_count
<= WL_GC_COUNT_MAX
)
105 list_for_each_entry_safe_reverse(wl
, aux
, &wakelocks_lru_list
, lru
) {
109 spin_lock_irq(&wl
->ws
.lock
);
110 idle_time_ns
= ktime_to_ns(ktime_sub(now
, wl
->ws
.last_time
));
111 active
= wl
->ws
.active
;
112 spin_unlock_irq(&wl
->ws
.lock
);
114 if (idle_time_ns
< ((u64
)WL_GC_TIME_SEC
* NSEC_PER_SEC
))
118 wakeup_source_remove(&wl
->ws
);
119 rb_erase(&wl
->node
, &wakelocks_tree
);
123 decrement_wakelocks_number();
126 wakelocks_gc_count
= 0;
128 #else /* !CONFIG_PM_WAKELOCKS_GC */
129 static inline void wakelocks_lru_add(struct wakelock
*wl
) {}
130 static inline void wakelocks_lru_most_recent(struct wakelock
*wl
) {}
131 static inline void wakelocks_gc(void) {}
132 #endif /* !CONFIG_PM_WAKELOCKS_GC */
134 static struct wakelock
*wakelock_lookup_add(const char *name
, size_t len
,
135 bool add_if_not_found
)
137 struct rb_node
**node
= &wakelocks_tree
.rb_node
;
138 struct rb_node
*parent
= *node
;
145 wl
= rb_entry(*node
, struct wakelock
, node
);
146 diff
= strncmp(name
, wl
->name
, len
);
154 node
= &(*node
)->rb_left
;
156 node
= &(*node
)->rb_right
;
158 if (!add_if_not_found
)
159 return ERR_PTR(-EINVAL
);
161 if (wakelocks_limit_exceeded())
162 return ERR_PTR(-ENOSPC
);
164 /* Not found, we have to add a new one. */
165 wl
= kzalloc(sizeof(*wl
), GFP_KERNEL
);
167 return ERR_PTR(-ENOMEM
);
169 wl
->name
= kstrndup(name
, len
, GFP_KERNEL
);
172 return ERR_PTR(-ENOMEM
);
174 wl
->ws
.name
= wl
->name
;
175 wakeup_source_add(&wl
->ws
);
176 rb_link_node(&wl
->node
, parent
, node
);
177 rb_insert_color(&wl
->node
, &wakelocks_tree
);
178 wakelocks_lru_add(wl
);
179 increment_wakelocks_number();
183 int pm_wake_lock(const char *buf
)
185 const char *str
= buf
;
191 while (*str
&& !isspace(*str
))
198 if (*str
&& *str
!= '\n') {
199 /* Find out if there's a valid timeout string appended. */
200 ret
= kstrtou64(skip_spaces(str
), 10, &timeout_ns
);
205 mutex_lock(&wakelocks_lock
);
207 wl
= wakelock_lookup_add(buf
, len
, true);
213 u64 timeout_ms
= timeout_ns
+ NSEC_PER_MSEC
- 1;
215 do_div(timeout_ms
, NSEC_PER_MSEC
);
216 __pm_wakeup_event(&wl
->ws
, timeout_ms
);
218 __pm_stay_awake(&wl
->ws
);
221 wakelocks_lru_most_recent(wl
);
224 mutex_unlock(&wakelocks_lock
);
228 int pm_wake_unlock(const char *buf
)
238 if (buf
[len
-1] == '\n')
244 mutex_lock(&wakelocks_lock
);
246 wl
= wakelock_lookup_add(buf
, len
, false);
253 wakelocks_lru_most_recent(wl
);
257 mutex_unlock(&wakelocks_lock
);