Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Auld <matthew.auld@intel.com>
To: Matthew Brost <matthew.brost@intel.com>,
	intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Cc: "Carlos Santa" <carlos.santa@intel.com>,
	"Ryan Neph" <ryanneph@google.com>,
	"Natalie Vock" <nat@pixelcluster.dev>,
	"Arun Pravin" <arunpravin.paneerselvam@amd.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	linux-kernel@vger.kernel.org,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>
Subject: Re: [PATCH v6 28/35] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs
Date: Fri, 4 Dec 2026 11:57:12 +0000	[thread overview]
Message-ID: <e2eb7532-e10b-4b39-b7d4-b0a8542d371e@intel.com> (raw)
In-Reply-To: <20261203202352.4153159-29-matthew.brost@intel.com>

On 03/12/2026 20:23, Matthew Brost wrote:
> Add support for mapping a ttm_tt's pages using the IOVA-based DMA API
> (dma_iova_try_alloc()/link()/sync()/unlink()/free()) as an alternative
> to the scatter-gather list. An IOVA mapping is a single contiguous DMA
> range, which is cheaper to walk and program than an sg list.
> 
> Whether the IOVA API can be used depends on the IOMMU mode, so
> eligibility is decided once per tt at xe_ttm_tt_create() time and
> recorded in struct xe_ttm_tt::iova_eligible. To keep the scope limited,
> only device BOs that are neither GGTT-mapped nor pinned
> (XE_BO_FLAG_PINNED) are eligible: dma-buf imports manage their own
> mapping, GGTT BOs are mostly pinned kernel BOs, and pinned BOs are not
> evicted by xe_bo_pci_dev_remove_all(), so their IOVA state could
> outlive the device's IOMMU group on hot-unplug.
> 
> The IOVA reservation is taken when the tt is DMA mapped and released
> when it is unmapped, so it never outlives the device. xe_tt_map() calls
> xe_tt_iova_reserve(), which allocates the range with
> dma_iova_try_alloc() and sets xe_ttm_tt::use_iova, then
> xe_tt_map_iova() links the tt pages (one folio at a time) and syncs the
> IOTLB. xe_tt_unmap_iova() destroys both the mapping and the reservation.
> If dma_iova_try_alloc() fails, the tt transparently falls back to the sg
> list, which remains the default. xe_tt_map()/xe_tt_unmap() dispatch
> between the IOVA and sg backends, and xe_bo_dma_unmap_pinned() learns
> about the IOVA case.
> 
> Consumers that walked the sg list via xe_res_first_sg(xe_bo_sg()),
> including xe_ggtt_map_bo(), now go through the new xe_res_first_tt()
> helper, which initializes either an IOVA cursor (xe_res_first_iova()) or
> an sg cursor depending on the tt's mapping mode.
> 
> v4:
>   - Shuffle around dma_iova_try_iova/link/sync/unlink/free to be unplug
>     safe (Auld)
> v5:
>   - Reword commit message (Sashiko)
>   - Rebase on 3 unrelated fixes flagged by Sashiko
> 
> Cc: Carlos Santa <carlos.santa@intel.com>
> Cc: Ryan Neph <ryanneph@google.com>
> Cc: Natalie Vock <nat@pixelcluster.dev>
> Cc: Arun Pravin <arunpravin.paneerselvam@amd.com>
> Cc: Matthew Auld <matthew.auld@intel.com>
> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
> Cc: Maxime Ripard <mripard@kernel.org>
> Cc: Thomas Zimmermann <tzimmermann@suse.de>
> Cc: David Airlie <airlied@gmail.com>
> Cc: Simona Vetter <simona@ffwll.ch>
> Cc: dri-devel@lists.freedesktop.org
> Cc: linux-kernel@vger.kernel.org
> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
> Signed-off-by: Matthew Brost <matthew.brost@intel.com>
> ---
>   drivers/gpu/drm/xe/tests/xe_migrate.c |   6 +-
>   drivers/gpu/drm/xe/xe_bo.c            | 323 ++++++++++++++++++++++++--
>   drivers/gpu/drm/xe/xe_bo.h            |  18 +-
>   drivers/gpu/drm/xe/xe_ggtt.c          |   2 +-
>   drivers/gpu/drm/xe/xe_migrate.c       |  14 +-
>   drivers/gpu/drm/xe/xe_pt.c            |   2 +-
>   6 files changed, 323 insertions(+), 42 deletions(-)
> 
> diff --git a/drivers/gpu/drm/xe/tests/xe_migrate.c b/drivers/gpu/drm/xe/tests/xe_migrate.c
> index eba991e28af0..045629c6d6da 100644
> --- a/drivers/gpu/drm/xe/tests/xe_migrate.c
> +++ b/drivers/gpu/drm/xe/tests/xe_migrate.c
> @@ -255,7 +255,7 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,
>   	if (xe_bo_is_vram(pt))
>   		xe_res_first(pt->ttm.resource, 0, xe_bo_size(pt), &src_it);
>   	else
> -		xe_res_first_sg(xe_bo_sg(pt), 0, xe_bo_size(pt), &src_it);
> +		xe_res_first_tt(pt->ttm.ttm, 0, xe_bo_size(pt), &src_it);
>   
>   	emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
>   		 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
> @@ -387,12 +387,12 @@ static struct dma_fence *blt_copy(struct xe_tile *tile,
>   	bool dst_is_vram = mem_type_is_vram(dst->mem_type);
>   
>   	if (!src_is_vram)
> -		xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
> +		xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
>   	else
>   		xe_res_first(src, 0, size, &src_it);
>   
>   	if (!dst_is_vram)
> -		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
> +		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
>   	else
>   		xe_res_first(dst, 0, size, &dst_it);
>   
> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index 8549a3157716..06922c386c28 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c
> @@ -6,6 +6,8 @@
>   #include "xe_bo.h"
>   
>   #include <linux/dma-buf.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/iommu-dma.h>
>   #include <linux/nodemask.h>
>   #include <linux/nospec.h>
>   
> @@ -17,6 +19,7 @@
>   #include <drm/ttm/ttm_backup.h>
>   #include <drm/ttm/ttm_device.h>
>   #include <drm/ttm/ttm_placement.h>
> +#include <drm/ttm/ttm_pool.h>
>   #include <drm/ttm/ttm_tt.h>
>   #include <uapi/drm/xe_drm.h>
>   
> @@ -442,6 +445,40 @@ struct xe_ttm_tt {
>   	struct ttm_tt ttm;
>   	struct sg_table sgt;
>   	struct sg_table *sg;
> +	/**
> +	 * @iova: IOVA state describing the single contiguous IOVA mapping of
> +	 * the tt pages. Only valid when @use_iova is true.
> +	 */
> +	struct dma_iova_state iova;
> +	/**
> +	 * @iova_eligible: Whether this tt may use the IOVA-based DMA API
> +	 * instead of a scatter-gather list. A policy decision taken once at tt
> +	 * creation time and stable for the lifetime of the tt.
> +	 */
> +	bool iova_eligible;
> +	/**
> +	 * @use_iova: Whether this tt currently holds an IOVA reservation and
> +	 * is therefore DMA mapped using the IOVA-based DMA API instead of a
> +	 * scatter-gather list. The reservation is taken when the tt is DMA
> +	 * mapped and released when it is unmapped, so that it never outlives
> +	 * the device's IOMMU domain (see xe_bo_pci_dev_remove_all()).
> +	 */
> +	bool use_iova;
> +	/**
> +	 * @iova_linked: Whether the tt pages are currently linked into the
> +	 * IOVA mapping. Only meaningful when @use_iova is true.
> +	 */
> +	bool iova_linked;
> +#if IS_ENABLED(CONFIG_DEBUG_FS)
> +	/**
> +	 * @iova_dma_pages: Per-order count of DMA-mapped pages this tt
> +	 * accounted into xe->mem.dma_mapped_pages[] for its IOVA mapping,
> +	 * recorded at link time so the unlink can apply an exactly matching
> +	 * decrement without re-walking tt->pages (which TTM may mutate, e.g.
> +	 * during defrag). Counter ownership stays isolated in xe_bo.c.
> +	 */
> +	unsigned long iova_dma_pages[NR_PAGE_ORDERS];
> +#endif
>   	/** @purgeable: Whether the content of the pages of @ttm is purgeable. */
>   	bool purgeable;
>   };
> @@ -490,6 +527,52 @@ static void xe_tt_account_dma_pages(struct xe_device *xe,
>   }
>   #endif
>   
> +/*
> + * xe_tt_account_iova_add - account one freshly linked IOVA folio
> + * @xe: the xe device
> + * @xe_tt: the xe_ttm_tt being mapped
> + * @order: page order of the linked folio
> + *
> + * Add the folio's 2^order pages to xe->mem.dma_mapped_pages[order] and record
> + * the same amount in @xe_tt so xe_tt_unaccount_iova() can later subtract an
> + * exactly matching count without re-walking tt->pages.
> + */
> +#if IS_ENABLED(CONFIG_DEBUG_FS)
> +static void xe_tt_account_iova_add(struct xe_device *xe,
> +				   struct xe_ttm_tt *xe_tt, unsigned int order)
> +{
> +	unsigned long chunk = 1UL << order;
> +
> +	xe_tt->iova_dma_pages[order] += chunk;
> +	atomic_long_add(chunk, &xe->mem.dma_mapped_pages[order]);
> +}
> +
> +static void xe_tt_unaccount_iova(struct xe_device *xe,
> +				 struct xe_ttm_tt *xe_tt)
> +{
> +	unsigned int order;
> +
> +	for (order = 0; order < NR_PAGE_ORDERS; order++) {
> +		unsigned long pages = xe_tt->iova_dma_pages[order];
> +
> +		if (pages) {
> +			atomic_long_sub(pages, &xe->mem.dma_mapped_pages[order]);
> +			xe_tt->iova_dma_pages[order] = 0;
> +		}
> +	}
> +}
> +#else
> +static void xe_tt_account_iova_add(struct xe_device *xe,
> +				   struct xe_ttm_tt *xe_tt, unsigned int order)
> +{
> +}
> +
> +static void xe_tt_unaccount_iova(struct xe_device *xe,
> +				 struct xe_ttm_tt *xe_tt)
> +{
> +}
> +#endif
> +
>   static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
>   {
>   	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> @@ -536,28 +619,197 @@ static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt)
>   	}
>   }
>   
> -struct sg_table *xe_bo_sg(struct xe_bo *bo)
> +/*
> + * xe_tt_iova_reserve - Reserve the IOVA range backing a tt's DMA mapping.
> + *
> + * The reservation is taken when the tt is DMA mapped rather than when it is
> + * created, so that it is always released by xe_tt_unmap_iova() while the
> + * device's IOMMU domain is still alive. A tt whose reservation outlived
> + * pci device removal would hit a NULL iommu group in dma_iova_free().
> + *
> + * Return: true if @xe_tt holds a reservation on return, false if the caller
> + * should fall back to the scatter-gather path.
> + */
> +static bool xe_tt_iova_reserve(struct xe_device *xe, struct ttm_tt *tt)
>   {
> -	struct ttm_tt *tt = bo->ttm.ttm;
>   	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
>   
> -	return xe_tt->sg;
> +	if (xe_tt->use_iova)
> +		return true;
> +
> +	if (!xe_tt->iova_eligible)
> +		return false;
> +
> +	if (!dma_iova_try_alloc(xe->drm.dev, &xe_tt->iova, 0,
> +				(size_t)tt->num_pages << PAGE_SHIFT))
> +		return false;
> +
> +	xe_tt->use_iova = true;
> +	return true;
> +}
> +
> +/*
> + * xe_tt_map_iova - DMA map a tt using the IOVA-based DMA API.
> + *
> + * Link all of the tt's pages into the IOVA range reserved by
> + * xe_tt_iova_reserve(), one folio at a time, and sync the IOTLB once at the
> + * end.
> + */
> +static int xe_tt_map_iova(struct xe_device *xe, struct ttm_tt *tt)
> +{
> +	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +	struct device *dev = xe->drm.dev;
> +	unsigned long num_pages = tt->num_pages;
> +	unsigned long i = 0;
> +	size_t mapped = 0;
> +	int ret;
> +
> +	if (xe_tt->iova_linked)
> +		return 0;
> +
> +	while (i < num_pages) {
> +		struct page *page = tt->pages[i];
> +		unsigned int order = ttm_pool_page_order_nodma(page);
> +		size_t len = PAGE_SIZE << order;
> +
> +		ret = dma_iova_link(dev, &xe_tt->iova, page_to_phys(page),
> +				    mapped, len, DMA_BIDIRECTIONAL,
> +				    DMA_ATTR_SKIP_CPU_SYNC);
> +		if (ret)
> +			goto err_unlink;
> +
> +		xe_tt_account_iova_add(xe, xe_tt, order);
> +		mapped += len;
> +		i += 1UL << order;
> +	}
> +
> +	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
> +	if (ret)
> +		goto err_unlink;
> +
> +	xe_tt->iova_linked = true;
> +	return 0;
> +
> +err_unlink:
> +	if (mapped) {
> +		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
> +				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
> +		xe_tt_unaccount_iova(xe, xe_tt);
> +	}
> +	/*
> +	 * Drop the reservation as well: a failed map leaves no DMA mapping
> +	 * behind, and the reservation must not outlive the device's IOMMU
> +	 * domain on a teardown path that never calls xe_tt_unmap().
> +	 */
> +	dma_iova_free(dev, &xe_tt->iova);
> +	xe_tt->use_iova = false;
> +	return ret;
> +}
> +
> +/*
> + * xe_tt_unmap_iova - Unlink a tt's pages and release its IOVA reservation.
> + *
> + * Both the mapping and the reservation are torn down here so that no IOMMU
> + * state survives beyond the tt's DMA mapping. The eviction performed by
> + * xe_bo_pci_dev_remove_all() therefore drains every reservation while the
> + * device is still bound to its IOMMU domain.
> + */
> +static void xe_tt_unmap_iova(struct xe_device *xe, struct ttm_tt *tt)
> +{
> +	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +
> +	if (!xe_tt->use_iova)
> +		return;
> +
> +	if (xe_tt->iova_linked) {
> +		/*
> +		 * Unmap with attrs 0 (matching the sg unmap path) so that the
> +		 * CPU copy-back sync is performed for non-coherent or bounced
> +		 * (SWIOTLB) mappings, even though the link was done with
> +		 * DMA_ATTR_SKIP_CPU_SYNC.
> +		 */
> +		dma_iova_destroy(xe->drm.dev, &xe_tt->iova,
> +				 dma_iova_size(&xe_tt->iova),
> +				 DMA_BIDIRECTIONAL, 0);
> +		xe_tt_unaccount_iova(xe, xe_tt);
> +		xe_tt->iova_linked = false;
> +	} else {
> +		dma_iova_free(xe->drm.dev, &xe_tt->iova);
> +	}
> +
> +	xe_tt->use_iova = false;
> +}
> +
> +/*
> + * xe_tt_map - DMA map a tt, using IOVA when available and sg otherwise.
> + */
> +static int xe_tt_map(struct xe_device *xe, struct ttm_tt *tt)
> +{
> +	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +
> +	/*
> +	 * An already sg-mapped tt keeps its sg mapping: taking an IOVA
> +	 * reservation here would establish a second, redundant mapping.
> +	 */
> +	if (!xe_tt->sg && xe_tt_iova_reserve(xe, tt))
> +		return xe_tt_map_iova(xe, tt);
> +
> +	return xe_tt_map_sg(xe, tt);
> +}
> +
> +/*
> + * xe_tt_unmap - Undo xe_tt_map().
> + */
> +static void xe_tt_unmap(struct xe_device *xe, struct ttm_tt *tt)
> +{
> +	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +
> +	if (xe_tt->use_iova)
> +		xe_tt_unmap_iova(xe, tt);
> +	else
> +		xe_tt_unmap_sg(xe, tt);
>   }
>   
>   /**
> - * xe_tt_sg() - Return the DMA scatter-gather table backing a ttm_tt.
> - * @tt: The ttm_tt to query.
> + * xe_res_first_tt() - Initialize a resource cursor over a ttm_tt's DMA mapping.
> + * @tt: The ttm_tt to walk.
> + * @start: Start of the range, in bytes.
> + * @size: Size of the range, in bytes.
> + * @cur: The cursor to initialize.
>    *
> - * Like xe_bo_sg(), but takes the ttm_tt directly. Used by the defrag copy
> - * path which needs the old (source) tt's sg table without a backing xe_bo.
> + * Initialize @cur to walk the DMA addresses backing @tt, transparently
> + * handling both the IOVA-based mapping (single contiguous range) and the
> + * scatter-gather list fallback.
> + */
> +void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
> +		     struct xe_res_cursor *cur)
> +{
> +	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +
> +	if (xe_tt->use_iova)
> +		xe_res_first_iova(&xe_tt->iova, start, size, cur);
> +	else
> +		xe_res_first_sg(xe_tt->sg, start, size, cur);
> +}
> +
> +/**
> + * xe_bo_sg_is_contiguous() - Check if a BO's DMA address space is contiguous.
> + * @bo: the BO to check (must have a valid tt mapping, i.e. !xe_bo_is_vram())
> + * @len: required contiguous length in bytes
>    *
> - * Return: The sg table, or NULL if the tt is not currently mapped.
> + * Returns true if the first @len bytes of the BO are mapped to a contiguous
> + * DMA address range. An IOVA-mapped tt is a single contiguous range by
> + * construction, so only the sg fallback has to be walked.
>    */
> -struct sg_table *xe_tt_sg(struct ttm_tt *tt)
> +bool xe_bo_sg_is_contiguous(struct xe_bo *bo, size_t len)
>   {
> +	struct ttm_tt *tt = bo->ttm.ttm;
>   	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
>   
> -	return xe_tt->sg;
> +	if (xe_tt->use_iova)
> +		return xe_tt->iova_linked && xe_bo_size(bo) >= len;
> +
> +	return drm_prime_get_contiguous_size(xe_tt->sg) >= len;
>   }
>   
>   /*
> @@ -772,6 +1024,30 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
>   		}
>   	}
>   
> +	/*
> +	 * Eligible BOs are DMA mapped using the IOVA-based DMA API, which
> +	 * yields a single contiguous IOVA range instead of a scatter-gather
> +	 * list. Only evictable device BOs that are not GGTT-mapped qualify:
> +	 * dma-buf imports manage their own mapping and GGTT bindings are
> +	 * programmed directly from the sg list.
> +	 *
> +	 * Pinned BOs are excluded because they are invisible to the eviction
> +	 * done by xe_bo_pci_dev_remove_all(): a BO pinned in XE_PL_TT (e.g. a
> +	 * page table on integrated) keeps its mapping until the BO is
> +	 * destroyed, which may happen long after the pci device was removed
> +	 * (an open drm file outliving a hot-unplug). Releasing IOVA state at
> +	 * that point would dereference a NULL iommu group.

This sounds like pre-existing bug? Whether its IOVA or other api I don't 
think iommu mappings are meant to survive the pci unplug? I think 
page-tables not being unmapped was a miss.

Otherwise,
Reviewed-by: Matthew Auld <matthew.auld@intel.com>

> +	 *
> +	 * The IOVA range itself is reserved when the tt is DMA mapped and
> +	 * released when it is unmapped, so it never outlives the device;
> +	 * dma_iova_try_alloc() may still fail there, in which case we
> +	 * transparently fall back to the sg list.
> +	 */
> +	xe_tt->iova_eligible = ttm_bo->type != ttm_bo_type_sg &&
> +		!(bo->flags & (XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_ALL |
> +			       XE_BO_FLAG_PINNED)) &&
> +		use_dma_iommu(xe->drm.dev);
> +
>   	return tt;
>   }
>   
> @@ -820,7 +1096,7 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
>   	    !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
>   		return;
>   
> -	xe_tt_unmap_sg(xe, tt);
> +	xe_tt_unmap(xe, tt);
>   
>   	ttm_pool_free(&ttm_dev->pool, tt);
>   	xe_ttm_tt_account_subtract(xe, tt);
> @@ -829,6 +1105,19 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
>   
>   static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
>   {
> +	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
> +	struct xe_device *xe = ttm_to_xe_device(ttm_dev);
> +
> +	/*
> +	 * No IOVA state may survive until here: a tt can be destroyed long
> +	 * after the pci device was removed (e.g. an open drm file outliving a
> +	 * hot-unplug), at which point the device has no IOMMU domain left and
> +	 * any dma_iova_*() call would dereference a NULL iommu group. The
> +	 * reservation is instead released by xe_tt_unmap_iova().
> +	 */
> +	xe_assert(xe, !xe_tt->use_iova);
> +	xe_assert(xe, !xe_tt->iova_linked);
> +
>   	ttm_tt_fini(tt);
>   	kfree(tt);
>   }
> @@ -1861,7 +2150,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
>   	/* Bo creation path, moving to system or TT. */
>   	if ((!old_mem && ttm) && !handle_system_ccs) {
>   		if (new_mem->mem_type == XE_PL_TT)
> -			ret = xe_tt_map_sg(xe, ttm);
> +			ret = xe_tt_map(xe, ttm);
>   		if (!ret)
>   			ttm_bo_move_null(ttm_bo, new_mem);
>   		goto out;
> @@ -1901,7 +2190,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
>   		(!ttm && ttm_bo->type == ttm_bo_type_device);
>   
>   	if (new_mem->mem_type == XE_PL_TT) {
> -		ret = xe_tt_map_sg(xe, ttm);
> +		ret = xe_tt_map(xe, ttm);
>   		if (ret)
>   			goto out;
>   	}
> @@ -2091,7 +2380,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
>   		if (IS_VF_CCS_READY(xe))
>   			xe_sriov_vf_ccs_detach_bo(bo);
>   
> -		xe_tt_unmap_sg(xe, ttm_bo->ttm);
> +		xe_tt_unmap(xe, ttm_bo->ttm);
>   	}
>   
>   	/*
> @@ -2212,7 +2501,7 @@ static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
>   		if (lret)
>   			return lret;
>   
> -		xe_tt_unmap_sg(xe, bo->ttm);
> +		xe_tt_unmap(xe, bo->ttm);
>   		ttm_bo_move_null(bo, new_resource);
>   	}
>   
> @@ -2647,6 +2936,8 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
>   					  DMA_BIDIRECTIONAL);
>   			ttm_bo->sg = NULL;
>   			xe_tt->sg = NULL;
> +		} else if (xe_tt->use_iova) {
> +			xe_tt_unmap_iova(ttm_to_xe_device(ttm_bo->bdev), tt);
>   		} else if (xe_tt->sg) {
>   			xe_tt_unmap_sg(ttm_to_xe_device(ttm_bo->bdev),
>   				       &xe_tt->ttm);
> @@ -4366,7 +4657,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,
> +		xe_res_first_tt(bo->ttm.ttm, page << PAGE_SHIFT,
>   				page_size, &cur);
>   		return xe_res_dma(&cur) + offset;
>   	} else {
> diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
> index 48298a372e70..871d1aa1425c 100644
> --- a/drivers/gpu/drm/xe/xe_bo.h
> +++ b/drivers/gpu/drm/xe/xe_bo.h
> @@ -573,21 +573,11 @@ void xe_bo_dev_init(struct xe_bo_dev *bo_device);
>   
>   void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
>   
> -struct sg_table *xe_bo_sg(struct xe_bo *bo);
> -struct sg_table *xe_tt_sg(struct ttm_tt *tt);
> +struct xe_res_cursor;
> +void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
> +		     struct xe_res_cursor *cur);
>   
> -/**
> - * xe_bo_sg_is_contiguous() - Check if a BO's DMA address space is contiguous.
> - * @bo: the BO to check (must have a valid sg table, i.e. !xe_bo_is_vram())
> - * @len: required contiguous length in bytes
> - *
> - * Returns true if the first @len bytes of the BO are mapped to a contiguous
> - * DMA address range.
> - */
> -static inline bool xe_bo_sg_is_contiguous(struct xe_bo *bo, size_t len)
> -{
> -	return drm_prime_get_contiguous_size(xe_bo_sg(bo)) >= len;
> -}
> +bool xe_bo_sg_is_contiguous(struct xe_bo *bo, size_t len);
>   
>   /*
>    * xe_sg_segment_size() - Provides upper limit for sg segment size.
> diff --git a/drivers/gpu/drm/xe/xe_ggtt.c b/drivers/gpu/drm/xe/xe_ggtt.c
> index c24b7814c3ab..9c86bd086323 100644
> --- a/drivers/gpu/drm/xe/xe_ggtt.c
> +++ b/drivers/gpu/drm/xe/xe_ggtt.c
> @@ -1012,7 +1012,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_tt(bo->ttm.ttm, 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_migrate.c b/drivers/gpu/drm/xe/xe_migrate.c
> index 1516985c9387..6f88aa210ec3 100644
> --- a/drivers/gpu/drm/xe/xe_migrate.c
> +++ b/drivers/gpu/drm/xe/xe_migrate.c
> @@ -1452,16 +1452,16 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
>   		return ERR_PTR(-EINVAL);
>   
>   	if (!src_is_vram && src->mem_type != XE_PL_STOLEN)
> -		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo), 0, size, &src_it);
> +		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm, 0, size, &src_it);
>   	else
>   		xe_res_first(src, 0, size, &src_it);
>   	if (!dst_is_vram && dst->mem_type != XE_PL_STOLEN)
> -		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
> +		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
>   	else
>   		xe_res_first(dst, 0, size, &dst_it);
>   
>   	if (copy_system_ccs)
> -		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo),
> +		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm,
>   				xe_bo_ccs_pages_start(src_bo),
>   				PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
>   				&ccs_it);
> @@ -1845,9 +1845,9 @@ int xe_migrate_ccs_rw_copy(struct xe_tile *tile, struct xe_exec_queue *q,
>   
>   	ctx = &xe->sriov.vf.ccs.contexts[read_write];
>   
> -	xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
> +	xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
>   
> -	xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo),
> +	xe_res_first_tt(src_bo->ttm.ttm, xe_bo_ccs_pages_start(src_bo),
>   			PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
>   			&ccs_it);
>   
> @@ -2054,7 +2054,7 @@ struct dma_fence *xe_migrate_vram_copy_chunk(struct xe_bo *vram_bo, u64 vram_off
>   	xe_assert(xe, !range_overflows(sysmem_offset, size, (u64)sysmem_bo->ttm.base.size));
>   
>   	xe_res_first(vram, vram_offset, size, &vram_it);
> -	xe_res_first_sg(xe_bo_sg(sysmem_bo), sysmem_offset, size, &sysmem_it);
> +	xe_res_first_tt(sysmem_bo->ttm.ttm, sysmem_offset, size, &sysmem_it);
>   
>   	while (size) {
>   		u32 pte_flags = PTE_UPDATE_FLAG_IS_VRAM;
> @@ -2265,7 +2265,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
>   		clear_only_system_ccs = true;
>   
>   	if (!clear_vram && dst->mem_type != XE_PL_STOLEN)
> -		xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &src_it);
> +		xe_res_first_tt(bo->ttm.ttm, 0, xe_bo_size(bo), &src_it);
>   	else
>   		xe_res_first(src, 0, xe_bo_size(bo), &src_it);
>   
> diff --git a/drivers/gpu/drm/xe/xe_pt.c b/drivers/gpu/drm/xe/xe_pt.c
> index 0e0bf428df0f..42b3fb544861 100644
> --- a/drivers/gpu/drm/xe/xe_pt.c
> +++ b/drivers/gpu/drm/xe/xe_pt.c
> @@ -887,7 +887,7 @@ 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_res_first_tt(bo->ttm.ttm, xe_vma_bo_offset(vma),
>   					xe_vma_size(vma), &curs);
>   	} else if (!range) {
>   		curs.size = xe_vma_size(vma);


  reply	other threads:[~2026-12-04 11:57 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 20:23 [PATCH v6 00/35] drm/ttm, drm/xe: Avoid foreground high-order reclaim and defragment sub-optimally backed BOs Matthew Brost
2026-12-03 20:23 ` [PATCH v6 01/35] drm/ttm/pool: Allow backing off reclaim at the beneficial order Matthew Brost
2026-12-03 20:23 ` [PATCH v6 02/35] drm/ttm/pool: Add ttm_pool_page_order_nodma() helper Matthew Brost
2026-12-04 13:12   ` Natalie Vock
2026-12-04 17:36     ` Matthew Brost
2026-12-03 20:23 ` [PATCH v6 03/35] drm/ttm: Record sub-optimal page order allocations in ttm_tt Matthew Brost
2026-12-04 13:16   ` Natalie Vock
2026-12-03 20:23 ` [PATCH v6 04/35] drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() Matthew Brost
2026-12-04 13:39   ` Natalie Vock
2026-12-03 20:23 ` [PATCH v6 05/35] drm/ttm: Support defragmentation moves Matthew Brost
2026-12-04 14:04   ` Natalie Vock
2026-12-04 19:17     ` Matthew Brost
2026-12-03 20:23 ` [PATCH v6 06/35] drm/ttm: Add fault injection for beneficial-order allocation failures Matthew Brost
2026-12-03 20:23 ` [PATCH v6 07/35] drm/ttm/pool: Keep chunks self-aligned when resuming an interrupted restore Matthew Brost
2026-12-03 21:00   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 08/35] drm/ttm: Harvest beneficial-order pages on defragmentation moves Matthew Brost
2026-12-03 23:11   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 09/35] drm/ttm: Bound page (re)allocation per defragmentation move Matthew Brost
2026-12-03 20:23 ` [PATCH v6 10/35] drm/ttm: Preallocate beneficial-order defrag pages outside the lock Matthew Brost
2026-12-04  2:37   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 11/35] drm/ttm: Add full out-of-lock preallocation for ttm_pool_alloc() Matthew Brost
2026-12-04  3:39   ` Niranjana Vishwanathapura
2026-12-04  4:20     ` Matthew Brost
2026-12-03 20:23 ` [PATCH v6 12/35] drm/gpusvm: Add a DMA-mapping accounting callback Matthew Brost
2026-12-03 20:23 ` [PATCH v6 13/35] drm/xe: Add debugfs stats for DMA-mapped pages per order Matthew Brost
2026-12-03 20:23 ` [PATCH v6 14/35] drm/xe: Track BOs backed at a sub-optimal page order Matthew Brost
2026-12-04 20:00   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 15/35] drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies Matthew Brost
2026-12-03 20:23 ` [PATCH v6 16/35] drm/xe: Handle defrag moves in xe_bo_move() Matthew Brost
2026-12-04  4:54   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 17/35] drm/xe: Skip self-copies for borrowed pages on defrag moves Matthew Brost
2026-12-04 21:13   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 18/35] drm/xe: Add a page defragmentation worker Matthew Brost
2026-12-04 21:22   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 19/35] drm/xe: Add defrag GT stats Matthew Brost
2026-12-03 20:23 ` [PATCH v6 20/35] drm/xe: Add Kconfig.profile options for BO defrag configuration Matthew Brost
2026-12-03 20:23 ` [PATCH v6 21/35] drm/xe: Defrag using out-of-lock page preallocation Matthew Brost
2026-12-04 21:33   ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 22/35] drm/xe: Add defrag profiling tracepoints Matthew Brost
2026-12-03 20:23 ` [PATCH v6 23/35] drm/xe: Preallocate system BO backing outside the dma-resv lock Matthew Brost
2026-12-03 20:23 ` [PATCH v6 24/35] drm/xe: Add tracepoint for xe_gem_create_ioctl Matthew Brost
2026-12-03 20:23 ` [PATCH v6 25/35] drm/xe: Add IOVA-based xe_res_cursor variant Matthew Brost
2026-12-03 20:23 ` [PATCH v6 26/35] drm/xe: Don't DMA map tt pages after device unplug Matthew Brost
2026-12-04 12:21   ` Matthew Auld
2026-12-03 20:23 ` [PATCH v6 27/35] drm/xe: Lock the bo in xe_bo_dma_unmap_pinned() Matthew Brost
2026-12-03 20:23 ` [PATCH v6 28/35] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs Matthew Brost
2026-12-04 11:57   ` Matthew Auld [this message]
2026-12-03 20:23 ` [PATCH v6 29/35] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize Matthew Brost
2026-12-03 20:23 ` [PATCH v6 30/35] drm/xe: Add packed copy-step IOVA mapping for defrag Matthew Brost
2026-12-03 20:23 ` [PATCH v6 31/35] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies Matthew Brost
2026-12-04 15:39   ` Matthew Auld
2026-12-03 20:23 ` [PATCH v6 32/35] drm/xe: Finalize defrag-IOVA moves with post-copy job Matthew Brost
2026-12-03 20:23 ` [PATCH v6 33/35] drm/xe: Grow the USM batch pool to 2M and map it via a single PDE Matthew Brost
2026-12-03 20:23 ` [PATCH v6 34/35] drm/xe: Add bo_defrag debugfs entry Matthew Brost
2026-12-03 20:23 ` [PATCH v6 35/35] drm/xe: Make BO defrag tunables configurable via sysfs Matthew Brost
2026-12-03 21:02 ` ✓ CI.KUnit: success for drm/ttm, drm/xe: Avoid foreground high-order reclaim and defragment sub-optimally backed BOs (rev3) Patchwork
2026-12-03 21:46 ` ✓ Xe.CI.BAT: " Patchwork
2026-12-04 21:42 ` ✗ Xe.CI.FULL: failure " Patchwork

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=e2eb7532-e10b-4b39-b7d4-b0a8542d371e@intel.com \
    --to=matthew.auld@intel.com \
    --cc=airlied@gmail.com \
    --cc=arunpravin.paneerselvam@amd.com \
    --cc=carlos.santa@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=mripard@kernel.org \
    --cc=nat@pixelcluster.dev \
    --cc=ryanneph@google.com \
    --cc=simona@ffwll.ch \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tzimmermann@suse.de \
    /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