18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	Bus error event handling code for systems equipped with ECC
48c2ecf20Sopenharmony_ci *	handling logic, i.e. DECstation/DECsystem 5000/200 (KN02),
58c2ecf20Sopenharmony_ci *	5000/240 (KN03), 5000/260 (KN05) and DECsystem 5900 (KN03),
68c2ecf20Sopenharmony_ci *	5900/260 (KN05) systems.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *	Copyright (c) 2003, 2005  Maciej W. Rozycki
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/sched.h>
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/addrspace.h>
188c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
198c2ecf20Sopenharmony_ci#include <asm/cpu.h>
208c2ecf20Sopenharmony_ci#include <asm/cpu-type.h>
218c2ecf20Sopenharmony_ci#include <asm/irq_regs.h>
228c2ecf20Sopenharmony_ci#include <asm/processor.h>
238c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
248c2ecf20Sopenharmony_ci#include <asm/traps.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <asm/dec/ecc.h>
278c2ecf20Sopenharmony_ci#include <asm/dec/kn02.h>
288c2ecf20Sopenharmony_ci#include <asm/dec/kn03.h>
298c2ecf20Sopenharmony_ci#include <asm/dec/kn05.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic volatile u32 *kn0x_erraddr;
328c2ecf20Sopenharmony_cistatic volatile u32 *kn0x_chksyn;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic inline void dec_ecc_be_ack(void)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	*kn0x_erraddr = 0;			/* any write clears the IRQ */
378c2ecf20Sopenharmony_ci	iob();
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int dec_ecc_be_backend(struct pt_regs *regs, int is_fixup, int invoker)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	static const char excstr[] = "exception";
438c2ecf20Sopenharmony_ci	static const char intstr[] = "interrupt";
448c2ecf20Sopenharmony_ci	static const char cpustr[] = "CPU";
458c2ecf20Sopenharmony_ci	static const char dmastr[] = "DMA";
468c2ecf20Sopenharmony_ci	static const char readstr[] = "read";
478c2ecf20Sopenharmony_ci	static const char mreadstr[] = "memory read";
488c2ecf20Sopenharmony_ci	static const char writestr[] = "write";
498c2ecf20Sopenharmony_ci	static const char mwritstr[] = "partial memory write";
508c2ecf20Sopenharmony_ci	static const char timestr[] = "timeout";
518c2ecf20Sopenharmony_ci	static const char overstr[] = "overrun";
528c2ecf20Sopenharmony_ci	static const char eccstr[] = "ECC error";
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	const char *kind, *agent, *cycle, *event;
558c2ecf20Sopenharmony_ci	const char *status = "", *xbit = "", *fmt = "";
568c2ecf20Sopenharmony_ci	unsigned long address;
578c2ecf20Sopenharmony_ci	u16 syn = 0, sngl;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	int i = 0;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	u32 erraddr = *kn0x_erraddr;
628c2ecf20Sopenharmony_ci	u32 chksyn = *kn0x_chksyn;
638c2ecf20Sopenharmony_ci	int action = MIPS_BE_FATAL;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/* For non-ECC ack ASAP, so that any subsequent errors get caught. */
668c2ecf20Sopenharmony_ci	if ((erraddr & (KN0X_EAR_VALID | KN0X_EAR_ECCERR)) == KN0X_EAR_VALID)
678c2ecf20Sopenharmony_ci		dec_ecc_be_ack();
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	kind = invoker ? intstr : excstr;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (!(erraddr & KN0X_EAR_VALID)) {
728c2ecf20Sopenharmony_ci		/* No idea what happened. */
738c2ecf20Sopenharmony_ci		printk(KERN_ALERT "Unidentified bus error %s\n", kind);
748c2ecf20Sopenharmony_ci		return action;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	agent = (erraddr & KN0X_EAR_CPU) ? cpustr : dmastr;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (erraddr & KN0X_EAR_ECCERR) {
808c2ecf20Sopenharmony_ci		/* An ECC error on a CPU or DMA transaction. */
818c2ecf20Sopenharmony_ci		cycle = (erraddr & KN0X_EAR_WRITE) ? mwritstr : mreadstr;
828c2ecf20Sopenharmony_ci		event = eccstr;
838c2ecf20Sopenharmony_ci	} else {
848c2ecf20Sopenharmony_ci		/* A CPU timeout or a DMA overrun. */
858c2ecf20Sopenharmony_ci		cycle = (erraddr & KN0X_EAR_WRITE) ? writestr : readstr;
868c2ecf20Sopenharmony_ci		event = (erraddr & KN0X_EAR_CPU) ? timestr : overstr;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	address = erraddr & KN0X_EAR_ADDRESS;
908c2ecf20Sopenharmony_ci	/* For ECC errors on reads adjust for MT pipelining. */
918c2ecf20Sopenharmony_ci	if ((erraddr & (KN0X_EAR_WRITE | KN0X_EAR_ECCERR)) == KN0X_EAR_ECCERR)
928c2ecf20Sopenharmony_ci		address = (address & ~0xfffLL) | ((address - 5) & 0xfffLL);
938c2ecf20Sopenharmony_ci	address <<= 2;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* Only CPU errors are fixable. */
968c2ecf20Sopenharmony_ci	if (erraddr & KN0X_EAR_CPU && is_fixup)
978c2ecf20Sopenharmony_ci		action = MIPS_BE_FIXUP;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (erraddr & KN0X_EAR_ECCERR) {
1008c2ecf20Sopenharmony_ci		static const u8 data_sbit[32] = {
1018c2ecf20Sopenharmony_ci			0x4f, 0x4a, 0x52, 0x54, 0x57, 0x58, 0x5b, 0x5d,
1028c2ecf20Sopenharmony_ci			0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x31, 0x34,
1038c2ecf20Sopenharmony_ci			0x0e, 0x0b, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c,
1048c2ecf20Sopenharmony_ci			0x62, 0x64, 0x67, 0x68, 0x6b, 0x6d, 0x70, 0x75,
1058c2ecf20Sopenharmony_ci		};
1068c2ecf20Sopenharmony_ci		static const u8 data_mbit[25] = {
1078c2ecf20Sopenharmony_ci			0x07, 0x0d, 0x1f,
1088c2ecf20Sopenharmony_ci			0x2f, 0x32, 0x37, 0x38, 0x3b, 0x3d, 0x3e,
1098c2ecf20Sopenharmony_ci			0x43, 0x45, 0x46, 0x49, 0x4c, 0x51, 0x5e,
1108c2ecf20Sopenharmony_ci			0x61, 0x6e, 0x73, 0x76, 0x79, 0x7a, 0x7c, 0x7f,
1118c2ecf20Sopenharmony_ci		};
1128c2ecf20Sopenharmony_ci		static const char sbestr[] = "corrected single";
1138c2ecf20Sopenharmony_ci		static const char dbestr[] = "uncorrectable double";
1148c2ecf20Sopenharmony_ci		static const char mbestr[] = "uncorrectable multiple";
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		if (!(address & 0x4))
1178c2ecf20Sopenharmony_ci			syn = chksyn;			/* Low bank. */
1188c2ecf20Sopenharmony_ci		else
1198c2ecf20Sopenharmony_ci			syn = chksyn >> 16;		/* High bank. */
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci		if (!(syn & KN0X_ESR_VLDLO)) {
1228c2ecf20Sopenharmony_ci			/* Ack now, no rewrite will happen. */
1238c2ecf20Sopenharmony_ci			dec_ecc_be_ack();
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			fmt = KERN_ALERT "%s" "invalid\n";
1268c2ecf20Sopenharmony_ci		} else {
1278c2ecf20Sopenharmony_ci			sngl = syn & KN0X_ESR_SNGLO;
1288c2ecf20Sopenharmony_ci			syn &= KN0X_ESR_SYNLO;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci			/*
1318c2ecf20Sopenharmony_ci			 * Multibit errors may be tagged incorrectly;
1328c2ecf20Sopenharmony_ci			 * check the syndrome explicitly.
1338c2ecf20Sopenharmony_ci			 */
1348c2ecf20Sopenharmony_ci			for (i = 0; i < 25; i++)
1358c2ecf20Sopenharmony_ci				if (syn == data_mbit[i])
1368c2ecf20Sopenharmony_ci					break;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci			if (i < 25) {
1398c2ecf20Sopenharmony_ci				status = mbestr;
1408c2ecf20Sopenharmony_ci			} else if (!sngl) {
1418c2ecf20Sopenharmony_ci				status = dbestr;
1428c2ecf20Sopenharmony_ci			} else {
1438c2ecf20Sopenharmony_ci				volatile u32 *ptr =
1448c2ecf20Sopenharmony_ci					(void *)CKSEG1ADDR(address);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci				*ptr = *ptr;		/* Rewrite. */
1478c2ecf20Sopenharmony_ci				iob();
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci				status = sbestr;
1508c2ecf20Sopenharmony_ci				action = MIPS_BE_DISCARD;
1518c2ecf20Sopenharmony_ci			}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci			/* Ack now, now we've rewritten (or not). */
1548c2ecf20Sopenharmony_ci			dec_ecc_be_ack();
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci			if (syn && syn == (syn & -syn)) {
1578c2ecf20Sopenharmony_ci				if (syn == 0x01) {
1588c2ecf20Sopenharmony_ci					fmt = KERN_ALERT "%s"
1598c2ecf20Sopenharmony_ci					      "%#04x -- %s bit error "
1608c2ecf20Sopenharmony_ci					      "at check bit C%s\n";
1618c2ecf20Sopenharmony_ci					xbit = "X";
1628c2ecf20Sopenharmony_ci				} else {
1638c2ecf20Sopenharmony_ci					fmt = KERN_ALERT "%s"
1648c2ecf20Sopenharmony_ci					      "%#04x -- %s bit error "
1658c2ecf20Sopenharmony_ci					      "at check bit C%s%u\n";
1668c2ecf20Sopenharmony_ci				}
1678c2ecf20Sopenharmony_ci				i = syn >> 2;
1688c2ecf20Sopenharmony_ci			} else {
1698c2ecf20Sopenharmony_ci				for (i = 0; i < 32; i++)
1708c2ecf20Sopenharmony_ci					if (syn == data_sbit[i])
1718c2ecf20Sopenharmony_ci						break;
1728c2ecf20Sopenharmony_ci				if (i < 32)
1738c2ecf20Sopenharmony_ci					fmt = KERN_ALERT "%s"
1748c2ecf20Sopenharmony_ci					      "%#04x -- %s bit error "
1758c2ecf20Sopenharmony_ci					      "at data bit D%s%u\n";
1768c2ecf20Sopenharmony_ci				else
1778c2ecf20Sopenharmony_ci					fmt = KERN_ALERT "%s"
1788c2ecf20Sopenharmony_ci					      "%#04x -- %s bit error\n";
1798c2ecf20Sopenharmony_ci			}
1808c2ecf20Sopenharmony_ci		}
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (action != MIPS_BE_FIXUP)
1848c2ecf20Sopenharmony_ci		printk(KERN_ALERT "Bus error %s: %s %s %s at %#010lx\n",
1858c2ecf20Sopenharmony_ci			kind, agent, cycle, event, address);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (action != MIPS_BE_FIXUP && erraddr & KN0X_EAR_ECCERR)
1888c2ecf20Sopenharmony_ci		printk(fmt, "  ECC syndrome ", syn, status, xbit, i);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return action;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ciint dec_ecc_be_handler(struct pt_regs *regs, int is_fixup)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	return dec_ecc_be_backend(regs, is_fixup, 0);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciirqreturn_t dec_ecc_be_interrupt(int irq, void *dev_id)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	struct pt_regs *regs = get_irq_regs();
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	int action = dec_ecc_be_backend(regs, 0, 1);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (action == MIPS_BE_DISCARD)
2058c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/*
2088c2ecf20Sopenharmony_ci	 * FIXME: Find the affected processes and kill them, otherwise
2098c2ecf20Sopenharmony_ci	 * we must die.
2108c2ecf20Sopenharmony_ci	 *
2118c2ecf20Sopenharmony_ci	 * The interrupt is asynchronously delivered thus EPC and RA
2128c2ecf20Sopenharmony_ci	 * may be irrelevant, but are printed for a reference.
2138c2ecf20Sopenharmony_ci	 */
2148c2ecf20Sopenharmony_ci	printk(KERN_ALERT "Fatal bus interrupt, epc == %08lx, ra == %08lx\n",
2158c2ecf20Sopenharmony_ci	       regs->cp0_epc, regs->regs[31]);
2168c2ecf20Sopenharmony_ci	die("Unrecoverable bus error", regs);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/*
2218c2ecf20Sopenharmony_ci * Initialization differs a bit between KN02 and KN03/KN05, so we
2228c2ecf20Sopenharmony_ci * need two variants.  Once set up, all systems can be handled the
2238c2ecf20Sopenharmony_ci * same way.
2248c2ecf20Sopenharmony_ci */
2258c2ecf20Sopenharmony_cistatic inline void dec_kn02_be_init(void)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	volatile u32 *csr = (void *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	kn0x_erraddr = (void *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_ERRADDR);
2308c2ecf20Sopenharmony_ci	kn0x_chksyn = (void *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CHKSYN);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* Preset write-only bits of the Control Register cache. */
2338c2ecf20Sopenharmony_ci	cached_kn02_csr = *csr | KN02_CSR_LEDS;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/* Set normal ECC detection and generation. */
2368c2ecf20Sopenharmony_ci	cached_kn02_csr &= ~(KN02_CSR_DIAGCHK | KN02_CSR_DIAGGEN);
2378c2ecf20Sopenharmony_ci	/* Enable ECC correction. */
2388c2ecf20Sopenharmony_ci	cached_kn02_csr |= KN02_CSR_CORRECT;
2398c2ecf20Sopenharmony_ci	*csr = cached_kn02_csr;
2408c2ecf20Sopenharmony_ci	iob();
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic inline void dec_kn03_be_init(void)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	volatile u32 *mcr = (void *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
2468c2ecf20Sopenharmony_ci	volatile u32 *mbcs = (void *)CKSEG1ADDR(KN4K_SLOT_BASE + KN4K_MB_CSR);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	kn0x_erraddr = (void *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_ERRADDR);
2498c2ecf20Sopenharmony_ci	kn0x_chksyn = (void *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_CHKSYN);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/*
2528c2ecf20Sopenharmony_ci	 * Set normal ECC detection and generation, enable ECC correction.
2538c2ecf20Sopenharmony_ci	 * For KN05 we also need to make sure EE (?) is enabled in the MB.
2548c2ecf20Sopenharmony_ci	 * Otherwise DBE/IBE exceptions would be masked but bus error
2558c2ecf20Sopenharmony_ci	 * interrupts would still arrive, resulting in an inevitable crash
2568c2ecf20Sopenharmony_ci	 * if get_dbe() triggers one.
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci	*mcr = (*mcr & ~(KN03_MCR_DIAGCHK | KN03_MCR_DIAGGEN)) |
2598c2ecf20Sopenharmony_ci	       KN03_MCR_CORRECT;
2608c2ecf20Sopenharmony_ci	if (current_cpu_type() == CPU_R4400SC)
2618c2ecf20Sopenharmony_ci		*mbcs |= KN4K_MB_CSR_EE;
2628c2ecf20Sopenharmony_ci	fast_iob();
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_civoid __init dec_ecc_be_init(void)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	if (mips_machtype == MACH_DS5000_200)
2688c2ecf20Sopenharmony_ci		dec_kn02_be_init();
2698c2ecf20Sopenharmony_ci	else
2708c2ecf20Sopenharmony_ci		dec_kn03_be_init();
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* Clear any leftover errors from the firmware. */
2738c2ecf20Sopenharmony_ci	dec_ecc_be_ack();
2748c2ecf20Sopenharmony_ci}
275