Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.62
      1  1.62   msaitoh /* $NetBSD: ofwoea_machdep.c,v 1.62 2021/12/05 07:13:48 msaitoh 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.62   msaitoh __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.62 2021/12/05 07:13:48 msaitoh Exp $");
     34   1.2   garbled 
     35  1.48       rin #include "ksyms.h"
     36  1.48       rin #include "wsdisplay.h"
     37  1.48       rin 
     38  1.48       rin #ifdef _KERNEL_OPT
     39  1.48       rin #include "opt_ddb.h"
     40   1.2   garbled #include "opt_kgdb.h"
     41  1.17       apb #include "opt_modular.h"
     42  1.49       rin #include "opt_multiprocessor.h"
     43  1.48       rin #include "opt_oea.h"
     44  1.48       rin #include "opt_ofwoea.h"
     45  1.48       rin #include "opt_ppcarch.h"
     46  1.48       rin #endif
     47  1.32  macallan 
     48   1.2   garbled #include <sys/param.h>
     49   1.2   garbled #include <sys/buf.h>
     50   1.2   garbled #include <sys/boot_flag.h>
     51   1.2   garbled #include <sys/extent.h>
     52   1.2   garbled #include <sys/kernel.h>
     53   1.2   garbled #include <sys/ksyms.h>
     54   1.2   garbled #include <uvm/uvm_extern.h>
     55   1.2   garbled 
     56   1.2   garbled #include <dev/ofw/openfirm.h>
     57  1.32  macallan #include <dev/wscons/wsconsio.h>
     58  1.32  macallan #include <dev/wscons/wsdisplayvar.h>
     59  1.32  macallan #include <dev/rasops/rasops.h>
     60  1.32  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     61   1.2   garbled #include <machine/pmap.h>
     62   1.2   garbled #include <machine/powerpc.h>
     63   1.2   garbled #include <machine/trap.h>
     64   1.5   garbled #include <machine/vmparam.h>
     65   1.6   garbled #include <machine/autoconf.h>
     66  1.24    dyoung #include <sys/bus.h>
     67   1.2   garbled #include <powerpc/oea/bat.h>
     68  1.32  macallan #include <powerpc/oea/ofw_rasconsvar.h>
     69  1.10   garbled #include <powerpc/oea/cpufeat.h>
     70  1.37  macallan #include <powerpc/include/oea/spr.h>
     71   1.2   garbled #include <powerpc/ofw_cons.h>
     72  1.52   thorpej #include <powerpc/ofw_machdep.h>
     73   1.2   garbled #include <powerpc/spr.h>
     74  1.22      matt #include <powerpc/pic/picvar.h>
     75   1.2   garbled 
     76   1.2   garbled #ifdef DDB
     77   1.2   garbled #include <machine/db_machdep.h>
     78   1.2   garbled #include <ddb/db_extern.h>
     79   1.2   garbled #endif
     80   1.2   garbled 
     81   1.2   garbled #ifdef KGDB
     82   1.2   garbled #include <sys/kgdb.h>
     83   1.2   garbled #endif
     84   1.2   garbled 
     85  1.11   garbled #ifdef ofppc
     86  1.11   garbled extern struct model_data modeldata;
     87  1.11   garbled #endif
     88  1.11   garbled 
     89   1.2   garbled #ifdef OFWOEA_DEBUG
     90   1.2   garbled #define DPRINTF printf
     91   1.2   garbled #else
     92   1.3   garbled #define DPRINTF while (0) printf
     93   1.2   garbled #endif
     94   1.2   garbled 
     95   1.2   garbled typedef struct _rangemap {
     96   1.2   garbled 	u_int32_t addr;
     97   1.2   garbled 	u_int32_t size;
     98   1.2   garbled 	int type;
     99   1.2   garbled } rangemap_t;
    100   1.2   garbled 
    101  1.52   thorpej struct OF_translation ofw_translations[OFW_MAX_TRANSLATIONS];
    102   1.2   garbled 
    103  1.54   thorpej /*
    104  1.54   thorpej  * Data structures holding OpenFirmware's translations when running
    105  1.54   thorpej  * in virtual-mode.
    106  1.54   thorpej  *
    107  1.54   thorpej  * When we call into OpenFirmware, we point the calling CPU's
    108  1.54   thorpej  * cpu_info::ci_battable at ofw_battable[].  For now, this table
    109  1.54   thorpej  * is empty, which will ensure that any DSI exceptions that occur
    110  1.54   thorpej  * during the firmware call will not erroneously load kernel BAT
    111  1.54   thorpej  * mappings that could clobber the firmware's translations.
    112  1.54   thorpej  */
    113   1.2   garbled struct pmap ofw_pmap;
    114  1.54   thorpej struct bat ofw_battable[BAT_VA2IDX(0xffffffff)+1];
    115  1.54   thorpej 
    116   1.2   garbled char bootpath[256];
    117   1.6   garbled char model_name[64];
    118  1.15        ad #if NKSYMS || defined(DDB) || defined(MODULAR)
    119   1.2   garbled void *startsym, *endsym;
    120   1.2   garbled #endif
    121  1.37  macallan 
    122  1.37  macallan #if PPC_OEA601
    123  1.37  macallan #define TIMEBASE_FREQ (1000000000)  /* RTC register */
    124  1.37  macallan #endif
    125  1.37  macallan 
    126   1.2   garbled #ifdef TIMEBASE_FREQ
    127   1.2   garbled u_int timebase_freq = TIMEBASE_FREQ;
    128   1.2   garbled #else
    129   1.2   garbled u_int timebase_freq = 0;
    130   1.2   garbled #endif
    131   1.2   garbled 
    132  1.50       rin int ofw_quiesce;
    133  1.50       rin 
    134  1.25  macallan extern int ofwmsr;
    135   1.2   garbled extern uint32_t ticks_per_sec;
    136   1.2   garbled extern uint32_t ns_per_tick;
    137   1.2   garbled extern uint32_t ticks_per_intr;
    138   1.2   garbled 
    139  1.58   thorpej static void get_timebase_frequency(void);
    140  1.58   thorpej static void init_decrementer(void);
    141  1.58   thorpej 
    142  1.52   thorpej static void restore_ofmap(void);
    143   1.2   garbled 
    144   1.2   garbled void
    145   1.2   garbled ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    146   1.2   garbled {
    147   1.2   garbled 	register_t scratch;
    148   1.2   garbled 
    149  1.15        ad #if NKSYMS || defined(DDB) || defined(MODULAR)
    150   1.2   garbled 	/* get info of kernel symbol table from bootloader */
    151   1.2   garbled 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    152   1.2   garbled 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    153   1.2   garbled 	    sizeof(endsym));
    154   1.2   garbled 	if (startsym == NULL || endsym == NULL)
    155   1.2   garbled 	    startsym = endsym = NULL;
    156   1.2   garbled #endif
    157   1.2   garbled 
    158   1.2   garbled 	/* Parse the args string */
    159   1.2   garbled 	if (args) {
    160   1.2   garbled 		strcpy(bootpath, args);
    161   1.2   garbled 		args = bootpath;
    162   1.2   garbled 		while (*++args && *args != ' ');
    163   1.2   garbled 		if (*args) {
    164   1.2   garbled 			*args++ = 0;
    165   1.2   garbled 			while (*args)
    166   1.2   garbled 				BOOT_FLAG(*args++, boothowto);
    167   1.2   garbled 		}
    168  1.44  macallan 	} else {
    169  1.44  macallan 		int chs = OF_finddevice("/chosen");
    170  1.44  macallan 		int len;
    171  1.44  macallan 
    172  1.44  macallan 		len = OF_getprop(chs, "bootpath", bootpath, sizeof(bootpath) - 1);
    173  1.44  macallan 		if (len > -1)
    174  1.44  macallan 			bootpath[len] = 0;
    175   1.2   garbled 	}
    176   1.2   garbled 
    177  1.58   thorpej 	/* Get the timebase frequency from the firmware. */
    178  1.58   thorpej 	get_timebase_frequency();
    179  1.58   thorpej 
    180  1.59   thorpej 	/* Probe for the console device; it's initialized later. */
    181  1.59   thorpej 	ofwoea_cnprobe();
    182  1.57   thorpej 
    183  1.57   thorpej 	if (ofw_quiesce)
    184  1.57   thorpej 		OF_quiesce();
    185  1.57   thorpej 
    186  1.55   thorpej 	oea_init(pic_ext_intr);
    187  1.55   thorpej 
    188  1.55   thorpej 	/*
    189  1.55   thorpej 	 * Now that we've installed our own exception vectors,
    190  1.55   thorpej 	 * ensure that exceptions that happen while running
    191  1.55   thorpej 	 * firmware code fall into ours.
    192  1.55   thorpej 	 */
    193  1.55   thorpej 	ofwmsr &= ~PSL_IP;
    194  1.55   thorpej 
    195  1.59   thorpej 	/* Initialize bus_space */
    196  1.59   thorpej 	ofwoea_bus_space_init();
    197  1.59   thorpej 
    198  1.59   thorpej 	/* Initialize the console device. */
    199  1.59   thorpej 	ofwoea_consinit();
    200  1.59   thorpej 
    201  1.39    cherry 	uvm_md_init();
    202  1.10   garbled 
    203   1.2   garbled 	pmap_bootstrap(startkernel, endkernel);
    204   1.2   garbled 
    205  1.13   garbled /* as far as I can tell, the pmap_setup_seg0 stuff is horribly broken */
    206   1.2   garbled #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    207   1.2   garbled #if defined (PMAC_G5)
    208   1.2   garbled 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    209  1.32  macallan 	if (oeacpufeat & OEACPU_64_BRIDGE) {
    210  1.32  macallan 		vaddr_t va;
    211  1.32  macallan 		paddr_t pa;
    212  1.53   thorpej 		vsize_t size;
    213  1.32  macallan 		int i;
    214  1.32  macallan 
    215  1.33  macallan 		pmap_setup_segment0_map(0, msgbuf_paddr, msgbuf_paddr,
    216  1.33  macallan 		    round_page(MSGBUFSIZE), 0x0);
    217  1.32  macallan 
    218  1.32  macallan 		/* Map OFW code+data */
    219  1.32  macallan 
    220  1.53   thorpej 		for (i = 0; i < __arraycount(ofw_translations); i++) {
    221  1.53   thorpej 			va = ofw_translations[i].virt;
    222  1.53   thorpej 			size = ofw_translations[i].size;
    223  1.53   thorpej 			pa = ofw_translations[i].phys;
    224  1.53   thorpej 			/* XXX mode */
    225  1.53   thorpej 
    226  1.53   thorpej 			if (size == 0) {
    227  1.53   thorpej 				/* No more, all done! */
    228  1.53   thorpej 				break;
    229  1.53   thorpej 			}
    230  1.53   thorpej 
    231  1.53   thorpej 			if (va < 0xff800000)
    232  1.32  macallan 				continue;
    233  1.32  macallan 
    234  1.53   thorpej 
    235  1.53   thorpej 			for (; va < (ofw_translations[i].virt + size);
    236  1.32  macallan 			    va += PAGE_SIZE, pa += PAGE_SIZE) {
    237  1.32  macallan 				pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
    238  1.32  macallan 				    VM_PROT_ALL | PMAP_WIRED);
    239  1.32  macallan 			}
    240  1.32  macallan 		}
    241  1.32  macallan 
    242  1.32  macallan #if NWSDISPLAY > 0
    243  1.32  macallan 		/* Map video frame buffer */
    244  1.32  macallan 
    245  1.32  macallan 		struct rasops_info *ri = &rascons_console_screen.scr_ri;
    246  1.32  macallan 
    247  1.32  macallan 		if (ri->ri_bits != NULL) {
    248  1.32  macallan 			for (va = (vaddr_t) ri->ri_bits;
    249  1.32  macallan 			    va < round_page((vaddr_t) ri->ri_bits +
    250  1.32  macallan 				ri->ri_height * ri->ri_stride);
    251  1.32  macallan 			    va += PAGE_SIZE) {
    252  1.32  macallan 				pmap_enter(pmap_kernel(), va, va,
    253  1.32  macallan 				    VM_PROT_READ | VM_PROT_WRITE,
    254  1.32  macallan 				    PMAP_NOCACHE | PMAP_WIRED);
    255  1.32  macallan 			}
    256  1.32  macallan 		}
    257  1.32  macallan #endif
    258  1.32  macallan 	}
    259   1.2   garbled #elif defined (MAMBO)
    260   1.2   garbled 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    261  1.10   garbled 	if (oeacpufeat & OEACPU_64_BRIDGE)
    262  1.10   garbled 		pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    263   1.2   garbled #endif /* PMAC_G5 */
    264  1.10   garbled #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    265   1.2   garbled 
    266   1.2   garbled 	/* Now enable translation (and machine checks/recoverable interrupts) */
    267   1.2   garbled 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    268   1.2   garbled 	    : "=r"(scratch)
    269   1.2   garbled 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    270   1.2   garbled 
    271  1.52   thorpej 	restore_ofmap();
    272   1.2   garbled 
    273  1.43  macallan 	rascons_finalize();
    274  1.43  macallan 
    275  1.15        ad #if NKSYMS || defined(DDB) || defined(MODULAR)
    276  1.36      matt 	ksyms_addsyms_elf((int)((uintptr_t)endsym - (uintptr_t)startsym), startsym, endsym);
    277   1.2   garbled #endif
    278   1.2   garbled 
    279  1.58   thorpej 	/* Kick off the clock. */
    280  1.58   thorpej 	init_decrementer();
    281  1.26       phx 
    282  1.26       phx #ifdef DDB
    283  1.26       phx 	if (boothowto & RB_KDB)
    284  1.26       phx 		Debugger();
    285  1.26       phx #endif
    286   1.2   garbled }
    287   1.2   garbled 
    288  1.58   thorpej static void
    289  1.58   thorpej get_timebase_frequency(void)
    290   1.2   garbled {
    291  1.58   thorpej 	int qhandle, phandle, node;
    292   1.2   garbled 	char type[32];
    293   1.2   garbled 
    294   1.2   garbled 	if (timebase_freq != 0) {
    295   1.2   garbled 		ticks_per_sec = timebase_freq;
    296  1.58   thorpej 		return;
    297   1.2   garbled 	}
    298   1.2   garbled 
    299  1.41  macallan 	node = OF_finddevice("/cpus/@0");
    300  1.42       mrg 	if (node != -1 &&
    301  1.42       mrg 	    OF_getprop(node, "timebase-frequency", &ticks_per_sec,
    302  1.42       mrg 		       sizeof ticks_per_sec) > 0) {
    303  1.58   thorpej 		return;
    304  1.41  macallan 	}
    305  1.41  macallan 
    306  1.41  macallan 	node = OF_finddevice("/");
    307  1.41  macallan 	for (qhandle = node; qhandle; qhandle = phandle) {
    308   1.2   garbled 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    309   1.2   garbled 		    && strcmp(type, "cpu") == 0
    310   1.2   garbled 		    && OF_getprop(qhandle, "timebase-frequency",
    311   1.2   garbled 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    312  1.58   thorpej 			return;
    313   1.2   garbled 		}
    314   1.2   garbled 		if ((phandle = OF_child(qhandle)))
    315   1.2   garbled 			continue;
    316   1.2   garbled 		while (qhandle) {
    317   1.2   garbled 			if ((phandle = OF_peer(qhandle)))
    318   1.2   garbled 				break;
    319   1.2   garbled 			qhandle = OF_parent(qhandle);
    320   1.2   garbled 		}
    321   1.2   garbled 	}
    322   1.2   garbled 	panic("no cpu node");
    323  1.58   thorpej }
    324  1.58   thorpej 
    325  1.58   thorpej static void
    326  1.58   thorpej init_decrementer(void)
    327  1.58   thorpej {
    328  1.58   thorpej 	int scratch, msr;
    329  1.58   thorpej 
    330  1.58   thorpej 	KASSERT(ticks_per_sec != 0);
    331   1.2   garbled 
    332   1.2   garbled 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    333   1.2   garbled 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    334   1.2   garbled 	ns_per_tick = 1000000000 / ticks_per_sec;
    335   1.2   garbled 	ticks_per_intr = ticks_per_sec / hz;
    336   1.2   garbled 	cpu_timebase = ticks_per_sec;
    337  1.37  macallan 
    338  1.37  macallan #ifdef PPC_OEA601
    339  1.37  macallan 	if ((mfpvr() >> 16) == MPC601)
    340  1.58   thorpej 		curcpu()->ci_lasttb = rtc_nanosecs();
    341  1.37  macallan 	else
    342  1.37  macallan #endif
    343  1.58   thorpej 		curcpu()->ci_lasttb = mftbl();
    344  1.37  macallan 
    345   1.2   garbled 	mtspr(SPR_DEC, ticks_per_intr);
    346   1.2   garbled 	mtmsr(msr);
    347   1.2   garbled }
    348   1.2   garbled 
    349   1.2   garbled void
    350  1.52   thorpej restore_ofmap(void)
    351   1.2   garbled {
    352  1.52   thorpej 	vaddr_t va, size;
    353  1.52   thorpej 	paddr_t pa;
    354   1.2   garbled 	int i;
    355   1.2   garbled 
    356   1.2   garbled 	pmap_pinit(&ofw_pmap);
    357   1.2   garbled 
    358  1.36      matt #ifndef _LP64
    359  1.32  macallan 	ofw_pmap.pm_sr[0] = KERNELN_SEGMENT(0)|SR_PRKEY;
    360  1.32  macallan 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT|SR_SUKEY|SR_PRKEY;
    361   1.2   garbled 
    362   1.2   garbled #ifdef KERNEL2_SR
    363  1.32  macallan 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT|SR_SUKEY|SR_PRKEY;
    364   1.2   garbled #endif
    365  1.36      matt #endif
    366   1.2   garbled 
    367  1.52   thorpej 	for (i = 0; i < __arraycount(ofw_translations); i++) {
    368  1.52   thorpej 		va = ofw_translations[i].virt;
    369  1.52   thorpej 		size = ofw_translations[i].size;
    370  1.52   thorpej 		pa = ofw_translations[i].phys;
    371  1.52   thorpej 		/* XXX mode */
    372  1.52   thorpej 
    373  1.52   thorpej 		if (size == 0) {
    374  1.52   thorpej 			/* No more, all done! */
    375  1.52   thorpej 			break;
    376  1.52   thorpej 		}
    377   1.2   garbled 
    378  1.52   thorpej 		if (va < 0xf0000000)	/* XXX */
    379   1.2   garbled 			continue;
    380   1.2   garbled 
    381  1.60  macallan 		/*
    382  1.61  macallan 		 * XXX macallan@
    383  1.61  macallan 		 * My beige G3 throws a DSI trap if we try to map the last page
    384  1.61  macallan 		 * of the 32bit address space. On old world macs the firmware
    385  1.61  macallan 		 * ROM occupies 4MB at 0xffc00000, triggering it when we
    386  1.61  macallan 		 * restore OF translations. This just works around a bug
    387  1.61  macallan 		 * elsewhere in pmap and should go away once fixed there.
    388  1.60  macallan 		 */
    389  1.60  macallan 		if (pa == 0xffc00000 && size == 0x400000)
    390  1.60  macallan 			size = 0x3ff000;
    391  1.60  macallan 
    392  1.52   thorpej 		while (size > 0) {
    393  1.52   thorpej 			pmap_enter(&ofw_pmap, va, pa, VM_PROT_ALL,
    394   1.2   garbled 			    VM_PROT_ALL|PMAP_WIRED);
    395   1.2   garbled 			pa += PAGE_SIZE;
    396   1.2   garbled 			va += PAGE_SIZE;
    397  1.52   thorpej 			size -= PAGE_SIZE;
    398   1.2   garbled 		}
    399   1.2   garbled 	}
    400   1.2   garbled 	pmap_update(&ofw_pmap);
    401  1.19  kiyohara }
    402   1.2   garbled 
    403   1.2   garbled /* we define these partially, as we will fill the rest in later */
    404   1.2   garbled struct powerpc_bus_space genppc_isa_io_space_tag = {
    405   1.2   garbled 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    406   1.2   garbled 	.pbs_base = 0x00000000,
    407   1.2   garbled };
    408   1.2   garbled 
    409   1.2   garbled struct powerpc_bus_space genppc_isa_mem_space_tag = {
    410   1.2   garbled 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    411   1.2   garbled 	.pbs_base = 0x00000000,
    412   1.2   garbled };
    413   1.2   garbled 
    414   1.2   garbled /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    415   1.2   garbled  * Increase if necc.
    416   1.2   garbled  */
    417   1.2   garbled static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    418   1.2   garbled 	__attribute__((aligned(8)));
    419   1.2   garbled 
    420   1.2   garbled 
    421   1.2   garbled static void
    422   1.2   garbled find_ranges(int base, rangemap_t *regions, int *cur, int type)
    423   1.2   garbled {
    424   1.2   garbled 	int node, i, len, reclen;
    425  1.32  macallan 	u_int32_t parent_acells, acells, scells, map[160];
    426   1.2   garbled 	char tmp[32];
    427   1.2   garbled 
    428   1.2   garbled 	node = base;
    429   1.2   garbled 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    430   1.2   garbled 		goto rec;
    431   1.2   garbled 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    432   1.2   garbled 	    strcmp("pci", tmp) != 0)
    433   1.2   garbled 		goto rec;
    434   1.2   garbled 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    435   1.2   garbled 		goto rec;
    436  1.40  macallan 	if (type == RANGE_TYPE_MACIO && strcmp("memory-controller", tmp) == 0) {
    437  1.40  macallan 		len = OF_getprop(node, "reg", map, sizeof(map));
    438  1.40  macallan 		acells = 1;
    439  1.40  macallan 		scells = 1;
    440  1.40  macallan 	} else {
    441  1.40  macallan 		len = OF_getprop(node, "ranges", map, sizeof(map));
    442  1.40  macallan 	}
    443   1.2   garbled 	if (len == -1)
    444   1.2   garbled 		goto rec;
    445  1.32  macallan 	if (OF_getprop(OF_parent(node), "#address-cells", &parent_acells,
    446  1.32  macallan 	    sizeof(parent_acells)) != sizeof(parent_acells))
    447  1.32  macallan 		parent_acells = 1;
    448   1.2   garbled 	if (OF_getprop(node, "#address-cells", &acells,
    449   1.2   garbled 	    sizeof(acells)) != sizeof(acells))
    450  1.32  macallan 		acells = 3;
    451   1.2   garbled 	if (OF_getprop(node, "#size-cells", &scells,
    452   1.2   garbled 	    sizeof(scells)) != sizeof(scells))
    453  1.32  macallan 		scells = 2;
    454  1.11   garbled #ifdef ofppc
    455  1.11   garbled 	if (modeldata.ranges_offset == 0)
    456  1.11   garbled 		scells -= 1;
    457  1.11   garbled #endif
    458   1.2   garbled 	if (type == RANGE_TYPE_ISA)
    459   1.2   garbled 		reclen = 6;
    460   1.2   garbled 	else
    461  1.32  macallan 		reclen = parent_acells + acells + scells;
    462   1.3   garbled 	/*
    463   1.3   garbled 	 * There exist ISA buses with empty ranges properties.  This is
    464   1.3   garbled 	 * known to occur on the Pegasos II machine, and likely others.
    465   1.3   garbled 	 * According to them, that means that the isa bus is a fake bus, and
    466  1.62   msaitoh 	 * the real maps are the PCI maps of the preceding bus.  To deal
    467   1.3   garbled 	 * with this, we will set cur to -1 and return.
    468   1.3   garbled 	 */
    469   1.3   garbled 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) == 0 && len == 0) {
    470   1.3   garbled 		*cur = -1;
    471   1.3   garbled 		DPRINTF("Found empty range in isa bus\n");
    472   1.3   garbled 		return;
    473   1.3   garbled 	}
    474   1.3   garbled 
    475   1.3   garbled 	DPRINTF("found a map reclen=%d cur=%d len=%d\n", reclen, *cur, len);
    476   1.2   garbled 	switch (type) {
    477   1.2   garbled 		case RANGE_TYPE_PCI:
    478   1.2   garbled 		case RANGE_TYPE_FIRSTPCI:
    479   1.2   garbled 			for (i=0; i < len/(4*reclen); i++) {
    480   1.3   garbled 				DPRINTF("FOUND PCI RANGE\n");
    481   1.8   garbled 				regions[*cur].size =
    482  1.32  macallan 				    map[i*reclen + parent_acells + acells + scells - 1];
    483   1.8   garbled 				/* skip ranges of size==0 */
    484   1.8   garbled 				if (regions[*cur].size == 0)
    485   1.8   garbled 					continue;
    486  1.32  macallan 				regions[*cur].type = (map[i*reclen] >> 24) & 0x3;
    487  1.32  macallan 				regions[*cur].addr = map[i*reclen + parent_acells + acells - 1];
    488   1.2   garbled 				(*cur)++;
    489   1.2   garbled 			}
    490   1.2   garbled 			break;
    491   1.2   garbled 		case RANGE_TYPE_ISA:
    492   1.2   garbled 			for (i=0; i < len/(4*reclen); i++) {
    493   1.2   garbled 				if (map[i*reclen] == 1)
    494   1.2   garbled 					regions[*cur].type = RANGE_IO;
    495   1.2   garbled 				else
    496   1.2   garbled 					regions[*cur].type = RANGE_MEM;
    497   1.3   garbled 				DPRINTF("FOUND ISA RANGE TYPE=%d\n",
    498   1.3   garbled 					regions[*cur].type);
    499   1.2   garbled 				regions[*cur].size =
    500   1.2   garbled 				    map[i*reclen + acells + scells];
    501   1.2   garbled 				(*cur)++;
    502   1.2   garbled 			}
    503   1.2   garbled 			break;
    504  1.40  macallan 		case RANGE_TYPE_MACIO:
    505  1.40  macallan 			regions[*cur].type = RANGE_MEM;
    506  1.41  macallan 			if (len == 8) {
    507  1.41  macallan 				regions[*cur].size = map[1];
    508  1.41  macallan 				regions[*cur].addr = map[0];
    509  1.41  macallan 			} else {
    510  1.41  macallan 				regions[*cur].size = map[2];
    511  1.41  macallan 				regions[*cur].addr = map[1];
    512  1.41  macallan 			}
    513  1.40  macallan 			(*cur)++;
    514  1.40  macallan 			break;
    515   1.2   garbled 	}
    516   1.3   garbled 	DPRINTF("returning with CUR=%d\n", *cur);
    517   1.2   garbled 	return;
    518   1.2   garbled rec:
    519   1.3   garbled 	for (node = OF_child(base); node; node = OF_peer(node)) {
    520   1.3   garbled 		DPRINTF("RECURSE 1 STEP\n");
    521   1.2   garbled 		find_ranges(node, regions, cur, type);
    522   1.3   garbled 		if (*cur == -1)
    523   1.3   garbled 			return;
    524   1.3   garbled 	}
    525   1.2   garbled }
    526   1.2   garbled 
    527   1.2   garbled static int
    528   1.2   garbled find_lowest_range(rangemap_t *ranges, int nrof, int type)
    529   1.2   garbled {
    530   1.2   garbled 	int i, low = 0;
    531   1.2   garbled 	u_int32_t addr = 0xffffffff;
    532   1.2   garbled 
    533   1.2   garbled 	for (i=0; i < nrof; i++) {
    534   1.2   garbled 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    535   1.2   garbled 		    ranges[i].addr < addr) {
    536   1.2   garbled 			low = i;
    537   1.2   garbled 			addr = ranges[i].addr;
    538   1.2   garbled 		}
    539   1.2   garbled 	}
    540   1.2   garbled 	if (addr == 0xffffffff)
    541   1.2   garbled 		return -1;
    542   1.2   garbled 	return low;
    543   1.2   garbled }
    544   1.2   garbled 
    545   1.2   garbled /*
    546   1.2   garbled  * Find a region of memory, and create a bus_space_tag for it.
    547   1.2   garbled  * Notes:
    548   1.2   garbled  * For ISA node is ignored.
    549   1.2   garbled  * node is the starting node.  if -1, we start at / and map everything.
    550   1.2   garbled  */
    551   1.2   garbled 
    552   1.2   garbled int
    553   1.2   garbled ofwoea_map_space(int rangetype, int iomem, int node,
    554   1.2   garbled     struct powerpc_bus_space *tag, const char *name)
    555   1.2   garbled {
    556   1.2   garbled 	int i, cur, range, nrofholes, error;
    557   1.2   garbled 	static int exmap=0;
    558   1.2   garbled 	rangemap_t region, holes[32], list[32];
    559   1.2   garbled 
    560   1.2   garbled 	memset(list, 0, sizeof(list));
    561  1.32  macallan 	memset(&region, 0, sizeof(region));
    562   1.2   garbled 	cur = 0;
    563   1.2   garbled 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    564   1.2   garbled 		node = OF_finddevice("/");
    565   1.2   garbled 	if (rangetype == RANGE_TYPE_ISA) {
    566   1.2   garbled 		u_int32_t size = 0;
    567   1.2   garbled 		rangemap_t regions[32];
    568   1.2   garbled 
    569   1.3   garbled 		DPRINTF("LOOKING FOR FIRSTPCI\n");
    570   1.2   garbled 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    571   1.2   garbled 		range = 0;
    572   1.3   garbled 		DPRINTF("LOOKING FOR ISA\n");
    573   1.2   garbled 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    574   1.2   garbled 		if (range == 0 || cur == 0)
    575   1.2   garbled 			return -1; /* no isa stuff found */
    576   1.3   garbled 		/*
    577   1.3   garbled 		 * This may be confusing to some.  The ISA ranges property
    578   1.3   garbled 		 * is supposed to be a set of IO ranges for the ISA bus, but
    579   1.3   garbled 		 * generally, it's just a set of pci devfunc lists that tell
    580   1.3   garbled 		 * you to go look at the parent PCI device for the actual
    581   1.3   garbled 		 * ranges.
    582   1.3   garbled 		 */
    583   1.3   garbled 		if (range == -1) {
    584   1.3   garbled 			/* we found a rangeless isa bus */
    585  1.20  kiyohara 			if (iomem == RANGE_IO)
    586  1.20  kiyohara 				size = 0x10000;
    587  1.20  kiyohara 			else
    588  1.20  kiyohara 				size = 0x1000000;
    589   1.3   garbled 		}
    590   1.3   garbled 		DPRINTF("found isa stuff\n");
    591   1.2   garbled 		for (i=0; i < range; i++)
    592   1.2   garbled 			if (regions[i].type == iomem)
    593   1.2   garbled 				size = regions[i].size;
    594   1.2   garbled 		if (iomem == RANGE_IO) {
    595   1.2   garbled 			/* the first io range is the one */
    596   1.2   garbled 			for (i=0; i < cur; i++)
    597   1.2   garbled 				if (list[i].type == RANGE_IO && size) {
    598   1.3   garbled 					DPRINTF("found IO\n");
    599   1.2   garbled 					tag->pbs_offset = list[i].addr;
    600   1.2   garbled 					tag->pbs_limit = size;
    601  1.20  kiyohara 					error = bus_space_init(tag, name,
    602  1.20  kiyohara 					    ex_storage[exmap],
    603  1.20  kiyohara 					    sizeof(ex_storage[exmap]));
    604  1.20  kiyohara 					exmap++;
    605   1.2   garbled 					return error;
    606   1.2   garbled 				}
    607   1.2   garbled 		} else {
    608   1.2   garbled 			for (i=0; i < cur; i++)
    609   1.2   garbled 				if (list[i].type == RANGE_MEM &&
    610   1.2   garbled 				    list[i].size == size) {
    611   1.3   garbled 					DPRINTF("found mem\n");
    612   1.2   garbled 					tag->pbs_offset = list[i].addr;
    613   1.2   garbled 					tag->pbs_limit = size;
    614  1.20  kiyohara 					error = bus_space_init(tag, name,
    615  1.20  kiyohara 					    ex_storage[exmap],
    616  1.20  kiyohara 					    sizeof(ex_storage[exmap]));
    617  1.20  kiyohara 					exmap++;
    618   1.2   garbled 					return error;
    619   1.2   garbled 				}
    620   1.2   garbled 		}
    621   1.2   garbled 		return -1; /* NO ISA FOUND */
    622   1.2   garbled 	}
    623   1.2   garbled 	find_ranges(node, list, &cur, rangetype);
    624   1.2   garbled 
    625   1.2   garbled 	DPRINTF("cur == %d\n", cur);
    626   1.2   garbled 	/* now list should contain a list of memory regions */
    627   1.2   garbled 	for (i=0; i < cur; i++)
    628   1.2   garbled 		DPRINTF("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    629   1.2   garbled 		    list[i].size, list[i].type);
    630   1.2   garbled 
    631   1.2   garbled 	range = find_lowest_range(list, cur, iomem);
    632   1.2   garbled 	i = 0;
    633   1.2   garbled 	nrofholes = 0;
    634   1.2   garbled 	while (range != -1) {
    635   1.2   garbled 		DPRINTF("range==%d\n", range);
    636   1.2   garbled 		DPRINTF("i==%d\n", i);
    637   1.2   garbled 		if (i == 0) {
    638   1.2   garbled 			memcpy(&region, &list[range], sizeof(rangemap_t));
    639   1.2   garbled 			list[range].addr = 0;
    640   1.2   garbled 			i++;
    641   1.2   garbled 			range = find_lowest_range(list, cur, iomem);
    642   1.2   garbled 			continue;
    643   1.2   garbled 		}
    644   1.2   garbled 		if (region.addr + region.size < list[range].addr) {
    645   1.2   garbled 			/* allocate a hole */
    646  1.11   garbled 			holes[nrofholes].type = iomem;
    647   1.2   garbled 			holes[nrofholes].addr = region.size + region.addr;
    648   1.2   garbled 			holes[nrofholes].size = list[range].addr -
    649   1.2   garbled 			    holes[nrofholes].addr - 1;
    650   1.2   garbled 			nrofholes++;
    651   1.2   garbled 		}
    652   1.2   garbled 		region.size = list[range].size + list[range].addr -
    653   1.2   garbled 		    region.addr;
    654   1.2   garbled 		list[range].addr = 0;
    655   1.2   garbled 		range = find_lowest_range(list, cur, iomem);
    656   1.2   garbled 	}
    657   1.2   garbled 	DPRINTF("RANGE iomem=%d FOUND\n", iomem);
    658   1.2   garbled 	DPRINTF("addr=0x%x size=0x%x type=%d\n", region.addr,
    659   1.2   garbled 		    region.size, region.type);
    660   1.2   garbled 	DPRINTF("HOLES FOUND\n");
    661   1.2   garbled 	for (i=0; i < nrofholes; i++)
    662   1.2   garbled 		DPRINTF("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    663   1.2   garbled 		    holes[i].size, holes[i].type);
    664   1.2   garbled 	/* AT THIS POINT WE MAP IT */
    665   1.2   garbled 
    666  1.40  macallan 	if ((rangetype == RANGE_TYPE_PCI) || (rangetype == RANGE_TYPE_MACIO)) {
    667   1.2   garbled 		if (exmap == EXSTORAGE_MAX)
    668   1.2   garbled 			panic("Not enough ex_storage space. "
    669   1.2   garbled 			    "Increase EXSTORAGE_MAX");
    670   1.2   garbled 
    671   1.2   garbled 		/* XXX doing this in here might be wrong */
    672   1.2   garbled 		if (iomem == 1) {
    673   1.2   garbled 			/* we map an IO region */
    674   1.2   garbled 			tag->pbs_offset = region.addr;
    675   1.2   garbled 			tag->pbs_base = 0;
    676   1.2   garbled 			tag->pbs_limit = region.size;
    677   1.2   garbled 		} else {
    678   1.2   garbled 			/* ... or a memory region */
    679   1.2   garbled 			tag->pbs_offset = 0;
    680   1.2   garbled 			tag->pbs_base = region.addr;
    681   1.2   garbled 			tag->pbs_limit = region.size + region.addr;
    682  1.19  kiyohara 		}
    683   1.2   garbled 
    684   1.2   garbled 		error = bus_space_init(tag, name, ex_storage[exmap],
    685   1.2   garbled 		    sizeof(ex_storage[exmap]));
    686   1.2   garbled 		exmap++;
    687   1.2   garbled 		if (error)
    688   1.2   garbled 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    689   1.2   garbled 		for (i=0; i < nrofholes; i++) {
    690  1.11   garbled 			if (holes[i].type == RANGE_IO) {
    691  1.11   garbled 				error = extent_alloc_region(tag->pbs_extent,
    692  1.11   garbled 				    holes[i].addr - tag->pbs_offset,
    693  1.11   garbled 				    holes[i].size, EX_NOWAIT);
    694  1.11   garbled 			} else {
    695  1.11   garbled 				error = extent_alloc_region(tag->pbs_extent,
    696  1.11   garbled 				    holes[i].addr, holes[i].size, EX_NOWAIT);
    697  1.11   garbled 			}
    698   1.2   garbled 			if (error)
    699   1.2   garbled 				panic("ofwoea_bus_space_init: can't block out"
    700   1.2   garbled 				    " reserved space 0x%x-0x%x: error=%d",
    701   1.2   garbled 				    holes[i].addr, holes[i].addr+holes[i].size,
    702   1.2   garbled 				    error);
    703   1.2   garbled 		}
    704   1.2   garbled 		return error;
    705   1.2   garbled 	}
    706   1.2   garbled 	return -1;
    707   1.2   garbled }
    708   1.2   garbled 
    709   1.2   garbled void
    710   1.2   garbled ofwoea_bus_space_init(void)
    711   1.2   garbled {
    712   1.2   garbled 	int error;
    713   1.2   garbled 
    714   1.2   garbled 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    715   1.2   garbled 	    &genppc_isa_io_space_tag, "isa-ioport");
    716   1.2   garbled 	if (error > 0)
    717   1.2   garbled 		panic("Could not map ISA IO");
    718   1.3   garbled 
    719   1.2   garbled 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    720   1.2   garbled 	    &genppc_isa_mem_space_tag, "isa-iomem");
    721   1.2   garbled 	if (error > 0)
    722   1.2   garbled 		panic("Could not map ISA MEM");
    723   1.2   garbled }
    724