18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2002,2003 Broadcom Corporation
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/*
78c2ecf20Sopenharmony_ci * The Bus Watcher monitors internal bus transactions and maintains
88c2ecf20Sopenharmony_ci * counts of transactions with error status, logging details and
98c2ecf20Sopenharmony_ci * causing one of several interrupts.  This driver provides a handler
108c2ecf20Sopenharmony_ci * for those interrupts which aggregates the counts (to avoid
118c2ecf20Sopenharmony_ci * saturating the 8-bit counters) and provides a presence in
128c2ecf20Sopenharmony_ci * /proc/bus_watcher if PROC_FS is on.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/sched.h>
198c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
208c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
218c2ecf20Sopenharmony_ci#include <asm/io.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250.h>
248c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250_regs.h>
258c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250_int.h>
268c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250_scd.h>
278c2ecf20Sopenharmony_ci#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
288c2ecf20Sopenharmony_ci#include <asm/sibyte/bcm1480_regs.h>
298c2ecf20Sopenharmony_ci#endif
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct bw_stats_struct {
338c2ecf20Sopenharmony_ci	uint64_t status;
348c2ecf20Sopenharmony_ci	uint32_t l2_err;
358c2ecf20Sopenharmony_ci	uint32_t memio_err;
368c2ecf20Sopenharmony_ci	int status_printed;
378c2ecf20Sopenharmony_ci	unsigned long l2_cor_d;
388c2ecf20Sopenharmony_ci	unsigned long l2_bad_d;
398c2ecf20Sopenharmony_ci	unsigned long l2_cor_t;
408c2ecf20Sopenharmony_ci	unsigned long l2_bad_t;
418c2ecf20Sopenharmony_ci	unsigned long mem_cor_d;
428c2ecf20Sopenharmony_ci	unsigned long mem_bad_d;
438c2ecf20Sopenharmony_ci	unsigned long bus_error;
448c2ecf20Sopenharmony_ci} bw_stats;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic void print_summary(uint32_t status, uint32_t l2_err,
488c2ecf20Sopenharmony_ci			  uint32_t memio_err)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	printk("Bus watcher error counters: %08x %08x\n", l2_err, memio_err);
518c2ecf20Sopenharmony_ci	printk("\nLast recorded signature:\n");
528c2ecf20Sopenharmony_ci	printk("Request %02x from %d, answered by %d with Dcode %d\n",
538c2ecf20Sopenharmony_ci	       (unsigned int)(G_SCD_BERR_TID(status) & 0x3f),
548c2ecf20Sopenharmony_ci	       (int)(G_SCD_BERR_TID(status) >> 6),
558c2ecf20Sopenharmony_ci	       (int)G_SCD_BERR_RID(status),
568c2ecf20Sopenharmony_ci	       (int)G_SCD_BERR_DCODE(status));
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * check_bus_watcher is exported for use in situations where we want
618c2ecf20Sopenharmony_ci * to see the most recent status of the bus watcher, which might have
628c2ecf20Sopenharmony_ci * already been destructively read out of the registers.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * notes: this is currently used by the cache error handler
658c2ecf20Sopenharmony_ci *	  should provide locking against the interrupt handler
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_civoid check_bus_watcher(void)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	u32 status, l2_err, memio_err;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#if defined(CONFIG_SIBYTE_BCM112X) || defined(CONFIG_SIBYTE_SB1250)
728c2ecf20Sopenharmony_ci	/* Use non-destructive register */
738c2ecf20Sopenharmony_ci	status = csr_in32(IOADDR(A_SCD_BUS_ERR_STATUS_DEBUG));
748c2ecf20Sopenharmony_ci#elif defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
758c2ecf20Sopenharmony_ci	/* Use non-destructive register */
768c2ecf20Sopenharmony_ci	/* Same as 1250 except BUS_ERR_STATUS_DEBUG is in a different place. */
778c2ecf20Sopenharmony_ci	status = csr_in32(IOADDR(A_BCM1480_BUS_ERR_STATUS_DEBUG));
788c2ecf20Sopenharmony_ci#else
798c2ecf20Sopenharmony_ci#error bus watcher being built for unknown Sibyte SOC!
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci	if (!(status & 0x7fffffff)) {
828c2ecf20Sopenharmony_ci		printk("Using last values reaped by bus watcher driver\n");
838c2ecf20Sopenharmony_ci		status = bw_stats.status;
848c2ecf20Sopenharmony_ci		l2_err = bw_stats.l2_err;
858c2ecf20Sopenharmony_ci		memio_err = bw_stats.memio_err;
868c2ecf20Sopenharmony_ci	} else {
878c2ecf20Sopenharmony_ci		l2_err = csr_in32(IOADDR(A_BUS_L2_ERRORS));
888c2ecf20Sopenharmony_ci		memio_err = csr_in32(IOADDR(A_BUS_MEM_IO_ERRORS));
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	if (status & ~(1UL << 31))
918c2ecf20Sopenharmony_ci		print_summary(status, l2_err, memio_err);
928c2ecf20Sopenharmony_ci	else
938c2ecf20Sopenharmony_ci		printk("Bus watcher indicates no error\n");
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* For simplicity, I want to assume a single read is required each
998c2ecf20Sopenharmony_ci   time */
1008c2ecf20Sopenharmony_cistatic int bw_proc_show(struct seq_file *m, void *v)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct bw_stats_struct *stats = m->private;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	seq_puts(m, "SiByte Bus Watcher statistics\n");
1058c2ecf20Sopenharmony_ci	seq_puts(m, "-----------------------------\n");
1068c2ecf20Sopenharmony_ci	seq_printf(m, "L2-d-cor %8ld\nL2-d-bad %8ld\n",
1078c2ecf20Sopenharmony_ci		   stats->l2_cor_d, stats->l2_bad_d);
1088c2ecf20Sopenharmony_ci	seq_printf(m, "L2-t-cor %8ld\nL2-t-bad %8ld\n",
1098c2ecf20Sopenharmony_ci		   stats->l2_cor_t, stats->l2_bad_t);
1108c2ecf20Sopenharmony_ci	seq_printf(m, "MC-d-cor %8ld\nMC-d-bad %8ld\n",
1118c2ecf20Sopenharmony_ci		   stats->mem_cor_d, stats->mem_bad_d);
1128c2ecf20Sopenharmony_ci	seq_printf(m, "IO-err   %8ld\n", stats->bus_error);
1138c2ecf20Sopenharmony_ci	seq_puts(m, "\nLast recorded signature:\n");
1148c2ecf20Sopenharmony_ci	seq_printf(m, "Request %02x from %d, answered by %d with Dcode %d\n",
1158c2ecf20Sopenharmony_ci		   (unsigned int)(G_SCD_BERR_TID(stats->status) & 0x3f),
1168c2ecf20Sopenharmony_ci		   (int)(G_SCD_BERR_TID(stats->status) >> 6),
1178c2ecf20Sopenharmony_ci		   (int)G_SCD_BERR_RID(stats->status),
1188c2ecf20Sopenharmony_ci		   (int)G_SCD_BERR_DCODE(stats->status));
1198c2ecf20Sopenharmony_ci	/* XXXKW indicate multiple errors between printings, or stats
1208c2ecf20Sopenharmony_ci	   collection (or both)? */
1218c2ecf20Sopenharmony_ci	if (stats->status & M_SCD_BERR_MULTERRS)
1228c2ecf20Sopenharmony_ci		seq_puts(m, "Multiple errors observed since last check.\n");
1238c2ecf20Sopenharmony_ci	if (stats->status_printed) {
1248c2ecf20Sopenharmony_ci		seq_puts(m, "(no change since last printing)\n");
1258c2ecf20Sopenharmony_ci	} else {
1268c2ecf20Sopenharmony_ci		stats->status_printed = 1;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic void create_proc_decoder(struct bw_stats_struct *stats)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct proc_dir_entry *ent;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	ent = proc_create_single_data("bus_watcher", S_IWUSR | S_IRUGO, NULL,
1378c2ecf20Sopenharmony_ci			bw_proc_show, stats);
1388c2ecf20Sopenharmony_ci	if (!ent) {
1398c2ecf20Sopenharmony_ci		printk(KERN_INFO "Unable to initialize bus_watcher /proc entry\n");
1408c2ecf20Sopenharmony_ci		return;
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#endif /* CONFIG_PROC_FS */
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/*
1478c2ecf20Sopenharmony_ci * sibyte_bw_int - handle bus watcher interrupts and accumulate counts
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * notes: possible re-entry due to multiple sources
1508c2ecf20Sopenharmony_ci *	  should check/indicate saturation
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_cistatic irqreturn_t sibyte_bw_int(int irq, void *data)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct bw_stats_struct *stats = data;
1558c2ecf20Sopenharmony_ci	unsigned long cntr;
1568c2ecf20Sopenharmony_ci#ifdef CONFIG_SIBYTE_BW_TRACE
1578c2ecf20Sopenharmony_ci	int i;
1588c2ecf20Sopenharmony_ci#endif
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#ifdef CONFIG_SIBYTE_BW_TRACE
1618c2ecf20Sopenharmony_ci	csr_out32(M_SCD_TRACE_CFG_FREEZE, IOADDR(A_SCD_TRACE_CFG));
1628c2ecf20Sopenharmony_ci	csr_out32(M_SCD_TRACE_CFG_START_READ, IOADDR(A_SCD_TRACE_CFG));
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	for (i=0; i<256*6; i++)
1658c2ecf20Sopenharmony_ci		printk("%016llx\n",
1668c2ecf20Sopenharmony_ci		       (long long)__raw_readq(IOADDR(A_SCD_TRACE_READ)));
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	csr_out32(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
1698c2ecf20Sopenharmony_ci	csr_out32(M_SCD_TRACE_CFG_START, IOADDR(A_SCD_TRACE_CFG));
1708c2ecf20Sopenharmony_ci#endif
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* Destructive read, clears register and interrupt */
1738c2ecf20Sopenharmony_ci	stats->status = csr_in32(IOADDR(A_SCD_BUS_ERR_STATUS));
1748c2ecf20Sopenharmony_ci	stats->status_printed = 0;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	stats->l2_err = cntr = csr_in32(IOADDR(A_BUS_L2_ERRORS));
1778c2ecf20Sopenharmony_ci	stats->l2_cor_d += G_SCD_L2ECC_CORR_D(cntr);
1788c2ecf20Sopenharmony_ci	stats->l2_bad_d += G_SCD_L2ECC_BAD_D(cntr);
1798c2ecf20Sopenharmony_ci	stats->l2_cor_t += G_SCD_L2ECC_CORR_T(cntr);
1808c2ecf20Sopenharmony_ci	stats->l2_bad_t += G_SCD_L2ECC_BAD_T(cntr);
1818c2ecf20Sopenharmony_ci	csr_out32(0, IOADDR(A_BUS_L2_ERRORS));
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	stats->memio_err = cntr = csr_in32(IOADDR(A_BUS_MEM_IO_ERRORS));
1848c2ecf20Sopenharmony_ci	stats->mem_cor_d += G_SCD_MEM_ECC_CORR(cntr);
1858c2ecf20Sopenharmony_ci	stats->mem_bad_d += G_SCD_MEM_ECC_BAD(cntr);
1868c2ecf20Sopenharmony_ci	stats->bus_error += G_SCD_MEM_BUSERR(cntr);
1878c2ecf20Sopenharmony_ci	csr_out32(0, IOADDR(A_BUS_MEM_IO_ERRORS));
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciint __init sibyte_bus_watcher(void)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	memset(&bw_stats, 0, sizeof(struct bw_stats_struct));
1958c2ecf20Sopenharmony_ci	bw_stats.status_printed = 1;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (request_irq(K_INT_BAD_ECC, sibyte_bw_int, 0, "Bus watcher", &bw_stats)) {
1988c2ecf20Sopenharmony_ci		printk("Failed to register bus watcher BAD_ECC irq\n");
1998c2ecf20Sopenharmony_ci		return -1;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci	if (request_irq(K_INT_COR_ECC, sibyte_bw_int, 0, "Bus watcher", &bw_stats)) {
2028c2ecf20Sopenharmony_ci		free_irq(K_INT_BAD_ECC, &bw_stats);
2038c2ecf20Sopenharmony_ci		printk("Failed to register bus watcher COR_ECC irq\n");
2048c2ecf20Sopenharmony_ci		return -1;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	if (request_irq(K_INT_IO_BUS, sibyte_bw_int, 0, "Bus watcher", &bw_stats)) {
2078c2ecf20Sopenharmony_ci		free_irq(K_INT_BAD_ECC, &bw_stats);
2088c2ecf20Sopenharmony_ci		free_irq(K_INT_COR_ECC, &bw_stats);
2098c2ecf20Sopenharmony_ci		printk("Failed to register bus watcher IO_BUS irq\n");
2108c2ecf20Sopenharmony_ci		return -1;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
2148c2ecf20Sopenharmony_ci	create_proc_decoder(&bw_stats);
2158c2ecf20Sopenharmony_ci#endif
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci#ifdef CONFIG_SIBYTE_BW_TRACE
2188c2ecf20Sopenharmony_ci	csr_out32((M_SCD_TRSEQ_ASAMPLE | M_SCD_TRSEQ_DSAMPLE |
2198c2ecf20Sopenharmony_ci		   K_SCD_TRSEQ_TRIGGER_ALL),
2208c2ecf20Sopenharmony_ci		  IOADDR(A_SCD_TRACE_SEQUENCE_0));
2218c2ecf20Sopenharmony_ci	csr_out32(M_SCD_TRACE_CFG_RESET, IOADDR(A_SCD_TRACE_CFG));
2228c2ecf20Sopenharmony_ci	csr_out32(M_SCD_TRACE_CFG_START, IOADDR(A_SCD_TRACE_CFG));
2238c2ecf20Sopenharmony_ci#endif
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return 0;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cidevice_initcall(sibyte_bus_watcher);
229