mn10300-watchdog.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* MN10300 Watchdog timer
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/i386/kernel/nmi.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/nmi.h>
  20. #include <asm/processor.h>
  21. #include <linux/atomic.h>
  22. #include <asm/intctl-regs.h>
  23. #include <asm/rtc-regs.h>
  24. #include <asm/div64.h>
  25. #include <asm/smp.h>
  26. #include <asm/gdb-stub.h>
  27. #include <proc/clock.h>
  28. static DEFINE_SPINLOCK(watchdog_print_lock);
  29. static unsigned int watchdog;
  30. static unsigned int watchdog_hz = 1;
  31. unsigned int watchdog_alert_counter[NR_CPUS];
  32. EXPORT_SYMBOL(touch_nmi_watchdog);
  33. /*
  34. * the best way to detect whether a CPU has a 'hard lockup' problem
  35. * is to check its timer makes IRQ counts. If they are not
  36. * changing then that CPU has some problem.
  37. *
  38. * since NMIs dont listen to _any_ locks, we have to be extremely
  39. * careful not to rely on unsafe variables. The printk might lock
  40. * up though, so we have to break up any console locks first ...
  41. * [when there will be more tty-related locks, break them up
  42. * here too!]
  43. */
  44. static unsigned int last_irq_sums[NR_CPUS];
  45. int __init check_watchdog(void)
  46. {
  47. irq_cpustat_t tmp[1];
  48. printk(KERN_INFO "Testing Watchdog... ");
  49. memcpy(tmp, irq_stat, sizeof(tmp));
  50. local_irq_enable();
  51. mdelay((10 * 1000) / watchdog_hz); /* wait 10 ticks */
  52. local_irq_disable();
  53. if (nmi_count(0) - tmp[0].__nmi_count <= 5) {
  54. printk(KERN_WARNING "CPU#%d: Watchdog appears to be stuck!\n",
  55. 0);
  56. return -1;
  57. }
  58. printk(KERN_INFO "OK.\n");
  59. /* now that we know it works we can reduce NMI frequency to something
  60. * more reasonable; makes a difference in some configs
  61. */
  62. watchdog_hz = 1;
  63. return 0;
  64. }
  65. static int __init setup_watchdog(char *str)
  66. {
  67. unsigned tmp;
  68. int opt;
  69. u8 ctr;
  70. get_option(&str, &opt);
  71. if (opt != 1)
  72. return 0;
  73. watchdog = opt;
  74. if (watchdog) {
  75. set_intr_stub(EXCEP_WDT, watchdog_handler);
  76. ctr = WDCTR_WDCK_65536th;
  77. WDCTR = WDCTR_WDRST | ctr;
  78. WDCTR = ctr;
  79. tmp = WDCTR;
  80. tmp = __muldiv64u(1 << (16 + ctr * 2), 1000000, MN10300_WDCLK);
  81. tmp = 1000000000 / tmp;
  82. watchdog_hz = (tmp + 500) / 1000;
  83. }
  84. return 1;
  85. }
  86. __setup("watchdog=", setup_watchdog);
  87. void __init watchdog_go(void)
  88. {
  89. u8 wdt;
  90. if (watchdog) {
  91. printk(KERN_INFO "Watchdog: running at %uHz\n", watchdog_hz);
  92. wdt = WDCTR & ~WDCTR_WDCNE;
  93. WDCTR = wdt | WDCTR_WDRST;
  94. wdt = WDCTR;
  95. WDCTR = wdt | WDCTR_WDCNE;
  96. wdt = WDCTR;
  97. check_watchdog();
  98. }
  99. }
  100. #ifdef CONFIG_SMP
  101. static void watchdog_dump_register(void *dummy)
  102. {
  103. printk(KERN_ERR "--- Register Dump (CPU%d) ---\n", CPUID);
  104. show_registers(current_frame());
  105. }
  106. #endif
  107. asmlinkage
  108. void watchdog_interrupt(struct pt_regs *regs, enum exception_code excep)
  109. {
  110. /*
  111. * Since current-> is always on the stack, and we always switch
  112. * the stack NMI-atomically, it's safe to use smp_processor_id().
  113. */
  114. int sum, cpu;
  115. int irq = NMIIRQ;
  116. u8 wdt, tmp;
  117. wdt = WDCTR & ~WDCTR_WDCNE;
  118. WDCTR = wdt;
  119. tmp = WDCTR;
  120. NMICR = NMICR_WDIF;
  121. nmi_count(smp_processor_id())++;
  122. kstat_incr_irq_this_cpu(irq);
  123. for_each_online_cpu(cpu) {
  124. sum = irq_stat[cpu].__irq_count;
  125. if ((last_irq_sums[cpu] == sum)
  126. #if defined(CONFIG_GDBSTUB) && defined(CONFIG_SMP)
  127. && !(CHK_GDBSTUB_BUSY()
  128. || atomic_read(&cpu_doing_single_step))
  129. #endif
  130. ) {
  131. /*
  132. * Ayiee, looks like this CPU is stuck ...
  133. * wait a few IRQs (5 seconds) before doing the oops ...
  134. */
  135. watchdog_alert_counter[cpu]++;
  136. if (watchdog_alert_counter[cpu] == 5 * watchdog_hz) {
  137. spin_lock(&watchdog_print_lock);
  138. /*
  139. * We are in trouble anyway, lets at least try
  140. * to get a message out.
  141. */
  142. bust_spinlocks(1);
  143. printk(KERN_ERR
  144. "NMI Watchdog detected LOCKUP on CPU%d,"
  145. " pc %08lx, registers:\n",
  146. cpu, regs->pc);
  147. #ifdef CONFIG_SMP
  148. printk(KERN_ERR
  149. "--- Register Dump (CPU%d) ---\n",
  150. CPUID);
  151. #endif
  152. show_registers(regs);
  153. #ifdef CONFIG_SMP
  154. smp_nmi_call_function(watchdog_dump_register,
  155. NULL, 1);
  156. #endif
  157. printk(KERN_NOTICE "console shuts up ...\n");
  158. console_silent();
  159. spin_unlock(&watchdog_print_lock);
  160. bust_spinlocks(0);
  161. #ifdef CONFIG_GDBSTUB
  162. if (CHK_GDBSTUB_BUSY_AND_ACTIVE())
  163. gdbstub_exception(regs, excep);
  164. else
  165. gdbstub_intercept(regs, excep);
  166. #endif
  167. do_exit(SIGSEGV);
  168. }
  169. } else {
  170. last_irq_sums[cpu] = sum;
  171. watchdog_alert_counter[cpu] = 0;
  172. }
  173. }
  174. WDCTR = wdt | WDCTR_WDRST;
  175. tmp = WDCTR;
  176. WDCTR = wdt | WDCTR_WDCNE;
  177. tmp = WDCTR;
  178. }