1 /* $NetBSD: ofw.c,v 1.56 2011/07/19 15:07:43 dyoung Exp $ */ 2 3 /* 4 * Copyright 1997 5 * Digital Equipment Corporation. All rights reserved. 6 * 7 * This software is furnished under license and may be used and 8 * copied only in accordance with the following terms and conditions. 9 * Subject to these conditions, you may download, copy, install, 10 * use, modify and distribute this software in source and/or binary 11 * form. No title or ownership is transferred hereby. 12 * 13 * 1) Any source code used, modified or distributed must reproduce 14 * and retain this copyright notice and list of conditions as 15 * they appear in the source file. 16 * 17 * 2) No right is granted to use any trade name, trademark, or logo of 18 * Digital Equipment Corporation. Neither the "Digital Equipment 19 * Corporation" name nor any trademark or logo of Digital Equipment 20 * Corporation may be used to endorse or promote products derived 21 * from this software without the prior written permission of 22 * Digital Equipment Corporation. 23 * 24 * 3) This software is provided "AS-IS" and any express or implied 25 * warranties, including but not limited to, any implied warranties 26 * of merchantability, fitness for a particular purpose, or 27 * non-infringement are disclaimed. In no event shall DIGITAL be 28 * liable for any damages whatsoever, and in particular, DIGITAL 29 * shall not be liable for special, indirect, consequential, or 30 * incidental damages or damages for lost profits, loss of 31 * revenue or loss of use, whether such damages arise in contract, 32 * negligence, tort, under statute, in equity, at law or otherwise, 33 * even if advised of the possibility of such damage. 34 */ 35 36 /* 37 * Routines for interfacing between NetBSD and OFW. 38 * 39 * Parts of this could be moved to an MI file in time. -JJK 40 * 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: ofw.c,v 1.56 2011/07/19 15:07:43 dyoung Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/device.h> 49 #include <sys/kernel.h> 50 #include <sys/reboot.h> 51 #include <sys/mbuf.h> 52 53 #include <uvm/uvm.h> 54 55 #include <dev/cons.h> 56 57 #define _ARM32_BUS_DMA_PRIVATE 58 #include <sys/bus.h> 59 #include <machine/frame.h> 60 #include <machine/bootconfig.h> 61 #include <machine/cpu.h> 62 #include <machine/intr.h> 63 #include <machine/irqhandler.h> 64 65 #include <dev/ofw/openfirm.h> 66 #include <machine/ofw.h> 67 68 #include <netinet/in.h> 69 70 #if BOOT_FW_DHCP 71 #include <nfs/bootdata.h> 72 #endif 73 74 #ifdef SHARK 75 #include "machine/pio.h" 76 #include "machine/isa_machdep.h" 77 #endif 78 79 #include "isadma.h" 80 #include "igsfb_ofbus.h" 81 #include "chipsfb_ofbus.h" 82 #include "vga_ofbus.h" 83 84 #define IO_VIRT_BASE (OFW_VIRT_BASE + OFW_VIRT_SIZE) 85 #define IO_VIRT_SIZE 0x01000000 86 87 #define KERNEL_IMG_PTS 2 88 #define KERNEL_VMDATA_PTS (KERNEL_VM_SIZE >> (L1_S_SHIFT + 2)) 89 #define KERNEL_OFW_PTS 4 90 #define KERNEL_IO_PTS 4 91 92 #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000) 93 /* 94 * The range 0xf1000000 - 0xf6ffffff is available for kernel VM space 95 * OFW sits at 0xf7000000 96 */ 97 #define KERNEL_VM_SIZE 0x06000000 98 99 /* 100 * Imported variables 101 */ 102 extern BootConfig bootconfig; /* temporary, I hope */ 103 104 #ifdef DIAGNOSTIC 105 /* NOTE: These variables will be removed, well some of them */ 106 extern u_int current_mask; 107 #endif 108 109 extern int ofw_handleticks; 110 111 112 /* 113 * Imported routines 114 */ 115 extern void dump_spl_masks(void); 116 extern void dumpsys(void); 117 extern void dotickgrovelling(vaddr_t); 118 119 #define WriteWord(a, b) \ 120 *((volatile unsigned int *)(a)) = (b) 121 122 #define ReadWord(a) \ 123 (*((volatile unsigned int *)(a))) 124 125 126 /* 127 * Exported variables 128 */ 129 /* These should all be in a meminfo structure. */ 130 paddr_t physical_start; 131 paddr_t physical_freestart; 132 paddr_t physical_freeend; 133 paddr_t physical_end; 134 u_int free_pages; 135 #ifndef OFWGENCFG 136 pv_addr_t irqstack; 137 #endif 138 pv_addr_t undstack; 139 pv_addr_t abtstack; 140 pv_addr_t kernelstack; 141 142 paddr_t msgbufphys; 143 144 /* for storage allocation, used to be local to ofw_construct_proc0_addrspace */ 145 static vaddr_t virt_freeptr; 146 147 int ofw_callbacks = 0; /* debugging counter */ 148 149 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0) 150 int console_ihandle = 0; 151 static void reset_screen(void); 152 #endif 153 154 /**************************************************************/ 155 156 157 /* 158 * Declarations and definitions private to this module 159 * 160 */ 161 162 struct mem_region { 163 paddr_t start; 164 psize_t size; 165 }; 166 167 struct mem_translation { 168 vaddr_t virt; 169 vsize_t size; 170 paddr_t phys; 171 unsigned int mode; 172 }; 173 174 struct isa_range { 175 paddr_t isa_phys_hi; 176 paddr_t isa_phys_lo; 177 paddr_t parent_phys_start; 178 psize_t isa_size; 179 }; 180 181 struct vl_range { 182 paddr_t vl_phys_hi; 183 paddr_t vl_phys_lo; 184 paddr_t parent_phys_start; 185 psize_t vl_size; 186 }; 187 188 struct vl_isa_range { 189 paddr_t isa_phys_hi; 190 paddr_t isa_phys_lo; 191 paddr_t parent_phys_hi; 192 paddr_t parent_phys_lo; 193 psize_t isa_size; 194 }; 195 196 struct dma_range { 197 paddr_t start; 198 psize_t size; 199 }; 200 201 struct ofw_cbargs { 202 char *name; 203 int nargs; 204 int nreturns; 205 int args_n_results[12]; 206 }; 207 208 209 /* Memory info */ 210 static int nOFphysmem; 211 static struct mem_region *OFphysmem; 212 static int nOFphysavail; 213 static struct mem_region *OFphysavail; 214 static int nOFtranslations; 215 static struct mem_translation *OFtranslations; 216 static int nOFdmaranges; 217 static struct dma_range *OFdmaranges; 218 219 /* The OFW client services handle. */ 220 /* Initialized by ofw_init(). */ 221 static ofw_handle_t ofw_client_services_handle; 222 223 224 static void ofw_callbackhandler(void *); 225 static void ofw_construct_proc0_addrspace(void); 226 static void ofw_getphysmeminfo(void); 227 static void ofw_getvirttranslations(void); 228 static void *ofw_malloc(vsize_t size); 229 static void ofw_claimpages(vaddr_t *, pv_addr_t *, vsize_t); 230 static void ofw_discardmappings(vaddr_t, vaddr_t, vsize_t); 231 static int ofw_mem_ihandle(void); 232 static int ofw_mmu_ihandle(void); 233 static paddr_t ofw_claimphys(paddr_t, psize_t, paddr_t); 234 #if 0 235 static paddr_t ofw_releasephys(paddr_t, psize_t); 236 #endif 237 static vaddr_t ofw_claimvirt(vaddr_t, vsize_t, vaddr_t); 238 static void ofw_settranslation(vaddr_t, paddr_t, vsize_t, int); 239 static void ofw_initallocator(void); 240 static void ofw_configisaonly(paddr_t *, paddr_t *); 241 static void ofw_configvl(int, paddr_t *, paddr_t *); 242 static vaddr_t ofw_valloc(vsize_t, vaddr_t); 243 244 245 /* 246 * DHCP hooks. For a first cut, we look to see if there is a DHCP 247 * packet that was saved by the firmware. If not, we proceed as before, 248 * getting hand-configured data from NVRAM. If there is one, we get the 249 * packet, and extract the data from it. For now, we hand that data up 250 * in the boot_args string as before. 251 */ 252 253 254 /**************************************************************/ 255 256 257 /* 258 * 259 * Support routines for xxx_machdep.c 260 * 261 * The intent is that all OFW-based configurations use the 262 * exported routines in this file to do their business. If 263 * they need to override some function they are free to do so. 264 * 265 * The exported routines are: 266 * 267 * openfirmware 268 * ofw_init 269 * ofw_boot 270 * ofw_getbootinfo 271 * ofw_configmem 272 * ofw_configisa 273 * ofw_configisadma 274 * ofw_gettranslation 275 * ofw_map 276 * ofw_getcleaninfo 277 */ 278 279 280 int 281 openfirmware(void *args) 282 { 283 int ofw_result; 284 u_int saved_irq_state; 285 286 /* OFW is not re-entrant, so we wrap a mutex around the call. */ 287 saved_irq_state = disable_interrupts(I32_bit); 288 ofw_result = ofw_client_services_handle(args); 289 (void)restore_interrupts(saved_irq_state); 290 291 return(ofw_result); 292 } 293 294 295 void 296 ofw_init(ofw_handle_t ofw_handle) 297 { 298 ofw_client_services_handle = ofw_handle; 299 300 /* Everything we allocate in the remainder of this block is 301 * constrained to be in the "kernel-static" portion of the 302 * virtual address space (i.e., 0xF0000000 - 0xF1000000). 303 * This is because all such objects are expected to be in 304 * that range by NetBSD, or the objects will be re-mapped 305 * after the page-table-switch to other specific locations. 306 * In the latter case, it's simplest if our pre-switch handles 307 * on those objects are in regions that are already "well- 308 * known." (Otherwise, the cloning of the OFW-managed address- 309 * space becomes more awkward.) To minimize the number of L2 310 * page tables that we use, we are further restricting the 311 * remaining allocations in this block to the bottom quarter of 312 * the legal range. OFW will have loaded the kernel text+data+bss 313 * starting at the bottom of the range, and we will allocate 314 * objects from the top, moving downwards. The two sub-regions 315 * will collide if their total sizes hit 8MB. The current total 316 * is <1.5MB, but INSTALL kernels are > 4MB, so hence the 8MB 317 * limit. The variable virt-freeptr represents the next free va 318 * (moving downwards). 319 */ 320 virt_freeptr = KERNEL_BASE + (0x00400000 * KERNEL_IMG_PTS); 321 } 322 323 324 void 325 ofw_boot(int howto, char *bootstr) 326 { 327 328 #ifdef DIAGNOSTIC 329 printf("boot: howto=%08x curlwp=%p\n", howto, curlwp); 330 printf("current_mask=%08x\n", current_mask); 331 332 printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n", 333 irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY], 334 irqmasks[IPL_VM]); 335 printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n", 336 irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]); 337 338 dump_spl_masks(); 339 #endif 340 341 /* 342 * If we are still cold then hit the air brakes 343 * and crash to earth fast 344 */ 345 if (cold) { 346 doshutdownhooks(); 347 pmf_system_shutdown(boothowto); 348 printf("Halted while still in the ICE age.\n"); 349 printf("The operating system has halted.\n"); 350 goto ofw_exit; 351 /*NOTREACHED*/ 352 } 353 354 /* 355 * If RB_NOSYNC was not specified sync the discs. 356 * Note: Unless cold is set to 1 here, syslogd will die during the unmount. 357 * It looks like syslogd is getting woken up only to find that it cannot 358 * page part of the binary in as the filesystem has been unmounted. 359 */ 360 if (!(howto & RB_NOSYNC)) 361 bootsync(); 362 363 /* Say NO to interrupts */ 364 splhigh(); 365 366 /* Do a dump if requested. */ 367 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 368 dumpsys(); 369 370 /* Run any shutdown hooks */ 371 doshutdownhooks(); 372 373 pmf_system_shutdown(boothowto); 374 375 /* Make sure IRQ's are disabled */ 376 IRQdisable; 377 378 if (howto & RB_HALT) { 379 printf("The operating system has halted.\n"); 380 goto ofw_exit; 381 } 382 383 /* Tell the user we are booting */ 384 printf("rebooting...\n"); 385 386 /* Jump into the OFW boot routine. */ 387 { 388 static char str[256]; 389 char *ap = str, *ap1 = ap; 390 391 if (bootstr && *bootstr) { 392 if (strlen(bootstr) > sizeof str - 5) 393 printf("boot string too large, ignored\n"); 394 else { 395 strcpy(str, bootstr); 396 ap1 = ap = str + strlen(str); 397 *ap++ = ' '; 398 } 399 } 400 *ap++ = '-'; 401 if (howto & RB_SINGLE) 402 *ap++ = 's'; 403 if (howto & RB_KDB) 404 *ap++ = 'd'; 405 *ap++ = 0; 406 if (ap[-2] == '-') 407 *ap1 = 0; 408 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0) 409 reset_screen(); 410 #endif 411 OF_boot(str); 412 /*NOTREACHED*/ 413 } 414 415 ofw_exit: 416 printf("Calling OF_exit...\n"); 417 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0) 418 reset_screen(); 419 #endif 420 OF_exit(); 421 /*NOTREACHED*/ 422 } 423 424 425 #if BOOT_FW_DHCP 426 427 extern char *ip2dotted(struct in_addr); 428 429 /* 430 * Get DHCP data from OFW 431 */ 432 433 void 434 get_fw_dhcp_data(struct bootdata *bdp) 435 { 436 int chosen; 437 int dhcplen; 438 439 memset((char *)bdp, 0, sizeof(*bdp)); 440 if ((chosen = OF_finddevice("/chosen")) == -1) 441 panic("no /chosen from OFW"); 442 if ((dhcplen = OF_getproplen(chosen, "bootp-response")) > 0) { 443 u_char *cp; 444 int dhcp_type = 0; 445 char *ip; 446 447 /* 448 * OFW saved a DHCP (or BOOTP) packet for us. 449 */ 450 if (dhcplen > sizeof(bdp->dhcp_packet)) 451 panic("DHCP packet too large"); 452 OF_getprop(chosen, "bootp-response", &bdp->dhcp_packet, 453 sizeof(bdp->dhcp_packet)); 454 SANITY(bdp->dhcp_packet.op == BOOTREPLY, "bogus DHCP packet"); 455 /* 456 * Collect the interesting data from DHCP into 457 * the bootdata structure. 458 */ 459 bdp->ip_address = bdp->dhcp_packet.yiaddr; 460 ip = ip2dotted(bdp->ip_address); 461 if (memcmp(bdp->dhcp_packet.options, DHCP_OPTIONS_COOKIE, 4) == 0) 462 parse_dhcp_options(&bdp->dhcp_packet, 463 bdp->dhcp_packet.options + 4, 464 &bdp->dhcp_packet.options[dhcplen 465 - DHCP_FIXED_NON_UDP], bdp, ip); 466 if (bdp->root_ip.s_addr == 0) 467 bdp->root_ip = bdp->dhcp_packet.siaddr; 468 if (bdp->swap_ip.s_addr == 0) 469 bdp->swap_ip = bdp->dhcp_packet.siaddr; 470 } 471 /* 472 * If the DHCP packet did not contain all the necessary data, 473 * look in NVRAM for the missing parts. 474 */ 475 { 476 int options; 477 int proplen; 478 #define BOOTJUNKV_SIZE 256 479 char bootjunkv[BOOTJUNKV_SIZE]; /* minimize stack usage */ 480 481 482 if ((options = OF_finddevice("/options")) == -1) 483 panic("can't find /options"); 484 if (bdp->ip_address.s_addr == 0 && 485 (proplen = OF_getprop(options, "ipaddr", 486 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) { 487 bootjunkv[proplen] = '\0'; 488 if (dotted2ip(bootjunkv, &bdp->ip_address.s_addr) == 0) 489 bdp->ip_address.s_addr = 0; 490 } 491 if (bdp->ip_mask.s_addr == 0 && 492 (proplen = OF_getprop(options, "netmask", 493 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) { 494 bootjunkv[proplen] = '\0'; 495 if (dotted2ip(bootjunkv, &bdp->ip_mask.s_addr) == 0) 496 bdp->ip_mask.s_addr = 0; 497 } 498 if (bdp->hostname[0] == '\0' && 499 (proplen = OF_getprop(options, "hostname", 500 bdp->hostname, sizeof(bdp->hostname) - 1)) > 0) { 501 bdp->hostname[proplen] = '\0'; 502 } 503 if (bdp->root[0] == '\0' && 504 (proplen = OF_getprop(options, "rootfs", 505 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) { 506 bootjunkv[proplen] = '\0'; 507 parse_server_path(bootjunkv, &bdp->root_ip, bdp->root); 508 } 509 if (bdp->swap[0] == '\0' && 510 (proplen = OF_getprop(options, "swapfs", 511 bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) { 512 bootjunkv[proplen] = '\0'; 513 parse_server_path(bootjunkv, &bdp->swap_ip, bdp->swap); 514 } 515 } 516 } 517 518 #endif /* BOOT_FW_DHCP */ 519 520 void 521 ofw_getbootinfo(char **bp_pp, char **ba_pp) 522 { 523 int chosen; 524 int bp_len; 525 int ba_len; 526 char *bootpathv; 527 char *bootargsv; 528 529 /* Read the bootpath and bootargs out of OFW. */ 530 /* XXX is bootpath still interesting? --emg */ 531 if ((chosen = OF_finddevice("/chosen")) == -1) 532 panic("no /chosen from OFW"); 533 bp_len = OF_getproplen(chosen, "bootpath"); 534 ba_len = OF_getproplen(chosen, "bootargs"); 535 if (bp_len < 0 || ba_len < 0) 536 panic("can't get boot data from OFW"); 537 538 bootpathv = (char *)ofw_malloc(bp_len); 539 bootargsv = (char *)ofw_malloc(ba_len); 540 541 if (bp_len) 542 OF_getprop(chosen, "bootpath", bootpathv, bp_len); 543 else 544 bootpathv[0] = '\0'; 545 546 if (ba_len) 547 OF_getprop(chosen, "bootargs", bootargsv, ba_len); 548 else 549 bootargsv[0] = '\0'; 550 551 *bp_pp = bootpathv; 552 *ba_pp = bootargsv; 553 #ifdef DIAGNOSTIC 554 printf("bootpath=<%s>, bootargs=<%s>\n", bootpathv, bootargsv); 555 #endif 556 } 557 558 paddr_t 559 ofw_getcleaninfo(void) 560 { 561 int cpu; 562 vaddr_t vclean; 563 paddr_t pclean; 564 565 if ((cpu = OF_finddevice("/cpu")) == -1) 566 panic("no /cpu from OFW"); 567 568 if ((OF_getprop(cpu, "d-cache-flush-address", &vclean, 569 sizeof(vclean))) != sizeof(vclean)) { 570 #ifdef DEBUG 571 printf("no OFW d-cache-flush-address property\n"); 572 #endif 573 return -1; 574 } 575 576 if ((pclean = ofw_gettranslation( 577 of_decode_int((unsigned char *)&vclean))) == -1) 578 panic("OFW failed to translate cache flush address"); 579 580 return pclean; 581 } 582 583 void 584 ofw_configisa(paddr_t *pio, paddr_t *pmem) 585 { 586 int vl; 587 588 if ((vl = OF_finddevice("/vlbus")) == -1) /* old style OFW dev info tree */ 589 ofw_configisaonly(pio, pmem); 590 else /* old style OFW dev info tree */ 591 ofw_configvl(vl, pio, pmem); 592 } 593 594 static void 595 ofw_configisaonly(paddr_t *pio, paddr_t *pmem) 596 { 597 int isa; 598 int rangeidx; 599 int size; 600 paddr_t hi, start; 601 struct isa_range ranges[2]; 602 603 if ((isa = OF_finddevice("/isa")) == -1) 604 panic("OFW has no /isa device node"); 605 606 /* expect to find two isa ranges: IO/data and memory/data */ 607 if ((size = OF_getprop(isa, "ranges", ranges, sizeof(ranges))) 608 != sizeof(ranges)) 609 panic("unexpected size of OFW /isa ranges property: %d", size); 610 611 *pio = *pmem = -1; 612 613 for (rangeidx = 0; rangeidx < 2; ++rangeidx) { 614 hi = of_decode_int((unsigned char *) 615 &ranges[rangeidx].isa_phys_hi); 616 start = of_decode_int((unsigned char *) 617 &ranges[rangeidx].parent_phys_start); 618 619 if (hi & 1) { /* then I/O space */ 620 *pio = start; 621 } else { 622 *pmem = start; 623 } 624 } /* END for */ 625 626 if ((*pio == -1) || (*pmem == -1)) 627 panic("bad OFW /isa ranges property"); 628 629 } 630 631 static void 632 ofw_configvl(int vl, paddr_t *pio, paddr_t *pmem) 633 { 634 int isa; 635 int ir, vr; 636 int size; 637 paddr_t hi, start; 638 struct vl_isa_range isa_ranges[2]; 639 struct vl_range vl_ranges[2]; 640 641 if ((isa = OF_finddevice("/vlbus/isa")) == -1) 642 panic("OFW has no /vlbus/isa device node"); 643 644 /* expect to find two isa ranges: IO/data and memory/data */ 645 if ((size = OF_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))) 646 != sizeof(isa_ranges)) 647 panic("unexpected size of OFW /vlbus/isa ranges property: %d", 648 size); 649 650 /* expect to find two vl ranges: IO/data and memory/data */ 651 if ((size = OF_getprop(vl, "ranges", vl_ranges, sizeof(vl_ranges))) 652 != sizeof(vl_ranges)) 653 panic("unexpected size of OFW /vlbus ranges property: %d", size); 654 655 *pio = -1; 656 *pmem = -1; 657 658 for (ir = 0; ir < 2; ++ir) { 659 for (vr = 0; vr < 2; ++vr) { 660 if ((isa_ranges[ir].parent_phys_hi 661 == vl_ranges[vr].vl_phys_hi) && 662 (isa_ranges[ir].parent_phys_lo 663 == vl_ranges[vr].vl_phys_lo)) { 664 hi = of_decode_int((unsigned char *) 665 &isa_ranges[ir].isa_phys_hi); 666 start = of_decode_int((unsigned char *) 667 &vl_ranges[vr].parent_phys_start); 668 669 if (hi & 1) { /* then I/O space */ 670 *pio = start; 671 } else { 672 *pmem = start; 673 } 674 } /* END if */ 675 } /* END for */ 676 } /* END for */ 677 678 if ((*pio == -1) || (*pmem == -1)) 679 panic("bad OFW /isa ranges property"); 680 } 681 682 #if NISADMA > 0 683 struct arm32_dma_range *shark_isa_dma_ranges; 684 int shark_isa_dma_nranges; 685 #endif 686 687 void 688 ofw_configisadma(paddr_t *pdma) 689 { 690 int root; 691 int rangeidx; 692 int size; 693 struct dma_range *dr; 694 695 if ((root = OF_finddevice("/")) == -1 || 696 (size = OF_getproplen(root, "dma-ranges")) <= 0 || 697 (OFdmaranges = (struct dma_range *)ofw_malloc(size)) == 0 || 698 OF_getprop(root, "dma-ranges", OFdmaranges, size) != size) 699 panic("bad / dma-ranges property"); 700 701 nOFdmaranges = size / sizeof(struct dma_range); 702 703 #if NISADMA > 0 704 /* Allocate storage for non-OFW representation of the range. */ 705 shark_isa_dma_ranges = ofw_malloc(nOFdmaranges * 706 sizeof(*shark_isa_dma_ranges)); 707 if (shark_isa_dma_ranges == NULL) 708 panic("unable to allocate shark_isa_dma_ranges"); 709 shark_isa_dma_nranges = nOFdmaranges; 710 #endif 711 712 for (rangeidx = 0, dr = OFdmaranges; rangeidx < nOFdmaranges; 713 ++rangeidx, ++dr) { 714 dr->start = of_decode_int((unsigned char *)&dr->start); 715 dr->size = of_decode_int((unsigned char *)&dr->size); 716 #if NISADMA > 0 717 shark_isa_dma_ranges[rangeidx].dr_sysbase = dr->start; 718 shark_isa_dma_ranges[rangeidx].dr_busbase = dr->start; 719 shark_isa_dma_ranges[rangeidx].dr_len = dr->size; 720 #endif 721 } 722 723 #ifdef DEBUG 724 printf("DMA ranges size = %d\n", size); 725 726 for (rangeidx = 0; rangeidx < nOFdmaranges; ++rangeidx) { 727 printf("%08lx %08lx\n", 728 (u_long)OFdmaranges[rangeidx].start, 729 (u_long)OFdmaranges[rangeidx].size); 730 } 731 #endif 732 } 733 734 /* 735 * Memory configuration: 736 * 737 * We start off running in the environment provided by OFW. 738 * This has the MMU turned on, the kernel code and data 739 * mapped-in at KERNEL_BASE (0xF0000000), OFW's text and 740 * data mapped-in at OFW_VIRT_BASE (0xF7000000), and (possibly) 741 * page0 mapped-in at 0x0. 742 * 743 * The strategy is to set-up the address space for proc0 -- 744 * including the allocation of space for new page tables -- while 745 * memory is still managed by OFW. We then effectively create a 746 * copy of the address space by dumping all of OFW's translations 747 * and poking them into the new page tables. We then notify OFW 748 * that we are assuming control of memory-management by installing 749 * our callback-handler, and switch to the NetBSD-managed page 750 * tables with the cpu_setttb() call. 751 * 752 * This scheme may cause some amount of memory to be wasted within 753 * OFW as dead page tables, but it shouldn't be more than about 754 * 20-30KB. (It's also possible that OFW will re-use the space.) 755 */ 756 void 757 ofw_configmem(void) 758 { 759 int i; 760 761 /* Set-up proc0 address space. */ 762 ofw_construct_proc0_addrspace(); 763 764 /* 765 * Get a dump of OFW's picture of physical memory. 766 * This is used below to initialize a load of variables used by pmap. 767 * We get it now rather than later because we are about to 768 * tell OFW to stop managing memory. 769 */ 770 ofw_getphysmeminfo(); 771 772 /* We are about to take control of memory-management from OFW. 773 * Establish callbacks for OFW to use for its future memory needs. 774 * This is required for us to keep using OFW services. 775 */ 776 777 /* First initialize our callback memory allocator. */ 778 ofw_initallocator(); 779 780 OF_set_callback(ofw_callbackhandler); 781 782 /* Switch to the proc0 pagetables. */ 783 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT); 784 cpu_setttb(kernel_l1pt.pv_pa); 785 cpu_tlb_flushID(); 786 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)); 787 788 /* 789 * Moved from cpu_startup() as data_abort_handler() references 790 * this during uvm init 791 */ 792 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va); 793 794 /* Set-up the various globals which describe physical memory for pmap. */ 795 { 796 struct mem_region *mp; 797 int totalcnt; 798 int availcnt; 799 800 /* physmem, physical_start, physical_end */ 801 physmem = 0; 802 for (totalcnt = 0, mp = OFphysmem; totalcnt < nOFphysmem; 803 totalcnt++, mp++) { 804 #ifdef OLDPRINTFS 805 printf("physmem: %x, %x\n", mp->start, mp->size); 806 #endif 807 physmem += btoc(mp->size); 808 } 809 physical_start = OFphysmem[0].start; 810 mp--; 811 physical_end = mp->start + mp->size; 812 813 /* free_pages, physical_freestart, physical_freeend */ 814 free_pages = 0; 815 for (availcnt = 0, mp = OFphysavail; availcnt < nOFphysavail; 816 availcnt++, mp++) { 817 #ifdef OLDPRINTFS 818 printf("physavail: %x, %x\n", mp->start, mp->size); 819 #endif 820 free_pages += btoc(mp->size); 821 } 822 physical_freestart = OFphysavail[0].start; 823 mp--; 824 physical_freeend = mp->start + mp->size; 825 #ifdef OLDPRINTFS 826 printf("pmap_bootstrap: physmem = %x, free_pages = %x\n", 827 physmem, free_pages); 828 #endif 829 830 /* 831 * This is a hack to work with the existing pmap code. 832 * That code depends on a RiscPC BootConfig structure 833 * containing, among other things, an array describing 834 * the regions of physical memory. So, for now, we need 835 * to stuff our OFW-derived physical memory info into a 836 * "fake" BootConfig structure. 837 * 838 * An added twist is that we initialize the BootConfig 839 * structure with our "available" physical memory regions 840 * rather than the "total" physical memory regions. Why? 841 * Because: 842 * 843 * (a) the VM code requires that the "free" pages it is 844 * initialized with have consecutive indices. This 845 * allows it to use more efficient data structures 846 * (presumably). 847 * (b) the current pmap routines which report the initial 848 * set of free page indices (pmap_next_page) and 849 * which map addresses to indices (pmap_page_index) 850 * assume that the free pages are consecutive across 851 * memory region boundaries. 852 * 853 * This means that memory which is "stolen" at startup time 854 * (say, for page descriptors) MUST come from either the 855 * bottom of the first region or the top of the last. 856 * 857 * This requirement doesn't mesh well with OFW (or at least 858 * our use of it). We can get around it for the time being 859 * by pretending that our "available" region array describes 860 * all of our physical memory. This may cause some important 861 * information to be excluded from a dump file, but so far 862 * I haven't come across any other negative effects. 863 * 864 * In the long-run we should fix the index 865 * generation/translation code in the pmap module. 866 */ 867 868 if (DRAM_BLOCKS < (availcnt + 1)) 869 panic("more ofw memory regions than bootconfig blocks"); 870 871 for (i = 0, mp = OFphysavail; i < nOFphysavail; i++, mp++) { 872 bootconfig.dram[i].address = mp->start; 873 bootconfig.dram[i].pages = btoc(mp->size); 874 } 875 bootconfig.dramblocks = availcnt; 876 } 877 878 /* Load memory into UVM. */ 879 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */ 880 881 /* XXX Please kill this code dead. */ 882 for (i = 0; i < bootconfig.dramblocks; i++) { 883 paddr_t start = (paddr_t)bootconfig.dram[i].address; 884 paddr_t end = start + (bootconfig.dram[i].pages * PAGE_SIZE); 885 #if NISADMA > 0 886 paddr_t istart, isize; 887 #endif 888 889 if (start < physical_freestart) 890 start = physical_freestart; 891 if (end > physical_freeend) 892 end = physical_freeend; 893 894 #if 0 895 printf("%d: %lx -> %lx\n", loop, start, end - 1); 896 #endif 897 898 #if NISADMA > 0 899 if (arm32_dma_range_intersect(shark_isa_dma_ranges, 900 shark_isa_dma_nranges, 901 start, end - start, 902 &istart, &isize)) { 903 /* 904 * Place the pages that intersect with the 905 * ISA DMA range onto the ISA DMA free list. 906 */ 907 #if 0 908 printf(" ISADMA 0x%lx -> 0x%lx\n", istart, 909 istart + isize - 1); 910 #endif 911 uvm_page_physload(atop(istart), 912 atop(istart + isize), atop(istart), 913 atop(istart + isize), VM_FREELIST_ISADMA); 914 915 /* 916 * Load the pieces that come before the 917 * intersection onto the default free list. 918 */ 919 if (start < istart) { 920 #if 0 921 printf(" BEFORE 0x%lx -> 0x%lx\n", 922 start, istart - 1); 923 #endif 924 uvm_page_physload(atop(start), 925 atop(istart), atop(start), 926 atop(istart), VM_FREELIST_DEFAULT); 927 } 928 929 /* 930 * Load the pieces that come after the 931 * intersection onto the default free list. 932 */ 933 if ((istart + isize) < end) { 934 #if 0 935 printf(" AFTER 0x%lx -> 0x%lx\n", 936 (istart + isize), end - 1); 937 #endif 938 uvm_page_physload(atop(istart + isize), 939 atop(end), atop(istart + isize), 940 atop(end), VM_FREELIST_DEFAULT); 941 } 942 } else { 943 uvm_page_physload(atop(start), atop(end), 944 atop(start), atop(end), VM_FREELIST_DEFAULT); 945 } 946 #else /* NISADMA > 0 */ 947 uvm_page_physload(atop(start), atop(end), 948 atop(start), atop(end), VM_FREELIST_DEFAULT); 949 #endif /* NISADMA > 0 */ 950 } 951 952 /* Initialize pmap module. */ 953 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE); 954 } 955 956 957 /* 958 ************************************************************ 959 960 Routines private to this module 961 962 ************************************************************ 963 */ 964 965 /* N.B. Not supposed to call printf in callback-handler! Could deadlock! */ 966 static void 967 ofw_callbackhandler(void *v) 968 { 969 struct ofw_cbargs *args = v; 970 char *name = args->name; 971 int nargs = args->nargs; 972 int nreturns = args->nreturns; 973 int *args_n_results = args->args_n_results; 974 975 ofw_callbacks++; 976 977 #if defined(OFWGENCFG) 978 /* Check this first, so that we don't waste IRQ time parsing. */ 979 if (strcmp(name, "tick") == 0) { 980 vaddr_t frame; 981 982 /* Check format. */ 983 if (nargs != 1 || nreturns < 1) { 984 args_n_results[nargs] = -1; 985 args->nreturns = 1; 986 return; 987 } 988 args_n_results[nargs] = 0; /* properly formatted request */ 989 990 /* 991 * Note that we are running in the IRQ frame, with interrupts 992 * disabled. 993 * 994 * We need to do two things here: 995 * - copy a few words out of the input frame into a global 996 * area, for later use by our real tick-handling code 997 * - patch a few words in the frame so that when OFW returns 998 * from the interrupt it will resume with our handler 999 * rather than the code that was actually interrupted. 1000 * Our handler will resume when it finishes with the code 1001 * that was actually interrupted. 1002 * 1003 * It's simplest to do this in assembler, since it requires 1004 * switching frames and grovelling about with registers. 1005 */ 1006 frame = (vaddr_t)args_n_results[0]; 1007 if (ofw_handleticks) 1008 dotickgrovelling(frame); 1009 args_n_results[nargs + 1] = frame; 1010 args->nreturns = 1; 1011 } else 1012 #endif 1013 1014 if (strcmp(name, "map") == 0) { 1015 vaddr_t va; 1016 paddr_t pa; 1017 vsize_t size; 1018 int mode; 1019 int ap_bits; 1020 int dom_bits; 1021 int cb_bits; 1022 1023 /* Check format. */ 1024 if (nargs != 4 || nreturns < 2) { 1025 args_n_results[nargs] = -1; 1026 args->nreturns = 1; 1027 return; 1028 } 1029 args_n_results[nargs] = 0; /* properly formatted request */ 1030 1031 pa = (paddr_t)args_n_results[0]; 1032 va = (vaddr_t)args_n_results[1]; 1033 size = (vsize_t)args_n_results[2]; 1034 mode = args_n_results[3]; 1035 ap_bits = (mode & 0x00000C00); 1036 dom_bits = (mode & 0x000001E0); 1037 cb_bits = (mode & 0x000000C0); 1038 1039 /* Sanity checks. */ 1040 if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE || 1041 (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) || 1042 (pa & PGOFSET) != 0 || (size & PGOFSET) != 0 || 1043 size == 0 || (dom_bits >> 5) != 0) { 1044 args_n_results[nargs + 1] = -1; 1045 args->nreturns = 1; 1046 return; 1047 } 1048 1049 /* Write-back anything stuck in the cache. */ 1050 cpu_idcache_wbinv_all(); 1051 1052 /* Install new mappings. */ 1053 { 1054 pt_entry_t *pte = vtopte(va); 1055 int npages = size >> PGSHIFT; 1056 1057 ap_bits >>= 10; 1058 for (; npages > 0; pte++, pa += PAGE_SIZE, npages--) 1059 *pte = (pa | L2_AP(ap_bits) | L2_TYPE_S | 1060 cb_bits); 1061 PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT); 1062 } 1063 1064 /* Clean out tlb. */ 1065 tlb_flush(); 1066 1067 args_n_results[nargs + 1] = 0; 1068 args->nreturns = 2; 1069 } else if (strcmp(name, "unmap") == 0) { 1070 vaddr_t va; 1071 vsize_t size; 1072 1073 /* Check format. */ 1074 if (nargs != 2 || nreturns < 1) { 1075 args_n_results[nargs] = -1; 1076 args->nreturns = 1; 1077 return; 1078 } 1079 args_n_results[nargs] = 0; /* properly formatted request */ 1080 1081 va = (vaddr_t)args_n_results[0]; 1082 size = (vsize_t)args_n_results[1]; 1083 1084 /* Sanity checks. */ 1085 if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE || 1086 (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) || 1087 (size & PGOFSET) != 0 || size == 0) { 1088 args_n_results[nargs + 1] = -1; 1089 args->nreturns = 1; 1090 return; 1091 } 1092 1093 /* Write-back anything stuck in the cache. */ 1094 cpu_idcache_wbinv_all(); 1095 1096 /* Zero the mappings. */ 1097 { 1098 pt_entry_t *pte = vtopte(va); 1099 int npages = size >> PGSHIFT; 1100 1101 for (; npages > 0; pte++, npages--) 1102 *pte = 0; 1103 PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT); 1104 } 1105 1106 /* Clean out tlb. */ 1107 tlb_flush(); 1108 1109 args->nreturns = 1; 1110 } else if (strcmp(name, "translate") == 0) { 1111 vaddr_t va; 1112 paddr_t pa; 1113 int mode; 1114 pt_entry_t pte; 1115 1116 /* Check format. */ 1117 if (nargs != 1 || nreturns < 4) { 1118 args_n_results[nargs] = -1; 1119 args->nreturns = 1; 1120 return; 1121 } 1122 args_n_results[nargs] = 0; /* properly formatted request */ 1123 1124 va = (vaddr_t)args_n_results[0]; 1125 1126 /* Sanity checks. 1127 * For now, I am only willing to translate va's in the 1128 * "ofw range." Eventually, I may be more generous. -JJK 1129 */ 1130 if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE || 1131 va >= (OFW_VIRT_BASE + OFW_VIRT_SIZE)) { 1132 args_n_results[nargs + 1] = -1; 1133 args->nreturns = 1; 1134 return; 1135 } 1136 1137 /* Lookup mapping. */ 1138 pte = *vtopte(va); 1139 if (pte == 0) { 1140 /* No mapping. */ 1141 args_n_results[nargs + 1] = -1; 1142 args->nreturns = 2; 1143 } else { 1144 /* Existing mapping. */ 1145 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET); 1146 mode = (pte & 0x0C00) | (0 << 5) | (pte & 0x000C); /* AP | DOM | CB */ 1147 1148 args_n_results[nargs + 1] = 0; 1149 args_n_results[nargs + 2] = pa; 1150 args_n_results[nargs + 3] = mode; 1151 args->nreturns = 4; 1152 } 1153 } else if (strcmp(name, "claim-phys") == 0) { 1154 struct pglist alloclist; 1155 paddr_t low, high, align; 1156 psize_t size; 1157 1158 /* 1159 * XXX 1160 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd 1161 * XXX 1162 */ 1163 1164 /* Check format. */ 1165 if (nargs != 4 || nreturns < 3) { 1166 args_n_results[nargs] = -1; 1167 args->nreturns = 1; 1168 return; 1169 } 1170 args_n_results[nargs] = 0; /* properly formatted request */ 1171 1172 low = args_n_results[0]; 1173 size = args_n_results[2]; 1174 align = args_n_results[3]; 1175 high = args_n_results[1] + size; 1176 1177 #if 0 1178 printf("claim-phys: low = 0x%x, size = 0x%x, align = 0x%x, high = 0x%x\n", 1179 low, size, align, high); 1180 align = size; 1181 printf("forcing align to be 0x%x\n", align); 1182 #endif 1183 1184 args_n_results[nargs + 1] = 1185 uvm_pglistalloc(size, low, high, align, 0, &alloclist, 1, 0); 1186 #if 0 1187 printf(" -> 0x%lx", args_n_results[nargs + 1]); 1188 #endif 1189 if (args_n_results[nargs + 1] != 0) { 1190 #if 0 1191 printf("(failed)\n"); 1192 #endif 1193 args_n_results[nargs + 1] = -1; 1194 args->nreturns = 2; 1195 return; 1196 } 1197 args_n_results[nargs + 2] = VM_PAGE_TO_PHYS(alloclist.tqh_first); 1198 #if 0 1199 printf("(succeeded: pa = 0x%lx)\n", args_n_results[nargs + 2]); 1200 #endif 1201 args->nreturns = 3; 1202 1203 } else if (strcmp(name, "release-phys") == 0) { 1204 printf("unimplemented ofw callback - %s\n", name); 1205 args_n_results[nargs] = -1; 1206 args->nreturns = 1; 1207 } else if (strcmp(name, "claim-virt") == 0) { 1208 vaddr_t va; 1209 vsize_t size; 1210 vaddr_t align; 1211 1212 /* XXX - notyet */ 1213 /* printf("unimplemented ofw callback - %s\n", name);*/ 1214 args_n_results[nargs] = -1; 1215 args->nreturns = 1; 1216 return; 1217 1218 /* Check format. */ 1219 if (nargs != 2 || nreturns < 3) { 1220 args_n_results[nargs] = -1; 1221 args->nreturns = 1; 1222 return; 1223 } 1224 args_n_results[nargs] = 0; /* properly formatted request */ 1225 1226 /* Allocate size bytes with specified alignment. */ 1227 size = (vsize_t)args_n_results[0]; 1228 align = (vaddr_t)args_n_results[1]; 1229 if (align % PAGE_SIZE != 0) { 1230 args_n_results[nargs + 1] = -1; 1231 args->nreturns = 2; 1232 return; 1233 } 1234 1235 if (va == 0) { 1236 /* Couldn't allocate. */ 1237 args_n_results[nargs + 1] = -1; 1238 args->nreturns = 2; 1239 } else { 1240 /* Successful allocation. */ 1241 args_n_results[nargs + 1] = 0; 1242 args_n_results[nargs + 2] = va; 1243 args->nreturns = 3; 1244 } 1245 } else if (strcmp(name, "release-virt") == 0) { 1246 vaddr_t va; 1247 vsize_t size; 1248 1249 /* XXX - notyet */ 1250 printf("unimplemented ofw callback - %s\n", name); 1251 args_n_results[nargs] = -1; 1252 args->nreturns = 1; 1253 return; 1254 1255 /* Check format. */ 1256 if (nargs != 2 || nreturns < 1) { 1257 args_n_results[nargs] = -1; 1258 args->nreturns = 1; 1259 return; 1260 } 1261 args_n_results[nargs] = 0; /* properly formatted request */ 1262 1263 /* Release bytes. */ 1264 va = (vaddr_t)args_n_results[0]; 1265 size = (vsize_t)args_n_results[1]; 1266 1267 args->nreturns = 1; 1268 } else { 1269 args_n_results[nargs] = -1; 1270 args->nreturns = 1; 1271 } 1272 } 1273 1274 static void 1275 ofw_construct_proc0_addrspace(void) 1276 { 1277 int i, oft; 1278 static pv_addr_t proc0_pt_sys; 1279 static pv_addr_t proc0_pt_kernel[KERNEL_IMG_PTS]; 1280 static pv_addr_t proc0_pt_vmdata[KERNEL_VMDATA_PTS]; 1281 static pv_addr_t proc0_pt_ofw[KERNEL_OFW_PTS]; 1282 static pv_addr_t proc0_pt_io[KERNEL_IO_PTS]; 1283 static pv_addr_t msgbuf; 1284 vaddr_t L1pagetable; 1285 struct mem_translation *tp; 1286 1287 /* Set-up the system page. */ 1288 KASSERT(vector_page == 0); /* XXX for now */ 1289 systempage.pv_va = ofw_claimvirt(vector_page, PAGE_SIZE, 0); 1290 if (systempage.pv_va == -1) { 1291 /* Something was already mapped to vector_page's VA. */ 1292 systempage.pv_va = vector_page; 1293 systempage.pv_pa = ofw_gettranslation(vector_page); 1294 if (systempage.pv_pa == -1) 1295 panic("bogus result from gettranslation(vector_page)"); 1296 } else { 1297 /* We were just allocated the page-length range at VA 0. */ 1298 if (systempage.pv_va != vector_page) 1299 panic("bogus result from claimvirt(vector_page, PAGE_SIZE, 0)"); 1300 1301 /* Now allocate a physical page, and establish the mapping. */ 1302 systempage.pv_pa = ofw_claimphys(0, PAGE_SIZE, PAGE_SIZE); 1303 if (systempage.pv_pa == -1) 1304 panic("bogus result from claimphys(0, PAGE_SIZE, PAGE_SIZE)"); 1305 ofw_settranslation(systempage.pv_va, systempage.pv_pa, 1306 PAGE_SIZE, -1); /* XXX - mode? -JJK */ 1307 1308 /* Zero the memory. */ 1309 memset((char *)systempage.pv_va, 0, PAGE_SIZE); 1310 } 1311 1312 /* Allocate/initialize space for the proc0, NetBSD-managed */ 1313 /* page tables that we will be switching to soon. */ 1314 ofw_claimpages(&virt_freeptr, &kernel_l1pt, L1_TABLE_SIZE); 1315 ofw_claimpages(&virt_freeptr, &proc0_pt_sys, L2_TABLE_SIZE); 1316 for (i = 0; i < KERNEL_IMG_PTS; i++) 1317 ofw_claimpages(&virt_freeptr, &proc0_pt_kernel[i], L2_TABLE_SIZE); 1318 for (i = 0; i < KERNEL_VMDATA_PTS; i++) 1319 ofw_claimpages(&virt_freeptr, &proc0_pt_vmdata[i], L2_TABLE_SIZE); 1320 for (i = 0; i < KERNEL_OFW_PTS; i++) 1321 ofw_claimpages(&virt_freeptr, &proc0_pt_ofw[i], L2_TABLE_SIZE); 1322 for (i = 0; i < KERNEL_IO_PTS; i++) 1323 ofw_claimpages(&virt_freeptr, &proc0_pt_io[i], L2_TABLE_SIZE); 1324 1325 /* Allocate/initialize space for stacks. */ 1326 #ifndef OFWGENCFG 1327 ofw_claimpages(&virt_freeptr, &irqstack, PAGE_SIZE); 1328 #endif 1329 ofw_claimpages(&virt_freeptr, &undstack, PAGE_SIZE); 1330 ofw_claimpages(&virt_freeptr, &abtstack, PAGE_SIZE); 1331 ofw_claimpages(&virt_freeptr, &kernelstack, UPAGES * PAGE_SIZE); 1332 1333 /* Allocate/initialize space for msgbuf area. */ 1334 ofw_claimpages(&virt_freeptr, &msgbuf, MSGBUFSIZE); 1335 msgbufphys = msgbuf.pv_pa; 1336 1337 /* Construct the proc0 L1 pagetable. */ 1338 L1pagetable = kernel_l1pt.pv_va; 1339 1340 pmap_link_l2pt(L1pagetable, 0x0, &proc0_pt_sys); 1341 for (i = 0; i < KERNEL_IMG_PTS; i++) 1342 pmap_link_l2pt(L1pagetable, KERNEL_BASE + i * 0x00400000, 1343 &proc0_pt_kernel[i]); 1344 for (i = 0; i < KERNEL_VMDATA_PTS; i++) 1345 pmap_link_l2pt(L1pagetable, KERNEL_VM_BASE + i * 0x00400000, 1346 &proc0_pt_vmdata[i]); 1347 for (i = 0; i < KERNEL_OFW_PTS; i++) 1348 pmap_link_l2pt(L1pagetable, OFW_VIRT_BASE + i * 0x00400000, 1349 &proc0_pt_ofw[i]); 1350 for (i = 0; i < KERNEL_IO_PTS; i++) 1351 pmap_link_l2pt(L1pagetable, IO_VIRT_BASE + i * 0x00400000, 1352 &proc0_pt_io[i]); 1353 1354 /* 1355 * OK, we're done allocating. 1356 * Get a dump of OFW's translations, and make the appropriate 1357 * entries in the L2 pagetables that we just allocated. 1358 */ 1359 1360 ofw_getvirttranslations(); 1361 1362 for (oft = 0, tp = OFtranslations; oft < nOFtranslations; 1363 oft++, tp++) { 1364 1365 vaddr_t va; 1366 paddr_t pa; 1367 int npages = tp->size / PAGE_SIZE; 1368 1369 /* Size must be an integral number of pages. */ 1370 if (npages == 0 || tp->size % PAGE_SIZE != 0) 1371 panic("illegal ofw translation (size)"); 1372 1373 /* Make an entry for each page in the appropriate table. */ 1374 for (va = tp->virt, pa = tp->phys; npages > 0; 1375 va += PAGE_SIZE, pa += PAGE_SIZE, npages--) { 1376 /* 1377 * Map the top bits to the appropriate L2 pagetable. 1378 * The only allowable regions are page0, the 1379 * kernel-static area, and the ofw area. 1380 */ 1381 switch (va >> (L1_S_SHIFT + 2)) { 1382 case 0: 1383 /* page0 */ 1384 break; 1385 1386 #if KERNEL_IMG_PTS != 2 1387 #error "Update ofw translation range list" 1388 #endif 1389 case ( KERNEL_BASE >> (L1_S_SHIFT + 2)): 1390 case ((KERNEL_BASE + 0x00400000) >> (L1_S_SHIFT + 2)): 1391 /* kernel static area */ 1392 break; 1393 1394 case ( OFW_VIRT_BASE >> (L1_S_SHIFT + 2)): 1395 case ((OFW_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)): 1396 case ((OFW_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)): 1397 case ((OFW_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)): 1398 /* ofw area */ 1399 break; 1400 1401 case ( IO_VIRT_BASE >> (L1_S_SHIFT + 2)): 1402 case ((IO_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)): 1403 case ((IO_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)): 1404 case ((IO_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)): 1405 /* io area */ 1406 break; 1407 1408 default: 1409 /* illegal */ 1410 panic("illegal ofw translation (addr) %#lx", 1411 va); 1412 } 1413 1414 /* Make the entry. */ 1415 pmap_map_entry(L1pagetable, va, pa, 1416 VM_PROT_READ|VM_PROT_WRITE, 1417 (tp->mode & 0xC) == 0xC ? PTE_CACHE 1418 : PTE_NOCACHE); 1419 } 1420 } 1421 1422 /* 1423 * We don't actually want some of the mappings that we just 1424 * set up to appear in proc0's address space. In particular, 1425 * we don't want aliases to physical addresses that the kernel 1426 * has-mapped/will-map elsewhere. 1427 */ 1428 ofw_discardmappings(proc0_pt_kernel[KERNEL_IMG_PTS - 1].pv_va, 1429 msgbuf.pv_va, MSGBUFSIZE); 1430 1431 /* update the top of the kernel VM */ 1432 pmap_curmaxkvaddr = 1433 KERNEL_VM_BASE + (KERNEL_VMDATA_PTS * 0x00400000); 1434 1435 /* 1436 * gross hack for the sake of not thrashing the TLB and making 1437 * cache flush more efficient: blast l1 ptes for sections. 1438 */ 1439 for (oft = 0, tp = OFtranslations; oft < nOFtranslations; oft++, tp++) { 1440 vaddr_t va = tp->virt; 1441 paddr_t pa = tp->phys; 1442 1443 if (((va | pa) & L1_S_OFFSET) == 0) { 1444 int nsections = tp->size / L1_S_SIZE; 1445 1446 while (nsections--) { 1447 /* XXXJRT prot?? */ 1448 pmap_map_section(L1pagetable, va, pa, 1449 VM_PROT_READ|VM_PROT_WRITE, 1450 (tp->mode & 0xC) == 0xC ? PTE_CACHE 1451 : PTE_NOCACHE); 1452 va += L1_S_SIZE; 1453 pa += L1_S_SIZE; 1454 } 1455 } 1456 } 1457 } 1458 1459 1460 static void 1461 ofw_getphysmeminfo(void) 1462 { 1463 int phandle; 1464 int mem_len; 1465 int avail_len; 1466 int i; 1467 1468 if ((phandle = OF_finddevice("/memory")) == -1 || 1469 (mem_len = OF_getproplen(phandle, "reg")) <= 0 || 1470 (OFphysmem = (struct mem_region *)ofw_malloc(mem_len)) == 0 || 1471 OF_getprop(phandle, "reg", OFphysmem, mem_len) != mem_len || 1472 (avail_len = OF_getproplen(phandle, "available")) <= 0 || 1473 (OFphysavail = (struct mem_region *)ofw_malloc(avail_len)) == 0 || 1474 OF_getprop(phandle, "available", OFphysavail, avail_len) 1475 != avail_len) 1476 panic("can't get physmeminfo from OFW"); 1477 1478 nOFphysmem = mem_len / sizeof(struct mem_region); 1479 nOFphysavail = avail_len / sizeof(struct mem_region); 1480 1481 /* 1482 * Sort the blocks in each array into ascending address order. 1483 * Also, page-align all blocks. 1484 */ 1485 for (i = 0; i < 2; i++) { 1486 struct mem_region *tmp = (i == 0) ? OFphysmem : OFphysavail; 1487 struct mem_region *mp; 1488 int cnt = (i == 0) ? nOFphysmem : nOFphysavail; 1489 int j; 1490 1491 #ifdef OLDPRINTFS 1492 printf("ofw_getphysmeminfo: %d blocks\n", cnt); 1493 #endif 1494 1495 /* XXX - Convert all the values to host order. -JJK */ 1496 for (j = 0, mp = tmp; j < cnt; j++, mp++) { 1497 mp->start = of_decode_int((unsigned char *)&mp->start); 1498 mp->size = of_decode_int((unsigned char *)&mp->size); 1499 } 1500 1501 for (j = 0, mp = tmp; j < cnt; j++, mp++) { 1502 u_int s, sz; 1503 struct mem_region *mp1; 1504 1505 /* Page-align start of the block. */ 1506 s = mp->start % PAGE_SIZE; 1507 if (s != 0) { 1508 s = (PAGE_SIZE - s); 1509 1510 if (mp->size >= s) { 1511 mp->start += s; 1512 mp->size -= s; 1513 } 1514 } 1515 1516 /* Page-align the size. */ 1517 mp->size -= mp->size % PAGE_SIZE; 1518 1519 /* Handle empty block. */ 1520 if (mp->size == 0) { 1521 memmove(mp, mp + 1, (cnt - (mp - tmp)) 1522 * sizeof(struct mem_region)); 1523 cnt--; 1524 mp--; 1525 continue; 1526 } 1527 1528 /* Bubble sort. */ 1529 s = mp->start; 1530 sz = mp->size; 1531 for (mp1 = tmp; mp1 < mp; mp1++) 1532 if (s < mp1->start) 1533 break; 1534 if (mp1 < mp) { 1535 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1); 1536 mp1->start = s; 1537 mp1->size = sz; 1538 } 1539 } 1540 1541 #ifdef OLDPRINTFS 1542 for (mp = tmp; mp->size; mp++) { 1543 printf("%x, %x\n", mp->start, mp->size); 1544 } 1545 #endif 1546 } 1547 } 1548 1549 1550 static void 1551 ofw_getvirttranslations(void) 1552 { 1553 int mmu_phandle; 1554 int mmu_ihandle; 1555 int trans_len; 1556 int over, len; 1557 int i; 1558 struct mem_translation *tp; 1559 1560 mmu_ihandle = ofw_mmu_ihandle(); 1561 1562 /* overallocate to avoid increases during allocation */ 1563 over = 4 * sizeof(struct mem_translation); 1564 if ((mmu_phandle = OF_instance_to_package(mmu_ihandle)) == -1 || 1565 (len = OF_getproplen(mmu_phandle, "translations")) <= 0 || 1566 (OFtranslations = ofw_malloc(len + over)) == 0 || 1567 (trans_len = OF_getprop(mmu_phandle, "translations", 1568 OFtranslations, len + over)) > (len + over)) 1569 panic("can't get virttranslations from OFW"); 1570 1571 /* XXX - Convert all the values to host order. -JJK */ 1572 nOFtranslations = trans_len / sizeof(struct mem_translation); 1573 #ifdef OLDPRINTFS 1574 printf("ofw_getvirtmeminfo: %d blocks\n", nOFtranslations); 1575 #endif 1576 for (i = 0, tp = OFtranslations; i < nOFtranslations; i++, tp++) { 1577 tp->virt = of_decode_int((unsigned char *)&tp->virt); 1578 tp->size = of_decode_int((unsigned char *)&tp->size); 1579 tp->phys = of_decode_int((unsigned char *)&tp->phys); 1580 tp->mode = of_decode_int((unsigned char *)&tp->mode); 1581 } 1582 } 1583 1584 /* 1585 * ofw_valloc: allocate blocks of VM for IO and other special purposes 1586 */ 1587 typedef struct _vfree { 1588 struct _vfree *pNext; 1589 vaddr_t start; 1590 vsize_t size; 1591 } VFREE, *PVFREE; 1592 1593 static VFREE vfinitial = { NULL, IO_VIRT_BASE, IO_VIRT_SIZE }; 1594 1595 static PVFREE vflist = &vfinitial; 1596 1597 static vaddr_t 1598 ofw_valloc(vsize_t size, vaddr_t align) 1599 { 1600 PVFREE *ppvf; 1601 PVFREE pNew; 1602 vaddr_t new; 1603 vaddr_t lead; 1604 1605 for (ppvf = &vflist; *ppvf; ppvf = &((*ppvf)->pNext)) { 1606 if (align == 0) { 1607 new = (*ppvf)->start; 1608 lead = 0; 1609 } else { 1610 new = ((*ppvf)->start + (align - 1)) & ~(align - 1); 1611 lead = new - (*ppvf)->start; 1612 } 1613 1614 if (((*ppvf)->size - lead) >= size) { 1615 if (lead == 0) { 1616 /* using whole block */ 1617 if (size == (*ppvf)->size) { 1618 /* splice out of list */ 1619 (*ppvf) = (*ppvf)->pNext; 1620 } else { /* tail of block is free */ 1621 (*ppvf)->start = new + size; 1622 (*ppvf)->size -= size; 1623 } 1624 } else { 1625 vsize_t tail = ((*ppvf)->start 1626 + (*ppvf)->size) - (new + size); 1627 /* free space at beginning */ 1628 (*ppvf)->size = lead; 1629 1630 if (tail != 0) { 1631 /* free space at tail */ 1632 pNew = ofw_malloc(sizeof(VFREE)); 1633 pNew->pNext = (*ppvf)->pNext; 1634 (*ppvf)->pNext = pNew; 1635 pNew->start = new + size; 1636 pNew->size = tail; 1637 } 1638 } 1639 return new; 1640 } /* END if */ 1641 } /* END for */ 1642 1643 return -1; 1644 } 1645 1646 vaddr_t 1647 ofw_map(paddr_t pa, vsize_t size, int cb_bits) 1648 { 1649 vaddr_t va; 1650 1651 if ((va = ofw_valloc(size, size)) == -1) 1652 panic("cannot alloc virtual memory for %#lx", pa); 1653 1654 ofw_claimvirt(va, size, 0); /* make sure OFW knows about the memory */ 1655 1656 ofw_settranslation(va, pa, size, L2_AP(AP_KRW) | cb_bits); 1657 1658 return va; 1659 } 1660 1661 static int 1662 ofw_mem_ihandle(void) 1663 { 1664 static int mem_ihandle = 0; 1665 int chosen; 1666 1667 if (mem_ihandle != 0) 1668 return(mem_ihandle); 1669 1670 if ((chosen = OF_finddevice("/chosen")) == -1 || 1671 OF_getprop(chosen, "memory", &mem_ihandle, sizeof(int)) < 0) 1672 panic("ofw_mem_ihandle"); 1673 1674 mem_ihandle = of_decode_int((unsigned char *)&mem_ihandle); 1675 1676 return(mem_ihandle); 1677 } 1678 1679 1680 static int 1681 ofw_mmu_ihandle(void) 1682 { 1683 static int mmu_ihandle = 0; 1684 int chosen; 1685 1686 if (mmu_ihandle != 0) 1687 return(mmu_ihandle); 1688 1689 if ((chosen = OF_finddevice("/chosen")) == -1 || 1690 OF_getprop(chosen, "mmu", &mmu_ihandle, sizeof(int)) < 0) 1691 panic("ofw_mmu_ihandle"); 1692 1693 mmu_ihandle = of_decode_int((unsigned char *)&mmu_ihandle); 1694 1695 return(mmu_ihandle); 1696 } 1697 1698 1699 /* Return -1 on failure. */ 1700 static paddr_t 1701 ofw_claimphys(paddr_t pa, psize_t size, paddr_t align) 1702 { 1703 int mem_ihandle = ofw_mem_ihandle(); 1704 1705 /* printf("ofw_claimphys (%x, %x, %x) --> ", pa, size, align);*/ 1706 if (align == 0) { 1707 /* Allocate at specified base; alignment is ignored. */ 1708 pa = OF_call_method_1("claim", mem_ihandle, 3, pa, size, align); 1709 } else { 1710 /* Allocate anywhere, with specified alignment. */ 1711 pa = OF_call_method_1("claim", mem_ihandle, 2, size, align); 1712 } 1713 1714 /* printf("%x\n", pa);*/ 1715 return(pa); 1716 } 1717 1718 1719 #if 0 1720 /* Return -1 on failure. */ 1721 static paddr_t 1722 ofw_releasephys(paddr_t pa, psize_t size) 1723 { 1724 int mem_ihandle = ofw_mem_ihandle(); 1725 1726 /* printf("ofw_releasephys (%x, %x)\n", pa, size);*/ 1727 1728 return (OF_call_method_1("release", mem_ihandle, 2, pa, size)); 1729 } 1730 #endif 1731 1732 /* Return -1 on failure. */ 1733 static vaddr_t 1734 ofw_claimvirt(vaddr_t va, vsize_t size, vaddr_t align) 1735 { 1736 int mmu_ihandle = ofw_mmu_ihandle(); 1737 1738 /*printf("ofw_claimvirt (%x, %x, %x) --> ", va, size, align);*/ 1739 if (align == 0) { 1740 /* Allocate at specified base; alignment is ignored. */ 1741 va = OF_call_method_1("claim", mmu_ihandle, 3, va, size, align); 1742 } else { 1743 /* Allocate anywhere, with specified alignment. */ 1744 va = OF_call_method_1("claim", mmu_ihandle, 2, size, align); 1745 } 1746 1747 /*printf("%x\n", va);*/ 1748 return(va); 1749 } 1750 1751 /* Return -1 if no mapping. */ 1752 paddr_t 1753 ofw_gettranslation(vaddr_t va) 1754 { 1755 int mmu_ihandle = ofw_mmu_ihandle(); 1756 paddr_t pa; 1757 int mode; 1758 int exists; 1759 1760 #ifdef OFW_DEBUG 1761 printf("ofw_gettranslation (%x) --> ", (uint32_t)va); 1762 #endif 1763 exists = 0; /* gets set to true if translation exists */ 1764 if (OF_call_method("translate", mmu_ihandle, 1, 3, va, &pa, &mode, 1765 &exists) != 0) 1766 return(-1); 1767 1768 #ifdef OFW_DEBUG 1769 printf("%d %x\n", exists, (uint32_t)pa); 1770 #endif 1771 return(exists ? pa : -1); 1772 } 1773 1774 1775 static void 1776 ofw_settranslation(vaddr_t va, paddr_t pa, vsize_t size, int mode) 1777 { 1778 int mmu_ihandle = ofw_mmu_ihandle(); 1779 1780 #ifdef OFW_DEBUG 1781 printf("ofw_settranslation (%x, %x, %x, %x) --> void", (uint32_t)va, 1782 (uint32_t)pa, (uint32_t)size, (uint32_t)mode); 1783 #endif 1784 if (OF_call_method("map", mmu_ihandle, 4, 0, pa, va, size, mode) != 0) 1785 panic("ofw_settranslation failed"); 1786 } 1787 1788 /* 1789 * Allocation routine used before the kernel takes over memory. 1790 * Use this for efficient storage for things that aren't rounded to 1791 * page size. 1792 * 1793 * The point here is not necessarily to be very efficient (even though 1794 * that's sort of nice), but to do proper dynamic allocation to avoid 1795 * size-limitation errors. 1796 * 1797 */ 1798 1799 typedef struct _leftover { 1800 struct _leftover *pNext; 1801 vsize_t size; 1802 } LEFTOVER, *PLEFTOVER; 1803 1804 /* leftover bits of pages. first word is pointer to next. 1805 second word is size of leftover */ 1806 static PLEFTOVER leftovers = NULL; 1807 1808 static void * 1809 ofw_malloc(vsize_t size) 1810 { 1811 PLEFTOVER *ppLeftover; 1812 PLEFTOVER pLeft; 1813 pv_addr_t new; 1814 vsize_t newSize, claim_size; 1815 1816 /* round and set minimum size */ 1817 size = max(sizeof(LEFTOVER), 1818 ((size + (sizeof(LEFTOVER) - 1)) & ~(sizeof(LEFTOVER) - 1))); 1819 1820 for (ppLeftover = &leftovers; *ppLeftover; 1821 ppLeftover = &((*ppLeftover)->pNext)) 1822 if ((*ppLeftover)->size >= size) 1823 break; 1824 1825 if (*ppLeftover) { /* have a leftover of the right size */ 1826 /* remember the leftover */ 1827 new.pv_va = (vaddr_t)*ppLeftover; 1828 if ((*ppLeftover)->size < (size + sizeof(LEFTOVER))) { 1829 /* splice out of chain */ 1830 *ppLeftover = (*ppLeftover)->pNext; 1831 } else { 1832 /* remember the next pointer */ 1833 pLeft = (*ppLeftover)->pNext; 1834 newSize = (*ppLeftover)->size - size; /* reduce size */ 1835 /* move pointer */ 1836 *ppLeftover = (PLEFTOVER)(((vaddr_t)*ppLeftover) 1837 + size); 1838 (*ppLeftover)->pNext = pLeft; 1839 (*ppLeftover)->size = newSize; 1840 } 1841 } else { 1842 claim_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 1843 ofw_claimpages(&virt_freeptr, &new, claim_size); 1844 if ((size + sizeof(LEFTOVER)) <= claim_size) { 1845 pLeft = (PLEFTOVER)(new.pv_va + size); 1846 pLeft->pNext = leftovers; 1847 pLeft->size = claim_size - size; 1848 leftovers = pLeft; 1849 } 1850 } 1851 1852 return (void *)(new.pv_va); 1853 } 1854 1855 /* 1856 * Here is a really, really sleazy free. It's not used right now, 1857 * because it's not worth the extra complexity for just a few bytes. 1858 * 1859 */ 1860 #if 0 1861 static void 1862 ofw_free(vaddr_t addr, vsize_t size) 1863 { 1864 PLEFTOVER pLeftover = (PLEFTOVER)addr; 1865 1866 /* splice right into list without checks or compaction */ 1867 pLeftover->pNext = leftovers; 1868 pLeftover->size = size; 1869 leftovers = pLeftover; 1870 } 1871 #endif 1872 1873 /* 1874 * Allocate and zero round(size)/PAGE_SIZE pages of memory. 1875 * We guarantee that the allocated memory will be 1876 * aligned to a boundary equal to the smallest power of 1877 * 2 greater than or equal to size. 1878 * free_pp is an IN/OUT parameter which points to the 1879 * last allocated virtual address in an allocate-downwards 1880 * stack. pv_p is an OUT parameter which contains the 1881 * virtual and physical base addresses of the allocated 1882 * memory. 1883 */ 1884 static void 1885 ofw_claimpages(vaddr_t *free_pp, pv_addr_t *pv_p, vsize_t size) 1886 { 1887 /* round-up to page boundary */ 1888 vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 1889 vsize_t aligned_size; 1890 vaddr_t va; 1891 paddr_t pa; 1892 1893 if (alloc_size == 0) 1894 panic("ofw_claimpages zero"); 1895 1896 for (aligned_size = 1; aligned_size < alloc_size; aligned_size <<= 1) 1897 ; 1898 1899 /* The only way to provide the alignment guarantees is to 1900 * allocate the virtual and physical ranges separately, 1901 * then do an explicit map call. 1902 */ 1903 va = (*free_pp & ~(aligned_size - 1)) - aligned_size; 1904 if (ofw_claimvirt(va, alloc_size, 0) != va) 1905 panic("ofw_claimpages va alloc"); 1906 pa = ofw_claimphys(0, alloc_size, aligned_size); 1907 if (pa == -1) 1908 panic("ofw_claimpages pa alloc"); 1909 /* XXX - what mode? -JJK */ 1910 ofw_settranslation(va, pa, alloc_size, -1); 1911 1912 /* The memory's mapped-in now, so we can zero it. */ 1913 memset((char *)va, 0, alloc_size); 1914 1915 /* Set OUT parameters. */ 1916 *free_pp = va; 1917 pv_p->pv_va = va; 1918 pv_p->pv_pa = pa; 1919 } 1920 1921 1922 static void 1923 ofw_discardmappings(vaddr_t L2pagetable, vaddr_t va, vsize_t size) 1924 { 1925 /* round-up to page boundary */ 1926 vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 1927 int npages = alloc_size / PAGE_SIZE; 1928 1929 if (npages == 0) 1930 panic("ofw_discardmappings zero"); 1931 1932 /* Discard each mapping. */ 1933 for (; npages > 0; va += PAGE_SIZE, npages--) { 1934 /* Sanity. The current entry should be non-null. */ 1935 if (ReadWord(L2pagetable + ((va >> 10) & 0x00000FFC)) == 0) 1936 panic("ofw_discardmappings zero entry"); 1937 1938 /* Clear the entry. */ 1939 WriteWord(L2pagetable + ((va >> 10) & 0x00000FFC), 0); 1940 } 1941 } 1942 1943 1944 static void 1945 ofw_initallocator(void) 1946 { 1947 1948 } 1949 1950 #if (NIGSFB_OFBUS > 0) || (NCHIPSFB_OFBUS > 0) || (NVGA_OFBUS > 0) 1951 static void 1952 reset_screen(void) 1953 { 1954 1955 if ((console_ihandle == 0) || (console_ihandle == -1)) 1956 return; 1957 1958 OF_call_method("install", console_ihandle, 0, 0); 1959 } 1960 #endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */ 1961