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