Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.22
      1 /* $NetBSD: ofwoea_machdep.c,v 1.22 2011/06/20 06:21:45 matt 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.22 2011/06/20 06:21:45 matt Exp $");
     34 
     35 #include "opt_ppcarch.h"
     36 #include "opt_compat_netbsd.h"
     37 #include "opt_ddb.h"
     38 #include "opt_kgdb.h"
     39 #include "opt_ipkdb.h"
     40 #include "opt_modular.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/buf.h>
     44 #include <sys/boot_flag.h>
     45 #include <sys/extent.h>
     46 #include <sys/kernel.h>
     47 #include <sys/ksyms.h>
     48 #include <uvm/uvm_extern.h>
     49 
     50 #include <dev/ofw/openfirm.h>
     51 #include <machine/pmap.h>
     52 #include <machine/powerpc.h>
     53 #include <machine/trap.h>
     54 #include <machine/vmparam.h>
     55 #include <machine/autoconf.h>
     56 #include <powerpc/bus.h>
     57 #include <powerpc/oea/bat.h>
     58 #include <powerpc/oea/cpufeat.h>
     59 #include <powerpc/ofw_bus.h>
     60 #include <powerpc/ofw_cons.h>
     61 #include <powerpc/spr.h>
     62 #include <powerpc/pic/picvar.h>
     63 
     64 #include "opt_oea.h"
     65 
     66 #include "ksyms.h"
     67 
     68 #ifdef DDB
     69 #include <machine/db_machdep.h>
     70 #include <ddb/db_extern.h>
     71 #endif
     72 
     73 #ifdef KGDB
     74 #include <sys/kgdb.h>
     75 #endif
     76 
     77 #ifdef IPKDB
     78 #include <ipkdb/ipkdb.h>
     79 #endif
     80 
     81 #include "opt_ofwoea.h"
     82 
     83 #ifdef ofppc
     84 extern struct model_data modeldata;
     85 #endif
     86 
     87 #ifdef OFWOEA_DEBUG
     88 #define DPRINTF printf
     89 #else
     90 #define DPRINTF while (0) printf
     91 #endif
     92 
     93 typedef struct _rangemap {
     94 	u_int32_t addr;
     95 	u_int32_t size;
     96 	int type;
     97 } rangemap_t;
     98 
     99 struct ofw_translations {
    100 	vaddr_t va;
    101 	int len;
    102 #if defined (PMAC_G5)
    103 	register64_t pa;
    104 #else
    105 	register_t pa;
    106 #endif
    107 	int mode;
    108 }__attribute__((packed));
    109 
    110 struct pmap ofw_pmap;
    111 struct ofw_translations ofmap[32];
    112 char bootpath[256];
    113 char model_name[64];
    114 #if NKSYMS || defined(DDB) || defined(MODULAR)
    115 void *startsym, *endsym;
    116 #endif
    117 #ifdef TIMEBASE_FREQ
    118 u_int timebase_freq = TIMEBASE_FREQ;
    119 #else
    120 u_int timebase_freq = 0;
    121 #endif
    122 
    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 	/* Initialize bus_space */
    170 	ofwoea_bus_space_init();
    171 
    172 	ofwoea_consinit();
    173 
    174 #if defined(MULTIPROCESSOR) && defined(ofppc)
    175 	for (i=1; i < CPU_MAXNUM; i++) {
    176 		sprintf(cpupath, "/cpus/@%x", i);
    177 		node = OF_finddevice(cpupath);
    178 		if (node <= 0)
    179 			continue;
    180 		aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
    181 		OF_start_cpu(node, (u_int)cpu_spinstart, i);
    182 		for (l=0; l < 100000000; l++) {
    183 			if (cpu_spinstart_ack == i) {
    184 				aprint_verbose("CPU %d spun up.\n", i);
    185 				break;
    186 			}
    187 			__asm volatile ("sync");
    188 		}
    189 	}
    190 #endif
    191 
    192 	oea_init(pic_ext_intr);
    193 
    194 	ofmaplen = save_ofmap(NULL, 0);
    195 	if (ofmaplen > 0)
    196 		save_ofmap(ofmap, ofmaplen);
    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_addsyms_elf((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 return them as bitmap 0..15
    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 #ifdef ofppc
    376 		reclen = acells + modeldata.ranges_offset + scells;
    377 #else
    378 		reclen = acells + 1 + scells;
    379 #endif
    380 
    381 		for (i=0; i < (mlen/4)/reclen; i++) {
    382 			addr = map[reclen * i + acells];
    383 			len = map[reclen * i + reclen - 1];
    384 			for (j = 0; j < len / 0x10000000; j++)
    385 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    386 			bitmap |= 1 << (addr >> 28);
    387 		}
    388 noranges:
    389 		bitmap |= ranges_bitmap(child, bitmap);
    390 		continue;
    391 	}
    392 	return bitmap;
    393 }
    394 
    395 void
    396 ofwoea_batinit(void)
    397 {
    398 #if defined (PPC_OEA)
    399         u_int16_t bitmap;
    400 	int node, i;
    401 
    402 	node = OF_finddevice("/");
    403 	bitmap = ranges_bitmap(node, 0);
    404 	oea_batinit(0);
    405 
    406 #ifdef macppc
    407 	/* XXX this is a macppc-specific hack */
    408 	bitmap = 0x8f00;
    409 #endif
    410 	for (i=1; i < 0x10; i++) {
    411 		/* skip the three vital SR regions */
    412 		if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
    413 			continue;
    414 		if (bitmap & (1 << i)) {
    415 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    416 			DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
    417 		}
    418 	}
    419 #endif /* OEA */
    420 }
    421 
    422 
    423 /* we define these partially, as we will fill the rest in later */
    424 struct powerpc_bus_space genppc_isa_io_space_tag = {
    425 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    426 	.pbs_base = 0x00000000,
    427 };
    428 
    429 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    430 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    431 	.pbs_base = 0x00000000,
    432 };
    433 
    434 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    435  * Increase if necc.
    436  */
    437 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    438 	__attribute__((aligned(8)));
    439 
    440 
    441 static void
    442 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    443 {
    444 	int node, i, len, reclen;
    445 	u_int32_t acells, scells, map[160];
    446 	char tmp[32];
    447 
    448 	node = base;
    449 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    450 		goto rec;
    451 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    452 	    strcmp("pci", tmp) != 0)
    453 		goto rec;
    454 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    455 		goto rec;
    456 	len = OF_getprop(node, "ranges", map, sizeof(map));
    457 	if (len == -1)
    458 		goto rec;
    459 	if (OF_getprop(node, "#address-cells", &acells,
    460 	    sizeof(acells)) != sizeof(acells))
    461 		acells = 1;
    462 	if (OF_getprop(node, "#size-cells", &scells,
    463 	    sizeof(scells)) != sizeof(scells))
    464 		scells = 1;
    465 #ifdef ofppc
    466 	if (modeldata.ranges_offset == 0)
    467 		scells -= 1;
    468 #endif
    469 	if (type == RANGE_TYPE_ISA)
    470 		reclen = 6;
    471 	else
    472 		reclen = acells + scells + 1;
    473 	/*
    474 	 * There exist ISA buses with empty ranges properties.  This is
    475 	 * known to occur on the Pegasos II machine, and likely others.
    476 	 * According to them, that means that the isa bus is a fake bus, and
    477 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    478 	 * with this, we will set cur to -1 and return.
    479 	 */
    480 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    481 		*cur = -1;
    482 		DPRINTF("Found empty range in isa bus\n");
    483 		return;
    484 	}
    485 
    486 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    487 	switch (type) {
    488 		case RANGE_TYPE_PCI:
    489 		case RANGE_TYPE_FIRSTPCI:
    490 			for (i=0; i < len/(4*reclen); i++) {
    491 				DPRINTF("FOUND PCI RANGE\n");
    492 				regions[*cur].size =
    493 				    map[i*reclen + acells + scells];
    494 				/* skip ranges of size==0 */
    495 				if (regions[*cur].size == 0)
    496 					continue;
    497 				regions[*cur].type = map[i*reclen] >> 24;
    498 				regions[*cur].addr = map[i*reclen + acells];
    499 				(*cur)++;
    500 			}
    501 			break;
    502 		case RANGE_TYPE_ISA:
    503 			for (i=0; i < len/(4*reclen); i++) {
    504 				if (map[i*reclen] == 1)
    505 					regions[*cur].type = RANGE_IO;
    506 				else
    507 					regions[*cur].type = RANGE_MEM;
    508 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    509 					regions[*cur].type);
    510 				regions[*cur].size =
    511 				    map[i*reclen + acells + scells];
    512 				(*cur)++;
    513 			}
    514 			break;
    515 	}
    516 	DPRINTF("returning with CUR=%d\n", *cur);
    517 	return;
    518 rec:
    519 	for (node = OF_child(base); node; node = OF_peer(node)) {
    520 		DPRINTF("RECURSE 1 STEP\n");
    521 		find_ranges(node, regions, cur, type);
    522 		if (*cur == -1)
    523 			return;
    524 	}
    525 }
    526 
    527 static int
    528 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    529 {
    530 	int i, low = 0;
    531 	u_int32_t addr = 0xffffffff;
    532 
    533 	for (i=0; i < nrof; i++) {
    534 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    535 		    ranges[i].addr < addr) {
    536 			low = i;
    537 			addr = ranges[i].addr;
    538 		}
    539 	}
    540 	if (addr == 0xffffffff)
    541 		return -1;
    542 	return low;
    543 }
    544 
    545 /*
    546  * Find a region of memory, and create a bus_space_tag for it.
    547  * Notes:
    548  * For ISA node is ignored.
    549  * node is the starting node.  if -1, we start at / and map everything.
    550  */
    551 
    552 int
    553 ofwoea_map_space(int rangetype, int iomem, int node,
    554     struct powerpc_bus_space *tag, const char *name)
    555 {
    556 	int i, cur, range, nrofholes, error;
    557 	static int exmap=0;
    558 	u_int32_t addr;
    559 	rangemap_t region, holes[32], list[32];
    560 
    561 	memset(list, 0, sizeof(list));
    562 	cur = 0;
    563 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    564 		node = OF_finddevice("/");
    565 	if (rangetype == RANGE_TYPE_ISA) {
    566 		u_int32_t size = 0;
    567 		rangemap_t regions[32];
    568 
    569 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    570 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    571 		range = 0;
    572 		DPRINTF("LOOKING FOR ISA\n");
    573 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    574 		if (range == 0 || cur == 0)
    575 			return -1; /* no isa stuff found */
    576 		/*
    577 		 * This may be confusing to some.  The ISA ranges property
    578 		 * is supposed to be a set of IO ranges for the ISA bus, but
    579 		 * generally, it's just a set of pci devfunc lists that tell
    580 		 * you to go look at the parent PCI device for the actual
    581 		 * ranges.
    582 		 */
    583 		if (range == -1) {
    584 			/* we found a rangeless isa bus */
    585 			if (iomem == RANGE_IO)
    586 				size = 0x10000;
    587 			else
    588 				size = 0x1000000;
    589 		}
    590 		DPRINTF("found isa stuff\n");
    591 		for (i=0; i < range; i++)
    592 			if (regions[i].type == iomem)
    593 				size = regions[i].size;
    594 		if (iomem == RANGE_IO) {
    595 			/* the first io range is the one */
    596 			for (i=0; i < cur; i++)
    597 				if (list[i].type == RANGE_IO && size) {
    598 					DPRINTF("found IO\n");
    599 					tag->pbs_offset = list[i].addr;
    600 					tag->pbs_limit = size;
    601 					error = bus_space_init(tag, name,
    602 					    ex_storage[exmap],
    603 					    sizeof(ex_storage[exmap]));
    604 					exmap++;
    605 					return error;
    606 				}
    607 		} else {
    608 			for (i=0; i < cur; i++)
    609 				if (list[i].type == RANGE_MEM &&
    610 				    list[i].size == size) {
    611 					DPRINTF("found mem\n");
    612 					tag->pbs_offset = list[i].addr;
    613 					tag->pbs_limit = size;
    614 					error = bus_space_init(tag, name,
    615 					    ex_storage[exmap],
    616 					    sizeof(ex_storage[exmap]));
    617 					exmap++;
    618 					return error;
    619 				}
    620 		}
    621 		return -1; /* NO ISA FOUND */
    622 	}
    623 	find_ranges(node, list, &cur, rangetype);
    624 
    625 	DPRINTF("cur == %d\n", cur);
    626 	/* now list should contain a list of memory regions */
    627 	for (i=0; i < cur; i++)
    628 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    629 		    list[i].size, list[i].type);
    630 
    631 	addr=0;
    632 	range = find_lowest_range(list, cur, iomem);
    633 	i = 0;
    634 	nrofholes = 0;
    635 	while (range != -1) {
    636 		DPRINTF("range==%d\n", range);
    637 		DPRINTF("i==%d\n", i);
    638 		if (i == 0) {
    639 			memcpy(&region, &list[range], sizeof(rangemap_t));
    640 			list[range].addr = 0;
    641 			i++;
    642 			range = find_lowest_range(list, cur, iomem);
    643 			continue;
    644 		}
    645 		if (region.addr + region.size < list[range].addr) {
    646 			/* allocate a hole */
    647 			holes[nrofholes].type = iomem;
    648 			holes[nrofholes].addr = region.size + region.addr;
    649 			holes[nrofholes].size = list[range].addr -
    650 			    holes[nrofholes].addr - 1;
    651 			nrofholes++;
    652 		}
    653 		region.size = list[range].size + list[range].addr -
    654 		    region.addr;
    655 		list[range].addr = 0;
    656 		range = find_lowest_range(list, cur, iomem);
    657 	}
    658 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    659 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    660 		    region.size, region.type);
    661 	DPRINTF("HOLES FOUND\n");
    662 	for (i=0; i < nrofholes; i++)
    663 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    664 		    holes[i].size, holes[i].type);
    665 	/* AT THIS POINT WE MAP IT */
    666 
    667 	if (rangetype == RANGE_TYPE_PCI) {
    668 		if (exmap == EXSTORAGE_MAX)
    669 			panic("Not enough ex_storage space. "
    670 			    "Increase EXSTORAGE_MAX");
    671 
    672 		/* XXX doing this in here might be wrong */
    673 		if (iomem == 1) {
    674 			/* we map an IO region */
    675 			tag->pbs_offset = region.addr;
    676 			tag->pbs_base = 0;
    677 			tag->pbs_limit = region.size;
    678 		} else {
    679 			/* ... or a memory region */
    680 			tag->pbs_offset = 0;
    681 			tag->pbs_base = region.addr;
    682 			tag->pbs_limit = region.size + region.addr;
    683 		}
    684 
    685 		error = bus_space_init(tag, name, ex_storage[exmap],
    686 		    sizeof(ex_storage[exmap]));
    687 		exmap++;
    688 		if (error)
    689 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    690 		for (i=0; i < nrofholes; i++) {
    691 			if (holes[i].type == RANGE_IO) {
    692 				error = extent_alloc_region(tag->pbs_extent,
    693 				    holes[i].addr - tag->pbs_offset,
    694 				    holes[i].size, EX_NOWAIT);
    695 			} else {
    696 				error = extent_alloc_region(tag->pbs_extent,
    697 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    698 			}
    699 			if (error)
    700 				panic("ofwoea_bus_space_init: can't block out"
    701 				    " reserved space 0x%x-0x%x: error=%d",
    702 				    holes[i].addr, holes[i].addr+holes[i].size,
    703 				    error);
    704 		}
    705 		return error;
    706 	}
    707 	return -1;
    708 }
    709 
    710 void
    711 ofwoea_bus_space_init(void)
    712 {
    713 	int error;
    714 
    715 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    716 	    &genppc_isa_io_space_tag, "isa-ioport");
    717 	if (error > 0)
    718 		panic("Could not map ISA IO");
    719 
    720 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    721 	    &genppc_isa_mem_space_tag, "isa-iomem");
    722 	if (error > 0)
    723 		panic("Could not map ISA MEM");
    724 }
    725