xref: /onnv-gate/usr/src/uts/sun4u/cpu/opl_olympus.c (revision 5394:f981b7bf78eb)
11772Sjl139090 /*
21772Sjl139090  * CDDL HEADER START
31772Sjl139090  *
41772Sjl139090  * The contents of this file are subject to the terms of the
51772Sjl139090  * Common Development and Distribution License (the "License").
61772Sjl139090  * You may not use this file except in compliance with the License.
71772Sjl139090  *
81772Sjl139090  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91772Sjl139090  * or http://www.opensolaris.org/os/licensing.
101772Sjl139090  * See the License for the specific language governing permissions
111772Sjl139090  * and limitations under the License.
121772Sjl139090  *
131772Sjl139090  * When distributing Covered Code, include this CDDL HEADER in each
141772Sjl139090  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151772Sjl139090  * If applicable, add the following below this CDDL HEADER, with the
161772Sjl139090  * fields enclosed by brackets "[]" replaced with your own identifying
171772Sjl139090  * information: Portions Copyright [yyyy] [name of copyright owner]
181772Sjl139090  *
191772Sjl139090  * CDDL HEADER END
201772Sjl139090  */
211772Sjl139090 /*
223382Sjimand  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
231772Sjl139090  * Use is subject to license terms.
241772Sjl139090  */
251772Sjl139090 
265037Sjl139090 /*
275037Sjl139090  * Support for Olympus-C (SPARC64-VI) and Jupiter (SPARC64-VII).
285037Sjl139090  */
295037Sjl139090 
301772Sjl139090 #pragma ident	"%Z%%M%	%I%	%E% SMI"
311772Sjl139090 
321772Sjl139090 #include <sys/types.h>
331772Sjl139090 #include <sys/systm.h>
341772Sjl139090 #include <sys/ddi.h>
351772Sjl139090 #include <sys/sysmacros.h>
361772Sjl139090 #include <sys/archsystm.h>
371772Sjl139090 #include <sys/vmsystm.h>
381772Sjl139090 #include <sys/machparam.h>
391772Sjl139090 #include <sys/machsystm.h>
401772Sjl139090 #include <sys/machthread.h>
411772Sjl139090 #include <sys/cpu.h>
421772Sjl139090 #include <sys/cmp.h>
431772Sjl139090 #include <sys/elf_SPARC.h>
441772Sjl139090 #include <vm/vm_dep.h>
451772Sjl139090 #include <vm/hat_sfmmu.h>
461772Sjl139090 #include <vm/seg_kpm.h>
473309Sjimand #include <vm/seg_kmem.h>
481772Sjl139090 #include <sys/cpuvar.h>
491772Sjl139090 #include <sys/opl_olympus_regs.h>
501772Sjl139090 #include <sys/opl_module.h>
511772Sjl139090 #include <sys/async.h>
521772Sjl139090 #include <sys/cmn_err.h>
531772Sjl139090 #include <sys/debug.h>
541772Sjl139090 #include <sys/dditypes.h>
551772Sjl139090 #include <sys/cpu_module.h>
561772Sjl139090 #include <sys/sysmacros.h>
571772Sjl139090 #include <sys/intreg.h>
581772Sjl139090 #include <sys/clock.h>
591772Sjl139090 #include <sys/platform_module.h>
601772Sjl139090 #include <sys/ontrap.h>
611772Sjl139090 #include <sys/panic.h>
621772Sjl139090 #include <sys/memlist.h>
631772Sjl139090 #include <sys/ndifm.h>
641772Sjl139090 #include <sys/ddifm.h>
651772Sjl139090 #include <sys/fm/protocol.h>
661772Sjl139090 #include <sys/fm/util.h>
671772Sjl139090 #include <sys/fm/cpu/SPARC64-VI.h>
681772Sjl139090 #include <sys/dtrace.h>
691772Sjl139090 #include <sys/watchpoint.h>
701772Sjl139090 #include <sys/promif.h>
711772Sjl139090 
721772Sjl139090 /*
731772Sjl139090  * Internal functions.
741772Sjl139090  */
751772Sjl139090 static int cpu_sync_log_err(void *flt);
761772Sjl139090 static void cpu_payload_add_aflt(struct async_flt *, nvlist_t *, nvlist_t *);
771772Sjl139090 static void opl_cpu_sync_error(struct regs *, ulong_t, ulong_t, uint_t, uint_t);
781772Sjl139090 static int  cpu_flt_in_memory(opl_async_flt_t *, uint64_t);
79*5394Sjimand static int prom_SPARC64VII_support_enabled(void);
801772Sjl139090 
811772Sjl139090 /*
821772Sjl139090  * Error counters resetting interval.
831772Sjl139090  */
841772Sjl139090 static int opl_async_check_interval = 60;		/* 1 min */
851772Sjl139090 
862241Shuah uint_t cpu_impl_dual_pgsz = 1;
871772Sjl139090 
881772Sjl139090 /*
891772Sjl139090  * PA[22:0] represent Displacement in Jupiter
901772Sjl139090  * configuration space.
911772Sjl139090  */
921772Sjl139090 uint_t	root_phys_addr_lo_mask = 0x7fffffu;
931772Sjl139090 
941772Sjl139090 /*
951772Sjl139090  * set in /etc/system to control logging of user BERR/TO's
961772Sjl139090  */
971772Sjl139090 int cpu_berr_to_verbose = 0;
981772Sjl139090 
995037Sjl139090 /*
1005037Sjl139090  * Set to 1 if booted with all Jupiter cpus (all-Jupiter features enabled).
1015037Sjl139090  */
1025037Sjl139090 int cpu_alljupiter = 0;
1035037Sjl139090 
1041772Sjl139090 static int min_ecache_size;
1051772Sjl139090 static uint_t priv_hcl_1;
1061772Sjl139090 static uint_t priv_hcl_2;
1071772Sjl139090 static uint_t priv_hcl_4;
1081772Sjl139090 static uint_t priv_hcl_8;
1091772Sjl139090 
1101772Sjl139090 /*
1111772Sjl139090  * Olympus error log
1121772Sjl139090  */
1131772Sjl139090 static opl_errlog_t	*opl_err_log;
1141772Sjl139090 
1151772Sjl139090 /*
1161772Sjl139090  * UE is classified into four classes (MEM, CHANNEL, CPU, PATH).
1171772Sjl139090  * No any other ecc_type_info insertion is allowed in between the following
1181772Sjl139090  * four UE classess.
1191772Sjl139090  */
1201772Sjl139090 ecc_type_to_info_t ecc_type_to_info[] = {
1211772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1221772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1231772Sjl139090 	FM_EREPORT_CPU_UE_MEM,
1241772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1251772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1261772Sjl139090 	FM_EREPORT_CPU_UE_CHANNEL,
1271772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1281772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1291772Sjl139090 	FM_EREPORT_CPU_UE_CPU,
1301772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1311772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1321772Sjl139090 	FM_EREPORT_CPU_UE_PATH,
1331772Sjl139090 	SFSR_BERR, "BERR ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1341772Sjl139090 	"Bus Error",  FM_EREPORT_PAYLOAD_SYNC,
1351772Sjl139090 	FM_EREPORT_CPU_BERR,
1361772Sjl139090 	SFSR_TO, "TO ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1371772Sjl139090 	"Bus Timeout",  FM_EREPORT_PAYLOAD_SYNC,
1381772Sjl139090 	FM_EREPORT_CPU_BTO,
1391772Sjl139090 	SFSR_TLB_MUL, "TLB_MUL ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1401772Sjl139090 	"TLB MultiHit",  FM_EREPORT_PAYLOAD_SYNC,
1411772Sjl139090 	FM_EREPORT_CPU_MTLB,
1421772Sjl139090 	SFSR_TLB_PRT, "TLB_PRT ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1431772Sjl139090 	"TLB Parity",  FM_EREPORT_PAYLOAD_SYNC,
1441772Sjl139090 	FM_EREPORT_CPU_TLBP,
1451772Sjl139090 
1461772Sjl139090 	UGESR_IAUG_CRE, "IAUG_CRE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1471772Sjl139090 	"IAUG CRE",  FM_EREPORT_PAYLOAD_URGENT,
1481772Sjl139090 	FM_EREPORT_CPU_CRE,
1491772Sjl139090 	UGESR_IAUG_TSBCTXT, "IAUG_TSBCTXT",
1501772Sjl139090 	OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1511772Sjl139090 	"IAUG TSBCTXT",  FM_EREPORT_PAYLOAD_URGENT,
1521772Sjl139090 	FM_EREPORT_CPU_TSBCTX,
1531772Sjl139090 	UGESR_IUG_TSBP, "IUG_TSBP", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1541772Sjl139090 	"IUG TSBP",  FM_EREPORT_PAYLOAD_URGENT,
1551772Sjl139090 	FM_EREPORT_CPU_TSBP,
1561772Sjl139090 	UGESR_IUG_PSTATE, "IUG_PSTATE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1571772Sjl139090 	"IUG PSTATE",  FM_EREPORT_PAYLOAD_URGENT,
1581772Sjl139090 	FM_EREPORT_CPU_PSTATE,
1591772Sjl139090 	UGESR_IUG_TSTATE, "IUG_TSTATE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1601772Sjl139090 	"IUG TSTATE",  FM_EREPORT_PAYLOAD_URGENT,
1611772Sjl139090 	FM_EREPORT_CPU_TSTATE,
1621772Sjl139090 	UGESR_IUG_F, "IUG_F", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1631772Sjl139090 	"IUG FREG",  FM_EREPORT_PAYLOAD_URGENT,
1641772Sjl139090 	FM_EREPORT_CPU_IUG_F,
1651772Sjl139090 	UGESR_IUG_R, "IUG_R", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1661772Sjl139090 	"IUG RREG",  FM_EREPORT_PAYLOAD_URGENT,
1671772Sjl139090 	FM_EREPORT_CPU_IUG_R,
1681772Sjl139090 	UGESR_AUG_SDC, "AUG_SDC", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1691772Sjl139090 	"AUG SDC",  FM_EREPORT_PAYLOAD_URGENT,
1701772Sjl139090 	FM_EREPORT_CPU_SDC,
1711772Sjl139090 	UGESR_IUG_WDT, "IUG_WDT", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1721772Sjl139090 	"IUG WDT",  FM_EREPORT_PAYLOAD_URGENT,
1731772Sjl139090 	FM_EREPORT_CPU_WDT,
1741772Sjl139090 	UGESR_IUG_DTLB, "IUG_DTLB", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1751772Sjl139090 	"IUG DTLB",  FM_EREPORT_PAYLOAD_URGENT,
1761772Sjl139090 	FM_EREPORT_CPU_DTLB,
1771772Sjl139090 	UGESR_IUG_ITLB, "IUG_ITLB", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1781772Sjl139090 	"IUG ITLB",  FM_EREPORT_PAYLOAD_URGENT,
1791772Sjl139090 	FM_EREPORT_CPU_ITLB,
1801772Sjl139090 	UGESR_IUG_COREERR, "IUG_COREERR",
1811772Sjl139090 	OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1821772Sjl139090 	"IUG COREERR",  FM_EREPORT_PAYLOAD_URGENT,
1831772Sjl139090 	FM_EREPORT_CPU_CORE,
1841772Sjl139090 	UGESR_MULTI_DAE, "MULTI_DAE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1851772Sjl139090 	"MULTI DAE",  FM_EREPORT_PAYLOAD_URGENT,
1861772Sjl139090 	FM_EREPORT_CPU_DAE,
1871772Sjl139090 	UGESR_MULTI_IAE, "MULTI_IAE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1881772Sjl139090 	"MULTI IAE",  FM_EREPORT_PAYLOAD_URGENT,
1891772Sjl139090 	FM_EREPORT_CPU_IAE,
1901772Sjl139090 	UGESR_MULTI_UGE, "MULTI_UGE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1911772Sjl139090 	"MULTI UGE",  FM_EREPORT_PAYLOAD_URGENT,
1921772Sjl139090 	FM_EREPORT_CPU_UGE,
1931772Sjl139090 	0,		NULL,		0,		0,
1941772Sjl139090 	NULL,  0,	   0,
1951772Sjl139090 };
1961772Sjl139090 
1971772Sjl139090 int (*p2get_mem_info)(int synd_code, uint64_t paddr,
1981772Sjl139090 		uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
1991772Sjl139090 		int *segsp, int *banksp, int *mcidp);
2001772Sjl139090 
2011772Sjl139090 
2021772Sjl139090 /*
2031772Sjl139090  * Setup trap handlers for 0xA, 0x32, 0x40 trap types.
2041772Sjl139090  */
2051772Sjl139090 void
2061772Sjl139090 cpu_init_trap(void)
2071772Sjl139090 {
2081772Sjl139090 	OPL_SET_TRAP(tt0_iae, opl_serr_instr);
2091772Sjl139090 	OPL_SET_TRAP(tt1_iae, opl_serr_instr);
2101772Sjl139090 	OPL_SET_TRAP(tt0_dae, opl_serr_instr);
2111772Sjl139090 	OPL_SET_TRAP(tt1_dae, opl_serr_instr);
2121772Sjl139090 	OPL_SET_TRAP(tt0_asdat, opl_ugerr_instr);
2131772Sjl139090 	OPL_SET_TRAP(tt1_asdat, opl_ugerr_instr);
2141772Sjl139090 }
2151772Sjl139090 
2161772Sjl139090 static int
2171772Sjl139090 getintprop(pnode_t node, char *name, int deflt)
2181772Sjl139090 {
2191772Sjl139090 	int	value;
2201772Sjl139090 
2211772Sjl139090 	switch (prom_getproplen(node, name)) {
2221772Sjl139090 	case sizeof (int):
2231772Sjl139090 		(void) prom_getprop(node, name, (caddr_t)&value);
2241772Sjl139090 		break;
2251772Sjl139090 
2261772Sjl139090 	default:
2271772Sjl139090 		value = deflt;
2281772Sjl139090 		break;
2291772Sjl139090 	}
2301772Sjl139090 
2311772Sjl139090 	return (value);
2321772Sjl139090 }
2331772Sjl139090 
2341772Sjl139090 /*
2351772Sjl139090  * Set the magic constants of the implementation.
2361772Sjl139090  */
2371772Sjl139090 /*ARGSUSED*/
2381772Sjl139090 void
2391772Sjl139090 cpu_fiximp(pnode_t dnode)
2401772Sjl139090 {
2411772Sjl139090 	int i, a;
2421772Sjl139090 	extern int vac_size, vac_shift;
2431772Sjl139090 	extern uint_t vac_mask;
2441772Sjl139090 
2451772Sjl139090 	static struct {
2461772Sjl139090 		char	*name;
2471772Sjl139090 		int	*var;
2481772Sjl139090 		int	defval;
2491772Sjl139090 	} prop[] = {
2501772Sjl139090 		"l1-dcache-size", &dcache_size, OPL_DCACHE_SIZE,
2511772Sjl139090 		"l1-dcache-line-size", &dcache_linesize, OPL_DCACHE_LSIZE,
2521772Sjl139090 		"l1-icache-size", &icache_size, OPL_ICACHE_SIZE,
2531772Sjl139090 		"l1-icache-line-size", &icache_linesize, OPL_ICACHE_LSIZE,
2541772Sjl139090 		"l2-cache-size", &ecache_size, OPL_ECACHE_SIZE,
2551772Sjl139090 		"l2-cache-line-size", &ecache_alignsize, OPL_ECACHE_LSIZE,
2561772Sjl139090 		"l2-cache-associativity", &ecache_associativity, OPL_ECACHE_NWAY
2571772Sjl139090 	};
2581772Sjl139090 
2591772Sjl139090 	for (i = 0; i < sizeof (prop) / sizeof (prop[0]); i++)
2601772Sjl139090 		*prop[i].var = getintprop(dnode, prop[i].name, prop[i].defval);
2611772Sjl139090 
2621772Sjl139090 	ecache_setsize = ecache_size / ecache_associativity;
2631772Sjl139090 
2641772Sjl139090 	vac_size = OPL_VAC_SIZE;
2651772Sjl139090 	vac_mask = MMU_PAGEMASK & (vac_size - 1);
2661772Sjl139090 	i = 0; a = vac_size;
2671772Sjl139090 	while (a >>= 1)
2681772Sjl139090 		++i;
2691772Sjl139090 	vac_shift = i;
2701772Sjl139090 	shm_alignment = vac_size;
2711772Sjl139090 	vac = 1;
2721772Sjl139090 }
2731772Sjl139090 
2745037Sjl139090 /*
2755037Sjl139090  * Enable features for Jupiter-only domains.
2765037Sjl139090  */
2775037Sjl139090 void
2785037Sjl139090 cpu_fix_alljupiter(void)
2795037Sjl139090 {
280*5394Sjimand 	if (!prom_SPARC64VII_support_enabled()) {
281*5394Sjimand 		/*
282*5394Sjimand 		 * Do not enable all-Jupiter features and do not turn on
283*5394Sjimand 		 * the cpu_alljupiter flag.
284*5394Sjimand 		 */
285*5394Sjimand 		return;
286*5394Sjimand 	}
287*5394Sjimand 
2885037Sjl139090 	cpu_alljupiter = 1;
2895037Sjl139090 
2905037Sjl139090 	/*
2915037Sjl139090 	 * Enable ima hwcap for Jupiter-only domains.  DR will prevent
2925037Sjl139090 	 * addition of Olympus-C to all-Jupiter domains to preserve ima
2935037Sjl139090 	 * hwcap semantics.
2945037Sjl139090 	 */
2955037Sjl139090 	cpu_hwcap_flags |= AV_SPARC_IMA;
2965037Sjl139090 }
2975037Sjl139090 
2982149Shyw #ifdef	OLYMPUS_C_REV_B_ERRATA_XCALL
2992149Shyw /*
3002149Shyw  * Quick and dirty way to redefine locally in
3012149Shyw  * OPL the value of IDSR_BN_SETS to 31 instead
3022149Shyw  * of the standard 32 value. This is to workaround
3032149Shyw  * REV_B of Olympus_c processor's problem in handling
3042149Shyw  * more than 31 xcall broadcast.
3052149Shyw  */
3062149Shyw #undef	IDSR_BN_SETS
3072149Shyw #define	IDSR_BN_SETS    31
3082149Shyw #endif	/* OLYMPUS_C_REV_B_ERRATA_XCALL */
3092149Shyw 
3101772Sjl139090 void
3111772Sjl139090 send_mondo_set(cpuset_t set)
3121772Sjl139090 {
3131772Sjl139090 	int lo, busy, nack, shipped = 0;
3141772Sjl139090 	uint16_t i, cpuids[IDSR_BN_SETS];
3151772Sjl139090 	uint64_t idsr, nackmask = 0, busymask, curnack, curbusy;
3161772Sjl139090 	uint64_t starttick, endtick, tick, lasttick;
3171772Sjl139090 #if (NCPU > IDSR_BN_SETS)
3181772Sjl139090 	int index = 0;
3191772Sjl139090 	int ncpuids = 0;
3201772Sjl139090 #endif
3211888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
3221772Sjl139090 	int bn_sets = IDSR_BN_SETS;
3231772Sjl139090 	uint64_t ver;
3241772Sjl139090 
3251772Sjl139090 	ASSERT(NCPU > bn_sets);
3261772Sjl139090 #endif
3271772Sjl139090 
3281772Sjl139090 	ASSERT(!CPUSET_ISNULL(set));
3291772Sjl139090 	starttick = lasttick = gettick();
3301772Sjl139090 
3311888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
3321772Sjl139090 	ver = ultra_getver();
3331772Sjl139090 	if (((ULTRA_VER_IMPL(ver)) == OLYMPUS_C_IMPL) &&
3345037Sjl139090 	    ((OLYMPUS_REV_MASK(ver)) == OLYMPUS_C_A))
3351772Sjl139090 		bn_sets = 1;
3361772Sjl139090 #endif
3371772Sjl139090 
3381772Sjl139090 #if (NCPU <= IDSR_BN_SETS)
3391772Sjl139090 	for (i = 0; i < NCPU; i++)
3401772Sjl139090 		if (CPU_IN_SET(set, i)) {
3411772Sjl139090 			shipit(i, shipped);
3421772Sjl139090 			nackmask |= IDSR_NACK_BIT(shipped);
3431772Sjl139090 			cpuids[shipped++] = i;
3441772Sjl139090 			CPUSET_DEL(set, i);
3451772Sjl139090 			if (CPUSET_ISNULL(set))
3461772Sjl139090 				break;
3471772Sjl139090 		}
3481772Sjl139090 	CPU_STATS_ADDQ(CPU, sys, xcalls, shipped);
3491772Sjl139090 #else
3501772Sjl139090 	for (i = 0; i < NCPU; i++)
3511772Sjl139090 		if (CPU_IN_SET(set, i)) {
3521772Sjl139090 			ncpuids++;
3531772Sjl139090 
3541772Sjl139090 			/*
3551772Sjl139090 			 * Ship only to the first (IDSR_BN_SETS) CPUs.  If we
3561772Sjl139090 			 * find we have shipped to more than (IDSR_BN_SETS)
3571772Sjl139090 			 * CPUs, set "index" to the highest numbered CPU in
3581772Sjl139090 			 * the set so we can ship to other CPUs a bit later on.
3591772Sjl139090 			 */
3601888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
3611772Sjl139090 			if (shipped < bn_sets) {
3621772Sjl139090 #else
3631772Sjl139090 			if (shipped < IDSR_BN_SETS) {
3641772Sjl139090 #endif
3651772Sjl139090 				shipit(i, shipped);
3661772Sjl139090 				nackmask |= IDSR_NACK_BIT(shipped);
3671772Sjl139090 				cpuids[shipped++] = i;
3681772Sjl139090 				CPUSET_DEL(set, i);
3691772Sjl139090 				if (CPUSET_ISNULL(set))
3701772Sjl139090 					break;
3711772Sjl139090 			} else
3721772Sjl139090 				index = (int)i;
3731772Sjl139090 		}
3741772Sjl139090 
3751772Sjl139090 	CPU_STATS_ADDQ(CPU, sys, xcalls, ncpuids);
3761772Sjl139090 #endif
3771772Sjl139090 
3781772Sjl139090 	busymask = IDSR_NACK_TO_BUSY(nackmask);
3791772Sjl139090 	busy = nack = 0;
3801772Sjl139090 	endtick = starttick + xc_tick_limit;
3811772Sjl139090 	for (;;) {
3821772Sjl139090 		idsr = getidsr();
3831772Sjl139090 #if (NCPU <= IDSR_BN_SETS)
3841772Sjl139090 		if (idsr == 0)
3851772Sjl139090 			break;
3861772Sjl139090 #else
3871772Sjl139090 		if (idsr == 0 && shipped == ncpuids)
3881772Sjl139090 			break;
3891772Sjl139090 #endif
3901772Sjl139090 		tick = gettick();
3911772Sjl139090 		/*
3921772Sjl139090 		 * If there is a big jump between the current tick
3931772Sjl139090 		 * count and lasttick, we have probably hit a break
3941772Sjl139090 		 * point.  Adjust endtick accordingly to avoid panic.
3951772Sjl139090 		 */
3961772Sjl139090 		if (tick > (lasttick + xc_tick_jump_limit))
3971772Sjl139090 			endtick += (tick - lasttick);
3981772Sjl139090 		lasttick = tick;
3991772Sjl139090 		if (tick > endtick) {
4001772Sjl139090 			if (panic_quiesce)
4011772Sjl139090 				return;
4025037Sjl139090 			cmn_err(CE_CONT, "send mondo timeout [%d NACK %d "
4035037Sjl139090 			    "BUSY]\nIDSR 0x%" PRIx64 "  cpuids:",
4045037Sjl139090 			    nack, busy, idsr);
4051888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
4061772Sjl139090 			for (i = 0; i < bn_sets; i++) {
4071772Sjl139090 #else
4081772Sjl139090 			for (i = 0; i < IDSR_BN_SETS; i++) {
4091772Sjl139090 #endif
4101772Sjl139090 				if (idsr & (IDSR_NACK_BIT(i) |
4111772Sjl139090 				    IDSR_BUSY_BIT(i))) {
4125037Sjl139090 					cmn_err(CE_CONT, " 0x%x", cpuids[i]);
4131772Sjl139090 				}
4141772Sjl139090 			}
4151772Sjl139090 			cmn_err(CE_CONT, "\n");
4161772Sjl139090 			cmn_err(CE_PANIC, "send_mondo_set: timeout");
4171772Sjl139090 		}
4181772Sjl139090 		curnack = idsr & nackmask;
4191772Sjl139090 		curbusy = idsr & busymask;
4201888Shyw 
4211888Shyw #ifdef OLYMPUS_C_REV_B_ERRATA_XCALL
4221888Shyw 		/*
4231888Shyw 		 * Only proceed to send more xcalls if all the
4241888Shyw 		 * cpus in the previous IDSR_BN_SETS were completed.
4251888Shyw 		 */
4261888Shyw 		if (curbusy) {
4271888Shyw 			busy++;
4281888Shyw 			continue;
4291888Shyw 		}
4301888Shyw #endif /* OLYMPUS_C_REV_B_ERRATA_XCALL */
4311888Shyw 
4321772Sjl139090 #if (NCPU > IDSR_BN_SETS)
4331772Sjl139090 		if (shipped < ncpuids) {
4341772Sjl139090 			uint64_t cpus_left;
4351772Sjl139090 			uint16_t next = (uint16_t)index;
4361772Sjl139090 
4371772Sjl139090 			cpus_left = ~(IDSR_NACK_TO_BUSY(curnack) | curbusy) &
4381772Sjl139090 			    busymask;
4391772Sjl139090 
4401772Sjl139090 			if (cpus_left) {
4411772Sjl139090 				do {
4421772Sjl139090 					/*
4431772Sjl139090 					 * Sequence through and ship to the
4441772Sjl139090 					 * remainder of the CPUs in the system
4451772Sjl139090 					 * (e.g. other than the first
4461772Sjl139090 					 * (IDSR_BN_SETS)) in reverse order.
4471772Sjl139090 					 */
4481772Sjl139090 					lo = lowbit(cpus_left) - 1;
4491772Sjl139090 					i = IDSR_BUSY_IDX(lo);
4501772Sjl139090 					shipit(next, i);
4511772Sjl139090 					shipped++;
4521772Sjl139090 					cpuids[i] = next;
4531772Sjl139090 
4541772Sjl139090 					/*
4551772Sjl139090 					 * If we've processed all the CPUs,
4561772Sjl139090 					 * exit the loop now and save
4571772Sjl139090 					 * instructions.
4581772Sjl139090 					 */
4591772Sjl139090 					if (shipped == ncpuids)
4601772Sjl139090 						break;
4611772Sjl139090 
4621772Sjl139090 					for ((index = ((int)next - 1));
4635037Sjl139090 					    index >= 0; index--)
4641772Sjl139090 						if (CPU_IN_SET(set, index)) {
4651772Sjl139090 							next = (uint16_t)index;
4661772Sjl139090 							break;
4671772Sjl139090 						}
4681772Sjl139090 
4691772Sjl139090 					cpus_left &= ~(1ull << lo);
4701772Sjl139090 				} while (cpus_left);
4711772Sjl139090 				continue;
4721772Sjl139090 			}
4731772Sjl139090 		}
4741772Sjl139090 #endif
4751888Shyw #ifndef	OLYMPUS_C_REV_B_ERRATA_XCALL
4761772Sjl139090 		if (curbusy) {
4771772Sjl139090 			busy++;
4781772Sjl139090 			continue;
4791772Sjl139090 		}
4801888Shyw #endif	/* OLYMPUS_C_REV_B_ERRATA_XCALL */
4811772Sjl139090 #ifdef SEND_MONDO_STATS
4821772Sjl139090 		{
4831772Sjl139090 			int n = gettick() - starttick;
4841772Sjl139090 			if (n < 8192)
4851772Sjl139090 				x_nack_stimes[n >> 7]++;
4861772Sjl139090 		}
4871772Sjl139090 #endif
4881772Sjl139090 		while (gettick() < (tick + sys_clock_mhz))
4891772Sjl139090 			;
4901772Sjl139090 		do {
4911772Sjl139090 			lo = lowbit(curnack) - 1;
4921772Sjl139090 			i = IDSR_NACK_IDX(lo);
4931772Sjl139090 			shipit(cpuids[i], i);
4941772Sjl139090 			curnack &= ~(1ull << lo);
4951772Sjl139090 		} while (curnack);
4961772Sjl139090 		nack++;
4971772Sjl139090 		busy = 0;
4981772Sjl139090 	}
4991772Sjl139090 #ifdef SEND_MONDO_STATS
5001772Sjl139090 	{
5011772Sjl139090 		int n = gettick() - starttick;
5021772Sjl139090 		if (n < 8192)
5031772Sjl139090 			x_set_stimes[n >> 7]++;
5041772Sjl139090 		else
5051772Sjl139090 			x_set_ltimes[(n >> 13) & 0xf]++;
5061772Sjl139090 	}
5071772Sjl139090 	x_set_cpus[shipped]++;
5081772Sjl139090 #endif
5091772Sjl139090 }
5101772Sjl139090 
5111772Sjl139090 /*
5121772Sjl139090  * Cpu private initialization.
5131772Sjl139090  */
5141772Sjl139090 void
5151772Sjl139090 cpu_init_private(struct cpu *cp)
5161772Sjl139090 {
5175037Sjl139090 	if (!((IS_OLYMPUS_C(cpunodes[cp->cpu_id].implementation)) ||
5185037Sjl139090 	    (IS_JUPITER(cpunodes[cp->cpu_id].implementation)))) {
5195037Sjl139090 		cmn_err(CE_PANIC, "CPU%d Impl %d: Only SPARC64-VI(I) is "
5205037Sjl139090 		    "supported", cp->cpu_id,
5215037Sjl139090 		    cpunodes[cp->cpu_id].implementation);
5221772Sjl139090 	}
5231772Sjl139090 
5241772Sjl139090 	adjust_hw_copy_limits(cpunodes[cp->cpu_id].ecache_size);
5251772Sjl139090 }
5261772Sjl139090 
5271772Sjl139090 void
5281772Sjl139090 cpu_setup(void)
5291772Sjl139090 {
5301772Sjl139090 	extern int at_flags;
5311772Sjl139090 	extern int cpc_has_overflow_intr;
5321772Sjl139090 	uint64_t cpu0_log;
5331772Sjl139090 	extern	 uint64_t opl_cpu0_err_log;
5341772Sjl139090 
5351772Sjl139090 	/*
5361772Sjl139090 	 * Initialize Error log Scratch register for error handling.
5371772Sjl139090 	 */
5381772Sjl139090 
5391772Sjl139090 	cpu0_log = va_to_pa(&opl_cpu0_err_log);
5401772Sjl139090 	opl_error_setup(cpu0_log);
5411772Sjl139090 
5421772Sjl139090 	/*
5431772Sjl139090 	 * Enable MMU translating multiple page sizes for
5441772Sjl139090 	 * sITLB and sDTLB.
5451772Sjl139090 	 */
5461772Sjl139090 	opl_mpg_enable();
5471772Sjl139090 
5481772Sjl139090 	/*
5491772Sjl139090 	 * Setup chip-specific trap handlers.
5501772Sjl139090 	 */
5511772Sjl139090 	cpu_init_trap();
5521772Sjl139090 
5531772Sjl139090 	cache |= (CACHE_VAC | CACHE_PTAG | CACHE_IOCOHERENT);
5541772Sjl139090 
5551772Sjl139090 	at_flags = EF_SPARC_32PLUS | EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3;
5561772Sjl139090 
5571772Sjl139090 	/*
5581772Sjl139090 	 * Due to the number of entries in the fully-associative tlb
5591772Sjl139090 	 * this may have to be tuned lower than in spitfire.
5601772Sjl139090 	 */
5611772Sjl139090 	pp_slots = MIN(8, MAXPP_SLOTS);
5621772Sjl139090 
5631772Sjl139090 	/*
5641772Sjl139090 	 * Block stores do not invalidate all pages of the d$, pagecopy
5651772Sjl139090 	 * et. al. need virtual translations with virtual coloring taken
5661772Sjl139090 	 * into consideration.  prefetch/ldd will pollute the d$ on the
5671772Sjl139090 	 * load side.
5681772Sjl139090 	 */
5691772Sjl139090 	pp_consistent_coloring = PPAGE_STORE_VCOLORING | PPAGE_LOADS_POLLUTE;
5701772Sjl139090 
5711772Sjl139090 	if (use_page_coloring) {
5721772Sjl139090 		do_pg_coloring = 1;
5731772Sjl139090 	}
5741772Sjl139090 
5751772Sjl139090 	isa_list =
5761772Sjl139090 	    "sparcv9+vis2 sparcv9+vis sparcv9 "
5771772Sjl139090 	    "sparcv8plus+vis2 sparcv8plus+vis sparcv8plus "
5781772Sjl139090 	    "sparcv8 sparcv8-fsmuld sparcv7 sparc";
5791772Sjl139090 
5802801Shyw 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 |
5812801Shyw 	    AV_SPARC_POPC | AV_SPARC_FMAF;
5821772Sjl139090 
5831772Sjl139090 	/*
5841772Sjl139090 	 * On SPARC64-VI, there's no hole in the virtual address space
5851772Sjl139090 	 */
5861772Sjl139090 	hole_start = hole_end = 0;
5871772Sjl139090 
5881772Sjl139090 	/*
5891772Sjl139090 	 * The kpm mapping window.
5901772Sjl139090 	 * kpm_size:
5911772Sjl139090 	 *	The size of a single kpm range.
5921772Sjl139090 	 *	The overall size will be: kpm_size * vac_colors.
5931772Sjl139090 	 * kpm_vbase:
5941772Sjl139090 	 *	The virtual start address of the kpm range within the kernel
5951772Sjl139090 	 *	virtual address space. kpm_vbase has to be kpm_size aligned.
5961772Sjl139090 	 */
5971772Sjl139090 	kpm_size = (size_t)(128ull * 1024 * 1024 * 1024 * 1024); /* 128TB */
5981772Sjl139090 	kpm_size_shift = 47;
5991772Sjl139090 	kpm_vbase = (caddr_t)0x8000000000000000ull; /* 8EB */
6001772Sjl139090 	kpm_smallpages = 1;
6011772Sjl139090 
6021772Sjl139090 	/*
6031772Sjl139090 	 * The traptrace code uses either %tick or %stick for
6041772Sjl139090 	 * timestamping.  We have %stick so we can use it.
6051772Sjl139090 	 */
6061772Sjl139090 	traptrace_use_stick = 1;
6071772Sjl139090 
6081772Sjl139090 	/*
6091772Sjl139090 	 * SPARC64-VI has a performance counter overflow interrupt
6101772Sjl139090 	 */
6111772Sjl139090 	cpc_has_overflow_intr = 1;
6121772Sjl139090 
6131772Sjl139090 	/*
6141772Sjl139090 	 * Declare that this architecture/cpu combination does not support
6151772Sjl139090 	 * fpRAS.
6161772Sjl139090 	 */
6171772Sjl139090 	fpras_implemented = 0;
6181772Sjl139090 }
6191772Sjl139090 
6201772Sjl139090 /*
6211772Sjl139090  * Called by setcpudelay
6221772Sjl139090  */
6231772Sjl139090 void
6241772Sjl139090 cpu_init_tick_freq(void)
6251772Sjl139090 {
6261772Sjl139090 	/*
6271772Sjl139090 	 * For SPARC64-VI we want to use the system clock rate as
6281772Sjl139090 	 * the basis for low level timing, due to support of mixed
6291772Sjl139090 	 * speed CPUs and power managment.
6301772Sjl139090 	 */
6311772Sjl139090 	if (system_clock_freq == 0)
6321772Sjl139090 		cmn_err(CE_PANIC, "setcpudelay: invalid system_clock_freq");
6331772Sjl139090 
6341772Sjl139090 	sys_tick_freq = system_clock_freq;
6351772Sjl139090 }
6361772Sjl139090 
6371772Sjl139090 #ifdef SEND_MONDO_STATS
6381772Sjl139090 uint32_t x_one_stimes[64];
6391772Sjl139090 uint32_t x_one_ltimes[16];
6401772Sjl139090 uint32_t x_set_stimes[64];
6411772Sjl139090 uint32_t x_set_ltimes[16];
6421772Sjl139090 uint32_t x_set_cpus[NCPU];
6431772Sjl139090 uint32_t x_nack_stimes[64];
6441772Sjl139090 #endif
6451772Sjl139090 
6461772Sjl139090 /*
6471772Sjl139090  * Note: A version of this function is used by the debugger via the KDI,
6481772Sjl139090  * and must be kept in sync with this version.  Any changes made to this
6491772Sjl139090  * function to support new chips or to accomodate errata must also be included
6501772Sjl139090  * in the KDI-specific version.  See us3_kdi.c.
6511772Sjl139090  */
6521772Sjl139090 void
6531772Sjl139090 send_one_mondo(int cpuid)
6541772Sjl139090 {
6551772Sjl139090 	int busy, nack;
6561772Sjl139090 	uint64_t idsr, starttick, endtick, tick, lasttick;
6571772Sjl139090 	uint64_t busymask;
6581772Sjl139090 
6591772Sjl139090 	CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
6601772Sjl139090 	starttick = lasttick = gettick();
6611772Sjl139090 	shipit(cpuid, 0);
6621772Sjl139090 	endtick = starttick + xc_tick_limit;
6631772Sjl139090 	busy = nack = 0;
6641772Sjl139090 	busymask = IDSR_BUSY;
6651772Sjl139090 	for (;;) {
6661772Sjl139090 		idsr = getidsr();
6671772Sjl139090 		if (idsr == 0)
6681772Sjl139090 			break;
6691772Sjl139090 
6701772Sjl139090 		tick = gettick();
6711772Sjl139090 		/*
6721772Sjl139090 		 * If there is a big jump between the current tick
6731772Sjl139090 		 * count and lasttick, we have probably hit a break
6741772Sjl139090 		 * point.  Adjust endtick accordingly to avoid panic.
6751772Sjl139090 		 */
6761772Sjl139090 		if (tick > (lasttick + xc_tick_jump_limit))
6771772Sjl139090 			endtick += (tick - lasttick);
6781772Sjl139090 		lasttick = tick;
6791772Sjl139090 		if (tick > endtick) {
6801772Sjl139090 			if (panic_quiesce)
6811772Sjl139090 				return;
6825037Sjl139090 			cmn_err(CE_PANIC, "send mondo timeout (target 0x%x) "
6835037Sjl139090 			    "[%d NACK %d BUSY]", cpuid, nack, busy);
6841772Sjl139090 		}
6851772Sjl139090 
6861772Sjl139090 		if (idsr & busymask) {
6871772Sjl139090 			busy++;
6881772Sjl139090 			continue;
6891772Sjl139090 		}
6901772Sjl139090 		drv_usecwait(1);
6911772Sjl139090 		shipit(cpuid, 0);
6921772Sjl139090 		nack++;
6931772Sjl139090 		busy = 0;
6941772Sjl139090 	}
6951772Sjl139090 #ifdef SEND_MONDO_STATS
6961772Sjl139090 	{
6971772Sjl139090 		int n = gettick() - starttick;
6981772Sjl139090 		if (n < 8192)
6991772Sjl139090 			x_one_stimes[n >> 7]++;
7001772Sjl139090 		else
7011772Sjl139090 			x_one_ltimes[(n >> 13) & 0xf]++;
7021772Sjl139090 	}
7031772Sjl139090 #endif
7041772Sjl139090 }
7051772Sjl139090 
7061772Sjl139090 /*
7071772Sjl139090  * init_mmu_page_sizes is set to one after the bootup time initialization
7081772Sjl139090  * via mmu_init_mmu_page_sizes, to indicate that mmu_page_sizes has a
7091772Sjl139090  * valid value.
7101772Sjl139090  *
7111772Sjl139090  * mmu_disable_ism_large_pages and mmu_disable_large_pages are the mmu-specific
7121772Sjl139090  * versions of disable_ism_large_pages and disable_large_pages, and feed back
7131772Sjl139090  * into those two hat variables at hat initialization time.
7141772Sjl139090  *
7151772Sjl139090  */
7161772Sjl139090 int init_mmu_page_sizes = 0;
7172991Ssusans 
7182991Ssusans static uint_t mmu_disable_large_pages = 0;
7192991Ssusans static uint_t mmu_disable_ism_large_pages = ((1 << TTE64K) |
7202659Ssusans 	(1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
7212991Ssusans static uint_t mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
7222659Ssusans 	(1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
7232991Ssusans static uint_t mmu_disable_auto_text_large_pages = ((1 << TTE64K) |
7242991Ssusans 	(1 << TTE512K));
7251772Sjl139090 
7261772Sjl139090 /*
7271772Sjl139090  * Re-initialize mmu_page_sizes and friends, for SPARC64-VI mmu support.
7281772Sjl139090  * Called during very early bootup from check_cpus_set().
7291772Sjl139090  * Can be called to verify that mmu_page_sizes are set up correctly.
7301772Sjl139090  *
7311772Sjl139090  * Set Olympus defaults. We do not use the function parameter.
7321772Sjl139090  */
7331772Sjl139090 /*ARGSUSED*/
7341772Sjl139090 int
7351772Sjl139090 mmu_init_mmu_page_sizes(int32_t not_used)
7361772Sjl139090 {
7371772Sjl139090 	if (!init_mmu_page_sizes) {
7381772Sjl139090 		mmu_page_sizes = MMU_PAGE_SIZES;
7391772Sjl139090 		mmu_hashcnt = MAX_HASHCNT;
7402659Ssusans 		mmu_ism_pagesize = DEFAULT_ISM_PAGESIZE;
7411772Sjl139090 		mmu_exported_pagesize_mask = (1 << TTE8K) |
7421772Sjl139090 		    (1 << TTE64K) | (1 << TTE512K) | (1 << TTE4M) |
7431772Sjl139090 		    (1 << TTE32M) | (1 << TTE256M);
7441772Sjl139090 		init_mmu_page_sizes = 1;
7451772Sjl139090 		return (0);
7461772Sjl139090 	}
7471772Sjl139090 	return (1);
7481772Sjl139090 }
7491772Sjl139090 
7501772Sjl139090 /* SPARC64-VI worst case DTLB parameters */
7511772Sjl139090 #ifndef	LOCKED_DTLB_ENTRIES
7521772Sjl139090 #define	LOCKED_DTLB_ENTRIES	5	/* 2 user TSBs, 2 nucleus, + OBP */
7531772Sjl139090 #endif
7541772Sjl139090 #define	TOTAL_DTLB_ENTRIES	32
7551772Sjl139090 #define	AVAIL_32M_ENTRIES	0
7561772Sjl139090 #define	AVAIL_256M_ENTRIES	0
7571772Sjl139090 #define	AVAIL_DTLB_ENTRIES	(TOTAL_DTLB_ENTRIES - LOCKED_DTLB_ENTRIES)
7581772Sjl139090 static uint64_t ttecnt_threshold[MMU_PAGE_SIZES] = {
7591772Sjl139090 	AVAIL_DTLB_ENTRIES, AVAIL_DTLB_ENTRIES,
7601772Sjl139090 	AVAIL_DTLB_ENTRIES, AVAIL_DTLB_ENTRIES,
7611772Sjl139090 	AVAIL_DTLB_ENTRIES, AVAIL_DTLB_ENTRIES};
7621772Sjl139090 
7631772Sjl139090 /*
7641772Sjl139090  * The function returns the mmu-specific values for the
7652368Sjimand  * hat's disable_large_pages, disable_ism_large_pages, and
7662991Ssusans  * disable_auto_data_large_pages and
7672991Ssusans  * disable_text_data_large_pages variables.
7681772Sjl139090  */
7692991Ssusans uint_t
7701772Sjl139090 mmu_large_pages_disabled(uint_t flag)
7711772Sjl139090 {
7722991Ssusans 	uint_t pages_disable = 0;
7732991Ssusans 	extern int use_text_pgsz64K;
7742991Ssusans 	extern int use_text_pgsz512K;
7751772Sjl139090 
7761772Sjl139090 	if (flag == HAT_LOAD) {
7771772Sjl139090 		pages_disable =  mmu_disable_large_pages;
7781772Sjl139090 	} else if (flag == HAT_LOAD_SHARE) {
7791772Sjl139090 		pages_disable = mmu_disable_ism_large_pages;
7802991Ssusans 	} else if (flag == HAT_AUTO_DATA) {
7812991Ssusans 		pages_disable = mmu_disable_auto_data_large_pages;
7822991Ssusans 	} else if (flag == HAT_AUTO_TEXT) {
7832991Ssusans 		pages_disable = mmu_disable_auto_text_large_pages;
7842991Ssusans 		if (use_text_pgsz512K) {
7852991Ssusans 			pages_disable &= ~(1 << TTE512K);
7862991Ssusans 		}
7872991Ssusans 		if (use_text_pgsz64K) {
7882991Ssusans 			pages_disable &= ~(1 << TTE64K);
7892991Ssusans 		}
7901772Sjl139090 	}
7911772Sjl139090 	return (pages_disable);
7921772Sjl139090 }
7931772Sjl139090 
7941772Sjl139090 /*
7951772Sjl139090  * mmu_init_large_pages is called with the desired ism_pagesize parameter.
7961772Sjl139090  * It may be called from set_platform_defaults, if some value other than 32M
7971772Sjl139090  * is desired.  mmu_ism_pagesize is the tunable.  If it has a bad value,
7981772Sjl139090  * then only warn, since it would be bad form to panic due to a user typo.
7991772Sjl139090  *
8001772Sjl139090  * The function re-initializes the mmu_disable_ism_large_pages variable.
8011772Sjl139090  */
8021772Sjl139090 void
8031772Sjl139090 mmu_init_large_pages(size_t ism_pagesize)
8041772Sjl139090 {
8051772Sjl139090 	switch (ism_pagesize) {
8061772Sjl139090 	case MMU_PAGESIZE4M:
8071772Sjl139090 		mmu_disable_ism_large_pages = ((1 << TTE64K) |
8081772Sjl139090 		    (1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
8092991Ssusans 		mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
8102368Sjimand 		    (1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
8111772Sjl139090 		break;
8121772Sjl139090 	case MMU_PAGESIZE32M:
8131772Sjl139090 		mmu_disable_ism_large_pages = ((1 << TTE64K) |
8141772Sjl139090 		    (1 << TTE512K) | (1 << TTE256M));
8152991Ssusans 		mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
8162368Sjimand 		    (1 << TTE512K) | (1 << TTE4M) | (1 << TTE256M));
8172991Ssusans 		adjust_data_maxlpsize(ism_pagesize);
8181772Sjl139090 		break;
8191772Sjl139090 	case MMU_PAGESIZE256M:
8201772Sjl139090 		mmu_disable_ism_large_pages = ((1 << TTE64K) |
8211772Sjl139090 		    (1 << TTE512K) | (1 << TTE32M));
8222991Ssusans 		mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
8232368Sjimand 		    (1 << TTE512K) | (1 << TTE4M) | (1 << TTE32M));
8242991Ssusans 		adjust_data_maxlpsize(ism_pagesize);
8251772Sjl139090 		break;
8261772Sjl139090 	default:
8271772Sjl139090 		cmn_err(CE_WARN, "Unrecognized mmu_ism_pagesize value 0x%lx",
8281772Sjl139090 		    ism_pagesize);
8291772Sjl139090 		break;
8301772Sjl139090 	}
8311772Sjl139090 }
8321772Sjl139090 
8331772Sjl139090 /*
8341772Sjl139090  * Function to reprogram the TLBs when page sizes used
8351772Sjl139090  * by a process change significantly.
8361772Sjl139090  */
8371772Sjl139090 void
8382272Sjimand mmu_setup_page_sizes(struct hat *hat, uint64_t *ttecnt, uint8_t *tmp_pgsz)
8391772Sjl139090 {
8401772Sjl139090 	uint8_t pgsz0, pgsz1;
8411772Sjl139090 
8421772Sjl139090 	/*
8431772Sjl139090 	 * Don't program 2nd dtlb for kernel and ism hat
8441772Sjl139090 	 */
8452272Sjimand 	ASSERT(hat->sfmmu_ismhat == NULL);
8462272Sjimand 	ASSERT(hat != ksfmmup);
8471772Sjl139090 
8481772Sjl139090 	/*
8491772Sjl139090 	 * hat->sfmmu_pgsz[] is an array whose elements
8501772Sjl139090 	 * contain a sorted order of page sizes.  Element
8511772Sjl139090 	 * 0 is the most commonly used page size, followed
8521772Sjl139090 	 * by element 1, and so on.
8531772Sjl139090 	 *
8541772Sjl139090 	 * ttecnt[] is an array of per-page-size page counts
8551772Sjl139090 	 * mapped into the process.
8561772Sjl139090 	 *
8571772Sjl139090 	 * If the HAT's choice for page sizes is unsuitable,
8581772Sjl139090 	 * we can override it here.  The new values written
8591772Sjl139090 	 * to the array will be handed back to us later to
8601772Sjl139090 	 * do the actual programming of the TLB hardware.
8611772Sjl139090 	 *
8621772Sjl139090 	 */
8632272Sjimand 	pgsz0 = (uint8_t)MIN(tmp_pgsz[0], tmp_pgsz[1]);
8642272Sjimand 	pgsz1 = (uint8_t)MAX(tmp_pgsz[0], tmp_pgsz[1]);
8651772Sjl139090 
8661772Sjl139090 	/*
8671772Sjl139090 	 * This implements PAGESIZE programming of the sTLB
8681772Sjl139090 	 * if large TTE counts don't exceed the thresholds.
8691772Sjl139090 	 */
8701772Sjl139090 	if (ttecnt[pgsz0] < ttecnt_threshold[pgsz0])
8711772Sjl139090 		pgsz0 = page_szc(MMU_PAGESIZE);
8721772Sjl139090 	if (ttecnt[pgsz1] < ttecnt_threshold[pgsz1])
8731772Sjl139090 		pgsz1 = page_szc(MMU_PAGESIZE);
8742272Sjimand 	tmp_pgsz[0] = pgsz0;
8752272Sjimand 	tmp_pgsz[1] = pgsz1;
8761772Sjl139090 	/* otherwise, accept what the HAT chose for us */
8771772Sjl139090 }
8781772Sjl139090 
8791772Sjl139090 /*
8801772Sjl139090  * The HAT calls this function when an MMU context is allocated so that we
8811772Sjl139090  * can reprogram the large TLBs appropriately for the new process using
8821772Sjl139090  * the context.
8831772Sjl139090  *
8841772Sjl139090  * The caller must hold the HAT lock.
8851772Sjl139090  */
8861772Sjl139090 void
8871772Sjl139090 mmu_set_ctx_page_sizes(struct hat *hat)
8881772Sjl139090 {
8891772Sjl139090 	uint8_t pgsz0, pgsz1;
8901772Sjl139090 	uint8_t new_cext;
8911772Sjl139090 
8921772Sjl139090 	ASSERT(sfmmu_hat_lock_held(hat));
8931772Sjl139090 	/*
8941772Sjl139090 	 * Don't program 2nd dtlb for kernel and ism hat
8951772Sjl139090 	 */
8961772Sjl139090 	if (hat->sfmmu_ismhat || hat == ksfmmup)
8971772Sjl139090 		return;
8981772Sjl139090 
8991772Sjl139090 	/*
9001772Sjl139090 	 * If supported, reprogram the TLBs to a larger pagesize.
9011772Sjl139090 	 */
9021772Sjl139090 	pgsz0 = hat->sfmmu_pgsz[0];
9031772Sjl139090 	pgsz1 = hat->sfmmu_pgsz[1];
9041772Sjl139090 	ASSERT(pgsz0 < mmu_page_sizes);
9051772Sjl139090 	ASSERT(pgsz1 < mmu_page_sizes);
9061772Sjl139090 	new_cext = TAGACCEXT_MKSZPAIR(pgsz1, pgsz0);
9071772Sjl139090 	if (hat->sfmmu_cext != new_cext) {
9082241Shuah #ifdef DEBUG
9092241Shuah 		int i;
9102241Shuah 		/*
9112241Shuah 		 * assert cnum should be invalid, this is because pagesize
9122241Shuah 		 * can only be changed after a proc's ctxs are invalidated.
9132241Shuah 		 */
9142241Shuah 		for (i = 0; i < max_mmu_ctxdoms; i++) {
9152241Shuah 			ASSERT(hat->sfmmu_ctxs[i].cnum == INVALID_CONTEXT);
9162241Shuah 		}
9172241Shuah #endif /* DEBUG */
9181772Sjl139090 		hat->sfmmu_cext = new_cext;
9191772Sjl139090 	}
9201772Sjl139090 	/*
9211772Sjl139090 	 * sfmmu_setctx_sec() will take care of the
9221772Sjl139090 	 * rest of the dirty work for us.
9231772Sjl139090 	 */
9241772Sjl139090 }
9251772Sjl139090 
9261772Sjl139090 /*
9272272Sjimand  * This function assumes that there are either four or six supported page
9282272Sjimand  * sizes and at most two programmable TLBs, so we need to decide which
9292272Sjimand  * page sizes are most important and then adjust the TLB page sizes
9302272Sjimand  * accordingly (if supported).
9312272Sjimand  *
9322272Sjimand  * If these assumptions change, this function will need to be
9332272Sjimand  * updated to support whatever the new limits are.
9342272Sjimand  */
9352272Sjimand void
9362272Sjimand mmu_check_page_sizes(sfmmu_t *sfmmup, uint64_t *ttecnt)
9372272Sjimand {
9382272Sjimand 	uint64_t sortcnt[MMU_PAGE_SIZES];
9392272Sjimand 	uint8_t tmp_pgsz[MMU_PAGE_SIZES];
9402272Sjimand 	uint8_t i, j, max;
9412272Sjimand 	uint16_t oldval, newval;
9422272Sjimand 
9432272Sjimand 	/*
9442272Sjimand 	 * We only consider reprogramming the TLBs if one or more of
9452272Sjimand 	 * the two most used page sizes changes and we're using
9462272Sjimand 	 * large pages in this process.
9472272Sjimand 	 */
9484528Spaulsan 	if (SFMMU_LGPGS_INUSE(sfmmup)) {
9492272Sjimand 		/* Sort page sizes. */
9502272Sjimand 		for (i = 0; i < mmu_page_sizes; i++) {
9512272Sjimand 			sortcnt[i] = ttecnt[i];
9522272Sjimand 		}
9532272Sjimand 		for (j = 0; j < mmu_page_sizes; j++) {
9542272Sjimand 			for (i = mmu_page_sizes - 1, max = 0; i > 0; i--) {
9552272Sjimand 				if (sortcnt[i] > sortcnt[max])
9562272Sjimand 					max = i;
9572272Sjimand 			}
9582272Sjimand 			tmp_pgsz[j] = max;
9592272Sjimand 			sortcnt[max] = 0;
9602272Sjimand 		}
9612272Sjimand 
9622272Sjimand 		oldval = sfmmup->sfmmu_pgsz[0] << 8 | sfmmup->sfmmu_pgsz[1];
9632272Sjimand 
9642272Sjimand 		mmu_setup_page_sizes(sfmmup, ttecnt, tmp_pgsz);
9652272Sjimand 
9662272Sjimand 		/* Check 2 largest values after the sort. */
9672272Sjimand 		newval = tmp_pgsz[0] << 8 | tmp_pgsz[1];
9682272Sjimand 		if (newval != oldval) {
9692272Sjimand 			sfmmu_reprog_pgsz_arr(sfmmup, tmp_pgsz);
9702272Sjimand 		}
9712272Sjimand 	}
9722272Sjimand }
9732272Sjimand 
9742272Sjimand /*
9751772Sjl139090  * Return processor specific async error structure
9761772Sjl139090  * size used.
9771772Sjl139090  */
9781772Sjl139090 int
9791772Sjl139090 cpu_aflt_size(void)
9801772Sjl139090 {
9811772Sjl139090 	return (sizeof (opl_async_flt_t));
9821772Sjl139090 }
9831772Sjl139090 
9841772Sjl139090 /*
9851772Sjl139090  * The cpu_sync_log_err() function is called via the [uc]e_drain() function to
9861772Sjl139090  * post-process CPU events that are dequeued.  As such, it can be invoked
9871772Sjl139090  * from softint context, from AST processing in the trap() flow, or from the
9881772Sjl139090  * panic flow.  We decode the CPU-specific data, and take appropriate actions.
9891772Sjl139090  * Historically this entry point was used to log the actual cmn_err(9F) text;
9901772Sjl139090  * now with FMA it is used to prepare 'flt' to be converted into an ereport.
9911772Sjl139090  * With FMA this function now also returns a flag which indicates to the
9921772Sjl139090  * caller whether the ereport should be posted (1) or suppressed (0).
9931772Sjl139090  */
9941772Sjl139090 /*ARGSUSED*/
9951772Sjl139090 static int
9961772Sjl139090 cpu_sync_log_err(void *flt)
9971772Sjl139090 {
9981772Sjl139090 	opl_async_flt_t *opl_flt = (opl_async_flt_t *)flt;
9991772Sjl139090 	struct async_flt *aflt = (struct async_flt *)flt;
10001772Sjl139090 
10011772Sjl139090 	/*
10021772Sjl139090 	 * No extra processing of urgent error events.
10031772Sjl139090 	 * Always generate ereports for these events.
10041772Sjl139090 	 */
10051772Sjl139090 	if (aflt->flt_status == OPL_ECC_URGENT_TRAP)
10061772Sjl139090 		return (1);
10071772Sjl139090 
10081772Sjl139090 	/*
10091772Sjl139090 	 * Additional processing for synchronous errors.
10101772Sjl139090 	 */
10111772Sjl139090 	switch (opl_flt->flt_type) {
10121772Sjl139090 	case OPL_CPU_INV_SFSR:
10131772Sjl139090 		return (1);
10141772Sjl139090 
10151772Sjl139090 	case OPL_CPU_SYNC_UE:
10161772Sjl139090 		/*
10171772Sjl139090 		 * The validity: SFSR_MK_UE bit has been checked
10181772Sjl139090 		 * in opl_cpu_sync_error()
10191772Sjl139090 		 * No more check is required.
10201772Sjl139090 		 *
10211772Sjl139090 		 * opl_flt->flt_eid_mod and flt_eid_sid have been set by H/W,
10221772Sjl139090 		 * and they have been retrieved in cpu_queue_events()
10231772Sjl139090 		 */
10241772Sjl139090 
10251772Sjl139090 		if (opl_flt->flt_eid_mod == OPL_ERRID_MEM) {
10261772Sjl139090 			ASSERT(aflt->flt_in_memory);
10271772Sjl139090 			/*
10281772Sjl139090 			 * We want to skip logging only if ALL the following
10291772Sjl139090 			 * conditions are true:
10301772Sjl139090 			 *
10311772Sjl139090 			 *	1. We are not panicing already.
10321772Sjl139090 			 *	2. The error is a memory error.
10331772Sjl139090 			 *	3. There is only one error.
10341772Sjl139090 			 *	4. The error is on a retired page.
10351772Sjl139090 			 *	5. The error occurred under on_trap
10361772Sjl139090 			 *	protection AFLT_PROT_EC
10371772Sjl139090 			 */
10381772Sjl139090 			if (!panicstr && aflt->flt_prot == AFLT_PROT_EC &&
10391772Sjl139090 			    page_retire_check(aflt->flt_addr, NULL) == 0) {
10401772Sjl139090 				/*
10411772Sjl139090 				 * Do not log an error from
10421772Sjl139090 				 * the retired page
10431772Sjl139090 				 */
10441772Sjl139090 				softcall(ecc_page_zero, (void *)aflt->flt_addr);
10451772Sjl139090 				return (0);
10461772Sjl139090 			}
10471772Sjl139090 			if (!panicstr)
10481772Sjl139090 				cpu_page_retire(opl_flt);
10491772Sjl139090 		}
10501772Sjl139090 		return (1);
10511772Sjl139090 
10521772Sjl139090 	case OPL_CPU_SYNC_OTHERS:
10531772Sjl139090 		/*
10541772Sjl139090 		 * For the following error cases, the processor HW does
10551772Sjl139090 		 * not set the flt_eid_mod/flt_eid_sid. Instead, SW will attempt
10561772Sjl139090 		 * to assign appropriate values here to reflect what we
10571772Sjl139090 		 * think is the most likely cause of the problem w.r.t to
10581772Sjl139090 		 * the particular error event.  For Buserr and timeout
10591772Sjl139090 		 * error event, we will assign OPL_ERRID_CHANNEL as the
10601772Sjl139090 		 * most likely reason.  For TLB parity or multiple hit
10611772Sjl139090 		 * error events, we will assign the reason as
10621772Sjl139090 		 * OPL_ERRID_CPU (cpu related problem) and set the
10631772Sjl139090 		 * flt_eid_sid to point to the cpuid.
10641772Sjl139090 		 */
10651772Sjl139090 
10661772Sjl139090 		if (opl_flt->flt_bit & (SFSR_BERR|SFSR_TO)) {
10671772Sjl139090 			/*
10681772Sjl139090 			 * flt_eid_sid will not be used for this case.
10691772Sjl139090 			 */
10701772Sjl139090 			opl_flt->flt_eid_mod = OPL_ERRID_CHANNEL;
10711772Sjl139090 		}
10721772Sjl139090 		if (opl_flt->flt_bit & (SFSR_TLB_MUL|SFSR_TLB_PRT)) {
10735037Sjl139090 			opl_flt->flt_eid_mod = OPL_ERRID_CPU;
10745037Sjl139090 			opl_flt->flt_eid_sid = aflt->flt_inst;
10751772Sjl139090 		}
10761772Sjl139090 
10771772Sjl139090 		/*
10781772Sjl139090 		 * In case of no effective error bit
10791772Sjl139090 		 */
10801772Sjl139090 		if ((opl_flt->flt_bit & SFSR_ERRS) == 0) {
10815037Sjl139090 			opl_flt->flt_eid_mod = OPL_ERRID_CPU;
10825037Sjl139090 			opl_flt->flt_eid_sid = aflt->flt_inst;
10831772Sjl139090 		}
10841772Sjl139090 		break;
10851772Sjl139090 
10861772Sjl139090 		default:
10871772Sjl139090 			return (1);
10881772Sjl139090 	}
10891772Sjl139090 	return (1);
10901772Sjl139090 }
10911772Sjl139090 
10921772Sjl139090 /*
10931772Sjl139090  * Retire the bad page that may contain the flushed error.
10941772Sjl139090  */
10951772Sjl139090 void
10961772Sjl139090 cpu_page_retire(opl_async_flt_t *opl_flt)
10971772Sjl139090 {
10981772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
10991772Sjl139090 	(void) page_retire(aflt->flt_addr, PR_UE);
11001772Sjl139090 }
11011772Sjl139090 
11021772Sjl139090 /*
11031772Sjl139090  * Invoked by error_init() early in startup and therefore before
11041772Sjl139090  * startup_errorq() is called to drain any error Q -
11051772Sjl139090  *
11061772Sjl139090  * startup()
11071772Sjl139090  *   startup_end()
11081772Sjl139090  *     error_init()
11091772Sjl139090  *       cpu_error_init()
11101772Sjl139090  * errorq_init()
11111772Sjl139090  *   errorq_drain()
11121772Sjl139090  * start_other_cpus()
11131772Sjl139090  *
11141772Sjl139090  * The purpose of this routine is to create error-related taskqs.  Taskqs
11151772Sjl139090  * are used for this purpose because cpu_lock can't be grabbed from interrupt
11161772Sjl139090  * context.
11171772Sjl139090  *
11181772Sjl139090  */
11191772Sjl139090 /*ARGSUSED*/
11201772Sjl139090 void
11211772Sjl139090 cpu_error_init(int items)
11221772Sjl139090 {
11231772Sjl139090 	opl_err_log = (opl_errlog_t *)
11241772Sjl139090 	    kmem_alloc(ERRLOG_ALLOC_SZ, KM_SLEEP);
11251772Sjl139090 	if ((uint64_t)opl_err_log & MMU_PAGEOFFSET)
11261772Sjl139090 		cmn_err(CE_PANIC, "The base address of the error log "
11271772Sjl139090 		    "is not page aligned");
11281772Sjl139090 }
11291772Sjl139090 
11301772Sjl139090 /*
11311772Sjl139090  * We route all errors through a single switch statement.
11321772Sjl139090  */
11331772Sjl139090 void
11341772Sjl139090 cpu_ue_log_err(struct async_flt *aflt)
11351772Sjl139090 {
11361772Sjl139090 	switch (aflt->flt_class) {
11371772Sjl139090 	case CPU_FAULT:
11381772Sjl139090 		if (cpu_sync_log_err(aflt))
11391772Sjl139090 			cpu_ereport_post(aflt);
11401772Sjl139090 		break;
11411772Sjl139090 
11421772Sjl139090 	case BUS_FAULT:
11431772Sjl139090 		bus_async_log_err(aflt);
11441772Sjl139090 		break;
11451772Sjl139090 
11461772Sjl139090 	default:
11471772Sjl139090 		cmn_err(CE_WARN, "discarding async error %p with invalid "
11481772Sjl139090 		    "fault class (0x%x)", (void *)aflt, aflt->flt_class);
11491772Sjl139090 		return;
11501772Sjl139090 	}
11511772Sjl139090 }
11521772Sjl139090 
11531772Sjl139090 /*
11541772Sjl139090  * Routine for panic hook callback from panic_idle().
11551772Sjl139090  *
11561772Sjl139090  * Nothing to do here.
11571772Sjl139090  */
11581772Sjl139090 void
11591772Sjl139090 cpu_async_panic_callb(void)
11601772Sjl139090 {
11611772Sjl139090 }
11621772Sjl139090 
11631772Sjl139090 /*
11641772Sjl139090  * Routine to return a string identifying the physical name
11651772Sjl139090  * associated with a memory/cache error.
11661772Sjl139090  */
11671772Sjl139090 /*ARGSUSED*/
11681772Sjl139090 int
11691772Sjl139090 cpu_get_mem_unum(int synd_status, ushort_t flt_synd, uint64_t flt_stat,
11701772Sjl139090     uint64_t flt_addr, int flt_bus_id, int flt_in_memory,
11711772Sjl139090     ushort_t flt_status, char *buf, int buflen, int *lenp)
11721772Sjl139090 {
11731772Sjl139090 	int synd_code;
11741772Sjl139090 	int ret;
11751772Sjl139090 
11761772Sjl139090 	/*
11771772Sjl139090 	 * An AFSR of -1 defaults to a memory syndrome.
11781772Sjl139090 	 */
11791772Sjl139090 	synd_code = (int)flt_synd;
11801772Sjl139090 
11811772Sjl139090 	if (&plat_get_mem_unum) {
11821772Sjl139090 		if ((ret = plat_get_mem_unum(synd_code, flt_addr, flt_bus_id,
11835037Sjl139090 		    flt_in_memory, flt_status, buf, buflen, lenp)) != 0) {
11841772Sjl139090 			buf[0] = '\0';
11851772Sjl139090 			*lenp = 0;
11861772Sjl139090 		}
11871772Sjl139090 		return (ret);
11881772Sjl139090 	}
11891772Sjl139090 	buf[0] = '\0';
11901772Sjl139090 	*lenp = 0;
11911772Sjl139090 	return (ENOTSUP);
11921772Sjl139090 }
11931772Sjl139090 
11941772Sjl139090 /*
11951772Sjl139090  * Wrapper for cpu_get_mem_unum() routine that takes an
11961772Sjl139090  * async_flt struct rather than explicit arguments.
11971772Sjl139090  */
11981772Sjl139090 int
11991772Sjl139090 cpu_get_mem_unum_aflt(int synd_status, struct async_flt *aflt,
12001772Sjl139090     char *buf, int buflen, int *lenp)
12011772Sjl139090 {
12021772Sjl139090 	/*
12031772Sjl139090 	 * We always pass -1 so that cpu_get_mem_unum will interpret this as a
12041772Sjl139090 	 * memory error.
12051772Sjl139090 	 */
12061772Sjl139090 	return (cpu_get_mem_unum(synd_status, aflt->flt_synd,
12071772Sjl139090 	    (uint64_t)-1,
12081772Sjl139090 	    aflt->flt_addr, aflt->flt_bus_id, aflt->flt_in_memory,
12091772Sjl139090 	    aflt->flt_status, buf, buflen, lenp));
12101772Sjl139090 }
12111772Sjl139090 
12121772Sjl139090 /*
12131772Sjl139090  * This routine is a more generic interface to cpu_get_mem_unum()
12141772Sjl139090  * that may be used by other modules (e.g. mm).
12151772Sjl139090  */
12161772Sjl139090 /*ARGSUSED*/
12171772Sjl139090 int
12181772Sjl139090 cpu_get_mem_name(uint64_t synd, uint64_t *afsr, uint64_t afar,
12191772Sjl139090     char *buf, int buflen, int *lenp)
12201772Sjl139090 {
12211772Sjl139090 	int synd_status, flt_in_memory, ret;
12221772Sjl139090 	ushort_t flt_status = 0;
12231772Sjl139090 	char unum[UNUM_NAMLEN];
12241772Sjl139090 
12251772Sjl139090 	/*
12261772Sjl139090 	 * Check for an invalid address.
12271772Sjl139090 	 */
12281772Sjl139090 	if (afar == (uint64_t)-1)
12291772Sjl139090 		return (ENXIO);
12301772Sjl139090 
12311772Sjl139090 	if (synd == (uint64_t)-1)
12321772Sjl139090 		synd_status = AFLT_STAT_INVALID;
12331772Sjl139090 	else
12341772Sjl139090 		synd_status = AFLT_STAT_VALID;
12351772Sjl139090 
12361772Sjl139090 	flt_in_memory = (*afsr & SFSR_MEMORY) &&
12375037Sjl139090 	    pf_is_memory(afar >> MMU_PAGESHIFT);
12381772Sjl139090 
12391772Sjl139090 	ret = cpu_get_mem_unum(synd_status, (ushort_t)synd, *afsr, afar,
12405037Sjl139090 	    CPU->cpu_id, flt_in_memory, flt_status, unum, UNUM_NAMLEN, lenp);
12411772Sjl139090 	if (ret != 0)
12421772Sjl139090 		return (ret);
12431772Sjl139090 
12441772Sjl139090 	if (*lenp >= buflen)
12451772Sjl139090 		return (ENAMETOOLONG);
12461772Sjl139090 
12471772Sjl139090 	(void) strncpy(buf, unum, buflen);
12481772Sjl139090 
12491772Sjl139090 	return (0);
12501772Sjl139090 }
12511772Sjl139090 
12521772Sjl139090 /*
12531772Sjl139090  * Routine to return memory information associated
12541772Sjl139090  * with a physical address and syndrome.
12551772Sjl139090  */
12561772Sjl139090 /*ARGSUSED*/
12571772Sjl139090 int
12581772Sjl139090 cpu_get_mem_info(uint64_t synd, uint64_t afar,
12591772Sjl139090     uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
12601772Sjl139090     int *segsp, int *banksp, int *mcidp)
12611772Sjl139090 {
12621772Sjl139090 	int synd_code = (int)synd;
12631772Sjl139090 
12641772Sjl139090 	if (afar == (uint64_t)-1)
12651772Sjl139090 		return (ENXIO);
12661772Sjl139090 
12671772Sjl139090 	if (p2get_mem_info != NULL)
12685037Sjl139090 		return ((p2get_mem_info)(synd_code, afar, mem_sizep, seg_sizep,
12695037Sjl139090 		    bank_sizep, segsp, banksp, mcidp));
12701772Sjl139090 	else
12711772Sjl139090 		return (ENOTSUP);
12721772Sjl139090 }
12731772Sjl139090 
12741772Sjl139090 /*
12751772Sjl139090  * Routine to return a string identifying the physical
12761772Sjl139090  * name associated with a cpuid.
12771772Sjl139090  */
12781772Sjl139090 int
12791772Sjl139090 cpu_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
12801772Sjl139090 {
12811772Sjl139090 	int ret;
12821772Sjl139090 	char unum[UNUM_NAMLEN];
12831772Sjl139090 
12841772Sjl139090 	if (&plat_get_cpu_unum) {
12855037Sjl139090 		if ((ret = plat_get_cpu_unum(cpuid, unum, UNUM_NAMLEN,
12865037Sjl139090 		    lenp)) != 0)
12871772Sjl139090 			return (ret);
12881772Sjl139090 	} else {
12891772Sjl139090 		return (ENOTSUP);
12901772Sjl139090 	}
12911772Sjl139090 
12921772Sjl139090 	if (*lenp >= buflen)
12931772Sjl139090 		return (ENAMETOOLONG);
12941772Sjl139090 
12951772Sjl139090 	(void) strncpy(buf, unum, *lenp);
12961772Sjl139090 
12971772Sjl139090 	return (0);
12981772Sjl139090 }
12991772Sjl139090 
13001772Sjl139090 /*
13011772Sjl139090  * This routine exports the name buffer size.
13021772Sjl139090  */
13031772Sjl139090 size_t
13041772Sjl139090 cpu_get_name_bufsize()
13051772Sjl139090 {
13061772Sjl139090 	return (UNUM_NAMLEN);
13071772Sjl139090 }
13081772Sjl139090 
13091772Sjl139090 /*
13101772Sjl139090  * Flush the entire ecache by ASI_L2_CNTL.U2_FLUSH
13111772Sjl139090  */
13121772Sjl139090 void
13131772Sjl139090 cpu_flush_ecache(void)
13141772Sjl139090 {
13151772Sjl139090 	flush_ecache(ecache_flushaddr, cpunodes[CPU->cpu_id].ecache_size,
13161772Sjl139090 	    cpunodes[CPU->cpu_id].ecache_linesize);
13171772Sjl139090 }
13181772Sjl139090 
13191772Sjl139090 static uint8_t
13201772Sjl139090 flt_to_trap_type(struct async_flt *aflt)
13211772Sjl139090 {
13221772Sjl139090 	if (aflt->flt_status & OPL_ECC_ISYNC_TRAP)
13231772Sjl139090 		return (TRAP_TYPE_ECC_I);
13241772Sjl139090 	if (aflt->flt_status & OPL_ECC_DSYNC_TRAP)
13251772Sjl139090 		return (TRAP_TYPE_ECC_D);
13261772Sjl139090 	if (aflt->flt_status & OPL_ECC_URGENT_TRAP)
13271772Sjl139090 		return (TRAP_TYPE_URGENT);
13283382Sjimand 	return (TRAP_TYPE_UNKNOWN);
13291772Sjl139090 }
13301772Sjl139090 
13311772Sjl139090 /*
13321772Sjl139090  * Encode the data saved in the opl_async_flt_t struct into
13331772Sjl139090  * the FM ereport payload.
13341772Sjl139090  */
13351772Sjl139090 /* ARGSUSED */
13361772Sjl139090 static void
13371772Sjl139090 cpu_payload_add_aflt(struct async_flt *aflt, nvlist_t *payload,
13381772Sjl139090 		nvlist_t *resource)
13391772Sjl139090 {
13401772Sjl139090 	opl_async_flt_t *opl_flt = (opl_async_flt_t *)aflt;
13411772Sjl139090 	char unum[UNUM_NAMLEN];
13421772Sjl139090 	char sbuf[21]; /* sizeof (UINT64_MAX) + '\0' */
13431772Sjl139090 	int len;
13441772Sjl139090 
13451772Sjl139090 
13461772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_SFSR) {
13471772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_SFSR,
13485037Sjl139090 		    DATA_TYPE_UINT64, aflt->flt_stat, NULL);
13491772Sjl139090 	}
13501772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_SFAR) {
13511772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_SFAR,
13525037Sjl139090 		    DATA_TYPE_UINT64, aflt->flt_addr, NULL);
13531772Sjl139090 	}
13541772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_UGESR) {
13551772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_UGESR,
13565037Sjl139090 		    DATA_TYPE_UINT64, aflt->flt_stat, NULL);
13571772Sjl139090 	}
13581772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_PC) {
13591772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_PC,
13601772Sjl139090 		    DATA_TYPE_UINT64, (uint64_t)aflt->flt_pc, NULL);
13611772Sjl139090 	}
13621772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_TL) {
13631772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_TL,
13641772Sjl139090 		    DATA_TYPE_UINT8, (uint8_t)aflt->flt_tl, NULL);
13651772Sjl139090 	}
13661772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_TT) {
13671772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_TT,
13681772Sjl139090 		    DATA_TYPE_UINT8, flt_to_trap_type(aflt), NULL);
13691772Sjl139090 	}
13701772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_PRIV) {
13711772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_PRIV,
13721772Sjl139090 		    DATA_TYPE_BOOLEAN_VALUE,
13731772Sjl139090 		    (aflt->flt_priv ? B_TRUE : B_FALSE), NULL);
13741772Sjl139090 	}
13751772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_FLT_STATUS) {
13761772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_FLT_STATUS,
13775037Sjl139090 		    DATA_TYPE_UINT64, (uint64_t)aflt->flt_status, NULL);
13781772Sjl139090 	}
13791772Sjl139090 
13801772Sjl139090 	switch (opl_flt->flt_eid_mod) {
13811772Sjl139090 	case OPL_ERRID_CPU:
13821772Sjl139090 		(void) snprintf(sbuf, sizeof (sbuf), "%llX",
13835037Sjl139090 		    (u_longlong_t)cpunodes[opl_flt->flt_eid_sid].device_id);
13841772Sjl139090 		(void) fm_fmri_cpu_set(resource, FM_CPU_SCHEME_VERSION,
13855037Sjl139090 		    NULL, opl_flt->flt_eid_sid,
13865037Sjl139090 		    (uint8_t *)&cpunodes[opl_flt->flt_eid_sid].version, sbuf);
13875037Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_RESOURCE,
13885037Sjl139090 		    DATA_TYPE_NVLIST, resource, NULL);
13891772Sjl139090 		break;
13901772Sjl139090 
13911772Sjl139090 	case OPL_ERRID_CHANNEL:
13921772Sjl139090 		/*
13931772Sjl139090 		 * No resource is created but the cpumem DE will find
13941772Sjl139090 		 * the defective path by retreiving EID from SFSR which is
13951772Sjl139090 		 * included in the payload.
13961772Sjl139090 		 */
13971772Sjl139090 		break;
13981772Sjl139090 
13991772Sjl139090 	case OPL_ERRID_MEM:
14001772Sjl139090 		(void) cpu_get_mem_unum_aflt(0, aflt, unum, UNUM_NAMLEN, &len);
14015037Sjl139090 		(void) fm_fmri_mem_set(resource, FM_MEM_SCHEME_VERSION, NULL,
14025037Sjl139090 		    unum, NULL, (uint64_t)-1);
14031772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_RESOURCE,
14045037Sjl139090 		    DATA_TYPE_NVLIST, resource, NULL);
14051772Sjl139090 		break;
14061772Sjl139090 
14071772Sjl139090 	case OPL_ERRID_PATH:
14081772Sjl139090 		/*
14091772Sjl139090 		 * No resource is created but the cpumem DE will find
14101772Sjl139090 		 * the defective path by retreiving EID from SFSR which is
14111772Sjl139090 		 * included in the payload.
14121772Sjl139090 		 */
14131772Sjl139090 		break;
14141772Sjl139090 	}
14151772Sjl139090 }
14161772Sjl139090 
14171772Sjl139090 /*
14181772Sjl139090  * Returns whether fault address is valid for this error bit and
14191772Sjl139090  * whether the address is "in memory" (i.e. pf_is_memory returns 1).
14201772Sjl139090  */
14211772Sjl139090 /*ARGSUSED*/
14221772Sjl139090 static int
14231772Sjl139090 cpu_flt_in_memory(opl_async_flt_t *opl_flt, uint64_t t_afsr_bit)
14241772Sjl139090 {
14251772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
14261772Sjl139090 
14271772Sjl139090 	if (aflt->flt_status & (OPL_ECC_SYNC_TRAP)) {
14281772Sjl139090 		return ((t_afsr_bit & SFSR_MEMORY) &&
14291772Sjl139090 		    pf_is_memory(aflt->flt_addr >> MMU_PAGESHIFT));
14301772Sjl139090 	}
14311772Sjl139090 	return (0);
14321772Sjl139090 }
14331772Sjl139090 
14341772Sjl139090 /*
14351772Sjl139090  * In OPL SCF does the stick synchronization.
14361772Sjl139090  */
14371772Sjl139090 void
14381772Sjl139090 sticksync_slave(void)
14391772Sjl139090 {
14401772Sjl139090 }
14411772Sjl139090 
14421772Sjl139090 /*
14431772Sjl139090  * In OPL SCF does the stick synchronization.
14441772Sjl139090  */
14451772Sjl139090 void
14461772Sjl139090 sticksync_master(void)
14471772Sjl139090 {
14481772Sjl139090 }
14491772Sjl139090 
14501772Sjl139090 /*
14511772Sjl139090  * Cpu private unitialization.  OPL cpus do not use the private area.
14521772Sjl139090  */
14531772Sjl139090 void
14541772Sjl139090 cpu_uninit_private(struct cpu *cp)
14551772Sjl139090 {
14561772Sjl139090 	cmp_delete_cpu(cp->cpu_id);
14571772Sjl139090 }
14581772Sjl139090 
14591772Sjl139090 /*
14601772Sjl139090  * Always flush an entire cache.
14611772Sjl139090  */
14621772Sjl139090 void
14631772Sjl139090 cpu_error_ecache_flush(void)
14641772Sjl139090 {
14651772Sjl139090 	cpu_flush_ecache();
14661772Sjl139090 }
14671772Sjl139090 
14681772Sjl139090 void
14691772Sjl139090 cpu_ereport_post(struct async_flt *aflt)
14701772Sjl139090 {
14711772Sjl139090 	char *cpu_type, buf[FM_MAX_CLASS];
14721772Sjl139090 	nv_alloc_t *nva = NULL;
14731772Sjl139090 	nvlist_t *ereport, *detector, *resource;
14741772Sjl139090 	errorq_elem_t *eqep;
14751772Sjl139090 	char sbuf[21]; /* sizeof (UINT64_MAX) + '\0' */
14761772Sjl139090 
14771772Sjl139090 	if (aflt->flt_panic || panicstr) {
14781772Sjl139090 		eqep = errorq_reserve(ereport_errorq);
14791772Sjl139090 		if (eqep == NULL)
14801772Sjl139090 			return;
14811772Sjl139090 		ereport = errorq_elem_nvl(ereport_errorq, eqep);
14821772Sjl139090 		nva = errorq_elem_nva(ereport_errorq, eqep);
14831772Sjl139090 	} else {
14841772Sjl139090 		ereport = fm_nvlist_create(nva);
14851772Sjl139090 	}
14861772Sjl139090 
14871772Sjl139090 	/*
14881772Sjl139090 	 * Create the scheme "cpu" FMRI.
14891772Sjl139090 	 */
14901772Sjl139090 	detector = fm_nvlist_create(nva);
14911772Sjl139090 	resource = fm_nvlist_create(nva);
14921772Sjl139090 	switch (cpunodes[aflt->flt_inst].implementation) {
14931772Sjl139090 	case OLYMPUS_C_IMPL:
14941772Sjl139090 		cpu_type = FM_EREPORT_CPU_SPARC64_VI;
14951772Sjl139090 		break;
14965037Sjl139090 	case JUPITER_IMPL:
14975037Sjl139090 		cpu_type = FM_EREPORT_CPU_SPARC64_VII;
14985037Sjl139090 		break;
14991772Sjl139090 	default:
15001772Sjl139090 		cpu_type = FM_EREPORT_CPU_UNSUPPORTED;
15011772Sjl139090 		break;
15021772Sjl139090 	}
15031772Sjl139090 	(void) snprintf(sbuf, sizeof (sbuf), "%llX",
15041772Sjl139090 	    (u_longlong_t)cpunodes[aflt->flt_inst].device_id);
15051772Sjl139090 	(void) fm_fmri_cpu_set(detector, FM_CPU_SCHEME_VERSION, NULL,
15061772Sjl139090 	    aflt->flt_inst, (uint8_t *)&cpunodes[aflt->flt_inst].version,
15071772Sjl139090 	    sbuf);
15081772Sjl139090 
15091772Sjl139090 	/*
15101772Sjl139090 	 * Encode all the common data into the ereport.
15111772Sjl139090 	 */
15121772Sjl139090 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s.%s",
15131772Sjl139090 	    FM_ERROR_CPU, cpu_type, aflt->flt_erpt_class);
15141772Sjl139090 
15151772Sjl139090 	fm_ereport_set(ereport, FM_EREPORT_VERSION, buf,
15161772Sjl139090 	    fm_ena_generate(aflt->flt_id, FM_ENA_FMT1), detector, NULL);
15171772Sjl139090 
15181772Sjl139090 	/*
15191772Sjl139090 	 * Encode the error specific data that was saved in
15201772Sjl139090 	 * the async_flt structure into the ereport.
15211772Sjl139090 	 */
15221772Sjl139090 	cpu_payload_add_aflt(aflt, ereport, resource);
15231772Sjl139090 
15241772Sjl139090 	if (aflt->flt_panic || panicstr) {
15251772Sjl139090 		errorq_commit(ereport_errorq, eqep, ERRORQ_SYNC);
15261772Sjl139090 	} else {
15271772Sjl139090 		(void) fm_ereport_post(ereport, EVCH_TRYHARD);
15281772Sjl139090 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
15291772Sjl139090 		fm_nvlist_destroy(detector, FM_NVA_FREE);
15301772Sjl139090 		fm_nvlist_destroy(resource, FM_NVA_FREE);
15311772Sjl139090 	}
15321772Sjl139090 }
15331772Sjl139090 
15341772Sjl139090 void
15351772Sjl139090 cpu_run_bus_error_handlers(struct async_flt *aflt, int expected)
15361772Sjl139090 {
15371772Sjl139090 	int status;
15381772Sjl139090 	ddi_fm_error_t de;
15391772Sjl139090 
15401772Sjl139090 	bzero(&de, sizeof (ddi_fm_error_t));
15411772Sjl139090 
15421772Sjl139090 	de.fme_version = DDI_FME_VERSION;
15431772Sjl139090 	de.fme_ena = fm_ena_generate(aflt->flt_id, FM_ENA_FMT1);
15441772Sjl139090 	de.fme_flag = expected;
15451772Sjl139090 	de.fme_bus_specific = (void *)aflt->flt_addr;
15461772Sjl139090 	status = ndi_fm_handler_dispatch(ddi_root_node(), NULL, &de);
15471772Sjl139090 	if ((aflt->flt_prot == AFLT_PROT_NONE) && (status == DDI_FM_FATAL))
15481772Sjl139090 		aflt->flt_panic = 1;
15491772Sjl139090 }
15501772Sjl139090 
15511772Sjl139090 void
15521772Sjl139090 cpu_errorq_dispatch(char *error_class, void *payload, size_t payload_sz,
15531772Sjl139090     errorq_t *eqp, uint_t flag)
15541772Sjl139090 {
15551772Sjl139090 	struct async_flt *aflt = (struct async_flt *)payload;
15561772Sjl139090 
15571772Sjl139090 	aflt->flt_erpt_class = error_class;
15581772Sjl139090 	errorq_dispatch(eqp, payload, payload_sz, flag);
15591772Sjl139090 }
15601772Sjl139090 
15611772Sjl139090 void
15621772Sjl139090 adjust_hw_copy_limits(int ecache_size)
15631772Sjl139090 {
15641772Sjl139090 	/*
15651772Sjl139090 	 * Set hw copy limits.
15661772Sjl139090 	 *
15671772Sjl139090 	 * /etc/system will be parsed later and can override one or more
15681772Sjl139090 	 * of these settings.
15691772Sjl139090 	 *
15701772Sjl139090 	 * At this time, ecache size seems only mildly relevant.
15711772Sjl139090 	 * We seem to run into issues with the d-cache and stalls
15721772Sjl139090 	 * we see on misses.
15731772Sjl139090 	 *
15741772Sjl139090 	 * Cycle measurement indicates that 2 byte aligned copies fare
15751772Sjl139090 	 * little better than doing things with VIS at around 512 bytes.
15761772Sjl139090 	 * 4 byte aligned shows promise until around 1024 bytes. 8 Byte
15771772Sjl139090 	 * aligned is faster whenever the source and destination data
15781772Sjl139090 	 * in cache and the total size is less than 2 Kbytes.  The 2K
15791772Sjl139090 	 * limit seems to be driven by the 2K write cache.
15801772Sjl139090 	 * When more than 2K of copies are done in non-VIS mode, stores
15811772Sjl139090 	 * backup in the write cache.  In VIS mode, the write cache is
15821772Sjl139090 	 * bypassed, allowing faster cache-line writes aligned on cache
15831772Sjl139090 	 * boundaries.
15841772Sjl139090 	 *
15851772Sjl139090 	 * In addition, in non-VIS mode, there is no prefetching, so
15861772Sjl139090 	 * for larger copies, the advantage of prefetching to avoid even
15871772Sjl139090 	 * occasional cache misses is enough to justify using the VIS code.
15881772Sjl139090 	 *
15891772Sjl139090 	 * During testing, it was discovered that netbench ran 3% slower
15901772Sjl139090 	 * when hw_copy_limit_8 was 2K or larger.  Apparently for server
15911772Sjl139090 	 * applications, data is only used once (copied to the output
15921772Sjl139090 	 * buffer, then copied by the network device off the system).  Using
15931772Sjl139090 	 * the VIS copy saves more L2 cache state.  Network copies are
15941772Sjl139090 	 * around 1.3K to 1.5K in size for historical reasons.
15951772Sjl139090 	 *
15961772Sjl139090 	 * Therefore, a limit of 1K bytes will be used for the 8 byte
15971772Sjl139090 	 * aligned copy even for large caches and 8 MB ecache.  The
15981772Sjl139090 	 * infrastructure to allow different limits for different sized
15991772Sjl139090 	 * caches is kept to allow further tuning in later releases.
16001772Sjl139090 	 */
16011772Sjl139090 
16021772Sjl139090 	if (min_ecache_size == 0 && use_hw_bcopy) {
16031772Sjl139090 		/*
16041772Sjl139090 		 * First time through - should be before /etc/system
16051772Sjl139090 		 * is read.
16061772Sjl139090 		 * Could skip the checks for zero but this lets us
16071772Sjl139090 		 * preserve any debugger rewrites.
16081772Sjl139090 		 */
16091772Sjl139090 		if (hw_copy_limit_1 == 0) {
16101772Sjl139090 			hw_copy_limit_1 = VIS_COPY_THRESHOLD;
16111772Sjl139090 			priv_hcl_1 = hw_copy_limit_1;
16121772Sjl139090 		}
16131772Sjl139090 		if (hw_copy_limit_2 == 0) {
16141772Sjl139090 			hw_copy_limit_2 = 2 * VIS_COPY_THRESHOLD;
16151772Sjl139090 			priv_hcl_2 = hw_copy_limit_2;
16161772Sjl139090 		}
16171772Sjl139090 		if (hw_copy_limit_4 == 0) {
16181772Sjl139090 			hw_copy_limit_4 = 4 * VIS_COPY_THRESHOLD;
16191772Sjl139090 			priv_hcl_4 = hw_copy_limit_4;
16201772Sjl139090 		}
16211772Sjl139090 		if (hw_copy_limit_8 == 0) {
16221772Sjl139090 			hw_copy_limit_8 = 4 * VIS_COPY_THRESHOLD;
16231772Sjl139090 			priv_hcl_8 = hw_copy_limit_8;
16241772Sjl139090 		}
16251772Sjl139090 		min_ecache_size = ecache_size;
16261772Sjl139090 	} else {
16271772Sjl139090 		/*
16281772Sjl139090 		 * MP initialization. Called *after* /etc/system has
16291772Sjl139090 		 * been parsed. One CPU has already been initialized.
16301772Sjl139090 		 * Need to cater for /etc/system having scragged one
16311772Sjl139090 		 * of our values.
16321772Sjl139090 		 */
16331772Sjl139090 		if (ecache_size == min_ecache_size) {
16341772Sjl139090 			/*
16351772Sjl139090 			 * Same size ecache. We do nothing unless we
16361772Sjl139090 			 * have a pessimistic ecache setting. In that
16371772Sjl139090 			 * case we become more optimistic (if the cache is
16381772Sjl139090 			 * large enough).
16391772Sjl139090 			 */
16401772Sjl139090 			if (hw_copy_limit_8 == 4 * VIS_COPY_THRESHOLD) {
16411772Sjl139090 				/*
16421772Sjl139090 				 * Need to adjust hw_copy_limit* from our
16431772Sjl139090 				 * pessimistic uniprocessor value to a more
16441772Sjl139090 				 * optimistic UP value *iff* it hasn't been
16451772Sjl139090 				 * reset.
16461772Sjl139090 				 */
16471772Sjl139090 				if ((ecache_size > 1048576) &&
16481772Sjl139090 				    (priv_hcl_8 == hw_copy_limit_8)) {
16491772Sjl139090 					if (ecache_size <= 2097152)
16501772Sjl139090 						hw_copy_limit_8 = 4 *
16511772Sjl139090 						    VIS_COPY_THRESHOLD;
16521772Sjl139090 					else if (ecache_size <= 4194304)
16531772Sjl139090 						hw_copy_limit_8 = 4 *
16541772Sjl139090 						    VIS_COPY_THRESHOLD;
16551772Sjl139090 					else
16561772Sjl139090 						hw_copy_limit_8 = 4 *
16571772Sjl139090 						    VIS_COPY_THRESHOLD;
16581772Sjl139090 					priv_hcl_8 = hw_copy_limit_8;
16591772Sjl139090 				}
16601772Sjl139090 			}
16611772Sjl139090 		} else if (ecache_size < min_ecache_size) {
16621772Sjl139090 			/*
16631772Sjl139090 			 * A different ecache size. Can this even happen?
16641772Sjl139090 			 */
16651772Sjl139090 			if (priv_hcl_8 == hw_copy_limit_8) {
16661772Sjl139090 				/*
16671772Sjl139090 				 * The previous value that we set
16681772Sjl139090 				 * is unchanged (i.e., it hasn't been
16691772Sjl139090 				 * scragged by /etc/system). Rewrite it.
16701772Sjl139090 				 */
16711772Sjl139090 				if (ecache_size <= 1048576)
16721772Sjl139090 					hw_copy_limit_8 = 8 *
16731772Sjl139090 					    VIS_COPY_THRESHOLD;
16741772Sjl139090 				else if (ecache_size <= 2097152)
16751772Sjl139090 					hw_copy_limit_8 = 8 *
16761772Sjl139090 					    VIS_COPY_THRESHOLD;
16771772Sjl139090 				else if (ecache_size <= 4194304)
16781772Sjl139090 					hw_copy_limit_8 = 8 *
16791772Sjl139090 					    VIS_COPY_THRESHOLD;
16801772Sjl139090 				else
16811772Sjl139090 					hw_copy_limit_8 = 10 *
16821772Sjl139090 					    VIS_COPY_THRESHOLD;
16831772Sjl139090 				priv_hcl_8 = hw_copy_limit_8;
16841772Sjl139090 				min_ecache_size = ecache_size;
16851772Sjl139090 			}
16861772Sjl139090 		}
16871772Sjl139090 	}
16881772Sjl139090 }
16891772Sjl139090 
16901772Sjl139090 #define	VIS_BLOCKSIZE		64
16911772Sjl139090 
16921772Sjl139090 int
16931772Sjl139090 dtrace_blksuword32_err(uintptr_t addr, uint32_t *data)
16941772Sjl139090 {
16951772Sjl139090 	int ret, watched;
16961772Sjl139090 
16971772Sjl139090 	watched = watch_disable_addr((void *)addr, VIS_BLOCKSIZE, S_WRITE);
16981772Sjl139090 	ret = dtrace_blksuword32(addr, data, 0);
16991772Sjl139090 	if (watched)
17001772Sjl139090 		watch_enable_addr((void *)addr, VIS_BLOCKSIZE, S_WRITE);
17011772Sjl139090 
17021772Sjl139090 	return (ret);
17031772Sjl139090 }
17041772Sjl139090 
17051772Sjl139090 void
17061772Sjl139090 opl_cpu_reg_init()
17071772Sjl139090 {
17081772Sjl139090 	uint64_t	this_cpu_log;
17091772Sjl139090 
17101772Sjl139090 	/*
17111772Sjl139090 	 * We do not need to re-initialize cpu0 registers.
17121772Sjl139090 	 */
17131772Sjl139090 	if (cpu[getprocessorid()] == &cpu0)
17141772Sjl139090 		return;
17151772Sjl139090 
17161772Sjl139090 	/*
17171772Sjl139090 	 * Initialize Error log Scratch register for error handling.
17181772Sjl139090 	 */
17191772Sjl139090 
17201772Sjl139090 	this_cpu_log = va_to_pa((void*)(((uint64_t)opl_err_log) +
17215037Sjl139090 	    ERRLOG_BUFSZ * (getprocessorid())));
17221772Sjl139090 	opl_error_setup(this_cpu_log);
17231772Sjl139090 
17241772Sjl139090 	/*
17251772Sjl139090 	 * Enable MMU translating multiple page sizes for
17261772Sjl139090 	 * sITLB and sDTLB.
17271772Sjl139090 	 */
17281772Sjl139090 	opl_mpg_enable();
17291772Sjl139090 }
17301772Sjl139090 
17311772Sjl139090 /*
17321772Sjl139090  * Queue one event in ue_queue based on ecc_type_to_info entry.
17331772Sjl139090  */
17341772Sjl139090 static void
17351772Sjl139090 cpu_queue_one_event(opl_async_flt_t *opl_flt, char *reason,
17361772Sjl139090     ecc_type_to_info_t *eccp)
17371772Sjl139090 {
17381772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
17391772Sjl139090 
17401772Sjl139090 	if (reason &&
17411772Sjl139090 	    strlen(reason) + strlen(eccp->ec_reason) < MAX_REASON_STRING) {
17421772Sjl139090 		(void) strcat(reason, eccp->ec_reason);
17431772Sjl139090 	}
17441772Sjl139090 
17451772Sjl139090 	opl_flt->flt_bit = eccp->ec_afsr_bit;
17461772Sjl139090 	opl_flt->flt_type = eccp->ec_flt_type;
17471772Sjl139090 	aflt->flt_in_memory = cpu_flt_in_memory(opl_flt, opl_flt->flt_bit);
17481772Sjl139090 	aflt->flt_payload = eccp->ec_err_payload;
17491772Sjl139090 
17501772Sjl139090 	ASSERT(aflt->flt_status & (OPL_ECC_SYNC_TRAP|OPL_ECC_URGENT_TRAP));
17515037Sjl139090 	cpu_errorq_dispatch(eccp->ec_err_class, (void *)opl_flt,
17525037Sjl139090 	    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
17531772Sjl139090 }
17541772Sjl139090 
17551772Sjl139090 /*
17561772Sjl139090  * Queue events on async event queue one event per error bit.
17571772Sjl139090  * Return number of events queued.
17581772Sjl139090  */
17591772Sjl139090 int
17601772Sjl139090 cpu_queue_events(opl_async_flt_t *opl_flt, char *reason, uint64_t t_afsr_errs)
17611772Sjl139090 {
17621772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
17631772Sjl139090 	ecc_type_to_info_t *eccp;
17641772Sjl139090 	int nevents = 0;
17651772Sjl139090 
17661772Sjl139090 	/*
17671772Sjl139090 	 * Queue expected errors, error bit and fault type must must match
17681772Sjl139090 	 * in the ecc_type_to_info table.
17691772Sjl139090 	 */
17701772Sjl139090 	for (eccp = ecc_type_to_info; t_afsr_errs != 0 && eccp->ec_desc != NULL;
17715037Sjl139090 	    eccp++) {
17721772Sjl139090 		if ((eccp->ec_afsr_bit & t_afsr_errs) != 0 &&
17731772Sjl139090 		    (eccp->ec_flags & aflt->flt_status) != 0) {
17741772Sjl139090 			/*
17751772Sjl139090 			 * UE error event can be further
17761772Sjl139090 			 * classified/breakdown into finer granularity
17771772Sjl139090 			 * based on the flt_eid_mod value set by HW.  We do
17781772Sjl139090 			 * special handling here so that we can report UE
17791772Sjl139090 			 * error in finer granularity as ue_mem,
17801772Sjl139090 			 * ue_channel, ue_cpu or ue_path.
17811772Sjl139090 			 */
17821772Sjl139090 			if (eccp->ec_flt_type == OPL_CPU_SYNC_UE) {
17835037Sjl139090 				opl_flt->flt_eid_mod = (aflt->flt_stat &
17845037Sjl139090 				    SFSR_EID_MOD) >> SFSR_EID_MOD_SHIFT;
17855037Sjl139090 				opl_flt->flt_eid_sid = (aflt->flt_stat &
17865037Sjl139090 				    SFSR_EID_SID) >> SFSR_EID_SID_SHIFT;
17871772Sjl139090 				/*
17881772Sjl139090 				 * Need to advance eccp pointer by flt_eid_mod
17891772Sjl139090 				 * so that we get an appropriate ecc pointer
17901772Sjl139090 				 *
17911772Sjl139090 				 * EID			# of advances
17921772Sjl139090 				 * ----------------------------------
17931772Sjl139090 				 * OPL_ERRID_MEM	0
17941772Sjl139090 				 * OPL_ERRID_CHANNEL	1
17951772Sjl139090 				 * OPL_ERRID_CPU	2
17961772Sjl139090 				 * OPL_ERRID_PATH	3
17971772Sjl139090 				 */
17981772Sjl139090 				eccp += opl_flt->flt_eid_mod;
17991772Sjl139090 			}
18001772Sjl139090 			cpu_queue_one_event(opl_flt, reason, eccp);
18011772Sjl139090 			t_afsr_errs &= ~eccp->ec_afsr_bit;
18021772Sjl139090 			nevents++;
18031772Sjl139090 		}
18041772Sjl139090 	}
18051772Sjl139090 
18061772Sjl139090 	return (nevents);
18071772Sjl139090 }
18081772Sjl139090 
18091772Sjl139090 /*
18101772Sjl139090  * Sync. error wrapper functions.
18111772Sjl139090  * We use these functions in order to transfer here from the
18121772Sjl139090  * nucleus trap handler information about trap type (data or
18131772Sjl139090  * instruction) and trap level (0 or above 0). This way we
18141772Sjl139090  * get rid of using SFSR's reserved bits.
18151772Sjl139090  */
18161772Sjl139090 
18171772Sjl139090 #define	OPL_SYNC_TL0	0
18181772Sjl139090 #define	OPL_SYNC_TL1	1
18191772Sjl139090 #define	OPL_ISYNC_ERR	0
18201772Sjl139090 #define	OPL_DSYNC_ERR	1
18211772Sjl139090 
18221772Sjl139090 void
18231772Sjl139090 opl_cpu_isync_tl0_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18241772Sjl139090 {
18251772Sjl139090 	uint64_t t_sfar = p_sfar;
18261772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18271772Sjl139090 
18281772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18291772Sjl139090 	    OPL_SYNC_TL0, OPL_ISYNC_ERR);
18301772Sjl139090 }
18311772Sjl139090 
18321772Sjl139090 void
18331772Sjl139090 opl_cpu_isync_tl1_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18341772Sjl139090 {
18351772Sjl139090 	uint64_t t_sfar = p_sfar;
18361772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18371772Sjl139090 
18381772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18391772Sjl139090 	    OPL_SYNC_TL1, OPL_ISYNC_ERR);
18401772Sjl139090 }
18411772Sjl139090 
18421772Sjl139090 void
18431772Sjl139090 opl_cpu_dsync_tl0_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18441772Sjl139090 {
18451772Sjl139090 	uint64_t t_sfar = p_sfar;
18461772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18471772Sjl139090 
18481772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18491772Sjl139090 	    OPL_SYNC_TL0, OPL_DSYNC_ERR);
18501772Sjl139090 }
18511772Sjl139090 
18521772Sjl139090 void
18531772Sjl139090 opl_cpu_dsync_tl1_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18541772Sjl139090 {
18551772Sjl139090 	uint64_t t_sfar = p_sfar;
18561772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18571772Sjl139090 
18581772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18591772Sjl139090 	    OPL_SYNC_TL1, OPL_DSYNC_ERR);
18601772Sjl139090 }
18611772Sjl139090 
18621772Sjl139090 /*
18631772Sjl139090  * The fj sync err handler transfers control here for UE, BERR, TO, TLB_MUL
18641772Sjl139090  * and TLB_PRT.
18651772Sjl139090  * This function is designed based on cpu_deferred_error().
18661772Sjl139090  */
18671772Sjl139090 
18681772Sjl139090 static void
18691772Sjl139090 opl_cpu_sync_error(struct regs *rp, ulong_t t_sfar, ulong_t t_sfsr,
18701772Sjl139090     uint_t tl, uint_t derr)
18711772Sjl139090 {
18721772Sjl139090 	opl_async_flt_t opl_flt;
18731772Sjl139090 	struct async_flt *aflt;
18741772Sjl139090 	int trampolined = 0;
18751772Sjl139090 	char pr_reason[MAX_REASON_STRING];
18761772Sjl139090 	uint64_t log_sfsr;
18771772Sjl139090 	int expected = DDI_FM_ERR_UNEXPECTED;
18781772Sjl139090 	ddi_acc_hdl_t *hp;
18791772Sjl139090 
18801772Sjl139090 	/*
18811772Sjl139090 	 * We need to look at p_flag to determine if the thread detected an
18821772Sjl139090 	 * error while dumping core.  We can't grab p_lock here, but it's ok
18831772Sjl139090 	 * because we just need a consistent snapshot and we know that everyone
18841772Sjl139090 	 * else will store a consistent set of bits while holding p_lock.  We
18851772Sjl139090 	 * don't have to worry about a race because SDOCORE is set once prior
18861772Sjl139090 	 * to doing i/o from the process's address space and is never cleared.
18871772Sjl139090 	 */
18881772Sjl139090 	uint_t pflag = ttoproc(curthread)->p_flag;
18891772Sjl139090 
18901772Sjl139090 	pr_reason[0] = '\0';
18911772Sjl139090 
18921772Sjl139090 	/*
18931772Sjl139090 	 * handle the specific error
18941772Sjl139090 	 */
18951772Sjl139090 	bzero(&opl_flt, sizeof (opl_async_flt_t));
18961772Sjl139090 	aflt = (struct async_flt *)&opl_flt;
18971772Sjl139090 	aflt->flt_id = gethrtime_waitfree();
18981772Sjl139090 	aflt->flt_bus_id = getprocessorid();
18991772Sjl139090 	aflt->flt_inst = CPU->cpu_id;
19001772Sjl139090 	aflt->flt_stat = t_sfsr;
19011772Sjl139090 	aflt->flt_addr = t_sfar;
19021772Sjl139090 	aflt->flt_pc = (caddr_t)rp->r_pc;
19031772Sjl139090 	aflt->flt_prot = (uchar_t)AFLT_PROT_NONE;
19041772Sjl139090 	aflt->flt_class = (uchar_t)CPU_FAULT;
19055037Sjl139090 	aflt->flt_priv = (uchar_t)(tl == 1 ? 1 : ((rp->r_tstate &
19065037Sjl139090 	    TSTATE_PRIV) ? 1 : 0));
19071772Sjl139090 	aflt->flt_tl = (uchar_t)tl;
19081772Sjl139090 	aflt->flt_panic = (uchar_t)(tl != 0 || aft_testfatal != 0 ||
19091772Sjl139090 	    (t_sfsr & (SFSR_TLB_MUL|SFSR_TLB_PRT)) != 0);
19101772Sjl139090 	aflt->flt_core = (pflag & SDOCORE) ? 1 : 0;
19111772Sjl139090 	aflt->flt_status = (derr) ? OPL_ECC_DSYNC_TRAP : OPL_ECC_ISYNC_TRAP;
19121772Sjl139090 
19131772Sjl139090 	/*
19141772Sjl139090 	 * If SFSR.FV is not set, both SFSR and SFAR/SFPAR values are uncertain.
19151772Sjl139090 	 * So, clear all error bits to avoid mis-handling and force the system
19161772Sjl139090 	 * panicked.
19171772Sjl139090 	 * We skip all the procedures below down to the panic message call.
19181772Sjl139090 	 */
19191772Sjl139090 	if (!(t_sfsr & SFSR_FV)) {
19201772Sjl139090 		opl_flt.flt_type = OPL_CPU_INV_SFSR;
19211772Sjl139090 		aflt->flt_panic = 1;
19221772Sjl139090 		aflt->flt_payload = FM_EREPORT_PAYLOAD_SYNC;
19235037Sjl139090 		cpu_errorq_dispatch(FM_EREPORT_CPU_INV_SFSR, (void *)&opl_flt,
19245037Sjl139090 		    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
19251772Sjl139090 		fm_panic("%sErrors(s)", "invalid SFSR");
19261772Sjl139090 	}
19271772Sjl139090 
19281772Sjl139090 	/*
19291772Sjl139090 	 * If either UE and MK bit is off, this is not valid UE error.
19301772Sjl139090 	 * If it is not valid UE error, clear UE & MK_UE bits to prevent
19311772Sjl139090 	 * mis-handling below.
19321772Sjl139090 	 * aflt->flt_stat keeps the original bits as a reference.
19331772Sjl139090 	 */
19341772Sjl139090 	if ((t_sfsr & (SFSR_MK_UE|SFSR_UE)) !=
19351772Sjl139090 	    (SFSR_MK_UE|SFSR_UE)) {
19361772Sjl139090 		t_sfsr &= ~(SFSR_MK_UE|SFSR_UE);
19371772Sjl139090 	}
19381772Sjl139090 
19391772Sjl139090 	/*
19401772Sjl139090 	 * If the trap occurred in privileged mode at TL=0, we need to check to
19411772Sjl139090 	 * see if we were executing in the kernel under on_trap() or t_lofault
19421772Sjl139090 	 * protection.  If so, modify the saved registers so that we return
19431772Sjl139090 	 * from the trap to the appropriate trampoline routine.
19441772Sjl139090 	 */
19451772Sjl139090 	if (!aflt->flt_panic && aflt->flt_priv && tl == 0) {
19461772Sjl139090 		if (curthread->t_ontrap != NULL) {
19471772Sjl139090 			on_trap_data_t *otp = curthread->t_ontrap;
19481772Sjl139090 
19491772Sjl139090 			if (otp->ot_prot & OT_DATA_EC) {
19501772Sjl139090 				aflt->flt_prot = (uchar_t)AFLT_PROT_EC;
19511772Sjl139090 				otp->ot_trap |= (ushort_t)OT_DATA_EC;
19521772Sjl139090 				rp->r_pc = otp->ot_trampoline;
19531772Sjl139090 				rp->r_npc = rp->r_pc + 4;
19541772Sjl139090 				trampolined = 1;
19551772Sjl139090 			}
19561772Sjl139090 
19571772Sjl139090 			if ((t_sfsr & (SFSR_TO | SFSR_BERR)) &&
19581772Sjl139090 			    (otp->ot_prot & OT_DATA_ACCESS)) {
19591772Sjl139090 				aflt->flt_prot = (uchar_t)AFLT_PROT_ACCESS;
19601772Sjl139090 				otp->ot_trap |= (ushort_t)OT_DATA_ACCESS;
19611772Sjl139090 				rp->r_pc = otp->ot_trampoline;
19621772Sjl139090 				rp->r_npc = rp->r_pc + 4;
19631772Sjl139090 				trampolined = 1;
19641772Sjl139090 				/*
19651772Sjl139090 				 * for peeks and caut_gets errors are expected
19661772Sjl139090 				 */
19671772Sjl139090 				hp = (ddi_acc_hdl_t *)otp->ot_handle;
19681772Sjl139090 				if (!hp)
19691772Sjl139090 					expected = DDI_FM_ERR_PEEK;
19701772Sjl139090 				else if (hp->ah_acc.devacc_attr_access ==
19711772Sjl139090 				    DDI_CAUTIOUS_ACC)
19721772Sjl139090 					expected = DDI_FM_ERR_EXPECTED;
19731772Sjl139090 			}
19741772Sjl139090 
19751772Sjl139090 		} else if (curthread->t_lofault) {
19761772Sjl139090 			aflt->flt_prot = AFLT_PROT_COPY;
19771772Sjl139090 			rp->r_g1 = EFAULT;
19781772Sjl139090 			rp->r_pc = curthread->t_lofault;
19791772Sjl139090 			rp->r_npc = rp->r_pc + 4;
19801772Sjl139090 			trampolined = 1;
19811772Sjl139090 		}
19821772Sjl139090 	}
19831772Sjl139090 
19841772Sjl139090 	/*
19851772Sjl139090 	 * If we're in user mode or we're doing a protected copy, we either
19861772Sjl139090 	 * want the ASTON code below to send a signal to the user process
19871772Sjl139090 	 * or we want to panic if aft_panic is set.
19881772Sjl139090 	 *
19891772Sjl139090 	 * If we're in privileged mode and we're not doing a copy, then we
19901772Sjl139090 	 * need to check if we've trampolined.  If we haven't trampolined,
19911772Sjl139090 	 * we should panic.
19921772Sjl139090 	 */
19931772Sjl139090 	if (!aflt->flt_priv || aflt->flt_prot == AFLT_PROT_COPY) {
19941772Sjl139090 		if (t_sfsr & (SFSR_ERRS & ~(SFSR_BERR | SFSR_TO)))
19951772Sjl139090 			aflt->flt_panic |= aft_panic;
19961772Sjl139090 	} else if (!trampolined) {
19971772Sjl139090 		aflt->flt_panic = 1;
19981772Sjl139090 	}
19991772Sjl139090 
20001772Sjl139090 	/*
20011772Sjl139090 	 * If we've trampolined due to a privileged TO or BERR, or if an
20021772Sjl139090 	 * unprivileged TO or BERR occurred, we don't want to enqueue an
20031772Sjl139090 	 * event for that TO or BERR.  Queue all other events (if any) besides
20041772Sjl139090 	 * the TO/BERR.
20051772Sjl139090 	 */
20061772Sjl139090 	log_sfsr = t_sfsr;
20071772Sjl139090 	if (trampolined) {
20081772Sjl139090 		log_sfsr &= ~(SFSR_TO | SFSR_BERR);
20091772Sjl139090 	} else if (!aflt->flt_priv) {
20101772Sjl139090 		/*
20111772Sjl139090 		 * User mode, suppress messages if
20121772Sjl139090 		 * cpu_berr_to_verbose is not set.
20131772Sjl139090 		 */
20141772Sjl139090 		if (!cpu_berr_to_verbose)
20151772Sjl139090 			log_sfsr &= ~(SFSR_TO | SFSR_BERR);
20161772Sjl139090 	}
20171772Sjl139090 
20185037Sjl139090 	if (((log_sfsr & SFSR_ERRS) && (cpu_queue_events(&opl_flt, pr_reason,
20195037Sjl139090 	    t_sfsr) == 0)) || ((t_sfsr & SFSR_ERRS) == 0)) {
20201772Sjl139090 		opl_flt.flt_type = OPL_CPU_INV_SFSR;
20211772Sjl139090 		aflt->flt_payload = FM_EREPORT_PAYLOAD_SYNC;
20225037Sjl139090 		cpu_errorq_dispatch(FM_EREPORT_CPU_INV_SFSR, (void *)&opl_flt,
20235037Sjl139090 		    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
20241772Sjl139090 	}
20251772Sjl139090 
20261772Sjl139090 	if (t_sfsr & (SFSR_UE|SFSR_TO|SFSR_BERR)) {
20271772Sjl139090 		cpu_run_bus_error_handlers(aflt, expected);
20281772Sjl139090 	}
20291772Sjl139090 
20301772Sjl139090 	/*
20311772Sjl139090 	 * Panic here if aflt->flt_panic has been set.  Enqueued errors will
20321772Sjl139090 	 * be logged as part of the panic flow.
20331772Sjl139090 	 */
20341772Sjl139090 	if (aflt->flt_panic) {
20351772Sjl139090 		if (pr_reason[0] == 0)
20361772Sjl139090 			strcpy(pr_reason, "invalid SFSR ");
20371772Sjl139090 
20381772Sjl139090 		fm_panic("%sErrors(s)", pr_reason);
20391772Sjl139090 	}
20401772Sjl139090 
20411772Sjl139090 	/*
20421772Sjl139090 	 * If we queued an error and we are going to return from the trap and
20431772Sjl139090 	 * the error was in user mode or inside of a copy routine, set AST flag
20441772Sjl139090 	 * so the queue will be drained before returning to user mode.  The
20451772Sjl139090 	 * AST processing will also act on our failure policy.
20461772Sjl139090 	 */
20471772Sjl139090 	if (!aflt->flt_priv || aflt->flt_prot == AFLT_PROT_COPY) {
20481772Sjl139090 		int pcb_flag = 0;
20491772Sjl139090 
20505037Sjl139090 		if (t_sfsr & (SFSR_ERRS & ~(SFSR_BERR | SFSR_TO)))
20511772Sjl139090 			pcb_flag |= ASYNC_HWERR;
20521772Sjl139090 
20531772Sjl139090 		if (t_sfsr & SFSR_BERR)
20541772Sjl139090 			pcb_flag |= ASYNC_BERR;
20551772Sjl139090 
20561772Sjl139090 		if (t_sfsr & SFSR_TO)
20571772Sjl139090 			pcb_flag |= ASYNC_BTO;
20581772Sjl139090 
20591772Sjl139090 		ttolwp(curthread)->lwp_pcb.pcb_flags |= pcb_flag;
20601772Sjl139090 		aston(curthread);
20611772Sjl139090 	}
20621772Sjl139090 }
20631772Sjl139090 
20641772Sjl139090 /*ARGSUSED*/
20651772Sjl139090 void
20661772Sjl139090 opl_cpu_urgent_error(struct regs *rp, ulong_t p_ugesr, ulong_t tl)
20671772Sjl139090 {
20681772Sjl139090 	opl_async_flt_t opl_flt;
20691772Sjl139090 	struct async_flt *aflt;
20701772Sjl139090 	char pr_reason[MAX_REASON_STRING];
20711772Sjl139090 
20721772Sjl139090 	/* normalize tl */
20731772Sjl139090 	tl = (tl >= 2 ? 1 : 0);
20741772Sjl139090 	pr_reason[0] = '\0';
20751772Sjl139090 
20761772Sjl139090 	bzero(&opl_flt, sizeof (opl_async_flt_t));
20771772Sjl139090 	aflt = (struct async_flt *)&opl_flt;
20781772Sjl139090 	aflt->flt_id = gethrtime_waitfree();
20791772Sjl139090 	aflt->flt_bus_id = getprocessorid();
20801772Sjl139090 	aflt->flt_inst = CPU->cpu_id;
20811772Sjl139090 	aflt->flt_stat = p_ugesr;
20821772Sjl139090 	aflt->flt_pc = (caddr_t)rp->r_pc;
20831772Sjl139090 	aflt->flt_class = (uchar_t)CPU_FAULT;
20841772Sjl139090 	aflt->flt_tl = tl;
20855037Sjl139090 	aflt->flt_priv = (uchar_t)(tl == 1 ? 1 : ((rp->r_tstate & TSTATE_PRIV) ?
20865037Sjl139090 	    1 : 0));
20871772Sjl139090 	aflt->flt_status = OPL_ECC_URGENT_TRAP;
20881772Sjl139090 	aflt->flt_panic = 1;
20891772Sjl139090 	/*
20901772Sjl139090 	 * HW does not set mod/sid in case of urgent error.
20911772Sjl139090 	 * So we have to set it here.
20921772Sjl139090 	 */
20931772Sjl139090 	opl_flt.flt_eid_mod = OPL_ERRID_CPU;
20941772Sjl139090 	opl_flt.flt_eid_sid = aflt->flt_inst;
20951772Sjl139090 
20961772Sjl139090 	if (cpu_queue_events(&opl_flt, pr_reason, p_ugesr) == 0) {
20971772Sjl139090 		opl_flt.flt_type = OPL_CPU_INV_UGESR;
20981772Sjl139090 		aflt->flt_payload = FM_EREPORT_PAYLOAD_URGENT;
20995037Sjl139090 		cpu_errorq_dispatch(FM_EREPORT_CPU_INV_URG, (void *)&opl_flt,
21005037Sjl139090 		    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
21011772Sjl139090 	}
21021772Sjl139090 
21031772Sjl139090 	fm_panic("Urgent Error");
21041772Sjl139090 }
21051772Sjl139090 
21061772Sjl139090 /*
21071772Sjl139090  * Initialization error counters resetting.
21081772Sjl139090  */
21091772Sjl139090 /* ARGSUSED */
21101772Sjl139090 static void
21111772Sjl139090 opl_ras_online(void *arg, cpu_t *cp, cyc_handler_t *hdlr, cyc_time_t *when)
21121772Sjl139090 {
21131772Sjl139090 	hdlr->cyh_func = (cyc_func_t)ras_cntr_reset;
21141772Sjl139090 	hdlr->cyh_level = CY_LOW_LEVEL;
21151772Sjl139090 	hdlr->cyh_arg = (void *)(uintptr_t)cp->cpu_id;
21161772Sjl139090 
21171772Sjl139090 	when->cyt_when = cp->cpu_id * (((hrtime_t)NANOSEC * 10)/ NCPU);
21181772Sjl139090 	when->cyt_interval = (hrtime_t)NANOSEC * opl_async_check_interval;
21191772Sjl139090 }
21201772Sjl139090 
21211772Sjl139090 void
21221772Sjl139090 cpu_mp_init(void)
21231772Sjl139090 {
21241772Sjl139090 	cyc_omni_handler_t hdlr;
21251772Sjl139090 
21261772Sjl139090 	hdlr.cyo_online = opl_ras_online;
21271772Sjl139090 	hdlr.cyo_offline = NULL;
21281772Sjl139090 	hdlr.cyo_arg = NULL;
21291772Sjl139090 	mutex_enter(&cpu_lock);
21301772Sjl139090 	(void) cyclic_add_omni(&hdlr);
21311772Sjl139090 	mutex_exit(&cpu_lock);
21321772Sjl139090 }
21331772Sjl139090 
21343840Sjimand int heaplp_use_stlb = 0;
21353309Sjimand 
21361772Sjl139090 void
21371772Sjl139090 mmu_init_kernel_pgsz(struct hat *hat)
21381772Sjl139090 {
21393309Sjimand 	uint_t tte = page_szc(segkmem_lpsize);
21403309Sjimand 	uchar_t new_cext_primary, new_cext_nucleus;
21413309Sjimand 
21423309Sjimand 	if (heaplp_use_stlb == 0) {
21433309Sjimand 		/* do not reprogram stlb */
21443309Sjimand 		tte = TTE8K;
21453309Sjimand 	}
21463309Sjimand 
21473309Sjimand 	new_cext_nucleus = TAGACCEXT_MKSZPAIR(tte, TTE8K);
21483309Sjimand 	new_cext_primary = TAGACCEXT_MKSZPAIR(TTE8K, tte);
21493309Sjimand 
21503309Sjimand 	hat->sfmmu_cext = new_cext_primary;
21513309Sjimand 	kcontextreg = ((uint64_t)new_cext_nucleus << CTXREG_NEXT_SHIFT) |
21525037Sjl139090 	    ((uint64_t)new_cext_primary << CTXREG_EXT_SHIFT);
21531772Sjl139090 }
21541772Sjl139090 
21551772Sjl139090 size_t
21561772Sjl139090 mmu_get_kernel_lpsize(size_t lpsize)
21571772Sjl139090 {
21581772Sjl139090 	uint_t tte;
21591772Sjl139090 
21601772Sjl139090 	if (lpsize == 0) {
21611772Sjl139090 		/* no setting for segkmem_lpsize in /etc/system: use default */
21621772Sjl139090 		return (MMU_PAGESIZE4M);
21631772Sjl139090 	}
21641772Sjl139090 
21651772Sjl139090 	for (tte = TTE8K; tte <= TTE4M; tte++) {
21661772Sjl139090 		if (lpsize == TTEBYTES(tte))
21671772Sjl139090 			return (lpsize);
21681772Sjl139090 	}
21691772Sjl139090 
21701772Sjl139090 	return (TTEBYTES(TTE8K));
21711772Sjl139090 }
21721772Sjl139090 
21731772Sjl139090 /*
21741772Sjl139090  * The following are functions that are unused in
21751772Sjl139090  * OPL cpu module. They are defined here to resolve
21761772Sjl139090  * dependencies in the "unix" module.
21771772Sjl139090  * Unused functions that should never be called in
21781772Sjl139090  * OPL are coded with ASSERT(0).
21791772Sjl139090  */
21801772Sjl139090 
21811772Sjl139090 void
21821772Sjl139090 cpu_disable_errors(void)
21831772Sjl139090 {}
21841772Sjl139090 
21851772Sjl139090 void
21861772Sjl139090 cpu_enable_errors(void)
21871772Sjl139090 { ASSERT(0); }
21881772Sjl139090 
21891772Sjl139090 /*ARGSUSED*/
21901772Sjl139090 void
21911772Sjl139090 cpu_ce_scrub_mem_err(struct async_flt *ecc, boolean_t t)
21921772Sjl139090 { ASSERT(0); }
21931772Sjl139090 
21941772Sjl139090 /*ARGSUSED*/
21951772Sjl139090 void
21961772Sjl139090 cpu_faulted_enter(struct cpu *cp)
21971772Sjl139090 {}
21981772Sjl139090 
21991772Sjl139090 /*ARGSUSED*/
22001772Sjl139090 void
22011772Sjl139090 cpu_faulted_exit(struct cpu *cp)
22021772Sjl139090 {}
22031772Sjl139090 
22041772Sjl139090 /*ARGSUSED*/
22051772Sjl139090 void
22061772Sjl139090 cpu_check_allcpus(struct async_flt *aflt)
22071772Sjl139090 {}
22081772Sjl139090 
22091772Sjl139090 /*ARGSUSED*/
22101772Sjl139090 void
22111772Sjl139090 cpu_ce_log_err(struct async_flt *aflt, errorq_elem_t *t)
22121772Sjl139090 { ASSERT(0); }
22131772Sjl139090 
22141772Sjl139090 /*ARGSUSED*/
22151772Sjl139090 void
22161772Sjl139090 cpu_check_ce(int flag, uint64_t pa, caddr_t va, uint_t psz)
22171772Sjl139090 { ASSERT(0); }
22181772Sjl139090 
22191772Sjl139090 /*ARGSUSED*/
22201772Sjl139090 void
22211772Sjl139090 cpu_ce_count_unum(struct async_flt *ecc, int len, char *unum)
22221772Sjl139090 { ASSERT(0); }
22231772Sjl139090 
22241772Sjl139090 /*ARGSUSED*/
22251772Sjl139090 void
22261772Sjl139090 cpu_busy_ecache_scrub(struct cpu *cp)
22271772Sjl139090 {}
22281772Sjl139090 
22291772Sjl139090 /*ARGSUSED*/
22301772Sjl139090 void
22311772Sjl139090 cpu_idle_ecache_scrub(struct cpu *cp)
22321772Sjl139090 {}
22331772Sjl139090 
22341772Sjl139090 /* ARGSUSED */
22351772Sjl139090 void
22361772Sjl139090 cpu_change_speed(uint64_t divisor, uint64_t arg2)
22371772Sjl139090 { ASSERT(0); }
22381772Sjl139090 
22391772Sjl139090 void
22401772Sjl139090 cpu_init_cache_scrub(void)
22411772Sjl139090 {}
22421772Sjl139090 
22431772Sjl139090 /* ARGSUSED */
22441772Sjl139090 int
22451772Sjl139090 cpu_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
22461772Sjl139090 {
22472214Sav145390 	if (&plat_get_mem_sid) {
22482214Sav145390 		return (plat_get_mem_sid(unum, buf, buflen, lenp));
22492214Sav145390 	} else {
22502214Sav145390 		return (ENOTSUP);
22512214Sav145390 	}
22521772Sjl139090 }
22531772Sjl139090 
22541772Sjl139090 /* ARGSUSED */
22551772Sjl139090 int
22561772Sjl139090 cpu_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp)
22571772Sjl139090 {
22582214Sav145390 	if (&plat_get_mem_addr) {
22592214Sav145390 		return (plat_get_mem_addr(unum, sid, offset, addrp));
22602214Sav145390 	} else {
22612214Sav145390 		return (ENOTSUP);
22622214Sav145390 	}
22631772Sjl139090 }
22641772Sjl139090 
22651772Sjl139090 /* ARGSUSED */
22661772Sjl139090 int
22671772Sjl139090 cpu_get_mem_offset(uint64_t flt_addr, uint64_t *offp)
22681772Sjl139090 {
22692214Sav145390 	if (&plat_get_mem_offset) {
22702214Sav145390 		return (plat_get_mem_offset(flt_addr, offp));
22712214Sav145390 	} else {
22722214Sav145390 		return (ENOTSUP);
22732214Sav145390 	}
22741772Sjl139090 }
22751772Sjl139090 
22761772Sjl139090 /*ARGSUSED*/
22771772Sjl139090 void
22781772Sjl139090 itlb_rd_entry(uint_t entry, tte_t *tte, uint64_t *va_tag)
22791772Sjl139090 { ASSERT(0); }
22801772Sjl139090 
22811772Sjl139090 /*ARGSUSED*/
22821772Sjl139090 void
22831772Sjl139090 dtlb_rd_entry(uint_t entry, tte_t *tte, uint64_t *va_tag)
22841772Sjl139090 { ASSERT(0); }
22852203Shyw 
22862203Shyw /*ARGSUSED*/
22872203Shyw void
22882203Shyw read_ecc_data(struct async_flt *aflt, short verbose, short ce_err)
22892203Shyw { ASSERT(0); }
22902203Shyw 
22912203Shyw /*ARGSUSED*/
22922203Shyw int
22932203Shyw ce_scrub_xdiag_recirc(struct async_flt *aflt, errorq_t *eqp,
22942203Shyw     errorq_elem_t *eqep, size_t afltoffset)
22952203Shyw {
22962203Shyw 	ASSERT(0);
22972203Shyw 	return (0);
22982203Shyw }
22992203Shyw 
23002203Shyw /*ARGSUSED*/
23012203Shyw char *
23022203Shyw flt_to_error_type(struct async_flt *aflt)
23032203Shyw {
23042203Shyw 	ASSERT(0);
23052203Shyw 	return (NULL);
23062203Shyw }
2307*5394Sjimand 
2308*5394Sjimand #define	PROM_SPARC64VII_MODE_PROPNAME	"SPARC64-VII-mode"
2309*5394Sjimand 
2310*5394Sjimand /*
2311*5394Sjimand  * Check for existence of OPL OBP property that indicates
2312*5394Sjimand  * SPARC64-VII support. By default, only enable Jupiter
2313*5394Sjimand  * features if the property is present.   It will be
2314*5394Sjimand  * present in all-Jupiter domains by OBP if the domain has
2315*5394Sjimand  * been selected by the user on the system controller to
2316*5394Sjimand  * run in Jupiter mode.  Basically, this OBP property must
2317*5394Sjimand  * be present to turn on the cpu_alljupiter flag.
2318*5394Sjimand  */
2319*5394Sjimand static int
2320*5394Sjimand prom_SPARC64VII_support_enabled(void)
2321*5394Sjimand {
2322*5394Sjimand 	int val;
2323*5394Sjimand 
2324*5394Sjimand 	return ((prom_getprop(prom_rootnode(), PROM_SPARC64VII_MODE_PROPNAME,
2325*5394Sjimand 	    (caddr_t)&val) == 0) ? 1 : 0);
2326*5394Sjimand }
2327