From nobody Mon Nov 23 04:38:03 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793024305774849.3642781159651; Mon, 26 Oct 2026 07:18:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLLVz-0005jN-4K; Mon, 26 Oct 2026 10:17:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xLLVc-0005hq-Vk; Mon, 26 Oct 2026 10:17:10 -0400 Received: from smtp81.cstnet.cn ([159.226.251.81] helo=cstnet.cn) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1xLLVY-00038d-1Z; Mon, 26 Oct 2026 10:17:07 -0400 Received: from k3-03 (unknown [159.226.43.8]) by APP-03 (Coremail) with SMTP id rQCowABHTz3MYN9qFyK6Cw--.1361S2; Mon, 26 Oct 2026 22:16:52 +0800 (CST) From: Tan Chi To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, Tan Chi Subject: [PATCH v2] target/riscv/kvm: Translate MSI addresses through the IOMMU Date: Mon, 26 Oct 2026 22:16:42 +0800 Message-ID: <20261026141642.113731-1-tanchi25@mails.ucas.ac.cn> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: rQCowABHTz3MYN9q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pf9x0JUv9a9UUUUU= X-Originating-IP: [159.226.43.8] X-CM-SenderInfo: pwdquxrlsvqzpdlo2hpxfd2hldfou0/ Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=159.226.251.81; envelope-from=tanchi25@mails.ucas.ac.cn; helo=cstnet.cn X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793024320534158500 Content-Type: text/plain; charset="utf-8" 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 mappings to an IMSIC MemoryRegion are installed in KVM routes. Other IOMMU MSI remapping forms are not converted to KVM routes and continue to use the existing userspace MSI delivery path. Select PCI for RISCV64 KVM builds, as QEMU currently enables the RISC-V KVM target only for riscv64-softmmu builds and the MSI route fixup requires PCI support. Also trace translation failures to help diagnose route setup failures. Signed-off-by: Tan Chi Reviewed-by: Nutty Liu --- The AddressSpace translation follows the existing kvm_arch_fixup_msi_route() implementation on ARM. Changes in v2: - Select PCI for RISCV64 KVM builds. - Add trace events for MSI translation failures. target/riscv/Kconfig | 2 ++ target/riscv/kvm/kvm-cpu.c | 54 ++++++++++++++++++++++++++++++++++++++ target/riscv/trace-events | 4 +++ 3 files changed, 60 insertions(+) diff --git a/target/riscv/Kconfig b/target/riscv/Kconfig index 11bc09b414..0cf2c8e1c4 100644 --- a/target/riscv/Kconfig +++ b/target/riscv/Kconfig @@ -7,3 +7,5 @@ config RISCV64 bool select ARM_COMPATIBLE_SEMIHOSTING if TCG select DEVICE_TREE # needed by boot.c + # kvm_arch_fixup_msi_route() needs to access PCIDevice + select PCI if KVM diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index 45ad5e1525..7c3d3c8c69 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -1507,6 +1507,60 @@ 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 *d= ev) { + AddressSpace *as; + hwaddr xlat, len =3D sizeof(uint32_t), translated; + MemoryRegionSection section; + MemoryRegion *mr; + Object *owner; + + if (!dev) { + return 0; + } + + as =3D pci_device_iommu_address_space(dev); + if (as =3D=3D &address_space_memory) { + return 0; + } + + RCU_READ_LOCK_GUARD(); + + mr =3D address_space_translate(as, address, &xlat, &len, true, + MEMTXATTRS_UNSPECIFIED); + + if (!mr) { + trace_kvm_riscv_fixup_msi_route_failed(address, + "address translation failed= ", + "none"); + 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 =3D memory_region_owner(mr); + if (!owner || !object_dynamic_cast(owner, TYPE_RISCV_IMSIC)) { + trace_kvm_riscv_fixup_msi_route_failed(address, + "non-IMSIC target", + memory_region_name(mr)); + return 1; + } + + section =3D memory_region_find(mr, xlat, 1); + if (!section.mr) { + trace_kvm_riscv_fixup_msi_route_failed(address, + "memory region lookup faile= d", + memory_region_name(mr)); + return 1; + } + + translated =3D section.offset_within_address_space; + memory_region_unref(section.mr); + + route->u.msi.address_lo =3D translated; + route->u.msi.address_hi =3D translated >> 32; + trace_kvm_riscv_fixup_msi_route_translated(address, translated); return 0; } =20 diff --git a/target/riscv/trace-events b/target/riscv/trace-events index 93837f82a1..50980c031d 100644 --- a/target/riscv/trace-events +++ b/target/riscv/trace-events @@ -12,3 +12,7 @@ mseccfg_csr_write(uint64_t mhartid, uint64_t val) "hart %= " PRIu64 ": write msecc =20 # 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_translated(uint64_t iova, uint64_t gpa) "MSI iov= a =3D 0x%"PRIx64" is translated into 0x%"PRIx64 +kvm_riscv_fixup_msi_route_failed(uint64_t iova, const char *reason, const = char *region) "MSI iova =3D 0x%"PRIx64" failed: %s (region=3D%s)" --=20 2.53.0