1 /* $NetBSD: pmap_bootstrap.c,v 1.76 2009/01/10 11:28:47 tsutsui 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.76 2009/01/10 11:28:47 tsutsui Exp $"); 40 41 #include "opt_ddb.h" 42 #include "opt_kgdb.h" 43 #include "zsc.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/reboot.h> 48 49 #include <uvm/uvm_extern.h> 50 51 #include <machine/pte.h> 52 #include <machine/vmparam.h> 53 #include <machine/cpu.h> 54 #include <machine/pmap.h> 55 #include <machine/autoconf.h> 56 #include <machine/video.h> 57 58 #include <mac68k/mac68k/macrom.h> 59 60 #define PA2VA(v, t) (t)((u_int)(v) - firstpa) 61 62 extern char *etext; 63 extern int Sysptsize; 64 extern char *extiobase, *proc0paddr; 65 extern st_entry_t *Sysseg; 66 extern pt_entry_t *Sysptmap, *Sysmap; 67 68 extern int physmem; 69 extern paddr_t avail_start; 70 extern paddr_t avail_end; 71 extern vaddr_t virtual_avail, virtual_end; 72 extern vsize_t mem_size; 73 extern int protection_codes[]; 74 75 #if NZSC > 0 76 extern int zsinited; 77 #endif 78 79 /* 80 * These are used to map the RAM: 81 */ 82 int numranges; /* = 0 == don't use the ranges */ 83 u_long low[8]; 84 u_long high[8]; 85 u_long maxaddr; /* PA of the last physical page */ 86 int vidlen; 87 #define VIDMAPSIZE btoc(vidlen) 88 static vaddr_t newvideoaddr; 89 90 extern void * ROMBase; 91 92 /* 93 * Special purpose kernel virtual addresses, used for mapping 94 * physical pages for a variety of temporary or permanent purposes: 95 * 96 * CADDR1, CADDR2: pmap zero/copy operations 97 * vmmap: /dev/mem, crash dumps, parity error checking 98 * msgbufaddr: kernel message buffer 99 */ 100 void *CADDR1, *CADDR2; 101 char *vmmap; 102 void *msgbufaddr; 103 104 void pmap_bootstrap(paddr_t, paddr_t); 105 void bootstrap_mac68k(int); 106 107 /* 108 * Bootstrap the VM system. 109 * 110 * This is called with the MMU either on or off. If it's on, we assume 111 * that it's mapped with the same PA <=> LA mapping that we eventually 112 * want. The page sizes and the protections will be wrong, anyway. 113 * 114 * nextpa is the first address following the loaded kernel. On a IIsi 115 * on 12 May 1996, that was 0xf9000 beyond firstpa. 116 */ 117 void 118 pmap_bootstrap(paddr_t nextpa, paddr_t firstpa) 119 { 120 paddr_t kstpa, kptpa, kptmpa, p0upa; 121 u_int nptpages, kstsize; 122 paddr_t avail_next; 123 int avail_remaining; 124 int avail_range; 125 int i; 126 st_entry_t protoste, *ste; 127 pt_entry_t protopte, *pte, *epte; 128 extern char start[]; 129 130 vidlen = m68k_round_page(mac68k_video.mv_height * 131 mac68k_video.mv_stride + m68k_page_offset(mac68k_video.mv_phys)); 132 133 /* 134 * Calculate important physical addresses: 135 * 136 * kstpa kernel segment table 1 page (!040) 137 * N pages (040) 138 * 139 * kptpa statically allocated 140 * kernel PT pages Sysptsize+ pages 141 * 142 * [ Sysptsize is the number of pages of PT, IIOMAPSIZE and 143 * NBMAPSIZE are the number of PTEs, hence we need to round 144 * the total to a page boundary with IO maps at the end. ] 145 * 146 * kptmpa kernel PT map 1 page 147 * 148 * p0upa proc 0 u-area UPAGES pages 149 * 150 */ 151 if (mmutype == MMU_68040) 152 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE); 153 else 154 kstsize = 1; 155 kstpa = nextpa; 156 nextpa += kstsize * PAGE_SIZE; 157 kptmpa = nextpa; 158 nextpa += PAGE_SIZE; 159 p0upa = nextpa; 160 nextpa += USPACE; 161 kptpa = nextpa; 162 nptpages = Sysptsize + 163 (IIOMAPSIZE + ROMMAPSIZE + VIDMAPSIZE + NPTEPG - 1) / NPTEPG; 164 nextpa += nptpages * PAGE_SIZE; 165 166 for (i = 0; i < numranges; i++) 167 if (low[i] <= firstpa && firstpa < high[i]) 168 break; 169 if (i >= numranges || nextpa > high[i]) { 170 if (mac68k_machine.do_graybars) { 171 printf("Failure in NetBSD boot; "); 172 if (i < numranges) 173 printf("nextpa=0x%lx, high[%d]=0x%lx.\n", 174 nextpa, i, high[i]); 175 else 176 printf("can't find kernel RAM segment.\n"); 177 printf("You're hosed! Try booting with 32-bit "); 178 printf("addressing enabled in the memory control "); 179 printf("panel.\n"); 180 printf("Older machines may need Mode32 to get that "); 181 printf("option.\n"); 182 } 183 panic("Cannot work with the current memory mappings."); 184 } 185 186 /* 187 * Initialize segment table and kernel page table map. 188 * 189 * On 68030s and earlier MMUs the two are identical except for 190 * the valid bits so both are initialized with essentially the 191 * same values. On the 68040, which has a mandatory 3-level 192 * structure, the segment table holds the level 1 table and part 193 * (or all) of the level 2 table and hence is considerably 194 * different. Here the first level consists of 128 descriptors 195 * (512 bytes) each mapping 32mb of address space. Each of these 196 * points to blocks of 128 second level descriptors (512 bytes) 197 * each mapping 256kb. Note that there may be additional "segment 198 * table" pages depending on how large MAXKL2SIZE is. 199 * 200 * XXX cramming two levels of mapping into the single "segment" 201 * table on the 68040 is intended as a temporary hack to get things 202 * working. The 224mb of address space that this allows will most 203 * likely be insufficient in the future (at least for the kernel). 204 */ 205 if (mmutype == MMU_68040) { 206 int num; 207 208 /* 209 * First invalidate the entire "segment table" pages 210 * (levels 1 and 2 have the same "invalid" value). 211 */ 212 pte = PA2VA(kstpa, u_int *); 213 epte = &pte[kstsize * NPTEPG]; 214 while (pte < epte) 215 *pte++ = SG_NV; 216 /* 217 * Initialize level 2 descriptors (which immediately 218 * follow the level 1 table). We need: 219 * NPTEPG / SG4_LEV3SIZE 220 * level 2 descriptors to map each of the nptpages 221 * pages of PTEs. Note that we set the "used" bit 222 * now to save the HW the expense of doing it. 223 */ 224 num = nptpages * (NPTEPG / SG4_LEV3SIZE); 225 pte = &(PA2VA(kstpa, u_int *))[SG4_LEV1SIZE]; 226 epte = &pte[num]; 227 protoste = kptpa | SG_U | SG_RW | SG_V; 228 while (pte < epte) { 229 *pte++ = protoste; 230 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 231 } 232 /* 233 * Initialize level 1 descriptors. We need: 234 * roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE 235 * level 1 descriptors to map the `num' level 2's. 236 */ 237 pte = PA2VA(kstpa, u_int *); 238 epte = &pte[roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE]; 239 protoste = (u_int)&pte[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V; 240 while (pte < epte) { 241 *pte++ = protoste; 242 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t)); 243 } 244 /* 245 * Initialize the final level 1 descriptor to map the last 246 * block of level 2 descriptors. 247 */ 248 ste = &(PA2VA(kstpa, u_int*))[SG4_LEV1SIZE-1]; 249 pte = &(PA2VA(kstpa, u_int*))[kstsize*NPTEPG - SG4_LEV2SIZE]; 250 *ste = (u_int)pte | SG_U | SG_RW | SG_V; 251 /* 252 * Now initialize the final portion of that block of 253 * descriptors to map Sysmap. 254 */ 255 pte = &(PA2VA(kstpa, u_int*)) 256 [kstsize*NPTEPG - NPTEPG/SG4_LEV3SIZE]; 257 epte = &pte[NPTEPG/SG4_LEV3SIZE]; 258 protoste = kptmpa | SG_U | SG_RW | SG_V; 259 while (pte < epte) { 260 *pte++ = protoste; 261 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t)); 262 } 263 /* 264 * Initialize Sysptmap 265 */ 266 pte = PA2VA(kptmpa, u_int *); 267 epte = &pte[nptpages]; 268 protopte = kptpa | PG_RW | PG_CI | PG_V; 269 while (pte < epte) { 270 *pte++ = protopte; 271 protopte += PAGE_SIZE; 272 } 273 /* 274 * Invalidate all but the last remaining entry. 275 */ 276 epte = &(PA2VA(kptmpa, u_int *))[NPTEPG - 1]; 277 while (pte < epte) { 278 *pte++ = PG_NV; 279 } 280 /* 281 * Initialize the last one to point to Sysptmap. 282 */ 283 *pte = kptmpa | PG_RW | PG_CI | PG_V; 284 } else { 285 /* 286 * Map the page table pages in both the HW segment table 287 * and the software Sysptmap. 288 */ 289 ste = PA2VA(kstpa, u_int*); 290 pte = PA2VA(kptmpa, u_int*); 291 epte = &pte[nptpages]; 292 protoste = kptpa | SG_RW | SG_V; 293 protopte = kptpa | PG_RW | PG_CI | PG_V; 294 while (pte < epte) { 295 *ste++ = protoste; 296 *pte++ = protopte; 297 protoste += PAGE_SIZE; 298 protopte += PAGE_SIZE; 299 } 300 /* 301 * Invalidate all but the last remaining entries in both. 302 */ 303 epte = &(PA2VA(kptmpa, u_int *))[NPTEPG - 1]; 304 while (pte < epte) { 305 *ste++ = SG_NV; 306 *pte++ = PG_NV; 307 } 308 /* 309 * Initialize the last one to point to Sysptmap. 310 */ 311 *ste = kptmpa | SG_RW | SG_V; 312 *pte = kptmpa | PG_RW | PG_CI | PG_V; 313 } 314 315 /* 316 * Initialize kernel page table. 317 * Start by invalidating the `nptpages' that we have allocated. 318 */ 319 pte = PA2VA(kptpa, u_int *); 320 epte = &pte[nptpages * NPTEPG]; 321 while (pte < epte) 322 *pte++ = PG_NV; 323 324 /* 325 * Validate PTEs for kernel text (RO). 326 * Pages up to "start" must be writable for the ROM. 327 */ 328 pte = &(PA2VA(kptpa, u_int *))[m68k_btop(KERNBASE)]; 329 /* XXX why KERNBASE relative? */ 330 epte = &pte[m68k_btop(m68k_round_page(start))]; 331 protopte = firstpa | PG_RW | PG_V; 332 while (pte < epte) { 333 *pte++ = protopte; 334 protopte += PAGE_SIZE; 335 } 336 /* XXX why KERNBASE relative? */ 337 epte = &pte[m68k_btop(m68k_trunc_page(&etext))]; 338 protopte = (protopte & ~PG_PROT) | PG_RO; 339 while (pte < epte) { 340 *pte++ = protopte; 341 protopte += PAGE_SIZE; 342 } 343 /* 344 * Validate PTEs for kernel data/bss, dynamic data allocated 345 * by us so far (nextpa - firstpa bytes), and pages for proc0 346 * u-area and page table allocated below (RW). 347 */ 348 epte = &(PA2VA(kptpa, u_int *))[m68k_btop(nextpa - firstpa)]; 349 protopte = (protopte & ~PG_PROT) | PG_RW; 350 /* 351 * Enable copy-back caching of data pages 352 */ 353 if (mmutype == MMU_68040) 354 protopte |= PG_CCB; 355 while (pte < epte) { 356 *pte++ = protopte; 357 protopte += PAGE_SIZE; 358 } 359 360 #define PTE2VA(pte) m68k_ptob(pte - PA2VA(kptpa, pt_entry_t *)) 361 362 protopte = IOBase | PG_RW | PG_CI | PG_V; 363 IOBase = PTE2VA(pte); 364 epte = &pte[IIOMAPSIZE]; 365 while (pte < epte) { 366 *pte++ = protopte; 367 protopte += PAGE_SIZE; 368 } 369 370 protopte = (pt_entry_t)ROMBase | PG_RO | PG_V; 371 ROMBase = (void *)PTE2VA(pte); 372 epte = &pte[ROMMAPSIZE]; 373 while (pte < epte) { 374 *pte++ = protopte; 375 protopte += PAGE_SIZE; 376 } 377 378 if (vidlen) { 379 protopte = m68k_trunc_page(mac68k_video.mv_phys) | 380 PG_RW | PG_V | PG_CI; 381 newvideoaddr = PTE2VA(pte) 382 + m68k_page_offset(mac68k_video.mv_phys); 383 epte = &pte[VIDMAPSIZE]; 384 while (pte < epte) { 385 *pte++ = protopte; 386 protopte += PAGE_SIZE; 387 } 388 } 389 virtual_avail = PTE2VA(pte); 390 391 /* 392 * Calculate important exported kernel virtual addresses 393 */ 394 /* 395 * Sysseg: base of kernel segment table 396 */ 397 Sysseg = PA2VA(kstpa, st_entry_t *); 398 /* 399 * Sysptmap: base of kernel page table map 400 */ 401 Sysptmap = PA2VA(kptmpa, pt_entry_t *); 402 /* 403 * Sysmap: kernel page table (as mapped through Sysptmap) 404 * Allocated at the end of KVA space. 405 */ 406 Sysmap = (pt_entry_t *)m68k_ptob((NPTEPG - 1) * NPTEPG); 407 408 /* 409 * Setup u-area for process 0. 410 */ 411 /* 412 * Zero the u-area. 413 * NOTE: `pte' and `epte' aren't PTEs here. 414 */ 415 pte = PA2VA(p0upa, u_int *); 416 epte = (u_int *)(PA2VA(p0upa, u_int) + USPACE); 417 while (pte < epte) 418 *pte++ = 0; 419 /* 420 * Remember the u-area address so it can be loaded in the 421 * proc struct p_addr field later. 422 */ 423 proc0paddr = PA2VA(p0upa, char *); 424 425 /* 426 * VM data structures are now initialized, set up data for 427 * the pmap module. 428 * 429 * Note about avail_end: msgbuf is initialized just after 430 * avail_end in machdep.c. Since the last page is used 431 * for rebooting the system (code is copied there and 432 * excution continues from copied code before the MMU 433 * is disabled), the msgbuf will get trounced between 434 * reboots if it's placed in the last physical page. 435 * To work around this, we move avail_end back one more 436 * page so the msgbuf can be preserved. 437 */ 438 avail_next = avail_start = m68k_round_page(nextpa); 439 avail_remaining = 0; 440 avail_range = -1; 441 for (i = 0; i < numranges; i++) { 442 if (low[i] <= avail_next && avail_next < high[i]) { 443 avail_range = i; 444 avail_remaining = high[i] - avail_next; 445 } else if (avail_range != -1) { 446 avail_remaining += (high[i] - low[i]); 447 } 448 } 449 physmem = m68k_btop(avail_remaining + nextpa - firstpa); 450 451 maxaddr = high[numranges - 1] - m68k_ptob(1); 452 high[numranges - 1] -= (m68k_round_page(MSGBUFSIZE) + m68k_ptob(1)); 453 avail_end = high[numranges - 1]; 454 mem_size = m68k_ptob(physmem); 455 virtual_end = VM_MAX_KERNEL_ADDRESS; 456 457 /* 458 * Initialize protection array. 459 * XXX don't use a switch statement, it might produce an 460 * absolute "jmp" table. 461 */ 462 { 463 int *kp; 464 465 kp = (int *)&protection_codes; 466 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_NONE] = 0; 467 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_NONE] = PG_RO; 468 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 469 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 470 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 471 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 472 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 473 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 474 } 475 476 /* 477 * Kernel page/segment table allocated above, 478 * just initialize pointers. 479 */ 480 { 481 struct pmap *kpm = kernel_pmap_ptr; 482 483 kpm->pm_stab = Sysseg; 484 kpm->pm_ptab = Sysmap; 485 simple_lock_init(&kpm->pm_lock); 486 kpm->pm_count = 1; 487 kpm->pm_stpa = (st_entry_t *)kstpa; 488 /* 489 * For the 040 we also initialize the free level 2 490 * descriptor mask noting that we have used: 491 * 0: level 1 table 492 * 1 to `num': map page tables 493 * MAXKL2SIZE-1: maps kptmpa 494 */ 495 if (mmutype == MMU_68040) { 496 int num; 497 498 kpm->pm_stfree = ~l2tobm(0); 499 num = roundup(nptpages * (NPTEPG / SG4_LEV3SIZE), 500 SG4_LEV2SIZE) / SG4_LEV2SIZE; 501 while (num) 502 kpm->pm_stfree &= ~l2tobm(num--); 503 kpm->pm_stfree &= ~l2tobm(MAXKL2SIZE-1); 504 for (num = MAXKL2SIZE; 505 num < sizeof(kpm->pm_stfree)*NBBY; 506 num++) 507 kpm->pm_stfree &= ~l2tobm(num); 508 } 509 } 510 511 /* 512 * Allocate some fixed, special purpose kernel virtual addresses 513 */ 514 { 515 vaddr_t va = virtual_avail; 516 517 CADDR1 = (void *)va; 518 va += PAGE_SIZE; 519 CADDR2 = (void *)va; 520 va += PAGE_SIZE; 521 vmmap = (void *)va; 522 va += PAGE_SIZE; 523 msgbufaddr = (void *)va; 524 va += m68k_round_page(MSGBUFSIZE); 525 virtual_avail = va; 526 } 527 } 528 529 void 530 bootstrap_mac68k(int tc) 531 { 532 #if NZSC > 0 533 extern void zs_init(void); 534 #endif 535 extern int *esym; 536 paddr_t nextpa; 537 void *oldROMBase; 538 539 if (mac68k_machine.do_graybars) 540 printf("Bootstrapping NetBSD/mac68k.\n"); 541 542 oldROMBase = ROMBase; 543 mac68k_video.mv_phys = mac68k_video.mv_kvaddr; 544 545 if (((tc & 0x80000000) && (mmutype == MMU_68030)) || 546 ((tc & 0x8000) && (mmutype == MMU_68040))) { 547 if (mac68k_machine.do_graybars) 548 printf("Getting mapping from MMU.\n"); 549 (void) get_mapping(); 550 if (mac68k_machine.do_graybars) 551 printf("Done.\n"); 552 } else { 553 /* MMU not enabled. Fake up ranges. */ 554 numranges = 1; 555 low[0] = 0; 556 high[0] = mac68k_machine.mach_memsize * (1024 * 1024); 557 if (mac68k_machine.do_graybars) 558 printf("Faked range to byte 0x%lx.\n", high[0]); 559 } 560 nextpa = load_addr + m68k_round_page(esym); 561 562 if (mac68k_machine.do_graybars) 563 printf("Bootstrapping the pmap system.\n"); 564 565 pmap_bootstrap(nextpa, load_addr); 566 567 if (mac68k_machine.do_graybars) 568 printf("Pmap bootstrapped.\n"); 569 570 if (!vidlen) 571 panic("Don't know how to relocate video!"); 572 573 if (mac68k_machine.do_graybars) 574 printf("Moving ROMBase from %p to %p.\n", oldROMBase, ROMBase); 575 576 mrg_fixupROMBase(oldROMBase, ROMBase); 577 578 if (mac68k_machine.do_graybars) 579 printf("Video address 0x%p -> 0x%p.\n", 580 (void *)mac68k_video.mv_kvaddr, (void *)newvideoaddr); 581 582 mac68k_set_io_offsets(IOBase); 583 584 /* 585 * If the serial ports are going (for console or 'echo'), then 586 * we need to make sure the IO change gets propagated properly. 587 * This resets the base addresses for the 8530 (serial) driver. 588 * 589 * WARNING!!! No printfs() (etc) BETWEEN zs_init() and the end 590 * of this function (where we start using the MMU, so the new 591 * address is correct. 592 */ 593 #if NZSC > 0 594 if (zsinited != 0) 595 zs_init(); 596 #endif 597 598 mac68k_video.mv_kvaddr = newvideoaddr; 599 } 600