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