xref: /netbsd-src/sys/arch/mips/rmi/rmixl_pcix.c (revision e6a4e4eb043b127d350a480e09484638c4b2f764)
1*e6a4e4ebSskrll /*	$NetBSD: rmixl_pcix.c,v 1.19 2022/01/22 15:10:31 skrll Exp $	*/
23e67b512Smatt 
33e67b512Smatt /*
43e67b512Smatt  * Copyright (c) 2001 Wasabi Systems, Inc.
53e67b512Smatt  * All rights reserved.
63e67b512Smatt  *
73e67b512Smatt  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
83e67b512Smatt  *
93e67b512Smatt  * Redistribution and use in source and binary forms, with or without
103e67b512Smatt  * modification, are permitted provided that the following conditions
113e67b512Smatt  * are met:
123e67b512Smatt  * 1. Redistributions of source code must retain the above copyright
133e67b512Smatt  *    notice, this list of conditions and the following disclaimer.
143e67b512Smatt  * 2. Redistributions in binary form must reproduce the above copyright
153e67b512Smatt  *    notice, this list of conditions and the following disclaimer in the
163e67b512Smatt  *    documentation and/or other materials provided with the distribution.
173e67b512Smatt  * 3. All advertising materials mentioning features or use of this software
183e67b512Smatt  *    must display the following acknowledgement:
193e67b512Smatt  *	This product includes software developed for the NetBSD Project by
203e67b512Smatt  *	Wasabi Systems, Inc.
213e67b512Smatt  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
223e67b512Smatt  *    or promote products derived from this software without specific prior
233e67b512Smatt  *    written permission.
243e67b512Smatt  *
253e67b512Smatt  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
263e67b512Smatt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
273e67b512Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
283e67b512Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
293e67b512Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
303e67b512Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
313e67b512Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
323e67b512Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
333e67b512Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
343e67b512Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
353e67b512Smatt  * POSSIBILITY OF SUCH DAMAGE.
363e67b512Smatt  */
373e67b512Smatt 
383e67b512Smatt /*
393e67b512Smatt  * PCI configuration support for RMI XLR SoC
403e67b512Smatt  */
413e67b512Smatt 
423e67b512Smatt #include <sys/cdefs.h>
43*e6a4e4ebSskrll __KERNEL_RCSID(0, "$NetBSD: rmixl_pcix.c,v 1.19 2022/01/22 15:10:31 skrll Exp $");
443e67b512Smatt 
453e67b512Smatt #include "opt_pci.h"
463e67b512Smatt #include "pci.h"
473e67b512Smatt 
483e67b512Smatt #include <sys/cdefs.h>
493e67b512Smatt 
503e67b512Smatt #include <sys/param.h>
51b6185cbdSmatt #include <sys/bus.h>
52b6185cbdSmatt #include <sys/cpu.h>
533e67b512Smatt #include <sys/device.h>
543e67b512Smatt #include <sys/extent.h>
55b6185cbdSmatt #include <sys/intr.h>
563e67b512Smatt #include <sys/malloc.h>
573e67b512Smatt #include <sys/kernel.h>		/* for 'hz' */
58b6185cbdSmatt #include <sys/systm.h>
593e67b512Smatt 
603e67b512Smatt #include <uvm/uvm_extern.h>
613e67b512Smatt 
623e67b512Smatt #include <mips/rmi/rmixlreg.h>
633e67b512Smatt #include <mips/rmi/rmixlvar.h>
643e67b512Smatt #include <mips/rmi/rmixl_intr.h>
653e67b512Smatt #include <mips/rmi/rmixl_pcixvar.h>
663e67b512Smatt 
673e67b512Smatt #include <mips/rmi/rmixl_obiovar.h>
683e67b512Smatt 
693e67b512Smatt #include <dev/pci/pcivar.h>
703e67b512Smatt #include <dev/pci/pcidevs.h>
713e67b512Smatt #include <dev/pci/pciconf.h>
723e67b512Smatt 
733e67b512Smatt #ifdef	PCI_NETBSD_CONFIGURE
743e67b512Smatt #include <mips/cache.h>
753e67b512Smatt #endif
763e67b512Smatt 
773e67b512Smatt #ifdef PCI_DEBUG
783e67b512Smatt int rmixl_pcix_debug = PCI_DEBUG;
793e67b512Smatt # define DPRINTF(x)	do { if (rmixl_pcix_debug) printf x ; } while (0)
803e67b512Smatt #else
813e67b512Smatt # define DPRINTF(x)
823e67b512Smatt #endif
833e67b512Smatt 
843e67b512Smatt #ifndef DDB
853e67b512Smatt # define STATIC static
863e67b512Smatt #else
873e67b512Smatt # define STATIC
883e67b512Smatt #endif
893e67b512Smatt 
903e67b512Smatt 
913e67b512Smatt /*
923e67b512Smatt  * XLR PCI-X Extended Configuration Registers
933e67b512Smatt  * Note:
943e67b512Smatt  * - MSI-related regs are omitted
953e67b512Smatt  * - Device mode regs are omitted
963e67b512Smatt  */
973e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR0_ADDR	0x100	/* Host BAR0 Address */
983e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR1_ADDR	0x104	/* Host BAR1 Address */
993e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR2_ADDR	0x108	/* Host BAR2 Address */
1003e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR3_ADDR	0x10c	/* Host BAR3 Address */
1013e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR4_ADDR	0x110	/* Host BAR4 Address */
1023e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR5_ADDR	0x114	/* Host BAR5 Address */
1033e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR0_SIZE	0x118	/* Host BAR0 Size */
1043e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR1_SIZE	0x11c	/* Host BAR1 Size */
1053e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR2_SIZE	0x120	/* Host BAR2 Size */
1063e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR3_SIZE	0x124	/* Host BAR3 Size */
1073e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR4_SIZE	0x128	/* Host BAR4 Size */
1083e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_BAR5_SIZE	0x12c	/* Host BAR5 Size */
1093e67b512Smatt #define RMIXL_PCIX_ECFG_MATCH_BIT_ADDR	0x130	/* Match Bit Address BAR */
1103e67b512Smatt #define RMIXL_PCIX_ECFG_MATCH_BIT_SIZE	0x134	/* Match Bit Size BAR */
1113e67b512Smatt #define RMIXL_PCIX_ECFG_XLR_CONTROL	0x138	/* XLR Control reg */
1123e67b512Smatt #define RMIXL_PCIX_ECFG_INTR_CONTROL	0x13c	/* Interrupt Control reg */
1133e67b512Smatt #define RMIXL_PCIX_ECFG_INTR_STATUS	0x140	/* Interrupt Status reg */
1143e67b512Smatt #define RMIXL_PCIX_ECFG_INTR_ERR_STATUS	0x144	/* Interrupt Error Status reg */
1153e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_MODE_STS	0x178	/* Host Mode Status */
1163e67b512Smatt #define RMIXL_PCIX_ECFG_XLR_MBLE	0x17c	/* XLR Match Byte Lane Enable */
1173e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_XROM_ADDR	0x180	/* Host Expansion ROM Address */
1183e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_XROM_SIZE	0x184	/* Host Expansion ROM Size */
1193e67b512Smatt #define RMIXL_PCIX_ECFG_HOST_MODE_CTL	0x18c	/* Host Mode Control */
1203e67b512Smatt #define RMIXL_PCIX_ECFG_TXCAL_CTL	0x1a0	/* TX Calibration Preset Control */
1213e67b512Smatt #define RMIXL_PCIX_ECFG_TXCAL_COUNT	0x1a4	/* TX Calibration Preset Count */
1223e67b512Smatt 
1233e67b512Smatt /*
1243e67b512Smatt  * RMIXL_PCIX_ECFG_INTR_CONTROL bit defines
1253e67b512Smatt  */
1263e67b512Smatt #define PCIX_INTR_CONTROL_RESV		__BITS(31,8)
1273e67b512Smatt #define PCIX_INTR_CONTROL_MSI1_MASK	__BIT(7)
1283e67b512Smatt #define PCIX_INTR_CONTROL_MSI0_MASK	__BIT(6)
1293e67b512Smatt #define PCIX_INTR_CONTROL_INTD_MASK	__BIT(5)
1303e67b512Smatt #define PCIX_INTR_CONTROL_INTC_MASK	__BIT(4)
1313e67b512Smatt #define PCIX_INTR_CONTROL_INTB_MASK	__BIT(3)
1323e67b512Smatt #define PCIX_INTR_CONTROL_INTA_MASK	__BIT(2)
1333e67b512Smatt #define PCIX_INTR_CONTROL_TMSI		__BIT(1)	/* Trigger MSI Interrupt */
1343e67b512Smatt #define PCIX_INTR_CONTROL_DIA		__BIT(0)	/* Device Interrupt through INTA Pin */
1353e67b512Smatt #define PCIX_INTR_CONTROL_MASK_ALL	\
1363e67b512Smatt 		(PCIX_INTR_CONTROL_MSI1_MASK|PCIX_INTR_CONTROL_MSI0_MASK	\
1373e67b512Smatt 		|PCIX_INTR_CONTROL_INTD_MASK|PCIX_INTR_CONTROL_INTC_MASK	\
1383e67b512Smatt 		|PCIX_INTR_CONTROL_INTB_MASK|PCIX_INTR_CONTROL_INTA_MASK)
1393e67b512Smatt 
1403e67b512Smatt /*
1413e67b512Smatt  * RMIXL_PCIX_ECFG_INTR_STATUS bit defines
1423e67b512Smatt  */
1433e67b512Smatt #define PCIX_INTR_STATUS_RESV		__BITS(31,6)
1443e67b512Smatt #define PCIX_INTR_STATUS_MSI1		__BIT(5)
1453e67b512Smatt #define PCIX_INTR_STATUS_MSI0		__BIT(4)
1463e67b512Smatt #define PCIX_INTR_STATUS_INTD		__BIT(3)
1473e67b512Smatt #define PCIX_INTR_STATUS_INTC		__BIT(2)
1483e67b512Smatt #define PCIX_INTR_STATUS_INTB		__BIT(1)
1493e67b512Smatt #define PCIX_INTR_STATUS_INTA		__BIT(0)
1503e67b512Smatt 
1513e67b512Smatt /*
1523e67b512Smatt  * RMIXL_PCIX_ECFG_INTR_ERR_STATUS bit defines
1533e67b512Smatt  */
1543e67b512Smatt #define PCIX_INTR_ERR_STATUS_RESa	__BITS(31,5)
1553e67b512Smatt #define PCIX_INTR_ERR_STATUS_SERR	__BIT(4)	/* System Error */
1563e67b512Smatt #define PCIX_INTR_ERR_STATUS_RESb	__BIT(3)
1573e67b512Smatt #define PCIX_INTR_ERR_STATUS_TE		__BIT(2)	/* Target Error */
1583e67b512Smatt #define PCIX_INTR_ERR_STATUS_IE		__BIT(1)	/* Initiator Error */
1593e67b512Smatt #define PCIX_INTR_ERR_STATUS_RCE	__BIT(0)	/* Retry Count Expired */
1603e67b512Smatt #define PCIX_INTR_ERR_STATUS_RESV	\
1613e67b512Smatt 		(PCIX_INTR_ERR_STATUS_RESa|PCIX_INTR_ERR_STATUS_RESb)
1623e67b512Smatt 
1633e67b512Smatt /*
1643e67b512Smatt  * RMIXL_PCIX_ECFG_HOST_MODE_CTL bit defines
1653e67b512Smatt  */
1663e67b512Smatt #define PCIX_HOST_MODE_CTL_HDMSTAT	__BIT(1)	/* Host/Dev Mode status
1673e67b512Smatt 							 *  read-only
1683e67b512Smatt 							 *  1 = host
1693e67b512Smatt 							 *  0 = device
1703e67b512Smatt 							 */
1713e67b512Smatt #define PCIX_HOST_MODE_CTL_HOSTSWRST	__BIT(0)	/* Host soft reset
1723e67b512Smatt 							 *  set to 1 to reset
1733e67b512Smatt 							 *  set to 0 to un-reset
1743e67b512Smatt 							 */
1753e67b512Smatt 
1763e67b512Smatt 
1773e67b512Smatt #if BYTE_ORDER == BIG_ENDIAN
1783e67b512Smatt # define RMIXL_PCIXREG_BASE	RMIXL_IO_DEV_PCIX_EB
1793e67b512Smatt #else
1803e67b512Smatt # define RMIXL_PCIXREG_BASE	RMIXL_IO_DEV_PCIX_EL
1813e67b512Smatt #endif
1823e67b512Smatt 
1833e67b512Smatt #define RMIXL_PCIXREG_VADDR(o)				\
1843e67b512Smatt 	(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(	\
1853e67b512Smatt 		rmixl_configuration.rc_io_pbase		\
1863e67b512Smatt 		+ RMIXL_PCIXREG_BASE + (o))
1873e67b512Smatt 
1883e67b512Smatt #define RMIXL_PCIXREG_READ(o)     (*RMIXL_PCIXREG_VADDR(o))
1893e67b512Smatt #define RMIXL_PCIXREG_WRITE(o,v)  *RMIXL_PCIXREG_VADDR(o) = (v)
1903e67b512Smatt 
1913e67b512Smatt 
1923e67b512Smatt #define RMIXL_PCIX_CONCAT3(a,b,c) a ## b ## c
1933e67b512Smatt #define RMIXL_PCIX_BAR_INIT(reg, bar, size, align) {			\
1943e67b512Smatt 	struct extent *ext = rmixl_configuration.rc_phys_ex;		\
1953e67b512Smatt 	u_long region_start;						\
1963e67b512Smatt 	uint64_t ba;							\
1973e67b512Smatt 	int err;							\
1983e67b512Smatt 									\
1993e67b512Smatt 	err = extent_alloc(ext, (size), (align), 0UL, EX_NOWAIT,	\
2003e67b512Smatt 		&region_start);						\
2013e67b512Smatt 	if (err != 0)							\
2023e67b512Smatt 		panic("%s: extent_alloc(%p, %#lx, %#lx, %#lx, %#x, %p)",\
2033e67b512Smatt 			__func__, ext, size, align, 0UL, EX_NOWAIT,	\
2043e67b512Smatt 			&region_start);					\
2053e67b512Smatt 	ba = (uint64_t)region_start;					\
2063e67b512Smatt 	ba *= (1024 * 1024);						\
2073e67b512Smatt 	bar = RMIXL_PCIX_CONCAT3(RMIXL_PCIX_,reg,_BAR)(ba, 1);		\
2083e67b512Smatt 	DPRINTF(("PCIX %s BAR was not enabled by firmware\n"		\
2093e67b512Smatt 		"enabling %s at phys %#" PRIxBUSADDR ", size %lu MB\n",	\
2103e67b512Smatt 		__STRING(reg), __STRING(reg), ba, size));		\
2113e67b512Smatt 	RMIXL_IOREG_WRITE(RMIXL_IO_DEV_BRIDGE + 			\
2123e67b512Smatt 		RMIXL_PCIX_CONCAT3(RMIXLR_SBC_PCIX_,reg,_BAR), bar);	\
2133e67b512Smatt 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE +			\
2143e67b512Smatt 		RMIXL_PCIX_CONCAT3(RMIXLR_SBC_PCIX_,reg,_BAR));		\
2153e67b512Smatt 	DPRINTF(("%s: %s BAR %#x\n", __func__, __STRING(reg), bar));	\
2163e67b512Smatt }
2173e67b512Smatt 
2183e67b512Smatt 
2193e67b512Smatt #define RMIXL_PCIX_EVCNT(sc, intrpin, cpu)	\
2203e67b512Smatt 	&(sc)->sc_evcnts[(intrpin) * (ncpu) + (cpu)]
2213e67b512Smatt 
2223e67b512Smatt 
2233e67b512Smatt static int	rmixl_pcix_match(device_t, cfdata_t, void *);
2243e67b512Smatt static void	rmixl_pcix_attach(device_t, device_t, void *);
2253e67b512Smatt static void	rmixl_pcix_init(rmixl_pcix_softc_t *);
2263e67b512Smatt static void	rmixl_pcix_init_errors(rmixl_pcix_softc_t *);
227cbab9cadSchs static void	rmixl_pcix_attach_hook(device_t, device_t,
2283e67b512Smatt 		    struct pcibus_attach_args *);
2293e67b512Smatt static void	rmixl_pcix_intcfg(rmixl_pcix_softc_t *);
2303e67b512Smatt static void	rmixl_pcix_errata(rmixl_pcix_softc_t *);
2313e67b512Smatt static void	rmixl_conf_interrupt(void *, int, int, int, int, int *);
2323e67b512Smatt static int	rmixl_pcix_bus_maxdevs(void *, int);
2333e67b512Smatt static pcitag_t	rmixl_pcix_make_tag(void *, int, int, int);
2343e67b512Smatt static void	rmixl_pcix_decompose_tag(void *, pcitag_t, int *, int *, int *);
2353e67b512Smatt void		rmixl_pcix_tag_print(const char *restrict, void *, pcitag_t,				int, vaddr_t, u_long);
2363e67b512Smatt static int	rmixl_pcix_conf_setup(rmixl_pcix_softc_t *,
2373e67b512Smatt 			pcitag_t, int *, bus_space_tag_t *,
2383e67b512Smatt 			bus_space_handle_t *);
2393e67b512Smatt static pcireg_t	rmixl_pcix_conf_read(void *, pcitag_t, int);
2403e67b512Smatt static void	rmixl_pcix_conf_write(void *, pcitag_t, int, pcireg_t);
2413e67b512Smatt 
242d3e53912Sdyoung static int	rmixl_pcix_intr_map(const struct pci_attach_args *,
2433e67b512Smatt 		    pci_intr_handle_t *);
2443e67b512Smatt static const char *
2451cf0ee07Sozaki-r 		rmixl_pcix_intr_string(void *, pci_intr_handle_t,
2461cf0ee07Sozaki-r 		    char *, size_t);
2473e67b512Smatt static const struct evcnt *
2483e67b512Smatt 		rmixl_pcix_intr_evcnt(void *, pci_intr_handle_t);
2493e67b512Smatt static pci_intr_handle_t
2503e67b512Smatt 		rmixl_pcix_make_pih(u_int, u_int);
2513e67b512Smatt static void	rmixl_pcix_decompose_pih(pci_intr_handle_t, u_int *, u_int *);
2523e67b512Smatt static void	rmixl_pcix_intr_disestablish(void *, void *);
2533e67b512Smatt static void	*rmixl_pcix_intr_establish(void *, pci_intr_handle_t,
2543e67b512Smatt 		    int, int (*)(void *), void *);
2553e67b512Smatt static rmixl_pcix_intr_t *
2563e67b512Smatt                 rmixl_pcix_pip_add_1(rmixl_pcix_softc_t *, int, int);
2573e67b512Smatt static void     rmixl_pcix_pip_free_callout(rmixl_pcix_intr_t *);
2583e67b512Smatt static void     rmixl_pcix_pip_free(void *);
2593e67b512Smatt static int	rmixl_pcix_intr(void *);
2603e67b512Smatt static int	rmixl_pcix_error_intr(void *);
2613e67b512Smatt 
2623e67b512Smatt 
2633e67b512Smatt CFATTACH_DECL_NEW(rmixl_pcix, sizeof(rmixl_pcix_softc_t),
2643e67b512Smatt     rmixl_pcix_match, rmixl_pcix_attach, NULL, NULL);
2653e67b512Smatt 
2663e67b512Smatt 
2673e67b512Smatt static int rmixl_pcix_found;
2683e67b512Smatt 
2693e67b512Smatt 
2703e67b512Smatt static int
rmixl_pcix_match(device_t parent,cfdata_t cf,void * aux)2713e67b512Smatt rmixl_pcix_match(device_t parent, cfdata_t cf, void *aux)
2723e67b512Smatt {
2733e67b512Smatt 	uint32_t r;
2743e67b512Smatt 
2753e67b512Smatt 	/*
2763e67b512Smatt 	 * PCI-X interface exists on XLR chips only
2773e67b512Smatt 	 */
2783e67b512Smatt 	if (! cpu_rmixlr(mips_options.mips_cpu))
2793e67b512Smatt 		return 0;
2803e67b512Smatt 
2813e67b512Smatt 	/* XXX
2823e67b512Smatt 	 * for now there is only one PCI-X Interface on chip
2833e67b512Smatt 	 * and only one chip in the system
2843e67b512Smatt 	 * this could change with furture RMI XL family designs
2853e67b512Smatt 	 * or when we have multi-chip systems.
2863e67b512Smatt 	 */
2873e67b512Smatt 	if (rmixl_pcix_found)
2883e67b512Smatt 		return 0;
2893e67b512Smatt 
2903e67b512Smatt 	/* read Host Mode Control register */
2913e67b512Smatt 	r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_MODE_CTL);
2923e67b512Smatt 	r &= PCIX_HOST_MODE_CTL_HDMSTAT;
2933e67b512Smatt 	if (r == 0)
2943e67b512Smatt 		return 0;	/* strapped for Device Mode */
2953e67b512Smatt 
2963e67b512Smatt 	return 1;
2973e67b512Smatt }
2983e67b512Smatt 
2993e67b512Smatt static void
rmixl_pcix_attach(device_t parent,device_t self,void * aux)3003e67b512Smatt rmixl_pcix_attach(device_t parent, device_t self, void *aux)
3013e67b512Smatt {
3023e67b512Smatt 	rmixl_pcix_softc_t *sc = device_private(self);
3033e67b512Smatt 	struct obio_attach_args *obio = aux;
3043e67b512Smatt 	struct rmixl_config *rcp = &rmixl_configuration;
3053e67b512Smatt         struct pcibus_attach_args pba;
3063e67b512Smatt 	uint32_t bar;
3073e67b512Smatt 
3083e67b512Smatt 	rmixl_pcix_found = 1;
3093e67b512Smatt 	sc->sc_dev = self;
3103e67b512Smatt 	sc->sc_29bit_dmat = obio->obio_29bit_dmat;
3113e67b512Smatt 	sc->sc_32bit_dmat = obio->obio_32bit_dmat;
3123e67b512Smatt 	sc->sc_64bit_dmat = obio->obio_64bit_dmat;
3133e67b512Smatt 	sc->sc_tmsk = obio->obio_tmsk;
3143e67b512Smatt 
3153e67b512Smatt 	aprint_normal(": RMI XLR PCI-X Interface\n");
3163e67b512Smatt 
3171a422a9cScliff 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_HIGH);
3181a422a9cScliff 
3193e67b512Smatt 	rmixl_pcix_intcfg(sc);
3203e67b512Smatt 
3213e67b512Smatt 	rmixl_pcix_errata(sc);
3223e67b512Smatt 
3233e67b512Smatt 	/*
3243e67b512Smatt 	 * check XLR Control Register
3253e67b512Smatt 	 */
3263e67b512Smatt 	DPRINTF(("%s: XLR_CONTROL=%#x\n", __func__,
3273e67b512Smatt 		RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_CONTROL)));
3283e67b512Smatt 
3293e67b512Smatt 	/*
3303e67b512Smatt 	 * HBAR[0]   if a 32 bit BAR, or
3313e67b512Smatt 	 * HBAR[0,1] if a 64 bit BAR pair
3323e67b512Smatt 	 * must cover all RAM
3333e67b512Smatt 	 */
3343e67b512Smatt 	extern u_quad_t mem_cluster_maxaddr;
3353e67b512Smatt 	uint64_t hbar_addr;
3363e67b512Smatt 	uint64_t hbar_size;
3373e67b512Smatt 	uint32_t hbar_size_lo, hbar_size_hi;
3383e67b512Smatt 	uint32_t hbar_addr_lo, hbar_addr_hi;
3393e67b512Smatt 
3403e67b512Smatt 	hbar_addr_lo = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR0_ADDR);
3413e67b512Smatt 	hbar_addr_hi = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR1_ADDR);
3423e67b512Smatt 	hbar_size_lo = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR0_SIZE);
3433e67b512Smatt 	hbar_size_hi = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR1_SIZE);
3443e67b512Smatt 
3453e67b512Smatt 	hbar_addr = (u_quad_t)(hbar_addr_lo & PCI_MAPREG_MEM_ADDR_MASK);
3463e67b512Smatt 	hbar_size = hbar_size_lo;
3473e67b512Smatt 	if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0) {
3483e67b512Smatt 		hbar_addr |= (uint64_t)hbar_addr_hi << 32;
3493e67b512Smatt 		hbar_size |= (uint64_t)hbar_size_hi << 32;
3503e67b512Smatt 	}
3513e67b512Smatt 	if ((hbar_addr != 0) || (hbar_size < mem_cluster_maxaddr)) {
3523e67b512Smatt 		int error;
3533e67b512Smatt 
3543e67b512Smatt 		aprint_error_dev(self, "HostBAR0 addr %#x, size %#x\n",
3553e67b512Smatt 			hbar_addr_lo, hbar_size_lo);
3563e67b512Smatt 		if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0)
3573e67b512Smatt 			aprint_error_dev(self, "HostBAR1 addr %#x, size %#x\n",
3583e67b512Smatt 				hbar_addr_hi, hbar_size_hi);
3593e67b512Smatt 		aprint_error_dev(self, "WARNING: firmware PCI-X setup error: "
3603e67b512Smatt 			"RAM %#"PRIx64"..%#"PRIx64" not accessible by Host BAR, "
3613e67b512Smatt 			"enabling DMA bounce buffers\n",
3623e67b512Smatt 			hbar_size, mem_cluster_maxaddr-1);
3633e67b512Smatt 
3643e67b512Smatt 		/*
3653e67b512Smatt 		 * force use of bouce buffers for inaccessible RAM addrs
3663e67b512Smatt 		 */
3673e67b512Smatt 		if (hbar_size < ((uint64_t)1 << 32)) {
3683e67b512Smatt 			error = bus_dmatag_subregion(sc->sc_32bit_dmat,
369*e6a4e4ebSskrll 				0, (bus_addr_t)hbar_size - 1, &sc->sc_32bit_dmat,
3703e67b512Smatt 				BUS_DMA_NOWAIT);
3713e67b512Smatt 			if (error)
3723e67b512Smatt 				panic("%s: failed to subregion 32-bit dma tag:"
3733e67b512Smatt 					 " error %d", __func__, error);
3743e67b512Smatt 			sc->sc_64bit_dmat = NULL;
3753e67b512Smatt 		} else {
3763e67b512Smatt 			error = bus_dmatag_subregion(sc->sc_64bit_dmat,
377*e6a4e4ebSskrll 				0, (bus_addr_t)hbar_size - 1, &sc->sc_64bit_dmat,
3783e67b512Smatt 				BUS_DMA_NOWAIT);
3793e67b512Smatt 			if (error)
3803e67b512Smatt 				panic("%s: failed to subregion 64-bit dma tag:"
3813e67b512Smatt 					" error %d", __func__, error);
3823e67b512Smatt 		}
3833e67b512Smatt 	}
3843e67b512Smatt 
3853e67b512Smatt 	/*
3863e67b512Smatt 	 * check PCI-X interface byteswap setup
3873e67b512Smatt 	 * ensure 'Match Byte Lane' is disabled
3883e67b512Smatt 	 */
38956d29395Smrg 	uint32_t mble;
3903e67b512Smatt 	mble = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_MBLE);
39156d29395Smrg #ifdef PCI_DEBUG
39256d29395Smrg 	uint32_t mba, mbs;
3933e67b512Smatt 	mba  = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_ADDR);
3943e67b512Smatt 	mbs  = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_SIZE);
3953e67b512Smatt 	DPRINTF(("%s: MBLE=%#x, MBA=%#x, MBS=%#x\n", __func__, mble, mba, mbs));
39656d29395Smrg #endif
3973e67b512Smatt 	if ((mble & __BIT(40)) != 0)
3983e67b512Smatt 		RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_XLR_MBLE, 0);
3993e67b512Smatt 
4003e67b512Smatt 	/*
4013e67b512Smatt 	 * get PCI config space base addr from SBC PCIe CFG BAR
4023e67b512Smatt 	 * initialize it if necessary
4033e67b512Smatt  	 */
4043e67b512Smatt 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_CFG_BAR);
4053e67b512Smatt 	DPRINTF(("%s: PCIX_CFG_BAR %#x\n", __func__, bar));
4063e67b512Smatt 	if ((bar & RMIXL_PCIX_CFG_BAR_ENB) == 0) {
4073e67b512Smatt 		u_long n = RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
4083e67b512Smatt 		RMIXL_PCIX_BAR_INIT(CFG, bar, n, n);
4093e67b512Smatt 	}
4103e67b512Smatt 	rcp->rc_pci_cfg_pbase = (bus_addr_t)RMIXL_PCIX_CFG_BAR_TO_BA(bar);
4113e67b512Smatt 	rcp->rc_pci_cfg_size  = (bus_size_t)RMIXL_PCIX_CFG_SIZE;
4123e67b512Smatt 
4133e67b512Smatt 	/*
4143e67b512Smatt 	 * get PCI MEM space base [addr, size] from SBC PCIe MEM BAR
4153e67b512Smatt 	 * initialize it if necessary
4163e67b512Smatt  	 */
4173e67b512Smatt 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
4183e67b512Smatt 	DPRINTF(("%s: PCIX_MEM_BAR %#x\n", __func__, bar));
4193e67b512Smatt 	if ((bar & RMIXL_PCIX_MEM_BAR_ENB) == 0) {
4203e67b512Smatt 		u_long n = 256;				/* 256 MB */
4213e67b512Smatt 		RMIXL_PCIX_BAR_INIT(MEM, bar, n, n);
4223e67b512Smatt 	}
4233e67b512Smatt 	rcp->rc_pci_mem_pbase = (bus_addr_t)RMIXL_PCIX_MEM_BAR_TO_BA(bar);
4243e67b512Smatt 	rcp->rc_pci_mem_size  = (bus_size_t)RMIXL_PCIX_MEM_BAR_TO_SIZE(bar);
4253e67b512Smatt 
4263e67b512Smatt 	/*
4273e67b512Smatt 	 * get PCI IO space base [addr, size] from SBC PCIe IO BAR
4283e67b512Smatt 	 * initialize it if necessary
4293e67b512Smatt  	 */
4303e67b512Smatt 	bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
4313e67b512Smatt 	DPRINTF(("%s: PCIX_IO_BAR %#x\n", __func__, bar));
4323e67b512Smatt 	if ((bar & RMIXL_PCIX_IO_BAR_ENB) == 0) {
4333e67b512Smatt 		u_long n = 32;				/* 32 MB */
4343e67b512Smatt 		RMIXL_PCIX_BAR_INIT(IO, bar, n, n);
4353e67b512Smatt 	}
4363e67b512Smatt 	rcp->rc_pci_io_pbase = (bus_addr_t)RMIXL_PCIX_IO_BAR_TO_BA(bar);
4373e67b512Smatt 	rcp->rc_pci_io_size  = (bus_size_t)RMIXL_PCIX_IO_BAR_TO_SIZE(bar);
4383e67b512Smatt 
4393e67b512Smatt 	/*
4403e67b512Smatt 	 * initialize the PCI CFG bus space tag
4413e67b512Smatt 	 */
4423e67b512Smatt 	rmixl_pci_cfg_bus_mem_init(&rcp->rc_pci_cfg_memt, rcp);
4433e67b512Smatt 	sc->sc_pci_cfg_memt = &rcp->rc_pci_cfg_memt;
4443e67b512Smatt 
4453e67b512Smatt 	/*
4463e67b512Smatt 	 * initialize the PCI MEM and IO bus space tags
4473e67b512Smatt 	 */
4483e67b512Smatt 	rmixl_pci_bus_mem_init(&rcp->rc_pci_memt, rcp);
4493e67b512Smatt 	rmixl_pci_bus_io_init(&rcp->rc_pci_iot, rcp);
4503e67b512Smatt 
4513e67b512Smatt 	/*
4523e67b512Smatt 	 * initialize the extended configuration regs
4533e67b512Smatt 	 */
4543e67b512Smatt 	rmixl_pcix_init_errors(sc);
4553e67b512Smatt 
4563e67b512Smatt 	/*
4573e67b512Smatt 	 * initialize the PCI chipset tag
4583e67b512Smatt 	 */
4593e67b512Smatt 	rmixl_pcix_init(sc);
4603e67b512Smatt 
4613e67b512Smatt 	/*
4623e67b512Smatt 	 * attach the PCI bus
4633e67b512Smatt 	 */
4643e67b512Smatt 	memset(&pba, 0, sizeof(pba));
4653e67b512Smatt 	pba.pba_memt = &rcp->rc_pci_memt;
4663e67b512Smatt 	pba.pba_iot =  &rcp->rc_pci_iot;
4673e67b512Smatt 	pba.pba_dmat = sc->sc_32bit_dmat;
4683e67b512Smatt 	pba.pba_dmat64 = sc->sc_64bit_dmat;
4693e67b512Smatt 	pba.pba_pc = &sc->sc_pci_chipset;
4703e67b512Smatt 	pba.pba_bus = 0;
4713e67b512Smatt 	pba.pba_bridgetag = NULL;
4723e67b512Smatt 	pba.pba_intrswiz = 0;
4733e67b512Smatt 	pba.pba_intrtag = 0;
474a6b2b839Sdyoung 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
4753e67b512Smatt 		PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
4763e67b512Smatt 
477c7fb772bSthorpej 	config_found(self, &pba, pcibusprint, CFARGS_NONE);
4783e67b512Smatt }
4793e67b512Smatt 
4803e67b512Smatt /*
4813e67b512Smatt  * rmixl_pcix_intcfg - init PCI-X interrupt control
4823e67b512Smatt  */
4833e67b512Smatt static void
rmixl_pcix_intcfg(rmixl_pcix_softc_t * sc)4843e67b512Smatt rmixl_pcix_intcfg(rmixl_pcix_softc_t *sc)
4853e67b512Smatt {
4863e67b512Smatt 	size_t size;
4873e67b512Smatt 	rmixl_pcix_evcnt_t *ev;
4883e67b512Smatt 
4893e67b512Smatt 	DPRINTF(("%s\n", __func__));
4903e67b512Smatt 
4913e67b512Smatt 	/* mask all interrupts until they are established */
4923e67b512Smatt 	RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL,
4933e67b512Smatt 		PCIX_INTR_CONTROL_MASK_ALL);
4943e67b512Smatt 
4953e67b512Smatt 	/*
4963e67b512Smatt 	 * read-to-clear any pre-existing interrupts
4973e67b512Smatt 	 * XXX MSI bits in STATUS are also documented as write 1 to clear in PRM
4983e67b512Smatt 	 */
4993e67b512Smatt 	(void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
5003e67b512Smatt 	(void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
5013e67b512Smatt 
5023e67b512Smatt 	/* initialize the (non-error interrupt) dispatch handles */
5033e67b512Smatt 	sc->sc_intr = NULL;
5043e67b512Smatt 
5053e67b512Smatt 	/*
5063e67b512Smatt 	 * allocate per-cpu, per-pin interrupt event counters
5073e67b512Smatt 	 */
5083e67b512Smatt 	size = ncpu * PCI_INTERRUPT_PIN_MAX * sizeof(rmixl_pcix_evcnt_t);
509d47bcd29Schs 	ev = malloc(size, M_DEVBUF, M_WAITOK);
5103e67b512Smatt 	sc->sc_evcnts = ev;
5113e67b512Smatt 	for (int pin=PCI_INTERRUPT_PIN_A; pin <= PCI_INTERRUPT_PIN_MAX; pin++) {
5123e67b512Smatt 		for (int cpu=0; cpu < ncpu; cpu++) {
5133e67b512Smatt 			ev = RMIXL_PCIX_EVCNT(sc, pin - 1, cpu);
5143e67b512Smatt 			snprintf(ev->name, sizeof(ev->name),
5153e67b512Smatt 				"cpu%d, pin %d", cpu, pin);
5163e67b512Smatt 			evcnt_attach_dynamic(&ev->evcnt, EVCNT_TYPE_INTR,
5173e67b512Smatt 				NULL, "rmixl_pcix", ev->name);
5183e67b512Smatt 		}
5193e67b512Smatt 	}
5203e67b512Smatt 
5213e67b512Smatt 	/*
5223e67b512Smatt 	 * establish PCIX error interrupt handler
5233e67b512Smatt 	 */
5243e67b512Smatt 	sc->sc_fatal_ih = rmixl_intr_establish(24, sc->sc_tmsk,
5253e67b512Smatt 		IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
5263e67b512Smatt 		rmixl_pcix_error_intr, sc, false);
5273e67b512Smatt 	if (sc->sc_fatal_ih == NULL)
5283e67b512Smatt 		panic("%s: cannot establish irq %d", __func__, 24);
5293e67b512Smatt }
5303e67b512Smatt 
5313e67b512Smatt static void
rmixl_pcix_errata(rmixl_pcix_softc_t * sc)5323e67b512Smatt rmixl_pcix_errata(rmixl_pcix_softc_t *sc)
5333e67b512Smatt {
5343e67b512Smatt 	/* nothing */
5353e67b512Smatt }
5363e67b512Smatt 
5373e67b512Smatt static void
rmixl_pcix_init(rmixl_pcix_softc_t * sc)5383e67b512Smatt rmixl_pcix_init(rmixl_pcix_softc_t *sc)
5393e67b512Smatt {
5403e67b512Smatt 	pci_chipset_tag_t pc = &sc->sc_pci_chipset;
5413e67b512Smatt #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
542ca8ce3aeSthorpej 	struct pciconf_resources *pcires;
5433e67b512Smatt #endif
5443e67b512Smatt 
5453e67b512Smatt 	pc->pc_conf_v = (void *)sc;
5463e67b512Smatt 	pc->pc_attach_hook = rmixl_pcix_attach_hook;
5473e67b512Smatt 	pc->pc_bus_maxdevs = rmixl_pcix_bus_maxdevs;
5483e67b512Smatt 	pc->pc_make_tag = rmixl_pcix_make_tag;
5493e67b512Smatt 	pc->pc_decompose_tag = rmixl_pcix_decompose_tag;
5503e67b512Smatt 	pc->pc_conf_read = rmixl_pcix_conf_read;
5513e67b512Smatt 	pc->pc_conf_write = rmixl_pcix_conf_write;
5523e67b512Smatt 
5533e67b512Smatt 	pc->pc_intr_v = (void *)sc;
5543e67b512Smatt 	pc->pc_intr_map = rmixl_pcix_intr_map;
5553e67b512Smatt 	pc->pc_intr_string = rmixl_pcix_intr_string;
5563e67b512Smatt 	pc->pc_intr_evcnt = rmixl_pcix_intr_evcnt;
5573e67b512Smatt 	pc->pc_intr_establish = rmixl_pcix_intr_establish;
5583e67b512Smatt 	pc->pc_intr_disestablish = rmixl_pcix_intr_disestablish;
5593e67b512Smatt 	pc->pc_conf_interrupt = rmixl_conf_interrupt;
5603e67b512Smatt 
5613e67b512Smatt #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
5623e67b512Smatt 	/*
5633e67b512Smatt 	 * Configure the PCI bus.
5643e67b512Smatt 	 */
5653e67b512Smatt 	struct rmixl_config *rcp = &rmixl_configuration;
5663e67b512Smatt 
567ca8ce3aeSthorpej 	aprint_normal_dev(sc->sc_dev, "configuring PCI bus\n");
5683e67b512Smatt 
569ca8ce3aeSthorpej 	pcires = pciconf_resource_init();
5703e67b512Smatt 
571ca8ce3aeSthorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
572ca8ce3aeSthorpej 	    rcp->rc_pci_io_pbase, rcp->rc_pci_io_size);
573ca8ce3aeSthorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
574ca8ce3aeSthorpej 	    rcp->rc_pci_mem_pbase, rcp->rc_pci_mem_size);
5753e67b512Smatt 
576ca8ce3aeSthorpej 	pci_configure_bus(pc, pcires, 0,
5773e67b512Smatt 	    mips_cache_info.mci_dcache_align);
5783e67b512Smatt 
579ca8ce3aeSthorpej 	pciconf_resource_fini(pcires);
5803e67b512Smatt #endif
5813e67b512Smatt }
5823e67b512Smatt 
5833e67b512Smatt static void
rmixl_pcix_init_errors(rmixl_pcix_softc_t * sc)5843e67b512Smatt rmixl_pcix_init_errors(rmixl_pcix_softc_t *sc)
5853e67b512Smatt {
5863e67b512Smatt 	/* nothing */
5873e67b512Smatt }
5883e67b512Smatt 
5893e67b512Smatt void
rmixl_conf_interrupt(void * v,int bus,int dev,int ipin,int swiz,int * iline)5903e67b512Smatt rmixl_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
5913e67b512Smatt {
5923e67b512Smatt 	DPRINTF(("%s: %p, %d, %d, %d, %d, %p\n",
5933e67b512Smatt 		__func__, v, bus, dev, ipin, swiz, iline));
5943e67b512Smatt }
5953e67b512Smatt 
5963e67b512Smatt void
rmixl_pcix_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)597cbab9cadSchs rmixl_pcix_attach_hook(device_t parent, device_t self,
5983e67b512Smatt 	struct pcibus_attach_args *pba)
5993e67b512Smatt {
6003e67b512Smatt 	DPRINTF(("%s: pba_bus %d, pba_bridgetag %p, pc_conf_v %p\n",
6013e67b512Smatt 		__func__, pba->pba_bus, pba->pba_bridgetag,
6023e67b512Smatt 		pba->pba_pc->pc_conf_v));
6033e67b512Smatt }
6043e67b512Smatt 
6053e67b512Smatt int
rmixl_pcix_bus_maxdevs(void * v,int busno)6063e67b512Smatt rmixl_pcix_bus_maxdevs(void *v, int busno)
6073e67b512Smatt {
6083e67b512Smatt 	return (32);	/* XXX depends on the family of XLS SoC */
6093e67b512Smatt }
6103e67b512Smatt 
6113e67b512Smatt /*
6123e67b512Smatt  * XLS pci tag is a 40 bit address composed thusly:
6133e67b512Smatt  *	39:25   (reserved)
6143e67b512Smatt  *	24      Swap (0=little, 1=big endian)
6153e67b512Smatt  *	23:16   Bus number
6163e67b512Smatt  *	15:11   Device number
6173e67b512Smatt  *	10:8    Function number
6183e67b512Smatt  *	7:0     Register number
6193e67b512Smatt  *
6203e67b512Smatt  * Note: this is the "native" composition for addressing CFG space, but not for ECFG space.
6213e67b512Smatt  */
6223e67b512Smatt pcitag_t
rmixl_pcix_make_tag(void * v,int bus,int dev,int fun)6233e67b512Smatt rmixl_pcix_make_tag(void *v, int bus, int dev, int fun)
6243e67b512Smatt {
6253e67b512Smatt 	return ((bus << 16) | (dev << 11) | (fun << 8));
6263e67b512Smatt }
6273e67b512Smatt 
6283e67b512Smatt void
rmixl_pcix_decompose_tag(void * v,pcitag_t tag,int * bp,int * dp,int * fp)6293e67b512Smatt rmixl_pcix_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
6303e67b512Smatt {
6313e67b512Smatt 	if (bp != NULL)
6323e67b512Smatt 		*bp = (tag >> 16) & 0xff;
6333e67b512Smatt 	if (dp != NULL)
6343e67b512Smatt 		*dp = (tag >> 11) & 0x1f;
6353e67b512Smatt 	if (fp != NULL)
6363e67b512Smatt 		*fp = (tag >> 8) & 0x7;
6373e67b512Smatt }
6383e67b512Smatt 
6393e67b512Smatt void
rmixl_pcix_tag_print(const char * restrict s,void * v,pcitag_t tag,int offset,vaddr_t va,u_long r)6403e67b512Smatt rmixl_pcix_tag_print(const char *restrict s, void *v, pcitag_t tag, int offset,
6413e67b512Smatt 	vaddr_t va, u_long r)
6423e67b512Smatt {
6433e67b512Smatt 	int bus, dev, fun;
6443e67b512Smatt 
6453e67b512Smatt 	rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
6463e67b512Smatt 	printf("%s: %d/%d/%d/%d - %#" PRIxVADDR ":%#lx\n",
6473e67b512Smatt 		s, bus, dev, fun, offset, va, r);
6483e67b512Smatt }
6493e67b512Smatt 
6503e67b512Smatt static int
rmixl_pcix_conf_setup(rmixl_pcix_softc_t * sc,pcitag_t tag,int * offp,bus_space_tag_t * bstp,bus_space_handle_t * bshp)6513e67b512Smatt rmixl_pcix_conf_setup(rmixl_pcix_softc_t *sc,
6523e67b512Smatt 	pcitag_t tag, int *offp, bus_space_tag_t *bstp,
6533e67b512Smatt 	bus_space_handle_t *bshp)
6543e67b512Smatt {
6553e67b512Smatt 	struct rmixl_config *rcp = &rmixl_configuration;
6563e67b512Smatt 	bus_space_tag_t bst;
6573e67b512Smatt 	bus_space_handle_t bsh;
6583e67b512Smatt 	bus_size_t size;
6593e67b512Smatt 	pcitag_t mask;
6603e67b512Smatt 	bus_addr_t ba;
6613e67b512Smatt 	int err;
6623e67b512Smatt 	static bus_space_handle_t cfg_bsh;
6633e67b512Smatt 	static bus_addr_t cfg_oba = -1;
6643e67b512Smatt 
6653e67b512Smatt 	/*
6663e67b512Smatt 	 * bus space depends on offset
6673e67b512Smatt 	 */
6683e67b512Smatt 	if ((*offp >= 0) && (*offp < 0x100)) {
6693e67b512Smatt 		mask = __BITS(15,0);
6703e67b512Smatt 		bst = sc->sc_pci_cfg_memt;
6713e67b512Smatt 		ba = rcp->rc_pci_cfg_pbase;
6723e67b512Smatt 		ba += (tag & ~mask);
6733e67b512Smatt 		*offp += (tag & mask);
6743e67b512Smatt 		if (ba != cfg_oba) {
6753e67b512Smatt 			size = (bus_size_t)(mask + 1);
6763e67b512Smatt 			if (cfg_oba != -1)
6773e67b512Smatt 				bus_space_unmap(bst, cfg_bsh, size);
6783e67b512Smatt 			err = bus_space_map(bst, ba, size, 0, &cfg_bsh);
6793e67b512Smatt 			if (err != 0) {
6803e67b512Smatt #ifdef DEBUG
6813e67b512Smatt 				panic("%s: bus_space_map err %d, CFG space",
6823e67b512Smatt 					__func__, err);	/* XXX */
6833e67b512Smatt #endif
6843e67b512Smatt 				return -1;
6853e67b512Smatt 			}
6863e67b512Smatt 			cfg_oba = ba;
6873e67b512Smatt 		}
6883e67b512Smatt 		bsh = cfg_bsh;
6893e67b512Smatt 	} else  {
6903e67b512Smatt 		return -1;
6913e67b512Smatt 	}
6923e67b512Smatt 
6933e67b512Smatt 	*bstp = bst;
6943e67b512Smatt 	*bshp = bsh;
6953e67b512Smatt 
6963e67b512Smatt 	return 0;
6973e67b512Smatt }
6983e67b512Smatt 
6993e67b512Smatt pcireg_t
rmixl_pcix_conf_read(void * v,pcitag_t tag,int offset)7003e67b512Smatt rmixl_pcix_conf_read(void *v, pcitag_t tag, int offset)
7013e67b512Smatt {
7023e67b512Smatt 	rmixl_pcix_softc_t *sc = v;
7033e67b512Smatt 	static bus_space_handle_t bsh;
7043e67b512Smatt 	bus_space_tag_t bst;
7053e67b512Smatt 	pcireg_t rv;
7063e67b512Smatt 	uint64_t cfg0;
7073e67b512Smatt 
7083e67b512Smatt 	mutex_enter(&sc->sc_mutex);
7093e67b512Smatt 
7103e67b512Smatt 	if (rmixl_pcix_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
7113e67b512Smatt 		cfg0 = rmixl_cache_err_dis();
7123e67b512Smatt 		rv = bus_space_read_4(bst, bsh, (bus_size_t)offset);
7133e67b512Smatt 		if (rmixl_cache_err_check() != 0) {
7143e67b512Smatt #ifdef DIAGNOSTIC
7153e67b512Smatt 			int bus, dev, fun;
7163e67b512Smatt 
7173e67b512Smatt 			rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
7183e67b512Smatt 			printf("%s: %d/%d/%d, offset %#x: bad address\n",
7193e67b512Smatt 				__func__, bus, dev, fun, offset);
7203e67b512Smatt #endif
7213e67b512Smatt 			rv = (pcireg_t) -1;
7223e67b512Smatt 		}
7233e67b512Smatt 		rmixl_cache_err_restore(cfg0);
7243e67b512Smatt 	} else {
7253e67b512Smatt 		rv = -1;
7263e67b512Smatt 	}
7273e67b512Smatt 
7283e67b512Smatt 	mutex_exit(&sc->sc_mutex);
7293e67b512Smatt 
7303e67b512Smatt 	return rv;
7313e67b512Smatt }
7323e67b512Smatt 
7333e67b512Smatt void
rmixl_pcix_conf_write(void * v,pcitag_t tag,int offset,pcireg_t val)7343e67b512Smatt rmixl_pcix_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
7353e67b512Smatt {
7363e67b512Smatt 	rmixl_pcix_softc_t *sc = v;
7373e67b512Smatt 	static bus_space_handle_t bsh;
7383e67b512Smatt 	bus_space_tag_t bst;
7393e67b512Smatt 	uint64_t cfg0;
7403e67b512Smatt 
7413e67b512Smatt 	mutex_enter(&sc->sc_mutex);
7423e67b512Smatt 
7433e67b512Smatt 	if (rmixl_pcix_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
7443e67b512Smatt 		cfg0 = rmixl_cache_err_dis();
7453e67b512Smatt 		bus_space_write_4(bst, bsh, (bus_size_t)offset, val);
7463e67b512Smatt 		if (rmixl_cache_err_check() != 0) {
7473e67b512Smatt #ifdef DIAGNOSTIC
7483e67b512Smatt 			int bus, dev, fun;
7493e67b512Smatt 
7503e67b512Smatt 			rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
7513e67b512Smatt 			printf("%s: %d/%d/%d, offset %#x: bad address\n",
7523e67b512Smatt 				__func__, bus, dev, fun, offset);
7533e67b512Smatt #endif
7543e67b512Smatt 		}
7553e67b512Smatt 		rmixl_cache_err_restore(cfg0);
7563e67b512Smatt 	}
7573e67b512Smatt 
7583e67b512Smatt 	mutex_exit(&sc->sc_mutex);
7593e67b512Smatt }
7603e67b512Smatt 
7613e67b512Smatt int
rmixl_pcix_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * pih)762d3e53912Sdyoung rmixl_pcix_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *pih)
7633e67b512Smatt {
7643e67b512Smatt 	const u_int irq = 16;	/* PCIX index in IRT */
7653e67b512Smatt 
7663e67b512Smatt #ifdef DEBUG
7673e67b512Smatt 	DPRINTF(("%s: ps_bus %d, pa_intrswiz %#x, pa_intrtag %#lx,"
7683e67b512Smatt 		" pa_intrpin %d,  pa_intrline %d, pa_rawintrpin %d\n",
7693e67b512Smatt 		__func__, pa->pa_bus, pa->pa_intrswiz, pa->pa_intrtag,
7703e67b512Smatt 		pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
7713e67b512Smatt #endif
7723e67b512Smatt 
7733e67b512Smatt 	if (pa->pa_intrpin != PCI_INTERRUPT_PIN_NONE)
7743e67b512Smatt 		*pih = rmixl_pcix_make_pih(pa->pa_intrpin - 1, irq);
7753e67b512Smatt 	else
7763e67b512Smatt 		*pih = ~0;
7773e67b512Smatt 
7783e67b512Smatt 	return 0;
7793e67b512Smatt }
7803e67b512Smatt 
7813e67b512Smatt const char *
rmixl_pcix_intr_string(void * v,pci_intr_handle_t pih,char * buf,size_t len)7821cf0ee07Sozaki-r rmixl_pcix_intr_string(void *v, pci_intr_handle_t pih, char *buf, size_t len)
7833e67b512Smatt {
7843e67b512Smatt 	u_int bitno, irq;
7853e67b512Smatt 
7863e67b512Smatt 	rmixl_pcix_decompose_pih(pih, &bitno, &irq);
7873e67b512Smatt 
7883e67b512Smatt 	if (! cpu_rmixlr(mips_options.mips_cpu))
7893e67b512Smatt 		panic("%s: cpu %#x not supported\n",
7903e67b512Smatt 			__func__, mips_options.mips_cpu_id);
7913e67b512Smatt 
7921cf0ee07Sozaki-r 	strlcpy(buf, rmixl_intr_string(RMIXL_IRT_VECTOR(irq)), len);
7931cf0ee07Sozaki-r 	return buf;
7943e67b512Smatt }
7953e67b512Smatt 
7963e67b512Smatt const struct evcnt *
rmixl_pcix_intr_evcnt(void * v,pci_intr_handle_t pih)7973e67b512Smatt rmixl_pcix_intr_evcnt(void *v, pci_intr_handle_t pih)
7983e67b512Smatt {
7993e67b512Smatt 	return NULL;
8003e67b512Smatt }
8013e67b512Smatt 
8023e67b512Smatt static pci_intr_handle_t
rmixl_pcix_make_pih(u_int bitno,u_int irq)8033e67b512Smatt rmixl_pcix_make_pih(u_int bitno, u_int irq)
8043e67b512Smatt {
8053e67b512Smatt 	pci_intr_handle_t pih;
8063e67b512Smatt 
8073e67b512Smatt 	KASSERT(bitno < 64);
8083e67b512Smatt 	KASSERT(irq < 32);
8093e67b512Smatt 
8103e67b512Smatt 	pih  = (irq << 6);
8113e67b512Smatt 	pih |= bitno;
8123e67b512Smatt 
8133e67b512Smatt 	return pih;
8143e67b512Smatt }
8153e67b512Smatt 
8163e67b512Smatt static void
rmixl_pcix_decompose_pih(pci_intr_handle_t pih,u_int * bitno,u_int * irq)8173e67b512Smatt rmixl_pcix_decompose_pih(pci_intr_handle_t pih, u_int *bitno, u_int *irq)
8183e67b512Smatt {
8193e67b512Smatt 	*bitno = (u_int)(pih & 0x3f);
8203e67b512Smatt 	*irq = (u_int)(pih >> 6);
8213e67b512Smatt 
8223e67b512Smatt 	KASSERT(*bitno < 64);
8233e67b512Smatt 	KASSERT(*irq < 31);
8243e67b512Smatt }
8253e67b512Smatt 
8263e67b512Smatt static void
rmixl_pcix_intr_disestablish(void * v,void * ih)8273e67b512Smatt rmixl_pcix_intr_disestablish(void *v, void *ih)
8283e67b512Smatt {
8293e67b512Smatt 	rmixl_pcix_softc_t *sc = v;
8303e67b512Smatt 	rmixl_pcix_dispatch_t *dip = ih;
8313e67b512Smatt 	rmixl_pcix_intr_t *pip = sc->sc_intr;
8323e67b512Smatt 	bool busy;
8333e67b512Smatt 
8343e67b512Smatt 	DPRINTF(("%s: pin=%d irq=%d\n",
8353e67b512Smatt 		__func__, dip->bitno + 1, dip->irq));
8363e67b512Smatt 	KASSERT(dip->bitno < RMIXL_PCIX_NINTR);
8373e67b512Smatt 
8383e67b512Smatt 	mutex_enter(&sc->sc_mutex);
8393e67b512Smatt 
8403e67b512Smatt 	dip->func = NULL;	/* prevent further dispatch */
8413e67b512Smatt 
8423e67b512Smatt 	/*
8433e67b512Smatt 	 * if no other dispatch handle is using this interrupt,
8443e67b512Smatt 	 * we can disable it
8453e67b512Smatt 	 */
8463e67b512Smatt 	busy = false;
8473e67b512Smatt 	for (int i=0; i < pip->dispatch_count; i++) {
8483e67b512Smatt 		rmixl_pcix_dispatch_t *d = &pip->dispatch_data[i];
8493e67b512Smatt 		if (d == dip)
8503e67b512Smatt 			continue;
8513e67b512Smatt 		if (d->bitno == dip->bitno) {
8523e67b512Smatt 			busy = true;
8533e67b512Smatt 			break;
8543e67b512Smatt 		}
8553e67b512Smatt 	}
8563e67b512Smatt 	if (! busy) {
8573e67b512Smatt 		uint32_t bit = 1 << (dip->bitno + 2);
8583e67b512Smatt 		uint32_t r;
8593e67b512Smatt 
8603e67b512Smatt 		r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
8613e67b512Smatt 		r |= bit;		/* set mask */
8623e67b512Smatt 		RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
8633e67b512Smatt 		DPRINTF(("%s: disabled pin %d\n", __func__, dip->bitno + 1));
8643e67b512Smatt 
8653e67b512Smatt 		pip->intenb &= ~(1 << dip->bitno);
8663e67b512Smatt 
8673e67b512Smatt 		if ((r & PCIX_INTR_CONTROL_MASK_ALL) == 0) {
8683e67b512Smatt 			/* tear down interrupt for this pcix */
8693e67b512Smatt 			rmixl_intr_disestablish(pip->ih);
8703e67b512Smatt 
8713e67b512Smatt 			/* commit NULL interrupt set */
8723e67b512Smatt 			sc->sc_intr = NULL;
8733e67b512Smatt 
8743e67b512Smatt 			/* schedule delayed free of the old interrupt set */
8753e67b512Smatt 			rmixl_pcix_pip_free_callout(pip);
8763e67b512Smatt 		}
8773e67b512Smatt 	}
8783e67b512Smatt 
8793e67b512Smatt 	mutex_exit(&sc->sc_mutex);
8803e67b512Smatt }
8813e67b512Smatt 
8823e67b512Smatt static void *
rmixl_pcix_intr_establish(void * v,pci_intr_handle_t pih,int ipl,int (* func)(void *),void * arg)8833e67b512Smatt rmixl_pcix_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
8843e67b512Smatt         int (*func)(void *), void *arg)
8853e67b512Smatt {
8863e67b512Smatt 	rmixl_pcix_softc_t *sc = v;
8873e67b512Smatt 	u_int bitno, irq;
8883e67b512Smatt 	rmixl_pcix_intr_t *pip;
8893e67b512Smatt 	rmixl_pcix_dispatch_t *dip = NULL;
8903e67b512Smatt 
8913e67b512Smatt 	if (pih == ~0) {
8923e67b512Smatt 		DPRINTF(("%s: bad pih=%#lx, implies PCI_INTERRUPT_PIN_NONE\n",
8933e67b512Smatt 			__func__, pih));
8943e67b512Smatt 		return NULL;
8953e67b512Smatt 	}
8963e67b512Smatt 
8973e67b512Smatt 	rmixl_pcix_decompose_pih(pih, &bitno, &irq);
8983e67b512Smatt 	DPRINTF(("%s: pin=%d irq=%d\n", __func__, bitno + 1, irq));
8993e67b512Smatt 
9003e67b512Smatt 	KASSERT(bitno < RMIXL_PCIX_NINTR);
9013e67b512Smatt 
9023e67b512Smatt 	/*
9033e67b512Smatt 	 * all PCI-X device intrs get same ipl
9043e67b512Smatt 	 */
9053e67b512Smatt 	KASSERT(ipl == IPL_VM);
9063e67b512Smatt 
9073e67b512Smatt 	mutex_enter(&sc->sc_mutex);
9083e67b512Smatt 
9093e67b512Smatt 	pip = rmixl_pcix_pip_add_1(sc, irq, ipl);
9103e67b512Smatt 	if (pip == NULL)
9113e67b512Smatt 		return NULL;
9123e67b512Smatt 
9133e67b512Smatt 	/*
9143e67b512Smatt 	 * initializae our new interrupt, the last element in dispatch_data[]
9153e67b512Smatt 	 */
9163e67b512Smatt 	dip = &pip->dispatch_data[pip->dispatch_count - 1];
9173e67b512Smatt 	dip->bitno = bitno;
9183e67b512Smatt 	dip->irq = irq;
9193e67b512Smatt 	dip->func = func;
9203e67b512Smatt 	dip->arg = arg;
921371b6bc2Scliff 	dip->counts = RMIXL_PCIX_EVCNT(sc, bitno, 0);
9223e67b512Smatt #if NEVER
9233e67b512Smatt 	snprintf(dip->count_name, sizeof(dip->count_name),
9243e67b512Smatt 		"pin %d", bitno + 1);
9253e67b512Smatt 	evcnt_attach_dynamic(&dip->count, EVCNT_TYPE_INTR, NULL,
9263e67b512Smatt 		"rmixl_pcix", dip->count_name);
9273e67b512Smatt #endif
9283e67b512Smatt 
9293e67b512Smatt 	/* commit the new interrupt set */
9303e67b512Smatt 	sc->sc_intr = pip;
9313e67b512Smatt 
9323e67b512Smatt 	/* enable this interrupt in the PCIX controller, if necessary */
9333e67b512Smatt 	if ((pip->intenb & (1 << bitno)) == 0) {
9343e67b512Smatt 		uint32_t bit = 1 << (bitno + 2);
9353e67b512Smatt 		uint32_t r;
9363e67b512Smatt 
9373e67b512Smatt 		r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
9383e67b512Smatt 		r &= ~bit;	/* clear mask */
9393e67b512Smatt 		RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
9403e67b512Smatt 
9413e67b512Smatt 		pip->sc = sc;
9423e67b512Smatt 		pip->ipl = ipl;
9433e67b512Smatt 		pip->intenb |= 1 << bitno;
9443e67b512Smatt 		DPRINTF(("%s: enabled pin %d\n", __func__, bitno + 1));
9453e67b512Smatt 	}
9463e67b512Smatt 
9473e67b512Smatt 	mutex_exit(&sc->sc_mutex);
9483e67b512Smatt 	return dip;
9493e67b512Smatt }
9503e67b512Smatt 
9513e67b512Smatt rmixl_pcix_intr_t *
rmixl_pcix_pip_add_1(rmixl_pcix_softc_t * sc,int irq,int ipl)9523e67b512Smatt rmixl_pcix_pip_add_1(rmixl_pcix_softc_t *sc, int irq, int ipl)
9533e67b512Smatt {
9543e67b512Smatt 	rmixl_pcix_intr_t *pip_old = sc->sc_intr;
9553e67b512Smatt 	rmixl_pcix_intr_t *pip_new;
9563e67b512Smatt 	u_int dispatch_count;
9573e67b512Smatt 	size_t size;
9583e67b512Smatt 
9593e67b512Smatt 	dispatch_count = 1;
9603e67b512Smatt 	size = sizeof(rmixl_pcix_intr_t);
9613e67b512Smatt 	if (pip_old != NULL) {
9623e67b512Smatt 		/*
9633e67b512Smatt 		 * count only those dispatch elements still in use
9643e67b512Smatt 		 * unused ones will be pruned during copy
9653e67b512Smatt 		 * i.e. we are "lazy" there is no rmixl_pcix_pip_sub_1
9663e67b512Smatt 		 */
9673e67b512Smatt 		for (int i=0; i < pip_old->dispatch_count; i++) {
9683e67b512Smatt 			if (pip_old->dispatch_data[i].func != NULL) {
9693e67b512Smatt 				dispatch_count++;
9703e67b512Smatt 				size += sizeof(rmixl_pcix_intr_t);
9713e67b512Smatt 			}
9723e67b512Smatt 		}
9733e67b512Smatt 	}
9743e67b512Smatt 
9753e67b512Smatt 	/*
9763e67b512Smatt 	 * allocate and initialize softc intr struct
9773e67b512Smatt 	 * with one or more dispatch handles
9783e67b512Smatt 	 */
979d47bcd29Schs 	pip_new = malloc(size, M_DEVBUF, M_WAITOK|M_ZERO);
9803e67b512Smatt 	if (pip_old == NULL) {
9813e67b512Smatt 		/* initialize the interrupt struct */
9823e67b512Smatt 		pip_new->sc = sc;
9833e67b512Smatt 		pip_new->ipl = ipl;
9843e67b512Smatt 		pip_new->ih = rmixl_intr_establish(irq, sc->sc_tmsk,
9853e67b512Smatt 			ipl, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
9863e67b512Smatt 			rmixl_pcix_intr, pip_new, false);
9873e67b512Smatt 		if (pip_new->ih == NULL)
9883e67b512Smatt 			panic("%s: cannot establish irq %d", __func__, irq);
9893e67b512Smatt 	} else {
9903e67b512Smatt 		/*
9913e67b512Smatt 		 * all intrs on a softc get same ipl and sc
9923e67b512Smatt 		 * first intr established sets the standard
9933e67b512Smatt 		 */
9943e67b512Smatt 		KASSERT(sc == pip_old->sc);
9953e67b512Smatt 		if (sc != pip_old->sc) {
9963e67b512Smatt 			printf("%s: sc %p mismatch\n", __func__, sc);
9973e67b512Smatt 			free(pip_new, M_DEVBUF);
9983e67b512Smatt 			return NULL;
9993e67b512Smatt 		}
10003e67b512Smatt 		KASSERT (ipl == pip_old->ipl);
10013e67b512Smatt 		if (ipl != pip_old->ipl) {
10023e67b512Smatt 			printf("%s: ipl %d mismatch\n", __func__, ipl);
10033e67b512Smatt 			free(pip_new, M_DEVBUF);
10043e67b512Smatt 			return NULL;
10053e67b512Smatt 		}
10063e67b512Smatt 		/*
10073e67b512Smatt 		 * copy pip_old to pip_new, skipping unused dispatch elemets
10083e67b512Smatt 		 */
10093e67b512Smatt 		memcpy(pip_new, pip_old, sizeof(rmixl_pcix_intr_t));
10103e67b512Smatt 		for (int j=0, i=0; i < pip_old->dispatch_count; i++) {
10113e67b512Smatt 			if (pip_old->dispatch_data[i].func != NULL) {
10123e67b512Smatt 				memcpy(&pip_new->dispatch_data[j],
10133e67b512Smatt 					&pip_old->dispatch_data[i],
10143e67b512Smatt 					sizeof(rmixl_pcix_dispatch_t));
10153e67b512Smatt 				j++;
10163e67b512Smatt 			}
10173e67b512Smatt 		}
10183e67b512Smatt 
10193e67b512Smatt 		/*
10203e67b512Smatt 		 * schedule delayed free of old interrupt set
10213e67b512Smatt 		 */
10223e67b512Smatt 		rmixl_pcix_pip_free_callout(pip_old);
10233e67b512Smatt 	}
10243e67b512Smatt 	pip_new->dispatch_count = dispatch_count;
10253e67b512Smatt 
10263e67b512Smatt 	return pip_new;
10273e67b512Smatt }
10283e67b512Smatt 
10293e67b512Smatt /*
10303e67b512Smatt  * delay free of the old interrupt set
10313e67b512Smatt  * to allow anyone still using it to do so safely
10323e67b512Smatt  * XXX 2 seconds should be plenty?
10333e67b512Smatt  */
10343e67b512Smatt static void
rmixl_pcix_pip_free_callout(rmixl_pcix_intr_t * pip)10353e67b512Smatt rmixl_pcix_pip_free_callout(rmixl_pcix_intr_t *pip)
10363e67b512Smatt {
10373e67b512Smatt 	callout_init(&pip->callout, 0);
10383e67b512Smatt 	callout_reset(&pip->callout, 2 * hz, rmixl_pcix_pip_free, pip);
10393e67b512Smatt }
10403e67b512Smatt 
10413e67b512Smatt static void
rmixl_pcix_pip_free(void * arg)10423e67b512Smatt rmixl_pcix_pip_free(void *arg)
10433e67b512Smatt {
10443e67b512Smatt 	rmixl_pcix_intr_t *pip = arg;
10453e67b512Smatt 
10463e67b512Smatt 	callout_destroy(&pip->callout);
10473e67b512Smatt 	free(pip, M_DEVBUF);
10483e67b512Smatt }
10493e67b512Smatt 
10503e67b512Smatt static int
rmixl_pcix_intr(void * arg)10513e67b512Smatt rmixl_pcix_intr(void *arg)
10523e67b512Smatt {
10533e67b512Smatt 	rmixl_pcix_intr_t *pip = arg;
10543e67b512Smatt 	int rv = 0;
10553e67b512Smatt 
10563e67b512Smatt 	uint32_t status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
10573e67b512Smatt 	DPRINTF(("%s: %#x\n", __func__, status));
10583e67b512Smatt 
10593e67b512Smatt 	if (status != 0) {
10603e67b512Smatt 		for (int i=0; i < pip->dispatch_count; i++) {
10613e67b512Smatt 			rmixl_pcix_dispatch_t *dip = &pip->dispatch_data[i];
10623e67b512Smatt 			uint32_t bit = 1 << dip->bitno;
10633e67b512Smatt 			int (*func)(void *) = dip->func;
10643e67b512Smatt 			if ((func != NULL) && (status & bit) != 0) {
10653e67b512Smatt 				(void)(*func)(dip->arg);
10663e67b512Smatt 				dip->counts[cpu_index(curcpu())].evcnt.ev_count++;
10673e67b512Smatt 				rv = 1;
10683e67b512Smatt 			}
10693e67b512Smatt 		}
10703e67b512Smatt 	}
10713e67b512Smatt 	return rv;
10723e67b512Smatt }
10733e67b512Smatt 
10743e67b512Smatt static int
rmixl_pcix_error_intr(void * arg)10753e67b512Smatt rmixl_pcix_error_intr(void *arg)
10763e67b512Smatt {
10773e67b512Smatt 	rmixl_pcix_softc_t *sc = arg;
10783e67b512Smatt 	uint32_t error_status;
10793e67b512Smatt 
10803e67b512Smatt 	error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
10813e67b512Smatt 
10823e67b512Smatt #ifdef DIAGNOSTIC
10833e67b512Smatt 	printf("%s: error status %#x\n", __func__, error_status);
10843e67b512Smatt #endif
10853e67b512Smatt 
10863e67b512Smatt #if DDB
10873e67b512Smatt 	Debugger();
10883e67b512Smatt #endif
10893e67b512Smatt 
10903e67b512Smatt 	/* XXX reset and recover? */
10913e67b512Smatt 
10923e67b512Smatt 	panic("%s: error %#x\n", device_xname(sc->sc_dev), error_status);
10933e67b512Smatt }
10943e67b512Smatt 
10953e67b512Smatt /*
10963e67b512Smatt  * rmixl_physaddr_init_pcix:
10973e67b512Smatt  *	called from rmixl_physaddr_init to get region addrs & sizes
10983e67b512Smatt  *	from PCIX CFG, ECFG, IO, MEM BARs
10993e67b512Smatt  */
11003e67b512Smatt void
rmixl_physaddr_init_pcix(struct extent * ext)11013e67b512Smatt rmixl_physaddr_init_pcix(struct extent *ext)
11023e67b512Smatt {
11033e67b512Smatt 	u_long base;
11043e67b512Smatt 	u_long size;
11053e67b512Smatt 	uint32_t r;
11063e67b512Smatt 
11073e67b512Smatt 	r = RMIXL_PCIXREG_READ(RMIXLR_SBC_PCIX_CFG_BAR);
11083e67b512Smatt 	if ((r & RMIXL_PCIX_CFG_BAR_ENB) != 0) {
11093e67b512Smatt 		base = (u_long)(RMIXL_PCIX_CFG_BAR_TO_BA((uint64_t)r)
11103e67b512Smatt 			/ (1024 * 1024));
11113e67b512Smatt 		size = (u_long)RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
11123e67b512Smatt 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
11133e67b512Smatt 			__LINE__, "CFG", r, base * 1024 * 1024, size));
11143e67b512Smatt 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
11153e67b512Smatt 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
11163e67b512Smatt 				"failed", __func__, ext, base, size, EX_NOWAIT);
11173e67b512Smatt 	}
11183e67b512Smatt 
11193e67b512Smatt 	r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
11203e67b512Smatt 	if ((r & RMIXL_PCIX_MEM_BAR_ENB) != 0) {
11213e67b512Smatt 		base = (u_long)(RMIXL_PCIX_MEM_BAR_TO_BA((uint64_t)r)
11223e67b512Smatt 			/ (1024 * 1024));
11233e67b512Smatt 		size = (u_long)(RMIXL_PCIX_MEM_BAR_TO_SIZE((uint64_t)r)
11243e67b512Smatt 			/ (1024 * 1024));
11253e67b512Smatt 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
11263e67b512Smatt 			__LINE__, "MEM", r, base * 1024 * 1024, size));
11273e67b512Smatt 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
11283e67b512Smatt 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
11293e67b512Smatt 				"failed", __func__, ext, base, size, EX_NOWAIT);
11303e67b512Smatt 	}
11313e67b512Smatt 
11323e67b512Smatt 	r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
11333e67b512Smatt 	if ((r & RMIXL_PCIX_IO_BAR_ENB) != 0) {
11343e67b512Smatt 		base = (u_long)(RMIXL_PCIX_IO_BAR_TO_BA((uint64_t)r)
11353e67b512Smatt 			/ (1024 * 1024));
11363e67b512Smatt 		size = (u_long)(RMIXL_PCIX_IO_BAR_TO_SIZE((uint64_t)r)
11373e67b512Smatt 			/ (1024 * 1024));
11383e67b512Smatt 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
11393e67b512Smatt 			__LINE__, "IO", r, base * 1024 * 1024, size));
11403e67b512Smatt 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
11413e67b512Smatt 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
11423e67b512Smatt 				"failed", __func__, ext, base, size, EX_NOWAIT);
11433e67b512Smatt 	}
11443e67b512Smatt }
11453e67b512Smatt 
11463e67b512Smatt #ifdef DDB
11473e67b512Smatt int rmixl_pcix_intr_chk(void);
11483e67b512Smatt int
rmixl_pcix_intr_chk(void)11493e67b512Smatt rmixl_pcix_intr_chk(void)
11503e67b512Smatt {
11513e67b512Smatt 	uint32_t control, status, error_status;
11523e67b512Smatt 
11533e67b512Smatt 	control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
11543e67b512Smatt 	status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
11553e67b512Smatt 	error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
11563e67b512Smatt 
11573e67b512Smatt 	printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
11583e67b512Smatt 
11593e67b512Smatt 	control |= PCIX_INTR_CONTROL_DIA;
11603e67b512Smatt 	RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, control);
11613e67b512Smatt 
11623e67b512Smatt 	control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
11633e67b512Smatt 	status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
11643e67b512Smatt 	error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
11653e67b512Smatt 
11663e67b512Smatt 	printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
11673e67b512Smatt 
11683e67b512Smatt 	return 0;
11693e67b512Smatt }
11703e67b512Smatt #endif
1171