#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: brh_machdep.c,v 1.56 2024/02/20 23:36:02 andvar Exp $");
#include "opt_arm_debug.h"
#include "opt_console.h"
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/exec.h>
#include <sys/proc.h>
#include <sys/msgbuf.h>
#include <sys/reboot.h>
#include <sys/termios.h>
#include <sys/ksyms.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include <machine/db_machdep.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#include <machine/bootconfig.h>
#include <arm/locore.h>
#include <arm/undefined.h>
#include <arm/arm32/machdep.h>
#include <arm/xscale/i80200reg.h>
#include <arm/xscale/i80200var.h>
#include <dev/pci/ppbreg.h>
#include <arm/xscale/beccreg.h>
#include <arm/xscale/beccvar.h>
#include <evbarm/adi_brh/brhreg.h>
#include <evbarm/adi_brh/brhvar.h>
#include <evbarm/adi_brh/obiovar.h>
#include "ksyms.h"
#define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00200000)
#define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
#define KERNEL_VM_SIZE 0x0C000000
BootConfig bootconfig;
char *boot_args = NULL;
char *boot_file = NULL;
vaddr_t physical_start;
vaddr_t physical_freestart;
vaddr_t physical_freeend;
vaddr_t physical_end;
u_int free_pages;
#ifndef PMAP_STATIC_L1S
int max_processes = 64;
#endif
pv_addr_t minidataclean;
paddr_t msgbufphys;
#define KERNEL_PT_SYS 0
#define KERNEL_PT_KERNEL 1
#define KERNEL_PT_KERNEL_NUM 2
#define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
#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];
void consinit(void);
#include "com.h"
#if NCOM > 0
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
#endif
#ifndef CONSPEED
#define CONSPEED B57600
#endif
#ifndef CONUNIT
#define CONUNIT 0
#endif
#ifndef CONMODE
#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
#endif
int comcnspeed = CONSPEED;
int comcnmode = CONMODE;
int comcnunit = CONUNIT;
void
cpu_reboot(int howto, char *bootstr)
{
if (cold) {
doshutdownhooks();
pmf_system_shutdown(boothowto);
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cngetc();
printf("rebooting...\n");
goto reset;
}
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) {
brh_7seg('8');
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cngetc();
}
printf("rebooting...\n\r");
reset:
cpu_reset();
}
static const struct pmap_devmap brh_devmap[] = {
DEVMAP_ENTRY(
BRH_PCI_CONF_VBASE,
BECC_PCI_CONF_BASE,
BRH_PCI_CONF_VSIZE
),
DEVMAP_ENTRY(
BRH_PCI_MEM1_VBASE,
BECC_PCI_MEM1_BASE,
BRH_PCI_MEM1_VSIZE
),
DEVMAP_ENTRY(
BRH_PCI_MEM2_VBASE,
BECC_PCI_MEM2_BASE,
BRH_PCI_MEM2_VSIZE
),
DEVMAP_ENTRY(
BRH_UART1_VBASE,
BRH_UART1_BASE,
BRH_UART1_VSIZE
),
DEVMAP_ENTRY(
BRH_UART2_VBASE,
BRH_UART2_BASE,
BRH_UART2_VSIZE
),
DEVMAP_ENTRY(
BRH_LED_VBASE,
BRH_LED_BASE,
BRH_LED_VSIZE
),
DEVMAP_ENTRY(
BRH_PCI_IO_VBASE,
BECC_PCI_IO_BASE,
BRH_PCI_IO_VSIZE
),
DEVMAP_ENTRY(
BRH_BECC_VBASE,
BECC_REG_BASE,
BRH_BECC_VSIZE
),
DEVMAP_ENTRY_END
};
static void
brh_hardclock_hook(void)
{
static int snakefreq;
if ((snakefreq++ & 15) == 0)
brh_7seg_snake();
}
vaddr_t
initarm(void *arg)
{
int loop;
int loop1;
u_int l1pagetable;
paddr_t memstart;
psize_t memsize;
brh_7seg(' ');
consinit();
#ifdef VERBOSE_INIT_ARM
printf("\nNetBSD/evbarm (ADI BRH) booting ...\n");
#endif
becc_hardclock_hook = brh_hardclock_hook;
if (set_cpufuncs())
panic("CPU not recognized!");
becc_icu_init();
memstart = 0xc0000000UL;
memsize = (128UL * 1024 * 1024);
#ifdef VERBOSE_INIT_ARM
printf("initarm: Configuring system ...\n");
#endif
bootconfig.dramblocks = 1;
bootconfig.dram[0].address = memstart;
bootconfig.dram[0].pages = memsize / PAGE_SIZE;
physical_start = bootconfig.dram[0].address;
physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
physical_freestart = 0xc0009000UL;
physical_freeend = 0xc0200000UL;
#ifdef VERBOSE_INIT_ARM
printf("physmemory: 0x%"PRIxPSIZE" pages at "
"0x%08"PRIxPADDR" -> 0x%08"PRIxPADDR"\n",
physmem, physical_start, physical_end - 1);
#endif
#ifdef VERBOSE_INIT_ARM
printf("Allocating page tables\n");
#endif
free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
#ifdef VERBOSE_INIT_ARM
printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
physical_freestart, free_pages, free_pages);
#endif
#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) \
physical_freeend -= ((np) * PAGE_SIZE); \
if (physical_freeend < physical_freestart) \
panic("initarm: out of memory"); \
(var) = physical_freeend; \
free_pages -= (np); \
memset((char *)(var), 0, ((np) * PAGE_SIZE));
loop1 = 0;
for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
if (((physical_freeend - L1_TABLE_SIZE) & (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;
}
}
if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
panic("initarm: Failed to align the kernel page directory\n");
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);
KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
valloc_pages(minidataclean, 1);
#ifdef VERBOSE_INIT_ARM
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);
#endif
alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
#ifdef VERBOSE_INIT_ARM
printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
#endif
l1pagetable = kernel_l1pt.pv_pa;
pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
&kernel_pt_table[KERNEL_PT_SYS]);
for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
&kernel_pt_table[KERNEL_PT_KERNEL + loop]);
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_curmaxkvaddr =
KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
#ifdef VERBOSE_INIT_ARM
printf("Mapping kernel\n");
#endif
{
extern char etext[], _end[];
size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
u_int logical;
textsize = (textsize + PGOFSET) & ~PGOFSET;
totalsize = (totalsize + PGOFSET) & ~PGOFSET;
logical = 0x00200000;
logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
physical_start + logical, textsize,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
physical_start + logical, totalsize - textsize,
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);
}
xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
minidataclean.pv_pa);
pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_devmap_bootstrap(l1pagetable, brh_devmap);
xscale_cache_clean_addr = 0xff500000U;
#ifdef VERBOSE_INIT_ARM
printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
#endif
cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
cpu_setttb(kernel_l1pt.pv_pa, true);
cpu_tlb_flushID();
cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
#ifdef VERBOSE_INIT_ARM
printf("done!\n");
#endif
#ifdef VERBOSE_INIT_ARM
printf("bootstrap done.\n");
#endif
becc_vaddr = BRH_BECC_VBASE;
becc_calibrate_delay();
{
vaddr_t va = BRH_PCI_CONF_VBASE | (1U << BECC_IDSEL_BIT) |
PCI_CLASS_REG;
uint32_t reg;
reg = *(volatile uint32_t *) va;
becc_rev = PCI_REVISION(reg);
if (becc_rev <= BECC_REV_V7 &&
memsize > (64UL * 1024 * 1024)) {
memsize = (64UL * 1024 * 1024);
bootconfig.dram[0].pages = memsize / PAGE_SIZE;
physical_end = physical_start +
(bootconfig.dram[0].pages * PAGE_SIZE);
printf("BECC <= Rev7: memory truncated to 64M\n");
}
}
{
extern char _end[];
physical_freestart = physical_start +
(((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
KERNEL_BASE);
physical_freeend = physical_end;
free_pages =
(physical_freeend - physical_freestart) / PAGE_SIZE;
}
#ifdef VERBOSE_INIT_ARM
printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
physical_freestart, free_pages, free_pages);
#endif
physmem = (physical_end - physical_start) / PAGE_SIZE;
arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
#ifdef VERBOSE_INIT_ARM
printf("init subsystems: stacks ");
#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("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;
#ifdef VERBOSE_INIT_ARM
printf("undefined ");
#endif
undefined_init();
#ifdef VERBOSE_INIT_ARM
printf("page ");
#endif
uvm_md_init();
uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
atop(physical_freestart), atop(physical_freeend),
VM_FREELIST_DEFAULT);
#ifdef VERBOSE_INIT_ARM
printf("pmap ");
#endif
pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
#ifdef VERBOSE_INIT_ARM
printf("irq ");
#endif
becc_intr_init();
#ifdef VERBOSE_INIT_ARM
printf("done.\n");
#endif
#ifdef DDB
db_machine_init();
if (boothowto & RB_KDB)
Debugger();
#endif
return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
}
void
consinit(void)
{
static const bus_addr_t comcnaddrs[] = {
BRH_UART1_BASE,
BRH_UART2_BASE,
};
static int consinit_called;
if (consinit_called != 0)
return;
consinit_called = 1;
pmap_devmap_register(brh_devmap);
#if NCOM > 0
if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
BECC_PERIPH_CLOCK, COM_TYPE_NORMAL, comcnmode))
panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
#else
panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
#endif
}