1 /* $NetBSD: machdep.c,v 1.79 2015/06/26 22:55:40 matt 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.79 2015/06/26 22:55:40 matt 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 #include "opt_execfmt.h" 50 51 #include "zsc.h" /* XXX */ 52 #include "com.h" /* XXX */ 53 #include "ksyms.h" 54 55 #include <sys/param.h> 56 #include <sys/boot_flag.h> 57 #include <sys/conf.h> 58 #include <sys/cpu.h> 59 #include <sys/device.h> 60 #include <sys/intr.h> 61 #include <sys/kcore.h> 62 #include <sys/ksyms.h> 63 #include <sys/kernel.h> 64 #include <sys/reboot.h> 65 #include <sys/systm.h> 66 67 #include <uvm/uvm_extern.h> 68 69 #include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */ 70 71 #include <dev/clock_subr.h> 72 #include <dev/cons.h> 73 74 #ifdef DDB 75 #include <machine/db_machdep.h> 76 #include <ddb/db_extern.h> 77 #endif 78 79 #include <machine/mainboard.h> 80 #include <machine/sysconf.h> 81 #include <machine/autoconf.h> 82 #include <machine/bootinfo.h> 83 #include <machine/prom.h> 84 85 /* maps for VM objects */ 86 87 struct vm_map *phys_map = NULL; 88 89 char *bootinfo = NULL; /* pointer to bootinfo structure */ 90 91 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; 92 int mem_cluster_cnt; 93 94 void to_monitor(int) __dead; 95 void prom_halt(int) __dead; 96 97 #ifdef KGDB 98 void zs_kgdb_init(void); 99 void kgdb_connect(int); 100 #endif 101 102 /* 103 * Local functions. 104 */ 105 int initcpu(void); 106 void configure(void); 107 108 void mach_init(int, char *[], char*[], u_int, char *); 109 int memsize_scan(void *); 110 111 #ifdef DEBUG 112 /* stacktrace code violates prototypes to get callee's registers */ 113 extern void stacktrace(void); /*XXX*/ 114 #endif 115 116 /* locore callback-vector setup */ 117 extern void prom_init(void); 118 extern void pizazz_init(void); 119 120 /* platform-specific initialization vector */ 121 static void unimpl_cons_init(void); 122 static void unimpl_iointr(uint32_t, vaddr_t, uint32_t); 123 static int unimpl_memsize(void *); 124 static void unimpl_intr_establish(int, int (*)(void *), void *); 125 126 struct platform platform = { 127 .iobus = "iobus not set", 128 .cons_init = unimpl_cons_init, 129 .iointr = unimpl_iointr, 130 .memsize = unimpl_memsize, 131 .intr_establish = unimpl_intr_establish, 132 .clkinit = NULL, 133 }; 134 135 extern struct consdev consdev_prom; 136 extern struct consdev consdev_zs; 137 138 static void null_cnprobe(struct consdev *); 139 static void prom_cninit(struct consdev *); 140 static int prom_cngetc(dev_t); 141 static void prom_cnputc(dev_t, int); 142 static void null_cnpollc(dev_t, int); 143 144 struct consdev consdev_prom = { 145 null_cnprobe, 146 prom_cninit, 147 prom_cngetc, 148 prom_cnputc, 149 null_cnpollc, 150 }; 151 152 153 /* 154 * Do all the stuff that locore normally does before calling main(). 155 * Process arguments passed to us by the prom monitor. 156 * Return the first page address following the system. 157 */ 158 void 159 mach_init(int argc, char *argv[], char *envp[], u_int bim, char *bip) 160 { 161 u_long first, last; 162 char *kernend; 163 char *cp; 164 int i, howto; 165 extern char edata[], end[]; 166 const char *bi_msg; 167 #if NKSYMS || defined(DDB) || defined(MODULAR) 168 char *ssym = 0; 169 char *esym = 0; 170 struct btinfo_symtab *bi_syms; 171 #endif 172 173 /* Check for valid bootinfo passed from bootstrap */ 174 if (bim == BOOTINFO_MAGIC) { 175 struct btinfo_magic *bi_magic; 176 177 bootinfo = (char *)BOOTINFO_ADDR; /* XXX */ 178 bi_magic = lookup_bootinfo(BTINFO_MAGIC); 179 if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC) 180 bi_msg = "invalid bootinfo structure.\n"; 181 else 182 bi_msg = NULL; 183 } else 184 bi_msg = "invalid bootinfo (standalone boot?)\n"; 185 186 /* clear the BSS segment */ 187 kernend = (void *)mips_round_page(end); 188 memset(edata, 0, end - edata); 189 190 /* 191 * Copy exception-dispatch code down to exception vector. 192 * Initialize locore-function vector. 193 * Clear out the I and D caches. 194 */ 195 mips_vector_init(NULL, false); 196 197 #if NKSYMS || defined(DDB) || defined(LKM) 198 bi_syms = lookup_bootinfo(BTINFO_SYMTAB); 199 200 /* Load sysmbol table if present */ 201 if (bi_syms != NULL) { 202 ssym = (void *)bi_syms->ssym; 203 esym = (void *)bi_syms->esym; 204 kernend = (void *)mips_round_page(esym); 205 } 206 #endif 207 208 prom_init(); 209 consinit(); 210 211 if (bi_msg != NULL) 212 printf(bi_msg); 213 214 /* 215 * Set the VM page size. 216 */ 217 uvm_setpagesize(); 218 219 /* Find out how much memory is available. */ 220 physmem = memsize_scan(kernend); 221 222 /* 223 * Now that we know how much memory we have, initialize the 224 * mem cluster array. 225 */ 226 mem_clusters[0].start = 0; /* XXX is this correct? */ 227 mem_clusters[0].size = ctob(physmem); 228 mem_cluster_cnt = 1; 229 230 /* Look at argv[0] and compute bootdev */ 231 makebootdev(argv[0]); 232 233 /* 234 * Look at arguments passed to us and compute boothowto. 235 */ 236 boothowto = RB_AUTOBOOT; 237 for (i = 1; i < argc; i++) { 238 for (cp = argv[i]; *cp; cp++) { 239 /* Ignore superfluous '-', if there is one */ 240 if (*cp == '-') 241 continue; 242 243 howto = 0; 244 BOOT_FLAG(*cp, howto); 245 if (! howto) 246 printf("bootflag '%c' not recognised\n", *cp); 247 else 248 boothowto |= howto; 249 } 250 } 251 252 253 #if NKSYMS || defined(DDB) || defined(MODULAR) 254 /* init symbols if present */ 255 if (esym) 256 ksyms_addsyms_elf(esym - ssym, ssym, esym); 257 #endif 258 #ifdef DDB 259 if (boothowto & RB_KDB) 260 Debugger(); 261 #endif 262 #ifdef KGDB 263 zs_kgdb_init(); /* XXX */ 264 if (boothowto & RB_KDB) 265 kgdb_connect(0); 266 #endif 267 268 /* 269 * Check to see if a mini-root was loaded into memory. It resides 270 * at the start of the next page just after the end of BSS. 271 */ 272 if (boothowto & RB_MINIROOT) 273 kernend += round_page(mfs_initminiroot(kernend)); 274 275 /* 276 * Load the rest of the available pages into the VM system. 277 */ 278 first = round_page(MIPS_KSEG0_TO_PHYS(kernend)); 279 last = mem_clusters[0].start + mem_clusters[0].size; 280 uvm_page_physload(atop(first), atop(last), atop(first), atop(last), 281 VM_FREELIST_DEFAULT); 282 283 /* 284 * Initialize error message buffer (at end of core). 285 */ 286 mips_init_msgbuf(); 287 288 /* 289 * Initialize the virtual memory system. 290 */ 291 pmap_bootstrap(); 292 293 /* 294 * Allocate uarea page for lwp0 and set it. 295 */ 296 mips_init_lwp0_uarea(); 297 298 /* 299 * Set up interrupt handling and I/O addresses. 300 */ 301 pizazz_init(); 302 } 303 304 305 306 /* 307 * cpu_startup: allocate memory for variable-sized tables, 308 * initialize CPU, and do autoconfiguration. 309 */ 310 void 311 cpu_startup(void) 312 { 313 cpu_startup_common(); 314 } 315 316 /* 317 * Look up information in bootinfo of boot loader. 318 */ 319 void * 320 lookup_bootinfo(int type) 321 { 322 struct btinfo_common *bt; 323 char *help = bootinfo; 324 325 /* Check for a bootinfo record first. */ 326 if (help == NULL) 327 return (NULL); 328 329 do { 330 bt = (struct btinfo_common *)help; 331 if (bt->type == type) 332 return ((void *)help); 333 help += bt->next; 334 } while (bt->next != 0 && 335 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE); 336 337 return (NULL); 338 } 339 340 int waittime = -1; 341 342 /* 343 * call PROM to halt or reboot. 344 */ 345 void 346 prom_halt(int howto) 347 { 348 if (howto & RB_HALT) 349 MIPS_PROM(reinit)(); 350 MIPS_PROM(reboot)(); 351 /* NOTREACHED */ 352 } 353 354 void 355 cpu_reboot(volatile int howto, char *bootstr) 356 { 357 /* take a snap shot before clobbering any registers */ 358 savectx(curpcb); 359 360 #ifdef DEBUG 361 if (panicstr) 362 stacktrace(); 363 #endif 364 365 /* If system is cold, just halt. */ 366 if (cold) { 367 howto |= RB_HALT; 368 goto haltsys; 369 } 370 371 /* If "always halt" was specified as a boot flag, obey. */ 372 if ((boothowto & RB_HALT) != 0) 373 howto |= RB_HALT; 374 375 boothowto = howto; 376 if ((howto & RB_NOSYNC) == 0 && waittime < 0) { 377 /* 378 * Synchronize the disks.... 379 */ 380 waittime = 0; 381 vfs_shutdown(); 382 383 /* 384 * If we've been adjusting the clock, the todr 385 * will be out of synch; adjust it now. 386 */ 387 resettodr(); 388 } 389 390 /* Disable interrupts. */ 391 splhigh(); 392 393 /* If rebooting and a dump is requested do it. */ 394 #if 0 395 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 396 #else 397 if (howto & RB_DUMP) 398 #endif 399 dumpsys(); 400 401 haltsys: 402 403 /* run any shutdown hooks */ 404 doshutdownhooks(); 405 406 pmf_system_shutdown(boothowto); 407 408 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 409 prom_halt(0x80); /* rom monitor RB_PWOFF */ 410 411 /* Finally, halt/reboot the system. */ 412 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting..."); 413 prom_halt(howto & RB_HALT); 414 /*NOTREACHED*/ 415 } 416 417 int 418 initcpu(void) 419 { 420 spl0(); /* safe to turn interrupts on now */ 421 return 0; 422 } 423 424 static void 425 unimpl_cons_init(void) 426 { 427 428 panic("sysconf.init didn't set cons_init"); 429 } 430 431 static void 432 unimpl_iointr(uint32_t status, vaddr_t pc, uint32_t ipending) 433 { 434 435 panic("sysconf.init didn't set intr"); 436 } 437 438 static int 439 unimpl_memsize(void *first) 440 { 441 442 panic("sysconf.init didn't set memsize"); 443 } 444 445 void 446 unimpl_intr_establish(int level, int (*func)(void *), void *arg) 447 { 448 panic("sysconf.init didn't init intr_establish"); 449 } 450 451 void 452 delay(int n) 453 { 454 DELAY(n); 455 } 456 457 /* 458 * Find out how much memory is available by testing memory. 459 * Be careful to save and restore the original contents for msgbuf. 460 */ 461 int 462 memsize_scan(void *first) 463 { 464 volatile int *vp, *vp0; 465 int mem, tmp, tmp0; 466 467 #define PATTERN1 0xa5a5a5a5 468 #define PATTERN2 ~PATTERN1 469 470 /* 471 * Non destructive scan of memory to determine the size 472 * Use the first page to test for memory aliases. This 473 * also has the side effect of flushing the bus alignment 474 * buffer 475 */ 476 mem = btoc((paddr_t)first - MIPS_KSEG0_START); 477 vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT); 478 vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */ 479 tmp0 = *vp0; 480 while (vp < (int *)MIPS_MAX_MEM_ADDR) { 481 tmp = *vp; 482 *vp = PATTERN1; 483 *vp0 = PATTERN2; 484 wbflush(); 485 if (*vp != PATTERN1) 486 break; 487 *vp = PATTERN2; 488 *vp0 = PATTERN1; 489 wbflush(); 490 if (*vp != PATTERN2) 491 break; 492 *vp = tmp; 493 vp += PAGE_SIZE/sizeof(int); 494 mem++; 495 } 496 *vp0 = tmp0; 497 return mem; 498 } 499 500 /* 501 * Console initialization: called early on from main, 502 * before vm init or startup. Do enough configuration 503 * to choose and initialize a console. 504 */ 505 506 static void 507 null_cnprobe(struct consdev *cn) 508 { 509 } 510 511 static void 512 prom_cninit(struct consdev *cn) 513 { 514 extern const struct cdevsw cons_cdevsw; 515 516 cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0); 517 cn->cn_pri = CN_REMOTE; 518 } 519 520 static int 521 prom_cngetc(dev_t dev) 522 { 523 return MIPS_PROM(getchar)(); 524 } 525 526 static void 527 prom_cnputc(dev_t dev, int c) 528 { 529 MIPS_PROM(putchar)(c); 530 } 531 532 static void 533 null_cnpollc(dev_t dev, int on) 534 { 535 } 536 537 void 538 consinit(void) 539 { 540 541 cn_tab = &consdev_zs; 542 543 (*cn_tab->cn_init)(cn_tab); 544 } 545