Skip to content

Commit

Permalink
sparc64: Implement NMI watchdog on capable cpus.
Browse files Browse the repository at this point in the history
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Jan 30, 2009
1 parent c3cf5e8 commit e5553a6
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 163 deletions.
2 changes: 1 addition & 1 deletion arch/sparc/include/asm/cpudata_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
typedef struct {
/* Dcache line 1 */
unsigned int __softirq_pending; /* must be 1st, see rtrap.S */
unsigned int __pad0;
unsigned int __nmi_count;
unsigned long clock_tick; /* %tick's per second */
unsigned long __pad;
unsigned int __pad1;
Expand Down
4 changes: 1 addition & 3 deletions arch/sparc/include/asm/irq_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ extern void virt_irq_free(unsigned int virt_irq);
extern void __init init_IRQ(void);
extern void fixup_irqs(void);

extern int register_perfctr_intr(void (*handler)(struct pt_regs *));
extern void release_perfctr_intr(void (*handler)(struct pt_regs *));

static inline void set_softint(unsigned long bits)
{
__asm__ __volatile__("wr %0, 0x0, %%set_softint"
Expand Down Expand Up @@ -98,5 +95,6 @@ void __trigger_all_cpu_backtrace(void);
extern void *hardirq_stack[NR_CPUS];
extern void *softirq_stack[NR_CPUS];
#define __ARCH_HAS_DO_SOFTIRQ
#define ARCH_HAS_NMI_WATCHDOG

#endif
2 changes: 2 additions & 0 deletions arch/sparc/include/asm/kdebug_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ enum die_val {
DIE_TRAP,
DIE_TRAP_TL1,
DIE_CALL,
DIE_NMI,
DIE_NMIWATCHDOG,
};

#endif
10 changes: 10 additions & 0 deletions arch/sparc/include/asm/nmi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __NMI_H
#define __NMI_H

extern int __init nmi_init(void);
extern void perfctr_irq(int irq, struct pt_regs *regs);
extern void nmi_adjust_hz(unsigned int new_hz);

extern int nmi_usable;

#endif /* __NMI_H */
16 changes: 16 additions & 0 deletions arch/sparc/include/asm/pcr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ extern void schedule_deferred_pcr_work(void);
#define PCR_N2_SL1_SHIFT 27
#define PCR_N2_OV1 0x80000000

extern unsigned int picl_shift;

/* In order to commonize as much of the implementation as
* possible, we use PICH as our counter. Mostly this is
* to accomodate Niagara-1 which can only count insn cycles
* in PICH.
*/
static inline u64 picl_value(unsigned int nmi_hz)
{
u32 delta = local_cpu_data().clock_tick / (nmi_hz << picl_shift);

return ((u64)((0 - delta) & 0xffffffff)) << 32;
}

extern u64 pcr_enable;

#endif /* __PCR_H */
1 change: 1 addition & 0 deletions arch/sparc/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ obj-$(CONFIG_SPARC64) += hvapi.o
obj-$(CONFIG_SPARC64) += sstate.o
obj-$(CONFIG_SPARC64) += mdesc.o
obj-$(CONFIG_SPARC64) += pcr.o
obj-$(CONFIG_SPARC64) += nmi.o

# sparc32 do not use GENERIC_HARDIRQS but uses the generic devres implementation
obj-$(CONFIG_SPARC32) += devres.o
Expand Down
68 changes: 5 additions & 63 deletions arch/sparc/kernel/irq_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ int show_interrupts(struct seq_file *p, void *v)
seq_putc(p, '\n');
skip:
spin_unlock_irqrestore(&irq_desc[i].lock, flags);
} else if (i == NR_IRQS) {
seq_printf(p, "NMI: ");
for_each_online_cpu(j)
seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
seq_printf(p, " Non-maskable interrupts\n");
}
return 0;
}
Expand Down Expand Up @@ -778,69 +783,6 @@ void do_softirq(void)
local_irq_restore(flags);
}

