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