xref: /onnv-gate/usr/src/uts/sun4v/cpu/niagara2.c (revision 6041:e35bc57647fd)
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 /*
235834Spt157919  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
243156Sgirish  * Use is subject to license terms.
253156Sgirish  */
263156Sgirish 
273156Sgirish #pragma ident	"%Z%%M%	%I%	%E% SMI"
283156Sgirish 
293156Sgirish #include <sys/types.h>
303156Sgirish #include <sys/systm.h>
313156Sgirish #include <sys/archsystm.h>
323156Sgirish #include <sys/machparam.h>
333156Sgirish #include <sys/machsystm.h>
343156Sgirish #include <sys/cpu.h>
353156Sgirish #include <sys/elf_SPARC.h>
363156Sgirish #include <vm/hat_sfmmu.h>
373156Sgirish #include <vm/page.h>
383177Sdp78419 #include <vm/vm_dep.h>
393156Sgirish #include <sys/cpuvar.h>
403156Sgirish #include <sys/async.h>
413156Sgirish #include <sys/cmn_err.h>
423156Sgirish #include <sys/debug.h>
433156Sgirish #include <sys/dditypes.h>
443156Sgirish #include <sys/sunddi.h>
453156Sgirish #include <sys/cpu_module.h>
463156Sgirish #include <sys/prom_debug.h>
473156Sgirish #include <sys/vmsystm.h>
483156Sgirish #include <sys/prom_plat.h>
493156Sgirish #include <sys/sysmacros.h>
503156Sgirish #include <sys/intreg.h>
513156Sgirish #include <sys/machtrap.h>
523156Sgirish #include <sys/ontrap.h>
533156Sgirish #include <sys/ivintr.h>
543156Sgirish #include <sys/atomic.h>
553156Sgirish #include <sys/panic.h>
563156Sgirish #include <sys/dtrace.h>
573156Sgirish #include <sys/simulate.h>
583156Sgirish #include <sys/fault.h>
593156Sgirish #include <sys/niagara2regs.h>
603156Sgirish #include <sys/hsvc.h>
613156Sgirish #include <sys/trapstat.h>
625834Spt157919 #include <sys/mutex_impl.h>
633156Sgirish 
643156Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
654732Sdavemq #if defined(NIAGARA2_IMPL)
663156Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
674732Sdavemq #elif defined(VFALLS_IMPL)
684732Sdavemq char cpu_module_name[] = "SUNW,UltraSPARC-T2+";
694732Sdavemq #endif
703156Sgirish 
713156Sgirish /*
724732Sdavemq  * Hypervisor services information for the NIAGARA2 and Victoria Falls
734732Sdavemq  * CPU module
743156Sgirish  */
754732Sdavemq static boolean_t cpu_hsvc_available = B_TRUE;
764732Sdavemq static uint64_t cpu_sup_minor;		/* Supported minor number */
774732Sdavemq #if defined(NIAGARA2_IMPL)
784732Sdavemq static hsvc_info_t cpu_hsvc = {
793156Sgirish 	HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA2_CPU, NIAGARA2_HSVC_MAJOR,
803156Sgirish 	NIAGARA2_HSVC_MINOR, cpu_module_name
813156Sgirish };
824732Sdavemq #elif defined(VFALLS_IMPL)
834732Sdavemq static hsvc_info_t cpu_hsvc = {
844732Sdavemq 	HSVC_REV_1, NULL, HSVC_GROUP_VFALLS_CPU, VFALLS_HSVC_MAJOR,
854732Sdavemq 	VFALLS_HSVC_MINOR, cpu_module_name
864732Sdavemq };
874732Sdavemq #endif
883156Sgirish 
893156Sgirish void
903156Sgirish cpu_setup(void)
913156Sgirish {
923156Sgirish 	extern int mmu_exported_pagesize_mask;
933156Sgirish 	extern int cpc_has_overflow_intr;
945631Swh94709 	extern size_t contig_mem_prealloc_base_size;
953156Sgirish 	int status;
963156Sgirish 
973156Sgirish 	/*
983156Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
993156Sgirish 	 * services.
1003156Sgirish 	 */
1014732Sdavemq 	status = hsvc_register(&cpu_hsvc, &cpu_sup_minor);
1023156Sgirish 	if (status != 0) {
1033156Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
1043156Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
1054732Sdavemq 		    cpu_hsvc.hsvc_modname, cpu_hsvc.hsvc_group,
1064732Sdavemq 		    cpu_hsvc.hsvc_major, cpu_hsvc.hsvc_minor, status);
1074732Sdavemq 		cpu_hsvc_available = B_FALSE;
1083156Sgirish 	}
1093156Sgirish 
1103156Sgirish 	/*
1113156Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
1123156Sgirish 	 * routine.
1133156Sgirish 	 */
1143156Sgirish 	cpu_setup_common(NULL);
1153156Sgirish 
1163156Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
1173156Sgirish 
1183156Sgirish 	if ((mmu_exported_pagesize_mask &
1193156Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
1203156Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
1213156Sgirish 		cmn_err(CE_PANIC, "machine description"
1223156Sgirish 		    " does not have required sun4v page sizes"
1233156Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
1243156Sgirish 		    mmu_exported_pagesize_mask);
1253156Sgirish 
1263156Sgirish 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 | AV_SPARC_ASI_BLK_INIT;
1273156Sgirish 
1283156Sgirish 	/*
1293156Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
1303156Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
1313156Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
1323156Sgirish 	 * and must never be mapped. In addition, software must not use
1333156Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
1343156Sgirish 	 * avoid problems with prefetching into the VA hole.
1353156Sgirish 	 */
1363156Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
1373156Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
1383156Sgirish 
1393156Sgirish 	/*
1403156Sgirish 	 * Niagara2 has a performance counter overflow interrupt
1413156Sgirish 	 */
1423156Sgirish 	cpc_has_overflow_intr = 1;
1433177Sdp78419 
1443177Sdp78419 	/*
1453177Sdp78419 	 * Enable 4M pages for OOB.
1463177Sdp78419 	 */
1473177Sdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
1483177Sdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
1493177Sdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
1504204Sha137994 
1515631Swh94709 #ifdef SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB
1525631Swh94709 	/*
1535631Swh94709 	 * Use CPU Makefile specific compile time define (if exists)
1545631Swh94709 	 * to add to the contig preallocation size.
1555631Swh94709 	 */
1565631Swh94709 	contig_mem_prealloc_base_size = MB(SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB);
1575631Swh94709 #endif
1583156Sgirish }
1593156Sgirish 
1603156Sgirish /*
1613156Sgirish  * Set the magic constants of the implementation.
1623156Sgirish  */
1633156Sgirish void
1643156Sgirish cpu_fiximp(struct cpu_node *cpunode)
1653156Sgirish {
1663156Sgirish 	/*
1673156Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
1683156Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
1693156Sgirish 	 * size, linesize.
1703156Sgirish 	 */
1713156Sgirish 	if (cpunode->ecache_size == 0)
1723156Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
1733156Sgirish 	if (cpunode->ecache_linesize == 0)
1743156Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
1753156Sgirish 	if (cpunode->ecache_associativity == 0)
1763156Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
1773156Sgirish }
1783156Sgirish 
1793156Sgirish void
1804203Srsmaeda cpu_map_exec_units(struct cpu *cp)
1813156Sgirish {
1824203Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
1833156Sgirish 
1843156Sgirish 	/*
1853434Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
1864203Srsmaeda 	 * the execution unit sharing information from the MD. They
1874203Srsmaeda 	 * default to the CPU id in the absence of such information.
1883156Sgirish 	 */
1893156Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
1903156Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
1913156Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
1923156Sgirish 
1933434Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
1943434Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
1953434Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
1963434Sesaxe 
1973434Sesaxe 	/*
1983434Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
1993434Sesaxe 	 */
2003434Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
2014732Sdavemq 
2024732Sdavemq 	/*
2034732Sdavemq 	 * The cpu_chip field is initialized based on the information
2044732Sdavemq 	 * in the MD and assume that all cpus within a chip
2054732Sdavemq 	 * share the same L2 cache. If no such info is available, we
2064732Sdavemq 	 * set the cpu to belong to the defacto chip 0.
2074732Sdavemq 	 */
2084769Sdp78419 	cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
2094769Sdp78419 	if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
2104769Sdp78419 		cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
2115079Sjc25722 
2125079Sjc25722 	cp->cpu_m.cpu_chip = cpunodes[cp->cpu_id].l2_cache_mapping;
2135079Sjc25722 	if (cp->cpu_m.cpu_chip == NO_L2_CACHE_MAPPING_FOUND)
2145079Sjc25722 		cp->cpu_m.cpu_chip = CPU_CHIPID_INVALID;
2154203Srsmaeda }
2164203Srsmaeda 
2174732Sdavemq static int cpucnt;
2184203Srsmaeda 
2194203Srsmaeda void
2204203Srsmaeda cpu_init_private(struct cpu *cp)
2214203Srsmaeda {
2224203Srsmaeda 	extern void niagara_kstat_init(void);
2233434Sesaxe 
2243156Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
2254203Srsmaeda 
2264203Srsmaeda 	cpu_map_exec_units(cp);
2274203Srsmaeda 
2284732Sdavemq 	if ((cpucnt++ == 0) && (cpu_hsvc_available == B_TRUE))
2294732Sdavemq 		(void) niagara_kstat_init();
2305834Spt157919 
2315834Spt157919 	mutex_delay = rdccr_delay;
2323156Sgirish }
2333156Sgirish 
2343156Sgirish /*ARGSUSED*/
2353156Sgirish void
2363156Sgirish cpu_uninit_private(struct cpu *cp)
2373156Sgirish {
2384203Srsmaeda 	extern void niagara_kstat_fini(void);
2393156Sgirish 
2403156Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
2414732Sdavemq 	if ((--cpucnt == 0) && (cpu_hsvc_available == B_TRUE))
2424732Sdavemq 		(void) niagara_kstat_fini();
2433156Sgirish }
2443156Sgirish 
2453156Sgirish /*
2463156Sgirish  * On Niagara2, any flush will cause all preceding stores to be
2473156Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
2483156Sgirish  * the address is ignored, so we always flush address 0.
2493156Sgirish  */
2503156Sgirish /*ARGSUSED*/
2513156Sgirish void
2523156Sgirish dtrace_flush_sec(uintptr_t addr)
2533156Sgirish {
2543156Sgirish 	doflush(0);
2553156Sgirish }
2563156Sgirish 
2573156Sgirish /*
2583156Sgirish  * Trapstat support for Niagara2 processor
2593156Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
2603156Sgirish  * enabled no TSB hit information will be available. Therefore setting
2613156Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
2623156Sgirish  */
2633156Sgirish int
2643156Sgirish cpu_trapstat_conf(int cmd)
2653156Sgirish {
2663156Sgirish 	int status = 0;
2673156Sgirish 
2683156Sgirish 	switch (cmd) {
2693156Sgirish 	case CPU_TSTATCONF_INIT:
2703156Sgirish 	case CPU_TSTATCONF_FINI:
2713156Sgirish 	case CPU_TSTATCONF_ENABLE:
2723156Sgirish 	case CPU_TSTATCONF_DISABLE:
2733156Sgirish 		break;
2743156Sgirish 	default:
2753156Sgirish 		status = EINVAL;
2763156Sgirish 		break;
2773156Sgirish 	}
2783156Sgirish 	return (status);
2793156Sgirish }
2803156Sgirish 
2813156Sgirish void
2823156Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
2833156Sgirish {
2843156Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
2853156Sgirish 	int	i;
2863156Sgirish 
2873156Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
2883156Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
2893156Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
2903156Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
2913156Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
2923156Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
2933156Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
2943156Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
2953156Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
2963156Sgirish 	}
2973156Sgirish }
2983177Sdp78419 
2994769Sdp78419 /*
3004769Sdp78419  * Page coloring support for hashed cache index mode
3014769Sdp78419  */
3024769Sdp78419 
3034769Sdp78419 /*
3044769Sdp78419  * Node id bits from machine description (MD).  Node id distinguishes
3054769Sdp78419  * local versus remote memory. Because of MPO, page allocation does
3064769Sdp78419  * not cross node boundaries. Therefore, remove the node id bits from
3074769Sdp78419  * the color, since they are fixed. Either bit 30, or 31:30 in
3084769Sdp78419  * Victoria Falls processors.
3094769Sdp78419  * The number of node id bits is always 0 in Niagara2.
3104769Sdp78419  */
3114769Sdp78419 typedef struct n2color {
3124769Sdp78419 	uchar_t nnbits;	/* number of node id bits */
3134769Sdp78419 	uchar_t nnmask; /* mask for node id bits */
3144769Sdp78419 	uchar_t	lomask;	/* mask for bits below node id */
3154769Sdp78419 	uchar_t lobits;	/* number of bits below node id */
3164769Sdp78419 } n2color_t;
3174769Sdp78419 
3184769Sdp78419 n2color_t n2color[MMU_PAGE_SIZES];
3194769Sdp78419 static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
3204769Sdp78419 
3214769Sdp78419 /*
3224769Sdp78419  * Remove node id bits from color bits 32:28.
3234769Sdp78419  * This will reduce the number of colors.
3244769Sdp78419  * No change if number of node bits is zero.
3254769Sdp78419  */
3264769Sdp78419 static inline uint_t
3274769Sdp78419 n2_hash2color(uint_t color, uchar_t szc)
3284769Sdp78419 {
3294769Sdp78419 	n2color_t m = n2color[szc];
3304769Sdp78419 
3314769Sdp78419 	if (m.nnbits > 0) {
3324769Sdp78419 		color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
3334769Sdp78419 		ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
3344769Sdp78419 	}
3354769Sdp78419 
3364769Sdp78419 	return (color);
3374769Sdp78419 }
3384769Sdp78419 
3394769Sdp78419 /*
3404769Sdp78419  * Restore node id bits into page color.
3414769Sdp78419  * This will increase the number of colors to match N2.
3424769Sdp78419  * No change if number of node bits is zero.
3434769Sdp78419  */
3444769Sdp78419 static inline uint_t
3454769Sdp78419 n2_color2hash(uint_t color, uchar_t szc, uint_t node)
3464769Sdp78419 {
3474769Sdp78419 	n2color_t m = n2color[szc];
3484769Sdp78419 
3494769Sdp78419 	if (m.nnbits > 0) {
3504769Sdp78419 		color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
3514769Sdp78419 		color |= (node & m.nnmask) << m.lobits;
3524769Sdp78419 	}
3534769Sdp78419 
3544769Sdp78419 	return (color);
3554769Sdp78419 }
3564769Sdp78419 
3573177Sdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
3584769Sdp78419 
3594769Sdp78419 /*
3604769Sdp78419  * iterator NULL means pfn is VA, do not adjust ra_to_pa
3614769Sdp78419  * iterator (-1) means pfn is RA, need to convert to PA
3624769Sdp78419  * iterator non-null means pfn is RA, use ra_to_pa
3634769Sdp78419  */
3643177Sdp78419 uint_t
3654769Sdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
3664769Sdp78419 {
3674769Sdp78419 	mem_node_iterator_t *it = cookie;
3684769Sdp78419 	uint_t color;
3694769Sdp78419 
3704769Sdp78419 	ASSERT(szc <= TTE256M);
3714769Sdp78419 
3724769Sdp78419 	if (it == ((mem_node_iterator_t *)(-1))) {
3734769Sdp78419 		pfn = plat_rapfn_to_papfn(pfn);
3744769Sdp78419 	} else if (it != NULL) {
3754769Sdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
3764769Sdp78419 		pfn = pfn + it->mi_ra_to_pa;
3774769Sdp78419 	}
3784769Sdp78419 	pfn = PFN_BASE(pfn, szc);
3794769Sdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
3804769Sdp78419 	if (szc < TTE4M) {
3814769Sdp78419 		/* 19:18 */
3824769Sdp78419 		color = (color << 2) | ((pfn >> 5) & 0x3);
3834769Sdp78419 		if (szc > TTE64K)
3844769Sdp78419 			color >>= 1;    /* 19 */
3854769Sdp78419 	}
3864769Sdp78419 	return (n2_hash2color(color, szc));
3874769Sdp78419 }
3884769Sdp78419 
3894769Sdp78419 static uint_t
3904769Sdp78419 page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
3913177Sdp78419 {
3923177Sdp78419 	uint_t color;
3933177Sdp78419 
3943177Sdp78419 	ASSERT(szc <= TTE256M);
3953177Sdp78419 
3964769Sdp78419 	papfn = PFN_BASE(papfn, szc);
3974769Sdp78419 	color = ((papfn >> 15) ^ papfn) & 0x1f;
3984769Sdp78419 	if (szc < TTE4M) {
3994769Sdp78419 		/* 19:18 */
4004769Sdp78419 		color = (color << 2) | ((papfn >> 5) & 0x3);
4014769Sdp78419 		if (szc > TTE64K)
4024769Sdp78419 			color >>= 1;    /* 19 */
4034769Sdp78419 	}
4044769Sdp78419 	return (color);
4053177Sdp78419 }
4063177Sdp78419 
4073177Sdp78419 #if TTE256M != 5
4083177Sdp78419 #error TTE256M is not 5
4093177Sdp78419 #endif
4103177Sdp78419 
4113177Sdp78419 uint_t
4123177Sdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
4133177Sdp78419 {
4143177Sdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
4153177Sdp78419 	ASSERT(szc < TTE256M);
4164769Sdp78419 	mask = n2_color2hash(mask, szc, 0);
4173177Sdp78419 	mask &= ni2_color_masks[szc];
4184769Sdp78419 	if (szc == TTE64K || szc == TTE512K)
4194769Sdp78419 		mask >>= 1;
4204769Sdp78419 	return (n2_hash2color(mask, szc + 1));
4213177Sdp78419 }
4223177Sdp78419 
4233177Sdp78419 uint_t
4243177Sdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
4253177Sdp78419 {
4263177Sdp78419 	ASSERT(szc < TTE256M);
4274769Sdp78419 	color = n2_color2hash(color, szc, 0);
4284769Sdp78419 	if (szc == TTE64K || szc == TTE512K)
4294769Sdp78419 		color >>= 1;
4304769Sdp78419 	return (n2_hash2color(color, szc + 1));
4313177Sdp78419 }
4323177Sdp78419 
4333177Sdp78419 uint_t
4343177Sdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
4353177Sdp78419 {
4364769Sdp78419 	uint_t s;
4374318Ssusans 	ASSERT(nszc >= szc);
4383177Sdp78419 	ASSERT(nszc <= TTE256M);
4393177Sdp78419 
4404769Sdp78419 	s = nhbits[szc] - n2color[szc].nnbits;
4414769Sdp78419 	s -= nhbits[nszc] - n2color[nszc].nnbits;
4424769Sdp78419 
4434769Sdp78419 	return (s);
4444769Sdp78419 }
4454769Sdp78419 
4464769Sdp78419 uint_t
4474769Sdp78419 page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
4484769Sdp78419 {
4494769Sdp78419 	uint_t color;
4503177Sdp78419 
4514769Sdp78419 	ASSERT(nszc > szc);
4524769Sdp78419 	ASSERT(nszc <= TTE256M);
4534769Sdp78419 	ncolor = n2_color2hash(ncolor, nszc, 0);
4544769Sdp78419 	color = ncolor << (nhbits[szc] - nhbits[nszc]);
4554769Sdp78419 	color = n2_hash2color(color, szc);
4564769Sdp78419 	return (color);
4573177Sdp78419 }
4583177Sdp78419 
4594769Sdp78419 #define	PAPFN_2_MNODE(pfn) \
4604769Sdp78419 	(((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
4614769Sdp78419 
4623177Sdp78419 /*ARGSUSED*/
4633177Sdp78419 pfn_t
4643177Sdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
4654769Sdp78419     uint_t ceq_mask, uint_t color_mask, void *cookie)
4663177Sdp78419 {
4674769Sdp78419 	mem_node_iterator_t *it = cookie;
4683177Sdp78419 	pfn_t pstep = PNUM_SIZE(szc);
4693177Sdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
4703177Sdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
4714769Sdp78419 	uint_t pfnmn;
4723177Sdp78419 
4733177Sdp78419 	ASSERT((color & ~ceq_mask) == 0);
4744769Sdp78419 	ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
4753177Sdp78419 
4764769Sdp78419 	/* convert RA to PA for accurate color calculation */
4774769Sdp78419 	if (it->mi_init) {
4784769Sdp78419 		/* first call after it, so cache these values */
4794769Sdp78419 		it->mi_hash_ceq_mask =
4804769Sdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
4814769Sdp78419 		it->mi_hash_color =
4824769Sdp78419 		    n2_color2hash(color, szc, it->mi_mnode);
4834769Sdp78419 		it->mi_init = 0;
4844769Sdp78419 	} else {
4854769Sdp78419 		ASSERT(it->mi_hash_ceq_mask ==
4864769Sdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
4874769Sdp78419 		ASSERT(it->mi_hash_color ==
4884769Sdp78419 		    n2_color2hash(color, szc, it->mi_mnode));
4894769Sdp78419 	}
4904769Sdp78419 	ceq_mask = it->mi_hash_ceq_mask;
4914769Sdp78419 	color = it->mi_hash_color;
4924769Sdp78419 	pfn += it->mi_ra_to_pa;
4934769Sdp78419 
4944769Sdp78419 	/* restart here when we switch memblocks */
4954769Sdp78419 next_mem_block:
4964769Sdp78419 	if (szc <= TTE64K) {
4974769Sdp78419 		pfnmn = PAPFN_2_MNODE(pfn);
4984769Sdp78419 	}
4994769Sdp78419 	if (((page_papfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0 &&
5004769Sdp78419 	    (szc > TTE64K || pfnmn == it->mi_mnode)) {
5013177Sdp78419 
5023177Sdp78419 		/* we start from the page with correct color */
5033177Sdp78419 		if (szc >= TTE512K) {
5043177Sdp78419 			if (szc >= TTE4M) {
5053177Sdp78419 				/* page color is PA[32:28] */
5063177Sdp78419 				pfn_ceq_mask = ceq_mask << 15;
5073177Sdp78419 			} else {
5083177Sdp78419 				/* page color is PA[32:28].PA[19:19] */
5093177Sdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
5103177Sdp78419 				    ((ceq_mask >> 1) << 15);
5113177Sdp78419 			}
5124769Sdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
5134769Sdp78419 			goto done;
5143177Sdp78419 		} else {
5153177Sdp78419 			/*
5163177Sdp78419 			 * We deal 64K or 8K page. Check if we could the
5173177Sdp78419 			 * satisfy the request without changing PA[32:28]
5183177Sdp78419 			 */
5193177Sdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
5204769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5213177Sdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
5223177Sdp78419 
5233177Sdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
5244769Sdp78419 				goto done;
5253177Sdp78419 
5263177Sdp78419 			/*
5273177Sdp78419 			 * for next pfn we have to change bits PA[32:28]
5283177Sdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
5293177Sdp78419 			 */
5303177Sdp78419 			npfn = (pfn >> 15) << 15;
5313177Sdp78419 			npfn |= (ceq_mask & color & 3) << 5;
5323177Sdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
5333177Sdp78419 			    (ceq_mask & 0x1c) << 13;
5344769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5353177Sdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
5363177Sdp78419 
5373177Sdp78419 			/*
5383177Sdp78419 			 * set bits PA[17:13] to match the color
5393177Sdp78419 			 */
5404769Sdp78419 			npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
5414769Sdp78419 			goto done;
5423177Sdp78419 		}
5433177Sdp78419 	}
5443177Sdp78419 
5453177Sdp78419 	/*
5463177Sdp78419 	 * we start from the page with incorrect color - rare case
5473177Sdp78419 	 */
5483177Sdp78419 	if (szc >= TTE512K) {
5493177Sdp78419 		if (szc >= TTE4M) {
5503177Sdp78419 			/* page color is in bits PA[32:28] */
5513177Sdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
5523177Sdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
5533177Sdp78419 		} else {
5543177Sdp78419 			/* try get the right color by changing bit PA[19:19] */
5553177Sdp78419 			npfn = pfn + pstep;
5564769Sdp78419 			if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
5573177Sdp78419 			    ceq_mask) == 0)
5584769Sdp78419 				goto done;
5593177Sdp78419 
5603177Sdp78419 			/* page color is PA[32:28].PA[19:19] */
5613177Sdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
5623177Sdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
5633177Sdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
5643177Sdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
5653177Sdp78419 		}
5663177Sdp78419 
5673177Sdp78419 		while (npfn <= pfn) {
5683177Sdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
5693177Sdp78419 		}
5704769Sdp78419 		goto done;
5713177Sdp78419 	}
5723177Sdp78419 
5733177Sdp78419 	/*
5744769Sdp78419 	 *  We deal 64K or 8K page of incorrect color.
5753177Sdp78419 	 * Try correcting color without changing PA[32:28]
5763177Sdp78419 	 */
5773177Sdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
5783177Sdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
5794769Sdp78419 	if (pfnmn == it->mi_mnode) {
5804769Sdp78419 		npfn = (pfn & ~(pfn_t)0x7f);
5814769Sdp78419 		npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
5824769Sdp78419 		npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
5833177Sdp78419 
5844769Sdp78419 		if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
5854769Sdp78419 		    ceq_mask) == 0) {
5864769Sdp78419 			/* the color is fixed - find the next page */
5874769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5884769Sdp78419 			while (npfn <= pfn) {
5894769Sdp78419 				npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
5904769Sdp78419 				    mask);
5914769Sdp78419 			}
5924769Sdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
5934769Sdp78419 				goto done;
5943177Sdp78419 		}
5953177Sdp78419 	}
5963177Sdp78419 
5973177Sdp78419 	/* to fix the color need to touch PA[32:28] */
5983177Sdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
5993177Sdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
6004769Sdp78419 
6014769Sdp78419 	/* fix mnode if input pfn is in the wrong mnode. */
6024769Sdp78419 	if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
6034769Sdp78419 		npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
6044769Sdp78419 		    it->mi_mnode_pfn_shift;
6054769Sdp78419 	}
6064769Sdp78419 
6073177Sdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
6084769Sdp78419 	tmpmask |= it->mi_mnode_pfn_mask;
6093177Sdp78419 
6103177Sdp78419 	while (npfn <= pfn) {
6113177Sdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
6123177Sdp78419 	}
6133177Sdp78419 
6143177Sdp78419 	/* set bits PA[19:13] to match the color */
6153177Sdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
6163177Sdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
6173177Sdp78419 
6184769Sdp78419 done:
6194769Sdp78419 	ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
6204769Sdp78419 	ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
6214769Sdp78419 
6224769Sdp78419 	/* PA to RA */
6234769Sdp78419 	npfn -= it->mi_ra_to_pa;
6244769Sdp78419 
6254769Sdp78419 	/* check for possible memblock switch */
6264769Sdp78419 	if (npfn > it->mi_mblock_end) {
627*6041Sdp78419 		pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, szc, it,
628*6041Sdp78419 		    0);
6294769Sdp78419 		if (pfn == (pfn_t)-1)
6304769Sdp78419 			return (pfn);
6314769Sdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
6324769Sdp78419 		pfn += it->mi_ra_to_pa;
6334769Sdp78419 		goto next_mem_block;
6344769Sdp78419 	}
6353177Sdp78419 
6363177Sdp78419 	return (npfn);
6373177Sdp78419 }
6383177Sdp78419 
6393177Sdp78419 /*
6403177Sdp78419  * init page coloring
6414769Sdp78419  * VF encodes node_id for an L-group in either bit 30 or 31:30,
6424769Sdp78419  * which effectively reduces the number of colors available per mnode.
6433177Sdp78419  */
6443177Sdp78419 void
6453177Sdp78419 page_coloring_init_cpu()
6463177Sdp78419 {
6473177Sdp78419 	int i;
6484769Sdp78419 	uchar_t id;
6494769Sdp78419 	uchar_t lo;
6504769Sdp78419 	uchar_t hi;
6514769Sdp78419 	n2color_t m;
6524769Sdp78419 	mem_node_iterator_t it;
6534769Sdp78419 	static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
6543177Sdp78419 
6555615Sdp78419 	for (i = 0; i < max_mem_nodes; i++) {
6565615Sdp78419 		memset(&it, 0, sizeof (it));
657*6041Sdp78419 		if (plat_mem_node_iterator_init(0, i, 0, &it, 1) != (pfn_t)-1)
6585615Sdp78419 			break;
6595615Sdp78419 	}
6605615Sdp78419 	ASSERT(i < max_mem_nodes);
6614769Sdp78419 	for (i = 0; i < mmu_page_sizes; i++) {
6625352Ssvemuri 		(void) memset(&m, 0, sizeof (m));
6634769Sdp78419 		id = it.mi_mnode_pfn_mask >> 15;	/* node id mask */
6644769Sdp78419 		id &= idmask[i];
6654769Sdp78419 		lo = lowbit(id);
6664769Sdp78419 		if (lo > 0) {
6674769Sdp78419 			hi = highbit(id);
6684769Sdp78419 			m.nnbits = hi - lo + 1;
6694769Sdp78419 			m.nnmask = (1 << m.nnbits) - 1;
6704769Sdp78419 			lo += nhbits[i] - 5;
6714769Sdp78419 			m.lomask = (1 << (lo - 1)) - 1;
6724769Sdp78419 			m.lobits = lo - 1;
6734769Sdp78419 		}
6744769Sdp78419 		hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
6754769Sdp78419 		n2color[i] = m;
6763177Sdp78419 	}
6773177Sdp78419 }
6783733Sdp78419 
6793733Sdp78419 /*
6803733Sdp78419  * group colorequiv colors on N2 by low order bits of the color first
6813733Sdp78419  */
6823733Sdp78419 void
6833733Sdp78419 page_set_colorequiv_arr_cpu(void)
6843733Sdp78419 {
6853733Sdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
6863733Sdp78419 
6874769Sdp78419 	nequiv_shades_log2[1] -= n2color[1].nnbits;
6883733Sdp78419 	if (colorequiv > 1) {
6893733Sdp78419 		int i;
6903733Sdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
6913733Sdp78419 
6923733Sdp78419 		if (sv_a > 15)
6933733Sdp78419 			sv_a = 15;
6943733Sdp78419 
6953733Sdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
6963733Sdp78419 			uint_t colors;
6973733Sdp78419 			uint_t a = sv_a;
6983733Sdp78419 
6993733Sdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
7003733Sdp78419 				continue;
7013733Sdp78419 			while ((colors >> a) == 0)
7023733Sdp78419 				a--;
7033733Sdp78419 			if (a > (colorequivszc[i] & 0xf) +
7043733Sdp78419 			    (colorequivszc[i] >> 4)) {
7053733Sdp78419 				if (a <= nequiv_shades_log2[i]) {
7064732Sdavemq 					colorequivszc[i] = (uchar_t)a;
7073733Sdp78419 				} else {
7083733Sdp78419 					colorequivszc[i] =
7093733Sdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
7103733Sdp78419 					    nequiv_shades_log2[i];
7113733Sdp78419 				}
7123733Sdp78419 			}
7133733Sdp78419 		}
7143733Sdp78419 	}
7153733Sdp78419 }
716