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