Home | History | Annotate | Line # | Download | only in nvidia
tegra_pcie.c revision 1.17
      1  1.17  jmcneill /* $NetBSD: tegra_pcie.c,v 1.17 2017/04/16 22:38:04 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.17  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_pcie.c,v 1.17 2017/04/16 22:38:04 jmcneill Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/bus.h>
     34   1.1  jmcneill #include <sys/device.h>
     35   1.1  jmcneill #include <sys/intr.h>
     36   1.1  jmcneill #include <sys/systm.h>
     37   1.1  jmcneill #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/extent.h>
     39   1.1  jmcneill #include <sys/queue.h>
     40   1.1  jmcneill #include <sys/mutex.h>
     41   1.1  jmcneill #include <sys/kmem.h>
     42   1.1  jmcneill 
     43   1.1  jmcneill #include <arm/cpufunc.h>
     44   1.1  jmcneill 
     45   1.1  jmcneill #include <dev/pci/pcireg.h>
     46   1.1  jmcneill #include <dev/pci/pcivar.h>
     47   1.1  jmcneill #include <dev/pci/pciconf.h>
     48   1.1  jmcneill 
     49   1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     50   1.1  jmcneill #include <arm/nvidia/tegra_pciereg.h>
     51   1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     52   1.1  jmcneill 
     53  1.14  jmcneill #include <dev/fdt/fdtvar.h>
     54  1.14  jmcneill 
     55  1.17  jmcneill /* Interrupt handle flags */
     56  1.17  jmcneill #define	IH_MPSAFE	0x80000000
     57  1.17  jmcneill 
     58   1.1  jmcneill static int	tegra_pcie_match(device_t, cfdata_t, void *);
     59   1.1  jmcneill static void	tegra_pcie_attach(device_t, device_t, void *);
     60   1.1  jmcneill 
     61  1.12  jakllsch #define TEGRA_PCIE_NBUS 256
     62  1.12  jakllsch #define TEGRA_PCIE_ECFB (1<<(12 - 8))	/* extended conf frags per bus */
     63  1.12  jakllsch 
     64   1.1  jmcneill struct tegra_pcie_ih {
     65   1.1  jmcneill 	int			(*ih_callback)(void *);
     66   1.1  jmcneill 	void			*ih_arg;
     67   1.1  jmcneill 	int			ih_ipl;
     68  1.16  jmcneill 	int			ih_mpsafe;
     69   1.1  jmcneill 	TAILQ_ENTRY(tegra_pcie_ih) ih_entry;
     70   1.1  jmcneill };
     71   1.1  jmcneill 
     72   1.1  jmcneill struct tegra_pcie_softc {
     73   1.1  jmcneill 	device_t		sc_dev;
     74   1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
     75   1.1  jmcneill 	bus_space_tag_t		sc_bst;
     76   1.1  jmcneill 	bus_space_handle_t	sc_bsh_afi;
     77   1.9  jakllsch 	bus_space_handle_t	sc_bsh_rpconf;
     78  1.14  jmcneill 	int			sc_phandle;
     79   1.1  jmcneill 
     80   1.1  jmcneill 	struct arm32_pci_chipset sc_pc;
     81   1.1  jmcneill 
     82   1.1  jmcneill 	void			*sc_ih;
     83   1.1  jmcneill 
     84   1.1  jmcneill 	kmutex_t		sc_lock;
     85   1.1  jmcneill 
     86   1.1  jmcneill 	TAILQ_HEAD(, tegra_pcie_ih) sc_intrs;
     87   1.1  jmcneill 	u_int			sc_intrgen;
     88  1.12  jakllsch 
     89  1.12  jakllsch 	bus_space_handle_t	sc_bsh_extc[TEGRA_PCIE_NBUS-1][TEGRA_PCIE_ECFB];
     90   1.1  jmcneill };
     91   1.1  jmcneill 
     92   1.1  jmcneill static int	tegra_pcie_intr(void *);
     93   1.1  jmcneill static void	tegra_pcie_init(pci_chipset_tag_t, void *);
     94   1.1  jmcneill static void	tegra_pcie_enable(struct tegra_pcie_softc *);
     95  1.10  jakllsch static void	tegra_pcie_setup(struct tegra_pcie_softc * const);
     96  1.12  jakllsch static void	tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const,
     97  1.12  jakllsch 					 uint, uint);
     98  1.12  jakllsch static void	tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const, uint);
     99  1.12  jakllsch static void	tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const);
    100   1.1  jmcneill 
    101   1.1  jmcneill static void	tegra_pcie_attach_hook(device_t, device_t,
    102   1.1  jmcneill 				       struct pcibus_attach_args *);
    103   1.1  jmcneill static int	tegra_pcie_bus_maxdevs(void *, int);
    104   1.1  jmcneill static pcitag_t	tegra_pcie_make_tag(void *, int, int, int);
    105   1.1  jmcneill static void	tegra_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
    106   1.1  jmcneill static pcireg_t	tegra_pcie_conf_read(void *, pcitag_t, int);
    107   1.1  jmcneill static void	tegra_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
    108   1.1  jmcneill static int	tegra_pcie_conf_hook(void *, int, int, int, pcireg_t);
    109   1.1  jmcneill static void	tegra_pcie_conf_interrupt(void *, int, int, int, int, int *);
    110   1.1  jmcneill 
    111   1.1  jmcneill static int	tegra_pcie_intr_map(const struct pci_attach_args *,
    112   1.1  jmcneill 				    pci_intr_handle_t *);
    113   1.1  jmcneill static const char *tegra_pcie_intr_string(void *, pci_intr_handle_t,
    114   1.1  jmcneill 					  char *, size_t);
    115   1.1  jmcneill const struct evcnt *tegra_pcie_intr_evcnt(void *, pci_intr_handle_t);
    116  1.16  jmcneill static int	tegra_pcie_intr_setattr(void *, pci_intr_handle_t *, int,
    117  1.16  jmcneill 					uint64_t);
    118   1.1  jmcneill static void *	tegra_pcie_intr_establish(void *, pci_intr_handle_t,
    119   1.1  jmcneill 					 int, int (*)(void *), void *);
    120   1.1  jmcneill static void	tegra_pcie_intr_disestablish(void *, void *);
    121   1.1  jmcneill 
    122   1.1  jmcneill CFATTACH_DECL_NEW(tegra_pcie, sizeof(struct tegra_pcie_softc),
    123   1.1  jmcneill 	tegra_pcie_match, tegra_pcie_attach, NULL, NULL);
    124   1.1  jmcneill 
    125   1.1  jmcneill static int
    126   1.1  jmcneill tegra_pcie_match(device_t parent, cfdata_t cf, void *aux)
    127   1.1  jmcneill {
    128  1.14  jmcneill 	const char * const compatible[] = { "nvidia,tegra124-pcie", NULL };
    129  1.14  jmcneill 	struct fdt_attach_args * const faa = aux;
    130  1.14  jmcneill 
    131  1.14  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    132   1.1  jmcneill }
    133   1.1  jmcneill 
    134   1.1  jmcneill static void
    135   1.1  jmcneill tegra_pcie_attach(device_t parent, device_t self, void *aux)
    136   1.1  jmcneill {
    137   1.1  jmcneill 	struct tegra_pcie_softc * const sc = device_private(self);
    138  1.14  jmcneill 	struct fdt_attach_args * const faa = aux;
    139  1.10  jakllsch 	struct extent *ioext, *memext, *pmemext;
    140   1.1  jmcneill 	struct pcibus_attach_args pba;
    141  1.14  jmcneill 	bus_addr_t afi_addr, cs_addr;
    142  1.14  jmcneill 	bus_size_t afi_size, cs_size;
    143  1.14  jmcneill 	char intrstr[128];
    144   1.1  jmcneill 	int error;
    145   1.1  jmcneill 
    146  1.14  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 1, &afi_addr, &afi_size) != 0) {
    147  1.14  jmcneill 		aprint_error(": couldn't get afi registers\n");
    148  1.14  jmcneill 		return;
    149  1.14  jmcneill 	}
    150  1.14  jmcneill #if notyet
    151  1.14  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 2, &cs_addr, &cs_size) != 0) {
    152  1.14  jmcneill 		aprint_error(": couldn't get cs registers\n");
    153  1.14  jmcneill 		return;
    154  1.14  jmcneill 	}
    155  1.14  jmcneill #else
    156  1.14  jmcneill 	cs_addr = TEGRA_PCIE_RPCONF_BASE;
    157  1.14  jmcneill 	cs_size = TEGRA_PCIE_RPCONF_SIZE;
    158  1.14  jmcneill #endif
    159  1.14  jmcneill 
    160   1.1  jmcneill 	sc->sc_dev = self;
    161  1.14  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    162  1.14  jmcneill 	sc->sc_bst = faa->faa_bst;
    163  1.14  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    164  1.14  jmcneill 	error = bus_space_map(sc->sc_bst, afi_addr, afi_size, 0,
    165  1.14  jmcneill 	    &sc->sc_bsh_afi);
    166  1.14  jmcneill 	if (error) {
    167  1.14  jmcneill 		aprint_error(": couldn't map afi registers: %d\n", error);
    168  1.14  jmcneill 		return;
    169  1.14  jmcneill 	}
    170  1.14  jmcneill 	error = bus_space_map(sc->sc_bst, cs_addr, cs_size, 0,
    171  1.14  jmcneill 	    &sc->sc_bsh_rpconf);
    172  1.14  jmcneill 	if (error) {
    173  1.14  jmcneill 		aprint_error(": couldn't map cs registers: %d\n", error);
    174  1.14  jmcneill 		return;
    175  1.14  jmcneill 	}
    176  1.14  jmcneill 
    177  1.12  jakllsch 	tegra_pcie_conf_map_buses(sc);
    178   1.1  jmcneill 
    179   1.1  jmcneill 	TAILQ_INIT(&sc->sc_intrs);
    180   1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    181   1.1  jmcneill 
    182   1.1  jmcneill 	aprint_naive("\n");
    183   1.1  jmcneill 	aprint_normal(": PCIE\n");
    184   1.1  jmcneill 
    185  1.14  jmcneill 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    186  1.14  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    187  1.14  jmcneill 		return;
    188  1.14  jmcneill 	}
    189  1.14  jmcneill 
    190  1.16  jmcneill 	sc->sc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_VM,
    191  1.16  jmcneill 	    FDT_INTR_MPSAFE, tegra_pcie_intr, sc);
    192   1.1  jmcneill 	if (sc->sc_ih == NULL) {
    193  1.14  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    194  1.14  jmcneill 		    intrstr);
    195   1.1  jmcneill 		return;
    196   1.1  jmcneill 	}
    197  1.14  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    198   1.1  jmcneill 
    199  1.10  jakllsch 	tegra_pcie_setup(sc);
    200  1.10  jakllsch 
    201   1.1  jmcneill 	tegra_pcie_init(&sc->sc_pc, sc);
    202   1.1  jmcneill 
    203  1.10  jakllsch 	ioext = extent_create("pciio", TEGRA_PCIE_IO_BASE,
    204  1.10  jakllsch 	    TEGRA_PCIE_IO_BASE + TEGRA_PCIE_IO_SIZE - 1,
    205  1.10  jakllsch 	    NULL, 0, EX_NOWAIT);
    206   1.1  jmcneill 	memext = extent_create("pcimem", TEGRA_PCIE_MEM_BASE,
    207   1.1  jmcneill 	    TEGRA_PCIE_MEM_BASE + TEGRA_PCIE_MEM_SIZE - 1,
    208   1.1  jmcneill 	    NULL, 0, EX_NOWAIT);
    209   1.1  jmcneill 	pmemext = extent_create("pcipmem", TEGRA_PCIE_PMEM_BASE,
    210   1.1  jmcneill 	    TEGRA_PCIE_PMEM_BASE + TEGRA_PCIE_PMEM_SIZE - 1,
    211   1.1  jmcneill 	    NULL, 0, EX_NOWAIT);
    212   1.1  jmcneill 
    213  1.10  jakllsch 	error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, 0,
    214   1.1  jmcneill 	    arm_dcache_align);
    215   1.1  jmcneill 
    216  1.10  jakllsch 	extent_destroy(ioext);
    217   1.1  jmcneill 	extent_destroy(memext);
    218   1.1  jmcneill 	extent_destroy(pmemext);
    219   1.1  jmcneill 
    220   1.1  jmcneill 	if (error) {
    221   1.1  jmcneill 		aprint_error_dev(self, "configuration failed (%d)\n",
    222   1.1  jmcneill 		    error);
    223   1.1  jmcneill 		return;
    224   1.1  jmcneill 	}
    225   1.1  jmcneill 
    226   1.1  jmcneill 	tegra_pcie_enable(sc);
    227   1.1  jmcneill 
    228   1.1  jmcneill 	memset(&pba, 0, sizeof(pba));
    229   1.1  jmcneill 	pba.pba_flags = PCI_FLAGS_MRL_OKAY |
    230   1.1  jmcneill 			PCI_FLAGS_MRM_OKAY |
    231   1.1  jmcneill 			PCI_FLAGS_MWI_OKAY |
    232  1.10  jakllsch 			PCI_FLAGS_MEM_OKAY |
    233  1.10  jakllsch 			PCI_FLAGS_IO_OKAY;
    234  1.10  jakllsch 	pba.pba_iot = sc->sc_bst;
    235   1.1  jmcneill 	pba.pba_memt = sc->sc_bst;
    236   1.1  jmcneill 	pba.pba_dmat = sc->sc_dmat;
    237   1.1  jmcneill 	pba.pba_pc = &sc->sc_pc;
    238   1.1  jmcneill 	pba.pba_bus = 0;
    239   1.1  jmcneill 
    240   1.1  jmcneill 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    241   1.1  jmcneill }
    242   1.1  jmcneill 
    243   1.1  jmcneill static int
    244   1.4  jmcneill tegra_pcie_legacy_intr(struct tegra_pcie_softc *sc)
    245   1.1  jmcneill {
    246   1.4  jmcneill 	const uint32_t msg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
    247   1.4  jmcneill 	    AFI_MSG_REG);
    248   1.1  jmcneill 	struct tegra_pcie_ih *pcie_ih;
    249   1.4  jmcneill 	int rv = 0;
    250   1.1  jmcneill 
    251   1.4  jmcneill 	if (msg & (AFI_MSG_INT0|AFI_MSG_INT1)) {
    252   1.1  jmcneill 		mutex_enter(&sc->sc_lock);
    253   1.1  jmcneill 		const u_int lastgen = sc->sc_intrgen;
    254   1.1  jmcneill 		TAILQ_FOREACH(pcie_ih, &sc->sc_intrs, ih_entry) {
    255   1.1  jmcneill 			int (*callback)(void *) = pcie_ih->ih_callback;
    256   1.1  jmcneill 			void *arg = pcie_ih->ih_arg;
    257  1.16  jmcneill 			const int mpsafe = pcie_ih->ih_mpsafe;
    258   1.1  jmcneill 			mutex_exit(&sc->sc_lock);
    259  1.16  jmcneill 
    260  1.16  jmcneill 			if (!mpsafe)
    261  1.16  jmcneill 				KERNEL_LOCK(1, curlwp);
    262   1.4  jmcneill 			rv += callback(arg);
    263  1.16  jmcneill 			if (!mpsafe)
    264  1.16  jmcneill 				KERNEL_UNLOCK_ONE(curlwp);
    265  1.16  jmcneill 
    266   1.1  jmcneill 			mutex_enter(&sc->sc_lock);
    267   1.1  jmcneill 			if (lastgen != sc->sc_intrgen)
    268   1.1  jmcneill 				break;
    269   1.1  jmcneill 		}
    270   1.1  jmcneill 		mutex_exit(&sc->sc_lock);
    271   1.4  jmcneill 	} else if (msg & (AFI_MSG_PM_PME0|AFI_MSG_PM_PME1)) {
    272   1.4  jmcneill 		device_printf(sc->sc_dev, "PM PME message; AFI_MSG=%08x\n",
    273   1.4  jmcneill 		    msg);
    274   1.4  jmcneill 	} else {
    275   1.4  jmcneill 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_MSG_REG, msg);
    276   1.4  jmcneill 		rv = 1;
    277   1.4  jmcneill 	}
    278   1.4  jmcneill 
    279   1.4  jmcneill 	return rv;
    280   1.4  jmcneill }
    281   1.4  jmcneill 
    282   1.4  jmcneill static int
    283   1.4  jmcneill tegra_pcie_intr(void *priv)
    284   1.4  jmcneill {
    285   1.4  jmcneill 	struct tegra_pcie_softc *sc = priv;
    286  1.11  jakllsch 	int rv;
    287   1.4  jmcneill 
    288   1.4  jmcneill 	const uint32_t code = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
    289   1.4  jmcneill 	    AFI_INTR_CODE_REG);
    290   1.4  jmcneill 	const uint32_t sig = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
    291   1.4  jmcneill 	    AFI_INTR_SIGNATURE_REG);
    292   1.4  jmcneill 
    293   1.4  jmcneill 	switch (__SHIFTOUT(code, AFI_INTR_CODE_INT_CODE)) {
    294   1.4  jmcneill 	case AFI_INTR_CODE_SM_MSG:
    295  1.11  jakllsch 		rv = tegra_pcie_legacy_intr(sc);
    296  1.11  jakllsch 		break;
    297   1.1  jmcneill 	default:
    298   1.1  jmcneill 		device_printf(sc->sc_dev, "intr: code %#x sig %#x\n",
    299   1.1  jmcneill 		    code, sig);
    300  1.11  jakllsch 		rv = 1;
    301  1.11  jakllsch 		break;
    302   1.1  jmcneill 	}
    303  1.11  jakllsch 
    304  1.11  jakllsch 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
    305  1.11  jakllsch 
    306  1.11  jakllsch 	return rv;
    307   1.1  jmcneill }
    308   1.1  jmcneill 
    309   1.1  jmcneill static void
    310  1.10  jakllsch tegra_pcie_setup(struct tegra_pcie_softc * const sc)
    311  1.10  jakllsch {
    312  1.10  jakllsch 	size_t i;
    313  1.10  jakllsch 
    314  1.10  jakllsch 	/*
    315  1.10  jakllsch 	 * Map PCI address spaces into ARM address space via
    316  1.10  jakllsch 	 * HyperTransport-like "FPCI".
    317  1.10  jakllsch 	 */
    318  1.10  jakllsch 	static const struct { uint32_t size, base, fpci; } pcie_init_table[] = {
    319  1.10  jakllsch 		/*
    320  1.10  jakllsch 		 * === BEWARE ===
    321  1.10  jakllsch 		 *
    322  1.10  jakllsch 		 * We depend on our TEGRA_PCIE_IO window overlaping the
    323  1.10  jakllsch 		 * TEGRA_PCIE_A1 window to allow us to use the same
    324  1.10  jakllsch 		 * bus_space_tag for both PCI IO and Memory spaces.
    325  1.10  jakllsch 		 *
    326  1.10  jakllsch 		 * 0xfdfc000000-0xfdfdffffff is the FPCI/HyperTransport
    327  1.10  jakllsch 		 * mapping for 0x0000000-0x1ffffff of PCI IO space.
    328  1.10  jakllsch 		 */
    329  1.10  jakllsch 		{ TEGRA_PCIE_IO_SIZE >> 12, TEGRA_PCIE_IO_BASE,
    330  1.10  jakllsch 		  (0xfdfc000000 + TEGRA_PCIE_IO_BASE) >> 8 | 0, },
    331  1.10  jakllsch 
    332  1.10  jakllsch 		/* HyperTransport Technology Type 1 Address Format */
    333  1.10  jakllsch 		{ TEGRA_PCIE_CONF_SIZE >> 12, TEGRA_PCIE_CONF_BASE,
    334  1.10  jakllsch 		  0xfdff000000 >> 8 | 0, },
    335  1.10  jakllsch 
    336  1.10  jakllsch 		/* 1:1 MMIO mapping */
    337  1.10  jakllsch 		{ TEGRA_PCIE_MEM_SIZE >> 12, TEGRA_PCIE_MEM_BASE,
    338  1.10  jakllsch 		  TEGRA_PCIE_MEM_BASE >> 8 | 1, },
    339  1.10  jakllsch 
    340  1.10  jakllsch 		/* Extended HyperTransport Technology Type 1 Address Format */
    341  1.10  jakllsch 		{ TEGRA_PCIE_EXTC_SIZE >> 12, TEGRA_PCIE_EXTC_BASE,
    342  1.10  jakllsch 		  0xfe10000000 >> 8 | 0, },
    343  1.10  jakllsch 
    344  1.10  jakllsch 		/* 1:1 prefetchable MMIO mapping */
    345  1.10  jakllsch 		{ TEGRA_PCIE_PMEM_SIZE >> 12, TEGRA_PCIE_PMEM_BASE,
    346  1.10  jakllsch 		  TEGRA_PCIE_PMEM_BASE >> 8 | 1, },
    347  1.10  jakllsch 	};
    348  1.10  jakllsch 
    349  1.10  jakllsch 	for (i = 0; i < AFI_AXI_NBAR; i++) {
    350  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    351  1.10  jakllsch 		    AFI_AXI_BARi_SZ(i), 0);
    352  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    353  1.10  jakllsch 		    AFI_AXI_BARi_START(i), 0);
    354  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    355  1.10  jakllsch 		    AFI_FPCI_BARi(i), 0);
    356  1.10  jakllsch 	}
    357  1.10  jakllsch 
    358  1.10  jakllsch 	for (i = 0; i < __arraycount(pcie_init_table); i++) {
    359  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    360  1.10  jakllsch 		    AFI_AXI_BARi_START(i), pcie_init_table[i].base);
    361  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    362  1.10  jakllsch 		    AFI_FPCI_BARi(i), pcie_init_table[i].fpci);
    363  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    364  1.10  jakllsch 		    AFI_AXI_BARi_SZ(i), pcie_init_table[i].size);
    365  1.10  jakllsch 	}
    366  1.10  jakllsch }
    367  1.10  jakllsch 
    368  1.10  jakllsch static void
    369   1.1  jmcneill tegra_pcie_enable(struct tegra_pcie_softc *sc)
    370   1.1  jmcneill {
    371   1.4  jmcneill 	/* disable MSI */
    372   1.4  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    373   1.4  jmcneill 	    AFI_MSI_BAR_SZ_REG, 0);
    374   1.4  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    375   1.4  jmcneill 	    AFI_MSI_FPCI_BAR_ST_REG, 0);
    376   1.4  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    377   1.4  jmcneill 	    AFI_MSI_AXI_BAR_ST_REG, 0);
    378   1.4  jmcneill 
    379   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    380   1.1  jmcneill 	    AFI_SM_INTR_ENABLE_REG, 0xffffffff);
    381   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    382   1.1  jmcneill 	    AFI_AFI_INTR_ENABLE_REG, 0);
    383   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
    384   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    385   1.1  jmcneill 	    AFI_INTR_MASK_REG, AFI_INTR_MASK_INT);
    386   1.1  jmcneill }
    387   1.1  jmcneill 
    388  1.12  jakllsch static void
    389  1.12  jakllsch tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const sc, uint bus,
    390  1.12  jakllsch     uint frg)
    391  1.12  jakllsch {
    392  1.12  jakllsch 	bus_addr_t a;
    393  1.12  jakllsch 
    394  1.12  jakllsch 	KASSERT(bus >= 1);
    395  1.12  jakllsch 	KASSERT(bus < TEGRA_PCIE_NBUS);
    396  1.12  jakllsch 	KASSERT(frg < TEGRA_PCIE_ECFB);
    397  1.12  jakllsch 
    398  1.12  jakllsch 	if (sc->sc_bsh_extc[bus-1][frg] != 0) {
    399  1.12  jakllsch 		device_printf(sc->sc_dev, "bus %u fragment %#x already "
    400  1.12  jakllsch 		    "mapped\n", bus, frg);
    401  1.12  jakllsch 		return;
    402  1.12  jakllsch 	}
    403  1.12  jakllsch 
    404  1.12  jakllsch 	a = TEGRA_PCIE_EXTC_BASE + (bus << 16) + (frg << 24);
    405  1.12  jakllsch 	if (bus_space_map(sc->sc_bst, a, 1 << 16, 0,
    406  1.12  jakllsch 	    &sc->sc_bsh_extc[bus-1][frg]) != 0)
    407  1.12  jakllsch 		device_printf(sc->sc_dev, "couldn't map PCIE "
    408  1.12  jakllsch 		    "configuration for bus %u fragment %#x", bus, frg);
    409  1.12  jakllsch }
    410  1.12  jakllsch 
    411  1.12  jakllsch /* map non-non-extended configuration space for full bus range */
    412  1.12  jakllsch static void
    413  1.12  jakllsch tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const sc, uint bus)
    414  1.12  jakllsch {
    415  1.12  jakllsch 	uint i;
    416  1.12  jakllsch 
    417  1.12  jakllsch 	for (i = 1; i < TEGRA_PCIE_ECFB; i++) {
    418  1.12  jakllsch 		tegra_pcie_conf_frag_map(sc, bus, i);
    419  1.12  jakllsch 	}
    420  1.12  jakllsch }
    421  1.12  jakllsch 
    422  1.12  jakllsch /* map non-extended configuration space for full bus range */
    423  1.12  jakllsch static void
    424  1.12  jakllsch tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const sc)
    425  1.12  jakllsch {
    426  1.12  jakllsch 	uint b;
    427  1.12  jakllsch 
    428  1.12  jakllsch 	for (b = 1; b < TEGRA_PCIE_NBUS; b++) {
    429  1.12  jakllsch 		tegra_pcie_conf_frag_map(sc, b, 0);
    430  1.12  jakllsch 	}
    431  1.12  jakllsch }
    432  1.12  jakllsch 
    433   1.1  jmcneill void
    434   1.1  jmcneill tegra_pcie_init(pci_chipset_tag_t pc, void *priv)
    435   1.1  jmcneill {
    436   1.1  jmcneill 	pc->pc_conf_v = priv;
    437   1.1  jmcneill 	pc->pc_attach_hook = tegra_pcie_attach_hook;
    438   1.1  jmcneill 	pc->pc_bus_maxdevs = tegra_pcie_bus_maxdevs;
    439   1.1  jmcneill 	pc->pc_make_tag = tegra_pcie_make_tag;
    440   1.1  jmcneill 	pc->pc_decompose_tag = tegra_pcie_decompose_tag;
    441   1.1  jmcneill 	pc->pc_conf_read = tegra_pcie_conf_read;
    442   1.1  jmcneill 	pc->pc_conf_write = tegra_pcie_conf_write;
    443   1.1  jmcneill 	pc->pc_conf_hook = tegra_pcie_conf_hook;
    444   1.1  jmcneill 	pc->pc_conf_interrupt = tegra_pcie_conf_interrupt;
    445   1.1  jmcneill 
    446   1.1  jmcneill 	pc->pc_intr_v = priv;
    447   1.1  jmcneill 	pc->pc_intr_map = tegra_pcie_intr_map;
    448   1.1  jmcneill 	pc->pc_intr_string = tegra_pcie_intr_string;
    449   1.1  jmcneill 	pc->pc_intr_evcnt = tegra_pcie_intr_evcnt;
    450  1.16  jmcneill 	pc->pc_intr_setattr = tegra_pcie_intr_setattr;
    451   1.1  jmcneill 	pc->pc_intr_establish = tegra_pcie_intr_establish;
    452   1.1  jmcneill 	pc->pc_intr_disestablish = tegra_pcie_intr_disestablish;
    453   1.1  jmcneill }
    454   1.1  jmcneill 
    455   1.1  jmcneill static void
    456   1.1  jmcneill tegra_pcie_attach_hook(device_t parent, device_t self,
    457   1.1  jmcneill     struct pcibus_attach_args *pba)
    458   1.1  jmcneill {
    459  1.12  jakllsch 	const pci_chipset_tag_t pc = pba->pba_pc;
    460  1.12  jakllsch 	struct tegra_pcie_softc * const sc = pc->pc_conf_v;
    461  1.12  jakllsch 
    462  1.12  jakllsch 	if (pba->pba_bus >= 1) {
    463  1.12  jakllsch 		tegra_pcie_conf_map_bus(sc, pba->pba_bus);
    464  1.12  jakllsch 	}
    465   1.1  jmcneill }
    466   1.1  jmcneill 
    467   1.1  jmcneill static int
    468   1.1  jmcneill tegra_pcie_bus_maxdevs(void *v, int busno)
    469   1.1  jmcneill {
    470   1.1  jmcneill 	return busno == 0 ? 2 : 32;
    471   1.1  jmcneill }
    472   1.1  jmcneill 
    473   1.1  jmcneill static pcitag_t
    474   1.1  jmcneill tegra_pcie_make_tag(void *v, int b, int d, int f)
    475   1.1  jmcneill {
    476   1.1  jmcneill 	return (b << 16) | (d << 11) | (f << 8);
    477   1.1  jmcneill }
    478   1.1  jmcneill 
    479   1.1  jmcneill static void
    480   1.1  jmcneill tegra_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    481   1.1  jmcneill {
    482   1.1  jmcneill 	if (bp)
    483   1.1  jmcneill 		*bp = (tag >> 16) & 0xff;
    484   1.1  jmcneill 	if (dp)
    485   1.1  jmcneill 		*dp = (tag >> 11) & 0x1f;
    486   1.1  jmcneill 	if (fp)
    487   1.1  jmcneill 		*fp = (tag >> 8) & 0x7;
    488   1.1  jmcneill }
    489   1.1  jmcneill 
    490   1.1  jmcneill static pcireg_t
    491   1.1  jmcneill tegra_pcie_conf_read(void *v, pcitag_t tag, int offset)
    492   1.1  jmcneill {
    493   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    494   1.1  jmcneill 	bus_space_handle_t bsh;
    495   1.1  jmcneill 	int b, d, f;
    496   1.1  jmcneill 	u_int reg;
    497   1.1  jmcneill 
    498   1.3   msaitoh 	if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
    499   1.3   msaitoh 		return (pcireg_t) -1;
    500   1.3   msaitoh 
    501   1.1  jmcneill 	tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
    502   1.1  jmcneill 
    503  1.12  jakllsch 	if (b >= TEGRA_PCIE_NBUS)
    504  1.12  jakllsch 		return (pcireg_t) -1;
    505  1.12  jakllsch 
    506   1.1  jmcneill 	if (b == 0) {
    507   1.6  jakllsch 		if (d >= 2 || f != 0)
    508   1.6  jakllsch 			return (pcireg_t) -1;
    509   1.1  jmcneill 		reg = d * 0x1000 + offset;
    510   1.9  jakllsch 		bsh = sc->sc_bsh_rpconf;
    511   1.1  jmcneill 	} else {
    512  1.12  jakllsch 		reg = (d << 11) | (f << 8) | (offset & 0xff);
    513  1.12  jakllsch 		bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
    514  1.12  jakllsch 		if (bsh == 0)
    515   1.7  jakllsch 			return (pcireg_t) -1;
    516   1.1  jmcneill 	}
    517   1.1  jmcneill 
    518   1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, bsh, reg);
    519   1.1  jmcneill }
    520   1.1  jmcneill 
    521   1.1  jmcneill static void
    522   1.1  jmcneill tegra_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    523   1.1  jmcneill {
    524   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    525   1.1  jmcneill 	bus_space_handle_t bsh;
    526   1.1  jmcneill 	int b, d, f;
    527   1.1  jmcneill 	u_int reg;
    528   1.1  jmcneill 
    529   1.3   msaitoh 	if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
    530   1.3   msaitoh 		return;
    531   1.3   msaitoh 
    532   1.1  jmcneill 	tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
    533   1.1  jmcneill 
    534  1.12  jakllsch 	if (b >= TEGRA_PCIE_NBUS)
    535  1.12  jakllsch 		return;
    536  1.12  jakllsch 
    537   1.1  jmcneill 	if (b == 0) {
    538   1.6  jakllsch 		if (d >= 2 || f != 0)
    539   1.6  jakllsch 			return;
    540   1.1  jmcneill 		reg = d * 0x1000 + offset;
    541   1.9  jakllsch 		bsh = sc->sc_bsh_rpconf;
    542   1.1  jmcneill 	} else {
    543  1.12  jakllsch 		reg = (d << 11) | (f << 8) | (offset & 0xff);
    544  1.12  jakllsch 		bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
    545  1.12  jakllsch 		if (bsh == 0)
    546   1.7  jakllsch 			return;
    547   1.1  jmcneill 	}
    548   1.1  jmcneill 
    549   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, bsh, reg, val);
    550   1.1  jmcneill }
    551   1.1  jmcneill 
    552   1.1  jmcneill static int
    553   1.1  jmcneill tegra_pcie_conf_hook(void *v, int b, int d, int f, pcireg_t id)
    554   1.1  jmcneill {
    555  1.15  jakllsch 	return PCI_CONF_DEFAULT & ~PCI_CONF_ENABLE_BM;
    556   1.1  jmcneill }
    557   1.1  jmcneill 
    558   1.1  jmcneill static void
    559   1.1  jmcneill tegra_pcie_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
    560   1.1  jmcneill     int *ilinep)
    561   1.1  jmcneill {
    562  1.14  jmcneill 	*ilinep = 5;
    563   1.1  jmcneill }
    564   1.1  jmcneill 
    565   1.1  jmcneill static int
    566   1.1  jmcneill tegra_pcie_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
    567   1.1  jmcneill {
    568   1.1  jmcneill 	if (pa->pa_intrpin == 0)
    569   1.1  jmcneill 		return EINVAL;
    570   1.1  jmcneill 	*ih = pa->pa_intrpin;
    571   1.1  jmcneill 	return 0;
    572   1.1  jmcneill }
    573   1.5  jakllsch 
    574   1.1  jmcneill static const char *
    575   1.1  jmcneill tegra_pcie_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
    576   1.1  jmcneill {
    577   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    578   1.1  jmcneill 
    579   1.1  jmcneill 	if (ih == PCI_INTERRUPT_PIN_NONE)
    580   1.1  jmcneill 		return NULL;
    581   1.1  jmcneill 
    582  1.14  jmcneill 	if (!fdtbus_intr_str(sc->sc_phandle, 0, buf, len))
    583  1.14  jmcneill 		return NULL;
    584  1.14  jmcneill 
    585   1.1  jmcneill 	return buf;
    586   1.1  jmcneill }
    587   1.1  jmcneill 
    588   1.1  jmcneill const struct evcnt *
    589   1.1  jmcneill tegra_pcie_intr_evcnt(void *v, pci_intr_handle_t ih)
    590   1.1  jmcneill {
    591   1.1  jmcneill 	return NULL;
    592   1.1  jmcneill }
    593   1.1  jmcneill 
    594  1.16  jmcneill static int
    595  1.16  jmcneill tegra_pcie_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
    596  1.16  jmcneill {
    597  1.16  jmcneill 	switch (attr) {
    598  1.16  jmcneill 	case PCI_INTR_MPSAFE:
    599  1.17  jmcneill 		if (data)
    600  1.17  jmcneill 			*ih |= IH_MPSAFE;
    601  1.17  jmcneill 		else
    602  1.17  jmcneill 			*ih &= ~IH_MPSAFE;
    603  1.16  jmcneill 		return 0;
    604  1.16  jmcneill 	default:
    605  1.16  jmcneill 		return ENODEV;
    606  1.16  jmcneill 	}
    607  1.16  jmcneill }
    608  1.16  jmcneill 
    609   1.1  jmcneill static void *
    610   1.1  jmcneill tegra_pcie_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    611   1.1  jmcneill     int (*callback)(void *), void *arg)
    612   1.1  jmcneill {
    613   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    614   1.1  jmcneill 	struct tegra_pcie_ih *pcie_ih;
    615   1.1  jmcneill 
    616   1.1  jmcneill 	if (ih == 0)
    617   1.1  jmcneill 		return NULL;
    618   1.1  jmcneill 
    619   1.1  jmcneill 	pcie_ih = kmem_alloc(sizeof(*pcie_ih), KM_SLEEP);
    620   1.1  jmcneill 	pcie_ih->ih_callback = callback;
    621   1.1  jmcneill 	pcie_ih->ih_arg = arg;
    622   1.1  jmcneill 	pcie_ih->ih_ipl = ipl;
    623  1.17  jmcneill 	pcie_ih->ih_mpsafe = (ih & IH_MPSAFE) != 0;
    624   1.1  jmcneill 
    625   1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    626   1.1  jmcneill 	TAILQ_INSERT_TAIL(&sc->sc_intrs, pcie_ih, ih_entry);
    627   1.1  jmcneill 	sc->sc_intrgen++;
    628   1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    629   1.1  jmcneill 
    630   1.1  jmcneill 	return pcie_ih;
    631   1.1  jmcneill }
    632   1.1  jmcneill 
    633   1.1  jmcneill static void
    634   1.1  jmcneill tegra_pcie_intr_disestablish(void *v, void *vih)
    635   1.1  jmcneill {
    636   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    637   1.1  jmcneill 	struct tegra_pcie_ih *pcie_ih = vih;
    638   1.1  jmcneill 
    639   1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    640   1.1  jmcneill 	TAILQ_REMOVE(&sc->sc_intrs, pcie_ih, ih_entry);
    641   1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    642   1.1  jmcneill 
    643   1.1  jmcneill 	kmem_free(pcie_ih, sizeof(*pcie_ih));
    644   1.1  jmcneill }
    645