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