git: b08d332da035 - main - devices: report iommu data for the device in the dev. sysctl tree

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Thu, 17 Oct 2024 14:32:16 UTC
The branch main has been updated by kib:

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

commit b08d332da035e11bfc9373f2d08244cb3e10cda4
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-10-12 22:54:33 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-10-17 14:31:50 +0000

    devices: report iommu data for the device in the dev. sysctl tree
    
    Show the iommu unit' device name/unit which translates the device'
    requests, if any, as parsed out from the BIOS tables.
    Also show the rid value for the device.
    
    Sponsored by:   Advanced Micro Devices (AMD)
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D47098
---
 sys/kern/subr_bus.c         | 28 ++++++++++++++++++++++++++++
 sys/sys/bus.h               |  1 +
 sys/x86/iommu/intel_drv.c   | 19 ++++++++++++++++---
 sys/x86/iommu/iommu_utils.c | 13 +++++++++++++
 sys/x86/iommu/x86_iommu.h   |  1 +
 5 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
index 26c476e9fef6..70e5e22854f3 100644
--- a/sys/kern/subr_bus.c
+++ b/sys/kern/subr_bus.c
@@ -29,6 +29,7 @@
 #include <sys/cdefs.h>
 #include "opt_bus.h"
 #include "opt_ddb.h"
+#include "opt_iommu.h"
 
 #include <sys/param.h>
 #include <sys/conf.h>
@@ -60,6 +61,8 @@
 #include <vm/uma.h>
 #include <vm/vm.h>
 
+#include <dev/iommu/iommu.h>
+
 #include <ddb/ddb.h>
 
 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
@@ -261,6 +264,7 @@ enum {
 	DEVICE_SYSCTL_LOCATION,
 	DEVICE_SYSCTL_PNPINFO,
 	DEVICE_SYSCTL_PARENT,
+	DEVICE_SYSCTL_IOMMU,
 };
 
 static int
@@ -268,7 +272,10 @@ device_sysctl_handler(SYSCTL_HANDLER_ARGS)
 {
 	struct sbuf sb;
 	device_t dev = (device_t)arg1;
+	device_t iommu;
 	int error;
+	uint16_t rid;
+	const char *c;
 
 	sbuf_new_for_sysctl(&sb, NULL, 1024, req);
 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
@@ -289,6 +296,22 @@ device_sysctl_handler(SYSCTL_HANDLER_ARGS)
 	case DEVICE_SYSCTL_PARENT:
 		sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : "");
 		break;
+	case DEVICE_SYSCTL_IOMMU:
+		iommu = NULL;
+		error = device_get_prop(dev, DEV_PROP_NAME_IOMMU,
+		    (void **)&iommu);
+		c = "";
+		if (error == 0 && iommu != NULL) {
+			sbuf_printf(&sb, "unit=%s", device_get_nameunit(iommu));
+			c = " ";
+		}
+		rid = 0;
+#ifdef IOMMU
+		iommu_get_requester(dev, &rid);
+#endif
+		if (rid != 0)
+			sbuf_printf(&sb, "%srid=%#x", c, rid);
+		break;
 	default:
 		error = EINVAL;
 		goto out;
@@ -338,6 +361,11 @@ device_sysctl_init(device_t dev)
 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
 	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
 	    "parent device");
