From patchwork Fri Apr 11 10:17:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 14048053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C9502C369A8 for ; Fri, 11 Apr 2025 10:22:13 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u3BVq-0002qv-IK; Fri, 11 Apr 2025 06:21:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVe-0002bX-0e for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:18 -0400 Received: from mgamail.intel.com ([198.175.65.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVT-0005O8-Nx for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1744366867; x=1775902867; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HDSjhWoNRiRif3N3R0QiTa26vrCzPQDa4qlgutcJNyQ=; b=Q51r4nunu81ybHsElx2jIXb98WCN7EIMUyG2Pzxb34btKYXuYJDtHGJI ArYQhgQ1NbaSmO3a0pvjsvKbpr/h+yLxYiDIEdt1MXV3BBDzyZCxGRtNe 9MderpwozUuabmgbPj6s9V9MzYWB9+HSfXuM2uD8uq0TUPdsq2M8VGZGr f954SZkTi/McFHkSWytp6YlHtNwUN3vwJpr/i7KvAmKYoqPc1ypX/bJtE jar8DAwucEWuYwI1pOxPsfFiaXiggQSh/oDI7nmv5aVPC0ZlIs3NJ90dz AH+vFdqAfJQ5QTyJgtV9P2hw2xxeglaHohtI7j1k3tf7HQ3OmYlWvDIIr w==; X-CSE-ConnectionGUID: zyO75PytTuqzTPKOy1+L9g== X-CSE-MsgGUID: BtSLvZ8BSaOsoR37dok+gg== X-IronPort-AV: E=McAfee;i="6700,10204,11400"; a="49566779" X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="49566779" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:48 -0700 X-CSE-ConnectionGUID: rLx+R/RrSjyBmrwGrbDKDg== X-CSE-MsgGUID: b+/cmEqZSj6QjggpoAxlEA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="133917410" Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:47 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Liu Subject: [PATCH 1/5] vfio/iommufd: Save host iommu capabilities in VFIODevice.caps Date: Fri, 11 Apr 2025 18:17:03 +0800 Message-Id: <20250411101707.3460429-2-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250411101707.3460429-1-zhenzhong.duan@intel.com> References: <20250411101707.3460429-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=198.175.65.15; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -49 X-Spam_score: -5.0 X-Spam_bar: ----- X-Spam_report: (-5.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.593, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The saved caps copy can be used to check dirty tracking capability. The capabilities is gotten through IOMMUFD interface, so define a new structure HostIOMMUDeviceIOMMUFDCaps which contains vendor caps raw data in "include/system/iommufd.h". This is a prepare work for moving .realize() after .attach_device(). Suggested-by: Cédric Le Goater Suggested-by: Eric Auger Suggested-by: Nicolin Chen Signed-off-by: Zhenzhong Duan --- include/hw/vfio/vfio-device.h | 1 + include/system/iommufd.h | 22 ++++++++++++++++++++++ hw/vfio/iommufd.c | 10 +++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 66797b4c92..09a7af891a 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -77,6 +77,7 @@ typedef struct VFIODevice { bool dirty_tracking; /* Protected by BQL */ bool iommu_dirty_tracking; HostIOMMUDevice *hiod; + HostIOMMUDeviceIOMMUFDCaps caps; int devid; IOMMUFDBackend *iommufd; VFIOIOASHwpt *hwpt; diff --git a/include/system/iommufd.h b/include/system/iommufd.h index cbab75bfbf..0f337585c9 100644 --- a/include/system/iommufd.h +++ b/include/system/iommufd.h @@ -18,6 +18,9 @@ #include "exec/hwaddr.h" #include "exec/cpu-common.h" #include "system/host_iommu_device.h" +#ifdef CONFIG_LINUX +#include +#endif #define TYPE_IOMMUFD_BACKEND "iommufd" OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass, IOMMUFD_BACKEND) @@ -63,4 +66,23 @@ bool iommufd_backend_get_dirty_bitmap(IOMMUFDBackend *be, uint32_t hwpt_id, Error **errp); #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd" + +typedef union VendorCaps { + struct iommu_hw_info_vtd vtd; + struct iommu_hw_info_arm_smmuv3 smmuv3; +} VendorCaps; + +/** + * struct HostIOMMUDeviceIOMMUFDCaps - Define host IOMMU device capabilities. + * + * @type: host platform IOMMU type. + * + * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this represents + * the @out_capabilities value returned from IOMMU_GET_HW_INFO ioctl) + */ +typedef struct HostIOMMUDeviceIOMMUFDCaps { + uint32_t type; + uint64_t hw_caps; + VendorCaps vendor_caps; +} HostIOMMUDeviceIOMMUFDCaps; #endif diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 48db105422..530cde6740 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -324,7 +324,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, * vfio_migration_realize() may decide to use VF dirty tracking * instead. */ - if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) { + if (vbasedev->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) { flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING; } @@ -475,6 +475,7 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, int ret, devfd; uint32_t ioas_id; Error *err = NULL; + HostIOMMUDeviceIOMMUFDCaps *caps = &vbasedev->caps; const VFIOIOMMUClass *iommufd_vioc = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); @@ -505,6 +506,13 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, goto err_alloc_ioas; } + if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid, + &caps->type, &caps->vendor_caps, + sizeof(VendorCaps), &caps->hw_caps, + errp)) { + goto err_alloc_ioas; + } + /* try to attach to an existing container in this space */ QLIST_FOREACH(bcontainer, &space->containers, next) { container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); From patchwork Fri Apr 11 10:17:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 14048052 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF684C36010 for ; Fri, 11 Apr 2025 10:21:54 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u3BVn-0002jb-UT; Fri, 11 Apr 2025 06:21:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVe-0002bb-1I for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:18 -0400 Received: from mgamail.intel.com ([198.175.65.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVT-0005Op-4m for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1744366867; x=1775902867; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZALbs/dbjCI0xMs0gD6081Unyv17rpEdGJH3ZlKWYPs=; b=jYpnrTeqNf8NM6udXhDg55MNnwR9V3il5GQfpAHB5vXl/6+hgNgYNppf TtgaHMdHboTNBVzSlJOBikh2UhuKwFy9GgwqyjHHGLvETw5ry98HIClCC wzILOOCl5kcCrbrH5y0vt/nyK2MHP37V8ZYR0Z8zvnsGrlyPk6meGAWIL 2QCEK/FpflxONN75JkkWNVAwKK1Hs1hCLgahOHNqBFu+WV35Zyp3JJiHf xNNkmTYPOLc8Jd+iGIHc1UXr8Svt51IW7wHZxT/viQavjYsJOTGSdpklD 3BkonD0cqEbSUxJLDitGTVOXtTZRyCdJ60Kg3th/YyHzWrmsn48F+Ov0W Q==; X-CSE-ConnectionGUID: euIePaKvQfie1XeE6Wo85Q== X-CSE-MsgGUID: f6psmTnOSoqhANMdPfNhwQ== X-IronPort-AV: E=McAfee;i="6700,10204,11400"; a="49566790" X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="49566790" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:51 -0700 X-CSE-ConnectionGUID: F4JueCLbR1uihvjZ7OCpRg== X-CSE-MsgGUID: ax4RPBrBRte8OW+i6C7t/Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="133917413" Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:49 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, chao.p.peng@intel.com, Zhenzhong Duan , Donald Dutile Subject: [PATCH 2/5] vfio: Move realize() after attach_device() Date: Fri, 11 Apr 2025 18:17:04 +0800 Message-Id: <20250411101707.3460429-3-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250411101707.3460429-1-zhenzhong.duan@intel.com> References: <20250411101707.3460429-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=198.175.65.15; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -49 X-Spam_score: -5.0 X-Spam_bar: ----- X-Spam_report: (-5.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.593, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Previously device attaching depends on realize() getting host iommu capabilities to check dirty tracking support. Now we save a caps copy in VFIODevice and check that copy for dirty tracking support, there is no dependency any more, move realize() call after attach_device() call in vfio_device_attach(). Drop vfio_device_hiod_realize() which looks redundant now. Suggested-by: Cédric Le Goater Suggested-by: Donald Dutile Signed-off-by: Zhenzhong Duan --- include/hw/vfio/vfio-device.h | 1 - hw/vfio/container.c | 4 ---- hw/vfio/device.c | 28 +++++++++++----------------- hw/vfio/iommufd.c | 4 ---- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 09a7af891a..14559733c6 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -124,7 +124,6 @@ bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex void vfio_device_reset_handler(void *opaque); bool vfio_device_is_mdev(VFIODevice *vbasedev); -bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); bool vfio_device_attach(char *name, VFIODevice *vbasedev, AddressSpace *as, Error **errp); void vfio_device_detach(VFIODevice *vbasedev); diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 23a3373470..676e88cef4 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -883,10 +883,6 @@ static bool vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev, trace_vfio_device_attach(vbasedev->name, groupid); - if (!vfio_device_hiod_realize(vbasedev, errp)) { - return false; - } - group = vfio_group_get(groupid, as, errp); if (!group) { return false; diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 4de6948cf4..6154d3f443 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -347,17 +347,6 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev) return subsys && (strcmp(subsys, "/sys/bus/mdev") == 0); } -bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp) -{ - HostIOMMUDevice *hiod = vbasedev->hiod; - - if (!hiod) { - return true; - } - - return HOST_IOMMU_DEVICE_GET_CLASS(hiod)->realize(hiod, vbasedev, errp); -} - VFIODevice *vfio_get_vfio_device(Object *obj) { if (object_dynamic_cast(obj, TYPE_VFIO_PCI)) { @@ -372,6 +361,7 @@ bool vfio_device_attach(char *name, VFIODevice *vbasedev, { const VFIOIOMMUClass *ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY)); + HostIOMMUDeviceClass *hiodc; HostIOMMUDevice *hiod = NULL; if (vbasedev->iommufd) { @@ -380,16 +370,20 @@ bool vfio_device_attach(char *name, VFIODevice *vbasedev, assert(ops); + if (!ops->attach_device(name, vbasedev, as, errp)) { + return false; + } if (!vbasedev->mdev) { hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename)); - vbasedev->hiod = hiod; - } + hiodc = HOST_IOMMU_DEVICE_GET_CLASS(hiod); - if (!ops->attach_device(name, vbasedev, as, errp)) { - object_unref(hiod); - vbasedev->hiod = NULL; - return false; + if (!hiodc->realize(hiod, vbasedev, errp)) { + object_unref(hiod); + ops->detach_device(vbasedev); + return false; + } + vbasedev->hiod = hiod; } return true; diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 530cde6740..e05b472e35 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -502,10 +502,6 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, * FD to be connected and having a devid to be able to successfully call * iommufd_backend_get_device_info(). */ - if (!vfio_device_hiod_realize(vbasedev, errp)) { - goto err_alloc_ioas; - } - if (!iommufd_backend_get_device_info(vbasedev->iommufd, vbasedev->devid, &caps->type, &caps->vendor_caps, sizeof(VendorCaps), &caps->hw_caps, From patchwork Fri Apr 11 10:17:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 14048051 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6E4F6C369AA for ; Fri, 11 Apr 2025 10:21:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u3BVo-0002nM-W6; Fri, 11 Apr 2025 06:21:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVh-0002fE-J8 for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:21 -0400 Received: from mgamail.intel.com ([198.175.65.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVe-0005Op-F7 for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1744366878; x=1775902878; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pognV82UVdHK8qT+B92vH8V1D8/ilicX35LTb3dliZA=; b=Th5rqfua5bRnQ/FBbJYbgvGX/TOFJfyzempPvvOLGLW9AESL9P3bZXx/ mQ87WwGsgiIpMGuelX8CUVZIji9EDUw5mwPp+Rl7jj22yFyZa+G6nxI0d 3L5E1FwHatRahL/FdTW1bGvgu0lR13Q1LA2alDfHEqQJ1XnXoAns1jRLF NuELsHc8wWDTbTQ4EbSvWpsjyZKmiFjDeH/sDR+lyVHoJXz1TKWUpNNAk dJfJ8caPHmotu2ZrNJVGFz0cH3qTbPyZv1Gg09hf5VGWmNQ5L5GD8vhRJ 7BmzUV0lu7Vnai5IOUJGQQpcw+HMlTsETsutqNMWvY/6RH7GBaJR90H3i A==; X-CSE-ConnectionGUID: fwWKyU7jQ/O3SWT2EmUygg== X-CSE-MsgGUID: oqFqfiSmScGYBklweoJqbQ== X-IronPort-AV: E=McAfee;i="6700,10204,11400"; a="49566798" X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="49566798" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:53 -0700 X-CSE-ConnectionGUID: SdcHZi/MQeOse6DGf1o+4A== X-CSE-MsgGUID: rC2gTDqzRQe+XxHFcn7dmw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="133917417" Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:52 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Liu Subject: [PATCH 3/5] vfio/iommufd: Implement .get_cap() in TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO sub-class Date: Fri, 11 Apr 2025 18:17:05 +0800 Message-Id: <20250411101707.3460429-4-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250411101707.3460429-1-zhenzhong.duan@intel.com> References: <20250411101707.3460429-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=198.175.65.15; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -49 X-Spam_score: -5.0 X-Spam_bar: ----- X-Spam_report: (-5.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.593, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Now we have saved a copy of host iommu capabilities in VFIODevice, implemented hiod_iommufd_vfio_get_cap() by querying the caps copy in sub-class. This overrides .get_cap() implementation hiod_iommufd_vfio_get_cap() in TYPE_HOST_IOMMU_DEVICE_IOMMUFD parent class. Vendor caps are checked for a specific capability, e.g., for vtd, checking code will be in hiod_iommufd_get_vtd_cap(). This also fixes an issue that calling vfio_device_get_aw_bits() in TYPE_HOST_IOMMU_DEVICE_IOMMUFD parent class .get_cap(). Signed-off-by: Zhenzhong Duan --- include/system/iommufd.h | 4 ++++ backends/iommufd.c | 40 ++++++++++++++++++++++++++++++++++++++++ hw/vfio/iommufd.c | 16 ++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/include/system/iommufd.h b/include/system/iommufd.h index 0f337585c9..baba5ec1d8 100644 --- a/include/system/iommufd.h +++ b/include/system/iommufd.h @@ -85,4 +85,8 @@ typedef struct HostIOMMUDeviceIOMMUFDCaps { uint64_t hw_caps; VendorCaps vendor_caps; } HostIOMMUDeviceIOMMUFDCaps; + +int hiod_iommufd_get_common_cap(HostIOMMUDevice *hiod, + HostIOMMUDeviceIOMMUFDCaps *caps, + int cap, Error **errp); #endif diff --git a/backends/iommufd.c b/backends/iommufd.c index 9587e4d99b..54fa3174d0 100644 --- a/backends/iommufd.c +++ b/backends/iommufd.c @@ -355,3 +355,43 @@ static const TypeInfo types[] = { }; DEFINE_TYPES(types) + +static int hiod_iommufd_get_vtd_cap(HostIOMMUDevice *hiod, + struct iommu_hw_info_vtd *vtd, + int cap, Error **errp) +{ + /* TODO: Check vtd->cap_reg/ecap_reg for capability */ + error_setg(errp, "%s: unsupported capability %x", hiod->name, cap); + return -EINVAL; +} + +static int hiod_iommufd_get_vendor_cap(HostIOMMUDevice *hiod, + HostIOMMUDeviceIOMMUFDCaps *caps, + int cap, Error **errp) +{ + enum iommu_hw_info_type type = caps->type; + + switch (type) { + case IOMMU_HW_INFO_TYPE_INTEL_VTD: + return hiod_iommufd_get_vtd_cap(hiod, &caps->vendor_caps.vtd, + cap, errp); + case IOMMU_HW_INFO_TYPE_ARM_SMMUV3: + case IOMMU_HW_INFO_TYPE_NONE: + break; + } + + error_setg(errp, "%s: unsupported capability type %x", hiod->name, type); + return -EINVAL; +} + +int hiod_iommufd_get_common_cap(HostIOMMUDevice *hiod, + HostIOMMUDeviceIOMMUFDCaps *caps, + int cap, Error **errp) +{ + switch (cap) { + case HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE: + return caps->type; + default: + return hiod_iommufd_get_vendor_cap(hiod, caps, cap, errp); + } +} diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index e05b472e35..e7ca92f81f 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -833,6 +833,21 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, return true; } +static int hiod_iommufd_vfio_get_cap(HostIOMMUDevice *hiod, int cap, + Error **errp) +{ + VFIODevice *vdev = hiod->agent; + HostIOMMUDeviceIOMMUFDCaps *caps = &vdev->caps; + + /* VFIO has its own way to get aw_bits which may be different from VDPA */ + switch (cap) { + case HOST_IOMMU_DEVICE_CAP_AW_BITS: + return vfio_device_get_aw_bits(hiod->agent); + default: + return hiod_iommufd_get_common_cap(hiod, caps, cap, errp); + } +} + static GList * hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod) { @@ -857,6 +872,7 @@ static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); hiodc->realize = hiod_iommufd_vfio_realize; + hiodc->get_cap = hiod_iommufd_vfio_get_cap; hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask; }; From patchwork Fri Apr 11 10:17:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 14048055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 66499C369A8 for ; Fri, 11 Apr 2025 10:22:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u3BVs-0002v0-6z; Fri, 11 Apr 2025 06:21:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVh-0002fG-KN for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:21 -0400 Received: from mgamail.intel.com ([198.175.65.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVe-0005O8-F9 for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1744366878; x=1775902878; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/rp/9HOUYDxv/ff9Y/ZSJzLtkR8kKGoMvG5rWR7Fdcw=; b=K6+Gx6IQSV8Vi/EVSVjna+MnezfkbQj4JZ2mpZNgsjaXjFbK27jtgnzJ pts+Zw9s29ZxqKQAD4k6kSu9kwx29sJTqGbmMxhTRdpypB0tE9Y1AG1NH ZGJ6PIQpxyyomoTvKZsVX8vxAX/uMHck4iCLmTsn/pLIE6yxrcRkr/Tz3 zSY8XeRUe0aW46yufkjDUKYOZzstGo+ToOxAqe8ZiK3U/P8uJKe5CfY7K FpmIPge/pGx+LWIIVXzD3yilgpCRLLLI1ERjJPyVc8Orh16etA/fyXpZI DqejYfML0LkSctJPlVcylLFKy1t/AGM7+EOTPzo3V667To+DQ3KqKqqtR w==; X-CSE-ConnectionGUID: JB5IziwcRmi95T6R9LWQLg== X-CSE-MsgGUID: /gc5jUWFQpOuaA2moWJGMQ== X-IronPort-AV: E=McAfee;i="6700,10204,11400"; a="49566809" X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="49566809" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:56 -0700 X-CSE-ConnectionGUID: xyN5YfHySWCyq4ci6QA5ow== X-CSE-MsgGUID: azB28qqXSkWJAsDymj72cw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="133917420" Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:54 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Liu Subject: [PATCH 4/5] backends/iommufd: Drop hiod_iommufd_get_cap() Date: Fri, 11 Apr 2025 18:17:06 +0800 Message-Id: <20250411101707.3460429-5-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250411101707.3460429-1-zhenzhong.duan@intel.com> References: <20250411101707.3460429-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=198.175.65.15; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -49 X-Spam_score: -5.0 X-Spam_bar: ----- X-Spam_report: (-5.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.593, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Because sub-class TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO has it's own implementation of .get_cap(), hiod_iommufd_get_cap() isn't used any more, drop it. Signed-off-by: Zhenzhong Duan --- backends/iommufd.c | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/backends/iommufd.c b/backends/iommufd.c index 54fa3174d0..d2ecdc9c82 100644 --- a/backends/iommufd.c +++ b/backends/iommufd.c @@ -311,28 +311,6 @@ bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, return true; } -static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp) -{ - HostIOMMUDeviceCaps *caps = &hiod->caps; - - switch (cap) { - case HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE: - return caps->type; - case HOST_IOMMU_DEVICE_CAP_AW_BITS: - return vfio_device_get_aw_bits(hiod->agent); - default: - error_setg(errp, "%s: unsupported capability %x", hiod->name, cap); - return -EINVAL; - } -} - -static void hiod_iommufd_class_init(ObjectClass *oc, void *data) -{ - HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc); - - hioc->get_cap = hiod_iommufd_get_cap; -}; - static const TypeInfo types[] = { { .name = TYPE_IOMMUFD_BACKEND, @@ -349,7 +327,6 @@ static const TypeInfo types[] = { }, { .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, .parent = TYPE_HOST_IOMMU_DEVICE, - .class_init = hiod_iommufd_class_init, .abstract = true, } }; From patchwork Fri Apr 11 10:17:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 14048054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4873EC36010 for ; Fri, 11 Apr 2025 10:22:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u3BVt-0002zY-Im; Fri, 11 Apr 2025 06:21:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVh-0002fF-JS for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:21 -0400 Received: from mgamail.intel.com ([198.175.65.15]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3BVe-0005Nr-Fb for qemu-devel@nongnu.org; Fri, 11 Apr 2025 06:21:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1744366878; x=1775902878; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a1rqJgngFJ3/FyQqDL1AmQI5pB8InAlZ+NX9kWvKC+w=; b=FJOtqGhZO1eP1jM2G8DjupddGiZTWMKq5/uXM6Jmxfql2pNtkbUKDzD2 AfZa+9ZUk1OC1uHCa6Blr2uy8BCndy3lDcTGPyzQnL3u5HLyHlFuS1uQ/ i24RUF+hAm6TH0WABknkz+6b+dxXG48L0L3pv5g/8WBAK1XsZjI9o4pyu ERcB1tL8JIh8HID+8WSIbcD5elg28YFsIziS5Sjsu7OXGGYFZVoMsGsAE SfmP1Izfizn3zGBtaWHpW5XQuaBqQLku9UMAw3bX/Zh3VweA3V8K3/8dd DhRCXjIMgWu2FEvzqB0xArk0rvoHLQVru7bB88v94s7lR4fYLwKzjT5yo g==; X-CSE-ConnectionGUID: YM9D1xBhRzKu7Ls317bVtw== X-CSE-MsgGUID: QaYg1I8XQ0S676d/EJsRfg== X-IronPort-AV: E=McAfee;i="6700,10204,11400"; a="49566820" X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="49566820" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:58 -0700 X-CSE-ConnectionGUID: 9/SwKHS4R6S3cgX0EKKMHQ== X-CSE-MsgGUID: oq4zwjX/S1+O/UgIkox3UA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,205,1739865600"; d="scan'208";a="133917424" Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2025 03:20:57 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Liu Subject: [PATCH 5/5] vfio/iommufd: Drop HostIOMMUDeviceCaps from HostIOMMUDevice Date: Fri, 11 Apr 2025 18:17:07 +0800 Message-Id: <20250411101707.3460429-6-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250411101707.3460429-1-zhenzhong.duan@intel.com> References: <20250411101707.3460429-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=198.175.65.15; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -49 X-Spam_score: -5.0 X-Spam_bar: ----- X-Spam_report: (-5.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.593, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Because hiod_iommufd_get_cap() was dropped, HostIOMMUDeviceCaps is not useful any more, drop it. This also hides HostIOMMUDeviceCaps from vIOMMU so the only way to check cap is through .get_cap() interface. This makes HostIOMMUDevice exposing data to vIOMMU as small as possible. Signed-off-by: Zhenzhong Duan --- include/system/host_iommu_device.h | 14 -------------- hw/vfio/iommufd.c | 15 --------------- 2 files changed, 29 deletions(-) diff --git a/include/system/host_iommu_device.h b/include/system/host_iommu_device.h index 809cced4ba..6f10bea25f 100644 --- a/include/system/host_iommu_device.h +++ b/include/system/host_iommu_device.h @@ -15,19 +15,6 @@ #include "qom/object.h" #include "qapi/error.h" -/** - * struct HostIOMMUDeviceCaps - Define host IOMMU device capabilities. - * - * @type: host platform IOMMU type. - * - * @hw_caps: host platform IOMMU capabilities (e.g. on IOMMUFD this represents - * the @out_capabilities value returned from IOMMU_GET_HW_INFO ioctl) - */ -typedef struct HostIOMMUDeviceCaps { - uint32_t type; - uint64_t hw_caps; -} HostIOMMUDeviceCaps; - #define TYPE_HOST_IOMMU_DEVICE "host-iommu-device" OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass, HOST_IOMMU_DEVICE) @@ -38,7 +25,6 @@ struct HostIOMMUDevice { void *agent; /* pointer to agent device, ie. VFIO or VDPA device */ PCIBus *aliased_bus; int aliased_devfn; - HostIOMMUDeviceCaps caps; }; /** diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index e7ca92f81f..947c5456d8 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -811,24 +811,9 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, Error **errp) { VFIODevice *vdev = opaque; - HostIOMMUDeviceCaps *caps = &hiod->caps; - enum iommu_hw_info_type type; - union { - struct iommu_hw_info_vtd vtd; - } data; - uint64_t hw_caps; hiod->agent = opaque; - - if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid, - &type, &data, sizeof(data), - &hw_caps, errp)) { - return false; - } - hiod->name = g_strdup(vdev->name); - caps->type = type; - caps->hw_caps = hw_caps; return true; }