git: b7fca06bbf3f - main - Split-out SID-getting code to a separate functions. Also ensure we are dealing with PCI-class devices.

From: Ruslan Bukin <br_at_FreeBSD.org>
Date: Mon, 09 May 2022 19:14:17 UTC
The branch main has been updated by br:

URL: https://cgit.FreeBSD.org/src/commit/?id=b7fca06bbf3f24313cb513842ad0078df43d211a

commit b7fca06bbf3f24313cb513842ad0078df43d211a
Author:     Ruslan Bukin <br@FreeBSD.org>
AuthorDate: 2022-05-09 19:08:01 +0000
Commit:     Ruslan Bukin <br@FreeBSD.org>
CommitDate: 2022-05-09 19:13:57 +0000

    Split-out SID-getting code to a separate functions.
    Also ensure we are dealing with PCI-class devices.
    
    Sponsored by:   UKRI
---
 sys/arm64/iommu/smmu.c | 57 ++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 13 deletions(-)

diff --git a/sys/arm64/iommu/smmu.c b/sys/arm64/iommu/smmu.c
index e1197db34375..20971a2d7e41 100644
--- a/sys/arm64/iommu/smmu.c
+++ b/sys/arm64/iommu/smmu.c
@@ -1760,6 +1760,42 @@ smmu_set_buswide(device_t dev, struct smmu_domain *domain,
 	return (0);
 }
 
+#ifdef DEV_ACPI
+static int
+smmu_pci_get_sid_acpi(device_t child, u_int *sid0)
+{
+	uint16_t rid;
+	u_int xref;
+	int seg;
+	int err;
+	int sid;
+
+	seg = pci_get_domain(child);
+	rid = pci_get_rid(child);
+
+	err = acpi_iort_map_pci_smmuv3(seg, rid, &xref, &sid);
+	if (err == 0)
+		*sid0 = sid;
+
+	return (err);
+}
+#endif
+
+#ifdef FDT
+static int
+smmu_pci_get_sid_fdt(device_t child, u_int *sid0)
+{
+	struct pci_id_ofw_iommu pi;
+	int err;
+
+	err = pci_get_id(child, PCI_ID_OFW_IOMMU, (uintptr_t *)&pi);
+	if (err == 0)
+		*sid0 = pi.id;
+
+	return (err);
+}
+#endif
+
 static struct iommu_ctx *
 smmu_ctx_alloc(device_t dev, struct iommu_domain *iodom, device_t child,
     bool disabled)
@@ -1767,28 +1803,23 @@ smmu_ctx_alloc(device_t dev, struct iommu_domain *iodom, device_t child,
 	struct smmu_domain *domain;
 	struct smmu_softc *sc;
 	struct smmu_ctx *ctx;
-#ifdef DEV_ACPI
-	uint16_t rid;
-	u_int xref;
-	int seg;
-#else
-	struct pci_id_ofw_iommu pi;
-#endif
+	devclass_t pci_class;
 	u_int sid;
 	int err;
 
 	sc = device_get_softc(dev);
 	domain = (struct smmu_domain *)iodom;
 
+	pci_class = devclass_find("pci");
+	if (device_get_devclass(device_get_parent(child)) != pci_class)
+		return (NULL);
+
 #ifdef DEV_ACPI
-	seg = pci_get_domain(child);
-	rid = pci_get_rid(child);
-	err = acpi_iort_map_pci_smmuv3(seg, rid, &xref, &sid);
+	err = smmu_pci_get_sid_acpi(child, &sid);
 #else
-	err = pci_get_id(child, PCI_ID_OFW_IOMMU, (uintptr_t *)&pi);
-	sid = pi.id;
+	err = smmu_pci_get_sid_fdt(child, &sid);
 #endif
-	if (err != 0)
+	if (err)
 		return (NULL);
 
 	if (sc->features & SMMU_FEATURE_2_LVL_STREAM_TABLE) {