1 /* $NetBSD: machdep.c,v 1.47 2006/09/15 16:37:18 gdamore Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 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, The Mach Operating System project at 10 * Carnegie-Mellon University and Ralph Campbell. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)machdep.c 8.3 (Berkeley) 1/12/94 37 */ 38 /* 39 * Copyright (c) 1988 University of Utah. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * the Systems Programming Group of the University of Utah Computer 43 * Science Department, The Mach Operating System project at 44 * Carnegie-Mellon University and Ralph Campbell. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. All advertising materials mentioning features or use of this software 55 * must display the following acknowledgement: 56 * This product includes software developed by the University of 57 * California, Berkeley and its contributors. 58 * 4. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * @(#)machdep.c 8.3 (Berkeley) 1/12/94 75 */ 76 77 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 78 79 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.47 2006/09/15 16:37:18 gdamore Exp $"); 80 81 /* from: Utah Hdr: machdep.c 1.63 91/04/24 */ 82 83 #include "opt_ddb.h" 84 #include "opt_kgdb.h" 85 86 #include <sys/param.h> 87 #include <sys/signalvar.h> 88 #include <sys/kernel.h> 89 #include <sys/proc.h> 90 #include <sys/buf.h> 91 #include <sys/reboot.h> 92 #include <sys/conf.h> 93 #include <sys/file.h> 94 #include <sys/callout.h> 95 #include <sys/malloc.h> 96 #include <sys/mbuf.h> 97 #include <sys/msgbuf.h> 98 #include <sys/ioctl.h> 99 #include <sys/device.h> 100 #include <sys/user.h> 101 #include <sys/exec.h> 102 #include <sys/mount.h> 103 #include <sys/sa.h> 104 #include <sys/syscallargs.h> 105 #include <sys/kcore.h> 106 #include <sys/ksyms.h> 107 108 #include <uvm/uvm_extern.h> 109 110 #include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */ 111 112 #include <machine/cpu.h> 113 #include <machine/reg.h> 114 #include <machine/psl.h> 115 #include <machine/pte.h> 116 117 #ifdef DDB 118 #include <machine/db_machdep.h> 119 #include <ddb/db_extern.h> 120 #endif 121 122 #include <machine/intr.h> 123 #include <machine/mainboard.h> 124 #include <machine/sysconf.h> 125 #include <machine/autoconf.h> 126 #include <machine/bootinfo.h> 127 #include <machine/prom.h> 128 #include <dev/clock_subr.h> 129 #include <dev/cons.h> 130 131 #include <sys/boot_flag.h> 132 133 #include "fs_mfs.h" 134 #include "opt_ddb.h" 135 #include "opt_execfmt.h" 136 137 #include "zsc.h" /* XXX */ 138 #include "com.h" /* XXX */ 139 #include "ksyms.h" 140 141 /* Our exported CPU info; we can have only one. */ 142 struct cpu_info cpu_info_store; 143 144 /* maps for VM objects */ 145 146 struct vm_map *exec_map = NULL; 147 struct vm_map *mb_map = NULL; 148 struct vm_map *phys_map = NULL; 149 150 int physmem; /* max supported memory, changes to actual */ 151 char *bootinfo = NULL; /* pointer to bootinfo structure */ 152 153 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; 154 int mem_cluster_cnt; 155 156 void to_monitor __P((int)) __attribute__((__noreturn__)); 157 void prom_halt __P((int)) __attribute__((__noreturn__)); 158 159 #ifdef KGDB 160 void zs_kgdb_init __P((void)); 161 void kgdb_connect __P((int)); 162 #endif 163 164 struct evcnt soft_evcnt[IPL_NSOFT]; 165 166 /* 167 * Local functions. 168 */ 169 int initcpu __P((void)); 170 void configure __P((void)); 171 172 void mach_init __P((int, char *[], char*[], u_int, char *)); 173 int memsize_scan __P((caddr_t)); 174 175 #ifdef DEBUG 176 /* stacktrace code violates prototypes to get callee's registers */ 177 extern void stacktrace __P((void)); /*XXX*/ 178 #endif 179 180 /* 181 * safepri is a safe priority for sleep to set for a spin-wait 182 * during autoconfiguration or after a panic. Used as an argument to splx(). 183 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also 184 * disables mips1 FPU interrupts. 185 */ 186 int safepri = MIPS3_PSL_LOWIPL; /* XXX */ 187 extern struct user *proc0paddr; 188 189 /* locore callback-vector setup */ 190 extern void mips_vector_init __P((void)); 191 extern void prom_init __P((void)); 192 extern void pizazz_init __P((void)); 193 194 /* platform-specific initialization vector */ 195 static void unimpl_cons_init __P((void)); 196 static void unimpl_iointr __P((unsigned, unsigned, unsigned, unsigned)); 197 static int unimpl_memsize __P((caddr_t)); 198 static void unimpl_intr_establish __P((int, int (*)__P((void *)), void *)); 199 200 struct platform platform = { 201 .iobus = "iobus not set", 202 .cons_init = unimpl_cons_init, 203 .iointr = unimpl_iointr, 204 .memsize = unimpl_memsize, 205 .intr_establish = unimpl_intr_establish, 206 .clkinit = NULL, 207 }; 208 209 struct consdev *cn_tab = NULL; 210 extern struct consdev consdev_prom; 211 extern struct consdev consdev_zs; 212 213 static void null_cnprobe __P((struct consdev *)); 214 static void prom_cninit __P((struct consdev *)); 215 static int prom_cngetc __P((dev_t)); 216 static void prom_cnputc __P((dev_t, int)); 217 static void null_cnpollc __P((dev_t, int)); 218 219 struct consdev consdev_prom = { 220 null_cnprobe, 221 prom_cninit, 222 prom_cngetc, 223 prom_cnputc, 224 null_cnpollc, 225 }; 226 227 228 /* 229 * Do all the stuff that locore normally does before calling main(). 230 * Process arguments passed to us by the prom monitor. 231 * Return the first page address following the system. 232 */ 233 void 234 mach_init(argc, argv, envp, bim, bip) 235 int argc; 236 char *argv[]; 237 char *envp[]; 238 u_int bim; 239 char *bip; 240 { 241 u_long first, last; 242 caddr_t kernend, v; 243 char *cp; 244 int i, howto; 245 extern char edata[], end[]; 246 const char *bi_msg; 247 #if NKSYMS || defined(DDB) || defined(LKM) 248 int nsym = 0; 249 caddr_t ssym = 0; 250 caddr_t esym = 0; 251 struct btinfo_symtab *bi_syms; 252 #endif 253 254 255 /* Check for valid bootinfo passed from bootstrap */ 256 if (bim == BOOTINFO_MAGIC) { 257 struct btinfo_magic *bi_magic; 258 259 bootinfo = (char *)BOOTINFO_ADDR; /* XXX */ 260 bi_magic = lookup_bootinfo(BTINFO_MAGIC); 261 if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC) 262 bi_msg = "invalid bootinfo structure.\n"; 263 else 264 bi_msg = NULL; 265 } else 266 bi_msg = "invalid bootinfo (standalone boot?)\n"; 267 268 /* clear the BSS segment */ 269 kernend = (caddr_t)mips_round_page(end); 270 memset(edata, 0, end - edata); 271 272 #if NKSYMS || defined(DDB) || defined(LKM) 273 bi_syms = lookup_bootinfo(BTINFO_SYMTAB); 274 275 /* Load sysmbol table if present */ 276 if (bi_syms != NULL) { 277 nsym = bi_syms->nsym; 278 ssym = (caddr_t)bi_syms->ssym; 279 esym = (caddr_t)bi_syms->esym; 280 kernend = (caddr_t)mips_round_page(esym); 281 } 282 #endif 283 284 prom_init(); 285 consinit(); 286 287 if (bi_msg != NULL) 288 printf(bi_msg); 289 290 /* 291 * Set the VM page size. 292 */ 293 uvm_setpagesize(); 294 295 /* Find out how much memory is available. */ 296 physmem = memsize_scan(kernend); 297 298 /* 299 * Now that we know how much memory we have, initialize the 300 * mem cluster array. 301 */ 302 mem_clusters[0].start = 0; /* XXX is this correct? */ 303 mem_clusters[0].size = ctob(physmem); 304 mem_cluster_cnt = 1; 305 306 /* 307 * Copy exception-dispatch code down to exception vector. 308 * Initialize locore-function vector. 309 * Clear out the I and D caches. 310 */ 311 mips_vector_init(); 312 313 /* Look at argv[0] and compute bootdev */ 314 makebootdev(argv[0]); 315 316 /* 317 * Look at arguments passed to us and compute boothowto. 318 */ 319 boothowto = RB_AUTOBOOT; 320 for (i = 1; i < argc; i++) { 321 for (cp = argv[i]; *cp; cp++) { 322 /* Ignore superfluous '-', if there is one */ 323 if (*cp == '-') 324 continue; 325 326 howto = 0; 327 BOOT_FLAG(*cp, howto); 328 if (! howto) 329 printf("bootflag '%c' not recognised\n", *cp); 330 else 331 boothowto |= howto; 332 } 333 } 334 335 336 #if NKSYMS || defined(DDB) || defined(LKM) 337 /* init symbols if present */ 338 if (esym) 339 ksyms_init(esym - ssym, ssym, esym); 340 #endif 341 #ifdef DDB 342 if (boothowto & RB_KDB) 343 Debugger(); 344 #endif 345 #ifdef KGDB 346 zs_kgdb_init(); /* XXX */ 347 if (boothowto & RB_KDB) 348 kgdb_connect(0); 349 #endif 350 351 #ifdef MFS 352 /* 353 * Check to see if a mini-root was loaded into memory. It resides 354 * at the start of the next page just after the end of BSS. 355 */ 356 if (boothowto & RB_MINIROOT) 357 kernend += round_page(mfs_initminiroot(kernend)); 358 #endif 359 360 /* 361 * Load the rest of the available pages into the VM system. 362 */ 363 first = round_page(MIPS_KSEG0_TO_PHYS(kernend)); 364 last = mem_clusters[0].start + mem_clusters[0].size; 365 uvm_page_physload(atop(first), atop(last), atop(first), atop(last), 366 VM_FREELIST_DEFAULT); 367 368 /* 369 * Initialize error message buffer (at end of core). 370 */ 371 mips_init_msgbuf(); 372 373 /* 374 * Initialize the virtual memory system. 375 */ 376 pmap_bootstrap(); 377 378 /* 379 * Allocate space for proc0's USPACE. 380 */ 381 v = (caddr_t)uvm_pageboot_alloc(USPACE); 382 lwp0.l_addr = proc0paddr = (struct user *)v; 383 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1; 384 curpcb = &lwp0.l_addr->u_pcb; 385 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */ 386 387 /* 388 * Set up interrupt handling and I/O addresses. 389 */ 390 pizazz_init(); 391 } 392 393 394 395 /* 396 * cpu_startup: allocate memory for variable-sized tables, 397 * initialize CPU, and do autoconfiguration. 398 */ 399 void 400 cpu_startup() 401 { 402 vaddr_t minaddr, maxaddr; 403 char pbuf[9]; 404 #ifdef DEBUG 405 extern int pmapdebug; 406 int opmapdebug = pmapdebug; 407 408 pmapdebug = 0; 409 #endif 410 411 /* 412 * Good {morning,afternoon,evening,night}. 413 */ 414 printf("%s%s", copyright, version); 415 printf("%s\n", cpu_model); 416 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 417 printf("total memory = %s\n", pbuf); 418 419 minaddr = 0; 420 /* 421 * Allocate a submap for exec arguments. This map effectively 422 * limits the number of processes exec'ing at any time. 423 */ 424 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 425 16 * NCARGS, TRUE, FALSE, NULL); 426 /* 427 * Allocate a submap for physio 428 */ 429 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 430 VM_PHYS_SIZE, TRUE, FALSE, NULL); 431 432 /* 433 * No need to allocate an mbuf cluster submap. Mbuf clusters 434 * are allocated via the pool allocator, and we use KSEG to 435 * map those pages. 436 */ 437 438 #ifdef DEBUG 439 pmapdebug = opmapdebug; 440 #endif 441 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 442 printf("avail memory = %s\n", pbuf); 443 } 444 445 /* 446 * Look up information in bootinfo of boot loader. 447 */ 448 void * 449 lookup_bootinfo(type) 450 int type; 451 { 452 struct btinfo_common *bt; 453 char *help = bootinfo; 454 455 /* Check for a bootinfo record first. */ 456 if (help == NULL) 457 return (NULL); 458 459 do { 460 bt = (struct btinfo_common *)help; 461 if (bt->type == type) 462 return ((void *)help); 463 help += bt->next; 464 } while (bt->next != 0 && 465 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE); 466 467 return (NULL); 468 } 469 470 int waittime = -1; 471 472 /* 473 * call PROM to halt or reboot. 474 */ 475 void 476 prom_halt(howto) 477 int howto; 478 { 479 if (howto & RB_HALT) 480 MIPS_PROM(reinit)(); 481 MIPS_PROM(reboot)(); 482 /* NOTREACHED */ 483 } 484 485 void 486 cpu_reboot(howto, bootstr) 487 volatile int howto; 488 char *bootstr; 489 { 490 /* take a snap shot before clobbering any registers */ 491 if (curlwp) 492 savectx((struct user *)curpcb); 493 494 #ifdef DEBUG 495 if (panicstr) 496 stacktrace(); 497 #endif 498 499 /* If system is cold, just halt. */ 500 if (cold) { 501 howto |= RB_HALT; 502 goto haltsys; 503 } 504 505 /* If "always halt" was specified as a boot flag, obey. */ 506 if ((boothowto & RB_HALT) != 0) 507 howto |= RB_HALT; 508 509 boothowto = howto; 510 if ((howto & RB_NOSYNC) == 0 && waittime < 0) { 511 /* 512 * Synchronize the disks.... 513 */ 514 waittime = 0; 515 vfs_shutdown(); 516 517 /* 518 * If we've been adjusting the clock, the todr 519 * will be out of synch; adjust it now. 520 */ 521 resettodr(); 522 } 523 524 /* Disable interrupts. */ 525 splhigh(); 526 527 /* If rebooting and a dump is requested do it. */ 528 #if 0 529 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 530 #else 531 if (howto & RB_DUMP) 532 #endif 533 dumpsys(); 534 535 haltsys: 536 537 /* run any shutdown hooks */ 538 doshutdownhooks(); 539 540 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 541 prom_halt(0x80); /* rom monitor RB_PWOFF */ 542 543 /* Finally, halt/reboot the system. */ 544 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting..."); 545 prom_halt(howto & RB_HALT); 546 /*NOTREACHED*/ 547 } 548 549 int 550 initcpu() 551 { 552 spl0(); /* safe to turn interrupts on now */ 553 return 0; 554 } 555 556 static void 557 unimpl_cons_init() 558 { 559 560 panic("sysconf.init didn't set cons_init"); 561 } 562 563 static void 564 unimpl_iointr(mask, pc, statusreg, causereg) 565 u_int mask; 566 u_int pc; 567 u_int statusreg; 568 u_int causereg; 569 { 570 571 panic("sysconf.init didn't set intr"); 572 } 573 574 static int 575 unimpl_memsize(first) 576 caddr_t first; 577 { 578 579 panic("sysconf.init didn't set memsize"); 580 } 581 582 void 583 unimpl_intr_establish(level, func, arg) 584 int level; 585 int (*func) __P((void *)); 586 void *arg; 587 { 588 panic("sysconf.init didn't init intr_establish"); 589 } 590 591 void 592 delay(n) 593 int n; 594 { 595 DELAY(n); 596 } 597 598 /* 599 * Find out how much memory is available by testing memory. 600 * Be careful to save and restore the original contents for msgbuf. 601 */ 602 int 603 memsize_scan(first) 604 caddr_t first; 605 { 606 volatile int *vp, *vp0; 607 int mem, tmp, tmp0; 608 609 #define PATTERN1 0xa5a5a5a5 610 #define PATTERN2 ~PATTERN1 611 612 /* 613 * Non destructive scan of memory to determine the size 614 * Use the first page to test for memory aliases. This 615 * also has the side effect of flushing the bus alignment 616 * buffer 617 */ 618 mem = btoc((paddr_t)first - MIPS_KSEG0_START); 619 vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT); 620 vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */ 621 tmp0 = *vp0; 622 while (vp < (int *)MIPS_MAX_MEM_ADDR) { 623 tmp = *vp; 624 *vp = PATTERN1; 625 *vp0 = PATTERN2; 626 wbflush(); 627 if (*vp != PATTERN1) 628 break; 629 *vp = PATTERN2; 630 *vp0 = PATTERN1; 631 wbflush(); 632 if (*vp != PATTERN2) 633 break; 634 *vp = tmp; 635 vp += PAGE_SIZE/sizeof(int); 636 mem++; 637 } 638 *vp0 = tmp0; 639 return mem; 640 } 641 642 /* 643 * Console initialization: called early on from main, 644 * before vm init or startup. Do enough configuration 645 * to choose and initialize a console. 646 */ 647 648 static void 649 null_cnprobe(cn) 650 struct consdev *cn; 651 { 652 } 653 654 static void 655 prom_cninit(cn) 656 struct consdev *cn; 657 { 658 extern const struct cdevsw cons_cdevsw; 659 660 cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0); 661 cn->cn_pri = CN_REMOTE; 662 } 663 664 static int 665 prom_cngetc(dev) 666 dev_t dev; 667 { 668 return MIPS_PROM(getchar)(); 669 } 670 671 static void 672 prom_cnputc(dev, c) 673 dev_t dev; 674 int c; 675 { 676 MIPS_PROM(putchar)(c); 677 } 678 679 static void 680 null_cnpollc(dev, on) 681 dev_t dev; 682 int on; 683 { 684 } 685 686 void 687 consinit() 688 { 689 int zs_unit; 690 691 zs_unit = 0; 692 cn_tab = &consdev_zs; 693 694 (*cn_tab->cn_init)(cn_tab); 695 } 696