Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Pseudo NMI support on sparc64 systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * The NMI watchdog support and infrastructure is based almost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * entirely upon the x86 NMI support code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/pcr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "kstack.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* We don't have a real NMI on sparc64, but we can fake one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * up using profiling counter overflow interrupts and interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * levels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * The profile overflow interrupts at level 15, so we use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * level 14 as our IRQ off level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static int panic_on_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* nmi_active:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * >0: the NMI watchdog is active, but can be disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * <0: the NMI watchdog has not been set up, and cannot be enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *  0: the NMI watchdog is disabled, but can be enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) atomic_t nmi_active = ATOMIC_INIT(0);		/* oprofile uses this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) EXPORT_SYMBOL(nmi_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int nmi_init_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static unsigned int nmi_hz = HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static DEFINE_PER_CPU(short, wd_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int endflag __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static DEFINE_PER_CPU(unsigned int, last_irq_sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static DEFINE_PER_CPU(long, alert_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static DEFINE_PER_CPU(int, nmi_touch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) void arch_touch_nmi_watchdog(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (atomic_read(&nmi_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		for_each_present_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			if (per_cpu(nmi_touch, cpu) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 				per_cpu(nmi_touch, cpu) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) EXPORT_SYMBOL(arch_touch_nmi_watchdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void die_nmi(const char *str, struct pt_regs *regs, int do_panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int this_cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (notify_die(DIE_NMIWATCHDOG, str, regs, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		       pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (do_panic || panic_on_oops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) notrace __kprobes void perfctr_irq(int irq, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	unsigned int sum, touched = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	void *orig_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	clear_softint(1 << irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	local_cpu_data().__nmi_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	nmi_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	orig_sp = set_hardirq_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (notify_die(DIE_NMI, "nmi", regs, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		       pt_regs_trap_type(regs), SIGINT) == NOTIFY_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	sum = local_cpu_data().irq0_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (__this_cpu_read(nmi_touch)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		__this_cpu_write(nmi_touch, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (!touched && __this_cpu_read(last_irq_sum) == sum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		__this_cpu_inc(alert_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (__this_cpu_read(alert_counter) == 30 * nmi_hz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			die_nmi("BUG: NMI Watchdog detected LOCKUP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				regs, panic_on_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		__this_cpu_write(last_irq_sum, sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		__this_cpu_write(alert_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (__this_cpu_read(wd_enabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		pcr_ops->write_pic(0, pcr_ops->nmi_picl_value(nmi_hz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	restore_hardirq_stack(orig_sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	nmi_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline unsigned int get_nmi_count(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return cpu_data(cpu).__nmi_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static __init void nmi_cpu_busy(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	while (endflag == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void report_broken_nmi(int cpu, int *prev_nmi_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		"WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			cpu, prev_nmi_count[cpu], get_nmi_count(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		"Please report this to bugzilla.kernel.org,\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		"and attach the output of the 'dmesg' command.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	per_cpu(wd_enabled, cpu) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	atomic_dec(&nmi_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void stop_nmi_watchdog(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (!__this_cpu_read(wd_enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	__this_cpu_write(wd_enabled, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	atomic_dec(&nmi_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int __init check_nmi_watchdog(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	unsigned int *prev_nmi_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int cpu, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (!atomic_read(&nmi_active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	prev_nmi_count = kmalloc_array(nr_cpu_ids, sizeof(unsigned int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (!prev_nmi_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	printk(KERN_INFO "Testing NMI watchdog ... ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	smp_call_function(nmi_cpu_busy, (void *)&endflag, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	for_each_possible_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		prev_nmi_count[cpu] = get_nmi_count(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (!per_cpu(wd_enabled, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			report_broken_nmi(cpu, prev_nmi_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	endflag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (!atomic_read(&nmi_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		kfree(prev_nmi_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		atomic_set(&nmi_active, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	printk("OK.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	nmi_hz = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	kfree(prev_nmi_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	on_each_cpu(stop_nmi_watchdog, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) void start_nmi_watchdog(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (__this_cpu_read(wd_enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	__this_cpu_write(wd_enabled, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	atomic_inc(&nmi_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	pcr_ops->write_pic(0, pcr_ops->nmi_picl_value(nmi_hz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void nmi_adjust_hz_one(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (!__this_cpu_read(wd_enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	pcr_ops->write_pic(0, pcr_ops->nmi_picl_value(nmi_hz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	pcr_ops->write_pcr(0, pcr_ops->pcr_nmi_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) void nmi_adjust_hz(unsigned int new_hz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	nmi_hz = new_hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	on_each_cpu(nmi_adjust_hz_one, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) EXPORT_SYMBOL_GPL(nmi_adjust_hz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int nmi_shutdown(struct notifier_block *nb, unsigned long cmd, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	on_each_cpu(stop_nmi_watchdog, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static struct notifier_block nmi_reboot_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	.notifier_call = nmi_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int __init nmi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	on_each_cpu(start_nmi_watchdog, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	err = check_nmi_watchdog();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		err = register_reboot_notifier(&nmi_reboot_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			on_each_cpu(stop_nmi_watchdog, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			atomic_set(&nmi_active, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	nmi_init_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static int __init setup_nmi_watchdog(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!strncmp(str, "panic", 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		panic_on_timeout = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) __setup("nmi_watchdog=", setup_nmi_watchdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * sparc specific NMI watchdog enable function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * Enables watchdog if it is not enabled already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int watchdog_nmi_enable(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (atomic_read(&nmi_active) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		pr_warn("NMI watchdog cannot be enabled or disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * watchdog thread could start even before nmi_init is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 * Just Return in that case. Let nmi_init finish the init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	 * process first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!nmi_init_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	smp_call_function_single(cpu, start_nmi_watchdog, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * sparc specific NMI watchdog disable function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * Disables watchdog if it is not disabled already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) void watchdog_nmi_disable(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (atomic_read(&nmi_active) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		pr_warn_once("NMI watchdog cannot be enabled or disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		smp_call_function_single(cpu, stop_nmi_watchdog, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }