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