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