1 /*
2 * kernel/power/wakeup_reason.c
3 *
4 * Logs the reasons which caused the kernel to resume from
5 * the suspend mode.
6 *
7 * Copyright (C) 2014 Google, Inc.
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18 #include <linux/wakeup_reason.h>
19 #include <linux/kernel.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/kobject.h>
24 #include <linux/sysfs.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.h>
27 #include <linux/notifier.h>
28 #include <linux/suspend.h>
29 #include <linux/debugfs.h>
30
31
32 #define MAX_WAKEUP_REASON_IRQS 32
33 static int irq_list[MAX_WAKEUP_REASON_IRQS];
34 static int irqcount;
35 static bool suspend_abort;
36 static char abort_reason[MAX_SUSPEND_ABORT_LEN];
37 static struct kobject *wakeup_reason;
38 static DEFINE_SPINLOCK(resume_reason_lock);
39
40 static ktime_t last_monotime; /* monotonic time before last suspend */
41 static ktime_t curr_monotime; /* monotonic time after last suspend */
42 static ktime_t last_stime; /* monotonic boottime offset before last suspend */
43 static ktime_t curr_stime; /* monotonic boottime offset after last suspend */
44 #if IS_ENABLED(CONFIG_SUSPEND_TIME)
45 static unsigned int time_in_suspend_bins[32];
46 #endif
47
last_resume_reason_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)48 static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
49 char *buf)
50 {
51 int irq_no, buf_offset = 0;
52 struct irq_desc *desc;
53 spin_lock(&resume_reason_lock);
54 if (suspend_abort) {
55 buf_offset = sprintf(buf, "Abort: %s", abort_reason);
56 } else {
57 for (irq_no = 0; irq_no < irqcount; irq_no++) {
58 desc = irq_to_desc(irq_list[irq_no]);
59 if (desc && desc->action && desc->action->name)
60 buf_offset += sprintf(buf + buf_offset, "%d %s\n",
61 irq_list[irq_no], desc->action->name);
62 else
63 buf_offset += sprintf(buf + buf_offset, "%d\n",
64 irq_list[irq_no]);
65 }
66 }
67 spin_unlock(&resume_reason_lock);
68 return buf_offset;
69 }
70
last_suspend_time_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)71 static ssize_t last_suspend_time_show(struct kobject *kobj,
72 struct kobj_attribute *attr, char *buf)
73 {
74 struct timespec sleep_time;
75 struct timespec total_time;
76 struct timespec suspend_resume_time;
77
78 /*
79 * total_time is calculated from monotonic bootoffsets because
80 * unlike CLOCK_MONOTONIC it include the time spent in suspend state.
81 */
82 total_time = ktime_to_timespec(ktime_sub(curr_stime, last_stime));
83
84 /*
85 * suspend_resume_time is calculated as monotonic (CLOCK_MONOTONIC)
86 * time interval before entering suspend and post suspend.
87 */
88 suspend_resume_time = ktime_to_timespec(ktime_sub(curr_monotime, last_monotime));
89
90 /* sleep_time = total_time - suspend_resume_time */
91 sleep_time = timespec_sub(total_time, suspend_resume_time);
92
93 /* Export suspend_resume_time and sleep_time in pair here. */
94 return sprintf(buf, "%lu.%09lu %lu.%09lu\n",
95 suspend_resume_time.tv_sec, suspend_resume_time.tv_nsec,
96 sleep_time.tv_sec, sleep_time.tv_nsec);
97 }
98
99 static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
100 static struct kobj_attribute suspend_time = __ATTR_RO(last_suspend_time);
101
102 static struct attribute *attrs[] = {
103 &resume_reason.attr,
104 &suspend_time.attr,
105 NULL,
106 };
107 static struct attribute_group attr_group = {
108 .attrs = attrs,
109 };
110
111 /*
112 * logs all the wake up reasons to the kernel
113 * stores the irqs to expose them to the userspace via sysfs
114 */
log_wakeup_reason(int irq)115 void log_wakeup_reason(int irq)
116 {
117 struct irq_desc *desc;
118 desc = irq_to_desc(irq);
119 if (desc && desc->action && desc->action->name)
120 printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
121 desc->action->name);
122 else
123 printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
124
125 spin_lock(&resume_reason_lock);
126 if (irqcount == MAX_WAKEUP_REASON_IRQS) {
127 spin_unlock(&resume_reason_lock);
128 printk(KERN_WARNING "Resume caused by more than %d IRQs\n",
129 MAX_WAKEUP_REASON_IRQS);
130 return;
131 }
132
133 irq_list[irqcount++] = irq;
134 spin_unlock(&resume_reason_lock);
135 }
136
check_wakeup_reason(int irq)137 int check_wakeup_reason(int irq)
138 {
139 int irq_no;
140 int ret = false;
141
142 spin_lock(&resume_reason_lock);
143 for (irq_no = 0; irq_no < irqcount; irq_no++)
144 if (irq_list[irq_no] == irq) {
145 ret = true;
146 break;
147 }
148 spin_unlock(&resume_reason_lock);
149 return ret;
150 }
151
log_suspend_abort_reason(const char * fmt,...)152 void log_suspend_abort_reason(const char *fmt, ...)
153 {
154 va_list args;
155
156 spin_lock(&resume_reason_lock);
157
158 //Suspend abort reason has already been logged.
159 if (suspend_abort) {
160 spin_unlock(&resume_reason_lock);
161 return;
162 }
163
164 suspend_abort = true;
165 va_start(args, fmt);
166 vsnprintf(abort_reason, MAX_SUSPEND_ABORT_LEN, fmt, args);
167 va_end(args);
168 spin_unlock(&resume_reason_lock);
169 }
170
171 /* Detects a suspend and clears all the previous wake up reasons*/
wakeup_reason_pm_event(struct notifier_block * notifier,unsigned long pm_event,void * unused)172 static int wakeup_reason_pm_event(struct notifier_block *notifier,
173 unsigned long pm_event, void *unused)
174 {
175 #if IS_ENABLED(CONFIG_SUSPEND_TIME)
176 ktime_t temp;
177 struct timespec suspend_time;
178 #endif
179
180 switch (pm_event) {
181 case PM_SUSPEND_PREPARE:
182 spin_lock(&resume_reason_lock);
183 irqcount = 0;
184 suspend_abort = false;
185 spin_unlock(&resume_reason_lock);
186 /* monotonic time since boot */
187 last_monotime = ktime_get();
188 /* monotonic time since boot including the time spent in suspend */
189 last_stime = ktime_get_boottime();
190 break;
191 case PM_POST_SUSPEND:
192 /* monotonic time since boot */
193 curr_monotime = ktime_get();
194 /* monotonic time since boot including the time spent in suspend */
195 curr_stime = ktime_get_boottime();
196
197 #if IS_ENABLED(CONFIG_SUSPEND_TIME)
198 temp = ktime_sub(ktime_sub(curr_stime, last_stime),
199 ktime_sub(curr_monotime, last_monotime));
200 suspend_time = ktime_to_timespec(temp);
201 time_in_suspend_bins[fls(suspend_time.tv_sec)]++;
202 pr_info("Suspended for %lu.%03lu seconds\n", suspend_time.tv_sec,
203 suspend_time.tv_nsec / NSEC_PER_MSEC);
204 #endif
205 break;
206 default:
207 break;
208 }
209 return NOTIFY_DONE;
210 }
211
212 static struct notifier_block wakeup_reason_pm_notifier_block = {
213 .notifier_call = wakeup_reason_pm_event,
214 };
215
216 #if IS_ENABLED(CONFIG_DEBUG_FS) && IS_ENABLED(CONFIG_SUSPEND_TIME)
suspend_time_debug_show(struct seq_file * s,void * data)217 static int suspend_time_debug_show(struct seq_file *s, void *data)
218 {
219 int bin;
220 seq_printf(s, "time (secs) count\n");
221 seq_printf(s, "------------------\n");
222 for (bin = 0; bin < 32; bin++) {
223 if (time_in_suspend_bins[bin] == 0)
224 continue;
225 seq_printf(s, "%4d - %4d %4u\n",
226 bin ? 1 << (bin - 1) : 0, 1 << bin,
227 time_in_suspend_bins[bin]);
228 }
229 return 0;
230 }
231
suspend_time_debug_open(struct inode * inode,struct file * file)232 static int suspend_time_debug_open(struct inode *inode, struct file *file)
233 {
234 return single_open(file, suspend_time_debug_show, NULL);
235 }
236
237 static const struct file_operations suspend_time_debug_fops = {
238 .open = suspend_time_debug_open,
239 .read = seq_read,
240 .llseek = seq_lseek,
241 .release = single_release,
242 };
243
suspend_time_debug_init(void)244 static int __init suspend_time_debug_init(void)
245 {
246 struct dentry *d;
247
248 d = debugfs_create_file("suspend_time", 0755, NULL, NULL,
249 &suspend_time_debug_fops);
250 if (!d) {
251 pr_err("Failed to create suspend_time debug file\n");
252 return -ENOMEM;
253 }
254
255 return 0;
256 }
257
258 late_initcall(suspend_time_debug_init);
259 #endif
260
261 /* Initializes the sysfs parameter
262 * registers the pm_event notifier
263 */
wakeup_reason_init(void)264 int __init wakeup_reason_init(void)
265 {
266 int retval;
267
268 retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
269 if (retval)
270 printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
271 __func__, retval);
272
273 wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
274 if (!wakeup_reason) {
275 printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
276 __func__);
277 return 1;
278 }
279 retval = sysfs_create_group(wakeup_reason, &attr_group);
280 if (retval) {
281 kobject_put(wakeup_reason);
282 printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
283 __func__, retval);
284 }
285 return 0;
286 }
287
288 late_initcall(wakeup_reason_init);
289