Support sparse mappings in Panthor
v7 cover Support sparse mappings in Panthor
This patch series implements sparse mappings in Panthor. Owing to the lack of HW MMU features for sparse page table entries, this had to be implemented using a dummy object over which sparse mappings requested over VM_BIND are mapped cyclically.
To that end, a new VM_BIND flag was added in the driver's uAPI.
The end goal of this patch series is to improve support of Vulkan sparse resources. At the moment, to implement this feature on Mali hardware, Vulkan sparse map is implemented by mapping the specified region to a "dummy bo" so that the accesses do not fault. A newly created sparse resource starts off unmapped, and therefore also has to be mapped to the "dummy bo". This "dummy bo" is small (a page size) in comparison to the sizes of va ranges that we might want to map to it, and a large number of vm_bind ops can be necessary. For example, if the user were to create a 100e6-byte sparse resident resource, we'd have to poke VM_BIND with ceil(100e6/0x1000)=24415 map operations.
The new VM_BIND sparse mapping feature addresses this particular inefficiency by letting us implement a single Vulkan sparse map operation and sparse resident resource initialization with just one map operation.
Link to the conversation for the previous patch series revision at:
https://lore.kernel.org/dri-devel/[email protected]/
Changes in v7:
- Switched back to Panthor BO-backed dummy object instead of raw pages so as to profit from the existing shrinker reclaim paths.
- Created Dummy BO's per file context to avoid information leaking between them.
- Reorganised some of the low-level page mapping code.
- Added commits deleting spurious white space and unused op contex field.
Changes in v6:
- Moved all the GPUVM core code into the driver backend.
- Discarded commits that touch on the gpuvm core too.
- Redesigned the uAPI so that no repeat range or user BO is supplied for sparse mappings.
- Replaced user-supplied BO with a kernel-allocated array of raw pages.
Changes in v5:
- Minor fixes to drm_gpuvm.c.
- Add panthor MMU page sizes device queriable param.
- Add helper to make sure unmaps of repeated regions are correct.
- Some fixes to Panthor's repeat mappings implementation.
- Lump arguments to panthor_vm_prepare_map_op_ctx into a single struct.
Changes in v4:
- Fixed the warnings reported by the kernel test robot.
https://lore.kernel.org/oe-kbuild-all/[email protected]/
- Fixed the warnings reported by the CI.
https://patchwork.freedesktop.org/series/151264/
No changes in v3.
Changes in v2:
- Make panthor use this stuff.
- Make it possible to express a repeated mappina of any suitably sized and aligned range of a BO, rather than strictly the page size -sized prefix, generalizing the API. Rename DRM_GPUVA_SINGLE_PAGE to DRM_GPUVA_REPEAT.
- Clean up parts of drm/gpuvm affected by these changes.
Adrián Larumbe (6):
drm/panthor: Expose GPU page sizes to UM drm/panthor: Pass vm_bind_op to vm_prepare_map_op_ctx drm/panthor: Delete spurious whitespace from uAPI header drm/panthor: Remove unused operation context field drm/panthor: Support sparse mappings drm/panthor: Bump the driver version to 1.9
drivers/gpu/drm/panthor/panthor_device.h | 3 + drivers/gpu/drm/panthor/panthor_drv.c | 10 + drivers/gpu/drm/panthor/panthor_gem.c | 35 ++++ drivers/gpu/drm/panthor/panthor_gem.h | 2 + drivers/gpu/drm/panthor/panthor_mmu.c | 230 ++++++++++++++++++----- include/uapi/drm/panthor_drm.h | 26 ++- 6 files changed, 260 insertions(+), 46 deletions(-)
--
2.53.0
PATCH v7 1/6 drm/panthor: Expose GPU page sizes to UM
In future commits that will implement repeated mappings, only repeat values multiple of GPU page sizes will be tolerated. That means these values must be made known to UM. Do it through a queriable GPU info value.
--- drivers/gpu/drm/panthor/panthor_device.h | 3 +++ drivers/gpu/drm/panthor/panthor_drv.c | 8 ++++++++ drivers/gpu/drm/panthor/panthor_mmu.c | 9 ++++++++- include/uapi/drm/panthor_drm.h | 13 +++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-)
drivers/gpu/drm/panthor/panthor_device.h +3 -0
@@ -158,6 +158,9 @@ struct panthor_device {/** @csif_info: Command stream interface information. */ struct drm_panthor_csif_info csif_info; +/** @mmu_info: MMU info */ +struct drm_panthor_mmu_info mmu_info; +/** @hw: GPU-specific data. */ struct panthor_hw *hw;
drivers/gpu/drm/panthor/panthor_drv.c +8 -0
@@ -175,6 +175,7 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride, _Generic(_obj_name, \ PANTHOR_UOBJ_DECL(struct drm_panthor_gpu_info, tiler_present), \ PANTHOR_UOBJ_DECL(struct drm_panthor_csif_info, pad), \ + PANTHOR_UOBJ_DECL(struct drm_panthor_mmu_info, page_size_bitmap), \ PANTHOR_UOBJ_DECL(struct drm_panthor_timestamp_info, current_timestamp), \ PANTHOR_UOBJ_DECL(struct drm_panthor_group_priorities_info, pad), \ PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \
@@ -946,6 +947,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d args->size =sizeof (ptdev->csif_info);return 0 ; +case DRM_PANTHOR_DEV_QUERY_MMU_INFO : + args->size =sizeof (ptdev->mmu_info); +return 0 ; +case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO : args->size =sizeof (timestamp_info);return 0 ;
@@ -966,6 +971,9 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct dcase DRM_PANTHOR_DEV_QUERY_CSIF_INFO :return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->csif_info); +case DRM_PANTHOR_DEV_QUERY_MMU_INFO : +return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->mmu_info); +case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO : ret = copy_struct_from_user(×tamp_info, sizeof(timestamp_info),
drivers/gpu/drm/panthor/panthor_mmu.c +8 -1
@@ -2769,7 +2769,7 @@ panthor_vm_create(struct panthor_device *ptdev, bool for_mcu,refcount_set (&vm->as.active_cnt,0 ); pgtbl_cfg = (struct io_pgtable_cfg ) { - .pgsize_bitmap =SZ_4K |SZ_2M , + .pgsize_bitmap = ptdev->mmu_info.page_size_bitmap, .ias = va_bits, .oas = pa_bits, .coherent_walk = ptdev->coherent,
@@ -3214,6 +3214,11 @@ static void panthor_mmu_release_wq(struct drm_device *ddev, void *res)destroy_workqueue (res); } +static void panthor_mmu_info_init (struct panthor_device *ptdev) +{ + ptdev->mmu_info.page_size_bitmap =SZ_4K |SZ_2M ; +} +/** * panthor_mmu_init() - Initialize the MMU logic. * @ptdev: Device.
@@ -3226,6 +3231,8 @@ int panthor_mmu_init(struct panthor_device *ptdev)struct panthor_mmu *mmu;int ret, irq; +panthor_mmu_info_init (ptdev); + mmu =drmm_kzalloc (&ptdev->base,sizeof (*mmu),GFP_KERNEL );if (!mmu)return -ENOMEM ;
include/uapi/drm/panthor_drm.h +13 -0
@@ -246,6 +246,9 @@ enum drm_panthor_dev_query_type {/** @DRM_PANTHOR_DEV_QUERY_CSIF_INFO: Query command-stream interface information. */ DRM_PANTHOR_DEV_QUERY_CSIF_INFO , +/** @DRM_PANTHOR_DEV_QUERY_MMU_INFO: Query MMU information. */ +DRM_PANTHOR_DEV_QUERY_MMU_INFO , +/** @DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO: Query timestamp information. */ DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO,
@@ -487,6 +490,16 @@ struct drm_panthor_timestamp_info {__u64 cpu_timestamp_nsec; }; +/** +* struct drm_panthor_mmu_info - MMU information +* +* Structure grouping all queryable information relating to the MMU. +*/ +struct drm_panthor_mmu_info { +/** @page_size_bitmap: Allowed page sizes */ +__u64 page_size_bitmap; +}; +/** * struct drm_panthor_group_priorities_info - Group priorities information *
Signature · 1 line
2.53.0
Re: [PATCH v7 1/6] drm/panthor: Expose GPU page sizes to UM
Quoted 6 lines · On Wed, 15 Apr 2026 12:28:45 +0100 Adrián Larumbe…
In future commits that will implement repeated mappings, only repeat values multiple of GPU page sizes will be tolerated. That means these values must be made known to UM. Do it through a queriable GPU info value.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 103 lines
--- drivers/gpu/drm/panthor/panthor_device.h | 3 +++ drivers/gpu/drm/panthor/panthor_drv.c | 8 ++++++++ drivers/gpu/drm/panthor/panthor_mmu.c | 9 ++++++++- include/uapi/drm/panthor_drm.h | 13 +++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-)drivers/gpu/drm/panthor/panthor_device.h +3
-0@@ -158,6 +158,9 @@ struct panthor_device {/** @csif_info: Command stream interface information. */ struct drm_panthor_csif_info csif_info; +/** @mmu_info: MMU info */ +struct drm_panthor_mmu_info mmu_info; +/** @hw: GPU-specific data. */ struct panthor_hw *hw;drivers/gpu/drm/panthor/panthor_drv.c +8
-0@@ -175,6 +175,7 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride, _Generic(_obj_name, \ PANTHOR_UOBJ_DECL(struct drm_panthor_gpu_info, tiler_present), \ PANTHOR_UOBJ_DECL(struct drm_panthor_csif_info, pad), \ + PANTHOR_UOBJ_DECL(struct drm_panthor_mmu_info, page_size_bitmap), \ PANTHOR_UOBJ_DECL(struct drm_panthor_timestamp_info, current_timestamp), \ PANTHOR_UOBJ_DECL(struct drm_panthor_group_priorities_info, pad), \ PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \@@ -946,6 +947,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d args->size =sizeof (ptdev->csif_info);return 0 ; +case DRM_PANTHOR_DEV_QUERY_MMU_INFO : + args->size =sizeof (ptdev->mmu_info); +return 0 ; +case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO : args->size =sizeof (timestamp_info);return 0 ;@@ -966,6 +971,9 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct dcase DRM_PANTHOR_DEV_QUERY_CSIF_INFO :return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->csif_info); +case DRM_PANTHOR_DEV_QUERY_MMU_INFO : +return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->mmu_info); +case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO : ret = copy_struct_from_user(×tamp_info, sizeof(timestamp_info),drivers/gpu/drm/panthor/panthor_mmu.c +8
-1@@ -2769,7 +2769,7 @@ panthor_vm_create(struct panthor_device *ptdev, bool for_mcu,refcount_set (&vm->as.active_cnt,0 ); pgtbl_cfg = (struct io_pgtable_cfg ) { - .pgsize_bitmap =SZ_4K |SZ_2M , + .pgsize_bitmap = ptdev->mmu_info.page_size_bitmap, .ias = va_bits, .oas = pa_bits, .coherent_walk = ptdev->coherent,@@ -3214,6 +3214,11 @@ static void panthor_mmu_release_wq(struct drm_device *ddev, void *res)destroy_workqueue (res); } +static void panthor_mmu_info_init (struct panthor_device *ptdev) +{ + ptdev->mmu_info.page_size_bitmap =SZ_4K |SZ_2M ; +} +/** * panthor_mmu_init() - Initialize the MMU logic. * @ptdev: Device. @@ -3226,6 +3231,8 @@ int panthor_mmu_init(struct panthor_device *ptdev)struct panthor_mmu *mmu;int ret, irq; +panthor_mmu_info_init (ptdev); + mmu =drmm_kzalloc (&ptdev->base,sizeof (*mmu),GFP_KERNEL );if (!mmu)return -ENOMEM ;include/uapi/drm/panthor_drm.h +3
-0@@ -246,6 +246,9 @@ enum drm_panthor_dev_query_type {/** @DRM_PANTHOR_DEV_QUERY_CSIF_INFO: Query command-stream interface information. */ DRM_PANTHOR_DEV_QUERY_CSIF_INFO , +/** @DRM_PANTHOR_DEV_QUERY_MMU_INFO: Query MMU information. */ +DRM_PANTHOR_DEV_QUERY_MMU_INFO , +/** @DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO: Query timestamp information. */ DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO,
include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 499
Quoted 10 lines
include/uapi/drm/panthor_drm.h +7
-0@@ -487,6 +490,16 @@ struct drm_panthor_timestamp_info {__u64 cpu_timestamp_nsec; }; +/** +* struct drm_panthor_mmu_info - MMU information +* +* Structure grouping all queryable information relating to the MMU. +*/ +struct drm_panthor_mmu_info { +/** @page_size_bitmap: Allowed page sizes */
Maybe add more info there: like, page size is a power of two, and each bit encodes a particular page size.
Quoted 6 lines
include/uapi/drm/panthor_drm.h +3
-0+__u64 page_size_bitmap; +}; +/** * struct drm_panthor_group_priorities_info - Group priorities information *
Re: [PATCH v7 1/6] drm/panthor: Expose GPU page sizes to UM
In reply to Adrián LarumbeQuoted 6 lines · Adrián Larumbe
In future commits that will implement repeated mappings, only repeat values multiple of GPU page sizes will be tolerated. That means these values must be made known to UM. Do it through a queriable GPU info value.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 119 lines
--- drivers/gpu/drm/panthor/panthor_device.h | 3 +++ drivers/gpu/drm/panthor/panthor_drv.c | 8 ++++++++ drivers/gpu/drm/panthor/panthor_mmu.c | 9 ++++++++- include/uapi/drm/panthor_drm.h | 13 +++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-)drivers/gpu/drm/panthor/panthor_device.h +3
-0@@ -158,6 +158,9 @@ struct panthor_device {/** @csif_info: Command stream interface information. */ struct drm_panthor_csif_info csif_info; +/** @mmu_info: MMU info */ +struct drm_panthor_mmu_info mmu_info; +/** @hw: GPU-specific data. */ struct panthor_hw *hw;drivers/gpu/drm/panthor/panthor_drv.c +8
-0@@ -175,6 +175,7 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride, _Generic(_obj_name, \ PANTHOR_UOBJ_DECL(struct drm_panthor_gpu_info, tiler_present), \ PANTHOR_UOBJ_DECL(struct drm_panthor_csif_info, pad), \ + PANTHOR_UOBJ_DECL(struct drm_panthor_mmu_info, page_size_bitmap), \ PANTHOR_UOBJ_DECL(struct drm_panthor_timestamp_info, current_timestamp), \ PANTHOR_UOBJ_DECL(struct drm_panthor_group_priorities_info, pad), \ PANTHOR_UOBJ_DECL(struct drm_panthor_sync_op, timeline_value), \@@ -946,6 +947,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d args->size =sizeof (ptdev->csif_info);return 0 ; +case DRM_PANTHOR_DEV_QUERY_MMU_INFO : + args->size =sizeof (ptdev->mmu_info); +return 0 ; +case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO : args->size =sizeof (timestamp_info);return 0 ;@@ -966,6 +971,9 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct dcase DRM_PANTHOR_DEV_QUERY_CSIF_INFO :return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->csif_info); +case DRM_PANTHOR_DEV_QUERY_MMU_INFO : +return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->mmu_info); +case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO : ret = copy_struct_from_user(×tamp_info, sizeof(timestamp_info),drivers/gpu/drm/panthor/panthor_mmu.c +8
-1@@ -2769,7 +2769,7 @@ panthor_vm_create(struct panthor_device *ptdev, bool for_mcu,refcount_set (&vm->as.active_cnt,0 ); pgtbl_cfg = (struct io_pgtable_cfg ) { - .pgsize_bitmap =SZ_4K |SZ_2M , + .pgsize_bitmap = ptdev->mmu_info.page_size_bitmap, .ias = va_bits, .oas = pa_bits, .coherent_walk = ptdev->coherent,@@ -3214,6 +3214,11 @@ static void panthor_mmu_release_wq(struct drm_device *ddev, void *res)destroy_workqueue (res); } +static void panthor_mmu_info_init (struct panthor_device *ptdev) +{ + ptdev->mmu_info.page_size_bitmap =SZ_4K |SZ_2M ; +} +/** * panthor_mmu_init() - Initialize the MMU logic. * @ptdev: Device. @@ -3226,6 +3231,8 @@ int panthor_mmu_init(struct panthor_device *ptdev)struct panthor_mmu *mmu;int ret, irq; +panthor_mmu_info_init (ptdev); + mmu =drmm_kzalloc (&ptdev->base,sizeof (*mmu),GFP_KERNEL );if (!mmu)return -ENOMEM ;include/uapi/drm/panthor_drm.h +13
-0@@ -246,6 +246,9 @@ enum drm_panthor_dev_query_type {/** @DRM_PANTHOR_DEV_QUERY_CSIF_INFO: Query command-stream interface information. */ DRM_PANTHOR_DEV_QUERY_CSIF_INFO , +/** @DRM_PANTHOR_DEV_QUERY_MMU_INFO: Query MMU information. */ +DRM_PANTHOR_DEV_QUERY_MMU_INFO , +/** @DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO: Query timestamp information. */ DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO,@@ -487,6 +490,16 @@ struct drm_panthor_timestamp_info {__u64 cpu_timestamp_nsec; }; +/** +* struct drm_panthor_mmu_info - MMU information +* +* Structure grouping all queryable information relating to the MMU. +*/ +struct drm_panthor_mmu_info { +/** @page_size_bitmap: Allowed page sizes */ +__u64 page_size_bitmap; +}; +/** * struct drm_panthor_group_priorities_info - Group priorities information *
PATCH v7 2/6 drm/panthor: Pass vm_bind_op to vm_prepare_map_op_ctx
In reply to Adrián LarumbeInstead of passing its constituent elements, pass the whole struct to simplify the function prototype.
--- drivers/gpu/drm/panthor/panthor_mmu.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/gpu/drm/panthor/panthor_mmu.c +14 -13
@@ -1275,9 +1275,7 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)static int panthor_vm_prepare_map_op_ctx (struct panthor_vm_op_ctx *op_ctx,struct panthor_vm *vm,struct panthor_gem_object *bo, -u64 offset, -u64 size,u64 va, -u32 flags) +const struct drm_panthor_vm_bind_op *op) {struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;
@@ -1286,12 +1284,12 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx,if (!bo)return -EINVAL ; -if ((flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || - (flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP ) +if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || + (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;/* Make sure the VA and size are in-bounds. */ -if (size > bo->base.size || offset > bo->base.size - size) +if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size)return -EINVAL ;/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */
@@ -1299,7 +1297,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, bo->exclusive_vm_root_gem != panthor_vm_root_gem(vm))return -EINVAL ; -panthor_vm_init_op_ctx (op_ctx, size, va, flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)
@@ -1328,7 +1326,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, } op_ctx->map.vm_bo =drm_gpuvm_bo_obtain_prealloc (preallocated_vm_bo); - op_ctx->map.bo_offset = offset; + op_ctx->map.bo_offset = op->bo_offset; ret =panthor_vm_op_ctx_prealloc_pts (op_ctx);if (ret)
@@ -2849,10 +2847,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file, gem =drm_gem_object_lookup (file, op->bo_handle); ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, gem ?to_panthor_bo (gem) :NULL , - op->bo_offset, - op->size, - op->va, - op->flags); + op);drm_gem_object_put (gem);return ret;
@@ -3048,10 +3043,16 @@ int panthor_vm_bind_exec_sync_op(struct drm_file *file,int panthor_vm_map_bo_range (struct panthor_vm *vm,struct panthor_gem_object *bo,u64 offset,u64 size,u64 va,u32 flags) { +struct drm_panthor_vm_bind_op op = { + .bo_offset = offset, + .size = size, + .va = va, + .flags = flags, + };struct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, offset, size, va, flags); + ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op);if (ret)return ret;
Signature · 1 line
2.53.0
Re: [PATCH v7 2/6] drm/panthor: Pass vm_bind_op to vm_prepare_map_op_ctx
Quoted 4 lines · On Wed, 15 Apr 2026 12:28:46 +0100 Adrián Larumbe…
Instead of passing its constituent elements, pass the whole struct to simplify the function prototype.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 66 lines
--- drivers/gpu/drm/panthor/panthor_mmu.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/gpu/drm/panthor/panthor_mmu.c +7
-12@@ -1275,9 +1275,7 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)static int panthor_vm_prepare_map_op_ctx (struct panthor_vm_op_ctx *op_ctx,struct panthor_vm *vm,struct panthor_gem_object *bo, -u64 offset, -u64 size,u64 va, -u32 flags) +const struct drm_panthor_vm_bind_op *op) {struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;@@ -1286,12 +1284,12 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx,if (!bo)return -EINVAL ; -if ((flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || - (flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP ) +if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || + (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;/* Make sure the VA and size are in-bounds. */ -if (size > bo->base.size || offset > bo->base.size - size) +if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size)return -EINVAL ;/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ @@ -1299,7 +1297,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, bo->exclusive_vm_root_gem != panthor_vm_root_gem(vm))return -EINVAL ; -panthor_vm_init_op_ctx (op_ctx, size, va, flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)@@ -1328,7 +1326,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, } op_ctx->map.vm_bo =drm_gpuvm_bo_obtain_prealloc (preallocated_vm_bo); - op_ctx->map.bo_offset = offset; + op_ctx->map.bo_offset = op->bo_offset; ret =panthor_vm_op_ctx_prealloc_pts (op_ctx);if (ret)@@ -2849,10 +2847,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file, gem =drm_gem_object_lookup (file, op->bo_handle); ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, gem ?to_panthor_bo (gem) :NULL , - op->bo_offset, - op->size, - op->va, - op->flags); + op);drm_gem_object_put (gem);return ret;
drivers/gpu/drm/panthor/panthor_mmu.c int panthor_vm_bind_exec_sync_op(struct drm_file *file, · line 3046
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-0@@ -3048,10 +3043,16 @@ int panthor_vm_bind_exec_sync_op(struct drm_file *file,int panthor_vm_map_bo_range (struct panthor_vm *vm,struct panthor_gem_object *bo,u64 offset,u64 size,u64 va,u32 flags) { +struct drm_panthor_vm_bind_op op = {
You could even make it const here, but I think you're adjusting some fields after the initialization in one of the following patches.
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-1+ .bo_offset = offset, + .size = size, + .va = va, + .flags = flags, + };struct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, offset, size, va, flags); + ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op);if (ret)return ret;
Re: [PATCH v7 2/6] drm/panthor: Pass vm_bind_op to vm_prepare_map_op_ctx
In reply to Adrián LarumbeQuoted 4 lines · Adrián Larumbe
Instead of passing its constituent elements, pass the whole struct to simplify the function prototype.
Signed-off-by Adrián Larumbe <[email protected]>
It's a little ugly the hack in panthor_vm_map_bo_range(), but I can't immediately see a neater solution, so:
Quoted 83 lines
--- drivers/gpu/drm/panthor/panthor_mmu.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/gpu/drm/panthor/panthor_mmu.c +14
-13@@ -1275,9 +1275,7 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)static int panthor_vm_prepare_map_op_ctx (struct panthor_vm_op_ctx *op_ctx,struct panthor_vm *vm,struct panthor_gem_object *bo, -u64 offset, -u64 size,u64 va, -u32 flags) +const struct drm_panthor_vm_bind_op *op) {struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;@@ -1286,12 +1284,12 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx,if (!bo)return -EINVAL ; -if ((flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || - (flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP ) +if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || + (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;/* Make sure the VA and size are in-bounds. */ -if (size > bo->base.size || offset > bo->base.size - size) +if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size)return -EINVAL ;/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ @@ -1299,7 +1297,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, bo->exclusive_vm_root_gem != panthor_vm_root_gem(vm))return -EINVAL ; -panthor_vm_init_op_ctx (op_ctx, size, va, flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)@@ -1328,7 +1326,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, } op_ctx->map.vm_bo =drm_gpuvm_bo_obtain_prealloc (preallocated_vm_bo); - op_ctx->map.bo_offset = offset; + op_ctx->map.bo_offset = op->bo_offset; ret =panthor_vm_op_ctx_prealloc_pts (op_ctx);if (ret)@@ -2849,10 +2847,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file, gem =drm_gem_object_lookup (file, op->bo_handle); ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, gem ?to_panthor_bo (gem) :NULL , - op->bo_offset, - op->size, - op->va, - op->flags); + op);drm_gem_object_put (gem);return ret;@@ -3048,10 +3043,16 @@ int panthor_vm_bind_exec_sync_op(struct drm_file *file,int panthor_vm_map_bo_range (struct panthor_vm *vm,struct panthor_gem_object *bo,u64 offset,u64 size,u64 va,u32 flags) { +struct drm_panthor_vm_bind_op op = { + .bo_offset = offset, + .size = size, + .va = va, + .flags = flags, + };struct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, offset, size, va, flags); + ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op);if (ret)return ret;
PATCH v7 3/6 drm/panthor: Delete spurious whitespace from uAPI header
In reply to Adrián LarumbeThere's no extra blank line after the last member of any other uAPI structures, so delete it.
--- include/uapi/drm/panthor_drm.h | 1 - 1 file changed, 1 deletion(-)
include/uapi/drm/panthor_drm.h +0 -1
@@ -677,7 +677,6 @@ struct drm_panthor_vm_bind_op {* This array shall not be empty for sync-only operations. */ struct drm_panthor_obj_array syncs; - };/**
Signature · 1 line
2.53.0
Re: [PATCH v7 3/6] drm/panthor: Delete spurious whitespace from uAPI header
Quoted 4 lines · On Wed, 15 Apr 2026 12:28:47 +0100 Adrián Larumbe…
There's no extra blank line after the last member of any other uAPI structures, so delete it.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 16 lines
--- include/uapi/drm/panthor_drm.h | 1 - 1 file changed, 1 deletion(-)include/uapi/drm/panthor_drm.h +0
-1@@ -677,7 +677,6 @@ struct drm_panthor_vm_bind_op {* This array shall not be empty for sync-only operations. */ struct drm_panthor_obj_array syncs; - };/**
Re: [PATCH v7 3/6] drm/panthor: Delete spurious whitespace from uAPI header
In reply to Adrián LarumbeQuoted 4 lines · Adrián Larumbe
There's no extra blank line after the last member of any other uAPI structures, so delete it.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 16 lines
--- include/uapi/drm/panthor_drm.h | 1 - 1 file changed, 1 deletion(-)include/uapi/drm/panthor_drm.h +0
-1@@ -677,7 +677,6 @@ struct drm_panthor_vm_bind_op {* This array shall not be empty for sync-only operations. */ struct drm_panthor_obj_array syncs; - };/**
PATCH v7 4/6 drm/panthor: Remove unused operation context field
In reply to Adrián LarumbeA Panthor BO's sgtable is now retrieved from its dmap field.
--- drivers/gpu/drm/panthor/panthor_mmu.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/gpu/drm/panthor/panthor_mmu.c +0 -8
@@ -195,14 +195,6 @@ struct panthor_vm_op_ctx {/** @map.bo_offset: Offset in the buffer object. */ u64 bo_offset; -/** -* @map.sgt: sg-table pointing to pages backing the GEM object. -* -* This is gathered at job creation time, such that we don't have -* to allocate in ::run_job(). -*/ -struct sg_table *sgt; -/** @map.bo: the BO being mapped. */ struct panthor_gem_object *bo; } map;
Signature · 1 line
2.53.0
Re: [PATCH v7 4/6] drm/panthor: Remove unused operation context field
Quoted 3 lines · On Wed, 15 Apr 2026 12:28:48 +0100 Adrián Larumbe…
A Panthor BO's sgtable is now retrieved from its dmap field.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 23 lines
--- drivers/gpu/drm/panthor/panthor_mmu.c | 8 -------- 1 file changed, 8 deletions(-)drivers/gpu/drm/panthor/panthor_mmu.c +0
-8@@ -195,14 +195,6 @@ struct panthor_vm_op_ctx {/** @map.bo_offset: Offset in the buffer object. */ u64 bo_offset; -/** -* @map.sgt: sg-table pointing to pages backing the GEM object. -* -* This is gathered at job creation time, such that we don't have -* to allocate in ::run_job(). -*/ -struct sg_table *sgt; -/** @map.bo: the BO being mapped. */ struct panthor_gem_object *bo; } map;
Re: [PATCH v7 4/6] drm/panthor: Remove unused operation context field
In reply to Adrián LarumbeQuoted 3 lines · Adrián Larumbe
A Panthor BO's sgtable is now retrieved from its dmap field.
Signed-off-by Adrián Larumbe <[email protected]>
Quoted 23 lines
--- drivers/gpu/drm/panthor/panthor_mmu.c | 8 -------- 1 file changed, 8 deletions(-)drivers/gpu/drm/panthor/panthor_mmu.c +0
-8@@ -195,14 +195,6 @@ struct panthor_vm_op_ctx {/** @map.bo_offset: Offset in the buffer object. */ u64 bo_offset; -/** -* @map.sgt: sg-table pointing to pages backing the GEM object. -* -* This is gathered at job creation time, such that we don't have -* to allocate in ::run_job(). -*/ -struct sg_table *sgt; -/** @map.bo: the BO being mapped. */ struct panthor_gem_object *bo; } map;
PATCH v7 5/6 drm/panthor: Support sparse mappings
In reply to Adrián LarumbeAllow UM to bind sparsely populated memory regions by cyclically mapping virtual ranges over a kernel-allocated dummy BO. This alternative is preferable to the old method of handling sparseness in the UMD, because it relied on the creation of a buffer object to the same end, despite the fact Vulkan sparse resources don't need to be backed by a driver BO.
The choice of backing sparsely-bound regions with a Panhtor BO was made so as to profit from the existing shrinker reclaim code. That way no special treatment must be given to the dummy sparse BOs when reclaiming memory, as would be the case if we had chosen a raw kernel page implementation.
A new dummy BO is allocated per open file context, because even though the Vulkan spec mandates that writes into sparsely bound regions must be discarded, our implementation is still a workaround over the fact Mali CSF GPUs cannot support this behaviour on the hardware level, so writes still make it into the backing BO. If we had a global one, then it could be a venue for information leaks between file contexts, which should never happen in DRM.
--- drivers/gpu/drm/panthor/panthor_gem.c | 35 +++++ drivers/gpu/drm/panthor/panthor_gem.h | 2 + drivers/gpu/drm/panthor/panthor_mmu.c | 192 ++++++++++++++++++++++---- include/uapi/drm/panthor_drm.h | 12 ++ 4 files changed, 215 insertions(+), 26 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +35 -0
@@ -1345,6 +1345,41 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,return ERR_PTR (ret); } +/** +* panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. +* @ptdev: Device. +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_gem_object * +panthor_dummy_bo_create (struct panthor_device *ptdev) +{ +u32 dummy_flags =DRM_PANTHOR_BO_NO_MMAP ; +struct panthor_gem_object *bo; +struct page **pages; + + bo =panthor_gem_create (&ptdev->base,SZ_2M , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + + pages =drm_gem_get_pages (&bo->base); +if (PTR_ERR (pages) == -ENOMEM ) { +drm_gem_object_put (&bo->base); + bo =panthor_gem_create (&ptdev->base,SZ_4K , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + pages =drm_gem_get_pages (&bo->base); + } + +if (IS_ERR_OR_NULL (pages)) { +drm_gem_object_put (&bo->base); +return ERR_CAST (pages); + } + + bo->backing.pages = pages; +return bo; +} +static bool can_swap (void ) {return get_nr_swap_pages () >0 ;
drivers/gpu/drm/panthor/panthor_gem.h +2 -0
@@ -264,6 +264,8 @@ void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *lint panthor_gem_sync (struct drm_gem_object *obj,u32 type,u64 offset,u64 size); +struct panthor_gem_object *panthor_dummy_bo_create (struct panthor_device *ptdev); +struct drm_gem_object *panthor_gem_prime_import (struct drm_device *dev,struct dma_buf *dma_buf);
drivers/gpu/drm/panthor/panthor_mmu.c +166 -26
337 lines not on this page · raw message
include/uapi/drm/panthor_drm.h +12 -0
@@ -614,6 +614,18 @@ enum drm_panthor_vm_bind_op_flags {*/ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED =1 <<2 , +/** +* @DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE: Repeat a BO range +* +* Only valid with DRM_PANTHOR_VM_BIND_OP_TYPE_MAP. +* +* When this flag is set, the whole vm_bind range is mapped over a dummy object in a cyclic +* fashion, and all GPU reads from addresses in the range return undefined values. This flag +* being set means drm_panthor_vm_bind_op:offset and drm_panthor_vm_bind_op::handle must +* both be set to 0. +*/ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE = 1 << 3, +/** * @DRM_PANTHOR_VM_BIND_OP_TYPE_MASK: Mask used to determine the type of operation. */
Signature · 1 line
2.53.0
Boris Brezillon
maintainerRe: [PATCH v7 5/6] drm/panthor: Support sparse mappings
Quoted 43 lines · On Wed, 15 Apr 2026 12:28:49 +0100 Adrián Larumbe…
Allow UM to bind sparsely populated memory regions by cyclically mapping virtual ranges over a kernel-allocated dummy BO. This alternative is preferable to the old method of handling sparseness in the UMD, because it relied on the creation of a buffer object to the same end, despite the fact Vulkan sparse resources don't need to be backed by a driver BO.
The choice of backing sparsely-bound regions with a Panhtor BO was made so as to profit from the existing shrinker reclaim code. That way no special treatment must be given to the dummy sparse BOs when reclaiming memory, as would be the case if we had chosen a raw kernel page implementation.
A new dummy BO is allocated per open file context, because even though the Vulkan spec mandates that writes into sparsely bound regions must be discarded, our implementation is still a workaround over the fact Mali CSF GPUs cannot support this behaviour on the hardware level, so writes still make it into the backing BO. If we had a global one, then it could be a venue for information leaks between file contexts, which should never happen in DRM.
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_gem.c | 35 +++++ drivers/gpu/drm/panthor/panthor_gem.h | 2 + drivers/gpu/drm/panthor/panthor_mmu.c | 192 ++++++++++++++++++++++---- include/uapi/drm/panthor_drm.h | 12 ++ 4 files changed, 215 insertions(+), 26 deletions(-)drivers/gpu/drm/panthor/panthor_gem.c +8
-0@@ -1345,6 +1345,41 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,return ERR_PTR (ret); } +/** +* panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. +* @ptdev: Device. +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_gem_object * +panthor_dummy_bo_create (struct panthor_device *ptdev)
drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 1365
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gem.c +10
-0+{ +u32 dummy_flags =DRM_PANTHOR_BO_NO_MMAP ; +struct panthor_gem_object *bo; +struct page **pages; + + bo =panthor_gem_create (&ptdev->base,SZ_2M , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + + pages =drm_gem_get_pages (&bo->base);
Why not use panthor_gem_backing_get_pages_locked() here? Also, drm_gem_get_pages() doesn't give any guarantee that you'll get a huge page, nor can you guarantee that the 2M won't be reclaimed and later on be re-allocated as 4k chunks. I'd probably keep things simple for now, and
- keep it a 2M GEM object
- force the page allocation at map time, just like we do for regular BOs
Quoted 38 lines
drivers/gpu/drm/panthor/panthor_gem.c +17
-0+if (PTR_ERR (pages) == -ENOMEM ) { +drm_gem_object_put (&bo->base); + bo =panthor_gem_create (&ptdev->base,SZ_4K , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + pages =drm_gem_get_pages (&bo->base); + } + +if (IS_ERR_OR_NULL (pages)) { +drm_gem_object_put (&bo->base); +return ERR_CAST (pages); + } + + bo->backing.pages = pages; +return bo; +} +static bool can_swap (void ) {return get_nr_swap_pages () >0 ;drivers/gpu/drm/panthor/panthor_gem.h +2
-0@@ -264,6 +264,8 @@ void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *lint panthor_gem_sync (struct drm_gem_object *obj,u32 type,u64 offset,u64 size); +struct panthor_gem_object *panthor_dummy_bo_create (struct panthor_device *ptdev); +struct drm_gem_object *panthor_gem_prime_import (struct drm_device *dev,struct dma_buf *dma_buf);drivers/gpu/drm/panthor/panthor_mmu.c +0
-0
drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_mmu { · line 116
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -112,6 +112,23 @@ struct panthor_mmu {struct panthor_vm_pool {/** @xa: Array used for VM handle tracking. */ struct xarray xa; + +/** @dummy: Dummy drm object related fields
/** * @dummy: Dummy drm object related fields.
Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +15
-0+* +* Sparse bindings map virtual address ranges onto a dummy +* BO in a modulo fashion. Even though sparse writes are meant +* to be discarded and reads undefined, writes are still reflected +* in the dummy buffer. That means we must keep a dummy object per +* file context, to avoid data leaks between them. +* +*/ +struct { +/** @dummy.obj: Dummy object used for sparse mappings. */ +struct panthor_gem_object *obj; + +/** @dummy.lock: Lock protecting against races on dummy object. */ +struct mutex lock; + } dummy; };/**
drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_vm { · line 412
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -391,6 +408,15 @@ struct panthor_vm {*/ struct list_head lru_node; } reclaim; + +/** @dummy: Dummy object used for sparse mappings.
/** * @dummy: Dummy object used for sparse mappings.
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-0+* +* VM's must keep a reference to the file context-wide dummy BO because +* they can outlive the file context, which includes the VM pool holding +* the original dummy BO reference. +* +*/ +struct panthor_gem_object *dummy; };/**
drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1053
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0@@ -1020,6 +1046,46 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,return 0 ; } +static int +panthor_vm_map_sparse (struct panthor_vm *vm,u64 iova,int prot, +struct sg_table *sgt,u64 size) +{ +u64 first_iova = iova;
s/first_iova/orig_iova/
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_mmu.c +22
-0+u64 first_size = size; +int ret; + +if (iova & (SZ_2M -1 )) { +u64 unaligned_size =min (ALIGN (iova,SZ_2M ) - iova, size); + + ret =panthor_vm_map_pages (vm, iova, prot, sgt, +0 , unaligned_size); +if (ret) +return ret; + + size -= unaligned_size; + iova += unaligned_size; + } + +/* TODO: we should probably optimize this at the io_pgtable level. */ +while (size >0 ) { +u64 next_size =min (size,sg_dma_len (sgt->sgl)); + + ret =panthor_vm_map_pages (vm, iova, prot, + sgt,0 , next_size); +if (ret)
drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1085
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ goto err_unmap; + + size -= next_size; + iova += next_size; + } + +return 0 ; + +err_unmap: +panthor_vm_unmap_pages (vm, first_iova, first_size - size);
If you do:
panthor_vm_unmap_pages(vm, orig_iova, iova - orig_iova);
you can get rid of the first_size variable.
Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +12
-0+return ret; +} +static int flags_to_prot (u32 flags) {int prot =0 ;@@ -1258,38 +1324,71 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)return 0 ; } +static struct panthor_gem_object * +panthor_vm_get_dummy_obj (struct panthor_vm_pool *pool, +struct panthor_vm *vm) +{ + scoped_guard(mutex, &pool->dummy.lock) { +if (!vm->dummy) { +if (!pool->dummy.obj) { +struct panthor_gem_object *obj = +panthor_dummy_bo_create (vm->ptdev);
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1345
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+if (IS_ERR (obj)) +return obj; + + pool->dummy.obj = obj; + } + +drm_gem_object_get (&pool->dummy.obj->base); + vm->dummy = pool->dummy.obj; + } + }
The lock is taken for the whole function scope, you you can simply use guard(mutex)() and get rid of two indentation levels:
guard(mutex)(&pool->dummy.lock);
if (vm->dummy)
return vm->dummy;
if (!pool->dummy.obj) {
struct panthor_gem_object *obj;
obj = panthor_dummy_bo_create(vm->ptdev);
if (IS_ERR(obj))
return obj;
pool->dummy.obj = obj;
}
drm_gem_object_get(&pool->dummy.obj->base);
vm->dummy = pool->dummy.obj;
return vm->dummy;drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1358
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0+ +return vm->dummy; +} +#define PANTHOR_VM_BIND_OP_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE | \ DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, + struct panthor_vm_pool *pool,
Can't we just make sure vm->dummy is allocated before panthor_vm_prepare_map_op_ctx() is called in case this is a sparse map request? This would prevent the conditional check on pool != NULL, but only when is_sparse=true, and you wouldn't have to pass the pool around.
Quoted 16 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-3struct panthor_vm *vm, struct panthor_gem_object *bo, const struct drm_panthor_vm_bind_op *op) { + bool is_sparse = op->flags & DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE;struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;int ret; -if (!bo) -return -EINVAL ; -if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1380
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-1/* Make sure the VA and size are in-bounds. */ -if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size) +if (bo && (is_sparse || op->size > bo->base.size || + op->bo_offset > bo->base.size - op->size))return -EINVAL ; +else if (is_sparse && (!pool || op->bo_handle || op->bo_offset)) +return -EINVAL ; + +if (is_sparse) { + bo =panthor_vm_get_dummy_obj (pool, vm);
Actually, you assign bo here, so you might as well just pass the dummy BO to panthor_vm_prepare_map_op_ctx() and keep the
if (!bo) return -EINVAL;
check.
As a side note, if gpuva.gem.obj != NULL for sparse mappings, it messes up with the can_merge checks done by gpuvm, which is not a problem right now because we simply ignore the .keep hint passed to unmap_op, but that's probably worth a comment somewhere.
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1391
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-1+if (IS_ERR_OR_NULL (bo)) +return PTR_ERR (bo); + }/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ if (bo->exclusive_vm_root_gem && bo->exclusive_vm_root_gem !=panthor_vm_root_gem (vm))return -EINVAL ; -panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags + | ((is_sparse) ?DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC :0 ));
I would actually enforce NOEXEC is set and return EINVAL if that's not the case.
drivers/gpu/drm/panthor/panthor_mmu.c void panthor_vm_pool_destroy(struct panthor_file *pfile) · line 1741
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)@@ -1634,6 +1733,13 @@ void panthor_vm_pool_destroy(struct panthor_file *pfile)xa_for_each (&pfile->vms->xa, i, vm)panthor_vm_destroy (vm); + scoped_guard(mutex, &pfile->vms->dummy.lock) { +struct panthor_gem_object *bo = pfile->vms->dummy.obj; + +if (bo) +drm_gem_object_put (&bo->base); + }
Missing
mutex_destroy(&pfile->vms->dummy.lock);
Quoted 30 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-1+xa_destroy (&pfile->vms->xa);kfree (pfile->vms); }@@ -1651,6 +1757,8 @@ int panthor_vm_pool_create(struct panthor_file *pfile)return -ENOMEM ;xa_init_flags (&pfile->vms->xa,XA_FLAGS_ALLOC1 ); + +mutex_init (&pfile->vms->dummy.lock);return 0 ; }@@ -1987,6 +2095,9 @@ static void panthor_vm_free(struct drm_gpuvm *gpuvm)free_io_pgtable_ops (vm->pgtbl_ops); +if (vm->dummy) +drm_gem_object_put (&vm->dummy->base); +drm_mm_takedown (&vm->mm);kfree (vm); }@@ -2146,7 +2257,26 @@ static void panthor_vma_init(struct panthor_vma *vma, u32 flags)#define PANTHOR_VM_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ - DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED) + DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE)
drivers/gpu/drm/panthor/panthor_mmu.c static void panthor_vma_init(struct panthor_vma *vma, u32 flags) · line 2271
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ +static int +panthor_vm_exec_map_op (struct panthor_vm *vm,u32 flags, +const struct drm_gpuva_op_map *op) +{ +struct panthor_gem_object *bo =to_panthor_bo (op->gem.obj); +int prot =flags_to_prot (flags); + +if (!op->va.range) +return 0 ;
Do we really expect a range of zero here? If not, I'd either drop the check, or at the very least, make it a drm_WARN_ON_ONCE().
Quoted 26 lines
drivers/gpu/drm/panthor/panthor_mmu.c +9
-3+ +if (flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ) +return panthor_vm_map_sparse (vm, op->va.addr, prot, + bo->dmap.sgt, op->va.range); + +return panthor_vm_map_pages (vm, op->va.addr, prot, bo->dmap.sgt, + op->gem.offset, op->va.range); +}static int panthor_gpuva_sm_step_map (struct drm_gpuva_op *op,void *priv) {@@ -2160,9 +2290,7 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv)panthor_vma_init (vma, op_ctx->flags &PANTHOR_VM_MAP_FLAGS ); - ret =panthor_vm_map_pages (vm, op->map.va.addr,flags_to_prot (vma->flags), - op_ctx->map.bo->dmap.sgt, op->map.gem.offset, - op->map.va.range); + ret =panthor_vm_exec_map_op (vm, vma->flags, &op->map);if (ret) {panthor_vm_op_ctx_return_vma (op_ctx, vma);return ret;@@ -2178,13 +2306,15 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) } static bool
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) · line 2317
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-1-iova_mapped_as_huge_page(struct drm_gpuva_op_map *op, u64 addr) +iova_mapped_as_huge_page(struct drm_gpuva_op_map *op, u64 addr, bool is_sparse) { struct panthor_gem_object *bo = to_panthor_bo(op->gem.obj);const struct page *pg;pgoff_t bo_offset; bo_offset = addr - op->va.addr + op->gem.offset; +if (is_sparse) + bo_offset %= bo->base.size;
If this is a sparse mapping, we just have to check the first page (so bo_offset=0).
Quoted 34 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-2pg = bo->backing.pages[bo_offset >>PAGE_SHIFT ];return folio_size (page_folio (pg)) >=SZ_2M ;@@ -2194,6 +2324,8 @@ static void unmap_hugepage_align(const struct drm_gpuva_op_remap *op, u64 *unmap_start, u64 *unmap_range) { + struct panthor_vma *unmap_vma = container_of(op->unmap->va, struct panthor_vma, base); +bool is_sparse = unmap_vma->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ;u64 aligned_unmap_start, aligned_unmap_end, unmap_end; unmap_end = *unmap_start + *unmap_range;@@ -2205,7 +2337,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->prev && aligned_unmap_start < *unmap_start && op->prev->va.addr <= aligned_unmap_start && -iova_mapped_as_huge_page (op->prev, *unmap_start)) { + (iova_mapped_as_huge_page (op->prev, *unmap_start, is_sparse))) { *unmap_range += *unmap_start - aligned_unmap_start; *unmap_start = aligned_unmap_start; }@@ -2215,7 +2347,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->next && aligned_unmap_end > unmap_end && op->next->va.addr + op->next->va.range >= aligned_unmap_end && -iova_mapped_as_huge_page (op->next, unmap_end -1 )) { + (iova_mapped_as_huge_page (op->next, *unmap_start, is_sparse))) { *unmap_range += aligned_unmap_end - unmap_end; } }@@ -2251,14 +2383,17 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }if (op->remap.prev) {
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2392
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-3-struct panthor_gem_object *bo =to_panthor_bo (op->remap.prev->gem.obj); -u64 offset = op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr; -u64 size = op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start; +const struct drm_gpuva_op_map map_op = { + .va.addr = unmap_start, + .va.range = + op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start, + .gem.obj = op->remap.prev->gem.obj, + .gem.offset = + op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr,
I believe it should be forced to zero if this is a sparse mapping, no? This makes me think we probably want this to be NULL, in the case of a sparse mapping. It shouldn't prevent reclaim from happening on the dummy BO, because the drm_gpuva has a separate vm_bo field. Yes it forces us to add bunch of is_sparse checks in a few other places, but I find it cleaner than pretending this is a regular BO.
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-3+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, unmap_start, -flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, offset, size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2269,14 +2404,15 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2411
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-3if (op->remap.next) { -struct panthor_gem_object *bo =to_panthor_bo (op->remap.next->gem.obj); -u64 addr = op->remap.next->va.addr; -u64 size = unmap_start + unmap_range - op->remap.next->va.addr; +const struct drm_gpuva_op_map map_op = { + .va.addr = op->remap.next->va.addr, + .va.range = unmap_start + unmap_range - op->remap.next->va.addr, + .gem.obj = op->remap.next->gem.obj, + .gem.offset = op->remap.next->gem.offset,
Same here, I'd rather have gem.obj=NULL and gem.offset=0 when remapping a porting of sparse mapping.
Quoted 62 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-5+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, addr,flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, op->remap.next->gem.offset, - size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2826,6 +2962,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,const struct drm_panthor_vm_bind_op *op, struct panthor_vm_op_ctx *op_ctx) { + struct panthor_file *pfile = file->driver_priv;ssize_t vm_pgsz =panthor_vm_page_size (vm);struct drm_gem_object *gem;int ret;@@ -2837,7 +2974,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP : gem =drm_gem_object_lookup (file, op->bo_handle); - ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, + ret =panthor_vm_prepare_map_op_ctx (op_ctx, pfile->vms, vm, gem ?to_panthor_bo (gem) :NULL , op);drm_gem_object_put (gem);@@ -3044,7 +3181,10 @@ int panthor_vm_map_bo_range(struct panthor_vm *vm, struct panthor_gem_object *bostruct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op); +if (drm_WARN_ON (&vm->ptdev->base, flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) +return -EINVAL ; + + ret =panthor_vm_prepare_map_op_ctx (&op_ctx,NULL , vm, bo, &op);if (ret)return ret;include/uapi/drm/panthor_drm.h +12
-0@@ -614,6 +614,18 @@ enum drm_panthor_vm_bind_op_flags {*/ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED =1 <<2 , +/** +* @DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE: Repeat a BO range +* +* Only valid with DRM_PANTHOR_VM_BIND_OP_TYPE_MAP. +* +* When this flag is set, the whole vm_bind range is mapped over a dummy object in a cyclic +* fashion, and all GPU reads from addresses in the range return undefined values. This flag +* being set means drm_panthor_vm_bind_op:offset and drm_panthor_vm_bind_op::handle must +* both be set to 0. +*/ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE = 1 << 3, +/** * @DRM_PANTHOR_VM_BIND_OP_TYPE_MASK: Mask used to determine the type of operation. */
Re: [PATCH v7 5/6] drm/panthor: Support sparse mappings
Hi Boris,
Quoted 129 lines · Boris Brezillon
Quoted 43 lines · On Wed, 15 Apr 2026 12:28:49 +0100 Adrián Larumbe…
Allow UM to bind sparsely populated memory regions by cyclically mapping virtual ranges over a kernel-allocated dummy BO. This alternative is preferable to the old method of handling sparseness in the UMD, because it relied on the creation of a buffer object to the same end, despite the fact Vulkan sparse resources don't need to be backed by a driver BO.
The choice of backing sparsely-bound regions with a Panhtor BO was made so as to profit from the existing shrinker reclaim code. That way no special treatment must be given to the dummy sparse BOs when reclaiming memory, as would be the case if we had chosen a raw kernel page implementation.
A new dummy BO is allocated per open file context, because even though the Vulkan spec mandates that writes into sparsely bound regions must be discarded, our implementation is still a workaround over the fact Mali CSF GPUs cannot support this behaviour on the hardware level, so writes still make it into the backing BO. If we had a global one, then it could be a venue for information leaks between file contexts, which should never happen in DRM.
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_gem.c | 35 +++++ drivers/gpu/drm/panthor/panthor_gem.h | 2 + drivers/gpu/drm/panthor/panthor_mmu.c | 192 ++++++++++++++++++++++---- include/uapi/drm/panthor_drm.h | 12 ++ 4 files changed, 215 insertions(+), 26 deletions(-)drivers/gpu/drm/panthor/panthor_gem.c +8
-0@@ -1345,6 +1345,41 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,return ERR_PTR (ret); } +/** +* panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. +* @ptdev: Device. +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_gem_object * +panthor_dummy_bo_create (struct panthor_device *ptdev)drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 1365
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gem.c +10
-0+{ +u32 dummy_flags =DRM_PANTHOR_BO_NO_MMAP ; +struct panthor_gem_object *bo; +struct page **pages; + + bo =panthor_gem_create (&ptdev->base,SZ_2M , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + + pages =drm_gem_get_pages (&bo->base);Why not use
panthor_gem_backing_get_pages_locked()here? Also,drm_gem_get_pages()doesn't give any guarantee that you'll get a huge page, nor can you guarantee that the 2M won't be reclaimed and later on be re-allocated as 4k chunks. I'd probably keep things simple for now, and
- keep it a 2M GEM object
- force the page allocation at map time, just like we do for regular BOsQuoted 38 lines
drivers/gpu/drm/panthor/panthor_gem.c +17
-0+if (PTR_ERR (pages) == -ENOMEM ) { +drm_gem_object_put (&bo->base); + bo =panthor_gem_create (&ptdev->base,SZ_4K , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + pages =drm_gem_get_pages (&bo->base); + } + +if (IS_ERR_OR_NULL (pages)) { +drm_gem_object_put (&bo->base); +return ERR_CAST (pages); + } + + bo->backing.pages = pages; +return bo; +} +static bool can_swap (void ) {return get_nr_swap_pages () >0 ;drivers/gpu/drm/panthor/panthor_gem.h +2
-0@@ -264,6 +264,8 @@ void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *lint panthor_gem_sync (struct drm_gem_object *obj,u32 type,u64 offset,u64 size); +struct panthor_gem_object *panthor_dummy_bo_create (struct panthor_device *ptdev); +struct drm_gem_object *panthor_gem_prime_import (struct drm_device *dev,struct dma_buf *dma_buf);drivers/gpu/drm/panthor/panthor_mmu.c +0
-0drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_mmu { · line 116
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -112,6 +112,23 @@ struct panthor_mmu {struct panthor_vm_pool {/** @xa: Array used for VM handle tracking. */ struct xarray xa; + +/** @dummy: Dummy drm object related fields /** * @dummy: Dummy drm object related fields.Quoted 17 lines
drivers/gpu/drm/panthor/panthor_mmu.c +15
-0+* +* Sparse bindings map virtual address ranges onto a dummy +* BO in a modulo fashion. Even though sparse writes are meant +* to be discarded and reads undefined, writes are still reflected +* in the dummy buffer. That means we must keep a dummy object per +* file context, to avoid data leaks between them. +* +*/ +struct { +/** @dummy.obj: Dummy object used for sparse mappings. */ +struct panthor_gem_object *obj; + +/** @dummy.lock: Lock protecting against races on dummy object. */ +struct mutex lock; + } dummy; };
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_vm { · line 412
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0/** @@ -391,6 +408,15 @@ struct panthor_vm {*/ struct list_head lru_node; } reclaim; + +/** @dummy: Dummy object used for sparse mappings. /** * @dummy: Dummy object used for sparse mappings.
Thanks for the catch. Do these comment formatting errors usually only show up when I build the sources with W=1 ?
Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-0+* +* VM's must keep a reference to the file context-wide dummy BO because +* they can outlive the file context, which includes the VM pool holding +* the original dummy BO reference. +* +*/ +struct panthor_gem_object *dummy; };/** @@ -1020,6 +1046,46 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,return 0 ; }
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1053
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0+static int +panthor_vm_map_sparse (struct panthor_vm *vm,u64 iova,int prot, +struct sg_table *sgt,u64 size) +{ +u64 first_iova = iova;s/first_iova/orig_iova/
Will do.
Quoted 35 lines
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_mmu.c +22
-0+u64 first_size = size; +int ret; + +if (iova & (SZ_2M -1 )) { +u64 unaligned_size =min (ALIGN (iova,SZ_2M ) - iova, size); + + ret =panthor_vm_map_pages (vm, iova, prot, sgt, +0 , unaligned_size); +if (ret) +return ret; + + size -= unaligned_size; + iova += unaligned_size; + } + +/* TODO: we should probably optimize this at the io_pgtable level. */ +while (size >0 ) { +u64 next_size =min (size,sg_dma_len (sgt->sgl)); + + ret =panthor_vm_map_pages (vm, iova, prot, + sgt,0 , next_size); +if (ret)drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1085
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ goto err_unmap; + + size -= next_size; + iova += next_size; + } + +return 0 ; + +err_unmap: +panthor_vm_unmap_pages (vm, first_iova, first_size - size);If you do:
Quoted 3 lines
panthor_vm_unmap_pages(vm, orig_iova, iova - orig_iova);you can get rid of the
first_sizevariable.
Will do.
Quoted 42 lines
Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +12
-0+return ret; +} +static int flags_to_prot (u32 flags) {int prot =0 ;@@ -1258,38 +1324,71 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)return 0 ; } +static struct panthor_gem_object * +panthor_vm_get_dummy_obj (struct panthor_vm_pool *pool, +struct panthor_vm *vm) +{ + scoped_guard(mutex, &pool->dummy.lock) { +if (!vm->dummy) { +if (!pool->dummy.obj) { +struct panthor_gem_object *obj = +panthor_dummy_bo_create (vm->ptdev);drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1345
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+if (IS_ERR (obj)) +return obj; + + pool->dummy.obj = obj; + } + +drm_gem_object_get (&pool->dummy.obj->base); + vm->dummy = pool->dummy.obj; + } + }The lock is taken for the whole function scope, you you can simply use guard(mutex)() and get rid of two indentation levels:
guard(mutex)(&pool->dummy.lock); if (vm->dummy) return vm->dummy; if (!pool->dummy.obj) { struct panthor_gem_object *obj; obj = panthor_dummy_bo_create(vm->ptdev); if (IS_ERR(obj))
Quoted 9 lines
return obj; pool->dummy.obj = obj; } drm_gem_object_get(&pool->dummy.obj->base); vm->dummy = pool->dummy.obj; return vm->dummy;
It's cleaner this way, will do.
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0+ +return vm->dummy; +} +#define PANTHOR_VM_BIND_OP_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE | \ DRM_PANTHOR_VM_BIND_OP_TYPE_MASK)
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1358
Quoted 2 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-0static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, + struct panthor_vm_pool *pool,Can't we just make sure
vm->dummyis allocated beforepanthor_vm_prepare_map_op_ctx()is called in case this is a sparse map request? This would prevent the conditional check on pool != NULL, but only when is_sparse=true, and you wouldn't have to pass the pool around.
I guess the ideal place for allocating the dummy bo would be panthor_vm_bind_prepare_op_ctx(), then panthor_vm_prepare_map_op_ctx() can remain untouched.
Quoted 33 lines
Quoted 16 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-3struct panthor_vm *vm, struct panthor_gem_object *bo, const struct drm_panthor_vm_bind_op *op) { + bool is_sparse = op->flags & DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE;struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;int ret; -if (!bo) -return -EINVAL ; -if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1380
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-1/* Make sure the VA and size are in-bounds. */ -if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size) +if (bo && (is_sparse || op->size > bo->base.size || + op->bo_offset > bo->base.size - op->size))return -EINVAL ; +else if (is_sparse && (!pool || op->bo_handle || op->bo_offset)) +return -EINVAL ; + +if (is_sparse) { + bo =panthor_vm_get_dummy_obj (pool, vm);Actually, you assign bo here, so you might as well just pass the dummy BO to
panthor_vm_prepare_map_op_ctx()and keep theif (!bo) return -EINVAL;
Quoted 6 lines
check.
As a side note, if gpuva.gem.obj != NULL for sparse mappings, it messes up with the
can_mergechecks done by gpuvm, which is not a problem right now because we simply ignore the .keep hint passed tounmap_op, but that's probably worth a comment somewhere.
I can mention that in panthor_gpuva_sm_step_unmap.
Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-0+if (IS_ERR_OR_NULL (bo)) +return PTR_ERR (bo); + }/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ if (bo->exclusive_vm_root_gem && bo->exclusive_vm_root_gem !=panthor_vm_root_gem (vm))return -EINVAL ;
Quoted 6 lines
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1391
Quoted 3 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-1-panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags + | ((is_sparse) ?DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC :0 ));I would actually enforce NOEXEC is set and return EINVAL if that's not the case.
Will make it part of the uAPI and check for it inside panthor_vm_prepare_map_op_ctx() .
Quoted 53 lines
drivers/gpu/drm/panthor/panthor_mmu.c void panthor_vm_pool_destroy(struct panthor_file *pfile) · line 1741
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)@@ -1634,6 +1733,13 @@ void panthor_vm_pool_destroy(struct panthor_file *pfile)xa_for_each (&pfile->vms->xa, i, vm)panthor_vm_destroy (vm); + scoped_guard(mutex, &pfile->vms->dummy.lock) { +struct panthor_gem_object *bo = pfile->vms->dummy.obj; + +if (bo) +drm_gem_object_put (&bo->base); + }Missing
mutex_destroy(&pfile->vms->dummy.lock);Quoted 35 lines
drivers/gpu/drm/panthor/panthor_mmu.c +13
-1+xa_destroy (&pfile->vms->xa);kfree (pfile->vms); }@@ -1651,6 +1757,8 @@ int panthor_vm_pool_create(struct panthor_file *pfile)return -ENOMEM ;xa_init_flags (&pfile->vms->xa,XA_FLAGS_ALLOC1 ); + +mutex_init (&pfile->vms->dummy.lock);return 0 ; }@@ -1987,6 +2095,9 @@ static void panthor_vm_free(struct drm_gpuvm *gpuvm)free_io_pgtable_ops (vm->pgtbl_ops); +if (vm->dummy) +drm_gem_object_put (&vm->dummy->base); +drm_mm_takedown (&vm->mm);kfree (vm); }@@ -2146,7 +2257,26 @@ static void panthor_vma_init(struct panthor_vma *vma, u32 flags)#define PANTHOR_VM_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ - DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED) + DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE) + +static int +panthor_vm_exec_map_op (struct panthor_vm *vm,u32 flags, +const struct drm_gpuva_op_map *op) +{
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c static void panthor_vma_init(struct panthor_vma *vma, u32 flags) · line 2271
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0+struct panthor_gem_object *bo =to_panthor_bo (op->gem.obj); +int prot =flags_to_prot (flags); + +if (!op->va.range) +return 0 ;Do we really expect a range of zero here? If not, I'd either drop the check, or at the very least, make it a
drm_WARN_ON_ONCE().
IIRC it can happen when panthor_vm_exec_map_op() is called from panthor_gpuva_sm_step_remap(), and the remap's unmap didn't have to be expanded to account for a THP. Although in that case, the check being done inside panthor_vm_map_pages() should be enough.
Quoted 33 lines
Quoted 33 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-4+ +if (flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ) +return panthor_vm_map_sparse (vm, op->va.addr, prot, + bo->dmap.sgt, op->va.range); + +return panthor_vm_map_pages (vm, op->va.addr, prot, bo->dmap.sgt, + op->gem.offset, op->va.range); +}static int panthor_gpuva_sm_step_map (struct drm_gpuva_op *op,void *priv) {@@ -2160,9 +2290,7 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv)panthor_vma_init (vma, op_ctx->flags &PANTHOR_VM_MAP_FLAGS ); - ret =panthor_vm_map_pages (vm, op->map.va.addr,flags_to_prot (vma->flags), - op_ctx->map.bo->dmap.sgt, op->map.gem.offset, - op->map.va.range); + ret =panthor_vm_exec_map_op (vm, vma->flags, &op->map);if (ret) {panthor_vm_op_ctx_return_vma (op_ctx, vma);return ret;@@ -2178,13 +2306,15 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) }static bool -iova_mapped_as_huge_page (struct drm_gpuva_op_map *op,u64 addr) +iova_mapped_as_huge_page (struct drm_gpuva_op_map *op,u64 addr,bool is_sparse) {struct panthor_gem_object *bo =to_panthor_bo (op->gem.obj);const struct page *pg;pgoff_t bo_offset;
Quoted 6 lines
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) · line 2317
Quoted 3 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0bo_offset = addr - op->va.addr + op->gem.offset; +if (is_sparse) + bo_offset %= bo->base.size;If this is a sparse mapping, we just have to check the first page (so bo_offset=0).
Will do.
Quoted 45 lines
Quoted 34 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-2pg = bo->backing.pages[bo_offset >>PAGE_SHIFT ];return folio_size (page_folio (pg)) >=SZ_2M ;@@ -2194,6 +2324,8 @@ static void unmap_hugepage_align(const struct drm_gpuva_op_remap *op, u64 *unmap_start, u64 *unmap_range) { + struct panthor_vma *unmap_vma = container_of(op->unmap->va, struct panthor_vma, base); +bool is_sparse = unmap_vma->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ;u64 aligned_unmap_start, aligned_unmap_end, unmap_end; unmap_end = *unmap_start + *unmap_range;@@ -2205,7 +2337,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->prev && aligned_unmap_start < *unmap_start && op->prev->va.addr <= aligned_unmap_start && -iova_mapped_as_huge_page (op->prev, *unmap_start)) { + (iova_mapped_as_huge_page (op->prev, *unmap_start, is_sparse))) { *unmap_range += *unmap_start - aligned_unmap_start; *unmap_start = aligned_unmap_start; }@@ -2215,7 +2347,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->next && aligned_unmap_end > unmap_end && op->next->va.addr + op->next->va.range >= aligned_unmap_end && -iova_mapped_as_huge_page (op->next, unmap_end -1 )) { + (iova_mapped_as_huge_page (op->next, *unmap_start, is_sparse))) { *unmap_range += aligned_unmap_end - unmap_end; } }@@ -2251,14 +2383,17 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }if (op->remap.prev) {drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2392
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-3-struct panthor_gem_object *bo =to_panthor_bo (op->remap.prev->gem.obj); -u64 offset = op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr; -u64 size = op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start; +const struct drm_gpuva_op_map map_op = { + .va.addr = unmap_start, + .va.range = + op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start, + .gem.obj = op->remap.prev->gem.obj, + .gem.offset = + op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr,
Quoted 7 lines
I believe it should be forced to zero if this is a sparse mapping, no? This makes me think we probably want this to be NULL, in the case of a sparse mapping. It shouldn't prevent reclaim from happening on the dummy BO, because the
drm_gpuvahas a separatevm_bofield. Yes it forces us to add bunch ofis_sparsechecks in a few other places, but I find it cleaner than pretending this is a regular BO.
The .gem.offset field is assigned here unconditionally, but discarded in cases it's a sparse mapping when calling panthor_vm_map_sparse() (which takes no offset argument). I assume what you mean is that in panthor_vm_exec_op(), I should abstain from assining .map.gem.obj and .map.gem.offset. However, if I do that, the 'va->vm_bo = drm_gpuvm_bo_get(vm_bo);' will never happen inside drm_gpuva_link().
Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-3+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, unmap_start, -flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, offset, size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2269,14 +2404,15 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2411
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-3if (op->remap.next) { -struct panthor_gem_object *bo =to_panthor_bo (op->remap.next->gem.obj); -u64 addr = op->remap.next->va.addr; -u64 size = unmap_start + unmap_range - op->remap.next->va.addr; +const struct drm_gpuva_op_map map_op = { + .va.addr = op->remap.next->va.addr, + .va.range = unmap_start + unmap_range - op->remap.next->va.addr, + .gem.obj = op->remap.next->gem.obj, + .gem.offset = op->remap.next->gem.offset,Same here, I'd rather have gem.obj=NULL and gem.offset=0 when remapping a porting of sparse mapping.
I could do that and then insert warnings in panthor_vm_map_pages() when it's sparse to make sure these fields are zero-initialised.
Quoted 47 lines
Quoted 47 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-5+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, addr,flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, op->remap.next->gem.offset, - size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2826,6 +2962,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,const struct drm_panthor_vm_bind_op *op, struct panthor_vm_op_ctx *op_ctx) { + struct panthor_file *pfile = file->driver_priv;ssize_t vm_pgsz =panthor_vm_page_size (vm);struct drm_gem_object *gem;int ret;@@ -2837,7 +2974,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP : gem =drm_gem_object_lookup (file, op->bo_handle); - ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, + ret =panthor_vm_prepare_map_op_ctx (op_ctx, pfile->vms, vm, gem ?to_panthor_bo (gem) :NULL , op);drm_gem_object_put (gem);@@ -3044,7 +3181,10 @@ int panthor_vm_map_bo_range(struct panthor_vm *vm, struct panthor_gem_object *bostruct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op); +if (drm_WARN_ON (&vm->ptdev->base, flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) +return -EINVAL ; + + ret =panthor_vm_prepare_map_op_ctx (&op_ctx,NULL , vm, bo, &op);if (ret)return ret;include/uapi/drm/panthor_drm.h +0
-0@@ -614,6 +614,18 @@ enum drm_panthor_vm_bind_op_flags {*/ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED = 1 << 2,
Quoted 15 lines
Quoted 5 lines
include/uapi/drm/panthor_drm.h +5
-0+/** +* @DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE: Repeat a BO range +* +* Only valid with DRM_PANTHOR_VM_BIND_OP_TYPE_MAP. +* include/uapi/drm/panthor_drm.h enum drm_panthor_vm_bind_op_flags { · line 631
Quoted 10 lines
include/uapi/drm/panthor_drm.h +7
-0+* When this flag is set, the whole vm_bind range is mapped over a dummy object in a cyclic +* fashion, and all GPU reads from addresses in the range return undefined values. This flag +* being set means drm_panthor_vm_bind_op:offset and drm_panthor_vm_bind_op::handle must +* both be set to 0. +*/ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE = 1 << 3, +/** * @DRM_PANTHOR_VM_BIND_OP_TYPE_MASK: Mask used to determine the type of operation. */
Adrian Larumbe
Boris Brezillon
maintainerRe: [PATCH v7 5/6] drm/panthor: Support sparse mappings
Quoted 23 lines · On Wed, 15 Apr 2026 23:09:17 +0100 Adrián Larumbe…
Quoted 15 lines
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0@@ -2251,14 +2383,17 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }if (op->remap.prev) {drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2392
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-3-struct panthor_gem_object *bo =to_panthor_bo (op->remap.prev->gem.obj); -u64 offset = op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr; -u64 size = op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start; +const struct drm_gpuva_op_map map_op = { + .va.addr = unmap_start, + .va.range = + op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start, + .gem.obj = op->remap.prev->gem.obj, + .gem.offset = + op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr,Quoted 7 lines
I believe it should be forced to zero if this is a sparse mapping, no? This makes me think we probably want this to be NULL, in the case of a sparse mapping. It shouldn't prevent reclaim from happening on the dummy BO, because the
drm_gpuvahas a separatevm_bofield. Yes it forces us to add bunch ofis_sparsechecks in a few other places, but I find it cleaner than pretending this is a regular BO.
Quoted 4 lines
The .gem.offset field is assigned here unconditionally, but discarded in cases it's a sparse mapping when calling
panthor_vm_map_sparse()(which takes no offset argument). I assume what you mean is that inpanthor_vm_exec_op(), I should abstain from assining .map.gem.obj and .map.gem.offset. However, if I do that, the 'va->vm_bo=drm_gpuvm_bo_get(vm_bo);' will never happen insidedrm_gpuva_link().
Ah, crap! I thought drm_gpuva_link() was only considering vm_bo. Okay, let's keep it the way you did it then, and just add a comment explaining unmap_op.keep can't be trusted when the mapping is sparse.
Boris Brezillon
maintainerRe: [PATCH v7 5/6] drm/panthor: Support sparse mappings
In reply to Adrián LarumbeQuoted 13 lines · On Wed, 15 Apr 2026 23:09:17 +0100 Adrián Larumbe…
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-0+static int +panthor_vm_exec_map_op (struct panthor_vm *vm,u32 flags, +const struct drm_gpuva_op_map *op) +{Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c static void panthor_vma_init(struct panthor_vma *vma, u32 flags) · line 2271
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0+struct panthor_gem_object *bo =to_panthor_bo (op->gem.obj); +int prot =flags_to_prot (flags); + +if (!op->va.range) +return 0 ;Do we really expect a range of zero here? If not, I'd either drop the check, or at the very least, make it a
drm_WARN_ON_ONCE().
Quoted 2 lines
IIRC it can happen when
panthor_vm_exec_map_op()is called frompanthor_gpuva_sm_step_remap(), and the remap's unmap didn't have to be expanded to account for a THP.
Can't we just replace that by an extra size > 0 check before calling panthor_vm_exec_map_op() in the remap() path? I mean, we already have an !evicted check there.
if (!unmap_vma->evicted && size) {
ret = panthor_vm_exec_map_op()
...
}Quoted 2 lines
Although in that case,
the check being done insidepanthor_vm_map_pages()should be enough.
Re: [PATCH v7 5/6] drm/panthor: Support sparse mappings
In reply to Boris BrezillonQuoted 50 lines · Boris Brezillon
Quoted 47 lines · On Wed, 15 Apr 2026 12:28:49 +0100 Adrián Larumbe…
Allow UM to bind sparsely populated memory regions by cyclically mapping virtual ranges over a kernel-allocated dummy BO. This alternative is preferable to the old method of handling sparseness in the UMD, because it relied on the creation of a buffer object to the same end, despite the fact Vulkan sparse resources don't need to be backed by a driver BO.
The choice of backing sparsely-bound regions with a Panhtor BO was made so as to profit from the existing shrinker reclaim code. That way no special treatment must be given to the dummy sparse BOs when reclaiming memory, as would be the case if we had chosen a raw kernel page implementation.
A new dummy BO is allocated per open file context, because even though the Vulkan spec mandates that writes into sparsely bound regions must be discarded, our implementation is still a workaround over the fact Mali CSF GPUs cannot support this behaviour on the hardware level, so writes still make it into the backing BO. If we had a global one, then it could be a venue for information leaks between file contexts, which should never happen in DRM.
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_gem.c | 35 +++++ drivers/gpu/drm/panthor/panthor_gem.h | 2 + drivers/gpu/drm/panthor/panthor_mmu.c | 192 ++++++++++++++++++++++---- include/uapi/drm/panthor_drm.h | 12 ++ 4 files changed, 215 insertions(+), 26 deletions(-)drivers/gpu/drm/panthor/panthor_gem.c +12
-0@@ -1345,6 +1345,41 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,return ERR_PTR (ret); } +/** +* panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. +* @ptdev: Device. +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_gem_object * +panthor_dummy_bo_create (struct panthor_device *ptdev) +{ +u32 dummy_flags =DRM_PANTHOR_BO_NO_MMAP ; +struct panthor_gem_object *bo; +struct page **pages;
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 1365
Quoted 6 lines
drivers/gpu/drm/panthor/panthor_gem.c +6
-0+ + bo =panthor_gem_create (&ptdev->base,SZ_2M , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + + pages =drm_gem_get_pages (&bo->base);Why not use
panthor_gem_backing_get_pages_locked()here? Also,
I think because I saw that it's a locked function, but because the BO hasn't been mapped or targeted by a job yet, then I thought I might just get the pages straight away rather than locking it before calling panthor_gem_backing_get_pages_locked().
Quoted 6 lines
drm_gem_get_pages()doesn't give any guarantee that you'll get a huge page, nor can you guarantee that the 2M won't be reclaimed and later on be re-allocated as 4k chunks. I'd probably keep things simple for now, and
- keep it a 2M GEM object
- force the page allocation at map time, just like we do for regular BOs
This sounds good, but I think it partly defeats the purpose of falling back on a smaller BO when we cannot get enough pages for a 2MiB one (whether those pages form a single PMD folio or not), which I think is something we should still bear in mind for memory-constrained systems.
Quoted 492 lines
Quoted 38 lines
drivers/gpu/drm/panthor/panthor_gem.c +17
-0+if (PTR_ERR (pages) == -ENOMEM ) { +drm_gem_object_put (&bo->base); + bo =panthor_gem_create (&ptdev->base,SZ_4K , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + pages =drm_gem_get_pages (&bo->base); + } + +if (IS_ERR_OR_NULL (pages)) { +drm_gem_object_put (&bo->base); +return ERR_CAST (pages); + } + + bo->backing.pages = pages; +return bo; +} +static bool can_swap (void ) {return get_nr_swap_pages () >0 ;drivers/gpu/drm/panthor/panthor_gem.h +2
-0@@ -264,6 +264,8 @@ void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *lint panthor_gem_sync (struct drm_gem_object *obj,u32 type,u64 offset,u64 size); +struct panthor_gem_object *panthor_dummy_bo_create (struct panthor_device *ptdev); +struct drm_gem_object *panthor_gem_prime_import (struct drm_device *dev,struct dma_buf *dma_buf);drivers/gpu/drm/panthor/panthor_mmu.c +0
-0drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_mmu { · line 116
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -112,6 +112,23 @@ struct panthor_mmu {struct panthor_vm_pool {/** @xa: Array used for VM handle tracking. */ struct xarray xa; + +/** @dummy: Dummy drm object related fields /** * @dummy: Dummy drm object related fields.Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +15
-0+* +* Sparse bindings map virtual address ranges onto a dummy +* BO in a modulo fashion. Even though sparse writes are meant +* to be discarded and reads undefined, writes are still reflected +* in the dummy buffer. That means we must keep a dummy object per +* file context, to avoid data leaks between them. +* +*/ +struct { +/** @dummy.obj: Dummy object used for sparse mappings. */ +struct panthor_gem_object *obj; + +/** @dummy.lock: Lock protecting against races on dummy object. */ +struct mutex lock; + } dummy; };/** drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_vm { · line 412
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -391,6 +408,15 @@ struct panthor_vm {*/ struct list_head lru_node; } reclaim; + +/** @dummy: Dummy object used for sparse mappings. /** * @dummy: Dummy object used for sparse mappings.Quoted 11 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-0+* +* VM's must keep a reference to the file context-wide dummy BO because +* they can outlive the file context, which includes the VM pool holding +* the original dummy BO reference. +* +*/ +struct panthor_gem_object *dummy; };/** drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1053
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0@@ -1020,6 +1046,46 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,return 0 ; } +static int +panthor_vm_map_sparse (struct panthor_vm *vm,u64 iova,int prot, +struct sg_table *sgt,u64 size) +{ +u64 first_iova = iova;s/first_iova/orig_iova/
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_mmu.c +22
-0+u64 first_size = size; +int ret; + +if (iova & (SZ_2M -1 )) { +u64 unaligned_size =min (ALIGN (iova,SZ_2M ) - iova, size); + + ret =panthor_vm_map_pages (vm, iova, prot, sgt, +0 , unaligned_size); +if (ret) +return ret; + + size -= unaligned_size; + iova += unaligned_size; + } + +/* TODO: we should probably optimize this at the io_pgtable level. */ +while (size >0 ) { +u64 next_size =min (size,sg_dma_len (sgt->sgl)); + + ret =panthor_vm_map_pages (vm, iova, prot, + sgt,0 , next_size); +if (ret)drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1085
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ goto err_unmap; + + size -= next_size; + iova += next_size; + } + +return 0 ; + +err_unmap: +panthor_vm_unmap_pages (vm, first_iova, first_size - size);If you do:
panthor_vm_unmap_pages(vm, orig_iova, iova - orig_iova);you can get rid of the
first_sizevariable.Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +12
-0+return ret; +} +static int flags_to_prot (u32 flags) {int prot =0 ;@@ -1258,38 +1324,71 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)return 0 ; } +static struct panthor_gem_object * +panthor_vm_get_dummy_obj (struct panthor_vm_pool *pool, +struct panthor_vm *vm) +{ + scoped_guard(mutex, &pool->dummy.lock) { +if (!vm->dummy) { +if (!pool->dummy.obj) { +struct panthor_gem_object *obj = +panthor_dummy_bo_create (vm->ptdev);drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1345
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+if (IS_ERR (obj)) +return obj; + + pool->dummy.obj = obj; + } + +drm_gem_object_get (&pool->dummy.obj->base); + vm->dummy = pool->dummy.obj; + } + }The lock is taken for the whole function scope, you you can simply use guard(mutex)() and get rid of two indentation levels:
guard(mutex)(&pool->dummy.lock); if (vm->dummy) return vm->dummy; if (!pool->dummy.obj) { struct panthor_gem_object *obj; obj = panthor_dummy_bo_create(vm->ptdev); if (IS_ERR(obj)) return obj; pool->dummy.obj = obj; } drm_gem_object_get(&pool->dummy.obj->base); vm->dummy = pool->dummy.obj; return vm->dummy;drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1358
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0+ +return vm->dummy; +} +#define PANTHOR_VM_BIND_OP_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE | \ DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, + struct panthor_vm_pool *pool,Can't we just make sure
vm->dummyis allocated beforepanthor_vm_prepare_map_op_ctx()is called in case this is a sparse map request? This would prevent the conditional check on pool != NULL, but only when is_sparse=true, and you wouldn't have to pass the pool around.Quoted 16 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-3struct panthor_vm *vm, struct panthor_gem_object *bo, const struct drm_panthor_vm_bind_op *op) { + bool is_sparse = op->flags & DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE;struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;int ret; -if (!bo) -return -EINVAL ; -if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1380
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-1/* Make sure the VA and size are in-bounds. */ -if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size) +if (bo && (is_sparse || op->size > bo->base.size || + op->bo_offset > bo->base.size - op->size))return -EINVAL ; +else if (is_sparse && (!pool || op->bo_handle || op->bo_offset)) +return -EINVAL ; + +if (is_sparse) { + bo =panthor_vm_get_dummy_obj (pool, vm);Actually, you assign bo here, so you might as well just pass the dummy BO to
panthor_vm_prepare_map_op_ctx()and keep theif (!bo) return -EINVAL;check.
As a side note, if gpuva.gem.obj != NULL for sparse mappings, it messes up with the
can_mergechecks done by gpuvm, which is not a problem right now because we simply ignore the .keep hint passed tounmap_op, but that's probably worth a comment somewhere.drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1391
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-1+if (IS_ERR_OR_NULL (bo)) +return PTR_ERR (bo); + }/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ if (bo->exclusive_vm_root_gem && bo->exclusive_vm_root_gem !=panthor_vm_root_gem (vm))return -EINVAL ; -panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags + | ((is_sparse) ?DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC :0 ));I would actually enforce NOEXEC is set and return EINVAL if that's not the case.
drivers/gpu/drm/panthor/panthor_mmu.c void panthor_vm_pool_destroy(struct panthor_file *pfile) · line 1741
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)@@ -1634,6 +1733,13 @@ void panthor_vm_pool_destroy(struct panthor_file *pfile)xa_for_each (&pfile->vms->xa, i, vm)panthor_vm_destroy (vm); + scoped_guard(mutex, &pfile->vms->dummy.lock) { +struct panthor_gem_object *bo = pfile->vms->dummy.obj; + +if (bo) +drm_gem_object_put (&bo->base); + }Missing
mutex_destroy(&pfile->vms->dummy.lock);Quoted 30 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-1+xa_destroy (&pfile->vms->xa);kfree (pfile->vms); }@@ -1651,6 +1757,8 @@ int panthor_vm_pool_create(struct panthor_file *pfile)return -ENOMEM ;xa_init_flags (&pfile->vms->xa,XA_FLAGS_ALLOC1 ); + +mutex_init (&pfile->vms->dummy.lock);return 0 ; }@@ -1987,6 +2095,9 @@ static void panthor_vm_free(struct drm_gpuvm *gpuvm)free_io_pgtable_ops (vm->pgtbl_ops); +if (vm->dummy) +drm_gem_object_put (&vm->dummy->base); +drm_mm_takedown (&vm->mm);kfree (vm); }@@ -2146,7 +2257,26 @@ static void panthor_vma_init(struct panthor_vma *vma, u32 flags)#define PANTHOR_VM_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ - DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED) + DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE)drivers/gpu/drm/panthor/panthor_mmu.c static void panthor_vma_init(struct panthor_vma *vma, u32 flags) · line 2271
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ +static int +panthor_vm_exec_map_op (struct panthor_vm *vm,u32 flags, +const struct drm_gpuva_op_map *op) +{ +struct panthor_gem_object *bo =to_panthor_bo (op->gem.obj); +int prot =flags_to_prot (flags); + +if (!op->va.range) +return 0 ;Do we really expect a range of zero here? If not, I'd either drop the check, or at the very least, make it a
drm_WARN_ON_ONCE().Quoted 26 lines
drivers/gpu/drm/panthor/panthor_mmu.c +9
-3+ +if (flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ) +return panthor_vm_map_sparse (vm, op->va.addr, prot, + bo->dmap.sgt, op->va.range); + +return panthor_vm_map_pages (vm, op->va.addr, prot, bo->dmap.sgt, + op->gem.offset, op->va.range); +}static int panthor_gpuva_sm_step_map (struct drm_gpuva_op *op,void *priv) {@@ -2160,9 +2290,7 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv)panthor_vma_init (vma, op_ctx->flags &PANTHOR_VM_MAP_FLAGS ); - ret =panthor_vm_map_pages (vm, op->map.va.addr,flags_to_prot (vma->flags), - op_ctx->map.bo->dmap.sgt, op->map.gem.offset, - op->map.va.range); + ret =panthor_vm_exec_map_op (vm, vma->flags, &op->map);if (ret) {panthor_vm_op_ctx_return_vma (op_ctx, vma);return ret;@@ -2178,13 +2306,15 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) } static booldrivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) · line 2317
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-1-iova_mapped_as_huge_page(struct drm_gpuva_op_map *op, u64 addr) +iova_mapped_as_huge_page(struct drm_gpuva_op_map *op, u64 addr, bool is_sparse) { struct panthor_gem_object *bo = to_panthor_bo(op->gem.obj);const struct page *pg;pgoff_t bo_offset; bo_offset = addr - op->va.addr + op->gem.offset; +if (is_sparse) + bo_offset %= bo->base.size;If this is a sparse mapping, we just have to check the first page (so bo_offset=0).
Quoted 34 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-2pg = bo->backing.pages[bo_offset >>PAGE_SHIFT ];return folio_size (page_folio (pg)) >=SZ_2M ;@@ -2194,6 +2324,8 @@ static void unmap_hugepage_align(const struct drm_gpuva_op_remap *op, u64 *unmap_start, u64 *unmap_range) { + struct panthor_vma *unmap_vma = container_of(op->unmap->va, struct panthor_vma, base); +bool is_sparse = unmap_vma->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ;u64 aligned_unmap_start, aligned_unmap_end, unmap_end; unmap_end = *unmap_start + *unmap_range;@@ -2205,7 +2337,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->prev && aligned_unmap_start < *unmap_start && op->prev->va.addr <= aligned_unmap_start && -iova_mapped_as_huge_page (op->prev, *unmap_start)) { + (iova_mapped_as_huge_page (op->prev, *unmap_start, is_sparse))) { *unmap_range += *unmap_start - aligned_unmap_start; *unmap_start = aligned_unmap_start; }@@ -2215,7 +2347,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->next && aligned_unmap_end > unmap_end && op->next->va.addr + op->next->va.range >= aligned_unmap_end && -iova_mapped_as_huge_page (op->next, unmap_end -1 )) { + (iova_mapped_as_huge_page (op->next, *unmap_start, is_sparse))) { *unmap_range += aligned_unmap_end - unmap_end; } }@@ -2251,14 +2383,17 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }if (op->remap.prev) {drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2392
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-3-struct panthor_gem_object *bo =to_panthor_bo (op->remap.prev->gem.obj); -u64 offset = op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr; -u64 size = op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start; +const struct drm_gpuva_op_map map_op = { + .va.addr = unmap_start, + .va.range = + op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start, + .gem.obj = op->remap.prev->gem.obj, + .gem.offset = + op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr,I believe it should be forced to zero if this is a sparse mapping, no? This makes me think we probably want this to be NULL, in the case of a sparse mapping. It shouldn't prevent reclaim from happening on the dummy BO, because the
drm_gpuvahas a separatevm_bofield. Yes it forces us to add bunch ofis_sparsechecks in a few other places, but I find it cleaner than pretending this is a regular BO.Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-3+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, unmap_start, -flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, offset, size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2269,14 +2404,15 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2411
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-3if (op->remap.next) { -struct panthor_gem_object *bo =to_panthor_bo (op->remap.next->gem.obj); -u64 addr = op->remap.next->va.addr; -u64 size = unmap_start + unmap_range - op->remap.next->va.addr; +const struct drm_gpuva_op_map map_op = { + .va.addr = op->remap.next->va.addr, + .va.range = unmap_start + unmap_range - op->remap.next->va.addr, + .gem.obj = op->remap.next->gem.obj, + .gem.offset = op->remap.next->gem.offset,Same here, I'd rather have gem.obj=NULL and gem.offset=0 when remapping a porting of sparse mapping.
Quoted 47 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-5+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, addr,flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, op->remap.next->gem.offset, - size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2826,6 +2962,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,const struct drm_panthor_vm_bind_op *op, struct panthor_vm_op_ctx *op_ctx) { + struct panthor_file *pfile = file->driver_priv;ssize_t vm_pgsz =panthor_vm_page_size (vm);struct drm_gem_object *gem;int ret;@@ -2837,7 +2974,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP : gem =drm_gem_object_lookup (file, op->bo_handle); - ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, + ret =panthor_vm_prepare_map_op_ctx (op_ctx, pfile->vms, vm, gem ?to_panthor_bo (gem) :NULL , op);drm_gem_object_put (gem);@@ -3044,7 +3181,10 @@ int panthor_vm_map_bo_range(struct panthor_vm *vm, struct panthor_gem_object *bostruct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op); +if (drm_WARN_ON (&vm->ptdev->base, flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) +return -EINVAL ; + + ret =panthor_vm_prepare_map_op_ctx (&op_ctx,NULL , vm, bo, &op);if (ret)return ret;include/uapi/drm/panthor_drm.h +0
-0@@ -614,6 +614,18 @@ enum drm_panthor_vm_bind_op_flags {*/ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED = 1 << 2,
Quoted 15 lines
Quoted 5 lines
include/uapi/drm/panthor_drm.h +5
-0+/** +* @DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE: Repeat a BO range +* +* Only valid with DRM_PANTHOR_VM_BIND_OP_TYPE_MAP. +* include/uapi/drm/panthor_drm.h enum drm_panthor_vm_bind_op_flags { · line 631
Quoted 10 lines
include/uapi/drm/panthor_drm.h +7
-0+* When this flag is set, the whole vm_bind range is mapped over a dummy object in a cyclic +* fashion, and all GPU reads from addresses in the range return undefined values. This flag +* being set means drm_panthor_vm_bind_op:offset and drm_panthor_vm_bind_op::handle must +* both be set to 0. +*/ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE = 1 << 3, +/** * @DRM_PANTHOR_VM_BIND_OP_TYPE_MASK: Mask used to determine the type of operation. */
Adrian Larumbe
Re: [PATCH v7 5/6] drm/panthor: Support sparse mappings
In reply to Boris BrezillonQuoted 63 lines · Boris Brezillon
Quoted 43 lines · On Wed, 15 Apr 2026 12:28:49 +0100 Adrián Larumbe…
Allow UM to bind sparsely populated memory regions by cyclically mapping virtual ranges over a kernel-allocated dummy BO. This alternative is preferable to the old method of handling sparseness in the UMD, because it relied on the creation of a buffer object to the same end, despite the fact Vulkan sparse resources don't need to be backed by a driver BO.
The choice of backing sparsely-bound regions with a Panhtor BO was made so as to profit from the existing shrinker reclaim code. That way no special treatment must be given to the dummy sparse BOs when reclaiming memory, as would be the case if we had chosen a raw kernel page implementation.
A new dummy BO is allocated per open file context, because even though the Vulkan spec mandates that writes into sparsely bound regions must be discarded, our implementation is still a workaround over the fact Mali CSF GPUs cannot support this behaviour on the hardware level, so writes still make it into the backing BO. If we had a global one, then it could be a venue for information leaks between file contexts, which should never happen in DRM.
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_gem.c | 35 +++++ drivers/gpu/drm/panthor/panthor_gem.h | 2 + drivers/gpu/drm/panthor/panthor_mmu.c | 192 ++++++++++++++++++++++---- include/uapi/drm/panthor_drm.h | 12 ++ 4 files changed, 215 insertions(+), 26 deletions(-)drivers/gpu/drm/panthor/panthor_gem.c +8
-0@@ -1345,6 +1345,41 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,return ERR_PTR (ret); } +/** +* panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. +* @ptdev: Device. +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_gem_object * +panthor_dummy_bo_create (struct panthor_device *ptdev)drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 1365
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gem.c +10
-0+{ +u32 dummy_flags =DRM_PANTHOR_BO_NO_MMAP ; +struct panthor_gem_object *bo; +struct page **pages; + + bo =panthor_gem_create (&ptdev->base,SZ_2M , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + + pages =drm_gem_get_pages (&bo->base);Why not use
panthor_gem_backing_get_pages_locked()here? Also,drm_gem_get_pages()doesn't give any guarantee that you'll get a huge page, nor can you guarantee that the 2M won't be reclaimed and later on be re-allocated as 4k chunks. I'd probably keep things simple for now, and
Quoted 2 lines
- keep it a 2M GEM object
- force the page allocation at map time, just like we do for regular BOs
On a second thought, if I force page allocation at map time, then there's no need to wait until a sparse vm_bind operation to create the dummy BO. I can just do it at VM creation time and then let the vm_bind ioctl path allocate the pages.
Quoted 492 lines
Quoted 38 lines
drivers/gpu/drm/panthor/panthor_gem.c +17
-0+if (PTR_ERR (pages) == -ENOMEM ) { +drm_gem_object_put (&bo->base); + bo =panthor_gem_create (&ptdev->base,SZ_4K , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + pages =drm_gem_get_pages (&bo->base); + } + +if (IS_ERR_OR_NULL (pages)) { +drm_gem_object_put (&bo->base); +return ERR_CAST (pages); + } + + bo->backing.pages = pages; +return bo; +} +static bool can_swap (void ) {return get_nr_swap_pages () >0 ;drivers/gpu/drm/panthor/panthor_gem.h +2
-0@@ -264,6 +264,8 @@ void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *lint panthor_gem_sync (struct drm_gem_object *obj,u32 type,u64 offset,u64 size); +struct panthor_gem_object *panthor_dummy_bo_create (struct panthor_device *ptdev); +struct drm_gem_object *panthor_gem_prime_import (struct drm_device *dev,struct dma_buf *dma_buf);drivers/gpu/drm/panthor/panthor_mmu.c +0
-0drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_mmu { · line 116
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -112,6 +112,23 @@ struct panthor_mmu {struct panthor_vm_pool {/** @xa: Array used for VM handle tracking. */ struct xarray xa; + +/** @dummy: Dummy drm object related fields /** * @dummy: Dummy drm object related fields.Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +15
-0+* +* Sparse bindings map virtual address ranges onto a dummy +* BO in a modulo fashion. Even though sparse writes are meant +* to be discarded and reads undefined, writes are still reflected +* in the dummy buffer. That means we must keep a dummy object per +* file context, to avoid data leaks between them. +* +*/ +struct { +/** @dummy.obj: Dummy object used for sparse mappings. */ +struct panthor_gem_object *obj; + +/** @dummy.lock: Lock protecting against races on dummy object. */ +struct mutex lock; + } dummy; };/** drivers/gpu/drm/panthor/panthor_mmu.c struct panthor_vm { · line 412
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-0@@ -391,6 +408,15 @@ struct panthor_vm {*/ struct list_head lru_node; } reclaim; + +/** @dummy: Dummy object used for sparse mappings. /** * @dummy: Dummy object used for sparse mappings.Quoted 11 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-0+* +* VM's must keep a reference to the file context-wide dummy BO because +* they can outlive the file context, which includes the VM pool holding +* the original dummy BO reference. +* +*/ +struct panthor_gem_object *dummy; };/** drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1053
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-0@@ -1020,6 +1046,46 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,return 0 ; } +static int +panthor_vm_map_sparse (struct panthor_vm *vm,u64 iova,int prot, +struct sg_table *sgt,u64 size) +{ +u64 first_iova = iova;s/first_iova/orig_iova/
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_mmu.c +22
-0+u64 first_size = size; +int ret; + +if (iova & (SZ_2M -1 )) { +u64 unaligned_size =min (ALIGN (iova,SZ_2M ) - iova, size); + + ret =panthor_vm_map_pages (vm, iova, prot, sgt, +0 , unaligned_size); +if (ret) +return ret; + + size -= unaligned_size; + iova += unaligned_size; + } + +/* TODO: we should probably optimize this at the io_pgtable level. */ +while (size >0 ) { +u64 next_size =min (size,sg_dma_len (sgt->sgl)); + + ret =panthor_vm_map_pages (vm, iova, prot, + sgt,0 , next_size); +if (ret)drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 1085
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ goto err_unmap; + + size -= next_size; + iova += next_size; + } + +return 0 ; + +err_unmap: +panthor_vm_unmap_pages (vm, first_iova, first_size - size);If you do:
panthor_vm_unmap_pages(vm, orig_iova, iova - orig_iova);you can get rid of the
first_sizevariable.Quoted 19 lines
drivers/gpu/drm/panthor/panthor_mmu.c +12
-0+return ret; +} +static int flags_to_prot (u32 flags) {int prot =0 ;@@ -1258,38 +1324,71 @@ static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx)return 0 ; } +static struct panthor_gem_object * +panthor_vm_get_dummy_obj (struct panthor_vm_pool *pool, +struct panthor_vm *vm) +{ + scoped_guard(mutex, &pool->dummy.lock) { +if (!vm->dummy) { +if (!pool->dummy.obj) { +struct panthor_gem_object *obj = +panthor_dummy_bo_create (vm->ptdev);drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1345
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+if (IS_ERR (obj)) +return obj; + + pool->dummy.obj = obj; + } + +drm_gem_object_get (&pool->dummy.obj->base); + vm->dummy = pool->dummy.obj; + } + }The lock is taken for the whole function scope, you you can simply use guard(mutex)() and get rid of two indentation levels:
guard(mutex)(&pool->dummy.lock); if (vm->dummy) return vm->dummy; if (!pool->dummy.obj) { struct panthor_gem_object *obj; obj = panthor_dummy_bo_create(vm->ptdev); if (IS_ERR(obj)) return obj; pool->dummy.obj = obj; } drm_gem_object_get(&pool->dummy.obj->base); vm->dummy = pool->dummy.obj; return vm->dummy;drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1358
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0+ +return vm->dummy; +} +#define PANTHOR_VM_BIND_OP_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE | \ DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, + struct panthor_vm_pool *pool,Can't we just make sure
vm->dummyis allocated beforepanthor_vm_prepare_map_op_ctx()is called in case this is a sparse map request? This would prevent the conditional check on pool != NULL, but only when is_sparse=true, and you wouldn't have to pass the pool around.Quoted 16 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-3struct panthor_vm *vm, struct panthor_gem_object *bo, const struct drm_panthor_vm_bind_op *op) { + bool is_sparse = op->flags & DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE;struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ;int ret; -if (!bo) -return -EINVAL ; -if ((op->flags & ~PANTHOR_VM_BIND_OP_MAP_FLAGS ) || (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) !=DRM_PANTHOR_VM_BIND_OP_TYPE_MAP )return -EINVAL ;drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1380
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-1/* Make sure the VA and size are in-bounds. */ -if (op->size > bo->base.size || op->bo_offset > bo->base.size - op->size) +if (bo && (is_sparse || op->size > bo->base.size || + op->bo_offset > bo->base.size - op->size))return -EINVAL ; +else if (is_sparse && (!pool || op->bo_handle || op->bo_offset)) +return -EINVAL ; + +if (is_sparse) { + bo =panthor_vm_get_dummy_obj (pool, vm);Actually, you assign bo here, so you might as well just pass the dummy BO to
panthor_vm_prepare_map_op_ctx()and keep theif (!bo) return -EINVAL;check.
As a side note, if gpuva.gem.obj != NULL for sparse mappings, it messes up with the
can_mergechecks done by gpuvm, which is not a problem right now because we simply ignore the .keep hint passed tounmap_op, but that's probably worth a comment somewhere.drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_vm_op_ctx_prealloc_pts(struct panthor_vm_op_ctx *op_ctx) · line 1391
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-1+if (IS_ERR_OR_NULL (bo)) +return PTR_ERR (bo); + }/* If the BO has an exclusive VM attached, it can't be mapped to other VMs. */ if (bo->exclusive_vm_root_gem && bo->exclusive_vm_root_gem !=panthor_vm_root_gem (vm))return -EINVAL ; -panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags); +panthor_vm_init_op_ctx (op_ctx, op->size, op->va, op->flags + | ((is_sparse) ?DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC :0 ));I would actually enforce NOEXEC is set and return EINVAL if that's not the case.
drivers/gpu/drm/panthor/panthor_mmu.c void panthor_vm_pool_destroy(struct panthor_file *pfile) · line 1741
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +6
-0ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)@@ -1634,6 +1733,13 @@ void panthor_vm_pool_destroy(struct panthor_file *pfile)xa_for_each (&pfile->vms->xa, i, vm)panthor_vm_destroy (vm); + scoped_guard(mutex, &pfile->vms->dummy.lock) { +struct panthor_gem_object *bo = pfile->vms->dummy.obj; + +if (bo) +drm_gem_object_put (&bo->base); + }Missing
mutex_destroy(&pfile->vms->dummy.lock);Quoted 30 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-1+xa_destroy (&pfile->vms->xa);kfree (pfile->vms); }@@ -1651,6 +1757,8 @@ int panthor_vm_pool_create(struct panthor_file *pfile)return -ENOMEM ;xa_init_flags (&pfile->vms->xa,XA_FLAGS_ALLOC1 ); + +mutex_init (&pfile->vms->dummy.lock);return 0 ; }@@ -1987,6 +2095,9 @@ static void panthor_vm_free(struct drm_gpuvm *gpuvm)free_io_pgtable_ops (vm->pgtbl_ops); +if (vm->dummy) +drm_gem_object_put (&vm->dummy->base); +drm_mm_takedown (&vm->mm);kfree (vm); }@@ -2146,7 +2257,26 @@ static void panthor_vma_init(struct panthor_vma *vma, u32 flags)#define PANTHOR_VM_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \ - DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED) + DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED | \ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE)drivers/gpu/drm/panthor/panthor_mmu.c static void panthor_vma_init(struct panthor_vma *vma, u32 flags) · line 2271
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ +static int +panthor_vm_exec_map_op (struct panthor_vm *vm,u32 flags, +const struct drm_gpuva_op_map *op) +{ +struct panthor_gem_object *bo =to_panthor_bo (op->gem.obj); +int prot =flags_to_prot (flags); + +if (!op->va.range) +return 0 ;Do we really expect a range of zero here? If not, I'd either drop the check, or at the very least, make it a
drm_WARN_ON_ONCE().Quoted 26 lines
drivers/gpu/drm/panthor/panthor_mmu.c +9
-3+ +if (flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ) +return panthor_vm_map_sparse (vm, op->va.addr, prot, + bo->dmap.sgt, op->va.range); + +return panthor_vm_map_pages (vm, op->va.addr, prot, bo->dmap.sgt, + op->gem.offset, op->va.range); +}static int panthor_gpuva_sm_step_map (struct drm_gpuva_op *op,void *priv) {@@ -2160,9 +2290,7 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv)panthor_vma_init (vma, op_ctx->flags &PANTHOR_VM_MAP_FLAGS ); - ret =panthor_vm_map_pages (vm, op->map.va.addr,flags_to_prot (vma->flags), - op_ctx->map.bo->dmap.sgt, op->map.gem.offset, - op->map.va.range); + ret =panthor_vm_exec_map_op (vm, vma->flags, &op->map);if (ret) {panthor_vm_op_ctx_return_vma (op_ctx, vma);return ret;@@ -2178,13 +2306,15 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) } static booldrivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_map(struct drm_gpuva_op *op, void *priv) · line 2317
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-1-iova_mapped_as_huge_page(struct drm_gpuva_op_map *op, u64 addr) +iova_mapped_as_huge_page(struct drm_gpuva_op_map *op, u64 addr, bool is_sparse) { struct panthor_gem_object *bo = to_panthor_bo(op->gem.obj);const struct page *pg;pgoff_t bo_offset; bo_offset = addr - op->va.addr + op->gem.offset; +if (is_sparse) + bo_offset %= bo->base.size;If this is a sparse mapping, we just have to check the first page (so bo_offset=0).
Quoted 34 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-2pg = bo->backing.pages[bo_offset >>PAGE_SHIFT ];return folio_size (page_folio (pg)) >=SZ_2M ;@@ -2194,6 +2324,8 @@ static void unmap_hugepage_align(const struct drm_gpuva_op_remap *op, u64 *unmap_start, u64 *unmap_range) { + struct panthor_vma *unmap_vma = container_of(op->unmap->va, struct panthor_vma, base); +bool is_sparse = unmap_vma->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE ;u64 aligned_unmap_start, aligned_unmap_end, unmap_end; unmap_end = *unmap_start + *unmap_range;@@ -2205,7 +2337,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->prev && aligned_unmap_start < *unmap_start && op->prev->va.addr <= aligned_unmap_start && -iova_mapped_as_huge_page (op->prev, *unmap_start)) { + (iova_mapped_as_huge_page (op->prev, *unmap_start, is_sparse))) { *unmap_range += *unmap_start - aligned_unmap_start; *unmap_start = aligned_unmap_start; }@@ -2215,7 +2347,7 @@ unmap_hugepage_align(const struct drm_gpuva_op_remap *op,*/ if (op->next && aligned_unmap_end > unmap_end && op->next->va.addr + op->next->va.range >= aligned_unmap_end && -iova_mapped_as_huge_page (op->next, unmap_end -1 )) { + (iova_mapped_as_huge_page (op->next, *unmap_start, is_sparse))) { *unmap_range += aligned_unmap_end - unmap_end; } }@@ -2251,14 +2383,17 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }if (op->remap.prev) {drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2392
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +7
-3-struct panthor_gem_object *bo =to_panthor_bo (op->remap.prev->gem.obj); -u64 offset = op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr; -u64 size = op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start; +const struct drm_gpuva_op_map map_op = { + .va.addr = unmap_start, + .va.range = + op->remap.prev->va.addr + op->remap.prev->va.range - unmap_start, + .gem.obj = op->remap.prev->gem.obj, + .gem.offset = + op->remap.prev->gem.offset + unmap_start - op->remap.prev->va.addr,I believe it should be forced to zero if this is a sparse mapping, no? This makes me think we probably want this to be NULL, in the case of a sparse mapping. It shouldn't prevent reclaim from happening on the dummy BO, because the
drm_gpuvahas a separatevm_bofield. Yes it forces us to add bunch ofis_sparsechecks in a few other places, but I find it cleaner than pretending this is a regular BO.Quoted 12 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-3+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, unmap_start, -flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, offset, size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2269,14 +2404,15 @@ static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, }drivers/gpu/drm/panthor/panthor_mmu.c static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, · line 2411
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +5
-3if (op->remap.next) { -struct panthor_gem_object *bo =to_panthor_bo (op->remap.next->gem.obj); -u64 addr = op->remap.next->va.addr; -u64 size = unmap_start + unmap_range - op->remap.next->va.addr; +const struct drm_gpuva_op_map map_op = { + .va.addr = op->remap.next->va.addr, + .va.range = unmap_start + unmap_range - op->remap.next->va.addr, + .gem.obj = op->remap.next->gem.obj, + .gem.offset = op->remap.next->gem.offset,Same here, I'd rather have gem.obj=NULL and gem.offset=0 when remapping a porting of sparse mapping.
Quoted 47 lines
drivers/gpu/drm/panthor/panthor_mmu.c +8
-5+ };if (!unmap_vma->evicted) { - ret =panthor_vm_map_pages (vm, addr,flags_to_prot (unmap_vma->flags), - bo->dmap.sgt, op->remap.next->gem.offset, - size); + ret =panthor_vm_exec_map_op (vm, unmap_vma->flags, &map_op);if (ret)return ret; }@@ -2826,6 +2962,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,const struct drm_panthor_vm_bind_op *op, struct panthor_vm_op_ctx *op_ctx) { + struct panthor_file *pfile = file->driver_priv;ssize_t vm_pgsz =panthor_vm_page_size (vm);struct drm_gem_object *gem;int ret;@@ -2837,7 +2974,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP : gem =drm_gem_object_lookup (file, op->bo_handle); - ret =panthor_vm_prepare_map_op_ctx (op_ctx, vm, + ret =panthor_vm_prepare_map_op_ctx (op_ctx, pfile->vms, vm, gem ?to_panthor_bo (gem) :NULL , op);drm_gem_object_put (gem);@@ -3044,7 +3181,10 @@ int panthor_vm_map_bo_range(struct panthor_vm *vm, struct panthor_gem_object *bostruct panthor_vm_op_ctx op_ctx;int ret; - ret =panthor_vm_prepare_map_op_ctx (&op_ctx, vm, bo, &op); +if (drm_WARN_ON (&vm->ptdev->base, flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) +return -EINVAL ; + + ret =panthor_vm_prepare_map_op_ctx (&op_ctx,NULL , vm, bo, &op);if (ret)return ret;include/uapi/drm/panthor_drm.h +0
-0@@ -614,6 +614,18 @@ enum drm_panthor_vm_bind_op_flags {*/ DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED = 1 << 2,
Quoted 15 lines
Quoted 5 lines
include/uapi/drm/panthor_drm.h +5
-0+/** +* @DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE: Repeat a BO range +* +* Only valid with DRM_PANTHOR_VM_BIND_OP_TYPE_MAP. +* include/uapi/drm/panthor_drm.h enum drm_panthor_vm_bind_op_flags { · line 631
Quoted 10 lines
include/uapi/drm/panthor_drm.h +7
-0+* When this flag is set, the whole vm_bind range is mapped over a dummy object in a cyclic +* fashion, and all GPU reads from addresses in the range return undefined values. This flag +* being set means drm_panthor_vm_bind_op:offset and drm_panthor_vm_bind_op::handle must +* both be set to 0. +*/ + DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE = 1 << 3, +/** * @DRM_PANTHOR_VM_BIND_OP_TYPE_MASK: Mask used to determine the type of operation. */
Adrian Larumbe
Boris Brezillon
maintainerRe: [PATCH v7 5/6] drm/panthor: Support sparse mappings
Quoted 25 lines · On Thu, 16 Apr 2026 19:25:01 +0100 Adrián Larumbe…
Quoted 25 lines
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_gem.c +8
-0+/** +* panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. +* @ptdev: Device. +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_gem_object * +panthor_dummy_bo_create (struct panthor_device *ptdev)drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 1365
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gem.c +10
-0+{ +u32 dummy_flags =DRM_PANTHOR_BO_NO_MMAP ; +struct panthor_gem_object *bo; +struct page **pages; + + bo =panthor_gem_create (&ptdev->base,SZ_2M , dummy_flags,NULL ,0 ); +if (IS_ERR_OR_NULL (bo)) +return bo; + + pages =drm_gem_get_pages (&bo->base);Why not use
panthor_gem_backing_get_pages_locked()here? Also,drm_gem_get_pages()doesn't give any guarantee that you'll get a huge page, nor can you guarantee that the 2M won't be reclaimed and later on be re-allocated as 4k chunks. I'd probably keep things simple for now, and
Quoted 6 lines
Quoted 2 lines
- keep it a 2M GEM object
- force the page allocation at map time, just like we do for regular BOsOn a second thought, if I force page allocation at map time, then there's no need to wait until a sparse
vm_bindoperation to create the dummy BO. I can just do it at VM creation time and then let thevm_bindioctl path allocate the pages.
Yep, you can do that, indeed.
PATCH v7 6/6 drm/panthor: Bump the driver version to 1.9
In reply to Adrián LarumbeBump the driver version to reflect the new MMU info query ioctl parameter and the VM_BIND map sparse flag.
--- drivers/gpu/drm/panthor/panthor_drv.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpu/drm/panthor/panthor_drv.c +2 -0
@@ -1787,6 +1787,8 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl * - adds drm_panthor_gpu_info::selected_coherency * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags +* - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query +* - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
Signature · 1 line
2.53.0
Re: [PATCH v7 6/6] drm/panthor: Bump the driver version to 1.9
Quoted 2 lines · Adrián Larumbe
Bump the driver version to reflect the new MMU info query ioctl parameter and the
VM_BINDmap sparse flag.
You're not actually bumping the version, just adding to the comment.
Does the version actually work for you for checking stuff? Do you have userspace checking it? Should it be some capability thing instead?
BR,
Jani.
Quoted 19 lines
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_drv.c | 2 ++ 1 file changed, 2 insertions(+)drivers/gpu/drm/panthor/panthor_drv.c +2
-0@@ -1787,6 +1787,8 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl * - adds drm_panthor_gpu_info::selected_coherency * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags +* - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query +* - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
Signature · 1 line
Jani Nikula, Intel
Re: [PATCH v7 6/6] drm/panthor: Bump the driver version to 1.9
Hi Jani,
Quoted 5 lines · Jani Nikula
Quoted 2 lines · Adrián Larumbe
Bump the driver version to reflect the new MMU info query ioctl parameter and the
VM_BINDmap sparse flag.You're not actually bumping the version, just adding to the comment.
Forgot to increase the driver's minor revision number, thanks for the catch.
Quoted 2 lines
Does the version actually work for you for checking stuff? Do you have userspace checking it? Should it be some capability thing instead?
We absolutely need it because the UMD must be compatible with previous kernel versions, which means in the case of sparse mappings, the old method of creating the dummy BO in user mode and then having the Mesa driver do repeated mappings over it through successive VM_BIND ioctl's must be available.
Quoted 9 lines
BR,
Jani.Quoted 6 lines
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_drv.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 16 lines
drivers/gpu/drm/panthor/panthor_drv.c static void panthor_debugfs_init(struct drm_minor *minor) · line 1794
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_drv.c +2
-0@@ -1787,6 +1787,8 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl * - adds drm_panthor_gpu_info::selected_coherency * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags +* - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query +* - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |--
Jani Nikula, Intel
Adrian Larumbe
Re: [PATCH v7 6/6] drm/panthor: Bump the driver version to 1.9
Quoted 11 lines · Adrián Larumbe
Hi Jani,
Quoted 5 lines · Jani Nikula
Quoted 2 lines · Adrián Larumbe
Bump the driver version to reflect the new MMU info query ioctl parameter and the
VM_BINDmap sparse flag.You're not actually bumping the version, just adding to the comment.
Forgot to increase the driver's minor revision number, thanks for the catch.
Quoted 7 lines
Quoted 2 lines
Does the version actually work for you for checking stuff? Do you have userspace checking it? Should it be some capability thing instead?
We absolutely need it because the UMD must be compatible with previous kernel versions, which means in the case of sparse mappings, the old method of creating the dummy BO in user mode and then having the Mesa driver do repeated mappings over it through successive
VM_BINDioctl's must be available.
Okay, I was just wondering as the last time we bumped i915 version was nearly 20 years ago. ;D
BR,
Jani.
Quoted 28 lines
Quoted 10 lines
BR,
Jani.Quoted 6 lines
Signed-off-by Adrián Larumbe <[email protected]>--- drivers/gpu/drm/panthor/panthor_drv.c | 2 ++ 1 file changed, 2 insertions(+)Quoted 16 lines
drivers/gpu/drm/panthor/panthor_drv.c static void panthor_debugfs_init(struct drm_minor *minor) · line 1794
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_drv.c +2
-0@@ -1787,6 +1787,8 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl * - adds drm_panthor_gpu_info::selected_coherency * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags +* - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query +* - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |--
Jani Nikula, IntelAdrian Larumbe
Signature · 1 line
Jani Nikula, Intel
Boris Brezillon
maintainerRe: [PATCH v7 6/6] drm/panthor: Bump the driver version to 1.9
In reply to Jani NikulaQuoted 6 lines · On Wed, 15 Apr 2026 16:54:06 +0300 Jani Nikula <[email protected]>…
Quoted 2 lines · Adrián Larumbe
Bump the driver version to reflect the new MMU info query ioctl parameter and the
VM_BINDmap sparse flag.Does the version actually work for you for checking stuff? Do you have userspace checking it? Should it be some capability thing instead?
Yep, we use versions to check for everything that's not a HW feature/capability (so basically all SW features).
Re: [PATCH v7 6/6] drm/panthor: Bump the driver version to 1.9
In reply to Adrián LarumbeQuoted 4 lines · On Wed, 15 Apr 2026 12:28:50 +0100 Adrián Larumbe…
Bump the driver version to reflect the new MMU info query ioctl parameter and the
VM_BINDmap sparse flag.Signed-off-by Adrián Larumbe <[email protected]>
Quoted 17 lines
--- drivers/gpu/drm/panthor/panthor_drv.c | 2 ++ 1 file changed, 2 insertions(+)drivers/gpu/drm/panthor/panthor_drv.c +2
-0@@ -1787,6 +1787,8 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl * - adds drm_panthor_gpu_info::selected_coherency * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags +* - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query +* - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
Boris Brezillon
maintainerRe: [PATCH v7 6/6] drm/panthor: Bump the driver version to 1.9
Quoted 9 lines · On Wed, 15 Apr 2026 17:22:33 +0200 Boris Brezillon…
Quoted 4 lines · On Wed, 15 Apr 2026 12:28:50 +0100 Adrián Larumbe…
Bump the driver version to reflect the new MMU info query ioctl parameter and the
VM_BINDmap sparse flag.Signed-off-by Adrián Larumbe <[email protected]>Reviewed-by Boris Brezillon <[email protected]>
After actually bumping the version number, as pointed out by Jani.
Quoted 19 lines
Quoted 18 lines
--- drivers/gpu/drm/panthor/panthor_drv.c | 2 ++ 1 file changed, 2 insertions(+)drivers/gpu/drm/panthor/panthor_drv.c +2
-0@@ -1787,6 +1787,8 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl * - adds drm_panthor_gpu_info::selected_coherency * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags +* - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query +* - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |