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