[RFC,2/7] raw/vfio_platform: add driver skeleton

Message ID 20221222232436.643514-3-tduszynski@marvell.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series support vfio platform PMD |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Tomasz Duszynski Dec. 22, 2022, 11:24 p.m. UTC
  Add driver skeleton which does not do anything beyond registering driver
itself to platform bus and providing means to create and destroy VFIO
container.

Signed-off-by: Tomasz Duszynski <tduszynski@marvell.com>
---
 doc/guides/rawdevs/index.rst                  |  1 +
 doc/guides/rawdevs/vfio_platform.rst          | 24 ++++++
 drivers/raw/meson.build                       |  1 +
 drivers/raw/vfio_platform/meson.build         | 16 ++++
 .../raw/vfio_platform/rte_pmd_vfio_platform.h | 49 +++++++++++
 drivers/raw/vfio_platform/version.map         | 10 +++
 drivers/raw/vfio_platform/vfio_platform.c     | 85 +++++++++++++++++++
 7 files changed, 186 insertions(+)
 create mode 100644 doc/guides/rawdevs/vfio_platform.rst
 create mode 100644 drivers/raw/vfio_platform/meson.build
 create mode 100644 drivers/raw/vfio_platform/rte_pmd_vfio_platform.h
 create mode 100644 drivers/raw/vfio_platform/version.map
 create mode 100644 drivers/raw/vfio_platform/vfio_platform.c
  

Patch

diff --git a/doc/guides/rawdevs/index.rst b/doc/guides/rawdevs/index.rst
index f34315f051..b95c155b48 100644
--- a/doc/guides/rawdevs/index.rst
+++ b/doc/guides/rawdevs/index.rst
@@ -16,3 +16,4 @@  application through rawdev API.
     dpaa2_cmdif
     ifpga
     ntb
+    vfio_platform
diff --git a/doc/guides/rawdevs/vfio_platform.rst b/doc/guides/rawdevs/vfio_platform.rst
new file mode 100644
index 0000000000..97443a85fb
--- /dev/null
+++ b/doc/guides/rawdevs/vfio_platform.rst
@@ -0,0 +1,24 @@ 
+..  SPDX-License-Identifier: BSD-3-Clause
+    Copyright(c) 2023 Marvell.
+
+VFIO Platform driver
+====================
+
+VFIO platform driver allows to configure and manage kernel VFIO platform devices.
+These devices do not normally have built-in bus discovery capabilities, known
+for example from PCI bus, and reside behind an IOMMU.
+
+Features
+--------
+
+Following features are available:
+
+- bind to devices managed by kernel vfio-platform driver
+- expose device memory resources
+- map/unmap DMA memory
+
+Requirements
+------------
+
+Since PMD is backed by vfio and vfio-platform kernel drivers they need to be enabled
+in kernel config.
diff --git a/drivers/raw/meson.build b/drivers/raw/meson.build
index 05cad143fe..a3e1126c98 100644
--- a/drivers/raw/meson.build
+++ b/drivers/raw/meson.build
@@ -12,5 +12,6 @@  drivers = [
         'ifpga',
         'ntb',
         'skeleton',
+        'vfio_platform',
 ]
 std_deps = ['rawdev']
