#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.64 2024/05/28 11:06:07 macallan Exp $");
#include "ksyms.h"
#include "wsdisplay.h"
#ifdef _KERNEL_OPT
#include "opt_ddb.h"
#include "opt_kgdb.h"
#include "opt_modular.h"
#include "opt_multiprocessor.h"
#include "opt_oea.h"
#include "opt_ofwoea.h"
#include "opt_ppcarch.h"
#endif
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/boot_flag.h>
#include <sys/extent.h>
#include <sys/kernel.h>
#include <sys/ksyms.h>
#include <uvm/uvm_extern.h>
#include <dev/ofw/openfirm.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/rasops/rasops.h>
#include <dev/wscons/wsdisplay_vconsvar.h>
#include <machine/pmap.h>
#include <machine/powerpc.h>
#include <machine/trap.h>
#include <machine/vmparam.h>
#include <machine/autoconf.h>
#include <sys/bus.h>
#include <powerpc/oea/bat.h>
#include <powerpc/oea/ofw_rasconsvar.h>
#include <powerpc/oea/cpufeat.h>
#include <powerpc/include/oea/spr.h>
#include <powerpc/ofw_cons.h>
#include <powerpc/ofw_machdep.h>
#include <powerpc/spr.h>
#include <powerpc/pic/picvar.h>
#ifdef DDB
#include <machine/db_machdep.h>
#include <ddb/db_extern.h>
#endif
#ifdef KGDB
#include <sys/kgdb.h>
#endif
#ifdef ofppc
extern struct model_data modeldata;
#endif
#ifdef OFWOEA_DEBUG
#define DPRINTF printf
#else
#define DPRINTF while (0) printf
#endif
typedef struct _rangemap {
u_int32_t addr;
u_int32_t size;
int type;
} rangemap_t;
struct OF_translation ofw_translations[OFW_MAX_TRANSLATIONS];
struct pmap ofw_pmap;
struct bat ofw_battable[BAT_VA2IDX(0xffffffff)+1];
char bootpath[256] = "";
char model_name[64];
#if NKSYMS || defined(DDB) || defined(MODULAR)
void *startsym, *endsym;
#endif
#if PPC_OEA601
#define TIMEBASE_FREQ (1000000000)
#endif
#ifdef TIMEBASE_FREQ
u_int timebase_freq = TIMEBASE_FREQ;
#else
u_int timebase_freq = 0;
#endif
int ofw_quiesce;
extern int ofwmsr;
extern uint32_t ticks_per_sec;
extern uint32_t ns_per_tick;
extern uint32_t ticks_per_intr;
static void get_timebase_frequency(void);
static void init_decrementer(void);
static void restore_ofmap(void);
void
ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
{
register_t scratch;
#if NKSYMS || defined(DDB) || defined(MODULAR)
memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
sizeof(endsym));
if (startsym == NULL || endsym == NULL)
startsym = endsym = NULL;
#endif
if (args) {
strcpy(bootpath, args);
args = bootpath;
while (*++args && *args != ' ');
if (*args) {
*args++ = 0;
while (*args)
BOOT_FLAG(*args++, boothowto);
}
}
if (bootpath[0] == 0) {
int chs = OF_finddevice("/chosen");
int len;
len = OF_getprop(chs, "bootpath", bootpath, sizeof(bootpath) - 1);
if (len > -1)
bootpath[len] = 0;
}
get_timebase_frequency();
ofwoea_cnprobe();
if (ofw_quiesce)
OF_quiesce();
oea_init(pic_ext_intr);
ofwmsr &= ~PSL_IP;
ofwoea_bus_space_init();
ofwoea_consinit();
uvm_md_init();
pmap_bootstrap(startkernel, endkernel);
#if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
#if defined (PMAC_G5)
if (oeacpufeat & OEACPU_64_BRIDGE) {
vaddr_t va;
paddr_t pa;
vsize_t size;
int i;
pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
round_page(MSGBUFSIZE), 0x0);
for (i = 0; i < __arraycount(ofw_translations); i++) {
va = ofw_translations[i].virt;
size = ofw_translations[i].size;
pa = ofw_translations[i].phys;
if (size == 0) {
break;
}
if (va < 0xff800000)
continue;
for (; va < (ofw_translations[i].virt + size);
va += PAGE_SIZE, pa += PAGE_SIZE) {
pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
VM_PROT_ALL | PMAP_WIRED);
}
}
#if NWSDISPLAY > 0
struct rasops_info *ri = &rascons_console_screen.scr_ri;
if (ri->ri_bits != NULL) {
for (va = (vaddr_t) ri->ri_bits;
va < round_page((vaddr_t) ri->ri_bits +
ri->ri_height * ri->ri_stride);
va += PAGE_SIZE) {
pmap_enter(pmap_kernel(), va, va,
VM_PROT_READ | VM_PROT_WRITE,
PMAP_NOCACHE | PMAP_WIRED);
}
}
#endif
}
#elif defined (MAMBO)
if (oeacpufeat & OEACPU_64_BRIDGE)
pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
#endif
#endif
__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
: "=r"(scratch)
: "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
restore_ofmap();
#if NWSDISPLAY > 0
rascons_finalize();
#endif
#if NKSYMS || defined(DDB) || defined(MODULAR)
ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
#endif
init_decrementer();
#ifdef DDB
if (boothowto & RB_KDB)
Debugger();
#endif
}
static void
get_timebase_frequency(void)
{
int qhandle, phandle, node;
char type[32];
if (timebase_freq != 0) {
ticks_per_sec = timebase_freq;
return;
}
node = OF_finddevice("/cpus/@0");
if (node != -1 &&
OF_getprop(node, "timebase-frequency", &ticks_per_sec,
sizeof ticks_per_sec) > 0) {
return;
}
node = OF_finddevice("/");
for (qhandle = node; qhandle; qhandle = phandle) {
if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
&& strcmp(type, "cpu") == 0
&& OF_getprop(qhandle, "timebase-frequency",
&ticks_per_sec, sizeof ticks_per_sec) > 0) {
return;
}
if ((phandle = OF_child(qhandle)))
continue;
while (qhandle) {
if ((phandle = OF_peer(qhandle)))
break;
qhandle = OF_parent(qhandle);
}
}
panic("no cpu node");
}
static void
init_decrementer(void)
{
int scratch, msr;
KASSERT(ticks_per_sec != 0);
__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
ns_per_tick = 1000000000 / ticks_per_sec;
ticks_per_intr = ticks_per_sec / hz;
cpu_timebase = ticks_per_sec;
#ifdef PPC_OEA601
if ((mfpvr() >> 16) == MPC601)
curcpu()->ci_lasttb = rtc_nanosecs();
else
#endif
curcpu()->ci_lasttb = mftbl();
mtspr(SPR_DEC, ticks_per_intr);
mtmsr(msr);
}
void
restore_ofmap(void)
{
vaddr_t va, size;
paddr_t pa;
int i;
pmap_pinit(&ofw_pmap);
#ifndef _LP64
ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
#ifdef KERNEL2_SR
ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
#endif
#endif
for (i = 0; i < __arraycount(ofw_translations); i++) {
va = ofw_translations[i].virt;
size = ofw_translations[i].size;
pa = ofw_translations[i].phys;
if (size == 0) {
break;
}
if (va < 0xf0000000)
continue;
if (pa == 0xffc00000 && size == 0x400000)
size = 0x3ff000;
while (size > 0) {
pmap_enter(&ofw_pmap, va, pa, VM_PROT_ALL,
VM_PROT_ALL|PMAP_WIRED);
pa += PAGE_SIZE;
va += PAGE_SIZE;
size -= PAGE_SIZE;
}
}
pmap_update(&ofw_pmap);
}
struct powerpc_bus_space genppc_isa_io_space_tag = {
.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
.pbs_base = 0x00000000,
};
struct powerpc_bus_space genppc_isa_mem_space_tag = {
.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
.pbs_base = 0x00000000,
};
static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
__attribute__((aligned(8)));
static void
find_ranges(int base, rangemap_t *regions, int *cur, int type)
{
int node, i, len, reclen;
u_int32_t parent_acells, acells, scells, map[160];
char tmp[32];
node = base;
if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
goto rec;
if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
strcmp("pci", tmp) != 0)
goto rec;
if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
goto rec;
if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
len = OF_getprop(node, "reg", map, sizeof(map));
acells = 1;
scells = 1;
} else {
len = OF_getprop(node, "ranges", map, sizeof(map));
}
if (len == -1)
goto rec;
if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
sizeof(parent_acells)) != sizeof(parent_acells))
parent_acells = 1;
if (OF_getprop(node, "#address-cells", &acells,
sizeof(acells)) != sizeof(acells))
acells = 3;
if (OF_getprop(node, "#size-cells", &scells,
sizeof(scells)) != sizeof(scells))
scells = 2;
#ifdef ofppc
if (modeldata.ranges_offset == 0)
scells -= 1;
#endif
if (type == RANGE_TYPE_ISA)
reclen = 6;
else
reclen = parent_acells + acells + scells;
if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
*cur = -1;
DPRINTF("Found empty range in isa bus\n");
return;
}
DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
switch (type) {
case RANGE_TYPE_PCI:
case RANGE_TYPE_FIRSTPCI:
for (i=0; i < len/(4*reclen); i++) {
DPRINTF("FOUND PCI RANGE\n");
regions[*cur].size =
map[i*reclen + parent_acells + acells + scells - 1];
if (regions[*cur].size == 0)
continue;
regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
(*cur)++;
}
break;
case RANGE_TYPE_ISA:
for (i=0; i < len/(4*reclen); i++) {
if (map[i*reclen] == 1)
regions[*cur].type = RANGE_IO;
else
regions[*cur].type = RANGE_MEM;
DPRINTF("FOUND ISA RANGE TYPE=%d\n",
regions[*cur].type);
regions[*cur].size =
map[i*reclen + acells + scells];
(*cur)++;
}
break;
case RANGE_TYPE_MACIO:
regions[*cur].type = RANGE_MEM;
if (len == 8) {
regions[*cur].size = map[1];
regions[*cur].addr = map[0];
} else {
regions[*cur].size = map[2];
regions[*cur].addr = map[1];
}
(*cur)++;
break;
}
DPRINTF("returning with CUR=%d\n", *cur);
return;
rec:
for (node = OF_child(base); node; node = OF_peer(node)) {
DPRINTF("RECURSE 1 STEP\n");
find_ranges(node, regions, cur, type);
if (*cur == -1)
return;
}
}
static int
find_lowest_range(rangemap_t *ranges, int nrof, int type)
{
int i, low = 0;
u_int32_t addr = 0xffffffff;
for (i=0; i < nrof; i++) {
if (ranges[i].type == type && ranges[i].addr != 0 &&
ranges[i].addr < addr) {
low = i;
addr = ranges[i].addr;
}
}
if (addr == 0xffffffff)
return -1;
return low;
}
int
ofwoea_map_space(int rangetype, int iomem, int node,
struct powerpc_bus_space *tag, const char *name)
{
int i, cur, range, nrofholes, error;
static int exmap=0;
rangemap_t region, holes[32], list[32];
memset(list, 0, sizeof(list));
memset(®ion, 0, sizeof(region));
cur = 0;
if (rangetype == RANGE_TYPE_ISA || node == -1)
node = OF_finddevice("/");
if (rangetype == RANGE_TYPE_ISA) {
u_int32_t size = 0;
rangemap_t regions[32];
DPRINTF("LOOKING FOR FIRSTPCI\n");
find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
range = 0;
DPRINTF("LOOKING FOR ISA\n");
find_ranges(node, regions, &range, RANGE_TYPE_ISA);
if (range == 0 || cur == 0)
return -1;
if (range == -1) {
if (iomem == RANGE_IO)
size = 0x10000;
else
size = 0x1000000;
}
DPRINTF("found isa stuff\n");
for (i=0; i < range; i++)
if (regions[i].type == iomem)
size = regions[i].size;
if (iomem == RANGE_IO) {
for (i=0; i < cur; i++)
if (list[i].type == RANGE_IO && size) {
DPRINTF("found IO\n");
tag->pbs_offset = list[i].addr;
tag->pbs_limit = size;
error = bus_space_init(tag, name,
ex_storage[exmap],
sizeof(ex_storage[exmap]));
exmap++;
return error;
}
} else {
for (i=0; i < cur; i++)
if (list[i].type == RANGE_MEM &&
list[i].size == size) {
DPRINTF("found mem\n");
tag->pbs_offset = list[i].addr;
tag->pbs_limit = size;
error = bus_space_init(tag, name,
ex_storage[exmap],
sizeof(ex_storage[exmap]));
exmap++;
return error;
}
}
return -1;
}
find_ranges(node, list, &cur, rangetype);
DPRINTF("cur == %d\n", cur);
for (i=0; i < cur; i++)
DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
list[i].size, list[i].type);
range = find_lowest_range(list, cur, iomem);
i = 0;
nrofholes = 0;
while (range != -1) {
DPRINTF("range==%d\n", range);
DPRINTF("i==%d\n", i);
if (i == 0) {
memcpy(®ion, &list[range], sizeof(rangemap_t));
list[range].addr = 0;
i++;
range = find_lowest_range(list, cur, iomem);
continue;
}
if (region.addr + region.size < list[range].addr) {
holes[nrofholes].type = iomem;
holes[nrofholes].addr = region.size + region.addr;
holes[nrofholes].size = list[range].addr -
holes[nrofholes].addr - 1;
nrofholes++;
}
region.size = list[range].size + list[range].addr -
region.addr;
list[range].addr = 0;
range = find_lowest_range(list, cur, iomem);
}
DPRINTF("RANGE iomem=%d FOUND\n", iomem);
DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
region.size, region.type);
DPRINTF("HOLES FOUND\n");
for (i=0; i < nrofholes; i++)
DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
holes[i].size, holes[i].type);
if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
if (exmap == EXSTORAGE_MAX)
panic("Not enough ex_storage space. "
"Increase EXSTORAGE_MAX");
if (iomem == 1) {
tag->pbs_offset = region.addr;
tag->pbs_base = 0;
tag->pbs_limit = region.size;
} else {
tag->pbs_offset = 0;
tag->pbs_base = region.addr;
tag->pbs_limit = region.size + region.addr;
}
error = bus_space_init(tag, name, ex_storage[exmap],
sizeof(ex_storage[exmap]));
exmap++;
if (error)
panic("ofwoea_bus_space_init: can't init tag %s", name);
for (i=0; i < nrofholes; i++) {
if (holes[i].type == RANGE_IO) {
error = extent_alloc_region(tag->pbs_extent,
holes[i].addr - tag->pbs_offset,
holes[i].size, EX_NOWAIT);
} else {
error = extent_alloc_region(tag->pbs_extent,
holes[i].addr, holes[i].size, EX_NOWAIT);
}
if (error)
panic("ofwoea_bus_space_init: can't block out"
" reserved space 0x%x-0x%x: error=%d",
holes[i].addr, holes[i].addr+holes[i].size,
error);
}
return error;
}
return -1;
}
void
ofwoea_bus_space_init(void)
{
int error;
error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
&genppc_isa_io_space_tag, "isa-ioport");
if (error > 0)
panic("Could not map ISA IO");
error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
&genppc_isa_mem_space_tag, "isa-iomem");
if (error > 0)
panic("Could not map ISA MEM");
}