1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/systm.h> 31 #include <sys/archsystm.h> 32 #include <sys/machparam.h> 33 #include <sys/machsystm.h> 34 #include <sys/cpu.h> 35 #include <sys/elf_SPARC.h> 36 #include <vm/hat_sfmmu.h> 37 #include <vm/page.h> 38 #include <sys/cpuvar.h> 39 #include <sys/async.h> 40 #include <sys/cmn_err.h> 41 #include <sys/debug.h> 42 #include <sys/dditypes.h> 43 #include <sys/sunddi.h> 44 #include <sys/cpu_module.h> 45 #include <sys/prom_debug.h> 46 #include <sys/vmsystm.h> 47 #include <sys/prom_plat.h> 48 #include <sys/sysmacros.h> 49 #include <sys/intreg.h> 50 #include <sys/machtrap.h> 51 #include <sys/ontrap.h> 52 #include <sys/ivintr.h> 53 #include <sys/atomic.h> 54 #include <sys/panic.h> 55 #include <sys/dtrace.h> 56 #include <sys/simulate.h> 57 #include <sys/fault.h> 58 #include <sys/niagararegs.h> 59 #include <sys/trapstat.h> 60 #include <sys/hsvc.h> 61 62 #define NI_MMU_PAGESIZE_MASK ((1 << TTE8K) | (1 << TTE64K) | (1 << TTE4M) \ 63 | (1 << TTE256M)) 64 65 uint_t root_phys_addr_lo_mask = 0xffffffffU; 66 static niagara_mmustat_t *cpu_tstat_va; /* VA of mmustat buffer */ 67 static uint64_t cpu_tstat_pa; /* PA of mmustat buffer */ 68 char cpu_module_name[] = "SUNW,UltraSPARC-T1"; 69 70 /* 71 * Hypervisor services information for the NIAGARA CPU module 72 */ 73 static boolean_t niagara_hsvc_available = B_TRUE; 74 static uint64_t niagara_sup_minor; /* Supported minor number */ 75 static hsvc_info_t niagara_hsvc = { 76 HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA_CPU, 1, 0, cpu_module_name 77 }; 78 79 void 80 cpu_setup(void) 81 { 82 extern int mmu_exported_pagesize_mask; 83 extern int cpc_has_overflow_intr; 84 int status; 85 char *ni_isa_set[] = { 86 "sparcv9+vis", 87 "sparcv9+vis2", 88 "sparcv8plus+vis", 89 "sparcv8plus+vis2", 90 NULL 91 }; 92 93 /* 94 * Negotiate the API version for Niagara specific hypervisor 95 * services. 96 */ 97 status = hsvc_register(&niagara_hsvc, &niagara_sup_minor); 98 if (status != 0) { 99 cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services " 100 "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d\n", 101 niagara_hsvc.hsvc_modname, niagara_hsvc.hsvc_group, 102 niagara_hsvc.hsvc_major, niagara_hsvc.hsvc_minor, status); 103 niagara_hsvc_available = B_FALSE; 104 } 105 106 /* 107 * The setup common to all CPU modules is done in cpu_setup_common 108 * routine. 109 */ 110 cpu_setup_common(ni_isa_set); 111 112 cache |= (CACHE_PTAG | CACHE_IOCOHERENT); 113 114 if (broken_md_flag) { 115 /* 116 * Turn on the missing bits supported by Niagara CPU in 117 * MMU pagesize mask returned by MD. 118 */ 119 mmu_exported_pagesize_mask |= NI_MMU_PAGESIZE_MASK; 120 } else { 121 if ((mmu_exported_pagesize_mask & 122 DEFAULT_SUN4V_MMU_PAGESIZE_MASK) != 123 DEFAULT_SUN4V_MMU_PAGESIZE_MASK) 124 cmn_err(CE_PANIC, "machine description" 125 " does not have required sun4v page sizes" 126 " 8K, 64K and 4M: MD mask is 0x%x", 127 mmu_exported_pagesize_mask); 128 } 129 130 cpu_hwcap_flags |= AV_SPARC_ASI_BLK_INIT; 131 132 /* 133 * Niagara supports a 48-bit subset of the full 64-bit virtual 134 * address space. Virtual addresses between 0x0000800000000000 135 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole" 136 * and must never be mapped. In addition, software must not use 137 * pages within 4GB of the VA hole as instruction pages to 138 * avoid problems with prefetching into the VA hole. 139 */ 140 hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32)); 141 hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32)); 142 143 /* 144 * Niagara has a performance counter overflow interrupt 145 */ 146 cpc_has_overflow_intr = 1; 147 148 shctx_on = 0; 149 } 150 151 #define MB(n) ((n) * 1024 * 1024) 152 /* 153 * Set the magic constants of the implementation. 154 */ 155 void 156 cpu_fiximp(struct cpu_node *cpunode) 157 { 158 /* 159 * The Cache node is optional in MD. Therefore in case "Cache" 160 * node does not exists in MD, set the default L2 cache associativity, 161 * size, linesize. 162 */ 163 if (cpunode->ecache_size == 0) 164 cpunode->ecache_size = MB(3); 165 if (cpunode->ecache_linesize == 0) 166 cpunode->ecache_linesize = 64; 167 if (cpunode->ecache_associativity == 0) 168 cpunode->ecache_associativity = 12; 169 } 170 171 void 172 cpu_map_exec_units(struct cpu *cp) 173 { 174 ASSERT(MUTEX_HELD(&cpu_lock)); 175 176 /* 177 * The cpu_ipipe and cpu_fpu fields are initialized based on 178 * the execution unit sharing information from the MD. They 179 * default to the CPU id in the absence of such information. 180 */ 181 cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping; 182 if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND) 183 cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id); 184 185 cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping; 186 if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND) 187 cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id); 188 189 /* 190 * Niagara defines the the core to be at the ipipe level 191 */ 192 cp->cpu_m.cpu_core = cp->cpu_m.cpu_ipipe; 193 194 /* 195 * Niagara systems just have one chip. Therefore, the chip id 196 * is always 0. 197 */ 198 cp->cpu_m.cpu_chip = 0; 199 } 200 201 static int niagara_cpucnt; 202 203 void 204 cpu_init_private(struct cpu *cp) 205 { 206 extern void niagara_kstat_init(void); 207 208 ASSERT(MUTEX_HELD(&cpu_lock)); 209 210 cpu_map_exec_units(cp); 211 212 if ((niagara_cpucnt++ == 0) && (niagara_hsvc_available == B_TRUE)) 213 niagara_kstat_init(); 214 } 215 216 /*ARGSUSED*/ 217 void 218 cpu_uninit_private(struct cpu *cp) 219 { 220 extern void niagara_kstat_fini(void); 221 222 ASSERT(MUTEX_HELD(&cpu_lock)); 223 224 if ((--niagara_cpucnt == 0) && (niagara_hsvc_available == B_TRUE)) 225 niagara_kstat_fini(); 226 } 227 228 /* 229 * On Niagara, any flush will cause all preceding stores to be 230 * synchronized wrt the i$, regardless of address or ASI. In fact, 231 * the address is ignored, so we always flush address 0. 232 */ 233 void 234 dtrace_flush_sec(uintptr_t addr) 235 { 236 doflush(0); 237 } 238 239 #define IS_FLOAT(i) (((i) & 0x1000000) != 0) 240 #define IS_IBIT_SET(x) (x & 0x2000) 241 #define IS_VIS1(op, op3)(op == 2 && op3 == 0x36) 242 #define IS_PARTIAL_OR_SHORT_FLOAT_LD_ST(op, op3, asi) \ 243 (op == 3 && (op3 == IOP_V8_LDDFA || \ 244 op3 == IOP_V8_STDFA) && asi > ASI_SNFL) 245 int 246 vis1_partial_support(struct regs *rp, k_siginfo_t *siginfo, uint_t *fault) 247 { 248 char *badaddr; 249 int instr; 250 uint_t optype, op3, asi; 251 uint_t rd, ignor; 252 253 if (!USERMODE(rp->r_tstate)) 254 return (-1); 255 256 instr = fetch_user_instr((caddr_t)rp->r_pc); 257 258 rd = (instr >> 25) & 0x1f; 259 optype = (instr >> 30) & 0x3; 260 op3 = (instr >> 19) & 0x3f; 261 ignor = (instr >> 5) & 0xff; 262 if (IS_IBIT_SET(instr)) { 263 asi = (uint32_t)((rp->r_tstate >> TSTATE_ASI_SHIFT) & 264 TSTATE_ASI_MASK); 265 } else { 266 asi = ignor; 267 } 268 269 if (!IS_VIS1(optype, op3) && 270 !IS_PARTIAL_OR_SHORT_FLOAT_LD_ST(optype, op3, asi)) { 271 return (-1); 272 } 273 switch (simulate_unimp(rp, &badaddr)) { 274 case SIMU_RETRY: 275 break; /* regs are already set up */ 276 /*NOTREACHED*/ 277 278 case SIMU_SUCCESS: 279 /* 280 * skip the successfully 281 * simulated instruction 282 */ 283 rp->r_pc = rp->r_npc; 284 rp->r_npc += 4; 285 break; 286 /*NOTREACHED*/ 287 288 case SIMU_FAULT: 289 siginfo->si_signo = SIGSEGV; 290 siginfo->si_code = SEGV_MAPERR; 291 siginfo->si_addr = badaddr; 292 *fault = FLTBOUNDS; 293 break; 294 295 case SIMU_DZERO: 296 siginfo->si_signo = SIGFPE; 297 siginfo->si_code = FPE_INTDIV; 298 siginfo->si_addr = (caddr_t)rp->r_pc; 299 *fault = FLTIZDIV; 300 break; 301 302 case SIMU_UNALIGN: 303 siginfo->si_signo = SIGBUS; 304 siginfo->si_code = BUS_ADRALN; 305 siginfo->si_addr = badaddr; 306 *fault = FLTACCESS; 307 break; 308 309 case SIMU_ILLEGAL: 310 default: 311 siginfo->si_signo = SIGILL; 312 op3 = (instr >> 19) & 0x3F; 313 if ((IS_FLOAT(instr) && (op3 == IOP_V8_STQFA) || 314 (op3 == IOP_V8_STDFA))) 315 siginfo->si_code = ILL_ILLADR; 316 else 317 siginfo->si_code = ILL_ILLOPC; 318 siginfo->si_addr = (caddr_t)rp->r_pc; 319 *fault = FLTILL; 320 break; 321 } 322 return (0); 323 } 324 325 /* 326 * Trapstat support for Niagara processor 327 */ 328 int 329 cpu_trapstat_conf(int cmd) 330 { 331 size_t len; 332 uint64_t mmustat_pa, hvret; 333 int status = 0; 334 335 if (niagara_hsvc_available == B_FALSE) 336 return (ENOTSUP); 337 338 switch (cmd) { 339 case CPU_TSTATCONF_INIT: 340 ASSERT(cpu_tstat_va == NULL); 341 len = (NCPU+1) * sizeof (niagara_mmustat_t); 342 cpu_tstat_va = contig_mem_alloc_align(len, 343 sizeof (niagara_mmustat_t)); 344 if (cpu_tstat_va == NULL) 345 status = EAGAIN; 346 else { 347 bzero(cpu_tstat_va, len); 348 cpu_tstat_pa = va_to_pa(cpu_tstat_va); 349 } 350 break; 351 352 case CPU_TSTATCONF_FINI: 353 if (cpu_tstat_va) { 354 len = (NCPU+1) * sizeof (niagara_mmustat_t); 355 contig_mem_free(cpu_tstat_va, len); 356 cpu_tstat_va = NULL; 357 cpu_tstat_pa = 0; 358 } 359 break; 360 361 case CPU_TSTATCONF_ENABLE: 362 hvret = hv_niagara_mmustat_conf((cpu_tstat_pa + 363 (CPU->cpu_id+1) * sizeof (niagara_mmustat_t)), 364 (uint64_t *)&mmustat_pa); 365 if (hvret != H_EOK) 366 status = EINVAL; 367 break; 368 369 case CPU_TSTATCONF_DISABLE: 370 hvret = hv_niagara_mmustat_conf(0, (uint64_t *)&mmustat_pa); 371 if (hvret != H_EOK) 372 status = EINVAL; 373 break; 374 375 default: 376 status = EINVAL; 377 break; 378 } 379 return (status); 380 } 381 382 void 383 cpu_trapstat_data(void *buf, uint_t tstat_pgszs) 384 { 385 niagara_mmustat_t *mmustatp; 386 tstat_pgszdata_t *tstatp = (tstat_pgszdata_t *)buf; 387 int i, pgcnt; 388 389 if (cpu_tstat_va == NULL) 390 return; 391 392 mmustatp = &((niagara_mmustat_t *)cpu_tstat_va)[CPU->cpu_id+1]; 393 if (tstat_pgszs > NIAGARA_MMUSTAT_PGSZS) 394 tstat_pgszs = NIAGARA_MMUSTAT_PGSZS; 395 396 for (i = 0; i < tstat_pgszs; i++, tstatp++) { 397 tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 398 mmustatp->kitsb[i].tsbhit_count; 399 tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 400 mmustatp->kitsb[i].tsbhit_time; 401 tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 402 mmustatp->uitsb[i].tsbhit_count; 403 tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 404 mmustatp->uitsb[i].tsbhit_time; 405 tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 406 mmustatp->kdtsb[i].tsbhit_count; 407 tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 408 mmustatp->kdtsb[i].tsbhit_time; 409 tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 410 mmustatp->udtsb[i].tsbhit_count; 411 tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 412 mmustatp->udtsb[i].tsbhit_time; 413 } 414 } 415