Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.1.2.4
      1  1.1.2.4  macallan /* $NetBSD: ofwoea_machdep.c,v 1.1.2.4 2007/06/18 03:31:33 macallan Exp $ */
      2  1.1.2.1   garbled 
      3  1.1.2.1   garbled /*-
      4  1.1.2.1   garbled  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1.2.1   garbled  * All rights reserved.
      6  1.1.2.1   garbled  *
      7  1.1.2.1   garbled  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1   garbled  * by Tim Rightnour
      9  1.1.2.1   garbled  *
     10  1.1.2.1   garbled  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1   garbled  * modification, are permitted provided that the following conditions
     12  1.1.2.1   garbled  * are met:
     13  1.1.2.1   garbled  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1   garbled  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1   garbled  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1   garbled  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1   garbled  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1   garbled  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.1   garbled  *    must display the following acknowledgement:
     20  1.1.2.1   garbled  *        This product includes software developed by the NetBSD
     21  1.1.2.1   garbled  *        Foundation, Inc. and its contributors.
     22  1.1.2.1   garbled  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.1   garbled  *    contributors may be used to endorse or promote products derived
     24  1.1.2.1   garbled  *    from this software without specific prior written permission.
     25  1.1.2.1   garbled  *
     26  1.1.2.1   garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.1   garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.1   garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.1   garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.1   garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.1   garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.1   garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.1   garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.1   garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.1   garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.1   garbled  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.1   garbled  */
     38  1.1.2.1   garbled 
     39  1.1.2.1   garbled #include <sys/cdefs.h>
     40  1.1.2.4  macallan __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.1.2.4 2007/06/18 03:31:33 macallan Exp $");
     41  1.1.2.1   garbled 
     42  1.1.2.1   garbled 
     43  1.1.2.1   garbled #include "opt_compat_netbsd.h"
     44  1.1.2.1   garbled #include "opt_ddb.h"
     45  1.1.2.1   garbled #include "opt_kgdb.h"
     46  1.1.2.1   garbled #include "opt_ipkdb.h"
     47  1.1.2.1   garbled 
     48  1.1.2.1   garbled #include <sys/param.h>
     49  1.1.2.1   garbled #include <sys/buf.h>
     50  1.1.2.1   garbled #include <sys/boot_flag.h>
     51  1.1.2.1   garbled #include <sys/extent.h>
     52  1.1.2.1   garbled #include <sys/kernel.h>
     53  1.1.2.1   garbled 
     54  1.1.2.1   garbled #include <uvm/uvm_extern.h>
     55  1.1.2.1   garbled 
     56  1.1.2.1   garbled #include <dev/ofw/openfirm.h>
     57  1.1.2.1   garbled #include <machine/pmap.h>
     58  1.1.2.1   garbled #include <machine/powerpc.h>
     59  1.1.2.1   garbled #include <machine/trap.h>
     60  1.1.2.1   garbled #include <powerpc/bus.h>
     61  1.1.2.1   garbled #include <powerpc/oea/bat.h>
     62  1.1.2.1   garbled #include <powerpc/ofw_bus.h>
     63  1.1.2.1   garbled #include <powerpc/ofw_cons.h>
     64  1.1.2.1   garbled #include <powerpc/spr.h>
     65  1.1.2.1   garbled #include <arch/powerpc/pic/picvar.h>
     66  1.1.2.1   garbled 
     67  1.1.2.1   garbled #ifdef DDB
     68  1.1.2.1   garbled #include <machine/db_machdep.h>
     69  1.1.2.1   garbled #include <ddb/db_extern.h>
     70  1.1.2.1   garbled #endif
     71  1.1.2.1   garbled 
     72  1.1.2.1   garbled #ifdef KGDB
     73  1.1.2.1   garbled #include <sys/kgdb.h>
     74  1.1.2.1   garbled #endif
     75  1.1.2.1   garbled 
     76  1.1.2.1   garbled #ifdef IPKDB
     77  1.1.2.1   garbled #include <ipkdb/ipkdb.h>
     78  1.1.2.1   garbled #endif
     79  1.1.2.1   garbled 
     80  1.1.2.1   garbled 
     81  1.1.2.1   garbled typedef struct _rangemap {
     82  1.1.2.1   garbled 	u_int32_t addr;
     83  1.1.2.1   garbled 	u_int32_t size;
     84  1.1.2.1   garbled 	int type;
     85  1.1.2.1   garbled } rangemap_t;
     86  1.1.2.1   garbled 
     87  1.1.2.1   garbled struct ofw_translations {
     88  1.1.2.1   garbled 	vaddr_t va;
     89  1.1.2.1   garbled 	int len;
     90  1.1.2.1   garbled #if defined (PMAC_G5)
     91  1.1.2.1   garbled 	register64_t pa;
     92  1.1.2.1   garbled #else
     93  1.1.2.1   garbled 	register_t pa;
     94  1.1.2.1   garbled #endif
     95  1.1.2.1   garbled 	int mode;
     96  1.1.2.1   garbled }__attribute__((packed));
     97  1.1.2.1   garbled 
     98  1.1.2.1   garbled struct pmap ofw_pmap;
     99  1.1.2.1   garbled struct ofw_translations ofmap[32];
    100  1.1.2.1   garbled char bootpath[256];
    101  1.1.2.1   garbled #if NKSYMS || defined(DDB) || defined(LKM)
    102  1.1.2.1   garbled void *startsym, *endsym;
    103  1.1.2.1   garbled #endif
    104  1.1.2.1   garbled #ifdef TIMEBASE_FREQ
    105  1.1.2.1   garbled u_int timebase_freq = TIMEBASE_FREQ;
    106  1.1.2.1   garbled #else
    107  1.1.2.1   garbled u_int timebase_freq = 0;
    108  1.1.2.1   garbled #endif
    109  1.1.2.1   garbled 
    110  1.1.2.1   garbled extern int ofmsr;
    111  1.1.2.1   garbled extern int chosen;
    112  1.1.2.1   garbled extern uint32_t ticks_per_sec;
    113  1.1.2.1   garbled extern uint32_t ns_per_tick;
    114  1.1.2.1   garbled extern uint32_t ticks_per_intr;
    115  1.1.2.1   garbled 
    116  1.1.2.1   garbled static int save_ofmap(struct ofw_translations *, int);
    117  1.1.2.1   garbled static void restore_ofmap(struct ofw_translations *, int);
    118  1.1.2.1   garbled static void set_timebase(void);
    119  1.1.2.1   garbled 
    120  1.1.2.1   garbled void
    121  1.1.2.1   garbled ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    122  1.1.2.1   garbled {
    123  1.1.2.1   garbled 	int ofmaplen;
    124  1.1.2.1   garbled #if defined (PPC_OEA64_BRIDGE)
    125  1.1.2.1   garbled 	register_t scratch;
    126  1.1.2.1   garbled #endif
    127  1.1.2.1   garbled 
    128  1.1.2.1   garbled #if defined (PPC_OEA)
    129  1.1.2.1   garbled 	/* initialze bats */
    130  1.1.2.1   garbled 	ofwoea_batinit();
    131  1.1.2.1   garbled #elif defined (PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    132  1.1.2.1   garbled #endif /* PPC_OEA */
    133  1.1.2.1   garbled 
    134  1.1.2.1   garbled #if NKSYMS || defined(DDB) || defined(LKM)
    135  1.1.2.1   garbled 	/* get info of kernel symbol table from bootloader */
    136  1.1.2.1   garbled 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    137  1.1.2.1   garbled 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    138  1.1.2.1   garbled 	    sizeof(endsym));
    139  1.1.2.1   garbled 	if (startsym == NULL || endsym == NULL)
    140  1.1.2.1   garbled 	    startsym = endsym = NULL;
    141  1.1.2.1   garbled #endif
    142  1.1.2.1   garbled 
    143  1.1.2.1   garbled 	ofwoea_bus_space_init();
    144  1.1.2.1   garbled 
    145  1.1.2.1   garbled 	ofwoea_consinit();
    146  1.1.2.1   garbled 
    147  1.1.2.1   garbled 	oea_init(pic_ext_intr);
    148  1.1.2.1   garbled 
    149  1.1.2.1   garbled 	ofmaplen = save_ofmap(NULL, 0);
    150  1.1.2.1   garbled 	if (ofmaplen > 0)
    151  1.1.2.1   garbled 		save_ofmap(ofmap, ofmaplen);
    152  1.1.2.1   garbled 
    153  1.1.2.1   garbled 	ofmsr &= ~PSL_IP;
    154  1.1.2.1   garbled 
    155  1.1.2.1   garbled 	/* Parse the args string */
    156  1.1.2.1   garbled 	if (args) {
    157  1.1.2.1   garbled 		strcpy(bootpath, args);
    158  1.1.2.1   garbled 		args = bootpath;
    159  1.1.2.1   garbled 		while (*++args && *args != ' ');
    160  1.1.2.1   garbled 		if (*args) {
    161  1.1.2.1   garbled 			*args++ = 0;
    162  1.1.2.1   garbled 			while (*args)
    163  1.1.2.1   garbled 				BOOT_FLAG(*args++, boothowto);
    164  1.1.2.1   garbled 		}
    165  1.1.2.1   garbled 	}
    166  1.1.2.1   garbled 
    167  1.1.2.1   garbled 	uvm_setpagesize();
    168  1.1.2.1   garbled 	pmap_bootstrap(startkernel, endkernel);
    169  1.1.2.1   garbled 
    170  1.1.2.1   garbled #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    171  1.1.2.1   garbled #if defined (PMAC_G5)
    172  1.1.2.1   garbled 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    173  1.1.2.1   garbled 	pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000, 0x0);
    174  1.1.2.1   garbled #elif defined (MAMBO)
    175  1.1.2.1   garbled 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    176  1.1.2.1   garbled 	pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    177  1.1.2.1   garbled #endif /* PMAC_G5 */
    178  1.1.2.1   garbled 
    179  1.1.2.1   garbled 	/* Now enable translation (and machine checks/recoverable interrupts) */
    180  1.1.2.1   garbled 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    181  1.1.2.1   garbled 	    : "=r"(scratch)
    182  1.1.2.1   garbled 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    183  1.1.2.1   garbled #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    184  1.1.2.1   garbled 
    185  1.1.2.1   garbled 	restore_ofmap(ofmap, ofmaplen);
    186  1.1.2.1   garbled 
    187  1.1.2.1   garbled 	/* CPU clock stuff */
    188  1.1.2.1   garbled 	set_timebase();
    189  1.1.2.1   garbled }
    190  1.1.2.1   garbled 
    191  1.1.2.1   garbled void
    192  1.1.2.1   garbled set_timebase(void)
    193  1.1.2.1   garbled {
    194  1.1.2.1   garbled 	int qhandle, phandle, msr, scratch;
    195  1.1.2.1   garbled 	char type[32];
    196  1.1.2.1   garbled 
    197  1.1.2.1   garbled 	if (timebase_freq != 0) {
    198  1.1.2.1   garbled 		ticks_per_sec = timebase_freq;
    199  1.1.2.1   garbled 		goto found;
    200  1.1.2.1   garbled 	}
    201  1.1.2.1   garbled 
    202  1.1.2.1   garbled 	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    203  1.1.2.1   garbled 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    204  1.1.2.1   garbled 		    && strcmp(type, "cpu") == 0
    205  1.1.2.1   garbled 		    && OF_getprop(qhandle, "timebase-frequency",
    206  1.1.2.1   garbled 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    207  1.1.2.1   garbled 			goto found;
    208  1.1.2.1   garbled 		}
    209  1.1.2.1   garbled 		if ((phandle = OF_child(qhandle)))
    210  1.1.2.1   garbled 			continue;
    211  1.1.2.1   garbled 		while (qhandle) {
    212  1.1.2.1   garbled 			if ((phandle = OF_peer(qhandle)))
    213  1.1.2.1   garbled 				break;
    214  1.1.2.1   garbled 			qhandle = OF_parent(qhandle);
    215  1.1.2.1   garbled 		}
    216  1.1.2.1   garbled 	}
    217  1.1.2.1   garbled 	panic("no cpu node");
    218  1.1.2.1   garbled 
    219  1.1.2.1   garbled found:
    220  1.1.2.1   garbled 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    221  1.1.2.1   garbled 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    222  1.1.2.1   garbled 	ns_per_tick = 1000000000 / ticks_per_sec;
    223  1.1.2.1   garbled 	ticks_per_intr = ticks_per_sec / hz;
    224  1.1.2.1   garbled 	cpu_timebase = ticks_per_sec;
    225  1.1.2.1   garbled 	curcpu()->ci_lasttb = mftbl();
    226  1.1.2.1   garbled 	mtspr(SPR_DEC, ticks_per_intr);
    227  1.1.2.1   garbled 	mtmsr(msr);
    228  1.1.2.1   garbled }
    229  1.1.2.1   garbled 
    230  1.1.2.1   garbled static int
    231  1.1.2.1   garbled save_ofmap(struct ofw_translations *map, int maxlen)
    232  1.1.2.1   garbled {
    233  1.1.2.1   garbled 	int mmui, mmu, len;
    234  1.1.2.1   garbled 
    235  1.1.2.1   garbled 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    236  1.1.2.1   garbled 	mmu = OF_instance_to_package(mmui);
    237  1.1.2.1   garbled 
    238  1.1.2.1   garbled 	if (map) {
    239  1.1.2.1   garbled 		memset(map, 0, maxlen); /* to be safe */
    240  1.1.2.1   garbled 		len = OF_getprop(mmu, "translations", map, maxlen);
    241  1.1.2.1   garbled 	} else
    242  1.1.2.1   garbled 		len = OF_getproplen(mmu, "translations");
    243  1.1.2.1   garbled 
    244  1.1.2.1   garbled 	if (len < 0)
    245  1.1.2.1   garbled 		len = 0;
    246  1.1.2.1   garbled 	return len;
    247  1.1.2.1   garbled }
    248  1.1.2.1   garbled 
    249  1.1.2.1   garbled void
    250  1.1.2.1   garbled restore_ofmap(struct ofw_translations *map, int len)
    251  1.1.2.1   garbled {
    252  1.1.2.1   garbled 	int n = len / sizeof(struct ofw_translations);
    253  1.1.2.1   garbled 	int i;
    254  1.1.2.1   garbled 
    255  1.1.2.1   garbled 	pmap_pinit(&ofw_pmap);
    256  1.1.2.1   garbled 
    257  1.1.2.1   garbled #if defined(PPC_OEA64_BRIDGE)
    258  1.1.2.1   garbled 	ofw_pmap.pm_sr[0x0] = KERNELN_SEGMENT(0);
    259  1.1.2.1   garbled #endif
    260  1.1.2.1   garbled 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
    261  1.1.2.1   garbled 
    262  1.1.2.1   garbled #ifdef KERNEL2_SR
    263  1.1.2.1   garbled 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
    264  1.1.2.1   garbled #endif
    265  1.1.2.1   garbled 
    266  1.1.2.1   garbled 	for (i = 0; i < n; i++) {
    267  1.1.2.1   garbled #if defined (PMAC_G5)
    268  1.1.2.1   garbled 		register64_t pa = map[i].pa;
    269  1.1.2.1   garbled #else
    270  1.1.2.1   garbled 		register_t pa = map[i].pa;
    271  1.1.2.1   garbled #endif
    272  1.1.2.1   garbled 		vaddr_t va = map[i].va;
    273  1.1.2.1   garbled 		size_t length = map[i].len;
    274  1.1.2.1   garbled 
    275  1.1.2.1   garbled 		if (va < 0xf0000000) /* XXX */
    276  1.1.2.1   garbled 			continue;
    277  1.1.2.1   garbled 
    278  1.1.2.1   garbled 		while (length > 0) {
    279  1.1.2.1   garbled 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    280  1.1.2.1   garbled 			    VM_PROT_ALL|PMAP_WIRED);
    281  1.1.2.1   garbled 			pa += PAGE_SIZE;
    282  1.1.2.1   garbled 			va += PAGE_SIZE;
    283  1.1.2.1   garbled 			length -= PAGE_SIZE;
    284  1.1.2.1   garbled 		}
    285  1.1.2.1   garbled 	}
    286  1.1.2.1   garbled 	pmap_update(&ofw_pmap);
    287  1.1.2.1   garbled }
    288  1.1.2.1   garbled 
    289  1.1.2.1   garbled 
    290  1.1.2.1   garbled 
    291  1.1.2.1   garbled /*
    292  1.1.2.1   garbled  * Scan the device tree for ranges, and batmap them.
    293  1.1.2.1   garbled  */
    294  1.1.2.1   garbled 
    295  1.1.2.1   garbled static u_int16_t
    296  1.1.2.1   garbled ranges_bitmap(int node, u_int16_t bitmap)
    297  1.1.2.1   garbled {
    298  1.1.2.1   garbled 	int child, mlen, len, acells, scells, reclen, i, addr, j;
    299  1.1.2.1   garbled 	u_int32_t map[160];
    300  1.1.2.1   garbled 
    301  1.1.2.1   garbled 	for (child = OF_child(node); child; child = OF_peer(child)) {
    302  1.1.2.1   garbled 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    303  1.1.2.1   garbled 		if (mlen == -1)
    304  1.1.2.1   garbled 			goto noranges;
    305  1.1.2.1   garbled 		len = OF_getprop(child, "#address-cells", &acells,
    306  1.1.2.1   garbled 		    sizeof(acells));
    307  1.1.2.1   garbled 		if (len == -1)
    308  1.1.2.1   garbled 			goto noranges;
    309  1.1.2.1   garbled 		len = OF_getprop(child, "#size-cells", &scells,
    310  1.1.2.1   garbled 		    sizeof(scells));
    311  1.1.2.1   garbled 		if (len == -1)
    312  1.1.2.1   garbled 			goto noranges;
    313  1.1.2.1   garbled 
    314  1.1.2.3  macallan 		reclen = acells + 1 + scells;
    315  1.1.2.1   garbled 
    316  1.1.2.1   garbled 		for (i=0; i < reclen/(mlen/4); i++) {
    317  1.1.2.3  macallan 			addr = map[reclen * i + acells + 1];
    318  1.1.2.3  macallan 			len = map[reclen * i + reclen - 1];
    319  1.1.2.3  macallan 			for (j = 0; j < len / 0x10000000; j++)
    320  1.1.2.1   garbled 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    321  1.1.2.1   garbled 			bitmap |= 1 << (addr >> 28);
    322  1.1.2.1   garbled 		}
    323  1.1.2.1   garbled 
    324  1.1.2.1   garbled noranges:
    325  1.1.2.1   garbled 		bitmap |= ranges_bitmap(child, bitmap);
    326  1.1.2.1   garbled 		continue;
    327  1.1.2.1   garbled 	}
    328  1.1.2.1   garbled 	return bitmap;
    329  1.1.2.1   garbled }
    330  1.1.2.1   garbled 
    331  1.1.2.1   garbled void
    332  1.1.2.1   garbled ofwoea_batinit(void)
    333  1.1.2.1   garbled {
    334  1.1.2.3  macallan #if 0
    335  1.1.2.3  macallan 	/* this is what macppc used to do */
    336  1.1.2.3  macallan         oea_batinit(0x80000000, BAT_BL_256M, 0xf0000000, BAT_BL_256M,
    337  1.1.2.3  macallan                     0x90000000, BAT_BL_256M, 0xa0000000, BAT_BL_256M,
    338  1.1.2.3  macallan                                 0xb0000000, BAT_BL_256M, 0);
    339  1.1.2.3  macallan #else
    340  1.1.2.3  macallan         u_int16_t bitmap;
    341  1.1.2.1   garbled 	int node, i;
    342  1.1.2.1   garbled 
    343  1.1.2.1   garbled 	node = OF_finddevice("/");
    344  1.1.2.1   garbled 	bitmap = ranges_bitmap(node, 0);
    345  1.1.2.1   garbled 	oea_batinit(0);
    346  1.1.2.1   garbled 
    347  1.1.2.4  macallan 	/* XXX this is a macppc-specific hack */
    348  1.1.2.3  macallan 	bitmap = 0x8f00;
    349  1.1.2.3  macallan 	for (i=1; i < 0x10; i++)
    350  1.1.2.1   garbled 		if (bitmap & (1 << i))
    351  1.1.2.1   garbled 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    352  1.1.2.3  macallan #endif
    353  1.1.2.1   garbled }
    354  1.1.2.1   garbled 
    355  1.1.2.1   garbled 
    356  1.1.2.1   garbled /* we define these partially, as we will fill the rest in later */
    357  1.1.2.1   garbled struct powerpc_bus_space genppc_isa_io_space_tag = {
    358  1.1.2.1   garbled 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    359  1.1.2.1   garbled 	.pbs_base = 0x00000000,
    360  1.1.2.1   garbled };
    361  1.1.2.1   garbled 
    362  1.1.2.1   garbled struct powerpc_bus_space genppc_isa_mem_space_tag = {
    363  1.1.2.1   garbled 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    364  1.1.2.1   garbled 	.pbs_base = 0x00000000,
    365  1.1.2.1   garbled };
    366  1.1.2.1   garbled 
    367  1.1.2.2   garbled /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    368  1.1.2.2   garbled  * Increase if necc.
    369  1.1.2.2   garbled  */
    370  1.1.2.2   garbled static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(8)]
    371  1.1.2.1   garbled 	__attribute__((aligned(8)));
    372  1.1.2.1   garbled 
    373  1.1.2.1   garbled 
    374  1.1.2.1   garbled static void
    375  1.1.2.1   garbled find_ranges(int base, rangemap_t *regions, int *cur, int type)
    376  1.1.2.1   garbled {
    377  1.1.2.3  macallan 	int node, i, len, reclen;
    378  1.1.2.1   garbled 	u_int32_t acells, scells, map[160];
    379  1.1.2.1   garbled 	char tmp[32];
    380  1.1.2.1   garbled 
    381  1.1.2.3  macallan 	node = base;
    382  1.1.2.3  macallan 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    383  1.1.2.3  macallan 		goto rec;
    384  1.1.2.3  macallan 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    385  1.1.2.3  macallan 	    strcmp("pci", tmp) != 0)
    386  1.1.2.3  macallan 		goto rec;
    387  1.1.2.3  macallan 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    388  1.1.2.3  macallan 		goto rec;
    389  1.1.2.3  macallan 	len = OF_getprop(node, "ranges", map, sizeof(map));
    390  1.1.2.3  macallan 	if (len == -1)
    391  1.1.2.3  macallan 		goto rec;
    392  1.1.2.3  macallan 	if (OF_getprop(node, "#address-cells", &acells,
    393  1.1.2.3  macallan 	    sizeof(acells)) != sizeof(acells))
    394  1.1.2.3  macallan 		acells = 1;
    395  1.1.2.3  macallan 	if (OF_getprop(node, "#size-cells", &scells,
    396  1.1.2.3  macallan 	    sizeof(scells)) != sizeof(scells))
    397  1.1.2.3  macallan 		scells = 1;
    398  1.1.2.3  macallan 	if (type == RANGE_TYPE_ISA)
    399  1.1.2.3  macallan 		reclen = 6;
    400  1.1.2.3  macallan 	else
    401  1.1.2.3  macallan 		reclen = acells + scells + 1;
    402  1.1.2.3  macallan 	printf("found a map reclen=%d cur=%d\n", reclen, *cur);
    403  1.1.2.3  macallan 	switch (type) {
    404  1.1.2.1   garbled 		case RANGE_TYPE_PCI:
    405  1.1.2.1   garbled 		case RANGE_TYPE_FIRSTPCI:
    406  1.1.2.1   garbled 			for (i=0; i < len/(4*reclen); i++) {
    407  1.1.2.1   garbled 				regions[*cur].type = map[i*reclen] >> 24;
    408  1.1.2.1   garbled 				regions[*cur].addr = map[i*reclen + acells];
    409  1.1.2.1   garbled 				regions[*cur].size =
    410  1.1.2.1   garbled 				    map[i*reclen + acells + scells];
    411  1.1.2.1   garbled 				(*cur)++;
    412  1.1.2.1   garbled 			}
    413  1.1.2.1   garbled 			break;
    414  1.1.2.1   garbled 		case RANGE_TYPE_ISA:
    415  1.1.2.1   garbled 			for (i=0; i < len/(4*reclen); i++) {
    416  1.1.2.1   garbled 				if (map[i*reclen] == 1)
    417  1.1.2.1   garbled 					regions[*cur].type = RANGE_IO;
    418  1.1.2.1   garbled 				else
    419  1.1.2.1   garbled 					regions[*cur].type = RANGE_MEM;
    420  1.1.2.1   garbled 				regions[*cur].size =
    421  1.1.2.1   garbled 				    map[i*reclen + acells + scells];
    422  1.1.2.1   garbled 				(*cur)++;
    423  1.1.2.1   garbled 			}
    424  1.1.2.1   garbled 			break;
    425  1.1.2.1   garbled 	}
    426  1.1.2.3  macallan 	return;
    427  1.1.2.3  macallan rec:
    428  1.1.2.3  macallan 	for (node = OF_child(base); node; node = OF_peer(node))
    429  1.1.2.3  macallan 		find_ranges(node, regions, cur, type);
    430  1.1.2.1   garbled }
    431  1.1.2.1   garbled 
    432  1.1.2.1   garbled static int
    433  1.1.2.1   garbled find_lowest_range(rangemap_t *ranges, int nrof, int type)
    434  1.1.2.1   garbled {
    435  1.1.2.1   garbled 	int i, low = 0;
    436  1.1.2.1   garbled 	u_int32_t addr = 0xffffffff;
    437  1.1.2.1   garbled 
    438  1.1.2.1   garbled 	for (i=0; i < nrof; i++) {
    439  1.1.2.1   garbled 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    440  1.1.2.1   garbled 		    ranges[i].addr < addr) {
    441  1.1.2.1   garbled 			low = i;
    442  1.1.2.1   garbled 			addr = ranges[i].addr;
    443  1.1.2.1   garbled 		}
    444  1.1.2.1   garbled 	}
    445  1.1.2.1   garbled 	if (addr == 0xffffffff)
    446  1.1.2.1   garbled 		return -1;
    447  1.1.2.1   garbled 	return low;
    448  1.1.2.1   garbled }
    449  1.1.2.2   garbled 
    450  1.1.2.2   garbled /*
    451  1.1.2.2   garbled  * Find a region of memory, and create a bus_space_tag for it.
    452  1.1.2.2   garbled  * Notes:
    453  1.1.2.2   garbled  * For ISA node is ignored.
    454  1.1.2.2   garbled  * node is the starting node.  if -1, we start at / and map everything.
    455  1.1.2.2   garbled  */
    456  1.1.2.2   garbled 
    457  1.1.2.2   garbled int
    458  1.1.2.2   garbled ofwoea_map_space(int rangetype, int iomem, int node,
    459  1.1.2.2   garbled     struct powerpc_bus_space *tag, const char *name)
    460  1.1.2.1   garbled {
    461  1.1.2.2   garbled 	int i, cur, range, nrofholes, error;
    462  1.1.2.2   garbled 	static int exmap=0;
    463  1.1.2.1   garbled 	u_int32_t addr;
    464  1.1.2.1   garbled 	rangemap_t region, holes[32], list[32];
    465  1.1.2.1   garbled 
    466  1.1.2.1   garbled 	memset(list, 0, sizeof(list));
    467  1.1.2.1   garbled 	cur = 0;
    468  1.1.2.2   garbled 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    469  1.1.2.2   garbled 		node = OF_finddevice("/");
    470  1.1.2.1   garbled 	if (rangetype == RANGE_TYPE_ISA) {
    471  1.1.2.1   garbled 		u_int32_t size = 0;
    472  1.1.2.1   garbled 		rangemap_t regions[32];
    473  1.1.2.1   garbled 
    474  1.1.2.1   garbled 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    475  1.1.2.1   garbled 		range = 0;
    476  1.1.2.1   garbled 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    477  1.1.2.1   garbled 		if (range == 0 || cur == 0)
    478  1.1.2.1   garbled 			return -1; /* no isa stuff found */
    479  1.1.2.1   garbled 		for (i=0; i < range; i++)
    480  1.1.2.1   garbled 			if (regions[i].type == iomem)
    481  1.1.2.1   garbled 				size = regions[i].size;
    482  1.1.2.1   garbled 		if (iomem == RANGE_IO) {
    483  1.1.2.1   garbled 			/* the first io range is the one */
    484  1.1.2.1   garbled 			for (i=0; i < cur; i++)
    485  1.1.2.1   garbled 				if (list[i].type == RANGE_IO && size) {
    486  1.1.2.2   garbled 					tag->pbs_offset = list[i].addr;
    487  1.1.2.2   garbled 					tag->pbs_limit = size;
    488  1.1.2.2   garbled 					error = bus_space_init(tag, name, NULL, 0);
    489  1.1.2.1   garbled 					return error;
    490  1.1.2.1   garbled 				}
    491  1.1.2.1   garbled 		} else {
    492  1.1.2.1   garbled 			for (i=0; i < cur; i++)
    493  1.1.2.1   garbled 				if (list[i].type == RANGE_MEM &&
    494  1.1.2.1   garbled 				    list[i].size == size) {
    495  1.1.2.2   garbled 					tag->pbs_offset = list[i].addr;
    496  1.1.2.2   garbled 					tag->pbs_limit = size;
    497  1.1.2.2   garbled 					error = bus_space_init(tag, name, NULL, 0);
    498  1.1.2.1   garbled 					return error;
    499  1.1.2.1   garbled 				}
    500  1.1.2.1   garbled 		}
    501  1.1.2.1   garbled 		return -1; /* NO ISA FOUND */
    502  1.1.2.1   garbled 	}
    503  1.1.2.1   garbled 	find_ranges(node, list, &cur, rangetype);
    504  1.1.2.1   garbled 
    505  1.1.2.1   garbled 	printf("cur == %d\n", cur);
    506  1.1.2.1   garbled 	/* now list should contain a list of memory regions */
    507  1.1.2.1   garbled 	for (i=0; i < cur; i++)
    508  1.1.2.1   garbled 		printf("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    509  1.1.2.1   garbled 		    list[i].size, list[i].type);
    510  1.1.2.1   garbled 
    511  1.1.2.1   garbled 	addr=0;
    512  1.1.2.1   garbled 	range = find_lowest_range(list, cur, iomem);
    513  1.1.2.1   garbled 	i = 0;
    514  1.1.2.1   garbled 	nrofholes = 0;
    515  1.1.2.1   garbled 	while (range != -1) {
    516  1.1.2.3  macallan 		printf("range==%d\n", range);
    517  1.1.2.1   garbled 		printf("i==%d\n", i);
    518  1.1.2.1   garbled 		if (i == 0) {
    519  1.1.2.1   garbled 			memcpy(&region, &list[range], sizeof(rangemap_t));
    520  1.1.2.1   garbled 			list[range].addr = 0;
    521  1.1.2.1   garbled 			i++;
    522  1.1.2.1   garbled 			range = find_lowest_range(list, cur, iomem);
    523  1.1.2.1   garbled 			continue;
    524  1.1.2.1   garbled 		}
    525  1.1.2.1   garbled 		if (region.addr + region.size < list[range].addr) {
    526  1.1.2.1   garbled 			/* allocate a hole */
    527  1.1.2.1   garbled 			holes[nrofholes].addr = region.size + region.addr;
    528  1.1.2.1   garbled 			holes[nrofholes].size = list[range].addr -
    529  1.1.2.1   garbled 			    holes[nrofholes].addr - 1;
    530  1.1.2.1   garbled 			nrofholes++;
    531  1.1.2.1   garbled 		}
    532  1.1.2.3  macallan 		region.size = list[range].size + list[range].addr -
    533  1.1.2.3  macallan 		    region.addr;
    534  1.1.2.1   garbled 		list[range].addr = 0;
    535  1.1.2.1   garbled 		range = find_lowest_range(list, cur, iomem);
    536  1.1.2.1   garbled 	}
    537  1.1.2.1   garbled 	printf("RANGE iomem=%d FOUND\n", iomem);
    538  1.1.2.1   garbled 	printf("addr=0x%x size=0x%x type=%d\n", region.addr,
    539  1.1.2.1   garbled 		    region.size, region.type);
    540  1.1.2.1   garbled 	printf("HOLES FOUND\n");
    541  1.1.2.1   garbled 	for (i=0; i < nrofholes; i++)
    542  1.1.2.1   garbled 		printf("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    543  1.1.2.1   garbled 		    holes[i].size, holes[i].type);
    544  1.1.2.1   garbled 	/* AT THIS POINT WE MAP IT */
    545  1.1.2.1   garbled 
    546  1.1.2.2   garbled 	if (rangetype == RANGE_TYPE_PCI) {
    547  1.1.2.2   garbled 		if (exmap == EXSTORAGE_MAX)
    548  1.1.2.2   garbled 			panic("Not enough ex_storage space. "
    549  1.1.2.2   garbled 			    "Increase EXSTORAGE_MAX");
    550  1.1.2.3  macallan 
    551  1.1.2.3  macallan 		/* XXX doing this in here might be wrong */
    552  1.1.2.3  macallan 		if (iomem == 1) {
    553  1.1.2.3  macallan 			/* we map an IO region */
    554  1.1.2.3  macallan 			tag->pbs_offset = region.addr;
    555  1.1.2.3  macallan 			tag->pbs_base = 0;
    556  1.1.2.3  macallan 			tag->pbs_limit = region.size;
    557  1.1.2.3  macallan 		} else {
    558  1.1.2.3  macallan 			/* ... or a memory region */
    559  1.1.2.3  macallan 			tag->pbs_offset = 0;
    560  1.1.2.3  macallan 			tag->pbs_base = region.addr;
    561  1.1.2.3  macallan 			tag->pbs_limit = region.size + region.addr;
    562  1.1.2.3  macallan 		}
    563  1.1.2.3  macallan 
    564  1.1.2.2   garbled 		error = bus_space_init(tag, name, ex_storage[exmap],
    565  1.1.2.2   garbled 		    sizeof(ex_storage[exmap]));
    566  1.1.2.2   garbled 		exmap++;
    567  1.1.2.1   garbled 		if (error)
    568  1.1.2.2   garbled 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    569  1.1.2.1   garbled 		for (i=0; i < nrofholes; i++) {
    570  1.1.2.1   garbled 			error =
    571  1.1.2.2   garbled 			    extent_alloc_region(tag->pbs_extent,
    572  1.1.2.1   garbled 				holes[i].addr, holes[i].size, EX_NOWAIT);
    573  1.1.2.1   garbled 			if (error)
    574  1.1.2.1   garbled 				panic("ofwoea_bus_space_init: can't block out"
    575  1.1.2.2   garbled 				    " reserved space 0x%x-0x%x: error=%d",
    576  1.1.2.1   garbled 				    holes[i].addr, holes[i].addr+holes[i].size,
    577  1.1.2.1   garbled 				    error);
    578  1.1.2.1   garbled 		}
    579  1.1.2.1   garbled 		return error;
    580  1.1.2.1   garbled 	}
    581  1.1.2.1   garbled 	return -1;
    582  1.1.2.1   garbled }
    583  1.1.2.1   garbled 
    584  1.1.2.1   garbled void
    585  1.1.2.1   garbled ofwoea_bus_space_init(void)
    586  1.1.2.1   garbled {
    587  1.1.2.1   garbled 	int error;
    588  1.1.2.1   garbled 
    589  1.1.2.2   garbled 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    590  1.1.2.2   garbled 	    &genppc_isa_io_space_tag, "isa-ioport");
    591  1.1.2.1   garbled 	if (error > 0)
    592  1.1.2.1   garbled 		panic("Could not map ISA IO");
    593  1.1.2.2   garbled 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    594  1.1.2.2   garbled 	    &genppc_isa_mem_space_tag, "isa-iomem");
    595  1.1.2.1   garbled 	if (error > 0)
    596  1.1.2.1   garbled 		panic("Could not map ISA MEM");
    597  1.1.2.1   garbled }
    598