1 /* $NetBSD: machdep.c,v 1.82 2024/03/05 14:15:33 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department, The Mach Operating System project at
11 * Carnegie-Mellon University and Ralph Campbell.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
38 */
39
40 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41
42 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.82 2024/03/05 14:15:33 thorpej Exp $");
43
44 /* from: Utah Hdr: machdep.c 1.63 91/04/24 */
45
46 #include "opt_ddb.h"
47 #include "opt_kgdb.h"
48 #include "opt_modular.h"
49 #include "opt_execfmt.h"
50
51 #include "zsc.h" /* XXX */
52 #include "com.h" /* XXX */
53 #include "ksyms.h"
54
55 #include <sys/param.h>
56 #include <sys/boot_flag.h>
57 #include <sys/conf.h>
58 #include <sys/cpu.h>
59 #include <sys/device.h>
60 #include <sys/intr.h>
61 #include <sys/kcore.h>
62 #include <sys/ksyms.h>
63 #include <sys/kernel.h>
64 #include <sys/reboot.h>
65 #include <sys/systm.h>
66
67 #include <uvm/uvm_extern.h>
68
69 #include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */
70
71 #include <dev/clock_subr.h>
72 #include <dev/cons.h>
73
74 #ifdef DDB
75 #include <machine/db_machdep.h>
76 #include <ddb/db_extern.h>
77 #endif
78
79 #include <machine/mainboard.h>
80 #include <machine/sysconf.h>
81 #include <machine/autoconf.h>
82 #include <machine/bootinfo.h>
83 #include <machine/prom.h>
84
85 /* maps for VM objects */
86
87 struct vm_map *phys_map = NULL;
88
89 char *bootinfo = NULL; /* pointer to bootinfo structure */
90
91 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
92 int mem_cluster_cnt;
93
94 void to_monitor(int) __dead;
95 void prom_halt(int) __dead;
96
97 #ifdef KGDB
98 void zs_kgdb_init(void);
99 void kgdb_connect(int);
100 #endif
101
102 /*
103 * Local functions.
104 */
105 int initcpu(void);
106 void configure(void);
107
108 void mach_init(int, char *[], char*[], u_int, char *);
109 int memsize_scan(void *);
110
111 #ifdef DEBUG
112 /* stacktrace code violates prototypes to get callee's registers */
113 extern void stacktrace(void); /*XXX*/
114 #endif
115
116 /* locore callback-vector setup */
117 extern void prom_init(void);
118 extern void pizazz_init(void);
119
120 /* platform-specific initialization vector */
121 static void unimpl_cons_init(void);
122 static void unimpl_iointr(uint32_t, vaddr_t, uint32_t);
123 static int unimpl_memsize(void *);
124 static void unimpl_intr_establish(int, int (*)(void *), void *);
125
126 struct platform platform = {
127 .iobus = "iobus not set",
128 .cons_init = unimpl_cons_init,
129 .iointr = unimpl_iointr,
130 .memsize = unimpl_memsize,
131 .intr_establish = unimpl_intr_establish,
132 .clkinit = NULL,
133 };
134
135 extern struct consdev consdev_prom;
136 extern struct consdev consdev_zs;
137
138 static void null_cnprobe(struct consdev *);
139 static void prom_cninit(struct consdev *);
140 static int prom_cngetc(dev_t);
141 static void prom_cnputc(dev_t, int);
142 static void null_cnpollc(dev_t, int);
143
144 struct consdev consdev_prom = {
145 null_cnprobe,
146 prom_cninit,
147 prom_cngetc,
148 prom_cnputc,
149 null_cnpollc,
150 };
151
152
153 /*
154 * Do all the stuff that locore normally does before calling main().
155 * Process arguments passed to us by the prom monitor.
156 * Return the first page address following the system.
157 */
158 void
mach_init(int argc,char * argv[],char * envp[],u_int bim,char * bip)159 mach_init(int argc, char *argv[], char *envp[], u_int bim, char *bip)
160 {
161 u_long first, last;
162 char *kernend;
163 char *cp;
164 int i, howto;
165 extern char edata[], end[];
166 const char *bi_msg;
167 #if NKSYMS || defined(DDB) || defined(MODULAR)
168 char *ssym = 0;
169 char *esym = 0;
170 struct btinfo_symtab *bi_syms;
171 #endif
172
173 /* Check for valid bootinfo passed from bootstrap */
174 if (bim == BOOTINFO_MAGIC) {
175 struct btinfo_magic *bi_magic;
176
177 bootinfo = (char *)BOOTINFO_ADDR; /* XXX */
178 bi_magic = lookup_bootinfo(BTINFO_MAGIC);
179 if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC)
180 bi_msg = "invalid bootinfo structure.\n";
181 else
182 bi_msg = NULL;
183 } else
184 bi_msg = "invalid bootinfo (standalone boot?)\n";
185
186 /* clear the BSS segment */
187 kernend = (void *)mips_round_page(end);
188 memset(edata, 0, end - edata);
189
190 /*
191 * Copy exception-dispatch code down to exception vector.
192 * Initialize locore-function vector.
193 * Clear out the I and D caches.
194 */
195 mips_vector_init(NULL, false);
196
197 #if NKSYMS || defined(DDB) || defined(LKM)
198 bi_syms = lookup_bootinfo(BTINFO_SYMTAB);
199
200 /* Load symbol table if present */
201 if (bi_syms != NULL) {
202 ssym = (void *)bi_syms->ssym;
203 esym = (void *)bi_syms->esym;
204 kernend = (void *)mips_round_page(esym);
205 }
206 #endif
207
208 prom_init();
209 consinit();
210
211 if (bi_msg != NULL)
212 printf(bi_msg);
213
214 uvm_md_init();
215
216 /* Find out how much memory is available. */
217 physmem = memsize_scan(kernend);
218
219 /*
220 * Now that we know how much memory we have, initialize the
221 * mem cluster array.
222 */
223 mem_clusters[0].start = 0; /* XXX is this correct? */
224 mem_clusters[0].size = ctob(physmem);
225 mem_cluster_cnt = 1;
226
227 /* Look at argv[0] and compute bootdev */
228 makebootdev(argv[0]);
229
230 /*
231 * Look at arguments passed to us and compute boothowto.
232 */
233 boothowto = RB_AUTOBOOT;
234 for (i = 1; i < argc; i++) {
235 for (cp = argv[i]; *cp; cp++) {
236 /* Ignore superfluous '-', if there is one */
237 if (*cp == '-')
238 continue;
239
240 howto = 0;
241 BOOT_FLAG(*cp, howto);
242 if (! howto)
243 printf("bootflag '%c' not recognised\n", *cp);
244 else
245 boothowto |= howto;
246 }
247 }
248
249
250 #if NKSYMS || defined(DDB) || defined(MODULAR)
251 /* init symbols if present */
252 if (esym)
253 ksyms_addsyms_elf(esym - ssym, ssym, esym);
254 #endif
255 #ifdef DDB
256 if (boothowto & RB_KDB)
257 Debugger();
258 #endif
259 #ifdef KGDB
260 zs_kgdb_init(); /* XXX */
261 if (boothowto & RB_KDB)
262 kgdb_connect(0);
263 #endif
264
265 /*
266 * Check to see if a mini-root was loaded into memory. It resides
267 * at the start of the next page just after the end of BSS.
268 */
269 if (boothowto & RB_MINIROOT)
270 kernend += round_page(mfs_initminiroot(kernend));
271
272 /*
273 * Load the rest of the available pages into the VM system.
274 */
275 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
276 last = mem_clusters[0].start + mem_clusters[0].size;
277 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
278 VM_FREELIST_DEFAULT);
279
280 /*
281 * Initialize error message buffer (at end of core).
282 */
283 mips_init_msgbuf();
284
285 /*
286 * Initialize the virtual memory system.
287 */
288 pmap_bootstrap();
289
290 /*
291 * Allocate uarea page for lwp0 and set it.
292 */
293 mips_init_lwp0_uarea();
294
295 /*
296 * Set up interrupt handling and I/O addresses.
297 */
298 pizazz_init();
299 }
300
301
302
303 /*
304 * cpu_startup: allocate memory for variable-sized tables,
305 * initialize CPU, and do autoconfiguration.
306 */
307 void
cpu_startup(void)308 cpu_startup(void)
309 {
310 cpu_startup_common();
311 }
312
313 /*
314 * Look up information in bootinfo of boot loader.
315 */
316 void *
lookup_bootinfo(int type)317 lookup_bootinfo(int type)
318 {
319 struct btinfo_common *bt;
320 char *help = bootinfo;
321
322 /* Check for a bootinfo record first. */
323 if (help == NULL)
324 return (NULL);
325
326 do {
327 bt = (struct btinfo_common *)help;
328 if (bt->type == type)
329 return ((void *)help);
330 help += bt->next;
331 } while (bt->next != 0 &&
332 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
333
334 return (NULL);
335 }
336
337 int waittime = -1;
338
339 /*
340 * call PROM to halt or reboot.
341 */
342 void
prom_halt(int howto)343 prom_halt(int howto)
344 {
345 if (howto & RB_HALT)
346 MIPS_PROM(reinit)();
347 MIPS_PROM(reboot)();
348 /* NOTREACHED */
349 }
350
351 void
cpu_reboot(volatile int howto,char * bootstr)352 cpu_reboot(volatile int howto, char *bootstr)
353 {
354 /* take a snap shot before clobbering any registers */
355 savectx(curpcb);
356
357 #ifdef DEBUG
358 if (panicstr)
359 stacktrace();
360 #endif
361
362 /* If system is cold, just halt. */
363 if (cold) {
364 howto |= RB_HALT;
365 goto haltsys;
366 }
367
368 /* If "always halt" was specified as a boot flag, obey. */
369 if ((boothowto & RB_HALT) != 0)
370 howto |= RB_HALT;
371
372 boothowto = howto;
373 if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
374 /*
375 * Synchronize the disks....
376 */
377 waittime = 0;
378 vfs_shutdown();
379 }
380
381 /* Disable interrupts. */
382 splhigh();
383
384 /* If rebooting and a dump is requested do it. */
385 #if 0
386 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
387 #else
388 if (howto & RB_DUMP)
389 #endif
390 dumpsys();
391
392 haltsys:
393
394 /* run any shutdown hooks */
395 doshutdownhooks();
396
397 pmf_system_shutdown(boothowto);
398
399 if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
400 prom_halt(0x80); /* rom monitor RB_PWOFF */
401
402 /* Finally, halt/reboot the system. */
403 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
404 prom_halt(howto & RB_HALT);
405 /*NOTREACHED*/
406 }
407
408 int
initcpu(void)409 initcpu(void)
410 {
411 spl0(); /* safe to turn interrupts on now */
412 return 0;
413 }
414
415 static void
unimpl_cons_init(void)416 unimpl_cons_init(void)
417 {
418
419 panic("sysconf.init didn't set cons_init");
420 }
421
422 static void
unimpl_iointr(uint32_t status,vaddr_t pc,uint32_t ipending)423 unimpl_iointr(uint32_t status, vaddr_t pc, uint32_t ipending)
424 {
425
426 panic("sysconf.init didn't set intr");
427 }
428
429 static int
unimpl_memsize(void * first)430 unimpl_memsize(void *first)
431 {
432
433 panic("sysconf.init didn't set memsize");
434 }
435
436 void
unimpl_intr_establish(int level,int (* func)(void *),void * arg)437 unimpl_intr_establish(int level, int (*func)(void *), void *arg)
438 {
439 panic("sysconf.init didn't init intr_establish");
440 }
441
442 void
delay(int n)443 delay(int n)
444 {
445 DELAY(n);
446 }
447
448 /*
449 * Find out how much memory is available by testing memory.
450 * Be careful to save and restore the original contents for msgbuf.
451 */
452 int
memsize_scan(void * first)453 memsize_scan(void *first)
454 {
455 volatile int *vp, *vp0;
456 int mem, tmp, tmp0;
457
458 #define PATTERN1 0xa5a5a5a5
459 #define PATTERN2 ~PATTERN1
460
461 /*
462 * Non destructive scan of memory to determine the size
463 * Use the first page to test for memory aliases. This
464 * also has the side effect of flushing the bus alignment
465 * buffer
466 */
467 mem = btoc((paddr_t)first - MIPS_KSEG0_START);
468 vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT);
469 vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */
470 tmp0 = *vp0;
471 while (vp < (int *)MIPS_MAX_MEM_ADDR) {
472 tmp = *vp;
473 *vp = PATTERN1;
474 *vp0 = PATTERN2;
475 wbflush();
476 if (*vp != PATTERN1)
477 break;
478 *vp = PATTERN2;
479 *vp0 = PATTERN1;
480 wbflush();
481 if (*vp != PATTERN2)
482 break;
483 *vp = tmp;
484 vp += PAGE_SIZE/sizeof(int);
485 mem++;
486 }
487 *vp0 = tmp0;
488 return mem;
489 }
490
491 /*
492 * Console initialization: called early on from main,
493 * before vm init or startup. Do enough configuration
494 * to choose and initialize a console.
495 */
496
497 static void
null_cnprobe(struct consdev * cn)498 null_cnprobe(struct consdev *cn)
499 {
500 }
501
502 static void
prom_cninit(struct consdev * cn)503 prom_cninit(struct consdev *cn)
504 {
505 extern const struct cdevsw cons_cdevsw;
506
507 cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0);
508 cn->cn_pri = CN_REMOTE;
509 }
510
511 static int
prom_cngetc(dev_t dev)512 prom_cngetc(dev_t dev)
513 {
514 return MIPS_PROM(getchar)();
515 }
516
517 static void
prom_cnputc(dev_t dev,int c)518 prom_cnputc(dev_t dev, int c)
519 {
520 MIPS_PROM(putchar)(c);
521 }
522
523 static void
null_cnpollc(dev_t dev,int on)524 null_cnpollc(dev_t dev, int on)
525 {
526 }
527
528 void
consinit(void)529 consinit(void)
530 {
531
532 cn_tab = &consdev_zs;
533
534 (*cn_tab->cn_init)(cn_tab);
535 }
536