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