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