#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.21 2025/11/30 01:46:00 thorpej Exp $");
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/mbuf.h>
#include <sys/reboot.h>
#include <sys/mount.h>
#include <sys/kcore.h>
#include <sys/boot_flag.h>
#include <sys/ksyms.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/cpu.h>
#include <sys/vmem.h>
#include <sys/vmem_impl.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include <ufs/mfs/mfs_extern.h>
#include <mips/cache.h>
#include <machine/psl.h>
#include <machine/autoconf.h>
#include <emips/stand/common/prom_iface.h>
#include <machine/sysconf.h>
#include <machine/bootinfo.h>
#include <machine/locore.h>
#include <emips/emips/machdep.h>
#include <machine/emipsreg.h>
#define _EMIPS_BUS_DMA_PRIVATE
#include <machine/bus.h>
#if NKSYMS || defined(DDB) || defined(MODULAR)
#include <machine/db_machdep.h>
#include <ddb/db_extern.h>
#endif
vaddr_t iospace;
vsize_t iospace_size = 64 * 1024;
#include "ksyms.h"
#define IOMAP_BTAG_COUNT VMEM_EST_BTCOUNT(1, 32)
static struct vmem iomap_arena_store;
static struct vmem_btag iomap_btag_store[IOMAP_BTAG_COUNT];
static vmem_t *iomap_arena;
struct vm_map *phys_map = NULL;
int systype;
char *bootinfo = NULL;
int cpuspeed = 30;
intptr_t physmem_boardmax;
int mem_cluster_cnt;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
void mach_init (int, char *[], int, intptr_t, u_int, char *);
static void unimpl_bus_reset(void);
static void unimpl_cons_init(void);
static void unimpl_iointr(uint32_t, vaddr_t, uint32_t);
static void unimpl_intr_establish(device_t, void *, int,
int (*)(void *, void *), void *);
static int unimpl_memsize(void *);
struct platform platform = {
"iobus not set",
unimpl_bus_reset,
unimpl_cons_init,
unimpl_iointr,
unimpl_intr_establish,
unimpl_memsize
};
extern char *esym;
extern struct consdev promcd;
extern const struct callback *callv;
extern const struct callback callvec;
void
mach_init(int argc, char *argv[], int code, intptr_t cv, u_int bim, char *bip)
{
char *cp;
const char *bootinfo_msg;
u_long first, last;
int i, howtoboot;
#if NKSYMS || defined(DDB) || defined(MODULAR)
void *ssym = 0;
struct btinfo_symtab *bi_syms;
#endif
void *kernend;
extern char edata[], end[];
if (bim == BOOTINFO_MAGIC) {
struct btinfo_magic *bi_magic;
bootinfo = bip;
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 = NULL;
} else
bootinfo_msg = "invalid bootinfo pointer (old bootblocks?)\n";
howtoboot = 0;
#ifdef KADB
howtoboot |= RB_KDB;
#endif
for (i = 1; i < argc; i++) {
for (cp = argv[i]; *cp; cp++) {
switch (*cp) {
#define RB_NOSYMBOLS 0x10000000
case 'e':
howtoboot |= RB_NOSYMBOLS;
break;
case 'n':
howtoboot |= RB_ASKNAME;
break;
case 'N':
howtoboot &= ~RB_ASKNAME;
break;
default:
BOOT_FLAG(*cp, howtoboot);
break;
}
}
}
#if NKSYMS || defined(DDB) || defined(MODULAR)
bi_syms = lookup_bootinfo(BTINFO_SYMTAB);
if ((bi_syms != NULL) && (!(howtoboot & RB_NOSYMBOLS))) {
ssym = (void *)(intptr_t)bi_syms->ssym;
esym = (void *)(intptr_t)bi_syms->esym;
kernend = (void *)mips_round_page(esym);
memset(edata, 0, end - edata);
} else
#endif
{
kernend = (char *)mips_round_page(end);
memset(edata, 0, (char *)kernend - (char *)edata);
}
callv = (code == PROM_MAGIC) ? (void *)cv : &callvec;
cn_tab = &promcd;
#if 1
if (bootinfo_msg != NULL)
printf(bootinfo_msg);
#endif
uvm_md_init();
mips_vector_init(NULL, false);
emips_bus_dma_init();
makebootdev(argv[0]);
boothowto = howtoboot & ~RB_NOSYMBOLS;
if (boothowto & RB_MINIROOT)
kernend = (char *)kernend
+ round_page(mfs_initminiroot(kernend));
#if NKSYMS || defined(DDB) || defined(MODULAR)
if (esym) {
ksyms_addsyms_elf((char *)esym - (char *)ssym, ssym, esym);
}
#endif
#ifdef DDB
if (boothowto & RB_KDB)
Debugger();
#endif
physmem_boardmax = MIPS_MAX_MEM_ADDR;
systype = ((prom_systype() >> 16) & 0xff);
if (systype >= nsysinit) {
platform_not_supported();
}
(*sysinit[systype].init)();
physmem = (*platform.memsize)(kernend);
for (i = 0, physmem = 0; i < mem_cluster_cnt; ++i) {
first = mem_clusters[i].start;
if (first < round_page(MIPS_KSEG0_TO_PHYS(kernend)))
first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
last = mem_clusters[i].start + mem_clusters[i].size;
physmem += atop(mem_clusters[i].size);
if (last <= first)
continue;
uvm_page_physload(atop(first), atop(last), atop(first),
atop(last), VM_FREELIST_DEFAULT);
}
mips_init_msgbuf();
iospace = pmap_limits.virtual_start;
pmap_limits.virtual_start += iospace_size;
pmap_bootstrap();
mips_init_lwp0_uarea();
}
void
mips_machdep_cache_config(void)
{
}
void
consinit(void)
{
KASSERT(iospace != 0);
iomap_arena = vmem_init(&iomap_arena_store,
"iomap",
0,
0,
PAGE_SIZE,
NULL,
NULL,
NULL,
0,
VM_NOSLEEP | VM_PRIVTAGS,
IPL_NONE);
KASSERT(iomap_arena != NULL);
vmem_add_bts(iomap_arena, iomap_btag_store, IOMAP_BTAG_COUNT);
int error = vmem_add(iomap_arena, iospace, iospace_size,
VM_NOSLEEP);
KASSERT(error == 0);
register_t s = mips_cp0_status_read();
s &= ~MIPS_SR_TS;
mips_cp0_status_write(s);
(*platform.cons_init)();
}
vaddr_t
mips_map_physmem(paddr_t phys, vsize_t size)
{
vmem_addr_t addr;
int error;
static int warned = 0;
size += phys & PAGE_MASK;
if (size > PAGE_SIZE) {
addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
if (addr == 0)
panic("mips_map_physmem: kernel map full");
} else {
error = vmem_alloc(iomap_arena, mips_round_page(size),
VM_BESTFIT | VM_NOSLEEP, &addr);
if (error) {
if (warned++ == 0)
printf("mips_map_physmem: iomap too small");
return 0;
}
}
ioaccess(addr, phys, size);
#ifdef PHYSMEMDEBUG
printf("mips_map_physmem: alloc'ed %x bytes for paddr %x, at %x\n",
size, phys, addr);
#endif
return addr | (phys & PAGE_MASK);
}
void
mips_unmap_physmem(vaddr_t addr, vsize_t size)
{
#ifdef PHYSMEMDEBUG
printf("mips_unmap_physmem: unmapping %x bytes at addr %x\n",
size, addr);
#endif
size += addr & PAGE_MASK;
addr &= ~PAGE_MASK;
iounaccess(addr, size);
if (size > PAGE_SIZE)
uvm_km_free(kernel_map, addr, size, UVM_KMF_VAONLY);
else
vmem_free(iomap_arena, addr, mips_round_page(size));
}
void
cpu_startup(void)
{
vaddr_t minaddr, maxaddr;
char pbuf[9];
#ifdef DEBUG
extern int pmapdebug;
int opmapdebug = pmapdebug;
pmapdebug = 0;
#endif
printf("%s%s", copyright, version);
printf("%s\n", cpu_getmodel());
format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
printf("total memory = %s\n", pbuf);
minaddr = 0;
phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
VM_PHYS_SIZE, 0, false, NULL);
#ifdef DEBUG
pmapdebug = opmapdebug;
#endif
format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
printf("avail memory = %s\n", pbuf);
}
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
cpu_reboot(volatile int howto,
char *bootstr)
{
if (curlwp)
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) {
vfs_shutdown();
}
splhigh();
if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
dumpsys();
haltsys:
doshutdownhooks();
pmf_system_shutdown(boothowto);
printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
prom_halt(howto);
for (;;) ;
}
#if defined(MIPS_4GB_PHYSICAL_MEMORY)
#define trim_memory(n) n
#else
#if 0
#define TOO_MUCH (MIPS_PHYS_MASK+1)
#else
#define TOO_MUCH (2*64*1024*1024)
#endif
u_long trim_memory(uint32_t nbytes);
u_long trim_memory(uint32_t nbytes)
{
int i;
u_long first, last;
nbytes *= 4096;
if (nbytes <= TOO_MUCH)
return nbytes;
mem_clusters[mem_cluster_cnt].start = 0;
mem_clusters[mem_cluster_cnt].size = 0;
for (i = 0; i < mem_cluster_cnt;) {
first = mem_clusters[i].start;
last = mem_clusters[i].start + mem_clusters[i].size;
if (first > TOO_MUCH) {
printf("Too much memory, ignoring memory "
"range %08lx..%08lx\n", first, last);
memcpy(mem_clusters+i,mem_clusters+i+1,
(sizeof(mem_clusters[0])*(mem_cluster_cnt-i)));
mem_cluster_cnt--;
continue;
}
if (last > TOO_MUCH) {
last = TOO_MUCH;
printf("Too much memory in cluster %d, trimming "
"memory to range %08lx..%08lx\n",
i, first, last);
mem_clusters[i].size = last - mem_clusters[i].start;
}
i++;
}
return TOO_MUCH;
}
#endif
int
memsize_scan(void *first)
{
int i, mem;
char *cp;
mem = btoc((paddr_t)first - MIPS_KSEG0_START);
cp = (char *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT);
while (cp < (char *)physmem_boardmax) {
int j;
if (badaddr(cp, 4))
break;
i = *(int *)cp;
j = ((int *)cp)[4];
*(int *)cp = 0xa5a5a5a5;
((int *)cp)[4] = 0x5a5a5a5a;
wbflush();
if (*(int *)cp != 0xa5a5a5a5)
break;
*(int *)cp = i;
((int *)cp)[4] = j;
cp += PAGE_SIZE;
mem++;
}
mem_clusters[0].start = 0;
mem_clusters[0].size = ctob(mem);
mem_cluster_cnt = 1;
(*platform.bus_reset)();
return (mem);
}
int
memsize_pmt(void * first)
{
int i, mem;
struct _Pmt *Pmt = ThePmt;
struct _Sram *ram;
uint32_t addr, len;
mem = 0;
for (i = 0; i < VM_PHYSSEG_MAX; Pmt--) {
uint16_t tag = Pmt->Tag;
if (tag == PMTTAG_END_OF_TABLE)
break;
if ((tag != PMTTAG_SRAM) && (tag != PMTTAG_DDRAM))
continue;
ram = (struct _Sram *)(Pmt->TopOfPhysicalAddress << 16);
for (;(ram->BaseAddressAndTag & SRAMBT_TAG) == tag;) {
addr = ram->BaseAddressAndTag & SRAMBT_BASE;
len = ram->Control & SRAMST_SIZE;
mem_clusters[i].start = addr;
mem_clusters[i].size = len;
printf("memory segment %2d start %08lx size %08lx\n", i,
(long)mem_clusters[i].start,
(long)mem_clusters[i].size);
i++;
mem += len;
ram = (tag == PMTTAG_SRAM) ? ram+1 : ram+2;
}
}
mem_cluster_cnt = i;
return trim_memory(btoc(mem));
}
static void
unimpl_bus_reset(void)
{
panic("sysconf.init didn't set bus_reset");
}
static void
unimpl_cons_init(void)
{
panic("sysconf.init didn't set cons_init");
}
static void
unimpl_iointr(uint32_t status, vaddr_t pc, uint32_t ipending)
{
panic("sysconf.init didn't set intr");
}
static void
unimpl_intr_establish(device_t dev, void *cookie, int level,
int (*handler) (void *,void *), void *arg)
{
panic("sysconf.init didn't set intr_establish");
}
static int
unimpl_memsize(void * first)
{
panic("sysconf.init didn't set memsize");
}
void
delay(int n)
{
DELAY(n);
}