#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.11 2025/12/21 07:00:26 skrll Exp $");
#include "opt_ddb.h"
#include "opt_execfmt.h"
#include "opt_modular.h"
#define _MIPS_BUS_DMA_PRIVATE
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/mount.h>
#include <sys/kcore.h>
#include <sys/boot_flag.h>
#include <sys/termios.h>
#include <sys/ksyms.h>
#include <sys/device.h>
#include <sys/cpu.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include "ksyms.h"
#if NKSYMS || defined(DDB) || defined(MODULAR)
#include <machine/db_machdep.h>
#include <ddb/db_extern.h>
#include <sys/exec_elf.h>
#endif
#include <evbmips/loongson/autoconf.h>
#include <evbmips/loongson/loongson_intr.h>
#include <evbmips/loongson/loongson_bus_defs.h>
#include <machine/cpu.h>
#include <machine/psl.h>
#include <mips/locore.h>
#include <mips/bonito/bonitoreg.h>
#include <mips/bonito/bonitovar.h>
#include <mips/pmon/pmon.h>
#include "sisfb.h"
#if NSISFB > 0
#include <dev/pci/sisfb.h>
#endif
#include "lynxfb.h"
#if NLYNXFB > 0
#include <dev/pci/lynxfbvar.h>
#endif
#include "pckbc.h"
#if NPCKBC > 0
#include <dev/isa/isareg.h>
#include <dev/ic/i8042reg.h>
#include <dev/ic/pckbcvar.h>
#endif
#include "pckbd.h"
#include "ukbd.h"
#if NUKBD > 0
#include <dev/usb/ukbdvar.h>
#endif
#if NPCKBD > 0 || NUKBD > 0
#include <dev/wscons/wskbdvar.h>
#endif
#include "com.h"
#if NCOM > 0
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
bus_space_tag_t comconsiot;
bus_addr_t comconsaddr;
int comconsrate = 0;
#endif
#ifdef LOW_DEBUG
#define DPRINTF(x) printf x
#define DPPRINTF(x) pmon_printf x
#else
#define DPRINTF(x)
#define DPPRINTF(x)
#endif
int ex_mallocsafe = 0;
struct extent *loongson_io_ex = NULL;
struct extent *loongson_mem_ex = NULL;
struct mips_bus_space bonito_iot;
struct mips_bus_space bonito_memt;
struct mips_bus_dma_tag bonito_dmat;
struct mips_pci_chipset bonito_pc;
uint loongson_ver;
const struct platform *sys_platform;
struct bonito_flavour {
const char *prefix;
const struct platform *platform;
};
extern const struct platform fuloong_platform;
extern const struct platform gdium_platform;
extern const struct platform generic2e_platform;
extern const struct platform lynloong_platform;
extern const struct platform yeeloong_platform;
const struct bonito_flavour bonito_flavours[] = {
{ "LM6002", &fuloong_platform },
{ "LM6003", &fuloong_platform },
{ "LM6004", &fuloong_platform },
{ "Gdium", &gdium_platform },
{ "LM8089", &yeeloong_platform },
{ "LM8101", &yeeloong_platform },
{ "LM9001", &lynloong_platform },
{ NULL }
};
struct vm_map *phys_map = NULL;
int netboot;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
int mem_cluster_cnt;
void mach_init(int, int32_t, int32_t, int32_t, char *);
static int pmoncngetc(dev_t);
static void pmoncnputc(dev_t, int);
struct consdev pmoncons = {
NULL,
NULL,
pmoncngetc,
pmoncnputc,
nullcnpollc,
NULL,
makedev(0, 0),
CN_DEAD
};
CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN);
void
mach_init(int32_t argc, int32_t argva, int32_t enva, int32_t callvec,
char *boot_esym)
{
void *kernend;
#ifdef NOTYET
int howto;
#endif
const char *env;
int i;
psize_t memlo, memhi;
const struct bonito_flavour *f;
char *ssym = NULL, *esym = NULL;
pcireg_t reg;
pcitag_t pcitag;
extern char edata[], end[];
memset(edata, 0, (char *)end - edata);
pmon_init(argc, argva, enva, callvec);
DPPRINTF(("pmon hello\n"));
cn_tab = &pmoncons;
DPRINTF(("hello 0x%x %d 0x%x 0x%x %p stack %p\n", pmon_callvec, argc, argva, enva, boot_esym, &i));
#if NKSYMS || defined(DDB) || defined(MODULAR)
if (end[0] == ELFMAG0 && end[1] == ELFMAG1 &&
end[2] == ELFMAG2 && end[3] == ELFMAG3) {
ssym = end;
esym = boot_esym;
kernend = (void *)mips_round_page(esym);
} else {
ssym = (char *)(vaddr_t)*(int32_t *)end;
if (((long)ssym - (long)end) >= 0 &&
((long)ssym - (long)end) <= 0x1000 &&
ssym[0] == ELFMAG0 && ssym[1] == ELFMAG1 &&
ssym[2] == ELFMAG2 && ssym[3] == ELFMAG3) {
esym = (char *)(vaddr_t)*((int32_t *)end + 1);
kernend = (void *)mips_round_page(esym);
} else {
ssym = NULL;
esym = NULL;
kernend = (void *)mips_round_page(end);
}
}
DPRINTF(("ssym %p esym %p\n", ssym, esym));
#endif
DPRINTF(("mips_vector_init "));
mips_vector_init(NULL, false);
DPRINTF(("uvm_md_init\n"));
uvm_md_init();
#if NKSYMS || defined(DDB) || defined(MODULAR)
#endif
env = pmon_getenv("systype");
if (env == NULL) {
printf("Unable to figure out system type!\n");
goto unsupported;
}
if (strcmp(env, "Bonito") != 0) {
printf("This kernel doesn't support system type \"%s\".\n",
env);
goto unsupported;
}
switch ((mips_options.mips_cpu_id >> 8) & 0xff) {
case MIPS_LOONGSON2:
switch (mips_options.mips_cpu_id & 0xff) {
case 0x00:
loongson_ver = 0x2c;
break;
case 0x02:
loongson_ver = 0x2e;
break;
case 0x03:
loongson_ver = 0x2f;
break;
case 0x05:
loongson_ver = 0x3a;
break;
}
if (loongson_ver == 0x2e || loongson_ver == 0x2f)
break;
default:
printf("This kernel doesn't support processor type 0x%x"
", version %d.%d.\n",
(mips_options.mips_cpu_id >> 8) & 0xff,
(mips_options.mips_cpu_id >> 4) & 0x0f,
mips_options.mips_cpu_id & 0x0f);
goto unsupported;
}
env = pmon_getenv("Version");
if (env == NULL) {
if (loongson_ver == 0x2e) {
sys_platform = &generic2e_platform;
} else {
printf("Unable to figure out model!\n");
goto unsupported;
}
} else {
for (f = bonito_flavours; f->prefix != NULL; f++)
if (strncmp(env, f->prefix, strlen(f->prefix)) ==
0) {
sys_platform = f->platform;
break;
}
if (sys_platform == NULL) {
if (strncmp(env, "1.2.", 4) == 0) {
printf("No model prefix in version"
" string \"%s\".\n"
"Attempting to match as Lemote Fuloong\n",
env);
sys_platform = &fuloong_platform;
}
}
if (sys_platform == NULL) {
printf("This kernel doesn't support model \"%s\"."
"\n", env);
goto unsupported;
}
}
cpu_setmodel("%s %s", sys_platform->vendor, sys_platform->product);
DPRINTF(("Found %s, setting up.\n", cpu_getmodel()));
env = pmon_getenv("memsize");
if (env == NULL) {
printf("Could not get memory information"
" from the firmware\n");
goto unsupported;
}
memlo = strtoul(env, NULL, 10);
DPRINTF(("memlo %" PRIdPSIZE, memlo));
if (memlo < 0 || memlo > 256) {
printf("Incorrect low memory size `%s'\n", env);
goto unsupported;
}
if (memlo >= 240) {
env = pmon_getenv("highmemsize");
if (env == NULL)
memhi = 0;
else
memhi = strtoul(env, NULL, 10);
if (memhi < 0 || memhi > (64 * 1024) - 256) {
printf("Incorrect high memory size `%s'\n",
env);
goto unsupported;
}
} else
memhi = 0;
DPRINTF(("memhi %" PRIdPSIZE "\n", memhi));
memlo = memlo * 1024 * 1024;
memhi = memhi * 1024 * 1024;
switch (loongson_ver) {
case 0x2e:
loongson2e_setup(memlo, memhi,
MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
break;
default:
case 0x2f:
case 0x3a:
loongson2f_setup(memlo, memhi,
MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
break;
}
DPRINTF(("bonito_pci_init "));
bonito_pci_init(&bonito_pc, sys_platform->bonito_config);
bonito_pc.pc_intr_v = __UNCONST(sys_platform->bonito_config);
bonito_pc.pc_intr_map = loongson_pci_intr_map;
bonito_pc.pc_intr_string = loongson_pci_intr_string;
bonito_pc.pc_intr_evcnt = loongson_pci_intr_evcnt;
bonito_pc.pc_intr_establish = loongson_pci_intr_establish;
bonito_pc.pc_intr_disestablish = loongson_pci_intr_disestablish;
bonito_pc.pc_conf_interrupt = loongson_pci_conf_interrupt;
bonito_pc.pc_pciide_compat_intr_establish =
loongson_pciide_compat_intr_establish;
DPRINTF(("bonito_bus_io_init "));
bonito_bus_io_init(&bonito_iot, NULL);
bonito_iot.bs_map = bonito_bus_io_legacy_map;
DPRINTF(("bonito_bus_mem_init\n"));
bonito_bus_mem_init(&bonito_memt, NULL);
bonito_dmat._cookie = __UNCONST(sys_platform);
bonito_dmat._dmamap_ops = mips_bus_dmamap_ops;
bonito_dmat._dmamem_ops = mips_bus_dmamem_ops;
bonito_dmat._dmatag_ops = mips_bus_dmatag_ops;
DPRINTF(("sys_platform->setup %p\n", sys_platform->setup));
if (sys_platform->setup != NULL)
(*(sys_platform->setup))();
#if NCOM > 0
DPRINTF(("comconsrate %d\n", comconsrate));
if (comconsrate > 0) {
if (comcnattach(comconsiot, comconsaddr, comconsrate,
COM_FREQ, COM_TYPE_NORMAL,
(TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
panic("unable to initialize serial console");
}
#endif
for (i = 0; i < 32 - 0x11; i++) {
pcitag = pci_make_tag(&bonito_pc, 0, i, 0);
reg = pci_conf_read(&bonito_pc, pcitag, PCI_CLASS_REG);
DPRINTF(("dev %d class 0x%x", i, reg));
reg = pci_conf_read(&bonito_pc, pcitag, PCI_ID_REG);
DPRINTF((" id 0x%x; ", reg));
#if NSISFB > 0
if (cn_tab == &pmoncons)
sisfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
pcitag, reg);
#endif
#if NLYNXFB > 0
if (cn_tab == &pmoncons)
lynxfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
pcitag, reg);
#endif
if (cn_tab == &pmoncons)
gdium_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
pcitag, reg);
if (cn_tab != &pmoncons)
break;
}
#if NPCKBC > 0 || NUKBD > 0
if (cn_tab != &pmoncons) {
int rc = ENXIO;
#if NPCKBC > 0
if (rc != 0)
rc = pckbc_cnattach(&bonito_iot, IO_KBD, KBCMDP, 0, 0);
#endif
#if NUKBD > 0
if (rc != 0)
rc = ukbd_cnattach();
#endif
}
#endif
DPRINTF(("\n"));
DPRINTF(("search cpuclock "));
env = pmon_getenv("cpuclock");
DPRINTF(("got %s ", env));
if (env != NULL) {
curcpu()->ci_cpu_freq =
strtoul(env, NULL, 10);
}
DPRINTF(("cpuclock %ld\n", curcpu()->ci_cpu_freq));
if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
curcpu()->ci_cpu_freq /= 2;
curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
curcpu()->ci_divisor_delay =
((curcpu()->ci_cpu_freq + 500000) / 1000000);
if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
curcpu()->ci_cpu_freq *= 2;
#ifdef DEBUG
printf("Timer calibration: %lu cycles/sec\n",
curcpu()->ci_cpu_freq);
#endif
#if NCOM > 0 && 0
delay(160000000 / comcnrate);
if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
COM_FREQ, COM_TYPE_NORMAL,
(TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
panic("malta: unable to initialize serial console");
#endif
boothowto = RB_AUTOBOOT;
#ifdef NOTYET
for (i = 1; i < argc; i++) {
for (cp = argv[i]; *cp; cp++) {
if (*cp == '-')
continue;
howto = 0;
BOOT_FLAG(*cp, howto);
if (! howto)
printf("bootflag '%c' not recognised\n", *cp);
else
boothowto |= howto;
}
}
#endif
mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
mem_clusters, mem_cluster_cnt, NULL, 0);
DPRINTF(("mips_init_msgbuf\n"));
mips_init_msgbuf();
DPRINTF(("pmap_bootstrap\n"));
pmap_bootstrap();
DPRINTF(("curlwp %p ", curlwp));
DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
mips_init_lwp0_uarea();
DPRINTF(("curlwp %p ", curlwp));
DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
#if defined(DDB)
if (boothowto & RB_KDB)
Debugger();
#endif
DPRINTF(("return\n"));
return;
unsupported:
panic("unsupported hardware\n");
}
void
consinit(void)
{
}
void
cpu_startup(void)
{
cpu_startup_common();
ex_mallocsafe = 1;
}
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_POWERDOWN) == RB_POWERDOWN) {
if (sys_platform->powerdown != NULL)
sys_platform->powerdown();
}
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("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
if (sys_platform->reset != NULL)
sys_platform->reset();
__asm__ __volatile__ (
"\t.long 0x3c02bfc0\n"
"\t.long 0x00400008\n"
::: "v0");
}
int
pmoncngetc(dev_t dev)
{
static char buf[1 + PMON_MAXLN];
static char *bufpos = buf;
int c;
if (*bufpos == '\0') {
bufpos = buf;
if (pmon_gets(buf) == NULL) {
return (int)'\n';
} else {
buf[strlen(buf)] = '\n';
}
}
c = (int)*bufpos++;
if (bufpos - buf > PMON_MAXLN) {
bufpos = buf;
*bufpos = '\0';
}
return c;
}
void
pmoncnputc(dev_t dev, int c)
{
if (c == '\n')
pmon_printf("\n");
else
pmon_printf("%c", c);
}