#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.136 2025/12/21 07:00:26 skrll Exp $");
#include "opt_ddb.h"
#include "opt_ddbparam.h"
#include "opt_md.h"
#include "opt_modular.h"
#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/time.h>
#include <sys/tty.h>
#include <sys/exec.h>
#include <uvm/uvm_extern.h>
#include <sys/mount.h>
#include <sys/device.h>
#include <sys/syscallargs.h>
#include <sys/kcore.h>
#include <sys/ksyms.h>
#include <sys/cpu.h>
#include <ufs/mfs/mfs_extern.h>
#include <machine/bootinfo.h>
#include <machine/cpu.h>
#include <machine/reg.h>
#include <machine/pio.h>
#include <sys/bus.h>
#include <machine/trap.h>
#include <machine/autoconf.h>
#include <machine/platform.h>
#include <machine/wired_map.h>
#include <mips/pte.h>
#include <mips/locore.h>
#include <mips/cpuregs.h>
#include <mips/psl.h>
#include <mips/cache.h>
#ifdef DDB
#include <mips/db_machdep.h>
#include <ddb/db_extern.h>
#endif
#include <dev/cons.h>
#include <dev/ic/i8042reg.h>
#include <dev/isa/isareg.h>
#include <arc/arc/arcbios.h>
#include <arc/arc/timervar.h>
#include "ksyms.h"
#include "com.h"
#if NCOM > 0
#include <sys/termios.h>
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
#ifndef COMCONSOLE
#define COMCONSOLE 0
#endif
#ifndef CONADDR
#define CONADDR 0
#endif
#ifndef CONSPEED
#define CONSPEED TTYDEF_SPEED
#endif
#ifndef CONMODE
#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
#endif
#endif
struct vm_map *phys_map = NULL;
int maxmem;
int cpuspeed = 150;
vsize_t kseg2iobufsize = 0;
struct arc_bus_space arc_bus_io;
struct arc_bus_space arc_bus_mem;
char *bootinfo;
static char bi_buf[BOOTINFO_SIZE];
static const char *bootinfo_msg = NULL;
#if NCOM > 0
int com_freq = COM_FREQ;
int com_console = COMCONSOLE;
int com_console_address = CONADDR;
int com_console_speed = CONSPEED;
int com_console_mode = CONMODE;
#else
#ifndef COMCONSOLE
#error COMCONSOLE is defined without com driver configured.
#endif
int com_console = 0;
#endif
char **environment;
int mem_reserved[VM_PHYSSEG_MAX];
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
int mem_cluster_cnt;
void mach_init(int, char *[], u_int, void *);
const char *firmware_getenv(const char *env);
void arc_sysreset(bus_addr_t, bus_size_t);
extern char kernel_text[], edata[], end[];
void
mach_init(int argc, char *argv[], u_int bim, void *bip)
{
const char *cp;
int i;
#if 0
paddr_t kernstartpfn;
#endif
paddr_t first, last, kernendpfn;
char *kernend;
#if NKSYMS > 0 || defined(DDB) || defined(MODULAR)
char *ssym = NULL;
char *esym = NULL;
struct btinfo_symtab *bi_syms;
#endif
if (bim == BOOTINFO_MAGIC && bip != NULL) {
struct btinfo_magic *bi_magic;
memcpy(bi_buf, bip, BOOTINFO_SIZE);
bootinfo = bi_buf;
bi_magic = lookup_bootinfo(BTINFO_MAGIC);
if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC)
bootinfo_msg =
"invalid magic number in bootinfo structure.\n";
else
bootinfo_msg = "bootinfo found.\n";
} else
bootinfo_msg = "no bootinfo found. (old bootblocks?)\n";
#if NKSYMS > 0 || defined(DDB) || defined(MODULAR)
bi_syms = lookup_bootinfo(BTINFO_SYMTAB);
if (bi_syms != NULL) {
ssym = (char *)bi_syms->ssym;
esym = (char *)bi_syms->esym;
kernend = (void *)mips_round_page(esym);
#if 0
memset(edata, 0, end - edata);
#endif
} else
#endif
{
kernend = (void *)mips_round_page(end);
memset(edata, 0, kernend - edata);
}
environment = &argv[1];
if (bios_ident()) {
bios_init_console();
bios_save_info();
physmem = bios_configure_memory(mem_reserved, mem_clusters,
&mem_cluster_cnt);
}
ident_platform();
if (platform == NULL) {
printf("kernel not configured for this system:\n");
printf("ID [%s] Vendor [%8.8s] Product [%02x",
arc_id, arc_vendor_id, arc_product_id[0]);
for (i = 1; i < sizeof(arc_product_id); i++)
printf(":%02x", arc_product_id[i]);
printf("]\n");
printf("DisplayController [%s]\n", arc_displayc_id);
#if 1
for (;;)
;
#else
cpu_reboot(RB_HALT | RB_NOSYNC, NULL);
#endif
}
physmem = btoc(physmem);
cp = firmware_getenv("OSLOADPARTITION");
makebootdev(cp != NULL ? cp : argv[0]);
#ifdef MEMORY_DISK_IS_ROOT
boothowto = RB_SINGLE;
#else
boothowto = RB_SINGLE | RB_ASKNAME;
#endif
#ifdef KADB
boothowto |= RB_KDB;
#endif
cp = firmware_getenv("OSLOADOPTIONS");
if (cp) {
while (*cp) {
switch (*cp++) {
case 'a':
boothowto &= ~RB_SINGLE;
break;
case 'm':
boothowto |= RB_MINIROOT;
break;
case 'n':
boothowto |= RB_ASKNAME;
break;
case 'N':
boothowto &= ~RB_ASKNAME;
break;
case 's':
com_console = 1;
break;
case 'q':
boothowto |= AB_QUIET;
break;
case 'v':
boothowto |= AB_VERBOSE;
break;
}
}
}
uvm_md_init();
cn_tab = NULL;
mips_vector_init(NULL, false);
(*platform->init)();
cpuspeed = platform->clock;
curcpu()->ci_cpu_freq = platform->clock * 1000000;
curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
curcpu()->ci_divisor_delay =
((curcpu()->ci_cpu_freq + 500000) / 1000000);
curcpu()->ci_cctr_freq = curcpu()->ci_cpu_freq;
if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT) {
curcpu()->ci_cycles_per_hz /= 2;
curcpu()->ci_divisor_delay /= 2;
curcpu()->ci_cctr_freq /= 2;
}
cpu_setmodel("%s %s%s",
platform->vendor, platform->model, platform->variant);
if (boothowto & RB_MINIROOT)
kernend += round_page(mfs_initminiroot(kernend));
#if NKSYMS || defined(DDB) || defined(MODULAR)
if (esym)
ksyms_addsyms_elf(esym - ssym, ssym, esym);
#endif
maxmem = physmem;
kernendpfn = atop(round_page(MIPS_KSEG0_TO_PHYS(kernend)));
#if 0
kernstartpfn = atop(trunc_page(
MIPS_KSEG0_TO_PHYS((kernel_text) - UPAGES * PAGE_SIZE)));
for (i = 0; i < mem_cluster_cnt; i++) {
if (mem_reserved[i])
continue;
first = atop(round_page(mem_clusters[i].start));
last = atop(trunc_page(mem_clusters[i].start +
mem_clusters[i].size));
if (last <= kernstartpfn || kernendpfn <= first) {
uvm_page_physload(first, last, first, last,
VM_FREELIST_DEFAULT);
} else {
if (first < kernstartpfn)
uvm_page_physload(first, kernstartpfn,
first, kernstartpfn, VM_FREELIST_DEFAULT);
if (kernendpfn < last)
uvm_page_physload(kernendpfn, last,
kernendpfn, last, , VM_FREELIST_DEFAULT);
}
}
#elif 0
for (i = 0; i < mem_cluster_cnt; i++) {
if (mem_reserved[i])
continue;
first = atop(round_page(mem_clusters[i].start));
last = atop(trunc_page(mem_clusters[i].start +
mem_clusters[i].size));
if (kernendpfn < last) {
if (first < kernendpfn)
first = kernendpfn;
uvm_page_physload(first, last, first, last,
VM_FREELIST_DEFAULT);
}
}
#else
#if 1
mem_clusters[0].start = 0;
mem_clusters[0].size = ctob(physmem);
mem_cluster_cnt = 1;
#endif
first = kernendpfn;
last = physmem;
uvm_page_physload(first, last, first, last, VM_FREELIST_DEFAULT);
#endif
mips_init_msgbuf();
pmap_bootstrap();
mips_init_lwp0_uarea();
}
void
mips_machdep_cache_config(void)
{
mips_cache_info.mci_sdcache_size = arc_cpu_l2cache_size;
}
const char *
firmware_getenv(const char *envname)
{
char **env;
int l;
l = strlen(envname);
for (env = environment; env[0]; env++) {
if (strncasecmp(envname, env[0], l) == 0 && env[0][l] == '=') {
return &env[0][l + 1];
}
}
return NULL;
}
void
consinit(void)
{
static int initted;
if (initted)
return;
initted = 1;
(*platform->cons_init)();
}
void
cpu_startup(void)
{
#ifdef BOOTINFO_DEBUG
if (bootinfo_msg)
printf(bootinfo_msg);
#endif
cpu_startup_common();
}
void *
lookup_bootinfo(int type)
{
struct btinfo_common *bt;
char *bip;
if (bootinfo == NULL)
return NULL;
bip = bootinfo;
do {
bt = (struct btinfo_common *)bip;
if (bt->type == type)
return (void *)bt;
bip += bt->next;
} while (bt->next != 0 &&
bt->next < BOOTINFO_SIZE &&
(size_t)bip < (size_t)bootinfo + BOOTINFO_SIZE);
return NULL;
}
static int waittime = -1;
void
cpu_reboot(int howto, char *bootstr)
{
savectx(curpcb);
#ifdef DEBUG
if (panicstr)
stacktrace();
#endif
boothowto = howto;
if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
if (curlwp == NULL)
curlwp = &lwp0;
waittime = 0;
vfs_shutdown();
}
(void)splhigh();
if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
dumpsys();
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");
delay(1000000);
(*platform->reset)();
__asm(" li $2, 0xbfc00000; jr $2; nop\n");
for (;;)
;
}
void
arc_sysreset(bus_addr_t addr, bus_size_t cmd_offset)
{
volatile uint8_t *kbdata = (uint8_t *)addr + KBDATAP;
volatile uint8_t *kbcmd = (uint8_t *)addr + cmd_offset;
#define KBC_ARC_SYSRESET 0xd1
delay(1000);
*kbcmd = KBC_ARC_SYSRESET;
delay(1000);
*kbdata = 0;
}