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