[PATCH v4 11/14] accel/thames: Install memory mappings populated and dirty
From: Tomeu Vizoso
Date: Sun Nov 29 2026 - 06:38:13 EST
The GEM shmem helper maps by page fault, one at each page's first touch
and another at its first write, which its write-notify needs. The driver
installs the pages of a page-backed buffer itself with
remap_pfn_range(), writable and dirty: the A53 in the J722S has no
hardware dirty-bit management, so a clean page would still fault on its
first write. Contiguous buffers keep the GEM DMA helper's mapping.
This saves those faults in the first invoke of a model.
Signed-off-by: Tomeu Vizoso <tomeu@xxxxxxxxxxxxxxx>
---
v4:
- Install only the pages of page-backed buffers; contiguous buffers keep
the GEM DMA helper's mapping.
---
drivers/accel/thames/thames_gem.c | 48 +++++++++++++++++++++++++++++++++++++--
1 file changed, 46 insertions(+), 2 deletions(-)
diff --git a/drivers/accel/thames/thames_gem.c b/drivers/accel/thames/thames_gem.c
index 3c2846bb5297..930444a732e7 100644
--- a/drivers/accel/thames/thames_gem.c
+++ b/drivers/accel/thames/thames_gem.c
@@ -834,6 +834,50 @@ static void thames_gem_shmem_bo_free(struct drm_gem_object *obj)
drm_gem_shmem_free(&bo->shmem);
}
+/*
+ * Every page installed now, dirty and writable: they are pinned for the
+ * object's life, and the helper would take two faults on each.
+ */
+static int thames_gem_shmem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
+{
+ struct thames_gem_object *bo = to_thames_bo(obj);
+ unsigned long addr = vma->vm_start;
+ struct scatterlist *sg;
+ pgprot_t prot;
+ unsigned int i;
+ int ret;
+
+ if (vma_is_cow_mapping(vma))
+ return -EINVAL;
+
+ vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
+
+ prot = vm_get_page_prot(vma->vm_flags);
+ if (vma->vm_flags & VM_WRITE)
+ prot = pte_pgprot(pte_mkdirty(pte_mkwrite_novma(pfn_pte(0, prot))));
+ vma->vm_page_prot = prot;
+
+ for_each_sgtable_sg(bo->shmem.sgt, sg, i) {
+ unsigned long len = min_t(unsigned long, sg->length, vma->vm_end - addr);
+
+ ret = remap_pfn_range(vma, addr, page_to_pfn(sg_page(sg)), len, prot);
+ if (ret)
+ return ret;
+
+ addr += len;
+ if (addr == vma->vm_end)
+ break;
+ }
+
+ return 0;
+}
+
+/* Only the GEM reference: the pages are pinned for the object's life. */
+static const struct vm_operations_struct thames_gem_shmem_vm_ops = {
+ .open = drm_gem_vm_open,
+ .close = drm_gem_vm_close,
+};
+
static const struct drm_gem_object_funcs thames_gem_funcs = {
.free = thames_gem_bo_free,
.close = thames_gem_close,
@@ -853,8 +897,8 @@ static const struct drm_gem_object_funcs thames_gem_shmem_funcs = {
.get_sg_table = drm_gem_shmem_object_get_sg_table,
.vmap = drm_gem_shmem_object_vmap,
.vunmap = drm_gem_shmem_object_vunmap,
- .mmap = drm_gem_shmem_object_mmap,
- .vm_ops = &drm_gem_shmem_vm_ops,
+ .mmap = thames_gem_shmem_mmap,
+ .vm_ops = &thames_gem_shmem_vm_ops,
};
int thames_ioctl_bo_create(struct drm_device *ddev, void *data, struct drm_file *file)
--
2.54.0