[PATCH] target/riscv/kvm: Translate MSI addresses through the IOMMU

Tan Chi posted 1 patch 4 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261025121334.40041-1-tanchi25@mails.ucas.ac.cn
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
There is a newer version of this series
target/riscv/kvm/kvm-cpu.c | 45 ++++++++++++++++++++++++++++++++++++++
target/riscv/trace-events  |  3 +++
2 files changed, 48 insertions(+)
[PATCH] target/riscv/kvm: Translate MSI addresses through the IOMMU
Posted by Tan Chi 4 weeks ago
KVM irqfd bypasses the PCI device's IOMMU AddressSpace, so MSI
addresses requiring translation may fail to reach the guest IMSIC.

Translate MSI addresses through the device's IOMMU AddressSpace
when setting up KVM routes. Only direct IMSIC mappings are
supported; Basic MSI PTE and MRIF remapping are not.

Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---

The AddressSpace translation follows the existing
kvm_arch_fixup_msi_route() implementation on ARM.

 target/riscv/kvm/kvm-cpu.c | 45 ++++++++++++++++++++++++++++++++++++++
 target/riscv/trace-events  |  3 +++
 2 files changed, 48 insertions(+)

diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
index 45ad5e1525..565cdec9d2 100644
--- a/target/riscv/kvm/kvm-cpu.c
+++ b/target/riscv/kvm/kvm-cpu.c
@@ -1507,6 +1507,51 @@ int kvm_arch_release_virq_post(int virq)
 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
                              uint64_t address, uint32_t data, PCIDevice *dev)
 {
+    AddressSpace *as;
+    hwaddr xlat, len = sizeof(uint32_t), translated;
+    MemoryRegionSection section;
+    MemoryRegion *mr;
+    Object *owner;
+
+    if (!dev) {
+        return 0;
+    }
+
+    as = pci_device_iommu_address_space(dev);
+    if (as == &address_space_memory) {
+        return 0;
+    }
+
+    RCU_READ_LOCK_GUARD();
+
+    mr = address_space_translate(as, address, &xlat, &len, true,
+                                 MEMTXATTRS_UNSPECIFIED);
+
+    if (!mr) {
+        return 1;
+    }
+
+    /*
+     * Only direct IMSIC writes can be represented by a KVM MSI route.
+     * MSI page-table translations instead target the RISC-V IOMMU trap
+     * region, which requires further BASIC/MRIF processing.
+     */
+    owner = memory_region_owner(mr);
+    if (!owner || !object_dynamic_cast(owner, TYPE_RISCV_IMSIC)) {
+        return 1;
+    }
+
+    section = memory_region_find(mr, xlat, 1);
+    if (!section.mr) {
+        return 1;
+    }
+
+    translated = section.offset_within_address_space;
+    memory_region_unref(section.mr);
+
+    route->u.msi.address_lo = translated;
+    route->u.msi.address_hi = translated >> 32;
+    trace_kvm_riscv_fixup_msi_route(address, translated);
     return 0;
 }
 
diff --git a/target/riscv/trace-events b/target/riscv/trace-events
index 93837f82a1..5766fbb457 100644
--- a/target/riscv/trace-events
+++ b/target/riscv/trace-events
@@ -12,3 +12,6 @@ mseccfg_csr_write(uint64_t mhartid, uint64_t val) "hart %" PRIu64 ": write msecc
 
 # op_helper.c
 riscv_exception(uint32_t exception, const char *desc, uint64_t epc) "%u (%s) on epc 0x%"PRIx64""
+
+# kvm-cpu.c
+kvm_riscv_fixup_msi_route(uint64_t iova, uint64_t gpa) "MSI iova = 0x%"PRIx64" is translated into 0x%"PRIx64
-- 
2.53.0
Re: [PATCH] target/riscv/kvm: Translate MSI addresses through the IOMMU
Posted by Alistair Francis 4 weeks ago
On Sun, 2026-10-25 at 20:13 +0800, Tan Chi wrote:
> KVM irqfd bypasses the PCI device's IOMMU AddressSpace, so MSI
> addresses requiring translation may fail to reach the guest IMSIC.
> 
> Translate MSI addresses through the device's IOMMU AddressSpace
> when setting up KVM routes. Only direct IMSIC mappings are
> supported; Basic MSI PTE and MRIF remapping are not.
> 
> Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>

My LLM review system said this:

- [MEDIUM] target/riscv: kvm-cpu.c kvm_arch_fixup_msi_route calls
  pci_device_iommu_address_space, breaking KVM builds without PCI. The
  symbol is defined only in hw/pci/pci.c, built under CONFIG_PCI, and
  hw/pci/pci-stub.c provides no stub for it, while target/riscv/Kconfig
  (RISCV32/RISCV64) has no PCI select. PCI-less RISC-V machines (SPIKE,
  SIFIVE_E, SIFIVE_U, OPENTITAN, SHAKTI_C, MICROCHIP_PFSOC) build kvm-
