DPDK patches and discussions
 help / color / mirror / Atom feed
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 <beilei.xing@intel.com>
Subject: [PATCH 3/4] bus/pci: add VFIO CDEV support
Date: Fri, 22 Dec 2023 19:44:52 +0000	[thread overview]
Message-ID: <20231222194453.3049693-4-beilei.xing@intel.com> (raw)
In-Reply-To: <20231222194453.3049693-1-beilei.xing@intel.com>

From: Beilei Xing <beilei.xing@intel.com>

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 <beilei.xing@intel.com>
Signed-off-by: Yahui Cao <yahui.cao@intel.com>
---
 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 <bus_driver.h>
 #include <rte_spinlock.h>
 #include <rte_tailq.h>
+#ifdef VFIO_IOMMUFD_PRESENT
+#include <rte_iommufd.h>
+#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


  parent reply	other threads:[~2023-12-22 11:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-22 19:44 [PATCH 0/4] add VFIO IOMMUFD/CDEV support beilei.xing
2023-12-22 19:44 ` [PATCH 1/4] iommufd: add IOMMUFD support beilei.xing
2023-12-22 19:44 ` [PATCH 2/4] vfio: add VFIO " beilei.xing
2023-12-22 17:17   ` Stephen Hemminger
2023-12-25  6:30     ` Xing, Beilei
2023-12-22 19:44 ` beilei.xing [this message]
2023-12-22 19:44 ` [PATCH 4/4] eal: add new args to choose VFIO mode beilei.xing
2023-12-22 17:17   ` Stephen Hemminger
2023-12-25  6:06     ` Xing, Beilei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231222194453.3049693-4-beilei.xing@intel.com \
    --to=beilei.xing@intel.com \
    --cc=anatoly.burakov@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=chenbox@nvidia.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=thomas@monjalon.net \
    --cc=yahui.cao@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).