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