machdep.c revision 1.12 1 /* $NetBSD: machdep.c,v 1.12 2003/07/15 01:37:33 lukem 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 <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.12 2003/07/15 01:37:33 lukem Exp $");
82
83 #include "opt_ddb.h"
84 #include "opt_execfmt.h"
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/buf.h>
90 #include <sys/reboot.h>
91 #include <sys/user.h>
92 #include <sys/mount.h>
93 #include <sys/kcore.h>
94 #include <sys/boot_flag.h>
95 #include <sys/termios.h>
96 #include <sys/ksyms.h>
97
98 #include <uvm/uvm_extern.h>
99
100 #include <dev/cons.h>
101
102 #include "ksyms.h"
103
104 #if NKSYMS || defined(DDB) || defined(LKM)
105 #include <machine/db_machdep.h>
106 #include <ddb/db_extern.h>
107 #endif
108
109 #include <machine/cpu.h>
110 #include <machine/psl.h>
111 #include <machine/yamon.h>
112
113 #include <evbmips/malta/autoconf.h>
114 #include <evbmips/malta/maltareg.h>
115 #include <evbmips/malta/maltavar.h>
116
117 #include "com.h"
118 #if NCOM > 0
119 #include <dev/ic/comreg.h>
120 #include <dev/ic/comvar.h>
121
122 int comcnrate = 38400; /* XXX should be config option */
123 #endif /* NCOM > 0 */
124
125
126 #define REGVAL(x) *((__volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
127
128 struct malta_config malta_configuration;
129
130 /* For sysctl_hw. */
131 extern char cpu_model[];
132
133 /* Our exported CPU info; we can have only one. */
134 struct cpu_info cpu_info_store;
135
136 /* Maps for VM objects. */
137 struct vm_map *exec_map = NULL;
138 struct vm_map *mb_map = NULL;
139 struct vm_map *phys_map = NULL;
140
141 int physmem; /* Total physical memory */
142
143 int netboot; /* Are we netbooting? */
144
145 yamon_env_var *yamon_envp;
146
147 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
148 int mem_cluster_cnt;
149
150 void configure(void);
151 void mach_init(int, char **, yamon_env_var *, u_long);
152
153 /*
154 * safepri is a safe priority for sleep to set for a spin-wait during
155 * autoconfiguration or after a panic. Used as an argument to splx().
156 */
157 int safepri = MIPS1_PSL_LOWIPL;
158
159 extern struct user *proc0paddr;
160
161 /*
162 * Do all the stuff that locore normally does before calling main().
163 */
164 void
165 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
166 {
167 struct malta_config *mcp = &malta_configuration;
168 bus_space_handle_t sh;
169 caddr_t kernend, v;
170 u_long first, last;
171 vsize_t size;
172 char *cp;
173 int i, howto;
174 uint8_t *brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
175
176 extern char edata[], end[];
177
178 *brkres = 0; /* Disable BREAK==reset on console */
179
180 /* Get the propaganda in early! */
181 led_display_str("NetBSD");
182
183 /*
184 * Clear the BSS segment.
185 */
186 kernend = (caddr_t)mips_round_page(end);
187 memset(edata, 0, kernend - edata);
188
189 /* save the yamon environment pointer */
190 yamon_envp = envp;
191
192 /* Use YAMON callbacks for early console I/O */
193 cn_tab = &yamon_promcd;
194
195 /*
196 * Set up the exception vectors and cpu-specific function
197 * vectors early on. We need the wbflush() vector set up
198 * before comcnattach() is called (or at least before the
199 * first printf() after that is called).
200 * Also clears the I+D caches.
201 */
202 mips_vector_init();
203
204 /* set the VM page size */
205 uvm_setpagesize();
206
207 physmem = btoc(memsize);
208
209 gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
210 malta_bus_io_init(&mcp->mc_iot, mcp);
211 malta_bus_mem_init(&mcp->mc_memt, mcp);
212 malta_dma_init(mcp);
213
214 /*
215 * Calibrate the timer, delay() relies on this.
216 */
217 bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
218 malta_cal_timer(&mcp->mc_iot, sh);
219 bus_space_unmap(&mcp->mc_iot, sh, 2);
220
221 #if NCOM > 0
222 /*
223 * Delay to allow firmware putchars to complete.
224 * FIFO depth * character time.
225 * character time = (1000000 / (defaultrate / 10))
226 */
227 delay(160000000 / comcnrate);
228 if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
229 COM_FREQ, COM_TYPE_NORMAL,
230 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
231 panic("malta: unable to initialize serial console");
232 #else
233 panic("malta: not configured to use serial console");
234 #endif /* NCOM > 0 */
235
236 consinit();
237
238 mem_clusters[0].start = 0;
239 mem_clusters[0].size = ctob(physmem);
240 mem_cluster_cnt = 1;
241
242 strcpy(cpu_model, "MIPS Malta Evaluation Board");
243
244 /*
245 * XXX: check argv[0] - do something if "gdb"???
246 */
247
248 /*
249 * Look at arguments passed to us and compute boothowto.
250 */
251 boothowto = RB_AUTOBOOT;
252 for (i = 1; i < argc; i++) {
253 for (cp = argv[i]; *cp; cp++) {
254 /* Ignore superfluous '-', if there is one */
255 if (*cp == '-')
256 continue;
257
258 howto = 0;
259 BOOT_FLAG(*cp, howto);
260 if (! howto)
261 printf("bootflag '%c' not recognised\n", *cp);
262 else
263 boothowto |= howto;
264 }
265 }
266
267 /*
268 * Load the rest of the available pages into the VM system.
269 */
270 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
271 last = mem_clusters[0].start + mem_clusters[0].size;
272 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
273 VM_FREELIST_DEFAULT);
274
275 /*
276 * Initialize error message buffer (at end of core).
277 */
278 mips_init_msgbuf();
279
280 /*
281 * Compute the size of system data structures. pmap_bootstrap()
282 * needs some of this information.
283 */
284 size = (vsize_t)allocsys(NULL, NULL);
285
286 pmap_bootstrap();
287
288 /*
289 * Allocate space for proc0's USPACE.
290 */
291 v = (caddr_t)uvm_pageboot_alloc(USPACE);
292 lwp0.l_addr = proc0paddr = (struct user *)v;
293 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
294 curpcb = &lwp0.l_addr->u_pcb;
295 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
296
297 /*
298 * Allocate space for system data structures. These data structures
299 * are allocated here instead of cpu_startup() because physical
300 * memory is directly addressable. We don't have to map these into
301 * the virtual address space.
302 */
303 v = (caddr_t)uvm_pageboot_alloc(size);
304 if ((allocsys(v, NULL) - v) != size)
305 panic("mach_init: table size inconsistency");
306
307 /*
308 * Initialize debuggers, and break into them, if appropriate.
309 */
310 #if NKSYMS || defined(DDB) || defined(LKM)
311 ksyms_init(0, 0, 0);
312 #endif
313
314 #if defined(DDB)
315 if (boothowto & RB_KDB)
316 Debugger();
317 #endif
318 }
319
320 void
321 consinit(void)
322 {
323
324 /*
325 * Everything related to console initialization is done
326 * in mach_init().
327 */
328 }
329
330 /*
331 * Allocate memory for variable-sized tables,
332 */
333 void
334 cpu_startup()
335 {
336 u_int i, base, residual;
337 vaddr_t minaddr, maxaddr;
338 vsize_t size;
339 char pbuf[9];
340
341 /*
342 * Good {morning,afternoon,evening,night}.
343 */
344 printf(version);
345 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
346 printf("%s memory", pbuf);
347
348 /*
349 * Virtual memory is bootstrapped -- notify the bus spaces
350 * that memory allocation is now safe.
351 */
352 malta_configuration.mc_mallocsafe = 1;
353
354 /*
355 * Allocate virtual address space for file I/O buffers.
356 * Note they are different than the array of headers, 'buf',
357 * and usually occupy more virtual memory than physical.
358 */
359 size = MAXBSIZE * nbuf;
360 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size),
361 NULL, UVM_UNKNOWN_OFFSET, 0,
362 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
363 UVM_ADV_NORMAL, 0)) != 0)
364 panic("startup: cannot allocate VM for buffers");
365 minaddr = (vaddr_t)buffers;
366 base = bufpages / nbuf;
367 residual = bufpages % nbuf;
368 for (i = 0; i < nbuf; i++) {
369 vsize_t curbufsize;
370 vaddr_t curbuf;
371 struct vm_page *pg;
372
373 /*
374 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
375 * that MAXBSIZE space, we allocate and map (base+1) pages
376 * for the first "residual" buffers, and then we allocate
377 * "base" pages for the rest.
378 */
379 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
380 curbufsize = PAGE_SIZE * ((i < residual) ? (base + 1) : base);
381
382 while (curbufsize) {
383 pg = uvm_pagealloc(NULL, 0, NULL, 0);
384 if (pg == NULL)
385 panic("cpu_startup: not enough memory for "
386 "buffer cache");
387 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
388 VM_PROT_READ|VM_PROT_WRITE);
389 curbuf += PAGE_SIZE;
390 curbufsize -= PAGE_SIZE;
391 }
392 }
393 pmap_update(pmap_kernel());
394
395 /*
396 * Allocate a submap for exec arguments. This map effectively
397 * limits the number of processes exec'ing at any time.
398 */
399 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
400 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
401 /*
402 * Allocate a submap for physio.
403 */
404 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
405 VM_PHYS_SIZE, 0, FALSE, NULL);
406
407 /*
408 * (No need to allocate an mbuf cluster submap. Mbuf clusters
409 * are allocated via the pool allocator, and we use KSEG to
410 * map those pages.)
411 */
412
413 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
414 printf(", %s free", pbuf);
415 format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
416 printf(", %s in %u buffers\n", pbuf, nbuf);
417
418 /*
419 * Set up buffers, so they can be used to read disk labels.
420 */
421 bufinit();
422 }
423
424 int waittime = -1;
425
426 void
427 cpu_reboot(howto, bootstr)
428 int howto;
429 char *bootstr;
430 {
431
432 /* Take a snapshot before clobbering any registers. */
433 if (curproc)
434 savectx((struct user *)curpcb);
435
436 if (cold) {
437 howto |= RB_HALT;
438 goto haltsys;
439 }
440
441 /* If "always halt" was specified as a boot flag, obey. */
442 if (boothowto & RB_HALT)
443 howto |= RB_HALT;
444
445 boothowto = howto;
446 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
447 waittime = 0;
448 vfs_shutdown();
449
450 /*
451 * If we've been adjusting the clock, the todr
452 * will be out of synch; adjust it now.
453 */
454 resettodr();
455 }
456
457 splhigh();
458
459 if (howto & RB_DUMP)
460 dumpsys();
461
462 haltsys:
463 doshutdownhooks();
464
465 if (howto & RB_HALT) {
466 printf("\n");
467 printf("The operating system has halted.\n");
468 printf("Please press any key to reboot.\n\n");
469 cnpollc(1); /* For proper keyboard command handling */
470 cngetc();
471 cnpollc(0);
472 }
473
474 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
475 yamon_exit(boothowto);
476 printf("Oops, back from yamon_exit()\n\nResetting...");
477
478 REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
479
480 /*
481 * Need a small delay here, otherwise we see the first few characters of
482 * the warning below.
483 */
484 delay(80000);
485
486 printf("WARNING: reset failed!\nSpinning...");
487
488 for (;;)
489 /* spin forever */ ; /* XXX */
490 }
491