static void unhandled_perf_irq(struct pt_regs *regs)
{
unsigned long pcr, pic;

read_pcr(pcr);
read_pic(pic);

write_pcr(0);

printk(KERN_EMERG "CPU %d: Got unexpected perf counter IRQ.\n",
smp_processor_id());
printk(KERN_EMERG "CPU %d: PCR[%016lx] PIC[%016lx]\n",
smp_processor_id(), pcr, pic);
}

/* Almost a direct copy of the powerpc PMC code. */
static DEFINE_SPINLOCK(perf_irq_lock);
static void *perf_irq_owner_caller; /* mostly for debugging */
static void (*perf_irq)(struct pt_regs *regs) = unhandled_perf_irq;

/* Invoked from level 15 PIL handler in trap table. */
void perfctr_irq(int irq, struct pt_regs *regs)
{
clear_softint(1 << irq);
perf_irq(regs);
}

int register_perfctr_intr(void (*handler)(struct pt_regs *))
{
int ret;

if (!handler)
return -EINVAL;

spin_lock(&perf_irq_lock);
if (perf_irq != unhandled_perf_irq) {
printk(KERN_WARNING "register_perfctr_intr: "
"perf IRQ busy (reserved by caller %p)\n",
perf_irq_owner_caller);
ret = -EBUSY;
goto out;
}

perf_irq_owner_caller = __builtin_return_address(0);
perf_irq = handler;

ret = 0;
out:
spin_unlock(&perf_irq_lock);

return ret;
}
EXPORT_SYMBOL_GPL(register_perfctr_intr);

void release_perfctr_intr(void (*handler)(struct pt_regs *))
{
spin_lock(&perf_irq_lock);
perf_irq_owner_caller = NULL;
perf_irq = unhandled_perf_irq;
spin_unlock(&perf_irq_lock);
}
EXPORT_SYMBOL_GPL(release_perfctr_intr);

#ifdef CONFIG_HOTPLUG_CPU
void fixup_irqs(void)
{
Expand Down
224 changes: 224 additions & 0 deletions arch/sparc/kernel/nmi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/* Pseudo NMI support on sparc64 systems.
*
* Copyright (C) 2009 David S. Miller <davem@davemloft.net>
*
* The NMI watchdog support and infrastructure is based almost
* entirely upon the x86 NMI support code.
*/
#include <linux/kernel.h>
#include <linux/param.h>
#include <linux/init.h>
#include <linux/percpu.h>
#include <linux/nmi.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/kernel_stat.h>
#include <linux/slab.h>
#include <linux/kdebug.h>
#include <linux/delay.h>
#include <linux/smp.h>

#include <asm/ptrace.h>
#include <asm/local.h>
#include <asm/pcr.h>

/* We don't have a real NMI on sparc64, but we can fake one
* up using profiling counter overflow interrupts and interrupt
* levels.
*
* The profile overflow interrupts at level 15, so we use
* level 14 as our IRQ off level.
*/

static int nmi_watchdog_active;
static int panic_on_timeout;

int nmi_usable;
EXPORT_SYMBOL_GPL(nmi_usable);

static unsigned int nmi_hz = HZ;

static DEFINE_PER_CPU(unsigned int, last_irq_sum);
static DEFINE_PER_CPU(local_t, alert_counter);
static DEFINE_PER_CPU(int, nmi_touch);

void touch_nmi_watchdog(void)
{
if (nmi_watchdog_active) {
int cpu;

for_each_present_cpu(cpu) {
if (per_cpu(nmi_touch, cpu) != 1)
per_cpu(nmi_touch, cpu) = 1;
}
}

touch_softlockup_watchdog();
}
EXPORT_SYMBOL(touch_nmi_watchdog);

static void die_nmi(const char *str, struct pt_regs *regs, int do_panic)
{
if (notify_die(DIE_NMIWATCHDOG, str, regs, 0,
pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
return;

console_verbose();
bust_spinlocks(1);

printk(KERN_EMERG "%s", str);
printk(" on CPU%d, ip %08lx, registers:\n",
smp_processor_id(), regs->tpc);
show_regs(regs);

bust_spinlocks(0);

if (do_panic || panic_on_oops)
panic("Non maskable interrupt");

local_irq_enable();
do_exit(SIGBUS);
}

notrace __kprobes void perfctr_irq(int irq, struct pt_regs *regs)
{
unsigned int sum, touched = 0;
int cpu = smp_processor_id();

clear_softint(1 << irq);
pcr_ops->write(PCR_PIC_PRIV);

local_cpu_data().__nmi_count++;

if (notify_die(DIE_NMI, "nmi", regs, 0,
pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
touched = 1;

sum = kstat_cpu(cpu).irqs[0];
if (__get_cpu_var(nmi_touch)) {
__get_cpu_var(nmi_touch) = 0;
touched = 1;
}
if (!touched && __get_cpu_var(last_irq_sum) == sum) {
local_inc(&__get_cpu_var(alert_counter));
if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
die_nmi("BUG: NMI Watchdog detected LOCKUP",
regs, panic_on_timeout);
} else {
__get_cpu_var(last_irq_sum) = sum;
local_set(&__get_cpu_var(alert_counter), 0);
}
if (nmi_usable) {
write_pic(picl_value(nmi_hz));
pcr_ops->write(pcr_enable);
}
}

static inline unsigned int get_nmi_count(int cpu)
{
return cpu_data(cpu).__nmi_count;
}

static int endflag __initdata;

static __init void nmi_cpu_busy(void *data)
{
local_irq_enable_in_hardirq();
while (endflag == 0)
mb();
}

static void report_broken_nmi(int cpu, int *prev_nmi_count)
{
printk(KERN_CONT "\n");

printk(KERN_WARNING
"WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
cpu, prev_nmi_count[cpu], get_nmi_count(cpu));

printk(KERN_WARNING
"Please report this to bugzilla.kernel.org,\n");
printk(KERN_WARNING
"and attach the output of the 'dmesg' command.\n");

nmi_usable = 0;
}

static void stop_watchdog(void *unused)
{
pcr_ops->write(PCR_PIC_PRIV);
}

static int __init check_nmi_watchdog(void)
{
unsigned int *prev_nmi_count;
int cpu, err;

prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(unsigned int), GFP_KERNEL);
if (!prev_nmi_count) {
err = -ENOMEM;
goto error;
}

printk(KERN_INFO "Testing NMI watchdog ... ");

smp_call_function(nmi_cpu_busy, (void *)&endflag, 0);

for_each_possible_cpu(cpu)
prev_nmi_count[cpu] = get_nmi_count(cpu);
local_irq_enable();
mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */

for_each_online_cpu(cpu) {
if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
report_broken_nmi(cpu, prev_nmi_count);
}
endflag = 1;
if (!nmi_usable) {
kfree(prev_nmi_count);
err = -ENODEV;
goto error;
}
printk("OK.\n");

nmi_hz = 1;

kfree(prev_nmi_count);
return 0;
error:
on_each_cpu(stop_watchdog, NULL, 1);
return err;
}

static void start_watchdog(void *unused)
{
pcr_ops->write(PCR_PIC_PRIV);
write_pic(picl_value(nmi_hz));

pcr_ops->write(pcr_enable);
}

void nmi_adjust_hz(unsigned int new_hz)
{
nmi_hz = new_hz;
on_each_cpu(start_watchdog, NULL, 1);
}
EXPORT_SYMBOL_GPL(nmi_adjust_hz);

int __init nmi_init(void)
{
nmi_usable = 1;

on_each_cpu(start_watchdog, NULL, 1);

return check_nmi_watchdog();
}

static int __init setup_nmi_watchdog(char *str)
{
if (!strncmp(str, "panic", 5))
panic_on_timeout = 1;

return 0;
}
__setup("nmi_watchdog=", setup_nmi_watchdog);
Loading

0 comments on commit e5553a6

Please sign in to comment.