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