[PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list

From: Alistair Popple

Date: Tue Dec 01 2026 - 02:09:53 EST


Xe maps an imported DMA-buf with dma_buf_map_sgt() and keeps the
sg_table as the backing of the buffer object. However the only thing it
ever reads from the table is the DMA address and length of each entry,
which the resource cursor walks when the buffer is bound to a VM or
mapped into the GGTT.

Map the attachment with dma_buf_map_dma_list() instead, which returns
just the DMA address ranges of the buffer. The list is kept in struct
xe_bo for as long as the buffer is mapped. It takes over from the sg
pointer in the TTM buffer object, which Xe no longer sets, as the record
of whether the attachment is mapped.

Add xe_res_first_bo_dma() to start a walk over either the list of an
imported buffer or the sg_table of a buffer's own pages, and use it
where an imported buffer can turn up: binding to a VM, mapping into the
GGTT and xe_bo_addr(). The other users of xe_bo_sg() only ever see
buffers which Xe allocated itself so they are left alone.

This works with any exporter. The DMA-buf core copies the addresses out
of the sg_table of an exporter which still maps one itself.

Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>

---

Note it wouldn't be too difficult to remove sg_table usage entirely
and instead use a dma_shared_buf. However this change is meant as an
example for how drivers could be switched to the new interface, so I
haven't done that.
---
drivers/gpu/drm/xe/xe_bo.c | 52 +++++++++++--------------
drivers/gpu/drm/xe/xe_bo_types.h | 6 +++
drivers/gpu/drm/xe/xe_ggtt.c | 2 +-
drivers/gpu/drm/xe/xe_pt.c | 4 +-
drivers/gpu/drm/xe/xe_res_cursor.h | 78 ++++++++++++++++++++++++++++++++++++++
5 files changed, 109 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 14b71d951659..475df4f24966 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -856,37 +856,35 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
struct ttm_resource *new_res)
{
struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
- struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
- ttm);
+ struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
bool device_unplugged = drm_dev_is_unplugged(&xe->drm);
- struct sg_table *sg;
+ const struct dma_addr_list *list;

xe_assert(xe, attach);
xe_assert(xe, ttm_bo->ttm);

if (device_unplugged && new_res->mem_type == XE_PL_SYSTEM &&
- ttm_bo->sg) {
+ bo->dma_list) {
dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
false, MAX_SCHEDULE_TIMEOUT);
- dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
+ dma_buf_unmap_dma_list(attach, bo->dma_list, DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
}

if (new_res->mem_type == XE_PL_SYSTEM)
goto out;

