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