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