Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.62
      1 /* $NetBSD: ofwoea_machdep.c,v 1.62 2021/12/05 07:13:48 msaitoh 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.62 2021/12/05 07:13:48 msaitoh Exp $");
     34 
     35 #include "ksyms.h"
     36 #include "wsdisplay.h"
     37 
     38 #ifdef _KERNEL_OPT
     39 #include "opt_ddb.h"
     40 #include "opt_kgdb.h"
     41 #include "opt_modular.h"
     42 #include "opt_multiprocessor.h"
     43 #include "opt_oea.h"
     44 #include "opt_ofwoea.h"
     45 #include "opt_ppcarch.h"
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/buf.h>
     50 #include <sys/boot_flag.h>
     51 #include <sys/extent.h>
     52 #include <sys/kernel.h>
     53 #include <sys/ksyms.h>
     54 #include <uvm/uvm_extern.h>
     55 
     56 #include <dev/ofw/openfirm.h>
     57 #include <dev/wscons/wsconsio.h>
     58 #include <dev/wscons/wsdisplayvar.h>
     59 #include <dev/rasops/rasops.h>
     60 #include <dev/wscons/wsdisplay_vconsvar.h>
     61 #include <machine/pmap.h>
     62 #include <machine/powerpc.h>
     63 #include <machine/trap.h>
     64 #include <machine/vmparam.h>
     65 #include <machine/autoconf.h>
     66 #include <sys/bus.h>
     67 #include <powerpc/oea/bat.h>
     68 #include <powerpc/oea/ofw_rasconsvar.h>
     69 #include <powerpc/oea/cpufeat.h>
     70 #include <powerpc/include/oea/spr.h>
     71 #include <powerpc/ofw_cons.h>
     72 #include <powerpc/ofw_machdep.h>
     73 #include <powerpc/spr.h>
     74 #include <powerpc/pic/picvar.h>
     75 
     76 #ifdef DDB
     77 #include <machine/db_machdep.h>
     78 #include <ddb/db_extern.h>
     79 #endif
     80 
     81 #ifdef KGDB
     82 #include <sys/kgdb.h>
     83 #endif
     84 
     85 #ifdef ofppc
     86 extern struct model_data modeldata;
     87 #endif
     88 
     89 #ifdef OFWOEA_DEBUG
     90 #define DPRINTF printf
     91 #else
     92 #define DPRINTF while (0) printf
     93 #endif
     94 
     95 typedef struct _rangemap {
     96 	u_int32_t addr;
     97 	u_int32_t size;
     98 	int type;
     99 } rangemap_t;
    100 
    101 struct OF_translation ofw_translations[OFW_MAX_TRANSLATIONS];
    102 
    103 /*
    104  * Data structures holding OpenFirmware's translations when running
    105  * in virtual-mode.
    106  *
    107  * When we call into OpenFirmware, we point the calling CPU's
    108  * cpu_info::ci_battable at ofw_battable[].  For now, this table
    109  * is empty, which will ensure that any DSI exceptions that occur
    110  * during the firmware call will not erroneously load kernel BAT
    111  * mappings that could clobber the firmware's translations.
    112  */
    113 struct pmap ofw_pmap;
    114 struct bat ofw_battable[BAT_VA2IDX(0xffffffff)+1];
    115 
    116 char bootpath[256];
    117 char model_name[64];
    118 #if NKSYMS || defined(DDB) || defined(MODULAR)
    119 void *startsym, *endsym;
    120 #endif
    121 
    122 #if PPC_OEA601
    123 #define TIMEBASE_FREQ (1000000000)  /* RTC register */
    124 #endif
    125 
    126 #ifdef TIMEBASE_FREQ
    127 u_int timebase_freq = TIMEBASE_FREQ;
    128 #else
    129 u_int timebase_freq = 0;
    130 #endif
    131 
    132 int ofw_quiesce;
    133 
    134 extern int ofwmsr;
    135 extern uint32_t ticks_per_sec;
    136 extern uint32_t ns_per_tick;
    137 extern uint32_t ticks_per_intr;
    138 
    139 static void get_timebase_frequency(void);
    140 static void init_decrementer(void);
    141 
    142 static void restore_ofmap(void);
    143 
    144 void
    145 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    146 {
    147 	register_t scratch;
    148 
    149 #if NKSYMS || defined(DDB) || defined(MODULAR)
    150 	/* get info of kernel symbol table from bootloader */
    151 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    152 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    153 	    sizeof(endsym));
    154 	if (startsym == NULL || endsym == NULL)
    155 	    startsym = endsym = NULL;
    156 #endif
    157 
    158 	/* Parse the args string */
    159 	if (args) {
    160 		strcpy(bootpath, args);
    161 		args = bootpath;
    162 		while (*++args && *args != ' ');
    163 		if (*args) {
    164 			*args++ = 0;
    165 			while (*args)
    166 				BOOT_FLAG(*args++, boothowto);
    167 		}
    168 	} else {
    169 		int chs = OF_finddevice("/chosen");
    170 		int len;
    171 
    172 		len = OF_getprop(chs, "bootpath", bootpath, sizeof(bootpath) - 1);
    173 		if (len > -1)
    174 			bootpath[len] = 0;
    175 	}
    176 
    177 	/* Get the timebase frequency from the firmware. */
    178 	get_timebase_frequency();
    179 
    180 	/* Probe for the console device; it's initialized later. */
    181 	ofwoea_cnprobe();
    182 
    183 	if (ofw_quiesce)
    184 		OF_quiesce();
    185 
    186 	oea_init(pic_ext_intr);
    187 
    188 	/*
    189 	 * Now that we've installed our own exception vectors,
    190 	 * ensure that exceptions that happen while running
    191 	 * firmware code fall into ours.
    192 	 */
    193 	ofwmsr &= ~PSL_IP;
    194 
    195 	/* Initialize bus_space */
    196 	ofwoea_bus_space_init();
    197 
    198 	/* Initialize the console device. */
    199 	ofwoea_consinit();
    200 
    201 	uvm_md_init();
    202 
    203 	pmap_bootstrap(startkernel, endkernel);
    204 
    205 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    206 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    207 #if defined (PMAC_G5)
    208 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    209 	if (oeacpufeat & OEACPU_64_BRIDGE) {
    210 		vaddr_t va;
    211 		paddr_t pa;
    212 		vsize_t size;
    213 		int i;
    214 
    215 		pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
    216 		    round_page(MSGBUFSIZE), 0x0);
    217 
    218 		/* Map OFW code+data */
    219 
    220 		for (i = 0; i < __arraycount(ofw_translations); i++) {
    221 			va = ofw_translations[i].virt;
    222 			size = ofw_translations[i].size;
    223 			pa = ofw_translations[i].phys;
    224 			/* XXX mode */
    225 
    226 			if (size == 0) {
    227 				/* No more, all done! */
    228 				break;
    229 			}
    230 
    231 			if (va < 0xff800000)
    232 				continue;
    233 
    234 
    235 			for (; va < (ofw_translations[i].virt + size);
    236 			    va += PAGE_SIZE, pa += PAGE_SIZE) {
    237 				pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
    238 				    VM_PROT_ALL | PMAP_WIRED);
    239 			}
    240 		}
    241 
    242 #if NWSDISPLAY > 0
    243 		/* Map video frame buffer */
    244 
    245 		struct rasops_info *ri = &rascons_console_screen.scr_ri;
    246 
    247 		if (ri->ri_bits != NULL) {
    248 			for (va = (vaddr_t) ri->ri_bits;
    249 			    va < round_page((vaddr_t) ri->ri_bits +
    250 				ri->ri_height * ri->ri_stride);
    251 			    va += PAGE_SIZE) {
    252 				pmap_enter(pmap_kernel(), va, va,
    253 				    VM_PROT_READ | VM_PROT_WRITE,
    254 				    PMAP_NOCACHE | PMAP_WIRED);
    255 			}
    256 		}
    257 #endif
    258 	}
    259 #elif defined (MAMBO)
    260 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    261 	if (oeacpufeat & OEACPU_64_BRIDGE)
    262 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    263 #endif /* PMAC_G5 */
    264 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    265 
    266 	/* Now enable translation (and machine checks/recoverable interrupts) */
    267 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    268 	    : "=r"(scratch)
    269 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    270 
    271 	restore_ofmap();
    272 
    273 	rascons_finalize();
    274 
    275 #if NKSYMS || defined(DDB) || defined(MODULAR)
    276 	ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
    277 #endif
    278 
    279 	/* Kick off the clock. */
    280 	init_decrementer();
    281 
    282 #ifdef DDB
    283 	if (boothowto & RB_KDB)
    284 		Debugger();
    285 #endif
    286 }
    287 
    288 static void
    289 get_timebase_frequency(void)
    290 {
    291 	int qhandle, phandle, node;
    292 	char type[32];
    293 
    294 	if (timebase_freq != 0) {
    295 		ticks_per_sec = timebase_freq;
    296 		return;
    297 	}
    298 
    299 	node = OF_finddevice("/cpus/@0");
    300 	if (node != -1 &&
    301 	    OF_getprop(node, "timebase-frequency", &ticks_per_sec,
    302 		       sizeof ticks_per_sec) > 0) {
    303 		return;
    304 	}
    305 
    306 	node = OF_finddevice("/");
    307 	for (qhandle = node; qhandle; qhandle = phandle) {
    308 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    309 		    && strcmp(type, "cpu") == 0
    310 		    && OF_getprop(qhandle, "timebase-frequency",
    311 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    312 			return;
    313 		}
    314 		if ((phandle = OF_child(qhandle)))
    315 			continue;
    316 		while (qhandle) {
    317 			if ((phandle = OF_peer(qhandle)))
    318 				break;
    319 			qhandle = OF_parent(qhandle);
    320 		}
    321 	}
    322 	panic("no cpu node");
    323 }
    324 
    325 static void
    326 init_decrementer(void)
    327 {
    328 	int scratch, msr;
    329 
    330 	KASSERT(ticks_per_sec != 0);
    331 
    332 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    333 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    334 	ns_per_tick = 1000000000 / ticks_per_sec;
    335 	ticks_per_intr = ticks_per_sec / hz;
    336 	cpu_timebase = ticks_per_sec;
    337 
    338 #ifdef PPC_OEA601
    339 	if ((mfpvr() >> 16) == MPC601)
    340 		curcpu()->ci_lasttb = rtc_nanosecs();
    341 	else
    342 #endif
    343 		curcpu()->ci_lasttb = mftbl();
    344 
    345 	mtspr(SPR_DEC, ticks_per_intr);
    346 	mtmsr(msr);
    347 }
    348 
    349 void
    350 restore_ofmap(void)
    351 {
    352 	vaddr_t va, size;
    353 	paddr_t pa;
    354 	int i;
    355 
    356 	pmap_pinit(&ofw_pmap);
    357 
    358 #ifndef _LP64
    359 	ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
    360 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
    361 
    362 #ifdef KERNEL2_SR
    363 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
    364 #endif
    365 #endif
    366 
    367 	for (i = 0; i < __arraycount(ofw_translations); i++) {
    368 		va = ofw_translations[i].virt;
    369 		size = ofw_translations[i].size;
    370 		pa = ofw_translations[i].phys;
    371 		/* XXX mode */
    372 
    373 		if (size == 0) {
    374 			/* No more, all done! */
    375 			break;
    376 		}
    377 
    378 		if (va < 0xf0000000)	/* XXX */
    379 			continue;
    380 
    381 		/*
    382 		 * XXX macallan@
    383 		 * My beige G3 throws a DSI trap if we try to map the last page
    384 		 * of the 32bit address space. On old world macs the firmware
    385 		 * ROM occupies 4MB at 0xffc00000, triggering it when we
    386 		 * restore OF translations. This just works around a bug
    387 		 * elsewhere in pmap and should go away once fixed there.
    388 		 */
    389 		if (pa == 0xffc00000 && size == 0x400000)
    390 			size = 0x3ff000;
    391 
    392 		while (size > 0) {
    393 			pmap_enter(&ofw_pmap, va, pa, VM_PROT_ALL,
    394 			    VM_PROT_ALL|PMAP_WIRED);
    395 			pa += PAGE_SIZE;
    396 			va += PAGE_SIZE;
    397 			size -= PAGE_SIZE;
    398 		}
    399 	}
    400 	pmap_update(&ofw_pmap);
    401 }
    402 
    403 /* we define these partially, as we will fill the rest in later */
    404 struct powerpc_bus_space genppc_isa_io_space_tag = {
    405 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    406 	.pbs_base = 0x00000000,
    407 };
    408 
    409 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    410 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    411 	.pbs_base = 0x00000000,
    412 };
    413 
    414 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    415  * Increase if necc.
    416  */
    417 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    418 	__attribute__((aligned(8)));
    419 
    420 
    421 static void
    422 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    423 {
    424 	int node, i, len, reclen;
    425 	u_int32_t parent_acells, acells, scells, map[160];
    426 	char tmp[32];
    427 
    428 	node = base;
    429 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    430 		goto rec;
    431 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    432 	    strcmp("pci", tmp) != 0)
    433 		goto rec;
    434 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    435 		goto rec;
    436 	if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
    437 		len = OF_getprop(node, "reg", map, sizeof(map));
    438 		acells = 1;
    439 		scells = 1;
    440 	} else {
    441 		len = OF_getprop(node, "ranges", map, sizeof(map));
    442 	}
    443 	if (len == -1)
    444 		goto rec;
    445 	if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
    446 	    sizeof(parent_acells)) != sizeof(parent_acells))
    447 		parent_acells = 1;
    448 	if (OF_getprop(node, "#address-cells", &acells,
    449 	    sizeof(acells)) != sizeof(acells))
    450 		acells = 3;
    451 	if (OF_getprop(node, "#size-cells", &scells,
    452 	    sizeof(scells)) != sizeof(scells))
    453 		scells = 2;
    454 #ifdef ofppc
    455 	if (modeldata.ranges_offset == 0)
    456 		scells -= 1;
    457 #endif
    458 	if (type == RANGE_TYPE_ISA)
    459 		reclen = 6;
    460 	else
    461 		reclen = parent_acells + acells + scells;
    462 	/*
    463 	 * There exist ISA buses with empty ranges properties.  This is
    464 	 * known to occur on the Pegasos II machine, and likely others.
    465 	 * According to them, that means that the isa bus is a fake bus, and
    466 	 * the real maps are the PCI maps of the preceding bus.  To deal
    467 	 * with this, we will set cur to -1 and return.
    468 	 */
    469 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    470 		*cur = -1;
    471 		DPRINTF("Found empty range in isa bus\n");
    472 		return;
    473 	}
    474 
    475 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    476 	switch (type) {
    477 		case RANGE_TYPE_PCI:
    478 		case RANGE_TYPE_FIRSTPCI:
    479 			for (i=0; i < len/(4*reclen); i++) {
    480 				DPRINTF("FOUND PCI RANGE\n");
    481 				regions[*cur].size =
    482 				    map[i*reclen + parent_acells + acells + scells - 1];
    483 				/* skip ranges of size==0 */
    484 				if (regions[*cur].size == 0)
    485 					continue;
    486 				regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
    487 				regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
    488 				(*cur)++;
    489 			}
    490 			break;
    491 		case RANGE_TYPE_ISA:
    492 			for (i=0; i < len/(4*reclen); i++) {
    493 				if (map[i*reclen] == 1)
    494 					regions[*cur].type = RANGE_IO;
    495 				else
    496 					regions[*cur].type = RANGE_MEM;
    497 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    498 					regions[*cur].type);
    499 				regions[*cur].size =
    500 				    map[i*reclen + acells + scells];
    501 				(*cur)++;
    502 			}
    503 			break;
    504 		case RANGE_TYPE_MACIO:
    505 			regions[*cur].type = RANGE_MEM;
    506 			if (len == 8) {
    507 				regions[*cur].size = map[1];
    508 				regions[*cur].addr = map[0];
    509 			} else {
    510 				regions[*cur].size = map[2];
    511 				regions[*cur].addr = map[1];
    512 			}
    513 			(*cur)++;
    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 	rangemap_t region, holes[32], list[32];
    559 
    560 	memset(list, 0, sizeof(list));
    561 	memset(&region, 0, sizeof(region));
    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 	range = find_lowest_range(list, cur, iomem);
    632 	i = 0;
    633 	nrofholes = 0;
    634 	while (range != -1) {
    635 		DPRINTF("range==%d\n", range);
    636 		DPRINTF("i==%d\n", i);
    637 		if (i == 0) {
    638 			memcpy(&region, &list[range], sizeof(rangemap_t));
    639 			list[range].addr = 0;
    640 			i++;
    641 			range = find_lowest_range(list, cur, iomem);
    642 			continue;
    643 		}
    644 		if (region.addr + region.size < list[range].addr) {
    645 			/* allocate a hole */
    646 			holes[nrofholes].type = iomem;
    647 			holes[nrofholes].addr = region.size + region.addr;
    648 			holes[nrofholes].size = list[range].addr -
    649 			    holes[nrofholes].addr - 1;
    650 			nrofholes++;
    651 		}
    652 		region.size = list[range].size + list[range].addr -
    653 		    region.addr;
    654 		list[range].addr = 0;
    655 		range = find_lowest_range(list, cur, iomem);
    656 	}
    657 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    658 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    659 		    region.size, region.type);
    660 	DPRINTF("HOLES FOUND\n");
    661 	for (i=0; i < nrofholes; i++)
    662 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    663 		    holes[i].size, holes[i].type);
    664 	/* AT THIS POINT WE MAP IT */
    665 
    666 	if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
    667 		if (exmap == EXSTORAGE_MAX)
    668 			panic("Not enough ex_storage space. "
    669 			    "Increase EXSTORAGE_MAX");
    670 
    671 		/* XXX doing this in here might be wrong */
    672 		if (iomem == 1) {
    673 			/* we map an IO region */
    674 			tag->pbs_offset = region.addr;
    675 			tag->pbs_base = 0;
    676 			tag->pbs_limit = region.size;
    677 		} else {
    678 			/* ... or a memory region */
    679 			tag->pbs_offset = 0;
    680 			tag->pbs_base = region.addr;
    681 			tag->pbs_limit = region.size + region.addr;
    682 		}
    683 
    684 		error = bus_space_init(tag, name, ex_storage[exmap],
    685 		    sizeof(ex_storage[exmap]));
    686 		exmap++;
    687 		if (error)
    688 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    689 		for (i=0; i < nrofholes; i++) {
    690 			if (holes[i].type == RANGE_IO) {
    691 				error = extent_alloc_region(tag->pbs_extent,
    692 				    holes[i].addr - tag->pbs_offset,
    693 				    holes[i].size, EX_NOWAIT);
    694 			} else {
    695 				error = extent_alloc_region(tag->pbs_extent,
    696 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    697 			}
    698 			if (error)
    699 				panic("ofwoea_bus_space_init: can't block out"
    700 				    " reserved space 0x%x-0x%x: error=%d",
    701 				    holes[i].addr, holes[i].addr+holes[i].size,
    702 				    error);
    703 		}
    704 		return error;
    705 	}
    706 	return -1;
    707 }
    708 
    709 void
    710 ofwoea_bus_space_init(void)
    711 {
    712 	int error;
    713 
    714 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    715 	    &genppc_isa_io_space_tag, "isa-ioport");
    716 	if (error > 0)
    717 		panic("Could not map ISA IO");
    718 
    719 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    720 	    &genppc_isa_mem_space_tag, "isa-iomem");
    721 	if (error > 0)
    722 		panic("Could not map ISA MEM");
    723 }
    724