Module Name:    src
Committed By:   jmcneill
Date:           Sat Sep  4 12:35:31 UTC 2021

Modified Files:
        src/sys/arch/arm/apple: apple_dart.c apple_pcie.c

Log Message:
Use fdt_iommu instead of calling appledart directly from applepcie


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/arm/apple/apple_dart.c \
    src/sys/arch/arm/apple/apple_pcie.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/arm/apple/apple_dart.c
diff -u src/sys/arch/arm/apple/apple_dart.c:1.1 src/sys/arch/arm/apple/apple_dart.c:1.2
--- src/sys/arch/arm/apple/apple_dart.c:1.1	Mon Aug 30 23:26:26 2021
+++ src/sys/arch/arm/apple/apple_dart.c	Sat Sep  4 12:35:31 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: apple_dart.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $ */
+/* $NetBSD: apple_dart.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2021 Mark Kettenis <kette...@openbsd.org>
@@ -20,7 +20,7 @@
 //#define APPLE_DART_DEBUG
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: apple_dart.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: apple_dart.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -36,46 +36,6 @@ __KERNEL_RCSID(0, "$NetBSD: apple_dart.c
 #include <dev/fdt/fdtvar.h>
 
 /*
- * DT node to bus_dma tag mappings
- */
-
-bus_dma_tag_t apple_dart_iommu_lookup(int);
-
-struct apple_dart_iommu {
-	int phandle;
-	bus_dma_tag_t dmat;
-	LIST_ENTRY(apple_dart_iommu) next;
-};
-
-static LIST_HEAD(, apple_dart_iommu) apple_dart_iommus =
-    LIST_HEAD_INITIALIZER(apple_dart_iommus);
-
-static void
-apple_dart_iommu_register(int phandle, bus_dma_tag_t dmat)
-{
-	struct apple_dart_iommu *iommu;
-
-	iommu = kmem_alloc(sizeof(*iommu), KM_SLEEP);
-	iommu->phandle = phandle;
-	iommu->dmat = dmat;
-	LIST_INSERT_HEAD(&apple_dart_iommus, iommu, next);
-}
-
-bus_dma_tag_t
-apple_dart_iommu_lookup(int phandle)
-{
-	struct apple_dart_iommu *iommu;
-
-	LIST_FOREACH(iommu, &apple_dart_iommus, next) {
-		if (iommu->phandle == phandle) {
-			return iommu->dmat;
-		}
-	}
-
-	panic("Couldn't find IOMMU for node 0x%x", phandle);
-}
-
-/*
  * DART registers
  */
 #define	DART_TLB_OP		0x0020
@@ -473,6 +433,18 @@ apple_dart_dmamap_unload(bus_dma_tag_t t
 	sc->sc_dmat->_dmamap_unload(sc->sc_dmat, map);
 }
 
+static bus_dma_tag_t
+apple_dart_iommu_map(device_t dev, const u_int *data, bus_dma_tag_t dmat)
+{
+	struct apple_dart_softc * const sc = device_private(dev);
+
+	return &sc->sc_bus_dmat;
+}
+
+const struct fdtbus_iommu_func apple_dart_iommu_funcs = {
+	.map = apple_dart_iommu_map,
+};
+
 static int
 apple_dart_match(device_t parent, cfdata_t cf, void *aux)
 {
@@ -623,7 +595,7 @@ apple_dart_attach(device_t parent, devic
 	sc->sc_bus_dmat._dmamap_load_raw = apple_dart_dmamap_load_raw;
 	sc->sc_bus_dmat._dmamap_unload = apple_dart_dmamap_unload;
 
-	apple_dart_iommu_register(phandle, &sc->sc_bus_dmat);
+	fdtbus_register_iommu(self, phandle, &apple_dart_iommu_funcs);
 }
 
 CFATTACH_DECL_NEW(apple_dart, sizeof(struct apple_dart_softc),
Index: src/sys/arch/arm/apple/apple_pcie.c
diff -u src/sys/arch/arm/apple/apple_pcie.c:1.1 src/sys/arch/arm/apple/apple_pcie.c:1.2
--- src/sys/arch/arm/apple/apple_pcie.c:1.1	Mon Aug 30 23:26:26 2021
+++ src/sys/arch/arm/apple/apple_pcie.c	Sat Sep  4 12:35:31 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: apple_pcie.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $ */
+/* $NetBSD: apple_pcie.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2021 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: apple_pcie.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: apple_pcie.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -52,9 +52,6 @@ __KERNEL_RCSID(0, "$NetBSD: apple_pcie.c
 #define	PCIE_MSI_REMAP		0x0128
 #define	PCIE_MSI_DOORBELL	0x0168
 
-/* XXX apple_dart.c */
-extern bus_dma_tag_t apple_dart_iommu_lookup(int);
-
 struct apple_pcie_softc {
 	struct pcihost_softc	sc_pcihost;
 
@@ -174,36 +171,15 @@ apple_pcie_attach_hook(device_t parent, 
 {
 	struct apple_pcie_softc *sc = pba->pba_pc->pc_conf_v;
 	const int phandle = sc->sc_pcihost.sc_phandle;
-	const u_int *iommu_map;
-	int len;
+	bus_dma_tag_t dmat;
 
 	KASSERT(device_is_a(sc->sc_pcihost.sc_dev, "applepcie"));
 
-	iommu_map = fdtbus_get_prop(phandle, "iommu-map", &len);
-	if (iommu_map == NULL) {
-		panic("%s: no iommu-map?!",
-		    device_xname(sc->sc_pcihost.sc_dev));
-		return;
-	}
-
-	while (len >= 16) {
-		const u_int ridbase = be32toh(iommu_map[0]);
-		const u_int xref = fdtbus_get_phandle_from_native(
-		    be32toh(iommu_map[1]));
-
-		const int bus = (ridbase >> 8) & 0xff;
-		if (bus == pba->pba_bus) {
-			pba->pba_dmat = apple_dart_iommu_lookup(xref);
-			pba->pba_dmat64 = pba->pba_dmat;
-			return;
-		}
-
-		iommu_map += 4;
-		len -= 16;
-	}
+	/* XXX this should be per-device, not per-bus */
+	const uint32_t rid = pba->pba_bus << 8;
 
-	//panic("no iommu for bus %d\n", pba->pba_bus);
-	pba->pba_dmat = pba->pba_dmat64 = sc->sc_pcihost.sc_dmat;
+	dmat = fdtbus_iommu_map_pci(phandle, rid, sc->sc_pcihost.sc_dmat);
+	pba->pba_dmat = pba->pba_dmat64 = dmat;
 }
 
 static int

Reply via email to