Home | History | Annotate | Line # | Download | only in rmi
rmixl_pcix.c revision 1.1.2.10
      1 /*	$NetBSD: rmixl_pcix.c,v 1.1.2.10 2011/12/24 01:57:54 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * PCI configuration support for RMI XLR SoC
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: rmixl_pcix.c,v 1.1.2.10 2011/12/24 01:57:54 matt Exp $");
     44 
     45 #include "opt_pci.h"
     46 #include "pci.h"
     47 
     48 #include <sys/cdefs.h>
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/device.h>
     53 #include <sys/extent.h>
     54 #include <sys/malloc.h>
     55 #include <sys/kernel.h>		/* for 'hz' */
     56 #include <sys/cpu.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #include <machine/bus.h>
     61 #include <machine/intr.h>
     62 
     63 #include <mips/rmi/rmixlreg.h>
     64 #include <mips/rmi/rmixlvar.h>
     65 #include <mips/rmi/rmixl_intr.h>
     66 #include <mips/rmi/rmixl_pcixvar.h>
     67 
     68 #include <mips/rmi/rmixl_obiovar.h>
     69 
     70 #include <dev/pci/pcivar.h>
     71 #include <dev/pci/pcidevs.h>
     72 #include <dev/pci/pciconf.h>
     73 
     74 #ifdef	PCI_NETBSD_CONFIGURE
     75 #include <mips/cache.h>
     76 #endif
     77 
     78 #include <machine/pci_machdep.h>
     79 
     80 #ifdef PCI_DEBUG
     81 int rmixl_pcix_debug = PCI_DEBUG;
     82 # define DPRINTF(x)	do { if (rmixl_pcix_debug) printf x ; } while (0)
     83 #else
     84 # define DPRINTF(x)
     85 #endif
     86 
     87 #ifndef DDB
     88 # define STATIC static
     89 #else
     90 # define STATIC
     91 #endif
     92 
     93 
     94 /*
     95  * XLR PCI-X Extended Configuration Registers
     96  * Note:
     97  * - MSI-related regs are omitted
     98  * - Device mode regs are omitted
     99  */
    100 #define RMIXL_PCIX_ECFG_HOST_BAR0_ADDR	0x100	/* Host BAR0 Address */
    101 #define RMIXL_PCIX_ECFG_HOST_BAR1_ADDR	0x104	/* Host BAR1 Address */
    102 #define RMIXL_PCIX_ECFG_HOST_BAR2_ADDR	0x108	/* Host BAR2 Address */
    103 #define RMIXL_PCIX_ECFG_HOST_BAR3_ADDR	0x10c	/* Host BAR3 Address */
    104 #define RMIXL_PCIX_ECFG_HOST_BAR4_ADDR	0x110	/* Host BAR4 Address */
    105 #define RMIXL_PCIX_ECFG_HOST_BAR5_ADDR	0x114	/* Host BAR5 Address */
    106 #define RMIXL_PCIX_ECFG_HOST_BAR0_SIZE	0x118	/* Host BAR0 Size */
    107 #define RMIXL_PCIX_ECFG_HOST_BAR1_SIZE	0x11c	/* Host BAR1 Size */
    108 #define RMIXL_PCIX_ECFG_HOST_BAR2_SIZE	0x120	/* Host BAR2 Size */
    109 #define RMIXL_PCIX_ECFG_HOST_BAR3_SIZE	0x124	/* Host BAR3 Size */
    110 #define RMIXL_PCIX_ECFG_HOST_BAR4_SIZE	0x128	/* Host BAR4 Size */
    111 #define RMIXL_PCIX_ECFG_HOST_BAR5_SIZE	0x12c	/* Host BAR5 Size */
    112 #define RMIXL_PCIX_ECFG_MATCH_BIT_ADDR	0x130	/* Match Bit Address BAR */
    113 #define RMIXL_PCIX_ECFG_MATCH_BIT_SIZE	0x134	/* Match Bit Size BAR */
    114 #define RMIXL_PCIX_ECFG_XLR_CONTROL	0x138	/* XLR Control reg */
    115 #define RMIXL_PCIX_ECFG_INTR_CONTROL	0x13c	/* Interrupt Control reg */
    116 #define RMIXL_PCIX_ECFG_INTR_STATUS	0x140	/* Interrupt Status reg */
    117 #define RMIXL_PCIX_ECFG_INTR_ERR_STATUS	0x144	/* Interrupt Error Status reg */
    118 #define RMIXL_PCIX_ECFG_HOST_MODE_STS	0x178	/* Host Mode Status */
    119 #define RMIXL_PCIX_ECFG_XLR_MBLE	0x17c	/* XLR Match Byte Lane Enable */
    120 #define RMIXL_PCIX_ECFG_HOST_XROM_ADDR	0x180	/* Host Expansion ROM Address */
    121 #define RMIXL_PCIX_ECFG_HOST_XROM_SIZE	0x184	/* Host Expansion ROM Size */
    122 #define RMIXL_PCIX_ECFG_HOST_MODE_CTL	0x18c	/* Host Mode Control */
    123 #define RMIXL_PCIX_ECFG_TXCAL_CTL	0x1a0	/* TX Calibration Preset Control */
    124 #define RMIXL_PCIX_ECFG_TXCAL_COUNT	0x1a4	/* TX Calibration Preset Count */
    125 
    126 /*
    127  * RMIXL_PCIX_ECFG_INTR_CONTROL bit defines
    128  */
    129 #define PCIX_INTR_CONTROL_RESV		__BITS(31,8)
    130 #define PCIX_INTR_CONTROL_MSI1_MASK	__BIT(7)
    131 #define PCIX_INTR_CONTROL_MSI0_MASK	__BIT(6)
    132 #define PCIX_INTR_CONTROL_INTD_MASK	__BIT(5)
    133 #define PCIX_INTR_CONTROL_INTC_MASK	__BIT(4)
    134 #define PCIX_INTR_CONTROL_INTB_MASK	__BIT(3)
    135 #define PCIX_INTR_CONTROL_INTA_MASK	__BIT(2)
    136 #define PCIX_INTR_CONTROL_TMSI		__BIT(1)	/* Trigger MSI Interrupt */
    137 #define PCIX_INTR_CONTROL_DIA		__BIT(0)	/* Device Interrupt through INTA Pin */
    138 #define PCIX_INTR_CONTROL_MASK_ALL	\
    139 		(PCIX_INTR_CONTROL_MSI1_MASK|PCIX_INTR_CONTROL_MSI0_MASK	\
    140 		|PCIX_INTR_CONTROL_INTD_MASK|PCIX_INTR_CONTROL_INTC_MASK	\
    141 		|PCIX_INTR_CONTROL_INTB_MASK|PCIX_INTR_CONTROL_INTA_MASK)
    142 
    143 /*
    144  * RMIXL_PCIX_ECFG_INTR_STATUS bit defines
    145  */
    146 #define PCIX_INTR_STATUS_RESV		__BITS(31,6)
    147 #define PCIX_INTR_STATUS_MSI1		__BIT(5)
    148 #define PCIX_INTR_STATUS_MSI0		__BIT(4)
    149 #define PCIX_INTR_STATUS_INTD		__BIT(3)
    150 #define PCIX_INTR_STATUS_INTC		__BIT(2)
    151 #define PCIX_INTR_STATUS_INTB		__BIT(1)
    152 #define PCIX_INTR_STATUS_INTA		__BIT(0)
    153 
    154 /*
    155  * RMIXL_PCIX_ECFG_INTR_ERR_STATUS bit defines
    156  */
    157 #define PCIX_INTR_ERR_STATUS_RESa	__BITS(31,5)
    158 #define PCIX_INTR_ERR_STATUS_SERR	__BIT(4)	/* System Error */
    159 #define PCIX_INTR_ERR_STATUS_RESb	__BIT(3)
    160 #define PCIX_INTR_ERR_STATUS_TE		__BIT(2)	/* Target Error */
    161 #define PCIX_INTR_ERR_STATUS_IE		__BIT(1)	/* Initiator Error */
    162 #define PCIX_INTR_ERR_STATUS_RCE	__BIT(0)	/* Retry Count Expired */
    163 #define PCIX_INTR_ERR_STATUS_RESV	\
    164 		(PCIX_INTR_ERR_STATUS_RESa|PCIX_INTR_ERR_STATUS_RESb)
    165 
    166 /*
    167  * RMIXL_PCIX_ECFG_HOST_MODE_CTL bit defines
    168  */
    169 #define PCIX_HOST_MODE_CTL_HDMSTAT	__BIT(1)	/* Host/Dev Mode status
    170 							 *  read-only
    171 							 *  1 = host
    172 							 *  0 = device
    173 							 */
    174 #define PCIX_HOST_MODE_CTL_HOSTSWRST	__BIT(0)	/* Host soft reset
    175 							 *  set to 1 to reset
    176 							 *  set to 0 to un-reset
    177 							 */
    178 
    179 
    180 #if BYTE_ORDER == BIG_ENDIAN
    181 # define RMIXL_PCIXREG_BASE	RMIXL_IO_DEV_PCIX_EB
    182 #else
    183 # define RMIXL_PCIXREG_BASE	RMIXL_IO_DEV_PCIX_EL
    184 #endif
    185 
    186 #define RMIXL_PCIXREG_VADDR(o)				\
    187 	(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(	\
    188 		rmixl_configuration.rc_io.r_pbase	\
    189 		+ RMIXL_PCIXREG_BASE + (o))
    190 
    191 #define RMIXL_PCIXREG_READ(o)     (*RMIXL_PCIXREG_VADDR(o))
    192 #define RMIXL_PCIXREG_WRITE(o,v)  *RMIXL_PCIXREG_VADDR(o) = (v)
    193 
    194 #define RMIXL_PCIX_BAR_INIT(rp, reg, bar, size, align) {		\
    195 	struct extent *ext = rmixl_configuration.rc_phys_ex;		\
    196 	u_long region_start;						\
    197 	int err;							\
    198 									\
    199 	err = extent_alloc(ext, (size), (align), 0UL, EX_NOWAIT,	\
    200 	    &region_start);						\
    201 	if (err != 0)							\
    202 		panic("%s: extent_alloc(%p, %#lx, %#lx, %#lx, %#x, %p)",\
    203 		    __func__, ext, size, align, 0UL, EX_NOWAIT,		\
    204 		    &region_start);					\
    205 	const uint64_t ba = (uint64_t)region_start << 20;		\
    206 	bar = RMIXL_PCIX_##reg##_BAR(ba, 1);				\
    207 	DPRINTF(("PCIX %s BAR was not enabled by firmware\n"		\
    208 	    "enabling %s at phys %#" PRIxBUSADDR ", size %lu MB\n",	\
    209 	    __STRING(reg), __STRING(reg), ba, size));			\
    210 	RMIXL_IOREG_WRITE(RMIXL_IO_DEV_BRIDGE 				\
    211 	    + RMIXLR_SBC_PCIX_##reg##_BAR, bar);			\
    212 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE			\
    213 	    + RMIXLR_SBC_PCIX_##reg##_BAR);				\
    214 	DPRINTF(("%s: %s BAR %#x\n", __func__, __STRING(reg), bar));	\
    215 	(rp)->r_pbase = ba;						\
    216 	(rp)->r_size = (size) << 20;					\
    217 }
    218 
    219 
    220 #define RMIXL_PCIX_EVCNT(sc, intrpin, cpu)	\
    221 	&(sc)->sc_evcnts[(intrpin) * (ncpu) + (cpu)]
    222 
    223 
    224 static int	rmixl_pcix_match(device_t, cfdata_t, void *);
    225 static void	rmixl_pcix_attach(device_t, device_t, void *);
    226 static void	rmixl_pcix_init(rmixl_pcix_softc_t *);
    227 static void	rmixl_pcix_init_errors(rmixl_pcix_softc_t *);
    228 static void	rmixl_pcix_attach_hook(struct device *, struct device *,
    229 		    struct pcibus_attach_args *);
    230 static void	rmixl_pcix_intcfg(rmixl_pcix_softc_t *);
    231 static void	rmixl_pcix_errata(rmixl_pcix_softc_t *);
    232 static void	rmixl_conf_interrupt(void *, int, int, int, int, int *);
    233 static int	rmixl_pcix_bus_maxdevs(void *, int);
    234 static pcitag_t	rmixl_pcix_make_tag(void *, int, int, int);
    235 static void	rmixl_pcix_decompose_tag(void *, pcitag_t, int *, int *, int *);
    236 void		rmixl_pcix_tag_print(const char *restrict, void *, pcitag_t,				int, vaddr_t, u_long);
    237 static pcireg_t	rmixl_pcix_conf_read(void *, pcitag_t, int);
    238 static void	rmixl_pcix_conf_write(void *, pcitag_t, int, pcireg_t);
    239 #ifdef __PCI_DEV_FUNCORDER
    240 static bool	rmixl_pcix_dev_funcorder(void *, int, int, int, char *);
    241 #endif
    242 
    243 static int	rmixl_pcix_intr_map(struct pci_attach_args *,
    244 		    pci_intr_handle_t *);
    245 static const char *
    246 		rmixl_pcix_intr_string(void *, pci_intr_handle_t);
    247 static const struct evcnt *
    248 		rmixl_pcix_intr_evcnt(void *, pci_intr_handle_t);
    249 static pci_intr_handle_t
    250 		rmixl_pcix_make_pih(u_int, u_int);
    251 static void	rmixl_pcix_decompose_pih(pci_intr_handle_t, u_int *, u_int *);
    252 static void	rmixl_pcix_intr_disestablish(void *, void *);
    253 static void	*rmixl_pcix_intr_establish(void *, pci_intr_handle_t,
    254 		    int, int (*)(void *), void *);
    255 static rmixl_pcix_intr_t *
    256                 rmixl_pcix_pip_add_1(rmixl_pcix_softc_t *, int, int);
    257 static void     rmixl_pcix_pip_free_callout(rmixl_pcix_intr_t *);
    258 static void     rmixl_pcix_pip_free(void *);
    259 static int	rmixl_pcix_intr(void *);
    260 static int	rmixl_pcix_error_intr(void *);
    261 
    262 
    263 CFATTACH_DECL_NEW(rmixl_pcix, sizeof(rmixl_pcix_softc_t),
    264     rmixl_pcix_match, rmixl_pcix_attach, NULL, NULL);
    265 
    266 
    267 static int rmixl_pcix_found;
    268 
    269 
    270 static int
    271 rmixl_pcix_match(device_t parent, cfdata_t cf, void *aux)
    272 {
    273 	uint32_t r;
    274 
    275 	/*
    276 	 * PCI-X interface exists on XLR chips only
    277 	 */
    278 	if (! cpu_rmixlr(mips_options.mips_cpu))
    279 		return 0;
    280 
    281 	/* XXX
    282 	 * for now there is only one PCI-X Interface on chip
    283 	 * and only one chip in the system
    284 	 * this could change with furture RMI XL family designs
    285 	 * or when we have multi-chip systems.
    286 	 */
    287 	if (rmixl_pcix_found)
    288 		return 0;
    289 
    290 	/* read Host Mode Control register */
    291 	r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_MODE_CTL);
    292 	r &= PCIX_HOST_MODE_CTL_HDMSTAT;
    293 	if (r == 0)
    294 		return 0;	/* strapped for Device Mode */
    295 
    296 	return 1;
    297 }
    298 
    299 static void
    300 rmixl_pcix_attach(device_t parent, device_t self, void *aux)
    301 {
    302 	rmixl_pcix_softc_t *sc = device_private(self);
    303 	struct obio_attach_args *obio = aux;
    304 	struct rmixl_config *rcp = &rmixl_configuration;
    305         struct pcibus_attach_args pba;
    306 	uint32_t bar;
    307 
    308 	rmixl_pcix_found = 1;
    309 	sc->sc_dev = self;
    310 	sc->sc_dmat29 = obio->obio_dmat29;
    311 	sc->sc_dmat32 = obio->obio_dmat32;
    312 	sc->sc_dmat64 = obio->obio_dmat64;
    313 	sc->sc_pc = &rcp->rc_pci_chipset;
    314 
    315 	aprint_normal(": RMI XLR PCI-X Interface\n");
    316 
    317 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_HIGH);
    318 
    319 	rmixl_pcix_intcfg(sc);
    320 
    321 	rmixl_pcix_errata(sc);
    322 
    323 	/*
    324 	 * check XLR Control Register
    325 	 */
    326 	DPRINTF(("%s: XLR_CONTROL=%#x\n", __func__,
    327 		RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_CONTROL)));
    328 
    329 	/*
    330 	 * HBAR[0]   if a 32 bit BAR, or
    331 	 * HBAR[0,1] if a 64 bit BAR pair
    332 	 * must cover all RAM
    333 	 */
    334 	extern u_quad_t mem_cluster_maxaddr;
    335 	uint64_t hbar_addr;
    336 	uint64_t hbar_size;
    337 	uint32_t hbar_size_lo, hbar_size_hi;
    338 	uint32_t hbar_addr_lo, hbar_addr_hi;
    339 
    340 	hbar_addr_lo = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR0_ADDR);
    341 	hbar_addr_hi = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR1_ADDR);
    342 	hbar_size_lo = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR0_SIZE);
    343 	hbar_size_hi = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR1_SIZE);
    344 
    345 	hbar_addr = (u_quad_t)(hbar_addr_lo & PCI_MAPREG_MEM_ADDR_MASK);
    346 	hbar_size = hbar_size_lo;
    347 	if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0) {
    348 		hbar_addr |= (uint64_t)hbar_addr_hi << 32;
    349 		hbar_size |= (uint64_t)hbar_size_hi << 32;
    350 	}
    351 	if ((hbar_addr != 0) || (hbar_size < mem_cluster_maxaddr)) {
    352 		int error;
    353 
    354 		aprint_error_dev(self, "HostBAR0 addr %#x, size %#x\n",
    355 			hbar_addr_lo, hbar_size_lo);
    356 		if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0)
    357 			aprint_error_dev(self, "HostBAR1 addr %#x, size %#x\n",
    358 				hbar_addr_hi, hbar_size_hi);
    359 		aprint_error_dev(self, "WARNING: firmware PCI-X setup error: "
    360 			"RAM %#"PRIx64"..%#"PRIx64" not accessible by Host BAR, "
    361 			"enabling DMA bounce buffers\n",
    362 			hbar_size, mem_cluster_maxaddr-1);
    363 
    364 		/*
    365 		 * force use of bouce buffers for inaccessible RAM addrs
    366 		 */
    367 		if (hbar_size < ((uint64_t)1 << 32)) {
    368 			error = bus_dmatag_subregion(sc->sc_dmat32,
    369 				0, (bus_addr_t)hbar_size, &sc->sc_dmat32,
    370 				BUS_DMA_NOWAIT);
    371 			if (error)
    372 				panic("%s: failed to subregion 32-bit dma tag:"
    373 					 " error %d", __func__, error);
    374 			sc->sc_dmat64 = NULL;
    375 		} else {
    376 			error = bus_dmatag_subregion(sc->sc_dmat64,
    377 				0, (bus_addr_t)hbar_size, &sc->sc_dmat64,
    378 				BUS_DMA_NOWAIT);
    379 			if (error)
    380 				panic("%s: failed to subregion 64-bit dma tag:"
    381 					" error %d", __func__, error);
    382 		}
    383 	}
    384 
    385 	/*
    386 	 * check PCI-X interface byteswap setup
    387 	 * ensure 'Match Byte Lane' is disabled
    388 	 */
    389 	uint32_t mble, mba, mbs;
    390 	mble = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_MBLE);
    391 	mba  = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_ADDR);
    392 	mbs  = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_SIZE);
    393 	DPRINTF(("%s: MBLE=%#x, MBA=%#x, MBS=%#x\n", __func__, mble, mba, mbs));
    394 	if ((mble & __BIT(40)) != 0)
    395 		RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_XLR_MBLE, 0);
    396 
    397 	/*
    398 	 * get PCI config space base addr from SBC PCIe CFG BAR
    399 	 * initialize it if necessary
    400  	 */
    401 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_CFG_BAR);
    402 	DPRINTF(("%s: PCIX_CFG_BAR %#x\n", __func__, bar));
    403 	if ((bar & RMIXL_PCIX_CFG_BAR_ENB) == 0) {
    404 		u_long n = RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
    405 		RMIXL_PCIX_BAR_INIT(&rcp->rc_pci_cfg, CFG, bar, n, n);
    406 	}
    407 
    408 	/*
    409 	 * get PCI MEM space base [addr, size] from SBC PCIe MEM BAR
    410 	 * initialize it if necessary
    411  	 */
    412 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
    413 	DPRINTF(("%s: PCIX_MEM_BAR %#x\n", __func__, bar));
    414 	if ((bar & RMIXL_PCIX_MEM_BAR_ENB) == 0) {
    415 		u_long n = 256;				/* 256 MB */
    416 		RMIXL_PCIX_BAR_INIT(&rcp->rc_pci_mem, MEM, bar, n, n);
    417 	}
    418 
    419 	/*
    420 	 * get PCI IO space base [addr, size] from SBC PCIe IO BAR
    421 	 * initialize it if necessary
    422  	 */
    423 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
    424 	DPRINTF(("%s: PCIX_IO_BAR %#x\n", __func__, bar));
    425 	if ((bar & RMIXL_PCIX_IO_BAR_ENB) == 0) {
    426 		u_long n = 32;				/* 32 MB */
    427 		RMIXL_PCIX_BAR_INIT(&rcp->rc_pci_io, IO, bar, n, n);
    428 	}
    429 
    430 	/*
    431 	 * initialize the PCI CFG bus space tag
    432 	 */
    433 	sc->sc_pci_cfg_memt = &rcp->rc_pci_cfg_memt;
    434 	rmixl_pci_cfg_el_bus_mem_init(sc->sc_pci_cfg_memt, rcp);
    435 	if (bus_space_map(sc->sc_pci_cfg_memt, 0, rcp->rc_pci_cfg.r_size / 2,
    436 	    0, &sc->sc_pci_cfg_memh))
    437 		panic("%s: failed to map pci CFG space!\n", __func__);
    438 
    439 	/*
    440 	 * initialize the PCI MEM and IO bus space tags
    441 	 */
    442 	rmixl_pci_bus_mem_init(&rcp->rc_pci_memt, rcp);
    443 	rmixl_pci_bus_io_init(&rcp->rc_pci_iot, rcp);
    444 
    445 	/*
    446 	 * initialize the extended configuration regs
    447 	 */
    448 	rmixl_pcix_init_errors(sc);
    449 
    450 	/*
    451 	 * initialize the PCI chipset tag
    452 	 */
    453 	rmixl_pcix_init(sc);
    454 
    455 	/*
    456 	 * attach the PCI bus
    457 	 */
    458 	memset(&pba, 0, sizeof(pba));
    459 	pba.pba_memt = &rcp->rc_pci_memt;
    460 	pba.pba_iot =  &rcp->rc_pci_iot;
    461 	pba.pba_dmat = sc->sc_dmat32;
    462 	pba.pba_dmat64 = sc->sc_dmat64;
    463 	pba.pba_pc = sc->sc_pc;
    464 	pba.pba_bus = 0;
    465 	pba.pba_bridgetag = NULL;
    466 	pba.pba_intrswiz = 0;
    467 	pba.pba_intrtag = 0;
    468 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
    469 		PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
    470 
    471 	(void) config_found_ia(self, "pcibus", &pba, pcibusprint);
    472 }
    473 
    474 /*
    475  * rmixl_pcix_intcfg - init PCI-X interrupt control
    476  */
    477 static void
    478 rmixl_pcix_intcfg(rmixl_pcix_softc_t *sc)
    479 {
    480 	size_t size;
    481 	rmixl_pcix_evcnt_t *ev;
    482 
    483 	DPRINTF(("%s\n", __func__));
    484 
    485 	/* mask all interrupts until they are established */
    486 	RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL,
    487 		PCIX_INTR_CONTROL_MASK_ALL);
    488 
    489 	/*
    490 	 * read-to-clear any pre-existing interrupts
    491 	 * XXX MSI bits in STATUS are also documented as write 1 to clear in PRM
    492 	 */
    493 	(void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
    494 	(void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
    495 
    496 	/* initialize the (non-error interrupt) dispatch handles */
    497 	sc->sc_intr = NULL;
    498 
    499 	/*
    500 	 * allocate per-cpu, per-pin interrupt event counters
    501 	 */
    502 	size = ncpu * PCI_INTERRUPT_PIN_MAX * sizeof(rmixl_pcix_evcnt_t);
    503 	ev = malloc(size, M_DEVBUF, M_NOWAIT);
    504 	if (ev == NULL)
    505 		panic("%s: cannot malloc evcnts\n", __func__);
    506 	sc->sc_evcnts = ev;
    507 	for (int pin=PCI_INTERRUPT_PIN_A; pin <= PCI_INTERRUPT_PIN_MAX; pin++) {
    508 		for (int cpu=0; cpu < ncpu; cpu++) {
    509 			ev = RMIXL_PCIX_EVCNT(sc, pin - 1, cpu);
    510 			snprintf(ev->name, sizeof(ev->name),
    511 				"cpu%d, pin %d", cpu, pin);
    512 			evcnt_attach_dynamic(&ev->evcnt, EVCNT_TYPE_INTR,
    513 				NULL, "rmixl_pcix", ev->name);
    514 		}
    515 	}
    516 
    517 	/*
    518 	 * establish PCIX error interrupt handler
    519 	 */
    520 	sc->sc_fatal_ih = rmixl_intr_establish(24,
    521 		IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
    522 		rmixl_pcix_error_intr, sc, false);
    523 	if (sc->sc_fatal_ih == NULL)
    524 		panic("%s: cannot establish irq %d", __func__, 24);
    525 }
    526 
    527 static void
    528 rmixl_pcix_errata(rmixl_pcix_softc_t *sc)
    529 {
    530 	/* nothing */
    531 }
    532 
    533 static void
    534 rmixl_pcix_init(rmixl_pcix_softc_t *sc)
    535 {
    536 	pci_chipset_tag_t pc = sc->sc_pc;
    537 
    538 	pc->pc_conf_v = (void *)sc;
    539 	pc->pc_attach_hook = rmixl_pcix_attach_hook;
    540 	pc->pc_bus_maxdevs = rmixl_pcix_bus_maxdevs;
    541 	pc->pc_make_tag = rmixl_pcix_make_tag;
    542 	pc->pc_decompose_tag = rmixl_pcix_decompose_tag;
    543 	pc->pc_conf_read = rmixl_pcix_conf_read;
    544 	pc->pc_conf_write = rmixl_pcix_conf_write;
    545 #ifdef __PCI_DEV_FUNCORDER
    546 	pc->pc_dev_funcorder = rmixl_pcix_dev_funcorder;
    547 #endif
    548 
    549 	pc->pc_intr_v = (void *)sc;
    550 	pc->pc_intr_map = rmixl_pcix_intr_map;
    551 	pc->pc_intr_string = rmixl_pcix_intr_string;
    552 	pc->pc_intr_evcnt = rmixl_pcix_intr_evcnt;
    553 	pc->pc_intr_establish = rmixl_pcix_intr_establish;
    554 	pc->pc_intr_disestablish = rmixl_pcix_intr_disestablish;
    555 	pc->pc_conf_interrupt = rmixl_conf_interrupt;
    556 
    557 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
    558 	/*
    559 	 * Configure the PCI bus.
    560 	 */
    561 	struct rmixl_config *rcp = &rmixl_configuration;
    562 	struct extent *ioext, *memext;
    563 
    564 	aprint_normal_dev(sc->sc_dev, "configuring PCI bus\n");
    565 
    566 	ioext  = extent_create("pciio",
    567 		rcp->rc_pci_io.r_pbase,
    568 		rcp->rc_pci_io.r_pbase + rcp->rc_pci_io.r_size - 1,
    569 		M_DEVBUF, NULL, 0, EX_NOWAIT);
    570 
    571 	memext = extent_create("pcimem",
    572 		rcp->rc_pci_mem.r_pbase,
    573 		rcp->rc_pci_mem.r_pbase + rcp->rc_pci_mem.r_size - 1,
    574 		M_DEVBUF, NULL, 0, EX_NOWAIT);
    575 
    576 	pci_configure_bus(pc, ioext, memext, NULL, 0,
    577 	    mips_cache_info.mci_dcache_align);
    578 
    579 	extent_destroy(ioext);
    580 	extent_destroy(memext);
    581 #endif
    582 }
    583 
    584 static void
    585 rmixl_pcix_init_errors(rmixl_pcix_softc_t *sc)
    586 {
    587 	/* nothing */
    588 }
    589 
    590 void
    591 rmixl_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
    592 {
    593 	DPRINTF(("%s: %p, %d, %d, %d, %d, %p\n",
    594 		__func__, v, bus, dev, ipin, swiz, iline));
    595 }
    596 
    597 void
    598 rmixl_pcix_attach_hook(struct device *parent, struct device *self,
    599 	struct pcibus_attach_args *pba)
    600 {
    601 	DPRINTF(("%s: pba_bus %d, pba_bridgetag %p, pc_conf_v %p\n",
    602 		__func__, pba->pba_bus, pba->pba_bridgetag,
    603 		pba->pba_pc->pc_conf_v));
    604 }
    605 
    606 int
    607 rmixl_pcix_bus_maxdevs(void *v, int busno)
    608 {
    609 	return (32);	/* XXX depends on the family of XLS SoC */
    610 }
    611 
    612 /*
    613  * XLS pci tag is a 40 bit address composed thusly:
    614  *	39:25   (reserved)
    615  *	24      Swap (0=little, 1=big endian)
    616  *	23:16   Bus number
    617  *	15:11   Device number
    618  *	10:8    Function number
    619  *	7:0     Register number
    620  *
    621  * Note: this is the "native" composition for addressing CFG space, but not for ECFG space.
    622  */
    623 pcitag_t
    624 rmixl_pcix_make_tag(void *v, int bus, int dev, int fun)
    625 {
    626 	return ((bus << 16) | (dev << 11) | (fun << 8));
    627 }
    628 
    629 void
    630 rmixl_pcix_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    631 {
    632 	if (bp != NULL)
    633 		*bp = (tag >> 16) & 0xff;
    634 	if (dp != NULL)
    635 		*dp = (tag >> 11) & 0x1f;
    636 	if (fp != NULL)
    637 		*fp = (tag >> 8) & 0x7;
    638 }
    639 
    640 void
    641 rmixl_pcix_tag_print(const char *restrict s, void *v, pcitag_t tag, int offset,
    642 	vaddr_t va, u_long r)
    643 {
    644 	int bus, dev, fun;
    645 
    646 	rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
    647 	printf("%s: %d/%d/%d/%d - %#" PRIxVADDR ":%#lx\n",
    648 		s, bus, dev, fun, offset, va, r);
    649 }
    650 
    651 pcireg_t
    652 rmixl_pcix_conf_read(void *v, pcitag_t tag, int offset)
    653 {
    654 	rmixl_pcix_softc_t *sc = v;
    655 	bus_space_handle_t bsh = sc->sc_pci_cfg_memh;
    656 	bus_space_tag_t bst = sc->sc_pci_cfg_memt;
    657 	pcireg_t rv;
    658 
    659 	if (offset >= 0x100)
    660 		return -1;
    661 
    662 	if (__predict_true(!cold))
    663 		mutex_enter(&sc->sc_mutex);
    664 
    665 	uint64_t cfg0 = rmixl_cache_err_dis();
    666 	rv = bus_space_read_4(bst, bsh, (bus_size_t)tag + offset);
    667 	if (rmixl_cache_err_check() != 0) {
    668 #ifdef DIAGNOSTIC
    669 		int bus, dev, fun;
    670 
    671 		rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
    672 		printf("%s: %d/%d/%d, offset %#x: bad address\n",
    673 			__func__, bus, dev, fun, offset);
    674 #endif
    675 		rv = (pcireg_t) -1;
    676 	}
    677 	rmixl_cache_err_restore(cfg0);
    678 
    679 	if (__predict_true(!cold))
    680 		mutex_exit(&sc->sc_mutex);
    681 
    682 	return rv;
    683 }
    684 
    685 void
    686 rmixl_pcix_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    687 {
    688 	rmixl_pcix_softc_t *sc = v;
    689 	bus_space_handle_t bsh = sc->sc_pci_cfg_memh;
    690 	bus_space_tag_t bst = sc->sc_pci_cfg_memt;
    691 
    692 	if (offset >= 0x100)
    693 		return;
    694 
    695 	if (__predict_true(!cold))
    696 		mutex_enter(&sc->sc_mutex);
    697 
    698 	uint64_t cfg0 = rmixl_cache_err_dis();
    699 	bus_space_write_4(bst, bsh, (bus_size_t)offset, val);
    700 	if (rmixl_cache_err_check() != 0) {
    701 #ifdef DIAGNOSTIC
    702 		int bus, dev, fun;
    703 
    704 		rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
    705 		printf("%s: %d/%d/%d, offset %#x: bad address\n",
    706 			__func__, bus, dev, fun, offset);
    707 #endif
    708 	}
    709 	rmixl_cache_err_restore(cfg0);
    710 
    711 	if (__predict_true(!cold))
    712 		mutex_exit(&sc->sc_mutex);
    713 }
    714 
    715 #ifdef __PCI_DEV_FUNCORDER
    716 bool
    717 rmixl_pcix_dev_funcorder(void *v, int bus, int device, int nfunctions,
    718 	char *funcs)
    719 {
    720 	return false;
    721 }
    722 #endif
    723 
    724 int
    725 rmixl_pcix_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *pih)
    726 {
    727 	const u_int irq = 16;	/* PCIX index in IRT */
    728 
    729 #ifdef DEBUG
    730 	DPRINTF(("%s: ps_bus %d, pa_intrswiz %#x, pa_intrtag %#lx,"
    731 		" pa_intrpin %d,  pa_intrline %d, pa_rawintrpin %d\n",
    732 		__func__, pa->pa_bus, pa->pa_intrswiz, pa->pa_intrtag,
    733 		pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
    734 #endif
    735 
    736 	if (pa->pa_intrpin != PCI_INTERRUPT_PIN_NONE)
    737 		*pih = rmixl_pcix_make_pih(pa->pa_intrpin - 1, irq);
    738 	else
    739 		*pih = ~0;
    740 
    741 	return 0;
    742 }
    743 
    744 const char *
    745 rmixl_pcix_intr_string(void *v, pci_intr_handle_t pih)
    746 {
    747 	u_int bitno, irq;
    748 
    749 	rmixl_pcix_decompose_pih(pih, &bitno, &irq);
    750 
    751 	if (! cpu_rmixlr(mips_options.mips_cpu))
    752 		panic("%s: cpu %#x not supported\n",
    753 			__func__, mips_options.mips_cpu_id);
    754 
    755 	return rmixl_irt_string(irq);
    756 }
    757 
    758 const struct evcnt *
    759 rmixl_pcix_intr_evcnt(void *v, pci_intr_handle_t pih)
    760 {
    761 	return NULL;
    762 }
    763 
    764 static pci_intr_handle_t
    765 rmixl_pcix_make_pih(u_int bitno, u_int irq)
    766 {
    767 	pci_intr_handle_t pih;
    768 
    769 	KASSERT(bitno < 64);
    770 	KASSERT(irq < 32);
    771 
    772 	pih  = (irq << 6);
    773 	pih |= bitno;
    774 
    775 	return pih;
    776 }
    777 
    778 static void
    779 rmixl_pcix_decompose_pih(pci_intr_handle_t pih, u_int *bitno, u_int *irq)
    780 {
    781 	*bitno = (u_int)(pih & 0x3f);
    782 	*irq = (u_int)(pih >> 6);
    783 
    784 	KASSERT(*bitno < 64);
    785 	KASSERT(*irq < 31);
    786 }
    787 
    788 static void
    789 rmixl_pcix_intr_disestablish(void *v, void *ih)
    790 {
    791 	rmixl_pcix_softc_t *sc = v;
    792 	rmixl_pcix_dispatch_t *dip = ih;
    793 	rmixl_pcix_intr_t *pip = sc->sc_intr;
    794 	bool busy;
    795 
    796 	DPRINTF(("%s: pin=%d irq=%d\n",
    797 		__func__, dip->bitno + 1, dip->irq));
    798 	KASSERT(dip->bitno < RMIXL_PCIX_NINTR);
    799 
    800 	mutex_enter(&sc->sc_mutex);
    801 
    802 	dip->func = NULL;	/* prevent further dispatch */
    803 
    804 	/*
    805 	 * if no other dispatch handle is using this interrupt,
    806 	 * we can disable it
    807 	 */
    808 	busy = false;
    809 	for (int i=0; i < pip->dispatch_count; i++) {
    810 		rmixl_pcix_dispatch_t *d = &pip->dispatch_data[i];
    811 		if (d == dip)
    812 			continue;
    813 		if (d->bitno == dip->bitno) {
    814 			busy = true;
    815 			break;
    816 		}
    817 	}
    818 	if (! busy) {
    819 		uint32_t bit = 1 << (dip->bitno + 2);
    820 		uint32_t r;
    821 
    822 		r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
    823 		r |= bit;		/* set mask */
    824 		RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
    825 		DPRINTF(("%s: disabled pin %d\n", __func__, dip->bitno + 1));
    826 
    827 		pip->intenb &= ~(1 << dip->bitno);
    828 
    829 		if ((r & PCIX_INTR_CONTROL_MASK_ALL) == 0) {
    830 			/* tear down interrupt for this pcix */
    831 			rmixl_intr_disestablish(pip->ih);
    832 
    833 			/* commit NULL interrupt set */
    834 			sc->sc_intr = NULL;
    835 
    836 			/* schedule delayed free of the old interrupt set */
    837 			rmixl_pcix_pip_free_callout(pip);
    838 		}
    839 	}
    840 
    841 	mutex_exit(&sc->sc_mutex);
    842 }
    843 
    844 static void *
    845 rmixl_pcix_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
    846         int (*func)(void *), void *arg)
    847 {
    848 	rmixl_pcix_softc_t *sc = v;
    849 	u_int bitno, irq;
    850 	rmixl_pcix_intr_t *pip;
    851 	rmixl_pcix_dispatch_t *dip = NULL;
    852 
    853 	if (pih == ~0) {
    854 		DPRINTF(("%s: bad pih=%#lx, implies PCI_INTERRUPT_PIN_NONE\n",
    855 			__func__, pih));
    856 		return NULL;
    857 	}
    858 
    859 	rmixl_pcix_decompose_pih(pih, &bitno, &irq);
    860 	DPRINTF(("%s: pin=%d irq=%d\n", __func__, bitno + 1, irq));
    861 
    862 	KASSERT(bitno < RMIXL_PCIX_NINTR);
    863 
    864 	/*
    865 	 * all PCI-X device intrs get same ipl
    866 	 */
    867 	KASSERT(ipl == IPL_VM);
    868 
    869 	mutex_enter(&sc->sc_mutex);
    870 
    871 	pip = rmixl_pcix_pip_add_1(sc, irq, ipl);
    872 	if (pip == NULL)
    873 		return NULL;
    874 
    875 	/*
    876 	 * initializae our new interrupt, the last element in dispatch_data[]
    877 	 */
    878 	dip = &pip->dispatch_data[pip->dispatch_count - 1];
    879 	dip->bitno = bitno;
    880 	dip->irq = irq;
    881 	dip->func = func;
    882 	dip->arg = arg;
    883 	dip->counts = RMIXL_PCIX_EVCNT(sc, bitno, 0);
    884 #if NEVER
    885 	snprintf(dip->count_name, sizeof(dip->count_name),
    886 		"pin %d", bitno + 1);
    887 	evcnt_attach_dynamic(&dip->count, EVCNT_TYPE_INTR, NULL,
    888 		"rmixl_pcix", dip->count_name);
    889 #endif
    890 
    891 	/* commit the new interrupt set */
    892 	sc->sc_intr = pip;
    893 
    894 	/* enable this interrupt in the PCIX controller, if necessary */
    895 	if ((pip->intenb & (1 << bitno)) == 0) {
    896 		uint32_t bit = 1 << (bitno + 2);
    897 		uint32_t r;
    898 
    899 		r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
    900 		r &= ~bit;	/* clear mask */
    901 		RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
    902 
    903 		pip->sc = sc;
    904 		pip->ipl = ipl;
    905 		pip->intenb |= 1 << bitno;
    906 		DPRINTF(("%s: enabled pin %d\n", __func__, bitno + 1));
    907 	}
    908 
    909 	mutex_exit(&sc->sc_mutex);
    910 	return dip;
    911 }
    912 
    913 rmixl_pcix_intr_t *
    914 rmixl_pcix_pip_add_1(rmixl_pcix_softc_t *sc, int irq, int ipl)
    915 {
    916 	rmixl_pcix_intr_t *pip_old = sc->sc_intr;
    917 	rmixl_pcix_intr_t *pip_new;
    918 	u_int dispatch_count;
    919 	size_t size;
    920 
    921 	dispatch_count = 1;
    922 	size = sizeof(rmixl_pcix_intr_t);
    923 	if (pip_old != NULL) {
    924 		/*
    925 		 * count only those dispatch elements still in use
    926 		 * unused ones will be pruned during copy
    927 		 * i.e. we are "lazy" there is no rmixl_pcix_pip_sub_1
    928 		 */
    929 		for (int i=0; i < pip_old->dispatch_count; i++) {
    930 			if (pip_old->dispatch_data[i].func != NULL) {
    931 				dispatch_count++;
    932 				size += sizeof(rmixl_pcix_intr_t);
    933 			}
    934 		}
    935 	}
    936 
    937 	/*
    938 	 * allocate and initialize softc intr struct
    939 	 * with one or more dispatch handles
    940 	 */
    941 	pip_new = malloc(size, M_DEVBUF, M_NOWAIT|M_ZERO);
    942 	if (pip_new == NULL) {
    943 #ifdef DIAGNOSTIC
    944 		printf("%s: cannot malloc\n", __func__);
    945 #endif
    946 		return NULL;
    947 	}
    948 
    949 	if (pip_old == NULL) {
    950 		/* initialize the interrupt struct */
    951 		pip_new->sc = sc;
    952 		pip_new->ipl = ipl;
    953 		pip_new->ih = rmixl_intr_establish(irq,
    954 			ipl, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
    955 			rmixl_pcix_intr, pip_new, false);
    956 		if (pip_new->ih == NULL)
    957 			panic("%s: cannot establish irq %d", __func__, irq);
    958 	} else {
    959 		/*
    960 		 * all intrs on a softc get same ipl and sc
    961 		 * first intr established sets the standard
    962 		 */
    963 		KASSERT(sc == pip_old->sc);
    964 		if (sc != pip_old->sc) {
    965 			printf("%s: sc %p mismatch\n", __func__, sc);
    966 			free(pip_new, M_DEVBUF);
    967 			return NULL;
    968 		}
    969 		KASSERT (ipl == pip_old->ipl);
    970 		if (ipl != pip_old->ipl) {
    971 			printf("%s: ipl %d mismatch\n", __func__, ipl);
    972 			free(pip_new, M_DEVBUF);
    973 			return NULL;
    974 		}
    975 		/*
    976 		 * copy pip_old to pip_new, skipping unused dispatch elemets
    977 		 */
    978 		memcpy(pip_new, pip_old, sizeof(rmixl_pcix_intr_t));
    979 		for (int j=0, i=0; i < pip_old->dispatch_count; i++) {
    980 			if (pip_old->dispatch_data[i].func != NULL) {
    981 				memcpy(&pip_new->dispatch_data[j],
    982 					&pip_old->dispatch_data[i],
    983 					sizeof(rmixl_pcix_dispatch_t));
    984 				j++;
    985 			}
    986 		}
    987 
    988 		/*
    989 		 * schedule delayed free of old interrupt set
    990 		 */
    991 		rmixl_pcix_pip_free_callout(pip_old);
    992 	}
    993 	pip_new->dispatch_count = dispatch_count;
    994 
    995 	return pip_new;
    996 }
    997 
    998 /*
    999  * delay free of the old interrupt set
   1000  * to allow anyone still using it to do so safely
   1001  * XXX 2 seconds should be plenty?
   1002  */
   1003 static void
   1004 rmixl_pcix_pip_free_callout(rmixl_pcix_intr_t *pip)
   1005 {
   1006 	callout_init(&pip->callout, 0);
   1007 	callout_reset(&pip->callout, 2 * hz, rmixl_pcix_pip_free, pip);
   1008 }
   1009 
   1010 static void
   1011 rmixl_pcix_pip_free(void *arg)
   1012 {
   1013 	rmixl_pcix_intr_t *pip = arg;
   1014 
   1015 	callout_destroy(&pip->callout);
   1016 	free(pip, M_DEVBUF);
   1017 }
   1018 
   1019 static int
   1020 rmixl_pcix_intr(void *arg)
   1021 {
   1022 	rmixl_pcix_intr_t *pip = arg;
   1023 	int rv = 0;
   1024 
   1025 	uint32_t status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
   1026 	DPRINTF(("%s: %#x\n", __func__, status));
   1027 
   1028 	if (status != 0) {
   1029 		for (int i=0; i < pip->dispatch_count; i++) {
   1030 			rmixl_pcix_dispatch_t *dip = &pip->dispatch_data[i];
   1031 			uint32_t bit = 1 << dip->bitno;
   1032 			int (*func)(void *) = dip->func;
   1033 			if ((func != NULL) && (status & bit) != 0) {
   1034 				(void)(*func)(dip->arg);
   1035 				dip->counts[cpu_index(curcpu())].evcnt.ev_count++;
   1036 				rv = 1;
   1037 			}
   1038 		}
   1039 	}
   1040 	return rv;
   1041 }
   1042 
   1043 static int
   1044 rmixl_pcix_error_intr(void *arg)
   1045 {
   1046 	rmixl_pcix_softc_t *sc = arg;
   1047 	uint32_t error_status;
   1048 
   1049 	error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
   1050 
   1051 #ifdef DIAGNOSTIC
   1052 	printf("%s: error status %#x\n", __func__, error_status);
   1053 #endif
   1054 
   1055 #if DDB
   1056 	Debugger();
   1057 #endif
   1058 
   1059 	/* XXX reset and recover? */
   1060 
   1061 	panic("%s: error %#x\n", device_xname(sc->sc_dev), error_status);
   1062 }
   1063 
   1064 /*
   1065  * rmixl_physaddr_init_pcix:
   1066  *	called from rmixl_physaddr_init to get region addrs & sizes
   1067  *	from PCIX CFG, IO, MEM BARs
   1068  */
   1069 void
   1070 rmixl_physaddr_init_pcix(struct extent *ext)
   1071 {
   1072 	struct rmixl_config * const rcp = &rmixl_configuration;
   1073 	uint32_t r;
   1074 
   1075 	r = RMIXL_PCIXREG_READ(RMIXLR_SBC_PCIX_CFG_BAR);
   1076 	if ((r & RMIXL_PCIX_CFG_BAR_ENB) != 0) {
   1077 		rmixl_physaddr_add(ext, "pcicfg", &rcp->rc_pci_cfg,
   1078 		    (bus_addr_t)RMIXL_PCIX_CFG_BAR_TO_BA((uint64_t)r),
   1079 		    (bus_size_t)RMIXL_PCIX_CFG_SIZE);
   1080 	}
   1081 
   1082 	r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
   1083 	if ((r & RMIXL_PCIE_MEM_BAR_ENB) != 0) {
   1084 		rmixl_physaddr_add(ext, "pcimem", &rcp->rc_pci_mem,
   1085 		    (bus_addr_t)RMIXL_PCIX_MEM_BAR_TO_BA((uint64_t)r),
   1086 		    (bus_size_t)RMIXL_PCIX_MEM_BAR_TO_SIZE((uint64_t)r));
   1087 	}
   1088 
   1089 	r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
   1090 	if ((r & RMIXL_PCIE_IO_BAR_ENB) != 0) {
   1091 		rmixl_physaddr_add(ext, "pciio", &rcp->rc_pci_io,
   1092 		    (bus_addr_t)RMIXL_PCIX_IO_BAR_TO_BA((uint64_t)r),
   1093 		    (bus_size_t)RMIXL_PCIX_IO_BAR_TO_SIZE((uint64_t)r));
   1094 	}
   1095 }
   1096 
   1097 #ifdef DDB
   1098 int rmixl_pcix_intr_chk(void);
   1099 int
   1100 rmixl_pcix_intr_chk(void)
   1101 {
   1102 	uint32_t control, status, error_status;
   1103 
   1104 	control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
   1105 	status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
   1106 	error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
   1107 
   1108 	printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
   1109 
   1110 	control |= PCIX_INTR_CONTROL_DIA;
   1111 	RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, control);
   1112 
   1113 	control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
   1114 	status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
   1115 	error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
   1116 
   1117 	printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
   1118 
   1119 	return 0;
   1120 }
   1121 #endif
   1122