From: Beilei Xing <beilei.x...@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.x...@intel.com>
Signed-off-by: Yahui Cao <yahui....@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

Reply via email to