1 /* $NetBSD: machdep.c,v 1.69 2009/11/27 03:23:11 rmind Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, The Mach Operating System project at
10 * Carnegie-Mellon University and Ralph Campbell.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
39 * Copyright (c) 1988 University of Utah.
41 * This code is derived from software contributed to Berkeley by
42 * the Systems Programming Group of the University of Utah Computer
43 * Science Department, The Mach Operating System project at
44 * Carnegie-Mellon University and Ralph Campbell.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
77 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
79 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.69 2009/11/27 03:23:11 rmind Exp $");
81 /* from: Utah Hdr: machdep.c 1.63 91/04/24 */
85 #include "opt_modular.h"
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/signalvar.h>
90 #include <sys/kernel.h>
93 #include <sys/reboot.h>
96 #include <sys/callout.h>
97 #include <sys/malloc.h>
99 #include <sys/msgbuf.h>
100 #include <sys/ioctl.h>
101 #include <sys/device.h>
102 #include <sys/exec.h>
103 #include <sys/mount.h>
104 #include <sys/syscallargs.h>
105 #include <sys/kcore.h>
106 #include <sys/ksyms.h>
108 #include <uvm/uvm_extern.h>
110 #include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */
112 #include <machine/cpu.h>
113 #include <machine/reg.h>
114 #include <machine/psl.h>
115 #include <machine/pte.h>
118 #include <machine/db_machdep.h>
119 #include <ddb/db_extern.h>
122 #include <machine/intr.h>
123 #include <machine/mainboard.h>
124 #include <machine/sysconf.h>
125 #include <machine/autoconf.h>
126 #include <machine/bootinfo.h>
127 #include <machine/prom.h>
128 #include <dev/clock_subr.h>
129 #include <dev/cons.h>
131 #include <sys/boot_flag.h>
134 #include "opt_execfmt.h"
136 #include "zsc.h" /* XXX */
137 #include "com.h" /* XXX */
140 /* Our exported CPU info; we can have only one. */
141 struct cpu_info cpu_info_store
;
143 /* maps for VM objects */
145 struct vm_map
*mb_map
= NULL
;
146 struct vm_map
*phys_map
= NULL
;
148 int physmem
; /* max supported memory, changes to actual */
149 char *bootinfo
= NULL
; /* pointer to bootinfo structure */
151 phys_ram_seg_t mem_clusters
[VM_PHYSSEG_MAX
];
154 void to_monitor(int) __attribute__((__noreturn__
));
155 void prom_halt(int) __attribute__((__noreturn__
));
158 void zs_kgdb_init(void);
159 void kgdb_connect(int);
166 void configure(void);
168 void mach_init(int, char *[], char*[], u_int
, char *);
169 int memsize_scan(void *);
172 /* stacktrace code violates prototypes to get callee's registers */
173 extern void stacktrace(void); /*XXX*/
177 * safepri is a safe priority for sleep to set for a spin-wait
178 * during autoconfiguration or after a panic. Used as an argument to splx().
179 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also
180 * disables mips1 FPU interrupts.
182 int safepri
= MIPS3_PSL_LOWIPL
; /* XXX */
184 /* locore callback-vector setup */
185 extern void mips_vector_init(void);
186 extern void prom_init(void);
187 extern void pizazz_init(void);
189 /* platform-specific initialization vector */
190 static void unimpl_cons_init(void);
191 static void unimpl_iointr(unsigned, unsigned, unsigned, unsigned);
192 static int unimpl_memsize(void *);
193 static void unimpl_intr_establish(int, int (*)(void *), void *);
195 struct platform platform
= {
196 .iobus
= "iobus not set",
197 .cons_init
= unimpl_cons_init
,
198 .iointr
= unimpl_iointr
,
199 .memsize
= unimpl_memsize
,
200 .intr_establish
= unimpl_intr_establish
,
204 struct consdev
*cn_tab
= NULL
;
205 extern struct consdev consdev_prom
;
206 extern struct consdev consdev_zs
;
208 static void null_cnprobe(struct consdev
*);
209 static void prom_cninit(struct consdev
*);
210 static int prom_cngetc(dev_t
);
211 static void prom_cnputc(dev_t
, int);
212 static void null_cnpollc(dev_t
, int);
214 struct consdev consdev_prom
= {
224 * Do all the stuff that locore normally does before calling main().
225 * Process arguments passed to us by the prom monitor.
226 * Return the first page address following the system.
229 mach_init(int argc
, char *argv
[], char *envp
[], u_int bim
, char *bip
)
235 extern char edata
[], end
[];
237 #if NKSYMS || defined(DDB) || defined(MODULAR)
241 struct btinfo_symtab
*bi_syms
;
245 /* Check for valid bootinfo passed from bootstrap */
246 if (bim
== BOOTINFO_MAGIC
) {
247 struct btinfo_magic
*bi_magic
;
249 bootinfo
= (char *)BOOTINFO_ADDR
; /* XXX */
250 bi_magic
= lookup_bootinfo(BTINFO_MAGIC
);
251 if (bi_magic
== NULL
|| bi_magic
->magic
!= BOOTINFO_MAGIC
)
252 bi_msg
= "invalid bootinfo structure.\n";
256 bi_msg
= "invalid bootinfo (standalone boot?)\n";
258 /* clear the BSS segment */
259 kernend
= (void *)mips_round_page(end
);
260 memset(edata
, 0, end
- edata
);
262 #if NKSYMS || defined(DDB) || defined(MODULAR)
263 bi_syms
= lookup_bootinfo(BTINFO_SYMTAB
);
265 /* Load sysmbol table if present */
266 if (bi_syms
!= NULL
) {
267 nsym
= bi_syms
->nsym
;
268 ssym
= (void *)bi_syms
->ssym
;
269 esym
= (void *)bi_syms
->esym
;
270 kernend
= (void *)mips_round_page(esym
);
281 * Set the VM page size.
285 /* Find out how much memory is available. */
286 physmem
= memsize_scan(kernend
);
289 * Now that we know how much memory we have, initialize the
292 mem_clusters
[0].start
= 0; /* XXX is this correct? */
293 mem_clusters
[0].size
= ctob(physmem
);
297 * Copy exception-dispatch code down to exception vector.
298 * Initialize locore-function vector.
299 * Clear out the I and D caches.
303 /* Look at argv[0] and compute bootdev */
304 makebootdev(argv
[0]);
307 * Look at arguments passed to us and compute boothowto.
309 boothowto
= RB_AUTOBOOT
;
310 for (i
= 1; i
< argc
; i
++) {
311 for (cp
= argv
[i
]; *cp
; cp
++) {
312 /* Ignore superfluous '-', if there is one */
317 BOOT_FLAG(*cp
, howto
);
319 printf("bootflag '%c' not recognised\n", *cp
);
326 #if NKSYMS || defined(DDB) || defined(MODULAR)
327 /* init symbols if present */
329 ksyms_addsyms_elf(esym
- ssym
, ssym
, esym
);
332 if (boothowto
& RB_KDB
)
336 zs_kgdb_init(); /* XXX */
337 if (boothowto
& RB_KDB
)
343 * Check to see if a mini-root was loaded into memory. It resides
344 * at the start of the next page just after the end of BSS.
346 if (boothowto
& RB_MINIROOT
)
347 kernend
+= round_page(mfs_initminiroot(kernend
));
351 * Load the rest of the available pages into the VM system.
353 first
= round_page(MIPS_KSEG0_TO_PHYS(kernend
));
354 last
= mem_clusters
[0].start
+ mem_clusters
[0].size
;
355 uvm_page_physload(atop(first
), atop(last
), atop(first
), atop(last
),
356 VM_FREELIST_DEFAULT
);
359 * Initialize error message buffer (at end of core).
364 * Initialize the virtual memory system.
369 * Allocate uarea page for lwp0 and set it.
371 mips_init_lwp0_uarea();
374 * Set up interrupt handling and I/O addresses.
382 * cpu_startup: allocate memory for variable-sized tables,
383 * initialize CPU, and do autoconfiguration.
388 vaddr_t minaddr
, maxaddr
;
391 extern int pmapdebug
;
392 int opmapdebug
= pmapdebug
;
398 * Good {morning,afternoon,evening,night}.
400 printf("%s%s", copyright
, version
);
401 printf("%s\n", cpu_model
);
402 format_bytes(pbuf
, sizeof(pbuf
), ctob(physmem
));
403 printf("total memory = %s\n", pbuf
);
407 * Allocate a submap for physio
409 phys_map
= uvm_km_suballoc(kernel_map
, &minaddr
, &maxaddr
,
410 VM_PHYS_SIZE
, true, false, NULL
);
413 * No need to allocate an mbuf cluster submap. Mbuf clusters
414 * are allocated via the pool allocator, and we use KSEG to
419 pmapdebug
= opmapdebug
;
421 format_bytes(pbuf
, sizeof(pbuf
), ptoa(uvmexp
.free
));
422 printf("avail memory = %s\n", pbuf
);
426 * Look up information in bootinfo of boot loader.
429 lookup_bootinfo(int type
)
431 struct btinfo_common
*bt
;
432 char *help
= bootinfo
;
434 /* Check for a bootinfo record first. */
439 bt
= (struct btinfo_common
*)help
;
440 if (bt
->type
== type
)
441 return ((void *)help
);
443 } while (bt
->next
!= 0 &&
444 (size_t)help
< (size_t)bootinfo
+ BOOTINFO_SIZE
);
452 * call PROM to halt or reboot.
464 cpu_reboot(volatile int howto
, char *bootstr
)
466 /* take a snap shot before clobbering any registers */
475 /* If system is cold, just halt. */
481 /* If "always halt" was specified as a boot flag, obey. */
482 if ((boothowto
& RB_HALT
) != 0)
486 if ((howto
& RB_NOSYNC
) == 0 && waittime
< 0) {
488 * Synchronize the disks....
494 * If we've been adjusting the clock, the todr
495 * will be out of synch; adjust it now.
500 /* Disable interrupts. */
503 /* If rebooting and a dump is requested do it. */
505 if ((howto
& (RB_DUMP
| RB_HALT
)) == RB_DUMP
)
513 /* run any shutdown hooks */
516 pmf_system_shutdown(boothowto
);
518 if ((howto
& RB_POWERDOWN
) == RB_POWERDOWN
)
519 prom_halt(0x80); /* rom monitor RB_PWOFF */
521 /* Finally, halt/reboot the system. */
522 printf("%s\n\n", howto
& RB_HALT
? "halted." : "rebooting...");
523 prom_halt(howto
& RB_HALT
);
530 spl0(); /* safe to turn interrupts on now */
535 unimpl_cons_init(void)
538 panic("sysconf.init didn't set cons_init");
542 unimpl_iointr(u_int mask
, u_int pc
, u_int statusreg
, u_int causereg
)
545 panic("sysconf.init didn't set intr");
549 unimpl_memsize(void *first
)
552 panic("sysconf.init didn't set memsize");
556 unimpl_intr_establish(int level
, int (*func
)(void *), void *arg
)
558 panic("sysconf.init didn't init intr_establish");
568 * Find out how much memory is available by testing memory.
569 * Be careful to save and restore the original contents for msgbuf.
572 memsize_scan(void *first
)
574 volatile int *vp
, *vp0
;
577 #define PATTERN1 0xa5a5a5a5
578 #define PATTERN2 ~PATTERN1
581 * Non destructive scan of memory to determine the size
582 * Use the first page to test for memory aliases. This
583 * also has the side effect of flushing the bus alignment
586 mem
= btoc((paddr_t
)first
- MIPS_KSEG0_START
);
587 vp
= (int *)MIPS_PHYS_TO_KSEG1(mem
<< PGSHIFT
);
588 vp0
= (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */
590 while (vp
< (int *)MIPS_MAX_MEM_ADDR
) {
603 vp
+= PAGE_SIZE
/sizeof(int);
611 * Console initialization: called early on from main,
612 * before vm init or startup. Do enough configuration
613 * to choose and initialize a console.
617 null_cnprobe(struct consdev
*cn
)
622 prom_cninit(struct consdev
*cn
)
624 extern const struct cdevsw cons_cdevsw
;
626 cn
->cn_dev
= makedev(cdevsw_lookup_major(&cons_cdevsw
), 0);
627 cn
->cn_pri
= CN_REMOTE
;
631 prom_cngetc(dev_t dev
)
633 return MIPS_PROM(getchar
)();
637 prom_cnputc(dev_t dev
, int c
)
639 MIPS_PROM(putchar
)(c
);
643 null_cnpollc(dev_t dev
, int on
)
653 cn_tab
= &consdev_zs
;
655 (*cn_tab
->cn_init
)(cn_tab
);