1 /* $NetBSD: machdep.c,v 1.134 2024/03/05 14:15:28 thorpej Exp $ */
2 /* $OpenBSD: machdep.c,v 1.36 1999/05/22 21:22:19 weingart Exp $ */
3
4 /*
5 * Copyright (c) 1988 University of Utah.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department, The Mach Operating System project at
12 * Carnegie-Mellon University and Ralph Campbell.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)machdep.c 8.3 (Berkeley) 1/12/94
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.134 2024/03/05 14:15:28 thorpej Exp $");
43
44 #include "opt_ddb.h"
45 #include "opt_ddbparam.h"
46 #include "opt_md.h"
47 #include "opt_modular.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/signalvar.h>
52 #include <sys/kernel.h>
53 #include <sys/proc.h>
54 #include <sys/buf.h>
55 #include <sys/reboot.h>
56 #include <sys/conf.h>
57 #include <sys/file.h>
58 #include <sys/mbuf.h>
59 #include <sys/msgbuf.h>
60 #include <sys/ioctl.h>
61 #include <sys/time.h>
62 #include <sys/tty.h>
63 #include <sys/exec.h>
64 #include <uvm/uvm_extern.h>
65 #include <sys/mount.h>
66 #include <sys/device.h>
67 #include <sys/syscallargs.h>
68 #include <sys/kcore.h>
69 #include <sys/ksyms.h>
70 #include <sys/cpu.h>
71 #include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */
72
73 #include <machine/bootinfo.h>
74 #include <machine/cpu.h>
75 #include <machine/reg.h>
76 #include <machine/pio.h>
77 #include <sys/bus.h>
78 #include <machine/trap.h>
79 #include <machine/autoconf.h>
80 #include <machine/platform.h>
81 #include <machine/wired_map.h>
82 #include <mips/pte.h>
83 #include <mips/locore.h>
84 #include <mips/cpuregs.h>
85 #include <mips/psl.h>
86 #include <mips/cache.h>
87 #ifdef DDB
88 #include <mips/db_machdep.h>
89 #include <ddb/db_extern.h>
90 #endif
91
92 #include <dev/cons.h>
93
94 #include <dev/ic/i8042reg.h>
95 #include <dev/isa/isareg.h>
96
97 #include <arc/arc/arcbios.h>
98 #include <arc/arc/timervar.h>
99
100 #include "ksyms.h"
101
102 #include "com.h"
103 #if NCOM > 0
104 #include <sys/termios.h>
105 #include <dev/ic/comreg.h>
106 #include <dev/ic/comvar.h>
107
108 #ifndef COMCONSOLE
109 #define COMCONSOLE 0
110 #endif
111
112 #ifndef CONADDR
113 #define CONADDR 0 /* use default address if CONADDR isn't configured */
114 #endif
115
116 #ifndef CONSPEED
117 #define CONSPEED TTYDEF_SPEED
118 #endif
119 #ifndef CONMODE
120 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
121 #endif
122 #endif /* NCOM */
123
124 /* maps for VM objects */
125 struct vm_map *phys_map = NULL;
126
127 int maxmem; /* max memory per process */
128 int cpuspeed = 150; /* approx CPU clock [MHz] */
129 vsize_t kseg2iobufsize = 0; /* to reserve PTEs for KSEG2 I/O space */
130 struct arc_bus_space arc_bus_io;/* Bus tag for bus.h macros */
131 struct arc_bus_space arc_bus_mem;/* Bus tag for bus.h macros */
132
133 char *bootinfo; /* pointer to bootinfo structure */
134 static char bi_buf[BOOTINFO_SIZE]; /* buffer to store bootinfo data */
135 static const char *bootinfo_msg = NULL;
136
137 #if NCOM > 0
138 int com_freq = COM_FREQ; /* unusual clock frequency of dev/ic/com.c */
139 int com_console = COMCONSOLE;
140 int com_console_address = CONADDR;
141 int com_console_speed = CONSPEED;
142 int com_console_mode = CONMODE;
143 #else
144 #ifndef COMCONSOLE
145 #error COMCONSOLE is defined without com driver configured.
146 #endif
147 int com_console = 0;
148 #endif /* NCOM */
149
150 char **environment; /* On some arches, pointer to environment */
151
152 int mem_reserved[VM_PHYSSEG_MAX]; /* the cluster is reserved, i.e. not free */
153 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
154 int mem_cluster_cnt;
155
156 /* initialize bss, etc. from kernel start, before main() is called. */
157 void mach_init(int, char *[], u_int, void *);
158
159 const char *firmware_getenv(const char *env);
160 void arc_sysreset(bus_addr_t, bus_size_t);
161
162 extern char kernel_text[], edata[], end[];
163
164 /*
165 * Do all the stuff that locore normally does before calling main().
166 * Process arguments passed to us by the BIOS.
167 * Reset mapping and set up mapping to hardware and init "wired" reg.
168 * Return the first page address following the system.
169 */
170 void
mach_init(int argc,char * argv[],u_int bim,void * bip)171 mach_init(int argc, char *argv[], u_int bim, void *bip)
172 {
173 const char *cp;
174 int i;
175 #if 0
176 paddr_t kernstartpfn;
177 #endif
178 paddr_t first, last, kernendpfn;
179 char *kernend;
180 #if NKSYMS > 0 || defined(DDB) || defined(MODULAR)
181 char *ssym = NULL;
182 char *esym = NULL;
183 struct btinfo_symtab *bi_syms;
184 #endif
185
186 /* set up bootinfo structures */
187 if (bim == BOOTINFO_MAGIC && bip != NULL) {
188 struct btinfo_magic *bi_magic;
189
190 memcpy(bi_buf, bip, BOOTINFO_SIZE);
191 bootinfo = bi_buf;
192 bi_magic = lookup_bootinfo(BTINFO_MAGIC);
193 if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC)
194 bootinfo_msg =
195 "invalid magic number in bootinfo structure.\n";
196 else
197 bootinfo_msg = "bootinfo found.\n";
198 } else
199 bootinfo_msg = "no bootinfo found. (old bootblocks?)\n";
200
201 /* clear the BSS segment in kernel code */
202 #if NKSYMS > 0 || defined(DDB) || defined(MODULAR)
203 bi_syms = lookup_bootinfo(BTINFO_SYMTAB);
204
205 /* check whether there is valid bootinfo symtab info */
206 if (bi_syms != NULL) {
207 ssym = (char *)bi_syms->ssym;
208 esym = (char *)bi_syms->esym;
209 kernend = (void *)mips_round_page(esym);
210 #if 0
211 /*
212 * Don't clear BSS here since bi_buf[] is allocated in BSS
213 * and it has been cleared by the bootloader in this case.
214 */
215 memset(edata, 0, end - edata);
216 #endif
217 } else
218 #endif
219 {
220 kernend = (void *)mips_round_page(end);
221 memset(edata, 0, kernend - edata);
222 }
223
224 environment = &argv[1];
225
226 if (bios_ident()) { /* ARC BIOS present */
227 bios_init_console();
228 bios_save_info();
229 physmem = bios_configure_memory(mem_reserved, mem_clusters,
230 &mem_cluster_cnt);
231 }
232
233 /* Initialize the CPU type */
234 ident_platform();
235 if (platform == NULL) {
236 /* This is probably the best we can do... */
237 printf("kernel not configured for this system:\n");
238 printf("ID [%s] Vendor [%8.8s] Product [%02x",
239 arc_id, arc_vendor_id, arc_product_id[0]);
240 for (i = 1; i < sizeof(arc_product_id); i++)
241 printf(":%02x", arc_product_id[i]);
242 printf("]\n");
243 printf("DisplayController [%s]\n", arc_displayc_id);
244 #if 1
245 for (;;)
246 ;
247 #else
248 cpu_reboot(RB_HALT | RB_NOSYNC, NULL);
249 #endif
250 }
251
252 physmem = btoc(physmem);
253
254 /* compute bootdev for autoconfig setup */
255 cp = firmware_getenv("OSLOADPARTITION");
256 makebootdev(cp != NULL ? cp : argv[0]);
257
258 /*
259 * Look at arguments passed to us and compute boothowto.
260 * Default to SINGLE and ASKNAME if no args or
261 * SINGLE and DFLTROOT if this is a ramdisk kernel.
262 */
263 #ifdef MEMORY_DISK_IS_ROOT
264 boothowto = RB_SINGLE;
265 #else
266 boothowto = RB_SINGLE | RB_ASKNAME;
267 #endif /* MEMORY_DISK_IS_ROOT */
268 #ifdef KADB
269 boothowto |= RB_KDB;
270 #endif
271
272 cp = firmware_getenv("OSLOADOPTIONS");
273 if (cp) {
274 while (*cp) {
275 switch (*cp++) {
276 case 'a': /* autoboot */
277 boothowto &= ~RB_SINGLE;
278 break;
279
280 case 'm': /* mini root present in memory */
281 boothowto |= RB_MINIROOT;
282 break;
283
284 case 'n': /* ask for names */
285 boothowto |= RB_ASKNAME;
286 break;
287
288 case 'N': /* don't ask for names */
289 boothowto &= ~RB_ASKNAME;
290 break;
291
292 case 's': /* use serial console */
293 com_console = 1;
294 break;
295
296 case 'q': /* boot quietly */
297 boothowto |= AB_QUIET;
298 break;
299
300 case 'v': /* boot verbosely */
301 boothowto |= AB_VERBOSE;
302 break;
303 }
304
305 }
306 }
307
308 uvm_md_init();
309
310 /* make sure that we don't call BIOS console from now on */
311 cn_tab = NULL;
312
313 /*
314 * Copy exception-dispatch code down to exception vector.
315 * Initialize locore-function vector.
316 * Clear out the I and D caches.
317 *
318 * Now its time to abandon the BIOS and be self supplying.
319 * Start with cleaning out the TLB. Bye bye Microsoft....
320 *
321 * This may clobber PTEs needed by the BIOS.
322 */
323 mips_vector_init(NULL, false);
324
325 /*
326 * Map critical I/O spaces (e.g. for console printf(9)) on KSEG2.
327 * We cannot call VM functions here, since uvm is not initialized,
328 * yet.
329 * Since printf(9) is called before uvm_init() in main(),
330 * we have to handcraft console I/O space anyway.
331 *
332 * XXX - reserve these KVA space after UVM initialization.
333 */
334 (*platform->init)();
335
336 cpuspeed = platform->clock;
337 curcpu()->ci_cpu_freq = platform->clock * 1000000;
338 curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
339 curcpu()->ci_divisor_delay =
340 ((curcpu()->ci_cpu_freq + 500000) / 1000000);
341 curcpu()->ci_cctr_freq = curcpu()->ci_cpu_freq;
342 if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT) {
343 curcpu()->ci_cycles_per_hz /= 2;
344 curcpu()->ci_divisor_delay /= 2;
345 curcpu()->ci_cctr_freq /= 2;
346 }
347 cpu_setmodel("%s %s%s",
348 platform->vendor, platform->model, platform->variant);
349
350 /*
351 * Check to see if a mini-root was loaded into memory. It resides
352 * at the start of the next page just after the end of BSS.
353 */
354 if (boothowto & RB_MINIROOT)
355 kernend += round_page(mfs_initminiroot(kernend));
356
357 #if NKSYMS || defined(DDB) || defined(MODULAR)
358 /* init symbols if present */
359 if (esym)
360 ksyms_addsyms_elf(esym - ssym, ssym, esym);
361 #endif
362
363 maxmem = physmem;
364
365 /* XXX: revisit here */
366
367 /*
368 * Load the rest of the pages into the VM system.
369 */
370 kernendpfn = atop(round_page(MIPS_KSEG0_TO_PHYS(kernend)));
371 #if 0
372 kernstartpfn = atop(trunc_page(
373 MIPS_KSEG0_TO_PHYS((kernel_text) - UPAGES * PAGE_SIZE)));
374 /* give all free memory to VM */
375 /* XXX - currently doesn't work, due to "panic: pmap_enter: pmap" */
376 for (i = 0; i < mem_cluster_cnt; i++) {
377 if (mem_reserved[i])
378 continue;
379 first = atop(round_page(mem_clusters[i].start));
380 last = atop(trunc_page(mem_clusters[i].start +
381 mem_clusters[i].size));
382 if (last <= kernstartpfn || kernendpfn <= first) {
383 uvm_page_physload(first, last, first, last,
384 VM_FREELIST_DEFAULT);
385 } else {
386 if (first < kernstartpfn)
387 uvm_page_physload(first, kernstartpfn,
388 first, kernstartpfn, VM_FREELIST_DEFAULT);
389 if (kernendpfn < last)
390 uvm_page_physload(kernendpfn, last,
391 kernendpfn, last, , VM_FREELIST_DEFAULT);
392 }
393 }
394 #elif 0 /* XXX */
395 /* give all free memory above the kernel to VM (non-contig version) */
396 for (i = 0; i < mem_cluster_cnt; i++) {
397 if (mem_reserved[i])
398 continue;
399 first = atop(round_page(mem_clusters[i].start));
400 last = atop(trunc_page(mem_clusters[i].start +
401 mem_clusters[i].size));
402 if (kernendpfn < last) {
403 if (first < kernendpfn)
404 first = kernendpfn;
405 uvm_page_physload(first, last, first, last,
406 VM_FREELIST_DEFAULT);
407 }
408 }
409 #else
410 /* give all memory above the kernel to VM (contig version) */
411 #if 1
412 mem_clusters[0].start = 0;
413 mem_clusters[0].size = ctob(physmem);
414 mem_cluster_cnt = 1;
415 #endif
416
417 first = kernendpfn;
418 last = physmem;
419 uvm_page_physload(first, last, first, last, VM_FREELIST_DEFAULT);
420 #endif
421
422 /*
423 * Initialize error message buffer (at end of core).
424 */
425 mips_init_msgbuf();
426
427 /*
428 * Initialize the virtual memory system.
429 */
430 pmap_bootstrap();
431
432 /*
433 * Allocate uarea page for lwp0 and set it.
434 */
435 mips_init_lwp0_uarea();
436 }
437
438 void
mips_machdep_cache_config(void)439 mips_machdep_cache_config(void)
440 {
441
442 mips_cache_info.mci_sdcache_size = arc_cpu_l2cache_size;
443 }
444
445 /*
446 * Return a pointer to the given environment variable.
447 */
448 const char *
firmware_getenv(const char * envname)449 firmware_getenv(const char *envname)
450 {
451 char **env;
452 int l;
453
454 l = strlen(envname);
455
456 for (env = environment; env[0]; env++) {
457 if (strncasecmp(envname, env[0], l) == 0 && env[0][l] == '=') {
458 return &env[0][l + 1];
459 }
460 }
461 return NULL;
462 }
463
464 /*
465 * Console initialization: called early on from main,
466 * before vm init or startup. Do enough configuration
467 * to choose and initialize a console.
468 */
469 void
consinit(void)470 consinit(void)
471 {
472 static int initted;
473
474 if (initted)
475 return;
476 initted = 1;
477
478 (*platform->cons_init)();
479 }
480
481 /*
482 * cpu_startup: allocate memory for variable-sized tables,
483 * initialize CPU, and do autoconfiguration.
484 */
485 void
cpu_startup(void)486 cpu_startup(void)
487 {
488 #ifdef BOOTINFO_DEBUG
489 if (bootinfo_msg)
490 printf(bootinfo_msg);
491 #endif
492
493 cpu_startup_common();
494 }
495
496 void *
lookup_bootinfo(int type)497 lookup_bootinfo(int type)
498 {
499 struct btinfo_common *bt;
500 char *bip;
501
502 /* check for a bootinfo record first */
503 if (bootinfo == NULL)
504 return NULL;
505
506 bip = bootinfo;
507 do {
508 bt = (struct btinfo_common *)bip;
509 if (bt->type == type)
510 return (void *)bt;
511 bip += bt->next;
512 } while (bt->next != 0 &&
513 bt->next < BOOTINFO_SIZE /* sanity */ &&
514 (size_t)bip < (size_t)bootinfo + BOOTINFO_SIZE);
515
516 return NULL;
517 }
518
519 static int waittime = -1;
520
521 void
cpu_reboot(int howto,char * bootstr)522 cpu_reboot(int howto, char *bootstr)
523 {
524
525 /* take a snap shot before clobbering any registers */
526 savectx(curpcb);
527
528 #ifdef DEBUG
529 if (panicstr)
530 stacktrace();
531 #endif
532
533 boothowto = howto;
534 if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
535 /* fill curlwp with live object */
536 if (curlwp == NULL)
537 curlwp = &lwp0;
538 /*
539 * Synchronize the disks....
540 */
541 waittime = 0;
542 vfs_shutdown();
543 }
544 (void)splhigh(); /* extreme priority */
545
546 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
547 dumpsys();
548
549 doshutdownhooks();
550
551 pmf_system_shutdown(boothowto);
552
553 if (howto & RB_HALT) {
554 printf("\n");
555 printf("The operating system has halted.\n");
556 printf("Please press any key to reboot.\n\n");
557 cnpollc(1); /* for proper keyboard command handling */
558 cngetc();
559 cnpollc(0);
560 }
561
562 printf("rebooting...\n");
563 delay(1000000);
564
565 (*platform->reset)();
566
567 __asm(" li $2, 0xbfc00000; jr $2; nop\n");
568 for (;;)
569 ; /* Forever */
570 /* NOTREACHED */
571 }
572
573 /*
574 * Pass system reset command to keyboard controller (8042).
575 */
576 void
arc_sysreset(bus_addr_t addr,bus_size_t cmd_offset)577 arc_sysreset(bus_addr_t addr, bus_size_t cmd_offset)
578 {
579 volatile uint8_t *kbdata = (uint8_t *)addr + KBDATAP;
580 volatile uint8_t *kbcmd = (uint8_t *)addr + cmd_offset;
581
582 #define KBC_ARC_SYSRESET 0xd1
583
584 delay(1000);
585 *kbcmd = KBC_ARC_SYSRESET;
586 delay(1000);
587 *kbdata = 0;
588 }
589