machdep.c revision 1.12 1 /* $NetBSD: machdep.c,v 1.12 2003/09/26 16:00:28 simonb Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, The Mach Operating System project at
10 * Carnegie-Mellon University and Ralph Campbell.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
37 * from: Utah Hdr: machdep.c 1.63 91/04/24
38 */
39 /*
40 * Copyright (c) 1988 University of Utah.
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. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
76 * from: Utah Hdr: machdep.c 1.63 91/04/24
77 */
78
79 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
80 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.12 2003/09/26 16:00:28 simonb Exp $");
81
82 #include "opt_ddb.h"
83 #include "opt_kgdb.h"
84
85 #include "opt_memsize.h"
86 #include "opt_ethaddr.h"
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/buf.h>
92 #include <sys/reboot.h>
93 #include <sys/user.h>
94 #include <sys/mount.h>
95 #include <sys/kcore.h>
96 #include <sys/boot_flag.h>
97 #include <sys/termios.h>
98 #include <sys/ksyms.h>
99
100 #include <net/if.h>
101 #include <net/if_ether.h>
102
103 #include <uvm/uvm_extern.h>
104
105 #include <dev/cons.h>
106
107 #include "ksyms.h"
108
109 #if NKSYMS || defined(DDB) || defined(LKM)
110 #include <machine/db_machdep.h>
111 #include <ddb/db_extern.h>
112 #endif
113
114 #include <mips/cache.h>
115 #include <mips/locore.h>
116 #include <machine/yamon.h>
117
118 #include <evbmips/alchemy/pb1000var.h>
119 #include <mips/alchemy/include/aureg.h>
120 #include <mips/alchemy/include/auvar.h>
121 #include <mips/alchemy/include/aubusvar.h>
122
123 #include "aucom.h"
124 #if NAUCOM > 0
125 #include <mips/alchemy/dev/aucomvar.h>
126
127 #ifndef CONSPEED
128 #define CONSPEED TTYDEF_SPEED
129 #endif
130 int aucomcnrate = CONSPEED;
131 #endif /* NAUCOM > 0 */
132
133 #include "ohci.h"
134
135 /* The following are used externally (sysctl_hw). */
136 extern char cpu_model[];
137
138 struct user *proc0paddr;
139
140 /* Our exported CPU info; we can have only one. */
141 struct cpu_info cpu_info_store;
142
143 /* Maps for VM objects. */
144 struct vm_map *exec_map = NULL;
145 struct vm_map *mb_map = NULL;
146 struct vm_map *phys_map = NULL;
147
148 int physmem; /* # pages of physical memory */
149 int maxmem; /* max memory per process */
150
151 int mem_cluster_cnt;
152 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
153
154 yamon_env_var *yamon_envp;
155 struct pb1000_config pb1000_configuration;
156
157 void mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
158
159 void
160 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
161 {
162 struct pb1000_config *pbc = &pb1000_configuration;
163 bus_space_handle_t sh;
164 caddr_t kernend;
165 const char *cp;
166 u_long first, last;
167 caddr_t v;
168 int howto, i;
169
170 extern char edata[], end[]; /* XXX */
171
172 /* clear the BSS segment */
173 kernend = (caddr_t)mips_round_page(end);
174 memset(edata, 0, kernend - (caddr_t)edata);
175
176 /* set cpu model info for sysctl_hw */
177 strcpy(cpu_model, "Alchemy Semiconductor Pb1000");
178
179 /* save the yamon environment pointer */
180 yamon_envp = envp;
181
182 /* Use YAMON callbacks for early console I/O */
183 cn_tab = &yamon_promcd;
184
185 /*
186 * Set up the exception vectors and cpu-specific function
187 * vectors early on. We need the wbflush() vector set up
188 * before comcnattach() is called (or at least before the
189 * first printf() after that is called).
190 * Also clears the I+D caches.
191 */
192 mips_vector_init();
193
194 /*
195 * Set the VM page size.
196 */
197 uvm_setpagesize();
198
199 /*
200 * Initialize bus space tags.
201 */
202 au_cpureg_bus_mem_init(&pbc->pc_cpuregt, pbc);
203 aubus_st = &pbc->pc_cpuregt; /* XXX: for aubus.c */
204
205 /*
206 * Calibrate the timer, delay() relies on this.
207 */
208 bus_space_map(&pbc->pc_cpuregt, PC_BASE, PC_SIZE, 0, &sh);
209 au_cal_timers(&pbc->pc_cpuregt, sh);
210 bus_space_unmap(&pbc->pc_cpuregt, sh, PC_SIZE);
211
212 /*
213 * Bring up the console.
214 */
215 #if NAUCOM > 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 / aucomcnrate);
222 if (aucomcnattach(&pbc->pc_cpuregt, UART0_BASE, aucomcnrate,
223 curcpu()->ci_cpu_freq / 4, COM_TYPE_NORMAL,
224 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
225 panic("pb1000: unable to initialize serial console");
226 #else
227 panic("pb1000: not configured to use serial console");
228 #endif /* NAUCOM > 0 */
229
230 /*
231 * Look at arguments passed to us and compute boothowto.
232 */
233 boothowto = RB_AUTOBOOT;
234 #ifdef KADB
235 boothowto |= RB_KDB;
236 #endif
237 for (i = 1; i < argc; i++) {
238 for (cp = argv[i]; *cp; cp++) {
239 /* Ignore superfluous '-', if there is one */
240 if (*cp == '-')
241 continue;
242
243 howto = 0;
244 BOOT_FLAG(*cp, howto);
245 if (! howto)
246 printf("bootflag '%c' not recognised\n", *cp);
247 else
248 boothowto |= howto;
249 }
250 }
251
252 /*
253 * Determine the memory size. Use the `memsize' PMON
254 * variable. If that's not available, panic.
255 *
256 * Note: Reserve the first page! That's where the trap
257 * vectors are located.
258 */
259
260 #if defined(MEMSIZE)
261 memsize = MEMSIZE;
262 #else
263 if (memsize == 0) {
264 if ((cp = yamon_getenv("memsize")) != NULL)
265 memsize = strtoul(cp, NULL, 0);
266 else {
267 printf("FATAL: `memsize' YAMON variable not set. Set it to\n");
268 printf(" the amount of memory (in MB) and try again.\n");
269 printf(" Or, build a kernel with the `MEMSIZE' "
270 "option.\n");
271 panic("pb1000_init");
272 }
273 }
274 #endif /* MEMSIZE */
275 printf("Memory size: 0x%08lx\n", memsize);
276 physmem = btoc(memsize);
277
278 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
279 mem_clusters[mem_cluster_cnt].size =
280 memsize - mem_clusters[mem_cluster_cnt].start;
281 mem_cluster_cnt++;
282
283 /*
284 * Load the rest of the available pages into the VM system.
285 */
286 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
287 last = mem_clusters[0].start + mem_clusters[0].size;
288 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
289 VM_FREELIST_DEFAULT);
290
291 /*
292 * Initialize message buffer (at end of core).
293 */
294 mips_init_msgbuf();
295
296 /*
297 * Compute the size of system data structures. pmap_bootstrap()
298 * needs some of this information.
299 */
300 memsize = (u_long)allocsys(NULL, NULL);
301
302 /*
303 * Initialize the virtual memory system.
304 */
305 pmap_bootstrap();
306
307 /*
308 * Init mapping for u page(s) for proc0.
309 */
310 v = (caddr_t) uvm_pageboot_alloc(USPACE);
311 lwp0.l_addr = proc0paddr = (struct user *)v;
312 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
313 curpcb = &lwp0.l_addr->u_pcb;
314 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
315
316 /*
317 * Allocate space for system data structures. These data structures
318 * are allocated here instead of cpu_startup() because physical
319 * memory is directly addressable. We don't have to map these into
320 * the virtual address space.
321 */
322 v = (caddr_t)uvm_pageboot_alloc(memsize);
323 if ((allocsys(v, NULL) - v) != memsize)
324 panic("mach_init: table size inconsistency");
325
326 #if NOHCI > 0
327 {
328 #define USBH_ALL (0x1f<<10) /* All relevant bits in USBH portion of SYS_CLKSRC */
329 /*
330 * Assign a clock for the USB Host controller.
331 */
332 volatile u_int32_t *scsreg;
333 u_int32_t tmp;
334
335 scsreg = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_CLKSRC));
336 #if 0
337 auxpll = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_AUXPLL));
338 *auxpll = 8; /* 96Mhz */
339 tmp = *sfc0;
340 tmp |= SFC_FE0|SFC_FRDIV0;
341 *sfc0 = tmp;
342 #endif
343 tmp = *scsreg;
344 tmp &= ~USBH_ALL; /* clear all USBH bits in SYS_CLKSRC first */
345 tmp |= (SCS_DUH|SCS_CUH|SCS_MUH(SCS_MEx_AUX)); /* 48Mhz */
346 *scsreg = tmp;
347 }
348 #endif /* NOHCI */
349 /*
350 * Initialize debuggers, and break into them, if appropriate.
351 */
352 #if NKSYMS || defined(DDB) || defined(LKM)
353 ksyms_init(0, 0, 0);
354 #endif
355 #ifdef DDB
356 if (boothowto & RB_KDB)
357 Debugger();
358 #endif
359 }
360
361 void
362 consinit(void)
363 {
364
365 /*
366 * Everything related to console initialization is done
367 * in mach_init().
368 */
369 }
370
371 void
372 cpu_startup(void)
373 {
374 char pbuf[9];
375 vaddr_t minaddr, maxaddr;
376 vsize_t size;
377 u_int i, base, residual;
378 #ifdef DEBUG
379 extern int pmapdebug; /* XXX */
380 int opmapdebug = pmapdebug;
381
382 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
383 #endif
384
385 /*
386 * Good {morning,afternoon,evening,night}.
387 */
388 printf(version);
389 printf("%s\n", cpu_model);
390 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
391 printf("total memory = %s\n", pbuf);
392
393 /*
394 * Allocate virtual address space for file I/O buffers.
395 * Note they are different than the array of headers, 'buf',
396 * and usually occupy more virtual memory than physical.
397 */
398 size = MAXBSIZE * nbuf;
399 if (uvm_map(kernel_map, (vaddr_t *)(void *)&buffers, round_page(size),
400 NULL, UVM_UNKNOWN_OFFSET, 0,
401 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
402 UVM_ADV_NORMAL, 0)) != 0)
403 panic("cpu_startup: cannot allocate VM for buffers");
404
405 minaddr = (vaddr_t)buffers;
406 if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
407 bufpages = btoc(MAXBSIZE) * nbuf; /* do not overallocate RAM */
408 }
409 base = bufpages / nbuf;
410 residual = bufpages % nbuf;
411
412 /* now allocate RAM for buffers */
413 for (i = 0; i < nbuf; i++) {
414 vsize_t curbufsize;
415 vaddr_t curbuf;
416 struct vm_page *pg;
417
418 /*
419 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
420 * that MAXBSIZE space, we allocate and map (base+1) pages
421 * for the first "residual" buffers, and then we allocate
422 * "base" pages for the rest.
423 */
424 curbuf = (vaddr_t)buffers + (i * MAXBSIZE);
425 curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
426
427 while (curbufsize) {
428 pg = uvm_pagealloc(NULL, 0, NULL, 0);
429 if (pg == NULL)
430 panic("cpu_startup: not enough memory for "
431 "buffer cache");
432 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
433 VM_PROT_READ|VM_PROT_WRITE);
434 curbuf += PAGE_SIZE;
435 curbufsize -= PAGE_SIZE;
436 }
437 }
438 pmap_update(pmap_kernel());
439
440 /*
441 * Allocate a submap for exec arguments. This map effectively
442 * limits the number of processes exec'ing at any time.
443 */
444 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
445 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
446
447 /*
448 * Allocate a submap for physio
449 */
450 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
451 VM_PHYS_SIZE, 0, FALSE, NULL);
452
453 /*
454 * No need to allocate an mbuf cluster submap. Mbuf clusters
455 * are allocated via the pool allocator, and we use KSEG to
456 * map those pages.
457 */
458
459 #ifdef DEBUG
460 pmapdebug = opmapdebug;
461 #endif
462 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
463 printf("avail memory = %s\n", pbuf);
464 format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
465 printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
466
467 /*
468 * Set up buffers, so they can be used to read disklabels.
469 */
470 bufinit();
471 }
472
473 void
474 cpu_reboot(int howto, char *bootstr)
475 {
476 static int waittime = -1;
477
478 /* Take a snapshot before clobbering any registers. */
479 if (curproc)
480 savectx((struct user *)curpcb);
481
482 /* If "always halt" was specified as a boot flag, obey. */
483 if (boothowto & RB_HALT)
484 howto |= RB_HALT;
485
486 boothowto = howto;
487
488 /* If system is cold, just halt. */
489 if (cold) {
490 boothowto |= RB_HALT;
491 goto haltsys;
492 }
493
494 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
495 waittime = 0;
496
497 /*
498 * Synchronize the disks....
499 */
500 vfs_shutdown();
501
502 /*
503 * If we've been adjusting the clock, the todr
504 * will be out of synch; adjust it now.
505 */
506 resettodr();
507 }
508
509 /* Disable interrupts. */
510 splhigh();
511
512 if (boothowto & RB_DUMP)
513 dumpsys();
514
515 haltsys:
516 /* Run any shutdown hooks. */
517 doshutdownhooks();
518
519 #if 1
520 /* XXX
521 * For some reason we are leaving the ethernet MAC in a state where
522 * YAMON isn't happy with it. So just call the reset vector (grr,
523 * Alchemy YAMON doesn't have a "reset" command).
524 */
525 printf("reseting board...\n\n");
526 mips_icache_sync_all();
527 mips_dcache_wbinv_all();
528 asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
529 #else
530 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
531 yamon_exit(boothowto);
532 printf("Oops, back from yamon_exit()\n\nSpinning...");
533 #endif
534 for (;;)
535 /* spin forever */ ; /* XXX */
536 /*NOTREACHED*/
537 }
538