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