Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 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 | |
| 30 | |
| 31 | #define MAX_WAKEUP_REASON_IRQS 32 |
| 32 | static int irq_list[MAX_WAKEUP_REASON_IRQS]; |
Greg Hackmann | e13dbc7 | 2014-03-10 14:21:30 -0700 | [diff] [blame] | 33 | static int irqcount; |
Ruchi Kandoi | 6118cb4 | 2014-10-29 10:36:27 -0700 | [diff] [blame] | 34 | static bool suspend_abort; |
| 35 | static char abort_reason[MAX_SUSPEND_ABORT_LEN]; |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 36 | static struct kobject *wakeup_reason; |
Dmitry Shmidt | dfa4033 | 2014-10-31 16:05:46 -0700 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(resume_reason_lock); |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 38 | |
Amit Pundir | 7df92a1 | 2015-04-14 02:38:20 +0530 | [diff] [blame] | 39 | static ktime_t last_monotime; /* monotonic time before last suspend */ |
| 40 | static ktime_t curr_monotime; /* monotonic time after last suspend */ |
| 41 | static ktime_t last_stime; /* monotonic boottime offset before last suspend */ |
| 42 | static ktime_t curr_stime; /* monotonic boottime offset after last suspend */ |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 43 | |
Ruchi Kandoi | b4e6247 | 2014-04-24 14:31:57 -0700 | [diff] [blame] | 44 | static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr, |
Ruchi Kandoi | 1135122 | 2014-02-20 19:47:38 -0800 | [diff] [blame] | 45 | char *buf) |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 46 | { |
| 47 | int irq_no, buf_offset = 0; |
| 48 | struct irq_desc *desc; |
| 49 | spin_lock(&resume_reason_lock); |
Ruchi Kandoi | 6118cb4 | 2014-10-29 10:36:27 -0700 | [diff] [blame] | 50 | if (suspend_abort) { |
| 51 | buf_offset = sprintf(buf, "Abort: %s", abort_reason); |
| 52 | } else { |
| 53 | for (irq_no = 0; irq_no < irqcount; irq_no++) { |
| 54 | desc = irq_to_desc(irq_list[irq_no]); |
| 55 | if (desc && desc->action && desc->action->name) |
| 56 | buf_offset += sprintf(buf + buf_offset, "%d %s\n", |
| 57 | irq_list[irq_no], desc->action->name); |
| 58 | else |
| 59 | buf_offset += sprintf(buf + buf_offset, "%d\n", |
| 60 | irq_list[irq_no]); |
| 61 | } |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 62 | } |
| 63 | spin_unlock(&resume_reason_lock); |
| 64 | return buf_offset; |
| 65 | } |
| 66 | |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 67 | static ssize_t last_suspend_time_show(struct kobject *kobj, |
| 68 | struct kobj_attribute *attr, char *buf) |
| 69 | { |
| 70 | struct timespec sleep_time; |
| 71 | struct timespec total_time; |
| 72 | struct timespec suspend_resume_time; |
| 73 | |
Amit Pundir | 7df92a1 | 2015-04-14 02:38:20 +0530 | [diff] [blame] | 74 | /* |
| 75 | * total_time is calculated from monotonic bootoffsets because |
| 76 | * unlike CLOCK_MONOTONIC it include the time spent in suspend state. |
| 77 | */ |
| 78 | total_time = ktime_to_timespec(ktime_sub(curr_stime, last_stime)); |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
Amit Pundir | 7df92a1 | 2015-04-14 02:38:20 +0530 | [diff] [blame] | 81 | * suspend_resume_time is calculated as monotonic (CLOCK_MONOTONIC) |
| 82 | * time interval before entering suspend and post suspend. |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 83 | */ |
Amit Pundir | 7df92a1 | 2015-04-14 02:38:20 +0530 | [diff] [blame] | 84 | suspend_resume_time = ktime_to_timespec(ktime_sub(curr_monotime, last_monotime)); |
| 85 | |
| 86 | /* sleep_time = total_time - suspend_resume_time */ |
| 87 | sleep_time = timespec_sub(total_time, suspend_resume_time); |
| 88 | |
| 89 | /* Export suspend_resume_time and sleep_time in pair here. */ |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 90 | return sprintf(buf, "%lu.%09lu %lu.%09lu\n", |
| 91 | suspend_resume_time.tv_sec, suspend_resume_time.tv_nsec, |
| 92 | sleep_time.tv_sec, sleep_time.tv_nsec); |
| 93 | } |
| 94 | |
Ruchi Kandoi | b4e6247 | 2014-04-24 14:31:57 -0700 | [diff] [blame] | 95 | static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason); |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 96 | static struct kobj_attribute suspend_time = __ATTR_RO(last_suspend_time); |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 97 | |
| 98 | static struct attribute *attrs[] = { |
| 99 | &resume_reason.attr, |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 100 | &suspend_time.attr, |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 101 | NULL, |
| 102 | }; |
| 103 | static struct attribute_group attr_group = { |
| 104 | .attrs = attrs, |
| 105 | }; |
| 106 | |
| 107 | /* |
| 108 | * logs all the wake up reasons to the kernel |
| 109 | * stores the irqs to expose them to the userspace via sysfs |
| 110 | */ |
| 111 | void log_wakeup_reason(int irq) |
| 112 | { |
| 113 | struct irq_desc *desc; |
| 114 | desc = irq_to_desc(irq); |
| 115 | if (desc && desc->action && desc->action->name) |
| 116 | printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq, |
| 117 | desc->action->name); |
| 118 | else |
| 119 | printk(KERN_INFO "Resume caused by IRQ %d\n", irq); |
| 120 | |
| 121 | spin_lock(&resume_reason_lock); |
Greg Hackmann | e13dbc7 | 2014-03-10 14:21:30 -0700 | [diff] [blame] | 122 | if (irqcount == MAX_WAKEUP_REASON_IRQS) { |
Ruchi Kandoi | 1ceb7e2 | 2014-03-07 12:54:30 -0800 | [diff] [blame] | 123 | spin_unlock(&resume_reason_lock); |
| 124 | printk(KERN_WARNING "Resume caused by more than %d IRQs\n", |
| 125 | MAX_WAKEUP_REASON_IRQS); |
| 126 | return; |
| 127 | } |
| 128 | |
Greg Hackmann | e13dbc7 | 2014-03-10 14:21:30 -0700 | [diff] [blame] | 129 | irq_list[irqcount++] = irq; |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 130 | spin_unlock(&resume_reason_lock); |
| 131 | } |
| 132 | |
Dmitry Shmidt | dfa4033 | 2014-10-31 16:05:46 -0700 | [diff] [blame] | 133 | int check_wakeup_reason(int irq) |
| 134 | { |
| 135 | int irq_no; |
| 136 | int ret = false; |
| 137 | |
| 138 | spin_lock(&resume_reason_lock); |
| 139 | for (irq_no = 0; irq_no < irqcount; irq_no++) |
| 140 | if (irq_list[irq_no] == irq) { |
| 141 | ret = true; |
| 142 | break; |
| 143 | } |
| 144 | spin_unlock(&resume_reason_lock); |
| 145 | return ret; |
| 146 | } |
| 147 | |
Ruchi Kandoi | 6118cb4 | 2014-10-29 10:36:27 -0700 | [diff] [blame] | 148 | void log_suspend_abort_reason(const char *fmt, ...) |
| 149 | { |
| 150 | va_list args; |
| 151 | |
| 152 | spin_lock(&resume_reason_lock); |
| 153 | |
| 154 | //Suspend abort reason has already been logged. |
| 155 | if (suspend_abort) { |
| 156 | spin_unlock(&resume_reason_lock); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | suspend_abort = true; |
| 161 | va_start(args, fmt); |
Ruchi Kandoi | 9d17e24 | 2015-08-05 16:54:53 -0700 | [diff] [blame] | 162 | vsnprintf(abort_reason, MAX_SUSPEND_ABORT_LEN, fmt, args); |
Ruchi Kandoi | 6118cb4 | 2014-10-29 10:36:27 -0700 | [diff] [blame] | 163 | va_end(args); |
| 164 | spin_unlock(&resume_reason_lock); |
| 165 | } |
| 166 | |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 167 | /* Detects a suspend and clears all the previous wake up reasons*/ |
| 168 | static int wakeup_reason_pm_event(struct notifier_block *notifier, |
| 169 | unsigned long pm_event, void *unused) |
| 170 | { |
| 171 | switch (pm_event) { |
| 172 | case PM_SUSPEND_PREPARE: |
| 173 | spin_lock(&resume_reason_lock); |
Greg Hackmann | e13dbc7 | 2014-03-10 14:21:30 -0700 | [diff] [blame] | 174 | irqcount = 0; |
Ruchi Kandoi | 6118cb4 | 2014-10-29 10:36:27 -0700 | [diff] [blame] | 175 | suspend_abort = false; |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 176 | spin_unlock(&resume_reason_lock); |
Amit Pundir | 7df92a1 | 2015-04-14 02:38:20 +0530 | [diff] [blame] | 177 | /* monotonic time since boot */ |
| 178 | last_monotime = ktime_get(); |
| 179 | /* monotonic time since boot including the time spent in suspend */ |
| 180 | last_stime = ktime_get_boottime(); |
jinqian | e2cc63f | 2015-03-25 16:18:44 -0700 | [diff] [blame] | 181 | break; |
| 182 | case PM_POST_SUSPEND: |
Amit Pundir | 7df92a1 | 2015-04-14 02:38:20 +0530 | [diff] [blame] | 183 | /* monotonic time since boot */ |
| 184 | curr_monotime = ktime_get(); |
| 185 | /* monotonic time since boot including the time spent in suspend */ |
| 186 | curr_stime = ktime_get_boottime(); |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 187 | break; |
| 188 | default: |
| 189 | break; |
| 190 | } |
| 191 | return NOTIFY_DONE; |
| 192 | } |
| 193 | |
| 194 | static struct notifier_block wakeup_reason_pm_notifier_block = { |
| 195 | .notifier_call = wakeup_reason_pm_event, |
| 196 | }; |
| 197 | |
| 198 | /* Initializes the sysfs parameter |
| 199 | * registers the pm_event notifier |
| 200 | */ |
Ruchi Kandoi | 1135122 | 2014-02-20 19:47:38 -0800 | [diff] [blame] | 201 | int __init wakeup_reason_init(void) |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 202 | { |
| 203 | int retval; |
Dmitry Shmidt | dfa4033 | 2014-10-31 16:05:46 -0700 | [diff] [blame] | 204 | |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 205 | retval = register_pm_notifier(&wakeup_reason_pm_notifier_block); |
| 206 | if (retval) |
| 207 | printk(KERN_WARNING "[%s] failed to register PM notifier %d\n", |
| 208 | __func__, retval); |
| 209 | |
| 210 | wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj); |
| 211 | if (!wakeup_reason) { |
| 212 | printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n", |
| 213 | __func__); |
Ruchi Kandoi | 1135122 | 2014-02-20 19:47:38 -0800 | [diff] [blame] | 214 | return 1; |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 215 | } |
| 216 | retval = sysfs_create_group(wakeup_reason, &attr_group); |
| 217 | if (retval) { |
| 218 | kobject_put(wakeup_reason); |
| 219 | printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n", |
| 220 | __func__, retval); |
| 221 | } |
Ruchi Kandoi | 1135122 | 2014-02-20 19:47:38 -0800 | [diff] [blame] | 222 | return 0; |
Ruchi Kandoi | 6acefbe | 2014-02-19 15:30:47 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | late_initcall(wakeup_reason_init); |