nmi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. dump_stack();
  62. bust_spinlocks(0);
  63. if (do_panic || panic_on_oops)
  64. panic("Non maskable interrupt");
  65. local_irq_enable();
  66. do_exit(SIGBUS);
  67. }
  68. notrace __kprobes void perfctr_irq(int irq, struct pt_regs *regs)
  69. {
  70. unsigned int sum, touched = 0;
  71. int cpu = smp_processor_id();
  72. clear_softint(1 << irq);
  73. pcr_ops->write(PCR_PIC_PRIV);
  74. local_cpu_data().__nmi_count++;
  75. if (notify_die(DIE_NMI, "nmi", regs, 0,
  76. pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
  77. touched = 1;
  78. sum = kstat_irqs_cpu(0, cpu);
  79. if (__get_cpu_var(nmi_touch)) {
  80. __get_cpu_var(nmi_touch) = 0;
  81. touched = 1;
  82. }
  83. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  84. local_inc(&__get_cpu_var(alert_counter));
  85. if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
  86. die_nmi("BUG: NMI Watchdog detected LOCKUP",
  87. regs, panic_on_timeout);
  88. } else {
  89. __get_cpu_var(last_irq_sum) = sum;
  90. local_set(&__get_cpu_var(alert_counter), 0);
  91. }
  92. if (nmi_usable) {
  93. write_pic(picl_value(nmi_hz));
  94. pcr_ops->write(pcr_enable);
  95. }
  96. }
  97. static inline unsigned int get_nmi_count(int cpu)
  98. {
  99. return cpu_data(cpu).__nmi_count;
  100. }
  101. static int endflag __initdata;
  102. static __init void nmi_cpu_busy(void *data)
  103. {
  104. local_irq_enable_in_hardirq();
  105. while (endflag == 0)
  106. mb();
  107. }
  108. static void report_broken_nmi(int cpu, int *prev_nmi_count)
  109. {
  110. printk(KERN_CONT "\n");
  111. printk(KERN_WARNING
  112. "WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  113. cpu, prev_nmi_count[cpu], get_nmi_count(cpu));
  114. printk(KERN_WARNING
  115. "Please report this to bugzilla.kernel.org,\n");
  116. printk(KERN_WARNING
  117. "and attach the output of the 'dmesg' command.\n");
  118. nmi_usable = 0;
  119. }
  120. static void stop_watchdog(void *unused)
  121. {
  122. pcr_ops->write(PCR_PIC_PRIV);
  123. }
  124. static int __init check_nmi_watchdog(void)
  125. {
  126. unsigned int *prev_nmi_count;
  127. int cpu, err;
  128. prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL);
  129. if (!prev_nmi_count) {
  130. err = -ENOMEM;
  131. goto error;
  132. }
  133. printk(KERN_INFO "Testing NMI watchdog ... ");
  134. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0);
  135. for_each_possible_cpu(cpu)
  136. prev_nmi_count[cpu] = get_nmi_count(cpu);
  137. local_irq_enable();
  138. mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
  139. for_each_online_cpu(cpu) {
  140. if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
  141. report_broken_nmi(cpu, prev_nmi_count);
  142. }
  143. endflag = 1;
  144. if (!nmi_usable) {
  145. kfree(prev_nmi_count);
  146. err = -ENODEV;
  147. goto error;
  148. }
  149. printk("OK.\n");
  150. nmi_hz = 1;
  151. kfree(prev_nmi_count);
  152. return 0;
  153. error:
  154. on_each_cpu(stop_watchdog, NULL, 1);
  155. return err;
  156. }
  157. static void start_watchdog(void *unused)
  158. {
  159. pcr_ops->write(PCR_PIC_PRIV);
  160. write_pic(picl_value(nmi_hz));
  161. pcr_ops->write(pcr_enable);
  162. }
  163. void nmi_adjust_hz(unsigned int new_hz)
  164. {
  165. nmi_hz = new_hz;
  166. on_each_cpu(start_watchdog, NULL, 1);
  167. }
  168. EXPORT_SYMBOL_GPL(nmi_adjust_hz);
  169. int __init nmi_init(void)
  170. {
  171. nmi_usable = 1;
  172. on_each_cpu(start_watchdog, NULL, 1);
  173. return check_nmi_watchdog();
  174. }
  175. static int __init setup_nmi_watchdog(char *str)
  176. {
  177. if (!strncmp(str, "panic", 5))
  178. panic_on_timeout = 1;
  179. return 0;
  180. }
  181. __setup("nmi_watchdog=", setup_nmi_watchdog);