machdep.c revision 1.28.10.10 1 /* machdep.c,v 1.28.10.9 2011/04/29 08:26:18 matt 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) 1992, 1993
40 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
72 * from: Utah Hdr: machdep.c 1.63 91/04/24
73 */
74 /*
75 * Copyright (c) 1988 University of Utah.
76 *
77 * This code is derived from software contributed to Berkeley by
78 * the Systems Programming Group of the University of Utah Computer
79 * Science Department, The Mach Operating System project at
80 * Carnegie-Mellon University and Ralph Campbell.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the University of
93 * California, Berkeley and its contributors.
94 * 4. Neither the name of the University nor the names of its contributors
95 * may be used to endorse or promote products derived from this software
96 * without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
111 * from: Utah Hdr: machdep.c 1.63 91/04/24
112 */
113
114 #include <sys/cdefs.h>
115 __KERNEL_RCSID(0, "machdep.c,v 1.28.10.9 2011/04/29 08:26:18 matt Exp");
116
117 #include "opt_ddb.h"
118 #include "opt_execfmt.h"
119
120 #include <sys/param.h>
121 #include <sys/systm.h>
122 #include <sys/kernel.h>
123 #include <sys/buf.h>
124 #include <sys/reboot.h>
125 #include <sys/user.h>
126 #include <sys/mount.h>
127 #include <sys/kcore.h>
128 #include <sys/boot_flag.h>
129 #include <sys/termios.h>
130 #include <sys/ksyms.h>
131 #include <sys/lwp.h>
132
133 #include <uvm/uvm_extern.h>
134
135 #include <dev/cons.h>
136
137 #include "ksyms.h"
138
139 #if NKSYMS || defined(DDB) || defined(LKM)
140 #include <machine/db_machdep.h>
141 #include <ddb/db_extern.h>
142 #endif
143
144 #include <machine/cpu.h>
145 #include <machine/psl.h>
146 #include <machine/yamon.h>
147
148 #include <mips/locore.h>
149
150 #include <evbmips/malta/autoconf.h>
151 #include <evbmips/malta/maltareg.h>
152 #include <evbmips/malta/maltavar.h>
153
154 #include "com.h"
155 #if NCOM > 0
156 #include <dev/ic/comreg.h>
157 #include <dev/ic/comvar.h>
158
159 int comcnrate = 38400; /* XXX should be config option */
160 #endif /* NCOM > 0 */
161
162
163 #define REGVAL(x) *((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
164
165 struct malta_config malta_configuration;
166
167 /* For sysctl_hw. */
168 extern char cpu_model[];
169
170 /* Maps for VM objects. */
171 struct vm_map *mb_map = NULL;
172 struct vm_map *phys_map = NULL;
173
174 int physmem; /* Total physical memory */
175
176 int netboot; /* Are we netbooting? */
177
178 yamon_env_var *yamon_envp;
179
180 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
181 u_int mem_cluster_cnt;
182
183 void configure(void);
184 void mach_init(int, char **, yamon_env_var *, u_long);
185
186 /*
187 * Do all the stuff that locore normally does before calling main().
188 */
189 void
190 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
191 {
192 struct malta_config *mcp = &malta_configuration;
193 uint8_t * const brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
194 bus_space_handle_t sh;
195 void *kernend;
196 int freqok;
197
198 extern char edata[], end[];
199
200 CTASSERT((intptr_t)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES) < 0);
201
202 *brkres = 0; /* Disable BREAK==reset on console */
203
204 /* Get the propaganda in early! */
205 led_display_str("NetBSD");
206
207 /*
208 * Clear the BSS segment.
209 */
210 kernend = (void *)mips_round_page(end);
211 memset(edata, 0, (char *)kernend - edata);
212
213 /* save the yamon environment pointer */
214 yamon_envp = envp;
215
216 /* Use YAMON callbacks for early console I/O */
217 cn_tab = &yamon_promcd;
218
219 /*
220 * Set up the exception vectors and CPU-specific function
221 * vectors early on. We need the wbflush() vector set up
222 * before comcnattach() is called (or at least before the
223 * first printf() after that is called).
224 * Also clears the I+D caches.
225 */
226 mips_vector_init(NULL, false);
227
228 /* set the VM page size */
229 uvm_setpagesize();
230
231 physmem = btoc(memsize);
232
233 /*
234 * Use YAMON's CPU frequency if available.
235 */
236 freqok = yamon_setcpufreq(1);
237
238 gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
239 malta_bus_io_init(&mcp->mc_iot, mcp);
240 malta_bus_mem_init(&mcp->mc_memt, mcp);
241 malta_dma_init(mcp);
242
243 /*
244 * Calibrate the timer if YAMON failed to tell us.
245 */
246 if (!freqok) {
247 bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
248 malta_cal_timer(&mcp->mc_iot, sh);
249 bus_space_unmap(&mcp->mc_iot, sh, 2);
250 }
251
252 #if NCOM > 0
253 /*
254 * Delay to allow firmware putchars to complete.
255 * FIFO depth * character time.
256 * character time = (1000000 / (defaultrate / 10))
257 */
258 delay(160000000 / comcnrate);
259 if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
260 COM_FREQ, COM_TYPE_NORMAL,
261 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
262 panic("malta: unable to initialize serial console");
263 #else
264 panic("malta: not configured to use serial console");
265 #endif /* NCOM > 0 */
266
267 mem_clusters[0].start = 0;
268 mem_clusters[0].size = ctob(physmem);
269 mem_cluster_cnt = 1;
270
271 strcpy(cpu_model, "MIPS Malta Evaluation Board");
272
273 /*
274 * XXX: check argv[0] - do something if "gdb"???
275 */
276
277 /*
278 * Look at arguments passed to us and compute boothowto.
279 */
280 boothowto = RB_AUTOBOOT;
281 #ifndef _LP64
282 for (int i = 1; i < argc; i++) {
283 for (char *cp = argv[i]; *cp; cp++) {
284 int howto;
285 /* Ignore superfluous '-', if there is one */
286 if (*cp == '-')
287 continue;
288
289 howto = 0;
290 BOOT_FLAG(*cp, howto);
291 if (! howto)
292 printf("bootflag '%c' not recognised\n", *cp);
293 else
294 boothowto |= howto;
295 }
296 }
297 #endif
298
299 /*
300 * Load the rest of the available pages into the VM system.
301 */
302 mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
303 mem_clusters, mem_cluster_cnt, NULL, 0);
304
305 /*
306 * Initialize error message buffer (at end of core).
307 */
308 mips_init_msgbuf();
309
310 pmap_bootstrap();
311
312 /*
313 * Allocate space for proc0's USPACE.
314 */
315 mips_init_lwp0_uarea();
316
317 /*
318 * Initialize debuggers, and break into them, if appropriate.
319 */
320 #if NKSYMS || defined(DDB) || defined(LKM)
321 ksyms_init(0, 0, 0);
322 #endif
323
324 #if defined(DDB)
325 if (boothowto & RB_KDB)
326 Debugger();
327 #endif
328
329 #ifdef MULTIPROCESSOR
330 /*
331 * We can never be running on more than one processor but we can dream.
332 */
333 mips_fixup_exceptions(mips_fixup_zero_relative);
334 #endif
335 }
336
337 void
338 consinit(void)
339 {
340
341 /*
342 * Everything related to console initialization is done
343 * in mach_init().
344 */
345 }
346
347 /*
348 * Allocate memory for variable-sized tables,
349 */
350 void
351 cpu_startup()
352 {
353 /*
354 * Do the common startup items.
355 */
356 cpu_startup_common();
357
358 /*
359 * Virtual memory is bootstrapped -- notify the bus spaces
360 * that memory allocation is now safe.
361 */
362 malta_configuration.mc_mallocsafe = 1;
363 }
364
365 int waittime = -1;
366
367 void
368 cpu_reboot(howto, bootstr)
369 int howto;
370 char *bootstr;
371 {
372
373 /* Take a snapshot before clobbering any registers. */
374 savectx(lwp_getpcb(curlwp));
375
376 if (cold) {
377 howto |= RB_HALT;
378 goto haltsys;
379 }
380
381 /* If "always halt" was specified as a boot flag, obey. */
382 if (boothowto & RB_HALT)
383 howto |= RB_HALT;
384
385 boothowto = howto;
386 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
387 waittime = 0;
388 vfs_shutdown();
389
390 /*
391 * If we've been adjusting the clock, the todr
392 * will be out of synch; adjust it now.
393 */
394 resettodr();
395 }
396
397 splhigh();
398
399 if (howto & RB_DUMP)
400 dumpsys();
401
402 haltsys:
403 doshutdownhooks();
404
405 if (howto & RB_HALT) {
406 printf("\n");
407 printf("The operating system has halted.\n");
408 printf("Please press any key to reboot.\n\n");
409 cnpollc(1); /* For proper keyboard command handling */
410 cngetc();
411 cnpollc(0);
412 }
413
414 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
415 yamon_exit(boothowto);
416 printf("Oops, back from yamon_exit()\n\nResetting...");
417
418 REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
419
420 /*
421 * Need a small delay here, otherwise we see the first few characters of
422 * the warning below.
423 */
424 delay(80000);
425
426 printf("WARNING: reset failed!\nSpinning...");
427
428 for (;;)
429 /* spin forever */ ; /* XXX */
430 }
431