Home | History | Annotate | Line # | Download | only in dev
ebus.c revision 1.3.4.3
      1  1.3.4.2  bouyer /*	$NetBSD: ebus.c,v 1.3.4.3 2001/01/18 09:23:03 bouyer Exp $	*/
      2      1.1     mrg 
      3      1.1     mrg /*
      4  1.3.4.1  bouyer  * Copyright (c) 1999, 2000 Matthew R. Green
      5      1.1     mrg  * All rights reserved.
      6      1.1     mrg  *
      7      1.1     mrg  * Redistribution and use in source and binary forms, with or without
      8      1.1     mrg  * modification, are permitted provided that the following conditions
      9      1.1     mrg  * are met:
     10      1.1     mrg  * 1. Redistributions of source code must retain the above copyright
     11      1.1     mrg  *    notice, this list of conditions and the following disclaimer.
     12      1.1     mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     mrg  *    notice, this list of conditions and the following disclaimer in the
     14      1.1     mrg  *    documentation and/or other materials provided with the distribution.
     15      1.1     mrg  * 3. The name of the author may not be used to endorse or promote products
     16      1.1     mrg  *    derived from this software without specific prior written permission.
     17      1.1     mrg  *
     18      1.1     mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19      1.1     mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20      1.1     mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21      1.1     mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22      1.1     mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23      1.1     mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24      1.1     mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25      1.1     mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26      1.1     mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27      1.1     mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28      1.1     mrg  * SUCH DAMAGE.
     29      1.1     mrg  */
     30      1.1     mrg 
     31  1.3.4.1  bouyer #include "opt_ddb.h"
     32  1.3.4.1  bouyer 
     33      1.1     mrg /*
     34      1.1     mrg  * UltraSPARC 5 and beyond ebus support.
     35  1.3.4.1  bouyer  *
     36  1.3.4.1  bouyer  * note that this driver is far from complete:
     37  1.3.4.1  bouyer  *	- ebus2 dma code is completely unwritten
     38  1.3.4.1  bouyer  *	- interrupt establish code is completely unwritten
     39  1.3.4.1  bouyer  *	- bus map code is written and appears to work
     40      1.1     mrg  */
     41      1.1     mrg 
     42      1.1     mrg #undef DEBUG
     43      1.1     mrg #define DEBUG
     44      1.1     mrg 
     45      1.1     mrg #ifdef DEBUG
     46  1.3.4.1  bouyer #define	EDB_PROM	0x01
     47  1.3.4.1  bouyer #define EDB_CHILD	0x02
     48  1.3.4.1  bouyer #define	EDB_INTRMAP	0x04
     49  1.3.4.1  bouyer #define EDB_BUSMAP	0x08
     50  1.3.4.1  bouyer #define EDB_BUSDMA	0x10
     51  1.3.4.1  bouyer #define EDB_INTR	0x20
     52      1.1     mrg int ebus_debug = 0;
     53      1.1     mrg #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
     54      1.1     mrg #else
     55      1.1     mrg #define DPRINTF(l, s)
     56      1.1     mrg #endif
     57      1.1     mrg 
     58      1.1     mrg #include <sys/param.h>
     59      1.1     mrg #include <sys/conf.h>
     60      1.1     mrg #include <sys/device.h>
     61  1.3.4.1  bouyer #include <sys/errno.h>
     62  1.3.4.1  bouyer #include <sys/extent.h>
     63      1.1     mrg #include <sys/malloc.h>
     64      1.1     mrg #include <sys/systm.h>
     65  1.3.4.1  bouyer #include <sys/time.h>
     66      1.1     mrg 
     67      1.1     mrg #define _SPARC_BUS_DMA_PRIVATE
     68      1.1     mrg #include <machine/bus.h>
     69      1.1     mrg #include <machine/autoconf.h>
     70      1.1     mrg 
     71      1.1     mrg #include <dev/pci/pcivar.h>
     72      1.1     mrg #include <dev/pci/pcireg.h>
     73      1.1     mrg #include <dev/pci/pcidevs.h>
     74      1.1     mrg 
     75  1.3.4.1  bouyer #include <sparc64/dev/iommureg.h>
     76  1.3.4.1  bouyer #include <sparc64/dev/iommuvar.h>
     77  1.3.4.1  bouyer #include <sparc64/dev/psychoreg.h>
     78  1.3.4.1  bouyer #include <sparc64/dev/psychovar.h>
     79      1.1     mrg #include <sparc64/dev/ebusreg.h>
     80      1.1     mrg #include <sparc64/dev/ebusvar.h>
     81  1.3.4.1  bouyer #include <sparc64/sparc64/cache.h>
     82      1.1     mrg 
     83      1.1     mrg int	ebus_match __P((struct device *, struct cfdata *, void *));
     84      1.1     mrg void	ebus_attach __P((struct device *, struct device *, void *));
     85      1.1     mrg 
     86      1.1     mrg struct cfattach ebus_ca = {
     87  1.3.4.1  bouyer 	sizeof(struct ebus_softc), ebus_match, ebus_attach
     88      1.1     mrg };
     89      1.1     mrg 
     90      1.1     mrg int	ebus_setup_attach_args __P((struct ebus_softc *, int,
     91      1.1     mrg 	    struct ebus_attach_args *));
     92      1.1     mrg void	ebus_destroy_attach_args __P((struct ebus_attach_args *));
     93      1.1     mrg int	ebus_print __P((void *, const char *));
     94      1.3     mrg void	ebus_find_ino __P((struct ebus_softc *, struct ebus_attach_args *));
     95  1.3.4.1  bouyer int	ebus_find_node __P((struct pci_attach_args *));
     96  1.3.4.1  bouyer 
     97  1.3.4.1  bouyer /*
     98  1.3.4.1  bouyer  * here are our bus space and bus dma routines.
     99  1.3.4.1  bouyer  */
    100  1.3.4.1  bouyer static int ebus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    101  1.3.4.1  bouyer 				int, bus_space_handle_t *));
    102  1.3.4.1  bouyer static int _ebus_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    103  1.3.4.1  bouyer 				bus_size_t, int, vaddr_t,
    104  1.3.4.1  bouyer 				bus_space_handle_t *));
    105  1.3.4.1  bouyer static void *ebus_intr_establish __P((bus_space_tag_t, int, int, int,
    106  1.3.4.1  bouyer 				int (*) __P((void *)), void *));
    107  1.3.4.1  bouyer 
    108  1.3.4.1  bouyer static int ebus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    109  1.3.4.1  bouyer 			  bus_size_t, struct proc *, int));
    110  1.3.4.1  bouyer static void ebus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    111  1.3.4.1  bouyer static void ebus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    112  1.3.4.1  bouyer 				  bus_size_t, int));
    113  1.3.4.1  bouyer int ebus_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
    114  1.3.4.1  bouyer 			   bus_dma_segment_t *, int, int *, int));
    115  1.3.4.1  bouyer void ebus_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
    116  1.3.4.1  bouyer int ebus_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
    117  1.3.4.1  bouyer 			 caddr_t *, int));
    118  1.3.4.1  bouyer void ebus_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
    119      1.1     mrg 
    120      1.1     mrg int
    121      1.1     mrg ebus_match(parent, match, aux)
    122      1.1     mrg 	struct device *parent;
    123      1.1     mrg 	struct cfdata *match;
    124      1.1     mrg 	void *aux;
    125      1.1     mrg {
    126      1.1     mrg 	struct pci_attach_args *pa = aux;
    127      1.1     mrg 
    128      1.1     mrg 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    129  1.3.4.1  bouyer 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    130  1.3.4.1  bouyer 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS &&
    131  1.3.4.1  bouyer 	    ebus_find_node(pa))
    132      1.1     mrg 		return (1);
    133      1.1     mrg 
    134      1.1     mrg 	return (0);
    135      1.1     mrg }
    136      1.1     mrg 
    137      1.1     mrg /*
    138      1.1     mrg  * attach an ebus and all it's children.  this code is modeled
    139      1.1     mrg  * after the sbus code which does similar things.
    140      1.1     mrg  */
    141      1.1     mrg void
    142      1.1     mrg ebus_attach(parent, self, aux)
    143      1.1     mrg 	struct device *parent, *self;
    144      1.1     mrg 	void *aux;
    145      1.1     mrg {
    146      1.1     mrg 	struct ebus_softc *sc = (struct ebus_softc *)self;
    147      1.1     mrg 	struct pci_attach_args *pa = aux;
    148      1.1     mrg 	struct ebus_attach_args eba;
    149      1.1     mrg 	struct ebus_interrupt_map_mask *immp;
    150  1.3.4.1  bouyer 	int node, nmapmask, error;
    151      1.1     mrg 	char devinfo[256];
    152      1.1     mrg 
    153      1.1     mrg 	printf("\n");
    154      1.1     mrg 
    155      1.1     mrg 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    156      1.1     mrg 	printf("%s: %s, revision 0x%02x\n", self->dv_xname, devinfo,
    157      1.1     mrg 	    PCI_REVISION(pa->pa_class));
    158      1.1     mrg 
    159  1.3.4.1  bouyer 	sc->sc_parent = (struct psycho_softc *)parent;
    160      1.1     mrg 	sc->sc_bustag = pa->pa_memt;
    161      1.1     mrg 	sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
    162      1.1     mrg 	sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
    163      1.1     mrg 
    164  1.3.4.1  bouyer 	node = ebus_find_node(pa);
    165      1.1     mrg 	if (node == 0)
    166      1.1     mrg 		panic("could not find ebus node");
    167      1.1     mrg 
    168      1.1     mrg 	sc->sc_node = node;
    169      1.1     mrg 
    170      1.1     mrg 	/*
    171      1.1     mrg 	 * fill in our softc with information from the prom
    172      1.1     mrg 	 */
    173      1.3     mrg 	sc->sc_intmap = NULL;
    174      1.3     mrg 	sc->sc_range = NULL;
    175  1.3.4.1  bouyer 	error = getprop(node, "interrupt-map",
    176  1.3.4.1  bouyer 			sizeof(struct ebus_interrupt_map),
    177  1.3.4.1  bouyer 			&sc->sc_nintmap, (void **)&sc->sc_intmap);
    178  1.3.4.1  bouyer 	switch (error) {
    179  1.3.4.1  bouyer 	case 0:
    180  1.3.4.1  bouyer 		immp = &sc->sc_intmapmask;
    181  1.3.4.1  bouyer 		error = getprop(node, "interrupt-map-mask",
    182  1.3.4.1  bouyer 			    sizeof(struct ebus_interrupt_map_mask), &nmapmask,
    183  1.3.4.1  bouyer 			    (void **)&immp);
    184  1.3.4.1  bouyer 		if (error)
    185  1.3.4.1  bouyer 			panic("could not get ebus interrupt-map-mask");
    186  1.3.4.1  bouyer 		if (nmapmask != 1)
    187  1.3.4.1  bouyer 			panic("ebus interrupt-map-mask is broken");
    188  1.3.4.1  bouyer 		break;
    189  1.3.4.1  bouyer 	case ENOENT:
    190  1.3.4.1  bouyer 		break;
    191  1.3.4.1  bouyer 	default:
    192  1.3.4.1  bouyer 		panic("ebus interrupt-map: error %d", error);
    193  1.3.4.1  bouyer 		break;
    194  1.3.4.1  bouyer 	}
    195      1.1     mrg 
    196  1.3.4.1  bouyer 	error = getprop(node, "ranges", sizeof(struct ebus_ranges),
    197      1.1     mrg 	    &sc->sc_nrange, (void **)&sc->sc_range);
    198  1.3.4.1  bouyer 	if (error)
    199  1.3.4.1  bouyer 		panic("ebus ranges: error %d", error);
    200      1.1     mrg 
    201      1.1     mrg 	/*
    202      1.1     mrg 	 * now attach all our children
    203      1.1     mrg 	 */
    204      1.1     mrg 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
    205      1.1     mrg 	for (node = firstchild(node); node; node = nextsibling(node)) {
    206      1.1     mrg 		char *name = getpropstring(node, "name");
    207      1.1     mrg 
    208      1.1     mrg 		if (ebus_setup_attach_args(sc, node, &eba) != 0) {
    209      1.1     mrg 			printf("ebus_attach: %s: incomplete\n", name);
    210      1.1     mrg 			continue;
    211  1.3.4.1  bouyer 		} else {
    212  1.3.4.1  bouyer 			DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n", eba.ea_name));
    213  1.3.4.1  bouyer 			(void)config_found(self, &eba, ebus_print);
    214      1.1     mrg 		}
    215      1.1     mrg 		ebus_destroy_attach_args(&eba);
    216      1.1     mrg 	}
    217      1.1     mrg }
    218      1.1     mrg 
    219      1.1     mrg int
    220      1.1     mrg ebus_setup_attach_args(sc, node, ea)
    221      1.1     mrg 	struct ebus_softc	*sc;
    222      1.1     mrg 	int			node;
    223      1.1     mrg 	struct ebus_attach_args	*ea;
    224      1.1     mrg {
    225      1.1     mrg 	int	n, rv;
    226      1.1     mrg 
    227      1.1     mrg 	bzero(ea, sizeof(struct ebus_attach_args));
    228      1.1     mrg 	rv = getprop(node, "name", 1, &n, (void **)&ea->ea_name);
    229      1.1     mrg 	if (rv != 0)
    230      1.1     mrg 		return (rv);
    231      1.1     mrg 	ea->ea_name[n] = '\0';
    232      1.1     mrg 
    233      1.1     mrg 	ea->ea_node = node;
    234      1.1     mrg 	ea->ea_bustag = sc->sc_childbustag;
    235      1.1     mrg 	ea->ea_dmatag = sc->sc_dmatag;
    236      1.1     mrg 
    237      1.1     mrg 	rv = getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nregs,
    238      1.1     mrg 	    (void **)&ea->ea_regs);
    239      1.1     mrg 	if (rv)
    240      1.1     mrg 		return (rv);
    241      1.1     mrg 
    242  1.3.4.1  bouyer 	rv = getprop(node, "address", sizeof(u_int32_t), &ea->ea_nvaddrs,
    243      1.1     mrg 	    (void **)&ea->ea_vaddrs);
    244      1.1     mrg 	if (rv != ENOENT) {
    245      1.1     mrg 		if (rv)
    246      1.1     mrg 			return (rv);
    247      1.1     mrg 
    248  1.3.4.1  bouyer 		if (ea->ea_nregs != ea->ea_nvaddrs)
    249      1.1     mrg 			printf("ebus loses: device %s: %d regs and %d addrs\n",
    250  1.3.4.1  bouyer 			    ea->ea_name, ea->ea_nregs, ea->ea_nvaddrs);
    251      1.3     mrg 	} else
    252  1.3.4.1  bouyer 		ea->ea_nvaddrs = 0;
    253      1.1     mrg 
    254      1.3     mrg 	if (getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintrs,
    255      1.3     mrg 	    (void **)&ea->ea_intrs))
    256      1.3     mrg 		ea->ea_nintrs = 0;
    257  1.3.4.1  bouyer 	else
    258      1.3     mrg 		ebus_find_ino(sc, ea);
    259      1.1     mrg 
    260      1.1     mrg 	return (0);
    261      1.1     mrg }
    262      1.1     mrg 
    263      1.1     mrg void
    264      1.1     mrg ebus_destroy_attach_args(ea)
    265      1.1     mrg 	struct ebus_attach_args	*ea;
    266      1.1     mrg {
    267      1.1     mrg 
    268      1.1     mrg 	if (ea->ea_name)
    269      1.1     mrg 		free((void *)ea->ea_name, M_DEVBUF);
    270      1.1     mrg 	if (ea->ea_regs)
    271      1.1     mrg 		free((void *)ea->ea_regs, M_DEVBUF);
    272      1.1     mrg 	if (ea->ea_intrs)
    273      1.1     mrg 		free((void *)ea->ea_intrs, M_DEVBUF);
    274      1.1     mrg 	if (ea->ea_vaddrs)
    275      1.1     mrg 		free((void *)ea->ea_vaddrs, M_DEVBUF);
    276      1.1     mrg }
    277      1.1     mrg 
    278      1.1     mrg int
    279      1.1     mrg ebus_print(aux, p)
    280      1.1     mrg 	void *aux;
    281      1.1     mrg 	const char *p;
    282      1.1     mrg {
    283      1.1     mrg 	struct ebus_attach_args *ea = aux;
    284  1.3.4.1  bouyer 	int i;
    285      1.1     mrg 
    286      1.1     mrg 	if (p)
    287      1.1     mrg 		printf("%s at %s", ea->ea_name, p);
    288  1.3.4.1  bouyer 	for (i = 0; i < ea->ea_nregs; i++)
    289  1.3.4.1  bouyer 		printf(" addr %x-%x", ea->ea_regs[i].lo,
    290  1.3.4.1  bouyer 		    ea->ea_regs[i].lo + ea->ea_regs[i].size - 1);
    291  1.3.4.1  bouyer 	for (i = 0; i < ea->ea_nintrs; i++)
    292  1.3.4.1  bouyer 		printf(" ipl %d", ea->ea_intrs[i]);
    293      1.1     mrg 	return (UNCONF);
    294      1.3     mrg }
    295      1.3     mrg 
    296      1.3     mrg /*
    297      1.3     mrg  * find the INO values for each interrupt and fill them in.
    298      1.3     mrg  *
    299      1.3     mrg  * for each "reg" property of this device, mask it's hi and lo
    300      1.3     mrg  * values with the "interrupt-map-mask"'s hi/lo values, and also
    301      1.3     mrg  * mask the interrupt number with the interrupt mask.  search the
    302      1.3     mrg  * "interrupt-map" list for matching values of hi, lo and interrupt
    303      1.3     mrg  * to give the INO for this interrupt.
    304      1.3     mrg  */
    305      1.3     mrg void
    306      1.3     mrg ebus_find_ino(sc, ea)
    307      1.3     mrg 	struct ebus_softc *sc;
    308      1.3     mrg 	struct ebus_attach_args *ea;
    309      1.3     mrg {
    310      1.3     mrg 	u_int32_t hi, lo, intr;
    311      1.3     mrg 	int i, j, k;
    312      1.3     mrg 
    313  1.3.4.1  bouyer 	if (sc->sc_nintmap == 0) {
    314  1.3.4.1  bouyer 		/*
    315  1.3.4.1  bouyer 		 * If there is no interrupt map in the ebus node,
    316  1.3.4.1  bouyer 		 * assume that the child's `interrupt' property is
    317  1.3.4.1  bouyer 		 * already in a format suitable for the parent bus.
    318  1.3.4.1  bouyer 		 */
    319  1.3.4.1  bouyer 		return;
    320  1.3.4.1  bouyer 	}
    321  1.3.4.1  bouyer 
    322      1.3     mrg 	DPRINTF(EDB_INTRMAP, ("ebus_find_ino: searching %d interrupts", ea->ea_nintrs));
    323  1.3.4.1  bouyer 
    324      1.3     mrg 	for (j = 0; j < ea->ea_nintrs; j++) {
    325  1.3.4.1  bouyer 
    326      1.3     mrg 		intr = ea->ea_intrs[j] & sc->sc_intmapmask.intr;
    327      1.3     mrg 
    328      1.3     mrg 		DPRINTF(EDB_INTRMAP, ("; intr %x masked to %x", ea->ea_intrs[j], intr));
    329      1.3     mrg 		for (i = 0; i < ea->ea_nregs; i++) {
    330      1.3     mrg 			hi = ea->ea_regs[i].hi & sc->sc_intmapmask.hi;
    331      1.3     mrg 			lo = ea->ea_regs[i].lo & sc->sc_intmapmask.lo;
    332      1.3     mrg 
    333  1.3.4.2  bouyer 			DPRINTF(EDB_INTRMAP, ("; reg hi.lo %08x.%08x masked to %08x.%08x", ea->ea_regs[i].hi, ea->ea_regs[i].lo, hi, lo));
    334      1.3     mrg 			for (k = 0; k < sc->sc_nintmap; k++) {
    335      1.3     mrg 				DPRINTF(EDB_INTRMAP, ("; checking hi.lo %08x.%08x intr %x", sc->sc_intmap[k].hi, sc->sc_intmap[k].lo, sc->sc_intmap[k].intr));
    336      1.3     mrg 				if (hi == sc->sc_intmap[k].hi &&
    337      1.3     mrg 				    lo == sc->sc_intmap[k].lo &&
    338      1.3     mrg 				    intr == sc->sc_intmap[k].intr) {
    339  1.3.4.1  bouyer 					ea->ea_intrs[j] =
    340  1.3.4.1  bouyer 						sc->sc_intmap[k].cintr|INTMAP_OBIO;
    341      1.3     mrg 					DPRINTF(EDB_INTRMAP, ("; FOUND IT! changing to %d\n", sc->sc_intmap[k].cintr));
    342      1.3     mrg 					goto next_intr;
    343      1.3     mrg 				}
    344      1.3     mrg 			}
    345      1.3     mrg 		}
    346      1.3     mrg next_intr:
    347      1.3     mrg 	}
    348      1.1     mrg }
    349      1.1     mrg 
    350      1.1     mrg /*
    351  1.3.4.1  bouyer  * what is our OFW node?  this depends on our pci chipset tag
    352  1.3.4.1  bouyer  * having it's "node" value set to the OFW node of the PCI bus,
    353  1.3.4.1  bouyer  * see the simba driver.
    354      1.1     mrg  */
    355      1.1     mrg int
    356  1.3.4.1  bouyer ebus_find_node(pa)
    357      1.1     mrg 	struct pci_attach_args *pa;
    358      1.1     mrg {
    359      1.1     mrg 	int node = pa->pa_pc->node;
    360      1.1     mrg 	int n, *ap;
    361      1.1     mrg 	int pcibus, bus, dev, fn;
    362      1.1     mrg 
    363      1.1     mrg 	DPRINTF(EDB_PROM, ("ebus_find_node: looking at pci node %08x\n", node));
    364  1.3.4.3  bouyer 
    365  1.3.4.3  bouyer 	/* pull the PCI bus out of the pa_tag */
    366  1.3.4.3  bouyer 	pcibus = (pa->pa_tag >> 16) & 0xff;
    367  1.3.4.3  bouyer 	DPRINTF(EDB_PROM, ("; pcibus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
    368  1.3.4.3  bouyer 
    369      1.1     mrg 	for (node = firstchild(node); node; node = nextsibling(node)) {
    370      1.1     mrg 		char *name = getpropstring(node, "name");
    371      1.1     mrg 
    372  1.3.4.3  bouyer 		DPRINTF(EDB_PROM, ("; looking at PCI device `%s', node = %08x\n", name, node));
    373      1.1     mrg 		/* must be "ebus" */
    374      1.1     mrg 		if (strcmp(name, "ebus") != 0)
    375      1.1     mrg 			continue;
    376      1.1     mrg 
    377      1.1     mrg 		/* get the PCI bus/device/function for this node */
    378      1.2     mrg 		ap = NULL;
    379      1.1     mrg 		if (getprop(node, "reg", sizeof(int), &n,
    380      1.1     mrg 		    (void **)&ap))
    381      1.1     mrg 			continue;
    382      1.1     mrg 
    383      1.1     mrg 		bus = (ap[0] >> 16) & 0xff;
    384      1.1     mrg 		dev = (ap[0] >> 11) & 0x1f;
    385      1.1     mrg 		fn = (ap[0] >> 8) & 0x7;
    386  1.3.4.3  bouyer 		free(ap, M_DEVBUF);
    387      1.1     mrg 
    388  1.3.4.3  bouyer 		DPRINTF(EDB_PROM, ("; looking at ebus node: bus %d dev %d fn %d\n", bus, dev, fn));
    389      1.1     mrg 		if (pa->pa_device != dev ||
    390      1.1     mrg 		    pa->pa_function != fn ||
    391      1.1     mrg 		    pcibus != bus)
    392      1.1     mrg 			continue;
    393      1.1     mrg 
    394      1.1     mrg 		DPRINTF(EDB_PROM, ("; found it, returning %08x\n", node));
    395      1.1     mrg 		/* found it! */
    396      1.1     mrg 		return (node);
    397      1.1     mrg 	}
    398      1.1     mrg 
    399      1.1     mrg 	/* damn! */
    400      1.1     mrg 	return (0);
    401  1.3.4.1  bouyer }
    402  1.3.4.1  bouyer 
    403  1.3.4.1  bouyer /*
    404  1.3.4.1  bouyer  * bus space and bus dma below here
    405  1.3.4.1  bouyer  */
    406  1.3.4.1  bouyer bus_space_tag_t
    407  1.3.4.1  bouyer ebus_alloc_bus_tag(sc, type)
    408  1.3.4.1  bouyer 	struct ebus_softc *sc;
    409  1.3.4.1  bouyer 	int type;
    410  1.3.4.1  bouyer {
    411  1.3.4.1  bouyer 	bus_space_tag_t bt;
    412  1.3.4.1  bouyer 
    413  1.3.4.1  bouyer 	bt = (bus_space_tag_t)
    414  1.3.4.1  bouyer 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    415  1.3.4.1  bouyer 	if (bt == NULL)
    416  1.3.4.1  bouyer 		panic("could not allocate ebus bus tag");
    417  1.3.4.1  bouyer 
    418  1.3.4.1  bouyer 	bzero(bt, sizeof *bt);
    419  1.3.4.1  bouyer 	bt->cookie = sc;
    420  1.3.4.1  bouyer 	bt->parent = sc->sc_bustag;
    421  1.3.4.1  bouyer 	bt->type = type;
    422  1.3.4.1  bouyer 	bt->sparc_bus_map = _ebus_bus_map;
    423  1.3.4.1  bouyer 	bt->sparc_bus_mmap = ebus_bus_mmap;
    424  1.3.4.1  bouyer 	bt->sparc_intr_establish = ebus_intr_establish;
    425  1.3.4.1  bouyer 	return (bt);
    426  1.3.4.1  bouyer }
    427  1.3.4.1  bouyer 
    428  1.3.4.1  bouyer /* XXX? */
    429  1.3.4.1  bouyer bus_dma_tag_t
    430  1.3.4.1  bouyer ebus_alloc_dma_tag(sc, pdt)
    431  1.3.4.1  bouyer 	struct ebus_softc *sc;
    432  1.3.4.1  bouyer 	bus_dma_tag_t pdt;
    433  1.3.4.1  bouyer {
    434  1.3.4.1  bouyer 	bus_dma_tag_t dt;
    435  1.3.4.1  bouyer 
    436  1.3.4.1  bouyer 	dt = (bus_dma_tag_t)
    437  1.3.4.1  bouyer 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    438  1.3.4.1  bouyer 	if (dt == NULL)
    439  1.3.4.1  bouyer 		panic("could not allocate ebus dma tag");
    440  1.3.4.1  bouyer 
    441  1.3.4.1  bouyer 	bzero(dt, sizeof *dt);
    442  1.3.4.1  bouyer 	dt->_cookie = sc;
    443  1.3.4.1  bouyer 	dt->_parent = pdt;
    444  1.3.4.1  bouyer #define PCOPY(x)	dt->x = pdt->x
    445  1.3.4.1  bouyer 	PCOPY(_dmamap_create);
    446  1.3.4.1  bouyer 	PCOPY(_dmamap_destroy);
    447  1.3.4.1  bouyer 	dt->_dmamap_load = ebus_dmamap_load;
    448  1.3.4.1  bouyer 	PCOPY(_dmamap_load_mbuf);
    449  1.3.4.1  bouyer 	PCOPY(_dmamap_load_uio);
    450  1.3.4.1  bouyer 	PCOPY(_dmamap_load_raw);
    451  1.3.4.1  bouyer 	dt->_dmamap_unload = ebus_dmamap_unload;
    452  1.3.4.1  bouyer 	dt->_dmamap_sync = ebus_dmamap_sync;
    453  1.3.4.1  bouyer 	dt->_dmamem_alloc = ebus_dmamem_alloc;
    454  1.3.4.1  bouyer 	dt->_dmamem_free = ebus_dmamem_free;
    455  1.3.4.1  bouyer 	dt->_dmamem_map = ebus_dmamem_map;
    456  1.3.4.1  bouyer 	dt->_dmamem_unmap = ebus_dmamem_unmap;
    457  1.3.4.1  bouyer 	PCOPY(_dmamem_mmap);
    458  1.3.4.1  bouyer #undef	PCOPY
    459  1.3.4.1  bouyer 	return (dt);
    460  1.3.4.1  bouyer }
    461  1.3.4.1  bouyer 
    462  1.3.4.1  bouyer /*
    463  1.3.4.1  bouyer  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
    464  1.3.4.1  bouyer  * about PCI physical addresses, which also applies to ebus.
    465  1.3.4.1  bouyer  */
    466  1.3.4.1  bouyer static int
    467  1.3.4.1  bouyer _ebus_bus_map(t, btype, offset, size, flags, vaddr, hp)
    468  1.3.4.1  bouyer 	bus_space_tag_t t;
    469  1.3.4.1  bouyer 	bus_type_t btype;
    470  1.3.4.1  bouyer 	bus_addr_t offset;
    471  1.3.4.1  bouyer 	bus_size_t size;
    472  1.3.4.1  bouyer 	int	flags;
    473  1.3.4.1  bouyer 	vaddr_t vaddr;
    474  1.3.4.1  bouyer 	bus_space_handle_t *hp;
    475  1.3.4.1  bouyer {
    476  1.3.4.1  bouyer 	struct ebus_softc *sc = t->cookie;
    477  1.3.4.1  bouyer 	bus_addr_t hi, lo;
    478  1.3.4.1  bouyer 	int i;
    479  1.3.4.1  bouyer 
    480  1.3.4.2  bouyer 	DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_map: type %d off %016llx sz %x flags %d va %p", (int)t->type, (unsigned long long)offset, (int)size, (int)flags, (void *)vaddr));
    481  1.3.4.1  bouyer 
    482  1.3.4.1  bouyer 	hi = offset >> 32UL;
    483  1.3.4.1  bouyer 	lo = offset & 0xffffffff;
    484  1.3.4.1  bouyer 	DPRINTF(EDB_BUSMAP, (" (hi %08x lo %08x)", (u_int)hi, (u_int)lo));
    485  1.3.4.1  bouyer 	for (i = 0; i < sc->sc_nrange; i++) {
    486  1.3.4.1  bouyer 		bus_addr_t pciaddr;
    487  1.3.4.1  bouyer 
    488  1.3.4.1  bouyer 		if (hi != sc->sc_range[i].child_hi)
    489  1.3.4.1  bouyer 			continue;
    490  1.3.4.1  bouyer 		if (lo < sc->sc_range[i].child_lo ||
    491  1.3.4.1  bouyer 		    (lo + size) > (sc->sc_range[i].child_lo + sc->sc_range[i].size))
    492  1.3.4.1  bouyer 			continue;
    493  1.3.4.1  bouyer 
    494  1.3.4.1  bouyer 		pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) |
    495  1.3.4.1  bouyer 				       sc->sc_range[i].phys_lo;
    496  1.3.4.1  bouyer 		pciaddr += lo;
    497  1.3.4.1  bouyer 		DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_map: mapping paddr offset %qx pciaddr %qx\n",
    498  1.3.4.2  bouyer 			       (unsigned long long)offset,
    499  1.3.4.2  bouyer 			       (unsigned long long)pciaddr));
    500  1.3.4.1  bouyer 		/* pass it onto the psycho */
    501  1.3.4.1  bouyer 		return (bus_space_map2(sc->sc_bustag, t->type, pciaddr,
    502  1.3.4.1  bouyer 					size, flags, vaddr, hp));
    503  1.3.4.1  bouyer 	}
    504  1.3.4.1  bouyer 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
    505  1.3.4.1  bouyer 	return (EINVAL);
    506  1.3.4.1  bouyer }
    507  1.3.4.1  bouyer 
    508  1.3.4.1  bouyer static int
    509  1.3.4.1  bouyer ebus_bus_mmap(t, btype, paddr, flags, hp)
    510  1.3.4.1  bouyer 	bus_space_tag_t t;
    511  1.3.4.1  bouyer 	bus_type_t btype;
    512  1.3.4.1  bouyer 	bus_addr_t paddr;
    513  1.3.4.1  bouyer 	int flags;
    514  1.3.4.1  bouyer 	bus_space_handle_t *hp;
    515  1.3.4.1  bouyer {
    516  1.3.4.1  bouyer 	bus_addr_t offset = paddr;
    517  1.3.4.1  bouyer 	struct ebus_softc *sc = t->cookie;
    518  1.3.4.1  bouyer 	int i;
    519  1.3.4.1  bouyer 
    520  1.3.4.1  bouyer 	for (i = 0; i < sc->sc_nrange; i++) {
    521  1.3.4.1  bouyer 		bus_addr_t paddr = ((bus_addr_t)sc->sc_range[i].child_hi << 32) |
    522  1.3.4.1  bouyer 		    sc->sc_range[i].child_lo;
    523  1.3.4.1  bouyer 
    524  1.3.4.1  bouyer 		if (offset != paddr)
    525  1.3.4.1  bouyer 			continue;
    526  1.3.4.1  bouyer 
    527  1.3.4.2  bouyer 		DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n", (unsigned long long)paddr));
    528  1.3.4.1  bouyer 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    529  1.3.4.1  bouyer 				       flags, hp));
    530  1.3.4.1  bouyer 	}
    531  1.3.4.1  bouyer 
    532  1.3.4.1  bouyer 	return (-1);
    533  1.3.4.1  bouyer }
    534  1.3.4.1  bouyer 
    535  1.3.4.1  bouyer /*
    536  1.3.4.1  bouyer  * install an interrupt handler for a PCI device
    537  1.3.4.1  bouyer  */
    538  1.3.4.1  bouyer void *
    539  1.3.4.1  bouyer ebus_intr_establish(t, pri, level, flags, handler, arg)
    540  1.3.4.1  bouyer 	bus_space_tag_t t;
    541  1.3.4.1  bouyer 	int pri;
    542  1.3.4.1  bouyer 	int level;
    543  1.3.4.1  bouyer 	int flags;
    544  1.3.4.1  bouyer 	int (*handler) __P((void *));
    545  1.3.4.1  bouyer 	void *arg;
    546  1.3.4.1  bouyer {
    547  1.3.4.1  bouyer 	return (bus_intr_establish(t->parent, pri, level, flags, handler, arg));
    548  1.3.4.1  bouyer }
    549  1.3.4.1  bouyer 
    550  1.3.4.1  bouyer /*
    551  1.3.4.1  bouyer  * bus dma support
    552  1.3.4.1  bouyer  */
    553  1.3.4.1  bouyer int
    554  1.3.4.1  bouyer ebus_dmamap_load(t, map, buf, buflen, p, flags)
    555  1.3.4.1  bouyer 	bus_dma_tag_t t;
    556  1.3.4.1  bouyer 	bus_dmamap_t map;
    557  1.3.4.1  bouyer 	void *buf;
    558  1.3.4.1  bouyer 	bus_size_t buflen;
    559  1.3.4.1  bouyer 	struct proc *p;
    560  1.3.4.1  bouyer 	int flags;
    561  1.3.4.1  bouyer {
    562  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    563  1.3.4.1  bouyer 
    564  1.3.4.1  bouyer 	return (iommu_dvmamap_load(t, sc->sc_parent->sc_is, map, buf, buflen,
    565  1.3.4.1  bouyer 	    p, flags));
    566  1.3.4.1  bouyer }
    567  1.3.4.1  bouyer 
    568  1.3.4.1  bouyer void
    569  1.3.4.1  bouyer ebus_dmamap_unload(t, map)
    570  1.3.4.1  bouyer 	bus_dma_tag_t t;
    571  1.3.4.1  bouyer 	bus_dmamap_t map;
    572  1.3.4.1  bouyer {
    573  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    574  1.3.4.1  bouyer 
    575  1.3.4.1  bouyer 	iommu_dvmamap_unload(t, sc->sc_parent->sc_is, map);
    576  1.3.4.1  bouyer }
    577  1.3.4.1  bouyer 
    578  1.3.4.1  bouyer void
    579  1.3.4.1  bouyer ebus_dmamap_sync(t, map, offset, len, ops)
    580  1.3.4.1  bouyer 	bus_dma_tag_t t;
    581  1.3.4.1  bouyer 	bus_dmamap_t map;
    582  1.3.4.1  bouyer 	bus_addr_t offset;
    583  1.3.4.1  bouyer 	bus_size_t len;
    584  1.3.4.1  bouyer 	int ops;
    585  1.3.4.1  bouyer {
    586  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    587  1.3.4.1  bouyer 
    588  1.3.4.1  bouyer 	iommu_dvmamap_sync(t, sc->sc_parent->sc_is, map, offset, len, ops);
    589  1.3.4.1  bouyer 	bus_dmamap_sync(t->_parent, map, offset, len, ops);
    590  1.3.4.1  bouyer }
    591  1.3.4.1  bouyer 
    592  1.3.4.1  bouyer int
    593  1.3.4.1  bouyer ebus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    594  1.3.4.1  bouyer 	bus_dma_tag_t t;
    595  1.3.4.1  bouyer 	bus_size_t size;
    596  1.3.4.1  bouyer 	bus_size_t alignment;
    597  1.3.4.1  bouyer 	bus_size_t boundary;
    598  1.3.4.1  bouyer 	bus_dma_segment_t *segs;
    599  1.3.4.1  bouyer 	int nsegs;
    600  1.3.4.1  bouyer 	int *rsegs;
    601  1.3.4.1  bouyer 	int flags;
    602  1.3.4.1  bouyer {
    603  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    604  1.3.4.1  bouyer 
    605  1.3.4.1  bouyer 	return (iommu_dvmamem_alloc(t, sc->sc_parent->sc_is, size, alignment,
    606  1.3.4.1  bouyer 	    boundary, segs, nsegs, rsegs, flags));
    607  1.3.4.1  bouyer }
    608  1.3.4.1  bouyer 
    609  1.3.4.1  bouyer void
    610  1.3.4.1  bouyer ebus_dmamem_free(t, segs, nsegs)
    611  1.3.4.1  bouyer 	bus_dma_tag_t t;
    612  1.3.4.1  bouyer 	bus_dma_segment_t *segs;
    613  1.3.4.1  bouyer 	int nsegs;
    614  1.3.4.1  bouyer {
    615  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    616  1.3.4.1  bouyer 
    617  1.3.4.1  bouyer 	iommu_dvmamem_free(t, sc->sc_parent->sc_is, segs, nsegs);
    618  1.3.4.1  bouyer }
    619  1.3.4.1  bouyer 
    620  1.3.4.1  bouyer int
    621  1.3.4.1  bouyer ebus_dmamem_map(t, segs, nsegs, size, kvap, flags)
    622  1.3.4.1  bouyer 	bus_dma_tag_t t;
    623  1.3.4.1  bouyer 	bus_dma_segment_t *segs;
    624  1.3.4.1  bouyer 	int nsegs;
    625  1.3.4.1  bouyer 	size_t size;
    626  1.3.4.1  bouyer 	caddr_t *kvap;
    627  1.3.4.1  bouyer 	int flags;
    628  1.3.4.1  bouyer {
    629  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    630  1.3.4.1  bouyer 
    631  1.3.4.1  bouyer 	return (iommu_dvmamem_map(t, sc->sc_parent->sc_is, segs, nsegs,
    632  1.3.4.1  bouyer 	    size, kvap, flags));
    633  1.3.4.1  bouyer }
    634  1.3.4.1  bouyer 
    635  1.3.4.1  bouyer void
    636  1.3.4.1  bouyer ebus_dmamem_unmap(t, kva, size)
    637  1.3.4.1  bouyer 	bus_dma_tag_t t;
    638  1.3.4.1  bouyer 	caddr_t kva;
    639  1.3.4.1  bouyer 	size_t size;
    640  1.3.4.1  bouyer {
    641  1.3.4.1  bouyer 	struct ebus_softc *sc = t->_cookie;
    642  1.3.4.1  bouyer 
    643  1.3.4.1  bouyer 	iommu_dvmamem_unmap(t, sc->sc_parent->sc_is, kva, size);
    644      1.1     mrg }
    645