Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.13
      1 /* $NetBSD: ofwoea_machdep.c,v 1.13 2008/04/08 02:33:03 garbled 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.13 2008/04/08 02:33:03 garbled Exp $");
     41 
     42 #include "opt_ppcarch.h"
     43 #include "opt_compat_netbsd.h"
     44 #include "opt_ddb.h"
     45 #include "opt_kgdb.h"
     46 #include "opt_ipkdb.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/buf.h>
     50 #include <sys/boot_flag.h>
     51 #include <sys/extent.h>
     52 #include <sys/kernel.h>
     53 #include <sys/ksyms.h>
     54 #include <uvm/uvm_extern.h>
     55 
     56 #include <dev/ofw/openfirm.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 <powerpc/bus.h>
     63 #include <powerpc/oea/bat.h>
     64 #include <powerpc/oea/cpufeat.h>
     65 #include <powerpc/ofw_bus.h>
     66 #include <powerpc/ofw_cons.h>
     67 #include <powerpc/spr.h>
     68 #include <arch/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(LKM)
    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 ofmsr;
    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 volatile u_int cpu_spinstart_ack, cpu_spinstart_cpunum;
    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(LKM)
    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 >= 0) {
    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 	ofwoea_consinit();
    178 
    179 #if defined(MULTIPROCESSOR) && defined(ofppc)
    180 	for (i=1; i < CPU_MAXNUM; i++) {
    181 		sprintf(cpupath, "/cpus/@%x", i);
    182 		node = OF_finddevice(cpupath);
    183 		if (node <= 0)
    184 			continue;
    185 		aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
    186 		OF_start_cpu(node, (u_int)cpu_spinstart, i);
    187 		for (l=0; l < 100000000; l++) {
    188 			if (cpu_spinstart_ack == i) {
    189 				aprint_verbose("CPU %d spun up.\n", i);
    190 				break;
    191 			}
    192 			__asm volatile ("sync");
    193 		}
    194 	}
    195 #endif
    196 
    197 	oea_init(pic_ext_intr);
    198 
    199 	ofmaplen = save_ofmap(NULL, 0);
    200 	if (ofmaplen > 0)
    201 		save_ofmap(ofmap, ofmaplen);
    202 
    203 	ofmsr &= ~PSL_IP;
    204 
    205 	/* Parse the args string */
    206 	if (args) {
    207 		strcpy(bootpath, args);
    208 		args = bootpath;
    209 		while (*++args && *args != ' ');
    210 		if (*args) {
    211 			*args++ = 0;
    212 			while (*args)
    213 				BOOT_FLAG(*args++, boothowto);
    214 		}
    215 	}
    216 
    217 	uvm_setpagesize();
    218 
    219 #if defined (PPC_OEA64_BRIDGE) && defined (PPC_OEA)
    220 	if (oeacpufeat & OEACPU_64_BRIDGE)
    221 		pmap_setup64bridge();
    222 	else
    223 		pmap_setup32();
    224 #endif
    225 	pmap_bootstrap(startkernel, endkernel);
    226 
    227 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    228 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    229 #if defined (PMAC_G5)
    230 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    231 	if (oeacpufeat & OEACPU_64_BRIDGE)
    232 		pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000,
    233 		    0x0);
    234 #elif defined (MAMBO)
    235 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    236 	if (oeacpufeat & OEACPU_64_BRIDGE)
    237 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    238 #endif /* PMAC_G5 */
    239 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    240 
    241 	/* Now enable translation (and machine checks/recoverable interrupts) */
    242 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    243 	    : "=r"(scratch)
    244 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    245 
    246 	restore_ofmap(ofmap, ofmaplen);
    247 
    248 #if NKSYMS || defined(DDB) || defined(LKM)
    249 	ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    250 #endif
    251 
    252 	/* CPU clock stuff */
    253 	set_timebase();
    254 }
    255 
    256 void
    257 set_timebase(void)
    258 {
    259 	int qhandle, phandle, msr, scratch;
    260 	char type[32];
    261 
    262 	if (timebase_freq != 0) {
    263 		ticks_per_sec = timebase_freq;
    264 		goto found;
    265 	}
    266 
    267 	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    268 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    269 		    && strcmp(type, "cpu") == 0
    270 		    && OF_getprop(qhandle, "timebase-frequency",
    271 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    272 			goto found;
    273 		}
    274 		if ((phandle = OF_child(qhandle)))
    275 			continue;
    276 		while (qhandle) {
    277 			if ((phandle = OF_peer(qhandle)))
    278 				break;
    279 			qhandle = OF_parent(qhandle);
    280 		}
    281 	}
    282 	panic("no cpu node");
    283 
    284 found:
    285 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    286 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    287 	ns_per_tick = 1000000000 / ticks_per_sec;
    288 	ticks_per_intr = ticks_per_sec / hz;
    289 	cpu_timebase = ticks_per_sec;
    290 	curcpu()->ci_lasttb = mftbl();
    291 	mtspr(SPR_DEC, ticks_per_intr);
    292 	mtmsr(msr);
    293 }
    294 
    295 static int
    296 save_ofmap(struct ofw_translations *map, int maxlen)
    297 {
    298 	int mmui, mmu, len;
    299 
    300 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    301 	mmu = OF_instance_to_package(mmui);
    302 
    303 	if (map) {
    304 		memset(map, 0, maxlen); /* to be safe */
    305 		len = OF_getprop(mmu, "translations", map, maxlen);
    306 	} else
    307 		len = OF_getproplen(mmu, "translations");
    308 
    309 	if (len < 0)
    310 		len = 0;
    311 	return len;
    312 }
    313 
    314 
    315 /* The PMAC_G5 code here needs to be replaced by code that looks for the
    316    size_cells and does the right thing automatically.
    317 */
    318 void
    319 restore_ofmap(struct ofw_translations *map, int len)
    320 {
    321 	int n = len / sizeof(struct ofw_translations);
    322 	int i;
    323 
    324 	pmap_pinit(&ofw_pmap);
    325 
    326 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
    327 
    328 #ifdef KERNEL2_SR
    329 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
    330 #endif
    331 
    332 	for (i = 0; i < n; i++) {
    333 #if defined (PMAC_G5)
    334 		register64_t pa = map[i].pa;
    335 #else
    336 		register_t pa = map[i].pa;
    337 #endif
    338 		vaddr_t va = map[i].va;
    339 		size_t length = map[i].len;
    340 
    341 		if (va < 0xf0000000) /* XXX */
    342 			continue;
    343 
    344 		while (length > 0) {
    345 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    346 			    VM_PROT_ALL|PMAP_WIRED);
    347 			pa += PAGE_SIZE;
    348 			va += PAGE_SIZE;
    349 			length -= PAGE_SIZE;
    350 		}
    351 	}
    352 	pmap_update(&ofw_pmap);
    353 }
    354 
    355 
    356 
    357 /*
    358  * Scan the device tree for ranges, and batmap them.
    359  */
    360 
    361 static u_int16_t
    362 ranges_bitmap(int node, u_int16_t bitmap)
    363 {
    364 	int child, mlen, acells, scells, reclen, i, j;
    365 	u_int32_t addr, len, map[160];
    366 
    367 	for (child = OF_child(node); child; child = OF_peer(child)) {
    368 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    369 		if (mlen == -1)
    370 			goto noranges;
    371 
    372 		j = OF_getprop(child, "#address-cells", &acells,
    373 		    sizeof(acells));
    374 		if (j == -1)
    375 			goto noranges;
    376 
    377 		j = OF_getprop(child, "#size-cells", &scells,
    378 		    sizeof(scells));
    379 		if (j == -1)
    380 			goto noranges;
    381 
    382 		reclen = acells + 1 + scells;
    383 
    384 		for (i=0; i < (mlen/4)/reclen; i++) {
    385 			addr = map[reclen * i + acells];
    386 			len = map[reclen * i + reclen - 1];
    387 			for (j = 0; j < len / 0x10000000; j++)
    388 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    389 			bitmap |= 1 << (addr >> 28);
    390 		}
    391 noranges:
    392 		bitmap |= ranges_bitmap(child, bitmap);
    393 		continue;
    394 	}
    395 	return bitmap;
    396 }
    397 
    398 void
    399 ofwoea_batinit(void)
    400 {
    401 #if defined (PPC_OEA)
    402         u_int16_t bitmap;
    403 	int node, i;
    404 
    405 	node = OF_finddevice("/");
    406 	bitmap = ranges_bitmap(node, 0);
    407 	oea_batinit(0);
    408 
    409 #ifdef macppc
    410 	/* XXX this is a macppc-specific hack */
    411 	bitmap = 0x8f00;
    412 #endif
    413 	for (i=1; i < 0x10; i++) {
    414 		/* skip the three vital SR regions */
    415 		if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
    416 			continue;
    417 		if (bitmap & (1 << i)) {
    418 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    419 			DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
    420 		}
    421 	}
    422 #endif /* OEA */
    423 }
    424 
    425 
    426 /* we define these partially, as we will fill the rest in later */
    427 struct powerpc_bus_space genppc_isa_io_space_tag = {
    428 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    429 	.pbs_base = 0x00000000,
    430 };
    431 
    432 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    433 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    434 	.pbs_base = 0x00000000,
    435 };
    436 
    437 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    438  * Increase if necc.
    439  */
    440 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    441 	__attribute__((aligned(8)));
    442 
    443 
    444 static void
    445 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    446 {
    447 	int node, i, len, reclen;
    448 	u_int32_t acells, scells, map[160];
    449 	char tmp[32];
    450 
    451 	node = base;
    452 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    453 		goto rec;
    454 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    455 	    strcmp("pci", tmp) != 0)
    456 		goto rec;
    457 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    458 		goto rec;
    459 	len = OF_getprop(node, "ranges", map, sizeof(map));
    460 	if (len == -1)
    461 		goto rec;
    462 	if (OF_getprop(node, "#address-cells", &acells,
    463 	    sizeof(acells)) != sizeof(acells))
    464 		acells = 1;
    465 	if (OF_getprop(node, "#size-cells", &scells,
    466 	    sizeof(scells)) != sizeof(scells))
    467 		scells = 1;
    468 #ifdef ofppc
    469 	if (modeldata.ranges_offset == 0)
    470 		scells -= 1;
    471 #endif
    472 	if (type == RANGE_TYPE_ISA)
    473 		reclen = 6;
    474 	else
    475 		reclen = acells + scells + 1;
    476 	/*
    477 	 * There exist ISA buses with empty ranges properties.  This is
    478 	 * known to occur on the Pegasos II machine, and likely others.
    479 	 * According to them, that means that the isa bus is a fake bus, and
    480 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    481 	 * with this, we will set cur to -1 and return.
    482 	 */
    483 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    484 		*cur = -1;
    485 		DPRINTF("Found empty range in isa bus\n");
    486 		return;
    487 	}
    488 
    489 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    490 	switch (type) {
    491 		case RANGE_TYPE_PCI:
    492 		case RANGE_TYPE_FIRSTPCI:
    493 			for (i=0; i < len/(4*reclen); i++) {
    494 				DPRINTF("FOUND PCI RANGE\n");
    495 				regions[*cur].size =
    496 				    map[i*reclen + acells + scells];
    497 				/* skip ranges of size==0 */
    498 				if (regions[*cur].size == 0)
    499 					continue;
    500 				regions[*cur].type = map[i*reclen] >> 24;
    501 				regions[*cur].addr = map[i*reclen + acells];
    502 				(*cur)++;
    503 			}
    504 			break;
    505 		case RANGE_TYPE_ISA:
    506 			for (i=0; i < len/(4*reclen); i++) {
    507 				if (map[i*reclen] == 1)
    508 					regions[*cur].type = RANGE_IO;
    509 				else
    510 					regions[*cur].type = RANGE_MEM;
    511 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    512 					regions[*cur].type);
    513 				regions[*cur].size =
    514 				    map[i*reclen + acells + scells];
    515 				(*cur)++;
    516 			}
    517 			break;
    518 	}
    519 	DPRINTF("returning with CUR=%d\n", *cur);
    520 	return;
    521 rec:
    522 	for (node = OF_child(base); node; node = OF_peer(node)) {
    523 		DPRINTF("RECURSE 1 STEP\n");
    524 		find_ranges(node, regions, cur, type);
    525 		if (*cur == -1)
    526 			return;
    527 	}
    528 }
    529 
    530 static int
    531 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    532 {
    533 	int i, low = 0;
    534 	u_int32_t addr = 0xffffffff;
    535 
    536 	for (i=0; i < nrof; i++) {
    537 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    538 		    ranges[i].addr < addr) {
    539 			low = i;
    540 			addr = ranges[i].addr;
    541 		}
    542 	}
    543 	if (addr == 0xffffffff)
    544 		return -1;
    545 	return low;
    546 }
    547 
    548 /*
    549  * Find a region of memory, and create a bus_space_tag for it.
    550  * Notes:
    551  * For ISA node is ignored.
    552  * node is the starting node.  if -1, we start at / and map everything.
    553  */
    554 
    555 int
    556 ofwoea_map_space(int rangetype, int iomem, int node,
    557     struct powerpc_bus_space *tag, const char *name)
    558 {
    559 	int i, cur, range, nrofholes, error;
    560 	static int exmap=0;
    561 	u_int32_t addr;
    562 	rangemap_t region, holes[32], list[32];
    563 
    564 	memset(list, 0, sizeof(list));
    565 	cur = 0;
    566 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    567 		node = OF_finddevice("/");
    568 	if (rangetype == RANGE_TYPE_ISA) {
    569 		u_int32_t size = 0;
    570 		rangemap_t regions[32];
    571 
    572 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    573 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    574 		range = 0;
    575 		DPRINTF("LOOKING FOR ISA\n");
    576 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    577 		if (range == 0 || cur == 0)
    578 			return -1; /* no isa stuff found */
    579 		/*
    580 		 * This may be confusing to some.  The ISA ranges property
    581 		 * is supposed to be a set of IO ranges for the ISA bus, but
    582 		 * generally, it's just a set of pci devfunc lists that tell
    583 		 * you to go look at the parent PCI device for the actual
    584 		 * ranges.
    585 		 */
    586 		if (range == -1) {
    587 			/* we found a rangeless isa bus */
    588 		}
    589 		DPRINTF("found isa stuff\n");
    590 		for (i=0; i < range; i++)
    591 			if (regions[i].type == iomem)
    592 				size = regions[i].size;
    593 		if (iomem == RANGE_IO) {
    594 			/* the first io range is the one */
    595 			for (i=0; i < cur; i++)
    596 				if (list[i].type == RANGE_IO && size) {
    597 					DPRINTF("found IO\n");
    598 					tag->pbs_offset = list[i].addr;
    599 					tag->pbs_limit = size;
    600 					error = bus_space_init(tag, name, NULL, 0);
    601 					return error;
    602 				}
    603 		} else {
    604 			for (i=0; i < cur; i++)
    605 				if (list[i].type == RANGE_MEM &&
    606 				    list[i].size == size) {
    607 					DPRINTF("found mem\n");
    608 					tag->pbs_offset = list[i].addr;
    609 					tag->pbs_limit = size;
    610 					error = bus_space_init(tag, name, NULL, 0);
    611 					return error;
    612 				}
    613 		}
    614 		return -1; /* NO ISA FOUND */
    615 	}
    616 	find_ranges(node, list, &cur, rangetype);
    617 
    618 	DPRINTF("cur == %d\n", cur);
    619 	/* now list should contain a list of memory regions */
    620 	for (i=0; i < cur; i++)
    621 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    622 		    list[i].size, list[i].type);
    623 
    624 	addr=0;
    625 	range = find_lowest_range(list, cur, iomem);
    626 	i = 0;
    627 	nrofholes = 0;
    628 	while (range != -1) {
    629 		DPRINTF("range==%d\n", range);
    630 		DPRINTF("i==%d\n", i);
    631 		if (i == 0) {
    632 			memcpy(&region, &list[range], sizeof(rangemap_t));
    633 			list[range].addr = 0;
    634 			i++;
    635 			range = find_lowest_range(list, cur, iomem);
    636 			continue;
    637 		}
    638 		if (region.addr + region.size < list[range].addr) {
    639 			/* allocate a hole */
    640 			holes[nrofholes].type = iomem;
    641 			holes[nrofholes].addr = region.size + region.addr;
    642 			holes[nrofholes].size = list[range].addr -
    643 			    holes[nrofholes].addr - 1;
    644 			nrofholes++;
    645 		}
    646 		region.size = list[range].size + list[range].addr -
    647 		    region.addr;
    648 		list[range].addr = 0;
    649 		range = find_lowest_range(list, cur, iomem);
    650 	}
    651 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    652 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    653 		    region.size, region.type);
    654 	DPRINTF("HOLES FOUND\n");
    655 	for (i=0; i < nrofholes; i++)
    656 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    657 		    holes[i].size, holes[i].type);
    658 	/* AT THIS POINT WE MAP IT */
    659 
    660 	if (rangetype == RANGE_TYPE_PCI) {
    661 		if (exmap == EXSTORAGE_MAX)
    662 			panic("Not enough ex_storage space. "
    663 			    "Increase EXSTORAGE_MAX");
    664 
    665 		/* XXX doing this in here might be wrong */
    666 		if (iomem == 1) {
    667 			/* we map an IO region */
    668 			tag->pbs_offset = region.addr;
    669 			tag->pbs_base = 0;
    670 			tag->pbs_limit = region.size;
    671 		} else {
    672 			/* ... or a memory region */
    673 			tag->pbs_offset = 0;
    674 			tag->pbs_base = region.addr;
    675 			tag->pbs_limit = region.size + region.addr;
    676 		}
    677 
    678 		error = bus_space_init(tag, name, ex_storage[exmap],
    679 		    sizeof(ex_storage[exmap]));
    680 		exmap++;
    681 		if (error)
    682 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    683 		for (i=0; i < nrofholes; i++) {
    684 			if (holes[i].type == RANGE_IO) {
    685 				error = extent_alloc_region(tag->pbs_extent,
    686 				    holes[i].addr - tag->pbs_offset,
    687 				    holes[i].size, EX_NOWAIT);
    688 			} else {
    689 				error = extent_alloc_region(tag->pbs_extent,
    690 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    691 			}
    692 			if (error)
    693 				panic("ofwoea_bus_space_init: can't block out"
    694 				    " reserved space 0x%x-0x%x: error=%d",
    695 				    holes[i].addr, holes[i].addr+holes[i].size,
    696 				    error);
    697 		}
    698 		return error;
    699 	}
    700 	return -1;
    701 }
    702 
    703 void
    704 ofwoea_bus_space_init(void)
    705 {
    706 	int error;
    707 
    708 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    709 	    &genppc_isa_io_space_tag, "isa-ioport");
    710 	if (error > 0)
    711 		panic("Could not map ISA IO");
    712 
    713 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    714 	    &genppc_isa_mem_space_tag, "isa-iomem");
    715 	if (error > 0)
    716 		panic("Could not map ISA MEM");
    717 }
    718