1 /* $NetBSD: pmap_bootstrap.c,v 1.93 2012/02/10 04:49:45 mhitch Exp $ */ 2 3 /* 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * the Systems Programming Group of the University of Utah Computer 9 * Science Department. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)pmap_bootstrap.c 8.1 (Berkeley) 6/10/93 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: pmap_bootstrap.c,v 1.93 2012/02/10 04:49:45 mhitch Exp $"); 40 41 #include "opt_ddb.h" 42 #include "opt_kgdb.h" 43 #include "opt_m68k_arch.h" 44 45 #include "zsc.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/reboot.h> 50 51 #include <uvm/uvm_extern.h> 52 53 #include <machine/cpu.h> 54 #include <machine/pte.h> 55 #include <machine/vmparam.h> 56 #include <machine/pmap.h> 57 #include <machine/autoconf.h> 58 #include <machine/video.h> 59 60 #include <mac68k/mac68k/macrom.h> 61 62 #define PA2VA(v, t) (t)((u_int)(v) - firstpa) 63 64 extern char *etext; 65 extern char *extiobase; 66 67 extern paddr_t avail_start, avail_end; 68 69 #if NZSC > 0 70 extern int zsinited; 71 #endif 72 73 /* 74 * These are used to map the RAM: 75 */ 76 int numranges; /* = 0 == don't use the ranges */ 77 u_long low[8]; 78 u_long high[8]; 79 u_long maxaddr; /* PA of the last physical page */ 80 int vidlen; 81 #define VIDMAPSIZE btoc(vidlen) 82 static vaddr_t newvideoaddr; 83 84 extern void * ROMBase; 85 86 /* 87 * Special purpose kernel virtual addresses, used for mapping 88 * physical pages for a variety of temporary or permanent purposes: 89 * 90 * CADDR1, CADDR2: pmap zero/copy operations 91 * vmmap: /dev/mem, crash dumps, parity error checking 92 * msgbufaddr: kernel message buffer 93 */ 94 void *CADDR1, *CADDR2; 95 char *vmmap; 96 void *msgbufaddr; 97 98 void pmap_bootstrap(paddr_t, paddr_t); 99 void bootstrap_mac68k(int); 100 101 /* 102 * Bootstrap the VM system. 103 * 104 * This is called with the MMU either on or off. If it's on, we assume 105 * that it's mapped with the same PA <=> LA mapping that we eventually 106 * want. The page sizes and the protections will be wrong, anyway. 107 * 108 * nextpa is the first address following the loaded kernel. On a IIsi 109 * on 12 May 1996, that was 0xf9000 beyond firstpa. 110 */ 111 void 112 pmap_bootstrap(paddr_t nextpa, paddr_t firstpa) 113 { 114 paddr_t lwp0upa, kstpa, kptmpa, kptpa; 115 u_int nptpages, kstsize; 116 paddr_t avail_next; 117 int avail_remaining; 118 int avail_range; 119 int i; 120 st_entry_t protoste, *ste, *este; 121 pt_entry_t protopte, *pte, *epte; 122 u_int stfree = 0; /* XXX: gcc -Wuninitialized */ 123 extern char start[]; 124 125 vidlen = m68k_round_page(mac68k_video.mv_height * 126 mac68k_video.mv_stride + m68k_page_offset(mac68k_video.mv_phys)); 127 128 /* 129 * Calculate important physical addresses: 130 * 131 * lwp0upa lwp0 u-area UPAGES pages 132 * 133 * kstpa kernel segment table 1 page (!040) 134 * N pages (040) 135 * 136 * kptmpa kernel PT map 1 page 137 * 138 * kptpa statically allocated 139 * kernel PT pages Sysptsize+ pages 140 * 141 * [ Sysptsize is the number of pages of PT, and IIOMAPSIZE and 142 * NBMAPSIZE are the number of PTEs, hence we need to round 143 * the total to a page boundary with IO maps at the end. ] 144 * 145 */ 146 lwp0upa = nextpa; 147 nextpa += USPACE; 148 if (mmutype == MMU_68040) 149 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 150 else 151 kstsize = 1; 152 kstpa = nextpa; 153 nextpa += kstsize * PAGE_SIZE; 154 kptmpa = nextpa; 155 nextpa += PAGE_SIZE; 156 kptpa = nextpa; 157 nptpages = Sysptsize + 158 (IIOMAPSIZE + ROMMAPSIZE + VIDMAPSIZE + NPTEPG - 1) / NPTEPG; 159 /* 160 * New kmem arena is allocated prior to pmap_init(), so we need 161 * additiona PT pages to account for that allocation, which is based 162 * on physical memory size. Just sum up memory and add enough PT 163 * pages for that size. 164 */ 165 mem_size = 0; 166 for (i = 0; i < numranges; i++) 167 mem_size += high[i] - low[i]; 168 nptpages += howmany(m68k_btop(mem_size), NPTEPG); 169 nextpa += nptpages * PAGE_SIZE; 170 171 for (i = 0; i < numranges; i++) 172 if (low[i] <= firstpa && firstpa < high[i]) 173 break; 174 if (i >= numranges || nextpa > high[i]) { 175 if (mac68k_machine.do_graybars) { 176 printf("Failure in NetBSD boot; "); 177 if (i < numranges) 178 printf("nextpa=0x%lx, high[%d]=0x%lx.\n", 179 nextpa, i, high[i]); 180 else 181 printf("can't find kernel RAM segment.\n"); 182 printf("You're hosed! Try booting with 32-bit "); 183 printf("addressing enabled in the memory control "); 184 printf("panel.\n"); 185 printf("Older machines may need Mode32 to get that "); 186 printf("option.\n"); 187 } 188 panic("Cannot work with the current memory mappings."); 189 } 190 191 /* 192 * Initialize segment table and kernel page table map. 193 * 194 * On 68030s and earlier MMUs the two are identical except for 195 * the valid bits so both are initialized with essentially the 196 * same values. On the 68040, which has a mandatory 3-level 197 * structure, the segment table holds the level 1 table and part 198 * (or all) of the level 2 table and hence is considerably 199 * different. Here the first level consists of 128 descriptors 200 * (512 bytes) each mapping 32mb of address space. Each of these 201 * points to blocks of 128 second level descriptors (512 bytes) 202 * each mapping 256kb. Note that there may be additional "segment 203 * table" pages depending on how large MAXKL2SIZE is. 204 * 205 * Portions of the last segment of KVA space (0xFFC00000 - 206 * 0xFFFFFFFF) are mapped for the kernel page tables. 207 * 208 * XXX cramming two levels of mapping into the single "segment" 209 * table on the 68040 is intended as a temporary hack to get things 210 * working. The 224mb of address space that this allows will most 211 * likely be insufficient in the future (at least for the kernel). 212 */ 213 if (mmutype == MMU_68040) { 214 int nl1desc, nl2desc; 215 216 /* 217 * First invalidate the entire "segment table" pages 218 * (levels 1 and 2 have the same "invalid" value). 219 */ 220 ste = PA2VA(kstpa, st_entry_t *); 221 este = &ste[kstsize * NPTEPG]; 222 while (ste < este) 223 *ste++ = SG_NV; 224 /* 225 * Initialize level 2 descriptors (which immediately 226 * follow the level 1 table). We need: 227 * NPTEPG / SG4_LEV3SIZE 228 * level 2 descriptors to map each of the nptpages 229 * pages of PTEs. Note that we set the "used" bit 230 * now to save the HW the expense of doing it. 231 */ 232 nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE); 233 ste = PA2VA(kstpa, st_entry_t *); 234 ste = &ste[SG4_LEV1SIZE]; 235 este = &ste[nl2desc]; 236 protoste = kptpa | SG_U | SG_RW | SG_V; 237 while (ste < este) { 238 *ste++ = protoste; 239 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 240 } 241 /* 242 * Initialize level 1 descriptors. We need: 243 * howmany(nl2desc, SG4_LEV2SIZE) 244 * level 1 descriptors to map the `nl2desc' level 2's. 245 */ 246 nl1desc = howmany(nl2desc, SG4_LEV2SIZE); 247 ste = PA2VA(kstpa, u_int *); 248 este = &ste[nl1desc]; 249 protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 250 while (ste < este) { 251 *ste++ = protoste; 252 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 253 } 254 /* 255 * Initialize the final level 1 descriptor to map the next 256 * block of level 2 descriptors for Sysptmap. 257 */ 258 ste = PA2VA(kstpa, st_entry_t *); 259 ste = &ste[SG4_LEV1SIZE - 1]; 260 *ste = protoste; 261 /* 262 * Now initialize the final portion of that block of 263 * descriptors to map Sysmap. 264 */ 265 i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE); 266 ste = PA2VA(kstpa, st_entry_t *); 267 ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)]; 268 este = &ste[NPTEPG / SG4_LEV3SIZE]; 269 protoste = kptmpa | SG_U | SG_RW | SG_V; 270 while (ste < este) { 271 *ste++ = protoste; 272 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 273 } 274 /* 275 * Calculate the free level 2 descriptor mask 276 * noting that we have used: 277 * 0: level 1 table 278 * 1 to nl1desc: map page tables 279 * nl1desc + 1: maps kptmpa and last-page page table 280 */ 281 /* mark an entry for level 1 table */ 282 stfree = ~l2tobm(0); 283 /* mark entries for map page tables */ 284 for (i = 1; i <= nl1desc; i++) 285 stfree &= ~l2tobm(i); 286 /* mark an entry for kptmpa and lkptpa */ 287 stfree &= ~l2tobm(i); 288 /* mark entries not available */ 289 for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++) 290 stfree &= ~l2tobm(i); 291 292 /* 293 * Initialize Sysptmap 294 */ 295 pte = PA2VA(kptmpa, pt_entry_t *); 296 epte = &pte[nptpages]; 297 protopte = kptpa | PG_RW | PG_CI | PG_V; 298 while (pte < epte) { 299 *pte++ = protopte; 300 protopte += PAGE_SIZE; 301 } 302 /* 303 * Invalidate all remaining entries. 304 */ 305 epte = PA2VA(kptmpa, pt_entry_t *); 306 epte = &epte[TIB_SIZE]; 307 while (pte < epte) { 308 *pte++ = PG_NV; 309 } 310 /* 311 * Initialize the last one to point to Sysptmap. 312 */ 313 pte = PA2VA(kptmpa, pt_entry_t *); 314 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 315 *pte = kptmpa | PG_RW | PG_CI | PG_V; 316 } else { 317 /* 318 * Map the page table pages in both the HW segment table 319 * and the software Sysptmap. 320 */ 321 ste = PA2VA(kstpa, st_entry_t *); 322 pte = PA2VA(kptmpa, pt_entry_t *); 323 epte = &pte[nptpages]; 324 protoste = kptpa | SG_RW | SG_V; 325 protopte = kptpa | PG_RW | PG_CI | PG_V; 326 while (pte < epte) { 327 *ste++ = protoste; 328 *pte++ = protopte; 329 protoste += PAGE_SIZE; 330 protopte += PAGE_SIZE; 331 } 332 /* 333 * Invalidate all remaining entries in both. 334 */ 335 este = PA2VA(kstpa, st_entry_t *); 336 este = &este[TIA_SIZE]; 337 while (ste < este) 338 *ste++ = SG_NV; 339 epte = PA2VA(kptmpa, pt_entry_t *); 340 epte = &epte[TIB_SIZE]; 341 while (pte < epte) 342 *pte++ = PG_NV; 343 /* 344 * Initialize the last one to point to Sysptmap. 345 */ 346 ste = PA2VA(kstpa, st_entry_t *); 347 ste = &ste[SYSMAP_VA >> SEGSHIFT]; 348 pte = PA2VA(kptmpa, pt_entry_t *); 349 pte = &pte[SYSMAP_VA >> SEGSHIFT]; 350 *ste = kptmpa | SG_RW | SG_V; 351 *pte = kptmpa | PG_RW | PG_CI | PG_V; 352 } 353 354 /* 355 * Initialize kernel page table. 356 * Start by invalidating the `nptpages' that we have allocated. 357 */ 358 pte = PA2VA(kptpa, pt_entry_t *); 359 epte = &pte[nptpages * NPTEPG]; 360 while (pte < epte) 361 *pte++ = PG_NV; 362 /* 363 * Validate PTEs for kernel text (RO). 364 * Pages up to "start" (vectors and Mac OS global variable space) 365 * must be writable for the ROM. 366 */ 367 pte = PA2VA(kptpa, pt_entry_t *); 368 pte = &pte[m68k_btop(KERNBASE)]; 369 epte = &pte[m68k_btop(m68k_round_page(start))]; 370 protopte = firstpa | PG_RW | PG_V; 371 while (pte < epte) { 372 *pte++ = protopte; 373 protopte += PAGE_SIZE; 374 } 375 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 376 protopte = (protopte & ~PG_PROT) | PG_RO; 377 while (pte < epte) { 378 *pte++ = protopte; 379 protopte += PAGE_SIZE; 380 } 381 /* 382 * Validate PTEs for kernel data/bss, dynamic data allocated 383 * by us so far (kstpa - firstpa bytes), and pages for lwp0 384 * u-area and page table allocated below (RW). 385 */ 386 epte = PA2VA(kptpa, pt_entry_t *); 387 epte = &epte[m68k_btop(kstpa - firstpa)]; 388 protopte = (protopte & ~PG_PROT) | PG_RW; 389 /* 390 * Enable copy-back caching of data pages 391 */ 392 if (mmutype == MMU_68040) 393 protopte |= PG_CCB; 394 while (pte < epte) { 395 *pte++ = protopte; 396 protopte += PAGE_SIZE; 397 } 398 /* 399 * Map the kernel segment table cache invalidated for 68040/68060. 400 * (for the 68040 not strictly necessary, but recommended by Motorola; 401 * for the 68060 mandatory) 402 */ 403 epte = PA2VA(kptpa, pt_entry_t *); 404 epte = &epte[m68k_btop(nextpa - firstpa)]; 405 protopte = (protopte & ~PG_PROT) | PG_RW; 406 if (mmutype == MMU_68040) { 407 protopte &= ~PG_CCB; 408 protopte |= PG_CIN; 409 } 410 while (pte < epte) { 411 *pte++ = protopte; 412 protopte += PAGE_SIZE; 413 } 414 415 /* 416 * Finally, validate the internal IO space PTEs (RW+CI). 417 */ 418 419 #define PTE2VA(pte) m68k_ptob(pte - PA2VA(kptpa, pt_entry_t *)) 420 421 protopte = IOBase | PG_RW | PG_CI | PG_V; 422 IOBase = PTE2VA(pte); 423 epte = &pte[IIOMAPSIZE]; 424 while (pte < epte) { 425 *pte++ = protopte; 426 protopte += PAGE_SIZE; 427 } 428 429 protopte = (pt_entry_t)ROMBase | PG_RO | PG_V; 430 ROMBase = (void *)PTE2VA(pte); 431 epte = &pte[ROMMAPSIZE]; 432 while (pte < epte) { 433 *pte++ = protopte; 434 protopte += PAGE_SIZE; 435 } 436 437 if (vidlen) { 438 protopte = m68k_trunc_page(mac68k_video.mv_phys) | 439 PG_RW | PG_V | PG_CI; 440 newvideoaddr = PTE2VA(pte) 441 + m68k_page_offset(mac68k_video.mv_phys); 442 epte = &pte[VIDMAPSIZE]; 443 while (pte < epte) { 444 *pte++ = protopte; 445 protopte += PAGE_SIZE; 446 } 447 } 448 virtual_avail = PTE2VA(pte); 449 450 /* 451 * Calculate important exported kernel addresses and related values. 452 */ 453 /* 454 * Sysseg: base of kernel segment table 455 */ 456 Sysseg = PA2VA(kstpa, st_entry_t *); 457 Sysseg_pa = PA2VA(kstpa, paddr_t); 458 #if defined(M68040) 459 if (mmutype == MMU_68040) 460 protostfree = stfree; 461 #endif 462 /* 463 * Sysptmap: base of kernel page table map 464 */ 465 Sysptmap = PA2VA(kptmpa, pt_entry_t *); 466 /* 467 * Sysmap: kernel page table (as mapped through Sysptmap) 468 * Allocated at the end of KVA space. 469 */ 470 Sysmap = (pt_entry_t *)SYSMAP_VA; 471 472 /* 473 * Remember the u-area address so it can be loaded in the lwp0 474 * via uvm_lwp_setuarea() later in pmap_bootstrap_finalize(). 475 */ 476 lwp0uarea = PA2VA(lwp0upa, vaddr_t); 477 478 /* 479 * VM data structures are now initialized, set up data for 480 * the pmap module. 481 * 482 * Note about avail_end: msgbuf is initialized just after 483 * avail_end in machdep.c. Since the last page is used 484 * for rebooting the system (code is copied there and 485 * excution continues from copied code before the MMU 486 * is disabled), the msgbuf will get trounced between 487 * reboots if it's placed in the last physical page. 488 * To work around this, we move avail_end back one more 489 * page so the msgbuf can be preserved. 490 */ 491 avail_next = avail_start = m68k_round_page(nextpa); 492 avail_remaining = 0; 493 avail_range = -1; 494 for (i = 0; i < numranges; i++) { 495 if (low[i] <= avail_next && avail_next < high[i]) { 496 avail_range = i; 497 avail_remaining = high[i] - avail_next; 498 } else if (avail_range != -1) { 499 avail_remaining += (high[i] - low[i]); 500 } 501 } 502 physmem = m68k_btop(avail_remaining + nextpa - firstpa); 503 504 maxaddr = high[numranges - 1] - m68k_ptob(1); 505 high[numranges - 1] -= (m68k_round_page(MSGBUFSIZE) + m68k_ptob(1)); 506 avail_end = high[numranges - 1]; 507 mem_size = m68k_ptob(physmem); 508 virtual_end = VM_MAX_KERNEL_ADDRESS; 509 510 /* 511 * Allocate some fixed, special purpose kernel virtual addresses 512 */ 513 { 514 vaddr_t va = virtual_avail; 515 516 CADDR1 = (void *)va; 517 va += PAGE_SIZE; 518 CADDR2 = (void *)va; 519 va += PAGE_SIZE; 520 vmmap = (void *)va; 521 va += PAGE_SIZE; 522 msgbufaddr = (void *)va; 523 va += m68k_round_page(MSGBUFSIZE); 524 virtual_avail = va; 525 } 526 } 527 528 void 529 bootstrap_mac68k(int tc) 530 { 531 #if NZSC > 0 532 extern void zs_init(void); 533 #endif 534 extern int *esym; 535 paddr_t nextpa; 536 void *oldROMBase; 537 538 if (mac68k_machine.do_graybars) 539 printf("Bootstrapping NetBSD/mac68k.\n"); 540 541 oldROMBase = ROMBase; 542 mac68k_video.mv_phys = mac68k_video.mv_kvaddr; 543 544 if (((tc & 0x80000000) && (mmutype == MMU_68030)) || 545 ((tc & 0x8000) && (mmutype == MMU_68040))) { 546 if (mac68k_machine.do_graybars) 547 printf("Getting mapping from MMU.\n"); 548 (void) get_mapping(); 549 if (mac68k_machine.do_graybars) 550 printf("Done.\n"); 551 } else { 552 /* MMU not enabled. Fake up ranges. */ 553 numranges = 1; 554 low[0] = 0; 555 high[0] = mac68k_machine.mach_memsize * (1024 * 1024); 556 if (mac68k_machine.do_graybars) 557 printf("Faked range to byte 0x%lx.\n", high[0]); 558 } 559 nextpa = load_addr + m68k_round_page(esym); 560 561 if (mac68k_machine.do_graybars) 562 printf("Bootstrapping the pmap system.\n"); 563 564 pmap_bootstrap(nextpa, load_addr); 565 566 if (mac68k_machine.do_graybars) 567 printf("Pmap bootstrapped.\n"); 568 569 if (!vidlen) 570 panic("Don't know how to relocate video!"); 571 572 if (mac68k_machine.do_graybars) 573 printf("Moving ROMBase from %p to %p.\n", oldROMBase, ROMBase); 574 575 mrg_fixupROMBase(oldROMBase, ROMBase); 576 577 if (mac68k_machine.do_graybars) 578 printf("Video address 0x%p -> 0x%p.\n", 579 (void *)mac68k_video.mv_kvaddr, (void *)newvideoaddr); 580 581 mac68k_set_io_offsets(IOBase); 582 583 /* 584 * If the serial ports are going (for console or 'echo'), then 585 * we need to make sure the IO change gets propagated properly. 586 * This resets the base addresses for the 8530 (serial) driver. 587 * 588 * WARNING!!! No printfs() (etc) BETWEEN zs_init() and the end 589 * of this function (where we start using the MMU, so the new 590 * address is correct. 591 */ 592 #if NZSC > 0 593 if (zsinited != 0) 594 zs_init(); 595 #endif 596 597 mac68k_video.mv_kvaddr = newvideoaddr; 598 } 599