xref: /illumos-gate/usr/src/uts/sun4u/cpu/us3_cheetahplus.c (revision b0fe7b8fa79924061f3bdf7f240ea116c2c0b704)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/systm.h>
30 #include <sys/ddi.h>
31 #include <sys/sysmacros.h>
32 #include <sys/archsystm.h>
33 #include <sys/vmsystm.h>
34 #include <sys/machparam.h>
35 #include <sys/machsystm.h>
36 #include <sys/machthread.h>
37 #include <sys/cpu.h>
38 #include <sys/cmp.h>
39 #include <sys/elf_SPARC.h>
40 #include <vm/hat_sfmmu.h>
41 #include <vm/seg_kmem.h>
42 #include <sys/cpuvar.h>
43 #include <sys/cheetahregs.h>
44 #include <sys/us3_module.h>
45 #include <sys/async.h>
46 #include <sys/cmn_err.h>
47 #include <sys/debug.h>
48 #include <sys/dditypes.h>
49 #include <sys/prom_debug.h>
50 #include <sys/prom_plat.h>
51 #include <sys/cpu_module.h>
52 #include <sys/sysmacros.h>
53 #include <sys/intreg.h>
54 #include <sys/clock.h>
55 #include <sys/platform_module.h>
56 #include <sys/machtrap.h>
57 #include <sys/ontrap.h>
58 #include <sys/panic.h>
59 #include <sys/memlist.h>
60 #include <sys/bootconf.h>
61 #include <sys/ivintr.h>
62 #include <sys/atomic.h>
63 #include <sys/fm/protocol.h>
64 #include <sys/fm/cpu/UltraSPARC-III.h>
65 #include <sys/fm/util.h>
66 #include <sys/pghw.h>
67 
68 #ifdef	CHEETAHPLUS_ERRATUM_25
69 #include <sys/cyclic.h>
70 #endif	/* CHEETAHPLUS_ERRATUM_25 */
71 
72 /*
73  * See comment above cpu_scrub_cpu_setup() for description
74  */
75 #define	SCRUBBER_NEITHER_CORE_ONLINE	0x0
76 #define	SCRUBBER_CORE_0_ONLINE		0x1
77 #define	SCRUBBER_CORE_1_ONLINE		0x2
78 #define	SCRUBBER_BOTH_CORES_ONLINE	(SCRUBBER_CORE_0_ONLINE | \
79 					SCRUBBER_CORE_1_ONLINE)
80 
81 static int pn_matching_valid_l2_line(uint64_t faddr, ch_ec_data_t *clo_l2_data);
82 static void cpu_async_log_tlb_parity_err(void *flt);
83 static cpu_t *cpu_get_sibling_core(cpu_t *cpup);
84 
85 
86 /*
87  * Setup trap handlers.
88  */
89 void
90 cpu_init_trap(void)
91 {
92 	CH_SET_TRAP(tt_pil15, ch_pil15_interrupt_instr);
93 
94 	CH_SET_TRAP(tt0_fecc, fecc_err_instr);
95 	CH_SET_TRAP(tt1_fecc, fecc_err_tl1_instr);
96 	CH_SET_TRAP(tt1_swtrap0, fecc_err_tl1_cont_instr);
97 
98 	CH_SET_TRAP(tt0_dperr, dcache_parity_instr);
99 	CH_SET_TRAP(tt1_dperr, dcache_parity_tl1_instr);
100 	CH_SET_TRAP(tt1_swtrap1, dcache_parity_tl1_cont_instr);
101 
102 	CH_SET_TRAP(tt0_iperr, icache_parity_instr);
103 	CH_SET_TRAP(tt1_iperr, icache_parity_tl1_instr);
104 	CH_SET_TRAP(tt1_swtrap2, icache_parity_tl1_cont_instr);
105 }
106 
107 /*
108  * Set the magic constants of the implementation.
109  */
110 /*ARGSUSED*/
111 void
112 cpu_fiximp(pnode_t dnode)
113 {
114 	int i, a;
115 	extern int vac_size, vac_shift;
116 	extern uint_t vac_mask;
117 
118 	dcache_size = CH_DCACHE_SIZE;
119 	dcache_linesize = CH_DCACHE_LSIZE;
120 
121 	icache_size = CHP_ICACHE_MAX_SIZE;
122 	icache_linesize = CHP_ICACHE_MIN_LSIZE;
123 
124 	ecache_size = CH_ECACHE_MAX_SIZE;
125 	ecache_alignsize = CH_ECACHE_MAX_LSIZE;
126 	ecache_associativity = CHP_ECACHE_MIN_NWAY;
127 
128 	/*
129 	 * ecache_setsize needs to maximum of all cpu ecache setsizes
130 	 */
131 	ecache_setsize = CHP_ECACHE_MAX_SETSIZE;
132 	ASSERT(ecache_setsize >= (ecache_size / ecache_associativity));
133 
134 	vac_size = CH_VAC_SIZE;
135 	vac_mask = MMU_PAGEMASK & (vac_size - 1);
136 	i = 0; a = vac_size;
137 	while (a >>= 1)
138 		++i;
139 	vac_shift = i;
140 	shm_alignment = vac_size;
141 	vac = 1;
142 }
143 
144 /*
145  * Use Panther values for Panther-only domains.
146  * See Panther PRM, 1.5.4 Cache Hierarchy
147  */
148 void
149 cpu_fix_allpanther(void)
150 {
151 	/* dcache same as Ch+ */
152 	icache_size = PN_ICACHE_SIZE;
153 	icache_linesize = PN_ICACHE_LSIZE;
154 	ecache_size = PN_L3_SIZE;
155 	ecache_alignsize = PN_L3_LINESIZE;
156 	ecache_associativity = PN_L3_NWAYS;
157 	ecache_setsize = PN_L3_SET_SIZE;
158 	ASSERT(ecache_setsize >= (ecache_size / ecache_associativity));
159 	/* vac same as Ch+ */
160 	/* fix hwcaps for USIV+-only domains */
161 	cpu_hwcap_flags |= AV_SPARC_POPC;
162 }
163 
164 void
165 send_mondo_set(cpuset_t set)
166 {
167 	int lo, busy, nack, shipped = 0;
168 	uint16_t i, cpuids[IDSR_BN_SETS];
169 	uint64_t idsr, nackmask = 0, busymask, curnack, curbusy;
170 	uint64_t starttick, endtick, tick, lasttick;
171 #if (NCPU > IDSR_BN_SETS)
172 	int index = 0;
173 	int ncpuids = 0;
174 #endif
175 #ifdef	CHEETAHPLUS_ERRATUM_25
176 	int recovered = 0;
177 	int cpuid;
178 #endif
179 
180 	ASSERT(!CPUSET_ISNULL(set));
181 	starttick = lasttick = gettick();
182 
183 #if (NCPU <= IDSR_BN_SETS)
184 	for (i = 0; i < NCPU; i++)
185 		if (CPU_IN_SET(set, i)) {
186 			shipit(i, shipped);
187 			nackmask |= IDSR_NACK_BIT(shipped);
188 			cpuids[shipped++] = i;
189 			CPUSET_DEL(set, i);
190 			if (CPUSET_ISNULL(set))
191 				break;
192 		}
193 	CPU_STATS_ADDQ(CPU, sys, xcalls, shipped);
194 #else
195 	for (i = 0; i < NCPU; i++)
196 		if (CPU_IN_SET(set, i)) {
197 			ncpuids++;
198 
199 			/*
200 			 * Ship only to the first (IDSR_BN_SETS) CPUs.  If we
201 			 * find we have shipped to more than (IDSR_BN_SETS)
202 			 * CPUs, set "index" to the highest numbered CPU in
203 			 * the set so we can ship to other CPUs a bit later on.
204 			 */
205 			if (shipped < IDSR_BN_SETS) {
206 				shipit(i, shipped);
207 				nackmask |= IDSR_NACK_BIT(shipped);
208 				cpuids[shipped++] = i;
209 				CPUSET_DEL(set, i);
210 				if (CPUSET_ISNULL(set))
211 					break;
212 			} else
213 				index = (int)i;
214 		}
215 
216 	CPU_STATS_ADDQ(CPU, sys, xcalls, ncpuids);
217 #endif
218 
219 	busymask = IDSR_NACK_TO_BUSY(nackmask);
220 	busy = nack = 0;
221 	endtick = starttick + xc_tick_limit;
222 	for (;;) {
223 		idsr = getidsr();
224 #if (NCPU <= IDSR_BN_SETS)
225 		if (idsr == 0)
226 			break;
227 #else
228 		if (idsr == 0 && shipped == ncpuids)
229 			break;
230 #endif
231 		tick = gettick();
232 		/*
233 		 * If there is a big jump between the current tick
234 		 * count and lasttick, we have probably hit a break
235 		 * point.  Adjust endtick accordingly to avoid panic.
236 		 */
237 		if (tick > (lasttick + xc_tick_jump_limit))
238 			endtick += (tick - lasttick);
239 		lasttick = tick;
240 		if (tick > endtick) {
241 			if (panic_quiesce)
242 				return;
243 #ifdef	CHEETAHPLUS_ERRATUM_25
244 			cpuid = -1;
245 			for (i = 0; i < IDSR_BN_SETS; i++) {
246 				if (idsr & (IDSR_NACK_BIT(i) |
247 				    IDSR_BUSY_BIT(i))) {
248 					cpuid = cpuids[i];
249 					break;
250 				}
251 			}
252 			if (cheetah_sendmondo_recover && cpuid != -1 &&
253 			    recovered == 0) {
254 				if (mondo_recover(cpuid, i)) {
255 					/*
256 					 * We claimed the whole memory or
257 					 * full scan is disabled.
258 					 */
259 					recovered++;
260 				}
261 				tick = gettick();
262 				endtick = tick + xc_tick_limit;
263 				lasttick = tick;
264 				/*
265 				 * Recheck idsr
266 				 */
267 				continue;
268 			} else
269 #endif	/* CHEETAHPLUS_ERRATUM_25 */
270 			{
271 				cmn_err(CE_CONT, "send mondo timeout "
272 				    "[%d NACK %d BUSY]\nIDSR 0x%"
273 				    "" PRIx64 "  cpuids:", nack, busy, idsr);
274 				for (i = 0; i < IDSR_BN_SETS; i++) {
275 					if (idsr & (IDSR_NACK_BIT(i) |
276 					    IDSR_BUSY_BIT(i))) {
277 						cmn_err(CE_CONT, " 0x%x",
278 						    cpuids[i]);
279 					}
280 				}
281 				cmn_err(CE_CONT, "\n");
282 				cmn_err(CE_PANIC, "send_mondo_set: timeout");
283 			}
284 		}
285 		curnack = idsr & nackmask;
286 		curbusy = idsr & busymask;
287 #if (NCPU > IDSR_BN_SETS)
288 		if (shipped < ncpuids) {
289 			uint64_t cpus_left;
290 			uint16_t next = (uint16_t)index;
291 
292 			cpus_left = ~(IDSR_NACK_TO_BUSY(curnack) | curbusy) &
293 				busymask;
294 
295 			if (cpus_left) {
296 				do {
297 					/*
298 					 * Sequence through and ship to the
299 					 * remainder of the CPUs in the system
300 					 * (e.g. other than the first
301 					 * (IDSR_BN_SETS)) in reverse order.
302 					 */
303 					lo = lowbit(cpus_left) - 1;
304 					i = IDSR_BUSY_IDX(lo);
305 					shipit(next, i);
306 					shipped++;
307 					cpuids[i] = next;
308 
309 					/*
310 					 * If we've processed all the CPUs,
311 					 * exit the loop now and save
312 					 * instructions.
313 					 */
314 					if (shipped == ncpuids)
315 						break;
316 
317 					for ((index = ((int)next - 1));
318 					    index >= 0; index--)
319 						if (CPU_IN_SET(set, index)) {
320 							next = (uint16_t)index;
321 							break;
322 						}
323 
324 					cpus_left &= ~(1ull << lo);
325 				} while (cpus_left);
326 #ifdef	CHEETAHPLUS_ERRATUM_25
327 				/*
328 				 * Clear recovered because we are sending to
329 				 * a new set of targets.
330 				 */
331 				recovered = 0;
332 #endif
333 				continue;
334 			}
335 		}
336 #endif
337 		if (curbusy) {
338 			busy++;
339 			continue;
340 		}
341 
342 #ifdef SEND_MONDO_STATS
343 		{
344 			int n = gettick() - starttick;
345 			if (n < 8192)
346 				x_nack_stimes[n >> 7]++;
347 		}
348 #endif
349 		while (gettick() < (tick + sys_clock_mhz))
350 			;
351 		do {
352 			lo = lowbit(curnack) - 1;
353 			i = IDSR_NACK_IDX(lo);
354 			shipit(cpuids[i], i);
355 			curnack &= ~(1ull << lo);
356 		} while (curnack);
357 		nack++;
358 		busy = 0;
359 	}
360 #ifdef SEND_MONDO_STATS
361 	{
362 		int n = gettick() - starttick;
363 		if (n < 8192)
364 			x_set_stimes[n >> 7]++;
365 		else
366 			x_set_ltimes[(n >> 13) & 0xf]++;
367 	}
368 	x_set_cpus[shipped]++;
369 #endif
370 }
371 
372 /*
373  * Handles error logging for implementation specific error types
374  */
375 /*ARGSUSED1*/
376 int
377 cpu_impl_async_log_err(void *flt, errorq_elem_t *eqep)
378 {
379 	ch_async_flt_t *ch_flt = (ch_async_flt_t *)flt;
380 	struct async_flt *aflt = (struct async_flt *)flt;
381 
382 	switch (ch_flt->flt_type) {
383 
384 	case CPU_IC_PARITY:
385 		cpu_async_log_ic_parity_err(flt);
386 		return (CH_ASYNC_LOG_DONE);
387 
388 	case CPU_DC_PARITY:
389 		cpu_async_log_dc_parity_err(flt);
390 		return (CH_ASYNC_LOG_DONE);
391 
392 	case CPU_DUE:
393 		cpu_log_err(aflt);
394 		cpu_page_retire(ch_flt);
395 		return (CH_ASYNC_LOG_DONE);
396 
397 	case CPU_ITLB_PARITY:
398 	case CPU_DTLB_PARITY:
399 		cpu_async_log_tlb_parity_err(flt);
400 		return (CH_ASYNC_LOG_DONE);
401 
402 	/* report the error and continue */
403 	case CPU_L3_ADDR_PE:
404 		cpu_log_err(aflt);
405 		return (CH_ASYNC_LOG_DONE);
406 
407 	default:
408 		return (CH_ASYNC_LOG_UNKNOWN);
409 	}
410 }
411 
412 /*
413  * Figure out if Ecache is direct-mapped (Cheetah or Cheetah+ with Ecache
414  * control ECCR_ASSOC bit off or 2-way (Cheetah+ with ECCR_ASSOC on).
415  * We need to do this on the fly because we may have mixed Cheetah+'s with
416  * both direct and 2-way Ecaches. Panther only supports 4-way L3$.
417  */
418 int
419 cpu_ecache_nway(void)
420 {
421 	if (IS_PANTHER(cpunodes[CPU->cpu_id].implementation))
422 		return (PN_L3_NWAYS);
423 	return ((get_ecache_ctrl() & ECCR_ASSOC) ? 2 : 1);
424 }
425 
426 /*
427  * Note that these are entered into the table: Fatal Errors (PERR, IERR, ISAP,
428  * EMU, IMU) first, orphaned UCU/UCC, AFAR Overwrite policy, finally IVU, IVC.
429  * Afar overwrite policy is:
430  *   Class 4:
431  *      AFSR     -- UCC, UCU, TUE, TSCE, TUE_SH
432  *      AFSR_EXT -- L3_UCC, L3_UCU, L3_TUE, L3_TUE_SH
433  *   Class 3:
434  *      AFSR     -- UE, DUE, EDU, WDU, CPU
435  *      AFSR_EXT -- L3_EDU, L3_WDU, L3_CPU
436  *   Class 2:
437  *      AFSR     -- CE, EDC, EMC, WDC, CPC, THCE
438  *      AFSR_EXT -- L3_EDC, L3_WDC, L3_CPC, L3_THCE
439  *   Class 1:
440  *      AFSR     -- TO, DTO, BERR, DBERR
441  */
442 ecc_type_to_info_t ecc_type_to_info[] = {
443 
444 	/* Fatal Errors */
445 	C_AFSR_PERR,		"PERR ",	ECC_ALL_TRAPS,
446 		CPU_FATAL,	"PERR Fatal",
447 		FM_EREPORT_PAYLOAD_SYSTEM2,
448 		FM_EREPORT_CPU_USIII_PERR,
449 	C_AFSR_IERR,		"IERR ", 	ECC_ALL_TRAPS,
450 		CPU_FATAL,	"IERR Fatal",
451 		FM_EREPORT_PAYLOAD_SYSTEM2,
452 		FM_EREPORT_CPU_USIII_IERR,
453 	C_AFSR_ISAP,		"ISAP ",	ECC_ALL_TRAPS,
454 		CPU_FATAL,	"ISAP Fatal",
455 		FM_EREPORT_PAYLOAD_SYSTEM1,
456 		FM_EREPORT_CPU_USIII_ISAP,
457 	C_AFSR_L3_TUE_SH,	"L3_TUE_SH ", 	ECC_C_TRAP,
458 		CPU_FATAL,	"L3_TUE_SH Fatal",
459 		FM_EREPORT_PAYLOAD_L3_TAG_ECC,
460 		FM_EREPORT_CPU_USIII_L3_TUE_SH,
461 	C_AFSR_L3_TUE,		"L3_TUE ", 	ECC_C_TRAP,
462 		CPU_FATAL,	"L3_TUE Fatal",
463 		FM_EREPORT_PAYLOAD_L3_TAG_ECC,
464 		FM_EREPORT_CPU_USIII_L3_TUE,
465 	C_AFSR_TUE_SH,		"TUE_SH ", 	ECC_C_TRAP,
466 		CPU_FATAL,	"TUE_SH Fatal",
467 		FM_EREPORT_PAYLOAD_L2_TAG_ECC,
468 		FM_EREPORT_CPU_USIII_TUE_SH,
469 	C_AFSR_TUE,		"TUE ", 	ECC_ALL_TRAPS,
470 		CPU_FATAL,	"TUE Fatal",
471 		FM_EREPORT_PAYLOAD_L2_TAG_ECC,
472 		FM_EREPORT_CPU_USIII_TUE,
473 	C_AFSR_EMU,		"EMU ",		ECC_ASYNC_TRAPS,
474 		CPU_FATAL,	"EMU Fatal",
475 		FM_EREPORT_PAYLOAD_MEMORY,
476 		FM_EREPORT_CPU_USIII_EMU,
477 	C_AFSR_IMU,		"IMU ",		ECC_C_TRAP,
478 		CPU_FATAL,	"IMU Fatal",
479 		FM_EREPORT_PAYLOAD_SYSTEM1,
480 		FM_EREPORT_CPU_USIII_IMU,
481 
482 	/* L3$ Address parity errors are reported via the MECC bit */
483 	C_AFSR_L3_MECC,		"L3_MECC ",	ECC_MECC_TRAPS,
484 		CPU_L3_ADDR_PE,	"L3 Address Parity",
485 		FM_EREPORT_PAYLOAD_L3_DATA,
486 		FM_EREPORT_CPU_USIII_L3_MECC,
487 
488 	/* Orphaned UCC/UCU Errors */
489 	C_AFSR_L3_UCU,		"L3_OUCU ",	ECC_ORPH_TRAPS,
490 		CPU_ORPH,	"Orphaned L3_UCU",
491 		FM_EREPORT_PAYLOAD_L3_DATA,
492 		FM_EREPORT_CPU_USIII_L3_UCU,
493 	C_AFSR_L3_UCC,		"L3_OUCC ",	ECC_ORPH_TRAPS,
494 		CPU_ORPH,	"Orphaned L3_UCC",
495 		FM_EREPORT_PAYLOAD_L3_DATA,
496 		FM_EREPORT_CPU_USIII_L3_UCC,
497 	C_AFSR_UCU,		"OUCU ",	ECC_ORPH_TRAPS,
498 		CPU_ORPH,	"Orphaned UCU",
499 		FM_EREPORT_PAYLOAD_L2_DATA,
500 		FM_EREPORT_CPU_USIII_UCU,
501 	C_AFSR_UCC,		"OUCC ",	ECC_ORPH_TRAPS,
502 		CPU_ORPH,	"Orphaned UCC",
503 		FM_EREPORT_PAYLOAD_L2_DATA,
504 		FM_EREPORT_CPU_USIII_UCC,
505 
506 	/* UCU, UCC */
507 	C_AFSR_L3_UCU,		"L3_UCU ",	ECC_F_TRAP,
508 		CPU_UE_ECACHE,	"L3_UCU",
509 		FM_EREPORT_PAYLOAD_L3_DATA,
510 		FM_EREPORT_CPU_USIII_L3_UCU,
511 	C_AFSR_L3_UCC,		"L3_UCC ",	ECC_F_TRAP,
512 		CPU_CE_ECACHE,	"L3_UCC",
513 		FM_EREPORT_PAYLOAD_L3_DATA,
514 		FM_EREPORT_CPU_USIII_L3_UCC,
515 	C_AFSR_UCU,		"UCU ",		ECC_F_TRAP,
516 		CPU_UE_ECACHE,	"UCU",
517 		FM_EREPORT_PAYLOAD_L2_DATA,
518 		FM_EREPORT_CPU_USIII_UCU,
519 	C_AFSR_UCC,		"UCC ",		ECC_F_TRAP,
520 		CPU_CE_ECACHE,	"UCC",
521 		FM_EREPORT_PAYLOAD_L2_DATA,
522 		FM_EREPORT_CPU_USIII_UCC,
523 	C_AFSR_TSCE,		"TSCE ",	ECC_F_TRAP,
524 		CPU_CE_ECACHE,	"TSCE",
525 		FM_EREPORT_PAYLOAD_L2_TAG_ECC,
526 		FM_EREPORT_CPU_USIII_TSCE,
527 
528 	/* UE, EDU:ST, EDU:BLD, WDU, CPU */
529 	C_AFSR_UE,		"UE ",		ECC_ASYNC_TRAPS,
530 		CPU_UE,		"Uncorrectable system bus (UE)",
531 		FM_EREPORT_PAYLOAD_MEMORY,
532 		FM_EREPORT_CPU_USIII_UE,
533 	C_AFSR_L3_EDU,		"L3_EDU ",	ECC_C_TRAP,
534 		CPU_UE_ECACHE_RETIRE,	"L3_EDU:ST",
535 		FM_EREPORT_PAYLOAD_L3_DATA,
536 		FM_EREPORT_CPU_USIII_L3_EDUST,
537 	C_AFSR_L3_EDU,		"L3_EDU ",	ECC_D_TRAP,
538 		CPU_UE_ECACHE_RETIRE,	"L3_EDU:BLD",
539 		FM_EREPORT_PAYLOAD_L3_DATA,
540 		FM_EREPORT_CPU_USIII_L3_EDUBL,
541 	C_AFSR_L3_WDU,		"L3_WDU ",	ECC_C_TRAP,
542 		CPU_UE_ECACHE_RETIRE,	"L3_WDU",
543 		FM_EREPORT_PAYLOAD_L3_DATA,
544 		FM_EREPORT_CPU_USIII_L3_WDU,
545 	C_AFSR_L3_CPU,		"L3_CPU ",	ECC_C_TRAP,
546 		CPU_UE_ECACHE,	"L3_CPU",
547 		FM_EREPORT_PAYLOAD_L3_DATA,
548 		FM_EREPORT_CPU_USIII_L3_CPU,
549 	C_AFSR_EDU,		"EDU ",		ECC_C_TRAP,
550 		CPU_UE_ECACHE_RETIRE,	"EDU:ST",
551 		FM_EREPORT_PAYLOAD_L2_DATA,
552 		FM_EREPORT_CPU_USIII_EDUST,
553 	C_AFSR_EDU,		"EDU ",		ECC_D_TRAP,
554 		CPU_UE_ECACHE_RETIRE,	"EDU:BLD",
555 		FM_EREPORT_PAYLOAD_L2_DATA,
556 		FM_EREPORT_CPU_USIII_EDUBL,
557 	C_AFSR_WDU,		"WDU ",		ECC_C_TRAP,
558 		CPU_UE_ECACHE_RETIRE,	"WDU",
559 		FM_EREPORT_PAYLOAD_L2_DATA,
560 		FM_EREPORT_CPU_USIII_WDU,
561 	C_AFSR_CPU,		"CPU ",		ECC_C_TRAP,
562 		CPU_UE_ECACHE,	"CPU",
563 		FM_EREPORT_PAYLOAD_L2_DATA,
564 		FM_EREPORT_CPU_USIII_CPU,
565 	C_AFSR_DUE,		"DUE ",		ECC_C_TRAP,
566 		CPU_DUE,	"DUE",
567 		FM_EREPORT_PAYLOAD_MEMORY,
568 		FM_EREPORT_CPU_USIII_DUE,
569 
570 	/* CE, EDC, EMC, WDC, CPC */
571 	C_AFSR_CE,		"CE ",		ECC_C_TRAP,
572 		CPU_CE,		"Corrected system bus (CE)",
573 		FM_EREPORT_PAYLOAD_MEMORY,
574 		FM_EREPORT_CPU_USIII_CE,
575 	C_AFSR_L3_EDC,		"L3_EDC ",	ECC_C_TRAP,
576 		CPU_CE_ECACHE,	"L3_EDC",
577 		FM_EREPORT_PAYLOAD_L3_DATA,
578 		FM_EREPORT_CPU_USIII_L3_EDC,
579 	C_AFSR_EDC,		"EDC ",		ECC_C_TRAP,
580 		CPU_CE_ECACHE,	"EDC",
581 		FM_EREPORT_PAYLOAD_L2_DATA,
582 		FM_EREPORT_CPU_USIII_EDC,
583 	C_AFSR_EMC,		"EMC ",		ECC_C_TRAP,
584 		CPU_EMC,	"EMC",
585 		FM_EREPORT_PAYLOAD_MEMORY,
586 		FM_EREPORT_CPU_USIII_EMC,
587 	C_AFSR_L3_WDC,		"L3_WDC ",	ECC_C_TRAP,
588 		CPU_CE_ECACHE,	"L3_WDC",
589 		FM_EREPORT_PAYLOAD_L3_DATA,
590 		FM_EREPORT_CPU_USIII_L3_WDC,
591 	C_AFSR_L3_CPC,		"L3_CPC ",	ECC_C_TRAP,
592 		CPU_CE_ECACHE,	"L3_CPC",
593 		FM_EREPORT_PAYLOAD_L3_DATA,
594 		FM_EREPORT_CPU_USIII_L3_CPC,
595 	C_AFSR_L3_THCE,		"L3_THCE ",	ECC_C_TRAP,
596 		CPU_CE_ECACHE,	"L3_THCE",
597 		FM_EREPORT_PAYLOAD_L3_TAG_ECC,
598 		FM_EREPORT_CPU_USIII_L3_THCE,
599 	C_AFSR_WDC,		"WDC ",		ECC_C_TRAP,
600 		CPU_CE_ECACHE,	"WDC",
601 		FM_EREPORT_PAYLOAD_L2_DATA,
602 		FM_EREPORT_CPU_USIII_WDC,
603 	C_AFSR_CPC,		"CPC ",		ECC_C_TRAP,
604 		CPU_CE_ECACHE,	"CPC",
605 		FM_EREPORT_PAYLOAD_L2_DATA,
606 		FM_EREPORT_CPU_USIII_CPC,
607 	C_AFSR_THCE,		"THCE ",	ECC_C_TRAP,
608 		CPU_CE_ECACHE,	"THCE",
609 		FM_EREPORT_PAYLOAD_L2_TAG_ECC,
610 		FM_EREPORT_CPU_USIII_THCE,
611 
612 	/* TO, BERR */
613 	C_AFSR_TO,		"TO ",		ECC_ASYNC_TRAPS,
614 		CPU_TO,		"Timeout (TO)",
615 		FM_EREPORT_PAYLOAD_IO,
616 		FM_EREPORT_CPU_USIII_TO,
617 	C_AFSR_BERR,		"BERR ",	ECC_ASYNC_TRAPS,
618 		CPU_BERR,	"Bus Error (BERR)",
619 		FM_EREPORT_PAYLOAD_IO,
620 		FM_EREPORT_CPU_USIII_BERR,
621 	C_AFSR_DTO,		"DTO ",		ECC_C_TRAP,
622 		CPU_TO,		"Disrupting Timeout (DTO)",
623 		FM_EREPORT_PAYLOAD_IO,
624 		FM_EREPORT_CPU_USIII_DTO,
625 	C_AFSR_DBERR,		"DBERR ",	ECC_C_TRAP,
626 		CPU_BERR,	"Disrupting Bus Error (DBERR)",
627 		FM_EREPORT_PAYLOAD_IO,
628 		FM_EREPORT_CPU_USIII_DBERR,
629 
630 	/* IVU, IVC, IMC */
631 	C_AFSR_IVU,		"IVU ",		ECC_C_TRAP,
632 		CPU_IV,		"IVU",
633 		FM_EREPORT_PAYLOAD_SYSTEM1,
634 		FM_EREPORT_CPU_USIII_IVU,
635 	C_AFSR_IVC,		"IVC ",		ECC_C_TRAP,
636 		CPU_IV,		"IVC",
637 		FM_EREPORT_PAYLOAD_SYSTEM1,
638 		FM_EREPORT_CPU_USIII_IVC,
639 	C_AFSR_IMC,		"IMC ",		ECC_C_TRAP,
640 		CPU_IV,		"IMC",
641 		FM_EREPORT_PAYLOAD_SYSTEM1,
642 		FM_EREPORT_CPU_USIII_IMC,
643 
644 	0,			NULL,		0,
645 		0,		NULL,
646 		FM_EREPORT_PAYLOAD_UNKNOWN,
647 		FM_EREPORT_CPU_USIII_UNKNOWN,
648 };
649 
650 /*
651  * See Cheetah+ Delta PRM 10.9 and section P.6.1 of the Panther PRM
652  *   Class 4:
653  *      AFSR     -- UCC, UCU, TUE, TSCE, TUE_SH
654  *      AFSR_EXT -- L3_UCC, L3_UCU, L3_TUE, L3_TUE_SH
655  *   Class 3:
656  *      AFSR     -- UE, DUE, EDU, EMU, WDU, CPU
657  *      AFSR_EXT -- L3_EDU, L3_WDU, L3_CPU
658  *   Class 2:
659  *      AFSR     -- CE, EDC, EMC, WDC, CPC, THCE
660  *      AFSR_EXT -- L3_EDC, L3_WDC, L3_CPC, L3_THCE
661  *   Class 1:
662  *      AFSR     -- TO, DTO, BERR, DBERR
663  *      AFSR_EXT --
664  */
665 uint64_t afar_overwrite[] = {
666 	/* class 4: */
667 	C_AFSR_UCC | C_AFSR_UCU | C_AFSR_TUE | C_AFSR_TSCE | C_AFSR_TUE_SH |
668 	C_AFSR_L3_UCC | C_AFSR_L3_UCU | C_AFSR_L3_TUE | C_AFSR_L3_TUE_SH,
669 	/* class 3: */
670 	C_AFSR_UE | C_AFSR_DUE | C_AFSR_EDU | C_AFSR_EMU | C_AFSR_WDU |
671 	C_AFSR_CPU | C_AFSR_L3_EDU | C_AFSR_L3_WDU | C_AFSR_L3_CPU,
672 	/* class 2: */
673 	C_AFSR_CE | C_AFSR_EDC | C_AFSR_EMC | C_AFSR_WDC | C_AFSR_CPC |
674 	C_AFSR_THCE | C_AFSR_L3_EDC | C_AFSR_L3_WDC | C_AFSR_L3_CPC |
675 	C_AFSR_L3_THCE,
676 	/* class 1: */
677 	C_AFSR_TO | C_AFSR_DTO | C_AFSR_BERR | C_AFSR_DBERR,
678 
679 	0
680 };
681 
682 /*
683  * For Cheetah+, the E_SYND and M_SYND overwrite priorities are combined.
684  * See Cheetah+ Delta PRM 10.9 and Cheetah+ PRM 11.6.2
685  *   Class 2:  UE, DUE, IVU, EDU, EMU, WDU, UCU, CPU
686  *   Class 1:  CE, IVC, EDC, EMC, WDC, UCC, CPC
687  */
688 uint64_t esynd_overwrite[] = {
689 	/* class 2: */
690 	C_AFSR_UE | C_AFSR_DUE | C_AFSR_IVU | C_AFSR_EDU | C_AFSR_EMU |
691 	    C_AFSR_WDU | C_AFSR_UCU | C_AFSR_CPU,
692 	/* class 1: */
693 	C_AFSR_CE | C_AFSR_IVC | C_AFSR_EDC | C_AFSR_EMC | C_AFSR_WDC |
694 	    C_AFSR_UCC | C_AFSR_CPC,
695 	0
696 };
697 
698 /*
699  * In panther, the E_SYND overwrite policy changed a little bit
700  * by adding one more level.
701  * See Panther PRM P.6.2
702  *   class 3:
703  *      AFSR     -- UCU, UCC
704  *      AFSR_EXT -- L3_UCU, L3_UCC
705  *   Class 2:
706  *      AFSR     -- UE, DUE, IVU, EDU, WDU, CPU
707  *      AFSR_EXT -- L3_EDU, L3_WDU, L3_CPU
708  *   Class 1:
709  *      AFSR     -- CE, IVC, EDC, WDC, CPC
710  *      AFSR_EXT -- L3_EDC, L3_WDC, L3_CPC
711  */
712 uint64_t pn_esynd_overwrite[] = {
713 	/* class 3: */
714 	C_AFSR_UCU | C_AFSR_UCC |
715 	C_AFSR_L3_UCU | C_AFSR_L3_UCC,
716 	/* class 2: */
717 	C_AFSR_UE | C_AFSR_DUE | C_AFSR_IVU | C_AFSR_EDU | C_AFSR_WDU |
718 	    C_AFSR_CPU |
719 	C_AFSR_L3_EDU | C_AFSR_L3_WDU | C_AFSR_L3_CPU,
720 	/* class 1: */
721 	C_AFSR_CE | C_AFSR_IVC | C_AFSR_EDC | C_AFSR_WDC | C_AFSR_CPC |
722 	C_AFSR_L3_EDC | C_AFSR_L3_WDC | C_AFSR_L3_CPC,
723 
724 	0
725 };
726 
727 int
728 afsr_to_pn_esynd_status(uint64_t afsr, uint64_t afsr_bit)
729 {
730 	return (afsr_to_overw_status(afsr, afsr_bit, pn_esynd_overwrite));
731 }
732 
733 /*
734  * Prioritized list of Error bits for MSYND overwrite.
735  * See Panther PRM P.6.2 (For Cheetah+, see esynd_overwrite classes)
736  *   Class 2:  EMU, IMU
737  *   Class 1:  EMC, IMC
738  *
739  * Panther adds IMU and IMC.
740  */
741 uint64_t msynd_overwrite[] = {
742 	/* class 2: */
743 	C_AFSR_EMU | C_AFSR_IMU,
744 	/* class 1: */
745 	C_AFSR_EMC | C_AFSR_IMC,
746 
747 	0
748 };
749 
750 /*
751  * change cpu speed bits -- new speed will be normal-speed/divisor.
752  *
753  * The Jalapeno memory controllers are required to drain outstanding
754  * memory transactions within 32 JBus clocks in order to be ready
755  * to enter Estar mode.  In some corner cases however, that time
756  * fell short.
757  *
758  * A safe software solution is to force MCU to act like in Estar mode,
759  * then delay 1us (in ppm code) prior to assert J_CHNG_L signal.
760  * To reverse the effect, upon exiting Estar, software restores the
761  * MCU to its original state.
762  */
763 /* ARGSUSED1 */
764 void
765 cpu_change_speed(uint64_t divisor, uint64_t arg2)
766 {
767 	bus_config_eclk_t *bceclk;
768 	uint64_t		reg;
769 
770 	for (bceclk = bus_config_eclk; bceclk->divisor; bceclk++) {
771 		if (bceclk->divisor != divisor)
772 			continue;
773 		reg = get_safari_config();
774 		reg &= ~SAFARI_CONFIG_ECLK_MASK;
775 		reg |= bceclk->mask;
776 		set_safari_config(reg);
777 		CPU->cpu_m.divisor = (uchar_t)divisor;
778 		return;
779 	}
780 	/*
781 	 * We will reach here only if OBP and kernel don't agree on
782 	 * the speeds supported by the CPU.
783 	 */
784 	cmn_err(CE_WARN, "cpu_change_speed: bad divisor %" PRIu64, divisor);
785 }
786 
787 /*
788  * Cpu private initialization.  This includes allocating the cpu_private
789  * data structure, initializing it, and initializing the scrubber for this
790  * cpu.  This function calls cpu_init_ecache_scrub_dr to init the scrubber.
791  * We use kmem_cache_create for the cheetah private data structure because
792  * it needs to be allocated on a PAGESIZE (8192) byte boundary.
793  */
794 void
795 cpu_init_private(struct cpu *cp)
796 {
797 	cheetah_private_t *chprp;
798 	int i;
799 
800 	ASSERT(CPU_PRIVATE(cp) == NULL);
801 
802 	/* LINTED: E_TRUE_LOGICAL_EXPR */
803 	ASSERT((offsetof(cheetah_private_t, chpr_tl1_err_data) +
804 	    sizeof (ch_err_tl1_data_t) * CH_ERR_TL1_TLMAX) <= PAGESIZE);
805 
806 	/*
807 	 * Running with Cheetah CPUs in a Cheetah+, Jaguar, Panther or
808 	 * mixed Cheetah+/Jaguar/Panther machine is not a supported
809 	 * configuration. Attempting to do so may result in unpredictable
810 	 * failures (e.g. running Cheetah+ CPUs with Cheetah E$ disp flush)
811 	 * so don't allow it.
812 	 *
813 	 * This is just defensive code since this configuration mismatch
814 	 * should have been caught prior to OS execution.
815 	 */
816 	if (!(IS_CHEETAH_PLUS(cpunodes[cp->cpu_id].implementation) ||
817 	    IS_JAGUAR(cpunodes[cp->cpu_id].implementation) ||
818 	    IS_PANTHER(cpunodes[cp->cpu_id].implementation))) {
819 		cmn_err(CE_PANIC, "CPU%d: UltraSPARC-III not supported"
820 		    " on UltraSPARC-III+/IV/IV+ code\n", cp->cpu_id);
821 	}
822 
823 	/*
824 	 * If the ch_private_cache has not been created, create it.
825 	 */
826 	if (ch_private_cache == NULL) {
827 		ch_private_cache = kmem_cache_create("ch_private_cache",
828 		    sizeof (cheetah_private_t), PAGESIZE, NULL, NULL,
829 		    NULL, NULL, static_arena, 0);
830 	}
831 
832 	chprp = CPU_PRIVATE(cp) = kmem_cache_alloc(ch_private_cache, KM_SLEEP);
833 
834 	bzero(chprp, sizeof (cheetah_private_t));
835 	chprp->chpr_fecctl0_logout.clo_data.chd_afar = LOGOUT_INVALID;
836 	chprp->chpr_cecc_logout.clo_data.chd_afar = LOGOUT_INVALID;
837 	chprp->chpr_async_logout.clo_data.chd_afar = LOGOUT_INVALID;
838 	chprp->chpr_tlb_logout.tlo_addr = LOGOUT_INVALID;
839 	for (i = 0; i < CH_ERR_TL1_TLMAX; i++)
840 		chprp->chpr_tl1_err_data[i].ch_err_tl1_logout.clo_data.chd_afar
841 		    = LOGOUT_INVALID;
842 
843 	/* Panther has a larger Icache compared to cheetahplus or Jaguar */
844 	if (IS_PANTHER(cpunodes[cp->cpu_id].implementation)) {
845 		chprp->chpr_icache_size = PN_ICACHE_SIZE;
846 		chprp->chpr_icache_linesize = PN_ICACHE_LSIZE;
847 	} else {
848 		chprp->chpr_icache_size = CH_ICACHE_SIZE;
849 		chprp->chpr_icache_linesize = CH_ICACHE_LSIZE;
850 	}
851 
852 	cpu_init_ecache_scrub_dr(cp);
853 
854 	/*
855 	 * Panther's L2$ and E$ are shared between cores, so the scrubber is
856 	 * only needed on one of the cores.  At this point, we assume all cores
857 	 * are online, and we only enable the scrubber on core 0.
858 	 */
859 	if (IS_PANTHER(cpunodes[cp->cpu_id].implementation)) {
860 		chprp->chpr_scrub_misc.chsm_core_state =
861 		    SCRUBBER_BOTH_CORES_ONLINE;
862 		if (cp->cpu_id != (processorid_t)cmp_cpu_to_chip(cp->cpu_id)) {
863 			chprp->chpr_scrub_misc.chsm_enable[
864 			    CACHE_SCRUBBER_INFO_E] = 0;
865 		}
866 	}
867 
868 	chprp->chpr_ec_set_size = cpunodes[cp->cpu_id].ecache_size /
869 	    cpu_ecache_nway();
870 
871 	adjust_hw_copy_limits(cpunodes[cp->cpu_id].ecache_size);
872 	ch_err_tl1_paddrs[cp->cpu_id] = va_to_pa(chprp);
873 	ASSERT(ch_err_tl1_paddrs[cp->cpu_id] != -1);
874 }
875 
876 /*
877  * Clear the error state registers for this CPU.
878  * For Cheetah+/Jaguar, just clear the AFSR but
879  * for Panther we also have to clear the AFSR_EXT.
880  */
881 void
882 set_cpu_error_state(ch_cpu_errors_t *cpu_error_regs)
883 {
884 	set_asyncflt(cpu_error_regs->afsr & ~C_AFSR_FATAL_ERRS);
885 	if (IS_PANTHER(cpunodes[CPU->cpu_id].implementation)) {
886 		set_afsr_ext(cpu_error_regs->afsr_ext & ~C_AFSR_EXT_FATAL_ERRS);
887 	}
888 }
889 
890 void
891 pn_cpu_log_diag_l2_info(ch_async_flt_t *ch_flt) {
892 	struct async_flt *aflt = (struct async_flt *)ch_flt;
893 	ch_ec_data_t *l2_data = &ch_flt->flt_diag_data.chd_l2_data[0];
894 	uint64_t faddr = aflt->flt_addr;
895 	uint8_t log_way_mask = 0;
896 	int i;
897 
898 	/*
899 	 * Only Panther CPUs have the additional L2$ data that needs
900 	 * to be logged here
901 	 */
902 	if (!IS_PANTHER(cpunodes[aflt->flt_inst].implementation))
903 		return;
904 
905 	/*
906 	 * We'll use a simple bit mask to keep track of which way(s)
907 	 * of the stored cache line we want to log. The idea is to
908 	 * log the entry if it is a valid line and it matches our
909 	 * fault AFAR. If no match is found, we will simply log all
910 	 * the ways.
911 	 */
912 	for (i = 0; i < PN_L2_NWAYS; i++)
913 		if (pn_matching_valid_l2_line(faddr, &l2_data[i]))
914 			log_way_mask |= (1 << i);
915 
916 	/* If no matching valid lines were found, we log all ways */
917 	if (log_way_mask == 0)
918 		log_way_mask = (1 << PN_L2_NWAYS) - 1;
919 
920 	/* Log the cache lines */
921 	for (i = 0; i < PN_L2_NWAYS; i++)
922 		if (log_way_mask & (1 << i))
923 			l2_data[i].ec_logflag = EC_LOGFLAG_MAGIC;
924 }
925 
926 /*
927  * For this routine to return true, the L2 tag in question must be valid
928  * and the tag PA must match the fault address (faddr) assuming the correct
929  * index is being used.
930  */
931 static int
932 pn_matching_valid_l2_line(uint64_t faddr, ch_ec_data_t *clo_l2_data) {
933 	if ((!PN_L2_LINE_INVALID(clo_l2_data->ec_tag)) &&
934 	((faddr & P2ALIGN(C_AFAR_PA, PN_L2_SET_SIZE)) ==
935 	    PN_L2TAG_TO_PA(clo_l2_data->ec_tag)))
936 		return (1);
937 	return (0);
938 }
939 
940 /*
941  * This array is used to convert the 3 digit PgSz encoding (as used in
942  * various MMU registers such as MMU_TAG_ACCESS_EXT) into the corresponding
943  * page size.
944  */
945 static uint64_t tlb_pgsz_to_size[] = {
946 	/* 000 = 8KB: */
947 	0x2000,
948 	/* 001 = 64KB: */
949 	0x10000,
950 	/* 010 = 512KB: */
951 	0x80000,
952 	/* 011 = 4MB: */
953 	0x400000,
954 	/* 100 = 32MB: */
955 	0x2000000,
956 	/* 101 = 256MB: */
957 	0x10000000,
958 	/* undefined for encodings 110 and 111: */
959 	0, 0
960 };
961 
962 /*
963  * The itlb_parity_trap and dtlb_parity_trap handlers transfer control here
964  * after collecting logout information related to the TLB parity error and
965  * flushing the offending TTE entries from the ITLB or DTLB.
966  *
967  * DTLB traps which occur at TL>0 are not recoverable because we will most
968  * likely be corrupting some other trap handler's alternate globals. As
969  * such, we simply panic here when that happens. ITLB parity errors are
970  * not expected to happen at TL>0.
971  */
972 void
973 cpu_tlb_parity_error(struct regs *rp, ulong_t trap_va, ulong_t tlb_info) {
974 	ch_async_flt_t ch_flt;
975 	struct async_flt *aflt;
976 	pn_tlb_logout_t *tlop = NULL;
977 	int immu_parity = (tlb_info & PN_TLO_INFO_IMMU) != 0;
978 	int tl1_trap = (tlb_info & PN_TLO_INFO_TL1) != 0;
979 	char *error_class;
980 
981 	bzero(&ch_flt, sizeof (ch_async_flt_t));
982 
983 	/*
984 	 * Get the CPU log out info. If we can't find our CPU private
985 	 * pointer, or if the logout information does not correspond to
986 	 * this error, then we will have to make due without detailed
987 	 * logout information.
988 	 */
989 	if (CPU_PRIVATE(CPU)) {
990 		tlop = CPU_PRIVATE_PTR(CPU, chpr_tlb_logout);
991 		if ((tlop->tlo_addr != trap_va) ||
992 		    (tlop->tlo_info != tlb_info))
993 			tlop = NULL;
994 	}
995 
996 	if (tlop) {
997 		ch_flt.tlb_diag_data = *tlop;
998 
999 		/* Zero out + invalidate TLB logout. */
1000 		bzero(tlop, sizeof (pn_tlb_logout_t));
1001 		tlop->tlo_addr = LOGOUT_INVALID;
1002 	} else {
1003 		/*
1004 		 * Copy what logout information we have and mark
1005 		 * it incomplete.
1006 		 */
1007 		ch_flt.flt_data_incomplete = 1;
1008 		ch_flt.tlb_diag_data.tlo_info = tlb_info;
1009 		ch_flt.tlb_diag_data.tlo_addr = trap_va;
1010 	}
1011 
1012 	/*
1013 	 * Log the error.
1014 	 */
1015 	aflt = (struct async_flt *)&ch_flt;
1016 	aflt->flt_id = gethrtime_waitfree();
1017 	aflt->flt_bus_id = getprocessorid();
1018 	aflt->flt_inst = CPU->cpu_id;
1019 	aflt->flt_pc = (caddr_t)rp->r_pc;
1020 	aflt->flt_addr = trap_va;
1021 	aflt->flt_prot = AFLT_PROT_NONE;
1022 	aflt->flt_class = CPU_FAULT;
1023 	aflt->flt_priv = (rp->r_tstate & TSTATE_PRIV) ?  1 : 0;
1024 	aflt->flt_tl = tl1_trap ? 1 : 0;
1025 	aflt->flt_panic = tl1_trap ? 1 : 0;
1026 
1027 	if (immu_parity) {
1028 		aflt->flt_status = ECC_ITLB_TRAP;
1029 		ch_flt.flt_type = CPU_ITLB_PARITY;
1030 		error_class = FM_EREPORT_CPU_USIII_ITLBPE;
1031 		aflt->flt_payload = FM_EREPORT_PAYLOAD_ITLB_PE;
1032 	} else {
1033 		aflt->flt_status = ECC_DTLB_TRAP;
1034 		ch_flt.flt_type = CPU_DTLB_PARITY;
1035 		error_class = FM_EREPORT_CPU_USIII_DTLBPE;
1036 		aflt->flt_payload = FM_EREPORT_PAYLOAD_DTLB_PE;
1037 	}
1038 
1039 	/*
1040 	 * The TLB entries have already been flushed by the TL1 trap
1041 	 * handler so at this point the only thing left to do is log
1042 	 * the error message.
1043 	 */
1044 	if (aflt->flt_panic) {
1045 		cpu_errorq_dispatch(error_class, (void *)&ch_flt,
1046 		    sizeof (ch_async_flt_t), ue_queue, aflt->flt_panic);
1047 		/*
1048 		 * Panic here if aflt->flt_panic has been set.  Enqueued
1049 		 * errors will be logged as part of the panic flow.
1050 		 */
1051 		fm_panic("%sError(s)", immu_parity ? "ITLBPE " : "DTLBPE ");
1052 	} else {
1053 		cpu_errorq_dispatch(error_class, (void *)&ch_flt,
1054 		    sizeof (ch_async_flt_t), ce_queue, aflt->flt_panic);
1055 	}
1056 }
1057 
1058 /*
1059  * This routine is called when a TLB parity error event is 'ue_drain'ed
1060  * or 'ce_drain'ed from the errorq.
1061  */
1062 void
1063 cpu_async_log_tlb_parity_err(void *flt) {
1064 	ch_async_flt_t *ch_flt = (ch_async_flt_t *)flt;
1065 	struct async_flt *aflt = (struct async_flt *)flt;
1066 #ifdef lint
1067 	aflt = aflt;
1068 #endif
1069 
1070 	/*
1071 	 * We only capture TLB information if we encountered
1072 	 * a TLB parity error and Panther is the only CPU which
1073 	 * can detect a TLB parity error.
1074 	 */
1075 	ASSERT(IS_PANTHER(cpunodes[aflt->flt_inst].implementation));
1076 	ASSERT((ch_flt->flt_type == CPU_ITLB_PARITY) ||
1077 	    (ch_flt->flt_type == CPU_DTLB_PARITY));
1078 
1079 	if (ch_flt->flt_data_incomplete == 0) {
1080 		if (ch_flt->flt_type == CPU_ITLB_PARITY)
1081 			ch_flt->tlb_diag_data.tlo_logflag = IT_LOGFLAG_MAGIC;
1082 		else /* parity error is in DTLB */
1083 			ch_flt->tlb_diag_data.tlo_logflag = DT_LOGFLAG_MAGIC;
1084 	}
1085 }
1086 
1087 /*
1088  * Add L1 Prefetch cache data to the ereport payload.
1089  */
1090 void
1091 cpu_payload_add_pcache(struct async_flt *aflt, nvlist_t *nvl)
1092 {
1093 	ch_async_flt_t *ch_flt = (ch_async_flt_t *)aflt;
1094 	ch_pc_data_t *pcp;
1095 	ch_pc_data_t pcdata[CH_PCACHE_NWAY];
1096 	uint_t nelem;
1097 	int i, ways_logged = 0;
1098 
1099 	/*
1100 	 * We only capture P$ information if we encountered
1101 	 * a P$ parity error and Panther is the only CPU which
1102 	 * can detect a P$ parity error.
1103 	 */
1104 	ASSERT(IS_PANTHER(cpunodes[aflt->flt_inst].implementation));
1105 	for (i = 0; i < CH_PCACHE_NWAY; i++) {
1106 		pcp = &ch_flt->parity_data.dpe.cpl_pc[i];
1107 		if (pcp->pc_logflag == PC_LOGFLAG_MAGIC) {
1108 			bcopy(pcp, &pcdata[ways_logged],
1109 				sizeof (ch_pc_data_t));
1110 			ways_logged++;
1111 		}
1112 	}
1113 
1114 	/*
1115 	 * Add the pcache data to the payload.
1116 	 */
1117 	fm_payload_set(nvl, FM_EREPORT_PAYLOAD_NAME_L1P_WAYS,
1118 	    DATA_TYPE_UINT8, (uint8_t)ways_logged, NULL);
1119 	if (ways_logged != 0) {
1120 		nelem = sizeof (ch_pc_data_t) / sizeof (uint64_t) * ways_logged;
1121 		fm_payload_set(nvl, FM_EREPORT_PAYLOAD_NAME_L1P_DATA,
1122 		    DATA_TYPE_UINT64_ARRAY, nelem, (uint64_t *)pcdata, NULL);
1123 	}
1124 }
1125 
1126 /*
1127  * Add TLB diagnostic data to the ereport payload.
1128  */
1129 void
1130 cpu_payload_add_tlb(struct async_flt *aflt, nvlist_t *nvl)
1131 {
1132 	ch_async_flt_t *ch_flt = (ch_async_flt_t *)aflt;
1133 	uint8_t num_entries, tlb_data_words;
1134 
1135 	/*
1136 	 * We only capture TLB information if we encountered
1137 	 * a TLB parity error and Panther is the only CPU which
1138 	 * can detect a TLB parity error.
1139 	 */
1140 	ASSERT(IS_PANTHER(cpunodes[aflt->flt_inst].implementation));
1141 	ASSERT((ch_flt->flt_type == CPU_ITLB_PARITY) ||
1142 	    (ch_flt->flt_type == CPU_DTLB_PARITY));
1143 
1144 	if (ch_flt->flt_type == CPU_ITLB_PARITY) {
1145 		num_entries = (uint8_t)(PN_ITLB_NWAYS * PN_NUM_512_ITLBS);
1146 		tlb_data_words = sizeof (ch_tte_entry_t) / sizeof (uint64_t) *
1147 		    num_entries;
1148 
1149 		/*
1150 		 * Add the TLB diagnostic data to the payload
1151 		 * if it was collected.
1152 		 */
1153 		if (ch_flt->tlb_diag_data.tlo_logflag == IT_LOGFLAG_MAGIC) {
1154 			fm_payload_set(nvl,
1155 			    FM_EREPORT_PAYLOAD_NAME_ITLB_ENTRIES,
1156 			    DATA_TYPE_UINT8, num_entries, NULL);
1157 			fm_payload_set(nvl, FM_EREPORT_PAYLOAD_NAME_ITLB_DATA,
1158 			    DATA_TYPE_UINT64_ARRAY, tlb_data_words,
1159 			    (uint64_t *)ch_flt->tlb_diag_data.tlo_itlb_tte,
1160 			    NULL);
1161 		}
1162 	} else {
1163 		num_entries = (uint8_t)(PN_DTLB_NWAYS * PN_NUM_512_DTLBS);
1164 		tlb_data_words = sizeof (ch_tte_entry_t) / sizeof (uint64_t) *
1165 		    num_entries;
1166 
1167 		fm_payload_set(nvl, FM_EREPORT_PAYLOAD_NAME_VA,
1168 		    DATA_TYPE_UINT64, ch_flt->tlb_diag_data.tlo_addr, NULL);
1169 
1170 		/*
1171 		 * Add the TLB diagnostic data to the payload
1172 		 * if it was collected.
1173 		 */
1174 		if (ch_flt->tlb_diag_data.tlo_logflag == DT_LOGFLAG_MAGIC) {
1175 			fm_payload_set(nvl,
1176 			    FM_EREPORT_PAYLOAD_NAME_DTLB_ENTRIES,
1177 			    DATA_TYPE_UINT8, num_entries, NULL);
1178 			fm_payload_set(nvl, FM_EREPORT_PAYLOAD_NAME_DTLB_DATA,
1179 			    DATA_TYPE_UINT64_ARRAY, tlb_data_words,
1180 			    (uint64_t *)ch_flt->tlb_diag_data.tlo_dtlb_tte,
1181 			    NULL);
1182 		}
1183 	}
1184 }
1185 
1186 /*
1187  * Panther Cache Scrubbing:
1188  *
1189  * In Jaguar, the E$ was split between cores, so the scrubber must run on both
1190  * cores.  For Panther, however, the L2$ and L3$ are shared across cores.
1191  * Therefore, the E$ scrubber only needs to run on one of the two cores.
1192  *
1193  * There are four possible states for the E$ scrubber:
1194  *
1195  * 0. If both cores are offline, add core 0 to cpu_offline_set so that
1196  *    the offline scrubber will run on it.
1197  * 1. If core 0 is online and core 1 off, we run the scrubber on core 0.
1198  * 2. If core 1 is online and core 0 off, we move the scrubber to run
1199  *    on core 1.
1200  * 3. If both cores are online, only run the scrubber on core 0.
1201  *
1202  * These states are enumerated by the SCRUBBER_[BOTH|CORE|NEITHER]_* defines
1203  * above.  One of those values is stored in
1204  * chpr_scrub_misc->chsm_core_state on each core.
1205  *
1206  * Also note that, for Panther, ecache_flush_line() will flush out the L2$
1207  * before the E$, so the L2$ will be scrubbed by the E$ scrubber.  No
1208  * additional code is necessary to scrub the L2$.
1209  *
1210  * For all cpu types, whenever a cpu or core is offlined, add it to
1211  * cpu_offline_set so the necessary scrubbers can still run.  This is still
1212  * necessary on Panther so the D$ scrubber can still run.
1213  */
1214 /*ARGSUSED*/
1215 int
1216 cpu_scrub_cpu_setup(cpu_setup_t what, int cpuid, void *arg)
1217 {
1218 	processorid_t core_0_id;
1219 	cpu_t *core_cpus[2];
1220 	ch_scrub_misc_t *core_scrub[2];
1221 	int old_state, i;
1222 	int new_state = SCRUBBER_NEITHER_CORE_ONLINE;
1223 
1224 	switch (what) {
1225 	case CPU_ON:
1226 	case CPU_INIT:
1227 		CPUSET_DEL(cpu_offline_set, cpuid);
1228 		break;
1229 	case CPU_OFF:
1230 		CPUSET_ADD(cpu_offline_set, cpuid);
1231 		break;
1232 	default:
1233 		return (0);
1234 	}
1235 
1236 	if (!IS_PANTHER(cpunodes[cpuid].implementation)) {
1237 		return (0);
1238 	}
1239 
1240 	/*
1241 	 * Update the chsm_enable[CACHE_SCRUBBER_INFO_E] value
1242 	 * if necessary
1243 	 */
1244 	core_0_id = cmp_cpu_to_chip(cpuid);
1245 	core_cpus[0] = cpu_get(core_0_id);
1246 	core_cpus[1] = cpu_get_sibling_core(core_cpus[0]);
1247 
1248 	for (i = 0; i < 2; i++) {
1249 		if (core_cpus[i] == NULL) {
1250 			/*
1251 			 * This may happen during DR - one core is offlined
1252 			 * and completely unconfigured before the second
1253 			 * core is offlined.  Give up and return quietly,
1254 			 * since the second core should quickly be removed
1255 			 * anyways.
1256 			 */
1257 			return (0);
1258 		}
1259 		core_scrub[i] = CPU_PRIVATE_PTR(core_cpus[i], chpr_scrub_misc);
1260 	}
1261 
1262 	if (cpuid == (processorid_t)cmp_cpu_to_chip(cpuid)) {
1263 		/* cpuid is core 0 */
1264 		if (cpu_is_active(core_cpus[1])) {
1265 			new_state |= SCRUBBER_CORE_1_ONLINE;
1266 		}
1267 		if (what != CPU_OFF) {
1268 			new_state |= SCRUBBER_CORE_0_ONLINE;
1269 		}
1270 	} else {
1271 		/* cpuid is core 1 */
1272 		if (cpu_is_active(core_cpus[0])) {
1273 			new_state |= SCRUBBER_CORE_0_ONLINE;
1274 		}
1275 		if (what != CPU_OFF) {
1276 			new_state |= SCRUBBER_CORE_1_ONLINE;
1277 		}
1278 	}
1279 
1280 	old_state = core_scrub[0]->chsm_core_state;
1281 
1282 	if (old_state == new_state) {
1283 		return (0);
1284 	}
1285 
1286 	if (old_state == SCRUBBER_CORE_1_ONLINE) {
1287 		/*
1288 		 * We need to move the scrubber state from core 1
1289 		 * back to core 0.  This data is not protected by
1290 		 * locks, but the worst that can happen is some
1291 		 * lines are scrubbed multiple times.  chsm_oustanding is
1292 		 * set to 0 to make sure an interrupt is scheduled the
1293 		 * first time through do_scrub().
1294 		 */
1295 		core_scrub[0]->chsm_flush_index[CACHE_SCRUBBER_INFO_E] =
1296 		    core_scrub[1]->chsm_flush_index[CACHE_SCRUBBER_INFO_E];
1297 		core_scrub[0]->chsm_outstanding[CACHE_SCRUBBER_INFO_E] = 0;
1298 	}
1299 
1300 	switch (new_state) {
1301 	case SCRUBBER_NEITHER_CORE_ONLINE:
1302 	case SCRUBBER_BOTH_CORES_ONLINE:
1303 	case SCRUBBER_CORE_0_ONLINE:
1304 		core_scrub[1]->chsm_enable[CACHE_SCRUBBER_INFO_E] = 0;
1305 		core_scrub[0]->chsm_enable[CACHE_SCRUBBER_INFO_E] = 1;
1306 		break;
1307 
1308 	case SCRUBBER_CORE_1_ONLINE:
1309 	default:
1310 		/*
1311 		 * We need to move the scrubber state from core 0
1312 		 * to core 1.
1313 		 */
1314 		core_scrub[1]->chsm_flush_index[CACHE_SCRUBBER_INFO_E] =
1315 		    core_scrub[0]->chsm_flush_index[CACHE_SCRUBBER_INFO_E];
1316 		core_scrub[1]->chsm_outstanding[CACHE_SCRUBBER_INFO_E] = 0;
1317 
1318 		core_scrub[0]->chsm_enable[CACHE_SCRUBBER_INFO_E] = 0;
1319 		core_scrub[1]->chsm_enable[CACHE_SCRUBBER_INFO_E] = 1;
1320 		break;
1321 	}
1322 
1323 	core_scrub[0]->chsm_core_state = new_state;
1324 	core_scrub[1]->chsm_core_state = new_state;
1325 	return (0);
1326 }
1327 
1328 /*
1329  * Returns a pointer to the cpu structure of the argument's sibling core.
1330  * If no sibling core can be found, return NULL.
1331  */
1332 static cpu_t *
1333 cpu_get_sibling_core(cpu_t *cpup)
1334 {
1335 	cpu_t		*nextp;
1336 	pg_t		*pg;
1337 	pg_cpu_itr_t	i;
1338 
1339 	if ((cpup == NULL) || (!cmp_cpu_is_cmp(cpup->cpu_id)))
1340 		return (NULL);
1341 	pg = (pg_t *)pghw_find_pg(cpup, PGHW_CHIP);
1342 	if (pg == NULL)
1343 		return (NULL);
1344 
1345 	/*
1346 	 * Iterate over the CPUs in the chip PG looking
1347 	 * for a CPU that isn't cpup
1348 	 */
1349 	PG_CPU_ITR_INIT(pg, i);
1350 	while ((nextp = pg_cpu_next(&i)) != NULL) {
1351 		if (nextp != cpup)
1352 			break;
1353 	}
1354 
1355 	if (nextp == NULL)
1356 		return (NULL);
1357 
1358 	return (nextp);
1359 }
1360