Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.36
      1 /* $NetBSD: ofwoea_machdep.c,v 1.36 2014/02/28 05:35:49 matt 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.36 2014/02/28 05:35:49 matt 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 "wsdisplay.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/buf.h>
     46 #include <sys/boot_flag.h>
     47 #include <sys/extent.h>
     48 #include <sys/kernel.h>
     49 #include <sys/ksyms.h>
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <dev/ofw/openfirm.h>
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wscons/wsdisplayvar.h>
     55 #include <dev/rasops/rasops.h>
     56 #include <dev/wscons/wsdisplay_vconsvar.h>
     57 #include <machine/pmap.h>
     58 #include <machine/powerpc.h>
     59 #include <machine/trap.h>
     60 #include <machine/vmparam.h>
     61 #include <machine/autoconf.h>
     62 #include <sys/bus.h>
     63 #include <powerpc/oea/bat.h>
     64 #include <powerpc/oea/ofw_rasconsvar.h>
     65 #include <powerpc/oea/cpufeat.h>
     66 #include <powerpc/ofw_cons.h>
     67 #include <powerpc/spr.h>
     68 #include <powerpc/pic/picvar.h>
     69 
     70 #include "opt_oea.h"
     71 
     72 #include "ksyms.h"
     73 
     74 #ifdef DDB
     75 #include <machine/db_machdep.h>
     76 #include <ddb/db_extern.h>
     77 #endif
     78 
     79 #ifdef KGDB
     80 #include <sys/kgdb.h>
     81 #endif
     82 
     83 #ifdef IPKDB
     84 #include <ipkdb/ipkdb.h>
     85 #endif
     86 
     87 #include "opt_ofwoea.h"
     88 
     89 #ifdef ofppc
     90 extern struct model_data modeldata;
     91 #endif
     92 
     93 #ifdef OFWOEA_DEBUG
     94 #define DPRINTF printf
     95 #else
     96 #define DPRINTF while (0) printf
     97 #endif
     98 
     99 typedef struct _rangemap {
    100 	u_int32_t addr;
    101 	u_int32_t size;
    102 	int type;
    103 } rangemap_t;
    104 
    105 struct ofw_translations {
    106 	vaddr_t va;
    107 	int len;
    108 #if defined (PMAC_G5)
    109 	register64_t pa;
    110 #else
    111 	register_t pa;
    112 #endif
    113 	int mode;
    114 }__attribute__((packed));
    115 
    116 struct pmap ofw_pmap;
    117 struct ofw_translations ofmap[32];
    118 char bootpath[256];
    119 char model_name[64];
    120 #if NKSYMS || defined(DDB) || defined(MODULAR)
    121 void *startsym, *endsym;
    122 #endif
    123 #ifdef TIMEBASE_FREQ
    124 u_int timebase_freq = TIMEBASE_FREQ;
    125 #else
    126 u_int timebase_freq = 0;
    127 #endif
    128 
    129 extern int ofwmsr;
    130 extern int chosen;
    131 extern uint32_t ticks_per_sec;
    132 extern uint32_t ns_per_tick;
    133 extern uint32_t ticks_per_intr;
    134 
    135 static int save_ofmap(struct ofw_translations *, int);
    136 static void restore_ofmap(struct ofw_translations *, int);
    137 static void set_timebase(void);
    138 
    139 extern void cpu_spinstart(u_int);
    140 extern volatile u_int cpu_spinstart_ack;
    141 
    142 void
    143 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    144 {
    145 	int ofmaplen, node, l;
    146 	register_t scratch;
    147 
    148 #if defined(MULTIPROCESSOR) && defined(ofppc)
    149 	char cpupath[32];
    150 	int i;
    151 #endif
    152 
    153 	/* initialze bats */
    154 	if ((oeacpufeat & OEACPU_NOBAT) == 0)
    155 		ofwoea_batinit();
    156 
    157 #if NKSYMS || defined(DDB) || defined(MODULAR)
    158 	/* get info of kernel symbol table from bootloader */
    159 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    160 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    161 	    sizeof(endsym));
    162 	if (startsym == NULL || endsym == NULL)
    163 	    startsym = endsym = NULL;
    164 #endif
    165 
    166 	/* get model name and perform model-specific actions */
    167 	memset(model_name, 0, sizeof(model_name));
    168 	node = OF_finddevice("/");
    169 	if (node != -1) {
    170 		l = OF_getprop(node, "model", model_name, sizeof(model_name));
    171 		if (l == -1)
    172 			OF_getprop(node, "name", model_name,
    173 			    sizeof(model_name));
    174 		model_init();
    175 	}
    176 
    177 	if (strcmp(model_name, "PowerMac11,2") == 0 ||
    178 	    strcmp(model_name, "PowerMac11,1") == 0)
    179 		OF_quiesce();
    180 
    181 	/* Initialize bus_space */
    182 	ofwoea_bus_space_init();
    183 
    184 	ofwoea_consinit();
    185 
    186 #if defined(MULTIPROCESSOR) && defined(ofppc)
    187 	for (i=1; i < CPU_MAXNUM; i++) {
    188 		sprintf(cpupath, "/cpus/@%x", i);
    189 		node = OF_finddevice(cpupath);
    190 		if (node <= 0)
    191 			continue;
    192 		aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
    193 		OF_start_cpu(node, (u_int)cpu_spinstart, i);
    194 		for (l=0; l < 100000000; l++) {
    195 			if (cpu_spinstart_ack == i) {
    196 				aprint_verbose("CPU %d spun up.\n", i);
    197 				break;
    198 			}
    199 			__asm volatile ("sync");
    200 		}
    201 	}
    202 #endif
    203 
    204 	oea_init(pic_ext_intr);
    205 
    206 	ofmaplen = save_ofmap(NULL, 0);
    207 	if (ofmaplen > 0)
    208 		save_ofmap(ofmap, ofmaplen);
    209 
    210 /*
    211  * XXX
    212  * we need to do this here instead of earlier on in ofwinit() for some reason
    213  * At least some versions of Apple OF 2.0.1 hang if we do this earlier
    214  */
    215 	ofwmsr &= ~PSL_IP;
    216 
    217 	/* Parse the args string */
    218 	if (args) {
    219 		strcpy(bootpath, args);
    220 		args = bootpath;
    221 		while (*++args && *args != ' ');
    222 		if (*args) {
    223 			*args++ = 0;
    224 			while (*args)
    225 				BOOT_FLAG(*args++, boothowto);
    226 		}
    227 	}
    228 
    229 	uvm_setpagesize();
    230 
    231 	pmap_bootstrap(startkernel, endkernel);
    232 
    233 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    234 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    235 #if defined (PMAC_G5)
    236 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    237 	if (oeacpufeat & OEACPU_64_BRIDGE) {
    238 		vaddr_t va;
    239 		paddr_t pa;
    240 		int i;
    241 
    242 		pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
    243 		    round_page(MSGBUFSIZE), 0x0);
    244 
    245 		/* Map OFW code+data */
    246 
    247 		for (i = 0; i < ofmaplen / sizeof(struct ofw_translations); i++) {
    248 			if (ofmap[i].va < 0xff800000)
    249 				continue;
    250 
    251 			for (va = ofmap[i].va, pa = ofmap[i].pa;
    252 			    va < ofmap[i].va + ofmap[i].len;
    253 			    va += PAGE_SIZE, pa += PAGE_SIZE) {
    254 				pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
    255 				    VM_PROT_ALL | PMAP_WIRED);
    256 			}
    257 		}
    258 
    259 #if NWSDISPLAY > 0
    260 		/* Map video frame buffer */
    261 
    262 		struct rasops_info *ri = &rascons_console_screen.scr_ri;
    263 
    264 		if (ri->ri_bits != NULL) {
    265 			for (va = (vaddr_t) ri->ri_bits;
    266 			    va < round_page((vaddr_t) ri->ri_bits +
    267 				ri->ri_height * ri->ri_stride);
    268 			    va += PAGE_SIZE) {
    269 				pmap_enter(pmap_kernel(), va, va,
    270 				    VM_PROT_READ | VM_PROT_WRITE,
    271 				    PMAP_NOCACHE | PMAP_WIRED);
    272 			}
    273 		}
    274 #endif
    275 	}
    276 #elif defined (MAMBO)
    277 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    278 	if (oeacpufeat & OEACPU_64_BRIDGE)
    279 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    280 #endif /* PMAC_G5 */
    281 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    282 
    283 	/* Now enable translation (and machine checks/recoverable interrupts) */
    284 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    285 	    : "=r"(scratch)
    286 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    287 
    288 	restore_ofmap(ofmap, ofmaplen);
    289 
    290 #if NKSYMS || defined(DDB) || defined(MODULAR)
    291 	ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
    292 #endif
    293 
    294 	/* CPU clock stuff */
    295 	set_timebase();
    296 
    297 #ifdef DDB
    298 	if (boothowto & RB_KDB)
    299 		Debugger();
    300 #endif
    301 }
    302 
    303 void
    304 set_timebase(void)
    305 {
    306 	int qhandle, phandle, msr, scratch;
    307 	char type[32];
    308 
    309 	if (timebase_freq != 0) {
    310 		ticks_per_sec = timebase_freq;
    311 		goto found;
    312 	}
    313 
    314 	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    315 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    316 		    && strcmp(type, "cpu") == 0
    317 		    && OF_getprop(qhandle, "timebase-frequency",
    318 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    319 			goto found;
    320 		}
    321 		if ((phandle = OF_child(qhandle)))
    322 			continue;
    323 		while (qhandle) {
    324 			if ((phandle = OF_peer(qhandle)))
    325 				break;
    326 			qhandle = OF_parent(qhandle);
    327 		}
    328 	}
    329 	panic("no cpu node");
    330 
    331 found:
    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 	curcpu()->ci_lasttb = mftbl();
    338 	mtspr(SPR_DEC, ticks_per_intr);
    339 	mtmsr(msr);
    340 }
    341 
    342 static int
    343 save_ofmap(struct ofw_translations *map, int maxlen)
    344 {
    345 	int mmui, mmu, len;
    346 
    347 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    348 	mmu = OF_instance_to_package(mmui);
    349 
    350 	if (map) {
    351 		memset(map, 0, maxlen); /* to be safe */
    352 		len = OF_getprop(mmu, "translations", map, maxlen);
    353 	} else
    354 		len = OF_getproplen(mmu, "translations");
    355 
    356 	if (len < 0)
    357 		len = 0;
    358 	return len;
    359 }
    360 
    361 
    362 /* The PMAC_G5 code here needs to be replaced by code that looks for the
    363    size_cells and does the right thing automatically.
    364 */
    365 void
    366 restore_ofmap(struct ofw_translations *map, int len)
    367 {
    368 	int n = len / sizeof(struct ofw_translations);
    369 	int i;
    370 
    371 	pmap_pinit(&ofw_pmap);
    372 
    373 #ifndef _LP64
    374 	ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
    375 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
    376 
    377 #ifdef KERNEL2_SR
    378 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
    379 #endif
    380 #endif
    381 
    382 	for (i = 0; i < n; i++) {
    383 #if defined (PMAC_G5)
    384 		register64_t pa = map[i].pa;
    385 #else
    386 		register_t pa = map[i].pa;
    387 #endif
    388 		vaddr_t va = map[i].va;
    389 		size_t length = map[i].len;
    390 
    391 		if (va < 0xf0000000) /* XXX */
    392 			continue;
    393 
    394 		while (length > 0) {
    395 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    396 			    VM_PROT_ALL|PMAP_WIRED);
    397 			pa += PAGE_SIZE;
    398 			va += PAGE_SIZE;
    399 			length -= PAGE_SIZE;
    400 		}
    401 	}
    402 	pmap_update(&ofw_pmap);
    403 }
    404 
    405 
    406 
    407 /*
    408  * Scan the device tree for ranges, and return them as bitmap 0..15
    409  */
    410 #if !defined(macppc) && defined(PPC_OEA)
    411 static u_int16_t
    412 ranges_bitmap(int node, u_int16_t bitmap)
    413 {
    414 	int child, mlen, acells, scells, reclen, i, j;
    415 	u_int32_t addr, len, map[160];
    416 
    417 	for (child = OF_child(node); child; child = OF_peer(child)) {
    418 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    419 		if (mlen == -1)
    420 			goto noranges;
    421 
    422 		j = OF_getprop(child, "#address-cells", &acells,
    423 		    sizeof(acells));
    424 		if (j == -1)
    425 			goto noranges;
    426 
    427 		j = OF_getprop(child, "#size-cells", &scells,
    428 		    sizeof(scells));
    429 		if (j == -1)
    430 			goto noranges;
    431 
    432 #ifdef ofppc
    433 		reclen = acells + modeldata.ranges_offset + scells;
    434 #else
    435 		reclen = acells + 1 + scells;
    436 #endif
    437 
    438 		for (i=0; i < (mlen/4)/reclen; i++) {
    439 			addr = map[reclen * i + acells];
    440 			len = map[reclen * i + reclen - 1];
    441 			for (j = 0; j < len / 0x10000000; j++)
    442 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    443 			bitmap |= 1 << (addr >> 28);
    444 		}
    445 noranges:
    446 		bitmap |= ranges_bitmap(child, bitmap);
    447 		continue;
    448 	}
    449 	return bitmap;
    450 }
    451 #endif /* !macppc && PPC_OEA */
    452 
    453 void
    454 ofwoea_batinit(void)
    455 {
    456 #if defined (PPC_OEA)
    457 
    458 #ifdef macppc
    459 	/*
    460 	 * cover PCI and register space but not the firmware ROM
    461 	 */
    462 	oea_batinit(0x80000000, BAT_BL_1G,
    463 		    0xf0000000, BAT_BL_128M,
    464 		    0xf8000000, BAT_BL_64M,
    465 		    0xfe000000, BAT_BL_8M,	/* Grackle IO */
    466 		    0);
    467 #else
    468 	uint16_t bitmap;
    469 	int node, i;
    470 
    471 	node = OF_finddevice("/");
    472 
    473 	bitmap = ranges_bitmap(node, 0);
    474 	oea_batinit(0);
    475 
    476 	for (i=1; i < 0x10; i++) {
    477 		/* skip the three vital SR regions */
    478 		if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
    479 			continue;
    480 		if (bitmap & (1 << i)) {
    481 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    482 			DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
    483 		}
    484 	}
    485 #endif
    486 #endif /* OEA */
    487 }
    488 
    489 
    490 /* we define these partially, as we will fill the rest in later */
    491 struct powerpc_bus_space genppc_isa_io_space_tag = {
    492 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    493 	.pbs_base = 0x00000000,
    494 };
    495 
    496 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    497 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    498 	.pbs_base = 0x00000000,
    499 };
    500 
    501 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    502  * Increase if necc.
    503  */
    504 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    505 	__attribute__((aligned(8)));
    506 
    507 
    508 static void
    509 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    510 {
    511 	int node, i, len, reclen;
    512 	u_int32_t parent_acells, acells, scells, map[160];
    513 	char tmp[32];
    514 
    515 	node = base;
    516 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    517 		goto rec;
    518 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    519 	    strcmp("pci", tmp) != 0)
    520 		goto rec;
    521 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    522 		goto rec;
    523 	len = OF_getprop(node, "ranges", map, sizeof(map));
    524 	if (len == -1)
    525 		goto rec;
    526 	if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
    527 	    sizeof(parent_acells)) != sizeof(parent_acells))
    528 		parent_acells = 1;
    529 	if (OF_getprop(node, "#address-cells", &acells,
    530 	    sizeof(acells)) != sizeof(acells))
    531 		acells = 3;
    532 	if (OF_getprop(node, "#size-cells", &scells,
    533 	    sizeof(scells)) != sizeof(scells))
    534 		scells = 2;
    535 #ifdef ofppc
    536 	if (modeldata.ranges_offset == 0)
    537 		scells -= 1;
    538 #endif
    539 	if (type == RANGE_TYPE_ISA)
    540 		reclen = 6;
    541 	else
    542 		reclen = parent_acells + acells + scells;
    543 	/*
    544 	 * There exist ISA buses with empty ranges properties.  This is
    545 	 * known to occur on the Pegasos II machine, and likely others.
    546 	 * According to them, that means that the isa bus is a fake bus, and
    547 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    548 	 * with this, we will set cur to -1 and return.
    549 	 */
    550 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    551 		*cur = -1;
    552 		DPRINTF("Found empty range in isa bus\n");
    553 		return;
    554 	}
    555 
    556 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    557 	switch (type) {
    558 		case RANGE_TYPE_PCI:
    559 		case RANGE_TYPE_FIRSTPCI:
    560 			for (i=0; i < len/(4*reclen); i++) {
    561 				DPRINTF("FOUND PCI RANGE\n");
    562 				regions[*cur].size =
    563 				    map[i*reclen + parent_acells + acells + scells - 1];
    564 				/* skip ranges of size==0 */
    565 				if (regions[*cur].size == 0)
    566 					continue;
    567 				regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
    568 				regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
    569 				(*cur)++;
    570 			}
    571 			break;
    572 		case RANGE_TYPE_ISA:
    573 			for (i=0; i < len/(4*reclen); i++) {
    574 				if (map[i*reclen] == 1)
    575 					regions[*cur].type = RANGE_IO;
    576 				else
    577 					regions[*cur].type = RANGE_MEM;
    578 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    579 					regions[*cur].type);
    580 				regions[*cur].size =
    581 				    map[i*reclen + acells + scells];
    582 				(*cur)++;
    583 			}
    584 			break;
    585 	}
    586 	DPRINTF("returning with CUR=%d\n", *cur);
    587 	return;
    588 rec:
    589 	for (node = OF_child(base); node; node = OF_peer(node)) {
    590 		DPRINTF("RECURSE 1 STEP\n");
    591 		find_ranges(node, regions, cur, type);
    592 		if (*cur == -1)
    593 			return;
    594 	}
    595 }
    596 
    597 static int
    598 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    599 {
    600 	int i, low = 0;
    601 	u_int32_t addr = 0xffffffff;
    602 
    603 	for (i=0; i < nrof; i++) {
    604 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    605 		    ranges[i].addr < addr) {
    606 			low = i;
    607 			addr = ranges[i].addr;
    608 		}
    609 	}
    610 	if (addr == 0xffffffff)
    611 		return -1;
    612 	return low;
    613 }
    614 
    615 /*
    616  * Find a region of memory, and create a bus_space_tag for it.
    617  * Notes:
    618  * For ISA node is ignored.
    619  * node is the starting node.  if -1, we start at / and map everything.
    620  */
    621 
    622 int
    623 ofwoea_map_space(int rangetype, int iomem, int node,
    624     struct powerpc_bus_space *tag, const char *name)
    625 {
    626 	int i, cur, range, nrofholes, error;
    627 	static int exmap=0;
    628 	rangemap_t region, holes[32], list[32];
    629 
    630 	memset(list, 0, sizeof(list));
    631 	memset(&region, 0, sizeof(region));
    632 	cur = 0;
    633 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    634 		node = OF_finddevice("/");
    635 	if (rangetype == RANGE_TYPE_ISA) {
    636 		u_int32_t size = 0;
    637 		rangemap_t regions[32];
    638 
    639 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    640 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    641 		range = 0;
    642 		DPRINTF("LOOKING FOR ISA\n");
    643 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    644 		if (range == 0 || cur == 0)
    645 			return -1; /* no isa stuff found */
    646 		/*
    647 		 * This may be confusing to some.  The ISA ranges property
    648 		 * is supposed to be a set of IO ranges for the ISA bus, but
    649 		 * generally, it's just a set of pci devfunc lists that tell
    650 		 * you to go look at the parent PCI device for the actual
    651 		 * ranges.
    652 		 */
    653 		if (range == -1) {
    654 			/* we found a rangeless isa bus */
    655 			if (iomem == RANGE_IO)
    656 				size = 0x10000;
    657 			else
    658 				size = 0x1000000;
    659 		}
    660 		DPRINTF("found isa stuff\n");
    661 		for (i=0; i < range; i++)
    662 			if (regions[i].type == iomem)
    663 				size = regions[i].size;
    664 		if (iomem == RANGE_IO) {
    665 			/* the first io range is the one */
    666 			for (i=0; i < cur; i++)
    667 				if (list[i].type == RANGE_IO && size) {
    668 					DPRINTF("found IO\n");
    669 					tag->pbs_offset = list[i].addr;
    670 					tag->pbs_limit = size;
    671 					error = bus_space_init(tag, name,
    672 					    ex_storage[exmap],
    673 					    sizeof(ex_storage[exmap]));
    674 					exmap++;
    675 					return error;
    676 				}
    677 		} else {
    678 			for (i=0; i < cur; i++)
    679 				if (list[i].type == RANGE_MEM &&
    680 				    list[i].size == size) {
    681 					DPRINTF("found mem\n");
    682 					tag->pbs_offset = list[i].addr;
    683 					tag->pbs_limit = size;
    684 					error = bus_space_init(tag, name,
    685 					    ex_storage[exmap],
    686 					    sizeof(ex_storage[exmap]));
    687 					exmap++;
    688 					return error;
    689 				}
    690 		}
    691 		return -1; /* NO ISA FOUND */
    692 	}
    693 	find_ranges(node, list, &cur, rangetype);
    694 
    695 	DPRINTF("cur == %d\n", cur);
    696 	/* now list should contain a list of memory regions */
    697 	for (i=0; i < cur; i++)
    698 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    699 		    list[i].size, list[i].type);
    700 
    701 	range = find_lowest_range(list, cur, iomem);
    702 	i = 0;
    703 	nrofholes = 0;
    704 	while (range != -1) {
    705 		DPRINTF("range==%d\n", range);
    706 		DPRINTF("i==%d\n", i);
    707 		if (i == 0) {
    708 			memcpy(&region, &list[range], sizeof(rangemap_t));
    709 			list[range].addr = 0;
    710 			i++;
    711 			range = find_lowest_range(list, cur, iomem);
    712 			continue;
    713 		}
    714 		if (region.addr + region.size < list[range].addr) {
    715 			/* allocate a hole */
    716 			holes[nrofholes].type = iomem;
    717 			holes[nrofholes].addr = region.size + region.addr;
    718 			holes[nrofholes].size = list[range].addr -
    719 			    holes[nrofholes].addr - 1;
    720 			nrofholes++;
    721 		}
    722 		region.size = list[range].size + list[range].addr -
    723 		    region.addr;
    724 		list[range].addr = 0;
    725 		range = find_lowest_range(list, cur, iomem);
    726 	}
    727 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    728 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    729 		    region.size, region.type);
    730 	DPRINTF("HOLES FOUND\n");
    731 	for (i=0; i < nrofholes; i++)
    732 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    733 		    holes[i].size, holes[i].type);
    734 	/* AT THIS POINT WE MAP IT */
    735 
    736 	if (rangetype == RANGE_TYPE_PCI) {
    737 		if (exmap == EXSTORAGE_MAX)
    738 			panic("Not enough ex_storage space. "
    739 			    "Increase EXSTORAGE_MAX");
    740 
    741 		/* XXX doing this in here might be wrong */
    742 		if (iomem == 1) {
    743 			/* we map an IO region */
    744 			tag->pbs_offset = region.addr;
    745 			tag->pbs_base = 0;
    746 			tag->pbs_limit = region.size;
    747 		} else {
    748 			/* ... or a memory region */
    749 			tag->pbs_offset = 0;
    750 			tag->pbs_base = region.addr;
    751 			tag->pbs_limit = region.size + region.addr;
    752 		}
    753 
    754 		error = bus_space_init(tag, name, ex_storage[exmap],
    755 		    sizeof(ex_storage[exmap]));
    756 		exmap++;
    757 		if (error)
    758 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    759 		for (i=0; i < nrofholes; i++) {
    760 			if (holes[i].type == RANGE_IO) {
    761 				error = extent_alloc_region(tag->pbs_extent,
    762 				    holes[i].addr - tag->pbs_offset,
    763 				    holes[i].size, EX_NOWAIT);
    764 			} else {
    765 				error = extent_alloc_region(tag->pbs_extent,
    766 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    767 			}
    768 			if (error)
    769 				panic("ofwoea_bus_space_init: can't block out"
    770 				    " reserved space 0x%x-0x%x: error=%d",
    771 				    holes[i].addr, holes[i].addr+holes[i].size,
    772 				    error);
    773 		}
    774 		return error;
    775 	}
    776 	return -1;
    777 }
    778 
    779 void
    780 ofwoea_bus_space_init(void)
    781 {
    782 	int error;
    783 
    784 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    785 	    &genppc_isa_io_space_tag, "isa-ioport");
    786 	if (error > 0)
    787 		panic("Could not map ISA IO");
    788 
    789 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    790 	    &genppc_isa_mem_space_tag, "isa-iomem");
    791 	if (error > 0)
    792 		panic("Could not map ISA MEM");
    793 }
    794