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