iommu/riscv: Add page request queue and IOPF support
Subsystems (3)
PATCH 1/1 iommu/riscv: Add page request queue and IOPF support
From: "bingyu.xian" <[email protected]>
The RISC-V IOMMU Architecture Specification, Version 1.0, Chapter 3.3 defines a Page-request Queue (PQ) for handling PCIe Page Request Interface (PRI) messages. The current driver implements only the Command Queue (CQ) and Fault Queue (FQ); the PQ is left unimplemented, which is the biggest functional gap compared with the ARM SMMUv3 driver.
Add PQ and IOPF support:
- Initialize and enable the PQ when the hardware advertises ATS capability (RISCV_IOMMU_CAPABILITIES_ATS), and add an IOMMU_IOPF Kconfig dependency.
- Implement the PQ interrupt handler riscv_iommu_priq_process(), which consumes PQ records from the hardware ring buffer.
- Translate PQ records into the kernel's generic iopf_fault format and pass them to the IOPF framework via iommu_report_device_fault().
- Implement the .page_response callback, which builds an ATS.PRGR (Page Request Group Response) command and sends it through the command queue to notify the requesting device.
Tested on QEMU 10.0 with '-M virt,iommu-sys=on' and CONFIG_IOMMU_IOPF=y; the PQ comes up cleanly:
riscv,iommu 3010000.iommu: page request queue enabled
Test patch
==========
drivers/iommu/riscv/iommu.c +59 -0
@@ -16,6 +16,7 @@#include <linux/acpi_rimt.h> #include <linux/compiler.h> #include <linux/crash_dump.h> +#include <linux/debugfs.h> #include <linux/init.h> #include <linux/iommu.h> #include <linux/iopoll.h>
@@ -1608,9 +1609,62 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)return 0 ; } +#ifdef CONFIG_DEBUG_FS +/* +* debugfs interface to inject a fake PQ record without real PRI hardware. +* +* Writing a PCI BDF-encoded device id to the 'inject_pq' file constructs a +* synthetic riscv_iommu_pq_record and feeds it directly to +* riscv_iommu_handle_pq(), exercising the IOPF report path and, via the +* IOPF framework, the .page_response -> ATS.PRGR command path. +* +* The IRQ-driven priq_process() consumer loop is not exercised here, but it +* reuses the same queue infrastructure as the existing fltq_process(). +*/ +static struct dentry *riscv_iommu_debugfs_dir; + +static int riscv_iommu_inject_pq_set (void *data,u64 val) +{ +struct riscv_iommu_device *iommu = data; +struct riscv_iommu_pq_record fake_req; +unsigned int devid = (unsigned int )val; + +if (!(iommu->caps &RISCV_IOMMU_CAPABILITIES_ATS )) { +dev_err (iommu->dev,"inject_pq: ATS not supported\n" ); +return -ENODEV ; + } + +memset (&fake_req,0 ,sizeof (fake_req)); + fake_req.hdr =FIELD_PREP (RISCV_IOMMU_PQ_HDR_DID , devid); + fake_req.payload =FIELD_PREP (RISCV_IOMMU_PQ_PAYLOAD_ADDR ,0xdead0 ) | +FIELD_PREP (RISCV_IOMMU_PQ_PAYLOAD_PRGI ,1 ) | +RISCV_IOMMU_PQ_PAYLOAD_L | +RISCV_IOMMU_PQ_PAYLOAD_R ; + +dev_info (iommu->dev, +"inject_pq: injecting fake PQ record for devid 0x%x\n" , devid); +riscv_iommu_handle_pq (iommu, &fake_req); +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (riscv_iommu_inject_pq_fops,NULL , + riscv_iommu_inject_pq_set,"%llu\n" ); + +static void riscv_iommu_debugfs_init (struct riscv_iommu_device *iommu) +{ + riscv_iommu_debugfs_dir =debugfs_create_dir ("riscv-iommu" ,NULL ); +debugfs_create_file ("inject_pq" ,0200 , riscv_iommu_debugfs_dir, + iommu, &riscv_iommu_inject_pq_fops); +} + +static void riscv_iommu_debugfs_remove (void ) +{ +debugfs_remove_recursive (riscv_iommu_debugfs_dir); + riscv_iommu_debugfs_dir =NULL ; +} +#else +static inline void riscv_iommu_debugfs_init (struct riscv_iommu_device *iommu) { } +static inline void riscv_iommu_debugfs_remove (void ) { } +#endif /* CONFIG_DEBUG_FS */ +void riscv_iommu_remove (struct riscv_iommu_device *iommu)
{
+ riscv_iommu_debugfs_remove();
iommu_device_unregister(&iommu->iommu); iommu_device_sysfs_remove(&iommu->iommu); riscv_iommu_iodir_set_mode(iommu, RISCV_IOMMU_DDTP_IOMMU_MODE_OFF);
@@ -1754,6 +1808,8 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
goto err_remove_sysfs;
}
+ riscv_iommu_debugfs_init(iommu);
+
return 0;
err_remove_sysfs:
---
Test logs:
root@Ubuntu-riscv64:~# dmesg | grep "iommu" [ 0.590293] iommu: Default domain type: Translated [ 0.590379] iommu: DMA domain TLB invalidation policy: lazy mode [ 1.374106] riscv,iommu 3010000.iommu: failed to find an MSI domain [ 1.374429] riscv,iommu 3010000.iommu: using wire-signaled interrupts [ 1.384786] riscv,iommu 3010000.iommu: page request queue enabled [ 1.391279] pci 0000:00:00.0: Adding to iommu group 0 [ 1.391798] pci 0000:00:01.0: Adding to iommu group 1 [ 1.392023] pci 0000:00:02.0: Adding to iommu group 2 [ 1.392252] pci 0000:00:03.0: Adding to iommu group 3
root@Ubuntu-riscv64:~# echo 8 > /sys/kernel/debug/riscv-iommu/inject_pq
root@Ubuntu-riscv64:~# dmesg | grep "iommu" [ 0.590293] iommu: Default domain type: Translated [ 0.590379] iommu: DMA domain TLB invalidation policy: lazy mode [ 1.374106] riscv,iommu 3010000.iommu: failed to find an MSI domain [ 1.374429] riscv,iommu 3010000.iommu: using wire-signaled interrupts [ 1.384786] riscv,iommu 3010000.iommu: page request queue enabled [ 1.391279] pci 0000:00:00.0: Adding to iommu group 0 [ 1.391798] pci 0000:00:01.0: Adding to iommu group 1 [ 1.392023] pci 0000:00:02.0: Adding to iommu group 2 [ 1.392252] pci 0000:00:03.0: Adding to iommu group 3 [ 1308.301611] riscv,iommu 3010000.iommu: inject_pq: injecting fake PQ record for devid 0x8 [ 1308.303467] riscv,iommu 3010000.iommu: page request fault report failed: -22
--- drivers/iommu/riscv/Kconfig | 1 + drivers/iommu/riscv/iommu.c | 178 +++++++++++++++++++++++++++++++++++- drivers/iommu/riscv/iommu.h | 3 + 3 files changed, 181 insertions(+), 1 deletion(-)
drivers/iommu/riscv/Kconfig +1 -0
@@ -7,6 +7,7 @@ config RISCV_IOMMU depends on GENERIC_MSI_IRQ depends on (RISCV || COMPILE_TEST) && 64BIT select IOMMU_API + select IOMMU_IOPF select GENERIC_PT select IOMMU_PT select IOMMU_PT_RISCV64
drivers/iommu/riscv/iommu.c +145 -1
160 lines not on this page · raw message
unsigned int devid)
@@ -1404,6 +1550,7 @@ static const struct iommu_ops riscv_iommu_ops = {
.device_group = riscv_iommu_device_group, .probe_device = riscv_iommu_probe_device,
.release_device = riscv_iommu_release_device,
+ .page_response = riscv_iommu_page_response,
};
static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
@@ -1466,6 +1613,8 @@ void riscv_iommu_remove(struct riscv_iommu_device *iommu)
riscv_iommu_iodir_set_mode(iommu, RISCV_IOMMU_DDTP_IOMMU_MODE_OFF); riscv_iommu_queue_disable(&iommu->cmdq); riscv_iommu_queue_disable(&iommu->fltq);
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_ATS)
+ riscv_iommu_queue_disable(&iommu->priq);
}
int riscv_iommu_init(struct riscv_iommu_device *iommu)
@@ -1494,6 +1643,15 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
if (rc)
return rc;
+ /* Allocate page request queue if ATS is supported */
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_ATS) {
+ RISCV_IOMMU_QUEUE_INIT(&iommu->priq, PQ);
+ rc = riscv_iommu_queue_alloc(iommu, &iommu->priq,
+ sizeof(struct riscv_iommu_pq_record));
+ if (rc)
+ return rc;
+ }
+
rc = riscv_iommu_queue_enable(iommu, &iommu->cmdq, riscv_iommu_cmdq_process); if (rc)
return rc;
@@ -1502,6 +1660,15 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
if (rc)
goto err_queue_disable;
+ /* Enable page request queue if ATS is supported */
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_ATS) {
+ rc = riscv_iommu_queue_enable(iommu, &iommu->priq,
+ riscv_iommu_priq_process);
+ if (rc)
+ goto err_queue_disable;
+ dev_info(iommu->dev, "page request queue enabled\n");
+ }
+
rc = riscv_iommu_iodir_set_mode(iommu, RISCV_IOMMU_DDTP_IOMMU_MODE_MAX); if (rc)
goto err_queue_disable;
@@ -1534,6 +1701,8 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
err_iodir_off:
riscv_iommu_iodir_set_mode(iommu, RISCV_IOMMU_DDTP_IOMMU_MODE_OFF);
err_queue_disable:
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_ATS)
+ riscv_iommu_queue_disable(&iommu->priq);
riscv_iommu_queue_disable(&iommu->fltq); riscv_iommu_queue_disable(&iommu->cmdq);
return rc;
drivers/iommu/riscv/iommu.h +3 -0
@@ -14,6 +14,8 @@#include <linux/iommu.h> #include <linux/types.h> #include <linux/iopoll.h> +#include <linux/pci.h> +#include <linux/pci-ats.h> #include "iommu-bits.h"
@@ -55,6 +57,7 @@ struct riscv_iommu_device {/* hardware queues */ struct riscv_iommu_queue cmdq;struct riscv_iommu_queue fltq; +struct riscv_iommu_queue priq;/* device directory */ unsigned int ddt_mode;
--
2.53.0