machdep.c revision 265954
1/*- 2 * Copyright (C) 2006-2012 Semihalf 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 17 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 19 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25/*- 26 * Copyright (C) 2001 Benno Rice 27 * All rights reserved. 28 * 29 * Redistribution and use in source and binary forms, with or without 30 * modification, are permitted provided that the following conditions 31 * are met: 32 * 1. Redistributions of source code must retain the above copyright 33 * notice, this list of conditions and the following disclaimer. 34 * 2. Redistributions in binary form must reproduce the above copyright 35 * notice, this list of conditions and the following disclaimer in the 36 * documentation and/or other materials provided with the distribution. 37 * 38 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 39 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 41 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 44 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 45 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 46 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 47 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $ 49 */ 50/*- 51 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 52 * Copyright (C) 1995, 1996 TooLs GmbH. 53 * All rights reserved. 54 * 55 * Redistribution and use in source and binary forms, with or without 56 * modification, are permitted provided that the following conditions 57 * are met: 58 * 1. Redistributions of source code must retain the above copyright 59 * notice, this list of conditions and the following disclaimer. 60 * 2. Redistributions in binary form must reproduce the above copyright 61 * notice, this list of conditions and the following disclaimer in the 62 * documentation and/or other materials provided with the distribution. 63 * 3. All advertising materials mentioning features or use of this software 64 * must display the following acknowledgement: 65 * This product includes software developed by TooLs GmbH. 66 * 4. The name of TooLs GmbH may not be used to endorse or promote products 67 * derived from this software without specific prior written permission. 68 * 69 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 70 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 71 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 72 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 73 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 74 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 75 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 76 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 77 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 78 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 79 */ 80 81#include <sys/cdefs.h> 82__FBSDID("$FreeBSD: stable/10/sys/powerpc/booke/machdep.c 265954 2014-05-13 16:59:50Z ian $"); 83 84#include "opt_compat.h" 85#include "opt_ddb.h" 86#include "opt_kstack_pages.h" 87#include "opt_platform.h" 88 89#include <sys/cdefs.h> 90#include <sys/types.h> 91#include <sys/param.h> 92#include <sys/proc.h> 93#include <sys/systm.h> 94#include <sys/time.h> 95#include <sys/bio.h> 96#include <sys/buf.h> 97#include <sys/bus.h> 98#include <sys/cons.h> 99#include <sys/cpu.h> 100#include <sys/kdb.h> 101#include <sys/kernel.h> 102#include <sys/lock.h> 103#include <sys/mutex.h> 104#include <sys/rwlock.h> 105#include <sys/sysctl.h> 106#include <sys/exec.h> 107#include <sys/ktr.h> 108#include <sys/syscallsubr.h> 109#include <sys/sysproto.h> 110#include <sys/signalvar.h> 111#include <sys/sysent.h> 112#include <sys/imgact.h> 113#include <sys/msgbuf.h> 114#include <sys/ptrace.h> 115 116#include <vm/vm.h> 117#include <vm/pmap.h> 118#include <vm/vm_page.h> 119#include <vm/vm_object.h> 120#include <vm/vm_pager.h> 121 122#include <machine/cpu.h> 123#include <machine/kdb.h> 124#include <machine/reg.h> 125#include <machine/vmparam.h> 126#include <machine/spr.h> 127#include <machine/hid.h> 128#include <machine/psl.h> 129#include <machine/trap.h> 130#include <machine/md_var.h> 131#include <machine/mmuvar.h> 132#include <machine/sigframe.h> 133#include <machine/machdep.h> 134#include <machine/metadata.h> 135#include <machine/platform.h> 136 137#include <sys/linker.h> 138#include <sys/reboot.h> 139 140#include <dev/fdt/fdt_common.h> 141#include <dev/ofw/openfirm.h> 142 143#ifdef DDB 144extern vm_offset_t ksym_start, ksym_end; 145#endif 146 147#ifdef DEBUG 148#define debugf(fmt, args...) printf(fmt, ##args) 149#else 150#define debugf(fmt, args...) 151#endif 152 153extern unsigned char kernel_text[]; 154extern unsigned char _etext[]; 155extern unsigned char _edata[]; 156extern unsigned char __bss_start[]; 157extern unsigned char __sbss_start[]; 158extern unsigned char __sbss_end[]; 159extern unsigned char _end[]; 160 161/* 162 * Bootinfo is passed to us by legacy loaders. Save the address of the 163 * structure to handle backward compatibility. 164 */ 165uint32_t *bootinfo; 166 167struct kva_md_info kmi; 168struct pcpu __pcpu[MAXCPU]; 169struct trapframe frame0; 170int cold = 1; 171long realmem = 0; 172long Maxmem = 0; 173char machine[] = "powerpc"; 174SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, ""); 175 176int cacheline_size = 32; 177 178SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size, 179 CTLFLAG_RD, &cacheline_size, 0, ""); 180 181int hw_direct_map = 0; 182 183static void cpu_booke_startup(void *); 184SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_booke_startup, NULL); 185 186void print_kernel_section_addr(void); 187void print_kenv(void); 188u_int booke_init(uint32_t, uint32_t); 189 190extern int elf32_nxstack; 191 192static void 193cpu_booke_startup(void *dummy) 194{ 195 int indx; 196 unsigned long size; 197 198 /* Initialise the decrementer-based clock. */ 199 decr_init(); 200 201 /* Good {morning,afternoon,evening,night}. */ 202 cpu_setup(PCPU_GET(cpuid)); 203 204 printf("real memory = %lu (%ld MB)\n", ptoa(physmem), 205 ptoa(physmem) / 1048576); 206 realmem = physmem; 207 208 /* Display any holes after the first chunk of extended memory. */ 209 if (bootverbose) { 210 printf("Physical memory chunk(s):\n"); 211 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 212 size = phys_avail[indx + 1] - phys_avail[indx]; 213 214 printf("0x%08x - 0x%08x, %lu bytes (%lu pages)\n", 215 phys_avail[indx], phys_avail[indx + 1] - 1, 216 size, size / PAGE_SIZE); 217 } 218 } 219 220 vm_ksubmap_init(&kmi); 221 222 printf("avail memory = %lu (%ld MB)\n", ptoa(cnt.v_free_count), 223 ptoa(cnt.v_free_count) / 1048576); 224 225 /* Set up buffers, so they can be used to read disk labels. */ 226 bufinit(); 227 vm_pager_bufferinit(); 228 229 /* Cpu supports execution permissions on the pages. */ 230 elf32_nxstack = 1; 231} 232 233static char * 234kenv_next(char *cp) 235{ 236 237 if (cp != NULL) { 238 while (*cp != 0) 239 cp++; 240 cp++; 241 if (*cp == 0) 242 cp = NULL; 243 } 244 return (cp); 245} 246 247void 248print_kenv(void) 249{ 250 int len; 251 char *cp; 252 253 debugf("loader passed (static) kenv:\n"); 254 if (kern_envp == NULL) { 255 debugf(" no env, null ptr\n"); 256 return; 257 } 258 debugf(" kern_envp = 0x%08x\n", (u_int32_t)kern_envp); 259 260 len = 0; 261 for (cp = kern_envp; cp != NULL; cp = kenv_next(cp)) 262 debugf(" %x %s\n", (u_int32_t)cp, cp); 263} 264 265void 266print_kernel_section_addr(void) 267{ 268 269 debugf("kernel image addresses:\n"); 270 debugf(" kernel_text = 0x%08x\n", (uint32_t)kernel_text); 271 debugf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext); 272 debugf(" _edata = 0x%08x\n", (uint32_t)_edata); 273 debugf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start); 274 debugf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end); 275 debugf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start); 276 debugf(" _end = 0x%08x\n", (uint32_t)_end); 277} 278 279u_int 280booke_init(uint32_t arg1, uint32_t arg2) 281{ 282 struct pcpu *pc; 283 void *kmdp, *mdp; 284 vm_offset_t dtbp, end; 285 286 kmdp = NULL; 287 288 end = (uintptr_t)_end; 289 dtbp = (vm_offset_t)NULL; 290 291 /* 292 * Handle the various ways we can get loaded and started: 293 * - FreeBSD's loader passes the pointer to the metadata 294 * in arg1, with arg2 undefined. arg1 has a value that's 295 * relative to the kernel's link address (i.e. larger 296 * than 0xc0000000). 297 * - Juniper's loader passes the metadata pointer in arg2 298 * and sets arg1 to zero. This is to signal that the 299 * loader maps the kernel and starts it at its link 300 * address (unlike the FreeBSD loader). 301 * - U-Boot passes the standard argc and argv parameters 302 * in arg1 and arg2 (resp). arg1 is between 1 and some 303 * relatively small number, such as 64K. arg2 is the 304 * physical address of the argv vector. 305 */ 306 if (arg1 > (uintptr_t)kernel_text) /* FreeBSD loader */ 307 mdp = (void *)arg1; 308 else if (arg1 == 0) /* Juniper loader */ 309 mdp = (void *)arg2; 310 else /* U-Boot */ 311 mdp = NULL; 312 313 /* 314 * Parse metadata and fetch parameters. 315 */ 316 if (mdp != NULL) { 317 preload_metadata = mdp; 318 kmdp = preload_search_by_type("elf kernel"); 319 if (kmdp != NULL) { 320 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 321 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 322 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t); 323 end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t); 324 325 bootinfo = (uint32_t *)preload_search_info(kmdp, 326 MODINFO_METADATA | MODINFOMD_BOOTINFO); 327 328#ifdef DDB 329 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t); 330 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t); 331#endif 332 } 333 } else { 334 bzero(__sbss_start, __sbss_end - __sbss_start); 335 bzero(__bss_start, _end - __bss_start); 336 } 337 338#if defined(FDT_DTB_STATIC) 339 /* 340 * In case the device tree blob was not retrieved (from metadata) try 341 * to use the statically embedded one. 342 */ 343 if (dtbp == (vm_offset_t)NULL) 344 dtbp = (vm_offset_t)&fdt_static_dtb; 345#endif 346 347 if (OF_install(OFW_FDT, 0) == FALSE) 348 while (1); 349 350 if (OF_init((void *)dtbp) != 0) 351 while (1); 352 353 if (fdt_immr_addr(CCSRBAR_VA) != 0) 354 while (1); 355 356 OF_interpret("perform-fixup", 0); 357 358 /* Set up TLB initially */ 359 booke_init_tlb(fdt_immr_pa); 360 361 /* Reset Time Base */ 362 mttb(0); 363 364 /* Init params/tunables that can be overridden by the loader. */ 365 init_param1(); 366 367 /* Start initializing proc0 and thread0. */ 368 proc_linkup0(&proc0, &thread0); 369 thread0.td_frame = &frame0; 370 371 /* Set up per-cpu data and store the pointer in SPR general 0. */ 372 pc = &__pcpu[0]; 373 pcpu_init(pc, 0, sizeof(struct pcpu)); 374 pc->pc_curthread = &thread0; 375 __asm __volatile("mtsprg 0, %0" :: "r"(pc)); 376 377 /* Initialize system mutexes. */ 378 mutex_init(); 379 380 /* Initialize the console before printing anything. */ 381 cninit(); 382 383 /* Print out some debug info... */ 384 debugf("%s: console initialized\n", __func__); 385 debugf(" arg3 mdp = 0x%08x\n", (u_int32_t)mdp); 386 debugf(" end = 0x%08x\n", (u_int32_t)end); 387 debugf(" boothowto = 0x%08x\n", boothowto); 388 debugf(" kernel ccsrbar = 0x%08x\n", CCSRBAR_VA); 389 debugf(" MSR = 0x%08x\n", mfmsr()); 390#if defined(BOOKE_E500) 391 debugf(" HID0 = 0x%08x\n", mfspr(SPR_HID0)); 392 debugf(" HID1 = 0x%08x\n", mfspr(SPR_HID1)); 393 debugf(" BUCSR = 0x%08x\n", mfspr(SPR_BUCSR)); 394#endif 395 396 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp); 397 398 print_kernel_section_addr(); 399 print_kenv(); 400#if defined(BOOKE_E500) 401 //tlb1_print_entries(); 402 //tlb1_print_tlbentries(); 403#endif 404 405 kdb_init(); 406 407#ifdef KDB 408 if (boothowto & RB_KDB) 409 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger"); 410#endif 411 412 /* Initialise platform module */ 413 platform_probe_and_attach(); 414 415 /* Initialise virtual memory. */ 416 pmap_mmu_install(MMU_TYPE_BOOKE, 0); 417 pmap_bootstrap((uintptr_t)kernel_text, end); 418 pmap_bootstrapped = 1; 419 debugf("MSR = 0x%08x\n", mfmsr()); 420#if defined(BOOKE_E500) 421 //tlb1_print_entries(); 422 //tlb1_print_tlbentries(); 423#endif 424 425 /* Initialize params/tunables that are derived from memsize. */ 426 init_param2(physmem); 427 428 /* Finish setting up thread0. */ 429 thread0.td_pcb = (struct pcb *) 430 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE - 431 sizeof(struct pcb)) & ~15); 432 bzero((void *)thread0.td_pcb, sizeof(struct pcb)); 433 pc->pc_curpcb = thread0.td_pcb; 434 435 /* Initialise the message buffer. */ 436 msgbufinit(msgbufp, msgbufsize); 437 438 /* Enable Machine Check interrupt. */ 439 mtmsr(mfmsr() | PSL_ME); 440 isync(); 441 442 /* Enable L1 caches */ 443 booke_enable_l1_cache(); 444 445 debugf("%s: SP = 0x%08x\n", __func__, 446 ((uintptr_t)thread0.td_pcb - 16) & ~15); 447 448 return (((uintptr_t)thread0.td_pcb - 16) & ~15); 449} 450 451#define RES_GRANULE 32 452extern uint32_t tlb0_miss_locks[]; 453 454/* Initialise a struct pcpu. */ 455void 456cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz) 457{ 458 459 pcpu->pc_tid_next = TID_MIN; 460 461#ifdef SMP 462 uint32_t *ptr; 463 int words_per_gran = RES_GRANULE / sizeof(uint32_t); 464 465 ptr = &tlb0_miss_locks[cpuid * words_per_gran]; 466 pcpu->pc_booke_tlb_lock = ptr; 467 *ptr = TLB_UNLOCKED; 468 *(ptr + 1) = 0; /* recurse counter */ 469#endif 470} 471 472/* 473 * Flush the D-cache for non-DMA I/O so that the I-cache can 474 * be made coherent later. 475 */ 476void 477cpu_flush_dcache(void *ptr, size_t len) 478{ 479 register_t addr, off; 480 481 /* 482 * Align the address to a cacheline and adjust the length 483 * accordingly. Then round the length to a multiple of the 484 * cacheline for easy looping. 485 */ 486 addr = (uintptr_t)ptr; 487 off = addr & (cacheline_size - 1); 488 addr -= off; 489 len = (len + off + cacheline_size - 1) & ~(cacheline_size - 1); 490 491 while (len > 0) { 492 __asm __volatile ("dcbf 0,%0" :: "r"(addr)); 493 __asm __volatile ("sync"); 494 addr += cacheline_size; 495 len -= cacheline_size; 496 } 497} 498 499void 500spinlock_enter(void) 501{ 502 struct thread *td; 503 register_t msr; 504 505 td = curthread; 506 if (td->td_md.md_spinlock_count == 0) { 507 msr = intr_disable(); 508 td->td_md.md_spinlock_count = 1; 509 td->td_md.md_saved_msr = msr; 510 } else 511 td->td_md.md_spinlock_count++; 512 critical_enter(); 513} 514 515void 516spinlock_exit(void) 517{ 518 struct thread *td; 519 register_t msr; 520 521 td = curthread; 522 critical_exit(); 523 msr = td->td_md.md_saved_msr; 524 td->td_md.md_spinlock_count--; 525 if (td->td_md.md_spinlock_count == 0) 526 intr_restore(msr); 527} 528 529/* Shutdown the CPU as much as possible. */ 530void 531cpu_halt(void) 532{ 533 534 mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE)); 535 while (1) 536 ; 537} 538 539int 540ptrace_set_pc(struct thread *td, unsigned long addr) 541{ 542 struct trapframe *tf; 543 544 tf = td->td_frame; 545 tf->srr0 = (register_t)addr; 546 547 return (0); 548} 549 550int 551ptrace_single_step(struct thread *td) 552{ 553 struct trapframe *tf; 554 555 tf = td->td_frame; 556 tf->srr1 |= PSL_DE; 557 tf->cpu.booke.dbcr0 |= (DBCR0_IDM | DBCR0_IC); 558 return (0); 559} 560 561int 562ptrace_clear_single_step(struct thread *td) 563{ 564 struct trapframe *tf; 565 566 tf = td->td_frame; 567 tf->srr1 &= ~PSL_DE; 568 tf->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC); 569 return (0); 570} 571 572void 573kdb_cpu_clear_singlestep(void) 574{ 575 register_t r; 576 577 r = mfspr(SPR_DBCR0); 578 mtspr(SPR_DBCR0, r & ~DBCR0_IC); 579 kdb_frame->srr1 &= ~PSL_DE; 580} 581 582void 583kdb_cpu_set_singlestep(void) 584{ 585 register_t r; 586 587 r = mfspr(SPR_DBCR0); 588 mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM); 589 kdb_frame->srr1 |= PSL_DE; 590} 591 592void 593bzero(void *buf, size_t len) 594{ 595 caddr_t p; 596 597 p = buf; 598 599 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) { 600 *p++ = 0; 601 len--; 602 } 603 604 while (len >= sizeof(u_long) * 8) { 605 *(u_long*) p = 0; 606 *((u_long*) p + 1) = 0; 607 *((u_long*) p + 2) = 0; 608 *((u_long*) p + 3) = 0; 609 len -= sizeof(u_long) * 8; 610 *((u_long*) p + 4) = 0; 611 *((u_long*) p + 5) = 0; 612 *((u_long*) p + 6) = 0; 613 *((u_long*) p + 7) = 0; 614 p += sizeof(u_long) * 8; 615 } 616 617 while (len >= sizeof(u_long)) { 618 *(u_long*) p = 0; 619 len -= sizeof(u_long); 620 p += sizeof(u_long); 621 } 622 623 while (len) { 624 *p++ = 0; 625 len--; 626 } 627} 628 629