#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.123 2024/03/05 14:15:33 thorpej Exp $");
#include "opt_ddb.h"
#include "opt_execfmt.h"
#include "opt_modular.h"
#define __INTR_PRIVATE
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signalvar.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/mbuf.h>
#include <sys/msgbuf.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/exec.h>
#include <sys/mount.h>
#include <sys/syscallargs.h>
#include <sys/kcore.h>
#include <sys/ksyms.h>
#include <sys/cpu.h>
#include <sys/intr.h>
#include <uvm/uvm_extern.h>
#include <ufs/mfs/mfs_extern.h>
#include <mips/cache.h>
#include <mips/locore.h>
#include <machine/reg.h>
#include <machine/psl.h>
#include <machine/pte.h>
#include <machine/autoconf.h>
#include <machine/bootinfo.h>
#include <machine/apbus.h>
#include <machine/apcall.h>
#define _NEWSMIPS_BUS_DMA_PRIVATE
#include <machine/bus.h>
#ifdef DDB
#include <machine/db_machdep.h>
#include <ddb/db_access.h>
#include <ddb/db_extern.h>
#include <ddb/db_sym.h>
#endif
#include <machine/adrsmap.h>
#include <machine/machConst.h>
#include <newsmips/newsmips/machid.h>
#include <dev/cons.h>
#include "ksyms.h"
struct vm_map *phys_map = NULL;
char *bootinfo = NULL;
int systype;
struct apbus_sysinfo *_sip = NULL;
void *sccport0a;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
int mem_cluster_cnt;
struct idrom idrom;
void (*hardware_intr)(int, vaddr_t, uint32_t);
void (*enable_intr)(void);
void (*disable_intr)(void);
void (*enable_timer)(void);
void mach_init(int, int, int, int);
void prom_halt(int) __attribute__((__noreturn__));
void to_monitor(int) __attribute__((__noreturn__));
#ifdef DEBUG
extern void stacktrace(void);
#endif
extern u_long bootdev;
extern char edata[], end[];
void
mach_init(int x_boothowto, int x_bootdev, int x_bootname, int x_maxmem)
{
u_long first, last;
char *kernend;
const char *model;
struct btinfo_magic *bi_magic;
struct btinfo_bootarg *bi_arg;
struct btinfo_systype *bi_systype;
#if NKSYMS || defined(DDB) || defined(MODULAR)
struct btinfo_symtab *bi_sym;
int nsym = 0;
char *ssym, *esym;
ssym = esym = NULL;
#endif
bi_arg = NULL;
bootinfo = (void *)BOOTINFO_ADDR;
bi_magic = lookup_bootinfo(BTINFO_MAGIC);
if (bi_magic && bi_magic->magic == BOOTINFO_MAGIC) {
bi_arg = lookup_bootinfo(BTINFO_BOOTARG);
if (bi_arg) {
x_boothowto = bi_arg->howto;
x_bootdev = bi_arg->bootdev;
x_maxmem = bi_arg->maxmem;
}
#if NKSYMS || defined(DDB) || defined(MODULAR)
bi_sym = lookup_bootinfo(BTINFO_SYMTAB);
if (bi_sym) {
nsym = bi_sym->nsym;
ssym = (void *)bi_sym->ssym;
esym = (void *)bi_sym->esym;
}
#endif
bi_systype = lookup_bootinfo(BTINFO_SYSTYPE);
if (bi_systype)
systype = bi_systype->type;
} else {
memset(edata, 0, end - edata);
}
if (systype == 0)
systype = NEWS3400;
#ifdef news3400
if (systype == NEWS3400) {
sccport0a = (void *)SCCPORT0A;
}
#endif
#if defined(news5000) || defined(news4000)
if (systype == NEWS5000 || systype == NEWS4000) {
int i;
char *bspec = (char *)x_bootdev;
if (bi_arg == NULL)
panic("%s requires BTINFO_BOOTARG to boot",
systype == NEWS5000 ? "news5000" : "news4000");
_sip = (void *)bi_arg->sip;
x_maxmem = _sip->apbsi_memsize;
x_maxmem -= 0x00100000;
if (strncmp(bspec, "scsi", 4) == 0) {
x_bootdev = (5 << 28) | 0;
bspec += 4;
if (*bspec != '(' )
goto bootspec_end;
i = strtoul(bspec + 1, &bspec, 10);
x_bootdev |= (i << 24);
if (*bspec != ',')
goto bootspec_end;
i = strtoul(bspec + 1, &bspec, 10);
x_bootdev |= (i / 10) << 20;
x_bootdev |= (i % 10) << 16;
if (*bspec != ',')
goto bootspec_end;
i = strtoul(bspec + 1, &bspec, 10);
x_bootdev |= (i << 8);
}
bootspec_end:
sccport0a = (systype == NEWS5000) ?
(void *)NEWS5000_SCCPORT0A : (void *)NEWS4000_SCCPORT0A;
consinit();
}
#endif
*(int *)(MIPS_PHYS_TO_KSEG1(MACH_MAXMEMSIZE_ADDR)) = x_maxmem;
*(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTDEV_ADDR)) = x_bootdev;
*(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTSW_ADDR)) = x_boothowto;
kernend = (char *)mips_round_page(end);
#if NKSYMS || defined(DDB) || defined(MODULAR)
if (nsym)
kernend = (char *)mips_round_page(esym);
#endif
uvm_md_init();
boothowto = x_boothowto;
bootdev = x_bootdev;
physmem = btoc(x_maxmem);
mem_clusters[0].start = 0;
mem_clusters[0].size = ctob(physmem);
mem_cluster_cnt = 1;
mips_vector_init(NULL, false);
newsmips_bus_dma_init();
#if NKSYMS || defined(DDB) || defined(MODULAR)
if (nsym)
ksyms_addsyms_elf(esym - ssym, ssym, esym);
#endif
#ifdef KADB
boothowto |= RB_KDB;
#endif
if (boothowto & RB_MINIROOT)
kernend += round_page(mfs_initminiroot(kernend));
first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
last = mem_clusters[0].start + mem_clusters[0].size;
uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
VM_FREELIST_DEFAULT);
mips_init_msgbuf();
pmap_bootstrap();
mips_init_lwp0_uarea();
switch (systype) {
#ifdef news3400
case NEWS3400:
news3400_init();
cpu_setmodel("%s", idrom.id_machine);
model = cpu_getmodel();
if (strcmp(model, "news3400") == 0 ||
strcmp(model, "news3200") == 0 ||
strcmp(model, "news3700") == 0) {
hardware_intr = news3400_intr;
cpuspeed = 10;
} else {
printf("kernel not configured for machine %s\n", model);
}
break;
#endif
#ifdef news5000
case NEWS5000:
news5000_init();
cpu_setmodel("%s", idrom.id_machine);
model = cpu_getmodel();
if (strcmp(model, "news5000") == 0 ||
strcmp(model, "news5900") == 0) {
hardware_intr = news5000_intr;
cpuspeed = 50;
} else {
printf("kernel not configured for machine %s\n", model);
}
break;
#endif
#ifdef news4000
case NEWS4000:
news4000_init();
cpu_setmodel("%s", idrom.id_machine);
model = cpu_getmodel();
if (strcmp(model, "news4000") == 0) {
hardware_intr = news4000_intr;
cpuspeed = 40;
} else {
printf("kernel not configured for machine %s\n", model);
}
break;
#endif
default:
printf("kernel not configured for systype %d\n", systype);
break;
}
}
void
mips_machdep_cache_config(void)
{
if (CPUISMIPS3)
mips_cache_info.mci_sdcache_size = 1024 * 1024;
}
void
cpu_startup(void)
{
printf("SONY NET WORK STATION, Model %s, ", idrom.id_model);
printf("Machine ID #%d\n", idrom.id_serial);
cpu_startup_common();
}
void *
lookup_bootinfo(int type)
{
struct btinfo_common *bt;
char *help = bootinfo;
if (help == NULL)
return NULL;
do {
bt = (struct btinfo_common *)help;
if (bt->type == type)
return (void *)help;
help += bt->next;
} while (bt->next != 0 &&
(size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
return NULL;
}
void
prom_halt(int howto)
{
#if defined(news5000) || defined(news4000)
if (systype == NEWS5000 || systype == NEWS4000)
apcall_exit(howto);
#endif
#ifdef news3400
if (systype == NEWS3400)
to_monitor(howto);
#endif
for (;;);
}
int waittime = -1;
void
cpu_reboot(volatile int howto, char *bootstr)
{
savectx(curpcb);
#ifdef DEBUG
if (panicstr)
stacktrace();
#endif
if (cold) {
howto |= RB_HALT;
goto haltsys;
}
if ((boothowto & RB_HALT) != 0)
howto |= RB_HALT;
boothowto = howto;
if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
waittime = 0;
vfs_shutdown();
}
disable_intr();
splhigh();
#if 0
if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
#else
if (howto & RB_DUMP)
#endif
dumpsys();
haltsys:
doshutdownhooks();
pmf_system_shutdown(boothowto);
if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
prom_halt(0x80);
printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
prom_halt(howto & RB_HALT);
}
void
delay(int n)
{
DELAY(n);
}
void
cpu_intr(int ppl, vaddr_t pc, uint32_t status)
{
curcpu()->ci_data.cpu_nintr++;
(*hardware_intr)(ppl, pc, status);
}