[RFC PATCH v2 3/4] iommu/riscv: Expose global QoS IDs in sysfs
From: Zhanpeng Zhang
Date: Mon Nov 16 2026 - 02:14:33 EST
Expose the per-IOMMU global RCID and MCID as separate attributes in the
IOMMU class device's qosid group. These IDs label IOMMU-originated
directory and queue accesses, and device requests in DDTP BARE mode.
Initialize the global IDs to zero before enabling translation. Preserve
the other field and reserved bits on each write, serialize updates, and
reject values that the WARL fields do not retain. Keep this interface
independent of resctrl device assignment and available without resctrl.
Document the attributes and add a maintainer entry for the new ABI and
device-assignment files.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
.../ABI/testing/sysfs-class-iommu-riscv-iommu | 27 ++++
MAINTAINERS | 7 +
drivers/iommu/riscv/iommu.c | 135 +++++++++++++++++-
3 files changed, 164 insertions(+), 5 deletions(-)
create mode 100644 Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
diff --git a/Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu b/Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
new file mode 100644
index 000000000000..4658cbcd625b
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
@@ -0,0 +1,27 @@
+What: /sys/class/iommu/<iommu>/qosid/rcid
+What: /sys/class/iommu/<iommu>/qosid/mcid
+Date: October 2026
+Contact: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
+Description:
+ The global default QoS IDs for a RISC-V IOMMU. These files
+ are present only when the IOMMU reports the QOSID capability.
+ Reading rcid or mcid returns the corresponding iommu_qosid
+ register field as a decimal number followed by a newline.
+
+ Writing a decimal number updates only the corresponding
+ field, preserving the other ID and reserved/WPRI bits. The
+ two writes are independent, not an atomic pair update.
+ Malformed input fails with EINVAL. Values outside
+ the 12-bit field range fail with ERANGE. If WARL readback
+ does not retain a value, the old register value is restored
+ and the write fails with ERANGE. Initialization sets both
+ IDs to zero.
+
+ These IDs tag IOMMU-originated DDT, CQ, FQ, PQ and MSI
+ accesses. In DDTP BARE mode they also tag device-originated
+ requests. In translated modes, device-specific requests
+ instead use the device context's QoS IDs.
+
+ This interface does not allocate resctrl IDs or configure
+ downstream resource controllers. The administrator must
+ choose IDs appropriate for the platform's QoS policy.
diff --git a/MAINTAINERS b/MAINTAINERS
index df051086b5dd..e14fddd07882 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -24072,6 +24072,13 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
F: Documentation/devicetree/bindings/iommu/riscv,iommu.yaml
F: drivers/iommu/riscv/
+RISC-V IOMMU QoS
+M: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
+L: iommu@xxxxxxxxxxxxxxx
+L: linux-riscv@xxxxxxxxxxxxxxxxxxx
+S: Maintained
+F: Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
+
RISC-V MICROCHIP SUPPORT
M: Conor Dooley <conor.dooley@xxxxxxxxxxxxx>
R: Valentina Fernandez <valentina.fernandezalanis@xxxxxxxxxxxxx>
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index dd2ebb25312d..b1b069bb2ad8 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -23,6 +23,7 @@
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/resctrl_device.h>
+#include <linux/string.h>
#include <linux/generic_pt/iommu.h>
#include "../iommu-pages.h"
@@ -1777,9 +1778,124 @@ static void riscv_iommu_probe_qosid_limits(struct riscv_iommu_device *iommu)
dev_warn(iommu->dev, "QoS ID limits unavailable; device QoS disabled\n");
}
+static int riscv_iommu_update_qosid(struct riscv_iommu_device *iommu,
+ u32 mask, u32 value)
+{
+ u32 old_qosid, qosid;
+ int ret = 0;
+
+ mutex_lock(&iommu->qosid_lock);
+ old_qosid = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ qosid = (old_qosid & ~mask) | value;
+ riscv_iommu_writel(iommu, RISCV_IOMMU_REG_IOMMU_QOSID, qosid);
+
+ /* Reject values that the WARL fields do not retain. */
+ qosid = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ if ((qosid & mask) != value) {
+ riscv_iommu_writel(iommu, RISCV_IOMMU_REG_IOMMU_QOSID,
+ old_qosid);
+ riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ ret = -ERANGE;
+ }
+ mutex_unlock(&iommu->qosid_lock);
+ return ret;
+}
+
+static ssize_t riscv_iommu_qosid_show(struct device *dev, char *buf, bool rcid)
+{
+ struct iommu_device *idev = dev_to_iommu_device(dev);
+ struct riscv_iommu_device *iommu;
+ u32 qosid;
+
+ if (!idev)
+ return -ENODEV;
+ iommu = container_of(idev, struct riscv_iommu_device, iommu);
+ mutex_lock(&iommu->qosid_lock);
+ qosid = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ mutex_unlock(&iommu->qosid_lock);
+ return sysfs_emit(buf, "%u\n", rcid ?
+ (u32)FIELD_GET(RISCV_IOMMU_IOMMU_QOSID_RCID, qosid) :
+ (u32)FIELD_GET(RISCV_IOMMU_IOMMU_QOSID_MCID, qosid));
+}
+
+static ssize_t riscv_iommu_qosid_store(struct device *dev, const char *buf,
+ size_t count, bool rcid)
+{
+ struct iommu_device *idev = dev_to_iommu_device(dev);
+ struct riscv_iommu_device *iommu;
+ u32 mask, value;
+ int ret;
+
+ if (!idev)
+ return -ENODEV;
+ if (memchr(buf, '\0', count))
+ return -EINVAL;
+ ret = kstrtou32(buf, 10, &value);
+ if (ret)
+ return ret;
+ if (rcid) {
+ if (value > FIELD_MAX(RISCV_IOMMU_IOMMU_QOSID_RCID))
+ return -ERANGE;
+ mask = RISCV_IOMMU_IOMMU_QOSID_RCID;
+ value = FIELD_PREP(RISCV_IOMMU_IOMMU_QOSID_RCID, value);
+ } else {
+ if (value > FIELD_MAX(RISCV_IOMMU_IOMMU_QOSID_MCID))
+ return -ERANGE;
+ mask = RISCV_IOMMU_IOMMU_QOSID_MCID;
+ value = FIELD_PREP(RISCV_IOMMU_IOMMU_QOSID_MCID, value);
+ }
+
+ iommu = container_of(idev, struct riscv_iommu_device, iommu);
+ ret = riscv_iommu_update_qosid(iommu, mask, value);
+ return ret ? ret : count;
+}
+
+static ssize_t rcid_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return riscv_iommu_qosid_show(dev, buf, true);
+}
+
+static ssize_t rcid_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ return riscv_iommu_qosid_store(dev, buf, count, true);
+}
+static DEVICE_ATTR_RW(rcid);
+
+static ssize_t mcid_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return riscv_iommu_qosid_show(dev, buf, false);
+}
+
+static ssize_t mcid_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ return riscv_iommu_qosid_store(dev, buf, count, false);
+}
+static DEVICE_ATTR_RW(mcid);
+
+static struct attribute *riscv_iommu_attrs[] = {
+ &dev_attr_rcid.attr,
+ &dev_attr_mcid.attr,
+ NULL,
+};
+
+static const struct attribute_group riscv_iommu_group = {
+ .name = "qosid",
+ .attrs = riscv_iommu_attrs,
+};
+
+static const struct attribute_group *riscv_iommu_groups[] = {
+ &riscv_iommu_group,
+ NULL,
+};
+
static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
{
u64 ddtp;
+ int ret;
/*
* Make sure the IOMMU is switched off or in pass-through mode during
@@ -1810,9 +1926,15 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
return -EINVAL;
}
- if (IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS) &&
- (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID))
- riscv_iommu_probe_qosid_limits(iommu);
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID) {
+ if (IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS))
+ riscv_iommu_probe_qosid_limits(iommu);
+ ret = riscv_iommu_update_qosid(iommu,
+ RISCV_IOMMU_IOMMU_QOSID_RCID |
+ RISCV_IOMMU_IOMMU_QOSID_MCID, 0);
+ if (ret)
+ return ret;
+ }
/*
* Distribute interrupt vectors, always use first vector for CIV.
@@ -1847,6 +1969,7 @@ void riscv_iommu_remove(struct riscv_iommu_device *iommu)
int riscv_iommu_init(struct riscv_iommu_device *iommu)
{
+ const struct attribute_group **sysfs_groups = NULL;
int rc;
RISCV_IOMMU_QUEUE_INIT(&iommu->cmdq, CQ);
@@ -1887,8 +2010,10 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
if (rc)
goto err_queue_disable;
- rc = iommu_device_sysfs_add(&iommu->iommu, NULL, NULL, "riscv-iommu@%s",
- dev_name(iommu->dev));
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID)
+ sysfs_groups = riscv_iommu_groups;
+ rc = iommu_device_sysfs_add(&iommu->iommu, NULL, sysfs_groups,
+ "riscv-iommu@%s", dev_name(iommu->dev));
if (rc) {
dev_err_probe(iommu->dev, rc, "cannot register sysfs interface\n");
goto err_iodir_off;
--
2.50.1 (Apple Git-155)