Home | History | Annotate | Line # | Download | only in fdt
pcihost_fdt.c revision 1.8
      1  1.8  jakllsch /* $NetBSD: pcihost_fdt.c,v 1.8 2019/02/28 00:47:10 jakllsch Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2018 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.8  jakllsch __KERNEL_RCSID(0, "$NetBSD: pcihost_fdt.c,v 1.8 2019/02/28 00:47:10 jakllsch Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/extent.h>
     39  1.1  jmcneill #include <sys/queue.h>
     40  1.1  jmcneill #include <sys/mutex.h>
     41  1.1  jmcneill #include <sys/kmem.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <machine/cpu.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <arm/cpufunc.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/pci/pcireg.h>
     48  1.1  jmcneill #include <dev/pci/pcivar.h>
     49  1.1  jmcneill #include <dev/pci/pciconf.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     52  1.1  jmcneill 
     53  1.3  jmcneill #include <arm/pci/pci_msi_machdep.h>
     54  1.8  jakllsch #include <arm/fdt/pcihost_fdtvar.h>
     55  1.3  jmcneill 
     56  1.2  jmcneill #define	IH_INDEX_MASK			0x0000ffff
     57  1.1  jmcneill #define	IH_MPSAFE			0x80000000
     58  1.1  jmcneill 
     59  1.1  jmcneill #define	PCIHOST_DEFAULT_BUS_MIN		0
     60  1.1  jmcneill #define	PCIHOST_DEFAULT_BUS_MAX		255
     61  1.1  jmcneill 
     62  1.1  jmcneill #define	PCIHOST_CACHELINE_SIZE		arm_dcache_align
     63  1.1  jmcneill 
     64  1.8  jakllsch int pcihost_segment = 0;
     65  1.1  jmcneill 
     66  1.1  jmcneill static int	pcihost_match(device_t, cfdata_t, void *);
     67  1.1  jmcneill static void	pcihost_attach(device_t, device_t, void *);
     68  1.1  jmcneill 
     69  1.1  jmcneill static int	pcihost_config(struct pcihost_softc *);
     70  1.1  jmcneill 
     71  1.1  jmcneill static void	pcihost_attach_hook(device_t, device_t,
     72  1.1  jmcneill 				       struct pcibus_attach_args *);
     73  1.1  jmcneill static int	pcihost_bus_maxdevs(void *, int);
     74  1.1  jmcneill static pcitag_t	pcihost_make_tag(void *, int, int, int);
     75  1.1  jmcneill static void	pcihost_decompose_tag(void *, pcitag_t, int *, int *, int *);
     76  1.3  jmcneill static u_int	pcihost_get_segment(void *);
     77  1.1  jmcneill static pcireg_t	pcihost_conf_read(void *, pcitag_t, int);
     78  1.1  jmcneill static void	pcihost_conf_write(void *, pcitag_t, int, pcireg_t);
     79  1.1  jmcneill static int	pcihost_conf_hook(void *, int, int, int, pcireg_t);
     80  1.1  jmcneill static void	pcihost_conf_interrupt(void *, int, int, int, int, int *);
     81  1.1  jmcneill 
     82  1.1  jmcneill static int	pcihost_intr_map(const struct pci_attach_args *,
     83  1.1  jmcneill 				    pci_intr_handle_t *);
     84  1.1  jmcneill static const char *pcihost_intr_string(void *, pci_intr_handle_t,
     85  1.1  jmcneill 					  char *, size_t);
     86  1.5  jakllsch static const struct evcnt *pcihost_intr_evcnt(void *, pci_intr_handle_t);
     87  1.1  jmcneill static int	pcihost_intr_setattr(void *, pci_intr_handle_t *, int,
     88  1.1  jmcneill 					uint64_t);
     89  1.1  jmcneill static void *	pcihost_intr_establish(void *, pci_intr_handle_t,
     90  1.4  jmcneill 					 int, int (*)(void *), void *,
     91  1.4  jmcneill 					 const char *);
     92  1.1  jmcneill static void	pcihost_intr_disestablish(void *, void *);
     93  1.1  jmcneill 
     94  1.7  jakllsch static int	pcihost_bus_space_map(void *, bus_addr_t, bus_size_t,
     95  1.7  jakllsch 		int, bus_space_handle_t *);
     96  1.7  jakllsch 
     97  1.1  jmcneill CFATTACH_DECL_NEW(pcihost_fdt, sizeof(struct pcihost_softc),
     98  1.1  jmcneill 	pcihost_match, pcihost_attach, NULL, NULL);
     99  1.1  jmcneill 
    100  1.1  jmcneill static const struct of_compat_data compat_data[] = {
    101  1.1  jmcneill 	{ "pci-host-cam-generic",	PCIHOST_CAM },
    102  1.1  jmcneill 	{ "pci-host-ecam-generic",	PCIHOST_ECAM },
    103  1.1  jmcneill 	{ NULL,				0 }
    104  1.1  jmcneill };
    105  1.1  jmcneill 
    106  1.1  jmcneill static int
    107  1.1  jmcneill pcihost_match(device_t parent, cfdata_t cf, void *aux)
    108  1.1  jmcneill {
    109  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    110  1.1  jmcneill 
    111  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    112  1.1  jmcneill }
    113  1.1  jmcneill 
    114  1.1  jmcneill static void
    115  1.1  jmcneill pcihost_attach(device_t parent, device_t self, void *aux)
    116  1.1  jmcneill {
    117  1.1  jmcneill 	struct pcihost_softc * const sc = device_private(self);
    118  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    119  1.1  jmcneill 	bus_addr_t cs_addr;
    120  1.1  jmcneill 	bus_size_t cs_size;
    121  1.8  jakllsch 	int error;
    122  1.1  jmcneill 
    123  1.1  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &cs_addr, &cs_size) != 0) {
    124  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    125  1.1  jmcneill 		return;
    126  1.1  jmcneill 	}
    127  1.1  jmcneill 
    128  1.1  jmcneill 	sc->sc_dev = self;
    129  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    130  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    131  1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    132  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, cs_addr, cs_size, 0, &sc->sc_bsh);
    133  1.1  jmcneill 	if (error) {
    134  1.1  jmcneill 		aprint_error(": couldn't map registers: %d\n", error);
    135  1.1  jmcneill 		return;
    136  1.1  jmcneill 	}
    137  1.1  jmcneill 	sc->sc_type = of_search_compatible(sc->sc_phandle, compat_data)->data;
    138  1.1  jmcneill 
    139  1.1  jmcneill 	aprint_naive("\n");
    140  1.1  jmcneill 	aprint_normal(": Generic PCI host controller\n");
    141  1.1  jmcneill 
    142  1.8  jakllsch 	pcihost_init(&sc->sc_pc, sc);
    143  1.8  jakllsch 	pcihost_init2(sc);
    144  1.8  jakllsch }
    145  1.8  jakllsch 
    146  1.8  jakllsch void
    147  1.8  jakllsch pcihost_init2(struct pcihost_softc *sc)
    148  1.8  jakllsch {
    149  1.8  jakllsch 	struct pcibus_attach_args pba;
    150  1.8  jakllsch 	const u_int *data;
    151  1.8  jakllsch 	int len;
    152  1.8  jakllsch 
    153  1.1  jmcneill 	if ((data = fdtbus_get_prop(sc->sc_phandle, "bus-range", &len)) != NULL) {
    154  1.1  jmcneill 		if (len != 8) {
    155  1.8  jakllsch 			aprint_error_dev(sc->sc_dev, "malformed 'bus-range' property\n");
    156  1.1  jmcneill 			return;
    157  1.1  jmcneill 		}
    158  1.1  jmcneill 		sc->sc_bus_min = be32toh(data[0]);
    159  1.1  jmcneill 		sc->sc_bus_max = be32toh(data[1]);
    160  1.1  jmcneill 	} else {
    161  1.1  jmcneill 		sc->sc_bus_min = PCIHOST_DEFAULT_BUS_MIN;
    162  1.1  jmcneill 		sc->sc_bus_max = PCIHOST_DEFAULT_BUS_MAX;
    163  1.1  jmcneill 	}
    164  1.1  jmcneill 
    165  1.3  jmcneill 	/*
    166  1.3  jmcneill 	 * Assign a fixed PCI segment ("domain") number. If the property is not
    167  1.3  jmcneill 	 * present, assign one. The binding spec says if this property is used to
    168  1.3  jmcneill 	 * assign static segment numbers, all host bridges should have segments
    169  1.3  jmcneill 	 * astatic assigned to prevent overlaps.
    170  1.3  jmcneill 	 */
    171  1.3  jmcneill 	if (of_getprop_uint32(sc->sc_phandle, "linux,pci-domain", &sc->sc_seg))
    172  1.3  jmcneill 		sc->sc_seg = pcihost_segment++;
    173  1.3  jmcneill 
    174  1.1  jmcneill 	if (pcihost_config(sc) != 0)
    175  1.1  jmcneill 		return;
    176  1.1  jmcneill 
    177  1.1  jmcneill 	memset(&pba, 0, sizeof(pba));
    178  1.1  jmcneill 	pba.pba_flags = PCI_FLAGS_MRL_OKAY |
    179  1.1  jmcneill 			PCI_FLAGS_MRM_OKAY |
    180  1.1  jmcneill 			PCI_FLAGS_MWI_OKAY |
    181  1.7  jakllsch 			PCI_FLAGS_IO_OKAY |
    182  1.6  jmcneill 			PCI_FLAGS_MEM_OKAY;
    183  1.3  jmcneill #ifdef __HAVE_PCI_MSI_MSIX
    184  1.3  jmcneill 	if (sc->sc_type == PCIHOST_ECAM) {
    185  1.3  jmcneill 		pba.pba_flags |= PCI_FLAGS_MSI_OKAY |
    186  1.3  jmcneill 				 PCI_FLAGS_MSIX_OKAY;
    187  1.3  jmcneill 	}
    188  1.3  jmcneill #endif
    189  1.7  jakllsch 	pba.pba_iot = &sc->sc_io.bst;
    190  1.7  jakllsch 	pba.pba_memt = &sc->sc_mem.bst;
    191  1.1  jmcneill 	pba.pba_dmat = sc->sc_dmat;
    192  1.1  jmcneill #ifdef _PCI_HAVE_DMA64
    193  1.1  jmcneill 	pba.pba_dmat64 = sc->sc_dmat;
    194  1.1  jmcneill #endif
    195  1.1  jmcneill 	pba.pba_pc = &sc->sc_pc;
    196  1.3  jmcneill 	pba.pba_bus = sc->sc_bus_min;
    197  1.1  jmcneill 
    198  1.8  jakllsch 	config_found_ia(sc->sc_dev, "pcibus", &pba, pcibusprint);
    199  1.1  jmcneill }
    200  1.1  jmcneill 
    201  1.8  jakllsch void
    202  1.1  jmcneill pcihost_init(pci_chipset_tag_t pc, void *priv)
    203  1.1  jmcneill {
    204  1.1  jmcneill 	pc->pc_conf_v = priv;
    205  1.1  jmcneill 	pc->pc_attach_hook = pcihost_attach_hook;
    206  1.1  jmcneill 	pc->pc_bus_maxdevs = pcihost_bus_maxdevs;
    207  1.1  jmcneill 	pc->pc_make_tag = pcihost_make_tag;
    208  1.1  jmcneill 	pc->pc_decompose_tag = pcihost_decompose_tag;
    209  1.3  jmcneill 	pc->pc_get_segment = pcihost_get_segment;
    210  1.1  jmcneill 	pc->pc_conf_read = pcihost_conf_read;
    211  1.1  jmcneill 	pc->pc_conf_write = pcihost_conf_write;
    212  1.1  jmcneill 	pc->pc_conf_hook = pcihost_conf_hook;
    213  1.1  jmcneill 	pc->pc_conf_interrupt = pcihost_conf_interrupt;
    214  1.1  jmcneill 
    215  1.1  jmcneill 	pc->pc_intr_v = priv;
    216  1.1  jmcneill 	pc->pc_intr_map = pcihost_intr_map;
    217  1.1  jmcneill 	pc->pc_intr_string = pcihost_intr_string;
    218  1.1  jmcneill 	pc->pc_intr_evcnt = pcihost_intr_evcnt;
    219  1.1  jmcneill 	pc->pc_intr_setattr = pcihost_intr_setattr;
    220  1.1  jmcneill 	pc->pc_intr_establish = pcihost_intr_establish;
    221  1.1  jmcneill 	pc->pc_intr_disestablish = pcihost_intr_disestablish;
    222  1.1  jmcneill }
    223  1.1  jmcneill 
    224  1.1  jmcneill static int
    225  1.1  jmcneill pcihost_config(struct pcihost_softc *sc)
    226  1.1  jmcneill {
    227  1.1  jmcneill 	struct extent *ioext = NULL, *memext = NULL, *pmemext = NULL;
    228  1.1  jmcneill 	const u_int *ranges;
    229  1.3  jmcneill 	u_int probe_only;
    230  1.1  jmcneill 	int error, len;
    231  1.1  jmcneill 
    232  1.7  jakllsch 	struct pcih_bus_space * const pibs = &sc->sc_io;
    233  1.7  jakllsch 	pibs->bst = *sc->sc_bst;
    234  1.7  jakllsch 	pibs->bst.bs_cookie = pibs;
    235  1.7  jakllsch 	pibs->map = pibs->bst.bs_map;
    236  1.7  jakllsch 	pibs->bst.bs_map = pcihost_bus_space_map;
    237  1.7  jakllsch 
    238  1.7  jakllsch 	struct pcih_bus_space * const pmbs = &sc->sc_mem;
    239  1.7  jakllsch 	pmbs->bst = *sc->sc_bst;
    240  1.7  jakllsch 	pmbs->bst.bs_cookie = pmbs;
    241  1.7  jakllsch 	pmbs->map = pmbs->bst.bs_map;
    242  1.7  jakllsch 	pmbs->bst.bs_map = pcihost_bus_space_map;
    243  1.7  jakllsch 
    244  1.3  jmcneill 	/*
    245  1.3  jmcneill 	 * If this flag is set, skip configuration of the PCI bus and use existing config.
    246  1.3  jmcneill 	 */
    247  1.3  jmcneill 	if (of_getprop_uint32(sc->sc_phandle, "linux,pci-probe-only", &probe_only))
    248  1.3  jmcneill 		probe_only = 0;
    249  1.3  jmcneill 	if (probe_only)
    250  1.3  jmcneill 		return 0;
    251  1.3  jmcneill 
    252  1.1  jmcneill 	ranges = fdtbus_get_prop(sc->sc_phandle, "ranges", &len);
    253  1.1  jmcneill 	if (ranges == NULL) {
    254  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "missing 'ranges' property\n");
    255  1.1  jmcneill 		return EINVAL;
    256  1.1  jmcneill 	}
    257  1.1  jmcneill 
    258  1.1  jmcneill 	/*
    259  1.1  jmcneill 	 * Each entry in the ranges table contains:
    260  1.1  jmcneill 	 *  - bus address (3 cells)
    261  1.1  jmcneill 	 *  - cpu physical address (2 cells)
    262  1.1  jmcneill 	 *  - size (2 cells)
    263  1.1  jmcneill 	 * Total size for each entry is 28 bytes (7 cells).
    264  1.1  jmcneill 	 */
    265  1.1  jmcneill 	while (len >= 28) {
    266  1.1  jmcneill 		const uint32_t phys_hi = be32dec(&ranges[0]);
    267  1.7  jakllsch 		const uint64_t bus_phys = be64dec(&ranges[1]);
    268  1.1  jmcneill 		const uint64_t cpu_phys = be64dec(&ranges[3]);
    269  1.1  jmcneill 		const uint64_t size = be64dec(&ranges[5]);
    270  1.1  jmcneill 
    271  1.7  jakllsch 		len -= 28;
    272  1.7  jakllsch 		ranges += 7;
    273  1.7  jakllsch 
    274  1.7  jakllsch 		const bool is64 = (__SHIFTOUT(phys_hi, PHYS_HI_SPACE) ==
    275  1.7  jakllsch 		    PHYS_HI_SPACE_MEM64) ? true : false;
    276  1.1  jmcneill 		switch (__SHIFTOUT(phys_hi, PHYS_HI_SPACE)) {
    277  1.1  jmcneill 		case PHYS_HI_SPACE_IO:
    278  1.7  jakllsch 			if (pibs->nranges + 1 >= __arraycount(pibs->ranges)) {
    279  1.7  jakllsch 				aprint_error_dev(sc->sc_dev, "too many IO ranges\n");
    280  1.7  jakllsch 				continue;
    281  1.7  jakllsch 			}
    282  1.7  jakllsch 			pibs->ranges[pibs->nranges].bpci = bus_phys;
    283  1.7  jakllsch 			pibs->ranges[pibs->nranges].bbus = cpu_phys;
    284  1.7  jakllsch 			pibs->ranges[pibs->nranges].size = size;
    285  1.7  jakllsch 			++pibs->nranges;
    286  1.1  jmcneill 			if (ioext != NULL) {
    287  1.1  jmcneill 				aprint_error_dev(sc->sc_dev, "ignoring duplicate IO space range\n");
    288  1.1  jmcneill 				continue;
    289  1.1  jmcneill 			}
    290  1.7  jakllsch 			ioext = extent_create("pciio", bus_phys, bus_phys + size - 1, NULL, 0, EX_NOWAIT);
    291  1.1  jmcneill 			aprint_verbose_dev(sc->sc_dev,
    292  1.7  jakllsch 			    "IO: 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
    293  1.7  jakllsch 			    bus_phys, size, cpu_phys);
    294  1.7  jakllsch 			/* reserve a PC-like legacy IO ports range, perhaps for access to VGA registers */
    295  1.7  jakllsch 			if (bus_phys == 0 && size >= 0x10000)
    296  1.7  jakllsch 				extent_alloc_region(ioext, 0, 0x1000, EX_WAITOK);
    297  1.1  jmcneill 			break;
    298  1.7  jakllsch 		case PHYS_HI_SPACE_MEM64:
    299  1.7  jakllsch 			/* FALLTHROUGH */
    300  1.1  jmcneill 		case PHYS_HI_SPACE_MEM32:
    301  1.7  jakllsch 			if (pmbs->nranges + 1 >= __arraycount(pmbs->ranges)) {
    302  1.7  jakllsch 				aprint_error_dev(sc->sc_dev, "too many mem ranges\n");
    303  1.7  jakllsch 				continue;
    304  1.7  jakllsch 			}
    305  1.7  jakllsch 			/* both pmem and mem spaces are in the same tag */
    306  1.7  jakllsch 			pmbs->ranges[pmbs->nranges].bpci = bus_phys;
    307  1.7  jakllsch 			pmbs->ranges[pmbs->nranges].bbus = cpu_phys;
    308  1.7  jakllsch 			pmbs->ranges[pmbs->nranges].size = size;
    309  1.7  jakllsch 			++pmbs->nranges;
    310  1.7  jakllsch 			if ((phys_hi & PHYS_HI_PREFETCH) != 0 ||
    311  1.7  jakllsch 			    __SHIFTOUT(phys_hi, PHYS_HI_SPACE) == PHYS_HI_SPACE_MEM64) {
    312  1.1  jmcneill 				if (pmemext != NULL) {
    313  1.1  jmcneill 					aprint_error_dev(sc->sc_dev, "ignoring duplicate mem (prefetchable) range\n");
    314  1.1  jmcneill 					continue;
    315  1.1  jmcneill 				}
    316  1.7  jakllsch 				pmemext = extent_create("pcipmem", bus_phys, bus_phys + size - 1, NULL, 0, EX_NOWAIT);
    317  1.1  jmcneill 				aprint_verbose_dev(sc->sc_dev,
    318  1.7  jakllsch 				    "MMIO (%d-bit prefetchable): 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
    319  1.7  jakllsch 				    is64 ? 64 : 32, bus_phys, size, cpu_phys);
    320  1.1  jmcneill 			} else {
    321  1.1  jmcneill 				if (memext != NULL) {
    322  1.1  jmcneill 					aprint_error_dev(sc->sc_dev, "ignoring duplicate mem (non-prefetchable) range\n");
    323  1.1  jmcneill 					continue;
    324  1.1  jmcneill 				}
    325  1.7  jakllsch 				memext = extent_create("pcimem", bus_phys, bus_phys + size - 1, NULL, 0, EX_NOWAIT);
    326  1.1  jmcneill 				aprint_verbose_dev(sc->sc_dev,
    327  1.7  jakllsch 				    "MMIO (%d-bit non-prefetchable): 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
    328  1.7  jakllsch 				    is64 ? 64 : 32, bus_phys, size, cpu_phys);
    329  1.1  jmcneill 			}
    330  1.1  jmcneill 			break;
    331  1.1  jmcneill 		default:
    332  1.1  jmcneill 			break;
    333  1.1  jmcneill 		}
    334  1.1  jmcneill 	}
    335  1.1  jmcneill 
    336  1.1  jmcneill 	error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, sc->sc_bus_min, PCIHOST_CACHELINE_SIZE);
    337  1.1  jmcneill 
    338  1.1  jmcneill 	if (ioext)
    339  1.1  jmcneill 		extent_destroy(ioext);
    340  1.1  jmcneill 	if (memext)
    341  1.1  jmcneill 		extent_destroy(memext);
    342  1.1  jmcneill 	if (pmemext)
    343  1.1  jmcneill 		extent_destroy(pmemext);
    344  1.1  jmcneill 
    345  1.1  jmcneill 	if (error) {
    346  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "configuration failed: %d\n", error);
    347  1.1  jmcneill 		return error;
    348  1.1  jmcneill 	}
    349  1.1  jmcneill 
    350  1.1  jmcneill 	return 0;
    351  1.1  jmcneill }
    352  1.1  jmcneill 
    353  1.1  jmcneill static void
    354  1.1  jmcneill pcihost_attach_hook(device_t parent, device_t self,
    355  1.1  jmcneill     struct pcibus_attach_args *pba)
    356  1.1  jmcneill {
    357  1.1  jmcneill }
    358  1.1  jmcneill 
    359  1.1  jmcneill static int
    360  1.1  jmcneill pcihost_bus_maxdevs(void *v, int busno)
    361  1.1  jmcneill {
    362  1.1  jmcneill 	return 32;
    363  1.1  jmcneill }
    364  1.1  jmcneill 
    365  1.1  jmcneill static pcitag_t
    366  1.1  jmcneill pcihost_make_tag(void *v, int b, int d, int f)
    367  1.1  jmcneill {
    368  1.1  jmcneill 	return (b << 16) | (d << 11) | (f << 8);
    369  1.1  jmcneill }
    370  1.1  jmcneill 
    371  1.1  jmcneill static void
    372  1.1  jmcneill pcihost_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    373  1.1  jmcneill {
    374  1.1  jmcneill 	if (bp)
    375  1.1  jmcneill 		*bp = (tag >> 16) & 0xff;
    376  1.1  jmcneill 	if (dp)
    377  1.1  jmcneill 		*dp = (tag >> 11) & 0x1f;
    378  1.1  jmcneill 	if (fp)
    379  1.1  jmcneill 		*fp = (tag >> 8) & 0x7;
    380  1.1  jmcneill }
    381  1.1  jmcneill 
    382  1.3  jmcneill static u_int
    383  1.3  jmcneill pcihost_get_segment(void *v)
    384  1.3  jmcneill {
    385  1.3  jmcneill 	struct pcihost_softc *sc = v;
    386  1.3  jmcneill 
    387  1.3  jmcneill 	return sc->sc_seg;
    388  1.3  jmcneill }
    389  1.3  jmcneill 
    390  1.1  jmcneill static pcireg_t
    391  1.1  jmcneill pcihost_conf_read(void *v, pcitag_t tag, int offset)
    392  1.1  jmcneill {
    393  1.1  jmcneill 	struct pcihost_softc *sc = v;
    394  1.1  jmcneill 	int b, d, f;
    395  1.1  jmcneill 	u_int reg;
    396  1.1  jmcneill 
    397  1.1  jmcneill 	pcihost_decompose_tag(v, tag, &b, &d, &f);
    398  1.1  jmcneill 
    399  1.1  jmcneill 	if (b < sc->sc_bus_min || b > sc->sc_bus_max)
    400  1.1  jmcneill 		return (pcireg_t) -1;
    401  1.1  jmcneill 
    402  1.1  jmcneill 	if (sc->sc_type == PCIHOST_CAM) {
    403  1.1  jmcneill 		if (offset & ~0xff)
    404  1.1  jmcneill 			return (pcireg_t) -1;
    405  1.1  jmcneill 		reg = (b << 16) | (d << 11) | (f << 8) | offset;
    406  1.1  jmcneill 	} else if (sc->sc_type == PCIHOST_ECAM) {
    407  1.1  jmcneill 		if (offset & ~0xfff)
    408  1.1  jmcneill 			return (pcireg_t) -1;
    409  1.1  jmcneill 		reg = (b << 20) | (d << 15) | (f << 12) | offset;
    410  1.1  jmcneill 	} else {
    411  1.1  jmcneill 		return (pcireg_t) -1;
    412  1.1  jmcneill 	}
    413  1.1  jmcneill 
    414  1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
    415  1.1  jmcneill }
    416  1.1  jmcneill 
    417  1.1  jmcneill static void
    418  1.1  jmcneill pcihost_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    419  1.1  jmcneill {
    420  1.1  jmcneill 	struct pcihost_softc *sc = v;
    421  1.1  jmcneill 	int b, d, f;
    422  1.1  jmcneill 	u_int reg;
    423  1.1  jmcneill 
    424  1.1  jmcneill 	pcihost_decompose_tag(v, tag, &b, &d, &f);
    425  1.1  jmcneill 
    426  1.1  jmcneill 	if (b < sc->sc_bus_min || b > sc->sc_bus_max)
    427  1.1  jmcneill 		return;
    428  1.1  jmcneill 
    429  1.1  jmcneill 	if (sc->sc_type == PCIHOST_CAM) {
    430  1.1  jmcneill 		if (offset & ~0xff)
    431  1.1  jmcneill 			return;
    432  1.1  jmcneill 		reg = (b << 16) | (d << 11) | (f << 8) | offset;
    433  1.1  jmcneill 	} else if (sc->sc_type == PCIHOST_ECAM) {
    434  1.1  jmcneill 		if (offset & ~0xfff)
    435  1.1  jmcneill 			return;
    436  1.1  jmcneill 		reg = (b << 20) | (d << 15) | (f << 12) | offset;
    437  1.1  jmcneill 	} else {
    438  1.1  jmcneill 		return;
    439  1.1  jmcneill 	}
    440  1.1  jmcneill 
    441  1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
    442  1.1  jmcneill }
    443  1.1  jmcneill 
    444  1.1  jmcneill static int
    445  1.1  jmcneill pcihost_conf_hook(void *v, int b, int d, int f, pcireg_t id)
    446  1.1  jmcneill {
    447  1.1  jmcneill 	return PCI_CONF_DEFAULT;
    448  1.1  jmcneill }
    449  1.1  jmcneill 
    450  1.1  jmcneill static void
    451  1.1  jmcneill pcihost_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
    452  1.1  jmcneill {
    453  1.1  jmcneill }
    454  1.1  jmcneill 
    455  1.1  jmcneill static int
    456  1.1  jmcneill pcihost_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
    457  1.1  jmcneill {
    458  1.2  jmcneill 	struct pcihost_softc *sc = pa->pa_pc->pc_intr_v;
    459  1.2  jmcneill 	u_int addr_cells, interrupt_cells;
    460  1.2  jmcneill 	const u_int *imap, *imask;
    461  1.2  jmcneill 	int imaplen, imasklen;
    462  1.2  jmcneill 	u_int match[4];
    463  1.2  jmcneill 	int index;
    464  1.2  jmcneill 
    465  1.1  jmcneill 	if (pa->pa_intrpin == 0)
    466  1.1  jmcneill 		return EINVAL;
    467  1.2  jmcneill 
    468  1.2  jmcneill 	imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
    469  1.2  jmcneill 	imask = fdtbus_get_prop(sc->sc_phandle, "interrupt-map-mask", &imasklen);
    470  1.2  jmcneill 	if (imap == NULL || imask == NULL || imasklen != 16)
    471  1.2  jmcneill 		return EINVAL;
    472  1.2  jmcneill 
    473  1.2  jmcneill 	/* Convert attach args to specifier */
    474  1.2  jmcneill 	match[0] = htobe32(
    475  1.2  jmcneill 			__SHIFTIN(pa->pa_bus, PHYS_HI_BUS) |
    476  1.2  jmcneill 			__SHIFTIN(pa->pa_device, PHYS_HI_DEVICE) |
    477  1.2  jmcneill 			__SHIFTIN(pa->pa_function, PHYS_HI_FUNCTION)
    478  1.2  jmcneill 		   ) & imask[0];
    479  1.2  jmcneill 	match[1] = htobe32(0) & imask[1];
    480  1.2  jmcneill 	match[2] = htobe32(0) & imask[2];
    481  1.2  jmcneill 	match[3] = htobe32(pa->pa_intrpin) & imask[3];
    482  1.2  jmcneill 
    483  1.2  jmcneill 	index = 0;
    484  1.2  jmcneill 	while (imaplen >= 20) {
    485  1.2  jmcneill 		const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
    486  1.2  jmcneill 	        if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
    487  1.2  jmcneill                 	addr_cells = 2;
    488  1.2  jmcneill 		if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
    489  1.2  jmcneill 			interrupt_cells = 0;
    490  1.2  jmcneill 		if (imaplen < (addr_cells + interrupt_cells) * 4)
    491  1.2  jmcneill 			return ENXIO;
    492  1.2  jmcneill 
    493  1.2  jmcneill 		if ((imap[0] & imask[0]) == match[0] &&
    494  1.2  jmcneill 		    (imap[1] & imask[1]) == match[1] &&
    495  1.2  jmcneill 		    (imap[2] & imask[2]) == match[2] &&
    496  1.2  jmcneill 		    (imap[3] & imask[3]) == match[3]) {
    497  1.2  jmcneill 			*ih = index;
    498  1.2  jmcneill 			return 0;
    499  1.2  jmcneill 		}
    500  1.2  jmcneill 
    501  1.2  jmcneill 		imap += (5 + addr_cells + interrupt_cells);
    502  1.2  jmcneill 		imaplen -= (5 + addr_cells + interrupt_cells) * 4;
    503  1.2  jmcneill 		index++;
    504  1.2  jmcneill 	}
    505  1.2  jmcneill 
    506  1.2  jmcneill 	return EINVAL;
    507  1.1  jmcneill }
    508  1.1  jmcneill 
    509  1.1  jmcneill static const u_int *
    510  1.2  jmcneill pcihost_find_intr(struct pcihost_softc *sc, pci_intr_handle_t ih, int *pihandle)
    511  1.1  jmcneill {
    512  1.1  jmcneill 	u_int addr_cells, interrupt_cells;
    513  1.2  jmcneill 	int imaplen, index;
    514  1.1  jmcneill 	const u_int *imap;
    515  1.1  jmcneill 
    516  1.1  jmcneill 	imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
    517  1.2  jmcneill 	KASSERT(imap != NULL);
    518  1.1  jmcneill 
    519  1.2  jmcneill 	index = 0;
    520  1.1  jmcneill 	while (imaplen >= 20) {
    521  1.1  jmcneill 		const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
    522  1.1  jmcneill 	        if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
    523  1.1  jmcneill                 	addr_cells = 2;
    524  1.1  jmcneill 		if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
    525  1.1  jmcneill 			interrupt_cells = 0;
    526  1.1  jmcneill 		if (imaplen < (addr_cells + interrupt_cells) * 4)
    527  1.1  jmcneill 			return NULL;
    528  1.1  jmcneill 
    529  1.2  jmcneill 		if (index == ih) {
    530  1.1  jmcneill 			*pihandle = map_ihandle;
    531  1.1  jmcneill 			return imap + 5 + addr_cells;
    532  1.1  jmcneill 		}
    533  1.1  jmcneill 
    534  1.2  jmcneill 		imap += (5 + addr_cells + interrupt_cells);
    535  1.2  jmcneill 		imaplen -= (5 + addr_cells + interrupt_cells) * 4;
    536  1.2  jmcneill 		index++;
    537  1.1  jmcneill 	}
    538  1.1  jmcneill 
    539  1.1  jmcneill 	return NULL;
    540  1.1  jmcneill }
    541  1.1  jmcneill 
    542  1.1  jmcneill static const char *
    543  1.1  jmcneill pcihost_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
    544  1.1  jmcneill {
    545  1.3  jmcneill 	const int irq = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    546  1.3  jmcneill 	const int vec = __SHIFTOUT(ih, ARM_PCI_INTR_MSI_VEC);
    547  1.1  jmcneill 	struct pcihost_softc *sc = v;
    548  1.1  jmcneill 	const u_int *specifier;
    549  1.1  jmcneill 	int ihandle;
    550  1.1  jmcneill 
    551  1.3  jmcneill 	if (ih & ARM_PCI_INTR_MSIX) {
    552  1.3  jmcneill 		snprintf(buf, len, "irq %d (MSI-X vec %d)", irq, vec);
    553  1.3  jmcneill 	} else if (ih & ARM_PCI_INTR_MSI) {
    554  1.3  jmcneill 		snprintf(buf, len, "irq %d (MSI vec %d)", irq, vec);
    555  1.3  jmcneill 	} else {
    556  1.3  jmcneill 		specifier = pcihost_find_intr(sc, ih & IH_INDEX_MASK, &ihandle);
    557  1.3  jmcneill 		if (specifier == NULL)
    558  1.3  jmcneill 			return NULL;
    559  1.1  jmcneill 
    560  1.3  jmcneill 		if (!fdtbus_intr_str_raw(ihandle, specifier, buf, len))
    561  1.3  jmcneill 			return NULL;
    562  1.3  jmcneill 	}
    563  1.1  jmcneill 
    564  1.1  jmcneill 	return buf;
    565  1.1  jmcneill }
    566  1.1  jmcneill 
    567  1.1  jmcneill const struct evcnt *
    568  1.1  jmcneill pcihost_intr_evcnt(void *v, pci_intr_handle_t ih)
    569  1.1  jmcneill {
    570  1.1  jmcneill 	return NULL;
    571  1.1  jmcneill }
    572  1.1  jmcneill 
    573  1.1  jmcneill static int
    574  1.1  jmcneill pcihost_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
    575  1.1  jmcneill {
    576  1.1  jmcneill 	switch (attr) {
    577  1.1  jmcneill 	case PCI_INTR_MPSAFE:
    578  1.1  jmcneill 		if (data)
    579  1.1  jmcneill 			*ih |= IH_MPSAFE;
    580  1.1  jmcneill 		else
    581  1.1  jmcneill 			*ih &= ~IH_MPSAFE;
    582  1.1  jmcneill 		return 0;
    583  1.1  jmcneill 	default:
    584  1.1  jmcneill 		return ENODEV;
    585  1.1  jmcneill 	}
    586  1.1  jmcneill }
    587  1.1  jmcneill 
    588  1.1  jmcneill static void *
    589  1.1  jmcneill pcihost_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    590  1.4  jmcneill     int (*callback)(void *), void *arg, const char *xname)
    591  1.1  jmcneill {
    592  1.1  jmcneill 	struct pcihost_softc *sc = v;
    593  1.1  jmcneill 	const int flags = (ih & IH_MPSAFE) ? FDT_INTR_MPSAFE : 0;
    594  1.1  jmcneill 	const u_int *specifier;
    595  1.1  jmcneill 	int ihandle;
    596  1.1  jmcneill 
    597  1.3  jmcneill 	if ((ih & (ARM_PCI_INTR_MSI | ARM_PCI_INTR_MSIX)) != 0)
    598  1.4  jmcneill 		return arm_pci_msi_intr_establish(&sc->sc_pc, ih, ipl, callback, arg, xname);
    599  1.3  jmcneill 
    600  1.2  jmcneill 	specifier = pcihost_find_intr(sc, ih & IH_INDEX_MASK, &ihandle);
    601  1.1  jmcneill 	if (specifier == NULL)
    602  1.1  jmcneill 		return NULL;
    603  1.1  jmcneill 
    604  1.1  jmcneill 	return fdtbus_intr_establish_raw(ihandle, specifier, ipl, flags, callback, arg);
    605  1.1  jmcneill }
    606  1.1  jmcneill 
    607  1.1  jmcneill static void
    608  1.1  jmcneill pcihost_intr_disestablish(void *v, void *vih)
    609  1.1  jmcneill {
    610  1.1  jmcneill 	struct pcihost_softc *sc = v;
    611  1.1  jmcneill 
    612  1.1  jmcneill 	fdtbus_intr_disestablish(sc->sc_phandle, vih);
    613  1.1  jmcneill }
    614  1.7  jakllsch 
    615  1.7  jakllsch static int
    616  1.7  jakllsch pcihost_bus_space_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
    617  1.7  jakllsch     bus_space_handle_t *bshp)
    618  1.7  jakllsch {
    619  1.7  jakllsch 	struct pcih_bus_space * const pbs = t;
    620  1.7  jakllsch 
    621  1.7  jakllsch 	for (size_t i = 0; i < pbs->nranges; i++) {
    622  1.7  jakllsch 		const bus_addr_t rmin = pbs->ranges[i].bpci;
    623  1.7  jakllsch 		const bus_addr_t rmax = pbs->ranges[i].bpci - 1 + pbs->ranges[i].size;
    624  1.7  jakllsch 		if ((bpa >= rmin) && ((bpa - 1 + size) <= rmax)) {
    625  1.7  jakllsch 			return pbs->map(t, bpa - pbs->ranges[i].bpci + pbs->ranges[i].bbus, size, flag, bshp);
    626  1.7  jakllsch 		}
    627  1.7  jakllsch 	}
    628  1.7  jakllsch 
    629  1.7  jakllsch 	return ERANGE;
    630  1.7  jakllsch }
    631