nmi.c 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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>
16
#include <linux/reboot.h>
17 18 19 20 21
#include <linux/slab.h>
#include <linux/kdebug.h>
#include <linux/delay.h>
#include <linux/smp.h>

22
#include <asm/perf_counter.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#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 panic_on_timeout;

37 38 39 40 41 42 43
/* nmi_active:
 * >0: the NMI watchdog is active, but can be disabled
 * <0: the NMI watchdog has not been set up, and cannot be enabled
 *  0: the NMI watchdog is disabled, but can be enabled
 */
atomic_t nmi_active = ATOMIC_INIT(0);		/* oprofile uses this */
EXPORT_SYMBOL(nmi_active);
44 45

static unsigned int nmi_hz = HZ;
46 47
static DEFINE_PER_CPU(short, wd_enabled);
static int endflag __initdata;
48 49 50 51 52 53 54

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)
{
55
	if (atomic_read(&nmi_active)) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		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);
81
	dump_stack();
82 83 84 85 86 87

	bust_spinlocks(0);

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

88
	nmi_exit();
89 90 91 92 93 94 95 96 97 98 99 100 101 102
	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++;

103 104
	nmi_enter();

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

109
	sum = kstat_irqs_cpu(0, cpu);
110 111 112 113 114 115
	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));
116
		if (local_read(&__get_cpu_var(alert_counter)) == 30 * nmi_hz)
117 118 119 120 121 122
			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);
	}
123
	if (__get_cpu_var(wd_enabled)) {
124 125 126
		write_pic(picl_value(nmi_hz));
		pcr_ops->write(pcr_enable);
	}
127 128

	nmi_exit();
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

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

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");

156 157
	per_cpu(wd_enabled, cpu) = 0;
	atomic_dec(&nmi_active);
158 159
}

160
void stop_nmi_watchdog(void *unused)
161 162
{
	pcr_ops->write(PCR_PIC_PRIV);
163 164
	__get_cpu_var(wd_enabled) = 0;
	atomic_dec(&nmi_active);
165 166 167 168 169 170 171
}

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

172 173 174
	if (!atomic_read(&nmi_active))
		return 0;

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	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) {
191 192
		if (!per_cpu(wd_enabled, cpu))
			continue;
193 194 195 196
		if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
			report_broken_nmi(cpu, prev_nmi_count);
	}
	endflag = 1;
197
	if (!atomic_read(&nmi_active)) {
198
		kfree(prev_nmi_count);
199
		atomic_set(&nmi_active, -1);
200 201 202 203 204 205 206 207 208 209
		err = -ENODEV;
		goto error;
	}
	printk("OK.\n");

	nmi_hz = 1;

	kfree(prev_nmi_count);
	return 0;
error:
210
	on_each_cpu(stop_nmi_watchdog, NULL, 1);
211 212 213
	return err;
}

214
void start_nmi_watchdog(void *unused)
215 216 217 218 219 220 221 222 223 224 225
{
	__get_cpu_var(wd_enabled) = 1;
	atomic_inc(&nmi_active);

	pcr_ops->write(PCR_PIC_PRIV);
	write_pic(picl_value(nmi_hz));

	pcr_ops->write(pcr_enable);
}

static void nmi_adjust_hz_one(void *unused)
226
{
227 228 229
	if (!__get_cpu_var(wd_enabled))
		return;

230 231 232 233 234 235 236 237 238
	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;
239
	on_each_cpu(nmi_adjust_hz_one, NULL, 1);
240 241 242
}
EXPORT_SYMBOL_GPL(nmi_adjust_hz);

243 244
static int nmi_shutdown(struct notifier_block *nb, unsigned long cmd, void *p)
{
245
	on_each_cpu(stop_nmi_watchdog, NULL, 1);
246 247 248 249 250 251 252
	return 0;
}

static struct notifier_block nmi_reboot_notifier = {
	.notifier_call = nmi_shutdown,
};

253 254
int __init nmi_init(void)
{
255 256
	int err;

257
	on_each_cpu(start_nmi_watchdog, NULL, 1);
258

259 260 261 262
	err = check_nmi_watchdog();
	if (!err) {
		err = register_reboot_notifier(&nmi_reboot_notifier);
		if (err) {
263 264
			on_each_cpu(stop_nmi_watchdog, NULL, 1);
			atomic_set(&nmi_active, -1);
265 266
		}
	}
267 268 269
	if (!err)
		init_hw_perf_counters();

270
	return err;
271 272 273 274 275 276 277 278 279 280
}

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);