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>
35102033aaSdp78419 #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>
59575a7426Spt157919 #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;
987ec363dcSwh94709 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;
163102033aaSdp78419
164102033aaSdp78419 /*
165102033aaSdp78419 * Enable 4M pages for OOB.
166102033aaSdp78419 */
167102033aaSdp78419 max_uheap_lpsize = MMU_PAGESIZE4M;
168102033aaSdp78419 max_ustack_lpsize = MMU_PAGESIZE4M;
169102033aaSdp78419 max_privmap_lpsize = MMU_PAGESIZE4M;
170aaa10e67Sha137994
1717ec363dcSwh94709 #ifdef SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB
1727ec363dcSwh94709 /*
1737ec363dcSwh94709 * Use CPU Makefile specific compile time define (if exists)
1747ec363dcSwh94709 * to add to the contig preallocation size.
1757ec363dcSwh94709 */
1767ec363dcSwh94709 contig_mem_prealloc_base_size = MB(SUN4V_CONTIG_MEM_PREALLOC_SIZE_MB);
1777ec363dcSwh94709 #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 */
228ce8eb11aSdp78419 cp->cpu_m.cpu_mpipe = cpunodes[cp->cpu_id].l2_cache_mapping;
229ce8eb11aSdp78419 if (cp->cpu_m.cpu_mpipe == NO_L2_CACHE_MAPPING_FOUND)
230ce8eb11aSdp78419 cp->cpu_m.cpu_mpipe = CPU_L2_CACHEID_INVALID;
231e853d8c3Sjc25722
232e853d8c3Sjc25722 cp->cpu_m.cpu_chip = cpunodes[cp->cpu_id].l2_cache_mapping;
233e853d8c3Sjc25722 if (cp->cpu_m.cpu_chip == NO_L2_CACHE_MAPPING_FOUND)
234e853d8c3Sjc25722 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();
250575a7426Spt157919
251575a7426Spt157919 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 }
318102033aaSdp78419
319ce8eb11aSdp78419 /*
320ce8eb11aSdp78419 * Page coloring support for hashed cache index mode
321ce8eb11aSdp78419 */
322ce8eb11aSdp78419
323ce8eb11aSdp78419 /*
324ce8eb11aSdp78419 * Node id bits from machine description (MD). Node id distinguishes
325ce8eb11aSdp78419 * local versus remote memory. Because of MPO, page allocation does
326ce8eb11aSdp78419 * not cross node boundaries. Therefore, remove the node id bits from
327ce8eb11aSdp78419 * the color, since they are fixed. Either bit 30, or 31:30 in
328ce8eb11aSdp78419 * Victoria Falls processors.
329ce8eb11aSdp78419 * The number of node id bits is always 0 in Niagara2.
330ce8eb11aSdp78419 */
331ce8eb11aSdp78419 typedef struct n2color {
332ce8eb11aSdp78419 uchar_t nnbits; /* number of node id bits */
333ce8eb11aSdp78419 uchar_t nnmask; /* mask for node id bits */
334ce8eb11aSdp78419 uchar_t lomask; /* mask for bits below node id */
335ce8eb11aSdp78419 uchar_t lobits; /* number of bits below node id */
336ce8eb11aSdp78419 } n2color_t;
337ce8eb11aSdp78419
338ce8eb11aSdp78419 n2color_t n2color[MMU_PAGE_SIZES];
339ce8eb11aSdp78419 static uchar_t nhbits[] = {7, 7, 6, 5, 5, 5};
340ce8eb11aSdp78419
341ce8eb11aSdp78419 /*
342ce8eb11aSdp78419 * Remove node id bits from color bits 32:28.
343ce8eb11aSdp78419 * This will reduce the number of colors.
344ce8eb11aSdp78419 * No change if number of node bits is zero.
345ce8eb11aSdp78419 */
346ce8eb11aSdp78419 static inline uint_t
n2_hash2color(uint_t color,uchar_t szc)347ce8eb11aSdp78419 n2_hash2color(uint_t color, uchar_t szc)
348ce8eb11aSdp78419 {
349ce8eb11aSdp78419 n2color_t m = n2color[szc];
350ce8eb11aSdp78419
351ce8eb11aSdp78419 if (m.nnbits > 0) {
352ce8eb11aSdp78419 color = ((color >> m.nnbits) & ~m.lomask) | (color & m.lomask);
353ce8eb11aSdp78419 ASSERT((color & ~(hw_page_array[szc].hp_colors - 1)) == 0);
354ce8eb11aSdp78419 }
355ce8eb11aSdp78419
356ce8eb11aSdp78419 return (color);
357ce8eb11aSdp78419 }
358ce8eb11aSdp78419
359ce8eb11aSdp78419 /*
360ce8eb11aSdp78419 * Restore node id bits into page color.
361ce8eb11aSdp78419 * This will increase the number of colors to match N2.
362ce8eb11aSdp78419 * No change if number of node bits is zero.
363ce8eb11aSdp78419 */
364ce8eb11aSdp78419 static inline uint_t
n2_color2hash(uint_t color,uchar_t szc,uint_t node)365ce8eb11aSdp78419 n2_color2hash(uint_t color, uchar_t szc, uint_t node)
366ce8eb11aSdp78419 {
367ce8eb11aSdp78419 n2color_t m = n2color[szc];
368ce8eb11aSdp78419
369ce8eb11aSdp78419 if (m.nnbits > 0) {
370ce8eb11aSdp78419 color = ((color & ~m.lomask) << m.nnbits) | (color & m.lomask);
371ce8eb11aSdp78419 color |= (node & m.nnmask) << m.lobits;
372ce8eb11aSdp78419 }
373ce8eb11aSdp78419
374ce8eb11aSdp78419 return (color);
375ce8eb11aSdp78419 }
376ce8eb11aSdp78419
377102033aaSdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
378ce8eb11aSdp78419
379ce8eb11aSdp78419 /*
380ce8eb11aSdp78419 * iterator NULL means pfn is VA, do not adjust ra_to_pa
381ce8eb11aSdp78419 * iterator (-1) means pfn is RA, need to convert to PA
382ce8eb11aSdp78419 * iterator non-null means pfn is RA, use ra_to_pa
383ce8eb11aSdp78419 */
384102033aaSdp78419 uint_t
page_pfn_2_color_cpu(pfn_t pfn,uchar_t szc,void * cookie)385ce8eb11aSdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc, void *cookie)
386ce8eb11aSdp78419 {
387ce8eb11aSdp78419 mem_node_iterator_t *it = cookie;
388ce8eb11aSdp78419 uint_t color;
389ce8eb11aSdp78419
390ce8eb11aSdp78419 ASSERT(szc <= TTE256M);
391ce8eb11aSdp78419
392ce8eb11aSdp78419 if (it == ((mem_node_iterator_t *)(-1))) {
393ce8eb11aSdp78419 pfn = plat_rapfn_to_papfn(pfn);
394ce8eb11aSdp78419 } else if (it != NULL) {
395ce8eb11aSdp78419 ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
396ce8eb11aSdp78419 pfn = pfn + it->mi_ra_to_pa;
397ce8eb11aSdp78419 }
398ce8eb11aSdp78419 pfn = PFN_BASE(pfn, szc);
399ce8eb11aSdp78419 color = ((pfn >> 15) ^ pfn) & 0x1f;
400ce8eb11aSdp78419 if (szc < TTE4M) {
401ce8eb11aSdp78419 /* 19:18 */
402ce8eb11aSdp78419 color = (color << 2) | ((pfn >> 5) & 0x3);
403ce8eb11aSdp78419 if (szc > TTE64K)
404ce8eb11aSdp78419 color >>= 1; /* 19 */
405ce8eb11aSdp78419 }
406ce8eb11aSdp78419 return (n2_hash2color(color, szc));
407ce8eb11aSdp78419 }
408ce8eb11aSdp78419
409ce8eb11aSdp78419 static uint_t
page_papfn_2_color_cpu(pfn_t papfn,uchar_t szc)410ce8eb11aSdp78419 page_papfn_2_color_cpu(pfn_t papfn, uchar_t szc)
411102033aaSdp78419 {
412102033aaSdp78419 uint_t color;
413102033aaSdp78419
414102033aaSdp78419 ASSERT(szc <= TTE256M);
415102033aaSdp78419
416ce8eb11aSdp78419 papfn = PFN_BASE(papfn, szc);
417ce8eb11aSdp78419 color = ((papfn >> 15) ^ papfn) & 0x1f;
418ce8eb11aSdp78419 if (szc < TTE4M) {
419ce8eb11aSdp78419 /* 19:18 */
420ce8eb11aSdp78419 color = (color << 2) | ((papfn >> 5) & 0x3);
421ce8eb11aSdp78419 if (szc > TTE64K)
422ce8eb11aSdp78419 color >>= 1; /* 19 */
423ce8eb11aSdp78419 }
424102033aaSdp78419 return (color);
425102033aaSdp78419 }
426102033aaSdp78419
427102033aaSdp78419 #if TTE256M != 5
428102033aaSdp78419 #error TTE256M is not 5
429102033aaSdp78419 #endif
430102033aaSdp78419
431102033aaSdp78419 uint_t
page_get_nsz_color_mask_cpu(uchar_t szc,uint_t mask)432102033aaSdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
433102033aaSdp78419 {
434102033aaSdp78419 static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
435102033aaSdp78419 ASSERT(szc < TTE256M);
436ce8eb11aSdp78419 mask = n2_color2hash(mask, szc, 0);
437102033aaSdp78419 mask &= ni2_color_masks[szc];
438ce8eb11aSdp78419 if (szc == TTE64K || szc == TTE512K)
439ce8eb11aSdp78419 mask >>= 1;
440ce8eb11aSdp78419 return (n2_hash2color(mask, szc + 1));
441102033aaSdp78419 }
442102033aaSdp78419
443102033aaSdp78419 uint_t
page_get_nsz_color_cpu(uchar_t szc,uint_t color)444102033aaSdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
445102033aaSdp78419 {
446102033aaSdp78419 ASSERT(szc < TTE256M);
447ce8eb11aSdp78419 color = n2_color2hash(color, szc, 0);
448ce8eb11aSdp78419 if (szc == TTE64K || szc == TTE512K)
449ce8eb11aSdp78419 color >>= 1;
450ce8eb11aSdp78419 return (n2_hash2color(color, szc + 1));
451102033aaSdp78419 }
452102033aaSdp78419
453102033aaSdp78419 uint_t
page_get_color_shift_cpu(uchar_t szc,uchar_t nszc)454102033aaSdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
455102033aaSdp78419 {
456ce8eb11aSdp78419 uint_t s;
457ef29e907Ssusans ASSERT(nszc >= szc);
458102033aaSdp78419 ASSERT(nszc <= TTE256M);
459102033aaSdp78419
460ce8eb11aSdp78419 s = nhbits[szc] - n2color[szc].nnbits;
461ce8eb11aSdp78419 s -= nhbits[nszc] - n2color[nszc].nnbits;
462102033aaSdp78419
463ce8eb11aSdp78419 return (s);
464102033aaSdp78419 }
465102033aaSdp78419
466ce8eb11aSdp78419 uint_t
page_convert_color_cpu(uint_t ncolor,uchar_t szc,uchar_t nszc)467ce8eb11aSdp78419 page_convert_color_cpu(uint_t ncolor, uchar_t szc, uchar_t nszc)
468ce8eb11aSdp78419 {
469ce8eb11aSdp78419 uint_t color;
470ce8eb11aSdp78419
471ce8eb11aSdp78419 ASSERT(nszc > szc);
472ce8eb11aSdp78419 ASSERT(nszc <= TTE256M);
473ce8eb11aSdp78419 ncolor = n2_color2hash(ncolor, nszc, 0);
474ce8eb11aSdp78419 color = ncolor << (nhbits[szc] - nhbits[nszc]);
475ce8eb11aSdp78419 color = n2_hash2color(color, szc);
476ce8eb11aSdp78419 return (color);
477ce8eb11aSdp78419 }
478ce8eb11aSdp78419
479ce8eb11aSdp78419 #define PAPFN_2_MNODE(pfn) \
480ce8eb11aSdp78419 (((pfn) & it->mi_mnode_pfn_mask) >> it->mi_mnode_pfn_shift)
481ce8eb11aSdp78419
482102033aaSdp78419 /*ARGSUSED*/
483102033aaSdp78419 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)484102033aaSdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
485ce8eb11aSdp78419 uint_t ceq_mask, uint_t color_mask, void *cookie)
486102033aaSdp78419 {
487ce8eb11aSdp78419 mem_node_iterator_t *it = cookie;
488102033aaSdp78419 pfn_t pstep = PNUM_SIZE(szc);
489102033aaSdp78419 pfn_t npfn, pfn_ceq_mask, pfn_color;
490102033aaSdp78419 pfn_t tmpmask, mask = (pfn_t)-1;
491ce8eb11aSdp78419 uint_t pfnmn;
492102033aaSdp78419
493102033aaSdp78419 ASSERT((color & ~ceq_mask) == 0);
494ce8eb11aSdp78419 ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
495102033aaSdp78419
496ce8eb11aSdp78419 /* convert RA to PA for accurate color calculation */
497ce8eb11aSdp78419 if (it->mi_init) {
498ce8eb11aSdp78419 /* first call after it, so cache these values */
499ce8eb11aSdp78419 it->mi_hash_ceq_mask =
500ce8eb11aSdp78419 n2_color2hash(ceq_mask, szc, it->mi_mnode_mask);
501ce8eb11aSdp78419 it->mi_hash_color =
502ce8eb11aSdp78419 n2_color2hash(color, szc, it->mi_mnode);
503ce8eb11aSdp78419 it->mi_init = 0;
504ce8eb11aSdp78419 } else {
505ce8eb11aSdp78419 ASSERT(it->mi_hash_ceq_mask ==
506ce8eb11aSdp78419 n2_color2hash(ceq_mask, szc, it->mi_mnode_mask));
507ce8eb11aSdp78419 ASSERT(it->mi_hash_color ==
508ce8eb11aSdp78419 n2_color2hash(color, szc, it->mi_mnode));
509ce8eb11aSdp78419 }
510ce8eb11aSdp78419 ceq_mask = it->mi_hash_ceq_mask;
511ce8eb11aSdp78419 color = it->mi_hash_color;
512ce8eb11aSdp78419 pfn += it->mi_ra_to_pa;
513ce8eb11aSdp78419
514ce8eb11aSdp78419 /* restart here when we switch memblocks */
515ce8eb11aSdp78419 next_mem_block:
516ce8eb11aSdp78419 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)) {
519102033aaSdp78419
5204df55fdeSJanie Lu /* we start from the page with correct color and mnode */
521102033aaSdp78419 if (szc >= TTE512K) {
522102033aaSdp78419 if (szc >= TTE4M) {
523102033aaSdp78419 /* page color is PA[32:28] */
524102033aaSdp78419 pfn_ceq_mask = ceq_mask << 15;
525102033aaSdp78419 } else {
526102033aaSdp78419 /* page color is PA[32:28].PA[19:19] */
527102033aaSdp78419 pfn_ceq_mask = ((ceq_mask & 1) << 6) |
528102033aaSdp78419 ((ceq_mask >> 1) << 15);
529102033aaSdp78419 }
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;
535ce8eb11aSdp78419 npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
536ce8eb11aSdp78419 goto done;
537102033aaSdp78419 } else {
538102033aaSdp78419 /*
539102033aaSdp78419 * We deal 64K or 8K page. Check if we could the
540102033aaSdp78419 * satisfy the request without changing PA[32:28]
541102033aaSdp78419 */
542102033aaSdp78419 pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
543ce8eb11aSdp78419 pfn_ceq_mask |= it->mi_mnode_pfn_mask;
544102033aaSdp78419 npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
545102033aaSdp78419
546102033aaSdp78419 if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
547ce8eb11aSdp78419 goto done;
548102033aaSdp78419
549102033aaSdp78419 /*
550102033aaSdp78419 * for next pfn we have to change bits PA[32:28]
551102033aaSdp78419 * set PA[63:28] and PA[19:18] of the next pfn
552102033aaSdp78419 */
553102033aaSdp78419 npfn = (pfn >> 15) << 15;
554102033aaSdp78419 npfn |= (ceq_mask & color & 3) << 5;
555102033aaSdp78419 pfn_ceq_mask = (szc == TTE8K) ? 0 :
556102033aaSdp78419 (ceq_mask & 0x1c) << 13;
557ce8eb11aSdp78419 pfn_ceq_mask |= it->mi_mnode_pfn_mask;
558102033aaSdp78419 npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
559102033aaSdp78419
560102033aaSdp78419 /*
561102033aaSdp78419 * set bits PA[17:13] to match the color
562102033aaSdp78419 */
563ce8eb11aSdp78419 npfn |= ((npfn >> 15) ^ (color >> 2)) & (ceq_mask >> 2);
564ce8eb11aSdp78419 goto done;
565102033aaSdp78419 }
566102033aaSdp78419 }
567102033aaSdp78419
568102033aaSdp78419 /*
569102033aaSdp78419 * we start from the page with incorrect color - rare case
570102033aaSdp78419 */
571102033aaSdp78419 if (szc >= TTE512K) {
572102033aaSdp78419 if (szc >= TTE4M) {
573102033aaSdp78419 /* page color is in bits PA[32:28] */
574102033aaSdp78419 npfn = ((pfn >> 20) << 20) | (color << 15);
575102033aaSdp78419 pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
576102033aaSdp78419 } else {
577102033aaSdp78419 /* try get the right color by changing bit PA[19:19] */
578102033aaSdp78419 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))
582ce8eb11aSdp78419 goto done;
583102033aaSdp78419
584102033aaSdp78419 /* page color is PA[32:28].PA[19:19] */
585102033aaSdp78419 pfn_ceq_mask = ((ceq_mask & 1) << 6) |
586102033aaSdp78419 ((ceq_mask >> 1) << 15) | (0xff << 7);
587102033aaSdp78419 pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
588102033aaSdp78419 npfn = ((pfn >> 20) << 20) | pfn_color;
589102033aaSdp78419 }
590102033aaSdp78419
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;
601102033aaSdp78419 while (npfn <= pfn) {
602102033aaSdp78419 npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
603102033aaSdp78419 }
604ce8eb11aSdp78419 goto done;
605102033aaSdp78419 }
606102033aaSdp78419
607102033aaSdp78419 /*
608102033aaSdp78419 * We deal 64K or 8K page of incorrect color.
609102033aaSdp78419 * Try correcting color without changing PA[32:28]
610102033aaSdp78419 */
611102033aaSdp78419 pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
612102033aaSdp78419 pfn_color = ((color & 3) << 5) | (color >> 2);
613ce8eb11aSdp78419 if (pfnmn == it->mi_mnode) {
614102033aaSdp78419 npfn = (pfn & ~(pfn_t)0x7f);
615102033aaSdp78419 npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
616102033aaSdp78419 npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
617102033aaSdp78419
618ce8eb11aSdp78419 if (((page_papfn_2_color_cpu(npfn, szc) ^ color) &
619ce8eb11aSdp78419 ceq_mask) == 0) {
620102033aaSdp78419 /* the color is fixed - find the next page */
621ce8eb11aSdp78419 pfn_ceq_mask |= it->mi_mnode_pfn_mask;
622102033aaSdp78419 while (npfn <= pfn) {
623ce8eb11aSdp78419 npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask,
624ce8eb11aSdp78419 mask);
625102033aaSdp78419 }
626102033aaSdp78419 if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
627ce8eb11aSdp78419 goto done;
628ce8eb11aSdp78419 }
629102033aaSdp78419 }
630102033aaSdp78419
631102033aaSdp78419 /* to fix the color need to touch PA[32:28] */
632102033aaSdp78419 npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
633102033aaSdp78419 (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
634ce8eb11aSdp78419
635ce8eb11aSdp78419 /* fix mnode if input pfn is in the wrong mnode. */
636ce8eb11aSdp78419 if ((pfnmn = PAPFN_2_MNODE(npfn)) != it->mi_mnode) {
637ce8eb11aSdp78419 npfn += ((it->mi_mnode - pfnmn) & it->mi_mnode_mask) <<
638ce8eb11aSdp78419 it->mi_mnode_pfn_shift;
639ce8eb11aSdp78419 }
640ce8eb11aSdp78419
641102033aaSdp78419 tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
642ce8eb11aSdp78419 tmpmask |= it->mi_mnode_pfn_mask;
643102033aaSdp78419
644102033aaSdp78419 while (npfn <= pfn) {
645102033aaSdp78419 npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
646102033aaSdp78419 }
647102033aaSdp78419
648102033aaSdp78419 /* set bits PA[19:13] to match the color */
649102033aaSdp78419 npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
650102033aaSdp78419 npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
651102033aaSdp78419
652ce8eb11aSdp78419 done:
653ce8eb11aSdp78419 ASSERT(((page_papfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
654ce8eb11aSdp78419 ASSERT(PAPFN_2_MNODE(npfn) == it->mi_mnode);
655ce8eb11aSdp78419
656ce8eb11aSdp78419 /* PA to RA */
657ce8eb11aSdp78419 npfn -= it->mi_ra_to_pa;
658ce8eb11aSdp78419
659ce8eb11aSdp78419 /* check for possible memblock switch */
660ce8eb11aSdp78419 if (npfn > it->mi_mblock_end) {
661b779d3e0Sdp78419 pfn = plat_mem_node_iterator_init(npfn, it->mi_mnode, szc, it,
662b779d3e0Sdp78419 0);
663ce8eb11aSdp78419 if (pfn == (pfn_t)-1)
664ce8eb11aSdp78419 return (pfn);
665ce8eb11aSdp78419 ASSERT(pfn >= it->mi_mblock_base && pfn <= it->mi_mblock_end);
666ce8eb11aSdp78419 pfn += it->mi_ra_to_pa;
667ce8eb11aSdp78419 goto next_mem_block;
668ce8eb11aSdp78419 }
669102033aaSdp78419
670102033aaSdp78419 return (npfn);
671102033aaSdp78419 }
672102033aaSdp78419
673102033aaSdp78419 /*
674102033aaSdp78419 * init page coloring
675ce8eb11aSdp78419 * VF encodes node_id for an L-group in either bit 30 or 31:30,
676ce8eb11aSdp78419 * which effectively reduces the number of colors available per mnode.
677102033aaSdp78419 */
678102033aaSdp78419 void
page_coloring_init_cpu()679102033aaSdp78419 page_coloring_init_cpu()
680102033aaSdp78419 {
681102033aaSdp78419 int i;
682ce8eb11aSdp78419 uchar_t id;
683ce8eb11aSdp78419 uchar_t lo;
684ce8eb11aSdp78419 uchar_t hi;
685ce8eb11aSdp78419 n2color_t m;
686ce8eb11aSdp78419 mem_node_iterator_t it;
687ce8eb11aSdp78419 static uchar_t idmask[] = {0, 0x7, 0x1f, 0x1f, 0x1f, 0x1f};
688102033aaSdp78419
689284953f5Sdp78419 for (i = 0; i < max_mem_nodes; i++) {
690284953f5Sdp78419 memset(&it, 0, sizeof (it));
691b779d3e0Sdp78419 if (plat_mem_node_iterator_init(0, i, 0, &it, 1) != (pfn_t)-1)
692284953f5Sdp78419 break;
693284953f5Sdp78419 }
694284953f5Sdp78419 ASSERT(i < max_mem_nodes);
695ce8eb11aSdp78419 for (i = 0; i < mmu_page_sizes; i++) {
696b02e9a2dSsvemuri (void) memset(&m, 0, sizeof (m));
697ce8eb11aSdp78419 id = it.mi_mnode_pfn_mask >> 15; /* node id mask */
698ce8eb11aSdp78419 id &= idmask[i];
699ce8eb11aSdp78419 lo = lowbit(id);
700ce8eb11aSdp78419 if (lo > 0) {
701ce8eb11aSdp78419 hi = highbit(id);
702ce8eb11aSdp78419 m.nnbits = hi - lo + 1;
703ce8eb11aSdp78419 m.nnmask = (1 << m.nnbits) - 1;
704ce8eb11aSdp78419 lo += nhbits[i] - 5;
705ce8eb11aSdp78419 m.lomask = (1 << (lo - 1)) - 1;
706ce8eb11aSdp78419 m.lobits = lo - 1;
707ce8eb11aSdp78419 }
708ce8eb11aSdp78419 hw_page_array[i].hp_colors = 1 << (nhbits[i] - m.nnbits);
709ce8eb11aSdp78419 n2color[i] = m;
710102033aaSdp78419 }
711102033aaSdp78419 }
712fe70c9cfSdp78419
713fe70c9cfSdp78419 /*
714fe70c9cfSdp78419 * group colorequiv colors on N2 by low order bits of the color first
715fe70c9cfSdp78419 */
716fe70c9cfSdp78419 void
page_set_colorequiv_arr_cpu(void)717fe70c9cfSdp78419 page_set_colorequiv_arr_cpu(void)
718fe70c9cfSdp78419 {
719fe70c9cfSdp78419 static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
720fe70c9cfSdp78419
721ce8eb11aSdp78419 nequiv_shades_log2[1] -= n2color[1].nnbits;
722fe70c9cfSdp78419 if (colorequiv > 1) {
723fe70c9cfSdp78419 int i;
724fe70c9cfSdp78419 uint_t sv_a = lowbit(colorequiv) - 1;
725fe70c9cfSdp78419
726fe70c9cfSdp78419 if (sv_a > 15)
727fe70c9cfSdp78419 sv_a = 15;
728fe70c9cfSdp78419
729fe70c9cfSdp78419 for (i = 0; i < MMU_PAGE_SIZES; i++) {
730fe70c9cfSdp78419 uint_t colors;
731fe70c9cfSdp78419 uint_t a = sv_a;
732fe70c9cfSdp78419
733fe70c9cfSdp78419 if ((colors = hw_page_array[i].hp_colors) <= 1)
734fe70c9cfSdp78419 continue;
735fe70c9cfSdp78419 while ((colors >> a) == 0)
736fe70c9cfSdp78419 a--;
737fe70c9cfSdp78419 if (a > (colorequivszc[i] & 0xf) +
738fe70c9cfSdp78419 (colorequivszc[i] >> 4)) {
739fe70c9cfSdp78419 if (a <= nequiv_shades_log2[i]) {
74059ac0c16Sdavemq colorequivszc[i] = (uchar_t)a;
741fe70c9cfSdp78419 } else {
742fe70c9cfSdp78419 colorequivszc[i] =
743fe70c9cfSdp78419 ((a - nequiv_shades_log2[i]) << 4) |
744fe70c9cfSdp78419 nequiv_shades_log2[i];
745fe70c9cfSdp78419 }
746fe70c9cfSdp78419 }
747fe70c9cfSdp78419 }
748fe70c9cfSdp78419 }
749fe70c9cfSdp78419 }
750