xref: /illumos-gate/usr/src/uts/sun4v/cpu/niagara2.c (revision 6a634c9d)
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 /*
23*c4c6ba57SVivek Gavaskar  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2444961713Sgirish  */
2544961713Sgirish 
2644961713Sgirish #include <sys/types.h>
2744961713Sgirish #include <sys/systm.h>
2844961713Sgirish #include <sys/archsystm.h>
2944961713Sgirish #include <sys/machparam.h>
3044961713Sgirish #include <sys/machsystm.h>
3144961713Sgirish #include <sys/cpu.h>
3244961713Sgirish #include <sys/elf_SPARC.h>
3344961713Sgirish #include <vm/hat_sfmmu.h>
3444961713Sgirish #include <vm/page.h>
35102033aaSdp #include <vm/vm_dep.h>
3644961713Sgirish #include <sys/cpuvar.h>
3744961713Sgirish #include <sys/async.h>
3844961713Sgirish #include <sys/cmn_err.h>
3944961713Sgirish #include <sys/debug.h>
4044961713Sgirish #include <sys/dditypes.h>
4144961713Sgirish #include <sys/sunddi.h>
4244961713Sgirish #include <sys/cpu_module.h>
4344961713Sgirish #include <sys/prom_debug.h>
4444961713Sgirish #include <sys/vmsystm.h>
4544961713Sgirish #include <sys/prom_plat.h>
4644961713Sgirish #include <sys/sysmacros.h>
4744961713Sgirish #include <sys/intreg.h>
4844961713Sgirish #include <sys/machtrap.h>
4944961713Sgirish #include <sys/ontrap.h>
5044961713Sgirish #include <sys/ivintr.h>
5144961713Sgirish #include <sys/atomic.h>
5244961713Sgirish #include <sys/panic.h>
5344961713Sgirish #include <sys/dtrace.h>
5444961713Sgirish #include <sys/simulate.h>
5544961713Sgirish #include <sys/fault.h>
5644961713Sgirish #include <sys/niagara2regs.h>
5744961713Sgirish #include <sys/hsvc.h>
5844961713Sgirish #include <sys/trapstat.h>
59575a7426Spt #include <sys/mutex_impl.h>
6044961713Sgirish 
6144961713Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
6259ac0c16Sdavemq #if defined(NIAGARA2_IMPL)
6344961713Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
6459ac0c16Sdavemq #elif defined(VFALLS_IMPL)
6559ac0c16Sdavemq char cpu_module_name[] = "SUNW,UltraSPARC-T2+";
664df55fdeSJanie Lu #elif defined(KT_IMPL)
67*c4c6ba57SVivek Gavaskar char cpu_module_name[] = "SPARC-T3";
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 };
864df55fdeSJanie Lu #elif defined(KT_IMPL)
874df55fdeSJanie Lu static hsvc_info_t cpu_hsvc = {
884df55fdeSJanie Lu 	HSVC_REV_1, NULL, HSVC_GROUP_KT_CPU, KT_HSVC_MAJOR,
894df55fdeSJanie Lu 	KT_HSVC_MINOR, cpu_module_name
904df55fdeSJanie Lu };
9159ac0c16Sdavemq #endif
9244961713Sgirish 
9344961713Sgirish void
cpu_setup(void)9444961713Sgirish cpu_setup(void)
9544961713Sgirish {
9644961713Sgirish 	extern int mmu_exported_pagesize_mask;
9744961713Sgirish 	extern int cpc_has_overflow_intr;
987ec363dcSwh 	extern size_t contig_mem_prealloc_base_size;
9944961713Sgirish 	int status;
10044961713Sgirish 
10144961713Sgirish 	/*
10244961713Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
10344961713Sgirish 	 * services.
10444961713Sgirish 	 */
10559ac0c16Sdavemq 	status = hsvc_register(&cpu_hsvc, &cpu_sup_minor);
10644961713Sgirish 	if (status != 0) {
10744961713Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
10844961713Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
10959ac0c16Sdavemq 		    cpu_hsvc.hsvc_modname, cpu_hsvc.hsvc_group,
11059ac0c16Sdavemq 		    cpu_hsvc.hsvc_major, cpu_hsvc.hsvc_minor, status);
11159ac0c16Sdavemq 		cpu_hsvc_available = B_FALSE;
11244961713Sgirish 	}
11344961713Sgirish 
11444961713Sgirish 	/*
11544961713Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
11644961713Sgirish 	 * routine.
11744961713Sgirish 	 */
11844961713Sgirish 	cpu_setup_common(NULL);
11944961713Sgirish 
1204df55fdeSJanie Lu 	/*
1214df55fdeSJanie Lu 	 * Initialize the cpu_hwcap_flags for N2 and VF if it is not already
1224df55fdeSJanie Lu 	 * set in cpu_setup_common() by the hwcap MD info. Note that this MD
1234df55fdeSJanie Lu 	 * info may not be available for N2/VF.
1244df55fdeSJanie Lu 	 */
1254df55fdeSJanie Lu 	if (cpu_hwcap_flags == 0) {
1264df55fdeSJanie Lu #ifdef KT_IMPL
1274df55fdeSJanie Lu 		/*
1284df55fdeSJanie Lu 		 * This should not happen since hwcap MD info is always
1294df55fdeSJanie Lu 		 * available for KT platforms.
1304df55fdeSJanie Lu 		 */
1314df55fdeSJanie Lu 		ASSERT(cpu_hwcap_flags != 0);	/* panic in DEBUG mode */
1324df55fdeSJanie Lu 		cpu_hwcap_flags |= AV_SPARC_VIS3 | AV_SPARC_HPC | AV_SPARC_FMAF;
1334df55fdeSJanie Lu #endif /* KT_IMPL */
1344df55fdeSJanie Lu 		cpu_hwcap_flags |= AV_SPARC_VIS | AV_SPARC_VIS2 |
1354df55fdeSJanie Lu 		    AV_SPARC_ASI_BLK_INIT | AV_SPARC_POPC;
1364df55fdeSJanie Lu 	}
1374df55fdeSJanie Lu 
13844961713Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
13944961713Sgirish 
14044961713Sgirish 	if ((mmu_exported_pagesize_mask &
14144961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
14244961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
14344961713Sgirish 		cmn_err(CE_PANIC, "machine description"
14444961713Sgirish 		    " does not have required sun4v page sizes"
14544961713Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
14644961713Sgirish 		    mmu_exported_pagesize_mask);
14744961713Sgirish 
14844961713Sgirish 	/*
14944961713Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
15044961713Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
15144961713Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
15244961713Sgirish 	 * and must never be mapped. In addition, software must not use
15344961713Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
15444961713Sgirish 	 * avoid problems with prefetching into the VA hole.
15544961713Sgirish 	 */
15644961713Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
15744961713Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
15844961713Sgirish 
15944961713Sgirish 	/*
16044961713Sgirish 	 * Niagara2 has a performance counter overflow interrupt
16144961713Sgirish 	 */
16244961713Sgirish 	cpc_has_overflow_intr = 1;
163102033aaSdp 
164102033aaSdp 	/*
165102033aaSdp 	 * Enable 4M pages for OOB.
166102033aaSdp 	 */
167102033aaSdp 	max_uheap_lpsize = MMU_PAGESIZE4M;
168102033aaSdp 	max_ustack_lpsize = MMU_PAGESIZE4M;
169102033aaSdp 	max_privmap_lpsize = MMU_PAGESIZE4M;
170aaa10e67Sha 
1717ec363dcSwh #ifdef SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB
1727ec363dcSwh 	/*
1737ec363dcSwh 	 * Use CPU Makefile specific compile time define (if exists)
1747ec363dcSwh 	 * to add to the contig preallocation size.
1757ec363dcSwh 	 */
1767ec363dcSwh 	contig_mem_prealloc_base_size = MB(SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB);
1777ec363dcSwh #endif
17844961713Sgirish }
17944961713Sgirish 
18044961713Sgirish /*
18144961713Sgirish  * Set the magic constants of the implementation.
18244961713Sgirish  */
18344961713Sgirish void
cpu_fiximp(struct cpu_node * cpunode)18444961713Sgirish cpu_fiximp(struct cpu_node *cpunode)
18544961713Sgirish {
18644961713Sgirish 	/*
18744961713Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
18844961713Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
18944961713Sgirish 	 * size, linesize.
19044961713Sgirish 	 */
19144961713Sgirish 	if (cpunode->ecache_size == 0)
19244961713Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
19344961713Sgirish 	if (cpunode->ecache_linesize == 0)
19444961713Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
19544961713Sgirish 	if (cpunode->ecache_associativity == 0)
19644961713Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
19744961713Sgirish }
19844961713Sgirish 
19944961713Sgirish void
cpu_map_exec_units(struct cpu * cp)200459190a5Srsmaeda cpu_map_exec_units(struct cpu *cp)
20144961713Sgirish {
202459190a5Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
20344961713Sgirish 
20444961713Sgirish 	/*
205fb2f18f8Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
206459190a5Srsmaeda 	 * the execution unit sharing information from the MD. They
207459190a5Srsmaeda 	 * default to the CPU id in the absence of such information.
20844961713Sgirish 	 */
20944961713Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
21044961713Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
21144961713Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
21244961713Sgirish 
213fb2f18f8Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
214fb2f18f8Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
215fb2f18f8Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
216fb2f18f8Sesaxe 
217fb2f18f8Sesaxe 	/*
218fb2f18f8Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
219fb2f18f8Sesaxe 	 */
220fb2f18f8Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
22159ac0c16Sdavemq 
22259ac0c16Sdavemq 	/*
22359ac0c16Sdavemq 	 * The cpu_chip field is initialized based on the information
22459ac0c16Sdavemq 	 * in the MD and assume that all cpus within a chip
22559ac0c16Sdavemq 	 * share the same L2 cache. If no such info is available, we
22659ac0c16Sdavemq 	 * set the cpu to belong to the defacto chip 0.
22759ac0c16Sdavemq 	 */
228ce8eb11aSdp 	cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
229ce8eb11aSdp 	if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
230ce8eb11aSdp 		cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
231e853d8c3Sjc 
232e853d8c3Sjc 	cp->cpu_m.cpu_chip = cpunodes[cp->cpu_id].l2_cache_mapping;
233e853d8c3Sjc 	if (cp->cpu_m.cpu_chip == NO_L2_CACHE_MAPPING_FOUND)
234e853d8c3Sjc 		cp->cpu_m.cpu_chip = CPU_CHIPID_INVALID;
235459190a5Srsmaeda }
236459190a5Srsmaeda 
23759ac0c16Sdavemq static int cpucnt;
238459190a5Srsmaeda 
239459190a5Srsmaeda void
cpu_init_private(struct cpu * cp)240459190a5Srsmaeda cpu_init_private(struct cpu *cp)
241459190a5Srsmaeda {
242459190a5Srsmaeda 	extern void niagara_kstat_init(void);
243fb2f18f8Sesaxe 
24444961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
245459190a5Srsmaeda 
246459190a5Srsmaeda 	cpu_map_exec_units(cp);
247459190a5Srsmaeda 
24859ac0c16Sdavemq 	if ((cpucnt++ == 0) && (cpu_hsvc_available == B_TRUE))
24959ac0c16Sdavemq 		(void) niagara_kstat_init();
250575a7426Spt 
251575a7426Spt 	mutex_delay = rdccr_delay;
25244961713Sgirish }
25344961713Sgirish 
25444961713Sgirish /*ARGSUSED*/
25544961713Sgirish void
cpu_uninit_private(struct cpu * cp)25644961713Sgirish cpu_uninit_private(struct cpu *cp)
25744961713Sgirish {
258459190a5Srsmaeda 	extern void niagara_kstat_fini(void);
25944961713Sgirish 
26044961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
26159ac0c16Sdavemq 	if ((--cpucnt == 0) && (cpu_hsvc_available == B_TRUE))
26259ac0c16Sdavemq 		(void) niagara_kstat_fini();
26344961713Sgirish }
26444961713Sgirish 
26544961713Sgirish /*
26644961713Sgirish  * On Niagara2, any flush will cause all preceding stores to be
26744961713Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
26844961713Sgirish  * the address is ignored, so we always flush address 0.
26944961713Sgirish  */
27044961713Sgirish /*ARGSUSED*/
27144961713Sgirish void
dtrace_flush_sec(uintptr_t addr)27244961713Sgirish dtrace_flush_sec(uintptr_t addr)
27344961713Sgirish {
27444961713Sgirish 	doflush(0);
27544961713Sgirish }
27644961713Sgirish 
27744961713Sgirish /*
27844961713Sgirish  * Trapstat support for Niagara2 processor
27944961713Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
28044961713Sgirish  * enabled no TSB hit information will be available. Therefore setting
28144961713Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
28244961713Sgirish  */
28344961713Sgirish int
cpu_trapstat_conf(int cmd)28444961713Sgirish cpu_trapstat_conf(int cmd)
28544961713Sgirish {
28644961713Sgirish 	int status = 0;
28744961713Sgirish 
28844961713Sgirish 	switch (cmd) {
28944961713Sgirish 	case CPU_TSTATCONF_INIT:
29044961713Sgirish 	case CPU_TSTATCONF_FINI:
29144961713Sgirish 	case CPU_TSTATCONF_ENABLE:
29244961713Sgirish 	case CPU_TSTATCONF_DISABLE:
29344961713Sgirish 		break;
29444961713Sgirish 	default:
29544961713Sgirish 		status = EINVAL;
29644961713Sgirish 		break;
29744961713Sgirish 	}
29844961713Sgirish 	return (status);
29944961713Sgirish }
30044961713Sgirish 
30144961713Sgirish void
cpu_trapstat_data(void * buf,uint_t tstat_pgszs)30244961713Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
30344961713Sgirish {
30444961713Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
30544961713Sgirish 	int	i;
30644961713Sgirish 
30744961713Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
30844961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
30944961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
31044961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
31144961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
31244961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
31344961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
31444961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
31544961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
31644961713Sgirish 	}
31744961713Sgirish }
318102033aaSdp 
319ce8eb11aSdp /*
320ce8eb11aSdp  * Page coloring support for hashed cache index mode
321ce8eb11aSdp  */
322ce8eb11aSdp 
323ce8eb11aSdp /*
324ce8eb11aSdp  * Node id bits from machine description (MD).  Node id distinguishes
325ce8eb11aSdp  * local versus remote memory. Because of MPO, page allocation does
326ce8eb11aSdp  * not cross node boundaries. Therefore, remove the node id bits from
327ce8eb11aSdp  * the color, since they are fixed. Either bit 30, or 31:30 in
328ce8eb11aSdp  * Victoria Falls processors.
329ce8eb11aSdp  * The number of node id bits is always 0 in Niagara2.
330ce8eb11aSdp  */
331ce8eb11aSdp typedef struct n2color {
332ce8eb11aSdp 	uchar_t nnbits;	/* number of node id bits */
333ce8eb11aSdp 	uchar_t nnmask; /* mask for node id bits */
334ce8eb11aSdp 	uchar_t	lomask;	/* mask for bits below node id */
335ce8eb11aSdp 	uchar_t lobits;	/* number of bits below node id */
336ce8eb11aSdp } n2color_t;
337ce8eb11aSdp 
338ce8eb11aSdp n2color_t n2color[MMU_PAGE_SIZES];
339ce8eb11aSdp static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
340ce8eb11aSdp 
341ce8eb11aSdp /*
342ce8eb11aSdp  * Remove node id bits from color bits 32:28.
343ce8eb11aSdp  * This will reduce the number of colors.
344ce8eb11aSdp  * No change if number of node bits is zero.
345ce8eb11aSdp  */
346ce8eb11aSdp static inline uint_t
n2_hash2color(uint_t color,uchar_t szc)347ce8eb11aSdp n2_hash2color(uint_t color, uchar_t szc)
348ce8eb11aSdp {
349ce8eb11aSdp 	n2color_t m = n2color[szc];
350ce8eb11aSdp 
351ce8eb11aSdp 	if (m.nnbits > 0) {
352ce8eb11aSdp 		color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
353ce8eb11aSdp 		ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
354ce8eb11aSdp 	}
355ce8eb11aSdp 
356ce8eb11aSdp 	return (color);
357ce8eb11aSdp }
358ce8eb11aSdp 
359ce8eb11aSdp /*
360ce8eb11aSdp  * Restore node id bits into page color.
361ce8eb11aSdp  * This will increase the number of colors to match N2.
362ce8eb11aSdp  * No change if number of node bits is zero.
363ce8eb11aSdp  */
364ce8eb11aSdp static inline uint_t
n2_color2hash(uint_t color,uchar_t szc,uint_t node)365ce8eb11aSdp n2_color2hash(uint_t color, uchar_t szc, uint_t node)
366ce8eb11aSdp {
367ce8eb11aSdp 	n2color_t m = n2color[szc];
368ce8eb11aSdp 
369ce8eb11aSdp 	if (m.nnbits > 0) {
370ce8eb11aSdp 		color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
371ce8eb11aSdp 		color |= (node & m.nnmask) << m.lobits;
372ce8eb11aSdp 	}
373ce8eb11aSdp 
374ce8eb11aSdp 	return (color);
375ce8eb11aSdp }
376ce8eb11aSdp 
377102033aaSdp /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
378ce8eb11aSdp 
379ce8eb11aSdp /*
380ce8eb11aSdp  * iterator NULL means pfn is VA, do not adjust ra_to_pa
381ce8eb11aSdp  * iterator (-1) means pfn is RA, need to convert to PA
382ce8eb11aSdp  * iterator non-null means pfn is RA, use ra_to_pa
383ce8eb11aSdp  */
384102033aaSdp uint_t
page_pfn_2_color_cpu(pfn_t pfn,uchar_t szc,void * cookie)385ce8eb11aSdp page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
386102033aaSdp {
387ce8eb11aSdp 	mem_node_iterator_t *it = cookie;
388102033aaSdp 	uint_t color;
389102033aaSdp 
390102033aaSdp 	ASSERT(szc <= TTE256M);
391102033aaSdp 
392ce8eb11aSdp 	if (it == ((mem_node_iterator_t *)(-1))) {
393ce8eb11aSdp 		pfn = plat_rapfn_to_papfn(pfn);
394ce8eb11aSdp 	} else if (it != NULL) {
395ce8eb11aSdp 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
396ce8eb11aSdp 		pfn = pfn + it->mi_ra_to_pa;
397ce8eb11aSdp 	}
398102033aaSdp 	pfn = PFN_BASE(pfn, szc);
399102033aaSdp 	color = ((pfn >> 15) ^ pfn) & 0x1f;
400ce8eb11aSdp 	if (szc < TTE4M) {
401ce8eb11aSdp 		/* 19:18 */
402ce8eb11aSdp 		color = (color << 2) | ((pfn >> 5) & 0x3);
403ce8eb11aSdp 		if (szc > TTE64K)
404ce8eb11aSdp 			color >>= 1;    /* 19 */
405ce8eb11aSdp 	}
406ce8eb11aSdp 	return (n2_hash2color(color, szc));
407ce8eb11aSdp }
408102033aaSdp 
409ce8eb11aSdp static uint_t
page_papfn_2_color_cpu(pfn_t papfn,uchar_t szc)410ce8eb11aSdp page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
411ce8eb11aSdp {
412ce8eb11aSdp 	uint_t color;
413ce8eb11aSdp 
414ce8eb11aSdp 	ASSERT(szc <= TTE256M);
415102033aaSdp 
416ce8eb11aSdp 	papfn = PFN_BASE(papfn, szc);
417ce8eb11aSdp 	color = ((papfn >> 15) ^ papfn) & 0x1f;
418ce8eb11aSdp 	if (szc < TTE4M) {
419ce8eb11aSdp 		/* 19:18 */
420ce8eb11aSdp 		color = (color << 2) | ((papfn >> 5) & 0x3);
421ce8eb11aSdp 		if (szc > TTE64K)
422ce8eb11aSdp 			color >>= 1;    /* 19 */
423ce8eb11aSdp 	}
424ce8eb11aSdp 	return (color);
425102033aaSdp }
426102033aaSdp 
427102033aaSdp #if TTE256M != 5
428102033aaSdp #error TTE256M is not 5
429102033aaSdp #endif
430102033aaSdp 
431102033aaSdp uint_t
page_get_nsz_color_mask_cpu(uchar_t szc,uint_t mask)432102033aaSdp page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
433102033aaSdp {
434102033aaSdp 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
435102033aaSdp 	ASSERT(szc < TTE256M);
436ce8eb11aSdp 	mask = n2_color2hash(mask, szc, 0);
437102033aaSdp 	mask &= ni2_color_masks[szc];
438ce8eb11aSdp 	if (szc == TTE64K || szc == TTE512K)
439ce8eb11aSdp 		mask >>= 1;
440ce8eb11aSdp 	return (n2_hash2color(mask, szc + 1));
441102033aaSdp }
442102033aaSdp 
443102033aaSdp uint_t
page_get_nsz_color_cpu(uchar_t szc,uint_t color)444102033aaSdp page_get_nsz_color_cpu(uchar_t szc, uint_t color)
445102033aaSdp {
446102033aaSdp 	ASSERT(szc < TTE256M);
447ce8eb11aSdp 	color = n2_color2hash(color, szc, 0);
448ce8eb11aSdp 	if (szc == TTE64K || szc == TTE512K)
449ce8eb11aSdp 		color >>= 1;
450ce8eb11aSdp 	return (n2_hash2color(color, szc + 1));
451102033aaSdp }
452102033aaSdp 
453102033aaSdp uint_t
page_get_color_shift_cpu(uchar_t szc,uchar_t nszc)454102033aaSdp page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
455102033aaSdp {
456ce8eb11aSdp 	uint_t s;
457ef29e907Ssusans 	ASSERT(nszc >= szc);
458102033aaSdp 	ASSERT(nszc <= TTE256M);
459102033aaSdp 
460ce8eb11aSdp 	s = nhbits[szc] - n2color[szc].nnbits;
461ce8eb11aSdp 	s -= nhbits[nszc] - n2color[nszc].nnbits;
462102033aaSdp 
463ce8eb11aSdp 	return (s);
464ce8eb11aSdp }
465ce8eb11aSdp 
466ce8eb11aSdp uint_t
page_convert_color_cpu(uint_t ncolor,uchar_t szc,uchar_t nszc)467ce8eb11aSdp page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
468ce8eb11aSdp {
469ce8eb11aSdp 	uint_t color;
470ce8eb11aSdp 
471ce8eb11aSdp 	ASSERT(nszc > szc);
472ce8eb11aSdp 	ASSERT(nszc <= TTE256M);
473ce8eb11aSdp 	ncolor = n2_color2hash(ncolor, nszc, 0);
474ce8eb11aSdp 	color = ncolor << (nhbits[szc] - nhbits[nszc]);
475ce8eb11aSdp 	color = n2_hash2color(color, szc);
476ce8eb11aSdp 	return (color);
477102033aaSdp }
478102033aaSdp 
479ce8eb11aSdp #define	PAPFN_2_MNODE(pfn) \
480ce8eb11aSdp 	(((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
481ce8eb11aSdp 
482102033aaSdp /*ARGSUSED*/
483102033aaSdp 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)484102033aaSdp page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
485ce8eb11aSdp     uint_t ceq_mask, uint_t color_mask, void *cookie)
486102033aaSdp {
487ce8eb11aSdp 	mem_node_iterator_t *it = cookie;
488102033aaSdp 	pfn_t pstep = PNUM_SIZE(szc);
489102033aaSdp 	pfn_t npfn, pfn_ceq_mask, pfn_color;
490102033aaSdp 	pfn_t tmpmask, mask = (pfn_t)-1;
491ce8eb11aSdp 	uint_t pfnmn;
492102033aaSdp 
493102033aaSdp 	ASSERT((color & ~ceq_mask) == 0);
494ce8eb11aSdp 	ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
495ce8eb11aSdp 
496ce8eb11aSdp 	/* convert RA to PA for accurate color calculation */
497ce8eb11aSdp 	if (it->mi_init) {
498ce8eb11aSdp 		/* first call after it, so cache these values */
499ce8eb11aSdp 		it->mi_hash_ceq_mask =
500ce8eb11aSdp 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
501ce8eb11aSdp 		it->mi_hash_color =
502ce8eb11aSdp 		    n2_color2hash(color, szc, it->mi_mnode);
503ce8eb11aSdp 		it->mi_init = 0;
504ce8eb11aSdp 	} else {
505ce8eb11aSdp 		ASSERT(it->mi_hash_ceq_mask ==
506ce8eb11aSdp 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
507ce8eb11aSdp 		ASSERT(it->mi_hash_color ==
508ce8eb11aSdp 		    n2_color2hash(color, szc, it->mi_mnode));
509ce8eb11aSdp 	}
510ce8eb11aSdp 	ceq_mask = it->mi_hash_ceq_mask;
511ce8eb11aSdp 	color = it->mi_hash_color;
512ce8eb11aSdp 	pfn += it->mi_ra_to_pa;
513ce8eb11aSdp 
514ce8eb11aSdp 	/* restart here when we switch memblocks */
515ce8eb11aSdp next_mem_block:
5164df55fdeSJanie Lu 	pfnmn = PAPFN_2_MNODE(pfn);
5174df55fdeSJanie Lu 	if ((((page_papfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0) &&
5184df55fdeSJanie Lu 	    (pfnmn == it->mi_mnode)) {
519102033aaSdp 
5204df55fdeSJanie Lu 		/* we start from the page with correct color and mnode */
521102033aaSdp 		if (szc >= TTE512K) {
522102033aaSdp 			if (szc >= TTE4M) {
523102033aaSdp 				/* page color is PA[32:28] */
524102033aaSdp 				pfn_ceq_mask = ceq_mask << 15;
525102033aaSdp 			} else {
526102033aaSdp 				/* page color is PA[32:28].PA[19:19] */
527102033aaSdp 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
528102033aaSdp 				    ((ceq_mask >> 1) << 15);
529102033aaSdp 			}
5304df55fdeSJanie Lu 			/*
5314df55fdeSJanie Lu 			 * Preserve mnode bits in case they are not part of the
5324df55fdeSJanie Lu 			 * color mask (eg., 8GB interleave, mnode bits 34:33).
5334df55fdeSJanie Lu 			 */
5344df55fdeSJanie Lu 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
535ce8eb11aSdp 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
536ce8eb11aSdp 			goto done;
537102033aaSdp 		} else {
538102033aaSdp 			/*
539102033aaSdp 			 * We deal 64K or 8K page. Check if we could the
540102033aaSdp 			 * satisfy the request without changing PA[32:28]
541102033aaSdp 			 */
542102033aaSdp 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
543ce8eb11aSdp 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
544102033aaSdp 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
545102033aaSdp 
546102033aaSdp 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
547ce8eb11aSdp 				goto done;
548102033aaSdp 
549102033aaSdp 			/*
550102033aaSdp 			 * for next pfn we have to change bits PA[32:28]
551102033aaSdp 			 * set PA[63:28] and PA[19:18] of the next pfn
552102033aaSdp 			 */
553102033aaSdp 			npfn = (pfn >> 15) << 15;
554102033aaSdp 			npfn |= (ceq_mask & color & 3) << 5;
555102033aaSdp 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
556102033aaSdp 			    (ceq_mask & 0x1c) << 13;
557ce8eb11aSdp 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
558102033aaSdp 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
559102033aaSdp 
560102033aaSdp 			/*
561102033aaSdp 			 * set bits PA[17:13] to match the color
562102033aaSdp 			 */
563ce8eb11aSdp 			npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
564ce8eb11aSdp 			goto done;
565102033aaSdp 		}
566102033aaSdp 	}
567102033aaSdp 
568102033aaSdp 	/*
569102033aaSdp 	 * we start from the page with incorrect color - rare case
570102033aaSdp 	 */
571102033aaSdp 	if (szc >= TTE512K) {
572102033aaSdp 		if (szc >= TTE4M) {
573102033aaSdp 			/* page color is in bits PA[32:28] */
574102033aaSdp 			npfn = ((pfn >> 20) << 20) | (color << 15);
575102033aaSdp 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
576102033aaSdp 		} else {
577102033aaSdp 			/* try get the right color by changing bit PA[19:19] */
578102033aaSdp 			npfn = pfn + pstep;
5794df55fdeSJanie Lu 			pfnmn = PAPFN_2_MNODE(npfn);
5804df55fdeSJanie Lu 			if ((((page_papfn_2_color_cpu(npfn, szc) ^ color) &
5814df55fdeSJanie Lu 			    ceq_mask) == 0) && (pfnmn == it->mi_mnode))
582ce8eb11aSdp 				goto done;
583102033aaSdp 
584102033aaSdp 			/* page color is PA[32:28].PA[19:19] */
585102033aaSdp 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
586102033aaSdp 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
587102033aaSdp 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
588102033aaSdp 			npfn = ((pfn >> 20) << 20) | pfn_color;
589102033aaSdp 		}
590102033aaSdp 
5914df55fdeSJanie Lu 		/* Fix mnode if necessary */
5924df55fdeSJanie Lu 		if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode)
5934df55fdeSJanie Lu 			npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
5944df55fdeSJanie Lu 			    it->mi_mnode_pfn_shift;
5954df55fdeSJanie Lu 
5964df55fdeSJanie Lu 		/*
5974df55fdeSJanie Lu 		 * Preserve mnode bits in case they are not part of the color
5984df55fdeSJanie Lu 		 * mask eg 8GB interleave, mnode bits 34:33).
5994df55fdeSJanie Lu 		 */
6004df55fdeSJanie Lu 		pfn_ceq_mask |= it->mi_mnode_pfn_mask;
601102033aaSdp 		while (npfn <= pfn) {
602102033aaSdp 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
603102033aaSdp 		}
604ce8eb11aSdp 		goto done;
605102033aaSdp 	}
606102033aaSdp 
607102033aaSdp 	/*
608ce8eb11aSdp 	 *  We deal 64K or 8K page of incorrect color.
609102033aaSdp 	 * Try correcting color without changing PA[32:28]
610102033aaSdp 	 */
611102033aaSdp 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
612102033aaSdp 	pfn_color = ((color & 3) << 5) | (color >> 2);
613ce8eb11aSdp 	if (pfnmn == it->mi_mnode) {
614ce8eb11aSdp 		npfn = (pfn & ~(pfn_t)0x7f);
615ce8eb11aSdp 		npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
616ce8eb11aSdp 		npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
617ce8eb11aSdp 
618ce8eb11aSdp 		if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
619ce8eb11aSdp 		    ceq_mask) == 0) {
620ce8eb11aSdp 			/* the color is fixed - find the next page */
621ce8eb11aSdp 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
622ce8eb11aSdp 			while (npfn <= pfn) {
623ce8eb11aSdp 				npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
624ce8eb11aSdp 				    mask);
625ce8eb11aSdp 			}
626ce8eb11aSdp 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
627ce8eb11aSdp 				goto done;
628102033aaSdp 		}
629102033aaSdp 	}
630102033aaSdp 
631102033aaSdp 	/* to fix the color need to touch PA[32:28] */
632102033aaSdp 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
633102033aaSdp 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
634ce8eb11aSdp 
635ce8eb11aSdp 	/* fix mnode if input pfn is in the wrong mnode. */
636ce8eb11aSdp 	if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
637ce8eb11aSdp 		npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
638ce8eb11aSdp 		    it->mi_mnode_pfn_shift;
639ce8eb11aSdp 	}
640ce8eb11aSdp 
641102033aaSdp 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
642ce8eb11aSdp 	tmpmask |= it->mi_mnode_pfn_mask;
643102033aaSdp 
644102033aaSdp 	while (npfn <= pfn) {
645102033aaSdp 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
646102033aaSdp 	}
647102033aaSdp 
648102033aaSdp 	/* set bits PA[19:13] to match the color */
649102033aaSdp 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
650102033aaSdp 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
651102033aaSdp 
652ce8eb11aSdp done:
653ce8eb11aSdp 	ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
654ce8eb11aSdp 	ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
655ce8eb11aSdp 
656ce8eb11aSdp 	/* PA to RA */
657ce8eb11aSdp 	npfn -= it->mi_ra_to_pa;
658ce8eb11aSdp 
659ce8eb11aSdp 	/* check for possible memblock switch */
660ce8eb11aSdp 	if (npfn > it->mi_mblock_end) {
661b779d3e0Sdp 		pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, szc, it,
662b779d3e0Sdp 		    0);
663ce8eb11aSdp 		if (pfn == (pfn_t)-1)
664ce8eb11aSdp 			return (pfn);
665ce8eb11aSdp 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
666ce8eb11aSdp 		pfn += it->mi_ra_to_pa;
667ce8eb11aSdp 		goto next_mem_block;
668ce8eb11aSdp 	}
669102033aaSdp 
670102033aaSdp 	return (npfn);
671102033aaSdp }
672102033aaSdp 
673102033aaSdp /*
674102033aaSdp  * init page coloring
675ce8eb11aSdp  * VF encodes node_id for an L-group in either bit 30 or 31:30,
676ce8eb11aSdp  * which effectively reduces the number of colors available per mnode.
677102033aaSdp  */
678102033aaSdp void
page_coloring_init_cpu()679102033aaSdp page_coloring_init_cpu()
680102033aaSdp {
681102033aaSdp 	int i;
682ce8eb11aSdp 	uchar_t id;
683ce8eb11aSdp 	uchar_t lo;
684ce8eb11aSdp 	uchar_t hi;
685ce8eb11aSdp 	n2color_t m;
686ce8eb11aSdp 	mem_node_iterator_t it;
687ce8eb11aSdp 	static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
688ce8eb11aSdp 
689284953f5Sdp 	for (i = 0; i < max_mem_nodes; i++) {
690284953f5Sdp 		memset(&it, 0, sizeof (it));
691b779d3e0Sdp 		if (plat_mem_node_iterator_init(0, i, 0, &it, 1) != (pfn_t)-1)
692284953f5Sdp 			break;
693284953f5Sdp 	}
694284953f5Sdp 	ASSERT(i < max_mem_nodes);
695ce8eb11aSdp 	for (i = 0; i < mmu_page_sizes; i++) {
696b02e9a2dSsvemuri 		(void) memset(&m, 0, sizeof (m));
697ce8eb11aSdp 		id = it.mi_mnode_pfn_mask >> 15;	/* node id mask */
698ce8eb11aSdp 		id &= idmask[i];
699ce8eb11aSdp 		lo = lowbit(id);
700ce8eb11aSdp 		if (lo > 0) {
701ce8eb11aSdp 			hi = highbit(id);
702ce8eb11aSdp 			m.nnbits = hi - lo + 1;
703ce8eb11aSdp 			m.nnmask = (1 << m.nnbits) - 1;
704ce8eb11aSdp 			lo += nhbits[i] - 5;
705ce8eb11aSdp 			m.lomask = (1 << (lo - 1)) - 1;
706ce8eb11aSdp 			m.lobits = lo - 1;
707ce8eb11aSdp 		}
708ce8eb11aSdp 		hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
709ce8eb11aSdp 		n2color[i] = m;
710102033aaSdp 	}
711102033aaSdp }
712fe70c9cfSdp 
713fe70c9cfSdp /*
714fe70c9cfSdp  * group colorequiv colors on N2 by low order bits of the color first
715fe70c9cfSdp  */
716fe70c9cfSdp void
page_set_colorequiv_arr_cpu(void)717fe70c9cfSdp page_set_colorequiv_arr_cpu(void)
718fe70c9cfSdp {
719fe70c9cfSdp 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
720fe70c9cfSdp 
721ce8eb11aSdp 	nequiv_shades_log2[1] -= n2color[1].nnbits;
722fe70c9cfSdp 	if (colorequiv > 1) {
723fe70c9cfSdp 		int i;
724fe70c9cfSdp 		uint_t sv_a = lowbit(colorequiv) - 1;
725fe70c9cfSdp 
726fe70c9cfSdp 		if (sv_a > 15)
727fe70c9cfSdp 			sv_a = 15;
728fe70c9cfSdp 
729fe70c9cfSdp 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
730fe70c9cfSdp 			uint_t colors;
731fe70c9cfSdp 			uint_t a = sv_a;
732fe70c9cfSdp 
733fe70c9cfSdp 			if ((colors = hw_page_array[i].hp_colors) <= 1)
734fe70c9cfSdp 				continue;
735fe70c9cfSdp 			while ((colors >> a) == 0)
736fe70c9cfSdp 				a--;
737fe70c9cfSdp 			if (a > (colorequivszc[i] & 0xf) +
738fe70c9cfSdp 			    (colorequivszc[i] >> 4)) {
739fe70c9cfSdp 				if (a <= nequiv_shades_log2[i]) {
74059ac0c16Sdavemq 					colorequivszc[i] = (uchar_t)a;
741fe70c9cfSdp 				} else {
742fe70c9cfSdp 					colorequivszc[i] =
743fe70c9cfSdp 					    ((a - nequiv_shades_log2[i]) << 4) |
744fe70c9cfSdp 					    nequiv_shades_log2[i];
745fe70c9cfSdp 				}
746fe70c9cfSdp 			}
747fe70c9cfSdp 		}
748fe70c9cfSdp 	}
749fe70c9cfSdp }
750