xref: /netbsd-src/sys/arch/mipsco/mipsco/machdep.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: machdep.c,v 1.76 2012/07/28 23:08:56 matt 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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
38  */
39 
40 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
41 
42 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.76 2012/07/28 23:08:56 matt Exp $");
43 
44 /* from: Utah Hdr: machdep.c 1.63 91/04/24 */
45 
46 #include "opt_ddb.h"
47 #include "opt_kgdb.h"
48 #include "opt_modular.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/signalvar.h>
53 #include <sys/kernel.h>
54 #include <sys/proc.h>
55 #include <sys/buf.h>
56 #include <sys/reboot.h>
57 #include <sys/conf.h>
58 #include <sys/file.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
61 #include <sys/mbuf.h>
62 #include <sys/msgbuf.h>
63 #include <sys/ioctl.h>
64 #include <sys/device.h>
65 #include <sys/exec.h>
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68 #include <sys/kcore.h>
69 #include <sys/ksyms.h>
70 
71 #include <uvm/uvm_extern.h>
72 
73 #include <ufs/mfs/mfs_extern.h>		/* mfs_initminiroot() */
74 
75 #include <machine/cpu.h>
76 #include <machine/reg.h>
77 #include <machine/psl.h>
78 #include <machine/pte.h>
79 
80 #ifdef DDB
81 #include <machine/db_machdep.h>
82 #include <ddb/db_extern.h>
83 #endif
84 
85 #include <machine/intr.h>
86 #include <machine/mainboard.h>
87 #include <machine/sysconf.h>
88 #include <machine/autoconf.h>
89 #include <machine/bootinfo.h>
90 #include <machine/prom.h>
91 #include <dev/clock_subr.h>
92 #include <dev/cons.h>
93 
94 #include <sys/boot_flag.h>
95 
96 #include "opt_execfmt.h"
97 
98 #include "zsc.h"			/* XXX */
99 #include "com.h"			/* XXX */
100 #include "ksyms.h"
101 
102 /* maps for VM objects */
103 
104 struct vm_map *phys_map = NULL;
105 
106 char	*bootinfo = NULL;	/* pointer to bootinfo structure */
107 
108 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
109 int mem_cluster_cnt;
110 
111 void to_monitor(int) __dead;
112 void prom_halt(int) __dead;
113 
114 #ifdef	KGDB
115 void zs_kgdb_init(void);
116 void kgdb_connect(int);
117 #endif
118 
119 /*
120  *  Local functions.
121  */
122 int initcpu(void);
123 void configure(void);
124 
125 void mach_init(int, char *[], char*[], u_int, char *);
126 int  memsize_scan(void *);
127 
128 #ifdef DEBUG
129 /* stacktrace code violates prototypes to get callee's registers */
130 extern void stacktrace(void); /*XXX*/
131 #endif
132 
133 /* locore callback-vector setup */
134 extern void prom_init(void);
135 extern void pizazz_init(void);
136 
137 /* platform-specific initialization vector */
138 static void	unimpl_cons_init(void);
139 static void	unimpl_iointr(uint32_t, vaddr_t, uint32_t);
140 static int	unimpl_memsize(void *);
141 static void	unimpl_intr_establish(int, int (*)(void *), void *);
142 
143 struct platform platform = {
144 	.iobus = "iobus not set",
145 	.cons_init = unimpl_cons_init,
146 	.iointr = unimpl_iointr,
147 	.memsize = unimpl_memsize,
148 	.intr_establish = unimpl_intr_establish,
149 	.clkinit = NULL,
150 };
151 
152 extern struct consdev consdev_prom;
153 extern struct consdev consdev_zs;
154 
155 static void null_cnprobe(struct consdev *);
156 static void prom_cninit(struct consdev *);
157 static int  prom_cngetc(dev_t);
158 static void prom_cnputc(dev_t, int);
159 static void null_cnpollc(dev_t, int);
160 
161 struct consdev consdev_prom = {
162         null_cnprobe,
163 	prom_cninit,
164 	prom_cngetc,
165 	prom_cnputc,
166         null_cnpollc,
167 };
168 
169 
170 /*
171  * Do all the stuff that locore normally does before calling main().
172  * Process arguments passed to us by the prom monitor.
173  * Return the first page address following the system.
174  */
175 void
176 mach_init(int argc, char *argv[], char *envp[], u_int bim, char *bip)
177 {
178 	u_long first, last;
179 	char *kernend;
180 	char *cp;
181 	int i, howto;
182 	extern char edata[], end[];
183 	const char *bi_msg;
184 #if NKSYMS || defined(DDB) || defined(MODULAR)
185 	int nsym = 0;
186 	char *ssym = 0;
187 	char *esym = 0;
188 	struct btinfo_symtab *bi_syms;
189 #endif
190 
191 	/* Check for valid bootinfo passed from bootstrap */
192 	if (bim == BOOTINFO_MAGIC) {
193 		struct btinfo_magic *bi_magic;
194 
195 		bootinfo = (char *)BOOTINFO_ADDR; /* XXX */
196 		bi_magic = lookup_bootinfo(BTINFO_MAGIC);
197 		if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC)
198 			bi_msg = "invalid bootinfo structure.\n";
199 		else
200 			bi_msg = NULL;
201 	} else
202 		bi_msg = "invalid bootinfo (standalone boot?)\n";
203 
204 	/* clear the BSS segment */
205 	kernend = (void *)mips_round_page(end);
206 	memset(edata, 0, end - edata);
207 
208 	/*
209 	 * Copy exception-dispatch code down to exception vector.
210 	 * Initialize locore-function vector.
211 	 * Clear out the I and D caches.
212 	 */
213 	mips_vector_init(NULL, false);
214 
215 #if NKSYMS || defined(DDB) || defined(LKM)
216 	bi_syms = lookup_bootinfo(BTINFO_SYMTAB);
217 
218 	/* Load sysmbol table if present */
219 	if (bi_syms != NULL) {
220 		nsym = bi_syms->nsym;
221 		ssym = (void *)bi_syms->ssym;
222 		esym = (void *)bi_syms->esym;
223 		kernend = (void *)mips_round_page(esym);
224 	}
225 #endif
226 
227 	prom_init();
228 	consinit();
229 
230 	if (bi_msg != NULL)
231 		printf(bi_msg);
232 
233 	/*
234 	 * Set the VM page size.
235 	 */
236 	uvm_setpagesize();
237 
238 	/* Find out how much memory is available. */
239 	physmem = memsize_scan(kernend);
240 
241 	/*
242 	 * Now that we know how much memory we have, initialize the
243 	 * mem cluster array.
244 	 */
245 	mem_clusters[0].start = 0;		/* XXX is this correct? */
246 	mem_clusters[0].size  = ctob(physmem);
247 	mem_cluster_cnt = 1;
248 
249 	/* Look at argv[0] and compute bootdev */
250 	makebootdev(argv[0]);
251 
252 	/*
253 	 * Look at arguments passed to us and compute boothowto.
254 	 */
255 	boothowto = RB_AUTOBOOT;
256 	for (i = 1; i < argc; i++) {
257 		for (cp = argv[i]; *cp; cp++) {
258 			/* Ignore superfluous '-', if there is one */
259 			if (*cp == '-')
260 				continue;
261 
262 			howto = 0;
263 			BOOT_FLAG(*cp, howto);
264 			if (! howto)
265 				printf("bootflag '%c' not recognised\n", *cp);
266 			else
267 				boothowto |= howto;
268 		}
269 	}
270 
271 
272 #if NKSYMS || defined(DDB) || defined(MODULAR)
273 	/* init symbols if present */
274 	if (esym)
275 		ksyms_addsyms_elf(esym - ssym, ssym, esym);
276 #endif
277 #ifdef DDB
278 	if (boothowto & RB_KDB)
279 		Debugger();
280 #endif
281 #ifdef KGDB
282 	zs_kgdb_init();			/* XXX */
283 	if (boothowto & RB_KDB)
284 		kgdb_connect(0);
285 #endif
286 
287 	/*
288 	 * Check to see if a mini-root was loaded into memory. It resides
289 	 * at the start of the next page just after the end of BSS.
290 	 */
291 	if (boothowto & RB_MINIROOT)
292 		kernend += round_page(mfs_initminiroot(kernend));
293 
294 	/*
295 	 * Load the rest of the available pages into the VM system.
296 	 */
297 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
298 	last = mem_clusters[0].start + mem_clusters[0].size;
299 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
300 	    VM_FREELIST_DEFAULT);
301 
302 	/*
303 	 * Initialize error message buffer (at end of core).
304 	 */
305 	mips_init_msgbuf();
306 
307 	/*
308 	 * Initialize the virtual memory system.
309 	 */
310 	pmap_bootstrap();
311 
312 	/*
313 	 * Allocate uarea page for lwp0 and set it.
314 	 */
315 	mips_init_lwp0_uarea();
316 
317 	/*
318 	 * Set up interrupt handling and I/O addresses.
319 	 */
320 	pizazz_init();
321 }
322 
323 
324 
325 /*
326  * cpu_startup: allocate memory for variable-sized tables,
327  * initialize CPU, and do autoconfiguration.
328  */
329 void
330 cpu_startup(void)
331 {
332 	vaddr_t minaddr, maxaddr;
333 	char pbuf[9];
334 #ifdef DEBUG
335 	extern int pmapdebug;
336 	int opmapdebug = pmapdebug;
337 
338 	pmapdebug = 0;
339 #endif
340 
341 	/*
342 	 * Good {morning,afternoon,evening,night}.
343 	 */
344 	printf("%s%s", copyright, version);
345 	printf("%s\n", cpu_model);
346 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
347 	printf("total memory = %s\n", pbuf);
348 
349 	minaddr = 0;
350 	/*
351 	 * Allocate a submap for physio
352 	 */
353 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
354 				   VM_PHYS_SIZE, true, false, NULL);
355 
356 	/*
357 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
358 	 * are allocated via the pool allocator, and we use KSEG to
359 	 * map those pages.
360 	 */
361 
362 #ifdef DEBUG
363 	pmapdebug = opmapdebug;
364 #endif
365 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
366 	printf("avail memory = %s\n", pbuf);
367 }
368 
369 /*
370  * Look up information in bootinfo of boot loader.
371  */
372 void *
373 lookup_bootinfo(int type)
374 {
375 	struct btinfo_common *bt;
376 	char *help = bootinfo;
377 
378 	/* Check for a bootinfo record first. */
379 	if (help == NULL)
380 		return (NULL);
381 
382 	do {
383 		bt = (struct btinfo_common *)help;
384 		if (bt->type == type)
385 			return ((void *)help);
386 		help += bt->next;
387 	} while (bt->next != 0 &&
388 		(size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
389 
390 	return (NULL);
391 }
392 
393 int	waittime = -1;
394 
395 /*
396  * call PROM to halt or reboot.
397  */
398 void
399 prom_halt(int howto)
400 {
401 	if (howto & RB_HALT)
402 		MIPS_PROM(reinit)();
403 	MIPS_PROM(reboot)();
404 	/* NOTREACHED */
405 }
406 
407 void
408 cpu_reboot(volatile int howto, char *bootstr)
409 {
410 	/* take a snap shot before clobbering any registers */
411 	savectx(curpcb);
412 
413 #ifdef DEBUG
414 	if (panicstr)
415 		stacktrace();
416 #endif
417 
418 	/* If system is cold, just halt. */
419 	if (cold) {
420 		howto |= RB_HALT;
421 		goto haltsys;
422 	}
423 
424 	/* If "always halt" was specified as a boot flag, obey. */
425 	if ((boothowto & RB_HALT) != 0)
426 		howto |= RB_HALT;
427 
428 	boothowto = howto;
429 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
430 		/*
431 		 * Synchronize the disks....
432 		 */
433 		waittime = 0;
434 		vfs_shutdown();
435 
436 		/*
437 		 * If we've been adjusting the clock, the todr
438 		 * will be out of synch; adjust it now.
439 		 */
440 		resettodr();
441 	}
442 
443 	/* Disable interrupts. */
444 	splhigh();
445 
446 	/* If rebooting and a dump is requested do it. */
447 #if 0
448 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
449 #else
450 	if (howto & RB_DUMP)
451 #endif
452 		dumpsys();
453 
454 haltsys:
455 
456 	/* run any shutdown hooks */
457 	doshutdownhooks();
458 
459 	pmf_system_shutdown(boothowto);
460 
461 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
462 		prom_halt(0x80);	/* rom monitor RB_PWOFF */
463 
464 	/* Finally, halt/reboot the system. */
465 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
466 	prom_halt(howto & RB_HALT);
467 	/*NOTREACHED*/
468 }
469 
470 int
471 initcpu(void)
472 {
473 	spl0();		/* safe to turn interrupts on now */
474 	return 0;
475 }
476 
477 static void
478 unimpl_cons_init(void)
479 {
480 
481 	panic("sysconf.init didn't set cons_init");
482 }
483 
484 static void
485 unimpl_iointr(uint32_t status, vaddr_t pc, uint32_t ipending)
486 {
487 
488 	panic("sysconf.init didn't set intr");
489 }
490 
491 static int
492 unimpl_memsize(void *first)
493 {
494 
495 	panic("sysconf.init didn't set memsize");
496 }
497 
498 void
499 unimpl_intr_establish(int level, int (*func)(void *), void *arg)
500 {
501 	panic("sysconf.init didn't init intr_establish");
502 }
503 
504 void
505 delay(int n)
506 {
507 	DELAY(n);
508 }
509 
510 /*
511  * Find out how much memory is available by testing memory.
512  * Be careful to save and restore the original contents for msgbuf.
513  */
514 int
515 memsize_scan(void *first)
516 {
517 	volatile int *vp, *vp0;
518 	int mem, tmp, tmp0;
519 
520 #define PATTERN1 0xa5a5a5a5
521 #define	PATTERN2 ~PATTERN1
522 
523 	/*
524 	 * Non destructive scan of memory to determine the size
525 	 * Use the first page to test for memory aliases.  This
526 	 * also has the side effect of flushing the bus alignment
527 	 * buffer
528 	 */
529 	mem = btoc((paddr_t)first - MIPS_KSEG0_START);
530 	vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT);
531 	vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */
532 	tmp0 = *vp0;
533 	while (vp < (int *)MIPS_MAX_MEM_ADDR) {
534 		tmp = *vp;
535 		*vp  = PATTERN1;
536 		*vp0 = PATTERN2;
537 		wbflush();
538 		if (*vp != PATTERN1)
539 			break;
540 		*vp  = PATTERN2;
541 		*vp0 = PATTERN1;
542 		wbflush();
543 		if (*vp != PATTERN2)
544 			break;
545 		*vp = tmp;
546 		vp += PAGE_SIZE/sizeof(int);
547 		mem++;
548 	}
549 	*vp0 = tmp0;
550 	return mem;
551 }
552 
553 /*
554  * Console initialization: called early on from main,
555  * before vm init or startup.  Do enough configuration
556  * to choose and initialize a console.
557  */
558 
559 static void
560 null_cnprobe(struct consdev *cn)
561 {
562 }
563 
564 static void
565 prom_cninit(struct consdev *cn)
566 {
567 	extern const struct cdevsw cons_cdevsw;
568 
569 	cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0);
570 	cn->cn_pri = CN_REMOTE;
571 }
572 
573 static int
574 prom_cngetc(dev_t dev)
575 {
576 	return MIPS_PROM(getchar)();
577 }
578 
579 static void
580 prom_cnputc(dev_t dev, int c)
581 {
582 	MIPS_PROM(putchar)(c);
583 }
584 
585 static void
586 null_cnpollc(dev_t dev, int on)
587 {
588 }
589 
590 void
591 consinit(void)
592 {
593 	int zs_unit;
594 
595 	zs_unit = 0;
596 	cn_tab = &consdev_zs;
597 
598 	(*cn_tab->cn_init)(cn_tab);
599 }
600