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