#include "opt_ddb.h"
#include "opt_modular.h"
#include "vidcvideo.h"
#include "pckbc.h"
#include <sys/param.h>
__KERNEL_RCSID(0, "$NetBSD: eb7500atx_machdep.c,v 1.38 2025/12/22 07:45:46 skrll Exp $");
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/reboot.h>
#include <sys/proc.h>
#include <sys/msgbuf.h>
#include <sys/exec.h>
#include <sys/exec_aout.h>
#include <sys/ksyms.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/intr.h>
#include <sys/device.h>
#include <dev/cons.h>
#include <dev/ic/pckbcvar.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/pcf8583var.h>
#include <machine/db_machdep.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#include <uvm/uvm.h>
#include <arm/locore.h>
#include <arm/undefined.h>
#include <machine/signal.h>
#include <machine/bootconfig.h>
#include <machine/io.h>
#include <arm/arm32/machdep.h>
#include <machine/rtc.h>
#include <arm/iomd/vidc.h>
#include <arm/iomd/iomdreg.h>
#include <arm/iomd/iomdvar.h>
#include <arm/iomd/vidcvideo.h>
#include <arm/iomd/iomdiicvar.h>
#include "ksyms.h"
#define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00000000)
#define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
#define KERNEL_VM_SIZE 0x05000000
#define VERBOSE_INIT_ARM
struct bootconfig bootconfig;
videomemory_t videomemory;
char *boot_args = NULL;
extern char *booted_kernel;
extern int *vidc_base;
extern uint32_t iomd_base;
extern struct bus_space iomd_bs_tag;
paddr_t physical_start;
paddr_t physical_freestart;
paddr_t physical_freeend;
paddr_t physical_end;
paddr_t dma_range_begin;
paddr_t dma_range_end;
u_int free_pages;
paddr_t memoryblock_end;
#ifndef PMAP_STATIC_L1S
int max_processes = 64;
#endif
u_int videodram_size = 0;
paddr_t msgbufphys;
#define KERNEL_PT_VMEM 0
#define KERNEL_PT_SYS 1
#define KERNEL_PT_KERNEL 2
#define KERNEL_PT_VMDATA 3
#define KERNEL_PT_VMDATA_NUM 4
#define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
#ifdef CPU_SA110
#define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
static vaddr_t sa110_cc_base;
#endif
void physcon_display_base(u_int);
extern void consinit(void);
void data_abort_handler(trapframe_t *);
void prefetch_abort_handler(trapframe_t *);
void undefinedinstruction_bounce(trapframe_t *frame);
static void canonicalise_bootconfig(struct bootconfig *, struct bootconfig *);
static void process_kernel_args(void);
extern void dump_spl_masks(void);
void rpc_sa110_cc_setup(void);
void parse_rpc_bootargs(char *args);
extern void dumpsys(void);
# define console_flush()
#define panic2(a) do { \
memset((void *) (videomemory.vidm_vbase), 0x55, 50*1024); \
consinit(); \
panic a; \
} while ( 0)
extern u_int current_mask;
void
cpu_reboot(int howto, char *bootstr)
{
#ifdef DIAGNOSTIC
printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
irqmasks[IPL_VM]);
printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
#endif
if (cold) {
doshutdownhooks();
pmf_system_shutdown(boothowto);
printf("Halted while still in the ICE age.\n");
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cngetc();
printf("rebooting...\n");
cpu_reset();
}
cnpollc(true);
if (!(howto & RB_NOSYNC))
bootsync();
splhigh();
if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
dumpsys();
doshutdownhooks();
pmf_system_shutdown(boothowto);
IRQdisable;
if (howto & RB_HALT) {
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cngetc();
}
printf("rebooting...\n");
cpu_reset();
}
#define ONE_MB 0x100000
struct l1_sec_map {
vaddr_t va;
paddr_t pa;
vsize_t size;
vm_prot_t prot;
int cache;
} l1_sec_table[] = {
{ VIDC_BASE, VIDC_HW_BASE,
ONE_MB, VM_PROT_READ|VM_PROT_WRITE,
PTE_NOCACHE },
{ IOMD_BASE, IOMD_HW_BASE,
ONE_MB, VM_PROT_READ|VM_PROT_WRITE,
PTE_NOCACHE },
{ IO_BASE, IO_HW_BASE,
ONE_MB, VM_PROT_READ|VM_PROT_WRITE,
PTE_NOCACHE },
{ 0, 0, 0, 0, 0 }
};
static void
canonicalise_bootconfig(struct bootconfig *bootconf, struct bootconfig *raw_bootconf)
{
if (raw_bootconf->magic == BOOTCONFIG_MAGIC) {
*bootconf = *raw_bootconf;
return;
} else {
panic2(("Internal error: no valid bootconfig block found"));
}
}
vaddr_t
initarm(void *cookie)
{
struct bootconfig *raw_bootconf = cookie;
int loop;
int loop1;
u_int logical;
u_int kerneldatasize;
u_int l1pagetable;
struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
set_cpufuncs();
canonicalise_bootconfig(&bootconfig, raw_bootconf);
booted_kernel = bootconfig.kernelname;
#if NVIDCVIDEO>0
# undef VERBOSE_INIT_ARM
#endif
videomemory.vidm_vbase = bootconfig.display_start;
videomemory.vidm_pbase = bootconfig.display_phys;
videomemory.vidm_size = bootconfig.display_size;
if (bootconfig.vram[0].pages)
videomemory.vidm_type = VIDEOMEM_TYPE_VRAM;
else
videomemory.vidm_type = VIDEOMEM_TYPE_DRAM;
vidc_base = (int *) VIDC_HW_BASE;
iomd_base = IOMD_HW_BASE;
#if NVIDCVIDEO == 0
consinit();
#endif
if (PAGE_SIZE != bootconfig.pagesize)
panic2(("Page size is %d bytes instead of %d !! (huh?)\n",
bootconfig.pagesize, PAGE_SIZE));
process_kernel_args();
#ifdef VERBOSE_INIT_ARM
printf("Allocating page tables\n");
#endif
physical_start = 0xffffffff;
physical_end = 0;
for (loop = 0, physmem = 0; loop < bootconfig.dramblocks; ++loop) {
if (bootconfig.dram[loop].address < physical_start)
physical_start = bootconfig.dram[loop].address;
memoryblock_end = bootconfig.dram[loop].address +
bootconfig.dram[loop].pages * PAGE_SIZE;
if (memoryblock_end > physical_end)
physical_end = memoryblock_end;
physmem += bootconfig.dram[loop].pages;
};
dma_range_begin = (paddr_t) physical_start;
dma_range_end = (paddr_t) MIN(physical_end, 512*1024*1024);
if (physical_start != bootconfig.dram[0].address) {
int oldblocks = 0;
for (loop = 0; loop < bootconfig.dramblocks; ++loop) {
if (bootconfig.dram[loop].address <
bootconfig.dram[0].address) {
bootconfig.dram[loop].address = 0;
physmem -= bootconfig.dram[loop].pages;
bootconfig.drampages -=
bootconfig.dram[loop].pages;
bootconfig.dram[loop].pages = 0;
oldblocks++;
}
}
physical_start = bootconfig.dram[0].address;
bootconfig.dramblocks -= oldblocks;
}
physical_freestart = physical_start;
free_pages = bootconfig.drampages;
physical_freeend = physical_end;
kerneldatasize = bootconfig.kernsize + bootconfig.MDFsize;
physical_freestart +=
bootconfig.kernsize + bootconfig.MDFsize + bootconfig.scratchsize;
free_pages -= (physical_freestart - physical_start) / PAGE_SIZE;
#define valloc_pages(var, np) \
alloc_pages((var).pv_pa, (np)); \
(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
#define alloc_pages(var, np) \
(var) = physical_freestart; \
physical_freestart += ((np) * PAGE_SIZE); \
free_pages -= (np); \
memset((char *)(var), 0, ((np) * PAGE_SIZE));
loop1 = 0;
kernel_l1pt.pv_pa = 0;
for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
&& kernel_l1pt.pv_pa == 0) {
valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
} else {
valloc_pages(kernel_pt_table[loop1],
L2_TABLE_SIZE / PAGE_SIZE);
++loop1;
}
}
#ifdef DIAGNOSTIC
if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
panic2(("initarm: Failed to align the kernel page "
"directory\n"));
#endif
alloc_pages(systempage.pv_pa, 1);
valloc_pages(irqstack, IRQ_STACK_SIZE);
valloc_pages(abtstack, ABT_STACK_SIZE);
valloc_pages(undstack, UND_STACK_SIZE);
valloc_pages(kernelstack, UPAGES);
#ifdef VERBOSE_INIT_ARM
printf("Setting up stacks :\n");
printf("IRQ stack: p0x%08lx v0x%08lx\n",
irqstack.pv_pa, irqstack.pv_va);
printf("ABT stack: p0x%08lx v0x%08lx\n",
abtstack.pv_pa, abtstack.pv_va);
printf("UND stack: p0x%08lx v0x%08lx\n",
undstack.pv_pa, undstack.pv_va);
printf("SVC stack: p0x%08lx v0x%08lx\n",
kernelstack.pv_pa, kernelstack.pv_va);
printf("\n");
#endif
alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
#ifdef CPU_SA110
sa110_cc_base = (KERNEL_BASE + (physical_freestart - physical_start)
+ (CPU_SA110_CACHE_CLEAN_SIZE - 1))
& ~(CPU_SA110_CACHE_CLEAN_SIZE - 1);
#endif
#ifdef VERBOSE_INIT_ARM
printf("Creating L1 page table\n");
#endif
l1pagetable = kernel_l1pt.pv_pa;
pmap_link_l2pt(l1pagetable, 0x00000000,
&kernel_pt_table[KERNEL_PT_SYS]);
pmap_link_l2pt(l1pagetable, KERNEL_BASE,
&kernel_pt_table[KERNEL_PT_KERNEL]);
for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
&kernel_pt_table[KERNEL_PT_VMDATA + loop]);
pmap_link_l2pt(l1pagetable, VMEM_VBASE,
&kernel_pt_table[KERNEL_PT_VMEM]);
pmap_curmaxkvaddr =
KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
#ifdef VERBOSE_INIT_ARM
printf("Mapping kernel\n");
#endif
if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
#if defined(CPU_ARM6) || defined(CPU_ARM7)
logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
physical_start, kernexec->a_text,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
#else
logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
physical_start, kernexec->a_text,
VM_PROT_READ, PTE_CACHE);
#endif
logical += pmap_map_chunk(l1pagetable,
KERNEL_TEXT_BASE + logical, physical_start + logical,
kerneldatasize - kernexec->a_text,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
} else {
pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
physical_start, kerneldatasize,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
};
#ifdef VERBOSE_INIT_ARM
printf("Constructing L2 page tables\n");
#endif
pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
}
pmap_map_chunk(l1pagetable, VMEM_VBASE, videomemory.vidm_pbase,
videomemory.vidm_size, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, VMEM_VBASE + videomemory.vidm_size,
videomemory.vidm_pbase, videomemory.vidm_size,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
loop = 0;
while (l1_sec_table[loop].size) {
vsize_t sz;
#ifdef VERBOSE_INIT_ARM
printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
l1_sec_table[loop].va);
#endif
for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
pmap_map_section(l1pagetable,
l1_sec_table[loop].va + sz,
l1_sec_table[loop].pa + sz,
l1_sec_table[loop].prot,
l1_sec_table[loop].cache);
++loop;
}
#ifdef VERBOSE_INIT_ARM
printf("switching domains\n");
#endif
cpu_domains(0x55555555);
#ifdef VERBOSE_INIT_ARM
printf("switching to new L1 page table\n");
#endif
cpu_setttb(kernel_l1pt.pv_pa, true);
cpu_idcache_wbinv_all();
cpu_tlb_flushID();
cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
if (bootconfig.vram[0].pages == 0) {
videomemory.vidm_vbase = VMEM_VBASE;
} else {
videomemory.vidm_vbase = VMEM_VBASE;
bootconfig.display_start = VMEM_VBASE;
};
vidc_base = (int *) VIDC_BASE;
iomd_base = IOMD_BASE;
#ifdef VERBOSE_INIT_ARM
printf("running on the new L1 page table!\n");
printf("done.\n");
#endif
arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
#ifdef VERBOSE_INIT_ARM
printf("\n");
#endif
#ifdef VERBOSE_INIT_ARM
printf("init subsystems: stacks ");
console_flush();
#endif
set_stackptr(PSR_IRQ32_MODE,
irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
set_stackptr(PSR_ABT32_MODE,
abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
set_stackptr(PSR_UND32_MODE,
undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
#ifdef VERBOSE_INIT_ARM
printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
kernelstack.pv_pa);
#endif
#ifdef VERBOSE_INIT_ARM
printf("vectors ");
#endif
data_abort_handler_address = (u_int)data_abort_handler;
prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
undefined_handler_address = (u_int)undefinedinstruction_bounce;
console_flush();
#ifdef VERBOSE_INIT_ARM
printf("undefined ");
#endif
undefined_init();
console_flush();
#ifdef VERBOSE_INIT_ARM
printf("page ");
#endif
uvm_md_init();
for (loop = 0; loop < bootconfig.dramblocks; loop++) {
paddr_t start = (paddr_t)bootconfig.dram[loop].address;
paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
if (start < physical_freestart)
start = physical_freestart;
if (end > physical_freeend)
end = physical_freeend;
uvm_page_physload(atop(start), atop(end),
atop(start), atop(end), VM_FREELIST_DEFAULT);
}
#ifdef VERBOSE_INIT_ARM
printf("pmap ");
#endif
pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
console_flush();
#ifdef VERBOSE_INIT_ARM
printf("irq ");
#endif
console_flush();
irq_init();
#ifdef VERBOSE_INIT_ARM
printf("done.\n\n");
#endif
#if NVIDCVIDEO>0
consinit();
#endif
printf("NetBSD/evbarm booting ... \n");
if ((bootconfig.magic < BOOTCONFIG_MAGIC) ||
(bootconfig.version != BOOTCONFIG_VERSION)) {
printf("\nDETECTED AN OLD BOOTLOADER. PLEASE UPGRADE IT\n\n");
delay(5000000);
}
printf("Kernel loaded from file %s\n", bootconfig.kernelname);
printf("Kernel arg string (@%p) %s\n",
bootconfig.args, bootconfig.args);
printf("\nBoot configuration structure reports the following "
"memory\n");
printf(" DRAM block 0a at %08x size %08x "
"DRAM block 0b at %08x size %08x\n\r",
bootconfig.dram[0].address,
bootconfig.dram[0].pages * bootconfig.pagesize,
bootconfig.dram[1].address,
bootconfig.dram[1].pages * bootconfig.pagesize);
printf(" DRAM block 1a at %08x size %08x "
"DRAM block 1b at %08x size %08x\n\r",
bootconfig.dram[2].address,
bootconfig.dram[2].pages * bootconfig.pagesize,
bootconfig.dram[3].address,
bootconfig.dram[3].pages * bootconfig.pagesize);
printf(" VRAM block 0 at %08x size %08x\n\r",
bootconfig.vram[0].address,
bootconfig.vram[0].pages * bootconfig.pagesize);
#if NKSYMS || defined(DDB) || defined(MODULAR)
ksyms_addsyms_elf(bootconfig.ksym_end - bootconfig.ksym_start,
(void *) bootconfig.ksym_start, (void *) bootconfig.ksym_end);
#endif
#ifdef DDB
db_machine_init();
if (boothowto & RB_KDB)
Debugger();
#endif
return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
}
static void
process_kernel_args(void)
{
char *args;
args = bootconfig.args;
boothowto = 0;
while (*args == ' ')
++args;
boot_args = args;
parse_mi_bootargs(boot_args);
parse_rpc_bootargs(boot_args);
}
void
parse_rpc_bootargs(char *args)
{
int integer;
if (get_bootconf_option(args, "videodram", BOOTOPT_TYPE_INT,
&integer)) {
videodram_size = integer;
videodram_size *= 1024;
videodram_size = round_page(videodram_size);
if (videodram_size > 1024*1024)
videodram_size = 1024*1024;
}
}