xref: /onnv-gate/usr/src/uts/sun4v/cpu/niagara2.c (revision 5352:30dae0788871)
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 /*
233434Sesaxe  * Copyright 2007 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>
623156Sgirish 
633156Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
644732Sdavemq #if defined(NIAGARA2_IMPL)
653156Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
664732Sdavemq #elif defined(VFALLS_IMPL)
674732Sdavemq char cpu_module_name[] = "SUNW,UltraSPARC-T2+";
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 };
864732Sdavemq #endif
873156Sgirish 
883156Sgirish void
893156Sgirish cpu_setup(void)
903156Sgirish {
913156Sgirish 	extern int mmu_exported_pagesize_mask;
923156Sgirish 	extern int cpc_has_overflow_intr;
934204Sha137994 	extern size_t contig_mem_prealloc_base;
943156Sgirish 	int status;
953156Sgirish 
963156Sgirish 	/*
973156Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
983156Sgirish 	 * services.
993156Sgirish 	 */
1004732Sdavemq 	status = hsvc_register(&cpu_hsvc, &cpu_sup_minor);
1013156Sgirish 	if (status != 0) {
1023156Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
1033156Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
1044732Sdavemq 		    cpu_hsvc.hsvc_modname, cpu_hsvc.hsvc_group,
1054732Sdavemq 		    cpu_hsvc.hsvc_major, cpu_hsvc.hsvc_minor, status);
1064732Sdavemq 		cpu_hsvc_available = B_FALSE;
1073156Sgirish 	}
1083156Sgirish 
1093156Sgirish 	/*
1103156Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
1113156Sgirish 	 * routine.
1123156Sgirish 	 */
1133156Sgirish 	cpu_setup_common(NULL);
1143156Sgirish 
1153156Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
1163156Sgirish 
1173156Sgirish 	if ((mmu_exported_pagesize_mask &
1183156Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
1193156Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
1203156Sgirish 		cmn_err(CE_PANIC, "machine description"
1213156Sgirish 		    " does not have required sun4v page sizes"
1223156Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
1233156Sgirish 		    mmu_exported_pagesize_mask);
1243156Sgirish 
1253156Sgirish 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 | AV_SPARC_ASI_BLK_INIT;
1263156Sgirish 
1273156Sgirish 	/*
1283156Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
1293156Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
1303156Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
1313156Sgirish 	 * and must never be mapped. In addition, software must not use
1323156Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
1333156Sgirish 	 * avoid problems with prefetching into the VA hole.
1343156Sgirish 	 */
1353156Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
1363156Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
1373156Sgirish 
1383156Sgirish 	/*
1393156Sgirish 	 * Niagara2 has a performance counter overflow interrupt
1403156Sgirish 	 */
1413156Sgirish 	cpc_has_overflow_intr = 1;
1423177Sdp78419 
1433177Sdp78419 	/*
1443177Sdp78419 	 * Enable 4M pages for OOB.
1453177Sdp78419 	 */
1463177Sdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
1473177Sdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
1483177Sdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
1494204Sha137994 
1504204Sha137994 	contig_mem_prealloc_base = NIAGARA2_PREALLOC_BASE;
1513156Sgirish }
1523156Sgirish 
1533156Sgirish /*
1543156Sgirish  * Set the magic constants of the implementation.
1553156Sgirish  */
1563156Sgirish void
1573156Sgirish cpu_fiximp(struct cpu_node *cpunode)
1583156Sgirish {
1593156Sgirish 	/*
1603156Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
1613156Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
1623156Sgirish 	 * size, linesize.
1633156Sgirish 	 */
1643156Sgirish 	if (cpunode->ecache_size == 0)
1653156Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
1663156Sgirish 	if (cpunode->ecache_linesize == 0)
1673156Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
1683156Sgirish 	if (cpunode->ecache_associativity == 0)
1693156Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
1703156Sgirish }
1713156Sgirish 
1723156Sgirish void
1734203Srsmaeda cpu_map_exec_units(struct cpu *cp)
1743156Sgirish {
1754203Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
1763156Sgirish 
1773156Sgirish 	/*
1783434Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
1794203Srsmaeda 	 * the execution unit sharing information from the MD. They
1804203Srsmaeda 	 * default to the CPU id in the absence of such information.
1813156Sgirish 	 */
1823156Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
1833156Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
1843156Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
1853156Sgirish 
1863434Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
1873434Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
1883434Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
1893434Sesaxe 
1903434Sesaxe 	/*
1913434Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
1923434Sesaxe 	 */
1933434Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
1944732Sdavemq 
1954732Sdavemq 	/*
1964732Sdavemq 	 * The cpu_chip field is initialized based on the information
1974732Sdavemq 	 * in the MD and assume that all cpus within a chip
1984732Sdavemq 	 * share the same L2 cache. If no such info is available, we
1994732Sdavemq 	 * set the cpu to belong to the defacto chip 0.
2004732Sdavemq 	 */
2014769Sdp78419 	cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
2024769Sdp78419 	if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
2034769Sdp78419 		cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
2045079Sjc25722 
2055079Sjc25722 	cp->cpu_m.cpu_chip = cpunodes[cp->cpu_id].l2_cache_mapping;
2065079Sjc25722 	if (cp->cpu_m.cpu_chip == NO_L2_CACHE_MAPPING_FOUND)
2075079Sjc25722 		cp->cpu_m.cpu_chip = CPU_CHIPID_INVALID;
2084203Srsmaeda }
2094203Srsmaeda 
2104732Sdavemq static int cpucnt;
2114203Srsmaeda 
2124203Srsmaeda void
2134203Srsmaeda cpu_init_private(struct cpu *cp)
2144203Srsmaeda {
2154203Srsmaeda 	extern void niagara_kstat_init(void);
2163434Sesaxe 
2173156Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
2184203Srsmaeda 
2194203Srsmaeda 	cpu_map_exec_units(cp);
2204203Srsmaeda 
2214732Sdavemq 	if ((cpucnt++ == 0) && (cpu_hsvc_available == B_TRUE))
2224732Sdavemq 		(void) niagara_kstat_init();
2233156Sgirish }
2243156Sgirish 
2253156Sgirish /*ARGSUSED*/
2263156Sgirish void
2273156Sgirish cpu_uninit_private(struct cpu *cp)
2283156Sgirish {
2294203Srsmaeda 	extern void niagara_kstat_fini(void);
2303156Sgirish 
2313156Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
2324732Sdavemq 	if ((--cpucnt == 0) && (cpu_hsvc_available == B_TRUE))
2334732Sdavemq 		(void) niagara_kstat_fini();
2343156Sgirish }
2353156Sgirish 
2363156Sgirish /*
2373156Sgirish  * On Niagara2, any flush will cause all preceding stores to be
2383156Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
2393156Sgirish  * the address is ignored, so we always flush address 0.
2403156Sgirish  */
2413156Sgirish /*ARGSUSED*/
2423156Sgirish void
2433156Sgirish dtrace_flush_sec(uintptr_t addr)
2443156Sgirish {
2453156Sgirish 	doflush(0);
2463156Sgirish }
2473156Sgirish 
2483156Sgirish /*
2493156Sgirish  * Trapstat support for Niagara2 processor
2503156Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
2513156Sgirish  * enabled no TSB hit information will be available. Therefore setting
2523156Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
2533156Sgirish  */
2543156Sgirish int
2553156Sgirish cpu_trapstat_conf(int cmd)
2563156Sgirish {
2573156Sgirish 	int status = 0;
2583156Sgirish 
2593156Sgirish 	switch (cmd) {
2603156Sgirish 	case CPU_TSTATCONF_INIT:
2613156Sgirish 	case CPU_TSTATCONF_FINI:
2623156Sgirish 	case CPU_TSTATCONF_ENABLE:
2633156Sgirish 	case CPU_TSTATCONF_DISABLE:
2643156Sgirish 		break;
2653156Sgirish 	default:
2663156Sgirish 		status = EINVAL;
2673156Sgirish 		break;
2683156Sgirish 	}
2693156Sgirish 	return (status);
2703156Sgirish }
2713156Sgirish 
2723156Sgirish void
2733156Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
2743156Sgirish {
2753156Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
2763156Sgirish 	int	i;
2773156Sgirish 
2783156Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
2793156Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
2803156Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
2813156Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
2823156Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
2833156Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
2843156Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
2853156Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
2863156Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
2873156Sgirish 	}
2883156Sgirish }
2893177Sdp78419 
2904769Sdp78419 /*
2914769Sdp78419  * Page coloring support for hashed cache index mode
2924769Sdp78419  */
2934769Sdp78419 
2944769Sdp78419 /*
2954769Sdp78419  * Node id bits from machine description (MD).  Node id distinguishes
2964769Sdp78419  * local versus remote memory. Because of MPO, page allocation does
2974769Sdp78419  * not cross node boundaries. Therefore, remove the node id bits from
2984769Sdp78419  * the color, since they are fixed. Either bit 30, or 31:30 in
2994769Sdp78419  * Victoria Falls processors.
3004769Sdp78419  * The number of node id bits is always 0 in Niagara2.
3014769Sdp78419  */
3024769Sdp78419 typedef struct n2color {
3034769Sdp78419 	uchar_t nnbits;	/* number of node id bits */
3044769Sdp78419 	uchar_t nnmask; /* mask for node id bits */
3054769Sdp78419 	uchar_t	lomask;	/* mask for bits below node id */
3064769Sdp78419 	uchar_t lobits;	/* number of bits below node id */
3074769Sdp78419 } n2color_t;
3084769Sdp78419 
3094769Sdp78419 n2color_t n2color[MMU_PAGE_SIZES];
3104769Sdp78419 static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
3114769Sdp78419 
3124769Sdp78419 /*
3134769Sdp78419  * Remove node id bits from color bits 32:28.
3144769Sdp78419  * This will reduce the number of colors.
3154769Sdp78419  * No change if number of node bits is zero.
3164769Sdp78419  */
3174769Sdp78419 static inline uint_t
3184769Sdp78419 n2_hash2color(uint_t color, uchar_t szc)
3194769Sdp78419 {
3204769Sdp78419 	n2color_t m = n2color[szc];
3214769Sdp78419 
3224769Sdp78419 	if (m.nnbits > 0) {
3234769Sdp78419 		color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
3244769Sdp78419 		ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
3254769Sdp78419 	}
3264769Sdp78419 
3274769Sdp78419 	return (color);
3284769Sdp78419 }
3294769Sdp78419 
3304769Sdp78419 /*
3314769Sdp78419  * Restore node id bits into page color.
3324769Sdp78419  * This will increase the number of colors to match N2.
3334769Sdp78419  * No change if number of node bits is zero.
3344769Sdp78419  */
3354769Sdp78419 static inline uint_t
3364769Sdp78419 n2_color2hash(uint_t color, uchar_t szc, uint_t node)
3374769Sdp78419 {
3384769Sdp78419 	n2color_t m = n2color[szc];
3394769Sdp78419 
3404769Sdp78419 	if (m.nnbits > 0) {
3414769Sdp78419 		color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
3424769Sdp78419 		color |= (node & m.nnmask) << m.lobits;
3434769Sdp78419 	}
3444769Sdp78419 
3454769Sdp78419 	return (color);
3464769Sdp78419 }
3474769Sdp78419 
3483177Sdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
3494769Sdp78419 
3504769Sdp78419 /*
3514769Sdp78419  * iterator NULL means pfn is VA, do not adjust ra_to_pa
3524769Sdp78419  * iterator (-1) means pfn is RA, need to convert to PA
3534769Sdp78419  * iterator non-null means pfn is RA, use ra_to_pa
3544769Sdp78419  */
3553177Sdp78419 uint_t
3564769Sdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
3574769Sdp78419 {
3584769Sdp78419 	mem_node_iterator_t *it = cookie;
3594769Sdp78419 	uint_t color;
3604769Sdp78419 
3614769Sdp78419 	ASSERT(szc <= TTE256M);
3624769Sdp78419 
3634769Sdp78419 	if (it == ((mem_node_iterator_t *)(-1))) {
3644769Sdp78419 		pfn = plat_rapfn_to_papfn(pfn);
3654769Sdp78419 	} else if (it != NULL) {
3664769Sdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
3674769Sdp78419 		pfn = pfn + it->mi_ra_to_pa;
3684769Sdp78419 	}
3694769Sdp78419 	pfn = PFN_BASE(pfn, szc);
3704769Sdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
3714769Sdp78419 	if (szc < TTE4M) {
3724769Sdp78419 		/* 19:18 */
3734769Sdp78419 		color = (color << 2) | ((pfn >> 5) & 0x3);
3744769Sdp78419 		if (szc > TTE64K)
3754769Sdp78419 			color >>= 1;    /* 19 */
3764769Sdp78419 	}
3774769Sdp78419 	return (n2_hash2color(color, szc));
3784769Sdp78419 }
3794769Sdp78419 
3804769Sdp78419 static uint_t
3814769Sdp78419 page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
3823177Sdp78419 {
3833177Sdp78419 	uint_t color;
3843177Sdp78419 
3853177Sdp78419 	ASSERT(szc <= TTE256M);
3863177Sdp78419 
3874769Sdp78419 	papfn = PFN_BASE(papfn, szc);
3884769Sdp78419 	color = ((papfn >> 15) ^ papfn) & 0x1f;
3894769Sdp78419 	if (szc < TTE4M) {
3904769Sdp78419 		/* 19:18 */
3914769Sdp78419 		color = (color << 2) | ((papfn >> 5) & 0x3);
3924769Sdp78419 		if (szc > TTE64K)
3934769Sdp78419 			color >>= 1;    /* 19 */
3944769Sdp78419 	}
3954769Sdp78419 	return (color);
3963177Sdp78419 }
3973177Sdp78419 
3983177Sdp78419 #if TTE256M != 5
3993177Sdp78419 #error TTE256M is not 5
4003177Sdp78419 #endif
4013177Sdp78419 
4023177Sdp78419 uint_t
4033177Sdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
4043177Sdp78419 {
4053177Sdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
4063177Sdp78419 	ASSERT(szc < TTE256M);
4074769Sdp78419 	mask = n2_color2hash(mask, szc, 0);
4083177Sdp78419 	mask &= ni2_color_masks[szc];
4094769Sdp78419 	if (szc == TTE64K || szc == TTE512K)
4104769Sdp78419 		mask >>= 1;
4114769Sdp78419 	return (n2_hash2color(mask, szc + 1));
4123177Sdp78419 }
4133177Sdp78419 
4143177Sdp78419 uint_t
4153177Sdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
4163177Sdp78419 {
4173177Sdp78419 	ASSERT(szc < TTE256M);
4184769Sdp78419 	color = n2_color2hash(color, szc, 0);
4194769Sdp78419 	if (szc == TTE64K || szc == TTE512K)
4204769Sdp78419 		color >>= 1;
4214769Sdp78419 	return (n2_hash2color(color, szc + 1));
4223177Sdp78419 }
4233177Sdp78419 
4243177Sdp78419 uint_t
4253177Sdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
4263177Sdp78419 {
4274769Sdp78419 	uint_t s;
4284318Ssusans 	ASSERT(nszc >= szc);
4293177Sdp78419 	ASSERT(nszc <= TTE256M);
4303177Sdp78419 
4314769Sdp78419 	s = nhbits[szc] - n2color[szc].nnbits;
4324769Sdp78419 	s -= nhbits[nszc] - n2color[nszc].nnbits;
4334769Sdp78419 
4344769Sdp78419 	return (s);
4354769Sdp78419 }
4364769Sdp78419 
4374769Sdp78419 uint_t
4384769Sdp78419 page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
4394769Sdp78419 {
4404769Sdp78419 	uint_t color;
4413177Sdp78419 
4424769Sdp78419 	ASSERT(nszc > szc);
4434769Sdp78419 	ASSERT(nszc <= TTE256M);
4444769Sdp78419 	ncolor = n2_color2hash(ncolor, nszc, 0);
4454769Sdp78419 	color = ncolor << (nhbits[szc] - nhbits[nszc]);
4464769Sdp78419 	color = n2_hash2color(color, szc);
4474769Sdp78419 	return (color);
4483177Sdp78419 }
4493177Sdp78419 
4504769Sdp78419 #define	PAPFN_2_MNODE(pfn) \
4514769Sdp78419 	(((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
4524769Sdp78419 
4533177Sdp78419 /*ARGSUSED*/
4543177Sdp78419 pfn_t
4553177Sdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
4564769Sdp78419     uint_t ceq_mask, uint_t color_mask, void *cookie)
4573177Sdp78419 {
4584769Sdp78419 	mem_node_iterator_t *it = cookie;
4593177Sdp78419 	pfn_t pstep = PNUM_SIZE(szc);
4603177Sdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
4613177Sdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
4624769Sdp78419 	uint_t pfnmn;
4633177Sdp78419 
4643177Sdp78419 	ASSERT((color & ~ceq_mask) == 0);
4654769Sdp78419 	ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
4663177Sdp78419 
4674769Sdp78419 	/* convert RA to PA for accurate color calculation */
4684769Sdp78419 	if (it->mi_init) {
4694769Sdp78419 		/* first call after it, so cache these values */
4704769Sdp78419 		it->mi_hash_ceq_mask =
4714769Sdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
4724769Sdp78419 		it->mi_hash_color =
4734769Sdp78419 		    n2_color2hash(color, szc, it->mi_mnode);
4744769Sdp78419 		it->mi_init = 0;
4754769Sdp78419 	} else {
4764769Sdp78419 		ASSERT(it->mi_hash_ceq_mask ==
4774769Sdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
4784769Sdp78419 		ASSERT(it->mi_hash_color ==
4794769Sdp78419 		    n2_color2hash(color, szc, it->mi_mnode));
4804769Sdp78419 	}
4814769Sdp78419 	ceq_mask = it->mi_hash_ceq_mask;
4824769Sdp78419 	color = it->mi_hash_color;
4834769Sdp78419 	pfn += it->mi_ra_to_pa;
4844769Sdp78419 
4854769Sdp78419 	/* restart here when we switch memblocks */
4864769Sdp78419 next_mem_block:
4874769Sdp78419 	if (szc <= TTE64K) {
4884769Sdp78419 		pfnmn = PAPFN_2_MNODE(pfn);
4894769Sdp78419 	}
4904769Sdp78419 	if (((page_papfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0 &&
4914769Sdp78419 	    (szc > TTE64K || pfnmn == it->mi_mnode)) {
4923177Sdp78419 
4933177Sdp78419 		/* we start from the page with correct color */
4943177Sdp78419 		if (szc >= TTE512K) {
4953177Sdp78419 			if (szc >= TTE4M) {
4963177Sdp78419 				/* page color is PA[32:28] */
4973177Sdp78419 				pfn_ceq_mask = ceq_mask << 15;
4983177Sdp78419 			} else {
4993177Sdp78419 				/* page color is PA[32:28].PA[19:19] */
5003177Sdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
5013177Sdp78419 				    ((ceq_mask >> 1) << 15);
5023177Sdp78419 			}
5034769Sdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
5044769Sdp78419 			goto done;
5053177Sdp78419 		} else {
5063177Sdp78419 			/*
5073177Sdp78419 			 * We deal 64K or 8K page. Check if we could the
5083177Sdp78419 			 * satisfy the request without changing PA[32:28]
5093177Sdp78419 			 */
5103177Sdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
5114769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5123177Sdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
5133177Sdp78419 
5143177Sdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
5154769Sdp78419 				goto done;
5163177Sdp78419 
5173177Sdp78419 			/*
5183177Sdp78419 			 * for next pfn we have to change bits PA[32:28]
5193177Sdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
5203177Sdp78419 			 */
5213177Sdp78419 			npfn = (pfn >> 15) << 15;
5223177Sdp78419 			npfn |= (ceq_mask & color & 3) << 5;
5233177Sdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
5243177Sdp78419 			    (ceq_mask & 0x1c) << 13;
5254769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5263177Sdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
5273177Sdp78419 
5283177Sdp78419 			/*
5293177Sdp78419 			 * set bits PA[17:13] to match the color
5303177Sdp78419 			 */
5314769Sdp78419 			npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
5324769Sdp78419 			goto done;
5333177Sdp78419 		}
5343177Sdp78419 	}
5353177Sdp78419 
5363177Sdp78419 	/*
5373177Sdp78419 	 * we start from the page with incorrect color - rare case
5383177Sdp78419 	 */
5393177Sdp78419 	if (szc >= TTE512K) {
5403177Sdp78419 		if (szc >= TTE4M) {
5413177Sdp78419 			/* page color is in bits PA[32:28] */
5423177Sdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
5433177Sdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
5443177Sdp78419 		} else {
5453177Sdp78419 			/* try get the right color by changing bit PA[19:19] */
5463177Sdp78419 			npfn = pfn + pstep;
5474769Sdp78419 			if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
5483177Sdp78419 			    ceq_mask) == 0)
5494769Sdp78419 				goto done;
5503177Sdp78419 
5513177Sdp78419 			/* page color is PA[32:28].PA[19:19] */
5523177Sdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
5533177Sdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
5543177Sdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
5553177Sdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
5563177Sdp78419 		}
5573177Sdp78419 
5583177Sdp78419 		while (npfn <= pfn) {
5593177Sdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
5603177Sdp78419 		}
5614769Sdp78419 		goto done;
5623177Sdp78419 	}
5633177Sdp78419 
5643177Sdp78419 	/*
5654769Sdp78419 	 *  We deal 64K or 8K page of incorrect color.
5663177Sdp78419 	 * Try correcting color without changing PA[32:28]
5673177Sdp78419 	 */
5683177Sdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
5693177Sdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
5704769Sdp78419 	if (pfnmn == it->mi_mnode) {
5714769Sdp78419 		npfn = (pfn & ~(pfn_t)0x7f);
5724769Sdp78419 		npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
5734769Sdp78419 		npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
5743177Sdp78419 
5754769Sdp78419 		if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
5764769Sdp78419 		    ceq_mask) == 0) {
5774769Sdp78419 			/* the color is fixed - find the next page */
5784769Sdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
5794769Sdp78419 			while (npfn <= pfn) {
5804769Sdp78419 				npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
5814769Sdp78419 				    mask);
5824769Sdp78419 			}
5834769Sdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
5844769Sdp78419 				goto done;
5853177Sdp78419 		}
5863177Sdp78419 	}
5873177Sdp78419 
5883177Sdp78419 	/* to fix the color need to touch PA[32:28] */
5893177Sdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
5903177Sdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
5914769Sdp78419 
5924769Sdp78419 	/* fix mnode if input pfn is in the wrong mnode. */
5934769Sdp78419 	if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
5944769Sdp78419 		npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
5954769Sdp78419 		    it->mi_mnode_pfn_shift;
5964769Sdp78419 	}
5974769Sdp78419 
5983177Sdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
5994769Sdp78419 	tmpmask |= it->mi_mnode_pfn_mask;
6003177Sdp78419 
6013177Sdp78419 	while (npfn <= pfn) {
6023177Sdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
6033177Sdp78419 	}
6043177Sdp78419 
6053177Sdp78419 	/* set bits PA[19:13] to match the color */
6063177Sdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
6073177Sdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
6083177Sdp78419 
6094769Sdp78419 done:
6104769Sdp78419 	ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
6114769Sdp78419 	ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
6124769Sdp78419 
6134769Sdp78419 	/* PA to RA */
6144769Sdp78419 	npfn -= it->mi_ra_to_pa;
6154769Sdp78419 
6164769Sdp78419 	/* check for possible memblock switch */
6174769Sdp78419 	if (npfn > it->mi_mblock_end) {
6184769Sdp78419 		pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, it, 0);
6194769Sdp78419 		if (pfn == (pfn_t)-1)
6204769Sdp78419 			return (pfn);
6214769Sdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
6224769Sdp78419 		pfn += it->mi_ra_to_pa;
6234769Sdp78419 		goto next_mem_block;
6244769Sdp78419 	}
6253177Sdp78419 
6263177Sdp78419 	return (npfn);
6273177Sdp78419 }
6283177Sdp78419 
6293177Sdp78419 /*
6303177Sdp78419  * init page coloring
6314769Sdp78419  * VF encodes node_id for an L-group in either bit 30 or 31:30,
6324769Sdp78419  * which effectively reduces the number of colors available per mnode.
6333177Sdp78419  */
6343177Sdp78419 void
6353177Sdp78419 page_coloring_init_cpu()
6363177Sdp78419 {
6373177Sdp78419 	int i;
6384769Sdp78419 	uchar_t id;
6394769Sdp78419 	uchar_t lo;
6404769Sdp78419 	uchar_t hi;
6414769Sdp78419 	n2color_t m;
6424769Sdp78419 	mem_node_iterator_t it;
6434769Sdp78419 	static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
6443177Sdp78419 
6454769Sdp78419 	(void) plat_mem_node_iterator_init(0, 0, &it, 1);
6464769Sdp78419 	for (i = 0; i < mmu_page_sizes; i++) {
647*5352Ssvemuri 		(void) memset(&m, 0, sizeof (m));
6484769Sdp78419 		id = it.mi_mnode_pfn_mask >> 15;	/* node id mask */
6494769Sdp78419 		id &= idmask[i];
6504769Sdp78419 		lo = lowbit(id);
6514769Sdp78419 		if (lo > 0) {
6524769Sdp78419 			hi = highbit(id);
6534769Sdp78419 			m.nnbits = hi - lo + 1;
6544769Sdp78419 			m.nnmask = (1 << m.nnbits) - 1;
6554769Sdp78419 			lo += nhbits[i] - 5;
6564769Sdp78419 			m.lomask = (1 << (lo - 1)) - 1;
6574769Sdp78419 			m.lobits = lo - 1;
6584769Sdp78419 		}
6594769Sdp78419 		hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
6604769Sdp78419 		n2color[i] = m;
6613177Sdp78419 	}
6623177Sdp78419 }
6633733Sdp78419 
6643733Sdp78419 /*
6653733Sdp78419  * group colorequiv colors on N2 by low order bits of the color first
6663733Sdp78419  */
6673733Sdp78419 void
6683733Sdp78419 page_set_colorequiv_arr_cpu(void)
6693733Sdp78419 {
6703733Sdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
6713733Sdp78419 
6724769Sdp78419 	nequiv_shades_log2[1] -= n2color[1].nnbits;
6733733Sdp78419 	if (colorequiv > 1) {
6743733Sdp78419 		int i;
6753733Sdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
6763733Sdp78419 
6773733Sdp78419 		if (sv_a > 15)
6783733Sdp78419 			sv_a = 15;
6793733Sdp78419 
6803733Sdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
6813733Sdp78419 			uint_t colors;
6823733Sdp78419 			uint_t a = sv_a;
6833733Sdp78419 
6843733Sdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
6853733Sdp78419 				continue;
6863733Sdp78419 			while ((colors >> a) == 0)
6873733Sdp78419 				a--;
6883733Sdp78419 			if (a > (colorequivszc[i] & 0xf) +
6893733Sdp78419 			    (colorequivszc[i] >> 4)) {
6903733Sdp78419 				if (a <= nequiv_shades_log2[i]) {
6914732Sdavemq 					colorequivszc[i] = (uchar_t)a;
6923733Sdp78419 				} else {
6933733Sdp78419 					colorequivszc[i] =
6943733Sdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
6953733Sdp78419 					    nequiv_shades_log2[i];
6963733Sdp78419 				}
6973733Sdp78419 			}
6983733Sdp78419 		}
6993733Sdp78419 	}
7003733Sdp78419 }
701