Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.12
      1 /* $NetBSD: ofwoea_machdep.c,v 1.12 2008/02/14 19:41:54 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.12 2008/02/14 19:41:54 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 void
    140 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    141 {
    142 	int ofmaplen, node, l;
    143 	register_t scratch;
    144 
    145 	/* initialze bats */
    146 	if ((oeacpufeat & OEACPU_NOBAT) == 0)
    147 		ofwoea_batinit();
    148 
    149 #if NKSYMS || defined(DDB) || defined(LKM)
    150 	/* get info of kernel symbol table from bootloader */
    151 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    152 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    153 	    sizeof(endsym));
    154 	if (startsym == NULL || endsym == NULL)
    155 	    startsym = endsym = NULL;
    156 #endif
    157 
    158 	/* get model name and perform model-specific actions */
    159 	memset(model_name, 0, sizeof(model_name));
    160 	node = OF_finddevice("/");
    161 	if (node >= 0) {
    162 		l = OF_getprop(node, "model", model_name, sizeof(model_name));
    163 		if (l == -1)
    164 			OF_getprop(node, "name", model_name,
    165 			    sizeof(model_name));
    166 		model_init();
    167 	}
    168 
    169 	ofwoea_consinit();
    170 
    171 	oea_init(pic_ext_intr);
    172 
    173 	ofmaplen = save_ofmap(NULL, 0);
    174 	if (ofmaplen > 0)
    175 		save_ofmap(ofmap, ofmaplen);
    176 
    177 	ofmsr &= ~PSL_IP;
    178 
    179 	/* Parse the args string */
    180 	if (args) {
    181 		strcpy(bootpath, args);
    182 		args = bootpath;
    183 		while (*++args && *args != ' ');
    184 		if (*args) {
    185 			*args++ = 0;
    186 			while (*args)
    187 				BOOT_FLAG(*args++, boothowto);
    188 		}
    189 	}
    190 
    191 	uvm_setpagesize();
    192 
    193 #if defined (PPC_OEA64_BRIDGE) && defined (PPC_OEA)
    194 	if (oeacpufeat & OEACPU_64_BRIDGE)
    195 		pmap_setup64bridge();
    196 	else
    197 		pmap_setup32();
    198 #endif
    199 	pmap_bootstrap(startkernel, endkernel);
    200 
    201 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    202 #if defined (PMAC_G5)
    203 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    204 	if (oeacpufeat & OEACPU_64_BRIDGE)
    205 		pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000,
    206 		    0x0);
    207 #elif defined (MAMBO)
    208 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    209 	if (oeacpufeat & OEACPU_64_BRIDGE)
    210 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    211 #endif /* PMAC_G5 */
    212 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    213 
    214 	/* Now enable translation (and machine checks/recoverable interrupts) */
    215 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    216 	    : "=r"(scratch)
    217 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    218 
    219 	restore_ofmap(ofmap, ofmaplen);
    220 
    221 #if NKSYMS || defined(DDB) || defined(LKM)
    222 	ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    223 #endif
    224 
    225 	/* CPU clock stuff */
    226 	set_timebase();
    227 }
    228 
    229 void
    230 set_timebase(void)
    231 {
    232 	int qhandle, phandle, msr, scratch;
    233 	char type[32];
    234 
    235 	if (timebase_freq != 0) {
    236 		ticks_per_sec = timebase_freq;
    237 		goto found;
    238 	}
    239 
    240 	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    241 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    242 		    && strcmp(type, "cpu") == 0
    243 		    && OF_getprop(qhandle, "timebase-frequency",
    244 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    245 			goto found;
    246 		}
    247 		if ((phandle = OF_child(qhandle)))
    248 			continue;
    249 		while (qhandle) {
    250 			if ((phandle = OF_peer(qhandle)))
    251 				break;
    252 			qhandle = OF_parent(qhandle);
    253 		}
    254 	}
    255 	panic("no cpu node");
    256 
    257 found:
    258 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    259 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    260 	ns_per_tick = 1000000000 / ticks_per_sec;
    261 	ticks_per_intr = ticks_per_sec / hz;
    262 	cpu_timebase = ticks_per_sec;
    263 	curcpu()->ci_lasttb = mftbl();
    264 	mtspr(SPR_DEC, ticks_per_intr);
    265 	mtmsr(msr);
    266 }
    267 
    268 static int
    269 save_ofmap(struct ofw_translations *map, int maxlen)
    270 {
    271 	int mmui, mmu, len;
    272 
    273 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    274 	mmu = OF_instance_to_package(mmui);
    275 
    276 	if (map) {
    277 		memset(map, 0, maxlen); /* to be safe */
    278 		len = OF_getprop(mmu, "translations", map, maxlen);
    279 	} else
    280 		len = OF_getproplen(mmu, "translations");
    281 
    282 	if (len < 0)
    283 		len = 0;
    284 	return len;
    285 }
    286 
    287 void
    288 restore_ofmap(struct ofw_translations *map, int len)
    289 {
    290 	int n = len / sizeof(struct ofw_translations);
    291 	int i;
    292 
    293 	pmap_pinit(&ofw_pmap);
    294 
    295 #if defined(PPC_OEA64_BRIDGE)
    296 	if (oeacpufeat & OEACPU_64_BRIDGE)
    297 		ofw_pmap.pm_sr[0x0] = KERNELN_SEGMENT(0);
    298 #endif
    299 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
    300 
    301 #ifdef KERNEL2_SR
    302 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
    303 #endif
    304 
    305 	for (i = 0; i < n; i++) {
    306 #if defined (PMAC_G5)
    307 		register64_t pa = map[i].pa;
    308 #else
    309 		register_t pa = map[i].pa;
    310 #endif
    311 		vaddr_t va = map[i].va;
    312 		size_t length = map[i].len;
    313 
    314 		if (va < 0xf0000000) /* XXX */
    315 			continue;
    316 
    317 		while (length > 0) {
    318 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    319 			    VM_PROT_ALL|PMAP_WIRED);
    320 			pa += PAGE_SIZE;
    321 			va += PAGE_SIZE;
    322 			length -= PAGE_SIZE;
    323 		}
    324 	}
    325 	pmap_update(&ofw_pmap);
    326 }
    327 
    328 
    329 
    330 /*
    331  * Scan the device tree for ranges, and batmap them.
    332  */
    333 
    334 static u_int16_t
    335 ranges_bitmap(int node, u_int16_t bitmap)
    336 {
    337 	int child, mlen, acells, scells, reclen, i, j;
    338 	u_int32_t addr, len, map[160];
    339 
    340 	for (child = OF_child(node); child; child = OF_peer(child)) {
    341 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    342 		if (mlen == -1)
    343 			goto noranges;
    344 
    345 		j = OF_getprop(child, "#address-cells", &acells,
    346 		    sizeof(acells));
    347 		if (j == -1)
    348 			goto noranges;
    349 
    350 		j = OF_getprop(child, "#size-cells", &scells,
    351 		    sizeof(scells));
    352 		if (j == -1)
    353 			goto noranges;
    354 
    355 		reclen = acells + 1 + scells;
    356 
    357 		for (i=0; i < (mlen/4)/reclen; i++) {
    358 			addr = map[reclen * i + acells];
    359 			len = map[reclen * i + reclen - 1];
    360 			for (j = 0; j < len / 0x10000000; j++)
    361 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    362 			bitmap |= 1 << (addr >> 28);
    363 		}
    364 noranges:
    365 		bitmap |= ranges_bitmap(child, bitmap);
    366 		continue;
    367 	}
    368 	return bitmap;
    369 }
    370 
    371 void
    372 ofwoea_batinit(void)
    373 {
    374 #if defined (PPC_OEA)
    375         u_int16_t bitmap;
    376 	int node, i;
    377 
    378 	node = OF_finddevice("/");
    379 	bitmap = ranges_bitmap(node, 0);
    380 	oea_batinit(0);
    381 
    382 #ifdef macppc
    383 	/* XXX this is a macppc-specific hack */
    384 	bitmap = 0x8f00;
    385 #endif
    386 	for (i=1; i < 0x10; i++) {
    387 		/* skip the three vital SR regions */
    388 		if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
    389 			continue;
    390 		if (bitmap & (1 << i)) {
    391 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    392 			DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
    393 		}
    394 	}
    395 #endif /* OEA */
    396 }
    397 
    398 
    399 /* we define these partially, as we will fill the rest in later */
    400 struct powerpc_bus_space genppc_isa_io_space_tag = {
    401 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    402 	.pbs_base = 0x00000000,
    403 };
    404 
    405 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    406 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    407 	.pbs_base = 0x00000000,
    408 };
    409 
    410 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    411  * Increase if necc.
    412  */
    413 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    414 	__attribute__((aligned(8)));
    415 
    416 
    417 static void
    418 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    419 {
    420 	int node, i, len, reclen;
    421 	u_int32_t acells, scells, map[160];
    422 	char tmp[32];
    423 
    424 	node = base;
    425 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    426 		goto rec;
    427 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    428 	    strcmp("pci", tmp) != 0)
    429 		goto rec;
    430 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    431 		goto rec;
    432 	len = OF_getprop(node, "ranges", map, sizeof(map));
    433 	if (len == -1)
    434 		goto rec;
    435 	if (OF_getprop(node, "#address-cells", &acells,
    436 	    sizeof(acells)) != sizeof(acells))
    437 		acells = 1;
    438 	if (OF_getprop(node, "#size-cells", &scells,
    439 	    sizeof(scells)) != sizeof(scells))
    440 		scells = 1;
    441 #ifdef ofppc
    442 	if (modeldata.ranges_offset == 0)
    443 		scells -= 1;
    444 #endif
    445 	if (type == RANGE_TYPE_ISA)
    446 		reclen = 6;
    447 	else
    448 		reclen = acells + scells + 1;
    449 	/*
    450 	 * There exist ISA buses with empty ranges properties.  This is
    451 	 * known to occur on the Pegasos II machine, and likely others.
    452 	 * According to them, that means that the isa bus is a fake bus, and
    453 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    454 	 * with this, we will set cur to -1 and return.
    455 	 */
    456 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    457 		*cur = -1;
    458 		DPRINTF("Found empty range in isa bus\n");
    459 		return;
    460 	}
    461 
    462 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    463 	switch (type) {
    464 		case RANGE_TYPE_PCI:
    465 		case RANGE_TYPE_FIRSTPCI:
    466 			for (i=0; i < len/(4*reclen); i++) {
    467 				DPRINTF("FOUND PCI RANGE\n");
    468 				regions[*cur].size =
    469 				    map[i*reclen + acells + scells];
    470 				/* skip ranges of size==0 */
    471 				if (regions[*cur].size == 0)
    472 					continue;
    473 				regions[*cur].type = map[i*reclen] >> 24;
    474 				regions[*cur].addr = map[i*reclen + acells];
    475 				(*cur)++;
    476 			}
    477 			break;
    478 		case RANGE_TYPE_ISA:
    479 			for (i=0; i < len/(4*reclen); i++) {
    480 				if (map[i*reclen] == 1)
    481 					regions[*cur].type = RANGE_IO;
    482 				else
    483 					regions[*cur].type = RANGE_MEM;
    484 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    485 					regions[*cur].type);
    486 				regions[*cur].size =
    487 				    map[i*reclen + acells + scells];
    488 				(*cur)++;
    489 			}
    490 			break;
    491 	}
    492 	DPRINTF("returning with CUR=%d\n", *cur);
    493 	return;
    494 rec:
    495 	for (node = OF_child(base); node; node = OF_peer(node)) {
    496 		DPRINTF("RECURSE 1 STEP\n");
    497 		find_ranges(node, regions, cur, type);
    498 		if (*cur == -1)
    499 			return;
    500 	}
    501 }
    502 
    503 static int
    504 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    505 {
    506 	int i, low = 0;
    507 	u_int32_t addr = 0xffffffff;
    508 
    509 	for (i=0; i < nrof; i++) {
    510 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    511 		    ranges[i].addr < addr) {
    512 			low = i;
    513 			addr = ranges[i].addr;
    514 		}
    515 	}
    516 	if (addr == 0xffffffff)
    517 		return -1;
    518 	return low;
    519 }
    520 
    521 /*
    522  * Find a region of memory, and create a bus_space_tag for it.
    523  * Notes:
    524  * For ISA node is ignored.
    525  * node is the starting node.  if -1, we start at / and map everything.
    526  */
    527 
    528 int
    529 ofwoea_map_space(int rangetype, int iomem, int node,
    530     struct powerpc_bus_space *tag, const char *name)
    531 {
    532 	int i, cur, range, nrofholes, error;
    533 	static int exmap=0;
    534 	u_int32_t addr;
    535 	rangemap_t region, holes[32], list[32];
    536 
    537 	memset(list, 0, sizeof(list));
    538 	cur = 0;
    539 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    540 		node = OF_finddevice("/");
    541 	if (rangetype == RANGE_TYPE_ISA) {
    542 		u_int32_t size = 0;
    543 		rangemap_t regions[32];
    544 
    545 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    546 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    547 		range = 0;
    548 		DPRINTF("LOOKING FOR ISA\n");
    549 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    550 		if (range == 0 || cur == 0)
    551 			return -1; /* no isa stuff found */
    552 		/*
    553 		 * This may be confusing to some.  The ISA ranges property
    554 		 * is supposed to be a set of IO ranges for the ISA bus, but
    555 		 * generally, it's just a set of pci devfunc lists that tell
    556 		 * you to go look at the parent PCI device for the actual
    557 		 * ranges.
    558 		 */
    559 		if (range == -1) {
    560 			/* we found a rangeless isa bus */
    561 		}
    562 		DPRINTF("found isa stuff\n");
    563 		for (i=0; i < range; i++)
    564 			if (regions[i].type == iomem)
    565 				size = regions[i].size;
    566 		if (iomem == RANGE_IO) {
    567 			/* the first io range is the one */
    568 			for (i=0; i < cur; i++)
    569 				if (list[i].type == RANGE_IO && size) {
    570 					DPRINTF("found IO\n");
    571 					tag->pbs_offset = list[i].addr;
    572 					tag->pbs_limit = size;
    573 					error = bus_space_init(tag, name, NULL, 0);
    574 					return error;
    575 				}
    576 		} else {
    577 			for (i=0; i < cur; i++)
    578 				if (list[i].type == RANGE_MEM &&
    579 				    list[i].size == size) {
    580 					DPRINTF("found mem\n");
    581 					tag->pbs_offset = list[i].addr;
    582 					tag->pbs_limit = size;
    583 					error = bus_space_init(tag, name, NULL, 0);
    584 					return error;
    585 				}
    586 		}
    587 		return -1; /* NO ISA FOUND */
    588 	}
    589 	find_ranges(node, list, &cur, rangetype);
    590 
    591 	DPRINTF("cur == %d\n", cur);
    592 	/* now list should contain a list of memory regions */
    593 	for (i=0; i < cur; i++)
    594 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    595 		    list[i].size, list[i].type);
    596 
    597 	addr=0;
    598 	range = find_lowest_range(list, cur, iomem);
    599 	i = 0;
    600 	nrofholes = 0;
    601 	while (range != -1) {
    602 		DPRINTF("range==%d\n", range);
    603 		DPRINTF("i==%d\n", i);
    604 		if (i == 0) {
    605 			memcpy(&region, &list[range], sizeof(rangemap_t));
    606 			list[range].addr = 0;
    607 			i++;
    608 			range = find_lowest_range(list, cur, iomem);
    609 			continue;
    610 		}
    611 		if (region.addr + region.size < list[range].addr) {
    612 			/* allocate a hole */
    613 			holes[nrofholes].type = iomem;
    614 			holes[nrofholes].addr = region.size + region.addr;
    615 			holes[nrofholes].size = list[range].addr -
    616 			    holes[nrofholes].addr - 1;
    617 			nrofholes++;
    618 		}
    619 		region.size = list[range].size + list[range].addr -
    620 		    region.addr;
    621 		list[range].addr = 0;
    622 		range = find_lowest_range(list, cur, iomem);
    623 	}
    624 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    625 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    626 		    region.size, region.type);
    627 	DPRINTF("HOLES FOUND\n");
    628 	for (i=0; i < nrofholes; i++)
    629 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    630 		    holes[i].size, holes[i].type);
    631 	/* AT THIS POINT WE MAP IT */
    632 
    633 	if (rangetype == RANGE_TYPE_PCI) {
    634 		if (exmap == EXSTORAGE_MAX)
    635 			panic("Not enough ex_storage space. "
    636 			    "Increase EXSTORAGE_MAX");
    637 
    638 		/* XXX doing this in here might be wrong */
    639 		if (iomem == 1) {
    640 			/* we map an IO region */
    641 			tag->pbs_offset = region.addr;
    642 			tag->pbs_base = 0;
    643 			tag->pbs_limit = region.size;
    644 		} else {
    645 			/* ... or a memory region */
    646 			tag->pbs_offset = 0;
    647 			tag->pbs_base = region.addr;
    648 			tag->pbs_limit = region.size + region.addr;
    649 		}
    650 
    651 		error = bus_space_init(tag, name, ex_storage[exmap],
    652 		    sizeof(ex_storage[exmap]));
    653 		exmap++;
    654 		if (error)
    655 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    656 		for (i=0; i < nrofholes; i++) {
    657 			if (holes[i].type == RANGE_IO) {
    658 				error = extent_alloc_region(tag->pbs_extent,
    659 				    holes[i].addr - tag->pbs_offset,
    660 				    holes[i].size, EX_NOWAIT);
    661 			} else {
    662 				error = extent_alloc_region(tag->pbs_extent,
    663 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    664 			}
    665 			if (error)
    666 				panic("ofwoea_bus_space_init: can't block out"
    667 				    " reserved space 0x%x-0x%x: error=%d",
    668 				    holes[i].addr, holes[i].addr+holes[i].size,
    669 				    error);
    670 		}
    671 		return error;
    672 	}
    673 	return -1;
    674 }
    675 
    676 void
    677 ofwoea_bus_space_init(void)
    678 {
    679 	int error;
    680 
    681 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    682 	    &genppc_isa_io_space_tag, "isa-ioport");
    683 	if (error > 0)
    684 		panic("Could not map ISA IO");
    685 
    686 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    687 	    &genppc_isa_mem_space_tag, "isa-iomem");
    688 	if (error > 0)
    689 		panic("Could not map ISA MEM");
    690 }
    691