mn10300-watchdog.c 4.8 KB

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