cpu.o
  with CONFIG_KVM=y and CONFIG_PCI=n and hit an undefined reference at
  link time. This is exactly what target/arm/Kconfig and
  target/i386/Kconfig prevent with select PCI if KVM and the comment
  kvm_arch_fixup_msi_route() needs to access PCIDevice, citing the very
  ARM implementation this patch models itself on. Add select PCI if KVM
  to target/riscv/Kconfig or add a pci-stub.c stub.

- [LOW] target/riscv: in kvm-cpu.c kvm_arch_fixup_msi_route, all three
  failure paths (address_space_translate miss, non-IMSIC target such as
  the IOMMU MSI trap region, memory_region_find failure) return 1 with
  no trace point or diagnostic, while the success path emits
  trace_kvm_riscv_fixup_msi_route. Functionally this is a graceful
  degradation to the userspace MSI path where the emulated riscv-iommu
  handles BASIC/MRIF, but in the field an operator cannot tell whether
  KVM MSI routing was set up or silently refused. Also, the commit
  message says Basic MSI PTE and MRIF remapping are not supported
  without stating those configurations keep working via the pre-
existing
  userspace MSI path, so a reviewer cannot tell whether this is a
  regression or a degradation. Add a trace point on the failure paths
  and state the fallback explicitly in the commit message.

So it would be worth making sure it builds without PCI and maybe add
some logs?

Alistair

> ---
> 
> The AddressSpace translation follows the existing
> kvm_arch_fixup_msi_route() implementation on ARM.
> 
>  target/riscv/kvm/kvm-cpu.c | 45
> ++++++++++++++++++++++++++++++++++++++
>  target/riscv/trace-events  |  3 +++
>  2 files changed, 48 insertions(+)
> 
> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
> index 45ad5e1525..565cdec9d2 100644
> --- a/target/riscv/kvm/kvm-cpu.c
> +++ b/target/riscv/kvm/kvm-cpu.c
> @@ -1507,6 +1507,51 @@ int kvm_arch_release_virq_post(int virq)
>  int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
>                               uint64_t address, uint32_t data,
> PCIDevice *dev)
>  {
> +    AddressSpace *as;
> +    hwaddr xlat, len = sizeof(uint32_t), translated;
> +    MemoryRegionSection section;
> +    MemoryRegion *mr;
> +    Object *owner;
> +
> +    if (!dev) {
> +        return 0;
> +    }
> +
> +    as = pci_device_iommu_address_space(dev);
> +    if (as == &address_space_memory) {
> +        return 0;
> +    }
> +
> +    RCU_READ_LOCK_GUARD();
> +
> +    mr = address_space_translate(as, address, &xlat, &len, true,
> +                                 MEMTXATTRS_UNSPECIFIED);
> +
> +    if (!mr) {
> +        return 1;
> +    }
> +
> +    /*
> +     * Only direct IMSIC writes can be represented by a KVM MSI
> route.
> +     * MSI page-table translations instead target the RISC-V IOMMU
> trap
> +     * region, which requires further BASIC/MRIF processing.
> +     */
> +    owner = memory_region_owner(mr);
> +    if (!owner || !object_dynamic_cast(owner, TYPE_RISCV_IMSIC)) {
> +        return 1;
> +    }
> +
> +    section = memory_region_find(mr, xlat, 1);
> +    if (!section.mr) {
> +        return 1;
> +    }
> +
> +    translated = section.offset_within_address_space;
> +    memory_region_unref(section.mr);
> +
> +    route->u.msi.address_lo = translated;
> +    route->u.msi.address_hi = translated >> 32;
> +    trace_kvm_riscv_fixup_msi_route(address, translated);
>      return 0;
>  }
>  
> diff --git a/target/riscv/trace-events b/target/riscv/trace-events
> index 93837f82a1..5766fbb457 100644
> --- a/target/riscv/trace-events
> +++ b/target/riscv/trace-events
> @@ -12,3 +12,6 @@ mseccfg_csr_write(uint64_t mhartid, uint64_t val)
> "hart %" PRIu64 ": write msecc
>  
>  # op_helper.c
>  riscv_exception(uint32_t exception, const char *desc, uint64_t epc)
> "%u (%s) on epc 0x%"PRIx64""
> +
> +# kvm-cpu.c
> +kvm_riscv_fixup_msi_route(uint64_t iova, uint64_t gpa) "MSI iova =
> 0x%"PRIx64" is translated into 0x%"PRIx64