[PATCH i-g-t,v3 1/5] lib/xe/xe_util: Promote xe_blt_mem_copy()

Francois Dugast francois.dugast at intel.com
Wed Mar 26 12:02:56 UTC 2025


This function abstracts copy with mem blt. Move it to xe_util so
that it can be also be used elsewhere without code duplication.

Signed-off-by: Francois Dugast <francois.dugast at intel.com>
---
 lib/xe/xe_util.c            | 55 +++++++++++++++++++++++++++++++++++++
 lib/xe/xe_util.h            |  3 ++
 tests/intel/xe_copy_basic.c | 36 +-----------------------
 3 files changed, 59 insertions(+), 35 deletions(-)

diff --git a/lib/xe/xe_util.c b/lib/xe/xe_util.c
index 06b378ce0..ba23bd7d8 100644
--- a/lib/xe/xe_util.c
+++ b/lib/xe/xe_util.c
@@ -7,6 +7,8 @@
 #include "igt_core.h"
 #include "igt_syncobj.h"
 #include "igt_sysfs.h"
+#include "intel_blt.h"
+#include "intel_mocs.h"
 #include "intel_pat.h"
 
 #include "xe/xe_ioctl.h"
@@ -275,3 +277,56 @@ uint32_t xe_nsec_to_ticks(int fd, int gt_id, uint64_t nsec)
 
 	return div64_u64_round_up(nsec * refclock, NSEC_PER_SEC);
 }
+
+/**
+ * xe_blt_mem_copy:
+ * @fd: drm fd
+ * @src_handle: handle of the source BO
+ * @dst_handle: handle of the destination BO
+ * @ctx: intel_ctx_t context
+ * @size: BO size
+ * @width: width
+ * @height: height
+ * @region: memory region
+ *
+ * Copy BO with mem blit from @src_handle into @dst_handle.
+ */
+void xe_blt_mem_copy(int fd, uint32_t src_handle, uint32_t dst_handle, const intel_ctx_t *ctx,
+		     uint32_t size, uint32_t width, uint32_t height, uint32_t region)
+{
+	struct blt_mem_data mem = {};
+	uint64_t bb_size = xe_bb_size(fd, SZ_4K);
+	uint64_t ahnd = intel_allocator_open_full(fd, ctx->vm, 0, 0,
+						  INTEL_ALLOCATOR_SIMPLE,
+						  ALLOC_STRATEGY_LOW_TO_HIGH, 0);
+	uint8_t src_mocs = intel_get_uc_mocs_index(fd);
+	uint8_t dst_mocs = src_mocs;
+	uint32_t bb;
+	int result;
+
+	bb = xe_bo_create(fd, 0, bb_size, region, 0);
+
+	blt_mem_init(fd, &mem);
+	blt_set_mem_object(&mem.src, src_handle, size, 0, width, height,
+			   region, src_mocs, DEFAULT_PAT_INDEX, M_LINEAR,
+			   COMPRESSION_DISABLED);
+	blt_set_mem_object(&mem.dst, dst_handle, size, 0, width, height,
+			   region, dst_mocs, DEFAULT_PAT_INDEX, M_LINEAR,
+			   COMPRESSION_DISABLED);
+	mem.src.ptr = xe_bo_map(fd, src_handle, size);
+	mem.dst.ptr = xe_bo_map(fd, dst_handle, size);
+
+	blt_set_batch(&mem.bb, bb, bb_size, region);
+	igt_assert(mem.src.width == mem.dst.width);
+
+	blt_mem_copy(fd, ctx, NULL, ahnd, &mem);
+	result = memcmp(mem.src.ptr, mem.dst.ptr, mem.src.size);
+
+	intel_allocator_bind(ahnd, 0, 0);
+	munmap(mem.src.ptr, size);
+	munmap(mem.dst.ptr, size);
+	gem_close(fd, bb);
+	put_ahnd(ahnd);
+
+	igt_assert_f(!result, "source and destination differ\n");
+}
diff --git a/lib/xe/xe_util.h b/lib/xe/xe_util.h
index 06ebd3c2a..550c62101 100644
--- a/lib/xe/xe_util.h
+++ b/lib/xe/xe_util.h
@@ -49,4 +49,7 @@ void xe_bind_unbind_async(int fd, uint32_t vm, uint32_t bind_engine,
 
 uint32_t xe_nsec_to_ticks(int fd, int gt_id, uint64_t ns);
 
+void xe_blt_mem_copy(int fd, uint32_t src_handle, uint32_t dst_handle, const intel_ctx_t *ctx,
+		     uint32_t size, uint32_t width, uint32_t height, uint32_t region);
+
 #endif /* XE_UTIL_H */
diff --git a/tests/intel/xe_copy_basic.c b/tests/intel/xe_copy_basic.c
index a43842e39..b7cee6f62 100644
--- a/tests/intel/xe_copy_basic.c
+++ b/tests/intel/xe_copy_basic.c
@@ -44,41 +44,7 @@ static void
 mem_copy(int fd, uint32_t src_handle, uint32_t dst_handle, const intel_ctx_t *ctx,
 	 uint32_t size, uint32_t width, uint32_t height, uint32_t region)
 {
-	struct blt_mem_data mem = {};
-	uint64_t bb_size = xe_bb_size(fd, SZ_4K);
-	uint64_t ahnd = intel_allocator_open_full(fd, ctx->vm, 0, 0,
-						  INTEL_ALLOCATOR_SIMPLE,
-						  ALLOC_STRATEGY_LOW_TO_HIGH, 0);
-	uint8_t src_mocs = intel_get_uc_mocs_index(fd);
-	uint8_t dst_mocs = src_mocs;
-	uint32_t bb;
-	int result;
-
-	bb = xe_bo_create(fd, 0, bb_size, region, 0);
-
-	blt_mem_init(fd, &mem);
-	blt_set_mem_object(&mem.src, src_handle, size, 0, width, height,
-			   region, src_mocs, DEFAULT_PAT_INDEX, M_LINEAR,
-			   COMPRESSION_DISABLED);
-	blt_set_mem_object(&mem.dst, dst_handle, size, 0, width, height,
-			   region, dst_mocs, DEFAULT_PAT_INDEX, M_LINEAR,
-			   COMPRESSION_DISABLED);
-	mem.src.ptr = xe_bo_map(fd, src_handle, size);
-	mem.dst.ptr = xe_bo_map(fd, dst_handle, size);
-
-	blt_set_batch(&mem.bb, bb, bb_size, region);
-	igt_assert(mem.src.width == mem.dst.width);
-
-	blt_mem_copy(fd, ctx, NULL, ahnd, &mem);
-	result = memcmp(mem.src.ptr, mem.dst.ptr, mem.src.size);
-
-	intel_allocator_bind(ahnd, 0, 0);
-	munmap(mem.src.ptr, size);
-	munmap(mem.dst.ptr, size);
-	gem_close(fd, bb);
-	put_ahnd(ahnd);
-
-	igt_assert_f(!result, "source and destination differ\n");
+	xe_blt_mem_copy(fd, src_handle, dst_handle, ctx, size, width, height, region);
 }
 
 /**
-- 
2.43.0



More information about the igt-dev mailing list