Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Cc: leonro@nvidia.com, francois.dugast@intel.com,
	thomas.hellstrom@linux.intel.com,
	himal.prasad.ghimiray@intel.com, jgg@ziepe.ca
Subject: [RFC PATCH v3 10/11] drm/xe: Implement DRM pagemap IOVA vfuncs
Date: Tue, 27 Jan 2026 16:48:40 -0800	[thread overview]
Message-ID: <20260128004841.2436896-11-matthew.brost@intel.com> (raw)
In-Reply-To: <20260128004841.2436896-1-matthew.brost@intel.com>

Implement the DRM pagemap IOVA vfuncs on top of the dma-map IOVA API.
Also add lockdep annotations to verify that the locking guidelines are
followed, paving the way for future implementations of the remaining
IOVA vfuncs.

Signed-off-by: Matthew Brost <matthew.brost@intel.com>
---
 drivers/gpu/drm/xe/xe_svm.c | 117 +++++++++++++++++++++++++++++++++++-
 1 file changed, 116 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/xe/xe_svm.c b/drivers/gpu/drm/xe/xe_svm.c
index c2a6bb367e0a..b46de21ed438 100644
--- a/drivers/gpu/drm/xe/xe_svm.c
+++ b/drivers/gpu/drm/xe/xe_svm.c
@@ -1660,14 +1660,125 @@ int xe_svm_alloc_vram(struct xe_svm_range *range, const struct drm_gpusvm_ctx *c
 	return err;
 }
 
+static void xe_drm_pagemap_device_iova_prove_locking(bool *locking_proved)
+{
+	struct ww_acquire_ctx ctx;
+	struct dma_resv obj;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
+		return;
+
+	if (*locking_proved)
+		return;
+
+	might_alloc(GFP_KERNEL);
+
+	dma_resv_init(&obj);
+	ww_acquire_init(&ctx, &reservation_ww_class);
+	ret = dma_resv_lock(&obj, &ctx);
+	if (ret == -EDEADLK)
+		dma_resv_lock_slow(&obj, &ctx);
+	ww_mutex_unlock(&obj.lock);
+	ww_acquire_fini(&ctx);
+
+	*locking_proved = true;
+}
+
+struct xe_svm_iova_cookie {
+	struct dma_iova_state state;
+};
+
 static void *xe_drm_pagemap_device_iova_alloc(struct drm_pagemap *dpagemap,
 					      struct device *dev, size_t length,
 					      enum dma_data_direction dir)
 {
-	/* NIY */
+	struct device *pgmap_dev = dpagemap->drm->dev;
+	struct xe_svm_iova_cookie *cookie;
+	static bool locking_proved = false;
+
+	xe_drm_pagemap_device_iova_prove_locking(&locking_proved);
+
+	if (pgmap_dev == dev)
+		return NULL;
+
+	cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
+	if (!cookie)
+		return NULL;
+
+	dma_iova_try_alloc(dev, &cookie->state, length >= SZ_2M ? SZ_2M : 0,
+			   length);
+	if (dma_use_iova(&cookie->state))
+		return cookie;
+
+	kfree(cookie);
 	return NULL;
 }
 
