FROMLIST: binder: fix an ret value override
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / kernel / power / wakeup_reason.c
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 #ifdef CONFIG_SOC_EXYNOS5433
32 #define MAX_WAKEUP_REASON_IRQS 64
33 #else
34 #define MAX_WAKEUP_REASON_IRQS 32
35 #endif
36 static int irq_list[MAX_WAKEUP_REASON_IRQS];
37 static int irqcount;
38 static bool suspend_abort;
39 static char abort_reason[MAX_SUSPEND_ABORT_LEN];
40 static struct kobject *wakeup_reason;
41 static DEFINE_SPINLOCK(resume_reason_lock);
42
43 static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
44 char *buf)
45 {
46 int irq_no, buf_offset = 0;
47 struct irq_desc *desc;
48 spin_lock(&resume_reason_lock);
49 if (suspend_abort) {
50 buf_offset = sprintf(buf, "Abort: %s", abort_reason);
51 } else {
52 for (irq_no = 0; irq_no < irqcount; irq_no++) {
53 desc = irq_to_desc(irq_list[irq_no]);
54 if (desc && desc->action && desc->action->name)
55 buf_offset += sprintf(buf + buf_offset, "%d %s\n",
56 irq_list[irq_no], desc->action->name);
57 else
58 buf_offset += sprintf(buf + buf_offset, "%d\n",
59 irq_list[irq_no]);
60 }
61 }
62 spin_unlock(&resume_reason_lock);
63 return buf_offset;
64 }
65
66 static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
67
68 static struct attribute *attrs[] = {
69 &resume_reason.attr,
70 NULL,
71 };
72 static struct attribute_group attr_group = {
73 .attrs = attrs,
74 };
75
76 /*
77 * logs all the wake up reasons to the kernel
78 * stores the irqs to expose them to the userspace via sysfs
79 */
80 void log_wakeup_reason(int irq)
81 {
82 struct irq_desc *desc;
83 desc = irq_to_desc(irq);
84 if (desc && desc->action && desc->action->name)
85 printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
86 desc->action->name);
87 else
88 printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
89
90 spin_lock(&resume_reason_lock);
91 if (irqcount == MAX_WAKEUP_REASON_IRQS) {
92 spin_unlock(&resume_reason_lock);
93 printk(KERN_WARNING "Resume caused by more than %d IRQs\n",
94 MAX_WAKEUP_REASON_IRQS);
95 return;
96 }
97
98 irq_list[irqcount++] = irq;
99 spin_unlock(&resume_reason_lock);
100 }
101
102 int check_wakeup_reason(int irq)
103 {
104 int irq_no;
105 int ret = false;
106
107 spin_lock(&resume_reason_lock);
108 for (irq_no = 0; irq_no < irqcount; irq_no++)
109 if (irq_list[irq_no] == irq) {
110 ret = true;
111 break;
112 }
113 spin_unlock(&resume_reason_lock);
114 return ret;
115 }
116
117 void log_suspend_abort_reason(const char *fmt, ...)
118 {
119 va_list args;
120
121 spin_lock(&resume_reason_lock);
122
123 //Suspend abort reason has already been logged.
124 if (suspend_abort) {
125 spin_unlock(&resume_reason_lock);
126 return;
127 }
128
129 suspend_abort = true;
130 va_start(args, fmt);
131 snprintf(abort_reason, MAX_SUSPEND_ABORT_LEN, fmt, args);
132 va_end(args);
133 spin_unlock(&resume_reason_lock);
134 }
135
136 /* Detects a suspend and clears all the previous wake up reasons*/
137 static int wakeup_reason_pm_event(struct notifier_block *notifier,
138 unsigned long pm_event, void *unused)
139 {
140 switch (pm_event) {
141 case PM_SUSPEND_PREPARE:
142 spin_lock(&resume_reason_lock);
143 irqcount = 0;
144 suspend_abort = false;
145 spin_unlock(&resume_reason_lock);
146 break;
147 default:
148 break;
149 }
150 return NOTIFY_DONE;
151 }
152
153 static struct notifier_block wakeup_reason_pm_notifier_block = {
154 .notifier_call = wakeup_reason_pm_event,
155 };
156
157 /* Initializes the sysfs parameter
158 * registers the pm_event notifier
159 */
160 int __init wakeup_reason_init(void)
161 {
162 int retval;
163
164 retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
165 if (retval)
166 printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
167 __func__, retval);
168
169 wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
170 if (!wakeup_reason) {
171 printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
172 __func__);
173 return 1;
174 }
175 retval = sysfs_create_group(wakeup_reason, &attr_group);
176 if (retval) {
177 kobject_put(wakeup_reason);
178 printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
179 __func__, retval);
180 }
181 return 0;
182 }
183
184 late_initcall(wakeup_reason_init);
185
186 #ifdef CONFIG_ARCH_EXYNOS
187 #ifdef CONFIG_SOC_EXYNOS5433
188 #define NR_EINT 64
189 #else
190 #define NR_EINT 32
191 #endif
192 struct wakeup_reason_stats {
193 int irq;
194 unsigned int wakeup_count;
195 };
196 static struct wakeup_reason_stats wakeup_reason_stats[NR_EINT] = {{0,},};
197
198 void update_wakeup_reason_stats(int irq, int eint)
199 {
200 if (eint >= NR_EINT) {
201 pr_info("%s : can't update wakeup reason stat infomation\n", __func__);
202 return;
203 }
204
205 wakeup_reason_stats[eint].irq = irq;
206 wakeup_reason_stats[eint].wakeup_count++;
207 }
208
209 #ifdef CONFIG_DEBUG_FS
210 static int wakeup_reason_stats_show(struct seq_file *s, void *unused)
211 {
212 int i;
213
214 seq_puts(s, "eint_no\tirq\twakeup_count\tname\n");
215 for (i = 0; i < NR_EINT; i++) {
216 struct irq_desc *desc = irq_to_desc(wakeup_reason_stats[i].irq);
217 const char *irq_name = NULL;
218
219 if (!wakeup_reason_stats[i].irq)
220 continue;
221
222 if (desc && desc->action && desc->action->name)
223 irq_name = desc->action->name;
224
225 seq_printf(s, "%d\t%d\t%u\t\t%s\n", i,
226 wakeup_reason_stats[i].irq,
227 wakeup_reason_stats[i].wakeup_count, irq_name);
228 }
229
230 return 0;
231 }
232
233 static int wakeup_reason_stats_open(struct inode *inode, struct file *file)
234 {
235 return single_open(file, wakeup_reason_stats_show, NULL);
236 }
237
238 static const struct file_operations wakeup_reason_stats_ops = {
239 .open = wakeup_reason_stats_open,
240 .read = seq_read,
241 .llseek = seq_lseek,
242 .release = single_release,
243 };
244
245 static int __init wakeup_reason_debugfs_init(void)
246 {
247 debugfs_create_file("wakeup_reason_stats", S_IFREG | S_IRUGO,
248 NULL, NULL, &wakeup_reason_stats_ops);
249 return 0;
250 }
251
252 late_initcall(wakeup_reason_debugfs_init);
253 #endif /* CONFIG_DEBUG_FS */
254 #endif /* CONFIG_ARCH_EXYNOS */