From patchwork Mon Apr 8 08:12:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620741 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 71D2ACD1292 for ; Mon, 8 Apr 2024 08:16:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtk9q-0002qD-9I; Mon, 08 Apr 2024 04:15:14 -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 1rtk9o-0002mX-6b for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:12 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtk9l-00088l-93 for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564109; x=1744100109; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wykm8CTdvs/a63yu30J9vOfyYa6zOvbij1LOSE5uNY0=; b=Frg0stdvjDGrSjSlNM/DjUiPTbe0+grjfNEdqUwfHFj+XDxlYWPLRkt9 UhgWhuJnN4Lo9Ad8d2w0bE8uVg+kWEHx4qswROjG0tqInhRr8KJQz//4R BChNXaWEUxQVDwcPHpQp6qp9cgs6q19TRj/xArkzZsqPK5BMxaP+MFrFR bMJYUslih+3sldKcBevb2DXYRSV7otBDzRsx7p1q87OdddjewqR3suon/ oWaF3DlWe+uXq7JrrbgakGFdh46rDs5qi4KBXxuV3u13mjvBbuOtW5LYT BBPBhN2h/0+s6tLoD+kIfidM4jno9xsD+MxoL+l0awqT9e3okjzbCu+CI w==; X-CSE-ConnectionGUID: DEm5sQyBS/27OiXiUhI7Ug== X-CSE-MsgGUID: wnNE+OFRQVq7Cnl3nXR2Eg== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199660" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199660" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:07 -0700 X-CSE-ConnectionGUID: uA+19ALmRUi0BuSiAzEreQ== X-CSE-MsgGUID: nyHeRnZ9QP+qXdTzXmjSJQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845170" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:03 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Paolo Bonzini Subject: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice Date: Mon, 8 Apr 2024 16:12:21 +0800 Message-Id: <20240408081230.1030078-2-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 Introduce HostIOMMUDevice as an abstraction of host IOMMU device. get_host_iommu_info() is used to get host IOMMU info, different backends can have different implementations and result format. Introduce a macro CONFIG_HOST_IOMMU_DEVICE to define the usage for VFIO, and VDPA in the future. Suggested-by: Cédric Le Goater Signed-off-by: Zhenzhong Duan --- MAINTAINERS | 2 ++ include/sysemu/host_iommu_device.h | 19 +++++++++++++++++++ backends/host_iommu_device.c | 19 +++++++++++++++++++ backends/Kconfig | 5 +++++ backends/meson.build | 1 + 5 files changed, 46 insertions(+) create mode 100644 include/sysemu/host_iommu_device.h create mode 100644 backends/host_iommu_device.c diff --git a/MAINTAINERS b/MAINTAINERS index e71183eef9..22f71cbe02 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2202,6 +2202,8 @@ M: Zhenzhong Duan S: Supported F: backends/iommufd.c F: include/sysemu/iommufd.h +F: backends/host_iommu_device.c +F: include/sysemu/host_iommu_device.h F: include/qemu/chardev_open.h F: util/chardev_open.c F: docs/devel/vfio-iommufd.rst diff --git a/include/sysemu/host_iommu_device.h b/include/sysemu/host_iommu_device.h new file mode 100644 index 0000000000..22ccbe3a5d --- /dev/null +++ b/include/sysemu/host_iommu_device.h @@ -0,0 +1,19 @@ +#ifndef HOST_IOMMU_DEVICE_H +#define HOST_IOMMU_DEVICE_H + +#include "qom/object.h" + +#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device" +OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass, HOST_IOMMU_DEVICE) + +struct HostIOMMUDevice { + Object parent; +}; + +struct HostIOMMUDeviceClass { + ObjectClass parent_class; + + int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len, + Error **errp); +}; +#endif diff --git a/backends/host_iommu_device.c b/backends/host_iommu_device.c new file mode 100644 index 0000000000..6cb6007d8c --- /dev/null +++ b/backends/host_iommu_device.c @@ -0,0 +1,19 @@ +#include "qemu/osdep.h" +#include "sysemu/host_iommu_device.h" + +OBJECT_DEFINE_ABSTRACT_TYPE(HostIOMMUDevice, + host_iommu_device, + HOST_IOMMU_DEVICE, + OBJECT) + +static void host_iommu_device_class_init(ObjectClass *oc, void *data) +{ +} + +static void host_iommu_device_init(Object *obj) +{ +} + +static void host_iommu_device_finalize(Object *obj) +{ +} diff --git a/backends/Kconfig b/backends/Kconfig index 2cb23f62fa..34ab29e994 100644 --- a/backends/Kconfig +++ b/backends/Kconfig @@ -3,3 +3,8 @@ source tpm/Kconfig config IOMMUFD bool depends on VFIO + +config HOST_IOMMU_DEVICE + bool + default y + depends on VFIO diff --git a/backends/meson.build b/backends/meson.build index 8b2b111497..2e975d641e 100644 --- a/backends/meson.build +++ b/backends/meson.build @@ -25,6 +25,7 @@ if have_vhost_user endif system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c')) system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c')) +system_ss.add(when: 'CONFIG_HOST_IOMMU_DEVICE', if_true: files('host_iommu_device.c')) if have_vhost_user_crypto system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c')) endif From patchwork Mon Apr 8 08:12:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620739 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 2BCECC67861 for ; Mon, 8 Apr 2024 08:15:46 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtk9r-0002rL-1u; Mon, 08 Apr 2024 04:15:15 -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 1rtk9p-0002p5-MT for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:13 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtk9n-00081z-9C for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564111; x=1744100111; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nvxHeLmjV4rkU7CI9wa3Pysp6fP4cvCvnf1CbZdvW5U=; b=Z1bImBPqbQ7LVibhmVH0M2CEkC0wVWb1es9+zjthD2pgnQXPSCbdgjk9 XhJz3HG7ZPKGug19wPROsS74JEcgmLBn9PPE/mXAriUa/10j2t0sTSIKh rvpvIHCH/sRzc5k8ncBQSEaJhvYiZgUpPuPECA3b/n9wqj376i17lKYp4 7xw1jxbtS8CbqmsGVkBqRsP1P3Fs25shCqCplf7w8MvuOnAsnz4yp3dk5 /A89vLIM4ZKzG0+uHpBMVp1NHzNRPhclH/0mQ3ysUT9RXOVnkBuMLfzap CUwqVNZiDdx+XcyxbZq0dNisx1D5u3AoksHgBCn5GHTSIeVQJMD+lMjjc g==; X-CSE-ConnectionGUID: Dts6YZpmSyyEtXe7VvJDng== X-CSE-MsgGUID: ZchQYIrfSLGoVmQKrvO50Q== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199666" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199666" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:11 -0700 X-CSE-ConnectionGUID: GAlrNFepRhe8+MfiBHG8dw== X-CSE-MsgGUID: TwAZU6QrSj+g917OTWRCiA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845177" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:07 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device Date: Mon, 8 Apr 2024 16:12:22 +0800 Message-Id: <20240408081230.1030078-3-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend. It includes a link to VFIODevice. Suggested-by: Eric Auger Suggested-by: Cédric Le Goater Signed-off-by: Zhenzhong Duan --- include/hw/vfio/vfio-common.h | 11 +++++++++++ hw/vfio/container.c | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index b9da6c08ef..f30772f534 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -31,6 +31,7 @@ #endif #include "sysemu/sysemu.h" #include "hw/vfio/vfio-container-base.h" +#include "sysemu/host_iommu_device.h" #define VFIO_MSG_PREFIX "vfio %s: " @@ -147,6 +148,16 @@ typedef struct VFIOGroup { bool ram_block_discard_allowed; } VFIOGroup; +#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio" +OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO) + +/* Abstraction of VFIO legacy host IOMMU device */ +struct HIODLegacyVFIO { + /*< private >*/ + HostIOMMUDevice parent; + VFIODevice *vdev; +}; + typedef struct VFIODMABuf { QemuDmaBuf buf; uint32_t pos_x, pos_y, pos_updates; diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 77bdec276e..44018ef085 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -1143,12 +1143,21 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) vioc->pci_hot_reset = vfio_legacy_pci_hot_reset; }; +static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data) +{ +}; + static const TypeInfo types[] = { { .name = TYPE_VFIO_IOMMU_LEGACY, .parent = TYPE_VFIO_IOMMU, .class_init = vfio_iommu_legacy_class_init, - }, + }, { + .name = TYPE_HIOD_LEGACY_VFIO, + .parent = TYPE_HOST_IOMMU_DEVICE, + .instance_size = sizeof(HIODLegacyVFIO), + .class_init = hiod_legacy_vfio_class_init, + } }; DEFINE_TYPES(types) From patchwork Mon Apr 8 08:12:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620740 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 7D008CD128A for ; Mon, 8 Apr 2024 08:16:22 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtk9v-00030i-3H; Mon, 08 Apr 2024 04:15:19 -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 1rtk9t-0002y7-Ha for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:17 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtk9r-00089q-Nz for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564116; x=1744100116; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mThwLA2cpqB/Ub1Co78HjfLcWVvgAn50zoxRT0ghxDo=; b=ebPNTtPakWIp6QOjpZHjEnsXFiUQmHnOSqEBlON5mEEO3t4o+aF+1m2O 7idCUguaw4n6xEGZzBKunvfJZZt4T3lxmSKZTqYAzvY6LwHQNDcCf3666 PVRRBKk2agzauEZokAVBbQA8KAWAOiuVDsinpP4G+izrGWudq/EOEoTl4 f5mGbMNIHz5uW10P3eolqWSQewYDkcD1NbQtfXJ5OgoKU9FyS8Atdesc9 5tRJ577rNvrHRA6wahcPjaUE4InY2lW2aiu/+lWkTAsYyq77Y0uNK3vnG o1rfVmWTtH3H3ogfiKKvXuOdjb0WCZrkQbFylYSMJN6CEiFysiOmJm5/G Q==; X-CSE-ConnectionGUID: WMmEY20hTrykIicrsAK46Q== X-CSE-MsgGUID: jpPJ00l+T6W/0vnbSeXCVw== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199680" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199680" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:14 -0700 X-CSE-ConnectionGUID: 8Mh8REmGQ/KKVYHWXwmyqw== X-CSE-MsgGUID: DogWqoJiTUyyZOH1poh80Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845186" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:11 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Sun Subject: [PATCH v2 03/10] backends/iommufd: Introduce abstract HIODIOMMUFD device Date: Mon, 8 Apr 2024 16:12:23 +0800 Message-Id: <20240408081230.1030078-4-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 HIODIOMMUFD represents a host IOMMU device under iommufd backend. Currently it includes only public iommufd handle and device id. which could be used to get hw IOMMU information. When nested translation is supported in future, vIOMMU is going to have iommufd related operations like attaching/detaching hwpt, So IOMMUFDDevice interface will be further extended at that time. VFIO and VDPA device have different way of attaching/detaching hwpt. So HIODIOMMUFD is still an abstract class which will be inherited by VFIO and VDPA device. Introduce a helper hiod_iommufd_init() to initialize HIODIOMMUFD device. Suggested-by: Cédric Le Goater Originally-by: Yi Liu Signed-off-by: Yi Sun Signed-off-by: Zhenzhong Duan --- include/sysemu/iommufd.h | 22 +++++++++++++++++++ backends/iommufd.c | 47 ++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/include/sysemu/iommufd.h b/include/sysemu/iommufd.h index 9af27ebd6c..71c53cbb45 100644 --- a/include/sysemu/iommufd.h +++ b/include/sysemu/iommufd.h @@ -4,6 +4,7 @@ #include "qom/object.h" #include "exec/hwaddr.h" #include "exec/cpu-common.h" +#include "sysemu/host_iommu_device.h" #define TYPE_IOMMUFD_BACKEND "iommufd" OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass, IOMMUFD_BACKEND) @@ -33,4 +34,25 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, ram_addr_t size, void *vaddr, bool readonly); int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, ram_addr_t size); + +#define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd" +OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD) + +struct HIODIOMMUFD { + /*< private >*/ + HostIOMMUDevice parent; + void *opaque; + + /*< public >*/ + IOMMUFDBackend *iommufd; + uint32_t devid; +}; + +struct HIODIOMMUFDClass { + /*< private >*/ + HostIOMMUDeviceClass parent_class; +}; + +void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd, + uint32_t devid); #endif diff --git a/backends/iommufd.c b/backends/iommufd.c index 62a79fa6b0..ef8b3a808b 100644 --- a/backends/iommufd.c +++ b/backends/iommufd.c @@ -212,23 +212,38 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, return ret; } -static const TypeInfo iommufd_backend_info = { - .name = TYPE_IOMMUFD_BACKEND, - .parent = TYPE_OBJECT, - .instance_size = sizeof(IOMMUFDBackend), - .instance_init = iommufd_backend_init, - .instance_finalize = iommufd_backend_finalize, - .class_size = sizeof(IOMMUFDBackendClass), - .class_init = iommufd_backend_class_init, - .interfaces = (InterfaceInfo[]) { - { TYPE_USER_CREATABLE }, - { } - } -}; +void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd, + uint32_t devid) +{ + idev->iommufd = iommufd; + idev->devid = devid; +} -static void register_types(void) +static void hiod_iommufd_class_init(ObjectClass *oc, void *data) { - type_register_static(&iommufd_backend_info); } -type_init(register_types); +static const TypeInfo types[] = { + { + .name = TYPE_IOMMUFD_BACKEND, + .parent = TYPE_OBJECT, + .instance_size = sizeof(IOMMUFDBackend), + .instance_init = iommufd_backend_init, + .instance_finalize = iommufd_backend_finalize, + .class_size = sizeof(IOMMUFDBackendClass), + .class_init = iommufd_backend_class_init, + .interfaces = (InterfaceInfo[]) { + { TYPE_USER_CREATABLE }, + { } + } + }, { + .name = TYPE_HIOD_IOMMUFD, + .parent = TYPE_HOST_IOMMU_DEVICE, + .instance_size = sizeof(HIODIOMMUFD), + .class_size = sizeof(HIODIOMMUFDClass), + .class_init = hiod_iommufd_class_init, + .abstract = true, + } +}; + +DEFINE_TYPES(types) From patchwork Mon Apr 8 08:12:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620738 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 DF71DCD128A for ; Mon, 8 Apr 2024 08:15:33 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtk9z-0003AI-PX; Mon, 08 Apr 2024 04:15:23 -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 1rtk9x-00039u-Q7 for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:21 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtk9v-00089q-Vi for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564120; x=1744100120; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bvbdJgRIGiY6OBmpA+3MhD5hfFd6dTo8QUt8PrlDTNk=; b=g/LSajqI7ZP0rk5rChsq3f/StY+wBYeCY8BCR5QvQgIL7Ued9PiicDr7 jbbXczSb3p0w0DJ3+FjPayp5/dISCgQdqFUt+9pmjWngS39TXpYRgagtM izO6iTjBURVtWwifFuiufdCsNzeL2ADkcofCexYUd4wu9hTOQ1gv0XF1e ZHA3TbY4zrNM4GdFVBzo8hdKeGHNEVtgrZGw4iRWHa6W+WYupEfCEZ9bt IYLbpNvLxUZsycIUnQ2LotkwnauXudSDub3MwwzWfO7bqSbXP7YTGc/s/ qHyYoiNDYWy+zMRP98k7kGqPdVjuR01i08tb0dZqJDLlBILG0ew3ZiRpa Q==; X-CSE-ConnectionGUID: h/gQGqHNR6yfTBzVxD5oiA== X-CSE-MsgGUID: mkg0Ugg+Q32PAUu9lItMgw== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199701" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199701" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:18 -0700 X-CSE-ConnectionGUID: aNHqgbzrS4K5rg6x79zEfw== X-CSE-MsgGUID: jvOGt1H/RoqGyMJE75LTKQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845198" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:15 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v2 04/10] vfio/iommufd: Introduce HIODIOMMUFDVFIO device Date: Mon, 8 Apr 2024 16:12:24 +0800 Message-Id: <20240408081230.1030078-5-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 HIODIOMMUFDVFIO represents a host IOMMU device under VFIO iommufd backend. It will be created during VFIO device attaching and passed to vIOMMU. It includes a link to VFIODevice so that we can do VFIO device specific hwpt attaching/detaching. Signed-off-by: Zhenzhong Duan --- include/hw/vfio/vfio-common.h | 11 +++++++++++ hw/vfio/iommufd.c | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index f30772f534..d382b12ec1 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -32,6 +32,7 @@ #include "sysemu/sysemu.h" #include "hw/vfio/vfio-container-base.h" #include "sysemu/host_iommu_device.h" +#include "sysemu/iommufd.h" #define VFIO_MSG_PREFIX "vfio %s: " @@ -158,6 +159,16 @@ struct HIODLegacyVFIO { VFIODevice *vdev; }; +#define TYPE_HIOD_IOMMUFD_VFIO TYPE_HIOD_IOMMUFD "-vfio" +OBJECT_DECLARE_SIMPLE_TYPE(HIODIOMMUFDVFIO, HIOD_IOMMUFD_VFIO) + +/* Abstraction of VFIO IOMMUFD host IOMMU device */ +struct HIODIOMMUFDVFIO { + /*< private >*/ + HIODIOMMUFD parent; + VFIODevice *vdev; +}; + typedef struct VFIODMABuf { QemuDmaBuf buf; uint32_t pos_x, pos_y, pos_updates; diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 8827ffe636..115b9f8e7f 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -634,12 +634,21 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; }; +static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) +{ +}; + static const TypeInfo types[] = { { .name = TYPE_VFIO_IOMMU_IOMMUFD, .parent = TYPE_VFIO_IOMMU, .class_init = vfio_iommu_iommufd_class_init, - }, + }, { + .name = TYPE_HIOD_IOMMUFD_VFIO, + .parent = TYPE_HIOD_IOMMUFD, + .instance_size = sizeof(HIODIOMMUFDVFIO), + .class_init = hiod_iommufd_vfio_class_init, + } }; DEFINE_TYPES(types) From patchwork Mon Apr 8 08:12:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620745 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 A1BC7CD128A for ; Mon, 8 Apr 2024 08:17:03 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkA2-0003B0-B0; Mon, 08 Apr 2024 04:15:26 -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 1rtkA0-0003AR-4y for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:24 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtk9y-00089q-FC for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564122; x=1744100122; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C4MsvBvx9B64HLRW4B7bTMYTxjVlbMxQElKE/nS9Vhc=; b=mzP+eHlcZpmLyNUd3jJHzPOQ1AX+zLpt9kLawSFPa7kJwNEaPpxkgqv0 hpWPdtuFYJ5+xni+DDLcz16NS8YfEjTUHpIYzrx1n/ejDl1ZBwkNDqyUK dOF+ExfFT0MawfKiT3sA0k8tuAG4gGe4wsKwi70s+HdVlROOjjxt0CG6V 85VPtSeUCNgljIQCHLcpqtnd+2cm9xAhcO7iokORdWp2pC4EG9QViVboq Ugulyv+hJdV1DCipeyw6GdkDdBzby4IDfnG9p8LCIktCjptEqL4bZS+0s 6Us5OjtBaxNf2FY+MFH4pbV2zw637plh8kRliZKypmrPTDdqGXCBp5dNU A==; X-CSE-ConnectionGUID: 3ge72sZrQ6i70Ao7v1HpPg== X-CSE-MsgGUID: dbqPhMitSJuAbFdDQwh0OQ== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199712" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199712" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:22 -0700 X-CSE-ConnectionGUID: CjhSjub6RROLMU6Ty4M7XQ== X-CSE-MsgGUID: wSi5G0lYQ7a3sz9JvvB0qw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845206" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:18 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v2 05/10] vfio: Implement get_host_iommu_info() callback Date: Mon, 8 Apr 2024 16:12:25 +0800 Message-Id: <20240408081230.1030078-6-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 Utilize iova_ranges to calculate host IOMMU address width and package it in HIOD_LEGACY_INFO for vIOMMU usage. HIOD_LEGACY_INFO will be used by both VFIO and VDPA so declare it in host_iommu_device.h. Signed-off-by: Zhenzhong Duan --- include/sysemu/host_iommu_device.h | 10 ++++++++++ hw/vfio/container.c | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/sysemu/host_iommu_device.h b/include/sysemu/host_iommu_device.h index 22ccbe3a5d..beb8be8231 100644 --- a/include/sysemu/host_iommu_device.h +++ b/include/sysemu/host_iommu_device.h @@ -16,4 +16,14 @@ struct HostIOMMUDeviceClass { int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len, Error **errp); }; + +/* + * Define the format of host IOMMU related info that current VFIO + * or VDPA can privode to vIOMMU. + * + * @aw_bits: Host IOMMU address width. 0xff if no limitation. + */ +typedef struct HIOD_LEGACY_INFO { + uint8_t aw_bits; +} HIOD_LEGACY_INFO; #endif diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 44018ef085..ba0ad4a41b 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -1143,8 +1143,32 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) vioc->pci_hot_reset = vfio_legacy_pci_hot_reset; }; +static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod, + void *data, uint32_t len, + Error **errp) +{ + VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev; + /* iova_ranges is a sorted list */ + GList *l = g_list_last(vbasedev->bcontainer->iova_ranges); + HIOD_LEGACY_INFO *info = data; + + assert(sizeof(HIOD_LEGACY_INFO) <= len); + + if (l) { + Range *range = l->data; + info->aw_bits = find_last_bit(&range->upb, BITS_PER_LONG) + 1; + } else { + info->aw_bits = 0xff; + } + + return 0; +} + static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data) { + HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc); + + hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info; }; static const TypeInfo types[] = { From patchwork Mon Apr 8 08:12:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620744 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 3C9D7CD128A for ; Mon, 8 Apr 2024 08:16:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkA6-0003EL-IL; Mon, 08 Apr 2024 04:15: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 1rtkA5-0003Bg-1D for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:29 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkA2-00089q-Bm for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564126; x=1744100126; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OEWdQXklwv4XAzXDZ24doa8pgMnpGHHQ2cUDfFr+BSg=; b=EscL1OsXRbd9lMpiKkbtrEyckacGYt2YgudMJ/h44iljKb4U2E9eGO5t iQIdVmpzmBvXuDXqKNHaJDFaPbXqYWPE2HfOe/yP0EfbBBQ7/g2s+p81Q bZOIDJmOMTSmY4llx6BAR9dOSLw5o0xJJiX0RE68iox7nRscW4RfdQbAA CaVWBwfCBlpLo5ShY6GbBZJaLb/bI5CKIrW04JnqnqEej6NXWNuqkdLFg 9T0WqEw4HSLQdw6nd4wn5kPZkdyz/Gevyp3pCOg7G7lreie/Pan/prPti auuge6vjd7K25EGDJIlknJa+cLSB75MVerLVQoYuDtWHF3JJu9Je7BPQe Q==; X-CSE-ConnectionGUID: 7/LfREySRqKgr6C9mo1fWw== X-CSE-MsgGUID: ccJNiClERRWSVYXvrqAduQ== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199722" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199722" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:26 -0700 X-CSE-ConnectionGUID: j4JXK4wBQImrPHCXFQIKeQ== X-CSE-MsgGUID: XnhhlLBaQBKl+om8ORecHg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845210" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:22 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Sun Subject: [PATCH v2 06/10] backends/iommufd: Introduce helper function iommufd_backend_get_device_info() Date: Mon, 8 Apr 2024 16:12:26 +0800 Message-Id: <20240408081230.1030078-7-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 Introduce a helper function iommufd_backend_get_device_info() to get host IOMMU related information through iommufd uAPI. Signed-off-by: Yi Liu Signed-off-by: Yi Sun Signed-off-by: Zhenzhong Duan --- include/sysemu/iommufd.h | 4 ++++ backends/iommufd.c | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/sysemu/iommufd.h b/include/sysemu/iommufd.h index 71c53cbb45..fa1a866237 100644 --- a/include/sysemu/iommufd.h +++ b/include/sysemu/iommufd.h @@ -4,6 +4,7 @@ #include "qom/object.h" #include "exec/hwaddr.h" #include "exec/cpu-common.h" +#include #include "sysemu/host_iommu_device.h" #define TYPE_IOMMUFD_BACKEND "iommufd" @@ -34,6 +35,9 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, ram_addr_t size, void *vaddr, bool readonly); int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, ram_addr_t size); +int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, + enum iommu_hw_info_type *type, + void *data, uint32_t len, Error **errp); #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd" OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD) diff --git a/backends/iommufd.c b/backends/iommufd.c index ef8b3a808b..559affa9ec 100644 --- a/backends/iommufd.c +++ b/backends/iommufd.c @@ -20,7 +20,6 @@ #include "monitor/monitor.h" #include "trace.h" #include -#include static void iommufd_backend_init(Object *obj) { @@ -212,6 +211,28 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, return ret; } +int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, + enum iommu_hw_info_type *type, + void *data, uint32_t len, Error **errp) +{ + struct iommu_hw_info info = { + .size = sizeof(info), + .dev_id = devid, + .data_len = len, + .data_uptr = (uintptr_t)data, + }; + int ret; + + ret = ioctl(be->fd, IOMMU_GET_HW_INFO, &info); + if (ret) { + error_setg_errno(errp, errno, "Failed to get hardware info"); + } else { + *type = info.out_data_type; + } + + return ret; +} + void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd, uint32_t devid) { From patchwork Mon Apr 8 08:12:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620742 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 0F206C67861 for ; Mon, 8 Apr 2024 08:16:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkAY-0003Nh-RB; Mon, 08 Apr 2024 04:16:00 -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 1rtkA7-0003Gz-Ku for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:31 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkA5-00089q-SB for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564130; x=1744100130; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RajnNcVKs+6ypzH6WcL3+uFvOHZi+tCP/jt2sxffnkU=; b=Cc8/KOWif++x1+v7k0r0xmFmvWabN3dCPADo09HvqeWoaw0nnkx8wFnB FfdiW7LI4i2cW+0nx1YT12xImMlD4jO1vdPxraPGsWXnspLxetjUiwSKB MVcUUIypcCP+SQfhR4tXS5VaMBsd/zCW3IS8P0+GOnbF25ZIKFW1yU3Mb eSOG+wdZX/vtG67QvBaeKD7ZrMMUH419PVnNleMq+iHyhlMwVlsEkyVxx heJ8HD8cY1uofZqCTI4rmeTp/Lx707kjxPkgqbisry8Rhkvaliwiyu6nh j5r1UANhCgLE3eSQGcfD9nNsIuO/I7t285HsgJw9w8/jhyUEqbY0ppMa0 w==; X-CSE-ConnectionGUID: hqIRHT3DT32Sx+7Ct/CIKw== X-CSE-MsgGUID: ahDLwpJITZ2afy+t+nQ4qA== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199736" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199736" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:29 -0700 X-CSE-ConnectionGUID: 13QfKuu2QaiwI0B/e4f+Jg== X-CSE-MsgGUID: Mr0nPNFWTk2LC9ThWK5AZQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845213" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:26 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v2 07/10] backends/iommufd: Implement get_host_iommu_info() callback Date: Mon, 8 Apr 2024 16:12:27 +0800 Message-Id: <20240408081230.1030078-8-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 It calls iommufd_backend_get_device_info() to get host IOMMU related information. Define a common structure HIOD_IOMMUFD_INFO to describe the info returned from kernel. Currently only vtd, but easy to add arm smmu when kernel supports. Signed-off-by: Zhenzhong Duan --- include/sysemu/iommufd.h | 7 +++++++ backends/iommufd.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/sysemu/iommufd.h b/include/sysemu/iommufd.h index fa1a866237..44ec1335b2 100644 --- a/include/sysemu/iommufd.h +++ b/include/sysemu/iommufd.h @@ -39,6 +39,13 @@ int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, enum iommu_hw_info_type *type, void *data, uint32_t len, Error **errp); +typedef struct HIOD_IOMMUFD_INFO { + enum iommu_hw_info_type type; + union { + struct iommu_hw_info_vtd vtd; + } data; +} HIOD_IOMMUFD_INFO; + #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd" OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD) diff --git a/backends/iommufd.c b/backends/iommufd.c index 559affa9ec..1e9c469e65 100644 --- a/backends/iommufd.c +++ b/backends/iommufd.c @@ -240,8 +240,25 @@ void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd, idev->devid = devid; } +static int hiod_iommufd_get_host_iommu_info(HostIOMMUDevice *hiod, + void *data, uint32_t len, + Error **errp) +{ + HIODIOMMUFD *idev = HIOD_IOMMUFD(hiod); + HIOD_IOMMUFD_INFO *info = data; + + assert(sizeof(HIOD_IOMMUFD_INFO) <= len); + + return iommufd_backend_get_device_info(idev->iommufd, idev->devid, + &info->type, &info->data, + sizeof(info->data), errp); +} + static void hiod_iommufd_class_init(ObjectClass *oc, void *data) { + HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); + + hiodc->get_host_iommu_info = hiod_iommufd_get_host_iommu_info; } static const TypeInfo types[] = { From patchwork Mon Apr 8 08:12:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620746 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 C6F6DC67861 for ; Mon, 8 Apr 2024 08:17:04 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkAv-0003jy-3f; Mon, 08 Apr 2024 04:16:23 -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 1rtkAC-0003N9-CJ for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:42 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkA9-00089q-JL for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564134; x=1744100134; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DVXEQ9yLYmU2ztZd49QkuJi4eo9VDVlNfFqfrJ3+k7g=; b=GSTDpJDXkwkBRm5ZgtjybBWLh1js5xg1Oc6IudeCVkv6qRfQq4FD1Kqo ckoYCi+vK+wepGEbLS+RY4ON/GIvF1g8rd+NDZKNexAnfv/3Wxlxa6TzS Ot7EGTZ1IdZLB4TtWuGbh9okGF8kwN2a5c5+6cJ78dw8SSXmRCgwDU5Vs qLSXdHXSF1qADDz7T0g3QPA/hbdLrhAJeWkKopZuUy/B7NG5c9OkdOedE LpsHHZu1YSfo82kABpxXB13GeoPplw+o3DeP8djfF9sniFGL6Y2LHzJ1p gGjhfiAfj99UK6dsxs2Zqnakb69uXA+YcVJpGqbfy1A5rndTGR0m0xhIt g==; X-CSE-ConnectionGUID: UgUpGAStRW2WiKo/+U31FQ== X-CSE-MsgGUID: fjvCROubSta6N+jMskoFhw== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199743" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199743" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:33 -0700 X-CSE-ConnectionGUID: nRXBpU6nSUqBwRBd1UeG5Q== X-CSE-MsgGUID: RcHR3eY3ThaPjkYeyG39Qw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845216" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:29 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v2 08/10] vfio: Create host IOMMU device instance Date: Mon, 8 Apr 2024 16:12:28 +0800 Message-Id: <20240408081230.1030078-9-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 Create host IOMMU device instance and initialize it based on backend. Signed-off-by: Zhenzhong Duan --- include/hw/vfio/vfio-common.h | 1 + hw/vfio/container.c | 5 +++++ hw/vfio/iommufd.c | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index d382b12ec1..4fbba85018 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -126,6 +126,7 @@ typedef struct VFIODevice { OnOffAuto pre_copy_dirty_page_tracking; bool dirty_pages_supported; bool dirty_tracking; + HostIOMMUDevice *hiod; int devid; IOMMUFDBackend *iommufd; } VFIODevice; diff --git a/hw/vfio/container.c b/hw/vfio/container.c index ba0ad4a41b..fc0c027501 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -915,6 +915,7 @@ static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev, VFIODevice *vbasedev_iter; VFIOGroup *group; VFIOContainerBase *bcontainer; + HIODLegacyVFIO *hiod_vfio; int ret; if (groupid < 0) { @@ -945,6 +946,9 @@ static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev, vbasedev->bcontainer = bcontainer; QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); + hiod_vfio = HIOD_LEGACY_VFIO(object_new(TYPE_HIOD_LEGACY_VFIO)); + hiod_vfio->vdev = vbasedev; + vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio); return ret; } @@ -959,6 +963,7 @@ static void vfio_legacy_detach_device(VFIODevice *vbasedev) trace_vfio_detach_device(vbasedev->name, group->groupid); vfio_put_base_device(vbasedev); vfio_put_group(group); + object_unref(vbasedev->hiod); } static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single) diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 115b9f8e7f..b6d058339b 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -308,6 +308,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, VFIOIOMMUFDContainer *container; VFIOAddressSpace *space; struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; + HIODIOMMUFDVFIO *hiod_vfio; int ret, devfd; uint32_t ioas_id; Error *err = NULL; @@ -431,6 +432,12 @@ found_container: QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); + hiod_vfio = HIOD_IOMMUFD_VFIO(object_new(TYPE_HIOD_IOMMUFD_VFIO)); + hiod_iommufd_init(HIOD_IOMMUFD(hiod_vfio), vbasedev->iommufd, + vbasedev->devid); + hiod_vfio->vdev = vbasedev; + vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio); + trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, vbasedev->num_regions, vbasedev->flags); return 0; @@ -468,6 +475,7 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev) iommufd_cdev_detach_container(vbasedev, container); iommufd_cdev_container_destroy(container); vfio_put_address_space(space); + object_unref(vbasedev->hiod); iommufd_cdev_unbind_and_disconnect(vbasedev); close(vbasedev->fd); From patchwork Mon Apr 8 08:12:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620743 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 70714C67861 for ; Mon, 8 Apr 2024 08:16:40 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkB3-00043k-2k; Mon, 08 Apr 2024 04:16: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 1rtkAG-0003Ni-2q for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:43 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkAE-00089q-39 for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564138; x=1744100138; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Rhf6gqcQqWTnIyTi86DlfojgFggLJA6Lto3EZkYbwsw=; b=MF661VO811RfbsjdfJqoLWt9MgBFywXdwUpGaRrYcOILQwwJDjPI2oXh tdiWeo3pP1EYZInNtc65FEInPeXkFMGtI+nrwpqUuT9dnCULwcB8Ak1sF AIe6+VC9htCZyX9qcI0IimvqXZEYlPrXz/EdFqkoLiQmldtRU5PDfRWuH JCVHkxdmW6IfDhbLFltgFf1JILozMIxfdODBc64imniwydy6RkNmD8tqZ xJonDBaQQmXS0zzygbW+X+RxcufOHGHi6pcfiJGuNJkwxwDnm7gcGlSjt 7Y0syI9mw63UrBupUfuSrtkYHvHXMOeEH5nKCD6Me40KblvjcGlDmzUYC A==; X-CSE-ConnectionGUID: 5mIXzPOOQUS/+ETMasnY2A== X-CSE-MsgGUID: 3giUB8t3RZ6acv4z7ZCjYQ== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199758" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199758" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:37 -0700 X-CSE-ConnectionGUID: hw28PJNJRH+f1N9Hd+KGVQ== X-CSE-MsgGUID: cDtIRS4qTTymxwX9aruT/g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845220" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:33 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Yi Sun , Zhenzhong Duan , Marcel Apfelbaum Subject: [PATCH v2 09/10] hw/pci: Introduce pci_device_set/unset_iommu_device() Date: Mon, 8 Apr 2024 16:12:29 +0800 Message-Id: <20240408081230.1030078-10-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 From: Yi Liu This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCI device. Caller of set should fail if set operation fails. Extract out pci_device_get_iommu_bus_devfn() to facilitate implementation of pci_device_set/unset_iommu_device(). Signed-off-by: Yi Liu Signed-off-by: Yi Sun Signed-off-by: Nicolin Chen Signed-off-by: Zhenzhong Duan --- include/hw/pci/pci.h | 40 ++++++++++++++++++++++- hw/pci/pci.c | 75 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index eaa3fc99d8..4ae7fe6f3f 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -3,6 +3,7 @@ #include "exec/memory.h" #include "sysemu/dma.h" +#include "sysemu/host_iommu_device.h" /* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" @@ -383,10 +384,47 @@ typedef struct PCIIOMMUOps { * * @devfn: device and function number */ - AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn); + AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn); + /** + * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU + * + * Optional callback, if not implemented in vIOMMU, then vIOMMU can't + * retrieve host information from the associated HostIOMMUDevice. + * + * Return true if HostIOMMUDevice is attached, or else return false + * with errp set. + * + * @bus: the #PCIBus of the PCI device. + * + * @opaque: the data passed to pci_setup_iommu(). + * + * @devfn: device and function number of the PCI device. + * + * @dev: the data structure representing host IOMMU device. + * + * @errp: pass an Error out only when return false + * + */ + int (*set_iommu_device)(PCIBus *bus, void *opaque, int devfn, + HostIOMMUDevice *dev, Error **errp); + /** + * @unset_iommu_device: detach a HostIOMMUDevice from a vIOMMU + * + * Optional callback. + * + * @bus: the #PCIBus of the PCI device. + * + * @opaque: the data passed to pci_setup_iommu(). + * + * @devfn: device and function number of the PCI device. + */ + void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn); } PCIIOMMUOps; AddressSpace *pci_device_iommu_address_space(PCIDevice *dev); +int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, + Error **errp); +void pci_device_unset_iommu_device(PCIDevice *dev); /** * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus diff --git a/hw/pci/pci.c b/hw/pci/pci.c index e7a39cb203..8ece617673 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2648,11 +2648,27 @@ static void pci_device_class_base_init(ObjectClass *klass, void *data) } } -AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) +/* + * Get IOMMU root bus, aliased bus and devfn of a PCI device + * + * IOMMU root bus is needed by all call sites to call into iommu_ops. + * For call sites which don't need aliased BDF, passing NULL to + * aliased_[bus/devfn] is allowed. + * + * @piommu_bus: return root #PCIBus backed by an IOMMU for the PCI device. + * + * @aliased_bus: return aliased #PCIBus of the PCI device, optional. + * + * @aliased_devfn: return aliased devfn of the PCI device, optional. + */ +static void pci_device_get_iommu_bus_devfn(PCIDevice *dev, + PCIBus **piommu_bus, + PCIBus **aliased_bus, + int *aliased_devfn) { PCIBus *bus = pci_get_bus(dev); PCIBus *iommu_bus = bus; - uint8_t devfn = dev->devfn; + int devfn = dev->devfn; while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) { PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev); @@ -2693,13 +2709,66 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) iommu_bus = parent_bus; } - if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) { + + assert(0 <= devfn && devfn < PCI_DEVFN_MAX); + assert(iommu_bus); + + if (pci_bus_bypass_iommu(bus) || !iommu_bus->iommu_ops) { + iommu_bus = NULL; + } + + *piommu_bus = iommu_bus; + + if (aliased_bus) { + *aliased_bus = bus; + } + + if (aliased_devfn) { + *aliased_devfn = devfn; + } +} + +AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) +{ + PCIBus *bus; + PCIBus *iommu_bus; + int devfn; + + pci_device_get_iommu_bus_devfn(dev, &iommu_bus, &bus, &devfn); + if (iommu_bus) { return iommu_bus->iommu_ops->get_address_space(bus, iommu_bus->iommu_opaque, devfn); } return &address_space_memory; } +int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod, + Error **errp) +{ + PCIBus *iommu_bus; + + /* set_iommu_device requires device's direct BDF instead of aliased BDF */ + pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL); + if (iommu_bus && iommu_bus->iommu_ops->set_iommu_device) { + return iommu_bus->iommu_ops->set_iommu_device(pci_get_bus(dev), + iommu_bus->iommu_opaque, + dev->devfn, hiod, errp); + } + return 0; +} + +void pci_device_unset_iommu_device(PCIDevice *dev) +{ + PCIBus *iommu_bus; + + pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL); + if (iommu_bus && iommu_bus->iommu_ops->unset_iommu_device) { + return iommu_bus->iommu_ops->unset_iommu_device(pci_get_bus(dev), + iommu_bus->iommu_opaque, + dev->devfn); + } +} + void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void *opaque) { /* From patchwork Mon Apr 8 08:12:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Duan, Zhenzhong" X-Patchwork-Id: 13620747 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 1A498CD1292 for ; Mon, 8 Apr 2024 08:17:05 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rtkB8-0004E4-DE; Mon, 08 Apr 2024 04:16:34 -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 1rtkAK-0003UB-GV for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:57 -0400 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rtkAI-00089q-9b for qemu-devel@nongnu.org; Mon, 08 Apr 2024 04:15:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712564142; x=1744100142; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=brdfmfoa2727Rep2O7aimUC9ZxCZnVThwz+OaTXE4tU=; b=AS4GVMAii4zrtKfKNxW680ydF2A5wAapkGeAlyS5dakgLveKhqQ+eVAj 3AwCLDEWYkuftGWWIevbdjeLxUQte/rCo7IiyLegE72PHRi5mjNa7mg+b uzfJtM2HUc8Gdd7/aN5YNQgkQoQhD8YYYBsIhY4LGwMUdRZIn2RoJN2Oy xUpGaCo/2CbbYpUB/WIQpNVuaM8PQVu9gAwq7fSo2S43eB6NccO+zLD7r +W5oN7tQpiXElHN8q05AHSEdRG1RiudmbapYR3ozzkCW2XYUiU5bJapRW W8k0RB6WKiUn1l0naLdb0HyOv853FihwilqfiMSd2aMOal+kgGqytb85Y Q==; X-CSE-ConnectionGUID: MGDtHaW3TAauZV5e3M2WUw== X-CSE-MsgGUID: BYzlxdZFSxqkR1gQiD0pZw== X-IronPort-AV: E=McAfee;i="6600,9927,11037"; a="19199771" X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19199771" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:41 -0700 X-CSE-ConnectionGUID: NsZ77xSCTZupDm0SFEWB4w== X-CSE-MsgGUID: 9+J4+smwSGO54IRKr9Cg9g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,186,1708416000"; d="scan'208";a="19845225" Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2024 01:15:37 -0700 From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Yi Sun Subject: [PATCH v2 10/10] vfio: Pass HostIOMMUDevice to vIOMMU Date: Mon, 8 Apr 2024 16:12:30 +0800 Message-Id: <20240408081230.1030078-11-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408081230.1030078-1-zhenzhong.duan@intel.com> References: <20240408081230.1030078-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.198.163.10; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.355, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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 With HostIOMMUDevice passed, vIOMMU can check compatibility with host IOMMU, call into IOMMUFD specific methods, etc. Originally-by: Yi Liu Signed-off-by: Nicolin Chen Signed-off-by: Yi Sun Signed-off-by: Zhenzhong Duan --- hw/vfio/pci.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 64780d1b79..224501a86e 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -3111,11 +3111,17 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_bars_register(vdev); - ret = vfio_add_capabilities(vdev, errp); + ret = pci_device_set_iommu_device(pdev, vbasedev->hiod, errp); if (ret) { + error_prepend(errp, "Failed to set iommu_device: "); goto out_teardown; } + ret = vfio_add_capabilities(vdev, errp); + if (ret) { + goto out_unset_idev; + } + if (vdev->vga) { vfio_vga_quirk_setup(vdev); } @@ -3132,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) error_setg(errp, "cannot support IGD OpRegion feature on hotplugged " "device"); - goto out_teardown; + goto out_unset_idev; } ret = vfio_get_dev_region_info(vbasedev, @@ -3141,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) if (ret) { error_setg_errno(errp, -ret, "does not support requested IGD OpRegion feature"); - goto out_teardown; + goto out_unset_idev; } ret = vfio_pci_igd_opregion_init(vdev, opregion, errp); g_free(opregion); if (ret) { - goto out_teardown; + goto out_unset_idev; } } @@ -3233,6 +3239,8 @@ out_deregister: if (vdev->intx.mmap_timer) { timer_free(vdev->intx.mmap_timer); } +out_unset_idev: + pci_device_unset_iommu_device(pdev); out_teardown: vfio_teardown_msi(vdev); vfio_bars_exit(vdev); @@ -3261,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj) static void vfio_exitfn(PCIDevice *pdev) { VFIOPCIDevice *vdev = VFIO_PCI(pdev); + VFIODevice *vbasedev = &vdev->vbasedev; vfio_unregister_req_notifier(vdev); vfio_unregister_err_notifier(vdev); @@ -3275,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev) vfio_teardown_msi(vdev); vfio_pci_disable_rp_atomics(vdev); vfio_bars_exit(vdev); - vfio_migration_exit(&vdev->vbasedev); + vfio_migration_exit(vbasedev); + pci_device_unset_iommu_device(pdev); } static void vfio_pci_reset(DeviceState *dev)