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