1 /* $NetBSD: machdep.c,v 1.80 2016/12/22 14:47:58 cherry 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.80 2016/12/22 14:47:58 cherry 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 uvm_md_init(); 215 216 /* Find out how much memory is available. */ 217 physmem = memsize_scan(kernend); 218 219 /* 220 * Now that we know how much memory we have, initialize the 221 * mem cluster array. 222 */ 223 mem_clusters[0].start = 0; /* XXX is this correct? */ 224 mem_clusters[0].size = ctob(physmem); 225 mem_cluster_cnt = 1; 226 227 /* Look at argv[0] and compute bootdev */ 228 makebootdev(argv[0]); 229 230 /* 231 * Look at arguments passed to us and compute boothowto. 232 */ 233 boothowto = RB_AUTOBOOT; 234 for (i = 1; i < argc; i++) { 235 for (cp = argv[i]; *cp; cp++) { 236 /* Ignore superfluous '-', if there is one */ 237 if (*cp == '-') 238 continue; 239 240 howto = 0; 241 BOOT_FLAG(*cp, howto); 242 if (! howto) 243 printf("bootflag '%c' not recognised\n", *cp); 244 else 245 boothowto |= howto; 246 } 247 } 248 249 250 #if NKSYMS || defined(DDB) || defined(MODULAR) 251 /* init symbols if present */ 252 if (esym) 253 ksyms_addsyms_elf(esym - ssym, ssym, esym); 254 #endif 255 #ifdef DDB 256 if (boothowto & RB_KDB) 257 Debugger(); 258 #endif 259 #ifdef KGDB 260 zs_kgdb_init(); /* XXX */ 261 if (boothowto & RB_KDB) 262 kgdb_connect(0); 263 #endif 264 265 /* 266 * Check to see if a mini-root was loaded into memory. It resides 267 * at the start of the next page just after the end of BSS. 268 */ 269 if (boothowto & RB_MINIROOT) 270 kernend += round_page(mfs_initminiroot(kernend)); 271 272 /* 273 * Load the rest of the available pages into the VM system. 274 */ 275 first = round_page(MIPS_KSEG0_TO_PHYS(kernend)); 276 last = mem_clusters[0].start + mem_clusters[0].size; 277 uvm_page_physload(atop(first), atop(last), atop(first), atop(last), 278 VM_FREELIST_DEFAULT); 279 280 /* 281 * Initialize error message buffer (at end of core). 282 */ 283 mips_init_msgbuf(); 284 285 /* 286 * Initialize the virtual memory system. 287 */ 288 pmap_bootstrap(); 289 290 /* 291 * Allocate uarea page for lwp0 and set it. 292 */ 293 mips_init_lwp0_uarea(); 294 295 /* 296 * Set up interrupt handling and I/O addresses. 297 */ 298 pizazz_init(); 299 } 300 301 302 303 /* 304 * cpu_startup: allocate memory for variable-sized tables, 305 * initialize CPU, and do autoconfiguration. 306 */ 307 void 308 cpu_startup(void) 309 { 310 cpu_startup_common(); 311 } 312 313 /* 314 * Look up information in bootinfo of boot loader. 315 */ 316 void * 317 lookup_bootinfo(int type) 318 { 319 struct btinfo_common *bt; 320 char *help = bootinfo; 321 322 /* Check for a bootinfo record first. */ 323 if (help == NULL) 324 return (NULL); 325 326 do { 327 bt = (struct btinfo_common *)help; 328 if (bt->type == type) 329 return ((void *)help); 330 help += bt->next; 331 } while (bt->next != 0 && 332 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE); 333 334 return (NULL); 335 } 336 337 int waittime = -1; 338 339 /* 340 * call PROM to halt or reboot. 341 */ 342 void 343 prom_halt(int howto) 344 { 345 if (howto & RB_HALT) 346 MIPS_PROM(reinit)(); 347 MIPS_PROM(reboot)(); 348 /* NOTREACHED */ 349 } 350 351 void 352 cpu_reboot(volatile int howto, char *bootstr) 353 { 354 /* take a snap shot before clobbering any registers */ 355 savectx(curpcb); 356 357 #ifdef DEBUG 358 if (panicstr) 359 stacktrace(); 360 #endif 361 362 /* If system is cold, just halt. */ 363 if (cold) { 364 howto |= RB_HALT; 365 goto haltsys; 366 } 367 368 /* If "always halt" was specified as a boot flag, obey. */ 369 if ((boothowto & RB_HALT) != 0) 370 howto |= RB_HALT; 371 372 boothowto = howto; 373 if ((howto & RB_NOSYNC) == 0 && waittime < 0) { 374 /* 375 * Synchronize the disks.... 376 */ 377 waittime = 0; 378 vfs_shutdown(); 379 380 /* 381 * If we've been adjusting the clock, the todr 382 * will be out of synch; adjust it now. 383 */ 384 resettodr(); 385 } 386 387 /* Disable interrupts. */ 388 splhigh(); 389 390 /* If rebooting and a dump is requested do it. */ 391 #if 0 392 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 393 #else 394 if (howto & RB_DUMP) 395 #endif 396 dumpsys(); 397 398 haltsys: 399 400 /* run any shutdown hooks */ 401 doshutdownhooks(); 402 403 pmf_system_shutdown(boothowto); 404 405 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 406 prom_halt(0x80); /* rom monitor RB_PWOFF */ 407 408 /* Finally, halt/reboot the system. */ 409 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting..."); 410 prom_halt(howto & RB_HALT); 411 /*NOTREACHED*/ 412 } 413 414 int 415 initcpu(void) 416 { 417 spl0(); /* safe to turn interrupts on now */ 418 return 0; 419 } 420 421 static void 422 unimpl_cons_init(void) 423 { 424 425 panic("sysconf.init didn't set cons_init"); 426 } 427 428 static void 429 unimpl_iointr(uint32_t status, vaddr_t pc, uint32_t ipending) 430 { 431 432 panic("sysconf.init didn't set intr"); 433 } 434 435 static int 436 unimpl_memsize(void *first) 437 { 438 439 panic("sysconf.init didn't set memsize"); 440 } 441 442 void 443 unimpl_intr_establish(int level, int (*func)(void *), void *arg) 444 { 445 panic("sysconf.init didn't init intr_establish"); 446 } 447 448 void 449 delay(int n) 450 { 451 DELAY(n); 452 } 453 454 /* 455 * Find out how much memory is available by testing memory. 456 * Be careful to save and restore the original contents for msgbuf. 457 */ 458 int 459 memsize_scan(void *first) 460 { 461 volatile int *vp, *vp0; 462 int mem, tmp, tmp0; 463 464 #define PATTERN1 0xa5a5a5a5 465 #define PATTERN2 ~PATTERN1 466 467 /* 468 * Non destructive scan of memory to determine the size 469 * Use the first page to test for memory aliases. This 470 * also has the side effect of flushing the bus alignment 471 * buffer 472 */ 473 mem = btoc((paddr_t)first - MIPS_KSEG0_START); 474 vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT); 475 vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */ 476 tmp0 = *vp0; 477 while (vp < (int *)MIPS_MAX_MEM_ADDR) { 478 tmp = *vp; 479 *vp = PATTERN1; 480 *vp0 = PATTERN2; 481 wbflush(); 482 if (*vp != PATTERN1) 483 break; 484 *vp = PATTERN2; 485 *vp0 = PATTERN1; 486 wbflush(); 487 if (*vp != PATTERN2) 488 break; 489 *vp = tmp; 490 vp += PAGE_SIZE/sizeof(int); 491 mem++; 492 } 493 *vp0 = tmp0; 494 return mem; 495 } 496 497 /* 498 * Console initialization: called early on from main, 499 * before vm init or startup. Do enough configuration 500 * to choose and initialize a console. 501 */ 502 503 static void 504 null_cnprobe(struct consdev *cn) 505 { 506 } 507 508 static void 509 prom_cninit(struct consdev *cn) 510 { 511 extern const struct cdevsw cons_cdevsw; 512 513 cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0); 514 cn->cn_pri = CN_REMOTE; 515 } 516 517 static int 518 prom_cngetc(dev_t dev) 519 { 520 return MIPS_PROM(getchar)(); 521 } 522 523 static void 524 prom_cnputc(dev_t dev, int c) 525 { 526 MIPS_PROM(putchar)(c); 527 } 528 529 static void 530 null_cnpollc(dev_t dev, int on) 531 { 532 } 533 534 void 535 consinit(void) 536 { 537 538 cn_tab = &consdev_zs; 539 540 (*cn_tab->cn_init)(cn_tab); 541 } 542