+	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
+	    OID_AUTO, "%iommu",
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
+	    dev, DEVICE_SYSCTL_IOMMU, device_sysctl_handler, "A",
+	    "iommu unit handling the device requests");
 	if (bus_get_domain(dev, &domain) == 0)
 		SYSCTL_ADD_INT(&dev->sysctl_ctx,
 		    SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
diff --git a/sys/sys/bus.h b/sys/sys/bus.h
index 49c5d62cebce..4f34990cd10f 100644
--- a/sys/sys/bus.h
+++ b/sys/sys/bus.h
@@ -924,6 +924,7 @@ device_location_cache_t *dev_wired_cache_init(void);
 void dev_wired_cache_fini(device_location_cache_t *dcp);
 bool dev_wired_cache_match(device_location_cache_t *dcp, device_t dev, const char *at);
 
+#define	DEV_PROP_NAME_IOMMU	"iommu-unit"
 typedef void (*device_prop_dtr_t)(device_t dev, const char *name, void *val,
     void *dtr_ctx);
 int device_set_prop(device_t dev, const char *name, void *val,
diff --git a/sys/x86/iommu/intel_drv.c b/sys/x86/iommu/intel_drv.c
index ebc77879480b..f4a1ec06b721 100644
--- a/sys/x86/iommu/intel_drv.c
+++ b/sys/x86/iommu/intel_drv.c
@@ -757,6 +757,7 @@ dmar_find(device_t dev, bool verbose)
 		dmar_print_path(dev_busno, dev_path_len, dev_path);
 		printf("\n");
 	}
+	iommu_device_set_iommu_prop(dev, unit->iommu.dev);
 	return (unit);
 }
 
@@ -826,16 +827,28 @@ dmar_find_nonpci(u_int id, u_int entry_type, uint16_t *rid)
 struct dmar_unit *
 dmar_find_hpet(device_t dev, uint16_t *rid)
 {
+	struct dmar_unit *unit;
 
-	return (dmar_find_nonpci(hpet_get_uid(dev), ACPI_DMAR_SCOPE_TYPE_HPET,
-	    rid));
+	unit = dmar_find_nonpci(hpet_get_uid(dev), ACPI_DMAR_SCOPE_TYPE_HPET,
+	    rid);
+	if (unit != NULL)
+		iommu_device_set_iommu_prop(dev, unit->iommu.dev);
+	return (unit);
 }
 
 struct dmar_unit *
 dmar_find_ioapic(u_int apic_id, uint16_t *rid)
 {
+	struct dmar_unit *unit;
+	device_t apic_dev;
 
-	return (dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid));
+	unit = dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid);
+	if (unit != NULL) {
+		apic_dev = ioapic_get_dev(apic_id);
+		if (apic_dev != NULL)
+			iommu_device_set_iommu_prop(apic_dev, unit->iommu.dev);
+	}
+	return (unit);
 }
 
 struct rmrr_iter_args {
diff --git a/sys/x86/iommu/iommu_utils.c b/sys/x86/iommu/iommu_utils.c
index 259c87403b07..479e8637ee8e 100644
--- a/sys/x86/iommu/iommu_utils.c
+++ b/sys/x86/iommu/iommu_utils.c
@@ -758,6 +758,19 @@ pglvl_page_size(int total_pglvl, int lvl)
 	return (pg_sz[rlvl]);
 }
 
+void
+iommu_device_set_iommu_prop(device_t dev, device_t iommu)
+{
+	device_t iommu_dev;
+	int error;
+
+	bus_topo_lock();
+	error = device_get_prop(dev, DEV_PROP_NAME_IOMMU, (void **)&iommu_dev);
+	if (error == ENOENT)
+		device_set_prop(dev, DEV_PROP_NAME_IOMMU, iommu, NULL, NULL);
+	bus_topo_unlock();
+}
+
 #ifdef DDB
 #include <ddb/ddb.h>
 #include <ddb/db_lex.h>
diff --git a/sys/x86/iommu/x86_iommu.h b/sys/x86/iommu/x86_iommu.h
index 92ac993e7c9c..eb4a9907a5d6 100644
--- a/sys/x86/iommu/x86_iommu.h
+++ b/sys/x86/iommu/x86_iommu.h
@@ -188,6 +188,7 @@ int iommu_alloc_irq(struct iommu_unit *unit, int idx);
 void iommu_release_intr(struct iommu_unit *unit, int idx);
 
 void iommu_device_tag_init(struct iommu_ctx *ctx, device_t dev);
+void iommu_device_set_iommu_prop(device_t dev, device_t iommu);
 
 int pglvl_pgtbl_pte_off(int pglvl, iommu_gaddr_t base, int lvl);
 vm_pindex_t pglvl_pgtbl_get_pindex(int pglvl, iommu_gaddr_t base, int lvl);