xref: /onnv-gate/usr/src/uts/sun4v/cpu/niagara2.c (revision 13102:91026ef504cf)
13156Sgirish /*
23156Sgirish  * CDDL HEADER START
33156Sgirish  *
43156Sgirish  * The contents of this file are subject to the terms of the
53156Sgirish  * Common Development and Distribution License (the "License").
63156Sgirish  * You may not use this file except in compliance with the License.
73156Sgirish  *
83156Sgirish  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93156Sgirish  * or http://www.opensolaris.org/os/licensing.
103156Sgirish  * See the License for the specific language governing permissions
113156Sgirish  * and limitations under the License.
123156Sgirish  *
133156Sgirish  * When distributing Covered Code, include this CDDL HEADER in each
143156Sgirish  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153156Sgirish  * If applicable, add the following below this CDDL HEADER, with the
163156Sgirish  * fields enclosed by brackets "[]" replaced with your own identifying
173156Sgirish  * information: Portions Copyright [yyyy] [name of copyright owner]
183156Sgirish  *
193156Sgirish  * CDDL HEADER END
203156Sgirish  */
214203Srsmaeda 
223156Sgirish /*
23*13102Svivek.gavaskar@oracle.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
243156Sgirish  */
253156Sgirish 
263156Sgirish #include <sys/types.h>
273156Sgirish #include <sys/systm.h>
283156Sgirish #include <sys/archsystm.h>
293156Sgirish #include <sys/machparam.h>
303156Sgirish #include <sys/machsystm.h>
313156Sgirish #include <sys/cpu.h>
323156Sgirish #include <sys/elf_SPARC.h>
333156Sgirish #include <vm/hat_sfmmu.h>
343156Sgirish #include <vm/page.h>
353177Sdp78419 #include <vm/vm_dep.h>
363156Sgirish #include <sys/cpuvar.h>
373156Sgirish #include <sys/async.h>
383156Sgirish #include <sys/cmn_err.h>
393156Sgirish #include <sys/debug.h>
403156Sgirish #include <sys/dditypes.h>
413156Sgirish #include <sys/sunddi.h>
423156Sgirish #include <sys/cpu_module.h>
433156Sgirish #include <sys/prom_debug.h>
443156Sgirish #include <sys/vmsystm.h>
453156Sgirish #include <sys/prom_plat.h>
463156Sgirish #include <sys/sysmacros.h>
473156Sgirish #include <sys/intreg.h>
483156Sgirish #include <sys/machtrap.h>
493156Sgirish #include <sys/ontrap.h>
503156Sgirish #include <sys/ivintr.h>
513156Sgirish #include <sys/atomic.h>
523156Sgirish #include <sys/panic.h>
533156Sgirish #include <sys/dtrace.h>
543156Sgirish #include <sys/simulate.h>
553156Sgirish #include <sys/fault.h>
563156Sgirish #include <sys/niagara2regs.h>
573156Sgirish #include <sys/hsvc.h>
583156Sgirish #include <sys/trapstat.h>
595834Spt157919 #include <sys/mutex_impl.h>
603156Sgirish 
613156Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
624732Sdavemq #if defined(NIAGARA2_IMPL)
633156Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
644732Sdavemq #elif defined(VFALLS_IMPL)
654732Sdavemq char cpu_module_name[] = "SUNW,UltraSPARC-T2+";
6611304SJanie.Lu@Sun.COM #elif defined(KT_IMPL)
67*13102Svivek.gavaskar@oracle.COM char cpu_module_name[] = "SPARC-T3";
684732Sdavemq #endif
693156Sgirish 
703156Sgirish /*
714732Sdavemq  * Hypervisor services information for the NIAGARA2 and Victoria Falls
724732Sdavemq  * CPU module
733156Sgirish  */
744732Sdavemq static boolean_t cpu_hsvc_available = B_TRUE;
754732Sdavemq static uint64_t cpu_sup_minor;		/* Supported minor number */
764732Sdavemq #if defined(NIAGARA2_IMPL)
774732Sdavemq static hsvc_info_t cpu_hsvc = {
783156Sgirish 	HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA2_CPU, NIAGARA2_HSVC_MAJOR,
793156Sgirish 	NIAGARA2_HSVC_MINOR, cpu_module_name
803156Sgirish };
814732Sdavemq #elif defined(VFALLS_IMPL)
824732Sdavemq static hsvc_info_t cpu_hsvc = {
834732Sdavemq 	HSVC_REV_1, NULL, HSVC_GROUP_VFALLS_CPU, VFALLS_HSVC_MAJOR,
844732Sdavemq 	VFALLS_HSVC_MINOR, cpu_module_name
854732Sdavemq };
8611304SJanie.Lu@Sun.COM #elif defined(KT_IMPL)
8711304SJanie.Lu@Sun.COM static hsvc_info_t cpu_hsvc = {
8811304SJanie.Lu@Sun.COM 	HSVC_REV_1, NULL, HSVC_GROUP_KT_CPU, KT_HSVC_MAJOR,
8911304SJanie.Lu@Sun.COM 	KT_HSVC_MINOR, cpu_module_name
9011304SJanie.Lu@Sun.COM };
914732Sdavemq #endif
923156Sgirish 
933156Sgirish void
cpu_setup(void)943156Sgirish cpu_setup(void)
953156Sgirish {
963156Sgirish 	extern int mmu_exported_pagesize_mask;
973156Sgirish 	extern int cpc_has_overflow_intr;
985631Swh94709 	extern size_t contig_mem_prealloc_base_size;
993156Sgirish 	int status;
1003156Sgirish 
1013156Sgirish 	/*
1023156Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
1033156Sgirish 	 * services.
1043156Sgirish 	 */
1054732Sdavemq 	status = hsvc_register(&cpu_hsvc, &cpu_sup_minor);
1063156Sgirish 	if (status != 0) {
1073156Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
1083156Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
1094732Sdavemq 		    cpu_hsvc.hsvc_modname, cpu_hsvc.hsvc_group,
1104732Sdavemq 		    cpu_hsvc.hsvc_major, cpu_hsvc.hsvc_minor, status);
1114732Sdavemq 		cpu_hsvc_available = B_FALSE;
1123156Sgirish 	}
1133156Sgirish 
1143156Sgirish 	/*
1153156Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
1163156Sgirish 	 * routine.
1173156Sgirish 	 */
1183156Sgirish 	cpu_setup_common(NULL);
1193156Sgirish 
12011304SJanie.Lu@Sun.COM 	/*
12111304SJanie.Lu@Sun.COM 	 * Initialize the cpu_hwcap_flags for N2 and VF if it is not already
12211304SJanie.Lu@Sun.COM 	 * set in cpu_setup_common() by the hwcap MD info. Note that this MD
12311304SJanie.Lu@Sun.COM 	 * info may not be available for N2/VF.
12411304SJanie.Lu@Sun.COM 	 */
12511304SJanie.Lu@Sun.COM 	if (cpu_hwcap_flags == 0) {
12611304SJanie.Lu@Sun.COM #ifdef KT_IMPL
12711304SJanie.Lu@Sun.COM 		/*
12811304SJanie.Lu@Sun.COM 		 * This should not happen since hwcap MD info is always
12911304SJanie.Lu@Sun.COM 		 * available for KT platforms.
13011304SJanie.Lu@Sun.COM 		 */
13111304SJanie.Lu@Sun.COM 		ASSERT(cpu_hwcap_flags != 0);	/* panic in DEBUG mode */
13211304SJanie.Lu@Sun.COM 		cpu_hwcap_flags |= AV_SPARC_VIS3 | AV_SPARC_HPC | AV_SPARC_FMAF;
13311304SJanie.Lu@Sun.COM #endif /* KT_IMPL */
13411304SJanie.Lu@Sun.COM 		cpu_hwcap_flags |= AV_SPARC_VIS | AV_SPARC_VIS2 |
13511304SJanie.Lu@Sun.COM 		    AV_SPARC_ASI_BLK_INIT | AV_SPARC_POPC;
13611304SJanie.Lu@Sun.COM 	}
13711304SJanie.Lu@Sun.COM 
1383156Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
1393156Sgirish 
1403156Sgirish 	if ((mmu_exported_pagesize_mask &
1413156Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
1423156Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
1433156Sgirish 		cmn_err(CE_PANIC, "machine description"
1443156Sgirish 		    " does not have required sun4v page sizes"
1453156Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
1463156Sgirish 		    mmu_exported_pagesize_mask);
1473156Sgirish 
1483156Sgirish 	/*
1493156Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
1503156Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
1513156Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
1523156Sgirish 	 * and must never be mapped. In addition, software must not use
1533156Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
1543156Sgirish 	 * avoid problems with prefetching into the VA hole.
1553156Sgirish 	 */
1563156Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
1573156Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
1583156Sgirish 
1593156Sgirish 	/*
1603156Sgirish 	 * Niagara2 has a performance counter overflow interrupt
1613156Sgirish 	 */
1623156Sgirish 	cpc_has_overflow_intr = 1;
1633177Sdp78419 
1643177Sdp78419 	/*
1653177Sdp78419 	 * Enable 4M pages for OOB.
1663177Sdp78419 	 */
1673177Sdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
1683177Sdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
1693177Sdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
1704204Sha137994 
1715631Swh94709 #ifdef SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB
1725631Swh94709 	/*
1735631Swh94709 	 * Use CPU Makefile specific compile time define (if exists)
1745631Swh94709 	 * to add to the contig preallocation size.
1755631Swh94709 	 */
1765631Swh94709 	contig_mem_prealloc_base_size = MB(SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB);
1775631Swh94709 #endif
1783156Sgirish }
1793156Sgirish 
1803156Sgirish /*
1813156Sgirish  * Set the magic constants of the implementation.
1823156Sgirish  */
1833156Sgirish void
cpu_fiximp(struct cpu_node * cpunode)1843156Sgirish cpu_fiximp(struct cpu_node *cpunode)
1853156Sgirish {
1863156Sgirish 	/*
1873156Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
1883156Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
1893156Sgirish 	 * size, linesize.
1903156Sgirish 	 */
1913156Sgirish 	if (cpunode->ecache_size == 0)
1923156Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
1933156Sgirish 	if (cpunode->ecache_linesize == 0)
1943156Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
1953156Sgirish 	if (cpunode->ecache_associativity == 0)
1963156Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
1973156Sgirish }
1983156Sgirish 
1993156Sgirish void
cpu_map_exec_units(struct cpu * cp)2004203Srsmaeda cpu_map_exec_units(struct cpu *cp)
2013156Sgirish {
2024203Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
2033156Sgirish 
2043156Sgirish 	/*
2053434Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
2064203Srsmaeda 	 * the execution unit sharing information from the MD. They
2074203Srsmaeda 	 * default to the CPU id in the absence of such information.
2083156Sgirish 	 */
2093156Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
2103156Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
2113156Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
2123156Sgirish 
2133434Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
2143434Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
2153434Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
2163434Sesaxe 
2173434Sesaxe 	/*
2183434Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
2193434Sesaxe 	 */
2203434Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
2214732Sdavemq 
2224732Sdavemq 	/*
2234732Sdavemq 	 * The cpu_chip field is initialized based on the information
2244732Sdavemq 	 * in the MD and assume that all cpus within a chip
2254732Sdavemq 	 * share the same L2 cache. If no such info is available, we
2264732Sdavemq 	 * set the cpu to belong to the defacto chip 0.
2274732Sdavemq 	 */
2284769Sdp78419 	cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
2294769Sdp78419 	if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
2304769Sdp78419 		cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
2315079Sjc25722 
2325079Sjc25722 	cp->cpu_m.cpu_chip = cpunodes[cp->cpu_id].l2_cache_mapping;
2335079Sjc25722 	if (cp->cpu_m.cpu_chip == NO_L2_CACHE_MAPPING_FOUND)
2345079Sjc25722 		cp->cpu_m.cpu_chip = CPU_CHIPID_INVALID;
2354203Srsmaeda }
2364203Srsmaeda 
2374732Sdavemq static int cpucnt;
2384203Srsmaeda 
2394203Srsmaeda void
cpu_init_private(struct cpu * cp)2404203Srsmaeda cpu_init_private(struct cpu *cp)
2414203Srsmaeda {
2424203Srsmaeda 	extern void niagara_kstat_init(void);
2433434Sesaxe 
2443156Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
2454203Srsmaeda 
2464203Srsmaeda 	cpu_map_exec_units(cp);
2474203Srsmaeda 
2484732Sdavemq 	if ((cpucnt++ == 0) && (cpu_hsvc_available == B_TRUE))
2494732Sdavemq 		(void) niagara_kstat_init();
2505834Spt157919 
2515834Spt157919 	mutex_delay = rdccr_delay;
2523156Sgirish }
2533156Sgirish 
2543156Sgirish /*ARGSUSED*/
2553156Sgirish void
cpu_uninit_private(struct cpu * cp)2563156Sgirish cpu_uninit_private(struct cpu *cp)
2573156Sgirish {
2584203Srsmaeda 	extern void niagara_kstat_fini(void);
2593156Sgirish 
2603156Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
2614732Sdavemq 	if ((--cpucnt == 0) && (cpu_hsvc_available == B_TRUE))
2624732Sdavemq 		(void) niagara_kstat_fini();
2633156Sgirish }
2643156Sgirish 
2653156Sgirish /*
2663156Sgirish  * On Niagara2, any flush will cause all preceding stores to be
2673156Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
2683156Sgirish  * the address is ignored, so we always flush address 0.
2693156Sgirish  */
2703156Sgirish /*ARGSUSED*/
2713156Sgirish void
dtrace_flush_sec(uintptr_t addr)2723156Sgirish dtrace_flush_sec(uintptr_t addr)
2733156Sgirish {
2743156Sgirish 	doflush(0);
2753156Sgirish }
2763156Sgirish 
2773156Sgirish /*
2783156Sgirish  * Trapstat support for Niagara2 processor
2793156Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
2803156Sgirish  * enabled no TSB hit information will be available. Therefore setting
2813156Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
2823156Sgirish  */
2833156Sgirish int
cpu_trapstat_conf(int cmd)2843156Sgirish cpu_trapstat_conf(int cmd)
2853156Sgirish {
2863156Sgirish 	int status = 0;
2873156Sgirish 
2883156Sgirish 	switch (cmd) {
2893156Sgirish 	case CPU_TSTATCONF_INIT:
2903156Sgirish 	case CPU_TSTATCONF_FINI:
2913156Sgirish 	case CPU_TSTATCONF_ENABLE:
2923156Sgirish 	case CPU_TSTATCONF_DISABLE:
2933156Sgirish 		break;
2943156Sgirish 	default:
2953156Sgirish 		status = EINVAL;
2963156Sgirish 		break;
2973156Sgirish 	}
2983156Sgirish 	return (status);
2993156Sgirish }
3003156Sgirish 
3013156Sgirish void
cpu_trapstat_data(void * buf,uint_t tstat_pgszs)3023156Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
3033156Sgirish {
3043156Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
3053156Sgirish 	int	i;
3063156Sgirish 
3073156Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
3083156Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
3093156Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
3103156Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
3113156Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
3123156Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
3133156Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
3143156Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
3153156Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
3163156Sgirish 	}
3173156Sgirish }
3183177Sdp78419 
3194769Sdp78419 /*
3204769Sdp78419  * Page coloring support for hashed cache index mode
3214769Sdp78419  */
3224769Sdp78419 
3234769Sdp78419 /*
3244769Sdp78419  * Node id bits from machine description (MD).  Node id distinguishes
3254769Sdp78419  * local versus remote memory. Because of MPO, page allocation does
3264769Sdp78419  * not cross node boundaries. Therefore, remove the node id bits from
3274769Sdp78419  * the color, since they are fixed. Either bit 30, or 31:30 in
3284769Sdp78419  * Victoria Falls processors.
3294769Sdp78419  * The number of node id bits is always 0 in Niagara2.
3304769Sdp78419  */
3314769Sdp78419 typedef struct n2color {
3324769Sdp78419 	uchar_t nnbits;	/* number of node id bits */
3334769Sdp78419 	uchar_t nnmask; /* mask for node id bits */
3344769Sdp78419 	uchar_t	lomask;	/* mask for bits below node id */
3354769Sdp78419 	uchar_t lobits;	/* number of bits below node id */
3364769Sdp78419 } n2color_t;
3374769Sdp78419 
3384769Sdp78419 n2color_t n2color[MMU_PAGE_SIZES];
3394769Sdp78419 static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
3404769Sdp78419 
3414769Sdp78419 /*
3424769Sdp78419  * Remove node id bits from color bits 32:28.
3434769Sdp78419  * This will reduce the number of colors.
3444769Sdp78419  * No change if number of node bits is zero.
3454769Sdp78419  */
3464769Sdp78419 static inline uint_t
n2_hash2color(uint_t color,uchar_t szc)3474769Sdp78419 n2_hash2color(uint_t color, uchar_t szc)
3484769Sdp78419 {
3494769Sdp78419 	n2color_t m = n2color[szc];
3504769Sdp78419 
3514769Sdp78419 	if (m.nnbits > 0) {
3524769Sdp78419 		color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
3534769Sdp78419 		ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
3544769Sdp78419 	}
3554769Sdp78419 
3564769Sdp78419 	return (color);
3574769Sdp78419 }
3584769Sdp78419 
3594769Sdp78419 /*
3604769Sdp78419  * Restore node id bits into page color.
3614769Sdp78419  * This will increase the number of colors to match N2.
3624769Sdp78419  * No change if number of node bits is zero.
3634769Sdp78419  */
3644769Sdp78419 static inline uint_t
n2_color2hash(uint_t color,uchar_t szc,uint_t node)3654769Sdp78419 n2_color2hash(uint_t color, uchar_t szc, uint_t node)
3664769Sdp78419 {
3674769Sdp78419 	n2color_t m = n2color[szc];
3684769Sdp78419 
3694769Sdp78419 	if (m.nnbits > 0) {
3704769Sdp78419 		color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
3714769Sdp78419 		color |= (node & m.nnmask) << m.lobits;
3724769Sdp78419 	}
3734769Sdp78419 
3744769Sdp78419 	return (color);
3754769Sdp78419 }
3764769Sdp78419 
3773177Sdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
3784769Sdp78419 
3794769Sdp78419 /*
3804769Sdp78419  * iterator NULL means pfn is VA, do not adjust ra_to_pa
3814769Sdp78419  * iterator (-1) means pfn is RA, need to convert to PA
3824769Sdp78419  * iterator non-null means pfn is RA, use ra_to_pa
3834769Sdp78419  */
3843177Sdp78419 uint_t
page_pfn_2_color_cpu(pfn_t pfn,uchar_t szc,void * cookie)3854769Sdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
3864769Sdp78419 {
3874769Sdp78419 	mem_node_iterator_t *it = cookie;
3884769Sdp78419 	uint_t color;
3894769Sdp78419 
3904769Sdp78419 	ASSERT(szc <= TTE256M);
3914769Sdp78419 
3924769Sdp78419 	if (it == ((mem_node_iterator_t *)(-1))) {
3934769Sdp78419 		pfn = plat_rapfn_to_papfn(pfn);
3944769Sdp78419 	} else if (it != NULL) {
3954769Sdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
3964769Sdp78419 		pfn = pfn + it->mi_ra_to_pa;
3974769Sdp78419 	}
3984769Sdp78419 	pfn = PFN_BASE(pfn, szc);
3994769Sdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
4004769Sdp78419 	if (szc < TTE4M) {
4014769Sdp78419 		/* 19:18 */
4024769Sdp78419 		color = (color << 2) | ((pfn >> 5) & 0x3);
4034769Sdp78419 		if (szc > TTE64K)
4044769Sdp78419 			color >>= 1;    /* 19 */
4054769Sdp78419 	}
4064769Sdp78419 	return (n2_hash2color(color, szc));
4074769Sdp78419 }
4084769Sdp78419 
4094769Sdp78419 static uint_t
page_papfn_2_color_cpu(pfn_t papfn,uchar_t szc)4104769Sdp78419 page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
4113177Sdp78419 {
4123177Sdp78419 	uint_t color;
4133177Sdp78419 
4143177Sdp78419 	ASSERT(szc <= TTE256M);
4153177Sdp78419 
4164769Sdp78419 	papfn = PFN_BASE(papfn, szc);
4174769Sdp78419 	color = ((papfn >> 15) ^ papfn) & 0x1f;
4184769Sdp78419 	if (szc < TTE4M) {
4194769Sdp78419 		/* 19:18 */
4204769Sdp78419 		color = (color << 2) | ((papfn >> 5) & 0x3);
4214769Sdp78419 		if (szc > TTE64K)
4224769Sdp78419 			color >>= 1;    /* 19 */
4234769Sdp78419 	}
4244769Sdp78419 	return (color);
4253177Sdp78419 }
4263177Sdp78419 
4273177Sdp78419 #if TTE256M != 5
4283177Sdp78419 #error TTE256M is not 5
4293177Sdp78419 #endif
4303177Sdp78419 
4313177Sdp78419 uint_t
page_get_nsz_color_mask_cpu(uchar_t szc,uint_t mask)4323177Sdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
4333177Sdp78419 {
4343177Sdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
4353177Sdp78419 	ASSERT(szc < TTE256M);
4364769Sdp78419 	mask = n2_color2hash(mask, szc, 0);
4373177Sdp78419 	mask &= ni2_color_masks[szc];
4384769Sdp78419 	if (szc == TTE64K || szc == TTE512K)
4394769Sdp78419 		mask >>= 1;
4404769Sdp78419 	return (n2_hash2color(mask, szc + 1));
4413177Sdp78419 }
4423177Sdp78419 
4433177Sdp78419 uint_t
page_get_nsz_color_cpu(uchar_t szc,uint_t color)4443177Sdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
4453177Sdp78419 {
4463177Sdp78419 	ASSERT(szc < TTE256M);
4474769Sdp78419 	color = n2_color2hash(color, szc, 0);
4484769Sdp78419 	if (szc == TTE64K || szc == TTE512K)
4494769Sdp78419 		color >>= 1;
4504769Sdp78419 	return (n2_hash2color(color, szc + 1));
4513177Sdp78419 }
4523177Sdp78419 
4533177Sdp78419 uint_t
page_get_color_shift_cpu(uchar_t szc,uchar_t nszc)4543177Sdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
4553177Sdp78419 {
4564769Sdp78419 	uint_t s;
4574318Ssusans 	ASSERT(nszc >= szc);
4583177Sdp78419 	ASSERT(nszc <= TTE256M);
4593177Sdp78419 
4604769Sdp78419 	s = nhbits[szc] - n2color[szc].nnbits;
4614769Sdp78419 	s -= nhbits[nszc] - n2color[nszc].nnbits;
4624769Sdp78419 
4634769Sdp78419 	return (s);
4644769Sdp78419 }
4654769Sdp78419 
4664769Sdp78419 uint_t
page_convert_color_cpu(uint_t ncolor,uchar_t szc,uchar_t nszc)4674769Sdp78419 page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
4684769Sdp78419 {
4694769Sdp78419 	uint_t color;
4703177Sdp78419 
4714769Sdp78419 	ASSERT(nszc > szc);
4724769Sdp78419 	ASSERT(nszc <= TTE256M);
4734769Sdp78419 	ncolor = n2_color2hash(ncolor, nszc, 0);
4744769Sdp78419 	color = ncolor << (nhbits[szc] - nhbits[nszc]);
4754769Sdp78419 	color = n2_hash2color(color, szc);
4764769Sdp78419 	return (color);
4773177Sdp78419 }
4783177Sdp78419 
4794769Sdp78419 #define	PAPFN_2_MNODE(pfn) \
4804769Sdp78419 	(((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
4814769Sdp78419 
4823177Sdp78419 /*ARGSUSED*/
4833177Sdp78419 pfn_t
page_next_pfn_for_color_cpu(pfn_t pfn,uchar_t szc,uint_t color,uint_t ceq_mask,uint_t color_mask,void * cookie)4843177Sdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
4854769Sdp78419     uint_t ceq_mask, uint_t color_mask, void *cookie)
4863177Sdp78419 {
4874769Sdp78419 	mem_node_iterator_t *it = cookie;
4883177Sdp78419 	pfn_t pstep = PNUM_SIZE(szc);
4893177Sdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
4903177Sdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
4914769Sdp78419 	uint_t pfnmn;
4923177Sdp78419 
4933177Sdp78419 	ASSERT((color & ~ceq_mask) == 0);
4944769Sdp78419 	ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
4953177Sdp78419 
4964769Sdp78419 	/* convert RA to PA for accurate color calculation */
4974769Sdp78419 	if (it->mi_init) {
4984769Sdp78419 		/* first call after it, so cache these values */
4994769Sdp78419 		it->mi_hash_ceq_mask =
5004769Sdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
5014769Sdp78419 		it->mi_hash_color =
5024769Sdp78419 		    n2_color2hash(color, szc, it->mi_mnode);
5034769Sdp78419 		it->mi_init = 0;
5044769Sdp78419 	} else {
5054769Sdp78419 		ASSERT(it->mi_hash_ceq_mask ==
5064769Sdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
5074769Sdp78419 		ASSERT(it->mi_hash_color ==
5084769Sdp78419 		    n2_color2hash(color, szc, it->mi_mnode));
5094769Sdp78419 	}
5104769Sdp78419 	ceq_mask = it->mi_hash_ceq_mask;
5114769Sdp78419 	color = it->mi_hash_color;
5124769Sdp78419 	pfn += it->mi_ra_to_pa;
5134769Sdp78419 
5144769Sdp78419 	/* restart here when we switch memblocks */
5154769Sdp78419 next_mem_block:
51611304SJanie.Lu@Sun.COM 	pfnmn = PAPFN_2_MNODE(pfn);
51711304SJanie.Lu@Sun.COM 	if ((((page_papfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0) &&
51811304SJanie.Lu@Sun.COM 	    (pfnmn == it->mi_mnode)) {
5193177Sdp78419 
52011304SJanie.Lu@Sun.COM 		/* we start from the page with correct color and mnode */
5213177Sdp78419 		if (szc >= TTE512K) {
5223177Sdp78419 			if (szc >= TTE4M) {
5233177Sdp78419 				/* page color is PA[32:28] */
5243177Sdp78419 				pfn_ceq_mask = ceq_mask << 15;
5253177Sdp78419 			} else {
5263177Sdp78419 				/* page color is PA[32:28].PA[19:19] */
5273177Sdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
5283177Sdp78419 				    ((ceq_mask >> 1) << 15);
5293177Sdp78419 			}
53011304SJanie.Lu@Sun.COM 			/*
53111304SJanie.Lu@Sun.COM 			 * Preserve mnode bits in case they are not part of the
53211304SJanie.Lu@Sun.COM 			 * color mask (eg., 8GB interleave, mnode bits 34:33).
53311304SJanie.Lu@Sun.COM 			 */
53411304SJanie.Lu@Sun.COM 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5354769Sdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
5364769Sdp78419 			goto done;
5373177Sdp78419 		} else {
5383177Sdp78419 			/*
5393177Sdp78419 			 * We deal 64K or 8K page. Check if we could the
5403177Sdp78419 			 * satisfy the request without changing PA[32:28]
5413177Sdp78419 			 */
5423177Sdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
5434769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5443177Sdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
5453177Sdp78419 
5463177Sdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
5474769Sdp78419 				goto done;
5483177Sdp78419 
5493177Sdp78419 			/*
5503177Sdp78419 			 * for next pfn we have to change bits PA[32:28]
5513177Sdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
5523177Sdp78419 			 */
5533177Sdp78419 			npfn = (pfn >> 15) << 15;
5543177Sdp78419 			npfn |= (ceq_mask & color & 3) << 5;
5553177Sdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
5563177Sdp78419 			    (ceq_mask & 0x1c) << 13;
5574769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5583177Sdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
5593177Sdp78419 
5603177Sdp78419 			/*
5613177Sdp78419 			 * set bits PA[17:13] to match the color
5623177Sdp78419 			 */
5634769Sdp78419 			npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
5644769Sdp78419 			goto done;
5653177Sdp78419 		}
5663177Sdp78419 	}
5673177Sdp78419 
5683177Sdp78419 	/*
5693177Sdp78419 	 * we start from the page with incorrect color - rare case
5703177Sdp78419 	 */
5713177Sdp78419 	if (szc >= TTE512K) {
5723177Sdp78419 		if (szc >= TTE4M) {
5733177Sdp78419 			/* page color is in bits PA[32:28] */
5743177Sdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
5753177Sdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
5763177Sdp78419 		} else {
5773177Sdp78419 			/* try get the right color by changing bit PA[19:19] */
5783177Sdp78419 			npfn = pfn + pstep;
57911304SJanie.Lu@Sun.COM 			pfnmn = PAPFN_2_MNODE(npfn);
58011304SJanie.Lu@Sun.COM 			if ((((page_papfn_2_color_cpu(npfn, szc) ^ color) &
58111304SJanie.Lu@Sun.COM 			    ceq_mask) == 0) && (pfnmn == it->mi_mnode))
5824769Sdp78419 				goto done;
5833177Sdp78419 
5843177Sdp78419 			/* page color is PA[32:28].PA[19:19] */
5853177Sdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
5863177Sdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
5873177Sdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
5883177Sdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
5893177Sdp78419 		}
5903177Sdp78419 
59111304SJanie.Lu@Sun.COM 		/* Fix mnode if necessary */
59211304SJanie.Lu@Sun.COM 		if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode)
59311304SJanie.Lu@Sun.COM 			npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
59411304SJanie.Lu@Sun.COM 			    it->mi_mnode_pfn_shift;
59511304SJanie.Lu@Sun.COM 
59611304SJanie.Lu@Sun.COM 		/*
59711304SJanie.Lu@Sun.COM 		 * Preserve mnode bits in case they are not part of the color
59811304SJanie.Lu@Sun.COM 		 * mask eg 8GB interleave, mnode bits 34:33).
59911304SJanie.Lu@Sun.COM 		 */
60011304SJanie.Lu@Sun.COM 		pfn_ceq_mask |= it->mi_mnode_pfn_mask;
6013177Sdp78419 		while (npfn <= pfn) {
6023177Sdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
6033177Sdp78419 		}
6044769Sdp78419 		goto done;
6053177Sdp78419 	}
6063177Sdp78419 
6073177Sdp78419 	/*
6084769Sdp78419 	 *  We deal 64K or 8K page of incorrect color.
6093177Sdp78419 	 * Try correcting color without changing PA[32:28]
6103177Sdp78419 	 */
6113177Sdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
6123177Sdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
6134769Sdp78419 	if (pfnmn == it->mi_mnode) {
6144769Sdp78419 		npfn = (pfn & ~(pfn_t)0x7f);
6154769Sdp78419 		npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
6164769Sdp78419 		npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
6173177Sdp78419 
6184769Sdp78419 		if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
6194769Sdp78419 		    ceq_mask) == 0) {
6204769Sdp78419 			/* the color is fixed - find the next page */
6214769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
6224769Sdp78419 			while (npfn <= pfn) {
6234769Sdp78419 				npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
6244769Sdp78419 				    mask);
6254769Sdp78419 			}
6264769Sdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
6274769Sdp78419 				goto done;
6283177Sdp78419 		}
6293177Sdp78419 	}
6303177Sdp78419 
6313177Sdp78419 	/* to fix the color need to touch PA[32:28] */
6323177Sdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
6333177Sdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
6344769Sdp78419 
6354769Sdp78419 	/* fix mnode if input pfn is in the wrong mnode. */
6364769Sdp78419 	if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
6374769Sdp78419 		npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
6384769Sdp78419 		    it->mi_mnode_pfn_shift;
6394769Sdp78419 	}
6404769Sdp78419 
6413177Sdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
6424769Sdp78419 	tmpmask |= it->mi_mnode_pfn_mask;
6433177Sdp78419 
6443177Sdp78419 	while (npfn <= pfn) {
6453177Sdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
6463177Sdp78419 	}
6473177Sdp78419 
6483177Sdp78419 	/* set bits PA[19:13] to match the color */
6493177Sdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
6503177Sdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
6513177Sdp78419 
6524769Sdp78419 done:
6534769Sdp78419 	ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
6544769Sdp78419 	ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
6554769Sdp78419 
6564769Sdp78419 	/* PA to RA */
6574769Sdp78419 	npfn -= it->mi_ra_to_pa;
6584769Sdp78419 
6594769Sdp78419 	/* check for possible memblock switch */
6604769Sdp78419 	if (npfn > it->mi_mblock_end) {
6616041Sdp78419 		pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, szc, it,
6626041Sdp78419 		    0);
6634769Sdp78419 		if (pfn == (pfn_t)-1)
6644769Sdp78419 			return (pfn);
6654769Sdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
6664769Sdp78419 		pfn += it->mi_ra_to_pa;
6674769Sdp78419 		goto next_mem_block;
6684769Sdp78419 	}
6693177Sdp78419 
6703177Sdp78419 	return (npfn);
6713177Sdp78419 }
6723177Sdp78419 
6733177Sdp78419 /*
6743177Sdp78419  * init page coloring
6754769Sdp78419  * VF encodes node_id for an L-group in either bit 30 or 31:30,
6764769Sdp78419  * which effectively reduces the number of colors available per mnode.
6773177Sdp78419  */
6783177Sdp78419 void
page_coloring_init_cpu()6793177Sdp78419 page_coloring_init_cpu()
6803177Sdp78419 {
6813177Sdp78419 	int i;
6824769Sdp78419 	uchar_t id;
6834769Sdp78419 	uchar_t lo;
6844769Sdp78419 	uchar_t hi;
6854769Sdp78419 	n2color_t m;
6864769Sdp78419 	mem_node_iterator_t it;
6874769Sdp78419 	static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
6883177Sdp78419 
6895615Sdp78419 	for (i = 0; i < max_mem_nodes; i++) {
6905615Sdp78419 		memset(&it, 0, sizeof (it));
6916041Sdp78419 		if (plat_mem_node_iterator_init(0, i, 0, &it, 1) != (pfn_t)-1)
6925615Sdp78419 			break;
6935615Sdp78419 	}
6945615Sdp78419 	ASSERT(i < max_mem_nodes);
6954769Sdp78419 	for (i = 0; i < mmu_page_sizes; i++) {
6965352Ssvemuri 		(void) memset(&m, 0, sizeof (m));
6974769Sdp78419 		id = it.mi_mnode_pfn_mask >> 15;	/* node id mask */
6984769Sdp78419 		id &= idmask[i];
6994769Sdp78419 		lo = lowbit(id);
7004769Sdp78419 		if (lo > 0) {
7014769Sdp78419 			hi = highbit(id);
7024769Sdp78419 			m.nnbits = hi - lo + 1;
7034769Sdp78419 			m.nnmask = (1 << m.nnbits) - 1;
7044769Sdp78419 			lo += nhbits[i] - 5;
7054769Sdp78419 			m.lomask = (1 << (lo - 1)) - 1;
7064769Sdp78419 			m.lobits = lo - 1;
7074769Sdp78419 		}
7084769Sdp78419 		hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
7094769Sdp78419 		n2color[i] = m;
7103177Sdp78419 	}
7113177Sdp78419 }
7123733Sdp78419 
7133733Sdp78419 /*
7143733Sdp78419  * group colorequiv colors on N2 by low order bits of the color first
7153733Sdp78419  */
7163733Sdp78419 void
page_set_colorequiv_arr_cpu(void)7173733Sdp78419 page_set_colorequiv_arr_cpu(void)
7183733Sdp78419 {
7193733Sdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
7203733Sdp78419 
7214769Sdp78419 	nequiv_shades_log2[1] -= n2color[1].nnbits;
7223733Sdp78419 	if (colorequiv > 1) {
7233733Sdp78419 		int i;
7243733Sdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
7253733Sdp78419 
7263733Sdp78419 		if (sv_a > 15)
7273733Sdp78419 			sv_a = 15;
7283733Sdp78419 
7293733Sdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
7303733Sdp78419 			uint_t colors;
7313733Sdp78419 			uint_t a = sv_a;
7323733Sdp78419 
7333733Sdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
7343733Sdp78419 				continue;
7353733Sdp78419 			while ((colors >> a) == 0)
7363733Sdp78419 				a--;
7373733Sdp78419 			if (a > (colorequivszc[i] & 0xf) +
7383733Sdp78419 			    (colorequivszc[i] >> 4)) {
7393733Sdp78419 				if (a <= nequiv_shades_log2[i]) {
7404732Sdavemq 					colorequivszc[i] = (uchar_t)a;
7413733Sdp78419 				} else {
7423733Sdp78419 					colorequivszc[i] =
7433733Sdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
7443733Sdp78419 					    nequiv_shades_log2[i];
7453733Sdp78419 				}
7463733Sdp78419 			}
7473733Sdp78419 		}
7483733Sdp78419 	}
7493733Sdp78419 }
750