summaryrefslogtreecommitdiff
path: root/lib/xe/xe_ioctl.c
diff options
context:
space:
mode:
authorNishit Sharma <nishit.sharma@intel.com>2026-03-12 13:48:50 +0000
committerNishit Sharma <nishit.sharma@intel.com>2026-03-14 06:56:16 +0000
commita6ec6925dc50f5ff4e3576e3841c83da7a79fd7b (patch)
treecbb644772a5dd20fe236dabb3f00f077e6ecb691 /lib/xe/xe_ioctl.c
parenta7e8b577d5f590093bb2aeb65d73666f12941383 (diff)
lib/xe: Introduce aligned buffer mapping
Added aligned buffer mapping which is providing an interface for mapping buffer objects with a specified alignment. This API improves cross-platform compatibility and simplifies batch buffer setup by eliminating the need for platform-specific address handling. Signed-off-by: Thomas Hellstrom <thomas.hellstrom@intel.com> Signed-off-by: Nishit Sharma <nishit.sharma@intel.com> Reviewed-by: Kamil Konieczny <kamil.konieczny@linux.intel.com>
Diffstat (limited to 'lib/xe/xe_ioctl.c')
-rw-r--r--lib/xe/xe_ioctl.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/xe/xe_ioctl.c b/lib/xe/xe_ioctl.c
index 16aae05c9..ea3f2fcaa 100644
--- a/lib/xe/xe_ioctl.c
+++ b/lib/xe/xe_ioctl.c
@@ -579,6 +579,52 @@ void *xe_bo_map_fixed(int fd, uint32_t bo, size_t size, uint64_t addr)
return map;
}
+/**
+ * xe_bo_map_aligned: Maps a buffer object (bo) into CPU address space with a specified alignment
+ * @fd: The device file-descriptor
+ * @bo: The buffer object
+ * @size: The size of the map
+ * @alignment: The requested map alignment
+ *
+ * This function reserves a virtual memory range, computes the first aligned address,
+ * maps the buffer object at that address, and asserts on errors. It unmaps any unused
+ * regions before and after the mapped buffer, freeing up memory and leaving only the
+ * aligned mapping.
+ *
+ * Return: Pointer to CPU-Mapped BO with requested alignment
+ */
+void *xe_bo_map_aligned(int fd, uint32_t bo, size_t size, size_t alignment)
+{
+ size_t anon_size = size + alignment;
+ uint64_t anon_addr;
+ void *anon_map, *map;
+ uint64_t map_addr;
+ size_t hole_size;
+
+ /* Reserve a range of virtual space where we can fit an aligned map */
+ anon_map = mmap(NULL, anon_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ igt_assert(anon_map != MAP_FAILED);
+ anon_addr = to_user_pointer(anon_map);
+
+ /* Compute the first aligned address within the virtual space. */
+ map_addr = ALIGN(anon_addr, alignment);
+ /* Map the bo there, replacing part of the reserved virtual range. */
+ map = xe_bo_map_fixed(fd, bo, size, map_addr);
+ igt_assert_eq((uintptr_t)map % alignment, 0);
+
+ /* Unreserve part of the virtual range (if any) *before* the bo map */
+ hole_size = map_addr - anon_addr;
+ if (hole_size)
+ igt_assert_eq(munmap(anon_map, hole_size), 0);
+
+ /* Unreserve part of the virtual range (if any) *after* the bo map */
+ hole_size = anon_size - hole_size - size;
+ if (hole_size)
+ igt_assert_eq(munmap(map + size, hole_size), 0);
+
+ return map;
+}
+
void *xe_bo_mmap_ext(int fd, uint32_t bo, size_t size, int prot)
{
return __xe_bo_map(fd, bo, size, prot);