@@ -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.
*
@@ -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, ®_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,
};