[PATCH v12 2/2] iommu/riscv: create a auxiliary device for HPM
From: Zong Li
Date: Fri Sep 18 2026 - 03:21:17 EST
Create an auxiliary device for HPM when the IOMMU supports a
hardware performance monitor.
Tested-by: Chen Pei <cp0613@xxxxxxxxxxxxxxxxx>
Tested-by: Fangyu Yu <fangyu.yu@xxxxxxxxxxxxxxxxx>
Reviewed-by: Nutty Liu <nutty.liu@xxxxxxxxxxx>
Reviewed-by: Guo Ren <guoren@xxxxxxxxxx>
Reviewed-by: Yicong Yang <yang.yicong@xxxxxxxxxxxxx>
Suggested-by: Samuel Holland <samuel.holland@xxxxxxxxxx>
Signed-off-by: Zong Li <zong.li@xxxxxxxxxx>
---
drivers/iommu/riscv/Kconfig | 1 +
drivers/iommu/riscv/iommu.c | 48 +++++++++++++++++++++++++++++++++++++
drivers/iommu/riscv/iommu.h | 4 ++++
3 files changed, 53 insertions(+)
diff --git a/drivers/iommu/riscv/Kconfig b/drivers/iommu/riscv/Kconfig
index b86e5ab94183..8025bf0fb67f 100644
--- a/drivers/iommu/riscv/Kconfig
+++ b/drivers/iommu/riscv/Kconfig
@@ -10,6 +10,7 @@ config RISCV_IOMMU
select GENERIC_PT
select IOMMU_PT
select IOMMU_PT_RISCV64
+ select AUXILIARY_BUS
help
Support for implementations of the RISC-V IOMMU architecture that
complements the RISC-V MMU capabilities, providing similar address
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index fe8e6d0f8a23..3c8f8adba647 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -14,6 +14,7 @@
#include <linux/acpi.h>
#include <linux/acpi_rimt.h>
+#include <linux/auxiliary_bus.h>
#include <linux/compiler.h>
#include <linux/crash_dump.h>
#include <linux/init.h>
@@ -48,6 +49,9 @@
static DEFINE_IDA(riscv_iommu_pscids);
#define RISCV_IOMMU_MAX_PSCID (BIT(20) - 1)
+/* IOMMU PMU auxiliary device id allocation namespace. */
+static DEFINE_IDA(riscv_iommu_pmu_ida);
+
/* Device resource-managed allocations */
struct riscv_iommu_devres {
void *addr;
@@ -587,6 +591,46 @@ static irqreturn_t riscv_iommu_fltq_process(int irq, void *data)
return IRQ_HANDLED;
}
+/*
+ * IOMMU Hardware performance monitor
+ */
+static void riscv_iommu_pmu_id_free(void *data)
+{
+ ida_free(&riscv_iommu_pmu_ida, (unsigned long)data);
+}
+
+static int riscv_iommu_hpm_enable(struct riscv_iommu_device *iommu)
+{
+ struct auxiliary_device *auxdev;
+ int id, ret;
+
+ id = ida_alloc(&riscv_iommu_pmu_ida, GFP_KERNEL);
+ if (id < 0)
+ return id;
+
+ ret = devm_add_action_or_reset(iommu->dev, riscv_iommu_pmu_id_free,
+ (void *)(unsigned long)id);
+ if (ret)
+ return ret;
+
+ auxdev = auxiliary_device_create(iommu->dev, "riscv-iommu",
+ "pmu", iommu, id);
+ if (!auxdev)
+ return -ENODEV;
+
+ iommu->pmu_dev = auxdev;
+ return 0;
+}
+
+static void riscv_iommu_hpm_disable(struct riscv_iommu_device *iommu)
+{
+ if (!iommu->pmu_dev)
+ return;
+
+ auxiliary_device_destroy(iommu->pmu_dev);
+ iommu->pmu_dev = NULL;
+}
+
/* Lookup and initialize device context info structure. */
static struct riscv_iommu_dc *riscv_iommu_get_dc(struct riscv_iommu_device *iommu,
unsigned int devid)
@@ -1570,6 +1614,7 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
void riscv_iommu_remove(struct riscv_iommu_device *iommu)
{
+ riscv_iommu_hpm_disable(iommu);
iommu_device_unregister(&iommu->iommu);
iommu_device_sysfs_remove(&iommu->iommu);
riscv_iommu_iodir_set_mode(iommu, RISCV_IOMMU_DDTP_IOMMU_MODE_OFF);
@@ -1636,6 +1681,9 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
goto err_remove_sysfs;
}
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_HPM)
+ riscv_iommu_hpm_enable(iommu);
+
return 0;
err_remove_sysfs:
diff --git a/drivers/iommu/riscv/iommu.h b/drivers/iommu/riscv/iommu.h
index 5676001548cc..7fcd79fa47c8 100644
--- a/drivers/iommu/riscv/iommu.h
+++ b/drivers/iommu/riscv/iommu.h
@@ -19,6 +19,7 @@
#include "iommu-bits.h"
struct riscv_iommu_device;
+struct auxiliary_device;
struct riscv_iommu_queue {
atomic_t prod; /* unbounded producer allocation index */
@@ -62,6 +63,9 @@ struct riscv_iommu_device {
unsigned int ddt_mode;
dma_addr_t ddt_phys;
u64 *ddt_root;
+
+ /* PMU auxiliary device; torn down explicitly before MSI teardown */
+ struct auxiliary_device *pmu_dev;
};
int riscv_iommu_init(struct riscv_iommu_device *iommu);
--
2.43.7