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