From nobody Mon Nov 23 04:38:02 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 1792930515146236.03928247725514; Sun, 25 Oct 2026 05:15:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKx7B-0005fB-VP; Sun, 25 Oct 2026 08:14:17 -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 1xKx79-0005ew-7C; Sun, 25 Oct 2026 08:14:15 -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 1xKx76-000527-FU; Sun, 25 Oct 2026 08:14:14 -0400 Received: from k3-03 (unknown [159.226.43.8]) by APP-01 (Coremail) with SMTP id qwCowACHXfGD8t1qmUOwCw--.36297S2; Sun, 25 Oct 2026 20:13:57 +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] target/riscv/kvm: Translate MSI addresses through the IOMMU Date: Sun, 25 Oct 2026 20:13:33 +0800 Message-ID: <20261025121334.40041-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: qwCowACHXfGD8t1qmUOwCw--.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JUPfHUUUUUU= 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: 1792930531996158500 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 IMSIC mappings are supported; Basic MSI PTE and MRIF remapping are not. Signed-off-by: Tan Chi --- 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 *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) { + 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)) { + return 1; + } + + section =3D memory_region_find(mr, xlat, 1); + if (!section.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(address, translated); return 0; } =20 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 =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(uint64_t iova, uint64_t gpa) "MSI iova =3D 0x%"P= RIx64" is translated into 0x%"PRIx64 --=20 2.53.0