DPDK patches and discussions
 help / color / mirror / Atom feed
From: Tomasz Duszynski <tduszynski@marvell.com>
To: <dev@dpdk.org>
Cc: <thomas@monjalon.net>, <jerinj@marvell.com>,
	Tomasz Duszynski <tduszynski@marvell.com>
Subject: [RFC PATCH 5/7] raw/vfio_platform: support rawdev configure
Date: Fri, 23 Dec 2022 00:24:33 +0100	[thread overview]
Message-ID: <20221222232436.643514-6-tduszynski@marvell.com> (raw)
In-Reply-To: <20221222232436.643514-1-tduszynski@marvell.com>

Add support for binding device VFIO group to a VFIO container.

Signed-off-by: Tomasz Duszynski <tduszynski@marvell.com>
---
 .../raw/vfio_platform/rte_pmd_vfio_platform.h |   6 +
 drivers/raw/vfio_platform/vfio_platform.c     | 147 ++++++++++++++++++
 2 files changed, 153 insertions(+)

diff --git a/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h b/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h
index 307a20d9fc..b595171af6 100644
--- a/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h
+++ b/drivers/raw/vfio_platform/rte_pmd_vfio_platform.h
@@ -17,6 +17,12 @@
 extern "C" {
 #endif
 
+/** vfio platform device configuration */
+struct vfio_platform_dev_config {
+	/** logical value representing the vfio container */
+	int container;
+};
+
 /**
  * Create a new VFIO container.
  *
diff --git a/drivers/raw/vfio_platform/vfio_platform.c b/drivers/raw/vfio_platform/vfio_platform.c
index ae0572dc99..ab5b96a0b0 100644
--- a/drivers/raw/vfio_platform/vfio_platform.c
+++ b/drivers/raw/vfio_platform/vfio_platform.c
@@ -3,9 +3,11 @@
  */
 
 #include <errno.h>
+#include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <unistd.h>
 
+#include <eal_filesystem.h>
 #include <rte_bus_platform.h>
 #include <rte_common.h>
 #include <rte_config.h>
@@ -92,6 +94,29 @@ rte_pmd_vfio_platform_container_destroy(int container)
 	return 0;
 }
 
+static char *
+of_resource_name(const char *dev_name, int index)
+{
+	char path[PATH_MAX], buf[BUFSIZ] = { };
+	int num = 0, ret;
+	char *name;
+
+	snprintf(path, sizeof(path), VFIO_PLATFORM_SYSFS_BASE "/%s/of_node/reg-names", dev_name);
+	/* save space for null byte */
+	ret = eal_parse_sysfs_string(path, buf, sizeof(buf) - 1);
+	if (ret)
+		return NULL;
+
+	for (name = buf; name; name += strlen(name) + 2) {
+		if (num++ != index)
+			continue;
+
+		return strdup(name);
+	}
+
+	return NULL;
+}
+
 static void
 device_release_maps(struct vfio_platform *plat)
 {
@@ -109,6 +134,109 @@ device_release_maps(struct vfio_platform *plat)
 	plat->num_resource = 0;
 }
 
