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