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