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