#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.9 2025/12/20 10:51:02 skrll Exp $");
#include "opt_ddb.h"
#include "opt_execfmt.h"
#include "opt_modular.h"
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/exec.h>
#include <sys/file.h>
#include <sys/intr.h>
#include <sys/kcore.h>
#include <sys/kernel.h>
#include <sys/ksyms.h>
#include <sys/mbuf.h>
#include <sys/mount.h>
#include <sys/msgbuf.h>
#include <sys/proc.h>
#include <sys/reboot.h>
#include <sys/syscallargs.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <mips/locore.h>
#include <mips/psl.h>
#include <mips/pte.h>
#include <mips/reg.h>
#include <mips/cfe/cfe_api.h>
#include <evbmips/sbmips/autoconf.h>
#include <evbmips/sbmips/swarm.h>
#include <evbmips/sbmips/systemsw.h>
#if 0
#include <evbmips/sbmips/nvram.h>
#endif
#include <evbmips/sbmips/leds.h>
#include <mips/sibyte/dev/sbbuswatchvar.h>
#include "ksyms.h"
#if NKSYMS || defined(DDB) || defined(MODULAR)
#include <mips/db_machdep.h>
#include <ddb/db_access.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#include <sys/exec_elf.h>
#endif
#include <dev/cons.h>
#if NKSYMS || defined(DDB) || defined(MODULAR)
void *ksym_start, *ksym_end;
#endif
struct vm_map *phys_map = NULL;
char bootstring[512];
int netboot;
int cfe_present;
struct bootinfo_v1 bootinfo;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
int mem_cluster_cnt;
void configure(void);
void mach_init(long, long, long, long);
extern void *esym;
#ifdef _LP64
#define CFE_TO_KERNEL_PTR(x) MIPS_PHYS_TO_KSEG0(MIPS_KSEG0_TO_PHYS(x))
#else
#define CFE_TO_KERNEL_PTR(x) (x)
#endif
void
mach_init(long fwhandle, long magic, long bootdata, long reserved)
{
void *kernend;
extern char edata[], end[];
uint32_t config;
config = mips3_cp0_config_read();
config &= ~MIPS3_CONFIG_K0_MASK;
config |= 0x05;
mips3_cp0_config_write(config);
memset(edata, 0, end - edata);
if (magic == BOOTINFO_MAGIC)
memcpy(&bootinfo, (struct bootinfo_v1 *)bootdata,
sizeof bootinfo);
else if (reserved == CFE_EPTSEAL) {
magic = BOOTINFO_MAGIC;
memset(&bootinfo, 0, sizeof bootinfo);
bootinfo.version = BOOTINFO_VERSION;
bootinfo.fwhandle = fwhandle;
bootinfo.fwentry = bootdata;
bootinfo.ssym = (vaddr_t)end;
bootinfo.esym = (vaddr_t)end;
}
kernend = (void *)mips_round_page(end);
#if NKSYMS || defined(DDB) || defined(MODULAR)
if (magic == BOOTINFO_MAGIC) {
ksym_start = (void *)CFE_TO_KERNEL_PTR(bootinfo.ssym);
ksym_end = (void *)CFE_TO_KERNEL_PTR(bootinfo.esym);
kernend = (void *)mips_round_page((vaddr_t)ksym_end);
}
#endif
consinit();
uvm_md_init();
#ifdef MULTIPROCESSOR
mips_vector_init(NULL, true);
#else
mips_vector_init(NULL, false);
#endif
mips_locoresw.lsw_bus_error = sibyte_bus_watch_check;
sb1250_ipl_map_init();
#ifdef DEBUG
printf("fwhandle=%08X magic=%08X bootdata=%08X reserved=%08X\n",
(u_int)fwhandle, (u_int)magic, (u_int)bootdata, (u_int)reserved);
#endif
cpu_setmodel("sb1250");
if (magic == BOOTINFO_MAGIC) {
int idx;
int added;
uint64_t start, len, type;
cfe_init(CFE_TO_KERNEL_PTR(bootinfo.fwhandle),
CFE_TO_KERNEL_PTR(bootinfo.fwentry));
cfe_present = 1;
idx = 0;
physmem = 0;
mem_cluster_cnt = 0;
while (cfe_enummem(idx, 0, &start, &len, &type) == 0) {
added = 0;
printf("Memory Block #%d start %08"PRIx64" len %08"PRIx64": %s: ",
idx, start, len, (type == CFE_MI_AVAILABLE) ?
"Available" : "Reserved");
if ((type == CFE_MI_AVAILABLE) &&
(mem_cluster_cnt < VM_PHYSSEG_MAX)) {
if (start < (256*1024*1024)) {
physmem += btoc(((int) len));
mem_clusters[mem_cluster_cnt].start =
(long) start;
mem_clusters[mem_cluster_cnt].size =
(long) len;
mem_cluster_cnt++;
added = 1;
}
}
if (added)
printf("added to map\n");
else
printf("not added to map\n");
idx++;
}
} else {
physmem = 32 * 1024 * 1024 / 4096;
mem_clusters[0].start = 0;
mem_clusters[0].size = ctob(physmem);
mem_cluster_cnt = 1;
}
for (u_int i = 0; i < sizeof(bootinfo.boot_flags); i++) {
switch (bootinfo.boot_flags[i]) {
case '\0':
break;
case ' ':
continue;
case '-':
while (bootinfo.boot_flags[i] != ' ' &&
bootinfo.boot_flags[i] != '\0') {
switch (bootinfo.boot_flags[i]) {
case 'a':
boothowto |= RB_ASKNAME;
break;
case 'd':
boothowto |= RB_KDB;
break;
case 's':
boothowto |= RB_SINGLE;
break;
}
i++;
}
}
}
mips_page_physload(MIPS_KSEG0_START, (vaddr_t) kernend,
mem_clusters, mem_cluster_cnt, NULL, 0);
mips_init_msgbuf();
pmap_bootstrap();
mips_init_lwp0_uarea();
#if NKSYMS || defined(DDB) || defined(MODULAR)
ksyms_addsyms_elf(((uintptr_t)ksym_end - (uintptr_t)ksym_start),
ksym_start, ksym_end);
#endif
if (boothowto & RB_KDB) {
#if defined(DDB)
Debugger();
#endif
}
#ifdef MULTIPROCESSOR
mips_fixup_exceptions(mips_fixup_zero_relative, NULL);
#endif
}
void
cpu_startup(void)
{
cpu_startup_common();
}
int waittime = -1;
void
cpu_reboot(int howto, char *bootstr)
{
savectx(curpcb);
if (cold) {
howto |= RB_HALT;
goto haltsys;
}
if (boothowto & RB_HALT)
howto |= RB_HALT;
boothowto = howto;
if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
waittime = 0;
vfs_shutdown();
}
splhigh();
if (howto & RB_DUMP)
dumpsys();
haltsys:
doshutdownhooks();
pmf_system_shutdown(boothowto);
if (howto & RB_HALT) {
printf("\n");
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cnpollc(true);
cngetc();
cnpollc(false);
}
printf("rebooting...\n\n");
if (cfe_present) {
cfe_exit(0, (howto & RB_DUMP) ? 1 : 0);
printf("cfe_exit didn't!\n");
}
printf("WARNING: reboot failed!\n");
for (;;);
}
static void
cswarm_setled(u_int index, char c)
{
volatile u_char *led_ptr =
(void *)MIPS_PHYS_TO_KSEG1(SWARM_LEDS_PHYS);
if (index < 4)
led_ptr[0x20 + ((3 - index) << 3)] = c;
}
void
cswarm_setleds(const char *str)
{
int i;
for (i = 0; i < 4 && str[i]; i++)
cswarm_setled(i, str[i]);
for (; i < 4; i++)
cswarm_setled(' ', str[i]);
}
int
sbmips_cca_for_pa(paddr_t pa)
{
int rv;
rv = 2;
if ((pa >= 0x0000000000 && pa <= 0x000fffffff) ||
(pa >= 0x0080000000 && pa <= 0x008fffffff) ||
(pa >= 0x0090000000 && pa <= 0x009fffffff) ||
(pa >= 0x00c0000000 && pa <= 0x00cfffffff) ||
#ifdef _MIPS_PADDR_T_64BIT
(pa >= 0x0100000000LL && pa <= 0x07ffffffffLL) ||
#endif
0) {
rv = 5;
}
return (rv);
}