From: Elena Agostini <eagost...@nvidia.com>

Enable the possibility to expose a GPU memory area and make it
accessible from the CPU.

GPU memory has to be allocated via rte_gpu_mem_alloc().

This patch allows the gpudev library to expose (and unexpose),
through the GPU driver, a chunk of GPU memory and to return
a memory pointer usable by the CPU to access the GPU memory area.

Changelog:
- Move to pin/unpin naming to expose/unexpose
- Minor changes to function definitions

Signed-off-by: Elena Agostini <eagost...@nvidia.com>
---
 lib/gpudev/gpudev.c        | 61 ++++++++++++++++++++++++++++++++++++++
 lib/gpudev/gpudev_driver.h |  6 ++++
 lib/gpudev/rte_gpudev.h    | 49 ++++++++++++++++++++++++++++++
 lib/gpudev/version.map     |  2 ++
 4 files changed, 118 insertions(+)

diff --git a/lib/gpudev/gpudev.c b/lib/gpudev/gpudev.c
index 9ae36dbae9..eafd5909e3 100644
--- a/lib/gpudev/gpudev.c
+++ b/lib/gpudev/gpudev.c
@@ -634,6 +634,67 @@ rte_gpu_mem_unregister(int16_t dev_id, void *ptr)
        return GPU_DRV_RET(dev->ops.mem_unregister(dev, ptr));
 }
 
+void *
+rte_gpu_mem_expose(int16_t dev_id, size_t size, void *ptr)
+{
+       struct rte_gpu *dev;
+       void *ptr_out;
+       int ret;
+
+       dev = gpu_get_by_id(dev_id);
+       if (dev == NULL) {
+               GPU_LOG(ERR, "expose mem for invalid device ID %d", dev_id);
+               rte_errno = ENODEV;
+               return NULL;
+       }
+
+       if (dev->ops.mem_expose == NULL) {
+               GPU_LOG(ERR, "mem exposure not supported");
+               rte_errno = ENOTSUP;
+               return NULL;
+       }
+
+       if (ptr == NULL || size == 0) /* dry-run  */
+               return NULL;
+
+       ret = GPU_DRV_RET(dev->ops.mem_expose(dev, size, ptr, &ptr_out));
+
+       switch (ret) {
+       case 0:
+               return ptr_out;
+       case -ENOMEM:
+       case -E2BIG:
+               rte_errno = -ret;
+               return NULL;
+       default:
+               rte_errno = -EPERM;
+               return NULL;
+       }
+}
+
+int
+rte_gpu_mem_unexpose(int16_t dev_id, void *ptr)
+{
+       struct rte_gpu *dev;
+
+       dev = gpu_get_by_id(dev_id);
+       if (dev == NULL) {
+               GPU_LOG(ERR, "unexpose mem for invalid device ID %d", dev_id);
+               rte_errno = ENODEV;
+               return -rte_errno;
+       }
+
+       if (dev->ops.mem_unexpose == NULL) {
+               rte_errno = ENOTSUP;
+               return -rte_errno;
+       }
+
+       if (ptr == NULL) /* dry-run */
+               return 0;
+
+       return GPU_DRV_RET(dev->ops.mem_unexpose(dev, ptr));
+}
+
 int
 rte_gpu_wmb(int16_t dev_id)
 {
diff --git a/lib/gpudev/gpudev_driver.h b/lib/gpudev/gpudev_driver.h
index cb7b101f2f..9049d11280 100644
--- a/lib/gpudev/gpudev_driver.h
+++ b/lib/gpudev/gpudev_driver.h
@@ -31,6 +31,8 @@ typedef int (rte_gpu_mem_alloc_t)(struct rte_gpu *dev, size_t 
size, void **ptr);
 typedef int (rte_gpu_mem_free_t)(struct rte_gpu *dev, void *ptr);
 typedef int (rte_gpu_mem_register_t)(struct rte_gpu *dev, size_t size, void 
*ptr);
 typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr);
+typedef int (rte_gpu_mem_expose_t)(struct rte_gpu *dev, size_t size, void 
*ptr_in, void **ptr_out);
+typedef int (rte_gpu_mem_unexpose_t)(struct rte_gpu *dev, void *ptr);
 typedef int (rte_gpu_wmb_t)(struct rte_gpu *dev);
 
 struct rte_gpu_ops {
@@ -46,6 +48,10 @@ struct rte_gpu_ops {
        rte_gpu_mem_register_t *mem_register;
        /* Unregister CPU memory from device. */
        rte_gpu_mem_unregister_t *mem_unregister;
+       /* Pin GPU memory. */
+       rte_gpu_mem_expose_t *mem_expose;
+       /* Unpin GPU memory. */
+       rte_gpu_mem_unexpose_t *mem_unexpose;
        /* Enforce GPU write memory barrier. */
        rte_gpu_wmb_t *wmb;
 };
diff --git a/lib/gpudev/rte_gpudev.h b/lib/gpudev/rte_gpudev.h
index fa3f3aad4f..2048133790 100644
--- a/lib/gpudev/rte_gpudev.h
+++ b/lib/gpudev/rte_gpudev.h
@@ -447,6 +447,55 @@ int rte_gpu_mem_register(int16_t dev_id, size_t size, void 
*ptr);
 __rte_experimental
 int rte_gpu_mem_unregister(int16_t dev_id, void *ptr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Expose a chunk of GPU memory to make it accessible from the CPU
+ * using the memory pointer returned by the function.
+ * GPU memory has to be allocated via rte_gpu_mem_alloc().
+ *
+ * @param dev_id
+ *   Device ID requiring exposed memory.
+ * @param size
+ *   Number of bytes to expose.
+ *   Requesting 0 will do nothing.
+ * @param ptr
+ *   Pointer to the GPU memory area to be exposed.
+ *   NULL is a no-op accepted value.
+
+ * @return
+ *   A pointer to the exposed GPU memory usable by the CPU, otherwise NULL and 
rte_errno is set:
+ *   - ENODEV if invalid dev_id
+ *   - ENOTSUP if operation not supported by the driver
+ *   - E2BIG if size is higher than limit
+ *   - ENOMEM if out of space
+ *   - EPERM if driver error
+ */
+__rte_experimental
+void *rte_gpu_mem_expose(int16_t dev_id, size_t size, void *ptr);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Unexpose a chunk of GPU memory previously exposed with rte_gpu_mem_expose()
+ *
+ * @param dev_id
+ *   Reference device ID.
+ * @param ptr
+ *   Pointer to the memory area to be unexposed.
+ *   NULL is a no-op accepted value.
+ *
+ * @return
+ *   0 on success, -rte_errno otherwise:
+ *   - ENODEV if invalid dev_id
+ *   - ENOTSUP if operation not supported by the driver
+ *   - EPERM if driver error
+ */
+__rte_experimental
+int rte_gpu_mem_unexpose(int16_t dev_id, void *ptr);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
diff --git a/lib/gpudev/version.map b/lib/gpudev/version.map
index 2e414c65cc..dce6085e44 100644
--- a/lib/gpudev/version.map
+++ b/lib/gpudev/version.map
@@ -20,8 +20,10 @@ EXPERIMENTAL {
        rte_gpu_init;
        rte_gpu_is_valid;
        rte_gpu_mem_alloc;
+       rte_gpu_mem_expose;
        rte_gpu_mem_free;
        rte_gpu_mem_register;
+       rte_gpu_mem_unexpose;
        rte_gpu_mem_unregister;
        rte_gpu_wmb;
 };
-- 
2.17.1

Reply via email to