Home | History | Annotate | Line # | Download | only in fdt
pcihost_fdt.c revision 1.24.4.1
      1  1.24.4.1      cjep /* $NetBSD: pcihost_fdt.c,v 1.24.4.1 2021/05/31 22:15:10 cjep 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.24.4.1      cjep __KERNEL_RCSID(0, "$NetBSD: pcihost_fdt.c,v 1.24.4.1 2021/05/31 22:15:10 cjep 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.20   thorpej static const struct device_compatible_entry compat_data[] = {
     99      1.20   thorpej 	{ .compat = "pci-host-cam-generic",	.value = PCIHOST_CAM },
    100      1.20   thorpej 	{ .compat = "pci-host-ecam-generic",	.value = PCIHOST_ECAM },
    101      1.22   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.23   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.23   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.24.4.1      cjep 	config_found(sc->sc_dev, &pba, pcibusprint,
    198  1.24.4.1      cjep 	    CFARG_DEVHANDLE, device_handle(sc->sc_dev),
    199  1.24.4.1      cjep 	    CFARG_EOL);
    200       1.1  jmcneill }
    201       1.1  jmcneill 
    202       1.8  jakllsch void
    203       1.1  jmcneill pcihost_init(pci_chipset_tag_t pc, void *priv)
    204       1.1  jmcneill {
    205       1.1  jmcneill 	pc->pc_conf_v = priv;
    206       1.1  jmcneill 	pc->pc_attach_hook = pcihost_attach_hook;
    207       1.1  jmcneill 	pc->pc_bus_maxdevs = pcihost_bus_maxdevs;
    208       1.1  jmcneill 	pc->pc_make_tag = pcihost_make_tag;
    209       1.1  jmcneill 	pc->pc_decompose_tag = pcihost_decompose_tag;
    210       1.3  jmcneill 	pc->pc_get_segment = pcihost_get_segment;
    211       1.1  jmcneill 	pc->pc_conf_read = pcihost_conf_read;
    212       1.1  jmcneill 	pc->pc_conf_write = pcihost_conf_write;
    213       1.1  jmcneill 	pc->pc_conf_hook = pcihost_conf_hook;
    214       1.1  jmcneill 	pc->pc_conf_interrupt = pcihost_conf_interrupt;
    215       1.1  jmcneill 
    216       1.1  jmcneill 	pc->pc_intr_v = priv;
    217       1.1  jmcneill 	pc->pc_intr_map = pcihost_intr_map;
    218       1.1  jmcneill 	pc->pc_intr_string = pcihost_intr_string;
    219       1.1  jmcneill 	pc->pc_intr_evcnt = pcihost_intr_evcnt;
    220       1.1  jmcneill 	pc->pc_intr_setattr = pcihost_intr_setattr;
    221       1.1  jmcneill 	pc->pc_intr_establish = pcihost_intr_establish;
    222       1.1  jmcneill 	pc->pc_intr_disestablish = pcihost_intr_disestablish;
    223       1.1  jmcneill }
    224       1.1  jmcneill 
    225       1.1  jmcneill static int
    226       1.1  jmcneill pcihost_config(struct pcihost_softc *sc)
    227       1.1  jmcneill {
    228       1.1  jmcneill 	const u_int *ranges;
    229       1.3  jmcneill 	u_int probe_only;
    230      1.17   thorpej 	int error, len, type;
    231       1.9  jmcneill 	bool swap;
    232       1.1  jmcneill 
    233       1.7  jakllsch 	struct pcih_bus_space * const pibs = &sc->sc_io;
    234       1.7  jakllsch 	pibs->bst = *sc->sc_bst;
    235       1.7  jakllsch 	pibs->bst.bs_cookie = pibs;
    236       1.7  jakllsch 	pibs->map = pibs->bst.bs_map;
    237      1.12  jmcneill 	pibs->flags = PCI_FLAGS_IO_OKAY;
    238       1.7  jakllsch 	pibs->bst.bs_map = pcihost_bus_space_map;
    239       1.7  jakllsch 
    240       1.7  jakllsch 	struct pcih_bus_space * const pmbs = &sc->sc_mem;
    241       1.7  jakllsch 	pmbs->bst = *sc->sc_bst;
    242       1.7  jakllsch 	pmbs->bst.bs_cookie = pmbs;
    243       1.7  jakllsch 	pmbs->map = pmbs->bst.bs_map;
    244      1.12  jmcneill 	pmbs->flags = PCI_FLAGS_MEM_OKAY;
    245       1.7  jakllsch 	pmbs->bst.bs_map = pcihost_bus_space_map;
    246       1.7  jakllsch 
    247       1.3  jmcneill 	/*
    248       1.3  jmcneill 	 * If this flag is set, skip configuration of the PCI bus and use existing config.
    249       1.3  jmcneill 	 */
    250      1.18  jmcneill 	const int chosen = OF_finddevice("/chosen");
    251      1.18  jmcneill 	if (chosen <= 0 || of_getprop_uint32(chosen, "linux,pci-probe-only", &probe_only))
    252       1.3  jmcneill 		probe_only = 0;
    253       1.3  jmcneill 	if (probe_only)
    254       1.3  jmcneill 		return 0;
    255       1.3  jmcneill 
    256       1.9  jmcneill 	if (sc->sc_pci_ranges != NULL) {
    257       1.9  jmcneill 		ranges = sc->sc_pci_ranges;
    258       1.9  jmcneill 		len = sc->sc_pci_ranges_cells * 4;
    259       1.9  jmcneill 		swap = false;
    260       1.9  jmcneill 	} else {
    261       1.9  jmcneill 		ranges = fdtbus_get_prop(sc->sc_phandle, "ranges", &len);
    262       1.9  jmcneill 		if (ranges == NULL) {
    263       1.9  jmcneill 			aprint_error_dev(sc->sc_dev, "missing 'ranges' property\n");
    264       1.9  jmcneill 			return EINVAL;
    265       1.9  jmcneill 		}
    266       1.9  jmcneill 		swap = true;
    267       1.1  jmcneill 	}
    268       1.1  jmcneill 
    269      1.17   thorpej 	struct pciconf_resources *pcires = pciconf_resource_init();
    270      1.17   thorpej 
    271       1.1  jmcneill 	/*
    272       1.1  jmcneill 	 * Each entry in the ranges table contains:
    273       1.1  jmcneill 	 *  - bus address (3 cells)
    274       1.1  jmcneill 	 *  - cpu physical address (2 cells)
    275       1.1  jmcneill 	 *  - size (2 cells)
    276       1.1  jmcneill 	 * Total size for each entry is 28 bytes (7 cells).
    277       1.1  jmcneill 	 */
    278       1.1  jmcneill 	while (len >= 28) {
    279       1.9  jmcneill #define	DECODE32(x,o)	(swap ? be32dec(&(x)[o]) : (x)[o])
    280       1.9  jmcneill #define	DECODE64(x,o)	(swap ? be64dec(&(x)[o]) : (((uint64_t)((x)[(o)+0]) << 32) + (x)[(o)+1]))
    281       1.9  jmcneill 		const uint32_t phys_hi = DECODE32(ranges, 0);
    282      1.17   thorpej 		      uint64_t bus_phys = DECODE64(ranges, 1);
    283       1.9  jmcneill 		const uint64_t cpu_phys = DECODE64(ranges, 3);
    284      1.17   thorpej 		      uint64_t size = DECODE64(ranges, 5);
    285       1.9  jmcneill #undef	DECODE32
    286       1.9  jmcneill #undef	DECODE64
    287       1.1  jmcneill 
    288       1.7  jakllsch 		len -= 28;
    289       1.7  jakllsch 		ranges += 7;
    290       1.7  jakllsch 
    291       1.7  jakllsch 		const bool is64 = (__SHIFTOUT(phys_hi, PHYS_HI_SPACE) ==
    292       1.7  jakllsch 		    PHYS_HI_SPACE_MEM64) ? true : false;
    293       1.1  jmcneill 		switch (__SHIFTOUT(phys_hi, PHYS_HI_SPACE)) {
    294       1.1  jmcneill 		case PHYS_HI_SPACE_IO:
    295       1.7  jakllsch 			if (pibs->nranges + 1 >= __arraycount(pibs->ranges)) {
    296       1.7  jakllsch 				aprint_error_dev(sc->sc_dev, "too many IO ranges\n");
    297       1.7  jakllsch 				continue;
    298       1.7  jakllsch 			}
    299       1.7  jakllsch 			pibs->ranges[pibs->nranges].bpci = bus_phys;
    300       1.7  jakllsch 			pibs->ranges[pibs->nranges].bbus = cpu_phys;
    301       1.7  jakllsch 			pibs->ranges[pibs->nranges].size = size;
    302       1.7  jakllsch 			++pibs->nranges;
    303       1.1  jmcneill 			aprint_verbose_dev(sc->sc_dev,
    304       1.7  jakllsch 			    "IO: 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
    305       1.7  jakllsch 			    bus_phys, size, cpu_phys);
    306      1.17   thorpej 			/*
    307      1.17   thorpej 			 * Reserve a PC-like legacy IO ports range, perhaps
    308      1.17   thorpej 			 * for access to VGA registers.
    309      1.17   thorpej 			 */
    310      1.17   thorpej 			if (bus_phys == 0 && size >= 0x10000) {
    311      1.17   thorpej 				bus_phys += 0x1000;
    312      1.17   thorpej 				size -= 0x1000;
    313      1.17   thorpej 			}
    314      1.17   thorpej 			error = pciconf_resource_add(pcires,
    315      1.17   thorpej 			    PCICONF_RESOURCE_IO, bus_phys, size);
    316      1.17   thorpej 			if (error == 0)
    317      1.17   thorpej 				sc->sc_pci_flags |= PCI_FLAGS_IO_OKAY;
    318       1.1  jmcneill 			break;
    319       1.7  jakllsch 		case PHYS_HI_SPACE_MEM64:
    320       1.7  jakllsch 			/* FALLTHROUGH */
    321       1.1  jmcneill 		case PHYS_HI_SPACE_MEM32:
    322       1.7  jakllsch 			if (pmbs->nranges + 1 >= __arraycount(pmbs->ranges)) {
    323       1.7  jakllsch 				aprint_error_dev(sc->sc_dev, "too many mem ranges\n");
    324       1.7  jakllsch 				continue;
    325       1.7  jakllsch 			}
    326       1.7  jakllsch 			/* both pmem and mem spaces are in the same tag */
    327       1.7  jakllsch 			pmbs->ranges[pmbs->nranges].bpci = bus_phys;
    328       1.7  jakllsch 			pmbs->ranges[pmbs->nranges].bbus = cpu_phys;
    329       1.7  jakllsch 			pmbs->ranges[pmbs->nranges].size = size;
    330       1.7  jakllsch 			++pmbs->nranges;
    331       1.7  jakllsch 			if ((phys_hi & PHYS_HI_PREFETCH) != 0 ||
    332       1.7  jakllsch 			    __SHIFTOUT(phys_hi, PHYS_HI_SPACE) == PHYS_HI_SPACE_MEM64) {
    333      1.17   thorpej 				type = PCICONF_RESOURCE_PREFETCHABLE_MEM;
    334       1.1  jmcneill 				aprint_verbose_dev(sc->sc_dev,
    335       1.7  jakllsch 				    "MMIO (%d-bit prefetchable): 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
    336       1.7  jakllsch 				    is64 ? 64 : 32, bus_phys, size, cpu_phys);
    337       1.1  jmcneill 			} else {
    338      1.17   thorpej 				type = PCICONF_RESOURCE_MEM;
    339       1.1  jmcneill 				aprint_verbose_dev(sc->sc_dev,
    340       1.7  jakllsch 				    "MMIO (%d-bit non-prefetchable): 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
    341       1.7  jakllsch 				    is64 ? 64 : 32, bus_phys, size, cpu_phys);
    342       1.1  jmcneill 			}
    343      1.17   thorpej 			error = pciconf_resource_add(pcires, type, bus_phys,
    344      1.17   thorpej 			    size);
    345      1.17   thorpej 			if (error == 0)
    346      1.17   thorpej 				sc->sc_pci_flags |= PCI_FLAGS_MEM_OKAY;
    347       1.1  jmcneill 			break;
    348       1.1  jmcneill 		default:
    349       1.1  jmcneill 			break;
    350       1.1  jmcneill 		}
    351       1.1  jmcneill 	}
    352       1.1  jmcneill 
    353      1.17   thorpej 	error = pci_configure_bus(&sc->sc_pc, pcires, sc->sc_bus_min,
    354      1.17   thorpej 	    PCIHOST_CACHELINE_SIZE);
    355      1.10  jmcneill 
    356      1.17   thorpej 	pciconf_resource_fini(pcires);
    357       1.1  jmcneill 
    358       1.1  jmcneill 	if (error) {
    359       1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "configuration failed: %d\n", error);
    360       1.1  jmcneill 		return error;
    361       1.1  jmcneill 	}
    362       1.1  jmcneill 
    363       1.1  jmcneill 	return 0;
    364       1.1  jmcneill }
    365       1.1  jmcneill 
    366       1.1  jmcneill static void
    367       1.1  jmcneill pcihost_attach_hook(device_t parent, device_t self,
    368       1.1  jmcneill     struct pcibus_attach_args *pba)
    369       1.1  jmcneill {
    370       1.1  jmcneill }
    371       1.1  jmcneill 
    372       1.1  jmcneill static int
    373       1.1  jmcneill pcihost_bus_maxdevs(void *v, int busno)
    374       1.1  jmcneill {
    375       1.1  jmcneill 	return 32;
    376       1.1  jmcneill }
    377       1.1  jmcneill 
    378       1.1  jmcneill static pcitag_t
    379       1.1  jmcneill pcihost_make_tag(void *v, int b, int d, int f)
    380       1.1  jmcneill {
    381       1.1  jmcneill 	return (b << 16) | (d << 11) | (f << 8);
    382       1.1  jmcneill }
    383       1.1  jmcneill 
    384       1.1  jmcneill static void
    385       1.1  jmcneill pcihost_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    386       1.1  jmcneill {
    387       1.1  jmcneill 	if (bp)
    388       1.1  jmcneill 		*bp = (tag >> 16) & 0xff;
    389       1.1  jmcneill 	if (dp)
    390       1.1  jmcneill 		*dp = (tag >> 11) & 0x1f;
    391       1.1  jmcneill 	if (fp)
    392       1.1  jmcneill 		*fp = (tag >> 8) & 0x7;
    393       1.1  jmcneill }
    394       1.1  jmcneill 
    395       1.3  jmcneill static u_int
    396       1.3  jmcneill pcihost_get_segment(void *v)
    397       1.3  jmcneill {
    398       1.3  jmcneill 	struct pcihost_softc *sc = v;
    399       1.3  jmcneill 
    400       1.3  jmcneill 	return sc->sc_seg;
    401       1.3  jmcneill }
    402       1.3  jmcneill 
    403       1.1  jmcneill static pcireg_t
    404       1.1  jmcneill pcihost_conf_read(void *v, pcitag_t tag, int offset)
    405       1.1  jmcneill {
    406       1.1  jmcneill 	struct pcihost_softc *sc = v;
    407       1.1  jmcneill 	int b, d, f;
    408       1.1  jmcneill 	u_int reg;
    409       1.1  jmcneill 
    410       1.1  jmcneill 	pcihost_decompose_tag(v, tag, &b, &d, &f);
    411       1.1  jmcneill 
    412       1.1  jmcneill 	if (b < sc->sc_bus_min || b > sc->sc_bus_max)
    413       1.1  jmcneill 		return (pcireg_t) -1;
    414       1.1  jmcneill 
    415       1.1  jmcneill 	if (sc->sc_type == PCIHOST_CAM) {
    416       1.1  jmcneill 		if (offset & ~0xff)
    417       1.1  jmcneill 			return (pcireg_t) -1;
    418       1.1  jmcneill 		reg = (b << 16) | (d << 11) | (f << 8) | offset;
    419       1.1  jmcneill 	} else if (sc->sc_type == PCIHOST_ECAM) {
    420       1.1  jmcneill 		if (offset & ~0xfff)
    421       1.1  jmcneill 			return (pcireg_t) -1;
    422       1.1  jmcneill 		reg = (b << 20) | (d << 15) | (f << 12) | offset;
    423       1.1  jmcneill 	} else {
    424       1.1  jmcneill 		return (pcireg_t) -1;
    425       1.1  jmcneill 	}
    426       1.1  jmcneill 
    427       1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
    428       1.1  jmcneill }
    429       1.1  jmcneill 
    430       1.1  jmcneill static void
    431       1.1  jmcneill pcihost_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    432       1.1  jmcneill {
    433       1.1  jmcneill 	struct pcihost_softc *sc = v;
    434       1.1  jmcneill 	int b, d, f;
    435       1.1  jmcneill 	u_int reg;
    436       1.1  jmcneill 
    437       1.1  jmcneill 	pcihost_decompose_tag(v, tag, &b, &d, &f);
    438       1.1  jmcneill 
    439       1.1  jmcneill 	if (b < sc->sc_bus_min || b > sc->sc_bus_max)
    440       1.1  jmcneill 		return;
    441       1.1  jmcneill 
    442       1.1  jmcneill 	if (sc->sc_type == PCIHOST_CAM) {
    443       1.1  jmcneill 		if (offset & ~0xff)
    444       1.1  jmcneill 			return;
    445       1.1  jmcneill 		reg = (b << 16) | (d << 11) | (f << 8) | offset;
    446       1.1  jmcneill 	} else if (sc->sc_type == PCIHOST_ECAM) {
    447       1.1  jmcneill 		if (offset & ~0xfff)
    448       1.1  jmcneill 			return;
    449       1.1  jmcneill 		reg = (b << 20) | (d << 15) | (f << 12) | offset;
    450       1.1  jmcneill 	} else {
    451       1.1  jmcneill 		return;
    452       1.1  jmcneill 	}
    453       1.1  jmcneill 
    454       1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
    455       1.1  jmcneill }
    456       1.1  jmcneill 
    457       1.1  jmcneill static int
    458       1.1  jmcneill pcihost_conf_hook(void *v, int b, int d, int f, pcireg_t id)
    459       1.1  jmcneill {
    460       1.1  jmcneill 	return PCI_CONF_DEFAULT;
    461       1.1  jmcneill }
    462       1.1  jmcneill 
    463       1.1  jmcneill static void
    464       1.1  jmcneill pcihost_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
    465       1.1  jmcneill {
    466       1.1  jmcneill }
    467       1.1  jmcneill 
    468       1.1  jmcneill static int
    469       1.1  jmcneill pcihost_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
    470       1.1  jmcneill {
    471       1.2  jmcneill 	struct pcihost_softc *sc = pa->pa_pc->pc_intr_v;
    472       1.2  jmcneill 	u_int addr_cells, interrupt_cells;
    473       1.2  jmcneill 	const u_int *imap, *imask;
    474       1.2  jmcneill 	int imaplen, imasklen;
    475       1.2  jmcneill 	u_int match[4];
    476       1.2  jmcneill 	int index;
    477       1.2  jmcneill 
    478       1.1  jmcneill 	if (pa->pa_intrpin == 0)
    479       1.1  jmcneill 		return EINVAL;
    480       1.2  jmcneill 
    481       1.2  jmcneill 	imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
    482       1.2  jmcneill 	imask = fdtbus_get_prop(sc->sc_phandle, "interrupt-map-mask", &imasklen);
    483       1.2  jmcneill 	if (imap == NULL || imask == NULL || imasklen != 16)
    484       1.2  jmcneill 		return EINVAL;
    485       1.2  jmcneill 
    486       1.2  jmcneill 	/* Convert attach args to specifier */
    487       1.2  jmcneill 	match[0] = htobe32(
    488       1.2  jmcneill 			__SHIFTIN(pa->pa_bus, PHYS_HI_BUS) |
    489       1.2  jmcneill 			__SHIFTIN(pa->pa_device, PHYS_HI_DEVICE) |
    490       1.2  jmcneill 			__SHIFTIN(pa->pa_function, PHYS_HI_FUNCTION)
    491       1.2  jmcneill 		   ) & imask[0];
    492       1.2  jmcneill 	match[1] = htobe32(0) & imask[1];
    493       1.2  jmcneill 	match[2] = htobe32(0) & imask[2];
    494       1.2  jmcneill 	match[3] = htobe32(pa->pa_intrpin) & imask[3];
    495       1.2  jmcneill 
    496       1.2  jmcneill 	index = 0;
    497       1.2  jmcneill 	while (imaplen >= 20) {
    498       1.2  jmcneill 		const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
    499       1.2  jmcneill 	        if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
    500       1.2  jmcneill                 	addr_cells = 2;
    501       1.2  jmcneill 		if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
    502       1.2  jmcneill 			interrupt_cells = 0;
    503       1.2  jmcneill 		if (imaplen < (addr_cells + interrupt_cells) * 4)
    504       1.2  jmcneill 			return ENXIO;
    505       1.2  jmcneill 
    506       1.2  jmcneill 		if ((imap[0] & imask[0]) == match[0] &&
    507       1.2  jmcneill 		    (imap[1] & imask[1]) == match[1] &&
    508       1.2  jmcneill 		    (imap[2] & imask[2]) == match[2] &&
    509       1.2  jmcneill 		    (imap[3] & imask[3]) == match[3]) {
    510       1.2  jmcneill 			*ih = index;
    511       1.2  jmcneill 			return 0;
    512       1.2  jmcneill 		}
    513       1.2  jmcneill 
    514       1.2  jmcneill 		imap += (5 + addr_cells + interrupt_cells);
    515       1.2  jmcneill 		imaplen -= (5 + addr_cells + interrupt_cells) * 4;
    516       1.2  jmcneill 		index++;
    517       1.2  jmcneill 	}
    518       1.2  jmcneill 
    519       1.2  jmcneill 	return EINVAL;
    520       1.1  jmcneill }
    521       1.1  jmcneill 
    522       1.1  jmcneill static const u_int *
    523       1.2  jmcneill pcihost_find_intr(struct pcihost_softc *sc, pci_intr_handle_t ih, int *pihandle)
    524       1.1  jmcneill {
    525       1.1  jmcneill 	u_int addr_cells, interrupt_cells;
    526       1.2  jmcneill 	int imaplen, index;
    527       1.1  jmcneill 	const u_int *imap;
    528       1.1  jmcneill 
    529       1.1  jmcneill 	imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
    530       1.2  jmcneill 	KASSERT(imap != NULL);
    531       1.1  jmcneill 
    532       1.2  jmcneill 	index = 0;
    533       1.1  jmcneill 	while (imaplen >= 20) {
    534       1.1  jmcneill 		const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
    535       1.1  jmcneill 	        if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
    536       1.1  jmcneill                 	addr_cells = 2;
    537       1.1  jmcneill 		if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
    538       1.1  jmcneill 			interrupt_cells = 0;
    539       1.1  jmcneill 		if (imaplen < (addr_cells + interrupt_cells) * 4)
    540       1.1  jmcneill 			return NULL;
    541       1.1  jmcneill 
    542       1.2  jmcneill 		if (index == ih) {
    543       1.1  jmcneill 			*pihandle = map_ihandle;
    544       1.1  jmcneill 			return imap + 5 + addr_cells;
    545       1.1  jmcneill 		}
    546       1.1  jmcneill 
    547       1.2  jmcneill 		imap += (5 + addr_cells + interrupt_cells);
    548       1.2  jmcneill 		imaplen -= (5 + addr_cells + interrupt_cells) * 4;
    549       1.2  jmcneill 		index++;
    550       1.1  jmcneill 	}
    551       1.1  jmcneill 
    552       1.1  jmcneill 	return NULL;
    553       1.1  jmcneill }
    554       1.1  jmcneill 
    555       1.1  jmcneill static const char *
    556       1.1  jmcneill pcihost_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
    557       1.1  jmcneill {
    558       1.3  jmcneill 	const int irq = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    559       1.3  jmcneill 	const int vec = __SHIFTOUT(ih, ARM_PCI_INTR_MSI_VEC);
    560       1.1  jmcneill 	struct pcihost_softc *sc = v;
    561       1.1  jmcneill 	const u_int *specifier;
    562       1.1  jmcneill 	int ihandle;
    563       1.1  jmcneill 
    564       1.3  jmcneill 	if (ih & ARM_PCI_INTR_MSIX) {
    565       1.3  jmcneill 		snprintf(buf, len, "irq %d (MSI-X vec %d)", irq, vec);
    566       1.3  jmcneill 	} else if (ih & ARM_PCI_INTR_MSI) {
    567       1.3  jmcneill 		snprintf(buf, len, "irq %d (MSI vec %d)", irq, vec);
    568       1.3  jmcneill 	} else {
    569      1.11  jmcneill 		specifier = pcihost_find_intr(sc, ih & ARM_PCI_INTR_IRQ, &ihandle);
    570       1.3  jmcneill 		if (specifier == NULL)
    571       1.3  jmcneill 			return NULL;
    572       1.1  jmcneill 
    573       1.3  jmcneill 		if (!fdtbus_intr_str_raw(ihandle, specifier, buf, len))
    574       1.3  jmcneill 			return NULL;
    575       1.3  jmcneill 	}
    576       1.1  jmcneill 
    577       1.1  jmcneill 	return buf;
    578       1.1  jmcneill }
    579       1.1  jmcneill 
    580       1.1  jmcneill const struct evcnt *
    581       1.1  jmcneill pcihost_intr_evcnt(void *v, pci_intr_handle_t ih)
    582       1.1  jmcneill {
    583       1.1  jmcneill 	return NULL;
    584       1.1  jmcneill }
    585       1.1  jmcneill 
    586       1.1  jmcneill static int
    587       1.1  jmcneill pcihost_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
    588       1.1  jmcneill {
    589       1.1  jmcneill 	switch (attr) {
    590       1.1  jmcneill 	case PCI_INTR_MPSAFE:
    591       1.1  jmcneill 		if (data)
    592      1.11  jmcneill 			*ih |= ARM_PCI_INTR_MPSAFE;
    593       1.1  jmcneill 		else
    594      1.11  jmcneill 			*ih &= ~ARM_PCI_INTR_MPSAFE;
    595       1.1  jmcneill 		return 0;
    596       1.1  jmcneill 	default:
    597       1.1  jmcneill 		return ENODEV;
    598       1.1  jmcneill 	}
    599       1.1  jmcneill }
    600       1.1  jmcneill 
    601       1.1  jmcneill static void *
    602       1.1  jmcneill pcihost_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    603       1.4  jmcneill     int (*callback)(void *), void *arg, const char *xname)
    604       1.1  jmcneill {
    605       1.1  jmcneill 	struct pcihost_softc *sc = v;
    606      1.11  jmcneill 	const int flags = (ih & ARM_PCI_INTR_MPSAFE) ? FDT_INTR_MPSAFE : 0;
    607       1.1  jmcneill 	const u_int *specifier;
    608       1.1  jmcneill 	int ihandle;
    609       1.1  jmcneill 
    610       1.3  jmcneill 	if ((ih & (ARM_PCI_INTR_MSI | ARM_PCI_INTR_MSIX)) != 0)
    611       1.4  jmcneill 		return arm_pci_msi_intr_establish(&sc->sc_pc, ih, ipl, callback, arg, xname);
    612       1.3  jmcneill 
    613      1.11  jmcneill 	specifier = pcihost_find_intr(sc, ih & ARM_PCI_INTR_IRQ, &ihandle);
    614       1.1  jmcneill 	if (specifier == NULL)
    615       1.1  jmcneill 		return NULL;
    616       1.1  jmcneill 
    617      1.19       ryo 	return fdtbus_intr_establish_raw(ihandle, specifier, ipl, flags,
    618      1.19       ryo 	    callback, arg, xname);
    619       1.1  jmcneill }
    620       1.1  jmcneill 
    621       1.1  jmcneill static void
    622       1.1  jmcneill pcihost_intr_disestablish(void *v, void *vih)
    623       1.1  jmcneill {
    624       1.1  jmcneill 	struct pcihost_softc *sc = v;
    625       1.1  jmcneill 
    626       1.1  jmcneill 	fdtbus_intr_disestablish(sc->sc_phandle, vih);
    627       1.1  jmcneill }
    628       1.7  jakllsch 
    629       1.7  jakllsch static int
    630       1.7  jakllsch pcihost_bus_space_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
    631       1.7  jakllsch     bus_space_handle_t *bshp)
    632       1.7  jakllsch {
    633       1.7  jakllsch 	struct pcih_bus_space * const pbs = t;
    634       1.7  jakllsch 
    635      1.12  jmcneill 	if ((pbs->flags & PCI_FLAGS_IO_OKAY) != 0) {
    636      1.12  jmcneill 		/* Force strongly ordered mapping for all I/O space */
    637      1.12  jmcneill 		flag = _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED;
    638      1.12  jmcneill 	}
    639      1.12  jmcneill 
    640       1.7  jakllsch 	for (size_t i = 0; i < pbs->nranges; i++) {
    641       1.7  jakllsch 		const bus_addr_t rmin = pbs->ranges[i].bpci;
    642       1.7  jakllsch 		const bus_addr_t rmax = pbs->ranges[i].bpci - 1 + pbs->ranges[i].size;
    643       1.7  jakllsch 		if ((bpa >= rmin) && ((bpa - 1 + size) <= rmax)) {
    644       1.7  jakllsch 			return pbs->map(t, bpa - pbs->ranges[i].bpci + pbs->ranges[i].bbus, size, flag, bshp);
    645       1.7  jakllsch 		}
    646       1.7  jakllsch 	}
    647       1.7  jakllsch 
    648       1.7  jakllsch 	return ERANGE;
    649       1.7  jakllsch }
    650