nmi.c 4.7 KB

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