machdep.c revision 1.62
1/*	$NetBSD: machdep.c,v 1.62 2009/02/13 22:41:02 apb Exp $	*/
2
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
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.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
36 *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
37 */
38/*
39 * Copyright (c) 1988 University of Utah.
40 *
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.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
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.
61 *
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
72 * SUCH DAMAGE.
73 *
74 *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
75 */
76
77#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
78
79__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.62 2009/02/13 22:41:02 apb Exp $");
80
81/* from: Utah Hdr: machdep.c 1.63 91/04/24 */
82
83#include "opt_ddb.h"
84#include "opt_kgdb.h"
85#include "opt_modular.h"
86
87#include <sys/param.h>
88#include <sys/systm.h>
89#include <sys/signalvar.h>
90#include <sys/kernel.h>
91#include <sys/proc.h>
92#include <sys/buf.h>
93#include <sys/reboot.h>
94#include <sys/conf.h>
95#include <sys/file.h>
96#include <sys/callout.h>
97#include <sys/malloc.h>
98#include <sys/mbuf.h>
99#include <sys/msgbuf.h>
100#include <sys/ioctl.h>
101#include <sys/device.h>
102#include <sys/user.h>
103#include <sys/exec.h>
104#include <sys/mount.h>
105#include <sys/syscallargs.h>
106#include <sys/kcore.h>
107#include <sys/ksyms.h>
108
109#include <uvm/uvm_extern.h>
110
111#include <ufs/mfs/mfs_extern.h>		/* mfs_initminiroot() */
112
113#include <machine/cpu.h>
114#include <machine/reg.h>
115#include <machine/psl.h>
116#include <machine/pte.h>
117
118#ifdef DDB
119#include <machine/db_machdep.h>
120#include <ddb/db_extern.h>
121#endif
122
123#include <machine/intr.h>
124#include <machine/mainboard.h>
125#include <machine/sysconf.h>
126#include <machine/autoconf.h>
127#include <machine/bootinfo.h>
128#include <machine/prom.h>
129#include <dev/clock_subr.h>
130#include <dev/cons.h>
131
132#include <sys/boot_flag.h>
133
134#include "fs_mfs.h"
135#include "opt_execfmt.h"
136
137#include "zsc.h"			/* XXX */
138#include "com.h"			/* XXX */
139#include "ksyms.h"
140
141/* Our exported CPU info; we can have only one. */
142struct cpu_info cpu_info_store;
143
144/* maps for VM objects */
145
146struct vm_map *mb_map = NULL;
147struct vm_map *phys_map = NULL;
148
149int	physmem;		/* max supported memory, changes to actual */
150char	*bootinfo = NULL;	/* pointer to bootinfo structure */
151
152phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
153int mem_cluster_cnt;
154
155void to_monitor __P((int)) __attribute__((__noreturn__));
156void prom_halt __P((int)) __attribute__((__noreturn__));
157
158#ifdef	KGDB
159void zs_kgdb_init __P((void));
160void kgdb_connect __P((int));
161#endif
162
163/*
164 *  Local functions.
165 */
166int initcpu __P((void));
167void configure __P((void));
168
169void mach_init __P((int, char *[], char*[], u_int, char *));
170int  memsize_scan __P((void *));
171
172#ifdef DEBUG
173/* stacktrace code violates prototypes to get callee's registers */
174extern void stacktrace __P((void)); /*XXX*/
175#endif
176
177/*
178 * safepri is a safe priority for sleep to set for a spin-wait
179 * during autoconfiguration or after a panic.  Used as an argument to splx().
180 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also
181 * disables mips1 FPU interrupts.
182 */
183int	safepri = MIPS3_PSL_LOWIPL;	/* XXX */
184extern struct user *proc0paddr;
185
186/* locore callback-vector setup */
187extern void mips_vector_init  __P((void));
188extern void prom_init  __P((void));
189extern void pizazz_init __P((void));
190
191/* platform-specific initialization vector */
192static void	unimpl_cons_init __P((void));
193static void	unimpl_iointr __P((unsigned, unsigned, unsigned, unsigned));
194static int	unimpl_memsize __P((void *));
195static void	unimpl_intr_establish __P((int, int (*)__P((void *)), void *));
196
197struct platform platform = {
198	.iobus = "iobus not set",
199	.cons_init = unimpl_cons_init,
200	.iointr = unimpl_iointr,
201	.memsize = unimpl_memsize,
202	.intr_establish = unimpl_intr_establish,
203	.clkinit = NULL,
204};
205
206struct consdev *cn_tab = NULL;
207extern struct consdev consdev_prom;
208extern struct consdev consdev_zs;
209
210static void null_cnprobe __P((struct consdev *));
211static void prom_cninit __P((struct consdev *));
212static int  prom_cngetc __P((dev_t));
213static void prom_cnputc __P((dev_t, int));
214static void null_cnpollc __P((dev_t, int));
215
216struct consdev consdev_prom = {
217        null_cnprobe,
218	prom_cninit,
219	prom_cngetc,
220	prom_cnputc,
221        null_cnpollc,
222};
223
224
225/*
226 * Do all the stuff that locore normally does before calling main().
227 * Process arguments passed to us by the prom monitor.
228 * Return the first page address following the system.
229 */
230void
231mach_init(argc, argv, envp, bim, bip)
232	int    argc;
233	char   *argv[];
234	char   *envp[];
235	u_int  bim;
236	char   *bip;
237{
238	u_long first, last;
239	char *kernend, *v;
240	char *cp;
241	int i, howto;
242	extern char edata[], end[];
243	const char *bi_msg;
244#if NKSYMS || defined(DDB) || defined(MODULAR)
245	int nsym = 0;
246	char *ssym = 0;
247	char *esym = 0;
248	struct btinfo_symtab *bi_syms;
249#endif
250
251
252	/* Check for valid bootinfo passed from bootstrap */
253	if (bim == BOOTINFO_MAGIC) {
254		struct btinfo_magic *bi_magic;
255
256		bootinfo = (char *)BOOTINFO_ADDR; /* XXX */
257		bi_magic = lookup_bootinfo(BTINFO_MAGIC);
258		if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC)
259			bi_msg = "invalid bootinfo structure.\n";
260		else
261			bi_msg = NULL;
262	} else
263		bi_msg = "invalid bootinfo (standalone boot?)\n";
264
265	/* clear the BSS segment */
266	kernend = (void *)mips_round_page(end);
267	memset(edata, 0, end - edata);
268
269#if NKSYMS || defined(DDB) || defined(MODULAR)
270	bi_syms = lookup_bootinfo(BTINFO_SYMTAB);
271
272	/* Load sysmbol table if present */
273	if (bi_syms != NULL) {
274		nsym = bi_syms->nsym;
275		ssym = (void *)bi_syms->ssym;
276		esym = (void *)bi_syms->esym;
277		kernend = (void *)mips_round_page(esym);
278	}
279#endif
280
281	prom_init();
282	consinit();
283
284	if (bi_msg != NULL)
285		printf(bi_msg);
286
287	/*
288	 * Set the VM page size.
289	 */
290	uvm_setpagesize();
291
292	/* Find out how much memory is available. */
293	physmem = memsize_scan(kernend);
294
295	/*
296	 * Now that we know how much memory we have, initialize the
297	 * mem cluster array.
298	 */
299	mem_clusters[0].start = 0;		/* XXX is this correct? */
300	mem_clusters[0].size  = ctob(physmem);
301	mem_cluster_cnt = 1;
302
303	/*
304	 * Copy exception-dispatch code down to exception vector.
305	 * Initialize locore-function vector.
306	 * Clear out the I and D caches.
307	 */
308	mips_vector_init();
309
310	/* Look at argv[0] and compute bootdev */
311	makebootdev(argv[0]);
312
313	/*
314	 * Look at arguments passed to us and compute boothowto.
315	 */
316	boothowto = RB_AUTOBOOT;
317	for (i = 1; i < argc; i++) {
318		for (cp = argv[i]; *cp; cp++) {
319			/* Ignore superfluous '-', if there is one */
320			if (*cp == '-')
321				continue;
322
323			howto = 0;
324			BOOT_FLAG(*cp, howto);
325			if (! howto)
326				printf("bootflag '%c' not recognised\n", *cp);
327			else
328				boothowto |= howto;
329		}
330	}
331
332
333#if NKSYMS || defined(DDB) || defined(MODULAR)
334	/* init symbols if present */
335	if (esym)
336		ksyms_addsyms_elf(esym - ssym, ssym, esym);
337#endif
338#ifdef DDB
339	if (boothowto & RB_KDB)
340		Debugger();
341#endif
342#ifdef KGDB
343	zs_kgdb_init();			/* XXX */
344	if (boothowto & RB_KDB)
345		kgdb_connect(0);
346#endif
347
348#ifdef MFS
349	/*
350	 * Check to see if a mini-root was loaded into memory. It resides
351	 * at the start of the next page just after the end of BSS.
352	 */
353	if (boothowto & RB_MINIROOT)
354		kernend += round_page(mfs_initminiroot(kernend));
355#endif
356
357	/*
358	 * Load the rest of the available pages into the VM system.
359	 */
360	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
361	last = mem_clusters[0].start + mem_clusters[0].size;
362	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
363	    VM_FREELIST_DEFAULT);
364
365	/*
366	 * Initialize error message buffer (at end of core).
367	 */
368	mips_init_msgbuf();
369
370	/*
371	 * Initialize the virtual memory system.
372	 */
373	pmap_bootstrap();
374
375	/*
376	 * Allocate space for proc0's USPACE.
377	 */
378	v = (void *)uvm_pageboot_alloc(USPACE);
379	lwp0.l_addr = proc0paddr = (struct user *)v;
380	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
381	proc0paddr->u_pcb.pcb_context[11] =
382	    MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
383
384	/*
385	 * Set up interrupt handling and I/O addresses.
386	 */
387	pizazz_init();
388}
389
390
391
392/*
393 * cpu_startup: allocate memory for variable-sized tables,
394 * initialize CPU, and do autoconfiguration.
395 */
396void
397cpu_startup()
398{
399	vaddr_t minaddr, maxaddr;
400	char pbuf[9];
401#ifdef DEBUG
402	extern int pmapdebug;
403	int opmapdebug = pmapdebug;
404
405	pmapdebug = 0;
406#endif
407
408	/*
409	 * Good {morning,afternoon,evening,night}.
410	 */
411	printf("%s%s", copyright, version);
412	printf("%s\n", cpu_model);
413	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
414	printf("total memory = %s\n", pbuf);
415
416	minaddr = 0;
417	/*
418	 * Allocate a submap for physio
419	 */
420	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
421				   VM_PHYS_SIZE, true, false, NULL);
422
423	/*
424	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
425	 * are allocated via the pool allocator, and we use KSEG to
426	 * map those pages.
427	 */
428
429#ifdef DEBUG
430	pmapdebug = opmapdebug;
431#endif
432	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
433	printf("avail memory = %s\n", pbuf);
434}
435
436/*
437 * Look up information in bootinfo of boot loader.
438 */
439void *
440lookup_bootinfo(type)
441	int type;
442{
443	struct btinfo_common *bt;
444	char *help = bootinfo;
445
446	/* Check for a bootinfo record first. */
447	if (help == NULL)
448		return (NULL);
449
450	do {
451		bt = (struct btinfo_common *)help;
452		if (bt->type == type)
453			return ((void *)help);
454		help += bt->next;
455	} while (bt->next != 0 &&
456		(size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
457
458	return (NULL);
459}
460
461int	waittime = -1;
462
463/*
464 * call PROM to halt or reboot.
465 */
466void
467prom_halt(howto)
468	int howto;
469{
470	if (howto & RB_HALT)
471		MIPS_PROM(reinit)();
472	MIPS_PROM(reboot)();
473	/* NOTREACHED */
474}
475
476void
477cpu_reboot(howto, bootstr)
478	volatile int howto;
479	char *bootstr;
480{
481	/* take a snap shot before clobbering any registers */
482	if (curlwp)
483		savectx((struct user *)curpcb);
484
485#ifdef DEBUG
486	if (panicstr)
487		stacktrace();
488#endif
489
490	/* If system is cold, just halt. */
491	if (cold) {
492		howto |= RB_HALT;
493		goto haltsys;
494	}
495
496	/* If "always halt" was specified as a boot flag, obey. */
497	if ((boothowto & RB_HALT) != 0)
498		howto |= RB_HALT;
499
500	boothowto = howto;
501	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
502		/*
503		 * Synchronize the disks....
504		 */
505		waittime = 0;
506		vfs_shutdown();
507
508		/*
509		 * If we've been adjusting the clock, the todr
510		 * will be out of synch; adjust it now.
511		 */
512		resettodr();
513	}
514
515	/* Disable interrupts. */
516	splhigh();
517
518	/* If rebooting and a dump is requested do it. */
519#if 0
520	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
521#else
522	if (howto & RB_DUMP)
523#endif
524		dumpsys();
525
526haltsys:
527
528	/* run any shutdown hooks */
529	doshutdownhooks();
530
531	pmf_system_shutdown(boothowto);
532
533	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
534		prom_halt(0x80);	/* rom monitor RB_PWOFF */
535
536	/* Finally, halt/reboot the system. */
537	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
538	prom_halt(howto & RB_HALT);
539	/*NOTREACHED*/
540}
541
542int
543initcpu()
544{
545	spl0();		/* safe to turn interrupts on now */
546	return 0;
547}
548
549static void
550unimpl_cons_init()
551{
552
553	panic("sysconf.init didn't set cons_init");
554}
555
556static void
557unimpl_iointr(mask, pc, statusreg, causereg)
558	u_int mask;
559	u_int pc;
560	u_int statusreg;
561	u_int causereg;
562{
563
564	panic("sysconf.init didn't set intr");
565}
566
567static int
568unimpl_memsize(first)
569void *first;
570{
571
572	panic("sysconf.init didn't set memsize");
573}
574
575void
576unimpl_intr_establish(level, func, arg)
577	int level;
578	int (*func) __P((void *));
579	void *arg;
580{
581	panic("sysconf.init didn't init intr_establish");
582}
583
584void
585delay(n)
586	int n;
587{
588	DELAY(n);
589}
590
591/*
592 * Find out how much memory is available by testing memory.
593 * Be careful to save and restore the original contents for msgbuf.
594 */
595int
596memsize_scan(first)
597	void *first;
598{
599	volatile int *vp, *vp0;
600	int mem, tmp, tmp0;
601
602#define PATTERN1 0xa5a5a5a5
603#define	PATTERN2 ~PATTERN1
604
605	/*
606	 * Non destructive scan of memory to determine the size
607	 * Use the first page to test for memory aliases.  This
608	 * also has the side effect of flushing the bus alignment
609	 * buffer
610	 */
611	mem = btoc((paddr_t)first - MIPS_KSEG0_START);
612	vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT);
613	vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */
614	tmp0 = *vp0;
615	while (vp < (int *)MIPS_MAX_MEM_ADDR) {
616		tmp = *vp;
617		*vp  = PATTERN1;
618		*vp0 = PATTERN2;
619		wbflush();
620		if (*vp != PATTERN1)
621			break;
622		*vp  = PATTERN2;
623		*vp0 = PATTERN1;
624		wbflush();
625		if (*vp != PATTERN2)
626			break;
627		*vp = tmp;
628		vp += PAGE_SIZE/sizeof(int);
629		mem++;
630	}
631	*vp0 = tmp0;
632	return mem;
633}
634
635/*
636 * Console initialization: called early on from main,
637 * before vm init or startup.  Do enough configuration
638 * to choose and initialize a console.
639 */
640
641static void
642null_cnprobe(cn)
643     struct consdev *cn;
644{
645}
646
647static void
648prom_cninit(cn)
649	struct consdev *cn;
650{
651	extern const struct cdevsw cons_cdevsw;
652
653	cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0);
654	cn->cn_pri = CN_REMOTE;
655}
656
657static int
658prom_cngetc(dev)
659	dev_t dev;
660{
661	return MIPS_PROM(getchar)();
662}
663
664static void
665prom_cnputc(dev, c)
666	dev_t dev;
667	int c;
668{
669	MIPS_PROM(putchar)(c);
670}
671
672static void
673null_cnpollc(dev, on)
674	dev_t dev;
675	int on;
676{
677}
678
679void
680consinit()
681{
682	int zs_unit;
683
684	zs_unit = 0;
685	cn_tab = &consdev_zs;
686
687	(*cn_tab->cn_init)(cn_tab);
688}
689