From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2FC9F4375E; Fri, 22 Dec 2023 12:22:35 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 09AC040E36; Fri, 22 Dec 2023 12:22:19 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by mails.dpdk.org (Postfix) with ESMTP id D690140A4B for ; Fri, 22 Dec 2023 12:22:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703244138; x=1734780138; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2eMrHUSL/vS8NjSWKzvHRv/QRPYCycwQNcYKLo0aKrQ=; b=lMp3zDIZJW3e926yBst7B6dIQyOLW5RC9iJdisQ3gF3jW9yXm48DPgns fokBTTfKyiZnHJe8oiCLfa8pkIbVZ4nw8VeCcDOWvIbPEPCRSD+n0g+xl o+ok0XPauJa64XMzqVVZqEKb0ixT0WBabU1xlBRJzSMlLD/Nlx0XM/4HR Kla5TD7DsLAirvT5XfXEOzcZ0qVVPsvYjMjhP3QNb5bLyZ0ka/uihc5Zd WvRrdcr8/mTvnSvrZYOPcHGYskj9VnoCWa+BrGUEsIBRViLVLqILZ52cb Fz09kXqEmmvfH+Evo1ufHsfW+KFuSu6q5DfDRlSRWoy759Ch/AuDgLZHU w==; X-IronPort-AV: E=McAfee;i="6600,9927,10931"; a="399927196" X-IronPort-AV: E=Sophos;i="6.04,296,1695711600"; d="scan'208";a="399927196" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Dec 2023 03:22:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,296,1695711600"; d="scan'208";a="11437161" Received: from dpdk-beileix-icelake.sh.intel.com ([10.67.116.155]) by fmviesa002.fm.intel.com with ESMTP; 22 Dec 2023 03:22:15 -0800 From: beilei.xing@intel.com To: anatoly.burakov@intel.com Cc: dev@dpdk.org, thomas@monjalon.net, ferruh.yigit@amd.com, bruce.richardson@intel.com, chenbox@nvidia.com, yahui.cao@intel.com, Beilei Xing Subject: [PATCH 3/4] bus/pci: add VFIO CDEV support Date: Fri, 22 Dec 2023 19:44:52 +0000 Message-Id: <20231222194453.3049693-4-beilei.xing@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231222194453.3049693-1-beilei.xing@intel.com> References: <20231222194453.3049693-1-beilei.xing@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Beilei Xing This patch adds VFIO CDEV support to probe PCI devices. For VFIO subsystem, mainline Linux supports both of VFIO Container/GROUP interface and VFIO IOMMUFD/CDEV interface. Comparing with VFIO Container and VFIO IOMMUFD, vfio device uAPI does not change while I/O page tables management is moved from VFIO Container into IOMMUFD interface. Signed-off-by: Beilei Xing Signed-off-by: Yahui Cao --- drivers/bus/pci/bus_pci_driver.h | 1 + drivers/bus/pci/linux/pci.c | 14 +++++++++ drivers/bus/pci/linux/pci_init.h | 4 +++ drivers/bus/pci/linux/pci_vfio.c | 52 ++++++++++++++++++++++++++------ 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/drivers/bus/pci/bus_pci_driver.h b/drivers/bus/pci/bus_pci_driver.h index be32263a82..6ac25546cf 100644 --- a/drivers/bus/pci/bus_pci_driver.h +++ b/drivers/bus/pci/bus_pci_driver.h @@ -26,6 +26,7 @@ enum rte_pci_kernel_driver { RTE_PCI_KDRV_NIC_UIO, /* nic_uio for FreeBSD */ RTE_PCI_KDRV_NONE, /* no attached driver */ RTE_PCI_KDRV_NET_UIO, /* NetUIO for Windows */ + RTE_PCI_KDRV_VFIO_IOMMUFD, /* VFIO IOMMUFD for Linux */ }; /** diff --git a/drivers/bus/pci/linux/pci.c b/drivers/bus/pci/linux/pci.c index 3d237398d9..1a37f5de22 100644 --- a/drivers/bus/pci/linux/pci.c +++ b/drivers/bus/pci/linux/pci.c @@ -65,6 +65,12 @@ rte_pci_map_device(struct rte_pci_device *dev) #ifdef VFIO_PRESENT if (pci_vfio_is_enabled()) ret = pci_vfio_map_resource(dev); +#endif + break; + case RTE_PCI_KDRV_VFIO_IOMMUFD: +#ifdef VFIO_IOMMUFD_PRESENT + if (pci_iommufd_is_enabled()) + ret = pci_vfio_map_resource(dev); #endif break; case RTE_PCI_KDRV_IGB_UIO: @@ -94,6 +100,12 @@ rte_pci_unmap_device(struct rte_pci_device *dev) #ifdef VFIO_PRESENT if (pci_vfio_is_enabled()) pci_vfio_unmap_resource(dev); +#endif + break; + case RTE_PCI_KDRV_VFIO_IOMMUFD: +#ifdef VFIO_IOMMUFD_PRESENT + if (pci_iommufd_is_enabled()) + pci_vfio_unmap_resource(dev); #endif break; case RTE_PCI_KDRV_IGB_UIO: @@ -645,6 +657,7 @@ int rte_pci_read_config(const struct rte_pci_device *device, return pci_uio_read_config(intr_handle, buf, len, offset); #ifdef VFIO_PRESENT case RTE_PCI_KDRV_VFIO: + case RTE_PCI_KDRV_VFIO_IOMMUFD: return pci_vfio_read_config(device, buf, len, offset); #endif default: @@ -669,6 +682,7 @@ int rte_pci_write_config(const struct rte_pci_device *device, return pci_uio_write_config(intr_handle, buf, len, offset); #ifdef VFIO_PRESENT case RTE_PCI_KDRV_VFIO: + case RTE_PCI_KDRV_VFIO_IOMMUFD: return pci_vfio_write_config(device, buf, len, offset); #endif default: diff --git a/drivers/bus/pci/linux/pci_init.h b/drivers/bus/pci/linux/pci_init.h index a4d37c0d0a..a096bc245b 100644 --- a/drivers/bus/pci/linux/pci_init.h +++ b/drivers/bus/pci/linux/pci_init.h @@ -79,4 +79,8 @@ int pci_vfio_is_enabled(void); #endif +#ifdef VFIO_IOMMUFD_PRESENT +int pci_iommufd_is_enabled(void); +#endif + #endif /* EAL_PCI_INIT_H_ */ diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c index 3f3201daf2..97032231d7 100644 --- a/drivers/bus/pci/linux/pci_vfio.c +++ b/drivers/bus/pci/linux/pci_vfio.c @@ -21,6 +21,9 @@ #include #include #include +#ifdef VFIO_IOMMUFD_PRESENT +#include +#endif #include "eal_filesystem.h" @@ -783,10 +786,21 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev) snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT, loc->domain, loc->bus, loc->devid, loc->function); - ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, - &vfio_dev_fd, &device_info); - if (ret) - return ret; +#ifdef VFIO_IOMMUFD_PRESENT + if (dev->kdrv == RTE_PCI_KDRV_VFIO_IOMMUFD) { + ret = rte_vfio_iommufd_setup_device(rte_pci_get_sysfs_path(), pci_addr, + &vfio_dev_fd, &device_info); + if (ret) + return ret; + } else { +#endif + ret = rte_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, + &vfio_dev_fd, &device_info); + if (ret) + return ret; +#ifdef VFIO_IOMMUFD_PRESENT + } +#endif if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd)) goto err_vfio_dev_fd; @@ -1148,12 +1162,24 @@ pci_vfio_unmap_resource_primary(struct rte_pci_device *dev) return -1; } - ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr, - vfio_dev_fd); - if (ret < 0) { - RTE_LOG(ERR, EAL, "Cannot release VFIO device\n"); - return ret; +#ifdef VFIO_IOMMUFD_PRESENT + if (dev->kdrv == RTE_PCI_KDRV_VFIO_IOMMUFD) { + ret = rte_vfio_iommufd_release_device(pci_addr, vfio_dev_fd); + if (ret < 0) { + RTE_LOG(ERR, EAL, "Cannot release VFIO device\n"); + return ret; + } + } else { +#endif + ret = rte_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr, + vfio_dev_fd); + if (ret < 0) { + RTE_LOG(ERR, EAL, "Cannot release VFIO device\n"); + return ret; + } +#ifdef VFIO_IOMMUFD_PRESENT } +#endif vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list); @@ -1327,3 +1353,11 @@ pci_vfio_is_enabled(void) return rte_vfio_is_enabled("vfio_pci"); } #endif + +#ifdef VFIO_IOMMUFD_PRESENT +int +pci_iommufd_is_enabled(void) +{ + return rte_iommufd_is_enabled("iommufd"); +} +#endif -- 2.34.1