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