From nobody Mon Nov 23 04:18:47 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 1793196166259108.83562563545604; Wed, 28 Oct 2026 07:02:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xM4EE-0006Mf-2I; Wed, 28 Oct 2026 10:02:10 -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 1xM4E6-0006ML-Lf; Wed, 28 Oct 2026 10:02:02 -0400 Received: from smtp21.cstnet.cn ([159.226.251.21] 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 1xM4E3-0001aB-O3; Wed, 28 Oct 2026 10:02:02 -0400 Received: from k3-03 (unknown [159.226.43.8]) by APP-01 (Coremail) with SMTP id qwCowACHW+80AOJqYJHjCw--.4832S2; Wed, 28 Oct 2026 22:01:46 +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, nutty.liu@hotmail.com, Tan Chi Subject: [PATCH v3] target/riscv/kvm: Translate MSI addresses through the IOMMU Date: Wed, 28 Oct 2026 22:01:22 +0800 Message-ID: <20261028140122.1560-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: q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6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x0JUTuWQUUUUU = 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.21; 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: 1793196180882154100 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 Acked-by: Alistair Francis --- Changes in v3: - Remove the unused length variable and pass NULL to address_space_translate(), following Nutty Liu's review. Changes in v2 (based on Alistair Francis's review): - 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..a1eecd72b7 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, 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, NULL, 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