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