Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.42
      1 /* $NetBSD: ofwoea_machdep.c,v 1.42 2018/01/21 08:46:48 mrg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tim Rightnour
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.42 2018/01/21 08:46:48 mrg Exp $");
     34 
     35 #include "opt_ppcarch.h"
     36 #include "opt_compat_netbsd.h"
     37 #include "opt_ddb.h"
     38 #include "opt_kgdb.h"
     39 #include "opt_ipkdb.h"
     40 #include "opt_modular.h"
     41 
     42 #include "wsdisplay.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/buf.h>
     46 #include <sys/boot_flag.h>
     47 #include <sys/extent.h>
     48 #include <sys/kernel.h>
     49 #include <sys/ksyms.h>
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <dev/ofw/openfirm.h>
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wscons/wsdisplayvar.h>
     55 #include <dev/rasops/rasops.h>
     56 #include <dev/wscons/wsdisplay_vconsvar.h>
     57 #include <machine/pmap.h>
     58 #include <machine/powerpc.h>
     59 #include <machine/trap.h>
     60 #include <machine/vmparam.h>
     61 #include <machine/autoconf.h>
     62 #include <sys/bus.h>
     63 #include <powerpc/oea/bat.h>
     64 #include <powerpc/oea/ofw_rasconsvar.h>
     65 #include <powerpc/oea/cpufeat.h>
     66 #include <powerpc/include/oea/spr.h>
     67 #include <powerpc/ofw_cons.h>
     68 #include <powerpc/spr.h>
     69 #include <powerpc/pic/picvar.h>
     70 
     71 #include "opt_oea.h"
     72 
     73 #include "ksyms.h"
     74 
     75 #ifdef DDB
     76 #include <machine/db_machdep.h>
     77 #include <ddb/db_extern.h>
     78 #endif
     79 
     80 #ifdef KGDB
     81 #include <sys/kgdb.h>
     82 #endif
     83 
     84 #ifdef IPKDB
     85 #include <ipkdb/ipkdb.h>
     86 #endif
     87 
     88 #include "opt_ofwoea.h"
     89 
     90 #ifdef ofppc
     91 extern struct model_data modeldata;
     92 #endif
     93 
     94 #ifdef OFWOEA_DEBUG
     95 #define DPRINTF printf
     96 #else
     97 #define DPRINTF while (0) printf
     98 #endif
     99 
    100 typedef struct _rangemap {
    101 	u_int32_t addr;
    102 	u_int32_t size;
    103 	int type;
    104 } rangemap_t;
    105 
    106 struct ofw_translations {
    107 	vaddr_t va;
    108 	int len;
    109 #if defined (PMAC_G5)
    110 	register64_t pa;
    111 #else
    112 	register_t pa;
    113 #endif
    114 	int mode;
    115 }__attribute__((packed));
    116 
    117 struct pmap ofw_pmap;
    118 struct ofw_translations ofmap[32];
    119 char bootpath[256];
    120 char model_name[64];
    121 #if NKSYMS || defined(DDB) || defined(MODULAR)
    122 void *startsym, *endsym;
    123 #endif
    124 
    125 #if PPC_OEA601
    126 #define TIMEBASE_FREQ (1000000000)  /* RTC register */
    127 #endif
    128 
    129 #ifdef TIMEBASE_FREQ
    130 u_int timebase_freq = TIMEBASE_FREQ;
    131 #else
    132 u_int timebase_freq = 0;
    133 #endif
    134 
    135 extern int ofwmsr;
    136 extern int chosen;
    137 extern uint32_t ticks_per_sec;
    138 extern uint32_t ns_per_tick;
    139 extern uint32_t ticks_per_intr;
    140 
    141 static int save_ofmap(struct ofw_translations *, int);
    142 static void restore_ofmap(struct ofw_translations *, int);
    143 static void set_timebase(void);
    144 
    145 extern void cpu_spinstart(u_int);
    146 extern volatile u_int cpu_spinstart_ack;
    147 
    148 void
    149 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    150 {
    151 	int ofmaplen, node, l;
    152 	register_t scratch;
    153 
    154 #if defined(MULTIPROCESSOR) && defined(ofppc)
    155 	char cpupath[32];
    156 	int i;
    157 #endif
    158 
    159 	/* initialze bats */
    160 	if ((oeacpufeat & OEACPU_NOBAT) == 0)
    161 		ofwoea_batinit();
    162 
    163 #if NKSYMS || defined(DDB) || defined(MODULAR)
    164 	/* get info of kernel symbol table from bootloader */
    165 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    166 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    167 	    sizeof(endsym));
    168 	if (startsym == NULL || endsym == NULL)
    169 	    startsym = endsym = NULL;
    170 #endif
    171 
    172 	/* get model name and perform model-specific actions */
    173 	memset(model_name, 0, sizeof(model_name));
    174 	node = OF_finddevice("/");
    175 	if (node != -1) {
    176 		l = OF_getprop(node, "model", model_name, sizeof(model_name));
    177 		if (l == -1)
    178 			OF_getprop(node, "name", model_name,
    179 			    sizeof(model_name));
    180 		model_init();
    181 	}
    182 
    183 	if (strcmp(model_name, "PowerMac11,2") == 0 ||
    184 	    strcmp(model_name, "PowerMac11,1") == 0)
    185 		OF_quiesce();
    186 
    187 	/* Initialize bus_space */
    188 	ofwoea_bus_space_init();
    189 
    190 	ofwoea_consinit();
    191 
    192 #if defined(MULTIPROCESSOR) && defined(ofppc)
    193 	for (i=1; i < CPU_MAXNUM; i++) {
    194 		snprintf(cpupath, sizeof(cpupath), "/cpus/@%x", i);
    195 		node = OF_finddevice(cpupath);
    196 		if (node <= 0)
    197 			continue;
    198 		aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
    199 		OF_start_cpu(node, (u_int)cpu_spinstart, i);
    200 		for (l=0; l < 100000000; l++) {
    201 			if (cpu_spinstart_ack == i) {
    202 				aprint_verbose("CPU %d spun up.\n", i);
    203 				break;
    204 			}
    205 			__asm volatile ("sync");
    206 		}
    207 	}
    208 #endif
    209 
    210 	oea_init(pic_ext_intr);
    211 
    212 	ofmaplen = save_ofmap(NULL, 0);
    213 	if (ofmaplen > 0)
    214 		save_ofmap(ofmap, ofmaplen);
    215 
    216 /*
    217  * XXX
    218  * we need to do this here instead of earlier on in ofwinit() for some reason
    219  * At least some versions of Apple OF 2.0.1 hang if we do this earlier
    220  */
    221 	ofwmsr &= ~PSL_IP;
    222 
    223 	/* Parse the args string */
    224 	if (args) {
    225 		strcpy(bootpath, args);
    226 		args = bootpath;
    227 		while (*++args && *args != ' ');
    228 		if (*args) {
    229 			*args++ = 0;
    230 			while (*args)
    231 				BOOT_FLAG(*args++, boothowto);
    232 		}
    233 	}
    234 
    235 	uvm_md_init();
    236 
    237 	pmap_bootstrap(startkernel, endkernel);
    238 
    239 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    240 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    241 #if defined (PMAC_G5)
    242 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    243 	if (oeacpufeat & OEACPU_64_BRIDGE) {
    244 		vaddr_t va;
    245 		paddr_t pa;
    246 		int i;
    247 
    248 		pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
    249 		    round_page(MSGBUFSIZE), 0x0);
    250 
    251 		/* Map OFW code+data */
    252 
    253 		for (i = 0; i < ofmaplen / sizeof(struct ofw_translations); i++) {
    254 			if (ofmap[i].va < 0xff800000)
    255 				continue;
    256 
    257 			for (va = ofmap[i].va, pa = ofmap[i].pa;
    258 			    va < ofmap[i].va + ofmap[i].len;
    259 			    va += PAGE_SIZE, pa += PAGE_SIZE) {
    260 				pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
    261 				    VM_PROT_ALL | PMAP_WIRED);
    262 			}
    263 		}
    264 
    265 #if NWSDISPLAY > 0
    266 		/* Map video frame buffer */
    267 
    268 		struct rasops_info *ri = &rascons_console_screen.scr_ri;
    269 
    270 		if (ri->ri_bits != NULL) {
    271 			for (va = (vaddr_t) ri->ri_bits;
    272 			    va < round_page((vaddr_t) ri->ri_bits +
    273 				ri->ri_height * ri->ri_stride);
    274 			    va += PAGE_SIZE) {
    275 				pmap_enter(pmap_kernel(), va, va,
    276 				    VM_PROT_READ | VM_PROT_WRITE,
    277 				    PMAP_NOCACHE | PMAP_WIRED);
    278 			}
    279 		}
    280 #endif
    281 	}
    282 #elif defined (MAMBO)
    283 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    284 	if (oeacpufeat & OEACPU_64_BRIDGE)
    285 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    286 #endif /* PMAC_G5 */
    287 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    288 
    289 	/* Now enable translation (and machine checks/recoverable interrupts) */
    290 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    291 	    : "=r"(scratch)
    292 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    293 
    294 	restore_ofmap(ofmap, ofmaplen);
    295 
    296 #if NKSYMS || defined(DDB) || defined(MODULAR)
    297 	ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
    298 #endif
    299 
    300 	/* CPU clock stuff */
    301 	set_timebase();
    302 
    303 #ifdef DDB
    304 	if (boothowto & RB_KDB)
    305 		Debugger();
    306 #endif
    307 }
    308 
    309 void
    310 set_timebase(void)
    311 {
    312 	int qhandle, phandle, msr, scratch, node;
    313 	char type[32];
    314 
    315 	if (timebase_freq != 0) {
    316 		ticks_per_sec = timebase_freq;
    317 		goto found;
    318 	}
    319 
    320 	node = OF_finddevice("/cpus/@0");
    321 	if (node != -1 &&
    322 	    OF_getprop(node, "timebase-frequency", &ticks_per_sec,
    323 		       sizeof ticks_per_sec) > 0) {
    324 		goto found;
    325 	}
    326 
    327 	node = OF_finddevice("/");
    328 	for (qhandle = node; qhandle; qhandle = phandle) {
    329 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    330 		    && strcmp(type, "cpu") == 0
    331 		    && OF_getprop(qhandle, "timebase-frequency",
    332 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    333 			goto found;
    334 		}
    335 		if ((phandle = OF_child(qhandle)))
    336 			continue;
    337 		while (qhandle) {
    338 			if ((phandle = OF_peer(qhandle)))
    339 				break;
    340 			qhandle = OF_parent(qhandle);
    341 		}
    342 	}
    343 	panic("no cpu node");
    344 
    345 found:
    346 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    347 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    348 	ns_per_tick = 1000000000 / ticks_per_sec;
    349 	ticks_per_intr = ticks_per_sec / hz;
    350 	cpu_timebase = ticks_per_sec;
    351 
    352 #ifdef PPC_OEA601
    353 	if ((mfpvr() >> 16) == MPC601)
    354 	    curcpu()->ci_lasttb = rtc_nanosecs();
    355 	else
    356 #endif
    357 	curcpu()->ci_lasttb = mftbl();
    358 
    359 	mtspr(SPR_DEC, ticks_per_intr);
    360 	mtmsr(msr);
    361 }
    362 
    363 static int
    364 save_ofmap(struct ofw_translations *map, int maxlen)
    365 {
    366 	int mmui, mmu, len;
    367 
    368 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    369 	mmu = OF_instance_to_package(mmui);
    370 
    371 	if (map) {
    372 		memset(map, 0, maxlen); /* to be safe */
    373 		len = OF_getprop(mmu, "translations", map, maxlen);
    374 	} else
    375 		len = OF_getproplen(mmu, "translations");
    376 
    377 	if (len < 0)
    378 		len = 0;
    379 	return len;
    380 }
    381 
    382 
    383 /* The PMAC_G5 code here needs to be replaced by code that looks for the
    384    size_cells and does the right thing automatically.
    385 */
    386 void
    387 restore_ofmap(struct ofw_translations *map, int len)
    388 {
    389 	int n = len / sizeof(struct ofw_translations);
    390 	int i;
    391 
    392 	pmap_pinit(&ofw_pmap);
    393 
    394 #ifndef _LP64
    395 	ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
    396 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
    397 
    398 #ifdef KERNEL2_SR
    399 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
    400 #endif
    401 #endif
    402 
    403 	for (i = 0; i < n; i++) {
    404 #if defined (PMAC_G5)
    405 		register64_t pa = map[i].pa;
    406 #else
    407 		register_t pa = map[i].pa;
    408 #endif
    409 		vaddr_t va = map[i].va;
    410 		size_t length = map[i].len;
    411 
    412 		if (va < 0xf0000000) /* XXX */
    413 			continue;
    414 
    415 		while (length > 0) {
    416 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    417 			    VM_PROT_ALL|PMAP_WIRED);
    418 			pa += PAGE_SIZE;
    419 			va += PAGE_SIZE;
    420 			length -= PAGE_SIZE;
    421 		}
    422 	}
    423 	pmap_update(&ofw_pmap);
    424 }
    425 
    426 
    427 
    428 /*
    429  * Scan the device tree for ranges, and return them as bitmap 0..15
    430  */
    431 #if !defined(macppc) && defined(PPC_OEA)
    432 static u_int16_t
    433 ranges_bitmap(int node, u_int16_t bitmap)
    434 {
    435 	int child, mlen, acells, scells, reclen, i, j;
    436 	u_int32_t addr, len, map[160];
    437 
    438 	for (child = OF_child(node); child; child = OF_peer(child)) {
    439 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    440 		if (mlen == -1)
    441 			goto noranges;
    442 
    443 		j = OF_getprop(child, "#address-cells", &acells,
    444 		    sizeof(acells));
    445 		if (j == -1)
    446 			goto noranges;
    447 
    448 		j = OF_getprop(child, "#size-cells", &scells,
    449 		    sizeof(scells));
    450 		if (j == -1)
    451 			goto noranges;
    452 
    453 #ifdef ofppc
    454 		reclen = acells + modeldata.ranges_offset + scells;
    455 #else
    456 		reclen = acells + 1 + scells;
    457 #endif
    458 
    459 		for (i=0; i < (mlen/4)/reclen; i++) {
    460 			addr = map[reclen * i + acells];
    461 			len = map[reclen * i + reclen - 1];
    462 			for (j = 0; j < len / 0x10000000; j++)
    463 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    464 			bitmap |= 1 << (addr >> 28);
    465 		}
    466 noranges:
    467 		bitmap |= ranges_bitmap(child, bitmap);
    468 		continue;
    469 	}
    470 	return bitmap;
    471 }
    472 #endif /* !macppc && PPC_OEA */
    473 
    474 void
    475 ofwoea_batinit(void)
    476 {
    477 #if defined (PPC_OEA)
    478 
    479 #ifdef macppc
    480 	/*
    481 	 * cover PCI and register space but not the firmware ROM
    482 	 */
    483 #ifdef PPC_OEA601
    484 
    485         /*
    486 	 * use segment registers for the 601
    487 	 */
    488 	if ((mfpvr() >> 16 ) == MPC601)
    489 	    oea_batinit(
    490 		0x80000000, BAT_BL_256M,
    491 		0x90000000, BAT_BL_256M,
    492 		0xa0000000, BAT_BL_256M,
    493 		0xb0000000, BAT_BL_256M,
    494 		0xf0000000, BAT_BL_256M,
    495 		0);
    496 	else
    497 #endif
    498 	/*
    499 	 * map to bats
    500 	 */
    501 	oea_batinit(0x80000000, BAT_BL_1G,
    502 		    0xf0000000, BAT_BL_128M,
    503 		    0xf8000000, BAT_BL_64M,
    504 		    0xfe000000, BAT_BL_8M,	/* Grackle IO */
    505 		    0);
    506 #else
    507 	uint16_t bitmap;
    508 	int node, i;
    509 
    510 	node = OF_finddevice("/");
    511 
    512 	bitmap = ranges_bitmap(node, 0);
    513 	oea_batinit(0);
    514 
    515 	for (i=1; i < 0x10; i++) {
    516 		/* skip the three vital SR regions */
    517 		if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
    518 			continue;
    519 		if (bitmap & (1 << i)) {
    520 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    521 			DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
    522 		}
    523 	}
    524 #endif
    525 #endif /* OEA */
    526 }
    527 
    528 
    529 /* we define these partially, as we will fill the rest in later */
    530 struct powerpc_bus_space genppc_isa_io_space_tag = {
    531 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    532 	.pbs_base = 0x00000000,
    533 };
    534 
    535 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    536 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    537 	.pbs_base = 0x00000000,
    538 };
    539 
    540 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    541  * Increase if necc.
    542  */
    543 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    544 	__attribute__((aligned(8)));
    545 
    546 
    547 static void
    548 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    549 {
    550 	int node, i, len, reclen;
    551 	u_int32_t parent_acells, acells, scells, map[160];
    552 	char tmp[32];
    553 
    554 	node = base;
    555 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    556 		goto rec;
    557 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    558 	    strcmp("pci", tmp) != 0)
    559 		goto rec;
    560 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    561 		goto rec;
    562 	if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
    563 		len = OF_getprop(node, "reg", map, sizeof(map));
    564 		acells = 1;
    565 		scells = 1;
    566 	} else {
    567 		len = OF_getprop(node, "ranges", map, sizeof(map));
    568 	}
    569 	if (len == -1)
    570 		goto rec;
    571 	if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
    572 	    sizeof(parent_acells)) != sizeof(parent_acells))
    573 		parent_acells = 1;
    574 	if (OF_getprop(node, "#address-cells", &acells,
    575 	    sizeof(acells)) != sizeof(acells))
    576 		acells = 3;
    577 	if (OF_getprop(node, "#size-cells", &scells,
    578 	    sizeof(scells)) != sizeof(scells))
    579 		scells = 2;
    580 #ifdef ofppc
    581 	if (modeldata.ranges_offset == 0)
    582 		scells -= 1;
    583 #endif
    584 	if (type == RANGE_TYPE_ISA)
    585 		reclen = 6;
    586 	else
    587 		reclen = parent_acells + acells + scells;
    588 	/*
    589 	 * There exist ISA buses with empty ranges properties.  This is
    590 	 * known to occur on the Pegasos II machine, and likely others.
    591 	 * According to them, that means that the isa bus is a fake bus, and
    592 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    593 	 * with this, we will set cur to -1 and return.
    594 	 */
    595 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    596 		*cur = -1;
    597 		DPRINTF("Found empty range in isa bus\n");
    598 		return;
    599 	}
    600 
    601 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    602 	switch (type) {
    603 		case RANGE_TYPE_PCI:
    604 		case RANGE_TYPE_FIRSTPCI:
    605 			for (i=0; i < len/(4*reclen); i++) {
    606 				DPRINTF("FOUND PCI RANGE\n");
    607 				regions[*cur].size =
    608 				    map[i*reclen + parent_acells + acells + scells - 1];
    609 				/* skip ranges of size==0 */
    610 				if (regions[*cur].size == 0)
    611 					continue;
    612 				regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
    613 				regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
    614 				(*cur)++;
    615 			}
    616 			break;
    617 		case RANGE_TYPE_ISA:
    618 			for (i=0; i < len/(4*reclen); i++) {
    619 				if (map[i*reclen] == 1)
    620 					regions[*cur].type = RANGE_IO;
    621 				else
    622 					regions[*cur].type = RANGE_MEM;
    623 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    624 					regions[*cur].type);
    625 				regions[*cur].size =
    626 				    map[i*reclen + acells + scells];
    627 				(*cur)++;
    628 			}
    629 			break;
    630 		case RANGE_TYPE_MACIO:
    631 			regions[*cur].type = RANGE_MEM;
    632 			if (len == 8) {
    633 				regions[*cur].size = map[1];
    634 				regions[*cur].addr = map[0];
    635 			} else {
    636 				regions[*cur].size = map[2];
    637 				regions[*cur].addr = map[1];
    638 			}
    639 			(*cur)++;
    640 			break;
    641 	}
    642 	DPRINTF("returning with CUR=%d\n", *cur);
    643 	return;
    644 rec:
    645 	for (node = OF_child(base); node; node = OF_peer(node)) {
    646 		DPRINTF("RECURSE 1 STEP\n");
    647 		find_ranges(node, regions, cur, type);
    648 		if (*cur == -1)
    649 			return;
    650 	}
    651 }
    652 
    653 static int
    654 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    655 {
    656 	int i, low = 0;
    657 	u_int32_t addr = 0xffffffff;
    658 
    659 	for (i=0; i < nrof; i++) {
    660 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    661 		    ranges[i].addr < addr) {
    662 			low = i;
    663 			addr = ranges[i].addr;
    664 		}
    665 	}
    666 	if (addr == 0xffffffff)
    667 		return -1;
    668 	return low;
    669 }
    670 
    671 /*
    672  * Find a region of memory, and create a bus_space_tag for it.
    673  * Notes:
    674  * For ISA node is ignored.
    675  * node is the starting node.  if -1, we start at / and map everything.
    676  */
    677 
    678 int
    679 ofwoea_map_space(int rangetype, int iomem, int node,
    680     struct powerpc_bus_space *tag, const char *name)
    681 {
    682 	int i, cur, range, nrofholes, error;
    683 	static int exmap=0;
    684 	rangemap_t region, holes[32], list[32];
    685 
    686 	memset(list, 0, sizeof(list));
    687 	memset(&region, 0, sizeof(region));
    688 	cur = 0;
    689 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    690 		node = OF_finddevice("/");
    691 	if (rangetype == RANGE_TYPE_ISA) {
    692 		u_int32_t size = 0;
    693 		rangemap_t regions[32];
    694 
    695 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    696 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    697 		range = 0;
    698 		DPRINTF("LOOKING FOR ISA\n");
    699 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    700 		if (range == 0 || cur == 0)
    701 			return -1; /* no isa stuff found */
    702 		/*
    703 		 * This may be confusing to some.  The ISA ranges property
    704 		 * is supposed to be a set of IO ranges for the ISA bus, but
    705 		 * generally, it's just a set of pci devfunc lists that tell
    706 		 * you to go look at the parent PCI device for the actual
    707 		 * ranges.
    708 		 */
    709 		if (range == -1) {
    710 			/* we found a rangeless isa bus */
    711 			if (iomem == RANGE_IO)
    712 				size = 0x10000;
    713 			else
    714 				size = 0x1000000;
    715 		}
    716 		DPRINTF("found isa stuff\n");
    717 		for (i=0; i < range; i++)
    718 			if (regions[i].type == iomem)
    719 				size = regions[i].size;
    720 		if (iomem == RANGE_IO) {
    721 			/* the first io range is the one */
    722 			for (i=0; i < cur; i++)
    723 				if (list[i].type == RANGE_IO && size) {
    724 					DPRINTF("found IO\n");
    725 					tag->pbs_offset = list[i].addr;
    726 					tag->pbs_limit = size;
    727 					error = bus_space_init(tag, name,
    728 					    ex_storage[exmap],
    729 					    sizeof(ex_storage[exmap]));
    730 					exmap++;
    731 					return error;
    732 				}
    733 		} else {
    734 			for (i=0; i < cur; i++)
    735 				if (list[i].type == RANGE_MEM &&
    736 				    list[i].size == size) {
    737 					DPRINTF("found mem\n");
    738 					tag->pbs_offset = list[i].addr;
    739 					tag->pbs_limit = size;
    740 					error = bus_space_init(tag, name,
    741 					    ex_storage[exmap],
    742 					    sizeof(ex_storage[exmap]));
    743 					exmap++;
    744 					return error;
    745 				}
    746 		}
    747 		return -1; /* NO ISA FOUND */
    748 	}
    749 	find_ranges(node, list, &cur, rangetype);
    750 
    751 	DPRINTF("cur == %d\n", cur);
    752 	/* now list should contain a list of memory regions */
    753 	for (i=0; i < cur; i++)
    754 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    755 		    list[i].size, list[i].type);
    756 
    757 	range = find_lowest_range(list, cur, iomem);
    758 	i = 0;
    759 	nrofholes = 0;
    760 	while (range != -1) {
    761 		DPRINTF("range==%d\n", range);
    762 		DPRINTF("i==%d\n", i);
    763 		if (i == 0) {
    764 			memcpy(&region, &list[range], sizeof(rangemap_t));
    765 			list[range].addr = 0;
    766 			i++;
    767 			range = find_lowest_range(list, cur, iomem);
    768 			continue;
    769 		}
    770 		if (region.addr + region.size < list[range].addr) {
    771 			/* allocate a hole */
    772 			holes[nrofholes].type = iomem;
    773 			holes[nrofholes].addr = region.size + region.addr;
    774 			holes[nrofholes].size = list[range].addr -
    775 			    holes[nrofholes].addr - 1;
    776 			nrofholes++;
    777 		}
    778 		region.size = list[range].size + list[range].addr -
    779 		    region.addr;
    780 		list[range].addr = 0;
    781 		range = find_lowest_range(list, cur, iomem);
    782 	}
    783 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    784 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    785 		    region.size, region.type);
    786 	DPRINTF("HOLES FOUND\n");
    787 	for (i=0; i < nrofholes; i++)
    788 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    789 		    holes[i].size, holes[i].type);
    790 	/* AT THIS POINT WE MAP IT */
    791 
    792 	if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
    793 		if (exmap == EXSTORAGE_MAX)
    794 			panic("Not enough ex_storage space. "
    795 			    "Increase EXSTORAGE_MAX");
    796 
    797 		/* XXX doing this in here might be wrong */
    798 		if (iomem == 1) {
    799 			/* we map an IO region */
    800 			tag->pbs_offset = region.addr;
    801 			tag->pbs_base = 0;
    802 			tag->pbs_limit = region.size;
    803 		} else {
    804 			/* ... or a memory region */
    805 			tag->pbs_offset = 0;
    806 			tag->pbs_base = region.addr;
    807 			tag->pbs_limit = region.size + region.addr;
    808 		}
    809 
    810 		error = bus_space_init(tag, name, ex_storage[exmap],
    811 		    sizeof(ex_storage[exmap]));
    812 		exmap++;
    813 		if (error)
    814 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    815 		for (i=0; i < nrofholes; i++) {
    816 			if (holes[i].type == RANGE_IO) {
    817 				error = extent_alloc_region(tag->pbs_extent,
    818 				    holes[i].addr - tag->pbs_offset,
    819 				    holes[i].size, EX_NOWAIT);
    820 			} else {
    821 				error = extent_alloc_region(tag->pbs_extent,
    822 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    823 			}
    824 			if (error)
    825 				panic("ofwoea_bus_space_init: can't block out"
    826 				    " reserved space 0x%x-0x%x: error=%d",
    827 				    holes[i].addr, holes[i].addr+holes[i].size,
    828 				    error);
    829 		}
    830 		return error;
    831 	}
    832 	return -1;
    833 }
    834 
    835 void
    836 ofwoea_bus_space_init(void)
    837 {
    838 	int error;
    839 
    840 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    841 	    &genppc_isa_io_space_tag, "isa-ioport");
    842 	if (error > 0)
    843 		panic("Could not map ISA IO");
    844 
    845 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    846 	    &genppc_isa_mem_space_tag, "isa-iomem");
    847 	if (error > 0)
    848 		panic("Could not map ISA MEM");
    849 }
    850