Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_pax.c revision 1.8
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #define _ARM32_BUS_DMA_PRIVATE
     31 #define PCIE_PRIVATE
     32 
     33 #include "locators.h"
     34 
     35 #include <sys/cdefs.h>
     36 
     37 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_pax.c,v 1.8 2013/02/19 02:03:06 matt Exp $");
     38 
     39 #include <sys/bus.h>
     40 #include <sys/device.h>
     41 #include <sys/extent.h>
     42 #include <sys/intr.h>
     43 #include <sys/kmem.h>
     44 #include <sys/systm.h>
     45 
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pciconf.h>
     49 
     50 #include <arm/broadcom/bcm53xx_reg.h>
     51 #include <arm/broadcom/bcm53xx_var.h>
     52 
     53 #ifndef __HAVE_PCI_CONF_HOOK
     54 #error __HAVE_PCI_CONF_HOOK must be defined
     55 #endif
     56 
     57 static const struct {
     58 	paddr_t owin_base;
     59 	psize_t owin_size;
     60 } bcmpax_owins[] = {
     61 	[0] = { BCM53XX_PCIE0_OWIN_PBASE, BCM53XX_PCIE0_OWIN_SIZE },
     62 	[1] = { BCM53XX_PCIE1_OWIN_PBASE, BCM53XX_PCIE1_OWIN_SIZE },
     63 	[2] = { BCM53XX_PCIE2_OWIN_PBASE, BCM53XX_PCIE2_OWIN_SIZE },
     64 };
     65 
     66 static int bcmpax_ccb_match(device_t, cfdata_t, void *);
     67 static void bcmpax_ccb_attach(device_t, device_t, void *);
     68 
     69 struct bcmpax_intrhand {
     70 	TAILQ_ENTRY(bcmpax_intrhand) ih_link;
     71 	int (*ih_func)(void *);
     72 	void *ih_arg;
     73 	int ih_ipl;
     74 };
     75 
     76 TAILQ_HEAD(bcmpax_ihqh, bcmpax_intrhand);
     77 
     78 struct bcmpax_softc {
     79 	device_t sc_dev;
     80 	bus_space_tag_t sc_bst;
     81 	bus_space_handle_t sc_bsh;
     82 	bus_dma_tag_t sc_dmat;
     83 	kmutex_t *sc_lock;
     84 	kmutex_t *sc_cfg_lock;
     85 	bool sc_linkup;
     86 	int sc_pba_flags;
     87 	uint32_t sc_intrgen;
     88 	struct arm32_pci_chipset sc_pc;
     89 	struct bcmpax_ihqh sc_intrs;
     90 	void *sc_ih[6];
     91 	int sc_port;
     92 	char sc_intrstring[4][32];
     93 };
     94 
     95 static inline uint32_t
     96 bcmpax_read_4(struct bcmpax_softc *sc, bus_size_t o)
     97 {
     98 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
     99 }
    100 
    101 static inline void
    102 bcmpax_write_4(struct bcmpax_softc *sc, bus_size_t o, uint32_t v)
    103 {
    104 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
    105 }
    106 
    107 static void bcmpax_attach_hook(device_t, device_t, struct pcibus_attach_args *);
    108 static int bcmpax_bus_maxdevs(void *, int);
    109 static pcitag_t bcmpax_make_tag(void *, int, int, int);
    110 static void bcmpax_decompose_tag(void *, pcitag_t, int *, int *, int *);
    111 static pcireg_t bcmpax_conf_read(void *, pcitag_t, int);
    112 static void bcmpax_conf_write(void *, pcitag_t, int, pcireg_t);
    113 
    114 static int bcmpax_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
    115 static const char *bcmpax_intr_string(void *, pci_intr_handle_t);
    116 static const struct evcnt *bcmpax_intr_evcnt(void *, pci_intr_handle_t);
    117 static void *bcmpax_intr_establish(void *, pci_intr_handle_t, int,
    118 	   int (*)(void *), void *);
    119 static void bcmpax_intr_disestablish(void *, void *);
    120 
    121 static int bcmpax_conf_hook(void *, int, int, int, pcireg_t);
    122 static void bcmpax_conf_interrupt(void *, int, int, int, int, int *);
    123 
    124 static int bcmpax_intr(void *);
    125 
    126 CFATTACH_DECL_NEW(bcmpax_ccb, sizeof(struct bcmpax_softc),
    127 	bcmpax_ccb_match, bcmpax_ccb_attach, NULL, NULL);
    128 
    129 static int
    130 bcmpax_ccb_match(device_t parent, cfdata_t cf, void *aux)
    131 {
    132 	struct bcmccb_attach_args * const ccbaa = aux;
    133 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    134 
    135 	if (strcmp(cf->cf_name, loc->loc_name))
    136 		return 0;
    137 
    138 #ifdef DIAGNOSTIC
    139 	const int port = cf->cf_loc[BCMCCBCF_PORT];
    140 #endif
    141 	KASSERT(port == BCMCCBCF_PORT_DEFAULT || port == loc->loc_port);
    142 
    143 	return 1;
    144 }
    145 
    146 static int
    147 bcmpax_iwin_init(struct bcmpax_softc *sc)
    148 {
    149 #if 0
    150 	uint32_t megs = (physical_end + 0xfffff - physical_start) >> 20;
    151 	uint32_t iwin_megs = min(256, megs);
    152 #if 1
    153 	bus_addr_t iwin1_start = physical_start;
    154 #else
    155 	bus_addr_t iwin1_start = 0;
    156 #endif
    157 #if 1
    158 	bcmpax_write_4(sc, PCIE_IARR_1_LOWER, iwin1_start | min(megs, 128));
    159 	bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, iwin1_start | 1);
    160 #else
    161 	bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, iwin1_start | min(megs, 128));
    162 	bcmpax_write_4(sc, PCIE_IARR_1_LOWER, iwin1_start | 1);
    163 #endif
    164 	bcmpax_conf_write(sc, 0, PCI_MAPREG_START+4, iwin1_start);
    165 	if (iwin_megs > 128) {
    166 		bus_addr_t iwin2_start = iwin1_start + 128*1024*1024;
    167 #if 1
    168 		bcmpax_write_4(sc, PCIE_IARR_2_LOWER, iwin2_start | min(megs - 128, 128));
    169 		bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, iwin2_start | 1);
    170 #else
    171 		bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, iwin2_start | min(megs - 128, 128));
    172 		bcmpax_write_4(sc, PCIE_IARR_2_LOWER, iwin2_start | 1);
    173 #endif
    174 		bcmpax_conf_write(sc, 0, PCI_MAPREG_START+8, iwin2_start);
    175 	}
    176 
    177 	if (megs <= iwin_megs) {
    178 		/*
    179 		 * We could can DMA to all of memory so we don't need to subregion!
    180 		 */
    181 		return 0;
    182 	}
    183 
    184 	return bus_dmatag_subregion(sc->sc_dmat, physical_start,
    185 	    physical_start + (iwin_megs << 20) - 1, &sc->sc_dmat, 0);
    186 #else
    187 	bcmpax_write_4(sc, PCIE_IARR_1_LOWER, 0);
    188 	bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, 0);
    189 	bcmpax_write_4(sc, PCIE_IARR_2_LOWER, 0);
    190 	bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, 0);
    191 	return 0;
    192 #endif
    193 }
    194 
    195 static void
    196 bcmpax_ccb_attach(device_t parent, device_t self, void *aux)
    197 {
    198 	struct bcmpax_softc * const sc = device_private(self);
    199 	struct bcmccb_attach_args * const ccbaa = aux;
    200 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    201 	const char * const xname = device_xname(self);
    202 	cfdata_t cf = device_cfdata(self);
    203 
    204 	sc->sc_dev = self;
    205 	if (cf->cf_flags & 2) {
    206 		sc->sc_dmat = &bcm53xx_coherent_dma_tag;
    207 	} else {
    208 		sc->sc_dmat = ccbaa->ccbaa_dmat;
    209 	}
    210 
    211 	for (u_int i = 0; i < 4; i++) {
    212 		snprintf(sc->sc_intrstring[i], sizeof(sc->sc_intrstring[i]),
    213 		    "%s int%c", xname, 'a' + i);
    214 	}
    215 
    216 	sc->sc_bst = ccbaa->ccbaa_ccb_bst;
    217 	bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
    218 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
    219 
    220 	/*
    221 	 * Kick the hardware into RC mode.
    222 	 */
    223 	bcmpax_write_4(sc, PCIE_CLK_CONTROL, 3);
    224 	delay(250);
    225 	bcmpax_write_4(sc, PCIE_CLK_CONTROL, 1);
    226 
    227 	uint32_t v = bcmpax_read_4(sc, PCIE_STRAP_STATUS);
    228 	const bool enabled = (v & STRAP_PCIE_IF_ENABLE) != 0;
    229 	const bool is_v2_p = (v & STRAP_PCIE_USER_FOR_CE_GEN1) == 0;
    230 	const bool is_x2_p = (v & STRAP_PCIE_USER_FOR_CE_1LANE) == 0;
    231 	const bool is_rc_p = (v & STRAP_PCIE_USER_RC_MODE) != 0;
    232 
    233 	aprint_naive("\n");
    234 	aprint_normal(": PCI Express V%u %u-lane %s Controller%s\n",
    235 	    is_v2_p ? 2 : 1,
    236 	    is_x2_p ? 2 : 1,
    237 	    is_rc_p ? "RC" : "EP",
    238 	    enabled ? "" : "(disabled)");
    239 	if (!enabled || !is_rc_p)
    240 		return;
    241 
    242 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
    243 	sc->sc_cfg_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
    244 
    245 	TAILQ_INIT(&sc->sc_intrs);
    246 
    247 	sc->sc_pc.pc_conf_v = sc;
    248 	sc->sc_pc.pc_attach_hook = bcmpax_attach_hook;
    249 	sc->sc_pc.pc_bus_maxdevs = bcmpax_bus_maxdevs;
    250 	sc->sc_pc.pc_make_tag = bcmpax_make_tag;
    251 	sc->sc_pc.pc_decompose_tag = bcmpax_decompose_tag;
    252 	sc->sc_pc.pc_conf_read = bcmpax_conf_read;
    253 	sc->sc_pc.pc_conf_write = bcmpax_conf_write;
    254 
    255 	sc->sc_pc.pc_intr_v = sc;
    256 	sc->sc_pc.pc_intr_map = bcmpax_intr_map;
    257 	sc->sc_pc.pc_intr_string = bcmpax_intr_string;
    258 	sc->sc_pc.pc_intr_evcnt = bcmpax_intr_evcnt;
    259 	sc->sc_pc.pc_intr_establish = bcmpax_intr_establish;
    260 	sc->sc_pc.pc_intr_disestablish = bcmpax_intr_disestablish;
    261 
    262 	sc->sc_pc.pc_conf_hook = bcmpax_conf_hook;
    263 	sc->sc_pc.pc_conf_interrupt = bcmpax_conf_interrupt;
    264 
    265 	sc->sc_pba_flags |= PCI_FLAGS_MRL_OKAY;
    266 	sc->sc_pba_flags |= PCI_FLAGS_MRM_OKAY;
    267 	sc->sc_pba_flags |= PCI_FLAGS_MWI_OKAY;
    268 	// sc->sc_pba_flags |= PCI_FLAGS_MSI_OKAY;
    269 	// sc->sc_pba_flags |= PCI_FLAGS_MSIX_OKAY;
    270 
    271 	for (size_t i = 0; i < loc->loc_nintrs; i++) {
    272 		sc->sc_ih[i] = intr_establish(loc->loc_intrs[0] + i, IPL_VM,
    273 		    IST_LEVEL, bcmpax_intr, sc);
    274 		if (sc->sc_ih[i] == NULL) {
    275 			aprint_error_dev(self,
    276 			    "failed to establish interrupt #%zu (%zu)\n", i,
    277 			    loc->loc_intrs[0] + i);
    278 			while (i-- > 0) {
    279 				intr_disestablish(sc->sc_ih[i]);
    280 			}
    281 			return;
    282 		}
    283 	}
    284 	aprint_normal_dev(self, "interrupting on irqs %d-%d\n",
    285 	     loc->loc_intrs[0], loc->loc_intrs[0] + loc->loc_nintrs - 1);
    286 
    287 	/*
    288 	 * Enable INTA-INTD
    289 	 */
    290 	bcmpax_write_4(sc, PCIE_SYS_RC_INTX_EN, 0x0f);
    291 
    292 	int offset;
    293 	const bool ok = pci_get_capability(&sc->sc_pc, 0, PCI_CAP_PCIEXPRESS,
    294 	    &offset, NULL);
    295 	KASSERT(ok);
    296 
    297 	/*
    298 	 * This will force the device to negotiate to a max of gen1.
    299 	 */
    300 	if (cf->cf_flags & 1) {
    301 		bcmpax_conf_write(sc, 0, offset + PCI_PCIE_LCSR2, 1);
    302 	}
    303 
    304 	/*
    305 	 * Now we wait (.25 sec) for the link to come up.
    306 	 */
    307 	offset += PCI_PCIE_LCSR;
    308 	for (size_t timo = 0;; timo++) {
    309 		const pcireg_t lcsr = bcmpax_conf_read(sc, 0, offset);
    310 		sc->sc_linkup = __SHIFTOUT(lcsr, PCI_PCIE_LCSR_NLW) != 0
    311 		    && (1 || (lcsr & PCI_PCIE_LCSR_DLACTIVE) != 0);
    312 		if (sc->sc_linkup || timo == 250) {
    313 			aprint_debug_dev(self,
    314 			    "lcsr=%#x nlw=%jd linkup=%d, timo=%zu\n",
    315 			    lcsr, __SHIFTOUT(lcsr, PCI_PCIE_LCSR_NLW),
    316 			    sc->sc_linkup, timo);
    317 			break;
    318 		}
    319 		DELAY(1000);
    320 	}
    321 
    322 	if (sc->sc_linkup) {
    323 		/*
    324 		 * Enable the inbound (device->memory) map.
    325 		 */
    326 		int error = bcmpax_iwin_init(sc);
    327 		if (error) {
    328 			aprint_error_dev(sc->sc_dev,
    329 			    "failed to subregion dma tag: %d\n", error);
    330 			return;
    331 		}
    332 
    333 		aprint_normal_dev(self, "iwin[1]=%#x/%#x iwin[2]=%#x/%#x\n",
    334 		    bcmpax_read_4(sc, PCIE_FUNC0_IMAP1),
    335 		    bcmpax_read_4(sc, PCIE_IARR_1_LOWER),
    336 		    bcmpax_read_4(sc, PCIE_FUNC0_IMAP2),
    337 		    bcmpax_read_4(sc, PCIE_IARR_2_LOWER));
    338 
    339 		paddr_t base = bcmpax_owins[loc->loc_port].owin_base;
    340 		psize_t size = bcmpax_owins[loc->loc_port].owin_size;
    341 		KASSERT((size & ~PCIE_OARR_ADDR) == 0);
    342 		if (size > 0) {
    343 			bcmpax_write_4(sc, PCIE_OARR_0, base);
    344 			bcmpax_write_4(sc, PCIE_OMAP_0_LOWER, base | 1);
    345 		}
    346 		if (size > __LOWEST_SET_BIT(PCIE_OARR_ADDR)) {
    347 			paddr_t base1 = base + __LOWEST_SET_BIT(PCIE_OARR_ADDR);
    348 			bcmpax_write_4(sc, PCIE_OARR_1, base1);
    349 			bcmpax_write_4(sc, PCIE_OMAP_1_LOWER, base1 | 1);
    350 		}
    351 
    352 		struct extent *memext = extent_create("pcimem", base,
    353 		     base + size, NULL, 0, EX_NOWAIT);
    354 
    355 		error = pci_configure_bus(&sc->sc_pc,
    356 		    NULL, memext, NULL, 0, arm_pcache.dcache_line_size);
    357 
    358 		extent_destroy(memext);
    359 
    360 		if (error) {
    361 			aprint_normal_dev(self, "configuration failed\n");
    362 			return;
    363 		}
    364 	}
    365 
    366 	struct pcibus_attach_args pba;
    367 	memset(&pba, 0, sizeof(pba));
    368 
    369 	pba.pba_flags = sc->sc_pba_flags;
    370 	pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
    371 	pba.pba_memt = sc->sc_bst;
    372 	pba.pba_dmat = sc->sc_dmat;
    373 	pba.pba_pc = &sc->sc_pc;
    374 	pba.pba_bus = 0;
    375 
    376 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    377 }
    378 
    379 static void
    380 bcmpax_attach_hook(device_t parent, device_t self,
    381     struct pcibus_attach_args *pba)
    382 {
    383 }
    384 
    385 static int
    386 bcmpax_bus_maxdevs(void *v, int bus)
    387 {
    388 	struct bcmpax_softc * const sc = v;
    389 
    390 	if (__predict_true(sc->sc_linkup))
    391 		return bus > 1 ? 32 : 1;
    392 
    393 	return bus ? 0 : 1;
    394 }
    395 
    396 static void
    397 bcmpax_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
    398 {
    399 	if (busp)
    400 		*busp = __SHIFTOUT(tag, CFG_ADDR_BUS);
    401 	if (devp)
    402 		*devp = __SHIFTOUT(tag, CFG_ADDR_DEV);
    403 	if (funcp)
    404 		*funcp = __SHIFTOUT(tag, CFG_ADDR_FUNC);
    405 }
    406 
    407 static pcitag_t
    408 bcmpax_make_tag(void *v, int bus, int dev, int func)
    409 {
    410 	return __SHIFTIN(bus, CFG_ADDR_BUS)
    411 	   | __SHIFTIN(dev, CFG_ADDR_DEV)
    412 	   | __SHIFTIN(func, CFG_ADDR_FUNC)
    413 	   | (bus == 0 ? CFG_ADDR_TYPE0 : CFG_ADDR_TYPE1);
    414 }
    415 
    416 static inline bus_size_t
    417 bcmpax_conf_addr_write(struct bcmpax_softc *sc, pcitag_t tag)
    418 {
    419 	if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV)) == 0) {
    420 		uint32_t reg = __SHIFTOUT(tag, CFG_ADDR_REG);
    421 		uint32_t func = __SHIFTOUT(tag, CFG_ADDR_FUNC);
    422 		bcmpax_write_4(sc, PCIE_CFG_IND_ADDR,
    423 		    __SHIFTIN(func, CFG_IND_ADDR_FUNC)
    424 		    | __SHIFTIN(reg, CFG_IND_ADDR_REG));
    425 		__asm __volatile("dsb");
    426 		return PCIE_CFG_IND_DATA;
    427 	}
    428 	if (sc->sc_linkup) {
    429 		bcmpax_write_4(sc, PCIE_CFG_ADDR, tag);
    430 		__asm __volatile("dsb");
    431 		return PCIE_CFG_DATA;
    432 	}
    433 	return 0;
    434 }
    435 
    436 static pcireg_t
    437 bcmpax_conf_read(void *v, pcitag_t tag, int reg)
    438 {
    439 	struct bcmpax_softc * const sc = v;
    440 
    441 	/*
    442 	 * Even in RC mode, the PCI Express Root Complex return itself
    443 	 * as BCM Ethernet Controller!.  We could change ppb.c to match it
    444 	 * but we'll just lie and say we are a PPB bridge.
    445 	 */
    446 	if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV|CFG_ADDR_FUNC)) == 0
    447 	    && reg == PCI_CLASS_REG) {
    448 		return PCI_CLASS_CODE(PCI_CLASS_BRIDGE,
    449 				      PCI_SUBCLASS_BRIDGE_PCI, 0);
    450 	}
    451 
    452 	//printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
    453 
    454 	mutex_enter(sc->sc_cfg_lock);
    455 	bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
    456 
    457 	//printf(" [from %#lx]:\n", data_reg);
    458 
    459 	pcireg_t rv;
    460 	if (data_reg)
    461 		rv = bcmpax_read_4(sc, data_reg);
    462 	else
    463 		rv = 0xffffffff;
    464 
    465 	mutex_exit(sc->sc_cfg_lock);
    466 
    467 	//printf(" %#x\n", rv);
    468 
    469 	return rv;
    470 }
    471 
    472 static void
    473 bcmpax_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
    474 {
    475 	struct bcmpax_softc * const sc = v;
    476 
    477 	mutex_enter(sc->sc_cfg_lock);
    478 	bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
    479 
    480 	//printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
    481 
    482 	if (data_reg) {
    483 		//printf(" [to %#lx]:\n", data_reg);
    484 		bcmpax_write_4(sc, data_reg, val);
    485 		//printf(" %#x\n", val);
    486 	}
    487 
    488 	mutex_exit(sc->sc_cfg_lock);
    489 }
    490 
    491 static void
    492 bcmpax_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
    493 {
    494 	*ilinep = 5; /* (ipin + swiz) & 3; */
    495 }
    496 
    497 static int
    498 bcmpax_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
    499 {
    500 	if (func > 0)
    501 		return 0;
    502 
    503 	return PCI_CONF_ENABLE_MEM | PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_BM;
    504 }
    505 
    506 static int
    507 bcmpax_intr(void *v)
    508 {
    509 	struct bcmpax_softc * const sc = v;
    510 
    511 	while (bcmpax_read_4(sc, PCIE_SYS_RC_INTX_CSR)) {
    512 		struct bcmpax_intrhand *ih;
    513 		mutex_enter(sc->sc_lock);
    514 		const uint32_t lastgen = sc->sc_intrgen;
    515 		TAILQ_FOREACH(ih, &sc->sc_intrs, ih_link) {
    516 			int (* const func)(void *) = ih->ih_func;
    517 			void * const arg = ih->ih_arg;
    518 			mutex_exit(sc->sc_lock);
    519 			int rv = (*func)(arg);
    520 			if (rv) {
    521 				return rv;
    522 			}
    523 			mutex_enter(sc->sc_lock);
    524 			/*
    525 			 * Check to see if the interrupt list changed.
    526 			 * If so, restart from the beginning.
    527 			 */
    528 			if (lastgen != sc->sc_intrgen)
    529 				break;
    530 		}
    531 		mutex_exit(sc->sc_lock);
    532 	}
    533 
    534 	return 0;
    535 }
    536 
    537 static int
    538 bcmpax_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *pihp)
    539 {
    540 	if (pa->pa_intrpin == 0)
    541 		return EINVAL;
    542 
    543 	*pihp = pa->pa_intrpin;
    544 	return 0;
    545 }
    546 
    547 static const char *
    548 bcmpax_intr_string(void *v, pci_intr_handle_t pih)
    549 {
    550 	struct bcmpax_softc * const sc = v;
    551 
    552 	if (pih)
    553 		return sc->sc_intrstring[pih - PCI_INTERRUPT_PIN_A];
    554 
    555 	return NULL;
    556 }
    557 
    558 static const struct evcnt *
    559 bcmpax_intr_evcnt(void *v, pci_intr_handle_t pih)
    560 {
    561 	return NULL;
    562 }
    563 
    564 static void *
    565 bcmpax_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
    566    int (*func)(void *), void *arg)
    567 {
    568 	struct bcmpax_softc * const sc = v;
    569 
    570 	KASSERT(!cpu_intr_p());
    571 	KASSERT(!cpu_softintr_p());
    572 	KASSERT(ipl == IPL_VM);
    573 	KASSERT(func != NULL);
    574 	KASSERT(arg != NULL);
    575 
    576 	if (pih == 0)
    577 		return NULL;
    578 
    579 	struct bcmpax_intrhand * const ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
    580 
    581 	ih->ih_func = func;
    582 	ih->ih_arg = arg;
    583 
    584 	mutex_enter(sc->sc_lock);
    585 	TAILQ_INSERT_TAIL(&sc->sc_intrs, ih, ih_link);
    586 	mutex_exit(sc->sc_lock);
    587 
    588 	return ih;
    589 }
    590 
    591 static void
    592 bcmpax_intr_disestablish(void *v, void *vih)
    593 {
    594 	struct bcmpax_softc * const sc = v;
    595 	struct bcmpax_intrhand * const ih = vih;
    596 
    597 	mutex_enter(sc->sc_lock);
    598 	TAILQ_REMOVE(&sc->sc_intrs, ih, ih_link);
    599 	sc->sc_intrgen++;
    600 	mutex_exit(sc->sc_lock);
    601 
    602 	kmem_free(ih, sizeof(*ih));
    603 }
    604