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