1 /* $NetBSD: machdep.c,v 1.77 2014/03/24 20:06:32 christos 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.77 2014/03/24 20:06:32 christos 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 int nsym = 0; 187 char *ssym = 0; 188 char *esym = 0; 189 struct btinfo_symtab *bi_syms; 190 #endif 191 192 /* Check for valid bootinfo passed from bootstrap */ 193 if (bim == BOOTINFO_MAGIC) { 194 struct btinfo_magic *bi_magic; 195 196 bootinfo = (char *)BOOTINFO_ADDR; /* XXX */ 197 bi_magic = lookup_bootinfo(BTINFO_MAGIC); 198 if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC) 199 bi_msg = "invalid bootinfo structure.\n"; 200 else 201 bi_msg = NULL; 202 } else 203 bi_msg = "invalid bootinfo (standalone boot?)\n"; 204 205 /* clear the BSS segment */ 206 kernend = (void *)mips_round_page(end); 207 memset(edata, 0, end - edata); 208 209 /* 210 * Copy exception-dispatch code down to exception vector. 211 * Initialize locore-function vector. 212 * Clear out the I and D caches. 213 */ 214 mips_vector_init(NULL, false); 215 216 #if NKSYMS || defined(DDB) || defined(LKM) 217 bi_syms = lookup_bootinfo(BTINFO_SYMTAB); 218 219 /* Load sysmbol table if present */ 220 if (bi_syms != NULL) { 221 nsym = bi_syms->nsym; 222 ssym = (void *)bi_syms->ssym; 223 esym = (void *)bi_syms->esym; 224 kernend = (void *)mips_round_page(esym); 225 } 226 #endif 227 228 prom_init(); 229 consinit(); 230 231 if (bi_msg != NULL) 232 printf(bi_msg); 233 234 /* 235 * Set the VM page size. 236 */ 237 uvm_setpagesize(); 238 239 /* Find out how much memory is available. */ 240 physmem = memsize_scan(kernend); 241 242 /* 243 * Now that we know how much memory we have, initialize the 244 * mem cluster array. 245 */ 246 mem_clusters[0].start = 0; /* XXX is this correct? */ 247 mem_clusters[0].size = ctob(physmem); 248 mem_cluster_cnt = 1; 249 250 /* Look at argv[0] and compute bootdev */ 251 makebootdev(argv[0]); 252 253 /* 254 * Look at arguments passed to us and compute boothowto. 255 */ 256 boothowto = RB_AUTOBOOT; 257 for (i = 1; i < argc; i++) { 258 for (cp = argv[i]; *cp; cp++) { 259 /* Ignore superfluous '-', if there is one */ 260 if (*cp == '-') 261 continue; 262 263 howto = 0; 264 BOOT_FLAG(*cp, howto); 265 if (! howto) 266 printf("bootflag '%c' not recognised\n", *cp); 267 else 268 boothowto |= howto; 269 } 270 } 271 272 273 #if NKSYMS || defined(DDB) || defined(MODULAR) 274 /* init symbols if present */ 275 if (esym) 276 ksyms_addsyms_elf(esym - ssym, ssym, esym); 277 #endif 278 #ifdef DDB 279 if (boothowto & RB_KDB) 280 Debugger(); 281 #endif 282 #ifdef KGDB 283 zs_kgdb_init(); /* XXX */ 284 if (boothowto & RB_KDB) 285 kgdb_connect(0); 286 #endif 287 288 /* 289 * Check to see if a mini-root was loaded into memory. It resides 290 * at the start of the next page just after the end of BSS. 291 */ 292 if (boothowto & RB_MINIROOT) 293 kernend += round_page(mfs_initminiroot(kernend)); 294 295 /* 296 * Load the rest of the available pages into the VM system. 297 */ 298 first = round_page(MIPS_KSEG0_TO_PHYS(kernend)); 299 last = mem_clusters[0].start + mem_clusters[0].size; 300 uvm_page_physload(atop(first), atop(last), atop(first), atop(last), 301 VM_FREELIST_DEFAULT); 302 303 /* 304 * Initialize error message buffer (at end of core). 305 */ 306 mips_init_msgbuf(); 307 308 /* 309 * Initialize the virtual memory system. 310 */ 311 pmap_bootstrap(); 312 313 /* 314 * Allocate uarea page for lwp0 and set it. 315 */ 316 mips_init_lwp0_uarea(); 317 318 /* 319 * Set up interrupt handling and I/O addresses. 320 */ 321 pizazz_init(); 322 } 323 324 325 326 /* 327 * cpu_startup: allocate memory for variable-sized tables, 328 * initialize CPU, and do autoconfiguration. 329 */ 330 void 331 cpu_startup(void) 332 { 333 vaddr_t minaddr, maxaddr; 334 char pbuf[9]; 335 #ifdef DEBUG 336 extern int pmapdebug; 337 int opmapdebug = pmapdebug; 338 339 pmapdebug = 0; 340 #endif 341 342 /* 343 * Good {morning,afternoon,evening,night}. 344 */ 345 printf("%s%s", copyright, version); 346 printf("%s\n", cpu_getmodel()); 347 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 348 printf("total memory = %s\n", pbuf); 349 350 minaddr = 0; 351 /* 352 * Allocate a submap for physio 353 */ 354 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 355 VM_PHYS_SIZE, true, false, NULL); 356 357 /* 358 * No need to allocate an mbuf cluster submap. Mbuf clusters 359 * are allocated via the pool allocator, and we use KSEG to 360 * map those pages. 361 */ 362 363 #ifdef DEBUG 364 pmapdebug = opmapdebug; 365 #endif 366 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 367 printf("avail memory = %s\n", pbuf); 368 } 369 370 /* 371 * Look up information in bootinfo of boot loader. 372 */ 373 void * 374 lookup_bootinfo(int type) 375 { 376 struct btinfo_common *bt; 377 char *help = bootinfo; 378 379 /* Check for a bootinfo record first. */ 380 if (help == NULL) 381 return (NULL); 382 383 do { 384 bt = (struct btinfo_common *)help; 385 if (bt->type == type) 386 return ((void *)help); 387 help += bt->next; 388 } while (bt->next != 0 && 389 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE); 390 391 return (NULL); 392 } 393 394 int waittime = -1; 395 396 /* 397 * call PROM to halt or reboot. 398 */ 399 void 400 prom_halt(int howto) 401 { 402 if (howto & RB_HALT) 403 MIPS_PROM(reinit)(); 404 MIPS_PROM(reboot)(); 405 /* NOTREACHED */ 406 } 407 408 void 409 cpu_reboot(volatile int howto, char *bootstr) 410 { 411 /* take a snap shot before clobbering any registers */ 412 savectx(curpcb); 413 414 #ifdef DEBUG 415 if (panicstr) 416 stacktrace(); 417 #endif 418 419 /* If system is cold, just halt. */ 420 if (cold) { 421 howto |= RB_HALT; 422 goto haltsys; 423 } 424 425 /* If "always halt" was specified as a boot flag, obey. */ 426 if ((boothowto & RB_HALT) != 0) 427 howto |= RB_HALT; 428 429 boothowto = howto; 430 if ((howto & RB_NOSYNC) == 0 && waittime < 0) { 431 /* 432 * Synchronize the disks.... 433 */ 434 waittime = 0; 435 vfs_shutdown(); 436 437 /* 438 * If we've been adjusting the clock, the todr 439 * will be out of synch; adjust it now. 440 */ 441 resettodr(); 442 } 443 444 /* Disable interrupts. */ 445 splhigh(); 446 447 /* If rebooting and a dump is requested do it. */ 448 #if 0 449 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 450 #else 451 if (howto & RB_DUMP) 452 #endif 453 dumpsys(); 454 455 haltsys: 456 457 /* run any shutdown hooks */ 458 doshutdownhooks(); 459 460 pmf_system_shutdown(boothowto); 461 462 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 463 prom_halt(0x80); /* rom monitor RB_PWOFF */ 464 465 /* Finally, halt/reboot the system. */ 466 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting..."); 467 prom_halt(howto & RB_HALT); 468 /*NOTREACHED*/ 469 } 470 471 int 472 initcpu(void) 473 { 474 spl0(); /* safe to turn interrupts on now */ 475 return 0; 476 } 477 478 static void 479 unimpl_cons_init(void) 480 { 481 482 panic("sysconf.init didn't set cons_init"); 483 } 484 485 static void 486 unimpl_iointr(uint32_t status, vaddr_t pc, uint32_t ipending) 487 { 488 489 panic("sysconf.init didn't set intr"); 490 } 491 492 static int 493 unimpl_memsize(void *first) 494 { 495 496 panic("sysconf.init didn't set memsize"); 497 } 498 499 void 500 unimpl_intr_establish(int level, int (*func)(void *), void *arg) 501 { 502 panic("sysconf.init didn't init intr_establish"); 503 } 504 505 void 506 delay(int n) 507 { 508 DELAY(n); 509 } 510 511 /* 512 * Find out how much memory is available by testing memory. 513 * Be careful to save and restore the original contents for msgbuf. 514 */ 515 int 516 memsize_scan(void *first) 517 { 518 volatile int *vp, *vp0; 519 int mem, tmp, tmp0; 520 521 #define PATTERN1 0xa5a5a5a5 522 #define PATTERN2 ~PATTERN1 523 524 /* 525 * Non destructive scan of memory to determine the size 526 * Use the first page to test for memory aliases. This 527 * also has the side effect of flushing the bus alignment 528 * buffer 529 */ 530 mem = btoc((paddr_t)first - MIPS_KSEG0_START); 531 vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT); 532 vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */ 533 tmp0 = *vp0; 534 while (vp < (int *)MIPS_MAX_MEM_ADDR) { 535 tmp = *vp; 536 *vp = PATTERN1; 537 *vp0 = PATTERN2; 538 wbflush(); 539 if (*vp != PATTERN1) 540 break; 541 *vp = PATTERN2; 542 *vp0 = PATTERN1; 543 wbflush(); 544 if (*vp != PATTERN2) 545 break; 546 *vp = tmp; 547 vp += PAGE_SIZE/sizeof(int); 548 mem++; 549 } 550 *vp0 = tmp0; 551 return mem; 552 } 553 554 /* 555 * Console initialization: called early on from main, 556 * before vm init or startup. Do enough configuration 557 * to choose and initialize a console. 558 */ 559 560 static void 561 null_cnprobe(struct consdev *cn) 562 { 563 } 564 565 static void 566 prom_cninit(struct consdev *cn) 567 { 568 extern const struct cdevsw cons_cdevsw; 569 570 cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0); 571 cn->cn_pri = CN_REMOTE; 572 } 573 574 static int 575 prom_cngetc(dev_t dev) 576 { 577 return MIPS_PROM(getchar)(); 578 } 579 580 static void 581 prom_cnputc(dev_t dev, int c) 582 { 583 MIPS_PROM(putchar)(c); 584 } 585 586 static void 587 null_cnpollc(dev_t dev, int on) 588 { 589 } 590 591 void 592 consinit(void) 593 { 594 int zs_unit; 595 596 zs_unit = 0; 597 cn_tab = &consdev_zs; 598 599 (*cn_tab->cn_init)(cn_tab); 600 } 601