xref: /onnv-gate/usr/src/uts/sun4u/cpu/opl_olympus.c (revision 8664:fa4744ba0999)
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 /*
22*8664SJames.Anderson@Sun.COM  * Copyright 2009 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 #include <sys/types.h>
311772Sjl139090 #include <sys/systm.h>
321772Sjl139090 #include <sys/ddi.h>
331772Sjl139090 #include <sys/sysmacros.h>
341772Sjl139090 #include <sys/archsystm.h>
351772Sjl139090 #include <sys/vmsystm.h>
361772Sjl139090 #include <sys/machparam.h>
371772Sjl139090 #include <sys/machsystm.h>
381772Sjl139090 #include <sys/machthread.h>
391772Sjl139090 #include <sys/cpu.h>
401772Sjl139090 #include <sys/cmp.h>
411772Sjl139090 #include <sys/elf_SPARC.h>
421772Sjl139090 #include <vm/vm_dep.h>
431772Sjl139090 #include <vm/hat_sfmmu.h>
441772Sjl139090 #include <vm/seg_kpm.h>
453309Sjimand #include <vm/seg_kmem.h>
461772Sjl139090 #include <sys/cpuvar.h>
471772Sjl139090 #include <sys/opl_olympus_regs.h>
481772Sjl139090 #include <sys/opl_module.h>
491772Sjl139090 #include <sys/async.h>
501772Sjl139090 #include <sys/cmn_err.h>
511772Sjl139090 #include <sys/debug.h>
521772Sjl139090 #include <sys/dditypes.h>
531772Sjl139090 #include <sys/cpu_module.h>
541772Sjl139090 #include <sys/sysmacros.h>
551772Sjl139090 #include <sys/intreg.h>
561772Sjl139090 #include <sys/clock.h>
571772Sjl139090 #include <sys/platform_module.h>
581772Sjl139090 #include <sys/ontrap.h>
591772Sjl139090 #include <sys/panic.h>
601772Sjl139090 #include <sys/memlist.h>
611772Sjl139090 #include <sys/ndifm.h>
621772Sjl139090 #include <sys/ddifm.h>
631772Sjl139090 #include <sys/fm/protocol.h>
641772Sjl139090 #include <sys/fm/util.h>
651772Sjl139090 #include <sys/fm/cpu/SPARC64-VI.h>
661772Sjl139090 #include <sys/dtrace.h>
671772Sjl139090 #include <sys/watchpoint.h>
681772Sjl139090 #include <sys/promif.h>
691772Sjl139090 
701772Sjl139090 /*
711772Sjl139090  * Internal functions.
721772Sjl139090  */
731772Sjl139090 static int cpu_sync_log_err(void *flt);
741772Sjl139090 static void cpu_payload_add_aflt(struct async_flt *, nvlist_t *, nvlist_t *);
751772Sjl139090 static void opl_cpu_sync_error(struct regs *, ulong_t, ulong_t, uint_t, uint_t);
761772Sjl139090 static int  cpu_flt_in_memory(opl_async_flt_t *, uint64_t);
775394Sjimand static int prom_SPARC64VII_support_enabled(void);
785414Smb158278 static void opl_ta3();
795584Sjimand static int plat_prom_preserve_kctx_is_supported(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 
1046127Ssm142603 /*
1056127Ssm142603  * The sfmmu_cext field to be used by processes in a shared context domain.
1066127Ssm142603  */
1076127Ssm142603 static uchar_t shctx_cext = TAGACCEXT_MKSZPAIR(DEFAULT_ISM_PAGESZC, TTE8K);
1086127Ssm142603 
1091772Sjl139090 static int min_ecache_size;
1101772Sjl139090 static uint_t priv_hcl_1;
1111772Sjl139090 static uint_t priv_hcl_2;
1121772Sjl139090 static uint_t priv_hcl_4;
1131772Sjl139090 static uint_t priv_hcl_8;
1141772Sjl139090 
1151772Sjl139090 /*
1161772Sjl139090  * Olympus error log
1171772Sjl139090  */
1181772Sjl139090 static opl_errlog_t	*opl_err_log;
1196259Smb158278 static int		opl_cpu0_log_setup;
1201772Sjl139090 
1211772Sjl139090 /*
1225414Smb158278  * OPL ta 3 save area.
1235414Smb158278  */
1245414Smb158278 char	*opl_ta3_save;
1255414Smb158278 
1265414Smb158278 /*
1271772Sjl139090  * UE is classified into four classes (MEM, CHANNEL, CPU, PATH).
1281772Sjl139090  * No any other ecc_type_info insertion is allowed in between the following
1291772Sjl139090  * four UE classess.
1301772Sjl139090  */
1311772Sjl139090 ecc_type_to_info_t ecc_type_to_info[] = {
1321772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1331772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1341772Sjl139090 	FM_EREPORT_CPU_UE_MEM,
1351772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1361772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1371772Sjl139090 	FM_EREPORT_CPU_UE_CHANNEL,
1381772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1391772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1401772Sjl139090 	FM_EREPORT_CPU_UE_CPU,
1411772Sjl139090 	SFSR_UE,	"UE ",	(OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_UE,
1421772Sjl139090 	"Uncorrectable ECC",  FM_EREPORT_PAYLOAD_SYNC,
1431772Sjl139090 	FM_EREPORT_CPU_UE_PATH,
1441772Sjl139090 	SFSR_BERR, "BERR ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1451772Sjl139090 	"Bus Error",  FM_EREPORT_PAYLOAD_SYNC,
1461772Sjl139090 	FM_EREPORT_CPU_BERR,
1471772Sjl139090 	SFSR_TO, "TO ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1481772Sjl139090 	"Bus Timeout",  FM_EREPORT_PAYLOAD_SYNC,
1491772Sjl139090 	FM_EREPORT_CPU_BTO,
1501772Sjl139090 	SFSR_TLB_MUL, "TLB_MUL ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1511772Sjl139090 	"TLB MultiHit",  FM_EREPORT_PAYLOAD_SYNC,
1521772Sjl139090 	FM_EREPORT_CPU_MTLB,
1531772Sjl139090 	SFSR_TLB_PRT, "TLB_PRT ", (OPL_ECC_SYNC_TRAP), OPL_CPU_SYNC_OTHERS,
1541772Sjl139090 	"TLB Parity",  FM_EREPORT_PAYLOAD_SYNC,
1551772Sjl139090 	FM_EREPORT_CPU_TLBP,
1561772Sjl139090 
1571772Sjl139090 	UGESR_IAUG_CRE, "IAUG_CRE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1581772Sjl139090 	"IAUG CRE",  FM_EREPORT_PAYLOAD_URGENT,
1591772Sjl139090 	FM_EREPORT_CPU_CRE,
1601772Sjl139090 	UGESR_IAUG_TSBCTXT, "IAUG_TSBCTXT",
1611772Sjl139090 	OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1621772Sjl139090 	"IAUG TSBCTXT",  FM_EREPORT_PAYLOAD_URGENT,
1631772Sjl139090 	FM_EREPORT_CPU_TSBCTX,
1641772Sjl139090 	UGESR_IUG_TSBP, "IUG_TSBP", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1651772Sjl139090 	"IUG TSBP",  FM_EREPORT_PAYLOAD_URGENT,
1661772Sjl139090 	FM_EREPORT_CPU_TSBP,
1671772Sjl139090 	UGESR_IUG_PSTATE, "IUG_PSTATE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1681772Sjl139090 	"IUG PSTATE",  FM_EREPORT_PAYLOAD_URGENT,
1691772Sjl139090 	FM_EREPORT_CPU_PSTATE,
1701772Sjl139090 	UGESR_IUG_TSTATE, "IUG_TSTATE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1711772Sjl139090 	"IUG TSTATE",  FM_EREPORT_PAYLOAD_URGENT,
1721772Sjl139090 	FM_EREPORT_CPU_TSTATE,
1731772Sjl139090 	UGESR_IUG_F, "IUG_F", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1741772Sjl139090 	"IUG FREG",  FM_EREPORT_PAYLOAD_URGENT,
1751772Sjl139090 	FM_EREPORT_CPU_IUG_F,
1761772Sjl139090 	UGESR_IUG_R, "IUG_R", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1771772Sjl139090 	"IUG RREG",  FM_EREPORT_PAYLOAD_URGENT,
1781772Sjl139090 	FM_EREPORT_CPU_IUG_R,
1791772Sjl139090 	UGESR_AUG_SDC, "AUG_SDC", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1801772Sjl139090 	"AUG SDC",  FM_EREPORT_PAYLOAD_URGENT,
1811772Sjl139090 	FM_EREPORT_CPU_SDC,
1821772Sjl139090 	UGESR_IUG_WDT, "IUG_WDT", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1831772Sjl139090 	"IUG WDT",  FM_EREPORT_PAYLOAD_URGENT,
1841772Sjl139090 	FM_EREPORT_CPU_WDT,
1851772Sjl139090 	UGESR_IUG_DTLB, "IUG_DTLB", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1861772Sjl139090 	"IUG DTLB",  FM_EREPORT_PAYLOAD_URGENT,
1871772Sjl139090 	FM_EREPORT_CPU_DTLB,
1881772Sjl139090 	UGESR_IUG_ITLB, "IUG_ITLB", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1891772Sjl139090 	"IUG ITLB",  FM_EREPORT_PAYLOAD_URGENT,
1901772Sjl139090 	FM_EREPORT_CPU_ITLB,
1911772Sjl139090 	UGESR_IUG_COREERR, "IUG_COREERR",
1921772Sjl139090 	OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1931772Sjl139090 	"IUG COREERR",  FM_EREPORT_PAYLOAD_URGENT,
1941772Sjl139090 	FM_EREPORT_CPU_CORE,
1951772Sjl139090 	UGESR_MULTI_DAE, "MULTI_DAE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1961772Sjl139090 	"MULTI DAE",  FM_EREPORT_PAYLOAD_URGENT,
1971772Sjl139090 	FM_EREPORT_CPU_DAE,
1981772Sjl139090 	UGESR_MULTI_IAE, "MULTI_IAE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
1991772Sjl139090 	"MULTI IAE",  FM_EREPORT_PAYLOAD_URGENT,
2001772Sjl139090 	FM_EREPORT_CPU_IAE,
2011772Sjl139090 	UGESR_MULTI_UGE, "MULTI_UGE", OPL_ECC_URGENT_TRAP, OPL_CPU_URGENT,
2021772Sjl139090 	"MULTI UGE",  FM_EREPORT_PAYLOAD_URGENT,
2031772Sjl139090 	FM_EREPORT_CPU_UGE,
2041772Sjl139090 	0,		NULL,		0,		0,
2051772Sjl139090 	NULL,  0,	   0,
2061772Sjl139090 };
2071772Sjl139090 
2081772Sjl139090 int (*p2get_mem_info)(int synd_code, uint64_t paddr,
2091772Sjl139090 		uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
2101772Sjl139090 		int *segsp, int *banksp, int *mcidp);
2111772Sjl139090 
2121772Sjl139090 
2131772Sjl139090 /*
2145414Smb158278  * Setup trap handlers for 0xA, 0x32, 0x40 trap types
2155414Smb158278  * and "ta 3" and "ta 4".
2161772Sjl139090  */
2171772Sjl139090 void
cpu_init_trap(void)2181772Sjl139090 cpu_init_trap(void)
2191772Sjl139090 {
2201772Sjl139090 	OPL_SET_TRAP(tt0_iae, opl_serr_instr);
2211772Sjl139090 	OPL_SET_TRAP(tt1_iae, opl_serr_instr);
2221772Sjl139090 	OPL_SET_TRAP(tt0_dae, opl_serr_instr);
2231772Sjl139090 	OPL_SET_TRAP(tt1_dae, opl_serr_instr);
2241772Sjl139090 	OPL_SET_TRAP(tt0_asdat, opl_ugerr_instr);
2251772Sjl139090 	OPL_SET_TRAP(tt1_asdat, opl_ugerr_instr);
2265414Smb158278 	OPL_SET_TRAP(tt0_flushw, opl_ta3_instr);
2275414Smb158278 	OPL_PATCH_28(opl_cleanw_patch, opl_ta4_instr);
2281772Sjl139090 }
2291772Sjl139090 
2301772Sjl139090 static int
getintprop(pnode_t node,char * name,int deflt)2311772Sjl139090 getintprop(pnode_t node, char *name, int deflt)
2321772Sjl139090 {
2331772Sjl139090 	int	value;
2341772Sjl139090 
2351772Sjl139090 	switch (prom_getproplen(node, name)) {
2361772Sjl139090 	case sizeof (int):
2371772Sjl139090 		(void) prom_getprop(node, name, (caddr_t)&value);
2381772Sjl139090 		break;
2391772Sjl139090 
2401772Sjl139090 	default:
2411772Sjl139090 		value = deflt;
2421772Sjl139090 		break;
2431772Sjl139090 	}
2441772Sjl139090 
2451772Sjl139090 	return (value);
2461772Sjl139090 }
2471772Sjl139090 
2481772Sjl139090 /*
2491772Sjl139090  * Set the magic constants of the implementation.
2501772Sjl139090  */
2511772Sjl139090 /*ARGSUSED*/
2521772Sjl139090 void
cpu_fiximp(pnode_t dnode)2531772Sjl139090 cpu_fiximp(pnode_t dnode)
2541772Sjl139090 {
2551772Sjl139090 	int i, a;
2561772Sjl139090 	extern int vac_size, vac_shift;
2571772Sjl139090 	extern uint_t vac_mask;
2581772Sjl139090 
2591772Sjl139090 	static struct {
2601772Sjl139090 		char	*name;
2611772Sjl139090 		int	*var;
2621772Sjl139090 		int	defval;
2631772Sjl139090 	} prop[] = {
2641772Sjl139090 		"l1-dcache-size", &dcache_size, OPL_DCACHE_SIZE,
2651772Sjl139090 		"l1-dcache-line-size", &dcache_linesize, OPL_DCACHE_LSIZE,
2661772Sjl139090 		"l1-icache-size", &icache_size, OPL_ICACHE_SIZE,
2671772Sjl139090 		"l1-icache-line-size", &icache_linesize, OPL_ICACHE_LSIZE,
2681772Sjl139090 		"l2-cache-size", &ecache_size, OPL_ECACHE_SIZE,
2691772Sjl139090 		"l2-cache-line-size", &ecache_alignsize, OPL_ECACHE_LSIZE,
2701772Sjl139090 		"l2-cache-associativity", &ecache_associativity, OPL_ECACHE_NWAY
2711772Sjl139090 	};
2721772Sjl139090 
2731772Sjl139090 	for (i = 0; i < sizeof (prop) / sizeof (prop[0]); i++)
2741772Sjl139090 		*prop[i].var = getintprop(dnode, prop[i].name, prop[i].defval);
2751772Sjl139090 
2761772Sjl139090 	ecache_setsize = ecache_size / ecache_associativity;
2771772Sjl139090 
2781772Sjl139090 	vac_size = OPL_VAC_SIZE;
2791772Sjl139090 	vac_mask = MMU_PAGEMASK & (vac_size - 1);
2801772Sjl139090 	i = 0; a = vac_size;
2811772Sjl139090 	while (a >>= 1)
2821772Sjl139090 		++i;
2831772Sjl139090 	vac_shift = i;
2841772Sjl139090 	shm_alignment = vac_size;
2851772Sjl139090 	vac = 1;
2861772Sjl139090 }
2871772Sjl139090 
2885037Sjl139090 /*
2895037Sjl139090  * Enable features for Jupiter-only domains.
2905037Sjl139090  */
2915037Sjl139090 void
cpu_fix_alljupiter(void)2925037Sjl139090 cpu_fix_alljupiter(void)
2935037Sjl139090 {
2945394Sjimand 	if (!prom_SPARC64VII_support_enabled()) {
2955394Sjimand 		/*
2965394Sjimand 		 * Do not enable all-Jupiter features and do not turn on
2975394Sjimand 		 * the cpu_alljupiter flag.
2985394Sjimand 		 */
2995394Sjimand 		return;
3005394Sjimand 	}
3015394Sjimand 
3025037Sjl139090 	cpu_alljupiter = 1;
3035037Sjl139090 
3045037Sjl139090 	/*
3055037Sjl139090 	 * Enable ima hwcap for Jupiter-only domains.  DR will prevent
3065037Sjl139090 	 * addition of Olympus-C to all-Jupiter domains to preserve ima
3075037Sjl139090 	 * hwcap semantics.
3085037Sjl139090 	 */
3095037Sjl139090 	cpu_hwcap_flags |= AV_SPARC_IMA;
3106127Ssm142603 
3116127Ssm142603 	/*
3127869SSean.McEnroe@Sun.COM 	 * Enable shared context support.
3136127Ssm142603 	 */
3147869SSean.McEnroe@Sun.COM 	shctx_on = 1;
3155037Sjl139090 }
3165037Sjl139090 
3172149Shyw #ifdef	OLYMPUS_C_REV_B_ERRATA_XCALL
3182149Shyw /*
3192149Shyw  * Quick and dirty way to redefine locally in
3202149Shyw  * OPL the value of IDSR_BN_SETS to 31 instead
3212149Shyw  * of the standard 32 value. This is to workaround
3222149Shyw  * REV_B of Olympus_c processor's problem in handling
3232149Shyw  * more than 31 xcall broadcast.
3242149Shyw  */
3252149Shyw #undef	IDSR_BN_SETS
3262149Shyw #define	IDSR_BN_SETS    31
3272149Shyw #endif	/* OLYMPUS_C_REV_B_ERRATA_XCALL */
3282149Shyw 
3291772Sjl139090 void
send_mondo_set(cpuset_t set)3301772Sjl139090 send_mondo_set(cpuset_t set)
3311772Sjl139090 {
3321772Sjl139090 	int lo, busy, nack, shipped = 0;
3331772Sjl139090 	uint16_t i, cpuids[IDSR_BN_SETS];
3341772Sjl139090 	uint64_t idsr, nackmask = 0, busymask, curnack, curbusy;
3351772Sjl139090 	uint64_t starttick, endtick, tick, lasttick;
3361772Sjl139090 #if (NCPU > IDSR_BN_SETS)
3371772Sjl139090 	int index = 0;
3381772Sjl139090 	int ncpuids = 0;
3391772Sjl139090 #endif
3401888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
3411772Sjl139090 	int bn_sets = IDSR_BN_SETS;
3421772Sjl139090 	uint64_t ver;
3431772Sjl139090 
3441772Sjl139090 	ASSERT(NCPU > bn_sets);
3451772Sjl139090 #endif
3461772Sjl139090 
3471772Sjl139090 	ASSERT(!CPUSET_ISNULL(set));
3481772Sjl139090 	starttick = lasttick = gettick();
3491772Sjl139090 
3501888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
3511772Sjl139090 	ver = ultra_getver();
3521772Sjl139090 	if (((ULTRA_VER_IMPL(ver)) == OLYMPUS_C_IMPL) &&
3535037Sjl139090 	    ((OLYMPUS_REV_MASK(ver)) == OLYMPUS_C_A))
3541772Sjl139090 		bn_sets = 1;
3551772Sjl139090 #endif
3561772Sjl139090 
3571772Sjl139090 #if (NCPU <= IDSR_BN_SETS)
3581772Sjl139090 	for (i = 0; i < NCPU; i++)
3591772Sjl139090 		if (CPU_IN_SET(set, i)) {
3601772Sjl139090 			shipit(i, shipped);
3611772Sjl139090 			nackmask |= IDSR_NACK_BIT(shipped);
3621772Sjl139090 			cpuids[shipped++] = i;
3631772Sjl139090 			CPUSET_DEL(set, i);
3641772Sjl139090 			if (CPUSET_ISNULL(set))
3651772Sjl139090 				break;
3661772Sjl139090 		}
3671772Sjl139090 	CPU_STATS_ADDQ(CPU, sys, xcalls, shipped);
3681772Sjl139090 #else
3691772Sjl139090 	for (i = 0; i < NCPU; i++)
3701772Sjl139090 		if (CPU_IN_SET(set, i)) {
3711772Sjl139090 			ncpuids++;
3721772Sjl139090 
3731772Sjl139090 			/*
3741772Sjl139090 			 * Ship only to the first (IDSR_BN_SETS) CPUs.  If we
3751772Sjl139090 			 * find we have shipped to more than (IDSR_BN_SETS)
3761772Sjl139090 			 * CPUs, set "index" to the highest numbered CPU in
3771772Sjl139090 			 * the set so we can ship to other CPUs a bit later on.
3781772Sjl139090 			 */
3791888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
3801772Sjl139090 			if (shipped < bn_sets) {
3811772Sjl139090 #else
3821772Sjl139090 			if (shipped < IDSR_BN_SETS) {
3831772Sjl139090 #endif
3841772Sjl139090 				shipit(i, shipped);
3851772Sjl139090 				nackmask |= IDSR_NACK_BIT(shipped);
3861772Sjl139090 				cpuids[shipped++] = i;
3871772Sjl139090 				CPUSET_DEL(set, i);
3881772Sjl139090 				if (CPUSET_ISNULL(set))
3891772Sjl139090 					break;
3901772Sjl139090 			} else
3911772Sjl139090 				index = (int)i;
3921772Sjl139090 		}
3931772Sjl139090 
3941772Sjl139090 	CPU_STATS_ADDQ(CPU, sys, xcalls, ncpuids);
3951772Sjl139090 #endif
3961772Sjl139090 
3971772Sjl139090 	busymask = IDSR_NACK_TO_BUSY(nackmask);
3981772Sjl139090 	busy = nack = 0;
3991772Sjl139090 	endtick = starttick + xc_tick_limit;
4001772Sjl139090 	for (;;) {
4011772Sjl139090 		idsr = getidsr();
4021772Sjl139090 #if (NCPU <= IDSR_BN_SETS)
4031772Sjl139090 		if (idsr == 0)
4041772Sjl139090 			break;
4051772Sjl139090 #else
4061772Sjl139090 		if (idsr == 0 && shipped == ncpuids)
4071772Sjl139090 			break;
4081772Sjl139090 #endif
4091772Sjl139090 		tick = gettick();
4101772Sjl139090 		/*
4111772Sjl139090 		 * If there is a big jump between the current tick
4121772Sjl139090 		 * count and lasttick, we have probably hit a break
4131772Sjl139090 		 * point.  Adjust endtick accordingly to avoid panic.
4141772Sjl139090 		 */
4151772Sjl139090 		if (tick > (lasttick + xc_tick_jump_limit))
4161772Sjl139090 			endtick += (tick - lasttick);
4171772Sjl139090 		lasttick = tick;
4181772Sjl139090 		if (tick > endtick) {
4191772Sjl139090 			if (panic_quiesce)
4201772Sjl139090 				return;
4215037Sjl139090 			cmn_err(CE_CONT, "send mondo timeout [%d NACK %d "
4225037Sjl139090 			    "BUSY]\nIDSR 0x%" PRIx64 "  cpuids:",
4235037Sjl139090 			    nack, busy, idsr);
4241888Shyw #ifdef	OLYMPUS_C_REV_A_ERRATA_XCALL
4251772Sjl139090 			for (i = 0; i < bn_sets; i++) {
4261772Sjl139090 #else
4271772Sjl139090 			for (i = 0; i < IDSR_BN_SETS; i++) {
4281772Sjl139090 #endif
4291772Sjl139090 				if (idsr & (IDSR_NACK_BIT(i) |
4301772Sjl139090 				    IDSR_BUSY_BIT(i))) {
4315037Sjl139090 					cmn_err(CE_CONT, " 0x%x", cpuids[i]);
4321772Sjl139090 				}
4331772Sjl139090 			}
4341772Sjl139090 			cmn_err(CE_CONT, "\n");
4351772Sjl139090 			cmn_err(CE_PANIC, "send_mondo_set: timeout");
4361772Sjl139090 		}
4371772Sjl139090 		curnack = idsr & nackmask;
4381772Sjl139090 		curbusy = idsr & busymask;
4391888Shyw 
4401888Shyw #ifdef OLYMPUS_C_REV_B_ERRATA_XCALL
4411888Shyw 		/*
4421888Shyw 		 * Only proceed to send more xcalls if all the
4431888Shyw 		 * cpus in the previous IDSR_BN_SETS were completed.
4441888Shyw 		 */
4451888Shyw 		if (curbusy) {
4461888Shyw 			busy++;
4471888Shyw 			continue;
4481888Shyw 		}
4491888Shyw #endif /* OLYMPUS_C_REV_B_ERRATA_XCALL */
4501888Shyw 
4511772Sjl139090 #if (NCPU > IDSR_BN_SETS)
4521772Sjl139090 		if (shipped < ncpuids) {
4531772Sjl139090 			uint64_t cpus_left;
4541772Sjl139090 			uint16_t next = (uint16_t)index;
4551772Sjl139090 
4561772Sjl139090 			cpus_left = ~(IDSR_NACK_TO_BUSY(curnack) | curbusy) &
4571772Sjl139090 			    busymask;
4581772Sjl139090 
4591772Sjl139090 			if (cpus_left) {
4601772Sjl139090 				do {
4611772Sjl139090 					/*
4621772Sjl139090 					 * Sequence through and ship to the
4631772Sjl139090 					 * remainder of the CPUs in the system
4641772Sjl139090 					 * (e.g. other than the first
4651772Sjl139090 					 * (IDSR_BN_SETS)) in reverse order.
4661772Sjl139090 					 */
4671772Sjl139090 					lo = lowbit(cpus_left) - 1;
4681772Sjl139090 					i = IDSR_BUSY_IDX(lo);
4691772Sjl139090 					shipit(next, i);
4701772Sjl139090 					shipped++;
4711772Sjl139090 					cpuids[i] = next;
4721772Sjl139090 
4731772Sjl139090 					/*
4741772Sjl139090 					 * If we've processed all the CPUs,
4751772Sjl139090 					 * exit the loop now and save
4761772Sjl139090 					 * instructions.
4771772Sjl139090 					 */
4781772Sjl139090 					if (shipped == ncpuids)
4791772Sjl139090 						break;
4801772Sjl139090 
4811772Sjl139090 					for ((index = ((int)next - 1));
4825037Sjl139090 					    index >= 0; index--)
4831772Sjl139090 						if (CPU_IN_SET(set, index)) {
4841772Sjl139090 							next = (uint16_t)index;
4851772Sjl139090 							break;
4861772Sjl139090 						}
4871772Sjl139090 
4881772Sjl139090 					cpus_left &= ~(1ull << lo);
4891772Sjl139090 				} while (cpus_left);
4901772Sjl139090 				continue;
4911772Sjl139090 			}
4921772Sjl139090 		}
4931772Sjl139090 #endif
4941888Shyw #ifndef	OLYMPUS_C_REV_B_ERRATA_XCALL
4951772Sjl139090 		if (curbusy) {
4961772Sjl139090 			busy++;
4971772Sjl139090 			continue;
4981772Sjl139090 		}
4991888Shyw #endif	/* OLYMPUS_C_REV_B_ERRATA_XCALL */
5001772Sjl139090 #ifdef SEND_MONDO_STATS
5011772Sjl139090 		{
5021772Sjl139090 			int n = gettick() - starttick;
5031772Sjl139090 			if (n < 8192)
5041772Sjl139090 				x_nack_stimes[n >> 7]++;
5051772Sjl139090 		}
5061772Sjl139090 #endif
5071772Sjl139090 		while (gettick() < (tick + sys_clock_mhz))
5081772Sjl139090 			;
5091772Sjl139090 		do {
5101772Sjl139090 			lo = lowbit(curnack) - 1;
5111772Sjl139090 			i = IDSR_NACK_IDX(lo);
5121772Sjl139090 			shipit(cpuids[i], i);
5131772Sjl139090 			curnack &= ~(1ull << lo);
5141772Sjl139090 		} while (curnack);
5151772Sjl139090 		nack++;
5161772Sjl139090 		busy = 0;
5171772Sjl139090 	}
5181772Sjl139090 #ifdef SEND_MONDO_STATS
5191772Sjl139090 	{
5201772Sjl139090 		int n = gettick() - starttick;
5211772Sjl139090 		if (n < 8192)
5221772Sjl139090 			x_set_stimes[n >> 7]++;
5231772Sjl139090 		else
5241772Sjl139090 			x_set_ltimes[(n >> 13) & 0xf]++;
5251772Sjl139090 	}
5261772Sjl139090 	x_set_cpus[shipped]++;
5271772Sjl139090 #endif
5281772Sjl139090 }
5291772Sjl139090 
5301772Sjl139090 /*
5311772Sjl139090  * Cpu private initialization.
5321772Sjl139090  */
5331772Sjl139090 void
5341772Sjl139090 cpu_init_private(struct cpu *cp)
5351772Sjl139090 {
5365037Sjl139090 	if (!((IS_OLYMPUS_C(cpunodes[cp->cpu_id].implementation)) ||
5375037Sjl139090 	    (IS_JUPITER(cpunodes[cp->cpu_id].implementation)))) {
5385037Sjl139090 		cmn_err(CE_PANIC, "CPU%d Impl %d: Only SPARC64-VI(I) is "
5395037Sjl139090 		    "supported", cp->cpu_id,
5405037Sjl139090 		    cpunodes[cp->cpu_id].implementation);
5411772Sjl139090 	}
5421772Sjl139090 
5431772Sjl139090 	adjust_hw_copy_limits(cpunodes[cp->cpu_id].ecache_size);
5441772Sjl139090 }
5451772Sjl139090 
5461772Sjl139090 void
5471772Sjl139090 cpu_setup(void)
5481772Sjl139090 {
5491772Sjl139090 	extern int at_flags;
5501772Sjl139090 	extern int cpc_has_overflow_intr;
5511772Sjl139090 	uint64_t cpu0_log;
5521772Sjl139090 	extern	 uint64_t opl_cpu0_err_log;
5531772Sjl139090 
5541772Sjl139090 	/*
5551772Sjl139090 	 * Initialize Error log Scratch register for error handling.
5561772Sjl139090 	 */
5571772Sjl139090 
5581772Sjl139090 	cpu0_log = va_to_pa(&opl_cpu0_err_log);
5591772Sjl139090 	opl_error_setup(cpu0_log);
5606259Smb158278 	opl_cpu0_log_setup = 1;
5611772Sjl139090 
5621772Sjl139090 	/*
5631772Sjl139090 	 * Enable MMU translating multiple page sizes for
5641772Sjl139090 	 * sITLB and sDTLB.
5651772Sjl139090 	 */
566*8664SJames.Anderson@Sun.COM 	cpu_early_feature_init();
5671772Sjl139090 
5681772Sjl139090 	/*
5691772Sjl139090 	 * Setup chip-specific trap handlers.
5701772Sjl139090 	 */
5711772Sjl139090 	cpu_init_trap();
5721772Sjl139090 
5731772Sjl139090 	cache |= (CACHE_VAC | CACHE_PTAG | CACHE_IOCOHERENT);
5741772Sjl139090 
5751772Sjl139090 	at_flags = EF_SPARC_32PLUS | EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3;
5761772Sjl139090 
5771772Sjl139090 	/*
5781772Sjl139090 	 * Due to the number of entries in the fully-associative tlb
5791772Sjl139090 	 * this may have to be tuned lower than in spitfire.
5801772Sjl139090 	 */
5811772Sjl139090 	pp_slots = MIN(8, MAXPP_SLOTS);
5821772Sjl139090 
5831772Sjl139090 	/*
5841772Sjl139090 	 * Block stores do not invalidate all pages of the d$, pagecopy
5851772Sjl139090 	 * et. al. need virtual translations with virtual coloring taken
5861772Sjl139090 	 * into consideration.  prefetch/ldd will pollute the d$ on the
5871772Sjl139090 	 * load side.
5881772Sjl139090 	 */
5891772Sjl139090 	pp_consistent_coloring = PPAGE_STORE_VCOLORING | PPAGE_LOADS_POLLUTE;
5901772Sjl139090 
5911772Sjl139090 	if (use_page_coloring) {
5921772Sjl139090 		do_pg_coloring = 1;
5931772Sjl139090 	}
5941772Sjl139090 
5951772Sjl139090 	isa_list =
5961772Sjl139090 	    "sparcv9+vis2 sparcv9+vis sparcv9 "
5971772Sjl139090 	    "sparcv8plus+vis2 sparcv8plus+vis sparcv8plus "
5981772Sjl139090 	    "sparcv8 sparcv8-fsmuld sparcv7 sparc";
5991772Sjl139090 
6002801Shyw 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 |
6012801Shyw 	    AV_SPARC_POPC | AV_SPARC_FMAF;
6021772Sjl139090 
6031772Sjl139090 	/*
6041772Sjl139090 	 * On SPARC64-VI, there's no hole in the virtual address space
6051772Sjl139090 	 */
6061772Sjl139090 	hole_start = hole_end = 0;
6071772Sjl139090 
6081772Sjl139090 	/*
6091772Sjl139090 	 * The kpm mapping window.
6101772Sjl139090 	 * kpm_size:
6111772Sjl139090 	 *	The size of a single kpm range.
6121772Sjl139090 	 *	The overall size will be: kpm_size * vac_colors.
6131772Sjl139090 	 * kpm_vbase:
6141772Sjl139090 	 *	The virtual start address of the kpm range within the kernel
6151772Sjl139090 	 *	virtual address space. kpm_vbase has to be kpm_size aligned.
6161772Sjl139090 	 */
6171772Sjl139090 	kpm_size = (size_t)(128ull * 1024 * 1024 * 1024 * 1024); /* 128TB */
6181772Sjl139090 	kpm_size_shift = 47;
6191772Sjl139090 	kpm_vbase = (caddr_t)0x8000000000000000ull; /* 8EB */
6201772Sjl139090 	kpm_smallpages = 1;
6211772Sjl139090 
6221772Sjl139090 	/*
6231772Sjl139090 	 * The traptrace code uses either %tick or %stick for
6241772Sjl139090 	 * timestamping.  We have %stick so we can use it.
6251772Sjl139090 	 */
6261772Sjl139090 	traptrace_use_stick = 1;
6271772Sjl139090 
6281772Sjl139090 	/*
6291772Sjl139090 	 * SPARC64-VI has a performance counter overflow interrupt
6301772Sjl139090 	 */
6311772Sjl139090 	cpc_has_overflow_intr = 1;
6321772Sjl139090 
6331772Sjl139090 	/*
6341772Sjl139090 	 * Declare that this architecture/cpu combination does not support
6351772Sjl139090 	 * fpRAS.
6361772Sjl139090 	 */
6371772Sjl139090 	fpras_implemented = 0;
6381772Sjl139090 }
6391772Sjl139090 
6401772Sjl139090 /*
6411772Sjl139090  * Called by setcpudelay
6421772Sjl139090  */
6431772Sjl139090 void
6441772Sjl139090 cpu_init_tick_freq(void)
6451772Sjl139090 {
6461772Sjl139090 	/*
6471772Sjl139090 	 * For SPARC64-VI we want to use the system clock rate as
6481772Sjl139090 	 * the basis for low level timing, due to support of mixed
6491772Sjl139090 	 * speed CPUs and power managment.
6501772Sjl139090 	 */
6511772Sjl139090 	if (system_clock_freq == 0)
6521772Sjl139090 		cmn_err(CE_PANIC, "setcpudelay: invalid system_clock_freq");
6531772Sjl139090 
6541772Sjl139090 	sys_tick_freq = system_clock_freq;
6551772Sjl139090 }
6561772Sjl139090 
6571772Sjl139090 #ifdef SEND_MONDO_STATS
6581772Sjl139090 uint32_t x_one_stimes[64];
6591772Sjl139090 uint32_t x_one_ltimes[16];
6601772Sjl139090 uint32_t x_set_stimes[64];
6611772Sjl139090 uint32_t x_set_ltimes[16];
6621772Sjl139090 uint32_t x_set_cpus[NCPU];
6631772Sjl139090 uint32_t x_nack_stimes[64];
6641772Sjl139090 #endif
6651772Sjl139090 
6661772Sjl139090 /*
6671772Sjl139090  * Note: A version of this function is used by the debugger via the KDI,
6681772Sjl139090  * and must be kept in sync with this version.  Any changes made to this
6691772Sjl139090  * function to support new chips or to accomodate errata must also be included
6701772Sjl139090  * in the KDI-specific version.  See us3_kdi.c.
6711772Sjl139090  */
6721772Sjl139090 void
6731772Sjl139090 send_one_mondo(int cpuid)
6741772Sjl139090 {
6751772Sjl139090 	int busy, nack;
6761772Sjl139090 	uint64_t idsr, starttick, endtick, tick, lasttick;
6771772Sjl139090 	uint64_t busymask;
6781772Sjl139090 
6791772Sjl139090 	CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
6801772Sjl139090 	starttick = lasttick = gettick();
6811772Sjl139090 	shipit(cpuid, 0);
6821772Sjl139090 	endtick = starttick + xc_tick_limit;
6831772Sjl139090 	busy = nack = 0;
6841772Sjl139090 	busymask = IDSR_BUSY;
6851772Sjl139090 	for (;;) {
6861772Sjl139090 		idsr = getidsr();
6871772Sjl139090 		if (idsr == 0)
6881772Sjl139090 			break;
6891772Sjl139090 
6901772Sjl139090 		tick = gettick();
6911772Sjl139090 		/*
6921772Sjl139090 		 * If there is a big jump between the current tick
6931772Sjl139090 		 * count and lasttick, we have probably hit a break
6941772Sjl139090 		 * point.  Adjust endtick accordingly to avoid panic.
6951772Sjl139090 		 */
6961772Sjl139090 		if (tick > (lasttick + xc_tick_jump_limit))
6971772Sjl139090 			endtick += (tick - lasttick);
6981772Sjl139090 		lasttick = tick;
6991772Sjl139090 		if (tick > endtick) {
7001772Sjl139090 			if (panic_quiesce)
7011772Sjl139090 				return;
7025037Sjl139090 			cmn_err(CE_PANIC, "send mondo timeout (target 0x%x) "
7035037Sjl139090 			    "[%d NACK %d BUSY]", cpuid, nack, busy);
7041772Sjl139090 		}
7051772Sjl139090 
7061772Sjl139090 		if (idsr & busymask) {
7071772Sjl139090 			busy++;
7081772Sjl139090 			continue;
7091772Sjl139090 		}
7101772Sjl139090 		drv_usecwait(1);
7111772Sjl139090 		shipit(cpuid, 0);
7121772Sjl139090 		nack++;
7131772Sjl139090 		busy = 0;
7141772Sjl139090 	}
7151772Sjl139090 #ifdef SEND_MONDO_STATS
7161772Sjl139090 	{
7171772Sjl139090 		int n = gettick() - starttick;
7181772Sjl139090 		if (n < 8192)
7191772Sjl139090 			x_one_stimes[n >> 7]++;
7201772Sjl139090 		else
7211772Sjl139090 			x_one_ltimes[(n >> 13) & 0xf]++;
7221772Sjl139090 	}
7231772Sjl139090 #endif
7241772Sjl139090 }
7251772Sjl139090 
7261772Sjl139090 /*
7271772Sjl139090  * init_mmu_page_sizes is set to one after the bootup time initialization
7281772Sjl139090  * via mmu_init_mmu_page_sizes, to indicate that mmu_page_sizes has a
7291772Sjl139090  * valid value.
7301772Sjl139090  *
7311772Sjl139090  * mmu_disable_ism_large_pages and mmu_disable_large_pages are the mmu-specific
7321772Sjl139090  * versions of disable_ism_large_pages and disable_large_pages, and feed back
7331772Sjl139090  * into those two hat variables at hat initialization time.
7341772Sjl139090  *
7351772Sjl139090  */
7361772Sjl139090 int init_mmu_page_sizes = 0;
7372991Ssusans 
7382991Ssusans static uint_t mmu_disable_large_pages = 0;
7392991Ssusans static uint_t mmu_disable_ism_large_pages = ((1 << TTE64K) |
7402659Ssusans 	(1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
7412991Ssusans static uint_t mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
7422659Ssusans 	(1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
7432991Ssusans static uint_t mmu_disable_auto_text_large_pages = ((1 << TTE64K) |
7442991Ssusans 	(1 << TTE512K));
7451772Sjl139090 
7461772Sjl139090 /*
7471772Sjl139090  * Re-initialize mmu_page_sizes and friends, for SPARC64-VI mmu support.
7481772Sjl139090  * Called during very early bootup from check_cpus_set().
7491772Sjl139090  * Can be called to verify that mmu_page_sizes are set up correctly.
7501772Sjl139090  *
7511772Sjl139090  * Set Olympus defaults. We do not use the function parameter.
7521772Sjl139090  */
7531772Sjl139090 /*ARGSUSED*/
7547869SSean.McEnroe@Sun.COM void
7557869SSean.McEnroe@Sun.COM mmu_init_scd(sf_scd_t *scdp)
7567869SSean.McEnroe@Sun.COM {
7577869SSean.McEnroe@Sun.COM 	scdp->scd_sfmmup->sfmmu_cext = shctx_cext;
7587869SSean.McEnroe@Sun.COM }
7597869SSean.McEnroe@Sun.COM 
7607869SSean.McEnroe@Sun.COM /*ARGSUSED*/
7611772Sjl139090 int
7621772Sjl139090 mmu_init_mmu_page_sizes(int32_t not_used)
7631772Sjl139090 {
7641772Sjl139090 	if (!init_mmu_page_sizes) {
7651772Sjl139090 		mmu_page_sizes = MMU_PAGE_SIZES;
7661772Sjl139090 		mmu_hashcnt = MAX_HASHCNT;
7672659Ssusans 		mmu_ism_pagesize = DEFAULT_ISM_PAGESIZE;
7681772Sjl139090 		mmu_exported_pagesize_mask = (1 << TTE8K) |
7691772Sjl139090 		    (1 << TTE64K) | (1 << TTE512K) | (1 << TTE4M) |
7701772Sjl139090 		    (1 << TTE32M) | (1 << TTE256M);
7711772Sjl139090 		init_mmu_page_sizes = 1;
7721772Sjl139090 		return (0);
7731772Sjl139090 	}
7741772Sjl139090 	return (1);
7751772Sjl139090 }
7761772Sjl139090 
7771772Sjl139090 /* SPARC64-VI worst case DTLB parameters */
7781772Sjl139090 #ifndef	LOCKED_DTLB_ENTRIES
7791772Sjl139090 #define	LOCKED_DTLB_ENTRIES	5	/* 2 user TSBs, 2 nucleus, + OBP */
7801772Sjl139090 #endif
7811772Sjl139090 #define	TOTAL_DTLB_ENTRIES	32
7821772Sjl139090 #define	AVAIL_32M_ENTRIES	0
7831772Sjl139090 #define	AVAIL_256M_ENTRIES	0
7841772Sjl139090 #define	AVAIL_DTLB_ENTRIES	(TOTAL_DTLB_ENTRIES - LOCKED_DTLB_ENTRIES)
7851772Sjl139090 static uint64_t ttecnt_threshold[MMU_PAGE_SIZES] = {
7861772Sjl139090 	AVAIL_DTLB_ENTRIES, AVAIL_DTLB_ENTRIES,
7871772Sjl139090 	AVAIL_DTLB_ENTRIES, AVAIL_DTLB_ENTRIES,
7881772Sjl139090 	AVAIL_DTLB_ENTRIES, AVAIL_DTLB_ENTRIES};
7891772Sjl139090 
7901772Sjl139090 /*
7911772Sjl139090  * The function returns the mmu-specific values for the
7922368Sjimand  * hat's disable_large_pages, disable_ism_large_pages, and
7932991Ssusans  * disable_auto_data_large_pages and
7942991Ssusans  * disable_text_data_large_pages variables.
7951772Sjl139090  */
7962991Ssusans uint_t
7971772Sjl139090 mmu_large_pages_disabled(uint_t flag)
7981772Sjl139090 {
7992991Ssusans 	uint_t pages_disable = 0;
8002991Ssusans 	extern int use_text_pgsz64K;
8012991Ssusans 	extern int use_text_pgsz512K;
8021772Sjl139090 
8031772Sjl139090 	if (flag == HAT_LOAD) {
8041772Sjl139090 		pages_disable =  mmu_disable_large_pages;
8051772Sjl139090 	} else if (flag == HAT_LOAD_SHARE) {
8061772Sjl139090 		pages_disable = mmu_disable_ism_large_pages;
8072991Ssusans 	} else if (flag == HAT_AUTO_DATA) {
8082991Ssusans 		pages_disable = mmu_disable_auto_data_large_pages;
8092991Ssusans 	} else if (flag == HAT_AUTO_TEXT) {
8102991Ssusans 		pages_disable = mmu_disable_auto_text_large_pages;
8112991Ssusans 		if (use_text_pgsz512K) {
8122991Ssusans 			pages_disable &= ~(1 << TTE512K);
8132991Ssusans 		}
8142991Ssusans 		if (use_text_pgsz64K) {
8152991Ssusans 			pages_disable &= ~(1 << TTE64K);
8162991Ssusans 		}
8171772Sjl139090 	}
8181772Sjl139090 	return (pages_disable);
8191772Sjl139090 }
8201772Sjl139090 
8211772Sjl139090 /*
8221772Sjl139090  * mmu_init_large_pages is called with the desired ism_pagesize parameter.
8236127Ssm142603  * It may be called from set_platform_defaults, if some value other than 4M
8241772Sjl139090  * is desired.  mmu_ism_pagesize is the tunable.  If it has a bad value,
8251772Sjl139090  * then only warn, since it would be bad form to panic due to a user typo.
8261772Sjl139090  *
8271772Sjl139090  * The function re-initializes the mmu_disable_ism_large_pages variable.
8281772Sjl139090  */
8291772Sjl139090 void
8301772Sjl139090 mmu_init_large_pages(size_t ism_pagesize)
8311772Sjl139090 {
8326127Ssm142603 
8331772Sjl139090 	switch (ism_pagesize) {
8341772Sjl139090 	case MMU_PAGESIZE4M:
8351772Sjl139090 		mmu_disable_ism_large_pages = ((1 << TTE64K) |
8361772Sjl139090 		    (1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
8372991Ssusans 		mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
8382368Sjimand 		    (1 << TTE512K) | (1 << TTE32M) | (1 << TTE256M));
8396127Ssm142603 		shctx_cext = TAGACCEXT_MKSZPAIR(TTE4M, TTE8K);
8401772Sjl139090 		break;
8411772Sjl139090 	case MMU_PAGESIZE32M:
8421772Sjl139090 		mmu_disable_ism_large_pages = ((1 << TTE64K) |
8431772Sjl139090 		    (1 << TTE512K) | (1 << TTE256M));
8442991Ssusans 		mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
8452368Sjimand 		    (1 << TTE512K) | (1 << TTE4M) | (1 << TTE256M));
8462991Ssusans 		adjust_data_maxlpsize(ism_pagesize);
8476127Ssm142603 		shctx_cext = TAGACCEXT_MKSZPAIR(TTE32M, TTE8K);
8481772Sjl139090 		break;
8491772Sjl139090 	case MMU_PAGESIZE256M:
8501772Sjl139090 		mmu_disable_ism_large_pages = ((1 << TTE64K) |
8511772Sjl139090 		    (1 << TTE512K) | (1 << TTE32M));
8522991Ssusans 		mmu_disable_auto_data_large_pages = ((1 << TTE64K) |
8532368Sjimand 		    (1 << TTE512K) | (1 << TTE4M) | (1 << TTE32M));
8542991Ssusans 		adjust_data_maxlpsize(ism_pagesize);
8556127Ssm142603 		shctx_cext = TAGACCEXT_MKSZPAIR(TTE256M, TTE8K);
8561772Sjl139090 		break;
8571772Sjl139090 	default:
8581772Sjl139090 		cmn_err(CE_WARN, "Unrecognized mmu_ism_pagesize value 0x%lx",
8591772Sjl139090 		    ism_pagesize);
8601772Sjl139090 		break;
8611772Sjl139090 	}
8621772Sjl139090 }
8631772Sjl139090 
8641772Sjl139090 /*
8651772Sjl139090  * Function to reprogram the TLBs when page sizes used
8661772Sjl139090  * by a process change significantly.
8671772Sjl139090  */
8686127Ssm142603 static void
8692272Sjimand mmu_setup_page_sizes(struct hat *hat, uint64_t *ttecnt, uint8_t *tmp_pgsz)
8701772Sjl139090 {
8711772Sjl139090 	uint8_t pgsz0, pgsz1;
8721772Sjl139090 
8731772Sjl139090 	/*
8741772Sjl139090 	 * Don't program 2nd dtlb for kernel and ism hat
8751772Sjl139090 	 */
8762272Sjimand 	ASSERT(hat->sfmmu_ismhat == NULL);
8772272Sjimand 	ASSERT(hat != ksfmmup);
8781772Sjl139090 
8791772Sjl139090 	/*
8801772Sjl139090 	 * hat->sfmmu_pgsz[] is an array whose elements
8811772Sjl139090 	 * contain a sorted order of page sizes.  Element
8821772Sjl139090 	 * 0 is the most commonly used page size, followed
8831772Sjl139090 	 * by element 1, and so on.
8841772Sjl139090 	 *
8851772Sjl139090 	 * ttecnt[] is an array of per-page-size page counts
8861772Sjl139090 	 * mapped into the process.
8871772Sjl139090 	 *
8881772Sjl139090 	 * If the HAT's choice for page sizes is unsuitable,
8891772Sjl139090 	 * we can override it here.  The new values written
8901772Sjl139090 	 * to the array will be handed back to us later to
8911772Sjl139090 	 * do the actual programming of the TLB hardware.
8921772Sjl139090 	 *
8931772Sjl139090 	 */
8942272Sjimand 	pgsz0 = (uint8_t)MIN(tmp_pgsz[0], tmp_pgsz[1]);
8952272Sjimand 	pgsz1 = (uint8_t)MAX(tmp_pgsz[0], tmp_pgsz[1]);
8961772Sjl139090 
8971772Sjl139090 	/*
8981772Sjl139090 	 * This implements PAGESIZE programming of the sTLB
8991772Sjl139090 	 * if large TTE counts don't exceed the thresholds.
9001772Sjl139090 	 */
9011772Sjl139090 	if (ttecnt[pgsz0] < ttecnt_threshold[pgsz0])
9021772Sjl139090 		pgsz0 = page_szc(MMU_PAGESIZE);
9031772Sjl139090 	if (ttecnt[pgsz1] < ttecnt_threshold[pgsz1])
9041772Sjl139090 		pgsz1 = page_szc(MMU_PAGESIZE);
9052272Sjimand 	tmp_pgsz[0] = pgsz0;
9062272Sjimand 	tmp_pgsz[1] = pgsz1;
9071772Sjl139090 	/* otherwise, accept what the HAT chose for us */
9081772Sjl139090 }
9091772Sjl139090 
9101772Sjl139090 /*
9111772Sjl139090  * The HAT calls this function when an MMU context is allocated so that we
9121772Sjl139090  * can reprogram the large TLBs appropriately for the new process using
9131772Sjl139090  * the context.
9141772Sjl139090  *
9151772Sjl139090  * The caller must hold the HAT lock.
9161772Sjl139090  */
9171772Sjl139090 void
9181772Sjl139090 mmu_set_ctx_page_sizes(struct hat *hat)
9191772Sjl139090 {
9201772Sjl139090 	uint8_t pgsz0, pgsz1;
9211772Sjl139090 	uint8_t new_cext;
9221772Sjl139090 
9231772Sjl139090 	ASSERT(sfmmu_hat_lock_held(hat));
9241772Sjl139090 	/*
9251772Sjl139090 	 * Don't program 2nd dtlb for kernel and ism hat
9261772Sjl139090 	 */
9271772Sjl139090 	if (hat->sfmmu_ismhat || hat == ksfmmup)
9281772Sjl139090 		return;
9291772Sjl139090 
9301772Sjl139090 	/*
9311772Sjl139090 	 * If supported, reprogram the TLBs to a larger pagesize.
9321772Sjl139090 	 */
9336127Ssm142603 	if (hat->sfmmu_scdp != NULL) {
9347869SSean.McEnroe@Sun.COM 		new_cext = hat->sfmmu_scdp->scd_sfmmup->sfmmu_cext;
9357869SSean.McEnroe@Sun.COM 		ASSERT(new_cext == shctx_cext);
9366127Ssm142603 	} else {
9376127Ssm142603 		pgsz0 = hat->sfmmu_pgsz[0];
9386127Ssm142603 		pgsz1 = hat->sfmmu_pgsz[1];
9396127Ssm142603 		ASSERT(pgsz0 < mmu_page_sizes);
9406127Ssm142603 		ASSERT(pgsz1 < mmu_page_sizes);
9416127Ssm142603 		new_cext = TAGACCEXT_MKSZPAIR(pgsz1, pgsz0);
9426127Ssm142603 	}
9431772Sjl139090 	if (hat->sfmmu_cext != new_cext) {
9442241Shuah #ifdef DEBUG
9452241Shuah 		int i;
9462241Shuah 		/*
9472241Shuah 		 * assert cnum should be invalid, this is because pagesize
9482241Shuah 		 * can only be changed after a proc's ctxs are invalidated.
9492241Shuah 		 */
9502241Shuah 		for (i = 0; i < max_mmu_ctxdoms; i++) {
9512241Shuah 			ASSERT(hat->sfmmu_ctxs[i].cnum == INVALID_CONTEXT);
9522241Shuah 		}
9532241Shuah #endif /* DEBUG */
9541772Sjl139090 		hat->sfmmu_cext = new_cext;
9551772Sjl139090 	}
9561772Sjl139090 	/*
9571772Sjl139090 	 * sfmmu_setctx_sec() will take care of the
9581772Sjl139090 	 * rest of the dirty work for us.
9591772Sjl139090 	 */
9601772Sjl139090 }
9611772Sjl139090 
9621772Sjl139090 /*
9632272Sjimand  * This function assumes that there are either four or six supported page
9642272Sjimand  * sizes and at most two programmable TLBs, so we need to decide which
9652272Sjimand  * page sizes are most important and then adjust the TLB page sizes
9662272Sjimand  * accordingly (if supported).
9672272Sjimand  *
9682272Sjimand  * If these assumptions change, this function will need to be
9692272Sjimand  * updated to support whatever the new limits are.
9702272Sjimand  */
9712272Sjimand void
9722272Sjimand mmu_check_page_sizes(sfmmu_t *sfmmup, uint64_t *ttecnt)
9732272Sjimand {
9742272Sjimand 	uint64_t sortcnt[MMU_PAGE_SIZES];
9752272Sjimand 	uint8_t tmp_pgsz[MMU_PAGE_SIZES];
9762272Sjimand 	uint8_t i, j, max;
9772272Sjimand 	uint16_t oldval, newval;
9782272Sjimand 
9792272Sjimand 	/*
9802272Sjimand 	 * We only consider reprogramming the TLBs if one or more of
9812272Sjimand 	 * the two most used page sizes changes and we're using
9822272Sjimand 	 * large pages in this process.
9832272Sjimand 	 */
9844528Spaulsan 	if (SFMMU_LGPGS_INUSE(sfmmup)) {
9852272Sjimand 		/* Sort page sizes. */
9862272Sjimand 		for (i = 0; i < mmu_page_sizes; i++) {
9872272Sjimand 			sortcnt[i] = ttecnt[i];
9882272Sjimand 		}
9892272Sjimand 		for (j = 0; j < mmu_page_sizes; j++) {
9902272Sjimand 			for (i = mmu_page_sizes - 1, max = 0; i > 0; i--) {
9912272Sjimand 				if (sortcnt[i] > sortcnt[max])
9922272Sjimand 					max = i;
9932272Sjimand 			}
9942272Sjimand 			tmp_pgsz[j] = max;
9952272Sjimand 			sortcnt[max] = 0;
9962272Sjimand 		}
9972272Sjimand 
9982272Sjimand 		oldval = sfmmup->sfmmu_pgsz[0] << 8 | sfmmup->sfmmu_pgsz[1];
9992272Sjimand 
10002272Sjimand 		mmu_setup_page_sizes(sfmmup, ttecnt, tmp_pgsz);
10012272Sjimand 
10022272Sjimand 		/* Check 2 largest values after the sort. */
10032272Sjimand 		newval = tmp_pgsz[0] << 8 | tmp_pgsz[1];
10042272Sjimand 		if (newval != oldval) {
10052272Sjimand 			sfmmu_reprog_pgsz_arr(sfmmup, tmp_pgsz);
10062272Sjimand 		}
10072272Sjimand 	}
10082272Sjimand }
10092272Sjimand 
10102272Sjimand /*
10111772Sjl139090  * Return processor specific async error structure
10121772Sjl139090  * size used.
10131772Sjl139090  */
10141772Sjl139090 int
10151772Sjl139090 cpu_aflt_size(void)
10161772Sjl139090 {
10171772Sjl139090 	return (sizeof (opl_async_flt_t));
10181772Sjl139090 }
10191772Sjl139090 
10201772Sjl139090 /*
10211772Sjl139090  * The cpu_sync_log_err() function is called via the [uc]e_drain() function to
10221772Sjl139090  * post-process CPU events that are dequeued.  As such, it can be invoked
10231772Sjl139090  * from softint context, from AST processing in the trap() flow, or from the
10241772Sjl139090  * panic flow.  We decode the CPU-specific data, and take appropriate actions.
10251772Sjl139090  * Historically this entry point was used to log the actual cmn_err(9F) text;
10261772Sjl139090  * now with FMA it is used to prepare 'flt' to be converted into an ereport.
10271772Sjl139090  * With FMA this function now also returns a flag which indicates to the
10281772Sjl139090  * caller whether the ereport should be posted (1) or suppressed (0).
10291772Sjl139090  */
10301772Sjl139090 /*ARGSUSED*/
10311772Sjl139090 static int
10321772Sjl139090 cpu_sync_log_err(void *flt)
10331772Sjl139090 {
10341772Sjl139090 	opl_async_flt_t *opl_flt = (opl_async_flt_t *)flt;
10351772Sjl139090 	struct async_flt *aflt = (struct async_flt *)flt;
10361772Sjl139090 
10371772Sjl139090 	/*
10381772Sjl139090 	 * No extra processing of urgent error events.
10391772Sjl139090 	 * Always generate ereports for these events.
10401772Sjl139090 	 */
10411772Sjl139090 	if (aflt->flt_status == OPL_ECC_URGENT_TRAP)
10421772Sjl139090 		return (1);
10431772Sjl139090 
10441772Sjl139090 	/*
10451772Sjl139090 	 * Additional processing for synchronous errors.
10461772Sjl139090 	 */
10471772Sjl139090 	switch (opl_flt->flt_type) {
10481772Sjl139090 	case OPL_CPU_INV_SFSR:
10491772Sjl139090 		return (1);
10501772Sjl139090 
10511772Sjl139090 	case OPL_CPU_SYNC_UE:
10521772Sjl139090 		/*
10531772Sjl139090 		 * The validity: SFSR_MK_UE bit has been checked
10541772Sjl139090 		 * in opl_cpu_sync_error()
10551772Sjl139090 		 * No more check is required.
10561772Sjl139090 		 *
10571772Sjl139090 		 * opl_flt->flt_eid_mod and flt_eid_sid have been set by H/W,
10581772Sjl139090 		 * and they have been retrieved in cpu_queue_events()
10591772Sjl139090 		 */
10601772Sjl139090 
10611772Sjl139090 		if (opl_flt->flt_eid_mod == OPL_ERRID_MEM) {
10621772Sjl139090 			ASSERT(aflt->flt_in_memory);
10631772Sjl139090 			/*
10641772Sjl139090 			 * We want to skip logging only if ALL the following
10651772Sjl139090 			 * conditions are true:
10661772Sjl139090 			 *
10671772Sjl139090 			 *	1. We are not panicing already.
10681772Sjl139090 			 *	2. The error is a memory error.
10691772Sjl139090 			 *	3. There is only one error.
10701772Sjl139090 			 *	4. The error is on a retired page.
10711772Sjl139090 			 *	5. The error occurred under on_trap
10721772Sjl139090 			 *	protection AFLT_PROT_EC
10731772Sjl139090 			 */
10741772Sjl139090 			if (!panicstr && aflt->flt_prot == AFLT_PROT_EC &&
10751772Sjl139090 			    page_retire_check(aflt->flt_addr, NULL) == 0) {
10761772Sjl139090 				/*
10771772Sjl139090 				 * Do not log an error from
10781772Sjl139090 				 * the retired page
10791772Sjl139090 				 */
10801772Sjl139090 				softcall(ecc_page_zero, (void *)aflt->flt_addr);
10811772Sjl139090 				return (0);
10821772Sjl139090 			}
10831772Sjl139090 			if (!panicstr)
10841772Sjl139090 				cpu_page_retire(opl_flt);
10851772Sjl139090 		}
10861772Sjl139090 		return (1);
10871772Sjl139090 
10881772Sjl139090 	case OPL_CPU_SYNC_OTHERS:
10891772Sjl139090 		/*
10901772Sjl139090 		 * For the following error cases, the processor HW does
10911772Sjl139090 		 * not set the flt_eid_mod/flt_eid_sid. Instead, SW will attempt
10921772Sjl139090 		 * to assign appropriate values here to reflect what we
10931772Sjl139090 		 * think is the most likely cause of the problem w.r.t to
10941772Sjl139090 		 * the particular error event.  For Buserr and timeout
10951772Sjl139090 		 * error event, we will assign OPL_ERRID_CHANNEL as the
10961772Sjl139090 		 * most likely reason.  For TLB parity or multiple hit
10971772Sjl139090 		 * error events, we will assign the reason as
10981772Sjl139090 		 * OPL_ERRID_CPU (cpu related problem) and set the
10991772Sjl139090 		 * flt_eid_sid to point to the cpuid.
11001772Sjl139090 		 */
11011772Sjl139090 
11021772Sjl139090 		if (opl_flt->flt_bit & (SFSR_BERR|SFSR_TO)) {
11031772Sjl139090 			/*
11041772Sjl139090 			 * flt_eid_sid will not be used for this case.
11051772Sjl139090 			 */
11061772Sjl139090 			opl_flt->flt_eid_mod = OPL_ERRID_CHANNEL;
11071772Sjl139090 		}
11081772Sjl139090 		if (opl_flt->flt_bit & (SFSR_TLB_MUL|SFSR_TLB_PRT)) {
11095037Sjl139090 			opl_flt->flt_eid_mod = OPL_ERRID_CPU;
11105037Sjl139090 			opl_flt->flt_eid_sid = aflt->flt_inst;
11111772Sjl139090 		}
11121772Sjl139090 
11131772Sjl139090 		/*
11141772Sjl139090 		 * In case of no effective error bit
11151772Sjl139090 		 */
11161772Sjl139090 		if ((opl_flt->flt_bit & SFSR_ERRS) == 0) {
11175037Sjl139090 			opl_flt->flt_eid_mod = OPL_ERRID_CPU;
11185037Sjl139090 			opl_flt->flt_eid_sid = aflt->flt_inst;
11191772Sjl139090 		}
11201772Sjl139090 		break;
11211772Sjl139090 
11221772Sjl139090 		default:
11231772Sjl139090 			return (1);
11241772Sjl139090 	}
11251772Sjl139090 	return (1);
11261772Sjl139090 }
11271772Sjl139090 
11281772Sjl139090 /*
11291772Sjl139090  * Retire the bad page that may contain the flushed error.
11301772Sjl139090  */
11311772Sjl139090 void
11321772Sjl139090 cpu_page_retire(opl_async_flt_t *opl_flt)
11331772Sjl139090 {
11341772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
11351772Sjl139090 	(void) page_retire(aflt->flt_addr, PR_UE);
11361772Sjl139090 }
11371772Sjl139090 
11381772Sjl139090 /*
11391772Sjl139090  * Invoked by error_init() early in startup and therefore before
11401772Sjl139090  * startup_errorq() is called to drain any error Q -
11411772Sjl139090  *
11421772Sjl139090  * startup()
11431772Sjl139090  *   startup_end()
11441772Sjl139090  *     error_init()
11451772Sjl139090  *       cpu_error_init()
11461772Sjl139090  * errorq_init()
11471772Sjl139090  *   errorq_drain()
11481772Sjl139090  * start_other_cpus()
11491772Sjl139090  *
11501772Sjl139090  * The purpose of this routine is to create error-related taskqs.  Taskqs
11511772Sjl139090  * are used for this purpose because cpu_lock can't be grabbed from interrupt
11521772Sjl139090  * context.
11531772Sjl139090  *
11541772Sjl139090  */
11551772Sjl139090 /*ARGSUSED*/
11561772Sjl139090 void
11571772Sjl139090 cpu_error_init(int items)
11581772Sjl139090 {
11591772Sjl139090 	opl_err_log = (opl_errlog_t *)
11601772Sjl139090 	    kmem_alloc(ERRLOG_ALLOC_SZ, KM_SLEEP);
11611772Sjl139090 	if ((uint64_t)opl_err_log & MMU_PAGEOFFSET)
11621772Sjl139090 		cmn_err(CE_PANIC, "The base address of the error log "
11631772Sjl139090 		    "is not page aligned");
11641772Sjl139090 }
11651772Sjl139090 
11661772Sjl139090 /*
11671772Sjl139090  * We route all errors through a single switch statement.
11681772Sjl139090  */
11691772Sjl139090 void
11701772Sjl139090 cpu_ue_log_err(struct async_flt *aflt)
11711772Sjl139090 {
11721772Sjl139090 	switch (aflt->flt_class) {
11731772Sjl139090 	case CPU_FAULT:
11741772Sjl139090 		if (cpu_sync_log_err(aflt))
11751772Sjl139090 			cpu_ereport_post(aflt);
11761772Sjl139090 		break;
11771772Sjl139090 
11781772Sjl139090 	case BUS_FAULT:
11791772Sjl139090 		bus_async_log_err(aflt);
11801772Sjl139090 		break;
11811772Sjl139090 
11821772Sjl139090 	default:
11831772Sjl139090 		cmn_err(CE_WARN, "discarding async error %p with invalid "
11841772Sjl139090 		    "fault class (0x%x)", (void *)aflt, aflt->flt_class);
11851772Sjl139090 		return;
11861772Sjl139090 	}
11871772Sjl139090 }
11881772Sjl139090 
11891772Sjl139090 /*
11901772Sjl139090  * Routine for panic hook callback from panic_idle().
11911772Sjl139090  *
11921772Sjl139090  * Nothing to do here.
11931772Sjl139090  */
11941772Sjl139090 void
11951772Sjl139090 cpu_async_panic_callb(void)
11961772Sjl139090 {
11971772Sjl139090 }
11981772Sjl139090 
11991772Sjl139090 /*
12001772Sjl139090  * Routine to return a string identifying the physical name
12011772Sjl139090  * associated with a memory/cache error.
12021772Sjl139090  */
12031772Sjl139090 /*ARGSUSED*/
12041772Sjl139090 int
12051772Sjl139090 cpu_get_mem_unum(int synd_status, ushort_t flt_synd, uint64_t flt_stat,
12061772Sjl139090     uint64_t flt_addr, int flt_bus_id, int flt_in_memory,
12071772Sjl139090     ushort_t flt_status, char *buf, int buflen, int *lenp)
12081772Sjl139090 {
12091772Sjl139090 	int synd_code;
12101772Sjl139090 	int ret;
12111772Sjl139090 
12121772Sjl139090 	/*
12131772Sjl139090 	 * An AFSR of -1 defaults to a memory syndrome.
12141772Sjl139090 	 */
12151772Sjl139090 	synd_code = (int)flt_synd;
12161772Sjl139090 
12171772Sjl139090 	if (&plat_get_mem_unum) {
12181772Sjl139090 		if ((ret = plat_get_mem_unum(synd_code, flt_addr, flt_bus_id,
12195037Sjl139090 		    flt_in_memory, flt_status, buf, buflen, lenp)) != 0) {
12201772Sjl139090 			buf[0] = '\0';
12211772Sjl139090 			*lenp = 0;
12221772Sjl139090 		}
12231772Sjl139090 		return (ret);
12241772Sjl139090 	}
12251772Sjl139090 	buf[0] = '\0';
12261772Sjl139090 	*lenp = 0;
12271772Sjl139090 	return (ENOTSUP);
12281772Sjl139090 }
12291772Sjl139090 
12301772Sjl139090 /*
12311772Sjl139090  * Wrapper for cpu_get_mem_unum() routine that takes an
12321772Sjl139090  * async_flt struct rather than explicit arguments.
12331772Sjl139090  */
12341772Sjl139090 int
12351772Sjl139090 cpu_get_mem_unum_aflt(int synd_status, struct async_flt *aflt,
12361772Sjl139090     char *buf, int buflen, int *lenp)
12371772Sjl139090 {
12381772Sjl139090 	/*
12391772Sjl139090 	 * We always pass -1 so that cpu_get_mem_unum will interpret this as a
12401772Sjl139090 	 * memory error.
12411772Sjl139090 	 */
12421772Sjl139090 	return (cpu_get_mem_unum(synd_status, aflt->flt_synd,
12431772Sjl139090 	    (uint64_t)-1,
12441772Sjl139090 	    aflt->flt_addr, aflt->flt_bus_id, aflt->flt_in_memory,
12451772Sjl139090 	    aflt->flt_status, buf, buflen, lenp));
12461772Sjl139090 }
12471772Sjl139090 
12481772Sjl139090 /*
12491772Sjl139090  * This routine is a more generic interface to cpu_get_mem_unum()
12501772Sjl139090  * that may be used by other modules (e.g. mm).
12511772Sjl139090  */
12521772Sjl139090 /*ARGSUSED*/
12531772Sjl139090 int
12541772Sjl139090 cpu_get_mem_name(uint64_t synd, uint64_t *afsr, uint64_t afar,
12551772Sjl139090     char *buf, int buflen, int *lenp)
12561772Sjl139090 {
12571772Sjl139090 	int synd_status, flt_in_memory, ret;
12581772Sjl139090 	ushort_t flt_status = 0;
12591772Sjl139090 	char unum[UNUM_NAMLEN];
12601772Sjl139090 
12611772Sjl139090 	/*
12621772Sjl139090 	 * Check for an invalid address.
12631772Sjl139090 	 */
12641772Sjl139090 	if (afar == (uint64_t)-1)
12651772Sjl139090 		return (ENXIO);
12661772Sjl139090 
12671772Sjl139090 	if (synd == (uint64_t)-1)
12681772Sjl139090 		synd_status = AFLT_STAT_INVALID;
12691772Sjl139090 	else
12701772Sjl139090 		synd_status = AFLT_STAT_VALID;
12711772Sjl139090 
12721772Sjl139090 	flt_in_memory = (*afsr & SFSR_MEMORY) &&
12735037Sjl139090 	    pf_is_memory(afar >> MMU_PAGESHIFT);
12741772Sjl139090 
12751772Sjl139090 	ret = cpu_get_mem_unum(synd_status, (ushort_t)synd, *afsr, afar,
12765037Sjl139090 	    CPU->cpu_id, flt_in_memory, flt_status, unum, UNUM_NAMLEN, lenp);
12771772Sjl139090 	if (ret != 0)
12781772Sjl139090 		return (ret);
12791772Sjl139090 
12801772Sjl139090 	if (*lenp >= buflen)
12811772Sjl139090 		return (ENAMETOOLONG);
12821772Sjl139090 
12831772Sjl139090 	(void) strncpy(buf, unum, buflen);
12841772Sjl139090 
12851772Sjl139090 	return (0);
12861772Sjl139090 }
12871772Sjl139090 
12881772Sjl139090 /*
12891772Sjl139090  * Routine to return memory information associated
12901772Sjl139090  * with a physical address and syndrome.
12911772Sjl139090  */
12921772Sjl139090 /*ARGSUSED*/
12931772Sjl139090 int
12941772Sjl139090 cpu_get_mem_info(uint64_t synd, uint64_t afar,
12951772Sjl139090     uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
12961772Sjl139090     int *segsp, int *banksp, int *mcidp)
12971772Sjl139090 {
12981772Sjl139090 	int synd_code = (int)synd;
12991772Sjl139090 
13001772Sjl139090 	if (afar == (uint64_t)-1)
13011772Sjl139090 		return (ENXIO);
13021772Sjl139090 
13031772Sjl139090 	if (p2get_mem_info != NULL)
13045037Sjl139090 		return ((p2get_mem_info)(synd_code, afar, mem_sizep, seg_sizep,
13055037Sjl139090 		    bank_sizep, segsp, banksp, mcidp));
13061772Sjl139090 	else
13071772Sjl139090 		return (ENOTSUP);
13081772Sjl139090 }
13091772Sjl139090 
13101772Sjl139090 /*
13111772Sjl139090  * Routine to return a string identifying the physical
13121772Sjl139090  * name associated with a cpuid.
13131772Sjl139090  */
13141772Sjl139090 int
13151772Sjl139090 cpu_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
13161772Sjl139090 {
13171772Sjl139090 	int ret;
13181772Sjl139090 	char unum[UNUM_NAMLEN];
13191772Sjl139090 
13201772Sjl139090 	if (&plat_get_cpu_unum) {
13215037Sjl139090 		if ((ret = plat_get_cpu_unum(cpuid, unum, UNUM_NAMLEN,
13225037Sjl139090 		    lenp)) != 0)
13231772Sjl139090 			return (ret);
13241772Sjl139090 	} else {
13251772Sjl139090 		return (ENOTSUP);
13261772Sjl139090 	}
13271772Sjl139090 
13281772Sjl139090 	if (*lenp >= buflen)
13291772Sjl139090 		return (ENAMETOOLONG);
13301772Sjl139090 
13311772Sjl139090 	(void) strncpy(buf, unum, *lenp);
13321772Sjl139090 
13331772Sjl139090 	return (0);
13341772Sjl139090 }
13351772Sjl139090 
13361772Sjl139090 /*
13371772Sjl139090  * This routine exports the name buffer size.
13381772Sjl139090  */
13391772Sjl139090 size_t
13401772Sjl139090 cpu_get_name_bufsize()
13411772Sjl139090 {
13421772Sjl139090 	return (UNUM_NAMLEN);
13431772Sjl139090 }
13441772Sjl139090 
13451772Sjl139090 /*
13461772Sjl139090  * Flush the entire ecache by ASI_L2_CNTL.U2_FLUSH
13471772Sjl139090  */
13481772Sjl139090 void
13491772Sjl139090 cpu_flush_ecache(void)
13501772Sjl139090 {
13511772Sjl139090 	flush_ecache(ecache_flushaddr, cpunodes[CPU->cpu_id].ecache_size,
13521772Sjl139090 	    cpunodes[CPU->cpu_id].ecache_linesize);
13531772Sjl139090 }
13541772Sjl139090 
13551772Sjl139090 static uint8_t
13561772Sjl139090 flt_to_trap_type(struct async_flt *aflt)
13571772Sjl139090 {
13581772Sjl139090 	if (aflt->flt_status & OPL_ECC_ISYNC_TRAP)
13591772Sjl139090 		return (TRAP_TYPE_ECC_I);
13601772Sjl139090 	if (aflt->flt_status & OPL_ECC_DSYNC_TRAP)
13611772Sjl139090 		return (TRAP_TYPE_ECC_D);
13621772Sjl139090 	if (aflt->flt_status & OPL_ECC_URGENT_TRAP)
13631772Sjl139090 		return (TRAP_TYPE_URGENT);
13643382Sjimand 	return (TRAP_TYPE_UNKNOWN);
13651772Sjl139090 }
13661772Sjl139090 
13671772Sjl139090 /*
13681772Sjl139090  * Encode the data saved in the opl_async_flt_t struct into
13691772Sjl139090  * the FM ereport payload.
13701772Sjl139090  */
13711772Sjl139090 /* ARGSUSED */
13721772Sjl139090 static void
13731772Sjl139090 cpu_payload_add_aflt(struct async_flt *aflt, nvlist_t *payload,
13741772Sjl139090 		nvlist_t *resource)
13751772Sjl139090 {
13761772Sjl139090 	opl_async_flt_t *opl_flt = (opl_async_flt_t *)aflt;
13771772Sjl139090 	char unum[UNUM_NAMLEN];
13781772Sjl139090 	char sbuf[21]; /* sizeof (UINT64_MAX) + '\0' */
13791772Sjl139090 	int len;
13801772Sjl139090 
13811772Sjl139090 
13821772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_SFSR) {
13831772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_SFSR,
13845037Sjl139090 		    DATA_TYPE_UINT64, aflt->flt_stat, NULL);
13851772Sjl139090 	}
13861772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_SFAR) {
13871772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_SFAR,
13885037Sjl139090 		    DATA_TYPE_UINT64, aflt->flt_addr, NULL);
13891772Sjl139090 	}
13901772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_UGESR) {
13911772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_UGESR,
13925037Sjl139090 		    DATA_TYPE_UINT64, aflt->flt_stat, NULL);
13931772Sjl139090 	}
13941772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_PC) {
13951772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_PC,
13961772Sjl139090 		    DATA_TYPE_UINT64, (uint64_t)aflt->flt_pc, NULL);
13971772Sjl139090 	}
13981772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_TL) {
13991772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_TL,
14001772Sjl139090 		    DATA_TYPE_UINT8, (uint8_t)aflt->flt_tl, NULL);
14011772Sjl139090 	}
14021772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_TT) {
14031772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_TT,
14041772Sjl139090 		    DATA_TYPE_UINT8, flt_to_trap_type(aflt), NULL);
14051772Sjl139090 	}
14061772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_PRIV) {
14071772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_PRIV,
14081772Sjl139090 		    DATA_TYPE_BOOLEAN_VALUE,
14091772Sjl139090 		    (aflt->flt_priv ? B_TRUE : B_FALSE), NULL);
14101772Sjl139090 	}
14111772Sjl139090 	if (aflt->flt_payload & FM_EREPORT_PAYLOAD_FLAG_FLT_STATUS) {
14121772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_FLT_STATUS,
14135037Sjl139090 		    DATA_TYPE_UINT64, (uint64_t)aflt->flt_status, NULL);
14141772Sjl139090 	}
14151772Sjl139090 
14161772Sjl139090 	switch (opl_flt->flt_eid_mod) {
14171772Sjl139090 	case OPL_ERRID_CPU:
14181772Sjl139090 		(void) snprintf(sbuf, sizeof (sbuf), "%llX",
14195037Sjl139090 		    (u_longlong_t)cpunodes[opl_flt->flt_eid_sid].device_id);
14201772Sjl139090 		(void) fm_fmri_cpu_set(resource, FM_CPU_SCHEME_VERSION,
14215037Sjl139090 		    NULL, opl_flt->flt_eid_sid,
14225037Sjl139090 		    (uint8_t *)&cpunodes[opl_flt->flt_eid_sid].version, sbuf);
14235037Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_RESOURCE,
14245037Sjl139090 		    DATA_TYPE_NVLIST, resource, NULL);
14251772Sjl139090 		break;
14261772Sjl139090 
14271772Sjl139090 	case OPL_ERRID_CHANNEL:
14281772Sjl139090 		/*
14291772Sjl139090 		 * No resource is created but the cpumem DE will find
14301772Sjl139090 		 * the defective path by retreiving EID from SFSR which is
14311772Sjl139090 		 * included in the payload.
14321772Sjl139090 		 */
14331772Sjl139090 		break;
14341772Sjl139090 
14351772Sjl139090 	case OPL_ERRID_MEM:
14361772Sjl139090 		(void) cpu_get_mem_unum_aflt(0, aflt, unum, UNUM_NAMLEN, &len);
14375037Sjl139090 		(void) fm_fmri_mem_set(resource, FM_MEM_SCHEME_VERSION, NULL,
14385037Sjl139090 		    unum, NULL, (uint64_t)-1);
14391772Sjl139090 		fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_RESOURCE,
14405037Sjl139090 		    DATA_TYPE_NVLIST, resource, NULL);
14411772Sjl139090 		break;
14421772Sjl139090 
14431772Sjl139090 	case OPL_ERRID_PATH:
14441772Sjl139090 		/*
14451772Sjl139090 		 * No resource is created but the cpumem DE will find
14461772Sjl139090 		 * the defective path by retreiving EID from SFSR which is
14471772Sjl139090 		 * included in the payload.
14481772Sjl139090 		 */
14491772Sjl139090 		break;
14501772Sjl139090 	}
14511772Sjl139090 }
14521772Sjl139090 
14531772Sjl139090 /*
14541772Sjl139090  * Returns whether fault address is valid for this error bit and
14551772Sjl139090  * whether the address is "in memory" (i.e. pf_is_memory returns 1).
14561772Sjl139090  */
14571772Sjl139090 /*ARGSUSED*/
14581772Sjl139090 static int
14591772Sjl139090 cpu_flt_in_memory(opl_async_flt_t *opl_flt, uint64_t t_afsr_bit)
14601772Sjl139090 {
14611772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
14621772Sjl139090 
14631772Sjl139090 	if (aflt->flt_status & (OPL_ECC_SYNC_TRAP)) {
14641772Sjl139090 		return ((t_afsr_bit & SFSR_MEMORY) &&
14651772Sjl139090 		    pf_is_memory(aflt->flt_addr >> MMU_PAGESHIFT));
14661772Sjl139090 	}
14671772Sjl139090 	return (0);
14681772Sjl139090 }
14691772Sjl139090 
14701772Sjl139090 /*
14711772Sjl139090  * In OPL SCF does the stick synchronization.
14721772Sjl139090  */
14731772Sjl139090 void
14741772Sjl139090 sticksync_slave(void)
14751772Sjl139090 {
14761772Sjl139090 }
14771772Sjl139090 
14781772Sjl139090 /*
14791772Sjl139090  * In OPL SCF does the stick synchronization.
14801772Sjl139090  */
14811772Sjl139090 void
14821772Sjl139090 sticksync_master(void)
14831772Sjl139090 {
14841772Sjl139090 }
14851772Sjl139090 
14861772Sjl139090 /*
14871772Sjl139090  * Cpu private unitialization.  OPL cpus do not use the private area.
14881772Sjl139090  */
14891772Sjl139090 void
14901772Sjl139090 cpu_uninit_private(struct cpu *cp)
14911772Sjl139090 {
14921772Sjl139090 	cmp_delete_cpu(cp->cpu_id);
14931772Sjl139090 }
14941772Sjl139090 
14951772Sjl139090 /*
14961772Sjl139090  * Always flush an entire cache.
14971772Sjl139090  */
14981772Sjl139090 void
14991772Sjl139090 cpu_error_ecache_flush(void)
15001772Sjl139090 {
15011772Sjl139090 	cpu_flush_ecache();
15021772Sjl139090 }
15031772Sjl139090 
15041772Sjl139090 void
15051772Sjl139090 cpu_ereport_post(struct async_flt *aflt)
15061772Sjl139090 {
15071772Sjl139090 	char *cpu_type, buf[FM_MAX_CLASS];
15081772Sjl139090 	nv_alloc_t *nva = NULL;
15091772Sjl139090 	nvlist_t *ereport, *detector, *resource;
15101772Sjl139090 	errorq_elem_t *eqep;
15111772Sjl139090 	char sbuf[21]; /* sizeof (UINT64_MAX) + '\0' */
15121772Sjl139090 
15131772Sjl139090 	if (aflt->flt_panic || panicstr) {
15141772Sjl139090 		eqep = errorq_reserve(ereport_errorq);
15151772Sjl139090 		if (eqep == NULL)
15161772Sjl139090 			return;
15171772Sjl139090 		ereport = errorq_elem_nvl(ereport_errorq, eqep);
15181772Sjl139090 		nva = errorq_elem_nva(ereport_errorq, eqep);
15191772Sjl139090 	} else {
15201772Sjl139090 		ereport = fm_nvlist_create(nva);
15211772Sjl139090 	}
15221772Sjl139090 
15231772Sjl139090 	/*
15241772Sjl139090 	 * Create the scheme "cpu" FMRI.
15251772Sjl139090 	 */
15261772Sjl139090 	detector = fm_nvlist_create(nva);
15271772Sjl139090 	resource = fm_nvlist_create(nva);
15281772Sjl139090 	switch (cpunodes[aflt->flt_inst].implementation) {
15291772Sjl139090 	case OLYMPUS_C_IMPL:
15301772Sjl139090 		cpu_type = FM_EREPORT_CPU_SPARC64_VI;
15311772Sjl139090 		break;
15325037Sjl139090 	case JUPITER_IMPL:
15335037Sjl139090 		cpu_type = FM_EREPORT_CPU_SPARC64_VII;
15345037Sjl139090 		break;
15351772Sjl139090 	default:
15361772Sjl139090 		cpu_type = FM_EREPORT_CPU_UNSUPPORTED;
15371772Sjl139090 		break;
15381772Sjl139090 	}
15391772Sjl139090 	(void) snprintf(sbuf, sizeof (sbuf), "%llX",
15401772Sjl139090 	    (u_longlong_t)cpunodes[aflt->flt_inst].device_id);
15411772Sjl139090 	(void) fm_fmri_cpu_set(detector, FM_CPU_SCHEME_VERSION, NULL,
15421772Sjl139090 	    aflt->flt_inst, (uint8_t *)&cpunodes[aflt->flt_inst].version,
15431772Sjl139090 	    sbuf);
15441772Sjl139090 
15451772Sjl139090 	/*
15461772Sjl139090 	 * Encode all the common data into the ereport.
15471772Sjl139090 	 */
15481772Sjl139090 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s.%s",
15491772Sjl139090 	    FM_ERROR_CPU, cpu_type, aflt->flt_erpt_class);
15501772Sjl139090 
15511772Sjl139090 	fm_ereport_set(ereport, FM_EREPORT_VERSION, buf,
15521772Sjl139090 	    fm_ena_generate(aflt->flt_id, FM_ENA_FMT1), detector, NULL);
15531772Sjl139090 
15541772Sjl139090 	/*
15551772Sjl139090 	 * Encode the error specific data that was saved in
15561772Sjl139090 	 * the async_flt structure into the ereport.
15571772Sjl139090 	 */
15581772Sjl139090 	cpu_payload_add_aflt(aflt, ereport, resource);
15591772Sjl139090 
15601772Sjl139090 	if (aflt->flt_panic || panicstr) {
15611772Sjl139090 		errorq_commit(ereport_errorq, eqep, ERRORQ_SYNC);
15621772Sjl139090 	} else {
15631772Sjl139090 		(void) fm_ereport_post(ereport, EVCH_TRYHARD);
15641772Sjl139090 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
15651772Sjl139090 		fm_nvlist_destroy(detector, FM_NVA_FREE);
15661772Sjl139090 		fm_nvlist_destroy(resource, FM_NVA_FREE);
15671772Sjl139090 	}
15681772Sjl139090 }
15691772Sjl139090 
15701772Sjl139090 void
15711772Sjl139090 cpu_run_bus_error_handlers(struct async_flt *aflt, int expected)
15721772Sjl139090 {
15731772Sjl139090 	int status;
15741772Sjl139090 	ddi_fm_error_t de;
15751772Sjl139090 
15761772Sjl139090 	bzero(&de, sizeof (ddi_fm_error_t));
15771772Sjl139090 
15781772Sjl139090 	de.fme_version = DDI_FME_VERSION;
15791772Sjl139090 	de.fme_ena = fm_ena_generate(aflt->flt_id, FM_ENA_FMT1);
15801772Sjl139090 	de.fme_flag = expected;
15811772Sjl139090 	de.fme_bus_specific = (void *)aflt->flt_addr;
15821772Sjl139090 	status = ndi_fm_handler_dispatch(ddi_root_node(), NULL, &de);
15831772Sjl139090 	if ((aflt->flt_prot == AFLT_PROT_NONE) && (status == DDI_FM_FATAL))
15841772Sjl139090 		aflt->flt_panic = 1;
15851772Sjl139090 }
15861772Sjl139090 
15871772Sjl139090 void
15881772Sjl139090 cpu_errorq_dispatch(char *error_class, void *payload, size_t payload_sz,
15891772Sjl139090     errorq_t *eqp, uint_t flag)
15901772Sjl139090 {
15911772Sjl139090 	struct async_flt *aflt = (struct async_flt *)payload;
15921772Sjl139090 
15931772Sjl139090 	aflt->flt_erpt_class = error_class;
15941772Sjl139090 	errorq_dispatch(eqp, payload, payload_sz, flag);
15951772Sjl139090 }
15961772Sjl139090 
15971772Sjl139090 void
15981772Sjl139090 adjust_hw_copy_limits(int ecache_size)
15991772Sjl139090 {
16001772Sjl139090 	/*
16011772Sjl139090 	 * Set hw copy limits.
16021772Sjl139090 	 *
16031772Sjl139090 	 * /etc/system will be parsed later and can override one or more
16041772Sjl139090 	 * of these settings.
16051772Sjl139090 	 *
16061772Sjl139090 	 * At this time, ecache size seems only mildly relevant.
16071772Sjl139090 	 * We seem to run into issues with the d-cache and stalls
16081772Sjl139090 	 * we see on misses.
16091772Sjl139090 	 *
16101772Sjl139090 	 * Cycle measurement indicates that 2 byte aligned copies fare
16111772Sjl139090 	 * little better than doing things with VIS at around 512 bytes.
16121772Sjl139090 	 * 4 byte aligned shows promise until around 1024 bytes. 8 Byte
16131772Sjl139090 	 * aligned is faster whenever the source and destination data
16141772Sjl139090 	 * in cache and the total size is less than 2 Kbytes.  The 2K
16151772Sjl139090 	 * limit seems to be driven by the 2K write cache.
16161772Sjl139090 	 * When more than 2K of copies are done in non-VIS mode, stores
16171772Sjl139090 	 * backup in the write cache.  In VIS mode, the write cache is
16181772Sjl139090 	 * bypassed, allowing faster cache-line writes aligned on cache
16191772Sjl139090 	 * boundaries.
16201772Sjl139090 	 *
16211772Sjl139090 	 * In addition, in non-VIS mode, there is no prefetching, so
16221772Sjl139090 	 * for larger copies, the advantage of prefetching to avoid even
16231772Sjl139090 	 * occasional cache misses is enough to justify using the VIS code.
16241772Sjl139090 	 *
16251772Sjl139090 	 * During testing, it was discovered that netbench ran 3% slower
16261772Sjl139090 	 * when hw_copy_limit_8 was 2K or larger.  Apparently for server
16271772Sjl139090 	 * applications, data is only used once (copied to the output
16281772Sjl139090 	 * buffer, then copied by the network device off the system).  Using
16291772Sjl139090 	 * the VIS copy saves more L2 cache state.  Network copies are
16301772Sjl139090 	 * around 1.3K to 1.5K in size for historical reasons.
16311772Sjl139090 	 *
16321772Sjl139090 	 * Therefore, a limit of 1K bytes will be used for the 8 byte
16331772Sjl139090 	 * aligned copy even for large caches and 8 MB ecache.  The
16341772Sjl139090 	 * infrastructure to allow different limits for different sized
16351772Sjl139090 	 * caches is kept to allow further tuning in later releases.
16361772Sjl139090 	 */
16371772Sjl139090 
16381772Sjl139090 	if (min_ecache_size == 0 && use_hw_bcopy) {
16391772Sjl139090 		/*
16401772Sjl139090 		 * First time through - should be before /etc/system
16411772Sjl139090 		 * is read.
16421772Sjl139090 		 * Could skip the checks for zero but this lets us
16431772Sjl139090 		 * preserve any debugger rewrites.
16441772Sjl139090 		 */
16451772Sjl139090 		if (hw_copy_limit_1 == 0) {
16461772Sjl139090 			hw_copy_limit_1 = VIS_COPY_THRESHOLD;
16471772Sjl139090 			priv_hcl_1 = hw_copy_limit_1;
16481772Sjl139090 		}
16491772Sjl139090 		if (hw_copy_limit_2 == 0) {
16501772Sjl139090 			hw_copy_limit_2 = 2 * VIS_COPY_THRESHOLD;
16511772Sjl139090 			priv_hcl_2 = hw_copy_limit_2;
16521772Sjl139090 		}
16531772Sjl139090 		if (hw_copy_limit_4 == 0) {
16541772Sjl139090 			hw_copy_limit_4 = 4 * VIS_COPY_THRESHOLD;
16551772Sjl139090 			priv_hcl_4 = hw_copy_limit_4;
16561772Sjl139090 		}
16571772Sjl139090 		if (hw_copy_limit_8 == 0) {
16581772Sjl139090 			hw_copy_limit_8 = 4 * VIS_COPY_THRESHOLD;
16591772Sjl139090 			priv_hcl_8 = hw_copy_limit_8;
16601772Sjl139090 		}
16611772Sjl139090 		min_ecache_size = ecache_size;
16621772Sjl139090 	} else {
16631772Sjl139090 		/*
16641772Sjl139090 		 * MP initialization. Called *after* /etc/system has
16651772Sjl139090 		 * been parsed. One CPU has already been initialized.
16661772Sjl139090 		 * Need to cater for /etc/system having scragged one
16671772Sjl139090 		 * of our values.
16681772Sjl139090 		 */
16691772Sjl139090 		if (ecache_size == min_ecache_size) {
16701772Sjl139090 			/*
16711772Sjl139090 			 * Same size ecache. We do nothing unless we
16721772Sjl139090 			 * have a pessimistic ecache setting. In that
16731772Sjl139090 			 * case we become more optimistic (if the cache is
16741772Sjl139090 			 * large enough).
16751772Sjl139090 			 */
16761772Sjl139090 			if (hw_copy_limit_8 == 4 * VIS_COPY_THRESHOLD) {
16771772Sjl139090 				/*
16781772Sjl139090 				 * Need to adjust hw_copy_limit* from our
16791772Sjl139090 				 * pessimistic uniprocessor value to a more
16801772Sjl139090 				 * optimistic UP value *iff* it hasn't been
16811772Sjl139090 				 * reset.
16821772Sjl139090 				 */
16831772Sjl139090 				if ((ecache_size > 1048576) &&
16841772Sjl139090 				    (priv_hcl_8 == hw_copy_limit_8)) {
16851772Sjl139090 					if (ecache_size <= 2097152)
16861772Sjl139090 						hw_copy_limit_8 = 4 *
16871772Sjl139090 						    VIS_COPY_THRESHOLD;
16881772Sjl139090 					else if (ecache_size <= 4194304)
16891772Sjl139090 						hw_copy_limit_8 = 4 *
16901772Sjl139090 						    VIS_COPY_THRESHOLD;
16911772Sjl139090 					else
16921772Sjl139090 						hw_copy_limit_8 = 4 *
16931772Sjl139090 						    VIS_COPY_THRESHOLD;
16941772Sjl139090 					priv_hcl_8 = hw_copy_limit_8;
16951772Sjl139090 				}
16961772Sjl139090 			}
16971772Sjl139090 		} else if (ecache_size < min_ecache_size) {
16981772Sjl139090 			/*
16991772Sjl139090 			 * A different ecache size. Can this even happen?
17001772Sjl139090 			 */
17011772Sjl139090 			if (priv_hcl_8 == hw_copy_limit_8) {
17021772Sjl139090 				/*
17031772Sjl139090 				 * The previous value that we set
17041772Sjl139090 				 * is unchanged (i.e., it hasn't been
17051772Sjl139090 				 * scragged by /etc/system). Rewrite it.
17061772Sjl139090 				 */
17071772Sjl139090 				if (ecache_size <= 1048576)
17081772Sjl139090 					hw_copy_limit_8 = 8 *
17091772Sjl139090 					    VIS_COPY_THRESHOLD;
17101772Sjl139090 				else if (ecache_size <= 2097152)
17111772Sjl139090 					hw_copy_limit_8 = 8 *
17121772Sjl139090 					    VIS_COPY_THRESHOLD;
17131772Sjl139090 				else if (ecache_size <= 4194304)
17141772Sjl139090 					hw_copy_limit_8 = 8 *
17151772Sjl139090 					    VIS_COPY_THRESHOLD;
17161772Sjl139090 				else
17171772Sjl139090 					hw_copy_limit_8 = 10 *
17181772Sjl139090 					    VIS_COPY_THRESHOLD;
17191772Sjl139090 				priv_hcl_8 = hw_copy_limit_8;
17201772Sjl139090 				min_ecache_size = ecache_size;
17211772Sjl139090 			}
17221772Sjl139090 		}
17231772Sjl139090 	}
17241772Sjl139090 }
17251772Sjl139090 
17261772Sjl139090 #define	VIS_BLOCKSIZE		64
17271772Sjl139090 
17281772Sjl139090 int
17291772Sjl139090 dtrace_blksuword32_err(uintptr_t addr, uint32_t *data)
17301772Sjl139090 {
17311772Sjl139090 	int ret, watched;
17321772Sjl139090 
17331772Sjl139090 	watched = watch_disable_addr((void *)addr, VIS_BLOCKSIZE, S_WRITE);
17341772Sjl139090 	ret = dtrace_blksuword32(addr, data, 0);
17351772Sjl139090 	if (watched)
17361772Sjl139090 		watch_enable_addr((void *)addr, VIS_BLOCKSIZE, S_WRITE);
17371772Sjl139090 
17381772Sjl139090 	return (ret);
17391772Sjl139090 }
17401772Sjl139090 
17411772Sjl139090 void
17421772Sjl139090 opl_cpu_reg_init()
17431772Sjl139090 {
17441772Sjl139090 	uint64_t	this_cpu_log;
17451772Sjl139090 
17466259Smb158278 	if (cpu[getprocessorid()] == &cpu0 && opl_cpu0_log_setup == 1) {
17475414Smb158278 		/*
17485414Smb158278 		 * Support for "ta 3"
17495414Smb158278 		 */
17505414Smb158278 		opl_ta3();
17516259Smb158278 
17526259Smb158278 		/*
17536259Smb158278 		 * If we are being called at boot time on cpu0 the error
17546259Smb158278 		 * log is already set up in cpu_setup. Clear the
17556259Smb158278 		 * opl_cpu0_log_setup flag so that a subsequent DR of cpu0 will
17566259Smb158278 		 * do the proper initialization.
17576259Smb158278 		 */
17586259Smb158278 		opl_cpu0_log_setup = 0;
17591772Sjl139090 		return;
17605414Smb158278 	}
17611772Sjl139090 
17621772Sjl139090 	/*
17631772Sjl139090 	 * Initialize Error log Scratch register for error handling.
17641772Sjl139090 	 */
17651772Sjl139090 
17661772Sjl139090 	this_cpu_log = va_to_pa((void*)(((uint64_t)opl_err_log) +
17675037Sjl139090 	    ERRLOG_BUFSZ * (getprocessorid())));
17681772Sjl139090 	opl_error_setup(this_cpu_log);
17691772Sjl139090 }
17701772Sjl139090 
17711772Sjl139090 /*
17721772Sjl139090  * Queue one event in ue_queue based on ecc_type_to_info entry.
17731772Sjl139090  */
17741772Sjl139090 static void
17751772Sjl139090 cpu_queue_one_event(opl_async_flt_t *opl_flt, char *reason,
17761772Sjl139090     ecc_type_to_info_t *eccp)
17771772Sjl139090 {
17781772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
17791772Sjl139090 
17801772Sjl139090 	if (reason &&
17811772Sjl139090 	    strlen(reason) + strlen(eccp->ec_reason) < MAX_REASON_STRING) {
17821772Sjl139090 		(void) strcat(reason, eccp->ec_reason);
17831772Sjl139090 	}
17841772Sjl139090 
17851772Sjl139090 	opl_flt->flt_bit = eccp->ec_afsr_bit;
17861772Sjl139090 	opl_flt->flt_type = eccp->ec_flt_type;
17871772Sjl139090 	aflt->flt_in_memory = cpu_flt_in_memory(opl_flt, opl_flt->flt_bit);
17881772Sjl139090 	aflt->flt_payload = eccp->ec_err_payload;
17891772Sjl139090 
17901772Sjl139090 	ASSERT(aflt->flt_status & (OPL_ECC_SYNC_TRAP|OPL_ECC_URGENT_TRAP));
17915037Sjl139090 	cpu_errorq_dispatch(eccp->ec_err_class, (void *)opl_flt,
17925037Sjl139090 	    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
17931772Sjl139090 }
17941772Sjl139090 
17951772Sjl139090 /*
17961772Sjl139090  * Queue events on async event queue one event per error bit.
17971772Sjl139090  * Return number of events queued.
17981772Sjl139090  */
17991772Sjl139090 int
18001772Sjl139090 cpu_queue_events(opl_async_flt_t *opl_flt, char *reason, uint64_t t_afsr_errs)
18011772Sjl139090 {
18021772Sjl139090 	struct async_flt *aflt = (struct async_flt *)opl_flt;
18031772Sjl139090 	ecc_type_to_info_t *eccp;
18041772Sjl139090 	int nevents = 0;
18051772Sjl139090 
18061772Sjl139090 	/*
18071772Sjl139090 	 * Queue expected errors, error bit and fault type must must match
18081772Sjl139090 	 * in the ecc_type_to_info table.
18091772Sjl139090 	 */
18101772Sjl139090 	for (eccp = ecc_type_to_info; t_afsr_errs != 0 && eccp->ec_desc != NULL;
18115037Sjl139090 	    eccp++) {
18121772Sjl139090 		if ((eccp->ec_afsr_bit & t_afsr_errs) != 0 &&
18131772Sjl139090 		    (eccp->ec_flags & aflt->flt_status) != 0) {
18141772Sjl139090 			/*
18151772Sjl139090 			 * UE error event can be further
18161772Sjl139090 			 * classified/breakdown into finer granularity
18171772Sjl139090 			 * based on the flt_eid_mod value set by HW.  We do
18181772Sjl139090 			 * special handling here so that we can report UE
18191772Sjl139090 			 * error in finer granularity as ue_mem,
18201772Sjl139090 			 * ue_channel, ue_cpu or ue_path.
18211772Sjl139090 			 */
18221772Sjl139090 			if (eccp->ec_flt_type == OPL_CPU_SYNC_UE) {
18235037Sjl139090 				opl_flt->flt_eid_mod = (aflt->flt_stat &
18245037Sjl139090 				    SFSR_EID_MOD) >> SFSR_EID_MOD_SHIFT;
18255037Sjl139090 				opl_flt->flt_eid_sid = (aflt->flt_stat &
18265037Sjl139090 				    SFSR_EID_SID) >> SFSR_EID_SID_SHIFT;
18271772Sjl139090 				/*
18281772Sjl139090 				 * Need to advance eccp pointer by flt_eid_mod
18291772Sjl139090 				 * so that we get an appropriate ecc pointer
18301772Sjl139090 				 *
18311772Sjl139090 				 * EID			# of advances
18321772Sjl139090 				 * ----------------------------------
18331772Sjl139090 				 * OPL_ERRID_MEM	0
18341772Sjl139090 				 * OPL_ERRID_CHANNEL	1
18351772Sjl139090 				 * OPL_ERRID_CPU	2
18361772Sjl139090 				 * OPL_ERRID_PATH	3
18371772Sjl139090 				 */
18381772Sjl139090 				eccp += opl_flt->flt_eid_mod;
18391772Sjl139090 			}
18401772Sjl139090 			cpu_queue_one_event(opl_flt, reason, eccp);
18411772Sjl139090 			t_afsr_errs &= ~eccp->ec_afsr_bit;
18421772Sjl139090 			nevents++;
18431772Sjl139090 		}
18441772Sjl139090 	}
18451772Sjl139090 
18461772Sjl139090 	return (nevents);
18471772Sjl139090 }
18481772Sjl139090 
18491772Sjl139090 /*
18501772Sjl139090  * Sync. error wrapper functions.
18511772Sjl139090  * We use these functions in order to transfer here from the
18521772Sjl139090  * nucleus trap handler information about trap type (data or
18531772Sjl139090  * instruction) and trap level (0 or above 0). This way we
18541772Sjl139090  * get rid of using SFSR's reserved bits.
18551772Sjl139090  */
18561772Sjl139090 
18571772Sjl139090 #define	OPL_SYNC_TL0	0
18581772Sjl139090 #define	OPL_SYNC_TL1	1
18591772Sjl139090 #define	OPL_ISYNC_ERR	0
18601772Sjl139090 #define	OPL_DSYNC_ERR	1
18611772Sjl139090 
18621772Sjl139090 void
18631772Sjl139090 opl_cpu_isync_tl0_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18641772Sjl139090 {
18651772Sjl139090 	uint64_t t_sfar = p_sfar;
18661772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18671772Sjl139090 
18681772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18691772Sjl139090 	    OPL_SYNC_TL0, OPL_ISYNC_ERR);
18701772Sjl139090 }
18711772Sjl139090 
18721772Sjl139090 void
18731772Sjl139090 opl_cpu_isync_tl1_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18741772Sjl139090 {
18751772Sjl139090 	uint64_t t_sfar = p_sfar;
18761772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18771772Sjl139090 
18781772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18791772Sjl139090 	    OPL_SYNC_TL1, OPL_ISYNC_ERR);
18801772Sjl139090 }
18811772Sjl139090 
18821772Sjl139090 void
18831772Sjl139090 opl_cpu_dsync_tl0_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18841772Sjl139090 {
18851772Sjl139090 	uint64_t t_sfar = p_sfar;
18861772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18871772Sjl139090 
18881772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18891772Sjl139090 	    OPL_SYNC_TL0, OPL_DSYNC_ERR);
18901772Sjl139090 }
18911772Sjl139090 
18921772Sjl139090 void
18931772Sjl139090 opl_cpu_dsync_tl1_error(struct regs *rp, ulong_t p_sfar, ulong_t p_sfsr)
18941772Sjl139090 {
18951772Sjl139090 	uint64_t t_sfar = p_sfar;
18961772Sjl139090 	uint64_t t_sfsr = p_sfsr;
18971772Sjl139090 
18981772Sjl139090 	opl_cpu_sync_error(rp, t_sfar, t_sfsr,
18991772Sjl139090 	    OPL_SYNC_TL1, OPL_DSYNC_ERR);
19001772Sjl139090 }
19011772Sjl139090 
19021772Sjl139090 /*
19031772Sjl139090  * The fj sync err handler transfers control here for UE, BERR, TO, TLB_MUL
19041772Sjl139090  * and TLB_PRT.
19051772Sjl139090  * This function is designed based on cpu_deferred_error().
19061772Sjl139090  */
19071772Sjl139090 
19081772Sjl139090 static void
19091772Sjl139090 opl_cpu_sync_error(struct regs *rp, ulong_t t_sfar, ulong_t t_sfsr,
19101772Sjl139090     uint_t tl, uint_t derr)
19111772Sjl139090 {
19121772Sjl139090 	opl_async_flt_t opl_flt;
19131772Sjl139090 	struct async_flt *aflt;
19141772Sjl139090 	int trampolined = 0;
19151772Sjl139090 	char pr_reason[MAX_REASON_STRING];
19161772Sjl139090 	uint64_t log_sfsr;
19171772Sjl139090 	int expected = DDI_FM_ERR_UNEXPECTED;
19181772Sjl139090 	ddi_acc_hdl_t *hp;
19191772Sjl139090 
19201772Sjl139090 	/*
19211772Sjl139090 	 * We need to look at p_flag to determine if the thread detected an
19221772Sjl139090 	 * error while dumping core.  We can't grab p_lock here, but it's ok
19231772Sjl139090 	 * because we just need a consistent snapshot and we know that everyone
19241772Sjl139090 	 * else will store a consistent set of bits while holding p_lock.  We
19251772Sjl139090 	 * don't have to worry about a race because SDOCORE is set once prior
19261772Sjl139090 	 * to doing i/o from the process's address space and is never cleared.
19271772Sjl139090 	 */
19281772Sjl139090 	uint_t pflag = ttoproc(curthread)->p_flag;
19291772Sjl139090 
19301772Sjl139090 	pr_reason[0] = '\0';
19311772Sjl139090 
19321772Sjl139090 	/*
19331772Sjl139090 	 * handle the specific error
19341772Sjl139090 	 */
19351772Sjl139090 	bzero(&opl_flt, sizeof (opl_async_flt_t));
19361772Sjl139090 	aflt = (struct async_flt *)&opl_flt;
19371772Sjl139090 	aflt->flt_id = gethrtime_waitfree();
19381772Sjl139090 	aflt->flt_bus_id = getprocessorid();
19391772Sjl139090 	aflt->flt_inst = CPU->cpu_id;
19401772Sjl139090 	aflt->flt_stat = t_sfsr;
19411772Sjl139090 	aflt->flt_addr = t_sfar;
19421772Sjl139090 	aflt->flt_pc = (caddr_t)rp->r_pc;
19431772Sjl139090 	aflt->flt_prot = (uchar_t)AFLT_PROT_NONE;
19441772Sjl139090 	aflt->flt_class = (uchar_t)CPU_FAULT;
19455037Sjl139090 	aflt->flt_priv = (uchar_t)(tl == 1 ? 1 : ((rp->r_tstate &
19465037Sjl139090 	    TSTATE_PRIV) ? 1 : 0));
19471772Sjl139090 	aflt->flt_tl = (uchar_t)tl;
19481772Sjl139090 	aflt->flt_panic = (uchar_t)(tl != 0 || aft_testfatal != 0 ||
19491772Sjl139090 	    (t_sfsr & (SFSR_TLB_MUL|SFSR_TLB_PRT)) != 0);
19501772Sjl139090 	aflt->flt_core = (pflag & SDOCORE) ? 1 : 0;
19511772Sjl139090 	aflt->flt_status = (derr) ? OPL_ECC_DSYNC_TRAP : OPL_ECC_ISYNC_TRAP;
19521772Sjl139090 
19531772Sjl139090 	/*
19541772Sjl139090 	 * If SFSR.FV is not set, both SFSR and SFAR/SFPAR values are uncertain.
19551772Sjl139090 	 * So, clear all error bits to avoid mis-handling and force the system
19561772Sjl139090 	 * panicked.
19571772Sjl139090 	 * We skip all the procedures below down to the panic message call.
19581772Sjl139090 	 */
19591772Sjl139090 	if (!(t_sfsr & SFSR_FV)) {
19601772Sjl139090 		opl_flt.flt_type = OPL_CPU_INV_SFSR;
19611772Sjl139090 		aflt->flt_panic = 1;
19621772Sjl139090 		aflt->flt_payload = FM_EREPORT_PAYLOAD_SYNC;
19635037Sjl139090 		cpu_errorq_dispatch(FM_EREPORT_CPU_INV_SFSR, (void *)&opl_flt,
19645037Sjl139090 		    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
19651772Sjl139090 		fm_panic("%sErrors(s)", "invalid SFSR");
19661772Sjl139090 	}
19671772Sjl139090 
19681772Sjl139090 	/*
19691772Sjl139090 	 * If either UE and MK bit is off, this is not valid UE error.
19701772Sjl139090 	 * If it is not valid UE error, clear UE & MK_UE bits to prevent
19711772Sjl139090 	 * mis-handling below.
19721772Sjl139090 	 * aflt->flt_stat keeps the original bits as a reference.
19731772Sjl139090 	 */
19741772Sjl139090 	if ((t_sfsr & (SFSR_MK_UE|SFSR_UE)) !=
19751772Sjl139090 	    (SFSR_MK_UE|SFSR_UE)) {
19761772Sjl139090 		t_sfsr &= ~(SFSR_MK_UE|SFSR_UE);
19771772Sjl139090 	}
19781772Sjl139090 
19791772Sjl139090 	/*
19801772Sjl139090 	 * If the trap occurred in privileged mode at TL=0, we need to check to
19811772Sjl139090 	 * see if we were executing in the kernel under on_trap() or t_lofault
19821772Sjl139090 	 * protection.  If so, modify the saved registers so that we return
19831772Sjl139090 	 * from the trap to the appropriate trampoline routine.
19841772Sjl139090 	 */
19851772Sjl139090 	if (!aflt->flt_panic && aflt->flt_priv && tl == 0) {
19861772Sjl139090 		if (curthread->t_ontrap != NULL) {
19871772Sjl139090 			on_trap_data_t *otp = curthread->t_ontrap;
19881772Sjl139090 
19891772Sjl139090 			if (otp->ot_prot & OT_DATA_EC) {
19901772Sjl139090 				aflt->flt_prot = (uchar_t)AFLT_PROT_EC;
19911772Sjl139090 				otp->ot_trap |= (ushort_t)OT_DATA_EC;
19921772Sjl139090 				rp->r_pc = otp->ot_trampoline;
19931772Sjl139090 				rp->r_npc = rp->r_pc + 4;
19941772Sjl139090 				trampolined = 1;
19951772Sjl139090 			}
19961772Sjl139090 
19971772Sjl139090 			if ((t_sfsr & (SFSR_TO | SFSR_BERR)) &&
19981772Sjl139090 			    (otp->ot_prot & OT_DATA_ACCESS)) {
19991772Sjl139090 				aflt->flt_prot = (uchar_t)AFLT_PROT_ACCESS;
20001772Sjl139090 				otp->ot_trap |= (ushort_t)OT_DATA_ACCESS;
20011772Sjl139090 				rp->r_pc = otp->ot_trampoline;
20021772Sjl139090 				rp->r_npc = rp->r_pc + 4;
20031772Sjl139090 				trampolined = 1;
20041772Sjl139090 				/*
20051772Sjl139090 				 * for peeks and caut_gets errors are expected
20061772Sjl139090 				 */
20071772Sjl139090 				hp = (ddi_acc_hdl_t *)otp->ot_handle;
20081772Sjl139090 				if (!hp)
20091772Sjl139090 					expected = DDI_FM_ERR_PEEK;
20101772Sjl139090 				else if (hp->ah_acc.devacc_attr_access ==
20111772Sjl139090 				    DDI_CAUTIOUS_ACC)
20121772Sjl139090 					expected = DDI_FM_ERR_EXPECTED;
20131772Sjl139090 			}
20141772Sjl139090 
20151772Sjl139090 		} else if (curthread->t_lofault) {
20161772Sjl139090 			aflt->flt_prot = AFLT_PROT_COPY;
20171772Sjl139090 			rp->r_g1 = EFAULT;
20181772Sjl139090 			rp->r_pc = curthread->t_lofault;
20191772Sjl139090 			rp->r_npc = rp->r_pc + 4;
20201772Sjl139090 			trampolined = 1;
20211772Sjl139090 		}
20221772Sjl139090 	}
20231772Sjl139090 
20241772Sjl139090 	/*
20251772Sjl139090 	 * If we're in user mode or we're doing a protected copy, we either
20261772Sjl139090 	 * want the ASTON code below to send a signal to the user process
20271772Sjl139090 	 * or we want to panic if aft_panic is set.
20281772Sjl139090 	 *
20291772Sjl139090 	 * If we're in privileged mode and we're not doing a copy, then we
20301772Sjl139090 	 * need to check if we've trampolined.  If we haven't trampolined,
20311772Sjl139090 	 * we should panic.
20321772Sjl139090 	 */
20331772Sjl139090 	if (!aflt->flt_priv || aflt->flt_prot == AFLT_PROT_COPY) {
20341772Sjl139090 		if (t_sfsr & (SFSR_ERRS & ~(SFSR_BERR | SFSR_TO)))
20351772Sjl139090 			aflt->flt_panic |= aft_panic;
20361772Sjl139090 	} else if (!trampolined) {
20371772Sjl139090 		aflt->flt_panic = 1;
20381772Sjl139090 	}
20391772Sjl139090 
20401772Sjl139090 	/*
20411772Sjl139090 	 * If we've trampolined due to a privileged TO or BERR, or if an
20421772Sjl139090 	 * unprivileged TO or BERR occurred, we don't want to enqueue an
20431772Sjl139090 	 * event for that TO or BERR.  Queue all other events (if any) besides
20441772Sjl139090 	 * the TO/BERR.
20451772Sjl139090 	 */
20461772Sjl139090 	log_sfsr = t_sfsr;
20471772Sjl139090 	if (trampolined) {
20481772Sjl139090 		log_sfsr &= ~(SFSR_TO | SFSR_BERR);
20491772Sjl139090 	} else if (!aflt->flt_priv) {
20501772Sjl139090 		/*
20511772Sjl139090 		 * User mode, suppress messages if
20521772Sjl139090 		 * cpu_berr_to_verbose is not set.
20531772Sjl139090 		 */
20541772Sjl139090 		if (!cpu_berr_to_verbose)
20551772Sjl139090 			log_sfsr &= ~(SFSR_TO | SFSR_BERR);
20561772Sjl139090 	}
20571772Sjl139090 
20585037Sjl139090 	if (((log_sfsr & SFSR_ERRS) && (cpu_queue_events(&opl_flt, pr_reason,
20595037Sjl139090 	    t_sfsr) == 0)) || ((t_sfsr & SFSR_ERRS) == 0)) {
20601772Sjl139090 		opl_flt.flt_type = OPL_CPU_INV_SFSR;
20611772Sjl139090 		aflt->flt_payload = FM_EREPORT_PAYLOAD_SYNC;
20625037Sjl139090 		cpu_errorq_dispatch(FM_EREPORT_CPU_INV_SFSR, (void *)&opl_flt,
20635037Sjl139090 		    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
20641772Sjl139090 	}
20651772Sjl139090 
20661772Sjl139090 	if (t_sfsr & (SFSR_UE|SFSR_TO|SFSR_BERR)) {
20671772Sjl139090 		cpu_run_bus_error_handlers(aflt, expected);
20681772Sjl139090 	}
20691772Sjl139090 
20701772Sjl139090 	/*
20711772Sjl139090 	 * Panic here if aflt->flt_panic has been set.  Enqueued errors will
20721772Sjl139090 	 * be logged as part of the panic flow.
20731772Sjl139090 	 */
20741772Sjl139090 	if (aflt->flt_panic) {
20751772Sjl139090 		if (pr_reason[0] == 0)
20761772Sjl139090 			strcpy(pr_reason, "invalid SFSR ");
20771772Sjl139090 
20781772Sjl139090 		fm_panic("%sErrors(s)", pr_reason);
20791772Sjl139090 	}
20801772Sjl139090 
20811772Sjl139090 	/*
20821772Sjl139090 	 * If we queued an error and we are going to return from the trap and
20831772Sjl139090 	 * the error was in user mode or inside of a copy routine, set AST flag
20841772Sjl139090 	 * so the queue will be drained before returning to user mode.  The
20851772Sjl139090 	 * AST processing will also act on our failure policy.
20861772Sjl139090 	 */
20871772Sjl139090 	if (!aflt->flt_priv || aflt->flt_prot == AFLT_PROT_COPY) {
20881772Sjl139090 		int pcb_flag = 0;
20891772Sjl139090 
20905037Sjl139090 		if (t_sfsr & (SFSR_ERRS & ~(SFSR_BERR | SFSR_TO)))
20911772Sjl139090 			pcb_flag |= ASYNC_HWERR;
20921772Sjl139090 
20931772Sjl139090 		if (t_sfsr & SFSR_BERR)
20941772Sjl139090 			pcb_flag |= ASYNC_BERR;
20951772Sjl139090 
20961772Sjl139090 		if (t_sfsr & SFSR_TO)
20971772Sjl139090 			pcb_flag |= ASYNC_BTO;
20981772Sjl139090 
20991772Sjl139090 		ttolwp(curthread)->lwp_pcb.pcb_flags |= pcb_flag;
21001772Sjl139090 		aston(curthread);
21011772Sjl139090 	}
21021772Sjl139090 }
21031772Sjl139090 
21041772Sjl139090 /*ARGSUSED*/
21051772Sjl139090 void
21061772Sjl139090 opl_cpu_urgent_error(struct regs *rp, ulong_t p_ugesr, ulong_t tl)
21071772Sjl139090 {
21081772Sjl139090 	opl_async_flt_t opl_flt;
21091772Sjl139090 	struct async_flt *aflt;
21101772Sjl139090 	char pr_reason[MAX_REASON_STRING];
21111772Sjl139090 
21121772Sjl139090 	/* normalize tl */
21131772Sjl139090 	tl = (tl >= 2 ? 1 : 0);
21141772Sjl139090 	pr_reason[0] = '\0';
21151772Sjl139090 
21161772Sjl139090 	bzero(&opl_flt, sizeof (opl_async_flt_t));
21171772Sjl139090 	aflt = (struct async_flt *)&opl_flt;
21181772Sjl139090 	aflt->flt_id = gethrtime_waitfree();
21191772Sjl139090 	aflt->flt_bus_id = getprocessorid();
21201772Sjl139090 	aflt->flt_inst = CPU->cpu_id;
21211772Sjl139090 	aflt->flt_stat = p_ugesr;
21221772Sjl139090 	aflt->flt_pc = (caddr_t)rp->r_pc;
21231772Sjl139090 	aflt->flt_class = (uchar_t)CPU_FAULT;
21241772Sjl139090 	aflt->flt_tl = tl;
21255037Sjl139090 	aflt->flt_priv = (uchar_t)(tl == 1 ? 1 : ((rp->r_tstate & TSTATE_PRIV) ?
21265037Sjl139090 	    1 : 0));
21271772Sjl139090 	aflt->flt_status = OPL_ECC_URGENT_TRAP;
21281772Sjl139090 	aflt->flt_panic = 1;
21291772Sjl139090 	/*
21301772Sjl139090 	 * HW does not set mod/sid in case of urgent error.
21311772Sjl139090 	 * So we have to set it here.
21321772Sjl139090 	 */
21331772Sjl139090 	opl_flt.flt_eid_mod = OPL_ERRID_CPU;
21341772Sjl139090 	opl_flt.flt_eid_sid = aflt->flt_inst;
21351772Sjl139090 
21361772Sjl139090 	if (cpu_queue_events(&opl_flt, pr_reason, p_ugesr) == 0) {
21371772Sjl139090 		opl_flt.flt_type = OPL_CPU_INV_UGESR;
21381772Sjl139090 		aflt->flt_payload = FM_EREPORT_PAYLOAD_URGENT;
21395037Sjl139090 		cpu_errorq_dispatch(FM_EREPORT_CPU_INV_URG, (void *)&opl_flt,
21405037Sjl139090 		    sizeof (opl_async_flt_t), ue_queue, aflt->flt_panic);
21411772Sjl139090 	}
21421772Sjl139090 
21431772Sjl139090 	fm_panic("Urgent Error");
21441772Sjl139090 }
21451772Sjl139090 
21461772Sjl139090 /*
21471772Sjl139090  * Initialization error counters resetting.
21481772Sjl139090  */
21491772Sjl139090 /* ARGSUSED */
21501772Sjl139090 static void
21511772Sjl139090 opl_ras_online(void *arg, cpu_t *cp, cyc_handler_t *hdlr, cyc_time_t *when)
21521772Sjl139090 {
21531772Sjl139090 	hdlr->cyh_func = (cyc_func_t)ras_cntr_reset;
21541772Sjl139090 	hdlr->cyh_level = CY_LOW_LEVEL;
21551772Sjl139090 	hdlr->cyh_arg = (void *)(uintptr_t)cp->cpu_id;
21561772Sjl139090 
21571772Sjl139090 	when->cyt_when = cp->cpu_id * (((hrtime_t)NANOSEC * 10)/ NCPU);
21581772Sjl139090 	when->cyt_interval = (hrtime_t)NANOSEC * opl_async_check_interval;
21591772Sjl139090 }
21601772Sjl139090 
21611772Sjl139090 void
21621772Sjl139090 cpu_mp_init(void)
21631772Sjl139090 {
21641772Sjl139090 	cyc_omni_handler_t hdlr;
21651772Sjl139090 
21661772Sjl139090 	hdlr.cyo_online = opl_ras_online;
21671772Sjl139090 	hdlr.cyo_offline = NULL;
21681772Sjl139090 	hdlr.cyo_arg = NULL;
21691772Sjl139090 	mutex_enter(&cpu_lock);
21701772Sjl139090 	(void) cyclic_add_omni(&hdlr);
21711772Sjl139090 	mutex_exit(&cpu_lock);
21721772Sjl139090 }
21731772Sjl139090 
21747029Sjimand int heaplp_use_stlb = 0;
21753309Sjimand 
21761772Sjl139090 void
21771772Sjl139090 mmu_init_kernel_pgsz(struct hat *hat)
21781772Sjl139090 {
21793309Sjimand 	uint_t tte = page_szc(segkmem_lpsize);
21803309Sjimand 	uchar_t new_cext_primary, new_cext_nucleus;
21813309Sjimand 
21823309Sjimand 	if (heaplp_use_stlb == 0) {
21833309Sjimand 		/* do not reprogram stlb */
21843309Sjimand 		tte = TTE8K;
21855584Sjimand 	} else if (!plat_prom_preserve_kctx_is_supported()) {
21865584Sjimand 		/* OBP does not support non-zero primary context */
21875584Sjimand 		tte = TTE8K;
21885584Sjimand 		heaplp_use_stlb = 0;
21893309Sjimand 	}
21903309Sjimand 
21913309Sjimand 	new_cext_nucleus = TAGACCEXT_MKSZPAIR(tte, TTE8K);
21923309Sjimand 	new_cext_primary = TAGACCEXT_MKSZPAIR(TTE8K, tte);
21933309Sjimand 
21943309Sjimand 	hat->sfmmu_cext = new_cext_primary;
21953309Sjimand 	kcontextreg = ((uint64_t)new_cext_nucleus << CTXREG_NEXT_SHIFT) |
21965037Sjl139090 	    ((uint64_t)new_cext_primary << CTXREG_EXT_SHIFT);
21971772Sjl139090 }
21981772Sjl139090 
21991772Sjl139090 size_t
22001772Sjl139090 mmu_get_kernel_lpsize(size_t lpsize)
22011772Sjl139090 {
22021772Sjl139090 	uint_t tte;
22031772Sjl139090 
22041772Sjl139090 	if (lpsize == 0) {
22051772Sjl139090 		/* no setting for segkmem_lpsize in /etc/system: use default */
22061772Sjl139090 		return (MMU_PAGESIZE4M);
22071772Sjl139090 	}
22081772Sjl139090 
22091772Sjl139090 	for (tte = TTE8K; tte <= TTE4M; tte++) {
22101772Sjl139090 		if (lpsize == TTEBYTES(tte))
22111772Sjl139090 			return (lpsize);
22121772Sjl139090 	}
22131772Sjl139090 
22141772Sjl139090 	return (TTEBYTES(TTE8K));
22151772Sjl139090 }
22161772Sjl139090 
22171772Sjl139090 /*
22185414Smb158278  * Support for ta 3.
22195414Smb158278  * We allocate here a buffer for each cpu
22205414Smb158278  * for saving the current register window.
22215414Smb158278  */
22225414Smb158278 typedef struct win_regs {
22235414Smb158278 	uint64_t l[8];
22245414Smb158278 	uint64_t i[8];
22255414Smb158278 } win_regs_t;
22265414Smb158278 static void
22275414Smb158278 opl_ta3(void)
22285414Smb158278 {
22296259Smb158278 	/*
22306259Smb158278 	 * opl_ta3 should only be called once at boot time.
22316259Smb158278 	 */
22326259Smb158278 	if (opl_ta3_save == NULL)
22336259Smb158278 		opl_ta3_save = (char *)kmem_alloc(NCPU * sizeof (win_regs_t),
22346259Smb158278 		    KM_SLEEP);
22355414Smb158278 }
22365414Smb158278 
22375414Smb158278 /*
22381772Sjl139090  * The following are functions that are unused in
22391772Sjl139090  * OPL cpu module. They are defined here to resolve
22401772Sjl139090  * dependencies in the "unix" module.
22411772Sjl139090  * Unused functions that should never be called in
22421772Sjl139090  * OPL are coded with ASSERT(0).
22431772Sjl139090  */
22441772Sjl139090 
22451772Sjl139090 void
22461772Sjl139090 cpu_disable_errors(void)
22471772Sjl139090 {}
22481772Sjl139090 
22491772Sjl139090 void
22501772Sjl139090 cpu_enable_errors(void)
22511772Sjl139090 { ASSERT(0); }
22521772Sjl139090 
22531772Sjl139090 /*ARGSUSED*/
22541772Sjl139090 void
22551772Sjl139090 cpu_ce_scrub_mem_err(struct async_flt *ecc, boolean_t t)
22561772Sjl139090 { ASSERT(0); }
22571772Sjl139090 
22581772Sjl139090 /*ARGSUSED*/
22591772Sjl139090 void
22601772Sjl139090 cpu_faulted_enter(struct cpu *cp)
22611772Sjl139090 {}
22621772Sjl139090 
22631772Sjl139090 /*ARGSUSED*/
22641772Sjl139090 void
22651772Sjl139090 cpu_faulted_exit(struct cpu *cp)
22661772Sjl139090 {}
22671772Sjl139090 
22681772Sjl139090 /*ARGSUSED*/
22691772Sjl139090 void
22701772Sjl139090 cpu_check_allcpus(struct async_flt *aflt)
22711772Sjl139090 {}
22721772Sjl139090 
22731772Sjl139090 /*ARGSUSED*/
22741772Sjl139090 void
22751772Sjl139090 cpu_ce_log_err(struct async_flt *aflt, errorq_elem_t *t)
22761772Sjl139090 { ASSERT(0); }
22771772Sjl139090 
22781772Sjl139090 /*ARGSUSED*/
22791772Sjl139090 void
22801772Sjl139090 cpu_check_ce(int flag, uint64_t pa, caddr_t va, uint_t psz)
22811772Sjl139090 { ASSERT(0); }
22821772Sjl139090 
22831772Sjl139090 /*ARGSUSED*/
22841772Sjl139090 void
22851772Sjl139090 cpu_ce_count_unum(struct async_flt *ecc, int len, char *unum)
22861772Sjl139090 { ASSERT(0); }
22871772Sjl139090 
22881772Sjl139090 /*ARGSUSED*/
22891772Sjl139090 void
22901772Sjl139090 cpu_busy_ecache_scrub(struct cpu *cp)
22911772Sjl139090 {}
22921772Sjl139090 
22931772Sjl139090 /*ARGSUSED*/
22941772Sjl139090 void
22951772Sjl139090 cpu_idle_ecache_scrub(struct cpu *cp)
22961772Sjl139090 {}
22971772Sjl139090 
22981772Sjl139090 /* ARGSUSED */
22991772Sjl139090 void
23001772Sjl139090 cpu_change_speed(uint64_t divisor, uint64_t arg2)
23011772Sjl139090 { ASSERT(0); }
23021772Sjl139090 
23031772Sjl139090 void
23041772Sjl139090 cpu_init_cache_scrub(void)
23051772Sjl139090 {}
23061772Sjl139090 
23071772Sjl139090 /* ARGSUSED */
23081772Sjl139090 int
23091772Sjl139090 cpu_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
23101772Sjl139090 {
23112214Sav145390 	if (&plat_get_mem_sid) {
23122214Sav145390 		return (plat_get_mem_sid(unum, buf, buflen, lenp));
23132214Sav145390 	} else {
23142214Sav145390 		return (ENOTSUP);
23152214Sav145390 	}
23161772Sjl139090 }
23171772Sjl139090 
23181772Sjl139090 /* ARGSUSED */
23191772Sjl139090 int
23201772Sjl139090 cpu_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp)
23211772Sjl139090 {
23222214Sav145390 	if (&plat_get_mem_addr) {
23232214Sav145390 		return (plat_get_mem_addr(unum, sid, offset, addrp));
23242214Sav145390 	} else {
23252214Sav145390 		return (ENOTSUP);
23262214Sav145390 	}
23271772Sjl139090 }
23281772Sjl139090 
23291772Sjl139090 /* ARGSUSED */
23301772Sjl139090 int
23311772Sjl139090 cpu_get_mem_offset(uint64_t flt_addr, uint64_t *offp)
23321772Sjl139090 {
23332214Sav145390 	if (&plat_get_mem_offset) {
23342214Sav145390 		return (plat_get_mem_offset(flt_addr, offp));
23352214Sav145390 	} else {
23362214Sav145390 		return (ENOTSUP);
23372214Sav145390 	}
23381772Sjl139090 }
23391772Sjl139090 
23401772Sjl139090 /*ARGSUSED*/
23411772Sjl139090 void
23421772Sjl139090 itlb_rd_entry(uint_t entry, tte_t *tte, uint64_t *va_tag)
23431772Sjl139090 { ASSERT(0); }
23441772Sjl139090 
23451772Sjl139090 /*ARGSUSED*/
23461772Sjl139090 void
23471772Sjl139090 dtlb_rd_entry(uint_t entry, tte_t *tte, uint64_t *va_tag)
23481772Sjl139090 { ASSERT(0); }
23492203Shyw 
23502203Shyw /*ARGSUSED*/
23512203Shyw void
23522203Shyw read_ecc_data(struct async_flt *aflt, short verbose, short ce_err)
23532203Shyw { ASSERT(0); }
23542203Shyw 
23552203Shyw /*ARGSUSED*/
23562203Shyw int
23572203Shyw ce_scrub_xdiag_recirc(struct async_flt *aflt, errorq_t *eqp,
23582203Shyw     errorq_elem_t *eqep, size_t afltoffset)
23592203Shyw {
23602203Shyw 	ASSERT(0);
23612203Shyw 	return (0);
23622203Shyw }
23632203Shyw 
23642203Shyw /*ARGSUSED*/
23652203Shyw char *
23662203Shyw flt_to_error_type(struct async_flt *aflt)
23672203Shyw {
23682203Shyw 	ASSERT(0);
23692203Shyw 	return (NULL);
23702203Shyw }
23715394Sjimand 
23725394Sjimand #define	PROM_SPARC64VII_MODE_PROPNAME	"SPARC64-VII-mode"
23735394Sjimand 
23745394Sjimand /*
23755394Sjimand  * Check for existence of OPL OBP property that indicates
23765394Sjimand  * SPARC64-VII support. By default, only enable Jupiter
23775394Sjimand  * features if the property is present.   It will be
23785394Sjimand  * present in all-Jupiter domains by OBP if the domain has
23795394Sjimand  * been selected by the user on the system controller to
23805394Sjimand  * run in Jupiter mode.  Basically, this OBP property must
23815394Sjimand  * be present to turn on the cpu_alljupiter flag.
23825394Sjimand  */
23835394Sjimand static int
23845394Sjimand prom_SPARC64VII_support_enabled(void)
23855394Sjimand {
23865394Sjimand 	int val;
23875394Sjimand 
23885394Sjimand 	return ((prom_getprop(prom_rootnode(), PROM_SPARC64VII_MODE_PROPNAME,
23895394Sjimand 	    (caddr_t)&val) == 0) ? 1 : 0);
23905394Sjimand }
23915584Sjimand 
23925584Sjimand #define	PROM_KCTX_PRESERVED_PROPNAME	"context0-page-size-preserved"
23935584Sjimand 
23945584Sjimand /*
23955584Sjimand  * Check for existence of OPL OBP property that indicates support for
23965584Sjimand  * preserving Solaris kernel page sizes when entering OBP.  We need to
23975584Sjimand  * check the prom tree since the ddi tree is not yet built when the
23985584Sjimand  * platform startup sequence is called.
23995584Sjimand  */
24005584Sjimand static int
24015584Sjimand plat_prom_preserve_kctx_is_supported(void)
24025584Sjimand {
24035584Sjimand 	pnode_t		pnode;
24045584Sjimand 	int		val;
24055584Sjimand 
24065584Sjimand 	/*
24075584Sjimand 	 * Check for existence of context0-page-size-preserved property
24085584Sjimand 	 * in virtual-memory prom node.
24095584Sjimand 	 */
24105584Sjimand 	pnode = (pnode_t)prom_getphandle(prom_mmu_ihandle());
24115584Sjimand 	return ((prom_getprop(pnode, PROM_KCTX_PRESERVED_PROPNAME,
24125584Sjimand 	    (caddr_t)&val) == 0) ? 1 : 0);
24135584Sjimand }
2414