mn10300-watchdog.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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;
  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. * as these watchdog NMI IRQs are generated on every CPU, we only
  40. * have to check the current processor.
  41. *
  42. * since NMIs dont listen to _any_ locks, we have to be extremely
  43. * careful not to rely on unsafe variables. The printk might lock
  44. * up though, so we have to break up any console locks first ...
  45. * [when there will be more tty-related locks, break them up
  46. * here too!]
  47. */
  48. static unsigned int last_irq_sums[NR_CPUS];
  49. int __init check_watchdog(void)
  50. {
  51. irq_cpustat_t tmp[1];
  52. printk(KERN_INFO "Testing Watchdog... ");
  53. memcpy(tmp, irq_stat, sizeof(tmp));
  54. local_irq_enable();
  55. mdelay((10 * 1000) / watchdog_hz); /* wait 10 ticks */
  56. local_irq_disable();
  57. if (nmi_count(0) - tmp[0].__nmi_count <= 5) {
  58. printk(KERN_WARNING "CPU#%d: Watchdog appears to be stuck!\n",
  59. 0);
  60. return -1;
  61. }
  62. printk(KERN_INFO "OK.\n");
  63. /* now that we know it works we can reduce NMI frequency to
  64. * something more reasonable; makes a difference in some configs
  65. */
  66. watchdog_hz = 1;
  67. return 0;
  68. }
  69. static int __init setup_watchdog(char *str)
  70. {
  71. unsigned tmp;
  72. int opt;
  73. u8 ctr;
  74. get_option(&str, &opt);
  75. if (opt != 1)
  76. return 0;
  77. watchdog = opt;
  78. if (watchdog) {
  79. set_intr_stub(EXCEP_WDT, watchdog_handler);
  80. ctr = WDCTR_WDCK_65536th;
  81. WDCTR = WDCTR_WDRST | ctr;
  82. WDCTR = ctr;
  83. tmp = WDCTR;
  84. tmp = __muldiv64u(1 << (16 + ctr * 2), 1000000, MN10300_WDCLK);
  85. tmp = 1000000000 / tmp;
  86. watchdog_hz = (tmp + 500) / 1000;
  87. }
  88. return 1;
  89. }
  90. __setup("watchdog=", setup_watchdog);
  91. void __init watchdog_go(void)
  92. {
  93. u8 wdt;
  94. if (watchdog) {
  95. printk(KERN_INFO "Watchdog: running at %uHz\n", watchdog_hz);
  96. wdt = WDCTR & ~WDCTR_WDCNE;
  97. WDCTR = wdt | WDCTR_WDRST;
  98. wdt = WDCTR;
  99. WDCTR = wdt | WDCTR_WDCNE;
  100. wdt = WDCTR;
  101. check_watchdog();
  102. }
  103. }
  104. asmlinkage
  105. void watchdog_interrupt(struct pt_regs *regs, enum exception_code excep)
  106. {
  107. /*
  108. * Since current-> is always on the stack, and we always switch
  109. * the stack NMI-atomically, it's safe to use smp_processor_id().
  110. */
  111. int sum, cpu = smp_processor_id();
  112. int irq = NMIIRQ;
  113. u8 wdt, tmp;
  114. wdt = WDCTR & ~WDCTR_WDCNE;
  115. WDCTR = wdt;
  116. tmp = WDCTR;
  117. NMICR = NMICR_WDIF;
  118. nmi_count(cpu)++;
  119. kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
  120. sum = irq_stat[cpu].__irq_count;
  121. if (last_irq_sums[cpu] == sum) {
  122. /*
  123. * Ayiee, looks like this CPU is stuck ...
  124. * wait a few IRQs (5 seconds) before doing the oops ...
  125. */
  126. watchdog_alert_counter++;
  127. if (watchdog_alert_counter == 5 * watchdog_hz) {
  128. spin_lock(&watchdog_print_lock);
  129. /*
  130. * We are in trouble anyway, lets at least try
  131. * to get a message out.
  132. */
  133. bust_spinlocks(1);
  134. printk(KERN_ERR
  135. "NMI Watchdog detected LOCKUP on CPU%d,"
  136. " pc %08lx, registers:\n",
  137. cpu, regs->pc);
  138. show_registers(regs);
  139. printk("console shuts up ...\n");
  140. console_silent();
  141. spin_unlock(&watchdog_print_lock);
  142. bust_spinlocks(0);
  143. #ifdef CONFIG_GDBSTUB
  144. if (gdbstub_busy)
  145. gdbstub_exception(regs, excep);
  146. else
  147. gdbstub_intercept(regs, excep);
  148. #endif
  149. do_exit(SIGSEGV);
  150. }
  151. } else {
  152. last_irq_sums[cpu] = sum;
  153. watchdog_alert_counter = 0;
  154. }
  155. WDCTR = wdt | WDCTR_WDRST;
  156. tmp = WDCTR;
  157. WDCTR = wdt | WDCTR_WDCNE;
  158. tmp = WDCTR;
  159. }