machdep.c revision 1.2.4.2 1 1.2.4.2 thorpej /* $NetBSD: machdep.c,v 1.2.4.2 2021/04/03 22:28:23 thorpej Exp $ */
2 1.2.4.2 thorpej
3 1.2.4.2 thorpej /*-
4 1.2.4.2 thorpej * Copyright (c) 2001,2021 The NetBSD Foundation, Inc.
5 1.2.4.2 thorpej * All rights reserved.
6 1.2.4.2 thorpej *
7 1.2.4.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 thorpej * by Jason R. Thorpe and Simon Burge.
9 1.2.4.2 thorpej *
10 1.2.4.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 thorpej * modification, are permitted provided that the following conditions
12 1.2.4.2 thorpej * are met:
13 1.2.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2.4.2 thorpej *
19 1.2.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.4.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.4.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.4.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.4.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.4.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.4.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.4.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.4.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.4.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.4.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.2.4.2 thorpej */
31 1.2.4.2 thorpej
32 1.2.4.2 thorpej #include <sys/cdefs.h>
33 1.2.4.2 thorpej __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.2.4.2 2021/04/03 22:28:23 thorpej Exp $");
34 1.2.4.2 thorpej
35 1.2.4.2 thorpej #include "opt_ddb.h"
36 1.2.4.2 thorpej #include "opt_kgdb.h"
37 1.2.4.2 thorpej #include "opt_modular.h"
38 1.2.4.2 thorpej
39 1.2.4.2 thorpej #include <sys/param.h>
40 1.2.4.2 thorpej #include <sys/boot_flag.h>
41 1.2.4.2 thorpej #include <sys/bus.h>
42 1.2.4.2 thorpej #include <sys/cpu.h>
43 1.2.4.2 thorpej #include <sys/device.h>
44 1.2.4.2 thorpej #include <sys/kcore.h>
45 1.2.4.2 thorpej #include <sys/kernel.h>
46 1.2.4.2 thorpej #include <sys/ksyms.h>
47 1.2.4.2 thorpej #include <sys/mount.h>
48 1.2.4.2 thorpej #include <sys/mutex.h>
49 1.2.4.2 thorpej #include <sys/reboot.h>
50 1.2.4.2 thorpej #include <sys/termios.h>
51 1.2.4.2 thorpej
52 1.2.4.2 thorpej #include <uvm/uvm_extern.h>
53 1.2.4.2 thorpej
54 1.2.4.2 thorpej #include <dev/cons.h>
55 1.2.4.2 thorpej #include <dev/ic/comvar.h>
56 1.2.4.2 thorpej #include <dev/ic/ns16450reg.h>
57 1.2.4.2 thorpej
58 1.2.4.2 thorpej #include <evbmips/mipssim/mipssimreg.h>
59 1.2.4.2 thorpej #include <evbmips/mipssim/mipssimvar.h>
60 1.2.4.2 thorpej
61 1.2.4.2 thorpej #include "ksyms.h"
62 1.2.4.2 thorpej
63 1.2.4.2 thorpej #if NKSYMS || defined(DDB) || defined(MODULAR)
64 1.2.4.2 thorpej #include <mips/db_machdep.h>
65 1.2.4.2 thorpej #include <ddb/db_extern.h>
66 1.2.4.2 thorpej #endif
67 1.2.4.2 thorpej
68 1.2.4.2 thorpej #include <mips/cache.h>
69 1.2.4.2 thorpej #include <mips/locore.h>
70 1.2.4.2 thorpej #include <mips/cpuregs.h>
71 1.2.4.2 thorpej
72 1.2.4.2 thorpej
73 1.2.4.2 thorpej #define COMCNRATE 115200 /* not important, emulated device */
74 1.2.4.2 thorpej #define COM_FREQ 1843200 /* not important, emulated device */
75 1.2.4.2 thorpej
76 1.2.4.2 thorpej /* XXX move phys map decl to a general mips location */
77 1.2.4.2 thorpej /* Maps for VM objects. */
78 1.2.4.2 thorpej struct vm_map *phys_map = NULL;
79 1.2.4.2 thorpej
80 1.2.4.2 thorpej struct mipssim_config mipssim_configuration;
81 1.2.4.2 thorpej
82 1.2.4.2 thorpej /* XXX move mem cluster decls to a general mips location */
83 1.2.4.2 thorpej int mem_cluster_cnt;
84 1.2.4.2 thorpej phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
85 1.2.4.2 thorpej
86 1.2.4.2 thorpej /* XXX move mach_init() prototype to general mips header file */
87 1.2.4.2 thorpej void mach_init(u_long, u_long, u_long, u_long);
88 1.2.4.2 thorpej static void mach_init_memory(void);
89 1.2.4.2 thorpej
90 1.2.4.2 thorpej /*
91 1.2.4.2 thorpej * Provide a very simple output-only console driver so that we can
92 1.2.4.2 thorpej * use printf() before the "real" console is initialised.
93 1.2.4.2 thorpej */
94 1.2.4.2 thorpej static void uart_putc(dev_t, int);
95 1.2.4.2 thorpej static struct consdev early_console = {
96 1.2.4.2 thorpej .cn_putc = uart_putc,
97 1.2.4.2 thorpej .cn_dev = makedev(0, 0),
98 1.2.4.2 thorpej .cn_pri = CN_DEAD
99 1.2.4.2 thorpej };
100 1.2.4.2 thorpej
101 1.2.4.2 thorpej static void
102 1.2.4.2 thorpej uart_putc(dev_t dev, int c)
103 1.2.4.2 thorpej {
104 1.2.4.2 thorpej volatile uint8_t *data = (void *)MIPS_PHYS_TO_KSEG1(
105 1.2.4.2 thorpej MIPSSIM_ISA_IO_BASE + MIPSSIM_UART0_ADDR + com_data);
106 1.2.4.2 thorpej
107 1.2.4.2 thorpej *data = (uint8_t)c;
108 1.2.4.2 thorpej /* emulated UART, don't need to wait for output to drain */
109 1.2.4.2 thorpej }
110 1.2.4.2 thorpej
111 1.2.4.2 thorpej static void
112 1.2.4.2 thorpej cal_timer(void)
113 1.2.4.2 thorpej {
114 1.2.4.2 thorpej uint32_t cntfreq;
115 1.2.4.2 thorpej
116 1.2.4.2 thorpej /*
117 1.2.4.2 thorpej * Qemu seems to default to 200 MHz; wall clock looks the right speed
118 1.2.4.2 thorpej * but we don't have an RTC to check.
119 1.2.4.2 thorpej */
120 1.2.4.2 thorpej cntfreq = curcpu()->ci_cpu_freq = 200 * 1000 * 1000;
121 1.2.4.2 thorpej
122 1.2.4.2 thorpej if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
123 1.2.4.2 thorpej cntfreq /= 2;
124 1.2.4.2 thorpej
125 1.2.4.2 thorpej curcpu()->ci_cctr_freq = cntfreq;
126 1.2.4.2 thorpej curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
127 1.2.4.2 thorpej
128 1.2.4.2 thorpej /* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
129 1.2.4.2 thorpej curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
130 1.2.4.2 thorpej }
131 1.2.4.2 thorpej
132 1.2.4.2 thorpej /*
133 1.2.4.2 thorpej *
134 1.2.4.2 thorpej */
135 1.2.4.2 thorpej void
136 1.2.4.2 thorpej mach_init(u_long arg0, u_long arg1, u_long arg2, u_long arg3)
137 1.2.4.2 thorpej {
138 1.2.4.2 thorpej struct mipssim_config *mcp = &mipssim_configuration;
139 1.2.4.2 thorpej void *kernend;
140 1.2.4.2 thorpej extern char edata[], end[]; /* XXX */
141 1.2.4.2 thorpej
142 1.2.4.2 thorpej kernend = (void *)mips_round_page(end);
143 1.2.4.2 thorpej
144 1.2.4.2 thorpej /* Zero BSS. QEMU appears to leave some memory uninitialised. */
145 1.2.4.2 thorpej memset(edata, 0, end - edata);
146 1.2.4.2 thorpej
147 1.2.4.2 thorpej /* enough of a console for printf() to work */
148 1.2.4.2 thorpej cn_tab = &early_console;
149 1.2.4.2 thorpej
150 1.2.4.2 thorpej cal_timer();
151 1.2.4.2 thorpej
152 1.2.4.2 thorpej /* set CPU model info for sysctl_hw */
153 1.2.4.2 thorpej cpu_setmodel("MIPSSIM");
154 1.2.4.2 thorpej
155 1.2.4.2 thorpej mips_vector_init(NULL, false);
156 1.2.4.2 thorpej
157 1.2.4.2 thorpej uvm_md_init();
158 1.2.4.2 thorpej
159 1.2.4.2 thorpej /*
160 1.2.4.2 thorpej * Initialize bus space tags and bring up the main console.
161 1.2.4.2 thorpej */
162 1.2.4.2 thorpej mipssim_bus_io_init(&mcp->mc_iot, mcp);
163 1.2.4.2 thorpej mipssim_dma_init(mcp);
164 1.2.4.2 thorpej
165 1.2.4.2 thorpej if (comcnattach(&mcp->mc_iot, MIPSSIM_UART0_ADDR, COMCNRATE,
166 1.2.4.2 thorpej COM_FREQ, COM_TYPE_NORMAL,
167 1.2.4.2 thorpej (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
168 1.2.4.2 thorpej panic("unable to initialize serial console");
169 1.2.4.2 thorpej
170 1.2.4.2 thorpej /*
171 1.2.4.2 thorpej * No way of passing arguments in mipssim.
172 1.2.4.2 thorpej */
173 1.2.4.2 thorpej boothowto = RB_AUTOBOOT;
174 1.2.4.2 thorpej #ifdef KADB
175 1.2.4.2 thorpej boothowto |= RB_KDB;
176 1.2.4.2 thorpej #endif
177 1.2.4.2 thorpej
178 1.2.4.2 thorpej mach_init_memory();
179 1.2.4.2 thorpej
180 1.2.4.2 thorpej /*
181 1.2.4.2 thorpej * Load the available pages into the VM system.
182 1.2.4.2 thorpej */
183 1.2.4.2 thorpej mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
184 1.2.4.2 thorpej mem_clusters, mem_cluster_cnt, NULL, 0);
185 1.2.4.2 thorpej
186 1.2.4.2 thorpej /*
187 1.2.4.2 thorpej * Initialize message buffer (at end of core).
188 1.2.4.2 thorpej */
189 1.2.4.2 thorpej mips_init_msgbuf();
190 1.2.4.2 thorpej
191 1.2.4.2 thorpej /*
192 1.2.4.2 thorpej * Initialize the virtual memory system.
193 1.2.4.2 thorpej */
194 1.2.4.2 thorpej pmap_bootstrap();
195 1.2.4.2 thorpej
196 1.2.4.2 thorpej /*
197 1.2.4.2 thorpej * Allocate uarea page for lwp0 and set it.
198 1.2.4.2 thorpej */
199 1.2.4.2 thorpej mips_init_lwp0_uarea();
200 1.2.4.2 thorpej
201 1.2.4.2 thorpej /*
202 1.2.4.2 thorpej * Initialize debuggers, and break into them, if appropriate.
203 1.2.4.2 thorpej */
204 1.2.4.2 thorpej #ifdef DDB
205 1.2.4.2 thorpej if (boothowto & RB_KDB)
206 1.2.4.2 thorpej Debugger();
207 1.2.4.2 thorpej #endif
208 1.2.4.2 thorpej }
209 1.2.4.2 thorpej
210 1.2.4.2 thorpej /*
211 1.2.4.2 thorpej * qemu for mipssim doesn't have a way of passing in the memory size, so
212 1.2.4.2 thorpej * we probe. lwp0 hasn't been set up this early, so use a dummy pcb to
213 1.2.4.2 thorpej * allow badaddr() to function. Limit total RAM to just before the IO
214 1.2.4.2 thorpej * memory at MIPSSIM_ISA_IO_BASE.
215 1.2.4.2 thorpej */
216 1.2.4.2 thorpej static void
217 1.2.4.2 thorpej mach_init_memory(void)
218 1.2.4.2 thorpej {
219 1.2.4.2 thorpej struct lwp *l = curlwp;
220 1.2.4.2 thorpej struct pcb dummypcb;
221 1.2.4.2 thorpej psize_t memsize;
222 1.2.4.2 thorpej size_t addr;
223 1.2.4.2 thorpej uint32_t *memptr;
224 1.2.4.2 thorpej extern char end[]; /* XXX */
225 1.2.4.2 thorpej
226 1.2.4.2 thorpej l->l_addr = &dummypcb;
227 1.2.4.2 thorpej memsize = roundup2(MIPS_KSEG0_TO_PHYS((uintptr_t)(end)), 1024 * 1024);
228 1.2.4.2 thorpej
229 1.2.4.2 thorpej for (addr = memsize; addr < MIPSSIM_ISA_IO_BASE; addr += 1024 * 1024) {
230 1.2.4.2 thorpej #ifdef MEM_DEBUG
231 1.2.4.2 thorpej printf("test %zd MB\n", addr / 1024 * 1024);
232 1.2.4.2 thorpej #endif
233 1.2.4.2 thorpej memptr = (void *)MIPS_PHYS_TO_KSEG1(addr - sizeof(*memptr));
234 1.2.4.2 thorpej
235 1.2.4.2 thorpej if (badaddr(memptr, sizeof(uint32_t)) < 0)
236 1.2.4.2 thorpej break;
237 1.2.4.2 thorpej
238 1.2.4.2 thorpej memsize = addr;
239 1.2.4.2 thorpej }
240 1.2.4.2 thorpej l->l_addr = NULL;
241 1.2.4.2 thorpej
242 1.2.4.2 thorpej printf("Memory size: 0x%" PRIxPSIZE " (%" PRIdPSIZE " MB)\n",
243 1.2.4.2 thorpej memsize, memsize / 1024 / 1024);
244 1.2.4.2 thorpej physmem = btoc(memsize);
245 1.2.4.2 thorpej
246 1.2.4.2 thorpej mem_clusters[0].start = PAGE_SIZE;
247 1.2.4.2 thorpej mem_clusters[0].size = memsize - PAGE_SIZE;
248 1.2.4.2 thorpej mem_cluster_cnt = 1;
249 1.2.4.2 thorpej }
250 1.2.4.2 thorpej
251 1.2.4.2 thorpej void
252 1.2.4.2 thorpej consinit(void)
253 1.2.4.2 thorpej {
254 1.2.4.2 thorpej /*
255 1.2.4.2 thorpej * Everything related to console initialization is done
256 1.2.4.2 thorpej * in mach_init().
257 1.2.4.2 thorpej */
258 1.2.4.2 thorpej }
259 1.2.4.2 thorpej
260 1.2.4.2 thorpej void
261 1.2.4.2 thorpej cpu_startup(void)
262 1.2.4.2 thorpej {
263 1.2.4.2 thorpej
264 1.2.4.2 thorpej /*
265 1.2.4.2 thorpej * Do the common startup items.
266 1.2.4.2 thorpej */
267 1.2.4.2 thorpej cpu_startup_common();
268 1.2.4.2 thorpej }
269 1.2.4.2 thorpej
270 1.2.4.2 thorpej /* XXX try to make this evbmips generic */
271 1.2.4.2 thorpej void
272 1.2.4.2 thorpej cpu_reboot(int howto, char *bootstr)
273 1.2.4.2 thorpej {
274 1.2.4.2 thorpej static int waittime = -1;
275 1.2.4.2 thorpej
276 1.2.4.2 thorpej /* Take a snapshot before clobbering any registers. */
277 1.2.4.2 thorpej savectx(curpcb);
278 1.2.4.2 thorpej
279 1.2.4.2 thorpej /* If "always halt" was specified as a boot flag, obey. */
280 1.2.4.2 thorpej if (boothowto & RB_HALT)
281 1.2.4.2 thorpej howto |= RB_HALT;
282 1.2.4.2 thorpej
283 1.2.4.2 thorpej boothowto = howto;
284 1.2.4.2 thorpej
285 1.2.4.2 thorpej /* If system is cold, just halt. */
286 1.2.4.2 thorpej if (cold) {
287 1.2.4.2 thorpej boothowto |= RB_HALT;
288 1.2.4.2 thorpej goto haltsys;
289 1.2.4.2 thorpej }
290 1.2.4.2 thorpej
291 1.2.4.2 thorpej if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
292 1.2.4.2 thorpej waittime = 0;
293 1.2.4.2 thorpej
294 1.2.4.2 thorpej /*
295 1.2.4.2 thorpej * Synchronize the disks....
296 1.2.4.2 thorpej */
297 1.2.4.2 thorpej vfs_shutdown();
298 1.2.4.2 thorpej
299 1.2.4.2 thorpej /*
300 1.2.4.2 thorpej * If we've been adjusting the clock, the todr
301 1.2.4.2 thorpej * will be out of synch; adjust it now.
302 1.2.4.2 thorpej */
303 1.2.4.2 thorpej resettodr();
304 1.2.4.2 thorpej }
305 1.2.4.2 thorpej
306 1.2.4.2 thorpej /* Disable interrupts. */
307 1.2.4.2 thorpej splhigh();
308 1.2.4.2 thorpej
309 1.2.4.2 thorpej if (boothowto & RB_DUMP)
310 1.2.4.2 thorpej dumpsys();
311 1.2.4.2 thorpej
312 1.2.4.2 thorpej haltsys:
313 1.2.4.2 thorpej /* Run any shutdown hooks. */
314 1.2.4.2 thorpej doshutdownhooks();
315 1.2.4.2 thorpej
316 1.2.4.2 thorpej /*
317 1.2.4.2 thorpej * Firmware may autoboot (depending on settings), and we cannot pass
318 1.2.4.2 thorpej * flags to it (at least I haven't figured out how to yet), so
319 1.2.4.2 thorpej * we "pseudo-halt" now.
320 1.2.4.2 thorpej */
321 1.2.4.2 thorpej if (boothowto & RB_HALT) {
322 1.2.4.2 thorpej printf("\n");
323 1.2.4.2 thorpej printf("The operating system has halted.\n");
324 1.2.4.2 thorpej printf("Please press any key to reboot.\n\n");
325 1.2.4.2 thorpej cnpollc(1); /* For proper keyboard command handling */
326 1.2.4.2 thorpej cngetc();
327 1.2.4.2 thorpej cnpollc(0);
328 1.2.4.2 thorpej }
329 1.2.4.2 thorpej
330 1.2.4.2 thorpej printf("resetting...\n\n");
331 1.2.4.2 thorpej __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
332 1.2.4.2 thorpej printf("Oops, back from reset\n\nSpinning...");
333 1.2.4.2 thorpej for (;;)
334 1.2.4.2 thorpej /* spin forever */ ; /* XXX */
335 1.2.4.2 thorpej /*NOTREACHED*/
336 1.2.4.2 thorpej }
337