162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ip22-berr.c: Bus error handling.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2002, 2003 Ladislav Michl (ladis@linux-mips.org)
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/init.h>
962306a36Sopenharmony_ci#include <linux/kernel.h>
1062306a36Sopenharmony_ci#include <linux/sched/signal.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <asm/addrspace.h>
1362306a36Sopenharmony_ci#include <asm/traps.h>
1462306a36Sopenharmony_ci#include <asm/branch.h>
1562306a36Sopenharmony_ci#include <asm/irq_regs.h>
1662306a36Sopenharmony_ci#include <asm/sgi/mc.h>
1762306a36Sopenharmony_ci#include <asm/sgi/hpc3.h>
1862306a36Sopenharmony_ci#include <asm/sgi/ioc.h>
1962306a36Sopenharmony_ci#include <asm/sgi/ip22.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic unsigned int cpu_err_stat;	/* Status reg for CPU */
2362306a36Sopenharmony_cistatic unsigned int gio_err_stat;	/* Status reg for GIO */
2462306a36Sopenharmony_cistatic unsigned int cpu_err_addr;	/* Error address reg for CPU */
2562306a36Sopenharmony_cistatic unsigned int gio_err_addr;	/* Error address reg for GIO */
2662306a36Sopenharmony_cistatic unsigned int extio_stat;
2762306a36Sopenharmony_cistatic unsigned int hpc3_berr_stat;	/* Bus error interrupt status */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic void save_and_clear_buserr(void)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	/* save status registers */
3262306a36Sopenharmony_ci	cpu_err_addr = sgimc->cerr;
3362306a36Sopenharmony_ci	cpu_err_stat = sgimc->cstat;
3462306a36Sopenharmony_ci	gio_err_addr = sgimc->gerr;
3562306a36Sopenharmony_ci	gio_err_stat = sgimc->gstat;
3662306a36Sopenharmony_ci	extio_stat = ip22_is_fullhouse() ? sgioc->extio : (sgint->errstat << 4);
3762306a36Sopenharmony_ci	hpc3_berr_stat = hpc3c0->bestat;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	sgimc->cstat = sgimc->gstat = 0;
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define GIO_ERRMASK	0xff00
4362306a36Sopenharmony_ci#define CPU_ERRMASK	0x3f00
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic void print_buserr(void)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	if (extio_stat & EXTIO_MC_BUSERR)
4862306a36Sopenharmony_ci		printk(KERN_ERR "MC Bus Error\n");
4962306a36Sopenharmony_ci	if (extio_stat & EXTIO_HPC3_BUSERR)
5062306a36Sopenharmony_ci		printk(KERN_ERR "HPC3 Bus Error 0x%x:<id=0x%x,%s,lane=0x%x>\n",
5162306a36Sopenharmony_ci			hpc3_berr_stat,
5262306a36Sopenharmony_ci			(hpc3_berr_stat & HPC3_BESTAT_PIDMASK) >>
5362306a36Sopenharmony_ci					  HPC3_BESTAT_PIDSHIFT,
5462306a36Sopenharmony_ci			(hpc3_berr_stat & HPC3_BESTAT_CTYPE) ? "PIO" : "DMA",
5562306a36Sopenharmony_ci			hpc3_berr_stat & HPC3_BESTAT_BLMASK);
5662306a36Sopenharmony_ci	if (extio_stat & EXTIO_EISA_BUSERR)
5762306a36Sopenharmony_ci		printk(KERN_ERR "EISA Bus Error\n");
5862306a36Sopenharmony_ci	if (cpu_err_stat & CPU_ERRMASK)
5962306a36Sopenharmony_ci		printk(KERN_ERR "CPU error 0x%x<%s%s%s%s%s%s> @ 0x%08x\n",
6062306a36Sopenharmony_ci			cpu_err_stat,
6162306a36Sopenharmony_ci			cpu_err_stat & SGIMC_CSTAT_RD ? "RD " : "",
6262306a36Sopenharmony_ci			cpu_err_stat & SGIMC_CSTAT_PAR ? "PAR " : "",
6362306a36Sopenharmony_ci			cpu_err_stat & SGIMC_CSTAT_ADDR ? "ADDR " : "",
6462306a36Sopenharmony_ci			cpu_err_stat & SGIMC_CSTAT_SYSAD_PAR ? "SYSAD " : "",
6562306a36Sopenharmony_ci			cpu_err_stat & SGIMC_CSTAT_SYSCMD_PAR ? "SYSCMD " : "",
6662306a36Sopenharmony_ci			cpu_err_stat & SGIMC_CSTAT_BAD_DATA ? "BAD_DATA " : "",
6762306a36Sopenharmony_ci			cpu_err_addr);
6862306a36Sopenharmony_ci	if (gio_err_stat & GIO_ERRMASK)
6962306a36Sopenharmony_ci		printk(KERN_ERR "GIO error 0x%x:<%s%s%s%s%s%s%s%s> @ 0x%08x\n",
7062306a36Sopenharmony_ci			gio_err_stat,
7162306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_RD ? "RD " : "",
7262306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_WR ? "WR " : "",
7362306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_TIME ? "TIME " : "",
7462306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_PROM ? "PROM " : "",
7562306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_ADDR ? "ADDR " : "",
7662306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_BC ? "BC " : "",
7762306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_PIO_RD ? "PIO_RD " : "",
7862306a36Sopenharmony_ci			gio_err_stat & SGIMC_GSTAT_PIO_WR ? "PIO_WR " : "",
7962306a36Sopenharmony_ci			gio_err_addr);
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/*
8362306a36Sopenharmony_ci * MC sends an interrupt whenever bus or parity errors occur. In addition,
8462306a36Sopenharmony_ci * if the error happened during a CPU read, it also asserts the bus error
8562306a36Sopenharmony_ci * pin on the R4K. Code in bus error handler save the MC bus error registers
8662306a36Sopenharmony_ci * and then clear the interrupt when this happens.
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_civoid ip22_be_interrupt(int irq)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	const int field = 2 * sizeof(unsigned long);
9262306a36Sopenharmony_ci	struct pt_regs *regs = get_irq_regs();
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	save_and_clear_buserr();
9562306a36Sopenharmony_ci	print_buserr();
9662306a36Sopenharmony_ci	printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
9762306a36Sopenharmony_ci	       (regs->cp0_cause & 4) ? "Data" : "Instruction",
9862306a36Sopenharmony_ci	       field, regs->cp0_epc, field, regs->regs[31]);
9962306a36Sopenharmony_ci	/* Assume it would be too dangerous to continue ... */
10062306a36Sopenharmony_ci	die_if_kernel("Oops", regs);
10162306a36Sopenharmony_ci	force_sig(SIGBUS);
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistatic int ip22_be_handler(struct pt_regs *regs, int is_fixup)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	save_and_clear_buserr();
10762306a36Sopenharmony_ci	if (is_fixup)
10862306a36Sopenharmony_ci		return MIPS_BE_FIXUP;
10962306a36Sopenharmony_ci	print_buserr();
11062306a36Sopenharmony_ci	return MIPS_BE_FATAL;
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_civoid __init ip22_be_init(void)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	mips_set_be_handler(ip22_be_handler);
11662306a36Sopenharmony_ci}
117