nmi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Pseudo NMI support on sparc64 systems.
  2. *
  3. * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
  4. *
  5. * The NMI watchdog support and infrastructure is based almost
  6. * entirely upon the x86 NMI support code.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/param.h>
  10. #include <linux/init.h>
  11. #include <linux/percpu.h>
  12. #include <linux/nmi.h>
  13. #include <linux/module.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/delay.h>
  20. #include <linux/smp.h>
  21. #include <asm/ptrace.h>
  22. #include <asm/local.h>
  23. #include <asm/pcr.h>
  24. /* We don't have a real NMI on sparc64, but we can fake one
  25. * up using profiling counter overflow interrupts and interrupt
  26. * levels.
  27. *
  28. * The profile overflow interrupts at level 15, so we use
  29. * level 14 as our IRQ off level.
  30. */
  31. static int nmi_watchdog_active;
  32. static int panic_on_timeout;
  33. int nmi_usable;
  34. EXPORT_SYMBOL_GPL(nmi_usable);
  35. static unsigned int nmi_hz = HZ;
  36. static DEFINE_PER_CPU(unsigned int, last_irq_sum);
  37. static DEFINE_PER_CPU(local_t, alert_counter);
  38. static DEFINE_PER_CPU(int, nmi_touch);
  39. void touch_nmi_watchdog(void)
  40. {
  41. if (nmi_watchdog_active) {
  42. int cpu;
  43. for_each_present_cpu(cpu) {
  44. if (per_cpu(nmi_touch, cpu) != 1)
  45. per_cpu(nmi_touch, cpu) = 1;
  46. }
  47. }
  48. touch_softlockup_watchdog();
  49. }
  50. EXPORT_SYMBOL(touch_nmi_watchdog);
  51. static void die_nmi(const char *str, struct pt_regs *regs, int do_panic)
  52. {
  53. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0,
  54. pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
  55. return;
  56. console_verbose();
  57. bust_spinlocks(1);
  58. printk(KERN_EMERG "%s", str);
  59. printk(" on CPU%d, ip %08lx, registers:\n",
  60. smp_processor_id(), regs->tpc);
  61. show_regs(regs);
  62. dump_stack();
  63. bust_spinlocks(0);
  64. if (do_panic || panic_on_oops)
  65. panic("Non maskable interrupt");
  66. local_irq_enable();
  67. do_exit(SIGBUS);
  68. }
  69. notrace __kprobes void perfctr_irq(int irq, struct pt_regs *regs)
  70. {
  71. unsigned int sum, touched = 0;
  72. int cpu = smp_processor_id();
  73. clear_softint(1 << irq);
  74. pcr_ops->write(PCR_PIC_PRIV);
  75. local_cpu_data().__nmi_count++;
  76. if (notify_die(DIE_NMI, "nmi", regs, 0,
  77. pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
  78. touched = 1;
  79. sum = kstat_irqs_cpu(0, cpu);
  80. if (__get_cpu_var(nmi_touch)) {
  81. __get_cpu_var(nmi_touch) = 0;
  82. touched = 1;
  83. }
  84. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  85. local_inc(&__get_cpu_var(alert_counter));
  86. if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
  87. die_nmi("BUG: NMI Watchdog detected LOCKUP",
  88. regs, panic_on_timeout);
  89. } else {
  90. __get_cpu_var(last_irq_sum) = sum;
  91. local_set(&__get_cpu_var(alert_counter), 0);
  92. }
  93. if (nmi_usable) {
  94. write_pic(picl_value(nmi_hz));
  95. pcr_ops->write(pcr_enable);
  96. }
  97. }
  98. static inline unsigned int get_nmi_count(int cpu)
  99. {
  100. return cpu_data(cpu).__nmi_count;
  101. }
  102. static int endflag __initdata;
  103. static __init void nmi_cpu_busy(void *data)
  104. {
  105. local_irq_enable_in_hardirq();
  106. while (endflag == 0)
  107. mb();
  108. }
  109. static void report_broken_nmi(int cpu, int *prev_nmi_count)
  110. {
  111. printk(KERN_CONT "\n");
  112. printk(KERN_WARNING
  113. "WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  114. cpu, prev_nmi_count[cpu], get_nmi_count(cpu));
  115. printk(KERN_WARNING
  116. "Please report this to bugzilla.kernel.org,\n");
  117. printk(KERN_WARNING
  118. "and attach the output of the 'dmesg' command.\n");
  119. nmi_usable = 0;
  120. }
  121. static void stop_watchdog(void *unused)
  122. {
  123. pcr_ops->write(PCR_PIC_PRIV);
  124. }
  125. static int __init check_nmi_watchdog(void)
  126. {
  127. unsigned int *prev_nmi_count;
  128. int cpu, err;
  129. prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL);
  130. if (!prev_nmi_count) {
  131. err = -ENOMEM;
  132. goto error;
  133. }
  134. printk(KERN_INFO "Testing NMI watchdog ... ");
  135. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0);
  136. for_each_possible_cpu(cpu)
  137. prev_nmi_count[cpu] = get_nmi_count(cpu);
  138. local_irq_enable();
  139. mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
  140. for_each_online_cpu(cpu) {
  141. if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
  142. report_broken_nmi(cpu, prev_nmi_count);
  143. }
  144. endflag = 1;
  145. if (!nmi_usable) {
  146. kfree(prev_nmi_count);
  147. err = -ENODEV;
  148. goto error;
  149. }
  150. printk("OK.\n");
  151. nmi_hz = 1;
  152. kfree(prev_nmi_count);
  153. return 0;
  154. error:
  155. on_each_cpu(stop_watchdog, NULL, 1);
  156. return err;
  157. }
  158. static void start_watchdog(void *unused)
  159. {
  160. pcr_ops->write(PCR_PIC_PRIV);
  161. write_pic(picl_value(nmi_hz));
  162. pcr_ops->write(pcr_enable);
  163. }
  164. void nmi_adjust_hz(unsigned int new_hz)
  165. {
  166. nmi_hz = new_hz;
  167. on_each_cpu(start_watchdog, NULL, 1);
  168. }
  169. EXPORT_SYMBOL_GPL(nmi_adjust_hz);
  170. static int nmi_shutdown(struct notifier_block *nb, unsigned long cmd, void *p)
  171. {
  172. on_each_cpu(stop_watchdog, NULL, 1);
  173. return 0;
  174. }
  175. static struct notifier_block nmi_reboot_notifier = {
  176. .notifier_call = nmi_shutdown,
  177. };
  178. int __init nmi_init(void)
  179. {
  180. int err;
  181. nmi_usable = 1;
  182. on_each_cpu(start_watchdog, NULL, 1);
  183. err = check_nmi_watchdog();
  184. if (!err) {
  185. err = register_reboot_notifier(&nmi_reboot_notifier);
  186. if (err) {
  187. nmi_usable = 0;
  188. on_each_cpu(stop_watchdog, NULL, 1);
  189. }
  190. }
  191. return err;
  192. }
  193. static int __init setup_nmi_watchdog(char *str)
  194. {
  195. if (!strncmp(str, "panic", 5))
  196. panic_on_timeout = 1;
  197. return 0;
  198. }
  199. __setup("nmi_watchdog=", setup_nmi_watchdog);