- if (ttm_bo->sg) {
- dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
+ if (bo->dma_list) {
+ dma_buf_unmap_dma_list(attach, bo->dma_list, DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
}

- sg = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
- if (IS_ERR(sg))
- return PTR_ERR(sg);
+ list = dma_buf_map_dma_list(attach, DMA_BIDIRECTIONAL);
+ if (IS_ERR(list))
+ return PTR_ERR(list);

- ttm_bo->sg = sg;
- xe_tt->sg = sg;
+ bo->dma_list = list;

out:
ttm_bo_move_null(ttm_bo, new_res);
@@ -1749,12 +1747,11 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
if (tt) {
struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);

- if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
- dma_buf_unmap_sgt(ttm_bo->base.import_attach,
- ttm_bo->sg,
- DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
- xe_tt->sg = NULL;
+ if (bo->dma_list) {
+ dma_buf_unmap_dma_list(ttm_bo->base.import_attach,
+ bo->dma_list,
+ DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
} else if (xe_tt->sg) {
dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
xe_tt->sg,
@@ -1863,14 +1860,10 @@ static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
* Object is idle and about to be destroyed. Release the
* dma-buf attachment.
*/
- if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
- struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
- struct xe_ttm_tt, ttm);
-
- dma_buf_unmap_sgt(ttm_bo->base.import_attach, ttm_bo->sg,
- DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
- xe_tt->sg = NULL;
+ if (bo->dma_list) {
+ dma_buf_unmap_dma_list(ttm_bo->base.import_attach, bo->dma_list,
+ DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
}
}

@@ -3453,8 +3446,7 @@ dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
xe_assert(xe, bo->ttm.ttm);

- xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
- page_size, &cur);
+ xe_res_first_bo_dma(bo, page << PAGE_SHIFT, page_size, &cur);
return xe_res_dma(&cur) + offset;
} else {
struct xe_res_cursor cur;
diff --git a/drivers/gpu/drm/xe/xe_bo_types.h b/drivers/gpu/drm/xe/xe_bo_types.h
index 8ec4a01a0092..dd00fae0610e 100644
--- a/drivers/gpu/drm/xe/xe_bo_types.h
+++ b/drivers/gpu/drm/xe/xe_bo_types.h
@@ -17,6 +17,7 @@
#include "xe_device_types.h"
#include "xe_ggtt_types.h"

+struct dma_addr_list;
struct xe_device;
struct xe_mem_pool_node;
struct xe_vm;
@@ -39,6 +40,11 @@ struct xe_bo {
struct xe_bo *parent_obj;
/** @dma_buf: Imported dma-buf ref to keep its resv alive. */
struct dma_buf *dma_buf;
+ /**
+ * @dma_list: DMA addresses of an imported dma-buf whilst it is mapped,
+ * NULL otherwise. Protected by the BO dma_resv.
+ */
+ const struct dma_addr_list *dma_list;
/** @flags: flags for this buffer object */
u32 flags;
/** @vm: VM this BO is attached to, for extobj this will be NULL */
diff --git a/drivers/gpu/drm/xe/xe_ggtt.c b/drivers/gpu/drm/xe/xe_ggtt.c
index b7c55dc8a0bb..21ea116a5f1c 100644
--- a/drivers/gpu/drm/xe/xe_ggtt.c
+++ b/drivers/gpu/drm/xe/xe_ggtt.c
@@ -741,7 +741,7 @@ static void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_ggtt_node *node,
if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
xe_assert(xe_bo_device(bo), bo->ttm.ttm);

- for (xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &cur);
+ for (xe_res_first_bo_dma(bo, 0, xe_bo_size(bo), &cur);
cur.remaining; xe_res_next(&cur, XE_PAGE_SIZE))
ggtt->pt_ops->ggtt_set_pte(ggtt, end - cur.remaining,
pte | xe_res_dma(&cur));
diff --git a/drivers/gpu/drm/xe/xe_pt.c b/drivers/gpu/drm/xe/xe_pt.c
index aeeac0f1ab83..b745de5210df 100644
--- a/drivers/gpu/drm/xe/xe_pt.c
+++ b/drivers/gpu/drm/xe/xe_pt.c
@@ -887,8 +887,8 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
xe_res_first(bo->ttm.resource, xe_vma_bo_offset(vma),
xe_vma_size(vma), &curs);
else
- xe_res_first_sg(xe_bo_sg(bo), xe_vma_bo_offset(vma),
- xe_vma_size(vma), &curs);
+ xe_res_first_bo_dma(bo, xe_vma_bo_offset(vma),
+ xe_vma_size(vma), &curs);
} else if (!range) {
curs.size = xe_vma_size(vma);
}
diff --git a/drivers/gpu/drm/xe/xe_res_cursor.h b/drivers/gpu/drm/xe/xe_res_cursor.h
index c3a037e5f34e..e6541b0c3eb0 100644
--- a/drivers/gpu/drm/xe/xe_res_cursor.h
+++ b/drivers/gpu/drm/xe/xe_res_cursor.h
@@ -24,6 +24,7 @@
#ifndef _XE_RES_CURSOR_H_
#define _XE_RES_CURSOR_H_

+#include <linux/dma-buf.h>
#include <linux/scatterlist.h>

#include <drm/drm_pagemap.h>
@@ -57,6 +58,8 @@ struct xe_res_cursor {
struct scatterlist *sgl;
/** @dma_addr: Current element in a struct drm_pagemap_addr array */
const struct drm_pagemap_addr *dma_addr;
+ /** @dma_range: Current range of a struct dma_addr_list */
+ const struct dma_addr_range *dma_range;
/** @mm: Buddy allocator for VRAM cursor */
struct gpu_buddy *mm;
/**
@@ -93,6 +96,7 @@ static inline void xe_res_first(struct ttm_resource *res,
{
cur->sgl = NULL;
cur->dma_addr = NULL;
+ cur->dma_range = NULL;
if (!res)
goto fallback;

@@ -172,6 +176,21 @@ static inline void __xe_res_sg_next(struct xe_res_cursor *cur)
cur->sgl = sgl;
}

+static inline void __xe_res_dma_list_next(struct xe_res_cursor *cur)
+{
+ const struct dma_addr_range *range = cur->dma_range;
+ u64 start = cur->start;
+
+ while (start >= range->len) {
+ start -= range->len;
+ range++;
+ }
+
+ cur->start = start;
+ cur->size = range->len - start;
+ cur->dma_range = range;
+}
+
/**
* __xe_res_dma_next() - Advance the cursor when end-of-segment is reached
* @cur: The cursor
@@ -222,11 +241,61 @@ static inline void xe_res_first_sg(const struct sg_table *sg,
cur->remaining = size;
cur->size = 0;
cur->dma_addr = NULL;
+ cur->dma_range = NULL;
cur->sgl = sg->sgl;
cur->mem_type = XE_PL_TT;
__xe_res_sg_next(cur);
}

+/**
+ * xe_res_first_dma_list - initialize a xe_res_cursor with a DMA address list
+ *
+ * @list: DMA address list of an imported dma-buf to walk
+ * @start: Start of the range
+ * @size: Size of the range
+ * @cur: cursor object to initialize
+ *
+ * Start walking over the range of allocations between @start and @size.
+ */
+static inline void xe_res_first_dma_list(const struct dma_addr_list *list,
+ u64 start, u64 size,
+ struct xe_res_cursor *cur)
+{
+ XE_WARN_ON(!list);
+ XE_WARN_ON(start + size > list->size);
+ cur->node = NULL;
+ cur->start = start;
+ cur->remaining = size;
+ cur->size = 0;
+ cur->dma_addr = NULL;
+ cur->sgl = NULL;
+ cur->dma_range = list->ranges;
+ cur->mem_type = XE_PL_TT;
+ __xe_res_dma_list_next(cur);
+}
+
+/**
+ * xe_res_first_bo_dma - initialize a xe_res_cursor with the DMA addresses of
+ * a buffer object
+ *
+ * @bo: buffer object to walk, which must not be in VRAM or stolen memory
+ * @start: Start of the range
+ * @size: Size of the range
+ * @cur: cursor object to initialize
+ *
+ * Start walking over the range of allocations between @start and @size. The
+ * addresses are those of the exporter's DMA address list for an imported
+ * dma-buf and those of the buffer object's own pages otherwise.
+ */
+static inline void xe_res_first_bo_dma(struct xe_bo *bo, u64 start, u64 size,
+ struct xe_res_cursor *cur)
+{
+ if (bo->dma_list)
+ xe_res_first_dma_list(bo->dma_list, start, size, cur);
+ else
+ xe_res_first_sg(xe_bo_sg(bo), start, size, cur);
+}
+
/**
* xe_res_first_dma - initialize a xe_res_cursor with dma_addr array
*
@@ -255,6 +324,7 @@ static inline void xe_res_first_dma(const struct drm_pagemap_addr *dma_addr,
cur->dma_addr = dma_addr;
__xe_res_dma_next(cur);
cur->sgl = NULL;
+ cur->dma_range = NULL;
cur->mem_type = XE_PL_TT;
}

@@ -296,6 +366,12 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
return;
}

+ if (cur->dma_range) {
+ cur->start += size;
+ __xe_res_dma_list_next(cur);
+ return;
+ }
+
switch (cur->mem_type) {
case XE_PL_STOLEN:
/* Just advance within the contiguous region. */
@@ -339,6 +415,8 @@ static inline u64 xe_res_dma(const struct xe_res_cursor *cur)
return cur->dma_start + cur->start;
else if (cur->sgl)
return sg_dma_address(cur->sgl) + cur->start;
+ else if (cur->dma_range)
+ return cur->dma_range->addr + cur->start;
else
return cur->start;
}

--
2.54.0