wakeup_reason.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 spinlock_t resume_reason_lock;
  35. static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
  36. char *buf)
  37. {
  38. int irq_no, buf_offset = 0;
  39. struct irq_desc *desc;
  40. spin_lock(&resume_reason_lock);
  41. if (suspend_abort) {
  42. buf_offset = sprintf(buf, "Abort: %s", abort_reason);
  43. } else {
  44. for (irq_no = 0; irq_no < irqcount; irq_no++) {
  45. desc = irq_to_desc(irq_list[irq_no]);
  46. if (desc && desc->action && desc->action->name)
  47. buf_offset += sprintf(buf + buf_offset, "%d %s\n",
  48. irq_list[irq_no], desc->action->name);
  49. else
  50. buf_offset += sprintf(buf + buf_offset, "%d\n",
  51. irq_list[irq_no]);
  52. }
  53. }
  54. spin_unlock(&resume_reason_lock);
  55. return buf_offset;
  56. }
  57. static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
  58. static struct attribute *attrs[] = {
  59. &resume_reason.attr,
  60. NULL,
  61. };
  62. static struct attribute_group attr_group = {
  63. .attrs = attrs,
  64. };
  65. /*
  66. * logs all the wake up reasons to the kernel
  67. * stores the irqs to expose them to the userspace via sysfs
  68. */
  69. void log_wakeup_reason(int irq)
  70. {
  71. struct irq_desc *desc;
  72. desc = irq_to_desc(irq);
  73. if (desc && desc->action && desc->action->name)
  74. printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
  75. desc->action->name);
  76. else
  77. printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
  78. spin_lock(&resume_reason_lock);
  79. if (irqcount == MAX_WAKEUP_REASON_IRQS) {
  80. spin_unlock(&resume_reason_lock);
  81. printk(KERN_WARNING "Resume caused by more than %d IRQs\n",
  82. MAX_WAKEUP_REASON_IRQS);
  83. return;
  84. }
  85. irq_list[irqcount++] = irq;
  86. spin_unlock(&resume_reason_lock);
  87. }
  88. void log_suspend_abort_reason(const char *fmt, ...)
  89. {
  90. va_list args;
  91. spin_lock(&resume_reason_lock);
  92. //Suspend abort reason has already been logged.
  93. if (suspend_abort) {
  94. spin_unlock(&resume_reason_lock);
  95. return;
  96. }
  97. suspend_abort = true;
  98. va_start(args, fmt);
  99. snprintf(abort_reason, MAX_SUSPEND_ABORT_LEN, fmt, args);
  100. va_end(args);
  101. spin_unlock(&resume_reason_lock);
  102. }
  103. /* Detects a suspend and clears all the previous wake up reasons*/
  104. static int wakeup_reason_pm_event(struct notifier_block *notifier,
  105. unsigned long pm_event, void *unused)
  106. {
  107. switch (pm_event) {
  108. case PM_SUSPEND_PREPARE:
  109. spin_lock(&resume_reason_lock);
  110. irqcount = 0;
  111. suspend_abort = false;
  112. spin_unlock(&resume_reason_lock);
  113. break;
  114. default:
  115. break;
  116. }
  117. return NOTIFY_DONE;
  118. }
  119. static struct notifier_block wakeup_reason_pm_notifier_block = {
  120. .notifier_call = wakeup_reason_pm_event,
  121. };
  122. /* Initializes the sysfs parameter
  123. * registers the pm_event notifier
  124. */
  125. int __init wakeup_reason_init(void)
  126. {
  127. int retval;
  128. spin_lock_init(&resume_reason_lock);
  129. retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
  130. if (retval)
  131. printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
  132. __func__, retval);
  133. wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
  134. if (!wakeup_reason) {
  135. printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
  136. __func__);
  137. return 1;
  138. }
  139. retval = sysfs_create_group(wakeup_reason, &attr_group);
  140. if (retval) {
  141. kobject_put(wakeup_reason);
  142. printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
  143. __func__, retval);
  144. }
  145. return 0;
  146. }
  147. late_initcall(wakeup_reason_init);
  148. #ifdef CONFIG_ARCH_MSM
  149. #include <linux/debugfs.h>
  150. #define NR_TOTAL_IRQS 1024
  151. struct wakeup_reason_stats {
  152. unsigned int wakeup_count;
  153. };
  154. static struct wakeup_reason_stats wakeup_reason_stats[NR_TOTAL_IRQS] = {{0,},};
  155. void update_wakeup_reason_stats(int irq)
  156. {
  157. pr_info("IRQ %d [%ps]\n", irq, __builtin_return_address(0));
  158. wakeup_reason_stats[irq].wakeup_count++;
  159. }
  160. #ifdef CONFIG_DEBUG_FS
  161. static int wakeup_reason_stats_show(struct seq_file *s, void *unused)
  162. {
  163. int i;
  164. pr_info("nr_irqs: %d\n", nr_irqs);
  165. seq_puts(s, "irq\twakeup_count\tname\n");
  166. for (i = 0; i < nr_irqs; i++) {
  167. if (wakeup_reason_stats[i].wakeup_count > 0) {
  168. struct irq_desc *desc = irq_to_desc(i);
  169. const char *irq_name = NULL;
  170. if (desc && desc->action && desc->action->name)
  171. irq_name = desc->action->name;
  172. seq_printf(s, "%d\t%u\t%s\n", i,
  173. wakeup_reason_stats[i].wakeup_count, irq_name);
  174. }
  175. }
  176. return 0;
  177. }
  178. static int wakeup_reason_stats_open(struct inode *inode, struct file *file)
  179. {
  180. return single_open(file, wakeup_reason_stats_show, NULL);
  181. }
  182. static const struct file_operations wakeup_reason_stats_ops = {
  183. .open = wakeup_reason_stats_open,
  184. .read = seq_read,
  185. .llseek = seq_lseek,
  186. .release = single_release,
  187. };
  188. static int __init wakeup_reason_debugfs_init(void)
  189. {
  190. debugfs_create_file("wakeup_reason_stats", S_IFREG | S_IRUGO,
  191. NULL, NULL, &wakeup_reason_stats_ops);
  192. return 0;
  193. }
  194. late_initcall(wakeup_reason_debugfs_init);
  195. #endif /* CONFIG_DEBUG_FS */
  196. #endif /* CONFIG_ARCH_MSM */