machdep.c revision 1.38.6.1 1 /* $NetBSD: machdep.c,v 1.38.6.1 2011/02/17 11:59:37 bouyer 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. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
73 * from: Utah Hdr: machdep.c 1.63 91/04/24
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.38.6.1 2011/02/17 11:59:37 bouyer Exp $");
78
79 #include "opt_ddb.h"
80 #include "opt_execfmt.h"
81 #include "opt_modular.h"
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/buf.h>
87 #include <sys/reboot.h>
88 #include <sys/mount.h>
89 #include <sys/kcore.h>
90 #include <sys/boot_flag.h>
91 #include <sys/termios.h>
92 #include <sys/ksyms.h>
93 #include <sys/device.h>
94
95 #include <uvm/uvm_extern.h>
96
97 #include <dev/cons.h>
98
99 #include "ksyms.h"
100
101 #if NKSYMS || defined(DDB) || defined(MODULAR)
102 #include <machine/db_machdep.h>
103 #include <ddb/db_extern.h>
104 #endif
105
106 #include <machine/cpu.h>
107 #include <machine/psl.h>
108 #include <machine/yamon.h>
109
110 #include <evbmips/malta/autoconf.h>
111 #include <evbmips/malta/maltareg.h>
112 #include <evbmips/malta/maltavar.h>
113
114 #include "com.h"
115 #if NCOM > 0
116 #include <dev/ic/comreg.h>
117 #include <dev/ic/comvar.h>
118
119 int comcnrate = 38400; /* XXX should be config option */
120 #endif /* NCOM > 0 */
121
122
123 #define REGVAL(x) *((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
124
125 struct malta_config malta_configuration;
126
127 /* For sysctl_hw. */
128 extern char cpu_model[];
129
130 /* Our exported CPU info; we can have only one. */
131 struct cpu_info cpu_info_store;
132
133 /* Maps for VM objects. */
134 struct vm_map *phys_map = NULL;
135
136 int physmem; /* Total physical memory */
137
138 int netboot; /* Are we netbooting? */
139
140 yamon_env_var *yamon_envp;
141
142 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
143 int mem_cluster_cnt;
144
145 void configure(void);
146 void mach_init(int, char **, yamon_env_var *, u_long);
147
148 /*
149 * safepri is a safe priority for sleep to set for a spin-wait during
150 * autoconfiguration or after a panic. Used as an argument to splx().
151 */
152 int safepri = MIPS1_PSL_LOWIPL;
153
154 /*
155 * Do all the stuff that locore normally does before calling main().
156 */
157 void
158 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
159 {
160 struct malta_config *mcp = &malta_configuration;
161 bus_space_handle_t sh;
162 void *kernend;
163 u_long first, last;
164 int freqok;
165 uint8_t *brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
166
167 extern char edata[], end[];
168
169 CTASSERT((intptr_t)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES) < 0);
170
171 *brkres = 0; /* Disable BREAK==reset on console */
172
173 /* Get the propaganda in early! */
174 led_display_str("NetBSD");
175
176 /*
177 * Clear the BSS segment.
178 */
179 kernend = (void *)mips_round_page(end);
180 memset(edata, 0, (char *)kernend - edata);
181
182 /* save the yamon environment pointer */
183 yamon_envp = envp;
184
185 /* Use YAMON callbacks for early console I/O */
186 cn_tab = &yamon_promcd;
187
188 /*
189 * Set up the exception vectors and CPU-specific function
190 * vectors early on. We need the wbflush() vector set up
191 * before comcnattach() is called (or at least before the
192 * first printf() after that is called).
193 * Also clears the I+D caches.
194 */
195 mips_vector_init();
196
197 /* set the VM page size */
198 uvm_setpagesize();
199
200 physmem = btoc(memsize);
201
202 /*
203 * Use YAMON's CPU frequency if available.
204 */
205 freqok = yamon_setcpufreq(1);
206
207 gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
208 malta_bus_io_init(&mcp->mc_iot, mcp);
209 malta_bus_mem_init(&mcp->mc_memt, mcp);
210 malta_dma_init(mcp);
211
212 /*
213 * Calibrate the timer if YAMON failed to tell us.
214 */
215 if (!freqok) {
216 bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
217 malta_cal_timer(&mcp->mc_iot, sh);
218 bus_space_unmap(&mcp->mc_iot, sh, 2);
219 }
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 mem_clusters[0].start = 0;
237 mem_clusters[0].size = ctob(physmem);
238 mem_cluster_cnt = 1;
239
240 strcpy(cpu_model, "MIPS Malta Evaluation Board");
241
242 /*
243 * XXX: check argv[0] - do something if "gdb"???
244 */
245
246 /*
247 * Look at arguments passed to us and compute boothowto.
248 */
249 boothowto = RB_AUTOBOOT;
250 #ifndef _LP64
251 for (int i = 1; i < argc; i++) {
252 for (char *cp = argv[i]; *cp; cp++) {
253 int howto;
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 #endif
267
268 /*
269 * Load the rest of the available pages into the VM system.
270 */
271 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
272 last = mem_clusters[0].start + mem_clusters[0].size;
273 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
274 VM_FREELIST_DEFAULT);
275
276 /*
277 * Initialize error message buffer (at end of core).
278 */
279 mips_init_msgbuf();
280
281 pmap_bootstrap();
282
283 /*
284 * Allocate uarea page for lwp0 and set it.
285 */
286 mips_init_lwp0_uarea();
287
288 /*
289 * Initialize debuggers, and break into them, if appropriate.
290 */
291 #if defined(DDB)
292 if (boothowto & RB_KDB)
293 Debugger();
294 #endif
295 }
296
297 void
298 consinit(void)
299 {
300
301 /*
302 * Everything related to console initialization is done
303 * in mach_init().
304 */
305 }
306
307 /*
308 * Allocate memory for variable-sized tables,
309 */
310 void
311 cpu_startup(void)
312 {
313 vaddr_t minaddr, maxaddr;
314 char pbuf[9];
315
316 /*
317 * Good {morning,afternoon,evening,night}.
318 */
319 printf("%s%s", copyright, version);
320 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
321 printf("total memory = %s\n", pbuf);
322
323 /*
324 * Virtual memory is bootstrapped -- notify the bus spaces
325 * that memory allocation is now safe.
326 */
327 malta_configuration.mc_mallocsafe = 1;
328
329 minaddr = 0;
330 /*
331 * Allocate a submap for physio.
332 */
333 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
334 VM_PHYS_SIZE, 0, FALSE, NULL);
335
336 /*
337 * (No need to allocate an mbuf cluster submap. Mbuf clusters
338 * are allocated via the pool allocator, and we use KSEG to
339 * map those pages.)
340 */
341
342 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
343 printf("avail memory = %s\n", pbuf);
344 }
345
346 int waittime = -1;
347
348 void
349 cpu_reboot(int howto, char *bootstr)
350 {
351
352 /* Take a snapshot before clobbering any registers. */
353 if (curproc)
354 savectx(curpcb);
355
356 if (cold) {
357 howto |= RB_HALT;
358 goto haltsys;
359 }
360
361 /* If "always halt" was specified as a boot flag, obey. */
362 if (boothowto & RB_HALT)
363 howto |= RB_HALT;
364
365 boothowto = howto;
366 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
367 waittime = 0;
368 vfs_shutdown();
369
370 /*
371 * If we've been adjusting the clock, the todr
372 * will be out of synch; adjust it now.
373 */
374 resettodr();
375 }
376
377 splhigh();
378
379 if (howto & RB_DUMP)
380 dumpsys();
381
382 haltsys:
383 doshutdownhooks();
384
385 pmf_system_shutdown(boothowto);
386
387 if (howto & RB_HALT) {
388 printf("\n");
389 printf("The operating system has halted.\n");
390 printf("Please press any key to reboot.\n\n");
391 cnpollc(1); /* For proper keyboard command handling */
392 cngetc();
393 cnpollc(0);
394 }
395
396 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
397 yamon_exit(boothowto);
398 printf("Oops, back from yamon_exit()\n\nResetting...");
399
400 REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
401
402 /*
403 * Need a small delay here, otherwise we see the first few characters of
404 * the warning below.
405 */
406 delay(80000);
407
408 printf("WARNING: reset failed!\nSpinning...");
409
410 for (;;)
411 /* spin forever */ ; /* XXX */
412 }
413