Home | History | Annotate | Line # | Download | only in dev
psycho.c revision 1.35
      1  1.35     eeh /*	$NetBSD: psycho.c,v 1.35 2001/09/10 16:17:06 eeh 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.34     eeh  * Support for `psycho' and `psycho+' UPA to PCI bridge and
     35  1.34     eeh  * UltraSPARC IIi and IIe `sabre' PCI controllers.
     36   1.1     mrg  */
     37   1.1     mrg 
     38   1.1     mrg #undef DEBUG
     39   1.1     mrg #define DEBUG
     40   1.1     mrg 
     41   1.1     mrg #ifdef DEBUG
     42   1.7     mrg #define PDB_PROM	0x01
     43  1.34     eeh #define PDB_BUSMAP	0x02
     44  1.34     eeh #define PDB_INTR	0x04
     45   1.3     mrg int psycho_debug = 0x0;
     46   1.1     mrg #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
     47   1.1     mrg #else
     48   1.1     mrg #define DPRINTF(l, s)
     49   1.1     mrg #endif
     50   1.1     mrg 
     51   1.1     mrg #include <sys/param.h>
     52   1.7     mrg #include <sys/device.h>
     53   1.7     mrg #include <sys/errno.h>
     54   1.1     mrg #include <sys/extent.h>
     55   1.7     mrg #include <sys/malloc.h>
     56   1.7     mrg #include <sys/systm.h>
     57   1.1     mrg #include <sys/time.h>
     58  1.34     eeh #include <sys/reboot.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.34     eeh static void psycho_set_intr __P((struct psycho_softc *, int, void *,
     81  1.34     eeh 	u_int64_t *, u_int64_t *));
     82  1.34     eeh 
     83  1.34     eeh /* Interrupt handlers */
     84  1.34     eeh static int psycho_ue __P((void *));
     85  1.34     eeh static int psycho_ce __P((void *));
     86  1.34     eeh static int psycho_bus_a __P((void *));
     87  1.34     eeh static int psycho_bus_b __P((void *));
     88  1.34     eeh static int psycho_powerfail __P((void *));
     89  1.34     eeh static int psycho_wakeup __P((void *));
     90  1.34     eeh 
     91   1.1     mrg 
     92   1.1     mrg /* IOMMU support */
     93  1.13     eeh static void psycho_iommu_init __P((struct psycho_softc *, int));
     94   1.1     mrg 
     95   1.7     mrg /*
     96   1.7     mrg  * bus space and bus dma support for UltraSPARC `psycho'.  note that most
     97   1.7     mrg  * of the bus dma support is provided by the iommu dvma controller.
     98   1.7     mrg  */
     99   1.7     mrg static int psycho_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    100   1.7     mrg 				int, bus_space_handle_t *));
    101   1.7     mrg static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    102   1.7     mrg 				bus_size_t, int, vaddr_t,
    103   1.7     mrg 				bus_space_handle_t *));
    104  1.21      pk static void *psycho_intr_establish __P((bus_space_tag_t, int, int, int,
    105   1.7     mrg 				int (*) __P((void *)), void *));
    106   1.7     mrg 
    107   1.7     mrg static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    108   1.7     mrg 				   bus_size_t, struct proc *, int));
    109   1.7     mrg static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    110   1.9     eeh static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    111   1.9     eeh 		    bus_dma_segment_t *, int, bus_size_t, int));
    112   1.7     mrg static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    113   1.7     mrg 				    bus_size_t, int));
    114   1.7     mrg int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
    115   1.7     mrg 			     bus_dma_segment_t *, int, int *, int));
    116   1.7     mrg void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
    117   1.7     mrg int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
    118   1.7     mrg 			   caddr_t *, int));
    119   1.7     mrg void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
    120   1.7     mrg 
    121   1.7     mrg /* base pci_chipset */
    122   1.1     mrg extern struct sparc_pci_chipset _sparc_pci_chipset;
    123   1.1     mrg 
    124   1.1     mrg /*
    125   1.1     mrg  * autoconfiguration
    126   1.1     mrg  */
    127   1.1     mrg static	int	psycho_match __P((struct device *, struct cfdata *, void *));
    128   1.1     mrg static	void	psycho_attach __P((struct device *, struct device *, void *));
    129   1.1     mrg static	int	psycho_print __P((void *aux, const char *p));
    130   1.1     mrg 
    131   1.1     mrg struct cfattach psycho_ca = {
    132   1.1     mrg         sizeof(struct psycho_softc), psycho_match, psycho_attach
    133   1.1     mrg };
    134   1.1     mrg 
    135   1.1     mrg /*
    136  1.34     eeh  * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
    137  1.34     eeh  * single PCI bus and does not have a streaming buffer.  It often has an APB
    138  1.34     eeh  * (advanced PCI bridge) connected to it, which was designed specifically for
    139  1.34     eeh  * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
    140  1.34     eeh  * appears as two "simba"'s underneath the sabre.
    141  1.34     eeh  *
    142  1.34     eeh  * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
    143  1.34     eeh  * and manages two PCI buses.  "psycho" has two 64-bit 33MHz buses, while
    144  1.34     eeh  * "psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus.  You
    145  1.34     eeh  * will usually find a "psycho+" since I don't think the original "psycho"
    146  1.34     eeh  * ever shipped, and if it did it would be in the U30.
    147  1.34     eeh  *
    148  1.34     eeh  * Each "psycho" PCI bus appears as a separate OFW node, but since they are
    149  1.34     eeh  * both part of the same IC, they only have a single register space.  As such,
    150  1.34     eeh  * they need to be configured together, even though the autoconfiguration will
    151  1.34     eeh  * attach them separately.
    152  1.34     eeh  *
    153  1.34     eeh  * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
    154  1.34     eeh  * as pci1 and pci2, although they have been implemented with other PCI bus
    155  1.34     eeh  * numbers on some machines.
    156  1.34     eeh  *
    157  1.34     eeh  * On UltraII machines, there can be any number of "psycho+" ICs, each
    158  1.34     eeh  * providing two PCI buses.
    159  1.34     eeh  *
    160  1.34     eeh  *
    161  1.34     eeh  * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
    162  1.34     eeh  * the values of the following interrupts in this order:
    163   1.1     mrg  *
    164  1.34     eeh  * PCI Bus Error	(30)
    165  1.34     eeh  * DMA UE		(2e)
    166  1.34     eeh  * DMA CE		(2f)
    167  1.34     eeh  * Power Fail		(25)
    168  1.34     eeh  *
    169  1.34     eeh  * We really should attach handlers for each.
    170   1.1     mrg  *
    171   1.1     mrg  */
    172  1.35     eeh 
    173   1.1     mrg #define	ROM_PCI_NAME		"pci"
    174  1.35     eeh 
    175  1.35     eeh struct psycho_names {
    176  1.35     eeh 	char *p_name;
    177  1.35     eeh 	int p_type;
    178  1.35     eeh } psycho_names[] = {
    179  1.35     eeh 	{ "SUNW,psycho",        PSYCHO_MODE_PSYCHO      },
    180  1.35     eeh 	{ "pci108e,8000",       PSYCHO_MODE_PSYCHO      },
    181  1.35     eeh 	{ "SUNW,sabre",         PSYCHO_MODE_SABRE       },
    182  1.35     eeh 	{ "pci108e,a000",       PSYCHO_MODE_SABRE       },
    183  1.35     eeh 	{ "pci108e,a001",       PSYCHO_MODE_SABRE       },
    184  1.35     eeh 	{ NULL, 0 }
    185  1.35     eeh };
    186   1.1     mrg 
    187   1.1     mrg static	int
    188   1.1     mrg psycho_match(parent, match, aux)
    189   1.1     mrg 	struct device	*parent;
    190   1.1     mrg 	struct cfdata	*match;
    191   1.1     mrg 	void		*aux;
    192   1.1     mrg {
    193   1.1     mrg 	struct mainbus_attach_args *ma = aux;
    194   1.1     mrg 	char *model = getpropstring(ma->ma_node, "model");
    195  1.35     eeh 	int i;
    196   1.1     mrg 
    197   1.1     mrg 	/* match on a name of "pci" and a sabre or a psycho */
    198  1.35     eeh 	if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
    199  1.35     eeh 		for (i=0; psycho_names[i].p_name; i++)
    200  1.35     eeh 			if (strcmp(model, psycho_names[i].p_name) == 0)
    201  1.35     eeh 				return (1);
    202  1.35     eeh 
    203  1.35     eeh 		model = getpropstring(ma->ma_node, "compatible");
    204  1.35     eeh 		for (i=0; psycho_names[i].p_name; i++)
    205  1.35     eeh 			if (strcmp(model, psycho_names[i].p_name) == 0)
    206  1.35     eeh 				return (1);
    207  1.35     eeh 	}
    208   1.1     mrg 	return (0);
    209   1.1     mrg }
    210   1.1     mrg 
    211  1.34     eeh /*
    212  1.34     eeh  * SUNW,psycho initialisation ..
    213  1.34     eeh  *	- find the per-psycho registers
    214  1.34     eeh  *	- figure out the IGN.
    215  1.34     eeh  *	- find our partner psycho
    216  1.34     eeh  *	- configure ourselves
    217  1.34     eeh  *	- bus range, bus,
    218  1.34     eeh  *	- get interrupt-map and interrupt-map-mask
    219  1.34     eeh  *	- setup the chipsets.
    220  1.34     eeh  *	- if we're the first of the pair, initialise the IOMMU, otherwise
    221  1.34     eeh  *	  just copy it's tags and addresses.
    222  1.34     eeh  */
    223   1.1     mrg static	void
    224   1.1     mrg psycho_attach(parent, self, aux)
    225   1.1     mrg 	struct device *parent, *self;
    226   1.1     mrg 	void *aux;
    227   1.1     mrg {
    228   1.1     mrg 	struct psycho_softc *sc = (struct psycho_softc *)self;
    229  1.34     eeh 	struct psycho_softc *osc = NULL;
    230  1.34     eeh 	struct psycho_pbm *pp;
    231   1.1     mrg 	struct pcibus_attach_args pba;
    232   1.1     mrg 	struct mainbus_attach_args *ma = aux;
    233  1.34     eeh 	bus_space_handle_t bh;
    234  1.34     eeh 	u_int64_t csr;
    235  1.35     eeh 	int psycho_br[2], n, i;
    236  1.34     eeh 	struct pci_ctl *pci_ctl;
    237   1.1     mrg 	char *model = getpropstring(ma->ma_node, "model");
    238   1.1     mrg 
    239   1.1     mrg 	printf("\n");
    240   1.1     mrg 
    241   1.1     mrg 	sc->sc_node = ma->ma_node;
    242   1.1     mrg 	sc->sc_bustag = ma->ma_bustag;
    243   1.1     mrg 	sc->sc_dmatag = ma->ma_dmatag;
    244   1.1     mrg 
    245   1.1     mrg 	/*
    246   1.1     mrg 	 * call the model-specific initialisation routine.
    247   1.1     mrg 	 */
    248  1.35     eeh 	for (i=0; psycho_names[i].p_name; i++)
    249  1.35     eeh 		if (strcmp(model, psycho_names[i].p_name) == 0) {
    250  1.35     eeh 			sc->sc_mode = psycho_names[i].p_type;
    251  1.35     eeh 			goto found;
    252  1.35     eeh 		}
    253  1.35     eeh 
    254  1.35     eeh 	model = getpropstring(ma->ma_node, "compatible");
    255  1.35     eeh 	for (i=0; psycho_names[i].p_name; i++)
    256  1.35     eeh 		if (strcmp(model, psycho_names[i].p_name) == 0) {
    257  1.35     eeh 			sc->sc_mode = psycho_names[i].p_type;
    258  1.35     eeh 			goto found;
    259  1.35     eeh 		}
    260  1.34     eeh 
    261  1.35     eeh 	panic("unknown psycho model %s", model);
    262  1.35     eeh found:
    263   1.1     mrg 
    264   1.1     mrg 	/*
    265  1.22      pk 	 * The psycho gets three register banks:
    266  1.22      pk 	 * (0) per-PBM configuration and status registers
    267  1.22      pk 	 * (1) per-PBM PCI configuration space, containing only the
    268  1.22      pk 	 *     PBM 256-byte PCI header
    269  1.22      pk 	 * (2) the shared psycho configuration registers (struct psychoreg)
    270  1.22      pk 	 *
    271  1.22      pk 	 * XXX use the prom address for the psycho registers?  we do so far.
    272  1.22      pk 	 */
    273  1.34     eeh 
    274  1.34     eeh 	/* Register layouts are different.  stuupid. */
    275  1.34     eeh 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
    276  1.34     eeh 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
    277  1.34     eeh 
    278  1.34     eeh 		if (ma->ma_naddress > 2) {
    279  1.34     eeh 			sc->sc_regs = (struct psychoreg *)
    280  1.34     eeh 				(u_long)ma->ma_address[2];
    281  1.34     eeh 			pci_ctl = (struct pci_ctl *)
    282  1.34     eeh 				(u_long)ma->ma_address[0];
    283  1.34     eeh 		} else if (ma->ma_nreg > 2) {
    284  1.34     eeh 			bus_space_handle_t handle;
    285  1.34     eeh 
    286  1.34     eeh 			/* We need to map this in ourselves. */
    287  1.34     eeh 			if (bus_space_map2(sc->sc_bustag, 0,
    288  1.34     eeh 				ma->ma_reg[2].ur_paddr,
    289  1.34     eeh 				ma->ma_reg[2].ur_len, 0, NULL, &handle))
    290  1.34     eeh 				panic("psycho_attach: cannot map regs");
    291  1.34     eeh 			sc->sc_regs = (struct psychoreg *)(u_long)handle;
    292  1.34     eeh 
    293  1.34     eeh 			if (bus_space_map2(sc->sc_bustag, 0,
    294  1.34     eeh 				ma->ma_reg[0].ur_paddr,
    295  1.34     eeh 				ma->ma_reg[0].ur_len, 0, NULL, &handle))
    296  1.34     eeh 				panic("psycho_attach: cannot map ctl");
    297  1.34     eeh /* XXX -- this is lost but never unmapped */
    298  1.34     eeh 			pci_ctl = (struct pci_ctl *)(u_long)handle;
    299  1.34     eeh 
    300  1.34     eeh 		} else
    301  1.34     eeh 			panic("psycho_attach: %d not enough registers",
    302  1.34     eeh 				ma->ma_nreg);
    303  1.34     eeh 	} else {
    304  1.34     eeh 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
    305  1.34     eeh 
    306  1.34     eeh 		if (ma->ma_naddress) {
    307  1.34     eeh 			sc->sc_regs = (struct psychoreg *)
    308  1.34     eeh 				(u_long)ma->ma_address[0];
    309  1.34     eeh 			pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
    310  1.34     eeh 		} else if (ma->ma_nreg) {
    311  1.34     eeh 			bus_space_handle_t handle;
    312  1.34     eeh 
    313  1.34     eeh 			/* We need to map this in ourselves. */
    314  1.34     eeh 			if (bus_space_map2(sc->sc_bustag, 0,
    315  1.34     eeh 				ma->ma_reg[0].ur_paddr,
    316  1.34     eeh 				ma->ma_reg[0].ur_len, 0, NULL, &handle))
    317  1.34     eeh 				panic("psycho_attach: cannot map regs");
    318  1.34     eeh 			sc->sc_regs = (struct psychoreg *)(u_long)handle;
    319  1.34     eeh 			pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
    320  1.34     eeh 		} else
    321  1.34     eeh 			panic("psycho_attach: %d not enough registers",
    322  1.34     eeh 				ma->ma_nreg);
    323  1.34     eeh 	}
    324  1.23      pk 
    325  1.23      pk 	csr = sc->sc_regs->psy_csr;
    326  1.34     eeh 	sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
    327  1.34     eeh 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
    328  1.34     eeh 		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
    329  1.24      pk 
    330  1.34     eeh 	printf("%s: impl %d, version %d: ign %x ",
    331  1.34     eeh 		model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
    332  1.34     eeh 		sc->sc_ign);
    333  1.22      pk 	/*
    334  1.24      pk 	 * Match other psycho's that are already configured against
    335  1.24      pk 	 * the base physical address. This will be the same for a
    336  1.24      pk 	 * pair of devices that share register space.
    337   1.1     mrg 	 */
    338   1.3     mrg 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
    339   1.8     mrg 
    340  1.24      pk 		struct psycho_softc *asc =
    341  1.24      pk 			(struct psycho_softc *)psycho_cd.cd_devs[n];
    342   1.3     mrg 
    343  1.24      pk 		if (asc == NULL || asc == sc)
    344  1.24      pk 			/* This entry is not there or it is me */
    345  1.24      pk 			continue;
    346  1.23      pk 
    347  1.24      pk 		if (asc->sc_basepaddr != sc->sc_basepaddr)
    348  1.24      pk 			/* This is an unrelated psycho */
    349   1.3     mrg 			continue;
    350   1.3     mrg 
    351  1.24      pk 		/* Found partner */
    352  1.24      pk 		osc = asc;
    353   1.8     mrg 		break;
    354   1.8     mrg 	}
    355   1.8     mrg 
    356   1.3     mrg 
    357   1.3     mrg 	/* Oh, dear.  OK, lets get started */
    358   1.3     mrg 
    359  1.24      pk 	/*
    360  1.24      pk 	 * Setup the PCI control register
    361  1.24      pk 	 */
    362  1.24      pk 	csr = bus_space_read_8(sc->sc_bustag,
    363  1.24      pk 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
    364   1.8     mrg 	csr |= PCICTL_MRLM |
    365   1.8     mrg 	       PCICTL_ARB_PARK |
    366   1.8     mrg 	       PCICTL_ERRINTEN |
    367   1.8     mrg 	       PCICTL_4ENABLE;
    368   1.8     mrg 	csr &= ~(PCICTL_SERR |
    369   1.8     mrg 		 PCICTL_CPU_PRIO |
    370   1.8     mrg 		 PCICTL_ARB_PRIO |
    371   1.8     mrg 		 PCICTL_RTRYWAIT);
    372  1.24      pk 	bus_space_write_8(sc->sc_bustag,
    373  1.24      pk 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
    374   1.8     mrg 
    375  1.24      pk 
    376  1.24      pk 	/*
    377  1.24      pk 	 * Allocate our psycho_pbm
    378  1.24      pk 	 */
    379  1.22      pk 	pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
    380  1.22      pk 	if (pp == NULL)
    381   1.8     mrg 		panic("could not allocate psycho pbm");
    382   1.8     mrg 
    383  1.22      pk 	memset(pp, 0, sizeof *pp);
    384  1.22      pk 
    385  1.22      pk 	pp->pp_sc = sc;
    386   1.8     mrg 
    387   1.8     mrg 	/* grab the psycho ranges */
    388  1.22      pk 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
    389   1.8     mrg 
    390   1.8     mrg 	/* get the bus-range for the psycho */
    391   1.8     mrg 	psycho_get_bus_range(sc->sc_node, psycho_br);
    392   1.8     mrg 
    393  1.34     eeh 	pba.pba_bus = psycho_br[0];
    394   1.8     mrg 
    395   1.8     mrg 	printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
    396  1.23      pk 	printf("; PCI bus %d", psycho_br[0]);
    397   1.8     mrg 
    398   1.8     mrg 	pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
    399   1.8     mrg 
    400   1.8     mrg 	/* allocate our tags */
    401   1.8     mrg 	pp->pp_memt = psycho_alloc_mem_tag(pp);
    402   1.8     mrg 	pp->pp_iot = psycho_alloc_io_tag(pp);
    403   1.8     mrg 	pp->pp_dmat = psycho_alloc_dma_tag(pp);
    404   1.8     mrg 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
    405   1.8     mrg 		       (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
    406   1.8     mrg 
    407   1.8     mrg 	/* allocate a chipset for this */
    408   1.8     mrg 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
    409   1.8     mrg 
    410   1.8     mrg 	/* setup the rest of the psycho pbm */
    411  1.34     eeh 	pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
    412   1.8     mrg 
    413   1.8     mrg 	printf("\n");
    414   1.8     mrg 
    415   1.8     mrg 	/*
    416  1.34     eeh 	 * And finally, if we're a sabre or the first of a pair of psycho's to
    417  1.24      pk 	 * arrive here, start up the IOMMU and get a config space tag.
    418   1.8     mrg 	 */
    419  1.24      pk 	if (osc == NULL) {
    420  1.34     eeh 
    421  1.34     eeh 		/*
    422  1.34     eeh 		 * Establish handlers for interesting interrupts....
    423  1.34     eeh 		 *
    424  1.34     eeh 		 * XXX We need to remember these and remove this to support
    425  1.34     eeh 		 * hotplug on the UPA/FHC bus.
    426  1.34     eeh 		 *
    427  1.34     eeh 		 * XXX Not all controllers have these, but installing them
    428  1.34     eeh 		 * is better than trying to sort through this mess.
    429  1.34     eeh 		 */
    430  1.34     eeh 		psycho_set_intr(sc, 15, psycho_ue,
    431  1.34     eeh 			&sc->sc_regs->ue_int_map,
    432  1.34     eeh 			&sc->sc_regs->ue_clr_int);
    433  1.34     eeh 		psycho_set_intr(sc, 1, psycho_ce,
    434  1.34     eeh 			&sc->sc_regs->ce_int_map,
    435  1.34     eeh 			&sc->sc_regs->ce_clr_int);
    436  1.34     eeh 		psycho_set_intr(sc, 15, psycho_bus_a,
    437  1.34     eeh 			&sc->sc_regs->pciaerr_int_map,
    438  1.34     eeh 			&sc->sc_regs->pciaerr_clr_int);
    439  1.34     eeh 		psycho_set_intr(sc, 15, psycho_bus_b,
    440  1.34     eeh 			&sc->sc_regs->pciberr_int_map,
    441  1.34     eeh 			&sc->sc_regs->pciberr_clr_int);
    442  1.34     eeh 		psycho_set_intr(sc, 15, psycho_powerfail,
    443  1.34     eeh 			&sc->sc_regs->power_int_map,
    444  1.34     eeh 			&sc->sc_regs->power_clr_int);
    445  1.34     eeh 		psycho_set_intr(sc, 1, psycho_wakeup,
    446  1.34     eeh 			&sc->sc_regs->pwrmgt_int_map,
    447  1.34     eeh 			&sc->sc_regs->pwrmgt_clr_int);
    448  1.34     eeh 
    449  1.13     eeh 		/*
    450  1.24      pk 		 * Setup IOMMU and PCI configuration if we're the first
    451  1.24      pk 		 * of a pair of psycho's to arrive here.
    452  1.24      pk 		 *
    453  1.13     eeh 		 * We should calculate a TSB size based on amount of RAM
    454  1.34     eeh 		 * and number of bus controllers and number an type of
    455  1.34     eeh 		 * child devices.
    456  1.13     eeh 		 *
    457  1.13     eeh 		 * For the moment, 32KB should be more than enough.
    458  1.13     eeh 		 */
    459  1.13     eeh 		psycho_iommu_init(sc, 2);
    460   1.8     mrg 
    461   1.8     mrg 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
    462   1.8     mrg 		if (bus_space_map2(sc->sc_bustag,
    463   1.8     mrg 				  PCI_CONFIG_BUS_SPACE,
    464   1.8     mrg 				  sc->sc_basepaddr + 0x01000000,
    465   1.8     mrg 				  0x0100000,
    466   1.8     mrg 				  0,
    467   1.8     mrg 				  0,
    468   1.8     mrg 				  &bh))
    469  1.23      pk 			panic("could not map psycho PCI configuration space");
    470  1.15  simonb 		sc->sc_configaddr = (off_t)bh;
    471   1.8     mrg 	} else {
    472  1.24      pk 		/* Just copy IOMMU state, config tag and address */
    473  1.24      pk 		sc->sc_is = osc->sc_is;
    474   1.8     mrg 		sc->sc_configtag = osc->sc_configtag;
    475   1.8     mrg 		sc->sc_configaddr = osc->sc_configaddr;
    476   1.8     mrg 	}
    477  1.34     eeh 
    478  1.34     eeh 	/*
    479  1.34     eeh 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
    480  1.34     eeh 	 */
    481  1.34     eeh 	pba.pba_busname = "pci";
    482  1.34     eeh 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
    483  1.34     eeh 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
    484  1.34     eeh 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
    485  1.34     eeh 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
    486  1.34     eeh 
    487  1.34     eeh 	config_found(self, &pba, psycho_print);
    488  1.34     eeh }
    489  1.34     eeh 
    490  1.34     eeh static	int
    491  1.34     eeh psycho_print(aux, p)
    492  1.34     eeh 	void *aux;
    493  1.34     eeh 	const char *p;
    494  1.34     eeh {
    495  1.34     eeh 
    496  1.34     eeh 	if (p == NULL)
    497  1.34     eeh 		return (UNCONF);
    498  1.34     eeh 	return (QUIET);
    499  1.34     eeh }
    500  1.34     eeh 
    501  1.34     eeh static void
    502  1.34     eeh psycho_set_intr(sc, ipl, handler, mapper, clearer)
    503  1.34     eeh 	struct psycho_softc *sc;
    504  1.34     eeh 	int ipl;
    505  1.34     eeh 	void *handler;
    506  1.34     eeh 	u_int64_t *mapper;
    507  1.34     eeh 	u_int64_t *clearer;
    508  1.34     eeh {
    509  1.34     eeh 	struct intrhand *ih;
    510  1.34     eeh 
    511  1.34     eeh 	ih = (struct intrhand *)malloc(sizeof(struct intrhand),
    512  1.34     eeh 		M_DEVBUF, M_NOWAIT);
    513  1.34     eeh 	ih->ih_arg = sc;
    514  1.34     eeh 	ih->ih_map = mapper;
    515  1.34     eeh 	ih->ih_clr = clearer;
    516  1.34     eeh 	ih->ih_fun = handler;
    517  1.34     eeh 	ih->ih_pil = (1<<ipl);
    518  1.34     eeh 	ih->ih_number = INTVEC(*(ih->ih_map));
    519  1.34     eeh 	intr_establish(ipl, ih);
    520  1.34     eeh 	*(ih->ih_map) |= INTMAP_V;
    521   1.1     mrg }
    522   1.1     mrg 
    523   1.1     mrg /*
    524   1.1     mrg  * PCI bus support
    525   1.1     mrg  */
    526   1.1     mrg 
    527   1.1     mrg /*
    528   1.1     mrg  * allocate a PCI chipset tag and set it's cookie.
    529   1.1     mrg  */
    530   1.1     mrg static pci_chipset_tag_t
    531   1.1     mrg psycho_alloc_chipset(pp, node, pc)
    532   1.1     mrg 	struct psycho_pbm *pp;
    533   1.1     mrg 	int node;
    534   1.1     mrg 	pci_chipset_tag_t pc;
    535   1.1     mrg {
    536   1.1     mrg 	pci_chipset_tag_t npc;
    537   1.1     mrg 
    538   1.1     mrg 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
    539   1.1     mrg 	if (npc == NULL)
    540   1.1     mrg 		panic("could not allocate pci_chipset_tag_t");
    541   1.1     mrg 	memcpy(npc, pc, sizeof *pc);
    542   1.1     mrg 	npc->cookie = pp;
    543  1.34     eeh 	npc->rootnode = node;
    544  1.34     eeh 	npc->curnode = node;
    545   1.1     mrg 
    546   1.1     mrg 	return (npc);
    547   1.1     mrg }
    548   1.1     mrg 
    549   1.1     mrg /*
    550   1.1     mrg  * grovel the OBP for various psycho properties
    551   1.1     mrg  */
    552   1.1     mrg static void
    553   1.1     mrg psycho_get_bus_range(node, brp)
    554   1.1     mrg 	int node;
    555   1.1     mrg 	int *brp;
    556   1.1     mrg {
    557   1.1     mrg 	int n;
    558   1.1     mrg 
    559   1.1     mrg 	if (getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
    560   1.1     mrg 		panic("could not get psycho bus-range");
    561   1.1     mrg 	if (n != 2)
    562   1.1     mrg 		panic("broken psycho bus-range");
    563   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
    564   1.1     mrg }
    565   1.1     mrg 
    566   1.1     mrg static void
    567   1.1     mrg psycho_get_ranges(node, rp, np)
    568   1.1     mrg 	int node;
    569   1.1     mrg 	struct psycho_ranges **rp;
    570   1.1     mrg 	int *np;
    571   1.1     mrg {
    572   1.1     mrg 
    573   1.1     mrg 	if (getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
    574   1.1     mrg 		panic("could not get psycho ranges");
    575   1.1     mrg 	DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
    576   1.1     mrg }
    577   1.1     mrg 
    578  1.34     eeh /*
    579  1.34     eeh  * Interrupt handlers.
    580  1.34     eeh  */
    581  1.34     eeh 
    582  1.34     eeh static int
    583  1.34     eeh psycho_ue(arg)
    584  1.34     eeh 	void *arg;
    585  1.34     eeh {
    586  1.34     eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    587  1.34     eeh 	struct psychoreg *regs = sc->sc_regs;
    588  1.34     eeh 
    589  1.34     eeh 	/*
    590  1.34     eeh 	 * It's uncorrectable.  Dump the regs and panic.
    591  1.34     eeh 	 */
    592  1.34     eeh 
    593  1.34     eeh 	panic("%s: uncorrectable DMA error AFAR %llx AFSR %llx\n",
    594  1.34     eeh 		sc->sc_dev.dv_xname,
    595  1.34     eeh 		(long long)regs->psy_ue_afar, (long long)regs->psy_ue_afsr);
    596  1.34     eeh 	return (1);
    597  1.34     eeh }
    598  1.34     eeh static int
    599  1.34     eeh psycho_ce(arg)
    600  1.34     eeh 	void *arg;
    601   1.1     mrg {
    602  1.34     eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    603  1.34     eeh 	struct psychoreg *regs = sc->sc_regs;
    604  1.34     eeh 
    605  1.34     eeh 	/*
    606  1.34     eeh 	 * It's correctable.  Dump the regs and continue.
    607  1.34     eeh 	 */
    608   1.1     mrg 
    609  1.34     eeh 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
    610  1.34     eeh 		sc->sc_dev.dv_xname,
    611  1.34     eeh 		(long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
    612  1.34     eeh 	return (1);
    613   1.1     mrg }
    614  1.34     eeh static int
    615  1.34     eeh psycho_bus_a(arg)
    616  1.34     eeh 	void *arg;
    617  1.34     eeh {
    618  1.34     eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    619  1.34     eeh 	struct psychoreg *regs = sc->sc_regs;
    620  1.34     eeh 
    621  1.34     eeh 	/*
    622  1.34     eeh 	 * It's uncorrectable.  Dump the regs and panic.
    623  1.34     eeh 	 */
    624   1.1     mrg 
    625  1.34     eeh 	panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
    626  1.34     eeh 		sc->sc_dev.dv_xname,
    627  1.35     eeh 		(long long)regs->psy_pcictl[0].pci_afar,
    628  1.35     eeh 		(long long)regs->psy_pcictl[0].pci_afsr);
    629  1.34     eeh 	return (1);
    630  1.34     eeh }
    631  1.34     eeh static int
    632  1.34     eeh psycho_bus_b(arg)
    633  1.34     eeh 	void *arg;
    634   1.1     mrg {
    635  1.34     eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    636  1.34     eeh 	struct psychoreg *regs = sc->sc_regs;
    637  1.34     eeh 
    638  1.34     eeh 	/*
    639  1.34     eeh 	 * It's uncorrectable.  Dump the regs and panic.
    640  1.34     eeh 	 */
    641   1.1     mrg 
    642  1.34     eeh 	panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
    643  1.34     eeh 		sc->sc_dev.dv_xname,
    644  1.35     eeh 		(long long)regs->psy_pcictl[0].pci_afar,
    645  1.35     eeh 		(long long)regs->psy_pcictl[0].pci_afsr);
    646  1.34     eeh 	return (1);
    647   1.1     mrg }
    648  1.34     eeh static int
    649  1.34     eeh psycho_powerfail(arg)
    650  1.34     eeh 	void *arg;
    651  1.34     eeh {
    652   1.1     mrg 
    653  1.34     eeh 	/*
    654  1.34     eeh 	 * We lost power.  Try to shut down NOW.
    655  1.34     eeh 	 */
    656  1.34     eeh 	printf("Power Failure Detected: Shutting down NOW.\n");
    657  1.34     eeh 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
    658  1.34     eeh 	return (1);
    659  1.34     eeh }
    660  1.34     eeh static
    661  1.34     eeh int psycho_wakeup(arg)
    662  1.34     eeh 	void *arg;
    663   1.1     mrg {
    664  1.34     eeh 	struct psycho_softc *sc = (struct psycho_softc *)arg;
    665   1.1     mrg 
    666  1.34     eeh 	/*
    667  1.34     eeh 	 * Gee, we don't really have a framework to deal with this
    668  1.34     eeh 	 * properly.
    669  1.34     eeh 	 */
    670  1.34     eeh 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
    671  1.34     eeh 	return (1);
    672   1.1     mrg }
    673   1.1     mrg 
    674  1.34     eeh 
    675  1.34     eeh 
    676   1.1     mrg /*
    677   1.1     mrg  * initialise the IOMMU..
    678   1.1     mrg  */
    679   1.1     mrg void
    680  1.13     eeh psycho_iommu_init(sc, tsbsize)
    681   1.1     mrg 	struct psycho_softc *sc;
    682  1.13     eeh 	int tsbsize;
    683   1.1     mrg {
    684   1.1     mrg 	char *name;
    685  1.24      pk 	struct iommu_state *is;
    686  1.34     eeh 	u_int32_t iobase = -1;
    687  1.34     eeh 	int *vdma = NULL;
    688  1.34     eeh 	int nitem;
    689  1.24      pk 
    690  1.24      pk 	is = malloc(sizeof(struct iommu_state), M_DEVBUF, M_NOWAIT);
    691  1.24      pk 	if (is == NULL)
    692  1.24      pk 		panic("psycho_iommu_init: malloc is");
    693  1.24      pk 
    694  1.24      pk 	sc->sc_is = is;
    695   1.1     mrg 
    696   1.1     mrg 	/* punch in our copies */
    697  1.24      pk 	is->is_bustag = sc->sc_bustag;
    698  1.24      pk 	is->is_iommu = &sc->sc_regs->psy_iommu;
    699  1.13     eeh 
    700  1.13     eeh 	if (getproplen(sc->sc_node, "no-streaming-cache") < 0)
    701  1.24      pk 		is->is_sb = 0;
    702  1.13     eeh 	else
    703  1.24      pk 		is->is_sb = &sc->sc_regs->psy_iommu_strbuf;
    704   1.1     mrg 
    705  1.34     eeh 	/*
    706  1.34     eeh 	 * Separate the men from the boys.  Get the `virtual-dma'
    707  1.34     eeh 	 * property for sabre and use that to make sure the damn
    708  1.34     eeh 	 * iommu works.
    709  1.34     eeh 	 *
    710  1.34     eeh 	 * We could query the `#virtual-dma-size-cells' and
    711  1.34     eeh 	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
    712  1.34     eeh 	 */
    713  1.34     eeh 	if (!getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
    714  1.34     eeh 		(void **)&vdma)) {
    715  1.34     eeh 		/* Damn.  Gotta use these values. */
    716  1.34     eeh 		iobase = vdma[0];
    717  1.34     eeh #define	TSBCASE(x)	case 1<<((x)+23): tsbsize = (x); break
    718  1.34     eeh 		switch (vdma[1]) {
    719  1.34     eeh 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
    720  1.34     eeh 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
    721  1.34     eeh 		default:
    722  1.34     eeh 			printf("bogus tsb size %x, using 7\n", vdma[1]);
    723  1.34     eeh 			TSBCASE(7);
    724  1.34     eeh 		}
    725  1.34     eeh #undef TSBCASE
    726  1.34     eeh 	}
    727  1.34     eeh 
    728   1.1     mrg 	/* give us a nice name.. */
    729   1.1     mrg 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
    730   1.1     mrg 	if (name == 0)
    731   1.1     mrg 		panic("couldn't malloc iommu name");
    732   1.1     mrg 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
    733   1.1     mrg 
    734  1.34     eeh 	iommu_init(name, is, tsbsize, iobase);
    735   1.7     mrg }
    736   1.7     mrg 
    737   1.7     mrg /*
    738   1.7     mrg  * below here is bus space and bus dma support
    739   1.7     mrg  */
    740   1.7     mrg bus_space_tag_t
    741   1.7     mrg psycho_alloc_bus_tag(pp, type)
    742   1.7     mrg 	struct psycho_pbm *pp;
    743   1.7     mrg 	int type;
    744   1.7     mrg {
    745   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    746   1.7     mrg 	bus_space_tag_t bt;
    747   1.7     mrg 
    748   1.7     mrg 	bt = (bus_space_tag_t)
    749   1.7     mrg 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    750   1.7     mrg 	if (bt == NULL)
    751   1.7     mrg 		panic("could not allocate psycho bus tag");
    752   1.7     mrg 
    753   1.7     mrg 	bzero(bt, sizeof *bt);
    754   1.7     mrg 	bt->cookie = pp;
    755   1.7     mrg 	bt->parent = sc->sc_bustag;
    756   1.7     mrg 	bt->type = type;
    757   1.7     mrg 	bt->sparc_bus_map = _psycho_bus_map;
    758   1.7     mrg 	bt->sparc_bus_mmap = psycho_bus_mmap;
    759   1.7     mrg 	bt->sparc_intr_establish = psycho_intr_establish;
    760   1.7     mrg 	return (bt);
    761   1.7     mrg }
    762   1.7     mrg 
    763   1.7     mrg bus_dma_tag_t
    764   1.7     mrg psycho_alloc_dma_tag(pp)
    765   1.7     mrg 	struct psycho_pbm *pp;
    766   1.7     mrg {
    767   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    768   1.7     mrg 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
    769   1.7     mrg 
    770   1.7     mrg 	dt = (bus_dma_tag_t)
    771   1.7     mrg 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    772   1.7     mrg 	if (dt == NULL)
    773   1.7     mrg 		panic("could not allocate psycho dma tag");
    774   1.7     mrg 
    775   1.7     mrg 	bzero(dt, sizeof *dt);
    776   1.7     mrg 	dt->_cookie = pp;
    777   1.7     mrg 	dt->_parent = pdt;
    778   1.7     mrg #define PCOPY(x)	dt->x = pdt->x
    779   1.7     mrg 	PCOPY(_dmamap_create);
    780   1.7     mrg 	PCOPY(_dmamap_destroy);
    781   1.7     mrg 	dt->_dmamap_load = psycho_dmamap_load;
    782   1.7     mrg 	PCOPY(_dmamap_load_mbuf);
    783   1.7     mrg 	PCOPY(_dmamap_load_uio);
    784   1.9     eeh 	dt->_dmamap_load_raw = psycho_dmamap_load_raw;
    785   1.7     mrg 	dt->_dmamap_unload = psycho_dmamap_unload;
    786   1.7     mrg 	dt->_dmamap_sync = psycho_dmamap_sync;
    787   1.7     mrg 	dt->_dmamem_alloc = psycho_dmamem_alloc;
    788   1.7     mrg 	dt->_dmamem_free = psycho_dmamem_free;
    789   1.7     mrg 	dt->_dmamem_map = psycho_dmamem_map;
    790   1.7     mrg 	dt->_dmamem_unmap = psycho_dmamem_unmap;
    791   1.7     mrg 	PCOPY(_dmamem_mmap);
    792   1.7     mrg #undef	PCOPY
    793   1.7     mrg 	return (dt);
    794   1.7     mrg }
    795   1.7     mrg 
    796   1.7     mrg /*
    797   1.7     mrg  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
    798   1.7     mrg  * PCI physical addresses.
    799   1.7     mrg  */
    800   1.7     mrg 
    801   1.7     mrg static int get_childspace __P((int));
    802   1.7     mrg 
    803   1.7     mrg static int
    804   1.7     mrg get_childspace(type)
    805   1.7     mrg 	int type;
    806   1.7     mrg {
    807   1.7     mrg 	int ss;
    808   1.7     mrg 
    809   1.7     mrg 	switch (type) {
    810   1.7     mrg 	case PCI_CONFIG_BUS_SPACE:
    811   1.7     mrg 		ss = 0x00;
    812   1.7     mrg 		break;
    813   1.7     mrg 	case PCI_IO_BUS_SPACE:
    814   1.7     mrg 		ss = 0x01;
    815   1.7     mrg 		break;
    816   1.7     mrg 	case PCI_MEMORY_BUS_SPACE:
    817   1.7     mrg 		ss = 0x02;
    818   1.7     mrg 		break;
    819   1.7     mrg #if 0
    820   1.7     mrg 	/* we don't do 64 bit memory space */
    821   1.7     mrg 	case PCI_MEMORY64_BUS_SPACE:
    822   1.7     mrg 		ss = 0x03;
    823   1.7     mrg 		break;
    824   1.7     mrg #endif
    825   1.7     mrg 	default:
    826   1.7     mrg 		panic("get_childspace: unknown bus type");
    827   1.7     mrg 	}
    828   1.7     mrg 
    829   1.7     mrg 	return (ss);
    830   1.7     mrg }
    831   1.7     mrg 
    832   1.7     mrg static int
    833   1.7     mrg _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
    834   1.7     mrg 	bus_space_tag_t t;
    835   1.7     mrg 	bus_type_t btype;
    836   1.7     mrg 	bus_addr_t offset;
    837   1.7     mrg 	bus_size_t size;
    838   1.7     mrg 	int	flags;
    839   1.7     mrg 	vaddr_t vaddr;
    840   1.7     mrg 	bus_space_handle_t *hp;
    841   1.7     mrg {
    842   1.7     mrg 	struct psycho_pbm *pp = t->cookie;
    843   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    844   1.7     mrg 	int i, ss;
    845   1.7     mrg 
    846  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,
    847  1.27    fvdl 	    (void *)vaddr));
    848   1.7     mrg 
    849   1.7     mrg 	ss = get_childspace(t->type);
    850   1.7     mrg 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
    851   1.7     mrg 
    852   1.7     mrg 	for (i = 0; i < pp->pp_nrange; i++) {
    853   1.7     mrg 		bus_addr_t paddr;
    854   1.7     mrg 
    855   1.7     mrg 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
    856   1.7     mrg 			continue;
    857   1.7     mrg 
    858   1.7     mrg 		paddr = pp->pp_range[i].phys_lo + offset;
    859   1.7     mrg 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
    860   1.7     mrg 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
    861  1.27    fvdl 			       (long)ss, (long)offset,
    862  1.27    fvdl 			       (unsigned long long)paddr));
    863   1.7     mrg 		return (bus_space_map2(sc->sc_bustag, t->type, paddr,
    864   1.7     mrg 					size, flags, vaddr, hp));
    865   1.7     mrg 	}
    866   1.7     mrg 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
    867   1.7     mrg 	return (EINVAL);
    868   1.7     mrg }
    869   1.7     mrg 
    870   1.7     mrg static int
    871   1.7     mrg psycho_bus_mmap(t, btype, paddr, flags, hp)
    872   1.7     mrg 	bus_space_tag_t t;
    873   1.7     mrg 	bus_type_t btype;
    874   1.7     mrg 	bus_addr_t paddr;
    875   1.7     mrg 	int flags;
    876   1.7     mrg 	bus_space_handle_t *hp;
    877   1.7     mrg {
    878   1.7     mrg 	bus_addr_t offset = paddr;
    879   1.7     mrg 	struct psycho_pbm *pp = t->cookie;
    880   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    881   1.7     mrg 	int i, ss;
    882   1.7     mrg 
    883   1.7     mrg 	ss = get_childspace(t->type);
    884   1.7     mrg 
    885  1.27    fvdl 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: type %d flags %d pa %qx\n", btype, flags, (unsigned long long)paddr));
    886   1.7     mrg 
    887   1.7     mrg 	for (i = 0; i < pp->pp_nrange; i++) {
    888   1.7     mrg 		bus_addr_t paddr;
    889   1.7     mrg 
    890   1.7     mrg 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
    891   1.7     mrg 			continue;
    892   1.7     mrg 
    893   1.7     mrg 		paddr = pp->pp_range[i].phys_lo + offset;
    894   1.7     mrg 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
    895   1.7     mrg 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr space %lx offset %lx paddr %qx\n",
    896  1.27    fvdl 			       (long)ss, (long)offset,
    897  1.27    fvdl 			       (unsigned long long)paddr));
    898   1.7     mrg 		return (bus_space_mmap(sc->sc_bustag, 0, paddr,
    899   1.7     mrg 				       flags, hp));
    900   1.7     mrg 	}
    901   1.7     mrg 
    902   1.7     mrg 	return (-1);
    903   1.7     mrg }
    904   1.7     mrg 
    905   1.7     mrg 
    906   1.7     mrg /*
    907   1.7     mrg  * install an interrupt handler for a PCI device
    908   1.7     mrg  */
    909   1.7     mrg void *
    910  1.21      pk psycho_intr_establish(t, ihandle, level, flags, handler, arg)
    911   1.7     mrg 	bus_space_tag_t t;
    912  1.21      pk 	int ihandle;
    913   1.7     mrg 	int level;
    914   1.7     mrg 	int flags;
    915   1.7     mrg 	int (*handler) __P((void *));
    916   1.7     mrg 	void *arg;
    917   1.7     mrg {
    918   1.7     mrg 	struct psycho_pbm *pp = t->cookie;
    919   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
    920   1.7     mrg 	struct intrhand *ih;
    921  1.34     eeh 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
    922  1.34     eeh 	int64_t intrmap = 0;
    923   1.7     mrg 	int ino;
    924  1.34     eeh 	long vec = INTVEC(ihandle);
    925   1.7     mrg 
    926   1.7     mrg 	ih = (struct intrhand *)
    927   1.7     mrg 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    928   1.7     mrg 	if (ih == NULL)
    929   1.7     mrg 		return (NULL);
    930   1.7     mrg 
    931  1.34     eeh 	/*
    932  1.34     eeh 	 * Hunt through all the interrupt mapping regs to look for our
    933  1.34     eeh 	 * interrupt vector.
    934  1.34     eeh 	 *
    935  1.34     eeh 	 * XXX We only compare INOs rather than IGNs since the firmware may
    936  1.34     eeh 	 * not provide the IGN and the IGN is constant for all device on that
    937  1.34     eeh 	 * PCI controller.  This could cause problems for the FFB/external
    938  1.34     eeh 	 * interrupt which has a full vector that can be set arbitrarily.
    939  1.34     eeh 	 */
    940  1.34     eeh 
    941  1.34     eeh 
    942  1.31     mrg 	DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
    943   1.7     mrg 	ino = INTINO(vec);
    944   1.7     mrg 	DPRINTF(PDB_INTR, (" ino %x", ino));
    945  1.34     eeh 
    946  1.34     eeh 	/* If the device didn't ask for an IPL, use the one encoded. */
    947  1.34     eeh 	if (level == IPL_NONE) level = INTLEV(vec);
    948  1.34     eeh 	/* If it still has no level, print a warning and assign IPL 2 */
    949  1.34     eeh 	if (level == IPL_NONE) {
    950  1.34     eeh 		printf("ERROR: no IPL, setting IPL 2.\n");
    951  1.34     eeh 		level = 2;
    952  1.34     eeh 	}
    953  1.34     eeh 
    954   1.7     mrg 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
    955   1.7     mrg 
    956  1.27    fvdl 		DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
    957   1.7     mrg 		    (long)ino, intrlev[ino]));
    958   1.7     mrg 
    959  1.34     eeh 		/* Hunt thru obio first */
    960  1.34     eeh 		for (intrmapptr = &sc->sc_regs->scsi_int_map,
    961  1.34     eeh 			     intrclrptr = &sc->sc_regs->scsi_clr_int;
    962  1.34     eeh 		     intrmapptr <= &sc->sc_regs->ffb1_int_map;
    963  1.34     eeh 		     intrmapptr++, intrclrptr++) {
    964  1.34     eeh 			if (INTINO(*intrmapptr) == ino)
    965  1.34     eeh 				goto found;
    966  1.34     eeh 		}
    967   1.7     mrg 
    968  1.34     eeh 		/* Now do PCI interrupts */
    969  1.34     eeh 		for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
    970  1.34     eeh 			     intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
    971  1.34     eeh 		     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
    972  1.34     eeh 		     intrmapptr++, intrclrptr += 4) {
    973  1.34     eeh 			if (((*intrmapptr ^ vec) & 0x3c) == 0) {
    974  1.34     eeh 				intrclrptr += vec & 0x3;
    975  1.34     eeh 				goto found;
    976  1.34     eeh 			}
    977   1.7     mrg 		}
    978  1.34     eeh 		printf("Cannot find interrupt vector %lx\n", vec);
    979  1.34     eeh 		return (NULL);
    980   1.7     mrg 
    981  1.34     eeh 	found:
    982   1.7     mrg 		/* Register the map and clear intr registers */
    983   1.7     mrg 		ih->ih_map = intrmapptr;
    984   1.7     mrg 		ih->ih_clr = intrclrptr;
    985   1.7     mrg 	}
    986  1.10     mrg #ifdef NOT_DEBUG
    987   1.7     mrg 	if (psycho_debug & PDB_INTR) {
    988   1.7     mrg 		long i;
    989   1.7     mrg 
    990   1.7     mrg 		for (i = 0; i < 500000000; i++)
    991   1.7     mrg 			continue;
    992   1.7     mrg 	}
    993   1.7     mrg #endif
    994   1.7     mrg 
    995   1.7     mrg 	ih->ih_fun = handler;
    996   1.7     mrg 	ih->ih_arg = arg;
    997  1.34     eeh 	ih->ih_pil = level;
    998  1.24      pk 	ih->ih_number = ino | sc->sc_ign;
    999  1.19      pk 
   1000  1.19      pk 	DPRINTF(PDB_INTR, (
   1001  1.19      pk 	    "; installing handler %p arg %p with ino %u pil %u\n",
   1002  1.19      pk 	    handler, arg, (u_int)ino, (u_int)ih->ih_pil));
   1003  1.19      pk 
   1004   1.7     mrg 	intr_establish(ih->ih_pil, ih);
   1005  1.34     eeh 
   1006  1.34     eeh 	/*
   1007  1.34     eeh 	 * Enable the interrupt now we have the handler installed.
   1008  1.34     eeh 	 * Read the current value as we can't change it besides the
   1009  1.34     eeh 	 * valid bit so so make sure only this bit is changed.
   1010  1.34     eeh 	 *
   1011  1.34     eeh 	 * XXXX --- we really should use bus_space for this.
   1012  1.34     eeh 	 */
   1013  1.34     eeh 	if (intrmapptr) {
   1014  1.34     eeh 		intrmap = *intrmapptr;
   1015  1.34     eeh 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
   1016  1.34     eeh 			(unsigned long long)intrmap));
   1017  1.34     eeh 
   1018  1.34     eeh 		/* Enable the interrupt */
   1019  1.34     eeh 		intrmap |= INTMAP_V;
   1020  1.34     eeh 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
   1021  1.34     eeh 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
   1022  1.34     eeh 			(unsigned long long)intrmap));
   1023  1.34     eeh 		*intrmapptr = intrmap;
   1024  1.34     eeh 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
   1025  1.34     eeh 			(unsigned long long)(intrmap = *intrmapptr)));
   1026  1.34     eeh 	}
   1027   1.7     mrg 	return (ih);
   1028   1.7     mrg }
   1029   1.7     mrg 
   1030   1.7     mrg /*
   1031   1.7     mrg  * hooks into the iommu dvma calls.
   1032   1.7     mrg  */
   1033   1.7     mrg int
   1034   1.7     mrg psycho_dmamap_load(t, map, buf, buflen, p, flags)
   1035   1.7     mrg 	bus_dma_tag_t t;
   1036   1.7     mrg 	bus_dmamap_t map;
   1037   1.7     mrg 	void *buf;
   1038   1.7     mrg 	bus_size_t buflen;
   1039   1.7     mrg 	struct proc *p;
   1040   1.7     mrg 	int flags;
   1041   1.7     mrg {
   1042   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1043   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1044   1.7     mrg 
   1045  1.24      pk 	return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
   1046   1.7     mrg }
   1047   1.7     mrg 
   1048   1.7     mrg void
   1049   1.7     mrg psycho_dmamap_unload(t, map)
   1050   1.7     mrg 	bus_dma_tag_t t;
   1051   1.7     mrg 	bus_dmamap_t map;
   1052   1.7     mrg {
   1053   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1054   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1055   1.7     mrg 
   1056  1.24      pk 	iommu_dvmamap_unload(t, sc->sc_is, map);
   1057   1.9     eeh }
   1058   1.9     eeh 
   1059   1.9     eeh int
   1060  1.10     mrg psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
   1061  1.10     mrg 	bus_dma_tag_t t;
   1062   1.9     eeh 	bus_dmamap_t map;
   1063   1.9     eeh 	bus_dma_segment_t *segs;
   1064   1.9     eeh 	int nsegs;
   1065   1.9     eeh 	bus_size_t size;
   1066   1.9     eeh 	int flags;
   1067   1.9     eeh {
   1068   1.9     eeh 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1069   1.9     eeh 	struct psycho_softc *sc = pp->pp_sc;
   1070   1.9     eeh 
   1071  1.24      pk 	return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
   1072   1.7     mrg }
   1073   1.7     mrg 
   1074   1.7     mrg void
   1075   1.7     mrg psycho_dmamap_sync(t, map, offset, len, ops)
   1076   1.7     mrg 	bus_dma_tag_t t;
   1077   1.7     mrg 	bus_dmamap_t map;
   1078   1.7     mrg 	bus_addr_t offset;
   1079   1.7     mrg 	bus_size_t len;
   1080   1.7     mrg 	int ops;
   1081   1.7     mrg {
   1082   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1083   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1084   1.7     mrg 
   1085  1.13     eeh 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
   1086  1.13     eeh 		/* Flush the CPU then the IOMMU */
   1087  1.13     eeh 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1088  1.24      pk 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
   1089  1.13     eeh 	}
   1090  1.13     eeh 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
   1091  1.13     eeh 		/* Flush the IOMMU then the CPU */
   1092  1.24      pk 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
   1093  1.13     eeh 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1094  1.13     eeh 	}
   1095  1.13     eeh 
   1096   1.7     mrg }
   1097   1.7     mrg 
   1098   1.7     mrg int
   1099   1.7     mrg psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
   1100   1.7     mrg 	bus_dma_tag_t t;
   1101   1.7     mrg 	bus_size_t size;
   1102   1.7     mrg 	bus_size_t alignment;
   1103   1.7     mrg 	bus_size_t boundary;
   1104   1.7     mrg 	bus_dma_segment_t *segs;
   1105   1.7     mrg 	int nsegs;
   1106   1.7     mrg 	int *rsegs;
   1107   1.7     mrg 	int flags;
   1108   1.7     mrg {
   1109   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1110   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1111   1.7     mrg 
   1112  1.24      pk 	return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
   1113   1.7     mrg 	    segs, nsegs, rsegs, flags));
   1114   1.7     mrg }
   1115   1.7     mrg 
   1116   1.7     mrg void
   1117   1.7     mrg psycho_dmamem_free(t, segs, nsegs)
   1118   1.7     mrg 	bus_dma_tag_t t;
   1119   1.7     mrg 	bus_dma_segment_t *segs;
   1120   1.7     mrg 	int nsegs;
   1121   1.7     mrg {
   1122   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1123   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1124   1.7     mrg 
   1125  1.24      pk 	iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
   1126   1.7     mrg }
   1127   1.7     mrg 
   1128   1.7     mrg int
   1129   1.7     mrg psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
   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 	size_t size;
   1134   1.7     mrg 	caddr_t *kvap;
   1135   1.7     mrg 	int flags;
   1136   1.7     mrg {
   1137   1.7     mrg 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
   1138   1.7     mrg 	struct psycho_softc *sc = pp->pp_sc;
   1139   1.7     mrg 
   1140  1.24      pk 	return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
   1141   1.7     mrg }
   1142   1.7     mrg 
   1143   1.7     mrg void
   1144   1.7     mrg psycho_dmamem_unmap(t, kva, size)
   1145   1.7     mrg 	bus_dma_tag_t t;
   1146   1.7     mrg 	caddr_t kva;
   1147   1.7     mrg 	size_t size;
   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 	iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
   1153   1.1     mrg }
   1154