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