machdep.c revision 1.24 1 /* $NetBSD: machdep.c,v 1.24 2011/02/08 20:20:12 rmind Exp $ */
2
3 /*
4 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 * Copyright (c) 2006 Garrett D'Amore.
6 * All rights reserved.
7 *
8 * Portions of this code were written by Garrett D'Amore for the
9 * Champaign-Urbana Community Wireless Network Project.
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgements:
22 * This product includes software developed by the Urbana-Champaign
23 * Independent Media Center.
24 * This product includes software developed by Garrett D'Amore.
25 * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 * D'Amore's name may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43 /*-
44 * Copyright (c) 2006 Itronix Inc.
45 * All rights reserved.
46 *
47 * Portions written by Garrett D'Amore for Itronix Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. The name of Itronix Inc. may not be used to endorse
58 * or promote products derived from this software without specific
59 * prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
65 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
68 * ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 * POSSIBILITY OF SUCH DAMAGE.
72 */
73
74 /*
75 * Copyright (c) 1988 University of Utah.
76 * Copyright (c) 1992, 1993
77 * The Regents of the University of California. All rights reserved.
78 *
79 * This code is derived from software contributed to Berkeley by
80 * the Systems Programming Group of the University of Utah Computer
81 * Science Department, The Mach Operating System project at
82 * Carnegie-Mellon University and Ralph Campbell.
83 *
84 * Redistribution and use in source and binary forms, with or without
85 * modification, are permitted provided that the following conditions
86 * are met:
87 * 1. Redistributions of source code must retain the above copyright
88 * notice, this list of conditions and the following disclaimer.
89 * 2. Redistributions in binary form must reproduce the above copyright
90 * notice, this list of conditions and the following disclaimer in the
91 * documentation and/or other materials provided with the distribution.
92 * 3. Neither the name of the University nor the names of its contributors
93 * may be used to endorse or promote products derived from this software
94 * without specific prior written permission.
95 *
96 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
97 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
99 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
100 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
101 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
102 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
104 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
105 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106 * SUCH DAMAGE.
107 *
108 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
109 * from: Utah Hdr: machdep.c 1.63 91/04/24
110 */
111
112 #include <sys/cdefs.h>
113 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.24 2011/02/08 20:20:12 rmind Exp $");
114
115 #include "opt_ddb.h"
116 #include "opt_kgdb.h"
117 #include "opt_modular.h"
118
119 #include <sys/param.h>
120 #include <sys/systm.h>
121 #include <sys/kernel.h>
122 #include <sys/buf.h>
123 #include <sys/reboot.h>
124 #include <sys/mount.h>
125 #include <sys/kcore.h>
126 #include <sys/boot_flag.h>
127 #include <sys/termios.h>
128 #include <sys/ksyms.h>
129 #include <sys/device.h>
130
131 #include <uvm/uvm_extern.h>
132
133 #include <dev/cons.h>
134
135 #include "ksyms.h"
136
137 #if NKSYMS || defined(DDB) || defined(MODULAR)
138 #include <machine/db_machdep.h>
139 #include <ddb/db_extern.h>
140 #endif
141
142 #include <mips/cache.h>
143 #include <mips/locore.h>
144 #include <mips/cpuregs.h>
145
146 #include <mips/atheros/include/ar531xvar.h>
147 #include <mips/atheros/include/arbusvar.h>
148
149 /* Our exported CPU info; we can have only one. */
150 struct cpu_info cpu_info_store;
151
152 /* Maps for VM objects. */
153 struct vm_map *phys_map = NULL;
154
155 int maxmem; /* max memory per process */
156
157 int mem_cluster_cnt;
158 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
159
160 void mach_init(void); /* XXX */
161
162 static void
163 cal_timer(void)
164 {
165 uint32_t cntfreq;
166
167 cntfreq = curcpu()->ci_cpu_freq = ar531x_cpu_freq();
168
169 /* MIPS 4Kc CP0 counts every other clock */
170 if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
171 cntfreq /= 2;
172
173 curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
174
175 /* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
176 curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
177 }
178
179 void
180 mach_init(void)
181 {
182 void *kernend;
183 u_long first, last;
184 uint32_t memsize;
185
186 extern char edata[], end[]; /* XXX */
187
188 /* clear the BSS segment */
189 kernend = (void *)mips_round_page(end);
190
191 memset(edata, 0, (char *)kernend - edata);
192
193 /* setup early console */
194 ar531x_early_console();
195
196 /* set CPU model info for sysctl_hw */
197 snprintf(cpu_model, 64, "%s", ar531x_cpuname());
198
199 /*
200 * Set up the exception vectors and CPU-specific function
201 * vectors early on. We need the wbflush() vector set up
202 * before comcnattach() is called (or at least before the
203 * first printf() after that is called).
204 * Sets up mips_cpu_flags that may be queried by other
205 * functions called during startup.
206 * Also clears the I+D caches.
207 */
208 mips_vector_init();
209
210 /*
211 * Calibrate timers.
212 */
213 cal_timer();
214
215 /*
216 * Set the VM page size.
217 */
218 uvm_setpagesize();
219
220 /*
221 * Look at arguments passed to us and compute boothowto.
222 */
223 boothowto = RB_AUTOBOOT;
224 #ifdef KADB
225 boothowto |= RB_KDB;
226 #endif
227
228 /*
229 * This would be a good place to parse a boot command line, if
230 * we got one from the bootloader. Right now we have no way to
231 * get one from e.g. vxworks.
232 */
233
234 /*
235 * Determine the memory size.
236 *
237 * Note: Reserve the first page! That's where the trap
238 * vectors are located.
239 */
240 memsize = ar531x_memsize();
241
242 printf("Memory size: 0x%08x\n", memsize);
243 physmem = btoc(memsize);
244
245 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
246 mem_clusters[mem_cluster_cnt].size =
247 memsize - mem_clusters[mem_cluster_cnt].start;
248 mem_cluster_cnt++;
249
250 /*
251 * Load the rest of the available pages into the VM system.
252 */
253 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
254 last = mem_clusters[0].start + mem_clusters[0].size;
255 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
256 VM_FREELIST_DEFAULT);
257
258 /*
259 * Initialize message buffer (at end of core).
260 */
261 mips_init_msgbuf();
262
263 /*
264 * Initialize the virtual memory system.
265 */
266 pmap_bootstrap();
267
268 /*
269 * Allocate uarea page for lwp0 and set it.
270 */
271 mips_init_lwp0_uarea();
272
273 /*
274 * Initialize busses.
275 */
276 ar531x_businit();
277
278 /*
279 * Turn off (ignore) the hardware watchdog. If we got this
280 * far, then we shouldn't need it anymore.
281 */
282 ar531x_wdog(0);
283
284 /*
285 * Turn off watchpoint that may have been enabled by the
286 * PROM. VxWorks bootloader seems to leave one set.
287 */
288 __asm volatile (
289 "mtc0 $0, $" ___STRING(MIPS_COP_0_WATCH_LO) " \n\t"
290 "nop\n\t"
291 "nop\n\t");
292
293 /*
294 * Initialize debuggers, and break into them, if appropriate.
295 */
296 #ifdef DDB
297 if (boothowto & RB_KDB)
298 Debugger();
299 #endif
300 }
301
302 void
303 consinit(void)
304 {
305
306 /*
307 * Everything related to console initialization is done
308 * in mach_init().
309 */
310 ar531x_consinit();
311 }
312
313 void
314 cpu_startup(void)
315 {
316 char pbuf[9];
317 vaddr_t minaddr, maxaddr;
318 #ifdef DEBUG
319 extern int pmapdebug; /* XXX */
320 int opmapdebug = pmapdebug;
321
322 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
323 #endif
324
325 /*
326 * Good {morning,afternoon,evening,night}.
327 */
328 printf("%s%s", copyright, version);
329 printf("%s\n", cpu_model);
330 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
331 printf("total memory = %s\n", pbuf);
332
333 minaddr = 0;
334 /*
335 * Allocate a submap for physio
336 */
337 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
338 VM_PHYS_SIZE, 0, FALSE, NULL);
339
340 /*
341 * No need to allocate an mbuf cluster submap. Mbuf clusters
342 * are allocated via the pool allocator, and we use KSEG to
343 * map those pages.
344 */
345
346 #ifdef DEBUG
347 pmapdebug = opmapdebug;
348 #endif
349 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
350 printf("avail memory = %s\n", pbuf);
351 }
352
353 void
354 cpu_reboot(int howto, char *bootstr)
355 {
356 static int waittime = -1;
357
358 /* Take a snapshot before clobbering any registers. */
359 if (curproc)
360 savectx(curpcb);
361
362 /* If "always halt" was specified as a boot flag, obey. */
363 if (boothowto & RB_HALT)
364 howto |= RB_HALT;
365
366 boothowto = howto;
367
368 /* If system is cold, just halt. */
369 if (cold) {
370 boothowto |= RB_HALT;
371 goto haltsys;
372 }
373
374 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
375 waittime = 0;
376
377 /*
378 * Synchronize the disks....
379 */
380 vfs_shutdown();
381
382 /*
383 * If we've been adjusting the clock, the todr
384 * will be out of synch; adjust it now.
385 */
386 resettodr();
387 }
388
389 /* Disable interrupts. */
390 splhigh();
391
392 if (boothowto & RB_DUMP)
393 dumpsys();
394
395 haltsys:
396 /* Run any shutdown hooks. */
397 doshutdownhooks();
398
399 pmf_system_shutdown(boothowto);
400
401 #if 0
402 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
403 if (board && board->ab_poweroff)
404 board->ab_poweroff();
405 #endif
406
407 /*
408 * Firmware may autoboot (depending on settings), and we cannot pass
409 * flags to it (at least I haven't figured out how to yet), so
410 * we "pseudo-halt" now.
411 */
412 if (boothowto & RB_HALT) {
413 printf("\n");
414 printf("The operating system has halted.\n");
415 printf("Please press any key to reboot.\n\n");
416 cnpollc(1); /* For proper keyboard command handling */
417 cngetc();
418 cnpollc(0);
419 }
420
421 printf("reseting board...\n\n");
422 mips_icache_sync_all();
423 mips_dcache_wbinv_all();
424 __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
425 printf("Oops, back from reset\n\nSpinning...");
426 for (;;)
427 /* spin forever */ ; /* XXX */
428 /*NOTREACHED*/
429 }
430