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