xref: /netbsd-src/sys/arch/evbmips/malta/machdep.c (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1 /*	$NetBSD: machdep.c,v 1.13 2003/08/07 16:27:22 agc Exp $	*/
2 
3 /*
4  * Copyright 2001, 2002 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1992, 1993
40  *	The Regents of the University of California.  All rights reserved.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * the Systems Programming Group of the University of Utah Computer
44  * Science Department, The Mach Operating System project at
45  * Carnegie-Mellon University and Ralph Campbell.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
73  */
74 /*
75  * Copyright (c) 1988 University of Utah.
76  *
77  * This code is derived from software contributed to Berkeley by
78  * the Systems Programming Group of the University of Utah Computer
79  * Science Department, The Mach Operating System project at
80  * Carnegie-Mellon University and Ralph Campbell.
81  *
82  * Redistribution and use in source and binary forms, with or without
83  * modification, are permitted provided that the following conditions
84  * are met:
85  * 1. Redistributions of source code must retain the above copyright
86  *    notice, this list of conditions and the following disclaimer.
87  * 2. Redistributions in binary form must reproduce the above copyright
88  *    notice, this list of conditions and the following disclaimer in the
89  *    documentation and/or other materials provided with the distribution.
90  * 3. All advertising materials mentioning features or use of this software
91  *    must display the following acknowledgement:
92  *	This product includes software developed by the University of
93  *	California, Berkeley and its contributors.
94  * 4. Neither the name of the University nor the names of its contributors
95  *    may be used to endorse or promote products derived from this software
96  *    without specific prior written permission.
97  *
98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108  * SUCH DAMAGE.
109  *
110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
112  */
113 
114 #include <sys/cdefs.h>
115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.13 2003/08/07 16:27:22 agc Exp $");
116 
117 #include "opt_ddb.h"
118 #include "opt_execfmt.h"
119 
120 #include <sys/param.h>
121 #include <sys/systm.h>
122 #include <sys/kernel.h>
123 #include <sys/buf.h>
124 #include <sys/reboot.h>
125 #include <sys/user.h>
126 #include <sys/mount.h>
127 #include <sys/kcore.h>
128 #include <sys/boot_flag.h>
129 #include <sys/termios.h>
130 #include <sys/ksyms.h>
131 
132 #include <uvm/uvm_extern.h>
133 
134 #include <dev/cons.h>
135 
136 #include "ksyms.h"
137 
138 #if NKSYMS || defined(DDB) || defined(LKM)
139 #include <machine/db_machdep.h>
140 #include <ddb/db_extern.h>
141 #endif
142 
143 #include <machine/cpu.h>
144 #include <machine/psl.h>
145 #include <machine/yamon.h>
146 
147 #include <evbmips/malta/autoconf.h>
148 #include <evbmips/malta/maltareg.h>
149 #include <evbmips/malta/maltavar.h>
150 
151 #include "com.h"
152 #if NCOM > 0
153 #include <dev/ic/comreg.h>
154 #include <dev/ic/comvar.h>
155 
156 int	comcnrate = 38400;	/* XXX should be config option */
157 #endif /* NCOM > 0 */
158 
159 
160 #define REGVAL(x)       *((__volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
161 
162 struct malta_config malta_configuration;
163 
164 /* For sysctl_hw. */
165 extern char cpu_model[];
166 
167 /* Our exported CPU info; we can have only one. */
168 struct cpu_info cpu_info_store;
169 
170 /* Maps for VM objects. */
171 struct vm_map *exec_map = NULL;
172 struct vm_map *mb_map = NULL;
173 struct vm_map *phys_map = NULL;
174 
175 int	physmem;		/* Total physical memory */
176 
177 int	netboot;		/* Are we netbooting? */
178 
179 yamon_env_var *yamon_envp;
180 
181 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
182 int mem_cluster_cnt;
183 
184 void	configure(void);
185 void	mach_init(int, char **, yamon_env_var *, u_long);
186 
187 /*
188  * safepri is a safe priority for sleep to set for a spin-wait during
189  * autoconfiguration or after a panic.  Used as an argument to splx().
190  */
191 int	safepri = MIPS1_PSL_LOWIPL;
192 
193 extern struct user *proc0paddr;
194 
195 /*
196  * Do all the stuff that locore normally does before calling main().
197  */
198 void
199 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
200 {
201 	struct malta_config *mcp = &malta_configuration;
202 	bus_space_handle_t sh;
203 	caddr_t kernend, v;
204         u_long first, last;
205 	vsize_t size;
206 	char *cp;
207 	int i, howto;
208 	uint8_t *brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
209 
210 	extern char edata[], end[];
211 
212 	*brkres = 0;	/* Disable BREAK==reset on console */
213 
214 	/* Get the propaganda in early! */
215 	led_display_str("NetBSD");
216 
217 	/*
218 	 * Clear the BSS segment.
219 	 */
220 	kernend = (caddr_t)mips_round_page(end);
221 	memset(edata, 0, kernend - edata);
222 
223 	/* save the yamon environment pointer */
224 	yamon_envp = envp;
225 
226 	/* Use YAMON callbacks for early console I/O */
227 	cn_tab = &yamon_promcd;
228 
229 	/*
230 	 * Set up the exception vectors and cpu-specific function
231 	 * vectors early on.  We need the wbflush() vector set up
232 	 * before comcnattach() is called (or at least before the
233 	 * first printf() after that is called).
234 	 * Also clears the I+D caches.
235 	 */
236 	mips_vector_init();
237 
238 	/* set the VM page size */
239 	uvm_setpagesize();
240 
241 	physmem = btoc(memsize);
242 
243 	gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
244 	malta_bus_io_init(&mcp->mc_iot, mcp);
245 	malta_bus_mem_init(&mcp->mc_memt, mcp);
246 	malta_dma_init(mcp);
247 
248 	/*
249 	 * Calibrate the timer, delay() relies on this.
250 	 */
251 	bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
252 	malta_cal_timer(&mcp->mc_iot, sh);
253 	bus_space_unmap(&mcp->mc_iot, sh, 2);
254 
255 #if NCOM > 0
256 	/*
257 	 * Delay to allow firmware putchars to complete.
258 	 * FIFO depth * character time.
259 	 * character time = (1000000 / (defaultrate / 10))
260 	 */
261 	delay(160000000 / comcnrate);
262 	if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
263 	    COM_FREQ, COM_TYPE_NORMAL,
264 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
265 		panic("malta: unable to initialize serial console");
266 #else
267 	panic("malta: not configured to use serial console");
268 #endif /* NCOM > 0 */
269 
270 	consinit();
271 
272 	mem_clusters[0].start = 0;
273 	mem_clusters[0].size = ctob(physmem);
274 	mem_cluster_cnt = 1;
275 
276 	strcpy(cpu_model, "MIPS Malta Evaluation Board");
277 
278 	/*
279 	 * XXX: check argv[0] - do something if "gdb"???
280 	 */
281 
282 	/*
283 	 * Look at arguments passed to us and compute boothowto.
284 	 */
285 	boothowto = RB_AUTOBOOT;
286 	for (i = 1; i < argc; i++) {
287 		for (cp = argv[i]; *cp; cp++) {
288 			/* Ignore superfluous '-', if there is one */
289 			if (*cp == '-')
290 				continue;
291 
292 			howto = 0;
293 			BOOT_FLAG(*cp, howto);
294 			if (! howto)
295 				printf("bootflag '%c' not recognised\n", *cp);
296 			else
297 				boothowto |= howto;
298 		}
299 	}
300 
301 	/*
302 	 * Load the rest of the available pages into the VM system.
303 	 */
304 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
305 	last = mem_clusters[0].start + mem_clusters[0].size;
306 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
307 		VM_FREELIST_DEFAULT);
308 
309 	/*
310 	 * Initialize error message buffer (at end of core).
311 	 */
312 	mips_init_msgbuf();
313 
314 	/*
315 	 * Compute the size of system data structures.  pmap_bootstrap()
316 	 * needs some of this information.
317 	 */
318 	size = (vsize_t)allocsys(NULL, NULL);
319 
320 	pmap_bootstrap();
321 
322 	/*
323 	 * Allocate space for proc0's USPACE.
324 	 */
325 	v = (caddr_t)uvm_pageboot_alloc(USPACE);
326 	lwp0.l_addr = proc0paddr = (struct user *)v;
327 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
328 	curpcb = &lwp0.l_addr->u_pcb;
329 	curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
330 
331 	/*
332 	 * Allocate space for system data structures.  These data structures
333 	 * are allocated here instead of cpu_startup() because physical
334 	 * memory is directly addressable.  We don't have to map these into
335 	 * the virtual address space.
336 	 */
337 	v = (caddr_t)uvm_pageboot_alloc(size);
338 	if ((allocsys(v, NULL) - v) != size)
339 		panic("mach_init: table size inconsistency");
340 
341 	/*
342 	 * Initialize debuggers, and break into them, if appropriate.
343 	 */
344 #if NKSYMS || defined(DDB) || defined(LKM)
345 	ksyms_init(0, 0, 0);
346 #endif
347 
348 #if defined(DDB)
349 	if (boothowto & RB_KDB)
350 		Debugger();
351 #endif
352 }
353 
354 void
355 consinit(void)
356 {
357 
358 	/*
359 	 * Everything related to console initialization is done
360 	 * in mach_init().
361 	 */
362 }
363 
364 /*
365  * Allocate memory for variable-sized tables,
366  */
367 void
368 cpu_startup()
369 {
370 	u_int i, base, residual;
371 	vaddr_t minaddr, maxaddr;
372 	vsize_t size;
373 	char pbuf[9];
374 
375 	/*
376 	 * Good {morning,afternoon,evening,night}.
377 	 */
378 	printf(version);
379 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
380 	printf("%s memory", pbuf);
381 
382 	/*
383 	 * Virtual memory is bootstrapped -- notify the bus spaces
384 	 * that memory allocation is now safe.
385 	 */
386 	malta_configuration.mc_mallocsafe = 1;
387 
388 	/*
389 	 * Allocate virtual address space for file I/O buffers.
390 	 * Note they are different than the array of headers, 'buf',
391 	 * and usually occupy more virtual memory than physical.
392 	 */
393 	size = MAXBSIZE * nbuf;
394 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size),
395 		    NULL, UVM_UNKNOWN_OFFSET, 0,
396 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
397 		    UVM_ADV_NORMAL, 0)) != 0)
398 		panic("startup: cannot allocate VM for buffers");
399 	minaddr = (vaddr_t)buffers;
400 	base = bufpages / nbuf;
401 	residual = bufpages % nbuf;
402 	for (i = 0; i < nbuf; i++) {
403 		vsize_t curbufsize;
404 		vaddr_t curbuf;
405 		struct vm_page *pg;
406 
407 		/*
408 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
409 		 * that MAXBSIZE space, we allocate and map (base+1) pages
410 		 * for the first "residual" buffers, and then we allocate
411 		 * "base" pages for the rest.
412 		 */
413 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
414 		curbufsize = PAGE_SIZE * ((i < residual) ? (base + 1) : base);
415 
416 		while (curbufsize) {
417 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
418 			if (pg == NULL)
419 				panic("cpu_startup: not enough memory for "
420 					"buffer cache");
421 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
422 				       VM_PROT_READ|VM_PROT_WRITE);
423 			curbuf += PAGE_SIZE;
424 			curbufsize -= PAGE_SIZE;
425 		}
426 	}
427 	pmap_update(pmap_kernel());
428 
429 	/*
430 	 * Allocate a submap for exec arguments.  This map effectively
431 	 * limits the number of processes exec'ing at any time.
432 	 */
433 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
434 				    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
435 	/*
436 	 * Allocate a submap for physio.
437 	 */
438 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
439 				    VM_PHYS_SIZE, 0, FALSE, NULL);
440 
441 	/*
442 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
443 	 * are allocated via the pool allocator, and we use KSEG to
444 	 * map those pages.)
445 	 */
446 
447 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
448 	printf(", %s free", pbuf);
449 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
450 	printf(", %s in %u buffers\n", pbuf, nbuf);
451 
452 	/*
453 	 * Set up buffers, so they can be used to read disk labels.
454 	 */
455 	bufinit();
456 }
457 
458 int	waittime = -1;
459 
460 void
461 cpu_reboot(howto, bootstr)
462 	int howto;
463 	char *bootstr;
464 {
465 
466 	/* Take a snapshot before clobbering any registers. */
467 	if (curproc)
468 		savectx((struct user *)curpcb);
469 
470 	if (cold) {
471 		howto |= RB_HALT;
472 		goto haltsys;
473 	}
474 
475 	/* If "always halt" was specified as a boot flag, obey. */
476 	if (boothowto & RB_HALT)
477 		howto |= RB_HALT;
478 
479 	boothowto = howto;
480 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
481 		waittime = 0;
482 		vfs_shutdown();
483 
484 		/*
485 		 * If we've been adjusting the clock, the todr
486 		 * will be out of synch; adjust it now.
487 		 */
488 		resettodr();
489 	}
490 
491 	splhigh();
492 
493 	if (howto & RB_DUMP)
494 		dumpsys();
495 
496 haltsys:
497 	doshutdownhooks();
498 
499 	if (howto & RB_HALT) {
500 		printf("\n");
501 		printf("The operating system has halted.\n");
502 		printf("Please press any key to reboot.\n\n");
503 		cnpollc(1);	/* For proper keyboard command handling */
504 		cngetc();
505 		cnpollc(0);
506 	}
507 
508 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
509 	yamon_exit(boothowto);
510 	printf("Oops, back from yamon_exit()\n\nResetting...");
511 
512 	REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
513 
514 	/*
515 	 * Need a small delay here, otherwise we see the first few characters of
516 	 * the warning below.
517 	 */
518 	delay(80000);
519 
520 	printf("WARNING: reset failed!\nSpinning...");
521 
522 	for (;;)
523 		/* spin forever */ ;	/* XXX */
524 }
525