Home | History | Annotate | Line # | Download | only in apple
apple_pcie.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: apple_pcie.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2021 Jared 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.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: apple_pcie.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/device.h>
     34  1.1  jmcneill #include <sys/kernel.h>
     35  1.1  jmcneill #include <sys/systm.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/kmem.h>
     38  1.1  jmcneill #include <sys/bitops.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/pci/pcireg.h>
     41  1.1  jmcneill #include <dev/pci/pcivar.h>
     42  1.1  jmcneill #include <dev/pci/pciconf.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <arm/pci/pci_msi_machdep.h>
     47  1.1  jmcneill #include <arm/fdt/pcihost_fdtvar.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #define	PCIE_MSI_CTRL		0x0124
     50  1.1  jmcneill #define	 PCIE_MSI_CTRL_EN	(1U << 0)
     51  1.1  jmcneill #define	 PCIE_MSI_CTRL_32	(5U << 4)
     52  1.1  jmcneill #define	PCIE_MSI_REMAP		0x0128
     53  1.1  jmcneill #define	PCIE_MSI_DOORBELL	0x0168
     54  1.1  jmcneill 
     55  1.1  jmcneill struct apple_pcie_softc {
     56  1.1  jmcneill 	struct pcihost_softc	sc_pcihost;
     57  1.1  jmcneill 
     58  1.1  jmcneill 	int			sc_phandle;
     59  1.1  jmcneill 	struct arm_pci_msi	sc_msi;
     60  1.1  jmcneill 	u_int			sc_msi_start;
     61  1.1  jmcneill 	u_int			sc_nmsi;
     62  1.1  jmcneill 	struct pci_attach_args	**sc_msi_pa;
     63  1.1  jmcneill 	void			**sc_msi_ih;
     64  1.1  jmcneill 	uint64_t		sc_msi_addr;
     65  1.1  jmcneill };
     66  1.1  jmcneill 
     67  1.1  jmcneill static int	apple_pcie_match(device_t, cfdata_t, void *);
     68  1.1  jmcneill static void	apple_pcie_attach(device_t, device_t, void *);
     69  1.1  jmcneill 
     70  1.1  jmcneill static void	apple_pcie_attach_hook(device_t, device_t,
     71  1.1  jmcneill 				       struct pcibus_attach_args *);
     72  1.1  jmcneill static int	apple_pcie_msi_init(struct apple_pcie_softc *);
     73  1.1  jmcneill 
     74  1.1  jmcneill CFATTACH_DECL_NEW(apple_pcie, sizeof(struct apple_pcie_softc),
     75  1.1  jmcneill 	apple_pcie_match, apple_pcie_attach, NULL, NULL);
     76  1.1  jmcneill 
     77  1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
     78  1.1  jmcneill 	{ .compat = "apple,pcie" },
     79  1.1  jmcneill 	DEVICE_COMPAT_EOL
     80  1.1  jmcneill };
     81  1.1  jmcneill 
     82  1.1  jmcneill static int
     83  1.1  jmcneill apple_pcie_match(device_t parent, cfdata_t cf, void *aux)
     84  1.1  jmcneill {
     85  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     86  1.1  jmcneill 
     87  1.1  jmcneill 	return of_compatible_match(faa->faa_phandle, compat_data);
     88  1.1  jmcneill }
     89  1.1  jmcneill 
     90  1.1  jmcneill static void
     91  1.1  jmcneill apple_pcie_attach(device_t parent, device_t self, void *aux)
     92  1.1  jmcneill {
     93  1.1  jmcneill 	struct apple_pcie_softc * const asc = device_private(self);
     94  1.1  jmcneill 	struct pcihost_softc * const sc = &asc->sc_pcihost;
     95  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     96  1.1  jmcneill 	const int phandle = faa->faa_phandle;
     97  1.1  jmcneill 	bus_addr_t cs_addr;
     98  1.1  jmcneill 	bus_size_t cs_size;
     99  1.1  jmcneill 	int error;
    100  1.1  jmcneill 
    101  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &cs_addr, &cs_size) != 0) {
    102  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    103  1.1  jmcneill 		return;
    104  1.1  jmcneill 	}
    105  1.1  jmcneill 
    106  1.1  jmcneill 	sc->sc_dev = self;
    107  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    108  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    109  1.1  jmcneill 	sc->sc_phandle = phandle;
    110  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, cs_addr, cs_size,
    111  1.1  jmcneill 	    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &sc->sc_bsh);
    112  1.1  jmcneill 	if (error) {
    113  1.1  jmcneill 		aprint_error(": couldn't map registers: %d\n", error);
    114  1.1  jmcneill 		return;
    115  1.1  jmcneill 	}
    116  1.1  jmcneill 	sc->sc_type = PCIHOST_ECAM;
    117  1.1  jmcneill 
    118  1.1  jmcneill 	if (apple_pcie_msi_init(asc) == 0) {
    119  1.1  jmcneill 		sc->sc_pci_flags |= PCI_FLAGS_MSI_OKAY;
    120  1.1  jmcneill #if notyet
    121  1.1  jmcneill 		sc->sc_pci_flags |= PCI_FLAGS_MSIX_OKAY;
    122  1.1  jmcneill #endif
    123  1.1  jmcneill 	}
    124  1.1  jmcneill 
    125  1.1  jmcneill 	aprint_naive("\n");
    126  1.1  jmcneill 	aprint_normal(": Apple PCIe host controller\n");
    127  1.1  jmcneill 
    128  1.1  jmcneill 	pcihost_init(&sc->sc_pc, sc);
    129  1.1  jmcneill 	sc->sc_pc.pc_attach_hook = apple_pcie_attach_hook;
    130  1.1  jmcneill 	pcihost_init2(sc);
    131  1.1  jmcneill }
    132  1.1  jmcneill 
    133  1.1  jmcneill static void
    134  1.1  jmcneill apple_pcie_setup_port(struct apple_pcie_softc *sc, u_int portno)
    135  1.1  jmcneill {
    136  1.1  jmcneill 	const int phandle = sc->sc_pcihost.sc_phandle;
    137  1.1  jmcneill 	bus_space_tag_t bst = sc->sc_pcihost.sc_bst;
    138  1.1  jmcneill 	char regname[sizeof("portX")];
    139  1.1  jmcneill 	bus_space_handle_t bsh;
    140  1.1  jmcneill 	bus_addr_t addr;
    141  1.1  jmcneill 	bus_size_t size;
    142  1.1  jmcneill 	int error;
    143  1.1  jmcneill 
    144  1.1  jmcneill 	snprintf(regname, sizeof(regname), "port%u", portno);
    145  1.1  jmcneill 	if (fdtbus_get_reg_byname(phandle, regname, &addr, &size) != 0) {
    146  1.1  jmcneill 		aprint_error(": couldn't get %s regs\n", regname);
    147  1.1  jmcneill 		return;
    148  1.1  jmcneill 	}
    149  1.1  jmcneill 	error = bus_space_map(bst, addr, size,
    150  1.1  jmcneill 	    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &bsh);
    151  1.1  jmcneill 	if (error != 0) {
    152  1.1  jmcneill 		aprint_error(": couldn't map %s regs\n", regname);
    153  1.1  jmcneill 		return;
    154  1.1  jmcneill 	}
    155  1.1  jmcneill 
    156  1.1  jmcneill 	/* Doorbell address must be below 4GB */
    157  1.1  jmcneill 	KASSERT((sc->sc_msi_addr & ~0xffffffffUL) == 0);
    158  1.1  jmcneill 
    159  1.1  jmcneill 	bus_space_write_4(bst, bsh, PCIE_MSI_CTRL,
    160  1.1  jmcneill 	    PCIE_MSI_CTRL_32 | PCIE_MSI_CTRL_EN);
    161  1.1  jmcneill 	bus_space_write_4(bst, bsh, PCIE_MSI_REMAP, 0);
    162  1.1  jmcneill 	bus_space_write_4(bst, bsh, PCIE_MSI_DOORBELL,
    163  1.1  jmcneill 	    (uint32_t)sc->sc_msi_addr);
    164  1.1  jmcneill 
    165  1.1  jmcneill 	bus_space_unmap(bst, bsh, size);
    166  1.1  jmcneill }
    167  1.1  jmcneill 
    168  1.1  jmcneill static void
    169  1.1  jmcneill apple_pcie_attach_hook(device_t parent, device_t self,
    170  1.1  jmcneill     struct pcibus_attach_args *pba)
    171  1.1  jmcneill {
    172  1.1  jmcneill 	struct apple_pcie_softc *sc = pba->pba_pc->pc_conf_v;
    173  1.1  jmcneill 	const int phandle = sc->sc_pcihost.sc_phandle;
    174  1.2  jmcneill 	bus_dma_tag_t dmat;
    175  1.1  jmcneill 
    176  1.1  jmcneill 	KASSERT(device_is_a(sc->sc_pcihost.sc_dev, "applepcie"));
    177  1.1  jmcneill 
    178  1.2  jmcneill 	/* XXX this should be per-device, not per-bus */
    179  1.2  jmcneill 	const uint32_t rid = pba->pba_bus << 8;
    180  1.1  jmcneill 
    181  1.2  jmcneill 	dmat = fdtbus_iommu_map_pci(phandle, rid, sc->sc_pcihost.sc_dmat);
    182  1.2  jmcneill 	pba->pba_dmat = pba->pba_dmat64 = dmat;
    183  1.1  jmcneill }
    184  1.1  jmcneill 
    185  1.1  jmcneill static int
    186  1.1  jmcneill apple_pcie_msi_alloc_msi(struct apple_pcie_softc *sc, int count,
    187  1.1  jmcneill     const struct pci_attach_args *pa)
    188  1.1  jmcneill {
    189  1.1  jmcneill 	struct pci_attach_args *new_pa;
    190  1.1  jmcneill 	int msi, n;
    191  1.1  jmcneill 
    192  1.1  jmcneill 	for (msi = 0; msi < sc->sc_nmsi; msi += count) {
    193  1.1  jmcneill 		if (sc->sc_msi_pa[msi] == NULL) {
    194  1.1  jmcneill 			for (n = 1; n < count; n++) {
    195  1.1  jmcneill 				if (msi + n < sc->sc_nmsi &&
    196  1.1  jmcneill 				    sc->sc_msi_pa[msi + n] != NULL) {
    197  1.1  jmcneill 					continue;
    198  1.1  jmcneill 				}
    199  1.1  jmcneill 			}
    200  1.1  jmcneill 
    201  1.1  jmcneill 			for (n = 0; n < count; n++) {
    202  1.1  jmcneill 				new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
    203  1.1  jmcneill 				memcpy(new_pa, pa, sizeof(*new_pa));
    204  1.1  jmcneill 				sc->sc_msi_pa[msi + n] = new_pa;
    205  1.1  jmcneill 			}
    206  1.1  jmcneill 
    207  1.1  jmcneill 			return msi;
    208  1.1  jmcneill 		}
    209  1.1  jmcneill 	}
    210  1.1  jmcneill 
    211  1.1  jmcneill 	return -1;
    212  1.1  jmcneill }
    213  1.1  jmcneill 
    214  1.1  jmcneill static void
    215  1.1  jmcneill apple_pcie_msi_free_msi(struct apple_pcie_softc *sc, int msi)
    216  1.1  jmcneill {
    217  1.1  jmcneill 	struct pci_attach_args *pa;
    218  1.1  jmcneill 
    219  1.1  jmcneill 	pa = sc->sc_msi_pa[msi];
    220  1.1  jmcneill 	sc->sc_msi_pa[msi] = NULL;
    221  1.1  jmcneill 
    222  1.1  jmcneill 	if (pa != NULL) {
    223  1.1  jmcneill 		kmem_free(pa, sizeof(*pa));
    224  1.1  jmcneill 	}
    225  1.1  jmcneill }
    226  1.1  jmcneill 
    227  1.1  jmcneill static int
    228  1.1  jmcneill apple_pcie_msi_available_msi(struct apple_pcie_softc *sc)
    229  1.1  jmcneill {
    230  1.1  jmcneill 	int msi, n;
    231  1.1  jmcneill 
    232  1.1  jmcneill 	for (n = 0, msi = 0; msi < sc->sc_nmsi; msi++) {
    233  1.1  jmcneill 		if (sc->sc_msi_pa[msi] == NULL) {
    234  1.1  jmcneill 			n++;
    235  1.1  jmcneill 		}
    236  1.1  jmcneill 	}
    237  1.1  jmcneill 
    238  1.1  jmcneill 	return n;
    239  1.1  jmcneill }
    240  1.1  jmcneill 
    241  1.1  jmcneill static void
    242  1.1  jmcneill apple_pcie_msi_msi_enable(struct apple_pcie_softc *sc, int msi, int count)
    243  1.1  jmcneill {
    244  1.1  jmcneill 	const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
    245  1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
    246  1.1  jmcneill 	pcitag_t tag = pa->pa_tag;
    247  1.1  jmcneill 	pcireg_t ctl;
    248  1.1  jmcneill 	int off;
    249  1.1  jmcneill 
    250  1.1  jmcneill 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    251  1.1  jmcneill 		panic("apple_pcie_msi_msi_enable: device is not MSI-capable");
    252  1.1  jmcneill 
    253  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    254  1.1  jmcneill 	ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    255  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    256  1.1  jmcneill 
    257  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    258  1.1  jmcneill 	ctl &= ~PCI_MSI_CTL_MME_MASK;
    259  1.1  jmcneill 	ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
    260  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    261  1.1  jmcneill 
    262  1.1  jmcneill 	const uint64_t addr = sc->sc_msi_addr;
    263  1.1  jmcneill 	const uint32_t data = msi;
    264  1.1  jmcneill 
    265  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    266  1.1  jmcneill 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
    267  1.1  jmcneill 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
    268  1.1  jmcneill 		    addr & 0xffffffff);
    269  1.1  jmcneill 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
    270  1.1  jmcneill 		    (addr >> 32) & 0xffffffff);
    271  1.1  jmcneill 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, data);
    272  1.1  jmcneill 	} else {
    273  1.1  jmcneill 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
    274  1.1  jmcneill 		    addr & 0xffffffff);
    275  1.1  jmcneill 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA, data);
    276  1.1  jmcneill 	}
    277  1.1  jmcneill 	ctl |= PCI_MSI_CTL_MSI_ENABLE;
    278  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    279  1.1  jmcneill }
    280  1.1  jmcneill 
    281  1.1  jmcneill static void
    282  1.1  jmcneill apple_pcie_msi_msi_disable(struct apple_pcie_softc *sc, int msi)
    283  1.1  jmcneill {
    284  1.1  jmcneill 	const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
    285  1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
    286  1.1  jmcneill 	pcitag_t tag = pa->pa_tag;
    287  1.1  jmcneill 	pcireg_t ctl;
    288  1.1  jmcneill 	int off;
    289  1.1  jmcneill 
    290  1.1  jmcneill 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    291  1.1  jmcneill 		panic("apple_pcie_msi_msi_disable: device is not MSI-capable");
    292  1.1  jmcneill 
    293  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    294  1.1  jmcneill 	ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    295  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    296  1.1  jmcneill }
    297  1.1  jmcneill 
    298  1.1  jmcneill static void
    299  1.1  jmcneill apple_pcie_msi_msix_enable(struct apple_pcie_softc *sc, int msi, int msix_vec,
    300  1.1  jmcneill     bus_space_tag_t bst, bus_space_handle_t bsh)
    301  1.1  jmcneill {
    302  1.1  jmcneill 	const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
    303  1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
    304  1.1  jmcneill 	pcitag_t tag = pa->pa_tag;
    305  1.1  jmcneill 	pcireg_t ctl;
    306  1.1  jmcneill 	uint32_t val;
    307  1.1  jmcneill 	int off;
    308  1.1  jmcneill 
    309  1.1  jmcneill 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    310  1.1  jmcneill 		panic("apple_pcie_msi_msix_enable: device is not MSI-X-capable");
    311  1.1  jmcneill 
    312  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    313  1.1  jmcneill 	ctl &= ~PCI_MSIX_CTL_ENABLE;
    314  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    315  1.1  jmcneill 
    316  1.1  jmcneill 	const uint64_t addr = sc->sc_msi_addr;
    317  1.1  jmcneill 	const uint32_t data = msi;
    318  1.1  jmcneill 	const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
    319  1.1  jmcneill 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO,
    320  1.1  jmcneill 	    (uint32_t)addr);
    321  1.1  jmcneill 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI,
    322  1.1  jmcneill 	    (uint32_t)(addr >> 32));
    323  1.1  jmcneill 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA,
    324  1.1  jmcneill 	    data);
    325  1.1  jmcneill 	val = bus_space_read_4(bst, bsh,
    326  1.1  jmcneill 	    entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL);
    327  1.1  jmcneill 	val &= ~PCI_MSIX_VECTCTL_MASK;
    328  1.1  jmcneill 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL,
    329  1.1  jmcneill 	    val);
    330  1.1  jmcneill 
    331  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    332  1.1  jmcneill 	ctl |= PCI_MSIX_CTL_ENABLE;
    333  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    334  1.1  jmcneill }
    335  1.1  jmcneill 
    336  1.1  jmcneill static void
    337  1.1  jmcneill apple_pcie_msi_msix_disable(struct apple_pcie_softc *sc, int msi)
    338  1.1  jmcneill {
    339  1.1  jmcneill 	const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
    340  1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
    341  1.1  jmcneill 	pcitag_t tag = pa->pa_tag;
    342  1.1  jmcneill 	pcireg_t ctl;
    343  1.1  jmcneill 	int off;
    344  1.1  jmcneill 
    345  1.1  jmcneill 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    346  1.1  jmcneill 		panic("apple_pcie_msi_msix_disable: device is not MSI-X-capable");
    347  1.1  jmcneill 
    348  1.1  jmcneill 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    349  1.1  jmcneill 	ctl &= ~PCI_MSIX_CTL_ENABLE;
    350  1.1  jmcneill 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    351  1.1  jmcneill }
    352  1.1  jmcneill 
    353  1.1  jmcneill static pci_intr_handle_t *
    354  1.1  jmcneill apple_pcie_msi_msi_alloc(struct arm_pci_msi *msi, int *count,
    355  1.1  jmcneill     const struct pci_attach_args *pa, bool exact)
    356  1.1  jmcneill {
    357  1.1  jmcneill 	struct apple_pcie_softc * const sc = msi->msi_priv;
    358  1.1  jmcneill 	pci_intr_handle_t *vectors;
    359  1.1  jmcneill 	int n, off;
    360  1.1  jmcneill 
    361  1.1  jmcneill 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
    362  1.1  jmcneill 		return NULL;
    363  1.1  jmcneill 
    364  1.1  jmcneill 	const int avail = apple_pcie_msi_available_msi(sc);
    365  1.1  jmcneill 	if (exact && *count > avail)
    366  1.1  jmcneill 		return NULL;
    367  1.1  jmcneill 
    368  1.1  jmcneill 	while (*count > avail) {
    369  1.1  jmcneill 		if (avail < *count)
    370  1.1  jmcneill 			(*count) >>= 1;
    371  1.1  jmcneill 	}
    372  1.1  jmcneill 	if (*count == 0)
    373  1.1  jmcneill 		return NULL;
    374  1.1  jmcneill 
    375  1.1  jmcneill 	const int msi_base = apple_pcie_msi_alloc_msi(sc, *count, pa);
    376  1.1  jmcneill 	if (msi_base == -1)
    377  1.1  jmcneill 		return NULL;
    378  1.1  jmcneill 
    379  1.1  jmcneill 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    380  1.1  jmcneill 	for (n = 0; n < *count; n++) {
    381  1.1  jmcneill 		const int msino = msi_base + n;
    382  1.1  jmcneill 		vectors[n] = ARM_PCI_INTR_MSI |
    383  1.1  jmcneill 		    __SHIFTIN(msino, ARM_PCI_INTR_IRQ) |
    384  1.1  jmcneill 		    __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
    385  1.1  jmcneill 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    386  1.1  jmcneill 	}
    387  1.1  jmcneill 
    388  1.1  jmcneill 	apple_pcie_msi_msi_enable(sc, msi_base, *count);
    389  1.1  jmcneill 
    390  1.1  jmcneill 	return vectors;
    391  1.1  jmcneill }
    392  1.1  jmcneill 
    393  1.1  jmcneill static pci_intr_handle_t *
    394  1.1  jmcneill apple_pcie_msi_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes,
    395  1.1  jmcneill     int *count, const struct pci_attach_args *pa, bool exact)
    396  1.1  jmcneill {
    397  1.1  jmcneill 	struct apple_pcie_softc * const sc = msi->msi_priv;
    398  1.1  jmcneill 	pci_intr_handle_t *vectors;
    399  1.1  jmcneill 	bus_space_tag_t bst;
    400  1.1  jmcneill 	bus_space_handle_t bsh;
    401  1.1  jmcneill 	bus_size_t bsz;
    402  1.1  jmcneill 	uint32_t table_offset, table_size;
    403  1.1  jmcneill 	int n, off, bar, error;
    404  1.1  jmcneill 	pcireg_t tbl;
    405  1.1  jmcneill 
    406  1.1  jmcneill 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
    407  1.1  jmcneill 		return NULL;
    408  1.1  jmcneill 
    409  1.1  jmcneill 	const int avail = apple_pcie_msi_available_msi(sc);
    410  1.1  jmcneill 	if (exact && *count > avail)
    411  1.1  jmcneill 		return NULL;
    412  1.1  jmcneill 
    413  1.1  jmcneill 	while (*count > avail) {
    414  1.1  jmcneill 		if (avail < *count)
    415  1.1  jmcneill 			(*count) >>= 1;
    416  1.1  jmcneill 	}
    417  1.1  jmcneill 	if (*count == 0)
    418  1.1  jmcneill 		return NULL;
    419  1.1  jmcneill 
    420  1.1  jmcneill 	tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
    421  1.1  jmcneill 	bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_TBLBIR_MASK));
    422  1.1  jmcneill 	table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
    423  1.1  jmcneill 	table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
    424  1.1  jmcneill 	if (table_size == 0)
    425  1.1  jmcneill 		return NULL;
    426  1.1  jmcneill 
    427  1.1  jmcneill 	error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
    428  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
    429  1.1  jmcneill 	    &bst, &bsh, NULL, &bsz);
    430  1.1  jmcneill 	if (error)
    431  1.1  jmcneill 		return NULL;
    432  1.1  jmcneill 
    433  1.1  jmcneill 	const int msi_base = apple_pcie_msi_alloc_msi(sc, *count, pa);
    434  1.1  jmcneill 	if (msi_base == -1) {
    435  1.1  jmcneill 		bus_space_unmap(bst, bsh, bsz);
    436  1.1  jmcneill 		return NULL;
    437  1.1  jmcneill 	}
    438  1.1  jmcneill 
    439  1.1  jmcneill 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    440  1.1  jmcneill 	for (n = 0; n < *count; n++) {
    441  1.1  jmcneill 		const int msino = msi_base + n;
    442  1.1  jmcneill 		const int msix_vec = table_indexes ? table_indexes[n] : n;
    443  1.1  jmcneill 		vectors[msix_vec] = ARM_PCI_INTR_MSIX |
    444  1.1  jmcneill 		    __SHIFTIN(msino, ARM_PCI_INTR_IRQ) |
    445  1.1  jmcneill 		    __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
    446  1.1  jmcneill 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    447  1.1  jmcneill 
    448  1.1  jmcneill 		apple_pcie_msi_msix_enable(sc, msino, msix_vec, bst, bsh);
    449  1.1  jmcneill 	}
    450  1.1  jmcneill 
    451  1.1  jmcneill 	bus_space_unmap(bst, bsh, bsz);
    452  1.1  jmcneill 
    453  1.1  jmcneill 	return vectors;
    454  1.1  jmcneill }
    455  1.1  jmcneill 
    456  1.1  jmcneill static void *
    457  1.1  jmcneill apple_pcie_msi_intr_establish(struct arm_pci_msi *msi,
    458  1.1  jmcneill     pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
    459  1.1  jmcneill {
    460  1.1  jmcneill 	struct apple_pcie_softc * const sc = msi->msi_priv;
    461  1.1  jmcneill 
    462  1.1  jmcneill 	const int msino = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    463  1.1  jmcneill 	const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? FDT_INTR_MPSAFE : 0;
    464  1.1  jmcneill 
    465  1.1  jmcneill 	KASSERT(sc->sc_msi_ih[msino] == NULL);
    466  1.1  jmcneill 	sc->sc_msi_ih[msino] = intr_establish_xname(sc->sc_msi_start + msino,
    467  1.1  jmcneill 	    ipl, IST_LEVEL | (mpsafe ? IST_MPSAFE : 0), func, arg, xname);
    468  1.1  jmcneill 
    469  1.1  jmcneill 	return sc->sc_msi_ih[msino];
    470  1.1  jmcneill }
    471  1.1  jmcneill 
    472  1.1  jmcneill static void
    473  1.1  jmcneill apple_pcie_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
    474  1.1  jmcneill     int count)
    475  1.1  jmcneill {
    476  1.1  jmcneill 	struct apple_pcie_softc * const sc = msi->msi_priv;
    477  1.1  jmcneill 	int n;
    478  1.1  jmcneill 
    479  1.1  jmcneill 	for (n = 0; n < count; n++) {
    480  1.1  jmcneill 		const int msino = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
    481  1.1  jmcneill 		if (pih[n] & ARM_PCI_INTR_MSIX)
    482  1.1  jmcneill 			apple_pcie_msi_msix_disable(sc, msino);
    483  1.1  jmcneill 		if (pih[n] & ARM_PCI_INTR_MSI)
    484  1.1  jmcneill 			apple_pcie_msi_msi_disable(sc, msino);
    485  1.1  jmcneill 		apple_pcie_msi_free_msi(sc, msino);
    486  1.1  jmcneill 		if (sc->sc_msi_ih[msino] != NULL) {
    487  1.1  jmcneill 			intr_disestablish(sc->sc_msi_ih[msino]);
    488  1.1  jmcneill 			sc->sc_msi_ih[msino] = NULL;
    489  1.1  jmcneill 		}
    490  1.1  jmcneill 	}
    491  1.1  jmcneill }
    492  1.1  jmcneill 
    493  1.1  jmcneill static int
    494  1.1  jmcneill apple_pcie_msi_init(struct apple_pcie_softc *sc)
    495  1.1  jmcneill {
    496  1.1  jmcneill 	struct arm_pci_msi *msi = &sc->sc_msi;
    497  1.1  jmcneill 	const int phandle = sc->sc_pcihost.sc_phandle;
    498  1.1  jmcneill 	u_int portno;
    499  1.1  jmcneill 	int len;
    500  1.1  jmcneill 
    501  1.1  jmcneill 	const u_int *data = fdtbus_get_prop(phandle, "msi-ranges", &len);
    502  1.1  jmcneill 	if (len != 8) {
    503  1.1  jmcneill 		aprint_error_dev(sc->sc_pcihost.sc_dev,
    504  1.1  jmcneill 		    "WARNING: bad msi-ranges property, MSI not enabled!\n");
    505  1.1  jmcneill 		return ENXIO;
    506  1.1  jmcneill 	}
    507  1.1  jmcneill 	sc->sc_msi_start = be32toh(data[0]);
    508  1.1  jmcneill 	sc->sc_nmsi = be32toh(data[1]);
    509  1.1  jmcneill 	sc->sc_msi_pa = kmem_zalloc(sizeof(*sc->sc_msi_pa) * sc->sc_nmsi,
    510  1.1  jmcneill 	    KM_SLEEP);
    511  1.1  jmcneill 	sc->sc_msi_ih = kmem_zalloc(sizeof(*sc->sc_msi_ih) * sc->sc_nmsi,
    512  1.1  jmcneill 	    KM_SLEEP);
    513  1.1  jmcneill 
    514  1.1  jmcneill 	if (of_getprop_uint64(phandle, "msi-doorbell", &sc->sc_msi_addr)) {
    515  1.1  jmcneill 		sc->sc_msi_addr = 0xffff000ULL;
    516  1.1  jmcneill 	}
    517  1.1  jmcneill 
    518  1.1  jmcneill 	for (portno = 0; portno < 3; portno++) {
    519  1.1  jmcneill 		apple_pcie_setup_port(sc, portno);
    520  1.1  jmcneill 	}
    521  1.1  jmcneill 
    522  1.1  jmcneill 	msi->msi_dev = sc->sc_pcihost.sc_dev;
    523  1.1  jmcneill 	msi->msi_priv = sc;
    524  1.1  jmcneill 	msi->msi_alloc = apple_pcie_msi_msi_alloc;
    525  1.1  jmcneill 	msi->msix_alloc = apple_pcie_msi_msix_alloc;
    526  1.1  jmcneill 	msi->msi_intr_establish = apple_pcie_msi_intr_establish;
    527  1.1  jmcneill 	msi->msi_intr_release = apple_pcie_msi_intr_release;
    528  1.1  jmcneill 
    529  1.1  jmcneill 	return arm_pci_msi_add(msi);
    530  1.1  jmcneill }
    531