xref: /titanic_51/usr/src/uts/sun4v/cpu/niagara2.c (revision ce8eb11a8717b4a57c68fd77ab9f8aac15b16bf2)
144961713Sgirish /*
244961713Sgirish  * CDDL HEADER START
344961713Sgirish  *
444961713Sgirish  * The contents of this file are subject to the terms of the
544961713Sgirish  * Common Development and Distribution License (the "License").
644961713Sgirish  * You may not use this file except in compliance with the License.
744961713Sgirish  *
844961713Sgirish  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
944961713Sgirish  * or http://www.opensolaris.org/os/licensing.
1044961713Sgirish  * See the License for the specific language governing permissions
1144961713Sgirish  * and limitations under the License.
1244961713Sgirish  *
1344961713Sgirish  * When distributing Covered Code, include this CDDL HEADER in each
1444961713Sgirish  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1544961713Sgirish  * If applicable, add the following below this CDDL HEADER, with the
1644961713Sgirish  * fields enclosed by brackets "[]" replaced with your own identifying
1744961713Sgirish  * information: Portions Copyright [yyyy] [name of copyright owner]
1844961713Sgirish  *
1944961713Sgirish  * CDDL HEADER END
2044961713Sgirish  */
21459190a5Srsmaeda 
2244961713Sgirish /*
23fb2f18f8Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2444961713Sgirish  * Use is subject to license terms.
2544961713Sgirish  */
2644961713Sgirish 
2744961713Sgirish #pragma ident	"%Z%%M%	%I%	%E% SMI"
2844961713Sgirish 
2944961713Sgirish #include <sys/types.h>
3044961713Sgirish #include <sys/systm.h>
3144961713Sgirish #include <sys/archsystm.h>
3244961713Sgirish #include <sys/machparam.h>
3344961713Sgirish #include <sys/machsystm.h>
3444961713Sgirish #include <sys/cpu.h>
3544961713Sgirish #include <sys/elf_SPARC.h>
3644961713Sgirish #include <vm/hat_sfmmu.h>
3744961713Sgirish #include <vm/page.h>
38102033aaSdp78419 #include <vm/vm_dep.h>
3944961713Sgirish #include <sys/cpuvar.h>
4044961713Sgirish #include <sys/async.h>
4144961713Sgirish #include <sys/cmn_err.h>
4244961713Sgirish #include <sys/debug.h>
4344961713Sgirish #include <sys/dditypes.h>
4444961713Sgirish #include <sys/sunddi.h>
4544961713Sgirish #include <sys/cpu_module.h>
4644961713Sgirish #include <sys/prom_debug.h>
4744961713Sgirish #include <sys/vmsystm.h>
4844961713Sgirish #include <sys/prom_plat.h>
4944961713Sgirish #include <sys/sysmacros.h>
5044961713Sgirish #include <sys/intreg.h>
5144961713Sgirish #include <sys/machtrap.h>
5244961713Sgirish #include <sys/ontrap.h>
5344961713Sgirish #include <sys/ivintr.h>
5444961713Sgirish #include <sys/atomic.h>
5544961713Sgirish #include <sys/panic.h>
5644961713Sgirish #include <sys/dtrace.h>
5744961713Sgirish #include <sys/simulate.h>
5844961713Sgirish #include <sys/fault.h>
5944961713Sgirish #include <sys/niagara2regs.h>
6044961713Sgirish #include <sys/hsvc.h>
6144961713Sgirish #include <sys/trapstat.h>
6244961713Sgirish 
6344961713Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
6459ac0c16Sdavemq #if defined(NIAGARA2_IMPL)
6544961713Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
6659ac0c16Sdavemq #elif defined(VFALLS_IMPL)
6759ac0c16Sdavemq char cpu_module_name[] = "SUNW,UltraSPARC-T2+";
6859ac0c16Sdavemq #endif
6944961713Sgirish 
7044961713Sgirish /*
7159ac0c16Sdavemq  * Hypervisor services information for the NIAGARA2 and Victoria Falls
7259ac0c16Sdavemq  * CPU module
7344961713Sgirish  */
7459ac0c16Sdavemq static boolean_t cpu_hsvc_available = B_TRUE;
7559ac0c16Sdavemq static uint64_t cpu_sup_minor;		/* Supported minor number */
7659ac0c16Sdavemq #if defined(NIAGARA2_IMPL)
7759ac0c16Sdavemq static hsvc_info_t cpu_hsvc = {
7844961713Sgirish 	HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA2_CPU, NIAGARA2_HSVC_MAJOR,
7944961713Sgirish 	NIAGARA2_HSVC_MINOR, cpu_module_name
8044961713Sgirish };
8159ac0c16Sdavemq #elif defined(VFALLS_IMPL)
8259ac0c16Sdavemq static hsvc_info_t cpu_hsvc = {
8359ac0c16Sdavemq 	HSVC_REV_1, NULL, HSVC_GROUP_VFALLS_CPU, VFALLS_HSVC_MAJOR,
8459ac0c16Sdavemq 	VFALLS_HSVC_MINOR, cpu_module_name
8559ac0c16Sdavemq };
8659ac0c16Sdavemq #endif
8744961713Sgirish 
8844961713Sgirish void
8944961713Sgirish cpu_setup(void)
9044961713Sgirish {
9144961713Sgirish 	extern int mmu_exported_pagesize_mask;
9244961713Sgirish 	extern int cpc_has_overflow_intr;
93aaa10e67Sha137994 	extern size_t contig_mem_prealloc_base;
9444961713Sgirish 	int status;
9544961713Sgirish 
9644961713Sgirish 	/*
9744961713Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
9844961713Sgirish 	 * services.
9944961713Sgirish 	 */
10059ac0c16Sdavemq 	status = hsvc_register(&cpu_hsvc, &cpu_sup_minor);
10144961713Sgirish 	if (status != 0) {
10244961713Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
10344961713Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
10459ac0c16Sdavemq 		    cpu_hsvc.hsvc_modname, cpu_hsvc.hsvc_group,
10559ac0c16Sdavemq 		    cpu_hsvc.hsvc_major, cpu_hsvc.hsvc_minor, status);
10659ac0c16Sdavemq 		cpu_hsvc_available = B_FALSE;
10744961713Sgirish 	}
10844961713Sgirish 
10944961713Sgirish 	/*
11044961713Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
11144961713Sgirish 	 * routine.
11244961713Sgirish 	 */
11344961713Sgirish 	cpu_setup_common(NULL);
11444961713Sgirish 
11544961713Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
11644961713Sgirish 
11744961713Sgirish 	if ((mmu_exported_pagesize_mask &
11844961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
11944961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
12044961713Sgirish 		cmn_err(CE_PANIC, "machine description"
12144961713Sgirish 		    " does not have required sun4v page sizes"
12244961713Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
12344961713Sgirish 		    mmu_exported_pagesize_mask);
12444961713Sgirish 
12544961713Sgirish 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 | AV_SPARC_ASI_BLK_INIT;
12644961713Sgirish 
12744961713Sgirish 	/*
12844961713Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
12944961713Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
13044961713Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
13144961713Sgirish 	 * and must never be mapped. In addition, software must not use
13244961713Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
13344961713Sgirish 	 * avoid problems with prefetching into the VA hole.
13444961713Sgirish 	 */
13544961713Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
13644961713Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
13744961713Sgirish 
13844961713Sgirish 	/*
13944961713Sgirish 	 * Niagara2 has a performance counter overflow interrupt
14044961713Sgirish 	 */
14144961713Sgirish 	cpc_has_overflow_intr = 1;
142102033aaSdp78419 
143102033aaSdp78419 	/*
144102033aaSdp78419 	 * Enable 4M pages for OOB.
145102033aaSdp78419 	 */
146102033aaSdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
147102033aaSdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
148102033aaSdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
149aaa10e67Sha137994 
150aaa10e67Sha137994 	contig_mem_prealloc_base = NIAGARA2_PREALLOC_BASE;
15144961713Sgirish }
15244961713Sgirish 
15344961713Sgirish /*
15444961713Sgirish  * Set the magic constants of the implementation.
15544961713Sgirish  */
15644961713Sgirish void
15744961713Sgirish cpu_fiximp(struct cpu_node *cpunode)
15844961713Sgirish {
15944961713Sgirish 	/*
16044961713Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
16144961713Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
16244961713Sgirish 	 * size, linesize.
16344961713Sgirish 	 */
16444961713Sgirish 	if (cpunode->ecache_size == 0)
16544961713Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
16644961713Sgirish 	if (cpunode->ecache_linesize == 0)
16744961713Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
16844961713Sgirish 	if (cpunode->ecache_associativity == 0)
16944961713Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
17044961713Sgirish }
17144961713Sgirish 
17244961713Sgirish void
173459190a5Srsmaeda cpu_map_exec_units(struct cpu *cp)
17444961713Sgirish {
175459190a5Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
17644961713Sgirish 
17744961713Sgirish 	/*
178fb2f18f8Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
179459190a5Srsmaeda 	 * the execution unit sharing information from the MD. They
180459190a5Srsmaeda 	 * default to the CPU id in the absence of such information.
18144961713Sgirish 	 */
18244961713Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
18344961713Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
18444961713Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
18544961713Sgirish 
186fb2f18f8Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
187fb2f18f8Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
188fb2f18f8Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
189fb2f18f8Sesaxe 
190fb2f18f8Sesaxe 	/*
191fb2f18f8Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
192fb2f18f8Sesaxe 	 */
193fb2f18f8Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
19459ac0c16Sdavemq 
19559ac0c16Sdavemq 	/*
19659ac0c16Sdavemq 	 * The cpu_chip field is initialized based on the information
19759ac0c16Sdavemq 	 * in the MD and assume that all cpus within a chip
19859ac0c16Sdavemq 	 * share the same L2 cache. If no such info is available, we
19959ac0c16Sdavemq 	 * set the cpu to belong to the defacto chip 0.
20059ac0c16Sdavemq 	 */
201*ce8eb11aSdp78419 	cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
202*ce8eb11aSdp78419 	if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
203*ce8eb11aSdp78419 		cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
204459190a5Srsmaeda }
205459190a5Srsmaeda 
20659ac0c16Sdavemq static int cpucnt;
207459190a5Srsmaeda 
208459190a5Srsmaeda void
209459190a5Srsmaeda cpu_init_private(struct cpu *cp)
210459190a5Srsmaeda {
211459190a5Srsmaeda 	extern void niagara_kstat_init(void);
212fb2f18f8Sesaxe 
21344961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
214459190a5Srsmaeda 
215459190a5Srsmaeda 	cpu_map_exec_units(cp);
216459190a5Srsmaeda 
21759ac0c16Sdavemq 	if ((cpucnt++ == 0) && (cpu_hsvc_available == B_TRUE))
21859ac0c16Sdavemq 		(void) niagara_kstat_init();
21944961713Sgirish }
22044961713Sgirish 
22144961713Sgirish /*ARGSUSED*/
22244961713Sgirish void
22344961713Sgirish cpu_uninit_private(struct cpu *cp)
22444961713Sgirish {
225459190a5Srsmaeda 	extern void niagara_kstat_fini(void);
22644961713Sgirish 
22744961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
22859ac0c16Sdavemq 	if ((--cpucnt == 0) && (cpu_hsvc_available == B_TRUE))
22959ac0c16Sdavemq 		(void) niagara_kstat_fini();
23044961713Sgirish }
23144961713Sgirish 
23244961713Sgirish /*
23344961713Sgirish  * On Niagara2, any flush will cause all preceding stores to be
23444961713Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
23544961713Sgirish  * the address is ignored, so we always flush address 0.
23644961713Sgirish  */
23744961713Sgirish /*ARGSUSED*/
23844961713Sgirish void
23944961713Sgirish dtrace_flush_sec(uintptr_t addr)
24044961713Sgirish {
24144961713Sgirish 	doflush(0);
24244961713Sgirish }
24344961713Sgirish 
24444961713Sgirish /*
24544961713Sgirish  * Trapstat support for Niagara2 processor
24644961713Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
24744961713Sgirish  * enabled no TSB hit information will be available. Therefore setting
24844961713Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
24944961713Sgirish  */
25044961713Sgirish int
25144961713Sgirish cpu_trapstat_conf(int cmd)
25244961713Sgirish {
25344961713Sgirish 	int status = 0;
25444961713Sgirish 
25544961713Sgirish 	switch (cmd) {
25644961713Sgirish 	case CPU_TSTATCONF_INIT:
25744961713Sgirish 	case CPU_TSTATCONF_FINI:
25844961713Sgirish 	case CPU_TSTATCONF_ENABLE:
25944961713Sgirish 	case CPU_TSTATCONF_DISABLE:
26044961713Sgirish 		break;
26144961713Sgirish 	default:
26244961713Sgirish 		status = EINVAL;
26344961713Sgirish 		break;
26444961713Sgirish 	}
26544961713Sgirish 	return (status);
26644961713Sgirish }
26744961713Sgirish 
26844961713Sgirish void
26944961713Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
27044961713Sgirish {
27144961713Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
27244961713Sgirish 	int	i;
27344961713Sgirish 
27444961713Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
27544961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
27644961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
27744961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
27844961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
27944961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
28044961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
28144961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
28244961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
28344961713Sgirish 	}
28444961713Sgirish }
285102033aaSdp78419 
286*ce8eb11aSdp78419 /*
287*ce8eb11aSdp78419  * Page coloring support for hashed cache index mode
288*ce8eb11aSdp78419  */
289*ce8eb11aSdp78419 
290*ce8eb11aSdp78419 /*
291*ce8eb11aSdp78419  * Node id bits from machine description (MD).  Node id distinguishes
292*ce8eb11aSdp78419  * local versus remote memory. Because of MPO, page allocation does
293*ce8eb11aSdp78419  * not cross node boundaries. Therefore, remove the node id bits from
294*ce8eb11aSdp78419  * the color, since they are fixed. Either bit 30, or 31:30 in
295*ce8eb11aSdp78419  * Victoria Falls processors.
296*ce8eb11aSdp78419  * The number of node id bits is always 0 in Niagara2.
297*ce8eb11aSdp78419  */
298*ce8eb11aSdp78419 typedef struct n2color {
299*ce8eb11aSdp78419 	uchar_t nnbits;	/* number of node id bits */
300*ce8eb11aSdp78419 	uchar_t nnmask; /* mask for node id bits */
301*ce8eb11aSdp78419 	uchar_t	lomask;	/* mask for bits below node id */
302*ce8eb11aSdp78419 	uchar_t lobits;	/* number of bits below node id */
303*ce8eb11aSdp78419 } n2color_t;
304*ce8eb11aSdp78419 
305*ce8eb11aSdp78419 n2color_t n2color[MMU_PAGE_SIZES];
306*ce8eb11aSdp78419 static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
307*ce8eb11aSdp78419 
308*ce8eb11aSdp78419 /*
309*ce8eb11aSdp78419  * Remove node id bits from color bits 32:28.
310*ce8eb11aSdp78419  * This will reduce the number of colors.
311*ce8eb11aSdp78419  * No change if number of node bits is zero.
312*ce8eb11aSdp78419  */
313*ce8eb11aSdp78419 static inline uint_t
314*ce8eb11aSdp78419 n2_hash2color(uint_t color, uchar_t szc)
315*ce8eb11aSdp78419 {
316*ce8eb11aSdp78419 	n2color_t m = n2color[szc];
317*ce8eb11aSdp78419 
318*ce8eb11aSdp78419 	if (m.nnbits > 0) {
319*ce8eb11aSdp78419 		color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
320*ce8eb11aSdp78419 		ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
321*ce8eb11aSdp78419 	}
322*ce8eb11aSdp78419 
323*ce8eb11aSdp78419 	return (color);
324*ce8eb11aSdp78419 }
325*ce8eb11aSdp78419 
326*ce8eb11aSdp78419 /*
327*ce8eb11aSdp78419  * Restore node id bits into page color.
328*ce8eb11aSdp78419  * This will increase the number of colors to match N2.
329*ce8eb11aSdp78419  * No change if number of node bits is zero.
330*ce8eb11aSdp78419  */
331*ce8eb11aSdp78419 static inline uint_t
332*ce8eb11aSdp78419 n2_color2hash(uint_t color, uchar_t szc, uint_t node)
333*ce8eb11aSdp78419 {
334*ce8eb11aSdp78419 	n2color_t m = n2color[szc];
335*ce8eb11aSdp78419 
336*ce8eb11aSdp78419 	if (m.nnbits > 0) {
337*ce8eb11aSdp78419 		color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
338*ce8eb11aSdp78419 		color |= (node & m.nnmask) << m.lobits;
339*ce8eb11aSdp78419 	}
340*ce8eb11aSdp78419 
341*ce8eb11aSdp78419 	return (color);
342*ce8eb11aSdp78419 }
343*ce8eb11aSdp78419 
344102033aaSdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
345*ce8eb11aSdp78419 
346*ce8eb11aSdp78419 /*
347*ce8eb11aSdp78419  * iterator NULL means pfn is VA, do not adjust ra_to_pa
348*ce8eb11aSdp78419  * iterator (-1) means pfn is RA, need to convert to PA
349*ce8eb11aSdp78419  * iterator non-null means pfn is RA, use ra_to_pa
350*ce8eb11aSdp78419  */
351102033aaSdp78419 uint_t
352*ce8eb11aSdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
353*ce8eb11aSdp78419 {
354*ce8eb11aSdp78419 	mem_node_iterator_t *it = cookie;
355*ce8eb11aSdp78419 	uint_t color;
356*ce8eb11aSdp78419 
357*ce8eb11aSdp78419 	ASSERT(szc <= TTE256M);
358*ce8eb11aSdp78419 
359*ce8eb11aSdp78419 	if (it == ((mem_node_iterator_t *)(-1))) {
360*ce8eb11aSdp78419 		pfn = plat_rapfn_to_papfn(pfn);
361*ce8eb11aSdp78419 	} else if (it != NULL) {
362*ce8eb11aSdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
363*ce8eb11aSdp78419 		pfn = pfn + it->mi_ra_to_pa;
364*ce8eb11aSdp78419 	}
365*ce8eb11aSdp78419 	pfn = PFN_BASE(pfn, szc);
366*ce8eb11aSdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
367*ce8eb11aSdp78419 	if (szc < TTE4M) {
368*ce8eb11aSdp78419 		/* 19:18 */
369*ce8eb11aSdp78419 		color = (color << 2) | ((pfn >> 5) & 0x3);
370*ce8eb11aSdp78419 		if (szc > TTE64K)
371*ce8eb11aSdp78419 			color >>= 1;    /* 19 */
372*ce8eb11aSdp78419 	}
373*ce8eb11aSdp78419 	return (n2_hash2color(color, szc));
374*ce8eb11aSdp78419 }
375*ce8eb11aSdp78419 
376*ce8eb11aSdp78419 static uint_t
377*ce8eb11aSdp78419 page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
378102033aaSdp78419 {
379102033aaSdp78419 	uint_t color;
380102033aaSdp78419 
381102033aaSdp78419 	ASSERT(szc <= TTE256M);
382102033aaSdp78419 
383*ce8eb11aSdp78419 	papfn = PFN_BASE(papfn, szc);
384*ce8eb11aSdp78419 	color = ((papfn >> 15) ^ papfn) & 0x1f;
385*ce8eb11aSdp78419 	if (szc < TTE4M) {
386*ce8eb11aSdp78419 		/* 19:18 */
387*ce8eb11aSdp78419 		color = (color << 2) | ((papfn >> 5) & 0x3);
388*ce8eb11aSdp78419 		if (szc > TTE64K)
389*ce8eb11aSdp78419 			color >>= 1;    /* 19 */
390*ce8eb11aSdp78419 	}
391102033aaSdp78419 	return (color);
392102033aaSdp78419 }
393102033aaSdp78419 
394102033aaSdp78419 #if TTE256M != 5
395102033aaSdp78419 #error TTE256M is not 5
396102033aaSdp78419 #endif
397102033aaSdp78419 
398102033aaSdp78419 uint_t
399102033aaSdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
400102033aaSdp78419 {
401102033aaSdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
402102033aaSdp78419 	ASSERT(szc < TTE256M);
403*ce8eb11aSdp78419 	mask = n2_color2hash(mask, szc, 0);
404102033aaSdp78419 	mask &= ni2_color_masks[szc];
405*ce8eb11aSdp78419 	if (szc == TTE64K || szc == TTE512K)
406*ce8eb11aSdp78419 		mask >>= 1;
407*ce8eb11aSdp78419 	return (n2_hash2color(mask, szc + 1));
408102033aaSdp78419 }
409102033aaSdp78419 
410102033aaSdp78419 uint_t
411102033aaSdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
412102033aaSdp78419 {
413102033aaSdp78419 	ASSERT(szc < TTE256M);
414*ce8eb11aSdp78419 	color = n2_color2hash(color, szc, 0);
415*ce8eb11aSdp78419 	if (szc == TTE64K || szc == TTE512K)
416*ce8eb11aSdp78419 		color >>= 1;
417*ce8eb11aSdp78419 	return (n2_hash2color(color, szc + 1));
418102033aaSdp78419 }
419102033aaSdp78419 
420102033aaSdp78419 uint_t
421102033aaSdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
422102033aaSdp78419 {
423*ce8eb11aSdp78419 	uint_t s;
424ef29e907Ssusans 	ASSERT(nszc >= szc);
425102033aaSdp78419 	ASSERT(nszc <= TTE256M);
426102033aaSdp78419 
427*ce8eb11aSdp78419 	s = nhbits[szc] - n2color[szc].nnbits;
428*ce8eb11aSdp78419 	s -= nhbits[nszc] - n2color[nszc].nnbits;
429102033aaSdp78419 
430*ce8eb11aSdp78419 	return (s);
431102033aaSdp78419 }
432102033aaSdp78419 
433*ce8eb11aSdp78419 uint_t
434*ce8eb11aSdp78419 page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
435*ce8eb11aSdp78419 {
436*ce8eb11aSdp78419 	uint_t color;
437*ce8eb11aSdp78419 
438*ce8eb11aSdp78419 	ASSERT(nszc > szc);
439*ce8eb11aSdp78419 	ASSERT(nszc <= TTE256M);
440*ce8eb11aSdp78419 	ncolor = n2_color2hash(ncolor, nszc, 0);
441*ce8eb11aSdp78419 	color = ncolor << (nhbits[szc] - nhbits[nszc]);
442*ce8eb11aSdp78419 	color = n2_hash2color(color, szc);
443*ce8eb11aSdp78419 	return (color);
444*ce8eb11aSdp78419 }
445*ce8eb11aSdp78419 
446*ce8eb11aSdp78419 #define	PAPFN_2_MNODE(pfn) \
447*ce8eb11aSdp78419 	(((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
448*ce8eb11aSdp78419 
449102033aaSdp78419 /*ARGSUSED*/
450102033aaSdp78419 pfn_t
451102033aaSdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
452*ce8eb11aSdp78419     uint_t ceq_mask, uint_t color_mask, void *cookie)
453102033aaSdp78419 {
454*ce8eb11aSdp78419 	mem_node_iterator_t *it = cookie;
455102033aaSdp78419 	pfn_t pstep = PNUM_SIZE(szc);
456102033aaSdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
457102033aaSdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
458*ce8eb11aSdp78419 	uint_t pfnmn;
459102033aaSdp78419 
460102033aaSdp78419 	ASSERT((color & ~ceq_mask) == 0);
461*ce8eb11aSdp78419 	ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
462102033aaSdp78419 
463*ce8eb11aSdp78419 	/* convert RA to PA for accurate color calculation */
464*ce8eb11aSdp78419 	if (it->mi_init) {
465*ce8eb11aSdp78419 		/* first call after it, so cache these values */
466*ce8eb11aSdp78419 		it->mi_hash_ceq_mask =
467*ce8eb11aSdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
468*ce8eb11aSdp78419 		it->mi_hash_color =
469*ce8eb11aSdp78419 		    n2_color2hash(color, szc, it->mi_mnode);
470*ce8eb11aSdp78419 		it->mi_init = 0;
471*ce8eb11aSdp78419 	} else {
472*ce8eb11aSdp78419 		ASSERT(it->mi_hash_ceq_mask ==
473*ce8eb11aSdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
474*ce8eb11aSdp78419 		ASSERT(it->mi_hash_color ==
475*ce8eb11aSdp78419 		    n2_color2hash(color, szc, it->mi_mnode));
476*ce8eb11aSdp78419 	}
477*ce8eb11aSdp78419 	ceq_mask = it->mi_hash_ceq_mask;
478*ce8eb11aSdp78419 	color = it->mi_hash_color;
479*ce8eb11aSdp78419 	pfn += it->mi_ra_to_pa;
480*ce8eb11aSdp78419 
481*ce8eb11aSdp78419 	/* restart here when we switch memblocks */
482*ce8eb11aSdp78419 next_mem_block:
483*ce8eb11aSdp78419 	if (szc <= TTE64K) {
484*ce8eb11aSdp78419 		pfnmn = PAPFN_2_MNODE(pfn);
485*ce8eb11aSdp78419 	}
486*ce8eb11aSdp78419 	if (((page_papfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0 &&
487*ce8eb11aSdp78419 	    (szc > TTE64K || pfnmn == it->mi_mnode)) {
488102033aaSdp78419 
489102033aaSdp78419 		/* we start from the page with correct color */
490102033aaSdp78419 		if (szc >= TTE512K) {
491102033aaSdp78419 			if (szc >= TTE4M) {
492102033aaSdp78419 				/* page color is PA[32:28] */
493102033aaSdp78419 				pfn_ceq_mask = ceq_mask << 15;
494102033aaSdp78419 			} else {
495102033aaSdp78419 				/* page color is PA[32:28].PA[19:19] */
496102033aaSdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
497102033aaSdp78419 				    ((ceq_mask >> 1) << 15);
498102033aaSdp78419 			}
499*ce8eb11aSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
500*ce8eb11aSdp78419 			goto done;
501102033aaSdp78419 		} else {
502102033aaSdp78419 			/*
503102033aaSdp78419 			 * We deal 64K or 8K page. Check if we could the
504102033aaSdp78419 			 * satisfy the request without changing PA[32:28]
505102033aaSdp78419 			 */
506102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
507*ce8eb11aSdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
508102033aaSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
509102033aaSdp78419 
510102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
511*ce8eb11aSdp78419 				goto done;
512102033aaSdp78419 
513102033aaSdp78419 			/*
514102033aaSdp78419 			 * for next pfn we have to change bits PA[32:28]
515102033aaSdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
516102033aaSdp78419 			 */
517102033aaSdp78419 			npfn = (pfn >> 15) << 15;
518102033aaSdp78419 			npfn |= (ceq_mask & color & 3) << 5;
519102033aaSdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
520102033aaSdp78419 			    (ceq_mask & 0x1c) << 13;
521*ce8eb11aSdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
522102033aaSdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
523102033aaSdp78419 
524102033aaSdp78419 			/*
525102033aaSdp78419 			 * set bits PA[17:13] to match the color
526102033aaSdp78419 			 */
527*ce8eb11aSdp78419 			npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
528*ce8eb11aSdp78419 			goto done;
529102033aaSdp78419 		}
530102033aaSdp78419 	}
531102033aaSdp78419 
532102033aaSdp78419 	/*
533102033aaSdp78419 	 * we start from the page with incorrect color - rare case
534102033aaSdp78419 	 */
535102033aaSdp78419 	if (szc >= TTE512K) {
536102033aaSdp78419 		if (szc >= TTE4M) {
537102033aaSdp78419 			/* page color is in bits PA[32:28] */
538102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
539102033aaSdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
540102033aaSdp78419 		} else {
541102033aaSdp78419 			/* try get the right color by changing bit PA[19:19] */
542102033aaSdp78419 			npfn = pfn + pstep;
543*ce8eb11aSdp78419 			if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
544102033aaSdp78419 			    ceq_mask) == 0)
545*ce8eb11aSdp78419 				goto done;
546102033aaSdp78419 
547102033aaSdp78419 			/* page color is PA[32:28].PA[19:19] */
548102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
549102033aaSdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
550102033aaSdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
551102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
552102033aaSdp78419 		}
553102033aaSdp78419 
554102033aaSdp78419 		while (npfn <= pfn) {
555102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
556102033aaSdp78419 		}
557*ce8eb11aSdp78419 		goto done;
558102033aaSdp78419 	}
559102033aaSdp78419 
560102033aaSdp78419 	/*
561102033aaSdp78419 	 *  We deal 64K or 8K page of incorrect color.
562102033aaSdp78419 	 * Try correcting color without changing PA[32:28]
563102033aaSdp78419 	 */
564102033aaSdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
565102033aaSdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
566*ce8eb11aSdp78419 	if (pfnmn == it->mi_mnode) {
567102033aaSdp78419 		npfn = (pfn & ~(pfn_t)0x7f);
568102033aaSdp78419 		npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
569102033aaSdp78419 		npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
570102033aaSdp78419 
571*ce8eb11aSdp78419 		if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
572*ce8eb11aSdp78419 		    ceq_mask) == 0) {
573102033aaSdp78419 			/* the color is fixed - find the next page */
574*ce8eb11aSdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
575102033aaSdp78419 			while (npfn <= pfn) {
576*ce8eb11aSdp78419 				npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
577*ce8eb11aSdp78419 				    mask);
578102033aaSdp78419 			}
579102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
580*ce8eb11aSdp78419 				goto done;
581*ce8eb11aSdp78419 		}
582102033aaSdp78419 	}
583102033aaSdp78419 
584102033aaSdp78419 	/* to fix the color need to touch PA[32:28] */
585102033aaSdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
586102033aaSdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
587*ce8eb11aSdp78419 
588*ce8eb11aSdp78419 	/* fix mnode if input pfn is in the wrong mnode. */
589*ce8eb11aSdp78419 	if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
590*ce8eb11aSdp78419 		npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
591*ce8eb11aSdp78419 		    it->mi_mnode_pfn_shift;
592*ce8eb11aSdp78419 	}
593*ce8eb11aSdp78419 
594102033aaSdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
595*ce8eb11aSdp78419 	tmpmask |= it->mi_mnode_pfn_mask;
596102033aaSdp78419 
597102033aaSdp78419 	while (npfn <= pfn) {
598102033aaSdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
599102033aaSdp78419 	}
600102033aaSdp78419 
601102033aaSdp78419 	/* set bits PA[19:13] to match the color */
602102033aaSdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
603102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
604102033aaSdp78419 
605*ce8eb11aSdp78419 done:
606*ce8eb11aSdp78419 	ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
607*ce8eb11aSdp78419 	ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
608*ce8eb11aSdp78419 
609*ce8eb11aSdp78419 	/* PA to RA */
610*ce8eb11aSdp78419 	npfn -= it->mi_ra_to_pa;
611*ce8eb11aSdp78419 
612*ce8eb11aSdp78419 	/* check for possible memblock switch */
613*ce8eb11aSdp78419 	if (npfn > it->mi_mblock_end) {
614*ce8eb11aSdp78419 		pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, it, 0);
615*ce8eb11aSdp78419 		if (pfn == (pfn_t)-1)
616*ce8eb11aSdp78419 			return (pfn);
617*ce8eb11aSdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
618*ce8eb11aSdp78419 		pfn += it->mi_ra_to_pa;
619*ce8eb11aSdp78419 		goto next_mem_block;
620*ce8eb11aSdp78419 	}
621102033aaSdp78419 
622102033aaSdp78419 	return (npfn);
623102033aaSdp78419 }
624102033aaSdp78419 
625102033aaSdp78419 /*
626102033aaSdp78419  * init page coloring
627*ce8eb11aSdp78419  * VF encodes node_id for an L-group in either bit 30 or 31:30,
628*ce8eb11aSdp78419  * which effectively reduces the number of colors available per mnode.
629102033aaSdp78419  */
630102033aaSdp78419 void
631102033aaSdp78419 page_coloring_init_cpu()
632102033aaSdp78419 {
633102033aaSdp78419 	int i;
634*ce8eb11aSdp78419 	uchar_t id;
635*ce8eb11aSdp78419 	uchar_t lo;
636*ce8eb11aSdp78419 	uchar_t hi;
637*ce8eb11aSdp78419 	n2color_t m;
638*ce8eb11aSdp78419 	mem_node_iterator_t it;
639*ce8eb11aSdp78419 	static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
640102033aaSdp78419 
641*ce8eb11aSdp78419 	(void) plat_mem_node_iterator_init(0, 0, &it, 1);
642*ce8eb11aSdp78419 	for (i = 0; i < mmu_page_sizes; i++) {
643*ce8eb11aSdp78419 		memset(&m, 0, sizeof (m));
644*ce8eb11aSdp78419 		id = it.mi_mnode_pfn_mask >> 15;	/* node id mask */
645*ce8eb11aSdp78419 		id &= idmask[i];
646*ce8eb11aSdp78419 		lo = lowbit(id);
647*ce8eb11aSdp78419 		if (lo > 0) {
648*ce8eb11aSdp78419 			hi = highbit(id);
649*ce8eb11aSdp78419 			m.nnbits = hi - lo + 1;
650*ce8eb11aSdp78419 			m.nnmask = (1 << m.nnbits) - 1;
651*ce8eb11aSdp78419 			lo += nhbits[i] - 5;
652*ce8eb11aSdp78419 			m.lomask = (1 << (lo - 1)) - 1;
653*ce8eb11aSdp78419 			m.lobits = lo - 1;
654*ce8eb11aSdp78419 		}
655*ce8eb11aSdp78419 		hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
656*ce8eb11aSdp78419 		n2color[i] = m;
657102033aaSdp78419 	}
658102033aaSdp78419 }
659fe70c9cfSdp78419 
660fe70c9cfSdp78419 /*
661fe70c9cfSdp78419  * group colorequiv colors on N2 by low order bits of the color first
662fe70c9cfSdp78419  */
663fe70c9cfSdp78419 void
664fe70c9cfSdp78419 page_set_colorequiv_arr_cpu(void)
665fe70c9cfSdp78419 {
666fe70c9cfSdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
667fe70c9cfSdp78419 
668*ce8eb11aSdp78419 	nequiv_shades_log2[1] -= n2color[1].nnbits;
669fe70c9cfSdp78419 	if (colorequiv > 1) {
670fe70c9cfSdp78419 		int i;
671fe70c9cfSdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
672fe70c9cfSdp78419 
673fe70c9cfSdp78419 		if (sv_a > 15)
674fe70c9cfSdp78419 			sv_a = 15;
675fe70c9cfSdp78419 
676fe70c9cfSdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
677fe70c9cfSdp78419 			uint_t colors;
678fe70c9cfSdp78419 			uint_t a = sv_a;
679fe70c9cfSdp78419 
680fe70c9cfSdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
681fe70c9cfSdp78419 				continue;
682fe70c9cfSdp78419 			while ((colors >> a) == 0)
683fe70c9cfSdp78419 				a--;
684fe70c9cfSdp78419 			if (a > (colorequivszc[i] & 0xf) +
685fe70c9cfSdp78419 			    (colorequivszc[i] >> 4)) {
686fe70c9cfSdp78419 				if (a <= nequiv_shades_log2[i]) {
68759ac0c16Sdavemq 					colorequivszc[i] = (uchar_t)a;
688fe70c9cfSdp78419 				} else {
689fe70c9cfSdp78419 					colorequivszc[i] =
690fe70c9cfSdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
691fe70c9cfSdp78419 					    nequiv_shades_log2[i];
692fe70c9cfSdp78419 				}
693fe70c9cfSdp78419 			}
694fe70c9cfSdp78419 		}
695fe70c9cfSdp78419 	}
696fe70c9cfSdp78419 }
697