xref: /OK3568_Linux_fs/kernel/arch/mips/mm/cerr-sb1.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2001,2002,2003 Broadcom Corporation
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #include <linux/sched.h>
6*4882a593Smuzhiyun #include <asm/mipsregs.h>
7*4882a593Smuzhiyun #include <asm/sibyte/sb1250.h>
8*4882a593Smuzhiyun #include <asm/sibyte/sb1250_regs.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #if !defined(CONFIG_SIBYTE_BUS_WATCHER) || defined(CONFIG_SIBYTE_BW_TRACE)
11*4882a593Smuzhiyun #include <asm/io.h>
12*4882a593Smuzhiyun #include <asm/sibyte/sb1250_scd.h>
13*4882a593Smuzhiyun #endif
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * We'd like to dump the L2_ECC_TAG register on errors, but errata make
17*4882a593Smuzhiyun  * that unsafe... So for now we don't.	(BCM1250/BCM112x erratum SOC-48.)
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun #undef DUMP_L2_ECC_TAG_ON_ERROR
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* SB1 definitions */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* XXX should come from config1 XXX */
24*4882a593Smuzhiyun #define SB1_CACHE_INDEX_MASK   0x1fe0
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define CP0_ERRCTL_RECOVERABLE (1 << 31)
27*4882a593Smuzhiyun #define CP0_ERRCTL_DCACHE      (1 << 30)
28*4882a593Smuzhiyun #define CP0_ERRCTL_ICACHE      (1 << 29)
29*4882a593Smuzhiyun #define CP0_ERRCTL_MULTIBUS    (1 << 23)
30*4882a593Smuzhiyun #define CP0_ERRCTL_MC_TLB      (1 << 15)
31*4882a593Smuzhiyun #define CP0_ERRCTL_MC_TIMEOUT  (1 << 14)
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define CP0_CERRI_TAG_PARITY   (1 << 29)
34*4882a593Smuzhiyun #define CP0_CERRI_DATA_PARITY  (1 << 28)
35*4882a593Smuzhiyun #define CP0_CERRI_EXTERNAL     (1 << 26)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define CP0_CERRI_IDX_VALID(c) (!((c) & CP0_CERRI_EXTERNAL))
38*4882a593Smuzhiyun #define CP0_CERRI_DATA	       (CP0_CERRI_DATA_PARITY)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define CP0_CERRD_MULTIPLE     (1 << 31)
41*4882a593Smuzhiyun #define CP0_CERRD_TAG_STATE    (1 << 30)
42*4882a593Smuzhiyun #define CP0_CERRD_TAG_ADDRESS  (1 << 29)
43*4882a593Smuzhiyun #define CP0_CERRD_DATA_SBE     (1 << 28)
44*4882a593Smuzhiyun #define CP0_CERRD_DATA_DBE     (1 << 27)
45*4882a593Smuzhiyun #define CP0_CERRD_EXTERNAL     (1 << 26)
46*4882a593Smuzhiyun #define CP0_CERRD_LOAD	       (1 << 25)
47*4882a593Smuzhiyun #define CP0_CERRD_STORE	       (1 << 24)
48*4882a593Smuzhiyun #define CP0_CERRD_FILLWB       (1 << 23)
49*4882a593Smuzhiyun #define CP0_CERRD_COHERENCY    (1 << 22)
50*4882a593Smuzhiyun #define CP0_CERRD_DUPTAG       (1 << 21)
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define CP0_CERRD_DPA_VALID(c) (!((c) & CP0_CERRD_EXTERNAL))
53*4882a593Smuzhiyun #define CP0_CERRD_IDX_VALID(c) \
54*4882a593Smuzhiyun    (((c) & (CP0_CERRD_LOAD | CP0_CERRD_STORE)) ? (!((c) & CP0_CERRD_EXTERNAL)) : 0)
55*4882a593Smuzhiyun #define CP0_CERRD_CAUSES \
56*4882a593Smuzhiyun    (CP0_CERRD_LOAD | CP0_CERRD_STORE | CP0_CERRD_FILLWB | CP0_CERRD_COHERENCY | CP0_CERRD_DUPTAG)
57*4882a593Smuzhiyun #define CP0_CERRD_TYPES \
58*4882a593Smuzhiyun    (CP0_CERRD_TAG_STATE | CP0_CERRD_TAG_ADDRESS | CP0_CERRD_DATA_SBE | CP0_CERRD_DATA_DBE | CP0_CERRD_EXTERNAL)
59*4882a593Smuzhiyun #define CP0_CERRD_DATA	       (CP0_CERRD_DATA_SBE | CP0_CERRD_DATA_DBE)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static uint32_t extract_ic(unsigned short addr, int data);
62*4882a593Smuzhiyun static uint32_t extract_dc(unsigned short addr, int data);
63*4882a593Smuzhiyun 
breakout_errctl(unsigned int val)64*4882a593Smuzhiyun static inline void breakout_errctl(unsigned int val)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	if (val & CP0_ERRCTL_RECOVERABLE)
67*4882a593Smuzhiyun 		printk(" recoverable");
68*4882a593Smuzhiyun 	if (val & CP0_ERRCTL_DCACHE)
69*4882a593Smuzhiyun 		printk(" dcache");
70*4882a593Smuzhiyun 	if (val & CP0_ERRCTL_ICACHE)
71*4882a593Smuzhiyun 		printk(" icache");
72*4882a593Smuzhiyun 	if (val & CP0_ERRCTL_MULTIBUS)
73*4882a593Smuzhiyun 		printk(" multiple-buserr");
74*4882a593Smuzhiyun 	printk("\n");
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
breakout_cerri(unsigned int val)77*4882a593Smuzhiyun static inline void breakout_cerri(unsigned int val)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	if (val & CP0_CERRI_TAG_PARITY)
80*4882a593Smuzhiyun 		printk(" tag-parity");
81*4882a593Smuzhiyun 	if (val & CP0_CERRI_DATA_PARITY)
82*4882a593Smuzhiyun 		printk(" data-parity");
83*4882a593Smuzhiyun 	if (val & CP0_CERRI_EXTERNAL)
84*4882a593Smuzhiyun 		printk(" external");
85*4882a593Smuzhiyun 	printk("\n");
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
breakout_cerrd(unsigned int val)88*4882a593Smuzhiyun static inline void breakout_cerrd(unsigned int val)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	switch (val & CP0_CERRD_CAUSES) {
91*4882a593Smuzhiyun 	case CP0_CERRD_LOAD:
92*4882a593Smuzhiyun 		printk(" load,");
93*4882a593Smuzhiyun 		break;
94*4882a593Smuzhiyun 	case CP0_CERRD_STORE:
95*4882a593Smuzhiyun 		printk(" store,");
96*4882a593Smuzhiyun 		break;
97*4882a593Smuzhiyun 	case CP0_CERRD_FILLWB:
98*4882a593Smuzhiyun 		printk(" fill/wb,");
99*4882a593Smuzhiyun 		break;
100*4882a593Smuzhiyun 	case CP0_CERRD_COHERENCY:
101*4882a593Smuzhiyun 		printk(" coherency,");
102*4882a593Smuzhiyun 		break;
103*4882a593Smuzhiyun 	case CP0_CERRD_DUPTAG:
104*4882a593Smuzhiyun 		printk(" duptags,");
105*4882a593Smuzhiyun 		break;
106*4882a593Smuzhiyun 	default:
107*4882a593Smuzhiyun 		printk(" NO CAUSE,");
108*4882a593Smuzhiyun 		break;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 	if (!(val & CP0_CERRD_TYPES))
111*4882a593Smuzhiyun 		printk(" NO TYPE");
112*4882a593Smuzhiyun 	else {
113*4882a593Smuzhiyun 		if (val & CP0_CERRD_MULTIPLE)
114*4882a593Smuzhiyun 			printk(" multi-err");
115*4882a593Smuzhiyun 		if (val & CP0_CERRD_TAG_STATE)
116*4882a593Smuzhiyun 			printk(" tag-state");
117*4882a593Smuzhiyun 		if (val & CP0_CERRD_TAG_ADDRESS)
118*4882a593Smuzhiyun 			printk(" tag-address");
119*4882a593Smuzhiyun 		if (val & CP0_CERRD_DATA_SBE)
120*4882a593Smuzhiyun 			printk(" data-SBE");
121*4882a593Smuzhiyun 		if (val & CP0_CERRD_DATA_DBE)
122*4882a593Smuzhiyun 			printk(" data-DBE");
123*4882a593Smuzhiyun 		if (val & CP0_CERRD_EXTERNAL)
124*4882a593Smuzhiyun 			printk(" external");
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	printk("\n");
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun #ifndef CONFIG_SIBYTE_BUS_WATCHER
130*4882a593Smuzhiyun 
check_bus_watcher(void)131*4882a593Smuzhiyun static void check_bus_watcher(void)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	uint32_t status, l2_err, memio_err;
134*4882a593Smuzhiyun #ifdef DUMP_L2_ECC_TAG_ON_ERROR
135*4882a593Smuzhiyun 	uint64_t l2_tag;
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* Destructive read, clears register and interrupt */
139*4882a593Smuzhiyun 	status = csr_in32(IOADDR(A_SCD_BUS_ERR_STATUS));
140*4882a593Smuzhiyun 	/* Bit 31 is always on, but there's no #define for that */
141*4882a593Smuzhiyun 	if (status & ~(1UL << 31)) {
142*4882a593Smuzhiyun 		l2_err = csr_in32(IOADDR(A_BUS_L2_ERRORS));
143*4882a593Smuzhiyun #ifdef DUMP_L2_ECC_TAG_ON_ERROR
144*4882a593Smuzhiyun 		l2_tag = in64(IOADDR(A_L2_ECC_TAG));
145*4882a593Smuzhiyun #endif
146*4882a593Smuzhiyun 		memio_err = csr_in32(IOADDR(A_BUS_MEM_IO_ERRORS));
147*4882a593Smuzhiyun 		printk("Bus watcher error counters: %08x %08x\n", l2_err, memio_err);
148*4882a593Smuzhiyun 		printk("\nLast recorded signature:\n");
149*4882a593Smuzhiyun 		printk("Request %02x from %d, answered by %d with Dcode %d\n",
150*4882a593Smuzhiyun 		       (unsigned int)(G_SCD_BERR_TID(status) & 0x3f),
151*4882a593Smuzhiyun 		       (int)(G_SCD_BERR_TID(status) >> 6),
152*4882a593Smuzhiyun 		       (int)G_SCD_BERR_RID(status),
153*4882a593Smuzhiyun 		       (int)G_SCD_BERR_DCODE(status));
154*4882a593Smuzhiyun #ifdef DUMP_L2_ECC_TAG_ON_ERROR
155*4882a593Smuzhiyun 		printk("Last L2 tag w/ bad ECC: %016llx\n", l2_tag);
156*4882a593Smuzhiyun #endif
157*4882a593Smuzhiyun 	} else {
158*4882a593Smuzhiyun 		printk("Bus watcher indicates no error\n");
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun #else
162*4882a593Smuzhiyun extern void check_bus_watcher(void);
163*4882a593Smuzhiyun #endif
164*4882a593Smuzhiyun 
sb1_cache_error(void)165*4882a593Smuzhiyun asmlinkage void sb1_cache_error(void)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	uint32_t errctl, cerr_i, cerr_d, dpalo, dpahi, eepc, res;
168*4882a593Smuzhiyun 	unsigned long long cerr_dpa;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun #ifdef CONFIG_SIBYTE_BW_TRACE
171*4882a593Smuzhiyun 	/* Freeze the trace buffer now */
172*4882a593Smuzhiyun 	csr_out32(M_SCD_TRACE_CFG_FREEZE, IOADDR(A_SCD_TRACE_CFG));
173*4882a593Smuzhiyun 	printk("Trace buffer frozen\n");
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	printk("Cache error exception on CPU %x:\n",
177*4882a593Smuzhiyun 	       (read_c0_prid() >> 25) & 0x7);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	__asm__ __volatile__ (
180*4882a593Smuzhiyun 	"	.set	push\n\t"
181*4882a593Smuzhiyun 	"	.set	mips64\n\t"
182*4882a593Smuzhiyun 	"	.set	noat\n\t"
183*4882a593Smuzhiyun 	"	mfc0	%0, $26\n\t"
184*4882a593Smuzhiyun 	"	mfc0	%1, $27\n\t"
185*4882a593Smuzhiyun 	"	mfc0	%2, $27, 1\n\t"
186*4882a593Smuzhiyun 	"	dmfc0	$1, $27, 3\n\t"
187*4882a593Smuzhiyun 	"	dsrl32	%3, $1, 0 \n\t"
188*4882a593Smuzhiyun 	"	sll	%4, $1, 0 \n\t"
189*4882a593Smuzhiyun 	"	mfc0	%5, $30\n\t"
190*4882a593Smuzhiyun 	"	.set	pop"
191*4882a593Smuzhiyun 	: "=r" (errctl), "=r" (cerr_i), "=r" (cerr_d),
192*4882a593Smuzhiyun 	  "=r" (dpahi), "=r" (dpalo), "=r" (eepc));
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	cerr_dpa = (((uint64_t)dpahi) << 32) | dpalo;
195*4882a593Smuzhiyun 	printk(" c0_errorepc ==	  %08x\n", eepc);
196*4882a593Smuzhiyun 	printk(" c0_errctl   ==	  %08x", errctl);
197*4882a593Smuzhiyun 	breakout_errctl(errctl);
198*4882a593Smuzhiyun 	if (errctl & CP0_ERRCTL_ICACHE) {
199*4882a593Smuzhiyun 		printk(" c0_cerr_i   ==	  %08x", cerr_i);
200*4882a593Smuzhiyun 		breakout_cerri(cerr_i);
201*4882a593Smuzhiyun 		if (CP0_CERRI_IDX_VALID(cerr_i)) {
202*4882a593Smuzhiyun 			/* Check index of EPC, allowing for delay slot */
203*4882a593Smuzhiyun 			if (((eepc & SB1_CACHE_INDEX_MASK) != (cerr_i & SB1_CACHE_INDEX_MASK)) &&
204*4882a593Smuzhiyun 			    ((eepc & SB1_CACHE_INDEX_MASK) != ((cerr_i & SB1_CACHE_INDEX_MASK) - 4)))
205*4882a593Smuzhiyun 				printk(" cerr_i idx doesn't match eepc\n");
206*4882a593Smuzhiyun 			else {
207*4882a593Smuzhiyun 				res = extract_ic(cerr_i & SB1_CACHE_INDEX_MASK,
208*4882a593Smuzhiyun 						 (cerr_i & CP0_CERRI_DATA) != 0);
209*4882a593Smuzhiyun 				if (!(res & cerr_i))
210*4882a593Smuzhiyun 					printk("...didn't see indicated icache problem\n");
211*4882a593Smuzhiyun 			}
212*4882a593Smuzhiyun 		}
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 	if (errctl & CP0_ERRCTL_DCACHE) {
215*4882a593Smuzhiyun 		printk(" c0_cerr_d   ==	  %08x", cerr_d);
216*4882a593Smuzhiyun 		breakout_cerrd(cerr_d);
217*4882a593Smuzhiyun 		if (CP0_CERRD_DPA_VALID(cerr_d)) {
218*4882a593Smuzhiyun 			printk(" c0_cerr_dpa == %010llx\n", cerr_dpa);
219*4882a593Smuzhiyun 			if (!CP0_CERRD_IDX_VALID(cerr_d)) {
220*4882a593Smuzhiyun 				res = extract_dc(cerr_dpa & SB1_CACHE_INDEX_MASK,
221*4882a593Smuzhiyun 						 (cerr_d & CP0_CERRD_DATA) != 0);
222*4882a593Smuzhiyun 				if (!(res & cerr_d))
223*4882a593Smuzhiyun 					printk("...didn't see indicated dcache problem\n");
224*4882a593Smuzhiyun 			} else {
225*4882a593Smuzhiyun 				if ((cerr_dpa & SB1_CACHE_INDEX_MASK) != (cerr_d & SB1_CACHE_INDEX_MASK))
226*4882a593Smuzhiyun 					printk(" cerr_d idx doesn't match cerr_dpa\n");
227*4882a593Smuzhiyun 				else {
228*4882a593Smuzhiyun 					res = extract_dc(cerr_d & SB1_CACHE_INDEX_MASK,
229*4882a593Smuzhiyun 							 (cerr_d & CP0_CERRD_DATA) != 0);
230*4882a593Smuzhiyun 					if (!(res & cerr_d))
231*4882a593Smuzhiyun 						printk("...didn't see indicated problem\n");
232*4882a593Smuzhiyun 				}
233*4882a593Smuzhiyun 			}
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	check_bus_watcher();
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/*
240*4882a593Smuzhiyun 	 * Calling panic() when a fatal cache error occurs scrambles the
241*4882a593Smuzhiyun 	 * state of the system (and the cache), making it difficult to
242*4882a593Smuzhiyun 	 * investigate after the fact.	However, if you just stall the CPU,
243*4882a593Smuzhiyun 	 * the other CPU may keep on running, which is typically very
244*4882a593Smuzhiyun 	 * undesirable.
245*4882a593Smuzhiyun 	 */
246*4882a593Smuzhiyun #ifdef CONFIG_SB1_CERR_STALL
247*4882a593Smuzhiyun 	while (1)
248*4882a593Smuzhiyun 		;
249*4882a593Smuzhiyun #else
250*4882a593Smuzhiyun 	panic("unhandled cache error");
251*4882a593Smuzhiyun #endif
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /* Parity lookup table. */
256*4882a593Smuzhiyun static const uint8_t parity[256] = {
257*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
258*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
259*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
260*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
261*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
262*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
263*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
264*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
265*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
266*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
267*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
268*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
269*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
270*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
271*4882a593Smuzhiyun 	1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
272*4882a593Smuzhiyun 	0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
273*4882a593Smuzhiyun };
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /* Masks to select bits for Hamming parity, mask_72_64[i] for bit[i] */
276*4882a593Smuzhiyun static const uint64_t mask_72_64[8] = {
277*4882a593Smuzhiyun 	0x0738C808099264FFULL,
278*4882a593Smuzhiyun 	0x38C808099264FF07ULL,
279*4882a593Smuzhiyun 	0xC808099264FF0738ULL,
280*4882a593Smuzhiyun 	0x08099264FF0738C8ULL,
281*4882a593Smuzhiyun 	0x099264FF0738C808ULL,
282*4882a593Smuzhiyun 	0x9264FF0738C80809ULL,
283*4882a593Smuzhiyun 	0x64FF0738C8080992ULL,
284*4882a593Smuzhiyun 	0xFF0738C808099264ULL
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /* Calculate the parity on a range of bits */
range_parity(uint64_t dword,int max,int min)288*4882a593Smuzhiyun static char range_parity(uint64_t dword, int max, int min)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	char parity = 0;
291*4882a593Smuzhiyun 	int i;
292*4882a593Smuzhiyun 	dword >>= min;
293*4882a593Smuzhiyun 	for (i=max-min; i>=0; i--) {
294*4882a593Smuzhiyun 		if (dword & 0x1)
295*4882a593Smuzhiyun 			parity = !parity;
296*4882a593Smuzhiyun 		dword >>= 1;
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 	return parity;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /* Calculate the 4-bit even byte-parity for an instruction */
inst_parity(uint32_t word)302*4882a593Smuzhiyun static unsigned char inst_parity(uint32_t word)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	int i, j;
305*4882a593Smuzhiyun 	char parity = 0;
306*4882a593Smuzhiyun 	for (j=0; j<4; j++) {
307*4882a593Smuzhiyun 		char byte_parity = 0;
308*4882a593Smuzhiyun 		for (i=0; i<8; i++) {
309*4882a593Smuzhiyun 			if (word & 0x80000000)
310*4882a593Smuzhiyun 				byte_parity = !byte_parity;
311*4882a593Smuzhiyun 			word <<= 1;
312*4882a593Smuzhiyun 		}
313*4882a593Smuzhiyun 		parity <<= 1;
314*4882a593Smuzhiyun 		parity |= byte_parity;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 	return parity;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
extract_ic(unsigned short addr,int data)319*4882a593Smuzhiyun static uint32_t extract_ic(unsigned short addr, int data)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	unsigned short way;
322*4882a593Smuzhiyun 	int valid;
323*4882a593Smuzhiyun 	uint32_t taghi, taglolo, taglohi;
324*4882a593Smuzhiyun 	unsigned long long taglo, va;
325*4882a593Smuzhiyun 	uint64_t tlo_tmp;
326*4882a593Smuzhiyun 	uint8_t lru;
327*4882a593Smuzhiyun 	int res = 0;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	printk("Icache index 0x%04x  ", addr);
330*4882a593Smuzhiyun 	for (way = 0; way < 4; way++) {
331*4882a593Smuzhiyun 		/* Index-load-tag-I */
332*4882a593Smuzhiyun 		__asm__ __volatile__ (
333*4882a593Smuzhiyun 		"	.set	push		\n\t"
334*4882a593Smuzhiyun 		"	.set	noreorder	\n\t"
335*4882a593Smuzhiyun 		"	.set	mips64		\n\t"
336*4882a593Smuzhiyun 		"	.set	noat		\n\t"
337*4882a593Smuzhiyun 		"	cache	4, 0(%3)	\n\t"
338*4882a593Smuzhiyun 		"	mfc0	%0, $29		\n\t"
339*4882a593Smuzhiyun 		"	dmfc0	$1, $28		\n\t"
340*4882a593Smuzhiyun 		"	dsrl32	%1, $1, 0	\n\t"
341*4882a593Smuzhiyun 		"	sll	%2, $1, 0	\n\t"
342*4882a593Smuzhiyun 		"	.set	pop"
343*4882a593Smuzhiyun 		: "=r" (taghi), "=r" (taglohi), "=r" (taglolo)
344*4882a593Smuzhiyun 		: "r" ((way << 13) | addr));
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		taglo = ((unsigned long long)taglohi << 32) | taglolo;
347*4882a593Smuzhiyun 		if (way == 0) {
348*4882a593Smuzhiyun 			lru = (taghi >> 14) & 0xff;
349*4882a593Smuzhiyun 			printk("[Bank %d Set 0x%02x]  LRU > %d %d %d %d > MRU\n",
350*4882a593Smuzhiyun 				    ((addr >> 5) & 0x3), /* bank */
351*4882a593Smuzhiyun 				    ((addr >> 7) & 0x3f), /* index */
352*4882a593Smuzhiyun 				    (lru & 0x3),
353*4882a593Smuzhiyun 				    ((lru >> 2) & 0x3),
354*4882a593Smuzhiyun 				    ((lru >> 4) & 0x3),
355*4882a593Smuzhiyun 				    ((lru >> 6) & 0x3));
356*4882a593Smuzhiyun 		}
357*4882a593Smuzhiyun 		va = (taglo & 0xC0000FFFFFFFE000ULL) | addr;
358*4882a593Smuzhiyun 		if ((taglo & (1 << 31)) && (((taglo >> 62) & 0x3) == 3))
359*4882a593Smuzhiyun 			va |= 0x3FFFF00000000000ULL;
360*4882a593Smuzhiyun 		valid = ((taghi >> 29) & 1);
361*4882a593Smuzhiyun 		if (valid) {
362*4882a593Smuzhiyun 			tlo_tmp = taglo & 0xfff3ff;
363*4882a593Smuzhiyun 			if (((taglo >> 10) & 1) ^ range_parity(tlo_tmp, 23, 0)) {
364*4882a593Smuzhiyun 				printk("   ** bad parity in VTag0/G/ASID\n");
365*4882a593Smuzhiyun 				res |= CP0_CERRI_TAG_PARITY;
366*4882a593Smuzhiyun 			}
367*4882a593Smuzhiyun 			if (((taglo >> 11) & 1) ^ range_parity(taglo, 63, 24)) {
368*4882a593Smuzhiyun 				printk("   ** bad parity in R/VTag1\n");
369*4882a593Smuzhiyun 				res |= CP0_CERRI_TAG_PARITY;
370*4882a593Smuzhiyun 			}
371*4882a593Smuzhiyun 		}
372*4882a593Smuzhiyun 		if (valid ^ ((taghi >> 27) & 1)) {
373*4882a593Smuzhiyun 			printk("   ** bad parity for valid bit\n");
374*4882a593Smuzhiyun 			res |= CP0_CERRI_TAG_PARITY;
375*4882a593Smuzhiyun 		}
376*4882a593Smuzhiyun 		printk(" %d  [VA %016llx]  [Vld? %d]  raw tags: %08X-%016llX\n",
377*4882a593Smuzhiyun 			    way, va, valid, taghi, taglo);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		if (data) {
380*4882a593Smuzhiyun 			uint32_t datahi, insta, instb;
381*4882a593Smuzhiyun 			uint8_t predecode;
382*4882a593Smuzhiyun 			int offset;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 			/* (hit all banks and ways) */
385*4882a593Smuzhiyun 			for (offset = 0; offset < 4; offset++) {
386*4882a593Smuzhiyun 				/* Index-load-data-I */
387*4882a593Smuzhiyun 				__asm__ __volatile__ (
388*4882a593Smuzhiyun 				"	.set	push\n\t"
389*4882a593Smuzhiyun 				"	.set	noreorder\n\t"
390*4882a593Smuzhiyun 				"	.set	mips64\n\t"
391*4882a593Smuzhiyun 				"	.set	noat\n\t"
392*4882a593Smuzhiyun 				"	cache	6, 0(%3)  \n\t"
393*4882a593Smuzhiyun 				"	mfc0	%0, $29, 1\n\t"
394*4882a593Smuzhiyun 				"	dmfc0  $1, $28, 1\n\t"
395*4882a593Smuzhiyun 				"	dsrl32 %1, $1, 0 \n\t"
396*4882a593Smuzhiyun 				"	sll    %2, $1, 0 \n\t"
397*4882a593Smuzhiyun 				"	.set	pop	    \n"
398*4882a593Smuzhiyun 				: "=r" (datahi), "=r" (insta), "=r" (instb)
399*4882a593Smuzhiyun 				: "r" ((way << 13) | addr | (offset << 3)));
400*4882a593Smuzhiyun 				predecode = (datahi >> 8) & 0xff;
401*4882a593Smuzhiyun 				if (((datahi >> 16) & 1) != (uint32_t)range_parity(predecode, 7, 0)) {
402*4882a593Smuzhiyun 					printk("   ** bad parity in predecode\n");
403*4882a593Smuzhiyun 					res |= CP0_CERRI_DATA_PARITY;
404*4882a593Smuzhiyun 				}
405*4882a593Smuzhiyun 				/* XXXKW should/could check predecode bits themselves */
406*4882a593Smuzhiyun 				if (((datahi >> 4) & 0xf) ^ inst_parity(insta)) {
407*4882a593Smuzhiyun 					printk("   ** bad parity in instruction a\n");
408*4882a593Smuzhiyun 					res |= CP0_CERRI_DATA_PARITY;
409*4882a593Smuzhiyun 				}
410*4882a593Smuzhiyun 				if ((datahi & 0xf) ^ inst_parity(instb)) {
411*4882a593Smuzhiyun 					printk("   ** bad parity in instruction b\n");
412*4882a593Smuzhiyun 					res |= CP0_CERRI_DATA_PARITY;
413*4882a593Smuzhiyun 				}
414*4882a593Smuzhiyun 				printk("  %05X-%08X%08X", datahi, insta, instb);
415*4882a593Smuzhiyun 			}
416*4882a593Smuzhiyun 			printk("\n");
417*4882a593Smuzhiyun 		}
418*4882a593Smuzhiyun 	}
419*4882a593Smuzhiyun 	return res;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /* Compute the ECC for a data doubleword */
dc_ecc(uint64_t dword)423*4882a593Smuzhiyun static uint8_t dc_ecc(uint64_t dword)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	uint64_t t;
426*4882a593Smuzhiyun 	uint32_t w;
427*4882a593Smuzhiyun 	uint8_t	 p;
428*4882a593Smuzhiyun 	int	 i;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	p = 0;
431*4882a593Smuzhiyun 	for (i = 7; i >= 0; i--)
432*4882a593Smuzhiyun 	{
433*4882a593Smuzhiyun 		p <<= 1;
434*4882a593Smuzhiyun 		t = dword & mask_72_64[i];
435*4882a593Smuzhiyun 		w = (uint32_t)(t >> 32);
436*4882a593Smuzhiyun 		p ^= (parity[w>>24] ^ parity[(w>>16) & 0xFF]
437*4882a593Smuzhiyun 		      ^ parity[(w>>8) & 0xFF] ^ parity[w & 0xFF]);
438*4882a593Smuzhiyun 		w = (uint32_t)(t & 0xFFFFFFFF);
439*4882a593Smuzhiyun 		p ^= (parity[w>>24] ^ parity[(w>>16) & 0xFF]
440*4882a593Smuzhiyun 		      ^ parity[(w>>8) & 0xFF] ^ parity[w & 0xFF]);
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 	return p;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun struct dc_state {
446*4882a593Smuzhiyun 	unsigned char val;
447*4882a593Smuzhiyun 	char *name;
448*4882a593Smuzhiyun };
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun static struct dc_state dc_states[] = {
451*4882a593Smuzhiyun 	{ 0x00, "INVALID" },
452*4882a593Smuzhiyun 	{ 0x0f, "COH-SHD" },
453*4882a593Smuzhiyun 	{ 0x13, "NCO-E-C" },
454*4882a593Smuzhiyun 	{ 0x19, "NCO-E-D" },
455*4882a593Smuzhiyun 	{ 0x16, "COH-E-C" },
456*4882a593Smuzhiyun 	{ 0x1c, "COH-E-D" },
457*4882a593Smuzhiyun 	{ 0xff, "*ERROR*" }
458*4882a593Smuzhiyun };
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun #define DC_TAG_VALID(state) \
461*4882a593Smuzhiyun     (((state) == 0x0) || ((state) == 0xf) || ((state) == 0x13) || \
462*4882a593Smuzhiyun      ((state) == 0x19) || ((state) == 0x16) || ((state) == 0x1c))
463*4882a593Smuzhiyun 
dc_state_str(unsigned char state)464*4882a593Smuzhiyun static char *dc_state_str(unsigned char state)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	struct dc_state *dsc = dc_states;
467*4882a593Smuzhiyun 	while (dsc->val != 0xff) {
468*4882a593Smuzhiyun 		if (dsc->val == state)
469*4882a593Smuzhiyun 			break;
470*4882a593Smuzhiyun 		dsc++;
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 	return dsc->name;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun 
extract_dc(unsigned short addr,int data)475*4882a593Smuzhiyun static uint32_t extract_dc(unsigned short addr, int data)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun 	int valid, way;
478*4882a593Smuzhiyun 	unsigned char state;
479*4882a593Smuzhiyun 	uint32_t taghi, taglolo, taglohi;
480*4882a593Smuzhiyun 	unsigned long long taglo, pa;
481*4882a593Smuzhiyun 	uint8_t ecc, lru;
482*4882a593Smuzhiyun 	int res = 0;
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	printk("Dcache index 0x%04x  ", addr);
485*4882a593Smuzhiyun 	for (way = 0; way < 4; way++) {
486*4882a593Smuzhiyun 		__asm__ __volatile__ (
487*4882a593Smuzhiyun 		"	.set	push\n\t"
488*4882a593Smuzhiyun 		"	.set	noreorder\n\t"
489*4882a593Smuzhiyun 		"	.set	mips64\n\t"
490*4882a593Smuzhiyun 		"	.set	noat\n\t"
491*4882a593Smuzhiyun 		"	cache	5, 0(%3)\n\t"	/* Index-load-tag-D */
492*4882a593Smuzhiyun 		"	mfc0	%0, $29, 2\n\t"
493*4882a593Smuzhiyun 		"	dmfc0	$1, $28, 2\n\t"
494*4882a593Smuzhiyun 		"	dsrl32	%1, $1, 0\n\t"
495*4882a593Smuzhiyun 		"	sll	%2, $1, 0\n\t"
496*4882a593Smuzhiyun 		"	.set	pop"
497*4882a593Smuzhiyun 		: "=r" (taghi), "=r" (taglohi), "=r" (taglolo)
498*4882a593Smuzhiyun 		: "r" ((way << 13) | addr));
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 		taglo = ((unsigned long long)taglohi << 32) | taglolo;
501*4882a593Smuzhiyun 		pa = (taglo & 0xFFFFFFE000ULL) | addr;
502*4882a593Smuzhiyun 		if (way == 0) {
503*4882a593Smuzhiyun 			lru = (taghi >> 14) & 0xff;
504*4882a593Smuzhiyun 			printk("[Bank %d Set 0x%02x]  LRU > %d %d %d %d > MRU\n",
505*4882a593Smuzhiyun 				    ((addr >> 11) & 0x2) | ((addr >> 5) & 1), /* bank */
506*4882a593Smuzhiyun 				    ((addr >> 6) & 0x3f), /* index */
507*4882a593Smuzhiyun 				    (lru & 0x3),
508*4882a593Smuzhiyun 				    ((lru >> 2) & 0x3),
509*4882a593Smuzhiyun 				    ((lru >> 4) & 0x3),
510*4882a593Smuzhiyun 				    ((lru >> 6) & 0x3));
511*4882a593Smuzhiyun 		}
512*4882a593Smuzhiyun 		state = (taghi >> 25) & 0x1f;
513*4882a593Smuzhiyun 		valid = DC_TAG_VALID(state);
514*4882a593Smuzhiyun 		printk(" %d  [PA %010llx]  [state %s (%02x)]  raw tags: %08X-%016llX\n",
515*4882a593Smuzhiyun 			    way, pa, dc_state_str(state), state, taghi, taglo);
516*4882a593Smuzhiyun 		if (valid) {
517*4882a593Smuzhiyun 			if (((taglo >> 11) & 1) ^ range_parity(taglo, 39, 26)) {
518*4882a593Smuzhiyun 				printk("   ** bad parity in PTag1\n");
519*4882a593Smuzhiyun 				res |= CP0_CERRD_TAG_ADDRESS;
520*4882a593Smuzhiyun 			}
521*4882a593Smuzhiyun 			if (((taglo >> 10) & 1) ^ range_parity(taglo, 25, 13)) {
522*4882a593Smuzhiyun 				printk("   ** bad parity in PTag0\n");
523*4882a593Smuzhiyun 				res |= CP0_CERRD_TAG_ADDRESS;
524*4882a593Smuzhiyun 			}
525*4882a593Smuzhiyun 		} else {
526*4882a593Smuzhiyun 			res |= CP0_CERRD_TAG_STATE;
527*4882a593Smuzhiyun 		}
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 		if (data) {
530*4882a593Smuzhiyun 			uint32_t datalohi, datalolo, datahi;
531*4882a593Smuzhiyun 			unsigned long long datalo;
532*4882a593Smuzhiyun 			int offset;
533*4882a593Smuzhiyun 			char bad_ecc = 0;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 			for (offset = 0; offset < 4; offset++) {
536*4882a593Smuzhiyun 				/* Index-load-data-D */
537*4882a593Smuzhiyun 				__asm__ __volatile__ (
538*4882a593Smuzhiyun 				"	.set	push\n\t"
539*4882a593Smuzhiyun 				"	.set	noreorder\n\t"
540*4882a593Smuzhiyun 				"	.set	mips64\n\t"
541*4882a593Smuzhiyun 				"	.set	noat\n\t"
542*4882a593Smuzhiyun 				"	cache	7, 0(%3)\n\t" /* Index-load-data-D */
543*4882a593Smuzhiyun 				"	mfc0	%0, $29, 3\n\t"
544*4882a593Smuzhiyun 				"	dmfc0	$1, $28, 3\n\t"
545*4882a593Smuzhiyun 				"	dsrl32	%1, $1, 0 \n\t"
546*4882a593Smuzhiyun 				"	sll	%2, $1, 0 \n\t"
547*4882a593Smuzhiyun 				"	.set	pop"
548*4882a593Smuzhiyun 				: "=r" (datahi), "=r" (datalohi), "=r" (datalolo)
549*4882a593Smuzhiyun 				: "r" ((way << 13) | addr | (offset << 3)));
550*4882a593Smuzhiyun 				datalo = ((unsigned long long)datalohi << 32) | datalolo;
551*4882a593Smuzhiyun 				ecc = dc_ecc(datalo);
552*4882a593Smuzhiyun 				if (ecc != datahi) {
553*4882a593Smuzhiyun 					int bits;
554*4882a593Smuzhiyun 					bad_ecc |= 1 << (3-offset);
555*4882a593Smuzhiyun 					ecc ^= datahi;
556*4882a593Smuzhiyun 					bits = hweight8(ecc);
557*4882a593Smuzhiyun 					res |= (bits == 1) ? CP0_CERRD_DATA_SBE : CP0_CERRD_DATA_DBE;
558*4882a593Smuzhiyun 				}
559*4882a593Smuzhiyun 				printk("  %02X-%016llX", datahi, datalo);
560*4882a593Smuzhiyun 			}
561*4882a593Smuzhiyun 			printk("\n");
562*4882a593Smuzhiyun 			if (bad_ecc)
563*4882a593Smuzhiyun 				printk("  dwords w/ bad ECC: %d %d %d %d\n",
564*4882a593Smuzhiyun 				       !!(bad_ecc & 8), !!(bad_ecc & 4),
565*4882a593Smuzhiyun 				       !!(bad_ecc & 2), !!(bad_ecc & 1));
566*4882a593Smuzhiyun 		}
567*4882a593Smuzhiyun 	}
568*4882a593Smuzhiyun 	return res;
569*4882a593Smuzhiyun }
570