Home | History | Annotate | Line # | Download | only in dev
psycho.c revision 1.28
      1  1.28    fvdl /*	$NetBSD: psycho.c,v 1.28 2000/12/04 20:29:34 fvdl Exp $	*/
      2   1.1     mrg 
      3   1.1     mrg /*
      4   1.3     mrg  * 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.7     mrg #include "opt_ddb.h"
     32   1.7     mrg 
     33   1.1     mrg /*
     34   1.1     mrg  * PCI support for UltraSPARC `psycho'
     35   1.1     mrg  */
     36   1.1     mrg 
     37   1.1     mrg #undef DEBUG
     38   1.1     mrg #define DEBUG
     39   1.1     mrg 
     40   1.1     mrg #ifdef DEBUG
     41   1.7     mrg #define PDB_PROM	0x01
     42   1.7     mrg #define PDB_IOMMU	0x02
     43   1.7     mrg #define PDB_BUSMAP	0x04
     44   1.7     mrg #define PDB_BUSDMA	0x08
     45   1.7     mrg #define PDB_INTR	0x10
     46   1.3     mrg int psycho_debug = 0x0;
     47   1.1     mrg #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
     48   1.1     mrg #else
     49   1.1     mrg #define DPRINTF(l, s)
     50   1.1     mrg #endif
     51   1.1     mrg 
     52   1.1     mrg #include <sys/param.h>
     53   1.7     mrg #include <sys/device.h>
     54   1.7     mrg #include <sys/errno.h>
     55   1.1     mrg #include <sys/extent.h>
     56   1.7     mrg #include <sys/malloc.h>
     57   1.7     mrg #include <sys/systm.h>
     58   1.1     mrg #include <sys/time.h>
     59   1.1     mrg 
     60   1.1     mrg #define _SPARC_BUS_DMA_PRIVATE
     61   1.1     mrg #include <machine/bus.h>
     62   1.1     mrg #include <machine/autoconf.h>
     63  1.18     eeh #include <machine/psl.h>
     64   1.1     mrg 
     65   1.1     mrg #include <dev/pci/pcivar.h>
     66   1.1     mrg #include <dev/pci/pcireg.h>
     67   1.1     mrg 
     68   1.1     mrg #include <sparc64/dev/iommureg.h>
     69   1.1     mrg #include <sparc64/dev/iommuvar.h>
     70   1.1     mrg #include <sparc64/dev/psychoreg.h>
     71   1.1     mrg #include <sparc64/dev/psychovar.h>
     72   1.7     mrg #include <sparc64/sparc64/cache.h>
     73   1.1     mrg 
     74   1.8     mrg #include "ioconf.h"
     75   1.8     mrg 
     76   1.1     mrg static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
     77   1.1     mrg 						   pci_chipset_tag_t));
     78   1.1     mrg static void psycho_get_bus_range __P((int, int *));
     79   1.1     mrg static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
     80   1.1     mrg static void psycho_get_registers __P((int, struct psycho_registers **, int *));
     81   1.1     mrg static void psycho_get_intmap __P((int, struct psycho_interrupt_map **, int *));
     82   1.1     mrg static void psycho_get_intmapmask __P((int, struct psycho_interrupt_map_mask *));
     83   1.1     mrg 
     84   1.1     mrg /* IOMMU support */
     85  1.13     eeh static void psycho_iommu_init __P((struct psycho_softc *, int));
     86   1.1     mrg 
     87   1.7     mrg /*
     88   1.7     mrg  * bus space and bus dma support for UltraSPARC `psycho'.  note that most
     89   1.7     mrg  * of the bus dma support is provided by the iommu dvma controller.
     90   1.7     mrg  */
     91   1.7     mrg static int psycho_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
     92   1.7     mrg 				int, bus_space_handle_t *));
     93   1.7     mrg static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
     94   1.7     mrg 				bus_size_t, int, vaddr_t,
     95   1.7     mrg 				bus_space_handle_t *));
     96  1.21      pk static void *psycho_intr_establish __P((bus_space_tag_t, int, int, int,
     97   1.7     mrg 				int (*) __P((void *)), void *));
     98   1.7     mrg 
     99   1.7     mrg static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    100   1.7     mrg 				   bus_size_t, struct proc *, int));
    101   1.7     mrg static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    102   1.9     eeh static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    103   1.9     eeh 		    bus_dma_segment_t *, int, bus_size_t, int));
    104   1.7     mrg static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    105   1.7     mrg 				    bus_size_t, int));
    106   1.7     mrg int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
    107   1.7     mrg 			     bus_dma_segment_t *, int, int *, int));
    108   1.7     mrg void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
    109   1.7     mrg int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
    110   1.7     mrg 			   caddr_t *, int));
    111   1.7     mrg void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
    112   1.7     mrg 
    113   1.7     mrg /* base pci_chipset */
    114   1.1     mrg extern struct sparc_pci_chipset _sparc_pci_chipset;
    115   1.1     mrg 
    116   1.1     mrg /*
    117   1.1     mrg  * autoconfiguration
    118   1.1     mrg  */
    119   1.1     mrg static	int	psycho_match __P((struct device *, struct cfdata *, void *));
    120   1.1     mrg static	void	psycho_attach __P((struct device *, struct device *, void *));
    121   1.1     mrg static	int	psycho_print __P((void *aux, const char *p));
    122   1.1     mrg 
    123  1.22      pk static	void	sabre_init __P((struct psycho_softc *,
    124  1.22      pk 				struct mainbus_attach_args *,
    125  1.22      pk 				struct pcibus_attach_args *));
    126  1.22      pk static	void	psycho_init __P((struct psycho_softc *,
    127  1.22      pk 				struct mainbus_attach_args *,
    128  1.22      pk 				struct pcibus_attach_args *));
    129   1.1     mrg 
    130   1.1     mrg struct cfattach psycho_ca = {
    131   1.1     mrg         sizeof(struct psycho_softc), psycho_match, psycho_attach
    132   1.1     mrg };
    133   1.1     mrg 
    134   1.1     mrg /*
    135   1.1     mrg  * "sabre" is the UltraSPARC IIi onboard PCI interface, normally connected to
    136   1.1     mrg  * an APB (advanced PCI bridge), which was designed specifically for the IIi.
    137   1.1     mrg  * the APB appears as two "simba"'s underneath the sabre.  real devices
    138   1.1     mrg  * typically appear on the "simba"'s only.
    139   1.1     mrg  *
    140   1.1     mrg  * a pair of "psycho"s sit on the mainbus and have real devices attached to
    141   1.1     mrg  * them.  they implemented in the U2P (UPA to PCI).  these two devices share
    142   1.1     mrg  * register space and as such need to be configured together, even though the
    143   1.1     mrg  * autoconfiguration will attach them separately.
    144   1.1     mrg  *
    145   1.1     mrg  * each of these appears as two usable PCI busses, though the sabre itself
    146   1.1     mrg  * takes pci0 in this case, leaving real devices on pci1 and pci2.  there can
    147   1.1     mrg  * be multiple pairs of psycho's, however, in multi-board machines.
    148   1.1     mrg  */
    149   1.1     mrg #define	ROM_PCI_NAME		"pci"
    150   1.1     mrg #define ROM_SABRE_MODEL		"SUNW,sabre"
    151   1.1     mrg #define ROM_SIMBA_MODEL		"SUNW,simba"
    152   1.1     mrg #define ROM_PSYCHO_MODEL	"SUNW,psycho"
    153   1.1     mrg 
    154   1.1     mrg static	int
    155   1.1     mrg psycho_match(parent, match, aux)
    156   1.1     mrg 	struct device	*parent;
    157   1.1     mrg 	struct cfdata	*match;
    158   1.1     mrg 	void		*aux;
    159   1.1     mrg {
    160   1.1     mrg 	struct mainbus_attach_args *ma = aux;
    161   1.1     mrg 	char *model = getpropstring(ma->ma_node, "model");
    162   1.1     mrg 
    163   1.1     mrg 	/* match on a name of "pci" and a sabre or a psycho */
    164   1.1     mrg 	if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0 &&
    165   1.1     mrg 	    (strcmp(model, ROM_SABRE_MODEL) == 0 ||
    166   1.1     mrg 	     strcmp(model, ROM_PSYCHO_MODEL) == 0))
    167   1.1     mrg 		return (1);
    168   1.1     mrg 
    169   1.1     mrg 	return (0);
    170   1.1     mrg }
    171   1.1     mrg 
    172   1.1     mrg static	void
    173   1.1     mrg psycho_attach(parent, self, aux)
    174   1.1     mrg 	struct device *parent, *self;
    175   1.1     mrg 	void *aux;
    176   1.1     mrg {
    177   1.1     mrg 	struct psycho_softc *sc = (struct psycho_softc *)self;
    178   1.1     mrg 	struct pcibus_attach_args pba;
    179   1.1     mrg 	struct mainbus_attach_args *ma = aux;
    180   1.1     mrg 	char *model = getpropstring(ma->ma_node, "model");
    181   1.1     mrg 
    182   1.1     mrg 	printf("\n");
    183   1.1     mrg 
    184   1.1     mrg 	sc->sc_node = ma->ma_node;
    185   1.1     mrg 	sc->sc_bustag = ma->ma_bustag;
    186   1.1     mrg 	sc->sc_dmatag = ma->ma_dmatag;
    187   1.1     mrg 
    188   1.1     mrg 	/*
    189   1.1     mrg 	 * pull in all the information about the psycho as we can.
    190   1.1     mrg 	 */
    191   1.1     mrg 
    192   1.1     mrg 	/*
    193   1.1     mrg 	 * call the model-specific initialisation routine.
    194   1.1     mrg 	 */
    195   1.1     mrg 	if (strcmp(model, ROM_SABRE_MODEL) == 0)
    196  1.22      pk 		sabre_init(sc, ma, &pba);
    197   1.1     mrg 	else if (strcmp(model, ROM_PSYCHO_MODEL) == 0)
    198  1.22      pk 		psycho_init(sc, ma, &pba);
    199   1.1     mrg #ifdef DIAGNOSTIC
    200   1.1     mrg 	else
    201   1.1     mrg 		panic("psycho_attach: unknown model %s?", model);
    202   1.1     mrg #endif
    203   1.1     mrg 
    204   1.1     mrg 	/*
    205   1.1     mrg 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
    206   1.1     mrg 	 */
    207   1.1     mrg 	pba.pba_busname = "pci";
    208   1.1     mrg 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
    209   1.1     mrg 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
    210   1.1     mrg 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
    211   1.1     mrg 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
    212   1.1     mrg 
    213   1.1     mrg 	config_found(self, &pba, psycho_print);
    214   1.1     mrg }
    215   1.1     mrg 
    216   1.1     mrg static	int
    217   1.1     mrg psycho_print(aux, p)
    218   1.1     mrg 	void *aux;
    219   1.1     mrg 	const char *p;
    220   1.1     mrg {
    221   1.1     mrg 
    222   1.1     mrg 	if (p == NULL)
    223   1.1     mrg 		return (UNCONF);
    224   1.1     mrg 	return (QUIET);
    225   1.1     mrg }
    226   1.1     mrg 
    227   1.1     mrg /*
    228   1.1     mrg  * SUNW,sabre initialisation ..
    229   1.1     mrg  *	- get the sabre's ranges.  this are used for both simba's.
    230   1.1     mrg  *	- find the two SUNW,simba's underneath (a and b)
    231   1.1     mrg  *	- work out which simba is which via the bus-range property
    232   1.1     mrg  *	- get each simba's interrupt-map and interrupt-map-mask.
    233   1.1     mrg  *	- turn on the iommu
    234   1.1     mrg  */
    235   1.1     mrg static void
    236  1.22      pk sabre_init(sc, ma, pba)
    237   1.1     mrg 	struct psycho_softc *sc;
    238  1.22      pk 	struct mainbus_attach_args *ma;
    239   1.1     mrg 	struct pcibus_attach_args *pba;
    240   1.1     mrg {
    241   1.1     mrg 	struct psycho_pbm *pp;
    242   1.3     mrg 	bus_space_handle_t bh;
    243   1.1     mrg 	u_int64_t csr;
    244  1.14     eeh 	unsigned int node;
    245   1.1     mrg 	int sabre_br[2], simba_br[2];
    246   1.1     mrg 
    247  1.22      pk 	/*
    248  1.22      pk 	 * The sabre gets two register banks:
    249  1.22      pk 	 * (0) per-PBM PCI configuration space, containing only the
    250  1.22      pk 	 *     PBM 256-byte PCI header
    251  1.22      pk 	 * (1) the shared psycho configuration registers (struct psychoreg)
    252  1.22      pk 	 *
    253  1.22      pk 	 * XXX use the prom address for the psycho registers?  we do so far.
    254  1.22      pk 	 */
    255  1.22      pk 	sc->sc_regs = (struct psychoreg *)(u_long)ma->ma_address[0];
    256  1.22      pk 	sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
    257  1.24      pk 	sc->sc_ign = 0x7c0; /* XXX - try not to hardcode? */
    258  1.22      pk 
    259   1.1     mrg 	/* who? said a voice, incredulous */
    260   1.1     mrg 	sc->sc_mode = PSYCHO_MODE_SABRE;
    261   1.1     mrg 	printf("sabre: ");
    262   1.1     mrg 
    263   1.1     mrg 	/* setup the PCI control register; there is only one for the sabre */
    264  1.12     eeh 	csr = bus_space_read_8(sc->sc_bustag, (bus_space_handle_t)(u_long)
    265  1.12     eeh 		&sc->sc_regs->psy_pcictl[0].pci_csr, 0);
    266   1.5     mrg 	csr |= PCICTL_MRLM |
    267   1.5     mrg 	       PCICTL_ARB_PARK |
    268   1.5     mrg 	       PCICTL_ERRINTEN |
    269   1.5     mrg 	       PCICTL_4ENABLE;
    270   1.5     mrg 	csr &= ~(PCICTL_SERR |
    271   1.5     mrg 		 PCICTL_CPU_PRIO |
    272   1.5     mrg 		 PCICTL_ARB_PRIO |
    273   1.5     mrg 		 PCICTL_RTRYWAIT);
    274  1.25     mrg 	bus_space_write_8(sc->sc_bustag,
    275  1.28    fvdl 	    (bus_space_handle_t)(u_long)&sc->sc_regs->psy_pcictl[0].pci_csr,
    276  1.28    fvdl 	    0, csr);
    277   1.1     mrg 
    278   1.1     mrg 	/* allocate a pair of psycho_pbm's for our simba's */
    279   1.1     mrg 	sc->sc_sabre = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
    280   1.1     mrg 	sc->sc_simba_a = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
    281   1.1     mrg 	sc->sc_simba_b = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
    282   1.5     mrg 	if (sc->sc_sabre == NULL || sc->sc_simba_a == NULL ||
    283   1.5     mrg 	    sc->sc_simba_b == NULL)
    284   1.1     mrg 		panic("could not allocate simba pbm's");
    285   1.1     mrg 
    286   1.1     mrg 	memset(sc->sc_sabre, 0, sizeof *pp);
    287   1.1     mrg 	memset(sc->sc_simba_a, 0, sizeof *pp);
    288   1.1     mrg 	memset(sc->sc_simba_b, 0, sizeof *pp);
    289   1.1     mrg 
    290   1.1     mrg 	/* grab the sabre ranges; use them for both simba's */
    291   1.1     mrg 	psycho_get_ranges(sc->sc_node, &sc->sc_sabre->pp_range,
    292   1.1     mrg 	    &sc->sc_sabre->pp_nrange);
    293   1.1     mrg 	sc->sc_simba_b->pp_range = sc->sc_simba_a->pp_range =
    294   1.1     mrg 	    sc->sc_sabre->pp_range;
    295   1.1     mrg 	sc->sc_simba_b->pp_nrange = sc->sc_simba_a->pp_nrange =
    296   1.1     mrg 	    sc->sc_sabre->pp_nrange;
    297   1.1     mrg 
    298   1.1     mrg 	/* get the bus-range for the sabre.  we expect 0..2 */
    299   1.1     mrg 	psycho_get_bus_range(sc->sc_node, sabre_br);
    300   1.1     mrg 
    301   1.1     mrg 	pba->pba_bus = sabre_br[0];
    302   1.1     mrg 
    303   1.1     mrg 	printf("bus range %u to %u", sabre_br[0], sabre_br[1]);
    304   1.1     mrg 
    305   1.1     mrg 	for (node = firstchild(sc->sc_node); node; node = nextsibling(node)) {
    306   1.1     mrg 		char *name = getpropstring(node, "name");
    307   1.1     mrg 		char *model, who;
    308  1.14     eeh 		struct psycho_registers *regs = NULL;
    309  1.12     eeh 		int nregs, fn;
    310   1.1     mrg 
    311   1.1     mrg 		if (strcmp(name, ROM_PCI_NAME) != 0)
    312   1.1     mrg 			continue;
    313   1.1     mrg 
    314   1.1     mrg 		model = getpropstring(node, "model");
    315   1.1     mrg 		if (strcmp(model, ROM_SIMBA_MODEL) != 0)
    316   1.1     mrg 			continue;
    317   1.1     mrg 
    318   1.1     mrg 		psycho_get_bus_range(node, simba_br);
    319  1.12     eeh 		psycho_get_registers(node, &regs, &nregs);
    320   1.1     mrg 
    321  1.12     eeh 		fn = TAG2FN(regs->phys_hi);
    322  1.12     eeh 		switch (fn) {
    323  1.12     eeh 		case 0:
    324  1.12     eeh 			pp = sc->sc_simba_a;
    325  1.12     eeh 			who = 'a';
    326  1.12     eeh 			pp->pp_regs = regs;
    327  1.12     eeh 			pp->pp_nregs = nregs;
    328  1.12     eeh 			break;
    329  1.12     eeh 		case 1:
    330   1.1     mrg 			pp = sc->sc_simba_b;
    331   1.1     mrg 			who = 'b';
    332  1.12     eeh 			pp->pp_regs = regs;
    333  1.12     eeh 			pp->pp_nregs = nregs;
    334  1.12     eeh 			break;
    335  1.12     eeh 		default:
    336  1.27    fvdl 			panic("illegal simba funcion %d\n", fn);
    337   1.1     mrg 		}
    338   1.8     mrg 		pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
    339   1.1     mrg 		/* link us in .. */
    340   1.1     mrg 		pp->pp_sc = sc;
    341   1.1     mrg 
    342   1.1     mrg 		printf("; simba %c, PCI bus %d", who, simba_br[0]);
    343   1.1     mrg 
    344   1.1     mrg 		/* grab the simba registers, interrupt map and map mask */
    345   1.1     mrg 		psycho_get_intmap(node, &pp->pp_intmap, &pp->pp_nintmap);
    346   1.1     mrg 		psycho_get_intmapmask(node, &pp->pp_intmapmask);
    347   1.1     mrg 
    348   1.1     mrg 		/* allocate our tags */
    349   1.1     mrg 		pp->pp_memt = psycho_alloc_mem_tag(pp);
    350   1.1     mrg 		pp->pp_iot = psycho_alloc_io_tag(pp);
    351   1.1     mrg 		pp->pp_dmat = psycho_alloc_dma_tag(pp);
    352   1.1     mrg 		pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
    353   1.1     mrg 			       (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
    354   1.1     mrg 
    355   1.1     mrg 		/* allocate a chipset for this */
    356   1.1     mrg 		pp->pp_pc = psycho_alloc_chipset(pp, node, &_sparc_pci_chipset);
    357  1.12     eeh 		pp->pp_pc->busno = pp->pp_bus = simba_br[0];
    358   1.1     mrg 	}
    359   1.1     mrg 
    360   1.1     mrg 	/* setup the rest of the sabre pbm */
    361   1.1     mrg 	pp = sc->sc_sabre;
    362   1.1     mrg 	pp->pp_sc = sc;
    363   1.1     mrg 	pp->pp_memt = sc->sc_psycho_this->pp_memt;
    364   1.1     mrg 	pp->pp_iot = sc->sc_psycho_this->pp_iot;
    365   1.1     mrg 	pp->pp_dmat = sc->sc_psycho_this->pp_dmat;
    366   1.1     mrg 	pp->pp_flags = sc->sc_psycho_this->pp_flags;
    367   1.1     mrg 	pp->pp_intmap = NULL;
    368   1.1     mrg 	pp->pp_regs = NULL;
    369   1.1     mrg 	pp->pp_pcictl = sc->sc_psycho_this->pp_pcictl;
    370   1.1     mrg 	pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node,
    371   1.1     mrg 	    sc->sc_psycho_this->pp_pc);
    372   1.1     mrg 
    373   1.1     mrg 	printf("\n");
    374   1.1     mrg 
    375  1.13     eeh 
    376  1.13     eeh 	/*
    377  1.13     eeh 	 * SABRE seems to be buggy.  It only appears to work with 128K IOTSB.
    378  1.13     eeh 	 * I have tried other sizes but they just don't seem to work.  Maybe
    379  1.13     eeh 	 * more testing is needed.
    380  1.13     eeh 	 *
    381  1.13     eeh 	 * The PROM reserves a certain amount of RAM for an IOTSB.  The
    382  1.13     eeh 	 * problem is that it's not necessarily the full 128K.  So we'll free
    383  1.13     eeh 	 * this space up and let iommu_init() allocate a full mapping.
    384  1.13     eeh 	 *
    385  1.13     eeh 	 * (Otherwise we would need to change the iommu code to handle a
    386  1.13     eeh 	 * preallocated TSB that may not cover the entire DVMA address
    387  1.13     eeh 	 * space...
    388  1.13     eeh 	 *
    389  1.13     eeh 	 * The information about this memory is shared between the
    390  1.13     eeh 	 * `virtual-dma' property, which describes the base and size of the
    391  1.13     eeh 	 * virtual region, and the IOMMU base address register which is the
    392  1.13     eeh 	 * only known pointer to the RAM.  To free up the memory you need to
    393  1.13     eeh 	 * read the base addres register and then calculate the size by taking
    394  1.13     eeh 	 * the virtual size and dividing it by 1K to get the size in bytes.
    395  1.13     eeh 	 * This range can then be freed up by calling uvm_page_physload().
    396  1.13     eeh 	 *
    397  1.13     eeh 	 */
    398  1.13     eeh 
    399   1.1     mrg 	/* and finally start up the IOMMU ... */
    400  1.13     eeh 	psycho_iommu_init(sc, 7);
    401   1.3     mrg 
    402   1.3     mrg 	/*
    403   1.3     mrg 	 * get us a config space tag, and punch in the physical address
    404   1.3     mrg 	 * of the PCI configuration space.  note that we use unmapped
    405   1.3     mrg 	 * access to PCI configuration space, relying on the bus space
    406   1.3     mrg 	 * macros to provide the proper ASI based on the bus tag.
    407   1.3     mrg 	 */
    408   1.3     mrg 	sc->sc_configtag = psycho_alloc_config_tag(sc->sc_simba_a);
    409   1.3     mrg 	if (bus_space_map2(sc->sc_bustag,
    410   1.3     mrg 			  PCI_CONFIG_BUS_SPACE,
    411   1.3     mrg 			  sc->sc_basepaddr + 0x01000000,
    412   1.3     mrg 			  0x0100000,
    413   1.3     mrg 			  0,
    414   1.3     mrg 			  0,
    415   1.3     mrg 			  &bh))
    416   1.3     mrg 		panic("could not map sabre PCI configuration space");
    417  1.10     mrg 	sc->sc_configaddr = bh;
    418   1.1     mrg }
    419   1.1     mrg 
    420   1.1     mrg /*
    421   1.1     mrg  * SUNW,psycho initialisation ..
    422   1.1     mrg  *	- XXX what do we do here?
    423   1.1     mrg  *
    424   1.1     mrg  * i think that an attaching psycho should here find it's partner psycho
    425   1.1     mrg  * and if they haven't been attached yet, allocate both psycho_pbm's and
    426   1.1     mrg  * fill them both in here, and when the partner attaches, there is little
    427   1.1     mrg  * to do... perhaps keep a static array of what psycho have been found so
    428   1.1     mrg  * far (or perhaps those that have not yet been finished).  .mrg.
    429   1.1     mrg  * note that the partner can be found via matching `ranges' properties.
    430   1.1     mrg  */
    431   1.1     mrg static void
    432  1.22      pk psycho_init(sc, ma, pba)
    433   1.1     mrg 	struct psycho_softc *sc;
    434  1.22      pk 	struct mainbus_attach_args *ma;
    435   1.1     mrg 	struct pcibus_attach_args *pba;
    436   1.1     mrg {
    437   1.8     mrg 	struct psycho_softc *osc = NULL;
    438   1.8     mrg 	struct psycho_pbm *pp;
    439   1.8     mrg 	bus_space_handle_t bh;
    440   1.8     mrg 	u_int64_t csr;
    441   1.8     mrg 	int psycho_br[2], n;
    442  1.23      pk 	struct pci_ctl *pci_ctl;
    443   1.1     mrg 
    444   1.1     mrg 	/*
    445  1.22      pk 	 * The psycho gets three register banks:
    446  1.22      pk 	 * (0) per-PBM configuration and status registers
    447  1.22      pk 	 * (1) per-PBM PCI configuration space, containing only the
    448  1.22      pk 	 *     PBM 256-byte PCI header
    449  1.22      pk 	 * (2) the shared psycho configuration registers (struct psychoreg)
    450  1.22      pk 	 *
    451  1.22      pk 	 * XXX use the prom address for the psycho registers?  we do so far.
    452  1.22      pk 	 */
    453  1.22      pk 	sc->sc_regs = (struct psychoreg *)(u_long)ma->ma_address[2];
    454  1.22      pk 	sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
    455  1.23      pk 	pci_ctl = (struct pci_ctl *)(u_long)ma->ma_address[0];
    456  1.23      pk 
    457  1.23      pk 	csr = sc->sc_regs->psy_csr;
    458  1.23      pk 	printf("psycho: impl %d, version %d: ",
    459  1.23      pk 		PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr) );
    460  1.22      pk 
    461  1.24      pk 	sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
    462  1.24      pk 
    463  1.24      pk 	sc->sc_mode = PSYCHO_MODE_PSYCHO;
    464  1.24      pk 
    465  1.22      pk 	/*
    466  1.24      pk 	 * Match other psycho's that are already configured against
    467  1.24      pk 	 * the base physical address. This will be the same for a
    468  1.24      pk 	 * pair of devices that share register space.
    469   1.1     mrg 	 */
    470   1.3     mrg 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
    471   1.8     mrg 
    472  1.24      pk 		struct psycho_softc *asc =
    473  1.24      pk 			(struct psycho_softc *)psycho_cd.cd_devs[n];
    474   1.3     mrg 
    475  1.24      pk 		if (asc == NULL || asc == sc)
    476  1.24      pk 			/* This entry is not there or it is me */
    477  1.24      pk 			continue;
    478  1.23      pk 
    479  1.24      pk 		if (asc->sc_basepaddr != sc->sc_basepaddr)
    480  1.24      pk 			/* This is an unrelated psycho */
    481   1.3     mrg 			continue;
    482   1.3     mrg 
    483  1.24      pk 		/* Found partner */
    484  1.24      pk 		osc = asc;
    485   1.8     mrg 		break;
    486   1.8     mrg 	}
    487   1.8     mrg 
    488   1.3     mrg 
    489   1.3     mrg 	/* Oh, dear.  OK, lets get started */
    490   1.3     mrg 
    491  1.24      pk 	/*
    492  1.24      pk 	 * Setup the PCI control register
    493  1.24      pk 	 */
    494  1.24      pk 	csr = bus_space_read_8(sc->sc_bustag,
    495  1.24      pk 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
    496   1.8     mrg 	csr |= PCICTL_MRLM |
    497   1.8     mrg 	       PCICTL_ARB_PARK |
    498   1.8     mrg 	       PCICTL_ERRINTEN |
    499   1.8     mrg 	       PCICTL_4ENABLE;
    500   1.8     mrg 	csr &= ~(PCICTL_SERR |
    501   1.8     mrg 		 PCICTL_CPU_PRIO |
    502   1.8     mrg 		 PCICTL_ARB_PRIO |
    503   1.8     mrg 		 PCICTL_RTRYWAIT);
    504  1.24      pk 	bus_space_write_8(sc->sc_bustag,
    505  1.24      pk 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
    506   1.8     mrg 
    507  1.24      pk 
    508  1.24      pk 	/*
    509  1.24      pk 	 * Allocate our psycho_pbm
    510  1.24      pk 	 */
    511  1.22      pk 	pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
    512  1.22      pk 	if (pp == NULL)
    513   1.8     mrg 		panic("could not allocate psycho pbm");
    514   1.8     mrg 
    515  1.22      pk 	memset(pp, 0, sizeof *pp);
    516  1.22      pk 
    517  1.22      pk 	pp->pp_sc = sc;
    518   1.8     mrg 
    519   1.8     mrg 	/* grab the psycho ranges */
    520  1.22      pk 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
    521   1.8     mrg 
    522   1.8     mrg 	/* get the bus-range for the psycho */
    523   1.8     mrg 	psycho_get_bus_range(sc->sc_node, psycho_br);
    524   1.8     mrg 
    525   1.8     mrg 	pba->pba_bus = psycho_br[0];
    526   1.8     mrg 
    527   1.8     mrg 	printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
    528  1.23      pk 	printf("; PCI bus %d", psycho_br[0]);
    529   1.8     mrg 
    530   1.8     mrg 	pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
    531   1.8     mrg 
    532  1.22      pk 	/* grab the interrupt map and map mask */
    533   1.8     mrg 	psycho_get_intmap(sc->sc_node, &pp->pp_intmap, &pp->pp_nintmap);
    534   1.8     mrg 	psycho_get_intmapmask(sc->sc_node, &pp->pp_intmapmask);
    535   1.8     mrg 
    536   1.8     mrg 	/* allocate our tags */
    537   1.8     mrg 	pp->pp_memt = psycho_alloc_mem_tag(pp);
    538   1.8     mrg 	pp->pp_iot = psycho_alloc_io_tag(pp);
    539   1.8     mrg 	pp->pp_dmat = psycho_alloc_dma_tag(pp);
    540   1.8     mrg 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
    541   1.8     mrg 		       (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
    542   1.8     mrg 
    543   1.8     mrg 	/* allocate a chipset for this */
    544   1.8     mrg 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
    545   1.8     mrg 
    546   1.8     mrg 	/* setup the rest of the psycho pbm */
    547  1.22      pk 	pba->pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
    548   1.8     mrg 
    549   1.8     mrg 	printf("\n");
    550   1.8     mrg 
    551   1.8     mrg 	/*
    552  1.24      pk 	 * And finally, if we're the first of a pair of psycho's to
    553  1.24      pk 	 * arrive here, start up the IOMMU and get a config space tag.
    554  1.24      pk 	 * Note that we use unmapped access to PCI configuration space,
    555  1.24      pk 	 * relying on the bus space macros to provide the proper ASI based
    556  1.24      pk 	 * on the bus tag.
    557   1.8     mrg 	 */
    558  1.24      pk 	if (osc == NULL) {
    559  1.13     eeh 		/*
    560  1.24      pk 		 * Setup IOMMU and PCI configuration if we're the first
    561  1.24      pk 		 * of a pair of psycho's to arrive here.
    562  1.24      pk 		 *
    563  1.13     eeh 		 * We should calculate a TSB size based on amount of RAM
    564  1.13     eeh 		 * and number of bus controllers.
    565  1.13     eeh 		 *
    566  1.13     eeh 		 * For the moment, 32KB should be more than enough.
    567  1.13     eeh 		 */
    568  1.13     eeh 		psycho_iommu_init(sc, 2);
    569   1.8     mrg 
    570   1.8     mrg 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
    571   1.8     mrg 		if (bus_space_map2(sc->sc_bustag,
    572   1.8     mrg 				  PCI_CONFIG_BUS_SPACE,
    573   1.8     mrg 				  sc->sc_basepaddr + 0x01000000,
    574   1.8     mrg 				  0x0100000,
    575   1.8     mrg 				  0,
    576   1.8     mrg 				  0,
    577   1.8     mrg 				  &bh))
    578  1.23      pk 			panic("could not map psycho PCI configuration space");
    579  1.15  simonb 		sc->sc_configaddr = (off_t)bh;
    580   1.8     mrg 	} else {
    581  1.24      pk 		/* Just copy IOMMU state, config tag and address */
    582  1.24      pk 		sc->sc_is = osc->sc_is;
    583   1.8     mrg 		sc->sc_configtag = osc->sc_configtag;
    584   1.8     mrg 		sc->sc_configaddr = osc->sc_configaddr;
    585   1.8     mrg 	}
    586   1.1     mrg }
    587   1.1     mrg 
    588   1.1     mrg /*
    589   1.1     mrg  * PCI bus support
    590   1.1     mrg  */
    591   1.1     mrg 
    592   1.1     mrg /*
    593   1.1     mrg  * allocate a PCI chipset tag and set it's cookie.
    594   1.1     mrg  */
    595   1.1     mrg static pci_chipset_tag_t
    596   1.1     mrg psycho_alloc_chipset(pp, node, pc)
    597   1.1     mrg 	struct psycho_pbm *pp;
    598   1.1     mrg 	int node;
    599   1.1     mrg 	pci_chipset_tag_t pc;
    600   1.1     mrg {
    601   1.1     mrg 	pci_chipset_tag_t npc;
    602   1.1     mrg 
    603   1.1     mrg 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
    604   1.1     mrg 	if (npc == NULL)
    605   1.1     mrg 		panic("could not allocate pci_chipset_tag_t");
    606   1.1     mrg 	memcpy(npc, pc, sizeof *pc);
    607   1.1     mrg 	npc->cookie = pp;
    608   1.1     mrg 	npc->node = node;
    609   1.1     mrg 
    610   1.1     mrg 	return (npc);
    611   1.1     mrg }
    612   1.1     mrg 
    613   1.1     mrg /*
    614   1.1     mrg  * grovel the OBP for various psycho properties
    615   1.1     mrg  */
    616   1.1     mrg static void
    617   1.1     mrg psycho_get_bus_range(node, brp)
    618   1.1     mrg 	int node;
    619   1.1     mrg 	int *brp;
    620   1.1     mrg {
    621   1.1     mrg 	int n;
    622   1.1     mrg 
    623   1.1     mrg 	if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
    624   1.1     mrg 		panic("could not get psycho bus-range");
    625   1.1     mrg 	if (n != 2)
    626   1.1     mrg 		panic("broken psycho bus-range");
    627   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
    628   1.1     mrg }
    629   1.1     mrg 
    630   1.1     mrg static void
    631   1.1     mrg psycho_get_ranges(node, rp, np)
    632   1.1     mrg 	int node;
    633   1.1     mrg 	struct psycho_ranges **rp;
    634   1.1     mrg 	int *np;
    635   1.1     mrg {
    636   1.1     mrg 
    637   1.1     mrg 	if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
    638   1.1     mrg 		panic("could not get psycho ranges");
    639   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
    640   1.1     mrg }
    641   1.1     mrg 
    642   1.1     mrg static void
    643   1.1     mrg psycho_get_registers(node, rp, np)
    644   1.1     mrg 	int node;
    645   1.1     mrg 	struct psycho_registers **rp;
    646   1.1     mrg 	int *np;
    647   1.1     mrg {
    648   1.1     mrg 
    649   1.1     mrg 	if (getprop(node, "reg", sizeof(**rp), np, (void **)rp))
    650   1.1     mrg 		panic("could not get psycho registers");
    651   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `reg' for node %08x: %d entries\n", node, *np));
    652   1.1     mrg }
    653   1.1     mrg 
    654   1.1     mrg static void
    655   1.1     mrg psycho_get_intmap(node, imp, np)
    656   1.1     mrg 	int node;
    657   1.1     mrg 	struct psycho_interrupt_map **imp;
    658   1.1     mrg 	int *np;
    659   1.1     mrg {
    660   1.1     mrg 
    661   1.1     mrg 	if (getprop(node, "interrupt-map", sizeof(**imp), np, (void **)imp))
    662   1.1     mrg 		panic("could not get psycho interrupt-map");
    663   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `interupt-map' for node %08x\n", node));
    664   1.1     mrg }
    665   1.1     mrg 
    666   1.1     mrg static void
    667   1.1     mrg psycho_get_intmapmask(node, immp)
    668   1.1     mrg 	int node;
    669   1.1     mrg 	struct psycho_interrupt_map_mask *immp;
    670   1.1     mrg {
    671   1.1     mrg 	int n;
    672   1.1     mrg 
    673   1.1     mrg 	if (getprop(node, "interrupt-map-mask", sizeof(*immp), &n,
    674   1.1     mrg 	    (void **)&immp))
    675   1.1     mrg 		panic("could not get psycho interrupt-map-mask");
    676   1.1     mrg 	if (n != 1)
    677   1.1     mrg 		panic("broken psycho interrupt-map-mask");
    678   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `interrupt-map-mask' for node %08x\n", node));
    679   1.1     mrg }
    680   1.1     mrg 
    681   1.1     mrg /*
    682   1.1     mrg  * initialise the IOMMU..
    683   1.1     mrg  */
    684   1.1     mrg void
    685  1.13     eeh psycho_iommu_init(sc, tsbsize)
    686   1.1     mrg 	struct psycho_softc *sc;
    687  1.13     eeh 	int tsbsize;
    688   1.1     mrg {
    689   1.1     mrg 	char *name;
    690  1.24      pk 	struct iommu_state *is;
    691  1.24      pk 
    692  1.24      pk 	is = malloc(sizeof(struct iommu_state), M_DEVBUF, M_NOWAIT);
    693  1.24      pk 	if (is == NULL)
    694  1.24      pk 		panic("psycho_iommu_init: malloc is");
    695  1.24      pk 
    696  1.24      pk 	sc->sc_is = is;
    697   1.1     mrg 
    698   1.1     mrg 	/* punch in our copies */
    699  1.24      pk 	is->is_bustag = sc->sc_bustag;
    700  1.24      pk 	is->is_iommu = &sc->sc_regs->psy_iommu;
    701  1.13     eeh 
    702  1.13     eeh 	if (getproplen(sc->sc_node, "no-streaming-cache") < 0)
    703  1.24      pk 		is->is_sb = 0;
    704  1.13     eeh 	else
    705  1.24      pk 		is->is_sb = &sc->sc_regs->psy_iommu_strbuf;
    706   1.1     mrg 
    707   1.1     mrg 	/* give us a nice name.. */
    708   1.1     mrg 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
    709   1.1     mrg 	if (name == 0)
    710   1.1     mrg 		panic("couldn't malloc iommu name");
    711   1.1     mrg 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
    712   1.1     mrg 
    713  1.24      pk 	iommu_init(name, is, tsbsize);
    714   1.7     mrg }
    715   1.7     mrg 
    716   1.7     mrg /*
    717   1.7     mrg  * below here is bus space and bus dma support
    718   1.7     mrg  */
    719   1.7     mrg bus_space_tag_t
    720   1.7     mrg psycho_alloc_bus_tag(pp, type)
    721   1.7     mrg 	struct psycho_pbm *pp;
    722   1.7     mrg 	int type;
    723   1.7     mrg {
    724   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    725   1.7     mrg 	bus_space_tag_t bt;
    726   1.7     mrg 
    727   1.7     mrg 	bt = (bus_space_tag_t)
    728   1.7     mrg 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    729   1.7     mrg 	if (bt == NULL)
    730   1.7     mrg 		panic("could not allocate psycho bus tag");
    731   1.7     mrg 
    732   1.7     mrg 	bzero(bt, sizeof *bt);
    733   1.7     mrg 	bt->cookie = pp;
    734   1.7     mrg 	bt->parent = sc->sc_bustag;
    735   1.7     mrg 	bt->type = type;
    736   1.7     mrg 	bt->sparc_bus_map = _psycho_bus_map;
    737   1.7     mrg 	bt->sparc_bus_mmap = psycho_bus_mmap;
    738   1.7     mrg 	bt->sparc_intr_establish = psycho_intr_establish;
    739   1.7     mrg 	return (bt);
    740   1.7     mrg }
    741   1.7     mrg 
    742   1.7     mrg bus_dma_tag_t
    743   1.7     mrg psycho_alloc_dma_tag(pp)
    744   1.7     mrg 	struct psycho_pbm *pp;
    745   1.7     mrg {
    746   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    747   1.7     mrg 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
    748   1.7     mrg 
    749   1.7     mrg 	dt = (bus_dma_tag_t)
    750   1.7     mrg 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    751   1.7     mrg 	if (dt == NULL)
    752   1.7     mrg 		panic("could not allocate psycho dma tag");
    753   1.7     mrg 
    754   1.7     mrg 	bzero(dt, sizeof *dt);
    755   1.7     mrg 	dt->_cookie = pp;
    756   1.7     mrg 	dt->_parent = pdt;
    757   1.7     mrg #define PCOPY(x)	dt->x = pdt->x
    758   1.7     mrg 	PCOPY(_dmamap_create);
    759   1.7     mrg 	PCOPY(_dmamap_destroy);
    760   1.7     mrg 	dt->_dmamap_load = psycho_dmamap_load;
    761   1.7     mrg 	PCOPY(_dmamap_load_mbuf);
    762   1.7     mrg 	PCOPY(_dmamap_load_uio);
    763   1.9     eeh 	dt->_dmamap_load_raw = psycho_dmamap_load_raw;
    764   1.7     mrg 	dt->_dmamap_unload = psycho_dmamap_unload;
    765   1.7     mrg 	dt->_dmamap_sync = psycho_dmamap_sync;
    766   1.7     mrg 	dt->_dmamem_alloc = psycho_dmamem_alloc;
    767   1.7     mrg 	dt->_dmamem_free = psycho_dmamem_free;
    768   1.7     mrg 	dt->_dmamem_map = psycho_dmamem_map;
    769   1.7     mrg 	dt->_dmamem_unmap = psycho_dmamem_unmap;
    770   1.7     mrg 	PCOPY(_dmamem_mmap);
    771   1.7     mrg #undef	PCOPY
    772   1.7     mrg 	return (dt);
    773   1.7     mrg }
    774   1.7     mrg 
    775   1.7     mrg /*
    776   1.7     mrg  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
    777   1.7     mrg  * PCI physical addresses.
    778   1.7     mrg  */
    779   1.7     mrg 
    780   1.7     mrg static int get_childspace __P((int));
    781   1.7     mrg 
    782   1.7     mrg static int
    783   1.7     mrg get_childspace(type)
    784   1.7     mrg 	int type;
    785   1.7     mrg {
    786   1.7     mrg 	int ss;
    787   1.7     mrg 
    788   1.7     mrg 	switch (type) {
    789   1.7     mrg 	case PCI_CONFIG_BUS_SPACE:
    790   1.7     mrg 		ss = 0x00;
    791   1.7     mrg 		break;
    792   1.7     mrg 	case PCI_IO_BUS_SPACE:
    793   1.7     mrg 		ss = 0x01;
    794   1.7     mrg 		break;
    795   1.7     mrg 	case PCI_MEMORY_BUS_SPACE:
    796   1.7     mrg 		ss = 0x02;
    797   1.7     mrg 		break;
    798   1.7     mrg #if 0
    799   1.7     mrg 	/* we don't do 64 bit memory space */
    800   1.7     mrg 	case PCI_MEMORY64_BUS_SPACE:
    801   1.7     mrg 		ss = 0x03;
    802   1.7     mrg 		break;
    803   1.7     mrg #endif
    804   1.7     mrg 	default:
    805   1.7     mrg 		panic("get_childspace: unknown bus type");
    806   1.7     mrg 	}
    807   1.7     mrg 
    808   1.7     mrg 	return (ss);
    809   1.7     mrg }
    810   1.7     mrg 
    811   1.7     mrg static int
    812   1.7     mrg _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
    813   1.7     mrg 	bus_space_tag_t t;
    814   1.7     mrg 	bus_type_t btype;
    815   1.7     mrg 	bus_addr_t offset;
    816   1.7     mrg 	bus_size_t size;
    817   1.7     mrg 	int	flags;
    818   1.7     mrg 	vaddr_t vaddr;
    819   1.7     mrg 	bus_space_handle_t *hp;
    820   1.7     mrg {
    821   1.7     mrg 	struct psycho_pbm *pp = t->cookie;
    822   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    823   1.7     mrg 	int i, ss;
    824   1.7     mrg 
    825  1.27    fvdl 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, (unsigned long long)offset, (unsigned long long)size, flags,
    826  1.27    fvdl 	    (void *)vaddr));
    827   1.7     mrg 
    828   1.7     mrg 	ss = get_childspace(t->type);
    829   1.7     mrg 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
    830   1.7     mrg 
    831   1.7     mrg 
    832   1.7     mrg 	for (i = 0; i < pp->pp_nrange; i++) {
    833   1.7     mrg 		bus_addr_t paddr;
    834   1.7     mrg 
    835   1.7     mrg 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
    836   1.7     mrg 			continue;
    837   1.7     mrg 
    838   1.7     mrg 		paddr = pp->pp_range[i].phys_lo + offset;
    839   1.7     mrg 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
    840   1.7     mrg 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
    841  1.27    fvdl 			       (long)ss, (long)offset,
    842  1.27    fvdl 			       (unsigned long long)paddr));
    843   1.7     mrg 		return (bus_space_map2(sc->sc_bustag, t->type, paddr,
    844   1.7     mrg 					size, flags, vaddr, hp));
    845   1.7     mrg 	}
    846   1.7     mrg 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
    847   1.7     mrg 	return (EINVAL);
    848   1.7     mrg }
    849   1.7     mrg 
    850   1.7     mrg static int
    851   1.7     mrg psycho_bus_mmap(t, btype, paddr, flags, hp)
    852   1.7     mrg 	bus_space_tag_t t;
    853   1.7     mrg 	bus_type_t btype;
    854   1.7     mrg 	bus_addr_t paddr;
    855   1.7     mrg 	int flags;
    856   1.7     mrg 	bus_space_handle_t *hp;
    857   1.7     mrg {
    858   1.7     mrg 	bus_addr_t offset = paddr;
    859   1.7     mrg 	struct psycho_pbm *pp = t->cookie;
    860   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    861   1.7     mrg 	int i, ss;
    862   1.7     mrg 
    863   1.7     mrg 	ss = get_childspace(t->type);
    864   1.7     mrg 
    865  1.27    fvdl 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, (unsigned long long)paddr));
    866   1.7     mrg 
    867   1.7     mrg 	for (i = 0; i < pp->pp_nrange; i++) {
    868   1.7     mrg 		bus_addr_t paddr;
    869   1.7     mrg 
    870   1.7     mrg 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
    871   1.7     mrg 			continue;
    872   1.7     mrg 
    873   1.7     mrg 		paddr = pp->pp_range[i].phys_lo + offset;
    874   1.7     mrg 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
    875   1.7     mrg 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
    876  1.27    fvdl 			       (long)ss, (long)offset,
    877  1.27    fvdl 			       (unsigned long long)paddr));
    878   1.7     mrg 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    879   1.7     mrg 				       flags, hp));
    880   1.7     mrg 	}
    881   1.7     mrg 
    882   1.7     mrg 	return (-1);
    883   1.7     mrg }
    884   1.7     mrg 
    885   1.7     mrg /*
    886   1.7     mrg  * interrupt mapping.  this tells what sparc ipl any given ino runs at.
    887   1.7     mrg  */
    888   1.7     mrg static int pci_ino_to_ipl_table[] = {
    889   1.7     mrg 	0, 0, 0, 0,	/* PCI A, Slot 0, INTA#/B#/C#/D# */
    890   1.7     mrg 	0, 0, 0, 0,	/* PCI A, Slot 1, INTA#/B#/C#/D# */
    891   1.7     mrg 	0, 0, 0, 0,	/* PCI A, Slot 2, INTA#/B#/C#/D# (unavailable) */
    892   1.7     mrg 	0, 0, 0, 0,	/* PCI A, Slot 3, INTA#/B#/C#/D# (unavailable) */
    893   1.7     mrg 	0, 0, 0, 0,	/* PCI B, Slot 0, INTA#/B#/C#/D# */
    894  1.19      pk 	0, 0, 0, 0,	/* PCI B, Slot 1, INTA#/B#/C#/D# */
    895   1.7     mrg 	0, 0, 0, 0,	/* PCI B, Slot 2, INTA#/B#/C#/D# */
    896   1.7     mrg 	0, 0, 0, 0,	/* PCI B, Slot 3, INTA#/B#/C#/D# */
    897  1.18     eeh 	PIL_SCSI,	/* SCSI */
    898  1.18     eeh 	PIL_NET,	/* Ethernet */
    899   1.7     mrg 	3,		/* Parallel */
    900  1.18     eeh 	PIL_AUD,	/* Audio Record */
    901  1.18     eeh 	PIL_AUD,	/* Audio Playback */
    902   1.7     mrg 	14,		/* Power Fail */
    903   1.7     mrg 	4,		/* Keyboard/Mouse/Serial */
    904  1.18     eeh 	PIL_FD,		/* Floppy */
    905   1.7     mrg 	14,		/* Thermal Warning */
    906  1.18     eeh 	PIL_SER,	/* Keyboard */
    907  1.18     eeh 	PIL_SER,	/* Mouse */
    908  1.18     eeh 	PIL_SER,	/* Serial */
    909   1.7     mrg 	0,		/* Reserved */
    910   1.7     mrg 	0,		/* Reserved */
    911   1.7     mrg 	14,		/* Uncorrectable ECC error */
    912   1.7     mrg 	14,		/* Correctable ECC error */
    913   1.7     mrg 	14,		/* PCI A bus error */
    914   1.7     mrg 	14,		/* PCI B bus error */
    915   1.7     mrg 	14,		/* power management */
    916   1.7     mrg };
    917   1.7     mrg 
    918   1.7     mrg int
    919   1.7     mrg psycho_intr_map(tag, pin, line, ihp)
    920   1.7     mrg 	pcitag_t tag;
    921   1.7     mrg 	int pin;
    922   1.7     mrg 	int line;
    923   1.7     mrg 	pci_intr_handle_t *ihp;
    924   1.7     mrg {
    925   1.7     mrg 
    926   1.7     mrg 	if (line < 0 || line > 0x32)
    927   1.7     mrg 		panic("psycho_intr_map: line line < 0 || line > 0x32");
    928   1.7     mrg 
    929   1.7     mrg 	/* UltraSPARC IIi does not use this register, but we have set it */
    930   1.7     mrg 	(*ihp) = line;
    931   1.7     mrg 	return (0);
    932   1.7     mrg }
    933   1.7     mrg 
    934   1.7     mrg /*
    935   1.7     mrg  * install an interrupt handler for a PCI device
    936   1.7     mrg  */
    937   1.7     mrg void *
    938  1.21      pk psycho_intr_establish(t, ihandle, level, flags, handler, arg)
    939   1.7     mrg 	bus_space_tag_t t;
    940  1.21      pk 	int ihandle;
    941   1.7     mrg 	int level;
    942   1.7     mrg 	int flags;
    943   1.7     mrg 	int (*handler) __P((void *));
    944   1.7     mrg 	void *arg;
    945   1.7     mrg {
    946   1.7     mrg 	struct psycho_pbm *pp = t->cookie;
    947   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    948   1.7     mrg 	struct intrhand *ih;
    949   1.7     mrg 	int ino;
    950  1.21      pk 	long vec = ihandle;
    951   1.7     mrg 
    952   1.7     mrg 	ih = (struct intrhand *)
    953   1.7     mrg 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    954   1.7     mrg 	if (ih == NULL)
    955   1.7     mrg 		return (NULL);
    956   1.7     mrg 
    957  1.21      pk 	DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x", ihandle));
    958   1.7     mrg 	ino = INTINO(vec);
    959   1.7     mrg 	DPRINTF(PDB_INTR, (" ino %x", ino));
    960   1.7     mrg 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
    961   1.7     mrg 		volatile int64_t *intrmapptr, *intrclrptr;
    962   1.7     mrg 		int64_t intrmap = 0;
    963   1.7     mrg 		int i;
    964   1.7     mrg 
    965  1.27    fvdl 		DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
    966   1.7     mrg 		    (long)ino, intrlev[ino]));
    967   1.7     mrg 		if ((ino & INTMAP_OBIO) == 0) {
    968   1.7     mrg 			/*
    969   1.7     mrg 			 * there are only 8 PCI interrupt INO's available
    970   1.7     mrg 			 */
    971   1.7     mrg 			i = INTPCIINOX(vec);
    972   1.7     mrg 
    973   1.7     mrg 			intrmapptr = &((&sc->sc_regs->pcia_slot0_int)[i]);
    974  1.19      pk 			intrclrptr = &sc->sc_regs->pcia0_clr_int[ino];
    975   1.7     mrg 
    976   1.7     mrg 			DPRINTF(PDB_INTR, ("- turning on PCI intr %d", i));
    977   1.7     mrg 		} else {
    978   1.7     mrg 			/*
    979   1.7     mrg 			 * there are INTPCI_MAXOBINO (0x16) OBIO interrupts
    980   1.7     mrg 			 * available here (i think).
    981   1.7     mrg 			 */
    982   1.7     mrg 			i = INTPCIOBINOX(vec);
    983   1.7     mrg 			if (i > INTPCI_MAXOBINO)
    984  1.27    fvdl 				panic("ino %ld", vec);
    985   1.7     mrg 
    986   1.7     mrg 			intrmapptr = &((&sc->sc_regs->scsi_int_map)[i]);
    987   1.7     mrg 			intrclrptr = &((&sc->sc_regs->scsi_clr_int)[i]);
    988   1.7     mrg 
    989   1.7     mrg 			DPRINTF(PDB_INTR, ("- turning on OBIO intr %d", i));
    990   1.7     mrg 		}
    991   1.7     mrg 
    992   1.7     mrg 		/* Register the map and clear intr registers */
    993   1.7     mrg 		ih->ih_map = intrmapptr;
    994   1.7     mrg 		ih->ih_clr = intrclrptr;
    995   1.7     mrg 
    996   1.7     mrg 		/*
    997   1.7     mrg 		 * Read the current value as we can't change it besides the
    998   1.7     mrg 		 * valid bit so so make sure only this bit is changed.
    999   1.7     mrg 		 */
   1000   1.7     mrg 		intrmap = *intrmapptr;
   1001  1.27    fvdl 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
   1002  1.27    fvdl 		    (unsigned long long)intrmap));
   1003   1.7     mrg 
   1004   1.7     mrg 		/* Enable the interrupt */
   1005   1.7     mrg 		intrmap |= INTMAP_V;
   1006   1.7     mrg 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
   1007  1.27    fvdl 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
   1008  1.27    fvdl 		    (unsigned long long)intrmap));
   1009   1.7     mrg 		*intrmapptr = intrmap;
   1010   1.7     mrg 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
   1011  1.27    fvdl 		    (unsigned long long)(intrmap = *intrmapptr)));
   1012   1.7     mrg 	}
   1013  1.10     mrg #ifdef NOT_DEBUG
   1014   1.7     mrg 	if (psycho_debug & PDB_INTR) {
   1015   1.7     mrg 		long i;
   1016   1.7     mrg 
   1017   1.7     mrg 		for (i = 0; i < 500000000; i++)
   1018   1.7     mrg 			continue;
   1019   1.7     mrg 	}
   1020   1.7     mrg #endif
   1021   1.7     mrg 
   1022   1.7     mrg 	ih->ih_fun = handler;
   1023   1.7     mrg 	ih->ih_arg = arg;
   1024  1.24      pk 	ih->ih_number = ino | sc->sc_ign;
   1025  1.21      pk 	/*
   1026  1.21      pk 	 * If a `device class' level is specified, use it,
   1027  1.21      pk 	 * else get the PIL from a built-in table.
   1028  1.21      pk 	 */
   1029  1.21      pk 	if (level != IPL_NONE)
   1030  1.21      pk 		ih->ih_pil = level;
   1031  1.21      pk 	else
   1032  1.21      pk 		ih->ih_pil = pci_ino_to_ipl_table[ino];
   1033  1.19      pk 
   1034  1.19      pk 	DPRINTF(PDB_INTR, (
   1035  1.19      pk 	    "; installing handler %p arg %p with ino %u pil %u\n",
   1036  1.19      pk 	    handler, arg, (u_int)ino, (u_int)ih->ih_pil));
   1037  1.19      pk 
   1038   1.7     mrg 	intr_establish(ih->ih_pil, ih);
   1039   1.7     mrg 	return (ih);
   1040   1.7     mrg }
   1041   1.7     mrg 
   1042   1.7     mrg /*
   1043   1.7     mrg  * hooks into the iommu dvma calls.
   1044   1.7     mrg  */
   1045   1.7     mrg int
   1046   1.7     mrg psycho_dmamap_load(t, map, buf, buflen, p, flags)
   1047   1.7     mrg 	bus_dma_tag_t t;
   1048   1.7     mrg 	bus_dmamap_t map;
   1049   1.7     mrg 	void *buf;
   1050   1.7     mrg 	bus_size_t buflen;
   1051   1.7     mrg 	struct proc *p;
   1052   1.7     mrg 	int flags;
   1053   1.7     mrg {
   1054   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1055   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1056   1.7     mrg 
   1057  1.24      pk 	return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
   1058   1.7     mrg }
   1059   1.7     mrg 
   1060   1.7     mrg void
   1061   1.7     mrg psycho_dmamap_unload(t, map)
   1062   1.7     mrg 	bus_dma_tag_t t;
   1063   1.7     mrg 	bus_dmamap_t map;
   1064   1.7     mrg {
   1065   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1066   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1067   1.7     mrg 
   1068  1.24      pk 	iommu_dvmamap_unload(t, sc->sc_is, map);
   1069   1.9     eeh }
   1070   1.9     eeh 
   1071   1.9     eeh int
   1072  1.10     mrg psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
   1073  1.10     mrg 	bus_dma_tag_t t;
   1074   1.9     eeh 	bus_dmamap_t map;
   1075   1.9     eeh 	bus_dma_segment_t *segs;
   1076   1.9     eeh 	int nsegs;
   1077   1.9     eeh 	bus_size_t size;
   1078   1.9     eeh 	int flags;
   1079   1.9     eeh {
   1080   1.9     eeh 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1081   1.9     eeh 	struct psycho_softc *sc = pp->pp_sc;
   1082   1.9     eeh 
   1083  1.24      pk 	return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
   1084   1.7     mrg }
   1085   1.7     mrg 
   1086   1.7     mrg void
   1087   1.7     mrg psycho_dmamap_sync(t, map, offset, len, ops)
   1088   1.7     mrg 	bus_dma_tag_t t;
   1089   1.7     mrg 	bus_dmamap_t map;
   1090   1.7     mrg 	bus_addr_t offset;
   1091   1.7     mrg 	bus_size_t len;
   1092   1.7     mrg 	int ops;
   1093   1.7     mrg {
   1094   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1095   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1096   1.7     mrg 
   1097  1.13     eeh 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
   1098  1.13     eeh 		/* Flush the CPU then the IOMMU */
   1099  1.13     eeh 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1100  1.24      pk 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
   1101  1.13     eeh 	}
   1102  1.13     eeh 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
   1103  1.13     eeh 		/* Flush the IOMMU then the CPU */
   1104  1.24      pk 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
   1105  1.13     eeh 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1106  1.13     eeh 	}
   1107  1.13     eeh 
   1108   1.7     mrg }
   1109   1.7     mrg 
   1110   1.7     mrg int
   1111   1.7     mrg psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
   1112   1.7     mrg 	bus_dma_tag_t t;
   1113   1.7     mrg 	bus_size_t size;
   1114   1.7     mrg 	bus_size_t alignment;
   1115   1.7     mrg 	bus_size_t boundary;
   1116   1.7     mrg 	bus_dma_segment_t *segs;
   1117   1.7     mrg 	int nsegs;
   1118   1.7     mrg 	int *rsegs;
   1119   1.7     mrg 	int flags;
   1120   1.7     mrg {
   1121   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1122   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1123   1.7     mrg 
   1124  1.24      pk 	return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
   1125   1.7     mrg 	    segs, nsegs, rsegs, flags));
   1126   1.7     mrg }
   1127   1.7     mrg 
   1128   1.7     mrg void
   1129   1.7     mrg psycho_dmamem_free(t, segs, nsegs)
   1130   1.7     mrg 	bus_dma_tag_t t;
   1131   1.7     mrg 	bus_dma_segment_t *segs;
   1132   1.7     mrg 	int nsegs;
   1133   1.7     mrg {
   1134   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1135   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1136   1.7     mrg 
   1137  1.24      pk 	iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
   1138   1.7     mrg }
   1139   1.7     mrg 
   1140   1.7     mrg int
   1141   1.7     mrg psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
   1142   1.7     mrg 	bus_dma_tag_t t;
   1143   1.7     mrg 	bus_dma_segment_t *segs;
   1144   1.7     mrg 	int nsegs;
   1145   1.7     mrg 	size_t size;
   1146   1.7     mrg 	caddr_t *kvap;
   1147   1.7     mrg 	int flags;
   1148   1.7     mrg {
   1149   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1150   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1151   1.7     mrg 
   1152  1.24      pk 	return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
   1153   1.7     mrg }
   1154   1.7     mrg 
   1155   1.7     mrg void
   1156   1.7     mrg psycho_dmamem_unmap(t, kva, size)
   1157   1.7     mrg 	bus_dma_tag_t t;
   1158   1.7     mrg 	caddr_t kva;
   1159   1.7     mrg 	size_t size;
   1160   1.7     mrg {
   1161   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1162   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1163   1.7     mrg 
   1164  1.24      pk 	iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
   1165   1.1     mrg }
   1166