machdep.c revision 1.1 1 1.1 simonb /* $NetBSD: machdep.c,v 1.1 2021/01/27 05:24:16 simonb 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.1 simonb __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.1 2021/01/27 05:24:16 simonb 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.1 simonb /* Pick a random clock frequency. XXX Any better way? */
117 1.1 simonb cntfreq = curcpu()->ci_cpu_freq = 10 * 1000 * 1000;
118 1.1 simonb
119 1.1 simonb if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
120 1.1 simonb cntfreq /= 2;
121 1.1 simonb
122 1.1 simonb curcpu()->ci_cctr_freq = cntfreq;
123 1.1 simonb curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
124 1.1 simonb
125 1.1 simonb /* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
126 1.1 simonb curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
127 1.1 simonb }
128 1.1 simonb
129 1.1 simonb /*
130 1.1 simonb *
131 1.1 simonb */
132 1.1 simonb void
133 1.1 simonb mach_init(u_long arg0, u_long arg1, u_long arg2, u_long arg3)
134 1.1 simonb {
135 1.1 simonb struct mipssim_config *mcp = &mipssim_configuration;
136 1.1 simonb void *kernend;
137 1.1 simonb extern char edata[], end[]; /* XXX */
138 1.1 simonb
139 1.1 simonb kernend = (void *)mips_round_page(end);
140 1.1 simonb
141 1.1 simonb /* Zero BSS. QEMU appears to leave some memory uninitialised. */
142 1.1 simonb memset(edata, 0, end - edata);
143 1.1 simonb
144 1.1 simonb /* enough of a console for printf() to work */
145 1.1 simonb cn_tab = &early_console;
146 1.1 simonb
147 1.1 simonb cal_timer();
148 1.1 simonb
149 1.1 simonb /* set CPU model info for sysctl_hw */
150 1.1 simonb cpu_setmodel("MIPSSIM");
151 1.1 simonb
152 1.1 simonb mips_vector_init(NULL, false);
153 1.1 simonb
154 1.1 simonb uvm_md_init();
155 1.1 simonb
156 1.1 simonb /*
157 1.1 simonb * Initialize bus space tags and bring up the main console.
158 1.1 simonb */
159 1.1 simonb mipssim_bus_io_init(&mcp->mc_iot, mcp);
160 1.1 simonb if (comcnattach(&mcp->mc_iot, MIPSSIM_UART0_ADDR, COMCNRATE,
161 1.1 simonb COM_FREQ, COM_TYPE_NORMAL,
162 1.1 simonb (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
163 1.1 simonb panic("unable to initialize serial console");
164 1.1 simonb
165 1.1 simonb /*
166 1.1 simonb * No way of passing arguments in mipssim.
167 1.1 simonb */
168 1.1 simonb boothowto = RB_AUTOBOOT;
169 1.1 simonb #ifdef KADB
170 1.1 simonb boothowto |= RB_KDB;
171 1.1 simonb #endif
172 1.1 simonb
173 1.1 simonb mach_init_memory();
174 1.1 simonb
175 1.1 simonb /*
176 1.1 simonb * Load the available pages into the VM system.
177 1.1 simonb */
178 1.1 simonb mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
179 1.1 simonb mem_clusters, mem_cluster_cnt, NULL, 0);
180 1.1 simonb
181 1.1 simonb /*
182 1.1 simonb * Initialize message buffer (at end of core).
183 1.1 simonb */
184 1.1 simonb mips_init_msgbuf();
185 1.1 simonb
186 1.1 simonb /*
187 1.1 simonb * Initialize the virtual memory system.
188 1.1 simonb */
189 1.1 simonb pmap_bootstrap();
190 1.1 simonb
191 1.1 simonb /*
192 1.1 simonb * Allocate uarea page for lwp0 and set it.
193 1.1 simonb */
194 1.1 simonb mips_init_lwp0_uarea();
195 1.1 simonb
196 1.1 simonb /*
197 1.1 simonb * Initialize debuggers, and break into them, if appropriate.
198 1.1 simonb */
199 1.1 simonb #ifdef DDB
200 1.1 simonb if (boothowto & RB_KDB)
201 1.1 simonb Debugger();
202 1.1 simonb #endif
203 1.1 simonb }
204 1.1 simonb
205 1.1 simonb /*
206 1.1 simonb * qemu for mipssim doesn't have a way of passing in the memory size, so
207 1.1 simonb * we probe. lwp0 hasn't been set up this early, so use a dummy pcb to
208 1.1 simonb * allow badaddr() to function. Limit total RAM to just before the IO
209 1.1 simonb * memory at MIPSSIM_ISA_IO_BASE.
210 1.1 simonb */
211 1.1 simonb static void
212 1.1 simonb mach_init_memory(void)
213 1.1 simonb {
214 1.1 simonb struct lwp *l = curlwp;
215 1.1 simonb struct pcb dummypcb;
216 1.1 simonb psize_t memsize;
217 1.1 simonb size_t addr;
218 1.1 simonb uint32_t *memptr;
219 1.1 simonb extern char end[]; /* XXX */
220 1.1 simonb
221 1.1 simonb l->l_addr = &dummypcb;
222 1.1 simonb memsize = roundup2(MIPS_KSEG0_TO_PHYS((uintptr_t)(end)), 1024 * 1024);
223 1.1 simonb
224 1.1 simonb for (addr = memsize; addr < MIPSSIM_ISA_IO_BASE; addr += 1024 * 1024) {
225 1.1 simonb #ifdef MEM_DEBUG
226 1.1 simonb printf("test %zd MB\n", addr / 1024 * 1024);
227 1.1 simonb #endif
228 1.1 simonb memptr = (void *)MIPS_PHYS_TO_KSEG1(addr - sizeof(*memptr));
229 1.1 simonb
230 1.1 simonb if (badaddr(memptr, sizeof(uint32_t)) < 0)
231 1.1 simonb break;
232 1.1 simonb
233 1.1 simonb memsize = addr;
234 1.1 simonb }
235 1.1 simonb l->l_addr = NULL;
236 1.1 simonb
237 1.1 simonb printf("Memory size: 0x%" PRIxPSIZE " (%" PRIdPSIZE " MB)\n",
238 1.1 simonb memsize, memsize / 1024 / 1024);
239 1.1 simonb physmem = btoc(memsize);
240 1.1 simonb
241 1.1 simonb mem_clusters[0].start = PAGE_SIZE;
242 1.1 simonb mem_clusters[0].size = memsize - PAGE_SIZE;
243 1.1 simonb mem_cluster_cnt = 1;
244 1.1 simonb }
245 1.1 simonb
246 1.1 simonb void
247 1.1 simonb consinit(void)
248 1.1 simonb {
249 1.1 simonb /*
250 1.1 simonb * Everything related to console initialization is done
251 1.1 simonb * in mach_init().
252 1.1 simonb */
253 1.1 simonb }
254 1.1 simonb
255 1.1 simonb void
256 1.1 simonb cpu_startup(void)
257 1.1 simonb {
258 1.1 simonb
259 1.1 simonb /*
260 1.1 simonb * Do the common startup items.
261 1.1 simonb */
262 1.1 simonb cpu_startup_common();
263 1.1 simonb }
264 1.1 simonb
265 1.1 simonb /* XXX try to make this evbmips generic */
266 1.1 simonb void
267 1.1 simonb cpu_reboot(int howto, char *bootstr)
268 1.1 simonb {
269 1.1 simonb static int waittime = -1;
270 1.1 simonb
271 1.1 simonb /* Take a snapshot before clobbering any registers. */
272 1.1 simonb savectx(curpcb);
273 1.1 simonb
274 1.1 simonb /* If "always halt" was specified as a boot flag, obey. */
275 1.1 simonb if (boothowto & RB_HALT)
276 1.1 simonb howto |= RB_HALT;
277 1.1 simonb
278 1.1 simonb boothowto = howto;
279 1.1 simonb
280 1.1 simonb /* If system is cold, just halt. */
281 1.1 simonb if (cold) {
282 1.1 simonb boothowto |= RB_HALT;
283 1.1 simonb goto haltsys;
284 1.1 simonb }
285 1.1 simonb
286 1.1 simonb if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
287 1.1 simonb waittime = 0;
288 1.1 simonb
289 1.1 simonb /*
290 1.1 simonb * Synchronize the disks....
291 1.1 simonb */
292 1.1 simonb vfs_shutdown();
293 1.1 simonb
294 1.1 simonb /*
295 1.1 simonb * If we've been adjusting the clock, the todr
296 1.1 simonb * will be out of synch; adjust it now.
297 1.1 simonb */
298 1.1 simonb resettodr();
299 1.1 simonb }
300 1.1 simonb
301 1.1 simonb /* Disable interrupts. */
302 1.1 simonb splhigh();
303 1.1 simonb
304 1.1 simonb if (boothowto & RB_DUMP)
305 1.1 simonb dumpsys();
306 1.1 simonb
307 1.1 simonb haltsys:
308 1.1 simonb /* Run any shutdown hooks. */
309 1.1 simonb doshutdownhooks();
310 1.1 simonb
311 1.1 simonb /*
312 1.1 simonb * Firmware may autoboot (depending on settings), and we cannot pass
313 1.1 simonb * flags to it (at least I haven't figured out how to yet), so
314 1.1 simonb * we "pseudo-halt" now.
315 1.1 simonb */
316 1.1 simonb if (boothowto & RB_HALT) {
317 1.1 simonb printf("\n");
318 1.1 simonb printf("The operating system has halted.\n");
319 1.1 simonb printf("Please press any key to reboot.\n\n");
320 1.1 simonb cnpollc(1); /* For proper keyboard command handling */
321 1.1 simonb cngetc();
322 1.1 simonb cnpollc(0);
323 1.1 simonb }
324 1.1 simonb
325 1.1 simonb printf("resetting...\n\n");
326 1.1 simonb __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
327 1.1 simonb printf("Oops, back from reset\n\nSpinning...");
328 1.1 simonb for (;;)
329 1.1 simonb /* spin forever */ ; /* XXX */
330 1.1 simonb /*NOTREACHED*/
331 1.1 simonb }
332