wakeup_reason.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include <linux/wakeup_reason.h>
  18. #include <linux/kernel.h>
  19. #include <linux/irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kobject.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/notifier.h>
  27. #include <linux/suspend.h>
  28. #define MAX_WAKEUP_REASON_IRQS 32
  29. static int irq_list[MAX_WAKEUP_REASON_IRQS];
  30. static int irqcount;
  31. static bool suspend_abort;
  32. static char abort_reason[MAX_SUSPEND_ABORT_LEN];
  33. static struct kobject *wakeup_reason;
  34. static DEFINE_SPINLOCK(resume_reason_lock);
  35. static struct timespec last_xtime; /* wall time before last suspend */
  36. static struct timespec curr_xtime; /* wall time after last suspend */
  37. static struct timespec last_stime; /* total_sleep_time before last suspend */
  38. static struct timespec curr_stime; /* total_sleep_time after last suspend */
  39. static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
  40. char *buf)
  41. {
  42. int irq_no, buf_offset = 0;
  43. struct irq_desc *desc;
  44. spin_lock(&resume_reason_lock);
  45. if (suspend_abort) {
  46. buf_offset = sprintf(buf, "Abort: %s", abort_reason);
  47. } else {
  48. for (irq_no = 0; irq_no < irqcount; irq_no++) {
  49. desc = irq_to_desc(irq_list[irq_no]);
  50. if (desc && desc->action && desc->action->name)
  51. buf_offset += sprintf(buf + buf_offset, "%d %s\n",
  52. irq_list[irq_no], desc->action->name);
  53. else
  54. buf_offset += sprintf(buf + buf_offset, "%d\n",
  55. irq_list[irq_no]);
  56. }
  57. }
  58. spin_unlock(&resume_reason_lock);
  59. return buf_offset;
  60. }
  61. static ssize_t last_suspend_time_show(struct kobject *kobj,
  62. struct kobj_attribute *attr, char *buf)
  63. {
  64. struct timespec sleep_time;
  65. struct timespec total_time;
  66. struct timespec suspend_resume_time;
  67. sleep_time = timespec_sub(curr_stime, last_stime);
  68. total_time = timespec_sub(curr_xtime, last_xtime);
  69. suspend_resume_time = timespec_sub(total_time, sleep_time);
  70. /*
  71. * suspend_resume_time is calculated from sleep_time. Userspace would
  72. * always need both. Export them in pair here.
  73. */
  74. return sprintf(buf, "%lu.%09lu %lu.%09lu\n",
  75. suspend_resume_time.tv_sec, suspend_resume_time.tv_nsec,
  76. sleep_time.tv_sec, sleep_time.tv_nsec);
  77. }
  78. static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
  79. static struct kobj_attribute suspend_time = __ATTR_RO(last_suspend_time);
  80. static struct attribute *attrs[] = {
  81. &resume_reason.attr,
  82. &suspend_time.attr,
  83. NULL,
  84. };
  85. static struct attribute_group attr_group = {
  86. .attrs = attrs,
  87. };
  88. /*
  89. * logs all the wake up reasons to the kernel
  90. * stores the irqs to expose them to the userspace via sysfs
  91. */
  92. void log_wakeup_reason(int irq)
  93. {
  94. struct irq_desc *desc;
  95. desc = irq_to_desc(irq);
  96. if (desc && desc->action && desc->action->name)
  97. printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
  98. desc->action->name);
  99. else
  100. printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
  101. spin_lock(&resume_reason_lock);
  102. if (irqcount == MAX_WAKEUP_REASON_IRQS) {
  103. spin_unlock(&resume_reason_lock);
  104. printk(KERN_WARNING "Resume caused by more than %d IRQs\n",
  105. MAX_WAKEUP_REASON_IRQS);
  106. return;
  107. }
  108. irq_list[irqcount++] = irq;
  109. spin_unlock(&resume_reason_lock);
  110. }
  111. int check_wakeup_reason(int irq)
  112. {
  113. int irq_no;
  114. int ret = false;
  115. spin_lock(&resume_reason_lock);
  116. for (irq_no = 0; irq_no < irqcount; irq_no++)
  117. if (irq_list[irq_no] == irq) {
  118. ret = true;
  119. break;
  120. }
  121. spin_unlock(&resume_reason_lock);
  122. return ret;
  123. }
  124. void log_suspend_abort_reason(const char *fmt, ...)
  125. {
  126. va_list args;
  127. spin_lock(&resume_reason_lock);
  128. //Suspend abort reason has already been logged.
  129. if (suspend_abort) {
  130. spin_unlock(&resume_reason_lock);
  131. return;
  132. }
  133. suspend_abort = true;
  134. va_start(args, fmt);
  135. vsnprintf(abort_reason, MAX_SUSPEND_ABORT_LEN, fmt, args);
  136. va_end(args);
  137. spin_unlock(&resume_reason_lock);
  138. }
  139. /* Detects a suspend and clears all the previous wake up reasons*/
  140. static int wakeup_reason_pm_event(struct notifier_block *notifier,
  141. unsigned long pm_event, void *unused)
  142. {
  143. struct timespec xtom; /* wall_to_monotonic, ignored */
  144. switch (pm_event) {
  145. case PM_SUSPEND_PREPARE:
  146. spin_lock(&resume_reason_lock);
  147. irqcount = 0;
  148. suspend_abort = false;
  149. spin_unlock(&resume_reason_lock);
  150. get_xtime_and_monotonic_and_sleep_offset(&last_xtime, &xtom,
  151. &last_stime);
  152. break;
  153. case PM_POST_SUSPEND:
  154. get_xtime_and_monotonic_and_sleep_offset(&curr_xtime, &xtom,
  155. &curr_stime);
  156. break;
  157. default:
  158. break;
  159. }
  160. return NOTIFY_DONE;
  161. }
  162. static struct notifier_block wakeup_reason_pm_notifier_block = {
  163. .notifier_call = wakeup_reason_pm_event,
  164. };
  165. /* Initializes the sysfs parameter
  166. * registers the pm_event notifier
  167. */
  168. int __init wakeup_reason_init(void)
  169. {
  170. int retval;
  171. retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
  172. if (retval)
  173. printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
  174. __func__, retval);
  175. wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
  176. if (!wakeup_reason) {
  177. printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
  178. __func__);
  179. return 1;
  180. }
  181. retval = sysfs_create_group(wakeup_reason, &attr_group);
  182. if (retval) {
  183. kobject_put(wakeup_reason);
  184. printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
  185. __func__, retval);
  186. }
  187. return 0;
  188. }
  189. late_initcall(wakeup_reason_init);
  190. #ifdef CONFIG_ARCH_MSM
  191. #include <linux/debugfs.h>
  192. #define NR_TOTAL_IRQS 1024
  193. struct wakeup_reason_stats {
  194. unsigned int wakeup_count;
  195. };
  196. static struct wakeup_reason_stats wakeup_reason_stats[NR_TOTAL_IRQS] = {{0,},};
  197. void update_wakeup_reason_stats(int irq)
  198. {
  199. pr_info("IRQ %d [%ps]\n", irq, __builtin_return_address(0));
  200. wakeup_reason_stats[irq].wakeup_count++;
  201. }
  202. #ifdef CONFIG_DEBUG_FS
  203. static int wakeup_reason_stats_show(struct seq_file *s, void *unused)
  204. {
  205. int i;
  206. pr_info("nr_irqs: %d\n", nr_irqs);
  207. seq_puts(s, "irq\twakeup_count\tname\n");
  208. for (i = 0; i < nr_irqs; i++) {
  209. if (wakeup_reason_stats[i].wakeup_count > 0) {
  210. struct irq_desc *desc = irq_to_desc(i);
  211. const char *irq_name = NULL;
  212. if (desc && desc->action && desc->action->name)
  213. irq_name = desc->action->name;
  214. seq_printf(s, "%d\t%u\t%s\n", i,
  215. wakeup_reason_stats[i].wakeup_count, irq_name);
  216. }
  217. }
  218. return 0;
  219. }
  220. static int wakeup_reason_stats_open(struct inode *inode, struct file *file)
  221. {
  222. return single_open(file, wakeup_reason_stats_show, NULL);
  223. }
  224. static const struct file_operations wakeup_reason_stats_ops = {
  225. .open = wakeup_reason_stats_open,
  226. .read = seq_read,
  227. .llseek = seq_lseek,
  228. .release = single_release,
  229. };
  230. static int __init wakeup_reason_debugfs_init(void)
  231. {
  232. debugfs_create_file("wakeup_reason_stats", S_IFREG | S_IRUGO,
  233. NULL, NULL, &wakeup_reason_stats_ops);
  234. return 0;
  235. }
  236. late_initcall(wakeup_reason_debugfs_init);
  237. #endif /* CONFIG_DEBUG_FS */
  238. #endif /* CONFIG_ARCH_MSM */