1 /* $NetBSD: booke_machdep.c,v 1.14 2011/06/30 00:52:58 matt Exp $ */ 2 /*- 3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects 8 * Agency and which was developed by Matt Thomas of 3am Software Foundry. 9 * 10 * This material is based upon work supported by the Defense Advanced Research 11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under 12 * Contract No. N66001-09-C-2073. 13 * Approved for Public Release, Distribution Unlimited 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #define __INTR_PRIVATE 38 #define _POWERPC_BUS_DMA_PRIVATE 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: booke_machdep.c,v 1.14 2011/06/30 00:52:58 matt Exp $"); 42 43 #include "opt_modular.h" 44 45 #include <sys/param.h> 46 #include <sys/cpu.h> 47 #include <sys/device.h> 48 #include <sys/intr.h> 49 #include <sys/mount.h> 50 #include <sys/msgbuf.h> 51 #include <sys/kernel.h> 52 #include <sys/reboot.h> 53 #include <sys/bus.h> 54 55 #include <uvm/uvm_extern.h> 56 57 #include <powerpc/cpuset.h> 58 #include <powerpc/pcb.h> 59 #include <powerpc/spr.h> 60 #include <powerpc/booke/spr.h> 61 #include <powerpc/booke/cpuvar.h> 62 63 /* 64 * Global variables used here and there 65 */ 66 paddr_t msgbuf_paddr; 67 psize_t pmemsize; 68 struct vm_map *phys_map; 69 70 #ifdef MODULAR 71 register_t cpu_psluserset = PSL_USERSET; 72 register_t cpu_pslusermod = PSL_USERMOD; 73 register_t cpu_pslusermask = PSL_USERMASK; 74 #endif 75 76 static bus_addr_t booke_dma_phys_to_bus_mem(bus_dma_tag_t, bus_addr_t); 77 static bus_addr_t booke_dma_bus_mem_to_phys(bus_dma_tag_t, bus_addr_t); 78 79 80 struct powerpc_bus_dma_tag booke_bus_dma_tag = { 81 ._dmamap_create = _bus_dmamap_create, 82 ._dmamap_destroy = _bus_dmamap_destroy, 83 ._dmamap_load = _bus_dmamap_load, 84 ._dmamap_load_mbuf = _bus_dmamap_load_mbuf, 85 ._dmamap_load_uio = _bus_dmamap_load_uio, 86 ._dmamap_load_raw = _bus_dmamap_load_raw, 87 ._dmamap_unload = _bus_dmamap_unload, 88 ._dmamap_sync = _bus_dmamap_sync, 89 ._dmamem_alloc = _bus_dmamem_alloc, 90 ._dmamem_free = _bus_dmamem_free, 91 ._dmamem_map = _bus_dmamem_map, 92 ._dmamem_unmap = _bus_dmamem_unmap, 93 ._dmamem_mmap = _bus_dmamem_mmap, 94 ._dma_phys_to_bus_mem = booke_dma_phys_to_bus_mem, 95 ._dma_bus_mem_to_phys = booke_dma_bus_mem_to_phys, 96 }; 97 98 static bus_addr_t 99 booke_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t a) 100 { 101 return a; 102 } 103 104 static bus_addr_t 105 booke_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t a) 106 { 107 return a; 108 } 109 110 struct cpu_md_ops cpu_md_ops; 111 112 struct cpu_softc cpu_softc[] = { 113 [0] = { 114 .cpu_ci = &cpu_info[0], 115 }, 116 #ifdef MULTIPROCESSOR 117 [CPU_MAXNUM-1] = { 118 .cpu_ci = &cpu_info[CPU_MAXNUM-1], 119 }, 120 #endif 121 }; 122 struct cpu_info cpu_info[] = { 123 [0] = { 124 .ci_curlwp = &lwp0, 125 .ci_tlb_info = &pmap_tlb0_info, 126 .ci_softc = &cpu_softc[0], 127 .ci_cpl = IPL_HIGH, 128 .ci_idepth = -1, 129 }, 130 #ifdef MULTIPROCESSOR 131 [CPU_MAXNUM-1] = { 132 .ci_curlwp = NULL, 133 .ci_tlb_info = &pmap_tlb0_info, 134 .ci_softc = &cpu_softc[CPU_MAXNUM-1], 135 .ci_cpl = IPL_HIGH, 136 .ci_idepth = -1, 137 }, 138 #endif 139 }; 140 __CTASSERT(__arraycount(cpu_info) == __arraycount(cpu_softc)); 141 142 /* 143 * This should probably be in autoconf! XXX 144 */ 145 char cpu_model[80]; 146 char machine[] = MACHINE; /* from <machine/param.h> */ 147 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */ 148 149 char bootpath[256]; 150 151 #if NKSYMS || defined(DDB) || defined(MODULAR) 152 void *startsym, *endsym; 153 #endif 154 155 #if defined(MULTIPROCESSOR) 156 volatile struct cpu_hatch_data cpu_hatch_data __cacheline_aligned; 157 #endif 158 159 int fake_mapiodev = 1; 160 161 void 162 booke_cpu_startup(const char *model) 163 { 164 vaddr_t minaddr, maxaddr; 165 char pbuf[9]; 166 167 strlcpy(cpu_model, model, sizeof(cpu_model)); 168 169 printf("%s%s", copyright, version); 170 171 format_bytes(pbuf, sizeof(pbuf), ctob((uint64_t)physmem)); 172 printf("total memory = %s\n", pbuf); 173 174 minaddr = 0; 175 /* 176 * Allocate a submap for physio 177 */ 178 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 179 VM_PHYS_SIZE, 0, false, NULL); 180 181 /* 182 * No need to allocate an mbuf cluster submap. Mbuf clusters 183 * are allocated via the pool allocator, and we use direct-mapped 184 * pool pages. 185 */ 186 187 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 188 printf("avail memory = %s\n", pbuf); 189 190 /* 191 * Register the tlb's evcnts 192 */ 193 pmap_tlb_info_evcnt_attach(curcpu()->ci_tlb_info); 194 195 /* 196 * Set up the board properties database. 197 */ 198 board_info_init(); 199 200 /* 201 * Now that we have VM, malloc()s are OK in bus_space. 202 */ 203 bus_space_mallocok(); 204 fake_mapiodev = 0; 205 206 #ifdef MULTIPROCESSOR 207 for (size_t i = 1; i < __arraycount(cpu_info); i++) { 208 struct cpu_info * const ci = &cpu_info[i]; 209 struct cpu_softc * const cpu = &cpu_softc[i]; 210 cpu->cpu_ci = ci; 211 cpu->cpu_bst = cpu_softc[0].cpu_bst; 212 cpu->cpu_le_bst = cpu_softc[0].cpu_le_bst; 213 cpu->cpu_bsh = cpu_softc[0].cpu_bsh; 214 cpu->cpu_highmem = cpu_softc[0].cpu_highmem; 215 ci->ci_softc = cpu; 216 ci->ci_tlb_info = &pmap_tlb0_info; 217 ci->ci_cpl = IPL_HIGH; 218 ci->ci_idepth = -1; 219 ci->ci_pmap_kern_segtab = curcpu()->ci_pmap_kern_segtab; 220 } 221 #endif /* MULTIPROCESSOR */ 222 } 223 224 static void 225 dumpsys(void) 226 { 227 228 printf("dumpsys: TBD\n"); 229 } 230 231 /* 232 * Halt or reboot the machine after syncing/dumping according to howto. 233 */ 234 void 235 cpu_reboot(int howto, char *what) 236 { 237 static int syncing; 238 static char str[256]; 239 char *ap = str, *ap1 = ap; 240 241 boothowto = howto; 242 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 243 syncing = 1; 244 vfs_shutdown(); /* sync */ 245 resettodr(); /* set wall clock */ 246 } 247 248 splhigh(); 249 250 if (!cold && (howto & RB_DUMP)) 251 dumpsys(); 252 253 doshutdownhooks(); 254 255 pmf_system_shutdown(boothowto); 256 257 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) { 258 /* Power off here if we know how...*/ 259 } 260 261 if (howto & RB_HALT) { 262 printf("halted\n\n"); 263 264 goto reboot; /* XXX for now... */ 265 266 #ifdef DDB 267 printf("dropping to debugger\n"); 268 while(1) 269 Debugger(); 270 #endif 271 } 272 273 printf("rebooting\n\n"); 274 if (what && *what) { 275 if (strlen(what) > sizeof str - 5) 276 printf("boot string too large, ignored\n"); 277 else { 278 strcpy(str, what); 279 ap1 = ap = str + strlen(str); 280 *ap++ = ' '; 281 } 282 } 283 *ap++ = '-'; 284 if (howto & RB_SINGLE) 285 *ap++ = 's'; 286 if (howto & RB_KDB) 287 *ap++ = 'd'; 288 *ap++ = 0; 289 if (ap[-2] == '-') 290 *ap1 = 0; 291 292 /* flush cache for msgbuf */ 293 dcache_wb(msgbuf_paddr, round_page(MSGBUFSIZE)); 294 295 reboot: 296 __asm volatile("msync; isync"); 297 (*cpu_md_ops.md_cpu_reset)(); 298 299 printf("%s: md_cpu_reset() failed!\n", __func__); 300 #ifdef DDB 301 for (;;) 302 Debugger(); 303 #else 304 for (;;) 305 /* nothing */; 306 #endif 307 } 308 309 /* 310 * mapiodev: 311 * 312 * Allocate vm space and mapin the I/O address. Use reserved TLB 313 * mapping if one is found. 314 */ 315 void * 316 mapiodev(paddr_t pa, psize_t len, bool prefetchable) 317 { 318 const vsize_t off = pa & PAGE_MASK; 319 320 /* 321 * See if we have reserved TLB entry for the pa. This needs to be 322 * true for console as we can't use uvm during early bootstrap. 323 */ 324 void * const p = tlb_mapiodev(pa, len, prefetchable); 325 if (p != NULL) 326 return p; 327 328 if (fake_mapiodev) 329 panic("mapiodev: no TLB entry reserved for %llx+%llx", 330 (long long)pa, (long long)len); 331 332 pa = trunc_page(pa); 333 len = round_page(off + len); 334 vaddr_t va = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY); 335 336 if (va == 0) 337 return NULL; 338 339 for (va += len, pa += len; len > 0; len -= PAGE_SIZE) { 340 va -= PAGE_SIZE; 341 pa -= PAGE_SIZE; 342 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 343 prefetchable ? 0 : PMAP_NOCACHE); 344 } 345 pmap_update(pmap_kernel()); 346 return (void *)(va + off); 347 } 348 349 void 350 unmapiodev(vaddr_t va, vsize_t len) 351 { 352 /* Nothing to do for reserved (ie. not uvm_km_alloc'd) mappings. */ 353 if (va < VM_MIN_KERNEL_ADDRESS || va > VM_MAX_KERNEL_ADDRESS) { 354 tlb_unmapiodev(va, len); 355 return; 356 } 357 358 len = round_page((va & PAGE_MASK) + len); 359 va = trunc_page(va); 360 361 pmap_kremove(va, len); 362 uvm_km_free(kernel_map, va, len, UVM_KMF_VAONLY); 363 } 364 365 void 366 cpu_evcnt_attach(struct cpu_info *ci) 367 { 368 struct cpu_softc * const cpu = ci->ci_softc; 369 const char * const xname = ci->ci_data.cpu_name; 370 371 evcnt_attach_dynamic_nozero(&ci->ci_ev_clock, EVCNT_TYPE_INTR, 372 NULL, xname, "clock"); 373 evcnt_attach_dynamic_nozero(&cpu->cpu_ev_late_clock, EVCNT_TYPE_INTR, 374 NULL, xname, "late clock"); 375 evcnt_attach_dynamic_nozero(&cpu->cpu_ev_exec_trap_sync, EVCNT_TYPE_TRAP, 376 NULL, xname, "exec pages synced (trap)"); 377 evcnt_attach_dynamic_nozero(&ci->ci_ev_traps, EVCNT_TYPE_TRAP, 378 NULL, xname, "traps"); 379 evcnt_attach_dynamic_nozero(&ci->ci_ev_kdsi, EVCNT_TYPE_TRAP, 380 &ci->ci_ev_traps, xname, "kernel DSI traps"); 381 evcnt_attach_dynamic_nozero(&ci->ci_ev_udsi, EVCNT_TYPE_TRAP, 382 &ci->ci_ev_traps, xname, "user DSI traps"); 383 evcnt_attach_dynamic_nozero(&ci->ci_ev_udsi_fatal, EVCNT_TYPE_TRAP, 384 &ci->ci_ev_udsi, xname, "user DSI failures"); 385 evcnt_attach_dynamic_nozero(&ci->ci_ev_kisi, EVCNT_TYPE_TRAP, 386 &ci->ci_ev_traps, xname, "kernel ISI traps"); 387 evcnt_attach_dynamic_nozero(&ci->ci_ev_isi, EVCNT_TYPE_TRAP, 388 &ci->ci_ev_traps, xname, "user ISI traps"); 389 evcnt_attach_dynamic_nozero(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP, 390 &ci->ci_ev_isi, xname, "user ISI failures"); 391 evcnt_attach_dynamic_nozero(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP, 392 &ci->ci_ev_traps, xname, "system call traps"); 393 evcnt_attach_dynamic_nozero(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP, 394 &ci->ci_ev_traps, xname, "PGM traps"); 395 evcnt_attach_dynamic_nozero(&ci->ci_ev_debug, EVCNT_TYPE_TRAP, 396 &ci->ci_ev_traps, xname, "debug traps"); 397 evcnt_attach_dynamic_nozero(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP, 398 &ci->ci_ev_traps, xname, "FPU unavailable traps"); 399 evcnt_attach_dynamic_nozero(&ci->ci_ev_fpusw, EVCNT_TYPE_MISC, 400 &ci->ci_ev_fpu, xname, "FPU context switches"); 401 evcnt_attach_dynamic_nozero(&ci->ci_ev_ali, EVCNT_TYPE_TRAP, 402 &ci->ci_ev_traps, xname, "user alignment traps"); 403 evcnt_attach_dynamic_nozero(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP, 404 &ci->ci_ev_ali, xname, "user alignment traps"); 405 evcnt_attach_dynamic_nozero(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP, 406 &ci->ci_ev_umchk, xname, "user MCHK failures"); 407 evcnt_attach_dynamic_nozero(&ci->ci_ev_vec, EVCNT_TYPE_TRAP, 408 &ci->ci_ev_traps, xname, "SPE unavailable"); 409 evcnt_attach_dynamic_nozero(&ci->ci_ev_vecsw, EVCNT_TYPE_MISC, 410 &ci->ci_ev_vec, xname, "SPE context switches"); 411 evcnt_attach_dynamic_nozero(&ci->ci_ev_ipi, EVCNT_TYPE_INTR, 412 NULL, xname, "IPIs"); 413 evcnt_attach_dynamic_nozero(&ci->ci_ev_tlbmiss_soft, EVCNT_TYPE_TRAP, 414 &ci->ci_ev_traps, xname, "soft tlb misses"); 415 evcnt_attach_dynamic_nozero(&ci->ci_ev_dtlbmiss_hard, EVCNT_TYPE_TRAP, 416 &ci->ci_ev_traps, xname, "data tlb misses"); 417 evcnt_attach_dynamic_nozero(&ci->ci_ev_itlbmiss_hard, EVCNT_TYPE_TRAP, 418 &ci->ci_ev_traps, xname, "inst tlb misses"); 419 } 420 421 #ifdef MULTIPROCESSOR 422 register_t 423 cpu_hatch(void) 424 { 425 volatile struct cpuset_info * const csi = &cpuset_info; 426 const size_t id = cpu_number(); 427 428 /* 429 * We've hatched so tell the spinup code. 430 */ 431 CPUSET_ADD(csi->cpus_hatched, id); 432 433 /* 434 * Loop until running bit for this cpu is set. 435 */ 436 while (!CPUSET_HAS_P(csi->cpus_running, id)) { 437 continue; 438 } 439 440 /* 441 * Now that we are active, start the clocks. 442 */ 443 cpu_initclocks(); 444 445 /* 446 * Return sp of the idlelwp. Which we should be already using but ... 447 */ 448 return curcpu()->ci_curpcb->pcb_sp; 449 } 450 451 void 452 cpu_boot_secondary_processors(void) 453 { 454 volatile struct cpuset_info * const csi = &cpuset_info; 455 CPU_INFO_ITERATOR cii; 456 struct cpu_info *ci; 457 __cpuset_t running = CPUSET_NULLSET; 458 459 for (CPU_INFO_FOREACH(cii, ci)) { 460 /* 461 * Skip this CPU if it didn't sucessfully hatch. 462 */ 463 if (! CPUSET_HAS_P(csi->cpus_hatched, cpu_index(ci))) 464 continue; 465 466 KASSERT(!CPU_IS_PRIMARY(ci)); 467 KASSERT(ci->ci_data.cpu_idlelwp); 468 469 CPUSET_ADD(running, cpu_index(ci)); 470 } 471 KASSERT(CPUSET_EQUAL_P(csi->cpus_hatched, running)); 472 if (!CPUSET_EMPTY_P(running)) { 473 CPUSET_ADDSET(csi->cpus_running, running); 474 } 475 } 476 #endif 477 478 uint32_t 479 cpu_read_4(bus_addr_t a) 480 { 481 struct cpu_softc * const cpu = curcpu()->ci_softc; 482 // printf(" %s(%p, %x, %x)", __func__, cpu->cpu_bst, cpu->cpu_bsh, a); 483 return bus_space_read_4(cpu->cpu_bst, cpu->cpu_bsh, a); 484 } 485 486 uint8_t 487 cpu_read_1(bus_addr_t a) 488 { 489 struct cpu_softc * const cpu = curcpu()->ci_softc; 490 // printf(" %s(%p, %x, %x)", __func__, cpu->cpu_bst, cpu->cpu_bsh, a); 491 return bus_space_read_1(cpu->cpu_bst, cpu->cpu_bsh, a); 492 } 493 494 void 495 cpu_write_4(bus_addr_t a, uint32_t v) 496 { 497 struct cpu_softc * const cpu = curcpu()->ci_softc; 498 bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh, a, v); 499 } 500 501 void 502 cpu_write_1(bus_addr_t a, uint8_t v) 503 { 504 struct cpu_softc * const cpu = curcpu()->ci_softc; 505 bus_space_write_1(cpu->cpu_bst, cpu->cpu_bsh, a, v); 506 } 507 508 void 509 booke_sstep(struct trapframe *tf) 510 { 511 KASSERT(tf->tf_srr1 & PSL_DE); 512 const uint32_t insn = ufetch_32((const void *)tf->tf_srr0); 513 register_t dbcr0 = DBCR0_IAC1 | DBCR0_IDM; 514 register_t dbcr1 = DBCR1_IAC1US_USER | DBCR1_IAC1ER_DS1; 515 if ((insn >> 28) == 4) { 516 uint32_t iac2 = 0; 517 if ((insn >> 26) == 0x12) { 518 const int32_t off = (((int32_t)insn << 6) >> 6) & ~3; 519 iac2 = ((insn & 2) ? 0 : tf->tf_srr0) + off; 520 dbcr0 |= DBCR0_IAC2; 521 } else if ((insn >> 26) == 0x10) { 522 const int16_t off = insn & ~3; 523 iac2 = ((insn & 2) ? 0 : tf->tf_srr0) + off; 524 dbcr0 |= DBCR0_IAC2; 525 } else if ((insn & 0xfc00ffde) == 0x4c000420) { 526 iac2 = tf->tf_ctr; 527 dbcr0 |= DBCR0_IAC2; 528 } else if ((insn & 0xfc00ffde) == 0x4c000020) { 529 iac2 = tf->tf_lr; 530 dbcr0 |= DBCR0_IAC2; 531 } 532 if (dbcr0 & DBCR0_IAC2) { 533 dbcr1 |= DBCR1_IAC2US_USER | DBCR1_IAC2ER_DS1; 534 mtspr(SPR_IAC2, iac2); 535 } 536 } 537 mtspr(SPR_IAC1, tf->tf_srr0 + 4); 538 mtspr(SPR_DBCR1, dbcr1); 539 mtspr(SPR_DBCR0, dbcr0); 540 } 541