From 9c92d92ab9a47006a92685c49619413aafa84074 Mon Sep 17 00:00:00 2001 From: Mary Guillemard Date: Tue, 19 Mar 2024 09:33:08 +0100 Subject: [PATCH] drm-shim: Add io region handling in mmap This allows to replace mappings of io regions. Signed-off-by: Mary Guillemard Reviewed-by: Brezillon Boris Part-of: --- src/drm-shim/device.c | 15 +++++++++++++++ src/drm-shim/drm_shim.h | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/drm-shim/device.c b/src/drm-shim/device.c index 345d72aa653..9979fb4bf9e 100644 --- a/src/drm-shim/device.c +++ b/src/drm-shim/device.c @@ -413,6 +413,15 @@ drm_shim_bo_get_mmap_offset(struct shim_fd *shim_fd, struct shim_bo *bo) return bo->mem_addr; } +void +drm_shim_init_iomem_region(off64_t offset, size_t size, + void *(*mmap_handler)(size_t, int, int, off64_t)) +{ + shim_device.iomem_region.mmap = mmap_handler; + shim_device.iomem_region.start = offset; + shim_device.iomem_region.size = size; +} + /* For mmap() on the DRM fd, look up the BO from the "offset" and map the BO's * fd. */ @@ -420,6 +429,12 @@ void * drm_shim_mmap(struct shim_fd *shim_fd, size_t length, int prot, int flags, int fd, off64_t offset) { + if (shim_device.iomem_region.mmap && + offset >= shim_device.iomem_region.start && + offset + length <= shim_device.iomem_region.start + shim_device.iomem_region.size) { + return shim_device.iomem_region.mmap(length, prot, flags, offset); + } + mtx_lock(&shim_device.mem_lock); struct shim_bo *bo = _mesa_hash_table_u64_search(shim_device.offset_map, offset); mtx_unlock(&shim_device.mem_lock); diff --git a/src/drm-shim/drm_shim.h b/src/drm-shim/drm_shim.h index 7b1bd72e70f..3176943f3ea 100644 --- a/src/drm-shim/drm_shim.h +++ b/src/drm-shim/drm_shim.h @@ -44,6 +44,13 @@ struct shim_device { /* Mapping from mmap offset to shim_bo */ struct hash_table_u64 *offset_map; + /* IOMEM region */ + struct { + off64_t start; + size_t size; + void *(*mmap)(size_t length, int prot, int flags, off64_t offset); + } iomem_region; + mtx_t mem_lock; /* Heap from which shim_bo are allocated */ struct util_vma_heap mem_heap; @@ -101,6 +108,8 @@ struct shim_bo *drm_shim_bo_lookup(struct shim_fd *shim_fd, int handle); int drm_shim_bo_get_handle(struct shim_fd *shim_fd, struct shim_bo *bo); uint64_t drm_shim_bo_get_mmap_offset(struct shim_fd *shim_fd, struct shim_bo *bo); +void drm_shim_init_iomem_region(off64_t offset, size_t size, + void *(*mmap_handler)(size_t, int, int, off64_t)); /* driver-specific hooks. */ void drm_shim_driver_init(void);