Home | History | Annotate | Line # | Download | only in sbmips
      1 /* $NetBSD: machdep.c,v 1.59 2025/12/20 10:51:05 skrll Exp $ */
      2 
      3 /*
      4  * Copyright 2000, 2001
      5  * Broadcom Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and copied only
      8  * in accordance with the following terms and conditions.  Subject to these
      9  * conditions, you may download, copy, install, use, modify and distribute
     10  * modified or unmodified copies of this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce and
     14  *    retain this copyright notice and list of conditions as they appear in
     15  *    the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
     19  *    used to endorse or promote products derived from this software
     20  *    without the prior written permission of Broadcom Corporation.
     21  *
     22  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
     23  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
     24  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     25  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
     26  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
     27  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     31  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     32  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 2000 Soren S. Jorvang.  All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions, and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  */
     59 
     60 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.59 2025/12/20 10:51:05 skrll Exp $");
     62 
     63 #include "opt_ddb.h"
     64 #include "opt_execfmt.h"
     65 #include "opt_modular.h"
     66 
     67 #include <sys/param.h>
     68 #include <sys/buf.h>
     69 #include <sys/conf.h>
     70 #include <sys/cpu.h>
     71 #include <sys/device.h>
     72 #include <sys/exec.h>
     73 #include <sys/file.h>
     74 #include <sys/intr.h>
     75 #include <sys/kcore.h>
     76 #include <sys/kernel.h>
     77 #include <sys/ksyms.h>
     78 #include <sys/mbuf.h>
     79 #include <sys/mount.h>
     80 #include <sys/msgbuf.h>
     81 #include <sys/proc.h>
     82 #include <sys/reboot.h>
     83 #include <sys/syscallargs.h>
     84 #include <sys/systm.h>
     85 
     86 #include <uvm/uvm_extern.h>
     87 
     88 #include <mips/locore.h>
     89 #include <mips/psl.h>
     90 #include <mips/pte.h>
     91 #include <mips/reg.h>
     92 
     93 #include <mips/cfe/cfe_api.h>
     94 
     95 #include <sbmips/autoconf.h>
     96 #include <sbmips/swarm.h>
     97 
     98 #if 0 /* XXXCGD */
     99 #include <sbmips/nvram.h>
    100 #endif /* XXXCGD */
    101 #include <sbmips/leds.h>
    102 
    103 #include <mips/sibyte/dev/sbbuswatchvar.h>
    104 
    105 #include "ksyms.h"
    106 
    107 #if NKSYMS || defined(DDB) || defined(MODULAR)
    108 #include <mips/db_machdep.h>
    109 #include <ddb/db_access.h>
    110 #include <ddb/db_sym.h>
    111 #include <ddb/db_extern.h>
    112 #include <sys/exec_elf.h>
    113 #endif
    114 
    115 #include <dev/cons.h>
    116 
    117 #if NKSYMS || defined(DDB) || defined(MODULAR)
    118 /* start and end of kernel symbol table */
    119 void	*ksym_start, *ksym_end;
    120 #endif
    121 
    122 /* Maps for VM objects. */
    123 struct vm_map *phys_map = NULL;
    124 
    125 char	bootstring[512];	/* Boot command */
    126 int	netboot;		/* Are we netbooting? */
    127 int	cfe_present;
    128 
    129 struct bootinfo_v1 bootinfo;
    130 
    131 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    132 int mem_cluster_cnt;
    133 
    134 void	configure(void);
    135 void	mach_init(long, long, long, long);
    136 
    137 extern void *esym;
    138 
    139 /*
    140  * Do all the stuff that locore normally does before calling main().
    141  */
    142 void
    143 mach_init(long fwhandle, long magic, long bootdata, long reserved)
    144 {
    145 	void *kernend;
    146 	extern char edata[], end[];
    147 	uint32_t config;
    148 
    149 	/* XXX this code must run on the target CPU */
    150 	config = mips3_cp0_config_read();
    151 	config &= ~MIPS3_CONFIG_K0_MASK;
    152 	config |= 0x05;				/* XXX.  cacheable coherent */
    153 	mips3_cp0_config_write(config);
    154 
    155 	/* Zero BSS.  XXXCGD: uh, is this really necessary still?  */
    156 	memset(edata, 0, end - edata);
    157 
    158 	/*
    159 	 * Copy the bootinfo structure from the boot loader.
    160 	 * this has to be done before mips_vector_init is
    161 	 * called because we may need CFE's TLB handler
    162 	 */
    163 
    164 	if (magic == BOOTINFO_MAGIC)
    165 		memcpy(&bootinfo, (struct bootinfo_v1 *)bootdata,
    166 		    sizeof bootinfo);
    167 	else if (reserved == CFE_EPTSEAL) {
    168 		magic = BOOTINFO_MAGIC;
    169 		memset(&bootinfo, 0, sizeof bootinfo);
    170 		bootinfo.version = BOOTINFO_VERSION;
    171 		bootinfo.fwhandle = fwhandle;
    172 		bootinfo.fwentry = bootdata;
    173 		bootinfo.ssym = (vaddr_t)end;
    174 		bootinfo.esym = (vaddr_t)end;
    175 	}
    176 
    177 	kernend = (void *)mips_round_page(end);
    178 #if NKSYMS || defined(DDB) || defined(MODULAR)
    179 	if (magic == BOOTINFO_MAGIC) {
    180 		ksym_start = (void *)(intptr_t)bootinfo.ssym;
    181 		ksym_end   = (void *)(intptr_t)bootinfo.esym;
    182 		kernend = (void *)mips_round_page((vaddr_t)ksym_end);
    183 	}
    184 #endif
    185 
    186 	consinit();
    187 
    188 	uvm_md_init();
    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 #ifdef MULTIPROCESSOR
    196 	mips_vector_init(NULL, true);
    197 #else
    198 	mips_vector_init(NULL, false);
    199 #endif
    200 
    201 	mips_locoresw.lsw_bus_error = sibyte_bus_watch_check;
    202 
    203 	sb1250_ipl_map_init();
    204 
    205 #ifdef DEBUG
    206 	printf("fwhandle=%08X magic=%08X bootdata=%08X reserved=%08X\n",
    207 	    (u_int)fwhandle, (u_int)magic, (u_int)bootdata, (u_int)reserved);
    208 #endif
    209 
    210 	cpu_setmodel("sb1250");
    211 
    212 	if (magic == BOOTINFO_MAGIC) {
    213 		int idx;
    214 		int added;
    215 		uint64_t start, len, type;
    216 
    217 		cfe_init(bootinfo.fwhandle, bootinfo.fwentry);
    218 		cfe_present = 1;
    219 
    220 		idx = 0;
    221 		physmem = 0;
    222 		mem_cluster_cnt = 0;
    223 		while (cfe_enummem(idx, 0, &start, &len, &type) == 0) {
    224 			added = 0;
    225 			printf("Memory Block #%d start %08"PRIx64"X len %08"PRIx64"X: %s: ",
    226 			    idx, start, len, (type == CFE_MI_AVAILABLE) ?
    227 			    "Available" : "Reserved");
    228 			if ((type == CFE_MI_AVAILABLE) &&
    229 			    (mem_cluster_cnt < VM_PHYSSEG_MAX)) {
    230 				/*
    231 				 * XXX Ignore memory above 256MB for now, it
    232 				 * XXX needs special handling.
    233 				 */
    234 				if (start < (256*1024*1024)) {
    235 				    physmem += btoc(((int) len));
    236 				    mem_clusters[mem_cluster_cnt].start =
    237 					(long) start;
    238 				    mem_clusters[mem_cluster_cnt].size =
    239 					(long) len;
    240 				    mem_cluster_cnt++;
    241 				    added = 1;
    242 				}
    243 			}
    244 			if (added)
    245 				printf("added to map\n");
    246 			else
    247 				printf("not added to map\n");
    248 			idx++;
    249 		}
    250 
    251 	} else {
    252 		/*
    253 		 * Handle the case of not being called from the firmware.
    254 		 */
    255 		/* XXX hardwire to 32MB; should be kernel config option */
    256 		physmem = 32 * 1024 * 1024 / 4096;
    257 		mem_clusters[0].start = 0;
    258 		mem_clusters[0].size = ctob(physmem);
    259 		mem_cluster_cnt = 1;
    260 	}
    261 
    262 
    263 	for (u_int i = 0; i < sizeof(bootinfo.boot_flags); i++) {
    264 		switch (bootinfo.boot_flags[i]) {
    265 		case '\0':
    266 			break;
    267 		case ' ':
    268 			continue;
    269 		case '-':
    270 			while (bootinfo.boot_flags[i] != ' ' &&
    271 			    bootinfo.boot_flags[i] != '\0') {
    272 				switch (bootinfo.boot_flags[i]) {
    273 				case 'a':
    274 					boothowto |= RB_ASKNAME;
    275 					break;
    276 				case 'd':
    277 					boothowto |= RB_KDB;
    278 					break;
    279 				case 's':
    280 					boothowto |= RB_SINGLE;
    281 					break;
    282 				}
    283 				i++;
    284 			}
    285 		}
    286 	}
    287 
    288 	/*
    289 	 * Load the rest of the available pages into the VM system.
    290 	 */
    291 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t) kernend,
    292 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    293 
    294 	/*
    295 	 * Initialize error message buffer (at end of core).
    296 	 */
    297 	mips_init_msgbuf();
    298 
    299 	pmap_bootstrap();
    300 
    301 	/*
    302 	 * Allocate uarea for lwp0 and set it.
    303 	 */
    304 	mips_init_lwp0_uarea();
    305 
    306 	/*
    307 	 * Initialize debuggers, and break into them, if appropriate.
    308 	 */
    309 #if NKSYMS || defined(DDB) || defined(MODULAR)
    310 	ksyms_addsyms_elf(((uintptr_t)ksym_end - (uintptr_t)ksym_start),
    311 	    ksym_start, ksym_end);
    312 #endif
    313 
    314 	if (boothowto & RB_KDB) {
    315 #if defined(DDB)
    316 		Debugger();
    317 #endif
    318 	}
    319 
    320 #ifdef MULTIPROCESSOR
    321 	mips_fixup_exceptions(mips_fixup_zero_relative, NULL);
    322 #endif
    323 }
    324 
    325 /*
    326  * Allocate memory for variable-sized tables,
    327  */
    328 void
    329 cpu_startup(void)
    330 {
    331 	/*
    332 	 * Just do the common stuff.
    333 	 */
    334 	cpu_startup_common();
    335 }
    336 
    337 int	waittime = -1;
    338 
    339 void
    340 cpu_reboot(int howto, char *bootstr)
    341 {
    342 
    343 	/* Take a snapshot before clobbering any registers. */
    344 	savectx(curpcb);
    345 
    346 	if (cold) {
    347 		howto |= RB_HALT;
    348 		goto haltsys;
    349 	}
    350 
    351 	/* If "always halt" was specified as a boot flag, obey. */
    352 	if (boothowto & RB_HALT)
    353 		howto |= RB_HALT;
    354 
    355 	boothowto = howto;
    356 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    357 		waittime = 0;
    358 		vfs_shutdown();
    359 	}
    360 
    361 	splhigh();
    362 
    363 	if (howto & RB_DUMP)
    364 		dumpsys();
    365 
    366 haltsys:
    367 	doshutdownhooks();
    368 
    369 	pmf_system_shutdown(boothowto);
    370 
    371 	if (howto & RB_HALT) {
    372 		printf("\n");
    373 		printf("The operating system has halted.\n");
    374 		printf("Please press any key to reboot.\n\n");
    375 		cnpollc(true);	/* For proper keyboard command handling */
    376 		cngetc();
    377 		cnpollc(false);
    378 	}
    379 
    380 	printf("rebooting...\n\n");
    381 
    382 	if (cfe_present) {
    383 		/*
    384 		 * XXX
    385 		 * For some reason we can't return to CFE with
    386 		 * and do a warm start.  Need to look into this...
    387 		 */
    388 		cfe_exit(0, (howto & RB_DUMP) ? 1 : 0);
    389 		printf("cfe_exit didn't!\n");
    390 	}
    391 
    392 	printf("WARNING: reboot failed!\n");
    393 
    394 	for (;;);
    395 }
    396 
    397 static void
    398 cswarm_setled(u_int index, char c)
    399 {
    400 	volatile u_char *led_ptr =
    401 	    (void *)MIPS_PHYS_TO_KSEG1(SWARM_LEDS_PHYS);
    402 
    403 	if (index < 4)
    404 		led_ptr[0x20 + ((3 - index) << 3)] = c;
    405 }
    406 
    407 void
    408 cswarm_setleds(const char *str)
    409 {
    410 	int i;
    411 
    412 	for (i = 0; i < 4 && str[i]; i++)
    413 		cswarm_setled(i, str[i]);
    414 	for (; i < 4; i++)
    415 		cswarm_setled(' ', str[i]);
    416 }
    417 
    418 int
    419 sbmips_cca_for_pa(paddr_t pa)
    420 {
    421 	int rv;
    422 
    423 	rv = 2;			/* Uncached. */
    424 
    425 	/* Check each DRAM region. */
    426 	if ((pa >= 0x0000000000   && pa <= 0x000fffffff) ||	/* DRAM 0 */
    427 	    (pa >= 0x0080000000   && pa <= 0x008fffffff) ||	/* DRAM 1 */
    428 	    (pa >= 0x0090000000   && pa <= 0x009fffffff) ||	/* DRAM 2 */
    429 	    (pa >= 0x00c0000000   && pa <= 0x00cfffffff) ||	/* DRAM 3 */
    430 #ifdef _MIPS_PADDR_T_64BIT
    431 	    (pa >= 0x0100000000LL && pa <= 0x07ffffffffLL) ||	/* DRAM exp */
    432 #endif
    433 	   0) {
    434 		rv = 5;		/* Cacheable coherent. */
    435 	}
    436 
    437 	return (rv);
    438 }
    439