DPDK patches and discussions
 help / color / mirror / Atom feed
From: Ajit Khaparde <ajit.khaparde@broadcom.com>
To: dev@dpdk.org
Cc: thomas@monjalon.net, eagostini@nvidia.com
Subject: [PATCH 1/2] gpudev: add API to get GPU physical address
Date: Thu,  5 Sep 2024 21:16:13 -0700	[thread overview]
Message-ID: <20240906041614.36962-2-ajit.khaparde@broadcom.com> (raw)
In-Reply-To: <20240906041614.36962-1-ajit.khaparde@broadcom.com>

Add API to get the physical address of the peer GPU.
This should allow some NIC hardware to directly use the
physical address for DMA instead of the CUDA Unified Memory
provided by rte_gpu_mem_map.

Signed-off-by: Ajit Khaparde <ajit.khaparde@broadcom.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 1c2011b856..acbe39361b 100644
--- a/lib/gpudev/gpudev.c
+++ b/lib/gpudev/gpudev.c
@@ -683,6 +683,44 @@ rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr)
 	}
 }
 
+void *
+rte_gpu_mem_dma_map(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, "mem CPU map for invalid device ID %d", dev_id);
+		rte_errno = ENODEV;
+		return NULL;
+	}
+
+	if (dev->ops.mem_cpu_map == NULL) {
+		GPU_LOG(ERR, "mem CPU map not supported");
+		rte_errno = ENOTSUP;
+		return NULL;
+	}
+
+	if (ptr == NULL || size == 0) /* dry-run  */
+		return NULL;
+
+	ret = GPU_DRV_RET(dev->ops.mem_dma_map(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_cpu_unmap(int16_t dev_id, void *ptr)
 {
@@ -706,6 +744,29 @@ rte_gpu_mem_cpu_unmap(int16_t dev_id, void *ptr)
 	return GPU_DRV_RET(dev->ops.mem_cpu_unmap(dev, ptr));
 }
 
+int
+rte_gpu_mem_dma_unmap(int16_t dev_id, void *ptr)
+{
+	struct rte_gpu *dev;
+
+	dev = gpu_get_by_id(dev_id);
+	if (dev == NULL) {
+		GPU_LOG(ERR, "cpu_unmap mem for invalid device ID %d", dev_id);
+		rte_errno = ENODEV;
+		return -rte_errno;
+	}
+
+	if (dev->ops.mem_dma_unmap == NULL) {
+		rte_errno = ENOTSUP;
+		return -rte_errno;
+	}
+
+	if (ptr == NULL) /* dry-run */
+		return 0;
+
+	return GPU_DRV_RET(dev->ops.mem_dma_unmap(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 37b6ae3149..ad3ab9e214 100644
--- a/lib/gpudev/gpudev_driver.h
+++ b/lib/gpudev/gpudev_driver.h
@@ -39,6 +39,8 @@ typedef int (rte_gpu_mem_unregister_t)(struct rte_gpu *dev, void *ptr);
 typedef int (rte_gpu_mem_cpu_map_t)(struct rte_gpu *dev, size_t size, void *ptr_in, void **ptr_out);
 typedef int (rte_gpu_mem_cpu_unmap_t)(struct rte_gpu *dev, void *ptr);
 typedef int (rte_gpu_wmb_t)(struct rte_gpu *dev);
+typedef int (rte_gpu_mem_dma_map_t)(struct rte_gpu *dev, size_t size, void *ptr_in, void **ptr_out);
+typedef int (rte_gpu_mem_dma_unmap_t)(struct rte_gpu *dev, void *ptr);
 
 struct rte_gpu_ops {
 	/* Get device info. If NULL, info is just copied. */
@@ -59,6 +61,10 @@ struct rte_gpu_ops {
 	rte_gpu_mem_cpu_unmap_t *mem_cpu_unmap;
 	/* Enforce GPU write memory barrier. */
 	rte_gpu_wmb_t *wmb;
+	/* DMA address of GPU memory. */
+	rte_gpu_mem_dma_map_t *mem_dma_map;
+	/* DMA unmap GPU memory. */
+	rte_gpu_mem_dma_unmap_t *mem_dma_unmap;
 };
 
 struct rte_gpu_mpshared {
diff --git a/lib/gpudev/rte_gpudev.h b/lib/gpudev/rte_gpudev.h
index 0a94a6abc4..ebc789880b 100644
--- a/lib/gpudev/rte_gpudev.h
+++ b/lib/gpudev/rte_gpudev.h
@@ -484,6 +484,34 @@ int rte_gpu_mem_unregister(int16_t dev_id, void *ptr);
 __rte_experimental
 void *rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Map a chunk of GPU memory to make it accessible for DMA
+ * 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 mapped memory.
+ * @param size
+ *   Number of bytes to map.
+ *   Requesting 0 will do nothing.
+ * @param ptr
+ *   Pointer to the GPU memory area to be mapped for DMA.
+ *   NULL is a no-op accepted value.
+
+ * @return
+ *   A pointer to the mapped 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_dma_map(int16_t dev_id, size_t size, void *ptr);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
@@ -505,6 +533,27 @@ void *rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr);
 __rte_experimental
 int rte_gpu_mem_cpu_unmap(int16_t dev_id, void *ptr);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Unmap a chunk of GPU memory previously mapped with rte_gpu_mem_dma_map()
+ *
+ * @param dev_id
+ *   Reference device ID.
+ * @param ptr
+ *   Pointer to the GPU memory area to be unmapped.
+ *   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_dma_unmap(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 a2c8ce5759..f267b5e029 100644
--- a/lib/gpudev/version.map
+++ b/lib/gpudev/version.map
@@ -28,6 +28,8 @@ EXPERIMENTAL {
 	rte_gpu_mem_cpu_unmap;
 	rte_gpu_mem_unregister;
 	rte_gpu_wmb;
+	rte_gpu_mem_dma_map;
+	rte_gpu_mem_dma_unmap;
 };
 
 INTERNAL {
-- 
2.39.3 (Apple Git-146)


  reply	other threads:[~2024-09-06  4:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-06  4:16 [PATCH 0/2] " Ajit Khaparde
2024-09-06  4:16 ` Ajit Khaparde [this message]
2024-09-06  4:16 ` [PATCH 2/2] gpu/cuda: extend cuda code to get PA of GPU Ajit Khaparde

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=20240906041614.36962-2-ajit.khaparde@broadcom.com \
    --to=ajit.khaparde@broadcom.com \
    --cc=dev@dpdk.org \
    --cc=eagostini@nvidia.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).