1 /* Pseudo NMI support on sparc64 systems.
3 * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
5 * The NMI watchdog support and infrastructure is based almost
6 * entirely upon the x86 NMI support code.
8 #include <linux/kernel.h>
9 #include <linux/param.h>
10 #include <linux/init.h>
11 #include <linux/percpu.h>
12 #include <linux/nmi.h>
13 #include <linux/module.h>
14 #include <linux/kprobes.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/slab.h>
17 #include <linux/kdebug.h>
18 #include <linux/delay.h>
19 #include <linux/smp.h>
21 #include <asm/ptrace.h>
22 #include <asm/local.h>
25 /* We don't have a real NMI on sparc64, but we can fake one
26 * up using profiling counter overflow interrupts and interrupt
29 * The profile overflow interrupts at level 15, so we use
30 * level 14 as our IRQ off level.
33 static int nmi_watchdog_active
;
34 static int panic_on_timeout
;
37 EXPORT_SYMBOL_GPL(nmi_usable
);
39 static unsigned int nmi_hz
= HZ
;
41 static DEFINE_PER_CPU(unsigned int, last_irq_sum
);
42 static DEFINE_PER_CPU(local_t
, alert_counter
);
43 static DEFINE_PER_CPU(int, nmi_touch
);
45 void touch_nmi_watchdog(void)
47 if (nmi_watchdog_active
) {
50 for_each_present_cpu(cpu
) {
51 if (per_cpu(nmi_touch
, cpu
) != 1)
52 per_cpu(nmi_touch
, cpu
) = 1;
56 touch_softlockup_watchdog();
58 EXPORT_SYMBOL(touch_nmi_watchdog
);
60 static void die_nmi(const char *str
, struct pt_regs
*regs
, int do_panic
)
62 if (notify_die(DIE_NMIWATCHDOG
, str
, regs
, 0,
63 pt_regs_trap_type(regs
), SIGINT
) == NOTIFY_STOP
)
69 printk(KERN_EMERG
"%s", str
);
70 printk(" on CPU%d, ip %08lx, registers:\n",
71 smp_processor_id(), regs
->tpc
);
76 if (do_panic
|| panic_on_oops
)
77 panic("Non maskable interrupt");
83 notrace __kprobes
void perfctr_irq(int irq
, struct pt_regs
*regs
)
85 unsigned int sum
, touched
= 0;
86 int cpu
= smp_processor_id();
88 clear_softint(1 << irq
);
89 pcr_ops
->write(PCR_PIC_PRIV
);
91 local_cpu_data().__nmi_count
++;
93 if (notify_die(DIE_NMI
, "nmi", regs
, 0,
94 pt_regs_trap_type(regs
), SIGINT
) == NOTIFY_STOP
)
97 sum
= kstat_cpu(cpu
).irqs
[0];
98 if (__get_cpu_var(nmi_touch
)) {
99 __get_cpu_var(nmi_touch
) = 0;
102 if (!touched
&& __get_cpu_var(last_irq_sum
) == sum
) {
103 local_inc(&__get_cpu_var(alert_counter
));
104 if (local_read(&__get_cpu_var(alert_counter
)) == 5 * nmi_hz
)
105 die_nmi("BUG: NMI Watchdog detected LOCKUP",
106 regs
, panic_on_timeout
);
108 __get_cpu_var(last_irq_sum
) = sum
;
109 local_set(&__get_cpu_var(alert_counter
), 0);
112 write_pic(picl_value(nmi_hz
));
113 pcr_ops
->write(pcr_enable
);
117 static inline unsigned int get_nmi_count(int cpu
)
119 return cpu_data(cpu
).__nmi_count
;
122 static int endflag __initdata
;
124 static __init
void nmi_cpu_busy(void *data
)
126 local_irq_enable_in_hardirq();
131 static void report_broken_nmi(int cpu
, int *prev_nmi_count
)
133 printk(KERN_CONT
"\n");
136 "WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
137 cpu
, prev_nmi_count
[cpu
], get_nmi_count(cpu
));
140 "Please report this to bugzilla.kernel.org,\n");
142 "and attach the output of the 'dmesg' command.\n");
147 static void stop_watchdog(void *unused
)
149 pcr_ops
->write(PCR_PIC_PRIV
);
152 static int __init
check_nmi_watchdog(void)
154 unsigned int *prev_nmi_count
;
157 prev_nmi_count
= kmalloc(nr_cpu_ids
* sizeof(unsigned int), GFP_KERNEL
);
158 if (!prev_nmi_count
) {
163 printk(KERN_INFO
"Testing NMI watchdog ... ");
165 smp_call_function(nmi_cpu_busy
, (void *)&endflag
, 0);
167 for_each_possible_cpu(cpu
)
168 prev_nmi_count
[cpu
] = get_nmi_count(cpu
);
170 mdelay((20 * 1000) / nmi_hz
); /* wait 20 ticks */
172 for_each_online_cpu(cpu
) {
173 if (get_nmi_count(cpu
) - prev_nmi_count
[cpu
] <= 5)
174 report_broken_nmi(cpu
, prev_nmi_count
);
178 kfree(prev_nmi_count
);
186 kfree(prev_nmi_count
);
189 on_each_cpu(stop_watchdog
, NULL
, 1);
193 static void start_watchdog(void *unused
)
195 pcr_ops
->write(PCR_PIC_PRIV
);
196 write_pic(picl_value(nmi_hz
));
198 pcr_ops
->write(pcr_enable
);
201 void nmi_adjust_hz(unsigned int new_hz
)
204 on_each_cpu(start_watchdog
, NULL
, 1);
206 EXPORT_SYMBOL_GPL(nmi_adjust_hz
);
208 int __init
nmi_init(void)
212 on_each_cpu(start_watchdog
, NULL
, 1);
214 return check_nmi_watchdog();
217 static int __init
setup_nmi_watchdog(char *str
)
219 if (!strncmp(str
, "panic", 5))
220 panic_on_timeout
= 1;
224 __setup("nmi_watchdog=", setup_nmi_watchdog
);