From nobody Mon Nov 23 04:18:49 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 1793253005138343.9699347218258; Wed, 28 Oct 2026 22:50:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0T-0007U3-5K; Thu, 29 Oct 2026 01:48:58 -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 1xMIzn-00074H-H2; Thu, 29 Oct 2026 01:48:17 -0400 Received: from mxhk.zte.com.cn ([160.30.148.35]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzj-0000dI-1a; Thu, 29 Oct 2026 01:48:15 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDG6KGQz8XrrX; Thu, 29 Oct 2026 13:47:58 +0800 (CST) Received: from szxlzmapp04.zte.com.cn ([10.5.231.166]) by mse-fl1.zte.com.cn with SMTP id 69T5lpwY079235; Thu, 29 Oct 2026 13:47:51 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:53 +0800 X-Zmail-TransId: 3e816ae2de08027-e631f X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 01/14] linux-headers: Update iommufd.h header for RISC-V IOMMU nested translation Date: Thu, 29 Oct 2026 13:47:38 +0800 Message-ID: <20261029054751.563493-2-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5lpwY079235 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:47:58 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE0E.000/4jFYDG6KGQz8XrrX 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=160.30.148.35; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253007023158500 Content-Type: text/plain; charset="utf-8" Add the RISC-V IOMMU UAPI needed by the accelerated IOMMU device. Only the RISC-V IOMMU definitions of iommufd.h are taken, instead of doing a full scripts/update-linux-headers.sh sync that would also pull in unrelated changes. The kernel UAPI this is taken from is not upstream yet, so this import is a placeholder and will be replaced by a regular linux-headers sync once the kernel patches are merged. Signed-off-by: Gong Shuai --- linux-headers/linux/iommufd.h | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/linux-headers/linux/iommufd.h b/linux-headers/linux/iommufd.h index 3c61caadbf..4a39da3271 100644 --- a/linux-headers/linux/iommufd.h +++ b/linux-headers/linux/iommufd.h @@ -506,18 +506,46 @@ struct iommu_hwpt_amd_guest { __aligned_u64 dte[4]; }; =20 +/** + * struct iommu_hwpt_riscv - RISC-V IOMMU stage-1 page table data + * (IOMMU_HWPT_DATA_RISCV_S1) + * + * @dc: The guest device-context (DC) in the base format defined in + * "Leaf DDT entry" of the RISC-V IOMMU Architecture Specification, + * refer to "Device-context fields" for the meaning of each + * doubleword. Only the guest-owned doublewords are validated: + * - dc[0] (tc): Allowed bits are V, EN_ATS, EN_PRI, T2GPA, DTF, + * PRPR, SADE and SBE. GADE is owned by the host and the value + * here is ignored. PDTV, DPE and SXL are not supported and must + * be 0. + * - dc[3] (fsc): Carries iosatp when tc.PDTV=3D0, allowed fields + * are MODE (Bare, Sv39, Sv48, Sv57) and PPN; Carries pdtp when + * tc.PDTV=3D1, which not supported now. + * + * The host-owned doublewords dc[1] (iohgatp) and dc[2] (ta) are + * ignored. iohgatp is installed by the kernel from the nesting + * parent at attach time and ta is managed by the host. The + * extended-format MSI doublewords are configured by the host for + * interrupt remapping and are not passed via this structure. + */ +struct iommu_hwpt_riscv { + __aligned_u64 dc[4]; +}; + /** * enum iommu_hwpt_data_type - IOMMU HWPT Data Type * @IOMMU_HWPT_DATA_NONE: no data * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table * @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table * @IOMMU_HWPT_DATA_AMD_GUEST: AMD IOMMU guest page table + * @IOMMU_HWPT_DATA_RISCV_S1: RISC-V IOMMU stage-1 page table */ enum iommu_hwpt_data_type { IOMMU_HWPT_DATA_NONE =3D 0, IOMMU_HWPT_DATA_VTD_S1 =3D 1, IOMMU_HWPT_DATA_ARM_SMMUV3 =3D 2, IOMMU_HWPT_DATA_AMD_GUEST =3D 3, + IOMMU_HWPT_DATA_RISCV_S1 =3D 4, }; =20 /** @@ -703,6 +731,28 @@ struct iommu_hw_info_amd { __aligned_u64 efr2; }; =20 +/** + * struct iommu_hw_info_riscv - RISC-V IOMMU device info + * (IOMMU_HW_INFO_TYPE_RISCV) + * + * @capabilities: Value of the RISC-V IOMMU capabilities register, defined + * in "IOMMU capabilities (capabilities)" of the RISC-V + * IOMMU Architecture Specification. The value is reported + * raw by the hardware. + * + * The VMM is responsible to determine the appropriate fields to be + * exposed to the VM since certain features are not currently supported + * by the kernel for nested domains. Current list of supported fields: + * - capabilities[Sv39, Sv48, Sv57, END, AMO_HWAD] + * + * This list may expand in the future. It is important that VMMs do not + * expose bits outside the list to the VM, to allow for compatibility + * with future kernels. + */ +struct iommu_hw_info_riscv { + __aligned_u64 capabilities; +}; + /** * enum iommu_hw_info_type - IOMMU Hardware Info Types * @IOMMU_HW_INFO_TYPE_NONE: Output by the drivers that do not report hard= ware @@ -713,6 +763,7 @@ struct iommu_hw_info_amd { * @IOMMU_HW_INFO_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension fo= r ARM * SMMUv3) info type * @IOMMU_HW_INFO_TYPE_AMD: AMD IOMMU info type + * @IOMMU_HW_INFO_TYPE_RISCV: RISC-V IOMMU info type */ enum iommu_hw_info_type { IOMMU_HW_INFO_TYPE_NONE =3D 0, @@ -721,6 +772,7 @@ enum iommu_hw_info_type { IOMMU_HW_INFO_TYPE_ARM_SMMUV3 =3D 2, IOMMU_HW_INFO_TYPE_TEGRA241_CMDQV =3D 3, IOMMU_HW_INFO_TYPE_AMD =3D 4, + IOMMU_HW_INFO_TYPE_RISCV =3D 5, }; =20 /** @@ -890,10 +942,12 @@ struct iommu_hwpt_get_dirty_bitmap { * Data Type * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1 * @IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3: Invalidation data for ARM SMM= Uv3 + * @IOMMU_VIOMMU_INVALIDATE_DATA_RISCV: Invalidation data for RISC-V IOMMU */ enum iommu_hwpt_invalidate_data_type { IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 =3D 0, IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3 =3D 1, + IOMMU_VIOMMU_INVALIDATE_DATA_RISCV =3D 2, }; =20 /** @@ -954,6 +1008,31 @@ struct iommu_viommu_arm_smmuv3_invalidate { __aligned_le64 cmd[2]; }; =20 +/** + * struct iommu_viommu_riscv_invalidate - RISC-V IOMMU cache invalidation + * (IOMMU_VIOMMU_INVALIDATE_DATA_RISCV) + * @cmd: 128-bit cache invalidation command that runs in the IOMMU command + * queue, in the format defined by the "IOMMU Page-table cache + * invalidation" section of the RISC-V IOMMU specification. Must be + * little-endian. + * + * Supported command list only when passing in a vIOMMU via @hwpt_id: + * IOTINVAL.VMA + * + * The isolation operands are owned by the kernel, which overwrites them + * before the command is issued: + * GV =3D 1 and GSCID =3D the GSCID assigned to the vIOMMU + * PSCV =3D 1 and PSCID =3D 0 + * + * All other reserved bits must be 0. + * + * -EIO will be returned if the command is not supported, -EINVAL if it is + * malformed. + */ +struct iommu_viommu_riscv_invalidate { + __aligned_le64 cmd[2]; +}; + /** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) @@ -1095,6 +1174,7 @@ struct iommu_fault_alloc { * @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type * @IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension for= ARM * SMMUv3) enabled ARM SMMUv3 type + * @IOMMU_VIOMMU_TYPE_RISCV: RISC-V IOMMU driver specific type */ enum iommu_viommu_type { IOMMU_VIOMMU_TYPE_DEFAULT =3D 0, @@ -1105,6 +1185,7 @@ enum iommu_viommu_type { * VMM must wire the HYP_OWN bit to 0 in guest VINTF_CONFIG register */ IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV =3D 2, + IOMMU_VIOMMU_TYPE_RISCV =3D 3, }; =20 /** --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793252916027675.1451007626301; Wed, 28 Oct 2026 22:48:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMIzt-00074W-UO; Thu, 29 Oct 2026 01:48:25 -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 1xMIzk-00072U-0N; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mxct.zte.com.cn ([183.62.165.209]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzg-0000dO-KN; Thu, 29 Oct 2026 01:48:11 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4jFYDF3STzz5B151; Thu, 29 Oct 2026 13:47:57 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl1.zte.com.cn with SMTP id 69T5lq0d079236; Thu, 29 Oct 2026 13:47:52 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:54 +0800 X-Zmail-TransId: 3e816ae2de09027-e632c X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 02/14] hw/riscv/Kconfig: Enable IOMMUFD on RISC-V virt machine Date: Thu, 29 Oct 2026 13:47:39 +0800 Message-ID: <20261029054751.563493-3-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5lq0d079236 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:47:57 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE0D.000/4jFYDF3STzz5B151 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=183.62.165.209; envelope-from=gong.shuai@sanechips.com.cn; helo=mxct.zte.com.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, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793252922041154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index d89fdfcc86..a423fafca1 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -49,6 +49,7 @@ config RISCV_VIRT imply VIRTIO_VGA imply TEST_DEVICES imply TPM_TIS_SYSBUS + imply IOMMUFD select DEVICE_TREE select RISCV_NUMA select GOLDFISH_RTC --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793252933278960.0419971809006; Wed, 28 Oct 2026 22:48:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0D-0007B9-FU; Thu, 29 Oct 2026 01:48:43 -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 1xMIzi-00072T-Q2; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mxhk.zte.com.cn ([160.30.148.34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzg-0000dk-Jw; Thu, 29 Oct 2026 01:48:10 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDL4WDLz5PkGy; Thu, 29 Oct 2026 13:48:02 +0800 (CST) Received: from szxl2zmapp05.zte.com.cn ([10.1.32.37]) by mse-fl2.zte.com.cn with SMTP id 69T5lqAY024140; Thu, 29 Oct 2026 13:47:52 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:54 +0800 X-Zmail-TransId: 3e816ae2de0a027-e6339 X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 03/14] hw/riscv/riscv-iommu: Allow PCI IOMMU ops to be overridden Date: Thu, 29 Oct 2026 13:47:40 +0800 Message-ID: <20261029054751.563493-4-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 69T5lqAY024140 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.133 unknown Thu, 29 Oct 2026 13:48:02 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE12.002/4jFYDL4WDLz5PkGy 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=160.30.148.34; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793252936309154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei Add an iommu_ops field to RISCVIOMMUState and use it when registering the IOMMU on a PCI bus, falling back to the default riscv_iommu_ops when unset. This lets callers install custom PCI IOMMU ops without changing existing behaviour. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu.c | 5 ++++- hw/riscv/riscv-iommu.h | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 0e6970773a..6381986265 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -3007,7 +3007,10 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *io= mmu, PCIBus *bus, RISCVIOMMUState *last =3D (RISCVIOMMUState *)bus->iommu_opaque; QLIST_INSERT_AFTER(last, iommu, iommus); } else if (!bus->iommu_ops && !bus->iommu_opaque) { - pci_setup_iommu(bus, &riscv_iommu_ops, iommu); + if (!iommu->iommu_ops) { + iommu->iommu_ops =3D &riscv_iommu_ops; + } + pci_setup_iommu(bus, iommu->iommu_ops, iommu); } else { error_setg(errp, "can't register secondary IOMMU for PCI bus #%d", pci_bus_num(bus)); diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 403d10b321..cf2904c118 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -24,6 +24,7 @@ #include "system/dma.h" #include "hw/riscv/iommu.h" #include "hw/riscv/riscv-iommu-bits.h" +#include "hw/pci/pci.h" =20 typedef enum riscv_iommu_igs_modes riscv_iommu_igs_mode; =20 @@ -97,6 +98,8 @@ struct RISCVIOMMUState { /* HPM event counters */ GHashTable *hpm_event_ctr_map; /* Mapping of events to counters */ uint8_t hpm_cntrs; + + const PCIIOMMUOps *iommu_ops; }; =20 void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus, --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 17932530862765.359232808026945; Wed, 28 Oct 2026 22:51:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0I-0007GJ-WD; Thu, 29 Oct 2026 01:48:47 -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 1xMIzl-00073A-E3; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mxhk.zte.com.cn ([160.30.148.34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzh-0000dt-G2; Thu, 29 Oct 2026 01:48:13 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDM13Jqz5PkGs; Thu, 29 Oct 2026 13:48:03 +0800 (CST) Received: from szxlzmapp01.zte.com.cn ([10.5.231.85]) by mse-fl2.zte.com.cn with SMTP id 69T5lrgg024141; Thu, 29 Oct 2026 13:47:53 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:55 +0800 X-Zmail-TransId: 3e816ae2de0a027-e6346 X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 04/14] hw/riscv/riscv-iommu: Expose RISCVIOMMUSpace and split its helpers Date: Thu, 29 Oct 2026 13:47:41 +0800 Message-ID: <20261029054751.563493-5-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 69T5lrgg024141 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.133 unknown Thu, 29 Oct 2026 13:48:03 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE13.000/4jFYDM13Jqz5PkGs 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=160.30.148.34; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253088647154100 Content-Type: text/plain; charset="utf-8" Move the per-device IOMMU address space structure (RISCVIOMMUSpace) into the public header and split its allocation into separate lookup and init helpers, so the space management can be reused by other devices without code duplication. No behavioural change. Signed-off-by: Gong Shuai Signed-off-by: Wu Fei --- hw/riscv/riscv-iommu.c | 68 ++++++++++++++++++++++-------------------- hw/riscv/riscv-iommu.h | 16 ++++++++++ 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 6381986265..986921dd5e 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -44,17 +44,6 @@ =20 typedef struct RISCVIOMMUEntry RISCVIOMMUEntry; =20 -/* Device assigned I/O address space */ -struct RISCVIOMMUSpace { - IOMMUMemoryRegion iova_mr; /* IOVA memory region for attached device = */ - AddressSpace iova_as; /* IOVA address space for attached device = */ - RISCVIOMMUState *iommu; /* Managing IOMMU device state */ - PCIBus *bus; /* PCI bus of the requester */ - uint8_t devfn; /* Requester identifier, AKA device_id */ - bool notifier; /* IOMMU unmap notifier enabled */ - QLIST_ENTRY(RISCVIOMMUSpace) list; -}; - typedef enum RISCVIOMMUTransTag { RISCV_IOMMU_TRANS_TAG_BY, /* Bypass */ RISCV_IOMMU_TRANS_TAG_SS, /* Single Stage */ @@ -1527,9 +1516,31 @@ static void riscv_iommu_ctx_put(RISCVIOMMUState *s, = void *ref) } } =20 -/* Find or allocate address space for a given device */ -static AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, - int devfn) +void riscv_iommu_init_space(RISCVIOMMUState *s, RISCVIOMMUSpace *as, + PCIBus *bus, int devfn) +{ + char name[64]; + + as->iommu =3D s; + as->bus =3D bus; + as->devfn =3D devfn; + + snprintf(name, sizeof(name), "riscv-iommu-%04x:%02x.%d-iova", + pci_bus_num(bus), PCI_SLOT(devfn), PCI_FUNC(devfn)); + + /* IOVA address space, untranslated addresses */ + memory_region_init_iommu(&as->iova_mr, sizeof(as->iova_mr), + TYPE_RISCV_IOMMU_MEMORY_REGION, + OBJECT(s), "riscv_iommu", UINT64_MAX); + address_space_init(&as->iova_as, MEMORY_REGION(&as->iova_mr), name); + + QLIST_INSERT_HEAD(&s->spaces, as, list); + + trace_riscv_iommu_new(s->parent_obj.id, pci_bus_num(bus), + PCI_SLOT(devfn), PCI_FUNC(devfn)); +} + +RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, in= t devfn) { RISCVIOMMUSpace *as; =20 @@ -1539,27 +1550,18 @@ static AddressSpace *riscv_iommu_space(RISCVIOMMUSt= ate *s, PCIBus *bus, } } =20 - if (as =3D=3D NULL) { - char name[64]; - as =3D g_new0(RISCVIOMMUSpace, 1); - - as->iommu =3D s; - as->bus =3D bus; - as->devfn =3D devfn; - - snprintf(name, sizeof(name), "riscv-iommu-%04x:%02x.%d-iova", - pci_bus_num(bus), PCI_SLOT(devfn), PCI_FUNC(devfn)); - - /* IOVA address space, untranslated addresses */ - memory_region_init_iommu(&as->iova_mr, sizeof(as->iova_mr), - TYPE_RISCV_IOMMU_MEMORY_REGION, - OBJECT(s), "riscv_iommu", UINT64_MAX); - address_space_init(&as->iova_as, MEMORY_REGION(&as->iova_mr), name= ); + return as; +} =20 - QLIST_INSERT_HEAD(&s->spaces, as, list); +/* Find or allocate address space for a given device */ +AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn) +{ + RISCVIOMMUSpace *as; =20 - trace_riscv_iommu_new(s->parent_obj.id, pci_bus_num(bus), - PCI_SLOT(devfn), PCI_FUNC(devfn)); + as =3D riscv_iommu_get_space(s, bus, devfn); + if (as =3D=3D NULL) { + as =3D g_new0(RISCVIOMMUSpace, 1); + riscv_iommu_init_space(s, as, bus, devfn); } return &as->iova_as; } diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index cf2904c118..099faa9480 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -28,6 +28,17 @@ =20 typedef enum riscv_iommu_igs_modes riscv_iommu_igs_mode; =20 +/* Device assigned I/O address space */ +struct RISCVIOMMUSpace { + IOMMUMemoryRegion iova_mr; /* IOVA memory region for attached device = */ + AddressSpace iova_as; /* IOVA address space for attached device = */ + RISCVIOMMUState *iommu; /* Managing IOMMU device state */ + PCIBus *bus; /* PCI bus of the requester */ + uint8_t devfn; /* Requester identifier, AKA device_id */ + bool notifier; /* IOMMU unmap notifier enabled */ + QLIST_ENTRY(RISCVIOMMUSpace) list; +}; + struct RISCVIOMMUState { /*< private >*/ DeviceState parent_obj; @@ -112,6 +123,11 @@ DeviceState *riscv_create_iommu_sys(DeviceState *mmio_= irqchip, hwaddr addr, int base_irq, bool is_32_bit); =20 +void riscv_iommu_init_space(RISCVIOMMUState *s, RISCVIOMMUSpace *as, + PCIBus *bus, int devfn); +AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn= ); +RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, in= t devfn); + typedef struct RISCVIOMMUContext RISCVIOMMUContext; /* Device translation context state. */ struct RISCVIOMMUContext { --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793252968113920.2329835858714; Wed, 28 Oct 2026 22:49:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0D-0007Am-5P; Thu, 29 Oct 2026 01:48:41 -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 1xMIzk-00072X-DC; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mxct.zte.com.cn ([183.62.165.209]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzh-0000db-3P; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4jFYDF6wSMz5B15K; Thu, 29 Oct 2026 13:47:57 +0800 (CST) Received: from szxlzmapp02.zte.com.cn ([10.5.231.79]) by mse-fl1.zte.com.cn with SMTP id 69T5lrP4079237; Thu, 29 Oct 2026 13:47:53 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:55 +0800 X-Zmail-TransId: 3e816ae2de0b027-e6360 X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 05/14] hw/riscv/riscv-iommu: Split device context fetch from cache lookup Date: Thu, 29 Oct 2026 13:47:42 +0800 Message-ID: <20261029054751.563493-6-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5lrP4079237 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:47:57 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE0D.001/4jFYDF6wSMz5B15K 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=183.62.165.209; envelope-from=gong.shuai@sanechips.com.cn; helo=mxct.zte.com.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, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793252968872158500 Content-Type: text/plain; charset="utf-8" The device context lookup mixes parsing the DC from guest memory with cache management and FQ fault reporting, which does not fit the command path. Export riscv_iommu_ctx_fetch() to parse a DC straight from guest memory without cache or fault side effects, and rename riscv_iommu_ctx() to riscv_iommu_ctx_lookup() to reflect its translation-path role. Signed-off-by: Gong Shuai Signed-off-by: Wu Fei --- hw/riscv/riscv-iommu.c | 31 ++++++++++++++++--------------- hw/riscv/riscv-iommu.h | 5 +++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 986921dd5e..10f81f8bdf 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -1154,13 +1154,13 @@ out: } =20 /* - * RISC-V IOMMU Device Context Loopkup - Device Directory Tree Walk + * RISC-V IOMMU Device Context Fetch - Device Directory Tree Walk * - * @s : IOMMU Device State - * @ctx : Device Translation Context with devid and process_id set. - * @return : success or fault code. + * Parse the device context for (devid, process_id) from guest memory + * into @ctx. Returns 0 on success or a fault code. */ -static int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ct= x) +int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, unsigned devid, + unsigned process_id, RISCVIOMMUContext *ctx) { const uint64_t ddtp =3D s->ddtp; unsigned mode =3D get_field(ddtp, RISCV_IOMMU_DDTP_MODE); @@ -1172,6 +1172,9 @@ static int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, = RISCVIOMMUContext *ctx) int depth; uint64_t de; =20 + ctx->devid =3D devid; + ctx->process_id =3D process_id; + switch (mode) { case RISCV_IOMMU_DDTP_MODE_OFF: return RISCV_IOMMU_FQ_CAUSE_DMA_DISABLED; @@ -1425,10 +1428,10 @@ static void riscv_iommu_ctx_inval(RISCVIOMMUState *= s, GHFunc func, } =20 /* Find or allocate translation context for a given {device_id, process_id= } */ -static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s, - unsigned devid, unsigned process= _id, - IOMMUAccessFlags perm, uint64_t = iova, - void **ref) +static RISCVIOMMUContext *riscv_iommu_ctx_lookup(RISCVIOMMUState *s, + unsigned devid, unsigned = process_id, + IOMMUAccessFlags perm, ui= nt64_t iova, + void **ref) { GHashTable *ctx_cache; RISCVIOMMUContext *ctx; @@ -1452,10 +1455,8 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMU= State *s, } =20 ctx =3D g_new0(RISCVIOMMUContext, 1); - ctx->devid =3D devid; - ctx->process_id =3D process_id; =20 - int fault =3D riscv_iommu_ctx_fetch(s, ctx); + int fault =3D riscv_iommu_ctx_fetch(s, devid, process_id, ctx); if (!fault) { if (mode !=3D RISCV_IOMMU_DDTP_MODE_BARE) { if (g_hash_table_size(ctx_cache) >=3D LIMIT_CACHE_CTX) { @@ -2260,7 +2261,7 @@ static void riscv_iommu_process_dbg(RISCVIOMMUState *= s) return; } =20 - ctx =3D riscv_iommu_ctx(s, devid, pid, perm, iova, &ref); + ctx =3D riscv_iommu_ctx_lookup(s, devid, pid, perm, iova, &ref); if (ctx =3D=3D NULL) { riscv_iommu_reg_set64(s, RISCV_IOMMU_REG_TR_RESPONSE, RISCV_IOMMU_TR_RESPONSE_FAULT | @@ -2619,7 +2620,7 @@ static MemTxResult riscv_iommu_trap_write(void *opaqu= e, hwaddr addr, /* FIXME: PCIe bus remapping for attached endpoints. */ devid |=3D s->bus << 8; =20 - ctx =3D riscv_iommu_ctx(s, devid, 0, IOMMU_RW, addr, &ref); + ctx =3D riscv_iommu_ctx_lookup(s, devid, 0, IOMMU_RW, addr, &ref); if (ctx =3D=3D NULL) { res =3D MEMTX_ACCESS_ERROR; } else { @@ -2927,7 +2928,7 @@ static IOMMUTLBEntry riscv_iommu_memory_region_transl= ate( }; uint32_t devid =3D riscv_iommu_space_devid(as); =20 - ctx =3D riscv_iommu_ctx(as->iommu, devid, iommu_idx, flag, addr, &ref); + ctx =3D riscv_iommu_ctx_lookup(as->iommu, devid, iommu_idx, flag, addr= , &ref); if (ctx =3D=3D NULL) { /* Translation disabled or invalid. */ iotlb.addr_mask =3D 0; diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 099faa9480..3e10647975 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -142,6 +142,11 @@ struct RISCVIOMMUContext { uint64_t msiptp; /* MSI redirection page table pointer */ }; =20 +/* Resolve {devid, process_id} straight from guest memory. Returns 0 or a + * RISCV_IOMMU_FQ_CAUSE_* code. */ +int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, unsigned devid, + unsigned process_id, RISCVIOMMUContext *ctx); + /* private helpers */ =20 /* Register helper functions */ --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793252934905976.8891889967538; Wed, 28 Oct 2026 22:48:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ06-00076d-Fc; Thu, 29 Oct 2026 01:48:34 -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 1xMIzk-00072W-9u; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mxhk.zte.com.cn ([160.30.148.34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzg-0000dP-IA; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDJ5lhJz5PkGt; Thu, 29 Oct 2026 13:48:00 +0800 (CST) Received: from szxl2zmapp05.zte.com.cn ([10.1.32.37]) by mse-fl1.zte.com.cn with SMTP id 69T5ls6N079238; Thu, 29 Oct 2026 13:47:54 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:56 +0800 X-Zmail-TransId: 3e816ae2de0b027-e636d X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 06/14] hw/riscv/riscv-iommu-accel: Introduce RISC-V IOMMU accel device Date: Thu, 29 Oct 2026 13:47:43 +0800 Message-ID: <20261029054751.563493-7-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5ls6N079238 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:48:00 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE10.001/4jFYDJ5lhJz5PkGt 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=160.30.148.34; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793252937223158500 Content-Type: text/plain; charset="utf-8" From: Wu Fei Add an optional accel device for the RISC-V IOMMU. Its behaviour matches the default path so far, leaving room for upcoming accelerated (nested) translation. When the accel device is enabled, each device's address space is embedded in RISCVIOMMUAccelDevice, a per-device struct that will store per-device data for nested translation. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/Kconfig | 5 ++++ hw/riscv/meson.build | 1 + hw/riscv/riscv-iommu-accel.c | 49 ++++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu-accel.h | 25 ++++++++++++++++++ hw/riscv/riscv-iommu.c | 5 ++++ hw/riscv/riscv-iommu.h | 3 +++ 6 files changed, 88 insertions(+) create mode 100644 hw/riscv/riscv-iommu-accel.c create mode 100644 hw/riscv/riscv-iommu-accel.h diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index a423fafca1..bed2e713f3 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -1,5 +1,10 @@ +config RISCV_IOMMU_ACCEL + bool + depends on RISCV_IOMMU + config RISCV_IOMMU bool + select RISCV_IOMMU_ACCEL if IOMMUFD =20 config RISCV_NUMA bool diff --git a/hw/riscv/meson.build b/hw/riscv/meson.build index 6bdabb9544..b1f3b3a2d2 100644 --- a/hw/riscv/meson.build +++ b/hw/riscv/meson.build @@ -17,6 +17,7 @@ riscv_ss.add(when: 'CONFIG_TENSTORRENT', if_true: files('= tt_atlantis.c')) riscv_ss.add(when: 'CONFIG_ACPI', if_true: files('virt-acpi-build.c')) riscv_ss.add(when: 'CONFIG_RISCV_IOMMU', if_true: files( 'riscv-iommu.c', 'riscv-iommu-pci.c', 'riscv-iommu-sys.c', 'riscv-iommu-h= pm.c')) +riscv_ss.add(when: 'CONFIG_RISCV_IOMMU_ACCEL', if_true: files('riscv-iommu= -accel.c')) riscv_ss.add(when: 'CONFIG_MICROBLAZE_V', if_true: files('microblaze-v-gen= eric.c')) riscv_ss.add(when: 'CONFIG_XIANGSHAN_KUNMINGHU', if_true: files('xiangshan= _kmh.c')) riscv_ss.add(when: 'CONFIG_RISCV_MIPS_CPS', if_true: files('cps.c')) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c new file mode 100644 index 0000000000..41e4d03be6 --- /dev/null +++ b/hw/riscv/riscv-iommu-accel.c @@ -0,0 +1,49 @@ +/* + * RISC-V IOMMU - accelerated (nested) translation support + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" + +#include "riscv-iommu.h" +#include "riscv-iommu-accel.h" + +static RISCVIOMMUAccelDevice *riscv_iommu_accel_get_dev(RISCVIOMMUState *s, + PCIBus *bus, int d= evfn) +{ + RISCVIOMMUSpace *as; + RISCVIOMMUAccelDevice *accel_dev; + + /* FIXME: for simplicity, assume only one iommu right now */ + as =3D riscv_iommu_get_space(s, bus, devfn); + if (as) { + return container_of(as, RISCVIOMMUAccelDevice, as); + } + + accel_dev =3D g_new0(RISCVIOMMUAccelDevice, 1); + as =3D &accel_dev->as; + riscv_iommu_init_space(s, as, bus, devfn); + + return accel_dev; +} + +static AddressSpace *riscv_iommu_accel_find_as(PCIBus *bus, void *opaque, + int devfn) +{ + RISCVIOMMUState *s =3D (RISCVIOMMUState *) opaque; + + RISCVIOMMUAccelDevice *accel_dev =3D riscv_iommu_accel_get_dev(s, bus,= devfn); + RISCVIOMMUSpace *as =3D &accel_dev->as; + + return &as->iova_as; +} + +static const PCIIOMMUOps riscv_iommu_accel_ops =3D { + .get_address_space =3D riscv_iommu_accel_find_as, +}; + +void riscv_iommu_accel_init(RISCVIOMMUState *s) +{ + s->iommu_ops =3D &riscv_iommu_accel_ops; +} diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h new file mode 100644 index 0000000000..c69fea87dd --- /dev/null +++ b/hw/riscv/riscv-iommu-accel.h @@ -0,0 +1,25 @@ +/* + * RISC-V IOMMU - accelerated (nested) translation support + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_RISCV_RISCV_IOMMU_ACCEL_H +#define HW_RISCV_RISCV_IOMMU_ACCEL_H + +#include "hw/riscv/riscv-iommu.h" +#include CONFIG_DEVICES + +typedef struct RISCVIOMMUAccelDevice { + RISCVIOMMUSpace as; +} RISCVIOMMUAccelDevice; + +#ifdef CONFIG_RISCV_IOMMU_ACCEL +void riscv_iommu_accel_init(RISCVIOMMUState *s); +#else +static inline void riscv_iommu_accel_init(RISCVIOMMUState *s) +{ +} +#endif + +#endif /* HW_RISCV_RISCV_IOMMU_ACCEL_H */ diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 10f81f8bdf..762691c8ed 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -31,6 +31,7 @@ =20 #include "cpu_bits.h" #include "riscv-iommu.h" +#include "riscv-iommu-accel.h" #include "riscv-iommu-bits.h" #include "riscv-iommu-hpm.h" #include "trace.h" @@ -2707,6 +2708,10 @@ static void riscv_iommu_realize(DeviceState *dev, Er= ror **errp) { RISCVIOMMUState *s =3D RISCV_IOMMU(dev); =20 + if (s->accel) { + riscv_iommu_accel_init(s); + } + /* Report QEMU target physical address space limits. */ s->cap =3D set_field(s->cap, RISCV_IOMMU_CAP_PAS, s->pas_bits); =20 diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 3e10647975..0bc096ba60 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -111,6 +111,9 @@ struct RISCVIOMMUState { uint8_t hpm_cntrs; =20 const PCIIOMMUOps *iommu_ops; + + /* nested iommu support */ + bool accel; }; =20 void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus, --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 179325291814324.689121002048523; Wed, 28 Oct 2026 22:48:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMIzt-00074Z-Tt; Thu, 29 Oct 2026 01:48:24 -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 1xMIzk-00072V-9v; Thu, 29 Oct 2026 01:48:12 -0400 Received: from mxhk.zte.com.cn ([160.30.148.34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzg-0000dx-JX; Thu, 29 Oct 2026 01:48:11 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDM4mC2z5PkH1; Thu, 29 Oct 2026 13:48:03 +0800 (CST) Received: from szxl2zmapp06.zte.com.cn ([10.1.32.108]) by mse-fl2.zte.com.cn with SMTP id 69T5lt3i024154; Thu, 29 Oct 2026 13:47:55 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:56 +0800 X-Zmail-TransId: 3e816ae2de0c027-e637f X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 07/14] hw/riscv/riscv-iommu-accel: Initialize shared system address space Date: Thu, 29 Oct 2026 13:47:44 +0800 Message-ID: <20261029054751.563493-8-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 69T5lt3i024154 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.133 unknown Thu, 29 Oct 2026 13:48:03 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE13.002/4jFYDM4mC2z5PkH1 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=160.30.148.34; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793252921026154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei To support accelerated RISC-V IOMMU instances, introduce a shared system-wide AddressSpace (shared_as_sysmem) that aliases the global system memory. This shared AddressSpace will be used in a subsequent patch for all vfio-pci devices behind all accelerated IOMMU instances within a VM. Sharing a single system AddressSpace ensures that all devices behind accelerated IOMMUs use the same system address space pointer. This allows VFIO/iommufd to reuse a single IOAS ID in iommufd_cdev_attach(), enabling the Stage-2 page tables to be shared within the VM rather than duplicated for each IOMMU instance. This follows the approach of the ARM SMMUv3 accel device. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu-accel.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index 41e4d03be6..a843cf9d01 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -9,6 +9,14 @@ #include "riscv-iommu.h" #include "riscv-iommu-accel.h" =20 +/* + * The root region aliases the global system memory, and shared_as_sysmem + * provides a shared Address Space referencing it. This Address Space is u= sed + * by all vfio-pci devices behind all accelerated IOMMU instances within a= VM. + */ +static MemoryRegion root, sysmem; +static AddressSpace *shared_as_sysmem; + static RISCVIOMMUAccelDevice *riscv_iommu_accel_get_dev(RISCVIOMMUState *s, PCIBus *bus, int d= evfn) { @@ -43,7 +51,24 @@ static const PCIIOMMUOps riscv_iommu_accel_ops =3D { .get_address_space =3D riscv_iommu_accel_find_as, }; =20 +static void riscv_iommu_accel_as_init(RISCVIOMMUState *s) +{ + if (shared_as_sysmem) { + return; + } + + memory_region_init(&root, OBJECT(s), "root", UINT64_MAX); + memory_region_init_alias(&sysmem, OBJECT(s), "riscv-iommu-accel-sysmem= ", + get_system_memory(), 0, + memory_region_size(get_system_memory())); + memory_region_add_subregion(&root, 0, &sysmem); + + shared_as_sysmem =3D g_new0(AddressSpace, 1); + address_space_init(shared_as_sysmem, &root, "riscv-iommu-accel-as-sysm= em"); +} + void riscv_iommu_accel_init(RISCVIOMMUState *s) { s->iommu_ops =3D &riscv_iommu_accel_ops; + riscv_iommu_accel_as_init(s); } --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793253090419149.4337171615873; Wed, 28 Oct 2026 22:51:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0I-0007Eo-1j; Thu, 29 Oct 2026 01:48:46 -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 1xMIzo-00074b-8t; Thu, 29 Oct 2026 01:48:17 -0400 Received: from mxhk.zte.com.cn ([160.30.148.34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzl-0000fS-3X; Thu, 29 Oct 2026 01:48:16 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDM5cQDz5PkH2; Thu, 29 Oct 2026 13:48:03 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl2.zte.com.cn with SMTP id 69T5ltiZ024159; Thu, 29 Oct 2026 13:47:55 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:57 +0800 X-Zmail-TransId: 3e816ae2de0c027-e638d X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 08/14] hw/riscv/riscv-iommu-accel: Restrict accelerated IOMMU to vfio-pci endpoints with iommufd Date: Thu, 29 Oct 2026 13:47:45 +0800 Message-ID: <20261029054751.563493-9-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 69T5ltiZ024159 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.133 unknown Thu, 29 Oct 2026 13:48:03 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE13.003/4jFYDM5cQDz5PkH2 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=160.30.148.34; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253092771154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei The accelerated mode is only meaningful for devices that can use the host IOMMU in nested mode (S1+S2). Following the ARM SMMUv3 accel device, restrict it to vfio-pci endpoints using the iommufd backend, and to PCIe bridges/root ports so endpoints can sit downstream. vfio-pci endpoints get the shared system address space so the VFIO core builds the stage-2 mappings for the nesting parent HWPT; all other devices get the IOMMU address space. Unlike ARM, where the SMMUv3 is a system device, the RISC-V IOMMU can also be exposed as a PCI device. It must stay usable in accelerated mode, so it is exempted from the restrictions and its address space lookup bypasses the IOMMU, returning the target address space as the default path does. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu-accel.c | 90 +++++++++++++++++++++++++++++++++++- hw/riscv/riscv-iommu.c | 5 -- hw/riscv/riscv-iommu.h | 6 ++- 3 files changed, 94 insertions(+), 7 deletions(-) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index a843cf9d01..7bb725d957 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -5,6 +5,11 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/error-report.h" + +#include "hw/pci/pci_bridge.h" +#include "hw/pci-host/gpex.h" +#include "hw/vfio/pci.h" =20 #include "riscv-iommu.h" #include "riscv-iommu-accel.h" @@ -36,18 +41,101 @@ static RISCVIOMMUAccelDevice *riscv_iommu_accel_get_de= v(RISCVIOMMUState *s, return accel_dev; } =20 +/* + * Only allow PCIe bridges, pxb-pcie roots, and GPEX roots so vfio-pci + * endpoints can sit downstream, and the RISC-V IOMMU PCI device + * itself. Unlike ARM, the RISC-V IOMMU can be exposed as a PCI device; + * it must stay usable in accelerated mode, but it is never translated + * and is handed its target address space directly. + * Accelerated IOMMU requires a vfio-pci endpoint using the iommufd + * backend; all other device types are rejected. This avoids supporting + * emulated endpoints, which would complicate IOTLB invalidation and + * hurt performance. + */ +static bool riscv_iommu_accel_pdev_allowed(PCIDevice *pdev, bool *vfio_pci) +{ + if (object_dynamic_cast(OBJECT(pdev), TYPE_PCI_BRIDGE) || + object_dynamic_cast(OBJECT(pdev), TYPE_PXB_PCIE_DEV) || + object_dynamic_cast(OBJECT(pdev), TYPE_GPEX_ROOT_DEVICE) || + object_dynamic_cast(OBJECT(pdev), TYPE_RISCV_IOMMU_PCI)) { + return true; + } else if ((object_dynamic_cast(OBJECT(pdev), TYPE_VFIO_PCI))) { + *vfio_pci =3D true; + if (object_property_get_link(OBJECT(pdev), "iommufd", NULL)) { + return true; + } + } + return false; +} + +static bool riscv_iommu_accel_supports_as(PCIBus *bus, void *opaque, int d= evfn, + Error **errp) +{ + PCIDevice *pdev =3D pci_find_device(bus, pci_bus_num(bus), devfn); + bool vfio_pci =3D false; + + if (pdev && !riscv_iommu_accel_pdev_allowed(pdev, &vfio_pci)) { + if (vfio_pci) { + error_setg(errp, "vfio-pci endpoint devices without an iommufd= " + "backend not allowed when using riscv_iommu accel= =3Don"); + } else { + error_setg(errp, "Emulated endpoint devices are not allowed wh= en " + "using riscv_iommu accel=3Don"); + } + return false; + } + return true; +} + static AddressSpace *riscv_iommu_accel_find_as(PCIBus *bus, void *opaque, int devfn) { RISCVIOMMUState *s =3D (RISCVIOMMUState *) opaque; + PCIDevice *pdev =3D pci_find_device(bus, pci_bus_num(bus), devfn); + + /* + * The riscv-iommu-pci itself is not a translated endpoint; hand out i= ts + * target address space as the default path does. + */ + if (pdev && pci_is_iommu(pdev)) { + return s->target_as; + } =20 RISCVIOMMUAccelDevice *accel_dev =3D riscv_iommu_accel_get_dev(s, bus,= devfn); RISCVIOMMUSpace *as =3D &accel_dev->as; =20 - return &as->iova_as; + bool vfio_pci =3D false; + + if (pdev && !riscv_iommu_accel_pdev_allowed(pdev, &vfio_pci)) { + /* Should never be here: supports_address_space() filters these ou= t */ + g_assert_not_reached(); + } + + /* + * In the accelerated mode, a vfio-pci device attached via the iommufd + * backend must remain in the system address space. Such a device is + * always translated by its physical IOMMU (using either stage-2-only + * or nested S1+S2 translation), where the parent stage-2 page table + * is allocated by the VFIO core to back the system address space. + * + * Return the shared_as_sysmem aliased to the global system memory in + * this case. Sharing a single system address space also lets devices + * behind different accelerated IOMMU instances in the same VM reuse a + * single nesting parent HWPT in the VFIO core. + * + * For the remaining allowed devices, such as PCIe root ports and + * bridges, which use the normal emulated translation path and software + * IOTLBs, return the IOMMU address space. + */ + if (vfio_pci) { + return shared_as_sysmem; + } else { + return &as->iova_as; + } } =20 static const PCIIOMMUOps riscv_iommu_accel_ops =3D { + .supports_address_space =3D riscv_iommu_accel_supports_as, .get_address_space =3D riscv_iommu_accel_find_as, }; =20 diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 762691c8ed..fbc7f72cb6 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -2973,11 +2973,6 @@ static int riscv_iommu_memory_region_notify( return 0; } =20 -static inline bool pci_is_iommu(PCIDevice *pdev) -{ - return pci_get_word(pdev->config + PCI_CLASS_DEVICE) =3D=3D 0x0806; -} - static AddressSpace *riscv_iommu_find_as(PCIBus *bus, void *opaque, int de= vfn) { RISCVIOMMUState *s =3D (RISCVIOMMUState *) opaque; diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 0bc096ba60..92eeb18386 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -24,7 +24,7 @@ #include "system/dma.h" #include "hw/riscv/iommu.h" #include "hw/riscv/riscv-iommu-bits.h" -#include "hw/pci/pci.h" +#include "hw/pci/pci_device.h" =20 typedef enum riscv_iommu_igs_modes riscv_iommu_igs_mode; =20 @@ -151,6 +151,10 @@ int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, unsigned= devid, unsigned process_id, RISCVIOMMUContext *ctx); =20 /* private helpers */ +static inline bool pci_is_iommu(PCIDevice *pdev) +{ + return pci_get_word(pdev->config + PCI_CLASS_DEVICE) =3D=3D 0x0806; +} =20 /* Register helper functions */ static inline uint32_t riscv_iommu_reg_mod32(RISCVIOMMUState *s, --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 179325295540661.77531046991044; Wed, 28 Oct 2026 22:49:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0K-0007GZ-4l; Thu, 29 Oct 2026 01:48:48 -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 1xMIzm-00073K-KY; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mxct.zte.com.cn ([183.62.165.209]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzj-0000dj-1K; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4jFYDJ0VWRz5B15L; Thu, 29 Oct 2026 13:48:00 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl1.zte.com.cn with SMTP id 69T5luJX079277; Thu, 29 Oct 2026 13:47:56 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:58 +0800 X-Zmail-TransId: 3e816ae2de0d027-e639a X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 09/14] hw/riscv/riscv-iommu-accel: Implement get_viommu_flags() callback Date: Thu, 29 Oct 2026 13:47:46 +0800 Message-ID: <20261029054751.563493-10-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5luJX079277 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:48:00 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE10.000/4jFYDJ0VWRz5B15L 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=183.62.165.209; envelope-from=gong.shuai@sanechips.com.cn; helo=mxct.zte.com.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, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793252956996158500 Content-Type: text/plain; charset="utf-8" From: Wu Fei For accelerated IOMMU, we need nested parent domain creation. Add the callback support so that VFIO can create a nested parent. This follows the approach of the ARM SMMUv3. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu-accel.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index 7bb725d957..2bafbfa5e0 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -10,6 +10,7 @@ #include "hw/pci/pci_bridge.h" #include "hw/pci-host/gpex.h" #include "hw/vfio/pci.h" +#include "hw/core/iommu.h" =20 #include "riscv-iommu.h" #include "riscv-iommu-accel.h" @@ -134,9 +135,21 @@ static AddressSpace *riscv_iommu_accel_find_as(PCIBus = *bus, void *opaque, } } =20 +static uint64_t riscv_iommu_accel_get_viommu_flags(void *opaque) +{ + /* + * We return VIOMMU_FLAG_WANT_NESTING_PARENT to inform VFIO core to cr= eate a + * nesting parent which is required for accelerated riscv iommu suppor= t. + * The real HW nested support should be reported from host riscv iommu= and if + * it doesn't, the nesting parent allocation will fail anyway in VFIO = core. + */ + return VIOMMU_FLAG_WANT_NESTING_PARENT; +} + static const PCIIOMMUOps riscv_iommu_accel_ops =3D { .supports_address_space =3D riscv_iommu_accel_supports_as, .get_address_space =3D riscv_iommu_accel_find_as, + .get_viommu_flags =3D riscv_iommu_accel_get_viommu_flags, }; =20 static void riscv_iommu_accel_as_init(RISCVIOMMUState *s) --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 179325308741766.78635009400807; Wed, 28 Oct 2026 22:51:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0V-0007Uk-31; Thu, 29 Oct 2026 01:48:59 -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 1xMIzo-00074a-4w; Thu, 29 Oct 2026 01:48:17 -0400 Received: from mxhk.zte.com.cn ([160.30.148.34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzk-0000fU-Vo; Thu, 29 Oct 2026 01:48:15 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDN22zvz5PkH3; Thu, 29 Oct 2026 13:48:04 +0800 (CST) Received: from szxlzmapp04.zte.com.cn ([10.5.231.166]) by mse-fl2.zte.com.cn with SMTP id 69T5luuI024179; Thu, 29 Oct 2026 13:47:56 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:58 +0800 X-Zmail-TransId: 3e816ae2de0e027-e63a9 X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 10/14] hw/riscv/riscv-iommu-accel: Add set/unset_iommu_device callback Date: Thu, 29 Oct 2026 13:47:47 +0800 Message-ID: <20261029054751.563493-11-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 69T5luuI024179 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.133 unknown Thu, 29 Oct 2026 13:48:04 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE14.000/4jFYDN22zvz5PkH3 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=160.30.148.34; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253088600154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei Implement the VFIO/PCI callbacks to attach and detach a HostIOMMUDevice to the vIOMMU when accel=3Don, following the ARM SMMUv3 accel device. In accelerated mode the guest IOMMU is backed by the host IOMMU via IOMMUFD, a vIOMMU object provides the per-VM, isolated handle to the physical IOMMU, so the attach callback allocates one when the first device attaches. Two proxy nested HWPTs (bypass and abort) are pre-allocated under the vIOMMU and one is installed based on the DDTP mode. The detach callback re-attaches the device to its default stage-2 HWPT and, once the last device detaches, releases the proxy HWPTs and the vIOMMU. Introduce RISCVIOMMUAccelState holding the vIOMMU object, the bypass/abort proxy HWPT IDs, and the attached device list. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu-accel.c | 164 +++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu-accel.h | 18 ++++ hw/riscv/riscv-iommu.h | 1 + hw/riscv/trace-events | 4 + 4 files changed, 187 insertions(+) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index 2bafbfa5e0..7b8dee5090 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -6,6 +6,7 @@ =20 #include "qemu/osdep.h" #include "qemu/error-report.h" +#include "trace.h" =20 #include "hw/pci/pci_bridge.h" #include "hw/pci-host/gpex.h" @@ -146,10 +147,172 @@ static uint64_t riscv_iommu_accel_get_viommu_flags(v= oid *opaque) return VIOMMU_FLAG_WANT_NESTING_PARENT; } =20 +static uint32_t riscv_iommu_accel_ddtp_hwpt(RISCVIOMMUState *s, + RISCVIOMMUAccelState *accel) +{ + unsigned mode =3D get_field(s->ddtp, RISCV_IOMMU_DDTP_MODE); + /* + * Initial proxy HWPT: with DDTP.Mode=3DOFF the IOMMU is + * disabled and all accesses abort; any other mode (Bare or a + * translation mode) starts with the bypass proxy until the guest + * programs a device context. + */ + return (mode =3D=3D RISCV_IOMMU_DDTP_MODE_OFF) ? + accel->abort_hwpt_id : accel->bypass_hwpt_id; +} + +static bool riscv_iommu_accel_alloc_viommu(RISCVIOMMUState *s, + HostIOMMUDeviceIOMMUFD *idev, + Error **errp) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + struct iommu_hwpt_riscv bypass_data =3D { + .dc[0] =3D RISCV_IOMMU_DC_TC_V, + .dc[3] =3D RISCV_IOMMU_DC_FSC_MODE_BARE, + }; + + struct iommu_hwpt_riscv abort_data =3D { + .dc[0] =3D 0, /* V =3D 0, invalid DC aborts */ + .dc[3] =3D 0, /* don't care */ + }; + + uint32_t s2_hwpt_id =3D idev->hwpt_id; + uint32_t viommu_id, hwpt_id; + IOMMUFDViommu *viommu; + + if (!iommufd_backend_alloc_viommu(idev->iommufd, idev->devid, + IOMMU_VIOMMU_TYPE_RISCV, s2_hwpt_id, + NULL, 0, &viommu_id, errp)) { + return false; + } + + viommu =3D g_new0(IOMMUFDViommu, 1); + viommu->viommu_id =3D viommu_id; + viommu->s2_hwpt_id =3D s2_hwpt_id; + viommu->iommufd =3D idev->iommufd; + accel->viommu =3D viommu; + + /* + * Pre-allocate HWPTs for the S1 bypass and abort cases, to be attached + * later based on a guest device context (DC) or the DDTP mode. + */ + if (!iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid, viommu_id, + 0, IOMMU_HWPT_DATA_RISCV_S1, + sizeof(abort_data), &abort_data, + &accel->abort_hwpt_id, errp)) { + goto free_viommu; + } + + if (!iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid, viommu_id, + 0, IOMMU_HWPT_DATA_RISCV_S1, + sizeof(bypass_data), &bypass_data, + &accel->bypass_hwpt_id, errp)) { + goto free_abort_hwpt; + } + + /* Attach a HWPT based on ddtp.iommu_mode value */ + hwpt_id =3D riscv_iommu_accel_ddtp_hwpt(s, accel); + if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_= id, + errp)) { + goto free_bypass_hwpt; + } + return true; + +free_bypass_hwpt: + iommufd_backend_free_id(idev->iommufd, accel->bypass_hwpt_id); +free_abort_hwpt: + iommufd_backend_free_id(idev->iommufd, accel->abort_hwpt_id); +free_viommu: + iommufd_backend_free_id(idev->iommufd, viommu->viommu_id); + g_free(viommu); + accel->viommu =3D NULL; + return false; +} + +static bool riscv_iommu_accel_set_iommu_device(PCIBus *bus, void *opaque, + int devfn, + HostIOMMUDevice *hiod, + Error **errp) +{ + HostIOMMUDeviceIOMMUFD *idev =3D HOST_IOMMU_DEVICE_IOMMUFD(hiod); + RISCVIOMMUState *s =3D (RISCVIOMMUState *) opaque; + RISCVIOMMUAccelDevice *accel_dev =3D riscv_iommu_accel_get_dev(s, bus,= devfn); + + if (!idev) { + return true; + } + + if (accel_dev->idev) { + if (accel_dev->idev !=3D idev) { + error_setg(errp, "Device already has an associated idev 0x%x", + idev->devid); + return false; + } + return true; + } + + if (s->accel_state->viommu) { + goto done; + } + + if (!riscv_iommu_accel_alloc_viommu(s, idev, errp)) { + error_append_hint(errp, "Unable to alloc vIOMMU: idev devid 0x%x: = ", + idev->devid); + return false; + } + +done: + accel_dev->idev =3D idev; + accel_dev->accel =3D s->accel_state; + QLIST_INSERT_HEAD(&s->accel_state->device_list, accel_dev, next); + trace_riscv_iommu_accel_set_iommu_device(devfn, idev->devid); + return true; +} + +static void riscv_iommu_accel_unset_iommu_device(PCIBus *bus, void *opaque, + int devfn) +{ + RISCVIOMMUState *s =3D (RISCVIOMMUState *) opaque; + HostIOMMUDeviceIOMMUFD *idev; + RISCVIOMMUAccelDevice *accel_dev; + RISCVIOMMUAccelState *accel; + RISCVIOMMUSpace *as; + + as =3D riscv_iommu_get_space(s, bus, devfn); + if (!as) { + return; + } + + accel_dev =3D container_of(as, RISCVIOMMUAccelDevice, as); + idev =3D accel_dev->idev; + accel =3D accel_dev->accel; + /* Re-attach the default s2 hwpt id */ + if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, + idev->hwpt_id, NULL)) { + error_report("Unable to attach the default HW pagetable: idev devi= d " + "0x%x", idev->devid); + } + + accel_dev->idev =3D NULL; + accel_dev->accel =3D NULL; + QLIST_REMOVE(accel_dev, next); + trace_riscv_iommu_accel_unset_iommu_device(devfn, idev->devid); + + if (QLIST_EMPTY(&accel->device_list)) { + iommufd_backend_free_id(accel->viommu->iommufd, accel->bypass_hwpt= _id); + iommufd_backend_free_id(accel->viommu->iommufd, accel->abort_hwpt_= id); + iommufd_backend_free_id(accel->viommu->iommufd, accel->viommu->vio= mmu_id); + g_free(accel->viommu); + accel->viommu =3D NULL; + } +} + static const PCIIOMMUOps riscv_iommu_accel_ops =3D { .supports_address_space =3D riscv_iommu_accel_supports_as, .get_address_space =3D riscv_iommu_accel_find_as, .get_viommu_flags =3D riscv_iommu_accel_get_viommu_flags, + .set_iommu_device =3D riscv_iommu_accel_set_iommu_device, + .unset_iommu_device =3D riscv_iommu_accel_unset_iommu_device, }; =20 static void riscv_iommu_accel_as_init(RISCVIOMMUState *s) @@ -170,6 +333,7 @@ static void riscv_iommu_accel_as_init(RISCVIOMMUState *= s) =20 void riscv_iommu_accel_init(RISCVIOMMUState *s) { + s->accel_state =3D g_new0(RISCVIOMMUAccelState, 1); s->iommu_ops =3D &riscv_iommu_accel_ops; riscv_iommu_accel_as_init(s); } diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h index c69fea87dd..49d022817c 100644 --- a/hw/riscv/riscv-iommu-accel.h +++ b/hw/riscv/riscv-iommu-accel.h @@ -8,10 +8,28 @@ #define HW_RISCV_RISCV_IOMMU_ACCEL_H =20 #include "hw/riscv/riscv-iommu.h" +#include "system/iommufd.h" +#ifdef CONFIG_LINUX +#include +#endif #include CONFIG_DEVICES =20 +/* + * Represents an accelerated IOMMU instance backed by an iommufd vIOMMU ob= ject. + * Holds bypass and abort proxy HWPT IDs used for device attachment. + */ +typedef struct RISCVIOMMUAccelState { + IOMMUFDViommu *viommu; + uint32_t bypass_hwpt_id; + uint32_t abort_hwpt_id; + QLIST_HEAD(, RISCVIOMMUAccelDevice) device_list; +} RISCVIOMMUAccelState; + typedef struct RISCVIOMMUAccelDevice { RISCVIOMMUSpace as; + HostIOMMUDeviceIOMMUFD *idev; + QLIST_ENTRY(RISCVIOMMUAccelDevice) next; + RISCVIOMMUAccelState *accel; } RISCVIOMMUAccelDevice; =20 #ifdef CONFIG_RISCV_IOMMU_ACCEL diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 92eeb18386..3262213093 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -114,6 +114,7 @@ struct RISCVIOMMUState { =20 /* nested iommu support */ bool accel; + struct RISCVIOMMUAccelState *accel_state; }; =20 void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus, diff --git a/hw/riscv/trace-events b/hw/riscv/trace-events index b50b14a654..9834f108b9 100644 --- a/hw/riscv/trace-events +++ b/hw/riscv/trace-events @@ -24,3 +24,7 @@ riscv_iommu_hpm_incr_ctr(uint64_t cntr_val) "cntr_val 0x%= "PRIx64 riscv_iommu_hpm_iocntinh_cy(bool prev_cy_inh) "prev_cy_inh %d" riscv_iommu_hpm_cycle_write(uint32_t ovf, uint64_t val) "ovf 0x%x val 0x%"= PRIx64 riscv_iommu_hpm_evt_write(uint32_t ctr_idx, uint32_t ovf, uint64_t val) "c= tr_idx 0x%x ovf 0x%x val 0x%"PRIx64 + +#riscv-iommu-accel.c +riscv_iommu_accel_set_iommu_device(int devfn, uint32_t devid) "devfn=3D0x%= x (idev devid=3D0x%x)" +riscv_iommu_accel_unset_iommu_device(int devfn, uint32_t devid) "devfn=3D0= x%x (idev devid=3D0x%x)" --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 179325304620882.30768302982983; Wed, 28 Oct 2026 22:50:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0G-0007DP-Vh; Thu, 29 Oct 2026 01:48:45 -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 1xMIzm-00073J-AW; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mxct.zte.com.cn ([183.62.165.209]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzh-0000eN-GP; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4jFYDM5sZ9z5B14p; Thu, 29 Oct 2026 13:48:03 +0800 (CST) Received: from szxl2zmapp05.zte.com.cn ([10.1.32.37]) by mse-fl1.zte.com.cn with SMTP id 69T5lvgl079303; Thu, 29 Oct 2026 13:47:57 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:59 +0800 X-Zmail-TransId: 3e816ae2de0e027-e63bb X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 11/14] hw/riscv/riscv-iommu-accel: Add nested vDC install/uninstall support Date: Thu, 29 Oct 2026 13:47:48 +0800 Message-ID: <20261029054751.563493-12-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5lvgl079303 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:48:03 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE13.003/4jFYDM5sZ9z5B14p 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=183.62.165.209; envelope-from=gong.shuai@sanechips.com.cn; helo=mxct.zte.com.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, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253047120158500 Content-Type: text/plain; charset="utf-8" A device behind an accelerated IOMMU must have a HWPT matching its guest device context, bypass, abort, or translate. The bypass and abort proxy HWPTs are pre-allocated; a translate HWPT needs a vDEVICE object first, which provides the virtual-to-physical devid mapping required for invalidations and event reporting. When the guest issues IODIR.INVAL_DDT, invalidate the context cache and re-install the HWPT matching the current DC. The DC is resolved straight from guest memory via riscv_iommu_ctx_fetch(), so a freshly written context always takes effect; a missing or invalid DC aborts the device's DMA. The translate HWPT is released when the device is detached. This follows the approach of the ARM SMMUv3. Signed-off-by: Gong Shuai Signed-off-by: Wu Fei --- hw/riscv/riscv-iommu-accel.c | 189 +++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu-accel.h | 18 ++++ hw/riscv/riscv-iommu.c | 34 ++++++- hw/riscv/riscv-iommu.h | 3 + hw/riscv/trace-events | 2 + 5 files changed, 241 insertions(+), 5 deletions(-) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index 7b8dee5090..f6cb2f77df 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -276,6 +276,7 @@ static void riscv_iommu_accel_unset_iommu_device(PCIBus= *bus, void *opaque, HostIOMMUDeviceIOMMUFD *idev; RISCVIOMMUAccelDevice *accel_dev; RISCVIOMMUAccelState *accel; + IOMMUFDVdev *vdev; RISCVIOMMUSpace *as; =20 as =3D riscv_iommu_get_space(s, bus, devfn); @@ -293,6 +294,20 @@ static void riscv_iommu_accel_unset_iommu_device(PCIBu= s *bus, void *opaque, "0x%x", idev->devid); } =20 + if (accel_dev->s1_hwpt) { + iommufd_backend_free_id(accel_dev->idev->iommufd, + accel_dev->s1_hwpt->hwpt_id); + g_free(accel_dev->s1_hwpt); + accel_dev->s1_hwpt =3D NULL; + } + + vdev =3D accel_dev->vdev; + if (vdev) { + iommufd_backend_free_id(accel->viommu->iommufd, vdev->vdevice_id); + g_free(vdev); + accel_dev->vdev =3D NULL; + } + accel_dev->idev =3D NULL; accel_dev->accel =3D NULL; QLIST_REMOVE(accel_dev, next); @@ -307,6 +322,180 @@ static void riscv_iommu_accel_unset_iommu_device(PCIB= us *bus, void *opaque, } } =20 +static bool riscv_iommu_accel_alloc_vdev(RISCVIOMMUAccelDevice *accel_dev, + int devid, Error **errp) +{ + RISCVIOMMUAccelState *accel =3D accel_dev->accel; + HostIOMMUDeviceIOMMUFD *idev =3D accel_dev->idev; + IOMMUFDVdev *vdev =3D accel_dev->vdev; + uint32_t vdevice_id; + + if (!idev || vdev) { + return true; + } + + if (!iommufd_backend_alloc_vdev(idev->iommufd, idev->devid, + accel->viommu->viommu_id, devid, + &vdevice_id, errp)) { + return false; + } + + vdev =3D g_new(IOMMUFDVdev, 1); + vdev->vdevice_id =3D vdevice_id; + vdev->virt_id =3D devid; + accel_dev->vdev =3D vdev; + return true; +} + +static RISCVIOMMUS1Hwpt * +riscv_iommu_accel_dev_alloc_translate(RISCVIOMMUAccelDevice *accel_dev, + RISCVIOMMUContext *ctx, Error **errp) +{ + HostIOMMUDeviceIOMMUFD *idev =3D accel_dev->idev; + RISCVIOMMUAccelState *accel =3D accel_dev->accel; + struct iommu_hwpt_riscv nested_data =3D { + .dc[0] =3D ctx->tc, + .dc[1] =3D ctx->gatp, + .dc[2] =3D ctx->ta, + .dc[3] =3D ctx->satp, + }; + uint32_t hwpt_id =3D 0, flags =3D 0; + RISCVIOMMUS1Hwpt *s1_hwpt; + + if (!iommufd_backend_alloc_hwpt(idev->iommufd, idev->devid, + accel->viommu->viommu_id, flags, + IOMMU_HWPT_DATA_RISCV_S1, + sizeof(nested_data), &nested_data, + &hwpt_id, errp)) { + return NULL; + } + + s1_hwpt =3D g_new0(RISCVIOMMUS1Hwpt, 1); + s1_hwpt->hwpt_id =3D hwpt_id; + trace_riscv_iommu_accel_translate_dc(accel_dev->vdev->virt_id, hwpt_id, + ctx->tc, ctx->satp); + return s1_hwpt; +} + +bool riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + RISCVIOMMUAccelDevice *accel_dev; + HostIOMMUDeviceIOMMUFD *idev; + uint32_t hwpt_id =3D 0; + RISCVIOMMUS1Hwpt *s1_hwpt =3D NULL; + const char *type; + RISCVIOMMUContext ctx; + RISCVIOMMUSpace *as; + Error *local_err =3D NULL; + Error **errp =3D &local_err; + + /* + * Nothing to install when accelerated mode is inactive, or when no + * vIOMMU has been allocated yet. + */ + if (!accel || !accel->viommu) { + return true; + } + + as =3D riscv_iommu_get_space_from_devid(s, devid); + if (!as) { + return true; + } + + accel_dev =3D container_of(as, RISCVIOMMUAccelDevice, as); + if (!accel_dev->accel) { + return true; + } + + idev =3D accel_dev->idev; + if (!riscv_iommu_accel_alloc_vdev(accel_dev, devid, errp)) { + goto err; + } + + /* + * Resolve the DC straight from guest memory so INVAL_DDT sees the + * freshly written context. Abort the device's DMA if the DC is + * missing or invalid. + * + * The vdev stays attached even if HWPT alloc/attach fails, since the + * Guest-Host DEVID mapping remains valid. + */ + if (riscv_iommu_ctx_fetch(s, devid, 0, &ctx)) { + /* DC missing or invalid, abort all device DMA */ + hwpt_id =3D accel->abort_hwpt_id; + } else if (get_field(ctx.satp, RISCV_IOMMU_DC_FSC_MODE) =3D=3D + RISCV_IOMMU_DC_FSC_MODE_BARE) { + hwpt_id =3D accel->bypass_hwpt_id; + } else { + s1_hwpt =3D riscv_iommu_accel_dev_alloc_translate(accel_dev, &ctx,= errp); + if (!s1_hwpt) { + goto err; + } + hwpt_id =3D s1_hwpt->hwpt_id; + } + + if (!hwpt_id) { + error_setg(errp, "Invalid DC for device 0x%x", devid); + goto err; + } + + if (!host_iommu_device_iommufd_attach_hwpt(idev, IOMMU_NO_PASID, hwpt_= id, + errp)) { + if (s1_hwpt) { + iommufd_backend_free_id(idev->iommufd, s1_hwpt->hwpt_id); + g_free(s1_hwpt); + } + error_append_hint(errp, "fail to attach hwpt for device 0x%x", dev= id); + goto err; + } + + /* Free the previous s1_hwpt */ + if (accel_dev->s1_hwpt) { + iommufd_backend_free_id(idev->iommufd, accel_dev->s1_hwpt->hwpt_id= ); + g_free(accel_dev->s1_hwpt); + } + + accel_dev->s1_hwpt =3D s1_hwpt; + if (hwpt_id =3D=3D accel->abort_hwpt_id) { + type =3D "abort"; + } else if (hwpt_id =3D=3D accel->bypass_hwpt_id) { + type =3D "bypass"; + } else { + type =3D "translate"; + } + + trace_riscv_iommu_accel_install_dc(devid, type, hwpt_id); + return true; + +err: + if (local_err) { + error_report_err(local_err); + } + return false; +} + +bool riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + RISCVIOMMUAccelDevice *accel_dev; + bool all_ok =3D true; + + if (!accel || !accel->viommu) { + return true; + } + + QLIST_FOREACH(accel_dev, &accel->device_list, next) { + uint32_t devid =3D riscv_iommu_space_devid(&accel_dev->as); + if (!riscv_iommu_accel_install_dc(s, devid)) { + error_report("Device 0x%x: Failed to install DC", devid); + all_ok =3D false; + } + } + + return all_ok; +} + static const PCIIOMMUOps riscv_iommu_accel_ops =3D { .supports_address_space =3D riscv_iommu_accel_supports_as, .get_address_space =3D riscv_iommu_accel_find_as, diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h index 49d022817c..85a1ab6d42 100644 --- a/hw/riscv/riscv-iommu-accel.h +++ b/hw/riscv/riscv-iommu-accel.h @@ -25,19 +25,37 @@ typedef struct RISCVIOMMUAccelState { QLIST_HEAD(, RISCVIOMMUAccelDevice) device_list; } RISCVIOMMUAccelState; =20 +typedef struct RISCVIOMMUS1Hwpt { + uint32_t hwpt_id; +} RISCVIOMMUS1Hwpt; + typedef struct RISCVIOMMUAccelDevice { RISCVIOMMUSpace as; HostIOMMUDeviceIOMMUFD *idev; + RISCVIOMMUS1Hwpt *s1_hwpt; + IOMMUFDVdev *vdev; QLIST_ENTRY(RISCVIOMMUAccelDevice) next; RISCVIOMMUAccelState *accel; } RISCVIOMMUAccelDevice; =20 #ifdef CONFIG_RISCV_IOMMU_ACCEL void riscv_iommu_accel_init(RISCVIOMMUState *s); +bool riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid); +bool riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s); #else static inline void riscv_iommu_accel_init(RISCVIOMMUState *s) { } +static inline bool +riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid) +{ + return true; +} +static inline bool +riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s) +{ + return true; +} #endif =20 #endif /* HW_RISCV_RISCV_IOMMU_ACCEL_H */ diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index fbc7f72cb6..fa751d566f 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -65,7 +65,7 @@ struct RISCVIOMMUEntry { /* IOMMU index for transactions without process_id specified. */ #define RISCV_IOMMU_NOPROCID 0 =20 -static uint32_t riscv_iommu_space_devid(RISCVIOMMUSpace *as) +uint32_t riscv_iommu_space_devid(RISCVIOMMUSpace *as) { uint32_t devid =3D PCI_BUILD_BDF(pci_bus_num(as->bus), as->devfn); =20 @@ -1555,6 +1555,19 @@ RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUSta= te *s, PCIBus *bus, int devf return as; } =20 +RISCVIOMMUSpace *riscv_iommu_get_space_from_devid(RISCVIOMMUState *s, + uint32_t devid) +{ + RISCVIOMMUSpace *as; + QLIST_FOREACH(as, &s->spaces, list) { + if (riscv_iommu_space_devid(as) =3D=3D devid) { + break; + } + } + + return as; +} + /* Find or allocate address space for a given device */ AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn) { @@ -2083,19 +2096,30 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUS= tate *s) =20 case RISCV_IOMMU_CMD(RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT, RISCV_IOMMU_CMD_IODIR_OPCODE): + { + uint32_t devid =3D get_field(cmd.dword0, RISCV_IOMMU_CMD_IODIR= _DID); + if (!(cmd.dword0 & RISCV_IOMMU_CMD_IODIR_DV)) { - /* invalidate all device context cache mappings */ + /* DV=3D0, invalidate and re-install every accelerated dev= ice */ ctx_func =3D riscv_iommu_ctx_inval_all; + riscv_iommu_ctx_inval(s, ctx_func, devid, 0); + if (!riscv_iommu_accel_install_dc_all(s)) { + goto cmd_ill; + } } else { - /* invalidate all device context matching DID */ + /* DV=3D1, invalidate and re-install device matching DID */ ctx_func =3D riscv_iommu_ctx_inval_devid; + riscv_iommu_ctx_inval(s, ctx_func, devid, 0); + if (!riscv_iommu_accel_install_dc(s, devid)) { + goto cmd_ill; + } } - riscv_iommu_ctx_inval(s, ctx_func, - get_field(cmd.dword0, RISCV_IOMMU_CMD_IODIR_DID), 0); break; + } =20 case RISCV_IOMMU_CMD(RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_PDT, RISCV_IOMMU_CMD_IODIR_OPCODE): + /* FIXME: add support for PDT inval for vIOMMU */ if (!(cmd.dword0 & RISCV_IOMMU_CMD_IODIR_DV)) { /* illegal command arguments IODIR_PDT & DV =3D=3D 0 */ goto cmd_ill; diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index 3262213093..ddd339aed1 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -131,6 +131,9 @@ void riscv_iommu_init_space(RISCVIOMMUState *s, RISCVIO= MMUSpace *as, PCIBus *bus, int devfn); AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn= ); RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, in= t devfn); +RISCVIOMMUSpace *riscv_iommu_get_space_from_devid(RISCVIOMMUState *s, + uint32_t devid); +uint32_t riscv_iommu_space_devid(RISCVIOMMUSpace *as); =20 typedef struct RISCVIOMMUContext RISCVIOMMUContext; /* Device translation context state. */ diff --git a/hw/riscv/trace-events b/hw/riscv/trace-events index 9834f108b9..55f7d1f0f3 100644 --- a/hw/riscv/trace-events +++ b/hw/riscv/trace-events @@ -28,3 +28,5 @@ riscv_iommu_hpm_evt_write(uint32_t ctr_idx, uint32_t ovf,= uint64_t val) "ctr_idx #riscv-iommu-accel.c riscv_iommu_accel_set_iommu_device(int devfn, uint32_t devid) "devfn=3D0x%= x (idev devid=3D0x%x)" riscv_iommu_accel_unset_iommu_device(int devfn, uint32_t devid) "devfn=3D0= x%x (idev devid=3D0x%x)" +riscv_iommu_accel_translate_dc(uint32_t vsid, uint32_t hwpt_id, uint64_t t= c, uint64_t iosatp) "vSID=3D0x%x hwpt_id=3D0x%x dc=3D%"PRIx64":%"PRIx64 +riscv_iommu_accel_install_dc(uint32_t vsid, const char * type, uint32_t hw= pt_id) "vSID=3D0x%x dc type=3D%s hwpt_id=3D0x%x" --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793253033513582.3727565882145; Wed, 28 Oct 2026 22:50:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ14-0007zb-0e; Thu, 29 Oct 2026 01:49: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 1xMIzl-00073G-Sh; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mxhk.zte.com.cn ([160.30.148.35]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzi-0000eq-Jj; Thu, 29 Oct 2026 01:48:13 -0400 Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDS2TPqz8Xrrs; Thu, 29 Oct 2026 13:48:08 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl2.zte.com.cn with SMTP id 69T5lw1L024181; Thu, 29 Oct 2026 13:47:58 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:47:59 +0800 X-Zmail-TransId: 3e816ae2de0f027-e63cc X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 12/14] hw/riscv/riscv-iommu-accel: Add support to issue invalidation cmd to host Date: Thu, 29 Oct 2026 13:47:49 +0800 Message-ID: <20261029054751.563493-13-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 69T5lw1L024181 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.133 unknown Thu, 29 Oct 2026 13:48:08 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE18.000/4jFYDS2TPqz8Xrrs 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=160.30.148.35; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253035817154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei Provide a helper to forward guest IOTLB invalidations to the host RISC-V IOMMU, and use it in the IOTINVAL command path so the accelerated translation caches stay in sync with the guest. Only one command is issued at a time for now. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu-accel.c | 44 ++++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu-accel.h | 8 +++++++ hw/riscv/riscv-iommu.c | 3 +++ 3 files changed, 55 insertions(+) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index f6cb2f77df..41de003f62 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -5,6 +5,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/bswap.h" #include "qemu/error-report.h" #include "trace.h" =20 @@ -161,6 +162,49 @@ static uint32_t riscv_iommu_accel_ddtp_hwpt(RISCVIOMMU= State *s, accel->abort_hwpt_id : accel->bypass_hwpt_id; } =20 +/* + * Issue the invalidation command to the host RISC-V IOMMU. + * + * The RISC-V IOTINVAL command carries no device (like ARM's SID) + * information, only (addr, gscid, pscid), so no per-device filtering + * is needed here. + */ +bool riscv_iommu_accel_issue_inv_cmd(RISCVIOMMUState *s, + const struct riscv_iommu_command *cmd) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + struct iommu_viommu_riscv_invalidate inv =3D { + .cmd =3D { cpu_to_le64(cmd->dword0), cpu_to_le64(cmd->dword1) }, + }; + uint32_t entry_num =3D 1; + bool ret; + Error *local_err =3D NULL; + + /* The uAPI entry must stay exactly one hardware command word */ + QEMU_BUILD_BUG_ON(sizeof(inv) !=3D sizeof(*cmd)); + + /* + * No accel or viommu means no VFIO/IOMMUFD devices, nothing to + * invalidate. + */ + if (!accel || !accel->viommu) { + return true; + } + + /* Single command (entry_num =3D 1); no need to check returned entry_n= um */ + ret =3D iommufd_backend_invalidate_cache(accel->viommu->iommufd, + accel->viommu->viommu_id, + IOMMU_VIOMMU_INVALIDATE_DATA_RI= SCV, + sizeof(inv), &entry_num, &inv, + &local_err); + + if (local_err) { + error_report_err(local_err); + } + + return ret; +} + static bool riscv_iommu_accel_alloc_viommu(RISCVIOMMUState *s, HostIOMMUDeviceIOMMUFD *idev, Error **errp) diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h index 85a1ab6d42..55d3c0dc62 100644 --- a/hw/riscv/riscv-iommu-accel.h +++ b/hw/riscv/riscv-iommu-accel.h @@ -42,6 +42,8 @@ typedef struct RISCVIOMMUAccelDevice { void riscv_iommu_accel_init(RISCVIOMMUState *s); bool riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int devid); bool riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s); +bool riscv_iommu_accel_issue_inv_cmd(RISCVIOMMUState *s, + const struct riscv_iommu_command *cmd= ); #else static inline void riscv_iommu_accel_init(RISCVIOMMUState *s) { @@ -56,6 +58,12 @@ riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s) { return true; } +static inline bool +riscv_iommu_accel_issue_inv_cmd(RISCVIOMMUState *s, + const struct riscv_iommu_command *cmd) +{ + return true; +} #endif =20 #endif /* HW_RISCV_RISCV_IOMMU_ACCEL_H */ diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index fa751d566f..4e92f3b15c 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -2091,6 +2091,9 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUSta= te *s) } =20 riscv_iommu_iot_inval(s, iot_func, gscid, pscid, iova, transta= g); + if (!riscv_iommu_accel_issue_inv_cmd(s, &cmd)) { + goto cmd_ill; + } break; } =20 --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793253004378240.31330261493827; Wed, 28 Oct 2026 22:50:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ06-00076s-Jt; Thu, 29 Oct 2026 01:48:34 -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 1xMIzm-00073I-An; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mxct.zte.com.cn ([183.62.165.209]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzh-0000eR-Hm; Thu, 29 Oct 2026 01:48:13 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4jFYDN2HdYz5B14q; Thu, 29 Oct 2026 13:48:04 +0800 (CST) Received: from szxlzmapp01.zte.com.cn ([10.5.231.85]) by mse-fl1.zte.com.cn with SMTP id 69T5lwuR079330; Thu, 29 Oct 2026 13:47:58 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:48:00 +0800 X-Zmail-TransId: 3e816ae2de0f027-e63d9 X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 13/14] hw/riscv/riscv-iommu-accel: Get host RISC-V IOMMU hw info and validate Date: Thu, 29 Oct 2026 13:47:50 +0800 Message-ID: <20261029054751.563493-14-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5lwuR079330 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:48:04 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE14.000/4jFYDN2HdYz5B14q 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=183.62.165.209; envelope-from=gong.shuai@sanechips.com.cn; helo=mxct.zte.com.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, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253015637154100 Content-Type: text/plain; charset="utf-8" Fetch the host IOMMU hardware info before attaching a device and reject a host IOMMU that cannot do S1 translation, following the ARM SMMUv3 accel device. The guest-visible capabilities are finalised once the machine initialisation is done, the last write to the capabilities register happening before the guest can observe it, host-controlled bits are intersected with what every attached host IOMMU supports, while QEMU-only properties are left as-is. A device attached after that point whose host IOMMU lacks an already advertised capability is refused. Signed-off-by: Gong Shuai Signed-off-by: Wu Fei --- hw/riscv/riscv-iommu-accel.c | 145 ++++++++++++++++++++++++++++++++++- hw/riscv/riscv-iommu-accel.h | 12 +++ hw/riscv/riscv-iommu.c | 5 ++ 3 files changed, 161 insertions(+), 1 deletion(-) diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c index 41de003f62..b1582e7d8f 100644 --- a/hw/riscv/riscv-iommu-accel.c +++ b/hw/riscv/riscv-iommu-accel.c @@ -7,6 +7,7 @@ #include "qemu/osdep.h" #include "qemu/bswap.h" #include "qemu/error-report.h" +#include "system/system.h" #include "trace.h" =20 #include "hw/pci/pci_bridge.h" @@ -25,6 +26,120 @@ static MemoryRegion root, sysmem; static AddressSpace *shared_as_sysmem; =20 +/* All S1 translation modes. */ +#define RISCV_IOMMU_CAP_S1_MODES \ + (RISCV_IOMMU_CAP_SV32 | RISCV_IOMMU_CAP_SV39 | \ + RISCV_IOMMU_CAP_SV48 | RISCV_IOMMU_CAP_SV57) + +/* + * Advertised only if the host IOMMU supports them, since the host + * performs the S1 translation. Finalised when the machine initialisation + * completes. Currently just the S1 modes. + */ +#define RISCV_IOMMU_ACCEL_HOST_CAP_MASK RISCV_IOMMU_CAP_S1_MODES + +static bool +riscv_iommu_accel_check_hw_compatible(RISCVIOMMUState *s, + struct iommu_hw_info_riscv *info, + Error **errp) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + + /* + * S1 translation is required for accelerated mode; reject a host + * IOMMU that cannot do any S1 mode. + */ + if (!(info->capabilities & RISCV_IOMMU_CAP_S1_MODES)) { + error_setg(errp, "Host RISC-V IOMMU does not support S1 translatio= n"); + return false; + } + + /* + * Once the capabilities are finalised the register is read-only from + * the guest point of view, a device whose host IOMMU cannot provide + * an advertised capability cannot be attached. + * + * Reachable only by a hotplugged device whose host IOMMU is weaker + * than the finalised vIOMMU capabilities; A hotplug sharing the + * cold-plug host never hits this. + */ + if (accel->finalised && + (s->cap & RISCV_IOMMU_ACCEL_HOST_CAP_MASK) & + ~info->capabilities) { + error_setg(errp, "Host RISC-V IOMMU capabilities (0x%"PRIx64") " + "do not provide the advertised capabilities (0x%"PRIx64= ")", + (uint64_t)info->capabilities, + (uint64_t)(s->cap & RISCV_IOMMU_ACCEL_HOST_CAP_MASK)); + return false; + } + + return true; +} + +static bool riscv_iommu_accel_hw_compatible(RISCVIOMMUState *s, + HostIOMMUDeviceIOMMUFD *idev, + uint64_t *host_caps, + Error **errp) +{ + struct iommu_hw_info_riscv info; + uint32_t data_type =3D IOMMU_HW_INFO_TYPE_RISCV; + uint64_t caps; + + if (!iommufd_backend_get_device_info(idev->iommufd, idev->devid, + &data_type, &info, sizeof(info), + &caps, NULL, errp)) { + return false; + } + + if (data_type !=3D IOMMU_HW_INFO_TYPE_RISCV) { + error_setg(errp, "Wrong data type (%d) for Host RISC-V IOMMU devic= e info", + data_type); + return false; + } + + if (!riscv_iommu_accel_check_hw_compatible(s, &info, errp)) { + return false; + } + + *host_caps =3D info.capabilities; + return true; +} + +/* + * Finalise the guest-visible capabilities register. Runs at machine init + * done and before the guest may observe the register. This is the last + * write to the register. + */ +static void riscv_iommu_accel_finalize_caps(RISCVIOMMUState *s) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + RISCVIOMMUAccelDevice *accel_dev; + uint64_t host_caps =3D ~0ULL; + + QLIST_FOREACH(accel_dev, &accel->device_list, next) { + host_caps &=3D accel_dev->host_caps; + } + + s->cap &=3D host_caps | ~RISCV_IOMMU_ACCEL_HOST_CAP_MASK; + accel->finalised =3D true; + + if (!(s->cap & RISCV_IOMMU_CAP_S1_MODES)) { + error_report("riscv-iommu: host IOMMUs of accelerated devices do " + "not share a common S1 translation mode"); + exit(1); + } + + stq_le_p(&s->regs[RISCV_IOMMU_REG_CAP], s->cap); +} + +static void riscv_iommu_accel_machine_done(Notifier *notifier, void *data) +{ + RISCVIOMMUAccelState *accel =3D container_of(notifier, RISCVIOMMUAccel= State, + machine_done); + + riscv_iommu_accel_finalize_caps(accel->iommu); +} + static RISCVIOMMUAccelDevice *riscv_iommu_accel_get_dev(RISCVIOMMUState *s, PCIBus *bus, int d= evfn) { @@ -281,6 +396,7 @@ static bool riscv_iommu_accel_set_iommu_device(PCIBus *= bus, void *opaque, HostIOMMUDeviceIOMMUFD *idev =3D HOST_IOMMU_DEVICE_IOMMUFD(hiod); RISCVIOMMUState *s =3D (RISCVIOMMUState *) opaque; RISCVIOMMUAccelDevice *accel_dev =3D riscv_iommu_accel_get_dev(s, bus,= devfn); + uint64_t host_caps; =20 if (!idev) { return true; @@ -295,6 +411,16 @@ static bool riscv_iommu_accel_set_iommu_device(PCIBus = *bus, void *opaque, return true; } =20 + /* + * Check the host IOMMU associated with the dev is compatible with the + * QEMU RISC-V IOMMU accel. The reported host_caps will be intersected + * into the final capabilities register when the machine initialisation + * completes. + */ + if (!riscv_iommu_accel_hw_compatible(s, idev, &host_caps, errp)) { + return false; + } + if (s->accel_state->viommu) { goto done; } @@ -306,6 +432,7 @@ static bool riscv_iommu_accel_set_iommu_device(PCIBus *= bus, void *opaque, } =20 done: + accel_dev->host_caps =3D host_caps; accel_dev->idev =3D idev; accel_dev->accel =3D s->accel_state; QLIST_INSERT_HEAD(&s->accel_state->device_list, accel_dev, next); @@ -566,7 +693,23 @@ static void riscv_iommu_accel_as_init(RISCVIOMMUState = *s) =20 void riscv_iommu_accel_init(RISCVIOMMUState *s) { - s->accel_state =3D g_new0(RISCVIOMMUAccelState, 1); + RISCVIOMMUAccelState *accel =3D g_new0(RISCVIOMMUAccelState, 1); + + accel->iommu =3D s; + s->accel_state =3D accel; s->iommu_ops =3D &riscv_iommu_accel_ops; riscv_iommu_accel_as_init(s); } + +/* + * Register the machine-done notifier. Kept separate from + * riscv_iommu_accel_init() so that registration happens only after + * riscv_iommu_realize() has done. + */ +void riscv_iommu_accel_register_machine_done(RISCVIOMMUState *s) +{ + RISCVIOMMUAccelState *accel =3D s->accel_state; + + accel->machine_done.notify =3D riscv_iommu_accel_machine_done; + qemu_add_machine_init_done_notifier(&accel->machine_done); +} diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h index 55d3c0dc62..5d42575747 100644 --- a/hw/riscv/riscv-iommu-accel.h +++ b/hw/riscv/riscv-iommu-accel.h @@ -7,6 +7,7 @@ #ifndef HW_RISCV_RISCV_IOMMU_ACCEL_H #define HW_RISCV_RISCV_IOMMU_ACCEL_H =20 +#include "qemu/notify.h" #include "hw/riscv/riscv-iommu.h" #include "system/iommufd.h" #ifdef CONFIG_LINUX @@ -19,10 +20,15 @@ * Holds bypass and abort proxy HWPT IDs used for device attachment. */ typedef struct RISCVIOMMUAccelState { + RISCVIOMMUState *iommu; IOMMUFDViommu *viommu; uint32_t bypass_hwpt_id; uint32_t abort_hwpt_id; QLIST_HEAD(, RISCVIOMMUAccelDevice) device_list; + /* Machine init done notifier */ + Notifier machine_done; + /* Set once machine init is done */ + bool finalised; } RISCVIOMMUAccelState; =20 typedef struct RISCVIOMMUS1Hwpt { @@ -34,6 +40,8 @@ typedef struct RISCVIOMMUAccelDevice { HostIOMMUDeviceIOMMUFD *idev; RISCVIOMMUS1Hwpt *s1_hwpt; IOMMUFDVdev *vdev; + /* Host IOMMU capabilities */ + uint64_t host_caps; QLIST_ENTRY(RISCVIOMMUAccelDevice) next; RISCVIOMMUAccelState *accel; } RISCVIOMMUAccelDevice; @@ -44,6 +52,7 @@ bool riscv_iommu_accel_install_dc(RISCVIOMMUState *s, int= devid); bool riscv_iommu_accel_install_dc_all(RISCVIOMMUState *s); bool riscv_iommu_accel_issue_inv_cmd(RISCVIOMMUState *s, const struct riscv_iommu_command *cmd= ); +void riscv_iommu_accel_register_machine_done(RISCVIOMMUState *s); #else static inline void riscv_iommu_accel_init(RISCVIOMMUState *s) { @@ -64,6 +73,9 @@ riscv_iommu_accel_issue_inv_cmd(RISCVIOMMUState *s, { return true; } +static inline void riscv_iommu_accel_register_machine_done(RISCVIOMMUState= *s) +{ +} #endif =20 #endif /* HW_RISCV_RISCV_IOMMU_ACCEL_H */ diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 4e92f3b15c..7fa6490888 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -2849,6 +2849,11 @@ static void riscv_iommu_realize(DeviceState *dev, Er= ror **errp) timer_new_ns(QEMU_CLOCK_VIRTUAL, riscv_iommu_hpm_timer_cb, s); s->hpm_event_ctr_map =3D g_hash_table_new(g_direct_hash, g_direct_= equal); } + + /* Register the machine-done notifier last. */ + if (s->accel) { + riscv_iommu_accel_register_machine_done(s); + } } =20 static void riscv_iommu_unrealize(DeviceState *dev) --=20 2.53.0 From nobody Mon Nov 23 04:18:49 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 1793253067706954.3532250240123; Wed, 28 Oct 2026 22:51:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xMJ0H-0007Eh-Gr; Thu, 29 Oct 2026 01:48:45 -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 1xMIzl-00073H-SO; Thu, 29 Oct 2026 01:48:14 -0400 Received: from mxhk.zte.com.cn ([160.30.148.35]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xMIzh-0000eV-Tq; Thu, 29 Oct 2026 01:48:13 -0400 Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4jFYDR174mz8Xrkn; Thu, 29 Oct 2026 13:48:07 +0800 (CST) Received: from szxl2zmapp06.zte.com.cn ([10.1.32.108]) by mse-fl1.zte.com.cn with SMTP id 69T5lxsu079346; Thu, 29 Oct 2026 13:47:59 +0800 (+08) (envelope-from gong.shuai@sanechips.com.cn) Received: from ubuntu-35-gs (unknown [10.230.214.35]) by smtp (Zmail) with SMTP; Thu, 29 Oct 2026 13:48:00 +0800 X-Zmail-TransId: 3e816ae2de10027-e63e9 X-Zmail-LocalSMTP: 1 X-Zmail-RealSender: gong.shuai@sanechips.com.cn From: Gong Shuai To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , "Michael S. Tsirkin" , Cornelia Huck , Wu Fei , Qingye Zhao , Gong Shuai , Gong Shuai Subject: [RFC PATCH v2 14/14] hw/riscv/riscv-iommu-accel: Add accel property for iommu device Date: Thu, 29 Oct 2026 13:47:51 +0800 Message-ID: <20261029054751.563493-15-gong.shuai@sanechips.com.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> References: <20261029054751.563493-1-gong.shuai@sanechips.com.cn> MIME-Version: 1.0 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 69T5lxsu079346 X-TLS: YES X-ENVELOPE-SENDER: gong.shuai@sanechips.com.cn X-SOURCE-IP: 10.5.228.132 unknown Thu, 29 Oct 2026 13:48:07 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6AE2DE17.000/4jFYDR174mz8Xrkn 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=160.30.148.35; envelope-from=gong.shuai@sanechips.com.cn; helo=mxhk.zte.com.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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: 1793253070385154100 Content-Type: text/plain; charset="utf-8" From: Wu Fei Add an "accel" property to enable the RISC-V IOMMU accelerator mode, following the ARM SMMUv3 accel device. Migration is not supported in accel mode, as the host-side vIOMMU and HWPT state cannot be migrated; a migration blocker is registered. Signed-off-by: Wu Fei Signed-off-by: Gong Shuai --- hw/riscv/riscv-iommu.c | 57 ++++++++++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu.h | 1 + 2 files changed, 58 insertions(+) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 7fa6490888..4926a361f5 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -23,6 +23,7 @@ #include "hw/pci/pci_device.h" #include "hw/core/qdev-properties.h" #include "hw/riscv/riscv_hart.h" +#include "migration/blocker.h" #include "migration/vmstate.h" #include "qapi/error.h" #include "qemu/timer.h" @@ -2731,12 +2732,63 @@ static void riscv_iommu_instance_finalize(Object *o= bj) qemu_mutex_destroy(&s->iot_lock); } =20 +static bool riscv_iommu_validate_property(RISCVIOMMUState *s, Error **errp) +{ +#ifndef CONFIG_RISCV_IOMMU_ACCEL + if (s->accel) { + error_setg(errp, "accel=3Don support not compiled in"); + return false; + } +#endif + + if (!s->accel) { + return true; + } + + if (!s->enable_s_stage) { + error_setg(errp, + "Only stage-1 is supported for RISC-V IOMMU with accel= =3Don"); + return false; + } + + return true; +} + static void riscv_iommu_realize(DeviceState *dev, Error **errp) { RISCVIOMMUState *s =3D RISCV_IOMMU(dev); =20 + if (!riscv_iommu_validate_property(s, errp)) { + return; + } + if (s->accel) { riscv_iommu_accel_init(s); + /* + * With accel=3Don, MSI remapping is done by the host IOMMU. Disable + * the emulated MSI capability so the guest's MSI message address + * is a plain GPA translated on the host side; the guest then sees + * no MSI capability bits and uses the base device context format. + */ + s->enable_msi =3D false; + /* G-stage is managed by the host (VFIO) in accel mode */ + s->enable_g_stage =3D false; + /* + * No ATS.INVAL path exists for the vIOMMU in accel mode now, + * vfio-pci devices are not notifiers of the emulated IOMMU and + * guest ATS.INVAL commands are not forwarded to the host, so hide + * ATS/PRI capabilities. + */ + s->enable_ats =3D false; + /* + * Per-PASID contexts and PDT inval for vIOMMU are unimplemented, + * hide the process-directory capabilities. + */ + s->cap &=3D ~(RISCV_IOMMU_CAP_PD8 | RISCV_IOMMU_CAP_PD17 | + RISCV_IOMMU_CAP_PD20); + error_setg(&s->migration_blocker, "Migration not supported with " + "RISC-V IOMMU accel=3Don"); + migrate_add_blocker(&s->migration_blocker, errp); } =20 /* Report QEMU target physical address space limits. */ @@ -2860,6 +2912,10 @@ static void riscv_iommu_unrealize(DeviceState *dev) { RISCVIOMMUState *s =3D RISCV_IOMMU(dev); =20 + if (s->migration_blocker) { + migrate_del_blocker(&s->migration_blocker); + } + if (s->cap & RISCV_IOMMU_CAP_HPM) { g_hash_table_unref(s->hpm_event_ctr_map); timer_free(s->hpm_timer); @@ -2920,6 +2976,7 @@ static const Property riscv_iommu_properties[] =3D { TYPE_MEMORY_REGION, MemoryRegion *), DEFINE_PROP_UINT8("hpm-counters", RISCVIOMMUState, hpm_cntrs, RISCV_IOMMU_IOCOUNT_NUM), + DEFINE_PROP_BOOL("accel", RISCVIOMMUState, accel, FALSE), }; =20 static void riscv_iommu_class_init(ObjectClass *klass, const void *data) diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index ddd339aed1..cffff83411 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -115,6 +115,7 @@ struct RISCVIOMMUState { /* nested iommu support */ bool accel; struct RISCVIOMMUAccelState *accel_state; + Error *migration_blocker; }; =20 void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus, --=20 2.53.0