Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.58
      1 /* $NetBSD: ofwoea_machdep.c,v 1.58 2021/03/05 02:58:13 thorpej 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.58 2021/03/05 02:58:13 thorpej 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 	/* Initialize bus_space */
    181 	ofwoea_bus_space_init();
    182 
    183 	ofwoea_consinit();
    184 
    185 	if (ofw_quiesce)
    186 		OF_quiesce();
    187 
    188 	oea_init(pic_ext_intr);
    189 
    190 	/*
    191 	 * Now that we've installed our own exception vectors,
    192 	 * ensure that exceptions that happen while running
    193 	 * firmware code fall into ours.
    194 	 */
    195 	ofwmsr &= ~PSL_IP;
    196 
    197 	uvm_md_init();
    198 
    199 	pmap_bootstrap(startkernel, endkernel);
    200 
    201 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    202 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    203 #if defined (PMAC_G5)
    204 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    205 	if (oeacpufeat & OEACPU_64_BRIDGE) {
    206 		vaddr_t va;
    207 		paddr_t pa;
    208 		vsize_t size;
    209 		int i;
    210 
    211 		pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
    212 		    round_page(MSGBUFSIZE), 0x0);
    213 
    214 		/* Map OFW code+data */
    215 
    216 		for (i = 0; i < __arraycount(ofw_translations); i++) {
    217 			va = ofw_translations[i].virt;
    218 			size = ofw_translations[i].size;
    219 			pa = ofw_translations[i].phys;
    220 			/* XXX mode */
    221 
    222 			if (size == 0) {
    223 				/* No more, all done! */
    224 				break;
    225 			}
    226 
    227 			if (va < 0xff800000)
    228 				continue;
    229 
    230 
    231 			for (; va < (ofw_translations[i].virt + size);
    232 			    va += PAGE_SIZE, pa += PAGE_SIZE) {
    233 				pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
    234 				    VM_PROT_ALL | PMAP_WIRED);
    235 			}
    236 		}
    237 
    238 #if NWSDISPLAY > 0
    239 		/* Map video frame buffer */
    240 
    241 		struct rasops_info *ri = &rascons_console_screen.scr_ri;
    242 
    243 		if (ri->ri_bits != NULL) {
    244 			for (va = (vaddr_t) ri->ri_bits;
    245 			    va < round_page((vaddr_t) ri->ri_bits +
    246 				ri->ri_height * ri->ri_stride);
    247 			    va += PAGE_SIZE) {
    248 				pmap_enter(pmap_kernel(), va, va,
    249 				    VM_PROT_READ | VM_PROT_WRITE,
    250 				    PMAP_NOCACHE | PMAP_WIRED);
    251 			}
    252 		}
    253 #endif
    254 	}
    255 #elif defined (MAMBO)
    256 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    257 	if (oeacpufeat & OEACPU_64_BRIDGE)
    258 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    259 #endif /* PMAC_G5 */
    260 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    261 
    262 	/* Now enable translation (and machine checks/recoverable interrupts) */
    263 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    264 	    : "=r"(scratch)
    265 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    266 
    267 	restore_ofmap();
    268 
    269 	rascons_finalize();
    270 
    271 #if NKSYMS || defined(DDB) || defined(MODULAR)
    272 	ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
    273 #endif
    274 
    275 	/* Kick off the clock. */
    276 	init_decrementer();
    277 
    278 #ifdef DDB
    279 	if (boothowto & RB_KDB)
    280 		Debugger();
    281 #endif
    282 }
    283 
    284 static void
    285 get_timebase_frequency(void)
    286 {
    287 	int qhandle, phandle, node;
    288 	char type[32];
    289 
    290 	if (timebase_freq != 0) {
    291 		ticks_per_sec = timebase_freq;
    292 		return;
    293 	}
    294 
    295 	node = OF_finddevice("/cpus/@0");
    296 	if (node != -1 &&
    297 	    OF_getprop(node, "timebase-frequency", &ticks_per_sec,
    298 		       sizeof ticks_per_sec) > 0) {
    299 		return;
    300 	}
    301 
    302 	node = OF_finddevice("/");
    303 	for (qhandle = node; qhandle; qhandle = phandle) {
    304 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    305 		    && strcmp(type, "cpu") == 0
    306 		    && OF_getprop(qhandle, "timebase-frequency",
    307 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    308 			return;
    309 		}
    310 		if ((phandle = OF_child(qhandle)))
    311 			continue;
    312 		while (qhandle) {
    313 			if ((phandle = OF_peer(qhandle)))
    314 				break;
    315 			qhandle = OF_parent(qhandle);
    316 		}
    317 	}
    318 	panic("no cpu node");
    319 }
    320 
    321 static void
    322 init_decrementer(void)
    323 {
    324 	int scratch, msr;
    325 
    326 	KASSERT(ticks_per_sec != 0);
    327 
    328 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    329 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    330 	ns_per_tick = 1000000000 / ticks_per_sec;
    331 	ticks_per_intr = ticks_per_sec / hz;
    332 	cpu_timebase = ticks_per_sec;
    333 
    334 #ifdef PPC_OEA601
    335 	if ((mfpvr() >> 16) == MPC601)
    336 		curcpu()->ci_lasttb = rtc_nanosecs();
    337 	else
    338 #endif
    339 		curcpu()->ci_lasttb = mftbl();
    340 
    341 	mtspr(SPR_DEC, ticks_per_intr);
    342 	mtmsr(msr);
    343 }
    344 
    345 void
    346 restore_ofmap(void)
    347 {
    348 	vaddr_t va, size;
    349 	paddr_t pa;
    350 	int i;
    351 
    352 	pmap_pinit(&ofw_pmap);
    353 
    354 #ifndef _LP64
    355 	ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
    356 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
    357 
    358 #ifdef KERNEL2_SR
    359 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
    360 #endif
    361 #endif
    362 
    363 	for (i = 0; i < __arraycount(ofw_translations); i++) {
    364 		va = ofw_translations[i].virt;
    365 		size = ofw_translations[i].size;
    366 		pa = ofw_translations[i].phys;
    367 		/* XXX mode */
    368 
    369 		if (size == 0) {
    370 			/* No more, all done! */
    371 			break;
    372 		}
    373 
    374 		if (va < 0xf0000000)	/* XXX */
    375 			continue;
    376 
    377 		while (size > 0) {
    378 			pmap_enter(&ofw_pmap, va, pa, VM_PROT_ALL,
    379 			    VM_PROT_ALL|PMAP_WIRED);
    380 			pa += PAGE_SIZE;
    381 			va += PAGE_SIZE;
    382 			size -= PAGE_SIZE;
    383 		}
    384 	}
    385 	pmap_update(&ofw_pmap);
    386 }
    387 
    388 /* we define these partially, as we will fill the rest in later */
    389 struct powerpc_bus_space genppc_isa_io_space_tag = {
    390 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    391 	.pbs_base = 0x00000000,
    392 };
    393 
    394 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    395 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    396 	.pbs_base = 0x00000000,
    397 };
    398 
    399 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    400  * Increase if necc.
    401  */
    402 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    403 	__attribute__((aligned(8)));
    404 
    405 
    406 static void
    407 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    408 {
    409 	int node, i, len, reclen;
    410 	u_int32_t parent_acells, acells, scells, map[160];
    411 	char tmp[32];
    412 
    413 	node = base;
    414 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    415 		goto rec;
    416 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    417 	    strcmp("pci", tmp) != 0)
    418 		goto rec;
    419 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    420 		goto rec;
    421 	if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
    422 		len = OF_getprop(node, "reg", map, sizeof(map));
    423 		acells = 1;
    424 		scells = 1;
    425 	} else {
    426 		len = OF_getprop(node, "ranges", map, sizeof(map));
    427 	}
    428 	if (len == -1)
    429 		goto rec;
    430 	if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
    431 	    sizeof(parent_acells)) != sizeof(parent_acells))
    432 		parent_acells = 1;
    433 	if (OF_getprop(node, "#address-cells", &acells,
    434 	    sizeof(acells)) != sizeof(acells))
    435 		acells = 3;
    436 	if (OF_getprop(node, "#size-cells", &scells,
    437 	    sizeof(scells)) != sizeof(scells))
    438 		scells = 2;
    439 #ifdef ofppc
    440 	if (modeldata.ranges_offset == 0)
    441 		scells -= 1;
    442 #endif
    443 	if (type == RANGE_TYPE_ISA)
    444 		reclen = 6;
    445 	else
    446 		reclen = parent_acells + acells + scells;
    447 	/*
    448 	 * There exist ISA buses with empty ranges properties.  This is
    449 	 * known to occur on the Pegasos II machine, and likely others.
    450 	 * According to them, that means that the isa bus is a fake bus, and
    451 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    452 	 * with this, we will set cur to -1 and return.
    453 	 */
    454 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    455 		*cur = -1;
    456 		DPRINTF("Found empty range in isa bus\n");
    457 		return;
    458 	}
    459 
    460 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    461 	switch (type) {
    462 		case RANGE_TYPE_PCI:
    463 		case RANGE_TYPE_FIRSTPCI:
    464 			for (i=0; i < len/(4*reclen); i++) {
    465 				DPRINTF("FOUND PCI RANGE\n");
    466 				regions[*cur].size =
    467 				    map[i*reclen + parent_acells + acells + scells - 1];
    468 				/* skip ranges of size==0 */
    469 				if (regions[*cur].size == 0)
    470 					continue;
    471 				regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
    472 				regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
    473 				(*cur)++;
    474 			}
    475 			break;
    476 		case RANGE_TYPE_ISA:
    477 			for (i=0; i < len/(4*reclen); i++) {
    478 				if (map[i*reclen] == 1)
    479 					regions[*cur].type = RANGE_IO;
    480 				else
    481 					regions[*cur].type = RANGE_MEM;
    482 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    483 					regions[*cur].type);
    484 				regions[*cur].size =
    485 				    map[i*reclen + acells + scells];
    486 				(*cur)++;
    487 			}
    488 			break;
    489 		case RANGE_TYPE_MACIO:
    490 			regions[*cur].type = RANGE_MEM;
    491 			if (len == 8) {
    492 				regions[*cur].size = map[1];
    493 				regions[*cur].addr = map[0];
    494 			} else {
    495 				regions[*cur].size = map[2];
    496 				regions[*cur].addr = map[1];
    497 			}
    498 			(*cur)++;
    499 			break;
    500 	}
    501 	DPRINTF("returning with CUR=%d\n", *cur);
    502 	return;
    503 rec:
    504 	for (node = OF_child(base); node; node = OF_peer(node)) {
    505 		DPRINTF("RECURSE 1 STEP\n");
    506 		find_ranges(node, regions, cur, type);
    507 		if (*cur == -1)
    508 			return;
    509 	}
    510 }
    511 
    512 static int
    513 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    514 {
    515 	int i, low = 0;
    516 	u_int32_t addr = 0xffffffff;
    517 
    518 	for (i=0; i < nrof; i++) {
    519 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    520 		    ranges[i].addr < addr) {
    521 			low = i;
    522 			addr = ranges[i].addr;
    523 		}
    524 	}
    525 	if (addr == 0xffffffff)
    526 		return -1;
    527 	return low;
    528 }
    529 
    530 /*
    531  * Find a region of memory, and create a bus_space_tag for it.
    532  * Notes:
    533  * For ISA node is ignored.
    534  * node is the starting node.  if -1, we start at / and map everything.
    535  */
    536 
    537 int
    538 ofwoea_map_space(int rangetype, int iomem, int node,
    539     struct powerpc_bus_space *tag, const char *name)
    540 {
    541 	int i, cur, range, nrofholes, error;
    542 	static int exmap=0;
    543 	rangemap_t region, holes[32], list[32];
    544 
    545 	memset(list, 0, sizeof(list));
    546 	memset(&region, 0, sizeof(region));
    547 	cur = 0;
    548 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    549 		node = OF_finddevice("/");
    550 	if (rangetype == RANGE_TYPE_ISA) {
    551 		u_int32_t size = 0;
    552 		rangemap_t regions[32];
    553 
    554 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    555 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    556 		range = 0;
    557 		DPRINTF("LOOKING FOR ISA\n");
    558 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    559 		if (range == 0 || cur == 0)
    560 			return -1; /* no isa stuff found */
    561 		/*
    562 		 * This may be confusing to some.  The ISA ranges property
    563 		 * is supposed to be a set of IO ranges for the ISA bus, but
    564 		 * generally, it's just a set of pci devfunc lists that tell
    565 		 * you to go look at the parent PCI device for the actual
    566 		 * ranges.
    567 		 */
    568 		if (range == -1) {
    569 			/* we found a rangeless isa bus */
    570 			if (iomem == RANGE_IO)
    571 				size = 0x10000;
    572 			else
    573 				size = 0x1000000;
    574 		}
    575 		DPRINTF("found isa stuff\n");
    576 		for (i=0; i < range; i++)
    577 			if (regions[i].type == iomem)
    578 				size = regions[i].size;
    579 		if (iomem == RANGE_IO) {
    580 			/* the first io range is the one */
    581 			for (i=0; i < cur; i++)
    582 				if (list[i].type == RANGE_IO && size) {
    583 					DPRINTF("found IO\n");
    584 					tag->pbs_offset = list[i].addr;
    585 					tag->pbs_limit = size;
    586 					error = bus_space_init(tag, name,
    587 					    ex_storage[exmap],
    588 					    sizeof(ex_storage[exmap]));
    589 					exmap++;
    590 					return error;
    591 				}
    592 		} else {
    593 			for (i=0; i < cur; i++)
    594 				if (list[i].type == RANGE_MEM &&
    595 				    list[i].size == size) {
    596 					DPRINTF("found mem\n");
    597 					tag->pbs_offset = list[i].addr;
    598 					tag->pbs_limit = size;
    599 					error = bus_space_init(tag, name,
    600 					    ex_storage[exmap],
    601 					    sizeof(ex_storage[exmap]));
    602 					exmap++;
    603 					return error;
    604 				}
    605 		}
    606 		return -1; /* NO ISA FOUND */
    607 	}
    608 	find_ranges(node, list, &cur, rangetype);
    609 
    610 	DPRINTF("cur == %d\n", cur);
    611 	/* now list should contain a list of memory regions */
    612 	for (i=0; i < cur; i++)
    613 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    614 		    list[i].size, list[i].type);
    615 
    616 	range = find_lowest_range(list, cur, iomem);
    617 	i = 0;
    618 	nrofholes = 0;
    619 	while (range != -1) {
    620 		DPRINTF("range==%d\n", range);
    621 		DPRINTF("i==%d\n", i);
    622 		if (i == 0) {
    623 			memcpy(&region, &list[range], sizeof(rangemap_t));
    624 			list[range].addr = 0;
    625 			i++;
    626 			range = find_lowest_range(list, cur, iomem);
    627 			continue;
    628 		}
    629 		if (region.addr + region.size < list[range].addr) {
    630 			/* allocate a hole */
    631 			holes[nrofholes].type = iomem;
    632 			holes[nrofholes].addr = region.size + region.addr;
    633 			holes[nrofholes].size = list[range].addr -
    634 			    holes[nrofholes].addr - 1;
    635 			nrofholes++;
    636 		}
    637 		region.size = list[range].size + list[range].addr -
    638 		    region.addr;
    639 		list[range].addr = 0;
    640 		range = find_lowest_range(list, cur, iomem);
    641 	}
    642 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    643 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    644 		    region.size, region.type);
    645 	DPRINTF("HOLES FOUND\n");
    646 	for (i=0; i < nrofholes; i++)
    647 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    648 		    holes[i].size, holes[i].type);
    649 	/* AT THIS POINT WE MAP IT */
    650 
    651 	if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
    652 		if (exmap == EXSTORAGE_MAX)
    653 			panic("Not enough ex_storage space. "
    654 			    "Increase EXSTORAGE_MAX");
    655 
    656 		/* XXX doing this in here might be wrong */
    657 		if (iomem == 1) {
    658 			/* we map an IO region */
    659 			tag->pbs_offset = region.addr;
    660 			tag->pbs_base = 0;
    661 			tag->pbs_limit = region.size;
    662 		} else {
    663 			/* ... or a memory region */
    664 			tag->pbs_offset = 0;
    665 			tag->pbs_base = region.addr;
    666 			tag->pbs_limit = region.size + region.addr;
    667 		}
    668 
    669 		error = bus_space_init(tag, name, ex_storage[exmap],
    670 		    sizeof(ex_storage[exmap]));
    671 		exmap++;
    672 		if (error)
    673 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    674 		for (i=0; i < nrofholes; i++) {
    675 			if (holes[i].type == RANGE_IO) {
    676 				error = extent_alloc_region(tag->pbs_extent,
    677 				    holes[i].addr - tag->pbs_offset,
    678 				    holes[i].size, EX_NOWAIT);
    679 			} else {
    680 				error = extent_alloc_region(tag->pbs_extent,
    681 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    682 			}
    683 			if (error)
    684 				panic("ofwoea_bus_space_init: can't block out"
    685 				    " reserved space 0x%x-0x%x: error=%d",
    686 				    holes[i].addr, holes[i].addr+holes[i].size,
    687 				    error);
    688 		}
    689 		return error;
    690 	}
    691 	return -1;
    692 }
    693 
    694 void
    695 ofwoea_bus_space_init(void)
    696 {
    697 	int error;
    698 
    699 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    700 	    &genppc_isa_io_space_tag, "isa-ioport");
    701 	if (error > 0)
    702 		panic("Could not map ISA IO");
    703 
    704 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    705 	    &genppc_isa_mem_space_tag, "isa-iomem");
    706 	if (error > 0)
    707 		panic("Could not map ISA MEM");
    708 }
    709