#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.33 2024/03/05 14:15:31 thorpej Exp $");
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.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 <sys/cpu.h>
#include <uvm/uvm_extern.h>
#include <machine/bootinfo.h>
#include <machine/locore.h>
#include <machine/sbdvar.h>
#include <machine/wired_map.h>
#include <mips/cache.h>
#ifdef DDB
#include <machine/db_machdep.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#include <ddb/db_output.h>
#include <sys/exec_elf.h>
#endif
#include <dev/cons.h>
#include <ews4800mips/ews4800mips/cons_machdep.h>
vsize_t kseg2iobufsize;
struct vm_map *phys_map;
int mem_cluster_cnt;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
void mach_init(int, char *[], struct bootinfo *);
void option(int, char *[], struct bootinfo *);
void nmi_exception(void);
void
mach_init(int argc, char *argv[], struct bootinfo *bi)
{
extern char kernel_text[], edata[], end[];
void *v;
int i;
if (bi == NULL || bi->bi_size != sizeof(struct bootinfo)) {
memset(edata, 0, end - edata);
}
rom_cons_init();
sbd_init();
__asm volatile("move %0, $29" : "=r"(v));
printf("kernel_text=%p edata=%p end=%p sp=%p\n",
kernel_text, edata, end, v);
option(argc, argv, bi);
uvm_md_init();
(*platform.mem_init)(kernel_text,
(bi && bi->bi_nsym) ? (void *)bi->bi_esym : end);
cn_tab = NULL;
mips_vector_init(NULL, false);
memcpy((void *)0x80000200, ews4800mips_nmi_vec, 32);
mips_dcache_wbinv_all();
mips_icache_sync_all();
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_cycles_per_hz /= 2;
curcpu()->ci_divisor_delay /= 2;
}
for (i = 1; i < mem_cluster_cnt; i++) {
phys_ram_seg_t *p;
paddr_t start;
size_t size;
p = &mem_clusters[i];
start = p->start;
size = p->size;
uvm_page_physload(atop(start), atop(start + size),
atop(start), atop(start + size), VM_FREELIST_DEFAULT);
}
cpu_setmodel("NEC %s", platform.name);
mips_init_msgbuf();
pmap_bootstrap();
mips_init_lwp0_uarea();
}
void
option(int argc, char *argv[], struct bootinfo *bi)
{
extern char __boot_kernel_name[];
bool boot_device_set;
char *p;
int i;
printf("argc=%d argv=%p syminfo=%p\n", argc, argv, bi);
printf("version=%d size=%d nsym=%d ssym=%p esym=%p\n",
bi->bi_version, bi->bi_size, bi->bi_nsym, bi->bi_ssym, bi->bi_esym);
for (i = 0; i < argc; i++)
printf("[%d] %s\n", i, argv[i]);
#ifdef DDB
if (bi->bi_nsym)
ksyms_addsyms_elf(bi->bi_esym - bi->bi_ssym,
(void *)bi->bi_ssym, (void *)bi->bi_esym);
#endif
boot_device_set = false;
for (i = 2; i < argc; i++) {
p = argv[i];
if (p[0] == '-' && p[1] == 'a')
boot_device_set = true;
if (p[0] == '-' && p[1] == 'b') {
boot_device_set = true;
strcpy(__boot_kernel_name, p + 3);
}
}
if (!boot_device_set)
strcpy(__boot_kernel_name, argv[1]);
sbd_memcluster_init(bi->bi_mainfo);
}
void
mips_machdep_cache_config(void)
{
if (platform.cache_config)
(*platform.cache_config)();
}
void
cpu_startup(void)
{
vaddr_t minaddr, maxaddr;
char pbuf[9];
printf("%s%s", copyright, version);
printf("%s %dMHz\n", cpu_getmodel(), platform.cpu_clock / 1000000);
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);
format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
printf("avail memory = %s\n", pbuf);
}
void
cpu_reboot(int howto, char *bootstr)
{
static int waittime = -1;
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 (platform.poweroff) {
DELAY(1000000);
(*platform.poweroff)();
}
}
if (howto & RB_HALT) {
printf("System halted. Hit any key to reboot.\n\n");
(void)cngetc();
}
printf("rebooting...\n");
DELAY(1000000);
if (platform.reboot)
(*platform.reboot)();
printf("reboot failed.\n");
for (;;)
;
}
#ifdef DDB
void
__db_print_symbol(db_expr_t value)
{
const char *name;
db_expr_t offset;
db_find_xtrn_sym_and_offset((db_addr_t)value, &name, &offset);
if (name != NULL && offset <= db_maxoff && offset != value)
db_print_loc_and_inst(value);
else
db_printf("\n");
}
#endif