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