Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.33
      1 /* $NetBSD: ofwoea_machdep.c,v 1.33 2013/05/13 00:12:01 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tim Rightnour
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.33 2013/05/13 00:12:01 macallan Exp $");
     34 
     35 #include "opt_ppcarch.h"
     36 #include "opt_compat_netbsd.h"
     37 #include "opt_ddb.h"
     38 #include "opt_kgdb.h"
     39 #include "opt_ipkdb.h"
     40 #include "opt_modular.h"
     41 
     42 #include "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 #if defined (PPC_OEA64_BRIDGE) && defined (PPC_OEA)
    205 	if (oeacpufeat & OEACPU_64_BRIDGE)
    206 		pmap_setup64bridge();
    207 	else
    208 		pmap_setup32();
    209 #endif
    210 
    211 	oea_init(pic_ext_intr);
    212 
    213 	ofmaplen = save_ofmap(NULL, 0);
    214 	if (ofmaplen > 0)
    215 		save_ofmap(ofmap, ofmaplen);
    216 
    217 /*
    218  * XXX
    219  * we need to do this here instead of earlier on in ofwinit() for some reason
    220  * At least some versions of Apple OF 2.0.1 hang if we do this earlier
    221  */
    222 	ofwmsr &= ~PSL_IP;
    223 
    224 	/* Parse the args string */
    225 	if (args) {
    226 		strcpy(bootpath, args);
    227 		args = bootpath;
    228 		while (*++args && *args != ' ');
    229 		if (*args) {
    230 			*args++ = 0;
    231 			while (*args)
    232 				BOOT_FLAG(*args++, boothowto);
    233 		}
    234 	}
    235 
    236 	uvm_setpagesize();
    237 
    238 	pmap_bootstrap(startkernel, endkernel);
    239 
    240 /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    241 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    242 #if defined (PMAC_G5)
    243 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    244 	if (oeacpufeat & OEACPU_64_BRIDGE) {
    245 		vaddr_t va;
    246 		paddr_t pa;
    247 		int i;
    248 
    249 		pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
    250 		    round_page(MSGBUFSIZE), 0x0);
    251 
    252 		/* Map OFW code+data */
    253 
    254 		for (i = 0; i < ofmaplen / sizeof(struct ofw_translations); i++) {
    255 			if (ofmap[i].va < 0xff800000)
    256 				continue;
    257 
    258 			for (va = ofmap[i].va, pa = ofmap[i].pa;
    259 			    va < ofmap[i].va + ofmap[i].len;
    260 			    va += PAGE_SIZE, pa += PAGE_SIZE) {
    261 				pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
    262 				    VM_PROT_ALL | PMAP_WIRED);
    263 			}
    264 		}
    265 
    266 #if NWSDISPLAY > 0
    267 		/* Map video frame buffer */
    268 
    269 		struct rasops_info *ri = &rascons_console_screen.scr_ri;
    270 
    271 		if (ri->ri_bits != NULL) {
    272 			for (va = (vaddr_t) ri->ri_bits;
    273 			    va < round_page((vaddr_t) ri->ri_bits +
    274 				ri->ri_height * ri->ri_stride);
    275 			    va += PAGE_SIZE) {
    276 				pmap_enter(pmap_kernel(), va, va,
    277 				    VM_PROT_READ | VM_PROT_WRITE,
    278 				    PMAP_NOCACHE | PMAP_WIRED);
    279 			}
    280 		}
    281 #endif
    282 	}
    283 #elif defined (MAMBO)
    284 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    285 	if (oeacpufeat & OEACPU_64_BRIDGE)
    286 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    287 #endif /* PMAC_G5 */
    288 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    289 
    290 	/* Now enable translation (and machine checks/recoverable interrupts) */
    291 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    292 	    : "=r"(scratch)
    293 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    294 
    295 	restore_ofmap(ofmap, ofmaplen);
    296 
    297 #if NKSYMS || defined(DDB) || defined(MODULAR)
    298 	ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    299 #endif
    300 
    301 	/* CPU clock stuff */
    302 	set_timebase();
    303 
    304 #ifdef DDB
    305 	if (boothowto & RB_KDB)
    306 		Debugger();
    307 #endif
    308 }
    309 
    310 void
    311 set_timebase(void)
    312 {
    313 	int qhandle, phandle, msr, scratch;
    314 	char type[32];
    315 
    316 	if (timebase_freq != 0) {
    317 		ticks_per_sec = timebase_freq;
    318 		goto found;
    319 	}
    320 
    321 	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    322 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    323 		    && strcmp(type, "cpu") == 0
    324 		    && OF_getprop(qhandle, "timebase-frequency",
    325 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    326 			goto found;
    327 		}
    328 		if ((phandle = OF_child(qhandle)))
    329 			continue;
    330 		while (qhandle) {
    331 			if ((phandle = OF_peer(qhandle)))
    332 				break;
    333 			qhandle = OF_parent(qhandle);
    334 		}
    335 	}
    336 	panic("no cpu node");
    337 
    338 found:
    339 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    340 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    341 	ns_per_tick = 1000000000 / ticks_per_sec;
    342 	ticks_per_intr = ticks_per_sec / hz;
    343 	cpu_timebase = ticks_per_sec;
    344 	curcpu()->ci_lasttb = mftbl();
    345 	mtspr(SPR_DEC, ticks_per_intr);
    346 	mtmsr(msr);
    347 }
    348 
    349 static int
    350 save_ofmap(struct ofw_translations *map, int maxlen)
    351 {
    352 	int mmui, mmu, len;
    353 
    354 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    355 	mmu = OF_instance_to_package(mmui);
    356 
    357 	if (map) {
    358 		memset(map, 0, maxlen); /* to be safe */
    359 		len = OF_getprop(mmu, "translations", map, maxlen);
    360 	} else
    361 		len = OF_getproplen(mmu, "translations");
    362 
    363 	if (len < 0)
    364 		len = 0;
    365 	return len;
    366 }
    367 
    368 
    369 /* The PMAC_G5 code here needs to be replaced by code that looks for the
    370    size_cells and does the right thing automatically.
    371 */
    372 void
    373 restore_ofmap(struct ofw_translations *map, int len)
    374 {
    375 	int n = len / sizeof(struct ofw_translations);
    376 	int i;
    377 
    378 	pmap_pinit(&ofw_pmap);
    379 
    380 	ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
    381 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
    382 
    383 #ifdef KERNEL2_SR
    384 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
    385 #endif
    386 
    387 	for (i = 0; i < n; i++) {
    388 #if defined (PMAC_G5)
    389 		register64_t pa = map[i].pa;
    390 #else
    391 		register_t pa = map[i].pa;
    392 #endif
    393 		vaddr_t va = map[i].va;
    394 		size_t length = map[i].len;
    395 
    396 		if (va < 0xf0000000) /* XXX */
    397 			continue;
    398 
    399 		while (length > 0) {
    400 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    401 			    VM_PROT_ALL|PMAP_WIRED);
    402 			pa += PAGE_SIZE;
    403 			va += PAGE_SIZE;
    404 			length -= PAGE_SIZE;
    405 		}
    406 	}
    407 	pmap_update(&ofw_pmap);
    408 }
    409 
    410 
    411 
    412 /*
    413  * Scan the device tree for ranges, and return them as bitmap 0..15
    414  */
    415 #ifndef macppc
    416 static u_int16_t
    417 ranges_bitmap(int node, u_int16_t bitmap)
    418 {
    419 	int child, mlen, acells, scells, reclen, i, j;
    420 	u_int32_t addr, len, map[160];
    421 
    422 	for (child = OF_child(node); child; child = OF_peer(child)) {
    423 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    424 		if (mlen == -1)
    425 			goto noranges;
    426 
    427 		j = OF_getprop(child, "#address-cells", &acells,
    428 		    sizeof(acells));
    429 		if (j == -1)
    430 			goto noranges;
    431 
    432 		j = OF_getprop(child, "#size-cells", &scells,
    433 		    sizeof(scells));
    434 		if (j == -1)
    435 			goto noranges;
    436 
    437 #ifdef ofppc
    438 		reclen = acells + modeldata.ranges_offset + scells;
    439 #else
    440 		reclen = acells + 1 + scells;
    441 #endif
    442 
    443 		for (i=0; i < (mlen/4)/reclen; i++) {
    444 			addr = map[reclen * i + acells];
    445 			len = map[reclen * i + reclen - 1];
    446 			for (j = 0; j < len / 0x10000000; j++)
    447 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    448 			bitmap |= 1 << (addr >> 28);
    449 		}
    450 noranges:
    451 		bitmap |= ranges_bitmap(child, bitmap);
    452 		continue;
    453 	}
    454 	return bitmap;
    455 }
    456 #endif /* !macppc */
    457 
    458 void
    459 ofwoea_batinit(void)
    460 {
    461 #if defined (PPC_OEA)
    462 
    463 #ifdef macppc
    464 	/*
    465 	 * cover PCI and register space but not the firmware ROM
    466 	 */
    467 	oea_batinit(0x80000000, BAT_BL_1G,
    468 		    0xf0000000, BAT_BL_128M,
    469 		    0xf8000000, BAT_BL_64M,
    470 		    0xfe000000, BAT_BL_8M,	/* Grackle IO */
    471 		    0);
    472 #else
    473 	uint16_t bitmap;
    474 	int node, i;
    475 
    476 	node = OF_finddevice("/");
    477 
    478 	bitmap = ranges_bitmap(node, 0);
    479 	oea_batinit(0);
    480 
    481 	for (i=1; i < 0x10; i++) {
    482 		/* skip the three vital SR regions */
    483 		if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR)
    484 			continue;
    485 		if (bitmap & (1 << i)) {
    486 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    487 			DPRINTF("Batmapped 256M at 0x%x\n", 0x10000000 * i);
    488 		}
    489 	}
    490 #endif
    491 #endif /* OEA */
    492 }
    493 
    494 
    495 /* we define these partially, as we will fill the rest in later */
    496 struct powerpc_bus_space genppc_isa_io_space_tag = {
    497 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    498 	.pbs_base = 0x00000000,
    499 };
    500 
    501 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    502 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    503 	.pbs_base = 0x00000000,
    504 };
    505 
    506 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    507  * Increase if necc.
    508  */
    509 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    510 	__attribute__((aligned(8)));
    511 
    512 
    513 static void
    514 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    515 {
    516 	int node, i, len, reclen;
    517 	u_int32_t parent_acells, acells, scells, map[160];
    518 	char tmp[32];
    519 
    520 	node = base;
    521 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    522 		goto rec;
    523 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    524 	    strcmp("pci", tmp) != 0)
    525 		goto rec;
    526 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    527 		goto rec;
    528 	len = OF_getprop(node, "ranges", map, sizeof(map));
    529 	if (len == -1)
    530 		goto rec;
    531 	if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
    532 	    sizeof(parent_acells)) != sizeof(parent_acells))
    533 		parent_acells = 1;
    534 	if (OF_getprop(node, "#address-cells", &acells,
    535 	    sizeof(acells)) != sizeof(acells))
    536 		acells = 3;
    537 	if (OF_getprop(node, "#size-cells", &scells,
    538 	    sizeof(scells)) != sizeof(scells))
    539 		scells = 2;
    540 #ifdef ofppc
    541 	if (modeldata.ranges_offset == 0)
    542 		scells -= 1;
    543 #endif
    544 	if (type == RANGE_TYPE_ISA)
    545 		reclen = 6;
    546 	else
    547 		reclen = parent_acells + acells + scells;
    548 	/*
    549 	 * There exist ISA buses with empty ranges properties.  This is
    550 	 * known to occur on the Pegasos II machine, and likely others.
    551 	 * According to them, that means that the isa bus is a fake bus, and
    552 	 * the real maps are the PCI maps of the preceeding bus.  To deal
    553 	 * with this, we will set cur to -1 and return.
    554 	 */
    555 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    556 		*cur = -1;
    557 		DPRINTF("Found empty range in isa bus\n");
    558 		return;
    559 	}
    560 
    561 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    562 	switch (type) {
    563 		case RANGE_TYPE_PCI:
    564 		case RANGE_TYPE_FIRSTPCI:
    565 			for (i=0; i < len/(4*reclen); i++) {
    566 				DPRINTF("FOUND PCI RANGE\n");
    567 				regions[*cur].size =
    568 				    map[i*reclen + parent_acells + acells + scells - 1];
    569 				/* skip ranges of size==0 */
    570 				if (regions[*cur].size == 0)
    571 					continue;
    572 				regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
    573 				regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
    574 				(*cur)++;
    575 			}
    576 			break;
    577 		case RANGE_TYPE_ISA:
    578 			for (i=0; i < len/(4*reclen); i++) {
    579 				if (map[i*reclen] == 1)
    580 					regions[*cur].type = RANGE_IO;
    581 				else
    582 					regions[*cur].type = RANGE_MEM;
    583 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    584 					regions[*cur].type);
    585 				regions[*cur].size =
    586 				    map[i*reclen + acells + scells];
    587 				(*cur)++;
    588 			}
    589 			break;
    590 	}
    591 	DPRINTF("returning with CUR=%d\n", *cur);
    592 	return;
    593 rec:
    594 	for (node = OF_child(base); node; node = OF_peer(node)) {
    595 		DPRINTF("RECURSE 1 STEP\n");
    596 		find_ranges(node, regions, cur, type);
    597 		if (*cur == -1)
    598 			return;
    599 	}
    600 }
    601 
    602 static int
    603 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    604 {
    605 	int i, low = 0;
    606 	u_int32_t addr = 0xffffffff;
    607 
    608 	for (i=0; i < nrof; i++) {
    609 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    610 		    ranges[i].addr < addr) {
    611 			low = i;
    612 			addr = ranges[i].addr;
    613 		}
    614 	}
    615 	if (addr == 0xffffffff)
    616 		return -1;
    617 	return low;
    618 }
    619 
    620 /*
    621  * Find a region of memory, and create a bus_space_tag for it.
    622  * Notes:
    623  * For ISA node is ignored.
    624  * node is the starting node.  if -1, we start at / and map everything.
    625  */
    626 
    627 int
    628 ofwoea_map_space(int rangetype, int iomem, int node,
    629     struct powerpc_bus_space *tag, const char *name)
    630 {
    631 	int i, cur, range, nrofholes, error;
    632 	static int exmap=0;
    633 	u_int32_t addr;
    634 	rangemap_t region, holes[32], list[32];
    635 
    636 	memset(list, 0, sizeof(list));
    637 	memset(&region, 0, sizeof(region));
    638 	cur = 0;
    639 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    640 		node = OF_finddevice("/");
    641 	if (rangetype == RANGE_TYPE_ISA) {
    642 		u_int32_t size = 0;
    643 		rangemap_t regions[32];
    644 
    645 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    646 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    647 		range = 0;
    648 		DPRINTF("LOOKING FOR ISA\n");
    649 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    650 		if (range == 0 || cur == 0)
    651 			return -1; /* no isa stuff found */
    652 		/*
    653 		 * This may be confusing to some.  The ISA ranges property
    654 		 * is supposed to be a set of IO ranges for the ISA bus, but
    655 		 * generally, it's just a set of pci devfunc lists that tell
    656 		 * you to go look at the parent PCI device for the actual
    657 		 * ranges.
    658 		 */
    659 		if (range == -1) {
    660 			/* we found a rangeless isa bus */
    661 			if (iomem == RANGE_IO)
    662 				size = 0x10000;
    663 			else
    664 				size = 0x1000000;
    665 		}
    666 		DPRINTF("found isa stuff\n");
    667 		for (i=0; i < range; i++)
    668 			if (regions[i].type == iomem)
    669 				size = regions[i].size;
    670 		if (iomem == RANGE_IO) {
    671 			/* the first io range is the one */
    672 			for (i=0; i < cur; i++)
    673 				if (list[i].type == RANGE_IO && size) {
    674 					DPRINTF("found IO\n");
    675 					tag->pbs_offset = list[i].addr;
    676 					tag->pbs_limit = size;
    677 					error = bus_space_init(tag, name,
    678 					    ex_storage[exmap],
    679 					    sizeof(ex_storage[exmap]));
    680 					exmap++;
    681 					return error;
    682 				}
    683 		} else {
    684 			for (i=0; i < cur; i++)
    685 				if (list[i].type == RANGE_MEM &&
    686 				    list[i].size == size) {
    687 					DPRINTF("found mem\n");
    688 					tag->pbs_offset = list[i].addr;
    689 					tag->pbs_limit = size;
    690 					error = bus_space_init(tag, name,
    691 					    ex_storage[exmap],
    692 					    sizeof(ex_storage[exmap]));
    693 					exmap++;
    694 					return error;
    695 				}
    696 		}
    697 		return -1; /* NO ISA FOUND */
    698 	}
    699 	find_ranges(node, list, &cur, rangetype);
    700 
    701 	DPRINTF("cur == %d\n", cur);
    702 	/* now list should contain a list of memory regions */
    703 	for (i=0; i < cur; i++)
    704 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    705 		    list[i].size, list[i].type);
    706 
    707 	addr=0;
    708 	range = find_lowest_range(list, cur, iomem);
    709 	i = 0;
    710 	nrofholes = 0;
    711 	while (range != -1) {
    712 		DPRINTF("range==%d\n", range);
    713 		DPRINTF("i==%d\n", i);
    714 		if (i == 0) {
    715 			memcpy(&region, &list[range], sizeof(rangemap_t));
    716 			list[range].addr = 0;
    717 			i++;
    718 			range = find_lowest_range(list, cur, iomem);
    719 			continue;
    720 		}
    721 		if (region.addr + region.size < list[range].addr) {
    722 			/* allocate a hole */
    723 			holes[nrofholes].type = iomem;
    724 			holes[nrofholes].addr = region.size + region.addr;
    725 			holes[nrofholes].size = list[range].addr -
    726 			    holes[nrofholes].addr - 1;
    727 			nrofholes++;
    728 		}
    729 		region.size = list[range].size + list[range].addr -
    730 		    region.addr;
    731 		list[range].addr = 0;
    732 		range = find_lowest_range(list, cur, iomem);
    733 	}
    734 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    735 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    736 		    region.size, region.type);
    737 	DPRINTF("HOLES FOUND\n");
    738 	for (i=0; i < nrofholes; i++)
    739 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    740 		    holes[i].size, holes[i].type);
    741 	/* AT THIS POINT WE MAP IT */
    742 
    743 	if (rangetype == RANGE_TYPE_PCI) {
    744 		if (exmap == EXSTORAGE_MAX)
    745 			panic("Not enough ex_storage space. "
    746 			    "Increase EXSTORAGE_MAX");
    747 
    748 		/* XXX doing this in here might be wrong */
    749 		if (iomem == 1) {
    750 			/* we map an IO region */
    751 			tag->pbs_offset = region.addr;
    752 			tag->pbs_base = 0;
    753 			tag->pbs_limit = region.size;
    754 		} else {
    755 			/* ... or a memory region */
    756 			tag->pbs_offset = 0;
    757 			tag->pbs_base = region.addr;
    758 			tag->pbs_limit = region.size + region.addr;
    759 		}
    760 
    761 		error = bus_space_init(tag, name, ex_storage[exmap],
    762 		    sizeof(ex_storage[exmap]));
    763 		exmap++;
    764 		if (error)
    765 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    766 		for (i=0; i < nrofholes; i++) {
    767 			if (holes[i].type == RANGE_IO) {
    768 				error = extent_alloc_region(tag->pbs_extent,
    769 				    holes[i].addr - tag->pbs_offset,
    770 				    holes[i].size, EX_NOWAIT);
    771 			} else {
    772 				error = extent_alloc_region(tag->pbs_extent,
    773 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    774 			}
    775 			if (error)
    776 				panic("ofwoea_bus_space_init: can't block out"
    777 				    " reserved space 0x%x-0x%x: error=%d",
    778 				    holes[i].addr, holes[i].addr+holes[i].size,
    779 				    error);
    780 		}
    781 		return error;
    782 	}
    783 	return -1;
    784 }
    785 
    786 void
    787 ofwoea_bus_space_init(void)
    788 {
    789 	int error;
    790 
    791 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    792 	    &genppc_isa_io_space_tag, "isa-ioport");
    793 	if (error > 0)
    794 		panic("Could not map ISA IO");
    795 
    796 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    797 	    &genppc_isa_mem_space_tag, "isa-iomem");
    798 	if (error > 0)
    799 		panic("Could not map ISA MEM");
    800 }
    801