xref: /titanic_54/usr/src/uts/sun4v/cpu/niagara2.c (revision 4df55fde49134f9735f84011f23a767c75e393c7)
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*4df55fdeSJanie Lu  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2444961713Sgirish  * Use is subject to license terms.
2544961713Sgirish  */
2644961713Sgirish 
2744961713Sgirish #include <sys/types.h>
2844961713Sgirish #include <sys/systm.h>
2944961713Sgirish #include <sys/archsystm.h>
3044961713Sgirish #include <sys/machparam.h>
3144961713Sgirish #include <sys/machsystm.h>
3244961713Sgirish #include <sys/cpu.h>
3344961713Sgirish #include <sys/elf_SPARC.h>
3444961713Sgirish #include <vm/hat_sfmmu.h>
3544961713Sgirish #include <vm/page.h>
36102033aaSdp78419 #include <vm/vm_dep.h>
3744961713Sgirish #include <sys/cpuvar.h>
3844961713Sgirish #include <sys/async.h>
3944961713Sgirish #include <sys/cmn_err.h>
4044961713Sgirish #include <sys/debug.h>
4144961713Sgirish #include <sys/dditypes.h>
4244961713Sgirish #include <sys/sunddi.h>
4344961713Sgirish #include <sys/cpu_module.h>
4444961713Sgirish #include <sys/prom_debug.h>
4544961713Sgirish #include <sys/vmsystm.h>
4644961713Sgirish #include <sys/prom_plat.h>
4744961713Sgirish #include <sys/sysmacros.h>
4844961713Sgirish #include <sys/intreg.h>
4944961713Sgirish #include <sys/machtrap.h>
5044961713Sgirish #include <sys/ontrap.h>
5144961713Sgirish #include <sys/ivintr.h>
5244961713Sgirish #include <sys/atomic.h>
5344961713Sgirish #include <sys/panic.h>
5444961713Sgirish #include <sys/dtrace.h>
5544961713Sgirish #include <sys/simulate.h>
5644961713Sgirish #include <sys/fault.h>
5744961713Sgirish #include <sys/niagara2regs.h>
5844961713Sgirish #include <sys/hsvc.h>
5944961713Sgirish #include <sys/trapstat.h>
60575a7426Spt157919 #include <sys/mutex_impl.h>
6144961713Sgirish 
6244961713Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
6359ac0c16Sdavemq #if defined(NIAGARA2_IMPL)
6444961713Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
6559ac0c16Sdavemq #elif defined(VFALLS_IMPL)
6659ac0c16Sdavemq char cpu_module_name[] = "SUNW,UltraSPARC-T2+";
67*4df55fdeSJanie Lu #elif defined(KT_IMPL)
68*4df55fdeSJanie Lu char cpu_module_name[] = "SUNW,UltraSPARC-KT";
6959ac0c16Sdavemq #endif
7044961713Sgirish 
7144961713Sgirish /*
7259ac0c16Sdavemq  * Hypervisor services information for the NIAGARA2 and Victoria Falls
7359ac0c16Sdavemq  * CPU module
7444961713Sgirish  */
7559ac0c16Sdavemq static boolean_t cpu_hsvc_available = B_TRUE;
7659ac0c16Sdavemq static uint64_t cpu_sup_minor;		/* Supported minor number */
7759ac0c16Sdavemq #if defined(NIAGARA2_IMPL)
7859ac0c16Sdavemq static hsvc_info_t cpu_hsvc = {
7944961713Sgirish 	HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA2_CPU, NIAGARA2_HSVC_MAJOR,
8044961713Sgirish 	NIAGARA2_HSVC_MINOR, cpu_module_name
8144961713Sgirish };
8259ac0c16Sdavemq #elif defined(VFALLS_IMPL)
8359ac0c16Sdavemq static hsvc_info_t cpu_hsvc = {
8459ac0c16Sdavemq 	HSVC_REV_1, NULL, HSVC_GROUP_VFALLS_CPU, VFALLS_HSVC_MAJOR,
8559ac0c16Sdavemq 	VFALLS_HSVC_MINOR, cpu_module_name
8659ac0c16Sdavemq };
87*4df55fdeSJanie Lu #elif defined(KT_IMPL)
88*4df55fdeSJanie Lu static hsvc_info_t cpu_hsvc = {
89*4df55fdeSJanie Lu 	HSVC_REV_1, NULL, HSVC_GROUP_KT_CPU, KT_HSVC_MAJOR,
90*4df55fdeSJanie Lu 	KT_HSVC_MINOR, cpu_module_name
91*4df55fdeSJanie Lu };
9259ac0c16Sdavemq #endif
9344961713Sgirish 
9444961713Sgirish void
9544961713Sgirish cpu_setup(void)
9644961713Sgirish {
9744961713Sgirish 	extern int mmu_exported_pagesize_mask;
9844961713Sgirish 	extern int cpc_has_overflow_intr;
997ec363dcSwh94709 	extern size_t contig_mem_prealloc_base_size;
10044961713Sgirish 	int status;
10144961713Sgirish 
10244961713Sgirish 	/*
10344961713Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
10444961713Sgirish 	 * services.
10544961713Sgirish 	 */
10659ac0c16Sdavemq 	status = hsvc_register(&cpu_hsvc, &cpu_sup_minor);
10744961713Sgirish 	if (status != 0) {
10844961713Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
10944961713Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
11059ac0c16Sdavemq 		    cpu_hsvc.hsvc_modname, cpu_hsvc.hsvc_group,
11159ac0c16Sdavemq 		    cpu_hsvc.hsvc_major, cpu_hsvc.hsvc_minor, status);
11259ac0c16Sdavemq 		cpu_hsvc_available = B_FALSE;
11344961713Sgirish 	}
11444961713Sgirish 
11544961713Sgirish 	/*
11644961713Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
11744961713Sgirish 	 * routine.
11844961713Sgirish 	 */
11944961713Sgirish 	cpu_setup_common(NULL);
12044961713Sgirish 
121*4df55fdeSJanie Lu 	/*
122*4df55fdeSJanie Lu 	 * Initialize the cpu_hwcap_flags for N2 and VF if it is not already
123*4df55fdeSJanie Lu 	 * set in cpu_setup_common() by the hwcap MD info. Note that this MD
124*4df55fdeSJanie Lu 	 * info may not be available for N2/VF.
125*4df55fdeSJanie Lu 	 */
126*4df55fdeSJanie Lu 	if (cpu_hwcap_flags == 0) {
127*4df55fdeSJanie Lu #ifdef KT_IMPL
128*4df55fdeSJanie Lu 		/*
129*4df55fdeSJanie Lu 		 * This should not happen since hwcap MD info is always
130*4df55fdeSJanie Lu 		 * available for KT platforms.
131*4df55fdeSJanie Lu 		 */
132*4df55fdeSJanie Lu 		ASSERT(cpu_hwcap_flags != 0);	/* panic in DEBUG mode */
133*4df55fdeSJanie Lu 		cpu_hwcap_flags |= AV_SPARC_VIS3 | AV_SPARC_HPC | AV_SPARC_FMAF;
134*4df55fdeSJanie Lu #endif /* KT_IMPL */
135*4df55fdeSJanie Lu 		cpu_hwcap_flags |= AV_SPARC_VIS | AV_SPARC_VIS2 |
136*4df55fdeSJanie Lu 		    AV_SPARC_ASI_BLK_INIT | AV_SPARC_POPC;
137*4df55fdeSJanie Lu 	}
138*4df55fdeSJanie Lu 
13944961713Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
14044961713Sgirish 
14144961713Sgirish 	if ((mmu_exported_pagesize_mask &
14244961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
14344961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
14444961713Sgirish 		cmn_err(CE_PANIC, "machine description"
14544961713Sgirish 		    " does not have required sun4v page sizes"
14644961713Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
14744961713Sgirish 		    mmu_exported_pagesize_mask);
14844961713Sgirish 
14944961713Sgirish 	/*
15044961713Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
15144961713Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
15244961713Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
15344961713Sgirish 	 * and must never be mapped. In addition, software must not use
15444961713Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
15544961713Sgirish 	 * avoid problems with prefetching into the VA hole.
15644961713Sgirish 	 */
15744961713Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
15844961713Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
15944961713Sgirish 
16044961713Sgirish 	/*
16144961713Sgirish 	 * Niagara2 has a performance counter overflow interrupt
16244961713Sgirish 	 */
16344961713Sgirish 	cpc_has_overflow_intr = 1;
164102033aaSdp78419 
165102033aaSdp78419 	/*
166102033aaSdp78419 	 * Enable 4M pages for OOB.
167102033aaSdp78419 	 */
168102033aaSdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
169102033aaSdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
170102033aaSdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
171aaa10e67Sha137994 
1727ec363dcSwh94709 #ifdef SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB
1737ec363dcSwh94709 	/*
1747ec363dcSwh94709 	 * Use CPU Makefile specific compile time define (if exists)
1757ec363dcSwh94709 	 * to add to the contig preallocation size.
1767ec363dcSwh94709 	 */
1777ec363dcSwh94709 	contig_mem_prealloc_base_size = MB(SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB);
1787ec363dcSwh94709 #endif
17944961713Sgirish }
18044961713Sgirish 
18144961713Sgirish /*
18244961713Sgirish  * Set the magic constants of the implementation.
18344961713Sgirish  */
18444961713Sgirish void
18544961713Sgirish cpu_fiximp(struct cpu_node *cpunode)
18644961713Sgirish {
18744961713Sgirish 	/*
18844961713Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
18944961713Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
19044961713Sgirish 	 * size, linesize.
19144961713Sgirish 	 */
19244961713Sgirish 	if (cpunode->ecache_size == 0)
19344961713Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
19444961713Sgirish 	if (cpunode->ecache_linesize == 0)
19544961713Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
19644961713Sgirish 	if (cpunode->ecache_associativity == 0)
19744961713Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
19844961713Sgirish }
19944961713Sgirish 
20044961713Sgirish void
201459190a5Srsmaeda cpu_map_exec_units(struct cpu *cp)
20244961713Sgirish {
203459190a5Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
20444961713Sgirish 
20544961713Sgirish 	/*
206fb2f18f8Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
207459190a5Srsmaeda 	 * the execution unit sharing information from the MD. They
208459190a5Srsmaeda 	 * default to the CPU id in the absence of such information.
20944961713Sgirish 	 */
21044961713Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
21144961713Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
21244961713Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
21344961713Sgirish 
214fb2f18f8Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
215fb2f18f8Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
216fb2f18f8Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
217fb2f18f8Sesaxe 
218fb2f18f8Sesaxe 	/*
219fb2f18f8Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
220fb2f18f8Sesaxe 	 */
221fb2f18f8Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
22259ac0c16Sdavemq 
22359ac0c16Sdavemq 	/*
22459ac0c16Sdavemq 	 * The cpu_chip field is initialized based on the information
22559ac0c16Sdavemq 	 * in the MD and assume that all cpus within a chip
22659ac0c16Sdavemq 	 * share the same L2 cache. If no such info is available, we
22759ac0c16Sdavemq 	 * set the cpu to belong to the defacto chip 0.
22859ac0c16Sdavemq 	 */
229ce8eb11aSdp78419 	cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
230ce8eb11aSdp78419 	if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
231ce8eb11aSdp78419 		cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
232e853d8c3Sjc25722 
233e853d8c3Sjc25722 	cp->cpu_m.cpu_chip = cpunodes[cp->cpu_id].l2_cache_mapping;
234e853d8c3Sjc25722 	if (cp->cpu_m.cpu_chip == NO_L2_CACHE_MAPPING_FOUND)
235e853d8c3Sjc25722 		cp->cpu_m.cpu_chip = CPU_CHIPID_INVALID;
236459190a5Srsmaeda }
237459190a5Srsmaeda 
23859ac0c16Sdavemq static int cpucnt;
239459190a5Srsmaeda 
240459190a5Srsmaeda void
241459190a5Srsmaeda cpu_init_private(struct cpu *cp)
242459190a5Srsmaeda {
243459190a5Srsmaeda 	extern void niagara_kstat_init(void);
244fb2f18f8Sesaxe 
24544961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
246459190a5Srsmaeda 
247459190a5Srsmaeda 	cpu_map_exec_units(cp);
248459190a5Srsmaeda 
24959ac0c16Sdavemq 	if ((cpucnt++ == 0) && (cpu_hsvc_available == B_TRUE))
25059ac0c16Sdavemq 		(void) niagara_kstat_init();
251575a7426Spt157919 
252575a7426Spt157919 	mutex_delay = rdccr_delay;
25344961713Sgirish }
25444961713Sgirish 
25544961713Sgirish /*ARGSUSED*/
25644961713Sgirish void
25744961713Sgirish cpu_uninit_private(struct cpu *cp)
25844961713Sgirish {
259459190a5Srsmaeda 	extern void niagara_kstat_fini(void);
26044961713Sgirish 
26144961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
26259ac0c16Sdavemq 	if ((--cpucnt == 0) && (cpu_hsvc_available == B_TRUE))
26359ac0c16Sdavemq 		(void) niagara_kstat_fini();
26444961713Sgirish }
26544961713Sgirish 
26644961713Sgirish /*
26744961713Sgirish  * On Niagara2, any flush will cause all preceding stores to be
26844961713Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
26944961713Sgirish  * the address is ignored, so we always flush address 0.
27044961713Sgirish  */
27144961713Sgirish /*ARGSUSED*/
27244961713Sgirish void
27344961713Sgirish dtrace_flush_sec(uintptr_t addr)
27444961713Sgirish {
27544961713Sgirish 	doflush(0);
27644961713Sgirish }
27744961713Sgirish 
27844961713Sgirish /*
27944961713Sgirish  * Trapstat support for Niagara2 processor
28044961713Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
28144961713Sgirish  * enabled no TSB hit information will be available. Therefore setting
28244961713Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
28344961713Sgirish  */
28444961713Sgirish int
28544961713Sgirish cpu_trapstat_conf(int cmd)
28644961713Sgirish {
28744961713Sgirish 	int status = 0;
28844961713Sgirish 
28944961713Sgirish 	switch (cmd) {
29044961713Sgirish 	case CPU_TSTATCONF_INIT:
29144961713Sgirish 	case CPU_TSTATCONF_FINI:
29244961713Sgirish 	case CPU_TSTATCONF_ENABLE:
29344961713Sgirish 	case CPU_TSTATCONF_DISABLE:
29444961713Sgirish 		break;
29544961713Sgirish 	default:
29644961713Sgirish 		status = EINVAL;
29744961713Sgirish 		break;
29844961713Sgirish 	}
29944961713Sgirish 	return (status);
30044961713Sgirish }
30144961713Sgirish 
30244961713Sgirish void
30344961713Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
30444961713Sgirish {
30544961713Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
30644961713Sgirish 	int	i;
30744961713Sgirish 
30844961713Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
30944961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
31044961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
31144961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
31244961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
31344961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
31444961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
31544961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
31644961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
31744961713Sgirish 	}
31844961713Sgirish }
319102033aaSdp78419 
320ce8eb11aSdp78419 /*
321ce8eb11aSdp78419  * Page coloring support for hashed cache index mode
322ce8eb11aSdp78419  */
323ce8eb11aSdp78419 
324ce8eb11aSdp78419 /*
325ce8eb11aSdp78419  * Node id bits from machine description (MD).  Node id distinguishes
326ce8eb11aSdp78419  * local versus remote memory. Because of MPO, page allocation does
327ce8eb11aSdp78419  * not cross node boundaries. Therefore, remove the node id bits from
328ce8eb11aSdp78419  * the color, since they are fixed. Either bit 30, or 31:30 in
329ce8eb11aSdp78419  * Victoria Falls processors.
330ce8eb11aSdp78419  * The number of node id bits is always 0 in Niagara2.
331ce8eb11aSdp78419  */
332ce8eb11aSdp78419 typedef struct n2color {
333ce8eb11aSdp78419 	uchar_t nnbits;	/* number of node id bits */
334ce8eb11aSdp78419 	uchar_t nnmask; /* mask for node id bits */
335ce8eb11aSdp78419 	uchar_t	lomask;	/* mask for bits below node id */
336ce8eb11aSdp78419 	uchar_t lobits;	/* number of bits below node id */
337ce8eb11aSdp78419 } n2color_t;
338ce8eb11aSdp78419 
339ce8eb11aSdp78419 n2color_t n2color[MMU_PAGE_SIZES];
340ce8eb11aSdp78419 static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
341ce8eb11aSdp78419 
342ce8eb11aSdp78419 /*
343ce8eb11aSdp78419  * Remove node id bits from color bits 32:28.
344ce8eb11aSdp78419  * This will reduce the number of colors.
345ce8eb11aSdp78419  * No change if number of node bits is zero.
346ce8eb11aSdp78419  */
347ce8eb11aSdp78419 static inline uint_t
348ce8eb11aSdp78419 n2_hash2color(uint_t color, uchar_t szc)
349ce8eb11aSdp78419 {
350ce8eb11aSdp78419 	n2color_t m = n2color[szc];
351ce8eb11aSdp78419 
352ce8eb11aSdp78419 	if (m.nnbits > 0) {
353ce8eb11aSdp78419 		color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
354ce8eb11aSdp78419 		ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
355ce8eb11aSdp78419 	}
356ce8eb11aSdp78419 
357ce8eb11aSdp78419 	return (color);
358ce8eb11aSdp78419 }
359ce8eb11aSdp78419 
360ce8eb11aSdp78419 /*
361ce8eb11aSdp78419  * Restore node id bits into page color.
362ce8eb11aSdp78419  * This will increase the number of colors to match N2.
363ce8eb11aSdp78419  * No change if number of node bits is zero.
364ce8eb11aSdp78419  */
365ce8eb11aSdp78419 static inline uint_t
366ce8eb11aSdp78419 n2_color2hash(uint_t color, uchar_t szc, uint_t node)
367ce8eb11aSdp78419 {
368ce8eb11aSdp78419 	n2color_t m = n2color[szc];
369ce8eb11aSdp78419 
370ce8eb11aSdp78419 	if (m.nnbits > 0) {
371ce8eb11aSdp78419 		color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
372ce8eb11aSdp78419 		color |= (node & m.nnmask) << m.lobits;
373ce8eb11aSdp78419 	}
374ce8eb11aSdp78419 
375ce8eb11aSdp78419 	return (color);
376ce8eb11aSdp78419 }
377ce8eb11aSdp78419 
378102033aaSdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
379ce8eb11aSdp78419 
380ce8eb11aSdp78419 /*
381ce8eb11aSdp78419  * iterator NULL means pfn is VA, do not adjust ra_to_pa
382ce8eb11aSdp78419  * iterator (-1) means pfn is RA, need to convert to PA
383ce8eb11aSdp78419  * iterator non-null means pfn is RA, use ra_to_pa
384ce8eb11aSdp78419  */
385102033aaSdp78419 uint_t
386ce8eb11aSdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
387ce8eb11aSdp78419 {
388ce8eb11aSdp78419 	mem_node_iterator_t *it = cookie;
389ce8eb11aSdp78419 	uint_t color;
390ce8eb11aSdp78419 
391ce8eb11aSdp78419 	ASSERT(szc <= TTE256M);
392ce8eb11aSdp78419 
393ce8eb11aSdp78419 	if (it == ((mem_node_iterator_t *)(-1))) {
394ce8eb11aSdp78419 		pfn = plat_rapfn_to_papfn(pfn);
395ce8eb11aSdp78419 	} else if (it != NULL) {
396ce8eb11aSdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
397ce8eb11aSdp78419 		pfn = pfn + it->mi_ra_to_pa;
398ce8eb11aSdp78419 	}
399ce8eb11aSdp78419 	pfn = PFN_BASE(pfn, szc);
400ce8eb11aSdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
401ce8eb11aSdp78419 	if (szc < TTE4M) {
402ce8eb11aSdp78419 		/* 19:18 */
403ce8eb11aSdp78419 		color = (color << 2) | ((pfn >> 5) & 0x3);
404ce8eb11aSdp78419 		if (szc > TTE64K)
405ce8eb11aSdp78419 			color >>= 1;    /* 19 */
406ce8eb11aSdp78419 	}
407ce8eb11aSdp78419 	return (n2_hash2color(color, szc));
408ce8eb11aSdp78419 }
409ce8eb11aSdp78419 
410ce8eb11aSdp78419 static uint_t
411ce8eb11aSdp78419 page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
412102033aaSdp78419 {
413102033aaSdp78419 	uint_t color;
414102033aaSdp78419 
415102033aaSdp78419 	ASSERT(szc <= TTE256M);
416102033aaSdp78419 
417ce8eb11aSdp78419 	papfn = PFN_BASE(papfn, szc);
418ce8eb11aSdp78419 	color = ((papfn >> 15) ^ papfn) & 0x1f;
419ce8eb11aSdp78419 	if (szc < TTE4M) {
420ce8eb11aSdp78419 		/* 19:18 */
421ce8eb11aSdp78419 		color = (color << 2) | ((papfn >> 5) & 0x3);
422ce8eb11aSdp78419 		if (szc > TTE64K)
423ce8eb11aSdp78419 			color >>= 1;    /* 19 */
424ce8eb11aSdp78419 	}
425102033aaSdp78419 	return (color);
426102033aaSdp78419 }
427102033aaSdp78419 
428102033aaSdp78419 #if TTE256M != 5
429102033aaSdp78419 #error TTE256M is not 5
430102033aaSdp78419 #endif
431102033aaSdp78419 
432102033aaSdp78419 uint_t
433102033aaSdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
434102033aaSdp78419 {
435102033aaSdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
436102033aaSdp78419 	ASSERT(szc < TTE256M);
437ce8eb11aSdp78419 	mask = n2_color2hash(mask, szc, 0);
438102033aaSdp78419 	mask &= ni2_color_masks[szc];
439ce8eb11aSdp78419 	if (szc == TTE64K || szc == TTE512K)
440ce8eb11aSdp78419 		mask >>= 1;
441ce8eb11aSdp78419 	return (n2_hash2color(mask, szc + 1));
442102033aaSdp78419 }
443102033aaSdp78419 
444102033aaSdp78419 uint_t
445102033aaSdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
446102033aaSdp78419 {
447102033aaSdp78419 	ASSERT(szc < TTE256M);
448ce8eb11aSdp78419 	color = n2_color2hash(color, szc, 0);
449ce8eb11aSdp78419 	if (szc == TTE64K || szc == TTE512K)
450ce8eb11aSdp78419 		color >>= 1;
451ce8eb11aSdp78419 	return (n2_hash2color(color, szc + 1));
452102033aaSdp78419 }
453102033aaSdp78419 
454102033aaSdp78419 uint_t
455102033aaSdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
456102033aaSdp78419 {
457ce8eb11aSdp78419 	uint_t s;
458ef29e907Ssusans 	ASSERT(nszc >= szc);
459102033aaSdp78419 	ASSERT(nszc <= TTE256M);
460102033aaSdp78419 
461ce8eb11aSdp78419 	s = nhbits[szc] - n2color[szc].nnbits;
462ce8eb11aSdp78419 	s -= nhbits[nszc] - n2color[nszc].nnbits;
463102033aaSdp78419 
464ce8eb11aSdp78419 	return (s);
465102033aaSdp78419 }
466102033aaSdp78419 
467ce8eb11aSdp78419 uint_t
468ce8eb11aSdp78419 page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
469ce8eb11aSdp78419 {
470ce8eb11aSdp78419 	uint_t color;
471ce8eb11aSdp78419 
472ce8eb11aSdp78419 	ASSERT(nszc > szc);
473ce8eb11aSdp78419 	ASSERT(nszc <= TTE256M);
474ce8eb11aSdp78419 	ncolor = n2_color2hash(ncolor, nszc, 0);
475ce8eb11aSdp78419 	color = ncolor << (nhbits[szc] - nhbits[nszc]);
476ce8eb11aSdp78419 	color = n2_hash2color(color, szc);
477ce8eb11aSdp78419 	return (color);
478ce8eb11aSdp78419 }
479ce8eb11aSdp78419 
480ce8eb11aSdp78419 #define	PAPFN_2_MNODE(pfn) \
481ce8eb11aSdp78419 	(((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
482ce8eb11aSdp78419 
483102033aaSdp78419 /*ARGSUSED*/
484102033aaSdp78419 pfn_t
485102033aaSdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
486ce8eb11aSdp78419     uint_t ceq_mask, uint_t color_mask, void *cookie)
487102033aaSdp78419 {
488ce8eb11aSdp78419 	mem_node_iterator_t *it = cookie;
489102033aaSdp78419 	pfn_t pstep = PNUM_SIZE(szc);
490102033aaSdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
491102033aaSdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
492ce8eb11aSdp78419 	uint_t pfnmn;
493102033aaSdp78419 
494102033aaSdp78419 	ASSERT((color & ~ceq_mask) == 0);
495ce8eb11aSdp78419 	ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
496102033aaSdp78419 
497ce8eb11aSdp78419 	/* convert RA to PA for accurate color calculation */
498ce8eb11aSdp78419 	if (it->mi_init) {
499ce8eb11aSdp78419 		/* first call after it, so cache these values */
500ce8eb11aSdp78419 		it->mi_hash_ceq_mask =
501ce8eb11aSdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
502ce8eb11aSdp78419 		it->mi_hash_color =
503ce8eb11aSdp78419 		    n2_color2hash(color, szc, it->mi_mnode);
504ce8eb11aSdp78419 		it->mi_init = 0;
505ce8eb11aSdp78419 	} else {
506ce8eb11aSdp78419 		ASSERT(it->mi_hash_ceq_mask ==
507ce8eb11aSdp78419 		    n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
508ce8eb11aSdp78419 		ASSERT(it->mi_hash_color ==
509ce8eb11aSdp78419 		    n2_color2hash(color, szc, it->mi_mnode));
510ce8eb11aSdp78419 	}
511ce8eb11aSdp78419 	ceq_mask = it->mi_hash_ceq_mask;
512ce8eb11aSdp78419 	color = it->mi_hash_color;
513ce8eb11aSdp78419 	pfn += it->mi_ra_to_pa;
514ce8eb11aSdp78419 
515ce8eb11aSdp78419 	/* restart here when we switch memblocks */
516ce8eb11aSdp78419 next_mem_block:
517ce8eb11aSdp78419 	pfnmn = PAPFN_2_MNODE(pfn);
518*4df55fdeSJanie Lu 	if ((((page_papfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0) &&
519*4df55fdeSJanie Lu 	    (pfnmn == it->mi_mnode)) {
520102033aaSdp78419 
521*4df55fdeSJanie Lu 		/* we start from the page with correct color and mnode */
522102033aaSdp78419 		if (szc >= TTE512K) {
523102033aaSdp78419 			if (szc >= TTE4M) {
524102033aaSdp78419 				/* page color is PA[32:28] */
525102033aaSdp78419 				pfn_ceq_mask = ceq_mask << 15;
526102033aaSdp78419 			} else {
527102033aaSdp78419 				/* page color is PA[32:28].PA[19:19] */
528102033aaSdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
529102033aaSdp78419 				    ((ceq_mask >> 1) << 15);
530102033aaSdp78419 			}
531*4df55fdeSJanie Lu 			/*
532*4df55fdeSJanie Lu 			 * Preserve mnode bits in case they are not part of the
533*4df55fdeSJanie Lu 			 * color mask (eg., 8GB interleave, mnode bits 34:33).
534*4df55fdeSJanie Lu 			 */
535*4df55fdeSJanie Lu 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
536ce8eb11aSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
537ce8eb11aSdp78419 			goto done;
538102033aaSdp78419 		} else {
539102033aaSdp78419 			/*
540102033aaSdp78419 			 * We deal 64K or 8K page. Check if we could the
541102033aaSdp78419 			 * satisfy the request without changing PA[32:28]
542102033aaSdp78419 			 */
543102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
544ce8eb11aSdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
545102033aaSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
546102033aaSdp78419 
547102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
548ce8eb11aSdp78419 				goto done;
549102033aaSdp78419 
550102033aaSdp78419 			/*
551102033aaSdp78419 			 * for next pfn we have to change bits PA[32:28]
552102033aaSdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
553102033aaSdp78419 			 */
554102033aaSdp78419 			npfn = (pfn >> 15) << 15;
555102033aaSdp78419 			npfn |= (ceq_mask & color & 3) << 5;
556102033aaSdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
557102033aaSdp78419 			    (ceq_mask & 0x1c) << 13;
558ce8eb11aSdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
559102033aaSdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
560102033aaSdp78419 
561102033aaSdp78419 			/*
562102033aaSdp78419 			 * set bits PA[17:13] to match the color
563102033aaSdp78419 			 */
564ce8eb11aSdp78419 			npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
565ce8eb11aSdp78419 			goto done;
566102033aaSdp78419 		}
567102033aaSdp78419 	}
568102033aaSdp78419 
569102033aaSdp78419 	/*
570102033aaSdp78419 	 * we start from the page with incorrect color - rare case
571102033aaSdp78419 	 */
572102033aaSdp78419 	if (szc >= TTE512K) {
573102033aaSdp78419 		if (szc >= TTE4M) {
574102033aaSdp78419 			/* page color is in bits PA[32:28] */
575102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
576102033aaSdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
577102033aaSdp78419 		} else {
578102033aaSdp78419 			/* try get the right color by changing bit PA[19:19] */
579102033aaSdp78419 			npfn = pfn + pstep;
580*4df55fdeSJanie Lu 			pfnmn = PAPFN_2_MNODE(npfn);
581*4df55fdeSJanie Lu 			if ((((page_papfn_2_color_cpu(npfn, szc) ^ color) &
582*4df55fdeSJanie Lu 			    ceq_mask) == 0) && (pfnmn == it->mi_mnode))
583ce8eb11aSdp78419 				goto done;
584102033aaSdp78419 
585102033aaSdp78419 			/* page color is PA[32:28].PA[19:19] */
586102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
587102033aaSdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
588102033aaSdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
589102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
590102033aaSdp78419 		}
591102033aaSdp78419 
592*4df55fdeSJanie Lu 		/* Fix mnode if necessary */
593*4df55fdeSJanie Lu 		if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode)
594*4df55fdeSJanie Lu 			npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
595*4df55fdeSJanie Lu 			    it->mi_mnode_pfn_shift;
596*4df55fdeSJanie Lu 
597*4df55fdeSJanie Lu 		/*
598*4df55fdeSJanie Lu 		 * Preserve mnode bits in case they are not part of the color
599*4df55fdeSJanie Lu 		 * mask eg 8GB interleave, mnode bits 34:33).
600*4df55fdeSJanie Lu 		 */
601*4df55fdeSJanie Lu 		pfn_ceq_mask |= it->mi_mnode_pfn_mask;
602102033aaSdp78419 		while (npfn <= pfn) {
603102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
604102033aaSdp78419 		}
605ce8eb11aSdp78419 		goto done;
606102033aaSdp78419 	}
607102033aaSdp78419 
608102033aaSdp78419 	/*
609102033aaSdp78419 	 *  We deal 64K or 8K page of incorrect color.
610102033aaSdp78419 	 * Try correcting color without changing PA[32:28]
611102033aaSdp78419 	 */
612102033aaSdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
613102033aaSdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
614ce8eb11aSdp78419 	if (pfnmn == it->mi_mnode) {
615102033aaSdp78419 		npfn = (pfn & ~(pfn_t)0x7f);
616102033aaSdp78419 		npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
617102033aaSdp78419 		npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
618102033aaSdp78419 
619ce8eb11aSdp78419 		if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
620ce8eb11aSdp78419 		    ceq_mask) == 0) {
621102033aaSdp78419 			/* the color is fixed - find the next page */
622ce8eb11aSdp78419 			pfn_ceq_mask |= it->mi_mnode_pfn_mask;
623102033aaSdp78419 			while (npfn <= pfn) {
624ce8eb11aSdp78419 				npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
625ce8eb11aSdp78419 				    mask);
626102033aaSdp78419 			}
627102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
628ce8eb11aSdp78419 				goto done;
629ce8eb11aSdp78419 		}
630102033aaSdp78419 	}
631102033aaSdp78419 
632102033aaSdp78419 	/* to fix the color need to touch PA[32:28] */
633102033aaSdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
634102033aaSdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
635ce8eb11aSdp78419 
636ce8eb11aSdp78419 	/* fix mnode if input pfn is in the wrong mnode. */
637ce8eb11aSdp78419 	if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
638ce8eb11aSdp78419 		npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
639ce8eb11aSdp78419 		    it->mi_mnode_pfn_shift;
640ce8eb11aSdp78419 	}
641ce8eb11aSdp78419 
642102033aaSdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
643ce8eb11aSdp78419 	tmpmask |= it->mi_mnode_pfn_mask;
644102033aaSdp78419 
645102033aaSdp78419 	while (npfn <= pfn) {
646102033aaSdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
647102033aaSdp78419 	}
648102033aaSdp78419 
649102033aaSdp78419 	/* set bits PA[19:13] to match the color */
650102033aaSdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
651102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
652102033aaSdp78419 
653ce8eb11aSdp78419 done:
654ce8eb11aSdp78419 	ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
655ce8eb11aSdp78419 	ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
656ce8eb11aSdp78419 
657ce8eb11aSdp78419 	/* PA to RA */
658ce8eb11aSdp78419 	npfn -= it->mi_ra_to_pa;
659ce8eb11aSdp78419 
660ce8eb11aSdp78419 	/* check for possible memblock switch */
661ce8eb11aSdp78419 	if (npfn > it->mi_mblock_end) {
662b779d3e0Sdp78419 		pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, szc, it,
663b779d3e0Sdp78419 		    0);
664ce8eb11aSdp78419 		if (pfn == (pfn_t)-1)
665ce8eb11aSdp78419 			return (pfn);
666ce8eb11aSdp78419 		ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
667ce8eb11aSdp78419 		pfn += it->mi_ra_to_pa;
668ce8eb11aSdp78419 		goto next_mem_block;
669ce8eb11aSdp78419 	}
670102033aaSdp78419 
671102033aaSdp78419 	return (npfn);
672102033aaSdp78419 }
673102033aaSdp78419 
674102033aaSdp78419 /*
675102033aaSdp78419  * init page coloring
676ce8eb11aSdp78419  * VF encodes node_id for an L-group in either bit 30 or 31:30,
677ce8eb11aSdp78419  * which effectively reduces the number of colors available per mnode.
678102033aaSdp78419  */
679102033aaSdp78419 void
680102033aaSdp78419 page_coloring_init_cpu()
681102033aaSdp78419 {
682102033aaSdp78419 	int i;
683ce8eb11aSdp78419 	uchar_t id;
684ce8eb11aSdp78419 	uchar_t lo;
685ce8eb11aSdp78419 	uchar_t hi;
686ce8eb11aSdp78419 	n2color_t m;
687ce8eb11aSdp78419 	mem_node_iterator_t it;
688ce8eb11aSdp78419 	static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
689102033aaSdp78419 
690284953f5Sdp78419 	for (i = 0; i < max_mem_nodes; i++) {
691284953f5Sdp78419 		memset(&it, 0, sizeof (it));
692b779d3e0Sdp78419 		if (plat_mem_node_iterator_init(0, i, 0, &it, 1) != (pfn_t)-1)
693284953f5Sdp78419 			break;
694284953f5Sdp78419 	}
695284953f5Sdp78419 	ASSERT(i < max_mem_nodes);
696ce8eb11aSdp78419 	for (i = 0; i < mmu_page_sizes; i++) {
697b02e9a2dSsvemuri 		(void) memset(&m, 0, sizeof (m));
698ce8eb11aSdp78419 		id = it.mi_mnode_pfn_mask >> 15;	/* node id mask */
699ce8eb11aSdp78419 		id &= idmask[i];
700ce8eb11aSdp78419 		lo = lowbit(id);
701ce8eb11aSdp78419 		if (lo > 0) {
702ce8eb11aSdp78419 			hi = highbit(id);
703ce8eb11aSdp78419 			m.nnbits = hi - lo + 1;
704ce8eb11aSdp78419 			m.nnmask = (1 << m.nnbits) - 1;
705ce8eb11aSdp78419 			lo += nhbits[i] - 5;
706ce8eb11aSdp78419 			m.lomask = (1 << (lo - 1)) - 1;
707ce8eb11aSdp78419 			m.lobits = lo - 1;
708ce8eb11aSdp78419 		}
709ce8eb11aSdp78419 		hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
710ce8eb11aSdp78419 		n2color[i] = m;
711102033aaSdp78419 	}
712102033aaSdp78419 }
713fe70c9cfSdp78419 
714fe70c9cfSdp78419 /*
715fe70c9cfSdp78419  * group colorequiv colors on N2 by low order bits of the color first
716fe70c9cfSdp78419  */
717fe70c9cfSdp78419 void
718fe70c9cfSdp78419 page_set_colorequiv_arr_cpu(void)
719fe70c9cfSdp78419 {
720fe70c9cfSdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
721fe70c9cfSdp78419 
722ce8eb11aSdp78419 	nequiv_shades_log2[1] -= n2color[1].nnbits;
723fe70c9cfSdp78419 	if (colorequiv > 1) {
724fe70c9cfSdp78419 		int i;
725fe70c9cfSdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
726fe70c9cfSdp78419 
727fe70c9cfSdp78419 		if (sv_a > 15)
728fe70c9cfSdp78419 			sv_a = 15;
729fe70c9cfSdp78419 
730fe70c9cfSdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
731fe70c9cfSdp78419 			uint_t colors;
732fe70c9cfSdp78419 			uint_t a = sv_a;
733fe70c9cfSdp78419 
734fe70c9cfSdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
735fe70c9cfSdp78419 				continue;
736fe70c9cfSdp78419 			while ((colors >> a) == 0)
737fe70c9cfSdp78419 				a--;
738fe70c9cfSdp78419 			if (a > (colorequivszc[i] & 0xf) +
739fe70c9cfSdp78419 			    (colorequivszc[i] >> 4)) {
740fe70c9cfSdp78419 				if (a <= nequiv_shades_log2[i]) {
74159ac0c16Sdavemq 					colorequivszc[i] = (uchar_t)a;
742fe70c9cfSdp78419 				} else {
743fe70c9cfSdp78419 					colorequivszc[i] =
744fe70c9cfSdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
745fe70c9cfSdp78419 					    nequiv_shades_log2[i];
746fe70c9cfSdp78419 				}
747fe70c9cfSdp78419 			}
748fe70c9cfSdp78419 		}
749fe70c9cfSdp78419 	}
750fe70c9cfSdp78419 }
751