+static void xe_drm_pagemap_device_iova_free(struct drm_pagemap *dpagemap,
+					    struct device *dev, size_t length,
+					    void *cookie)
+{
+	struct xe_svm_iova_cookie *__cookie = cookie;
+	struct xe_device *xe = to_xe_device(dpagemap->drm);
+	static bool locking_proved = false;
+
+	xe_assert(xe, dma_use_iova(&__cookie->state));
+	xe_drm_pagemap_device_iova_prove_locking(&locking_proved);
+
+	dma_iova_free(dev, &__cookie->state);
+	kfree(cookie);
+}
+
+static struct drm_pagemap_addr
+xe_drm_pagemap_device_iova_link(struct drm_pagemap *dpagemap,
+				struct device *dev, struct page *page,
+				size_t length, size_t offset, void *cookie,
+				enum dma_data_direction dir)
+{
+	struct xe_svm_iova_cookie *__cookie = cookie;
+	struct xe_device *xe = to_xe_device(dpagemap->drm);
+	dma_addr_t addr = __cookie->state.addr + offset;
+	int err;
+
+	xe_assert(xe, dma_use_iova(&__cookie->state));
+
+	err = dma_iova_link(dev, &__cookie->state, xe_page_to_pcie(page),
+			    offset, length, dir, DMA_ATTR_SKIP_CPU_SYNC |
+			    DMA_ATTR_MMIO);
+	if (err)
+		addr = DMA_MAPPING_ERROR;
+
+	return drm_pagemap_addr_encode(addr, XE_INTERCONNECT_P2P, ilog2(length),
+				       dir);
+}
+
+static int
+xe_drm_pagemap_device_iova_sync(struct drm_pagemap *dpagemap,
+				struct device *dev, size_t length, void *cookie)
+{
+	struct xe_svm_iova_cookie *__cookie = cookie;
+	struct xe_device *xe = to_xe_device(dpagemap->drm);
+
+	xe_assert(xe, dma_use_iova(&__cookie->state));
+
+	return dma_iova_sync(dev, &__cookie->state, 0, length);
+}
+
+static void
+xe_drm_pagemap_device_iova_unlink(struct drm_pagemap *dpagemap,
+				  struct device *dev, size_t length,
+				  void *cookie, enum dma_data_direction dir)
+{
+	struct xe_svm_iova_cookie *__cookie = cookie;
+	struct xe_device *xe = to_xe_device(dpagemap->drm);
+
+	xe_assert(xe, dma_use_iova(&__cookie->state));
+
+	dma_iova_unlink(dev, &__cookie->state, 0, length, dir,
+			DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO);
+}
+
 static struct drm_pagemap_addr
 xe_drm_pagemap_device_map(struct drm_pagemap *dpagemap,
 			  struct device *dev,
@@ -1740,6 +1851,10 @@ static void xe_pagemap_destroy(struct drm_pagemap *dpagemap, bool from_atomic_or
 
 static const struct drm_pagemap_ops xe_drm_pagemap_ops = {
 	.device_iova_alloc = xe_drm_pagemap_device_iova_alloc,
+	.device_iova_free = xe_drm_pagemap_device_iova_free,
+	.device_iova_link = xe_drm_pagemap_device_iova_link,
+	.device_iova_sync = xe_drm_pagemap_device_iova_sync,
+	.device_iova_unlink = xe_drm_pagemap_device_iova_unlink,
 	.device_map = xe_drm_pagemap_device_map,
 	.device_unmap = xe_drm_pagemap_device_unmap,
 	.populate_mm = xe_drm_pagemap_populate_mm,
-- 
2.34.1


  parent reply	other threads:[~2026-01-28  0:48 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-28  0:48 [RFC PATCH v3 00/11] Use new dma-map IOVA alloc, link, and sync API in GPU SVM and DRM pagemap Matthew Brost
2026-01-28  0:48 ` [RFC PATCH v3 01/11] drm/pagemap: Add helper to access zone_device_data Matthew Brost
2026-01-28 13:53   ` Leon Romanovsky
2026-01-28  0:48 ` [RFC PATCH v3 02/11] drm/gpusvm: Use dma-map IOVA alloc, link, and sync API in GPU SVM Matthew Brost
2026-01-28 14:04   ` Leon Romanovsky
2026-01-28  0:48 ` [RFC PATCH v3 03/11] drm/pagemap: Split drm_pagemap_migrate_map_pages into device / system Matthew Brost
2026-01-28  0:48 ` [RFC PATCH v3 04/11] drm/pagemap: Use dma-map IOVA alloc, link, and sync API for DRM pagemap Matthew Brost
2026-01-28 14:28   ` Leon Romanovsky
2026-01-28 17:46     ` Matthew Brost
     [not found]       ` <20260128175531.GR1641016@ziepe.ca>
2026-01-28 19:29         ` Matthew Brost
2026-01-28 19:45           ` Leon Romanovsky
2026-01-28 21:04             ` Matthew Brost
2026-01-29 10:14               ` Leon Romanovsky
2026-01-29 18:22                 ` Matthew Brost
2026-01-28  0:48 ` [RFC PATCH v3 05/11] drm/pagemap: Reduce number of IOVA link calls Matthew Brost
2026-01-28  0:48 ` [RFC PATCH v3 06/11] drm/pagemap: Add IOVA interface to DRM pagemap Matthew Brost
     [not found]   ` <20260128151458.GJ1641016@ziepe.ca>
2026-01-28 18:42     ` Matthew Brost
2026-01-28 19:41       ` Matthew Brost
     [not found]       ` <20260128193509.GU1641016@ziepe.ca>
2026-01-28 20:24         ` Matthew Brost
2026-01-29 18:57           ` Jason Gunthorpe
2026-01-29 19:28             ` Matthew Brost
2026-01-29 19:32               ` Jason Gunthorpe
2026-01-28  0:48 ` [RFC PATCH v3 07/11] drm/xe: Stub out DRM pagemap IOVA alloc implementation Matthew Brost
2026-01-28  0:48 ` [RFC PATCH v3 08/11] drm/pagemap: Use device-to-device IOVA alloc, link, and sync API for DRM pagemap Matthew Brost
2026-01-28  0:48 ` [RFC PATCH v3 09/11] drm/xe: Drop BO dma-resv lock during SVM migrate-to-device Matthew Brost
2026-01-28  0:48 ` Matthew Brost [this message]
2026-01-28  0:48 ` [RFC PATCH v3 11/11] drm/gpusvm: Use device-to-device IOVA alloc, link, and sync API in GPU SVM Matthew Brost
2026-01-28  0:59 ` ✗ CI.checkpatch: warning for Use new dma-map IOVA alloc, link, and sync API in GPU SVM and DRM pagemap (rev3) Patchwork
2026-01-28  1:01 ` ✓ CI.KUnit: success " Patchwork
2026-01-28  1:42 ` ✓ Xe.CI.BAT: " 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=20260128004841.2436896-11-matthew.brost@intel.com \
    --to=matthew.brost@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=francois.dugast@intel.com \
    --cc=himal.prasad.ghimiray@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=jgg@ziepe.ca \
    --cc=leonro@nvidia.com \
    --cc=thomas.hellstrom@linux.intel.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