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