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