1 /* $NetBSD: machdep.c,v 1.64 2002/09/25 22:21:15 thorpej 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. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)machdep.c 8.3 (Berkeley) 1/12/94 42 */ 43 44 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 45 46 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.64 2002/09/25 22:21:15 thorpej Exp $"); 47 48 /* from: Utah Hdr: machdep.c 1.63 91/04/24 */ 49 50 #include "fs_mfs.h" 51 #include "opt_ddb.h" 52 #include "opt_execfmt.h" 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/signalvar.h> 57 #include <sys/kernel.h> 58 #include <sys/proc.h> 59 #include <sys/buf.h> 60 #include <sys/reboot.h> 61 #include <sys/conf.h> 62 #include <sys/file.h> 63 #include <sys/malloc.h> 64 #include <sys/mbuf.h> 65 #include <sys/msgbuf.h> 66 #include <sys/ioctl.h> 67 #include <sys/device.h> 68 #include <sys/user.h> 69 #include <sys/exec.h> 70 #include <sys/mount.h> 71 #include <sys/syscallargs.h> 72 #include <sys/kcore.h> 73 74 #include <uvm/uvm_extern.h> 75 76 #include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */ 77 78 #include <machine/cpu.h> 79 #include <machine/reg.h> 80 #include <machine/psl.h> 81 #include <machine/pte.h> 82 #include <machine/autoconf.h> 83 #include <machine/bootinfo.h> 84 #include <machine/apbus.h> 85 #include <machine/apcall.h> 86 87 #include <mips/cache.h> 88 #include <mips/locore.h> 89 90 #define _NEWSMIPS_BUS_DMA_PRIVATE 91 #include <machine/bus.h> 92 93 #ifdef DDB 94 #include <machine/db_machdep.h> 95 #include <ddb/db_access.h> 96 #include <ddb/db_extern.h> 97 #include <ddb/db_sym.h> 98 #endif 99 100 #include <machine/adrsmap.h> 101 #include <machine/machConst.h> 102 #include <machine/intr.h> 103 #include <newsmips/newsmips/clockreg.h> 104 #include <newsmips/newsmips/machid.h> 105 #include <dev/cons.h> 106 107 /* the following is used externally (sysctl_hw) */ 108 extern char cpu_model[]; 109 110 /* Our exported CPU info; we can have only one. */ 111 struct cpu_info cpu_info_store; 112 113 /* maps for VM objects */ 114 115 struct vm_map *exec_map = NULL; 116 struct vm_map *mb_map = NULL; 117 struct vm_map *phys_map = NULL; 118 119 char *bootinfo = NULL; /* pointer to bootinfo structure */ 120 int physmem; /* max supported memory, changes to actual */ 121 int systype; /* what type of NEWS we are */ 122 struct apbus_sysinfo *_sip = NULL; 123 124 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; 125 int mem_cluster_cnt; 126 127 struct idrom idrom; 128 void (*enable_intr) __P((void)); 129 void (*disable_intr) __P((void)); 130 void (*readmicrotime) __P((struct timeval *tvp)); 131 132 /* System type dependent initializations. */ 133 extern void news3400_init __P((void)); 134 extern void news5000_init __P((void)); 135 136 static void (*hardware_intr) __P((u_int, u_int, u_int, u_int)); 137 u_int ssir; 138 139 /* 140 * Local functions. 141 */ 142 143 /* initialize bss, etc. from kernel start, before main() is called. */ 144 void mach_init __P((int, int, int, int)); 145 146 void prom_halt __P((int)) __attribute__((__noreturn__)); 147 void to_monitor __P((int)) __attribute__((__noreturn__)); 148 149 #ifdef DEBUG 150 /* stacktrace code violates prototypes to get callee's registers */ 151 extern void stacktrace __P((void)); /*XXX*/ 152 #endif 153 154 /* 155 * safepri is a safe priority for sleep to set for a spin-wait 156 * during autoconfiguration or after a panic. Used as an argument to splx(). 157 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also 158 * disables mips1 FPU interrupts. 159 */ 160 int safepri = MIPS3_PSL_LOWIPL; /* XXX */ 161 162 extern struct user *proc0paddr; 163 extern u_long bootdev; 164 extern char edata[], end[]; 165 166 /* 167 * Do all the stuff that locore normally does before calling main(). 168 * Process arguments passed to us by the prom monitor. 169 * Return the first page address following the system. 170 */ 171 void 172 mach_init(x_boothowto, x_bootdev, x_bootname, x_maxmem) 173 int x_boothowto; 174 int x_bootdev; 175 int x_bootname; 176 int x_maxmem; 177 { 178 u_long first, last; 179 caddr_t kernend, v; 180 vsize_t size; 181 struct btinfo_magic *bi_magic; 182 struct btinfo_bootarg *bi_arg; 183 struct btinfo_systype *bi_systype; 184 #ifdef DDB 185 struct btinfo_symtab *bi_sym; 186 int nsym = 0; 187 char *ssym, *esym; 188 #endif 189 190 /* clear the BSS segment */ 191 bzero(edata, end - edata); 192 193 systype = NEWS3400; /* XXX compatibility */ 194 195 bootinfo = (void *)BOOTINFO_ADDR; /* XXX */ 196 bi_magic = lookup_bootinfo(BTINFO_MAGIC); 197 if (bi_magic && bi_magic->magic == BOOTINFO_MAGIC) { 198 bi_arg = lookup_bootinfo(BTINFO_BOOTARG); 199 if (bi_arg) { 200 x_boothowto = bi_arg->howto; 201 x_bootdev = bi_arg->bootdev; 202 x_maxmem = bi_arg->maxmem; 203 } 204 #ifdef DDB 205 bi_sym = lookup_bootinfo(BTINFO_SYMTAB); 206 if (bi_sym) { 207 nsym = bi_sym->nsym; 208 ssym = (void *)bi_sym->ssym; 209 esym = (void *)bi_sym->esym; 210 } 211 #endif 212 213 bi_systype = lookup_bootinfo(BTINFO_SYSTYPE); 214 if (bi_systype) 215 systype = bi_systype->type; 216 } 217 218 #ifdef news5000 219 if (systype == NEWS5000) { 220 int i; 221 char *bootspec = (char *)x_bootdev; 222 223 _sip = (void *)bi_arg->sip; 224 x_maxmem = _sip->apbsi_memsize; 225 x_maxmem -= 0x00100000; /* reserve 1MB for ROM monitor */ 226 if (strncmp(bootspec, "scsi", 4) == 0) { 227 x_bootdev = (5 << 28) | 0; /* magic, sd */ 228 bootspec += 4; 229 if (*bootspec != '(' /*)*/) 230 goto bootspec_end; 231 i = strtoul(bootspec + 1, &bootspec, 10); 232 x_bootdev |= (i << 24); /* bus */ 233 if (*bootspec != ',') 234 goto bootspec_end; 235 i = strtoul(bootspec + 1, &bootspec, 10); 236 x_bootdev |= (i / 10) << 20; /* controller */ 237 x_bootdev |= (i % 10) << 16; /* unit */ 238 if (*bootspec != ',') 239 goto bootspec_end; 240 i = strtoul(bootspec + 1, &bootspec, 10); 241 x_bootdev |= (i << 8); /* partition */ 242 } 243 bootspec_end: 244 consinit(); 245 } 246 #endif 247 248 /* 249 * Save parameters into kernel work area. 250 */ 251 *(int *)(MIPS_PHYS_TO_KSEG1(MACH_MAXMEMSIZE_ADDR)) = x_maxmem; 252 *(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTDEV_ADDR)) = x_bootdev; 253 *(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTSW_ADDR)) = x_boothowto; 254 255 kernend = (caddr_t)mips_round_page(end); 256 #ifdef DDB 257 if (nsym) 258 kernend = (caddr_t)mips_round_page(esym); 259 #endif 260 261 /* 262 * Set the VM page size. 263 */ 264 uvm_setpagesize(); 265 266 boothowto = x_boothowto; 267 bootdev = x_bootdev; 268 physmem = btoc(x_maxmem); 269 270 /* 271 * Now that we know how much memory we have, initialize the 272 * mem cluster array. 273 */ 274 mem_clusters[0].start = 0; /* XXX is this correct? */ 275 mem_clusters[0].size = ctob(physmem); 276 mem_cluster_cnt = 1; 277 278 /* 279 * Copy exception-dispatch code down to exception vector. 280 * Initialize locore-function vector. 281 * Clear out the I and D caches. 282 */ 283 mips_vector_init(); 284 285 /* 286 * We know the CPU type now. Initialize our DMA tags (might 287 * need this early). 288 */ 289 newsmips_bus_dma_init(); 290 291 #if 0 292 if (systype == NEWS5000) { 293 mips_L2CacheSize = 1024 * 1024; /* XXX to be safe */ 294 mips3_FlushCache(); 295 } 296 #endif 297 298 #ifdef DDB 299 if (nsym) 300 ddb_init(esym - ssym, ssym, esym); 301 #endif 302 303 #ifdef KADB 304 boothowto |= RB_KDB; 305 #endif 306 307 #ifdef MFS 308 /* 309 * Check to see if a mini-root was loaded into memory. It resides 310 * at the start of the next page just after the end of BSS. 311 */ 312 if (boothowto & RB_MINIROOT) 313 kernend += round_page(mfs_initminiroot(kernend)); 314 #endif 315 316 /* 317 * Load the rest of the available pages into the VM system. 318 */ 319 first = round_page(MIPS_KSEG0_TO_PHYS(kernend)); 320 last = mem_clusters[0].start + mem_clusters[0].size; 321 uvm_page_physload(atop(first), atop(last), atop(first), atop(last), 322 VM_FREELIST_DEFAULT); 323 324 /* 325 * Initialize error message buffer (at end of core). 326 */ 327 mips_init_msgbuf(); 328 329 /* 330 * Compute the size of system data structures. pmap_bootstrap() 331 * needs some of this information. 332 */ 333 size = (vsize_t)allocsys(NULL, NULL); 334 335 /* 336 * Initialize the virtual memory system. 337 */ 338 pmap_bootstrap(); 339 340 /* 341 * Allocate space for proc0's USPACE. 342 */ 343 v = (caddr_t)uvm_pageboot_alloc(USPACE); 344 proc0.p_addr = proc0paddr = (struct user *)v; 345 proc0.p_md.md_regs = (struct frame *)(v + USPACE) - 1; 346 curpcb = &proc0.p_addr->u_pcb; 347 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */ 348 349 /* 350 * Allocate space for system data structures. These data structures 351 * are allocated here instead of cpu_startup() because physical 352 * memory is directly addressable. We don't have to map these into 353 * virtual address space. 354 */ 355 v = (caddr_t)uvm_pageboot_alloc(size); 356 if ((allocsys(v, NULL) - v) != size) 357 panic("mach_init: table size inconsistency"); 358 359 /* 360 * Determine what model of computer we are running on. 361 */ 362 switch (systype) { 363 #ifdef news3400 364 case NEWS3400: 365 news3400_init(); 366 strcpy(cpu_model, idrom.id_machine); 367 if (strcmp(cpu_model, "news3400") == 0 || 368 strcmp(cpu_model, "news3200") == 0 || 369 strcmp(cpu_model, "news3700") == 0) { 370 /* 371 * Set up interrupt handling and I/O addresses. 372 */ 373 hardware_intr = news3400_intr; 374 cpuspeed = 10; 375 } else { 376 printf("kernel not configured for machine %s\n", 377 cpu_model); 378 } 379 break; 380 #endif 381 382 #ifdef news5000 383 case NEWS5000: 384 news5000_init(); 385 strcpy(cpu_model, idrom.id_machine); 386 if (strcmp(cpu_model, "news5000") == 0 || 387 strcmp(cpu_model, "news5900") == 0) { 388 /* 389 * Set up interrupt handling and I/O addresses. 390 */ 391 hardware_intr = news5000_intr; 392 cpuspeed = 50; /* ??? XXX */ 393 } else { 394 printf("kernel not configured for machine %s\n", 395 cpu_model); 396 } 397 break; 398 #endif 399 400 default: 401 printf("kernel not configured for systype %d\n", systype); 402 break; 403 } 404 } 405 406 void 407 mips_machdep_cache_config(void) 408 { 409 /* All r4k news boxen have a 1MB L2 cache. */ 410 if (CPUISMIPS3) 411 mips_sdcache_size = 1024 * 1024; 412 } 413 414 /* 415 * cpu_startup: allocate memory for variable-sized tables, 416 * initialize cpu, and do autoconfiguration. 417 */ 418 void 419 cpu_startup() 420 { 421 u_int i, base, residual; 422 vaddr_t minaddr, maxaddr; 423 vsize_t size; 424 char pbuf[9]; 425 #ifdef DEBUG 426 extern int pmapdebug; 427 int opmapdebug = pmapdebug; 428 429 pmapdebug = 0; 430 #endif 431 432 /* 433 * Good {morning,afternoon,evening,night}. 434 */ 435 printf(version); 436 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 437 printf("total memory = %s\n", pbuf); 438 439 /* 440 * Allocate virtual address space for file I/O buffers. 441 * Note they are different than the array of headers, 'buf', 442 * and usually occupy more virtual memory than physical. 443 */ 444 size = MAXBSIZE * nbuf; 445 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size), 446 NULL, UVM_UNKNOWN_OFFSET, 0, 447 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE, 448 UVM_ADV_NORMAL, 0)) != 0) 449 panic("startup: cannot allocate VM for buffers"); 450 minaddr = (vaddr_t)buffers; 451 base = bufpages / nbuf; 452 residual = bufpages % nbuf; 453 for (i = 0; i < nbuf; i++) { 454 vsize_t curbufsize; 455 vaddr_t curbuf; 456 struct vm_page *pg; 457 458 /* 459 * Each buffer has MAXBSIZE bytes of VM space allocated. Of 460 * that MAXBSIZE space, we allocate and map (base+1) pages 461 * for the first "residual" buffers, and then we allocate 462 * "base" pages for the rest. 463 */ 464 curbuf = (vaddr_t) buffers + (i * MAXBSIZE); 465 curbufsize = NBPG * ((i < residual) ? (base+1) : base); 466 467 while (curbufsize) { 468 pg = uvm_pagealloc(NULL, 0, NULL, 0); 469 if (pg == NULL) 470 panic("cpu_startup: not enough memory for " 471 "buffer cache"); 472 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg), 473 VM_PROT_READ|VM_PROT_WRITE); 474 curbuf += PAGE_SIZE; 475 curbufsize -= PAGE_SIZE; 476 } 477 } 478 pmap_update(pmap_kernel()); 479 480 /* 481 * Allocate a submap for exec arguments. This map effectively 482 * limits the number of processes exec'ing at any time. 483 */ 484 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 485 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL); 486 /* 487 * Allocate a submap for physio 488 */ 489 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 490 VM_PHYS_SIZE, 0, FALSE, NULL); 491 492 /* 493 * No need to allocate an mbuf cluster submap. Mbuf clusters 494 * are allocated via the pool allocator, and we use KSEG to 495 * map those pages. 496 */ 497 498 #ifdef DEBUG 499 pmapdebug = opmapdebug; 500 #endif 501 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 502 printf("avail memory = %s\n", pbuf); 503 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG); 504 printf("using %u buffers containing %s of memory\n", nbuf, pbuf); 505 506 /* 507 * Set up buffers, so they can be used to read disk labels. 508 */ 509 bufinit(); 510 } 511 512 /* 513 * lookup_bootinfo: 514 * Look up information in bootinfo of boot loader. 515 */ 516 void * 517 lookup_bootinfo(type) 518 int type; 519 { 520 struct btinfo_common *bt; 521 char *help = bootinfo; 522 523 /* Check for a bootinfo record first. */ 524 if (help == NULL) 525 return (NULL); 526 527 do { 528 bt = (struct btinfo_common *)help; 529 if (bt->type == type) 530 return ((void *)help); 531 help += bt->next; 532 } while (bt->next != 0 && 533 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE); 534 535 return (NULL); 536 } 537 538 /* 539 * call PROM to halt or reboot. 540 */ 541 void 542 prom_halt(howto) 543 int howto; 544 545 { 546 #ifdef news5000 547 if (systype == NEWS5000) 548 apcall_exit(howto); 549 #endif 550 #ifdef news3400 551 if (systype == NEWS3400) 552 to_monitor(howto); 553 #endif 554 for (;;); 555 } 556 557 int waittime = -1; 558 559 void 560 cpu_reboot(howto, bootstr) 561 volatile int howto; 562 char *bootstr; 563 { 564 565 /* take a snap shot before clobbering any registers */ 566 if (curproc) 567 savectx((struct user *)curpcb); 568 569 #ifdef DEBUG 570 if (panicstr) 571 stacktrace(); 572 #endif 573 574 /* If system is cold, just halt. */ 575 if (cold) { 576 howto |= RB_HALT; 577 goto haltsys; 578 } 579 580 /* If "always halt" was specified as a boot flag, obey. */ 581 if ((boothowto & RB_HALT) != 0) 582 howto |= RB_HALT; 583 584 boothowto = howto; 585 if ((howto & RB_NOSYNC) == 0 && waittime < 0) { 586 /* 587 * Synchronize the disks.... 588 */ 589 waittime = 0; 590 vfs_shutdown(); 591 592 /* 593 * If we've been adjusting the clock, the todr 594 * will be out of synch; adjust it now. 595 */ 596 resettodr(); 597 } 598 599 /* Disable interrupts. */ 600 disable_intr(); 601 602 splhigh(); 603 604 /* If rebooting and a dump is requested do it. */ 605 #if 0 606 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 607 #else 608 if (howto & RB_DUMP) 609 #endif 610 dumpsys(); 611 612 haltsys: 613 614 /* run any shutdown hooks */ 615 doshutdownhooks(); 616 617 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 618 prom_halt(0x80); /* rom monitor RB_PWOFF */ 619 620 /* Finally, halt/reboot the system. */ 621 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting..."); 622 prom_halt(howto & RB_HALT); 623 /*NOTREACHED*/ 624 } 625 626 /* 627 * Return the best possible estimate of the time in the timeval 628 * to which tvp points. Unfortunately, we can't read the hardware registers. 629 * We guarantee that the time will be greater than the value obtained by a 630 * previous call. 631 */ 632 void 633 microtime(tvp) 634 register struct timeval *tvp; 635 { 636 int s = splclock(); 637 static struct timeval lasttime; 638 639 if (readmicrotime) 640 readmicrotime(tvp); 641 else 642 *tvp = time; 643 644 if (tvp->tv_sec == lasttime.tv_sec && 645 tvp->tv_usec <= lasttime.tv_usec && 646 (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) { 647 tvp->tv_sec++; 648 tvp->tv_usec -= 1000000; 649 } 650 lasttime = *tvp; 651 splx(s); 652 } 653 654 void 655 delay(n) 656 int n; 657 { 658 DELAY(n); 659 } 660 661 #include "zsc.h" 662 663 int zssoft __P((void)); 664 665 void 666 cpu_intr(status, cause, pc, ipending) 667 u_int32_t status; 668 u_int32_t cause; 669 u_int32_t pc; 670 u_int32_t ipending; 671 { 672 uvmexp.intrs++; 673 674 /* device interrupts */ 675 (*hardware_intr)(status, cause, pc, ipending); 676 677 /* software simulated interrupt */ 678 if ((ipending & MIPS_SOFT_INT_MASK_1) || 679 (ssir && (status & MIPS_SOFT_INT_MASK_1))) { 680 681 #define DO_SIR(bit, fn) \ 682 do { \ 683 if (n & (bit)) { \ 684 uvmexp.softs++; \ 685 fn; \ 686 } \ 687 } while (0) 688 689 unsigned n; 690 n = ssir; ssir = 0; 691 _clrsoftintr(MIPS_SOFT_INT_MASK_1); 692 693 #if NZSC > 0 694 DO_SIR(SIR_SERIAL, zssoft()); 695 #endif 696 DO_SIR(SIR_NET, netintr()); 697 #undef DO_SIR 698 } 699 700 /* 'softclock' interrupt */ 701 if (ipending & MIPS_SOFT_INT_MASK_0) { 702 _clrsoftintr(MIPS_SOFT_INT_MASK_0); 703 uvmexp.softs++; 704 intrcnt[SOFTCLOCK_INTR]++; 705 softclock(NULL); 706 } 707 } 708