Path: blob/master/arch/mn10300/kernel/mn10300-watchdog.c
10817 views
/* MN10300 Watchdog timer1*2* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.3* Written by David Howells ([email protected])4* - Derived from arch/i386/kernel/nmi.c5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public Licence8* as published by the Free Software Foundation; either version9* 2 of the Licence, or (at your option) any later version.10*/11#include <linux/module.h>12#include <linux/sched.h>13#include <linux/kernel.h>14#include <linux/init.h>15#include <linux/delay.h>16#include <linux/interrupt.h>17#include <linux/kernel_stat.h>18#include <linux/nmi.h>19#include <asm/processor.h>20#include <asm/system.h>21#include <asm/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>2829static DEFINE_SPINLOCK(watchdog_print_lock);30static unsigned int watchdog;31static unsigned int watchdog_hz = 1;32unsigned int watchdog_alert_counter[NR_CPUS];3334EXPORT_SYMBOL(touch_nmi_watchdog);3536/*37* the best way to detect whether a CPU has a 'hard lockup' problem38* is to check its timer makes IRQ counts. If they are not39* changing then that CPU has some problem.40*41* since NMIs dont listen to _any_ locks, we have to be extremely42* careful not to rely on unsafe variables. The printk might lock43* up though, so we have to break up any console locks first ...44* [when there will be more tty-related locks, break them up45* here too!]46*/47static unsigned int last_irq_sums[NR_CPUS];4849int __init check_watchdog(void)50{51irq_cpustat_t tmp[1];5253printk(KERN_INFO "Testing Watchdog... ");5455memcpy(tmp, irq_stat, sizeof(tmp));56local_irq_enable();57mdelay((10 * 1000) / watchdog_hz); /* wait 10 ticks */58local_irq_disable();5960if (nmi_count(0) - tmp[0].__nmi_count <= 5) {61printk(KERN_WARNING "CPU#%d: Watchdog appears to be stuck!\n",620);63return -1;64}6566printk(KERN_INFO "OK.\n");6768/* now that we know it works we can reduce NMI frequency to something69* more reasonable; makes a difference in some configs70*/71watchdog_hz = 1;7273return 0;74}7576static int __init setup_watchdog(char *str)77{78unsigned tmp;79int opt;80u8 ctr;8182get_option(&str, &opt);83if (opt != 1)84return 0;8586watchdog = opt;87if (watchdog) {88set_intr_stub(EXCEP_WDT, watchdog_handler);89ctr = WDCTR_WDCK_65536th;90WDCTR = WDCTR_WDRST | ctr;91WDCTR = ctr;92tmp = WDCTR;9394tmp = __muldiv64u(1 << (16 + ctr * 2), 1000000, MN10300_WDCLK);95tmp = 1000000000 / tmp;96watchdog_hz = (tmp + 500) / 1000;97}9899return 1;100}101102__setup("watchdog=", setup_watchdog);103104void __init watchdog_go(void)105{106u8 wdt;107108if (watchdog) {109printk(KERN_INFO "Watchdog: running at %uHz\n", watchdog_hz);110wdt = WDCTR & ~WDCTR_WDCNE;111WDCTR = wdt | WDCTR_WDRST;112wdt = WDCTR;113WDCTR = wdt | WDCTR_WDCNE;114wdt = WDCTR;115116check_watchdog();117}118}119120#ifdef CONFIG_SMP121static void watchdog_dump_register(void *dummy)122{123printk(KERN_ERR "--- Register Dump (CPU%d) ---\n", CPUID);124show_registers(current_frame());125}126#endif127128asmlinkage129void watchdog_interrupt(struct pt_regs *regs, enum exception_code excep)130{131/*132* Since current-> is always on the stack, and we always switch133* the stack NMI-atomically, it's safe to use smp_processor_id().134*/135int sum, cpu;136int irq = NMIIRQ;137u8 wdt, tmp;138139wdt = WDCTR & ~WDCTR_WDCNE;140WDCTR = wdt;141tmp = WDCTR;142NMICR = NMICR_WDIF;143144nmi_count(smp_processor_id())++;145kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));146147for_each_online_cpu(cpu) {148149sum = irq_stat[cpu].__irq_count;150151if ((last_irq_sums[cpu] == sum)152#if defined(CONFIG_GDBSTUB) && defined(CONFIG_SMP)153&& !(CHK_GDBSTUB_BUSY()154|| atomic_read(&cpu_doing_single_step))155#endif156) {157/*158* Ayiee, looks like this CPU is stuck ...159* wait a few IRQs (5 seconds) before doing the oops ...160*/161watchdog_alert_counter[cpu]++;162if (watchdog_alert_counter[cpu] == 5 * watchdog_hz) {163spin_lock(&watchdog_print_lock);164/*165* We are in trouble anyway, lets at least try166* to get a message out.167*/168bust_spinlocks(1);169printk(KERN_ERR170"NMI Watchdog detected LOCKUP on CPU%d,"171" pc %08lx, registers:\n",172cpu, regs->pc);173#ifdef CONFIG_SMP174printk(KERN_ERR175"--- Register Dump (CPU%d) ---\n",176CPUID);177#endif178show_registers(regs);179#ifdef CONFIG_SMP180smp_nmi_call_function(watchdog_dump_register,181NULL, 1);182#endif183printk(KERN_NOTICE "console shuts up ...\n");184console_silent();185spin_unlock(&watchdog_print_lock);186bust_spinlocks(0);187#ifdef CONFIG_GDBSTUB188if (CHK_GDBSTUB_BUSY_AND_ACTIVE())189gdbstub_exception(regs, excep);190else191gdbstub_intercept(regs, excep);192#endif193do_exit(SIGSEGV);194}195} else {196last_irq_sums[cpu] = sum;197watchdog_alert_counter[cpu] = 0;198}199}200201WDCTR = wdt | WDCTR_WDRST;202tmp = WDCTR;203WDCTR = wdt | WDCTR_WDCNE;204tmp = WDCTR;205}206207208