#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.36 2024/03/05 14:15:34 thorpej Exp $");
#include "opt_ddb.h"
#include "opt_kloader.h"
#include "opt_kloader_kernel_path.h"
#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/device.h>
#include <uvm/uvm.h>
#include <uvm/uvm_extern.h>
#ifdef DDB
#include <machine/db_machdep.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#include <sys/exec_elf.h>
#endif
#include <dev/cons.h>
#include <machine/bootinfo.h>
#include <machine/locore.h>
#include <machine/psl.h>
#include <machine/intr.h>
#include <playstation2/playstation2/sifbios.h>
#include <playstation2/playstation2/interrupt.h>
#if defined KLOADER_KERNEL_PATH && !defined KLOADER
#error "define KLOADER"
#endif
#ifdef KLOADER
#include <machine/kloader.h>
#endif
struct cpu_info cpu_info_store;
struct vm_map *phys_map;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
int mem_cluster_cnt;
#ifdef DEBUG
static void bootinfo_dump(void);
#endif
void mach_init(void);
void
mach_init(void)
{
extern char kernel_text[], edata[], end[];
char *kernend;
struct pcb *pcb0;
vaddr_t v;
paddr_t start;
size_t size;
kernend = (void *)mips_round_page(end);
memset(edata, 0, kernend - edata);
interrupt_init_bootstrap();
sifbios_init();
consinit();
printf("kernel_text=%p edata=%p end=%p\n", kernel_text, edata, end);
#ifdef DEBUG
bootinfo_dump();
#endif
uvm_md_init();
physmem = atop(PS2_MEMORY_SIZE);
mips_vector_init(NULL, false);
start = (paddr_t)round_page(MIPS_KSEG0_TO_PHYS(kernend));
size = PS2_MEMORY_SIZE - start - BOOTINFO_BLOCK_SIZE;
memset((void *)MIPS_PHYS_TO_KSEG1(start), 0, size);
mem_clusters[0].start = trunc_page(MIPS_KSEG0_TO_PHYS(kernel_text));
mem_clusters[0].size = start - mem_clusters[0].start;
mem_clusters[1].start = start;
mem_clusters[1].size = size;
printf("load memory %#x, %#lx\n", start, size);
uvm_page_physload(atop(start), atop(start + size),
atop(start), atop(start + size), VM_FREELIST_DEFAULT);
mips_init_msgbuf();
pmap_bootstrap();
v = uvm_pageboot_alloc(USPACE);
pcb0 = lwp_getpcb(&lwp0);
pcb0->pcb_context.val[_L_SR] = PSL_LOWIPL;
#ifdef IPL_ICU_MASK
pcb0->pcb_ppl = 0;
#endif
lwp0.l_md.md_utf = (struct trapframe *)(v + USPACE) - 1;
}
void
cpu_startup(void)
{
cpu_startup_common();
}
void
cpu_reboot(int howto, char *bootstr)
{
#ifdef KLOADER
struct kloader_bootinfo kbi;
#endif
static int waittime = -1;
if (curlwp)
savectx(curpcb);
if (cold) {
howto |= RB_HALT;
goto haltsys;
}
if (boothowto & RB_HALT) {
howto |= RB_HALT;
}
#ifdef KLOADER
kloader_bootinfo_set(&kbi, 0, NULL, NULL, true);
#ifndef KLOADER_KERNEL_PATH
#define KLOADER_KERNEL_PATH "/netbsd"
#endif
if ((howto & RB_HALT) == 0)
kloader_reboot_setup(KLOADER_KERNEL_PATH);
#endif
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)
sifbios_halt(0);
else if (howto & RB_HALT)
sifbios_halt(1);
else {
#ifdef KLOADER
kloader_reboot();
#endif
sifbios_halt(2);
}
while (1)
;
}
#ifdef DEBUG
void
bootinfo_dump(void)
{
printf("devconf=%#x, option=%#x, rtc=%#x, pcmcia_type=%#x,"
"sysconf=%#x\n",
BOOTINFO_REF(BOOTINFO_DEVCONF),
BOOTINFO_REF(BOOTINFO_OPTION_PTR),
BOOTINFO_REF(BOOTINFO_RTC),
BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE),
BOOTINFO_REF(BOOTINFO_SYSCONF));
}
#endif