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