machdep.c revision 1.20 1 /* $NetBSD: machdep.c,v 1.20 2005/12/21 18:55:33 christos 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.20 2005/12/21 18:55:33 christos 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 int aucomcnrate = 0;
128 #endif /* NAUCOM > 0 */
129
130 #include "ohci.h"
131
132 /* The following are used externally (sysctl_hw). */
133 extern char cpu_model[];
134
135 struct user *proc0paddr;
136
137 /* Our exported CPU info; we can have only one. */
138 struct cpu_info cpu_info_store;
139
140 /* Maps for VM objects. */
141 struct vm_map *exec_map = NULL;
142 struct vm_map *mb_map = NULL;
143 struct vm_map *phys_map = NULL;
144
145 int physmem; /* # pages of physical memory */
146 int maxmem; /* max memory per process */
147
148 int mem_cluster_cnt;
149 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
150
151 yamon_env_var *yamon_envp;
152 struct pb1000_config pb1000_configuration;
153
154 void mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
155
156 void
157 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
158 {
159 struct pb1000_config *pbc = &pb1000_configuration;
160 bus_space_handle_t sh;
161 caddr_t kernend;
162 const char *cp;
163 u_long first, last;
164 caddr_t v;
165 int freqok, howto, i;
166
167 extern char edata[], end[]; /* XXX */
168
169 /* clear the BSS segment */
170 kernend = (caddr_t)mips_round_page(end);
171 memset(edata, 0, kernend - (caddr_t)edata);
172
173 /* set CPU model info for sysctl_hw */
174 strcpy(cpu_model, "Alchemy Semiconductor Pb1000");
175
176 /* save the yamon environment pointer */
177 yamon_envp = envp;
178
179 /* Use YAMON callbacks for early console I/O */
180 cn_tab = &yamon_promcd;
181
182 /*
183 * Set up the exception vectors and CPU-specific function
184 * vectors early on. We need the wbflush() vector set up
185 * before comcnattach() is called (or at least before the
186 * first printf() after that is called).
187 * Sets up mips_cpu_flags that may be queried by other
188 * functions called during startup.
189 * Also clears the I+D caches.
190 */
191 mips_vector_init();
192
193 /*
194 * Set the VM page size.
195 */
196 uvm_setpagesize();
197
198 /*
199 * Use YAMON's CPU frequency if available.
200 */
201 freqok = yamon_setcpufreq(1);
202
203 /*
204 * Initialize bus space tags.
205 */
206 au_cpureg_bus_mem_init(&pbc->pc_cpuregt, pbc);
207 aubus_st = &pbc->pc_cpuregt; /* XXX: for aubus.c */
208
209 /*
210 * Calibrate the timer if YAMON failed to tell us.
211 */
212 if (!freqok) {
213 bus_space_map(&pbc->pc_cpuregt, PC_BASE, PC_SIZE, 0, &sh);
214 au_cal_timers(&pbc->pc_cpuregt, sh);
215 bus_space_unmap(&pbc->pc_cpuregt, sh, PC_SIZE);
216 }
217
218 /*
219 * Bring up the console.
220 */
221 #if NAUCOM > 0
222 #ifdef CONSPEED
223 if (aucomcnrate == 0)
224 aucomcnrate = CONSPEED;
225 #else /* !CONSPEED */
226 /*
227 * Learn default console speed. We use the YAMON environment,
228 * though we could probably also figure it out by checking the
229 * aucom registers directly.
230 */
231 if ((aucomcnrate == 0) && ((cp = yamon_getenv("modetty0")) != NULL))
232 aucomcnrate = strtoul(cp, NULL, 0);
233
234 if (aucomcnrate == 0) {
235 panic("pb1000: The `modetty0' YAMON variable not set. "
236 "Either set it to the speed of the console and try again, "
237 or build a kernel with the `CONSPEED' option.");
238 }
239 #endif /* CONSPEED */
240
241 /*
242 * Delay to allow firmware putchars to complete.
243 * FIFO depth * character time.
244 * character time = (1000000 / (defaultrate / 10))
245 */
246 delay(160000000 / aucomcnrate);
247 if (aucomcnattach(&pbc->pc_cpuregt, UART0_BASE, aucomcnrate,
248 curcpu()->ci_cpu_freq / 4, COM_TYPE_AU1x00,
249 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
250 panic("pb1000: unable to initialize serial console");
251 #else
252 panic("pb1000: not configured to use serial console");
253 #endif /* NAUCOM > 0 */
254
255 /*
256 * Look at arguments passed to us and compute boothowto.
257 */
258 boothowto = RB_AUTOBOOT;
259 #ifdef KADB
260 boothowto |= RB_KDB;
261 #endif
262 for (i = 1; i < argc; i++) {
263 for (cp = argv[i]; *cp; cp++) {
264 /* Ignore superfluous '-', if there is one */
265 if (*cp == '-')
266 continue;
267
268 howto = 0;
269 BOOT_FLAG(*cp, howto);
270 if (! howto)
271 printf("bootflag '%c' not recognised\n", *cp);
272 else
273 boothowto |= howto;
274 }
275 }
276
277 /*
278 * Determine the memory size. Use the `memsize' PMON
279 * variable. If that's not available, panic.
280 *
281 * Note: Reserve the first page! That's where the trap
282 * vectors are located.
283 */
284
285 #if defined(MEMSIZE)
286 memsize = MEMSIZE;
287 #else
288 if (memsize == 0) {
289 if ((cp = yamon_getenv("memsize")) != NULL)
290 memsize = strtoul(cp, NULL, 0);
291 else {
292 printf("FATAL: `memsize' YAMON variable not set. Set it to\n");
293 printf(" the amount of memory (in MB) and try again.\n");
294 printf(" Or, build a kernel with the `MEMSIZE' "
295 "option.\n");
296 panic("pb1000_init");
297 }
298 }
299 #endif /* MEMSIZE */
300 printf("Memory size: 0x%08lx\n", memsize);
301 physmem = btoc(memsize);
302
303 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
304 mem_clusters[mem_cluster_cnt].size =
305 memsize - mem_clusters[mem_cluster_cnt].start;
306 mem_cluster_cnt++;
307
308 /*
309 * Load the rest of the available pages into the VM system.
310 */
311 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
312 last = mem_clusters[0].start + mem_clusters[0].size;
313 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
314 VM_FREELIST_DEFAULT);
315
316 /*
317 * Initialize message buffer (at end of core).
318 */
319 mips_init_msgbuf();
320
321 /*
322 * Initialize the virtual memory system.
323 */
324 pmap_bootstrap();
325
326 /*
327 * Init mapping for u page(s) for proc0.
328 */
329 v = (caddr_t) uvm_pageboot_alloc(USPACE);
330 lwp0.l_addr = proc0paddr = (struct user *)v;
331 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
332 curpcb = &lwp0.l_addr->u_pcb;
333 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
334
335 #if NOHCI > 0
336 {
337 #define USBH_ALL (0x1f<<10) /* All relevant bits in USBH portion of SYS_CLKSRC */
338 /*
339 * Assign a clock for the USB Host controller.
340 */
341 volatile u_int32_t *scsreg, *auxpll;
342 u_int32_t tmp;
343
344 scsreg = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_CLKSRC));
345 auxpll = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_AUXPLL));
346 *auxpll = 8; /* 96Mhz */
347 tmp = *scsreg;
348 tmp &= ~USBH_ALL; /* clear all USBH bits in SYS_CLKSRC first */
349 tmp |= (SCS_DUH|SCS_CUH|SCS_MUH(SCS_MEx_AUX)); /* 48Mhz */
350 *scsreg = tmp;
351 }
352 #endif /* NOHCI */
353 /*
354 * Initialize debuggers, and break into them, if appropriate.
355 */
356 #if NKSYMS || defined(DDB) || defined(LKM)
357 ksyms_init(0, 0, 0);
358 #endif
359 #ifdef DDB
360 if (boothowto & RB_KDB)
361 Debugger();
362 #endif
363 }
364
365 void
366 consinit(void)
367 {
368
369 /*
370 * Everything related to console initialization is done
371 * in mach_init().
372 */
373 }
374
375 void
376 cpu_startup(void)
377 {
378 char pbuf[9];
379 vaddr_t minaddr, maxaddr;
380 #ifdef DEBUG
381 extern int pmapdebug; /* XXX */
382 int opmapdebug = pmapdebug;
383
384 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
385 #endif
386
387 /*
388 * Good {morning,afternoon,evening,night}.
389 */
390 printf("%s%s", copyright, version);
391 printf("%s\n", cpu_model);
392 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
393 printf("total memory = %s\n", pbuf);
394
395 minaddr = 0;
396 /*
397 * Allocate a submap for exec arguments. This map effectively
398 * limits the number of processes exec'ing at any time.
399 */
400 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
401 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
402
403 /*
404 * Allocate a submap for physio
405 */
406 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
407 VM_PHYS_SIZE, 0, FALSE, NULL);
408
409 /*
410 * No need to allocate an mbuf cluster submap. Mbuf clusters
411 * are allocated via the pool allocator, and we use KSEG to
412 * map those pages.
413 */
414
415 #ifdef DEBUG
416 pmapdebug = opmapdebug;
417 #endif
418 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
419 printf("avail memory = %s\n", pbuf);
420 }
421
422 void
423 cpu_reboot(int howto, char *bootstr)
424 {
425 static int waittime = -1;
426
427 /* Take a snapshot before clobbering any registers. */
428 if (curproc)
429 savectx((struct user *)curpcb);
430
431 /* If "always halt" was specified as a boot flag, obey. */
432 if (boothowto & RB_HALT)
433 howto |= RB_HALT;
434
435 boothowto = howto;
436
437 /* If system is cold, just halt. */
438 if (cold) {
439 boothowto |= RB_HALT;
440 goto haltsys;
441 }
442
443 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
444 waittime = 0;
445
446 /*
447 * Synchronize the disks....
448 */
449 vfs_shutdown();
450
451 /*
452 * If we've been adjusting the clock, the todr
453 * will be out of synch; adjust it now.
454 */
455 resettodr();
456 }
457
458 /* Disable interrupts. */
459 splhigh();
460
461 if (boothowto & RB_DUMP)
462 dumpsys();
463
464 haltsys:
465 /* Run any shutdown hooks. */
466 doshutdownhooks();
467
468 #if 1
469 /* XXX
470 * For some reason we are leaving the ethernet MAC in a state where
471 * YAMON isn't happy with it. So just call the reset vector (grr,
472 * Alchemy YAMON doesn't have a "reset" command).
473 */
474 printf("reseting board...\n\n");
475 mips_icache_sync_all();
476 mips_dcache_wbinv_all();
477 asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
478 #else
479 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
480 yamon_exit(boothowto);
481 printf("Oops, back from yamon_exit()\n\nSpinning...");
482 #endif
483 for (;;)
484 /* spin forever */ ; /* XXX */
485 /*NOTREACHED*/
486 }
487