1e5553a6dSDavid S. Miller /* Pseudo NMI support on sparc64 systems. 2e5553a6dSDavid S. Miller * 3e5553a6dSDavid S. Miller * Copyright (C) 2009 David S. Miller <davem@davemloft.net> 4e5553a6dSDavid S. Miller * 5e5553a6dSDavid S. Miller * The NMI watchdog support and infrastructure is based almost 6e5553a6dSDavid S. Miller * entirely upon the x86 NMI support code. 7e5553a6dSDavid S. Miller */ 8e5553a6dSDavid S. Miller #include <linux/kernel.h> 9e5553a6dSDavid S. Miller #include <linux/param.h> 10e5553a6dSDavid S. Miller #include <linux/init.h> 11e5553a6dSDavid S. Miller #include <linux/percpu.h> 12e5553a6dSDavid S. Miller #include <linux/nmi.h> 13e5553a6dSDavid S. Miller #include <linux/module.h> 14e5553a6dSDavid S. Miller #include <linux/kprobes.h> 15e5553a6dSDavid S. Miller #include <linux/kernel_stat.h> 16ffaba674SDavid S. Miller #include <linux/reboot.h> 17e5553a6dSDavid S. Miller #include <linux/slab.h> 18e5553a6dSDavid S. Miller #include <linux/kdebug.h> 19e5553a6dSDavid S. Miller #include <linux/delay.h> 20e5553a6dSDavid S. Miller #include <linux/smp.h> 21e5553a6dSDavid S. Miller 22*cdd6c482SIngo Molnar #include <asm/perf_event.h> 23e5553a6dSDavid S. Miller #include <asm/ptrace.h> 24e5553a6dSDavid S. Miller #include <asm/local.h> 25e5553a6dSDavid S. Miller #include <asm/pcr.h> 26e5553a6dSDavid S. Miller 27e5553a6dSDavid S. Miller /* We don't have a real NMI on sparc64, but we can fake one 28e5553a6dSDavid S. Miller * up using profiling counter overflow interrupts and interrupt 29e5553a6dSDavid S. Miller * levels. 30e5553a6dSDavid S. Miller * 31e5553a6dSDavid S. Miller * The profile overflow interrupts at level 15, so we use 32e5553a6dSDavid S. Miller * level 14 as our IRQ off level. 33e5553a6dSDavid S. Miller */ 34e5553a6dSDavid S. Miller 35e5553a6dSDavid S. Miller static int panic_on_timeout; 36e5553a6dSDavid S. Miller 37a8f22264SDavid S. Miller /* nmi_active: 38a8f22264SDavid S. Miller * >0: the NMI watchdog is active, but can be disabled 39a8f22264SDavid S. Miller * <0: the NMI watchdog has not been set up, and cannot be enabled 40a8f22264SDavid S. Miller * 0: the NMI watchdog is disabled, but can be enabled 41a8f22264SDavid S. Miller */ 42a8f22264SDavid S. Miller atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */ 43a8f22264SDavid S. Miller EXPORT_SYMBOL(nmi_active); 44e5553a6dSDavid S. Miller 45e5553a6dSDavid S. Miller static unsigned int nmi_hz = HZ; 46a8f22264SDavid S. Miller static DEFINE_PER_CPU(short, wd_enabled); 47a8f22264SDavid S. Miller static int endflag __initdata; 48e5553a6dSDavid S. Miller 49e5553a6dSDavid S. Miller static DEFINE_PER_CPU(unsigned int, last_irq_sum); 50e5553a6dSDavid S. Miller static DEFINE_PER_CPU(local_t, alert_counter); 51e5553a6dSDavid S. Miller static DEFINE_PER_CPU(int, nmi_touch); 52e5553a6dSDavid S. Miller 53e5553a6dSDavid S. Miller void touch_nmi_watchdog(void) 54e5553a6dSDavid S. Miller { 55d89be56bSDavid S. Miller if (atomic_read(&nmi_active)) { 56e5553a6dSDavid S. Miller int cpu; 57e5553a6dSDavid S. Miller 58e5553a6dSDavid S. Miller for_each_present_cpu(cpu) { 59e5553a6dSDavid S. Miller if (per_cpu(nmi_touch, cpu) != 1) 60e5553a6dSDavid S. Miller per_cpu(nmi_touch, cpu) = 1; 61e5553a6dSDavid S. Miller } 62e5553a6dSDavid S. Miller } 63e5553a6dSDavid S. Miller 64e5553a6dSDavid S. Miller touch_softlockup_watchdog(); 65e5553a6dSDavid S. Miller } 66e5553a6dSDavid S. Miller EXPORT_SYMBOL(touch_nmi_watchdog); 67e5553a6dSDavid S. Miller 68e5553a6dSDavid S. Miller static void die_nmi(const char *str, struct pt_regs *regs, int do_panic) 69e5553a6dSDavid S. Miller { 70e5553a6dSDavid S. Miller if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 71e5553a6dSDavid S. Miller pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP) 72e5553a6dSDavid S. Miller return; 73e5553a6dSDavid S. Miller 74e5553a6dSDavid S. Miller console_verbose(); 75e5553a6dSDavid S. Miller bust_spinlocks(1); 76e5553a6dSDavid S. Miller 77e5553a6dSDavid S. Miller printk(KERN_EMERG "%s", str); 78e5553a6dSDavid S. Miller printk(" on CPU%d, ip %08lx, registers:\n", 79e5553a6dSDavid S. Miller smp_processor_id(), regs->tpc); 80e5553a6dSDavid S. Miller show_regs(regs); 81dc4ff585SDavid S. Miller dump_stack(); 82e5553a6dSDavid S. Miller 83e5553a6dSDavid S. Miller bust_spinlocks(0); 84e5553a6dSDavid S. Miller 85e5553a6dSDavid S. Miller if (do_panic || panic_on_oops) 86e5553a6dSDavid S. Miller panic("Non maskable interrupt"); 87e5553a6dSDavid S. Miller 882d0740c4SDavid S. Miller nmi_exit(); 89e5553a6dSDavid S. Miller local_irq_enable(); 90e5553a6dSDavid S. Miller do_exit(SIGBUS); 91e5553a6dSDavid S. Miller } 92e5553a6dSDavid S. Miller 93e5553a6dSDavid S. Miller notrace __kprobes void perfctr_irq(int irq, struct pt_regs *regs) 94e5553a6dSDavid S. Miller { 95e5553a6dSDavid S. Miller unsigned int sum, touched = 0; 96e5553a6dSDavid S. Miller int cpu = smp_processor_id(); 97e5553a6dSDavid S. Miller 98e5553a6dSDavid S. Miller clear_softint(1 << irq); 99e5553a6dSDavid S. Miller pcr_ops->write(PCR_PIC_PRIV); 100e5553a6dSDavid S. Miller 101e5553a6dSDavid S. Miller local_cpu_data().__nmi_count++; 102e5553a6dSDavid S. Miller 1032d0740c4SDavid S. Miller nmi_enter(); 1042d0740c4SDavid S. Miller 105e5553a6dSDavid S. Miller if (notify_die(DIE_NMI, "nmi", regs, 0, 106e5553a6dSDavid S. Miller pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP) 107e5553a6dSDavid S. Miller touched = 1; 108e5553a6dSDavid S. Miller 10947a4a0e7SStephen Rothwell sum = kstat_irqs_cpu(0, cpu); 110e5553a6dSDavid S. Miller if (__get_cpu_var(nmi_touch)) { 111e5553a6dSDavid S. Miller __get_cpu_var(nmi_touch) = 0; 112e5553a6dSDavid S. Miller touched = 1; 113e5553a6dSDavid S. Miller } 114e5553a6dSDavid S. Miller if (!touched && __get_cpu_var(last_irq_sum) == sum) { 115e5553a6dSDavid S. Miller local_inc(&__get_cpu_var(alert_counter)); 116e6617c6eSDavid S. Miller if (local_read(&__get_cpu_var(alert_counter)) == 30 * nmi_hz) 117e5553a6dSDavid S. Miller die_nmi("BUG: NMI Watchdog detected LOCKUP", 118e5553a6dSDavid S. Miller regs, panic_on_timeout); 119e5553a6dSDavid S. Miller } else { 120e5553a6dSDavid S. Miller __get_cpu_var(last_irq_sum) = sum; 121e5553a6dSDavid S. Miller local_set(&__get_cpu_var(alert_counter), 0); 122e5553a6dSDavid S. Miller } 123a8f22264SDavid S. Miller if (__get_cpu_var(wd_enabled)) { 124e5553a6dSDavid S. Miller write_pic(picl_value(nmi_hz)); 125e5553a6dSDavid S. Miller pcr_ops->write(pcr_enable); 126e5553a6dSDavid S. Miller } 1272d0740c4SDavid S. Miller 1282d0740c4SDavid S. Miller nmi_exit(); 129e5553a6dSDavid S. Miller } 130e5553a6dSDavid S. Miller 131e5553a6dSDavid S. Miller static inline unsigned int get_nmi_count(int cpu) 132e5553a6dSDavid S. Miller { 133e5553a6dSDavid S. Miller return cpu_data(cpu).__nmi_count; 134e5553a6dSDavid S. Miller } 135e5553a6dSDavid S. Miller 136e5553a6dSDavid S. Miller static __init void nmi_cpu_busy(void *data) 137e5553a6dSDavid S. Miller { 138e5553a6dSDavid S. Miller local_irq_enable_in_hardirq(); 139e5553a6dSDavid S. Miller while (endflag == 0) 140e5553a6dSDavid S. Miller mb(); 141e5553a6dSDavid S. Miller } 142e5553a6dSDavid S. Miller 143e5553a6dSDavid S. Miller static void report_broken_nmi(int cpu, int *prev_nmi_count) 144e5553a6dSDavid S. Miller { 145e5553a6dSDavid S. Miller printk(KERN_CONT "\n"); 146e5553a6dSDavid S. Miller 147e5553a6dSDavid S. Miller printk(KERN_WARNING 148e5553a6dSDavid S. Miller "WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n", 149e5553a6dSDavid S. Miller cpu, prev_nmi_count[cpu], get_nmi_count(cpu)); 150e5553a6dSDavid S. Miller 151e5553a6dSDavid S. Miller printk(KERN_WARNING 152e5553a6dSDavid S. Miller "Please report this to bugzilla.kernel.org,\n"); 153e5553a6dSDavid S. Miller printk(KERN_WARNING 154e5553a6dSDavid S. Miller "and attach the output of the 'dmesg' command.\n"); 155e5553a6dSDavid S. Miller 156a8f22264SDavid S. Miller per_cpu(wd_enabled, cpu) = 0; 157a8f22264SDavid S. Miller atomic_dec(&nmi_active); 158e5553a6dSDavid S. Miller } 159e5553a6dSDavid S. Miller 16059abbd1eSDavid S. Miller void stop_nmi_watchdog(void *unused) 161e5553a6dSDavid S. Miller { 162e5553a6dSDavid S. Miller pcr_ops->write(PCR_PIC_PRIV); 163a8f22264SDavid S. Miller __get_cpu_var(wd_enabled) = 0; 164a8f22264SDavid S. Miller atomic_dec(&nmi_active); 165e5553a6dSDavid S. Miller } 166e5553a6dSDavid S. Miller 167e5553a6dSDavid S. Miller static int __init check_nmi_watchdog(void) 168e5553a6dSDavid S. Miller { 169e5553a6dSDavid S. Miller unsigned int *prev_nmi_count; 170e5553a6dSDavid S. Miller int cpu, err; 171e5553a6dSDavid S. Miller 172a8f22264SDavid S. Miller if (!atomic_read(&nmi_active)) 173a8f22264SDavid S. Miller return 0; 174a8f22264SDavid S. Miller 175e5553a6dSDavid S. Miller prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL); 176e5553a6dSDavid S. Miller if (!prev_nmi_count) { 177e5553a6dSDavid S. Miller err = -ENOMEM; 178e5553a6dSDavid S. Miller goto error; 179e5553a6dSDavid S. Miller } 180e5553a6dSDavid S. Miller 181e5553a6dSDavid S. Miller printk(KERN_INFO "Testing NMI watchdog ... "); 182e5553a6dSDavid S. Miller 183e5553a6dSDavid S. Miller smp_call_function(nmi_cpu_busy, (void *)&endflag, 0); 184e5553a6dSDavid S. Miller 185e5553a6dSDavid S. Miller for_each_possible_cpu(cpu) 186e5553a6dSDavid S. Miller prev_nmi_count[cpu] = get_nmi_count(cpu); 187e5553a6dSDavid S. Miller local_irq_enable(); 188e5553a6dSDavid S. Miller mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */ 189e5553a6dSDavid S. Miller 190e5553a6dSDavid S. Miller for_each_online_cpu(cpu) { 191a8f22264SDavid S. Miller if (!per_cpu(wd_enabled, cpu)) 192a8f22264SDavid S. Miller continue; 193e5553a6dSDavid S. Miller if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5) 194e5553a6dSDavid S. Miller report_broken_nmi(cpu, prev_nmi_count); 195e5553a6dSDavid S. Miller } 196e5553a6dSDavid S. Miller endflag = 1; 197a8f22264SDavid S. Miller if (!atomic_read(&nmi_active)) { 198e5553a6dSDavid S. Miller kfree(prev_nmi_count); 199a8f22264SDavid S. Miller atomic_set(&nmi_active, -1); 200e5553a6dSDavid S. Miller err = -ENODEV; 201e5553a6dSDavid S. Miller goto error; 202e5553a6dSDavid S. Miller } 203e5553a6dSDavid S. Miller printk("OK.\n"); 204e5553a6dSDavid S. Miller 205e5553a6dSDavid S. Miller nmi_hz = 1; 206e5553a6dSDavid S. Miller 207e5553a6dSDavid S. Miller kfree(prev_nmi_count); 208e5553a6dSDavid S. Miller return 0; 209e5553a6dSDavid S. Miller error: 210a8f22264SDavid S. Miller on_each_cpu(stop_nmi_watchdog, NULL, 1); 211e5553a6dSDavid S. Miller return err; 212e5553a6dSDavid S. Miller } 213e5553a6dSDavid S. Miller 21459abbd1eSDavid S. Miller void start_nmi_watchdog(void *unused) 215e5553a6dSDavid S. Miller { 216a8f22264SDavid S. Miller __get_cpu_var(wd_enabled) = 1; 217a8f22264SDavid S. Miller atomic_inc(&nmi_active); 218a8f22264SDavid S. Miller 219a8f22264SDavid S. Miller pcr_ops->write(PCR_PIC_PRIV); 220a8f22264SDavid S. Miller write_pic(picl_value(nmi_hz)); 221a8f22264SDavid S. Miller 222a8f22264SDavid S. Miller pcr_ops->write(pcr_enable); 223a8f22264SDavid S. Miller } 224a8f22264SDavid S. Miller 225a8f22264SDavid S. Miller static void nmi_adjust_hz_one(void *unused) 226a8f22264SDavid S. Miller { 227a8f22264SDavid S. Miller if (!__get_cpu_var(wd_enabled)) 228a8f22264SDavid S. Miller return; 229a8f22264SDavid S. Miller 230e5553a6dSDavid S. Miller pcr_ops->write(PCR_PIC_PRIV); 231e5553a6dSDavid S. Miller write_pic(picl_value(nmi_hz)); 232e5553a6dSDavid S. Miller 233e5553a6dSDavid S. Miller pcr_ops->write(pcr_enable); 234e5553a6dSDavid S. Miller } 235e5553a6dSDavid S. Miller 236e5553a6dSDavid S. Miller void nmi_adjust_hz(unsigned int new_hz) 237e5553a6dSDavid S. Miller { 238e5553a6dSDavid S. Miller nmi_hz = new_hz; 239a8f22264SDavid S. Miller on_each_cpu(nmi_adjust_hz_one, NULL, 1); 240e5553a6dSDavid S. Miller } 241e5553a6dSDavid S. Miller EXPORT_SYMBOL_GPL(nmi_adjust_hz); 242e5553a6dSDavid S. Miller 243ffaba674SDavid S. Miller static int nmi_shutdown(struct notifier_block *nb, unsigned long cmd, void *p) 244ffaba674SDavid S. Miller { 245a8f22264SDavid S. Miller on_each_cpu(stop_nmi_watchdog, NULL, 1); 246ffaba674SDavid S. Miller return 0; 247ffaba674SDavid S. Miller } 248ffaba674SDavid S. Miller 249ffaba674SDavid S. Miller static struct notifier_block nmi_reboot_notifier = { 250ffaba674SDavid S. Miller .notifier_call = nmi_shutdown, 251ffaba674SDavid S. Miller }; 252ffaba674SDavid S. Miller 253e5553a6dSDavid S. Miller int __init nmi_init(void) 254e5553a6dSDavid S. Miller { 255ffaba674SDavid S. Miller int err; 256ffaba674SDavid S. Miller 257a8f22264SDavid S. Miller on_each_cpu(start_nmi_watchdog, NULL, 1); 258e5553a6dSDavid S. Miller 259ffaba674SDavid S. Miller err = check_nmi_watchdog(); 260ffaba674SDavid S. Miller if (!err) { 261ffaba674SDavid S. Miller err = register_reboot_notifier(&nmi_reboot_notifier); 262ffaba674SDavid S. Miller if (err) { 263a8f22264SDavid S. Miller on_each_cpu(stop_nmi_watchdog, NULL, 1); 264a8f22264SDavid S. Miller atomic_set(&nmi_active, -1); 265ffaba674SDavid S. Miller } 266ffaba674SDavid S. Miller } 26759abbd1eSDavid S. Miller if (!err) 268*cdd6c482SIngo Molnar init_hw_perf_events(); 26959abbd1eSDavid S. Miller 270ffaba674SDavid S. Miller return err; 271e5553a6dSDavid S. Miller } 272e5553a6dSDavid S. Miller 273e5553a6dSDavid S. Miller static int __init setup_nmi_watchdog(char *str) 274e5553a6dSDavid S. Miller { 275e5553a6dSDavid S. Miller if (!strncmp(str, "panic", 5)) 276e5553a6dSDavid S. Miller panic_on_timeout = 1; 277e5553a6dSDavid S. Miller 278e5553a6dSDavid S. Miller return 0; 279e5553a6dSDavid S. Miller } 280e5553a6dSDavid S. Miller __setup("nmi_watchdog=", setup_nmi_watchdog); 281