diff --git a/drivers/raw/vfio_platform/meson.build b/drivers/raw/vfio_platform/meson.build
new file mode 100644
index 0000000000..a03836d890
--- /dev/null
+++ b/drivers/raw/vfio_platform/meson.build
@@ -0,0 +1,16 @@ 
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2023 Marvell.
+#
+
+if not is_linux
+  build = false
+  reason = 'only supported on Linux'
+endif
+
+deps += ['bus_platform', 'rawdev']
+sources = files(
+        'vfio_platform.c',
+)
+headers = files(
+        'rte_pmd_vfio_platform.h',
+)
diff --git a/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h b/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h
new file mode 100644
index 0000000000..307a20d9fc
--- /dev/null
+++ b/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h
@@ -0,0 +1,49 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2023 Marvell.
+ */
+
+#ifndef _RTE_PMD_VFIO_PLATFORM_H_
+#define _RTE_PMD_VFIO_PLATFORM_H_
+
+/**
+ * @file
+ *
+ * VFIO platform specific structures and interface.
+ *
+ * @b EXPERIMENTAL: this API may change or be removed without prior notice
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Create a new VFIO container.
+ *
+ * @return
+ *   container on success
+ *   <0 on failure
+ */
+__rte_experimental
+int
+rte_pmd_vfio_platform_container_create(void);
+
+/**
+ * Destroy previously created container.
+ *
+ * @param container
+ *   Container to destroy.
+ *
+ * @return
+ *   0 on success
+ *   <0 on failure
+ */
+__rte_experimental
+int
+rte_pmd_vfio_platform_container_destroy(int container);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_PMD_VFIO_PLATFORM_H_ */
diff --git a/drivers/raw/vfio_platform/version.map b/drivers/raw/vfio_platform/version.map
new file mode 100644
index 0000000000..2aea50f4c1
--- /dev/null
+++ b/drivers/raw/vfio_platform/version.map
@@ -0,0 +1,10 @@ 
+DPDK_23 {
+	local: *;
+};
+
+EXPERIMENTAL {
+	global:
+
+	rte_pmd_vfio_platform_container_create;
+	rte_pmd_vfio_platform_container_destroy;
+};
diff --git a/drivers/raw/vfio_platform/vfio_platform.c b/drivers/raw/vfio_platform/vfio_platform.c
new file mode 100644
index 0000000000..93558b310b
--- /dev/null
+++ b/drivers/raw/vfio_platform/vfio_platform.c
@@ -0,0 +1,85 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2023 Marvell.
+ */
+
+#include <errno.h>
+
+#include <rte_bus_platform.h>
+#include <rte_common.h>
+#include <rte_config.h>
+#include <rte_dev.h>
+#include <rte_errno.h>
+#include <rte_pmd_vfio_platform.h>
+#include <rte_vfio.h>
+
+static struct {
+	int container_fd;
+	int refcnt;
+} container_tbl[RTE_MAX_VFIO_CONTAINERS];
+
+static int
+container_next_free_index(void)
+{
+	int i;
+
+	for (i = 0; i < (int)RTE_DIM(container_tbl); i++) {
+		if (container_tbl[i].refcnt == 0)
+			return i;
+	}
+
+	return -1;
+}
+
+static void
+container_put(int index)
+{
+
+	if (container_tbl[index].refcnt == 0)
+		return;
+
+	if (--container_tbl[index].refcnt == 0)
+		rte_vfio_container_destroy(container_tbl[index].container_fd);
+}
+
+static void
+container_get(int index)
+{
+	container_tbl[index].refcnt++;
+}
+
+int
+rte_pmd_vfio_platform_container_create(void)
+{
+	int index, fd;
+
+	fd = rte_vfio_container_create();
+	if (fd < 0)
+		return -1;
+
+	index = container_next_free_index();
+	if (index < 0)
+		return -ENOSPC;
+
+	container_tbl[index].container_fd = fd;
+	container_get(index);
+
+	return index;
+}
+
+int
+rte_pmd_vfio_platform_container_destroy(int container)
+{
+	if ((unsigned int)container >= RTE_DIM(container_tbl))
+		return -EINVAL;
+
+	container_put(container);
+
+	return 0;
+}
+
+static struct rte_platform_driver vfio_platform = {
+};
+
+RTE_PMD_REGISTER_PLATFORM(vfio_platform, vfio_platform);
+RTE_PMD_REGISTER_ALIAS(vfio_platform, vfio-platform);
+RTE_PMD_REGISTER_KMOD_DEP(vfio_platform, "vfio-platform");