From: Alistair Popple <apopple@nvidia.com>
To: "Karol Wachowski" <karol.wachowski@linux.intel.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Christian König" <christian.koenig@amd.com>,
"Philipp Stanner" <phasta@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
"Matthew Brost" <matthew.brost@intel.com>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
"Leon Romanovsky" <leon@kernel.org>,
"Kees Cook" <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
"Alex Williamson" <alex@shazbot.org>,
"Kevin Tian" <kevin.tian@intel.com>,
"Joerg Roedel (AMD)" <joro@8bytes.org>,
"Will Deacon" <will@kernel.org>,
"Robin Murphy" <robin.murphy@arm.com>
Cc: dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org,
linaro-mm-sig@lists.linaro.org, intel-xe@lists.freedesktop.org,
linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org, kvm@vger.kernel.org,
iommu@lists.linux.dev, Alexey Kardashevskiy <aik@amd.com>,
Mina Almasry <almasrymina@google.com>,
Baolu Lu <baolu.lu@linux.intel.com>,
chaitanyak@nvidia.com, Dongwon Kim <dongwon.kim@intel.com>,
Felix Kuehling <felix.kuehling@amd.com>,
jhubbard@nvidia.com, Karl Mehltretter <kmehltretter@gmail.com>,
Vivek Kasireddy <vivek.kasireddy@intel.com>,
Xu Yilun <yilun.xu@linux.intel.com>,
zhipingz@meta.com, Alistair Popple <apopple@nvidia.com>
Subject: [PATCH RFC 15/16] drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer
Date: Tue, 01 Dec 2026 18:06:31 +1100 [thread overview]
Message-ID: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-15-b672717c7fc9@nvidia.com> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com>
Xe DMA maps its DMA-bufs for the importing device itself. A buffer in
system memory has its pages put in an sg_table and mapped with
dma_map_sgtable(). A buffer in VRAM has the address of each of its
blocks in the VRAM BAR mapped with dma_map_resource() and put in an
sg_table without pages. However nothing in either path depends on the
driver other than where the buffer currently is, and the VRAM path
duplicates what the dma-shared-buf code now does for exporters of MMIO.
Instead hand out a dma_shared_buf through map_dma_shared_buf() and
unmap_dma_shared_buf(). The dma_shared_buf for a buffer in system memory
is built from its pages by dma_shared_buf_from_pages(). A buffer in VRAM
is a list of its blocks with the p2pdma provider of the VRAM BAR, so
call pcim_p2pdma_init() at probe to have one. The placement is still
decided when the importer maps, and importers without allow_peer2peer
still have the buffer migrated to system memory first, so the
dma_shared_buf always matches what the core will hand that importer.
This removes open-coding of VRAM mapping, instead using the PCI
peer-to-peer core to correctly map it, and allows iommufd to create
mappings for GPU buffers.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/gpu/drm/xe/xe_dma_buf.c | 55 +++++-----------
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 117 +++++++++++++----------------------
drivers/gpu/drm/xe/xe_ttm_vram_mgr.h | 13 ++--
drivers/gpu/drm/xe/xe_vram.c | 21 +++++++
drivers/gpu/drm/xe/xe_vram.h | 2 +
5 files changed, 84 insertions(+), 124 deletions(-)
diff --git a/drivers/gpu/drm/xe/xe_dma_buf.c b/drivers/gpu/drm/xe/xe_dma_buf.c
index 97299ca8f831..8727ca456f2f 100644
--- a/drivers/gpu/drm/xe/xe_dma_buf.c
+++ b/drivers/gpu/drm/xe/xe_dma_buf.c
@@ -111,14 +111,14 @@ static void xe_dma_buf_unpin(struct dma_buf_attachment *attach)
xe_bo_unpin_external(bo);
}
-static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
- enum dma_data_direction dir)
+static struct dma_shared_buf *
+xe_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attach)
{
struct dma_buf *dma_buf = attach->dmabuf;
struct drm_gem_object *obj = dma_buf->priv;
struct xe_bo *bo = gem_to_xe_bo(obj);
struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
- struct sg_table *sgt;
+ struct dma_shared_buf *shared_buf;
int r = 0;
if (xe_bo_is_purged(bo))
@@ -138,49 +138,24 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
switch (bo->ttm.resource->mem_type) {
case XE_PL_TT:
- sgt = drm_prime_pages_to_sg(obj->dev,
- bo->ttm.ttm->pages,
- obj->size >> PAGE_SHIFT);
- if (IS_ERR(sgt))
- return sgt;
-
- if (dma_map_sgtable(attach->dev, sgt, dir,
- DMA_ATTR_SKIP_CPU_SYNC))
- goto error_free;
- break;
-
+ shared_buf = dma_shared_buf_from_pages(bo->ttm.ttm->pages,
+ obj->size >> PAGE_SHIFT);
+ if (!IS_ERR(shared_buf))
+ shared_buf->attrs = DMA_ATTR_SKIP_CPU_SYNC;
+ return shared_buf;
case XE_PL_VRAM0:
case XE_PL_VRAM1:
- r = xe_ttm_vram_mgr_alloc_sgt(xe_bo_device(bo),
- bo->ttm.resource, 0,
- bo->ttm.base.size, attach->dev,
- dir, &sgt);
- if (r)
- return ERR_PTR(r);
- break;
+ return xe_ttm_vram_mgr_alloc_shared_buf(xe_bo_device(bo),
+ bo->ttm.resource);
default:
return ERR_PTR(-EINVAL);
}
-
- return sgt;
-
-error_free:
- sg_free_table(sgt);
- kfree(sgt);
- return ERR_PTR(-EBUSY);
}
-static void xe_dma_buf_unmap(struct dma_buf_attachment *attach,
- struct sg_table *sgt,
- enum dma_data_direction dir)
+static void xe_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attach,
+ struct dma_shared_buf *shared_buf)
{
- if (sg_page(sgt->sgl)) {
- dma_unmap_sgtable(attach->dev, sgt, dir, 0);
- sg_free_table(sgt);
- kfree(sgt);
- } else {
- xe_ttm_vram_mgr_free_sgt(attach->dev, dir, sgt);
- }
+ kvfree(shared_buf);
}
static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
@@ -230,8 +205,8 @@ static const struct dma_buf_ops xe_dmabuf_ops = {
.detach = xe_dma_buf_detach,
.pin = xe_dma_buf_pin,
.unpin = xe_dma_buf_unpin,
- .map_dma_buf = xe_dma_buf_map,
- .unmap_dma_buf = xe_dma_buf_unmap,
+ .map_dma_shared_buf = xe_dma_buf_map_dma_shared_buf,
+ .unmap_dma_shared_buf = xe_dma_buf_unmap_dma_shared_buf,
.release = xe_dma_buf_release,
.begin_cpu_access = xe_dma_buf_begin_cpu_access,
.mmap = drm_gem_dmabuf_mmap,
diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
index f28550b29f6b..6e41af57ef78 100644
--- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
+++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
@@ -6,6 +6,7 @@
#include <linux/cgroup_dmem.h>
#include <linux/debugfs.h>
+#include <linux/dma-buf.h>
#include <drm/drm_managed.h>
#include <drm/drm_drv.h>
@@ -27,6 +28,7 @@
#include "xe_res_cursor.h"
#include "xe_ttm_stolen_mgr.h"
#include "xe_ttm_vram_mgr.h"
+#include "xe_vram.h"
#include "xe_vram_types.h"
static inline struct gpu_buddy_block *
@@ -500,98 +502,63 @@ int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram)
PAGE_SIZE);
}
-int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
- struct ttm_resource *res,
- u64 offset, u64 length,
- struct device *dev,
- enum dma_data_direction dir,
- struct sg_table **sgt)
+/**
+ * xe_ttm_vram_mgr_alloc_shared_buf() - dma_shared_buf of a VRAM resource
+ * @xe: xe device
+ * @res: The VRAM resource, all of which must be CPU visible
+ *
+ * The ranges are the addresses of the resource's blocks in the VRAM BAR, for
+ * a dma-buf exporter to hand out through &dma_buf_ops.map_dma_shared_buf.
+ *
+ * Return: The dma_shared_buf, to be freed with kvfree(), or an ERR_PTR() on
+ * failure.
+ */
+struct dma_shared_buf *
+xe_ttm_vram_mgr_alloc_shared_buf(struct xe_device *xe,
+ struct ttm_resource *res)
{
struct xe_tile *tile = &xe->tiles[res->mem_type - XE_PL_VRAM0];
struct xe_ttm_vram_mgr_resource *vres = to_xe_ttm_vram_mgr_resource(res);
+ struct p2pdma_provider *provider = xe_vram_p2pdma_provider(xe);
+ struct dma_shared_buf *shared_buf;
struct xe_res_cursor cursor;
- struct scatterlist *sg;
- int num_entries = 0;
- int i, r;
+ size_t nr_ranges = 0;
+ size_t i;
if (vres->used_visible_size < res->size)
- return -EOPNOTSUPP;
+ return ERR_PTR(-EOPNOTSUPP);
- *sgt = kmalloc_obj(**sgt);
- if (!*sgt)
- return -ENOMEM;
+ /* Without a provider the core would map the BAR as system memory */
+ if (!provider)
+ return ERR_PTR(-EOPNOTSUPP);
/* Determine the number of GPU_BUDDY blocks to export */
- xe_res_first(res, offset, length, &cursor);
+ xe_res_first(res, 0, res->size, &cursor);
while (cursor.remaining) {
- num_entries++;
- /* Limit maximum size to 2GiB due to SG table limitations. */
- xe_res_next(&cursor, min_t(u64, cursor.size, SZ_2G));
+ nr_ranges++;
+ xe_res_next(&cursor, cursor.size);
}
- r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
- if (r)
- goto error_free;
-
- /* Initialize scatterlist nodes of sg_table */
- for_each_sgtable_sg((*sgt), sg, i)
- sg->length = 0;
-
- /*
- * Walk down GPU_BUDDY blocks to populate scatterlist nodes
- * @note: Use iterator api to get first the GPU_BUDDY block
- * and the number of bytes from it. Access the following
- * GPU_BUDDY block(s) if more buffer needs to exported
- */
- xe_res_first(res, offset, length, &cursor);
- for_each_sgtable_sg((*sgt), sg, i) {
- phys_addr_t phys = cursor.start + xe_vram_region_io_start(tile->mem.vram);
- size_t size = min_t(u64, cursor.size, SZ_2G);
- dma_addr_t addr;
-
- addr = dma_map_resource(dev, phys, size, dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- r = dma_mapping_error(dev, addr);
- if (r)
- goto error_unmap;
-
- sg_set_page(sg, NULL, size, 0);
- sg_dma_address(sg) = addr;
- sg_dma_len(sg) = size;
-
- xe_res_next(&cursor, size);
- }
+ shared_buf = kvmalloc(struct_size(shared_buf, ranges, nr_ranges),
+ GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
- return 0;
+ shared_buf->provider = provider;
+ shared_buf->attrs = 0;
+ shared_buf->size = res->size;
+ shared_buf->nr_ranges = nr_ranges;
-error_unmap:
- for_each_sgtable_sg((*sgt), sg, i) {
- if (!sg->length)
- continue;
+ xe_res_first(res, 0, res->size, &cursor);
+ for (i = 0; i < nr_ranges; i++) {
+ shared_buf->ranges[i].paddr = cursor.start +
+ xe_vram_region_io_start(tile->mem.vram);
+ shared_buf->ranges[i].len = cursor.size;
- dma_unmap_resource(dev, sg->dma_address,
- sg->length, dir,
- DMA_ATTR_SKIP_CPU_SYNC);
+ xe_res_next(&cursor, cursor.size);
}
- sg_free_table(*sgt);
-
-error_free:
- kfree(*sgt);
- return r;
-}
-void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir,
- struct sg_table *sgt)
-{
- struct scatterlist *sg;
- int i;
-
- for_each_sgtable_sg(sgt, sg, i)
- dma_unmap_resource(dev, sg->dma_address,
- sg->length, dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- sg_free_table(sgt);
- kfree(sgt);
+ return shared_buf;
}
u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man)
diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h
index d77f067d197b..c1613efde23a 100644
--- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h
+++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h
@@ -8,7 +8,7 @@
#include "xe_ttm_vram_mgr_types.h"
-enum dma_data_direction;
+struct dma_shared_buf;
struct xe_device;
struct xe_tile;
struct xe_vram_region;
@@ -18,14 +18,9 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
u64 default_page_size);
int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram);
void xe_ttm_vram_debugfs_init(struct xe_device *xe, struct dentry *root);
-int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
- struct ttm_resource *res,
- u64 offset, u64 length,
- struct device *dev,
- enum dma_data_direction dir,
- struct sg_table **sgt);
-void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir,
- struct sg_table *sgt);
+struct dma_shared_buf *
+xe_ttm_vram_mgr_alloc_shared_buf(struct xe_device *xe,
+ struct ttm_resource *res);
u64 xe_ttm_vram_get_avail(struct ttm_resource_manager *man);
u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man);
diff --git a/drivers/gpu/drm/xe/xe_vram.c b/drivers/gpu/drm/xe/xe_vram.c
index dcedd8cfd731..c0b040582b81 100644
--- a/drivers/gpu/drm/xe/xe_vram.c
+++ b/drivers/gpu/drm/xe/xe_vram.c
@@ -5,6 +5,7 @@
#include <kunit/visibility.h>
#include <linux/pci.h>
+#include <linux/pci-p2pdma.h>
#include <drm/drm_managed.h>
#include <drm/drm_print.h>
@@ -320,6 +321,11 @@ int xe_vram_probe(struct xe_device *xe)
return err;
drm_info(&xe->drm, "VISIBLE VRAM: %pa, %pa\n", &lmem_bar.io_start, &lmem_bar.io_size);
+ /* VRAM is exported to other devices as ranges of the BAR */
+ err = pcim_p2pdma_init(to_pci_dev(xe->drm.dev));
+ if (err && err != -EOPNOTSUPP)
+ return err;
+
remain_io_size = lmem_bar.io_size;
for_each_tile(tile, xe, id) {
@@ -355,6 +361,21 @@ int xe_vram_probe(struct xe_device *xe)
return devm_add_action_or_reset(xe->drm.dev, vram_fini, xe);
}
+/**
+ * xe_vram_p2pdma_provider - Get the p2pdma provider of the VRAM BAR
+ * @xe: xe device
+ *
+ * Return: the provider, or NULL if the device has no VRAM BAR or the BAR
+ * cannot be used for PCI peer-to-peer DMA
+ */
+struct p2pdma_provider *xe_vram_p2pdma_provider(struct xe_device *xe)
+{
+ if (!IS_DGFX(xe))
+ return NULL;
+
+ return pcim_p2pdma_provider(to_pci_dev(xe->drm.dev), LMEM_BAR);
+}
+
/**
* xe_vram_region_io_start - Get the IO start of a VRAM region
* @vram: the VRAM region
diff --git a/drivers/gpu/drm/xe/xe_vram.h b/drivers/gpu/drm/xe/xe_vram.h
index 38425d81f777..3abe6c6e50f9 100644
--- a/drivers/gpu/drm/xe/xe_vram.h
+++ b/drivers/gpu/drm/xe/xe_vram.h
@@ -8,12 +8,14 @@
#include <linux/types.h>
+struct p2pdma_provider;
struct xe_device;
struct xe_vram_region;
struct ttm_resource;
struct xe_vram_region *xe_map_resource_to_region(struct ttm_resource *res);
int xe_vram_probe(struct xe_device *xe);
+struct p2pdma_provider *xe_vram_p2pdma_provider(struct xe_device *xe);
struct xe_vram_region *xe_vram_region_alloc(struct xe_device *xe, u8 id, u32 placement);
--
2.54.0
next prev parent reply other threads:[~2026-12-01 7:08 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 7:06 [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 01/16] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 08/16] dma-buf: Factor out the pin and fence wait around the exporter's map Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 09/16] dma-buf: Add dma_buf_map_dma_list() for DMA address only importers Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 10/16] vfio/pci: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 11/16] RDMA/uverbs: " Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 14/16] vfio/pci: Remove vfio_pci_dma_buf_iommufd_map() Alistair Popple
2026-12-01 7:06 ` Alistair Popple [this message]
2026-12-01 7:06 ` [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list Alistair Popple
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=20261201-dmabuf_shared_buf_v1_xe_rc5-v1-15-b672717c7fc9@nvidia.com \
--to=apopple@nvidia.com \
--cc=aik@amd.com \
--cc=airlied@gmail.com \
--cc=alex@shazbot.org \
--cc=almasrymina@google.com \
--cc=baolu.lu@linux.intel.com \
--cc=chaitanyak@nvidia.com \
--cc=christian.koenig@amd.com \
--cc=dongwon.kim@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=felix.kuehling@amd.com \
--cc=gustavoars@kernel.org \
--cc=intel-xe@lists.freedesktop.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joro@8bytes.org \
--cc=karol.wachowski@linux.intel.com \
--cc=kees@kernel.org \
--cc=kevin.tian@intel.com \
--cc=kmehltretter@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=leon@kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=matthew.brost@intel.com \
--cc=phasta@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rodrigo.vivi@intel.com \
--cc=simona@ffwll.ch \
--cc=sumit.semwal@linaro.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=vivek.kasireddy@intel.com \
--cc=will@kernel.org \
--cc=yilun.xu@linux.intel.com \
--cc=zhipingz@meta.com \
/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
all inboxes | Powered by JetHome®