+static int
+device_configure_maps(struct vfio_platform *plat, struct vfio_device_info *dev_info)
+{
+	struct vfio_region_info reg_info = { .argsz = sizeof(reg_info), };
+	struct vfio_platform_resource *res;
+	unsigned int i;
+	void *map;
+	int ret;
+
+	plat->resource = calloc(dev_info->num_regions, sizeof(*plat->resource));
+	if (!plat->resource)
+		return -ENOMEM;
+
+	for (i = 0; i < dev_info->num_regions; i++) {
+		reg_info.index = i;
+		ret = ioctl(plat->dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
+		if (ret) {
+			VFIO_PLATFORM_LOG(ERR, "failed to get region info at %d\n", i);
+			ret = -errno;
+			goto out;
+		}
+
+		map = mmap(NULL, reg_info.size, PROT_READ | PROT_WRITE, MAP_SHARED, plat->dev_fd,
+			   reg_info.offset);
+		if (map == MAP_FAILED) {
+			ret = -errno;
+			goto out;
+		}
+
+		res = &plat->resource[plat->num_resource];
+		res->mem.addr = map;
+		res->mem.len = reg_info.size;
+		res->name = of_resource_name(plat->device->name, reg_info.index);
+		plat->num_resource++;
+
+		VFIO_PLATFORM_LOG(DEBUG, "adding resource va=%p len=%lu name = %s\n",
+				  res->mem.addr, res->mem.len, res->name);
+	}
+
+	return 0;
+out:
+	device_release_maps(plat);
+
+	return ret;
+}
+
+static int
+device_configure(struct vfio_platform *plat, struct vfio_platform_dev_config *config)
+{
+	struct vfio_device_info dev_info = { .argsz = sizeof(dev_info), };
+	struct rte_platform_device *pdev = plat->device;
+	const char *name = pdev->device.name;
+	int ret;
+
+	ret = rte_vfio_get_group_num(VFIO_PLATFORM_SYSFS_BASE, name, &plat->group);
+	if (ret != 1) {
+		VFIO_PLATFORM_LOG(ERR, "failed to read device %s iommu group number\n", name);
+		return -ENODEV;
+	}
+
+	container_get(config->container);
+	plat->group_fd = rte_vfio_container_group_bind(container_fd(config->container), plat->group);
+	if (plat->group_fd < 0) {
+		VFIO_PLATFORM_LOG(ERR, "failed to bind group to container\n");
+		ret = -ENODEV;
+		goto out;
+	}
+
+	ret = rte_vfio_setup_device(VFIO_PLATFORM_SYSFS_BASE, name, &plat->dev_fd, &dev_info);
+	if (ret) {
+		VFIO_PLATFORM_LOG(ERR, "failed to setup %s\n", name);
+		ret = -ENODEV;
+		goto out_unbind;
+	}
+
+	if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PLATFORM)) {
+		VFIO_PLATFORM_LOG(ERR, "device not backed by vfio platform\n");
+		ret = -ENOTSUP;
+		goto out_release;
+	}
+
+	plat->container = config->container;
+
+	ret = device_configure_maps(plat, &dev_info);
+	if (ret) {
+		VFIO_PLATFORM_LOG(ERR, "failed to setup memory maps\n");
+		goto out_release;
+	}
+
+	return 0;
+
+out_release:
+	rte_vfio_release_device(VFIO_PLATFORM_SYSFS_BASE, name, plat->dev_fd);
+out_unbind:
+	rte_vfio_container_group_unbind(container_fd(config->container), plat->group);
+out:
+	close(plat->group_fd);
+	rte_vfio_clear_group(plat->group_fd);
+	container_put(config->container);
+
+	return ret;
+}
+
 static void
 device_release(struct vfio_platform *plat)
 {
@@ -120,6 +248,24 @@ device_release(struct vfio_platform *plat)
 	container_put(plat->container);
 }
 
+static int
+vfio_rawdev_dev_configure(const struct rte_rawdev *dev, rte_rawdev_obj_t config, size_t config_size)
+{
+	struct vfio_platform_dev_config *dev_config = config;
+	struct vfio_platform *plat = dev->dev_private;
+
+	if (!dev_config)
+		return -EINVAL;
+
+	if (sizeof(*dev_config) != config_size)
+		return -EINVAL;
+
+	if ((unsigned int)dev_config->container >= RTE_DIM(container_tbl))
+		return -EINVAL;
+
+	return device_configure(plat, dev_config);
+}
+
 static int
 vfio_rawdev_dev_close(struct rte_rawdev *dev)
 {
@@ -131,6 +277,7 @@ vfio_rawdev_dev_close(struct rte_rawdev *dev)
 }
 
 static const struct rte_rawdev_ops vfio_platform_rawdev_ops = {
+	.dev_configure = vfio_rawdev_dev_configure,
 	.dev_close = vfio_rawdev_dev_close,
 };
 
-- 
2.25.1


  parent reply	other threads:[~2022-12-22 23:25 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-22 23:24 [RFC PATCH 0/7] support vfio platform PMD Tomasz Duszynski
2022-12-22 23:24 ` [RFC PATCH 1/7] lib: add helper to read strings from sysfs files Tomasz Duszynski
2022-12-23 16:40   ` Stephen Hemminger
2023-01-11 18:19     ` [EXT] " Tomasz Duszynski
2022-12-22 23:24 ` [RFC PATCH 2/7] raw/vfio_platform: add driver skeleton Tomasz Duszynski
2022-12-22 23:24 ` [RFC PATCH 3/7] raw/vfio_platform: add platform probe and remove Tomasz Duszynski
2022-12-22 23:24 ` [RFC PATCH 4/7] raw/vfio_platform: support rawdev close Tomasz Duszynski
2022-12-22 23:24 ` Tomasz Duszynski [this message]
2022-12-22 23:24 ` [RFC PATCH 6/7] raw/vfio_platform: support rawdev device info Tomasz Duszynski
2022-12-22 23:24 ` [RFC PATCH 7/7] raw/vfio_platform: support DMA map/unmap Tomasz Duszynski
2022-12-23  7:05 ` [RFC PATCH 0/7] support vfio platform PMD Xia, Chenbo
2023-01-12  8:14   ` Tomasz Duszynski

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=20221222232436.643514-6-tduszynski@marvell.com \
    --to=tduszynski@marvell.com \
    --cc=dev@dpdk.org \
    --cc=jerinj@marvell.com \
    --cc=thomas@monjalon.net \
    /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).