drm/vc4: Move mmap implementation into GEM object function
authorThomas Zimmermann <tzimmermann@suse.de>
Fri, 8 Jan 2021 14:08:08 +0000 (15:08 +0100)
committerThomas Zimmermann <tzimmermann@suse.de>
Wed, 13 Jan 2021 11:52:33 +0000 (12:52 +0100)
Moving vc4's mmap code from vc4_mmap() into a GEM object function
allows for the use drm_gem_mmap() and drm_gem_prime_mmap(). The content
of vc4_drm_fpos can then be generated by DEFINE_DRM_GEM_FOPS().

The actual mmap implementation is just a check if the BO is a validated
shader plus the default CMA mmap code.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Acked-by: Maxime Ripard <mripard@kernel.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20210108140808.25775-4-tzimmermann@suse.de
drivers/gpu/drm/vc4/vc4_bo.c
drivers/gpu/drm/vc4/vc4_drv.c
drivers/gpu/drm/vc4/vc4_drv.h

index f9b42ff098e3f63b19ce47cc7412d4bb38e1290c..28e48ef2d2950e507dffd85cddef241bddae15ba 100644 (file)
@@ -704,19 +704,9 @@ static vm_fault_t vc4_fault(struct vm_fault *vmf)
        return VM_FAULT_SIGBUS;
 }
 
-int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
+static int vc4_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
 {
-       struct drm_gem_object *gem_obj;
-       unsigned long vm_pgoff;
-       struct vc4_bo *bo;
-       int ret;
-
-       ret = drm_gem_mmap(filp, vma);
-       if (ret)
-               return ret;
-
-       gem_obj = vma->vm_private_data;
-       bo = to_vc4_bo(gem_obj);
+       struct vc4_bo *bo = to_vc4_bo(obj);
 
        if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
                DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
@@ -730,45 +720,7 @@ int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
                return -EINVAL;
        }
 
-       /*
-        * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
-        * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
-        * the whole buffer.
-        */
-       vma->vm_flags &= ~VM_PFNMAP;
-
-       /* This ->vm_pgoff dance is needed to make all parties happy:
-        * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
-        *   mem-region, hence the need to set it to zero (the value set by
-        *   the DRM core is a virtual offset encoding the GEM object-id)
-        * - the mmap() core logic needs ->vm_pgoff to be restored to its
-        *   initial value before returning from this function because it
-        *   encodes the  offset of this GEM in the dev->anon_inode pseudo-file
-        *   and this information will be used when we invalidate userspace
-        *   mappings  with drm_vma_node_unmap() (called from vc4_gem_purge()).
-        */
-       vm_pgoff = vma->vm_pgoff;
-       vma->vm_pgoff = 0;
-       ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
-                         bo->base.paddr, vma->vm_end - vma->vm_start);
-       vma->vm_pgoff = vm_pgoff;
-
-       if (ret)
-               drm_gem_vm_close(vma);
-
-       return ret;
-}
-
-int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
-{
-       struct vc4_bo *bo = to_vc4_bo(obj);
-
-       if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
-               DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
-               return -EINVAL;
-       }
-
-       return drm_gem_prime_mmap(obj, vma);
+       return drm_gem_cma_mmap(obj, vma);
 }
 
 static const struct vm_operations_struct vc4_vm_ops = {
@@ -782,6 +734,7 @@ static const struct drm_gem_object_funcs vc4_gem_object_funcs = {
        .export = vc4_prime_export,
        .get_sg_table = drm_gem_cma_get_sg_table,
        .vmap = drm_gem_cma_vmap,
+       .mmap = vc4_gem_object_mmap,
        .vm_ops = &vc4_vm_ops,
 };
 
index 2cd97a39c28688ff2ca203c2605a5d8ac53ce769..d9b3bba7f2b7e21bc0d86aa597a34eb4481d695a 100644 (file)
@@ -140,17 +140,7 @@ static void vc4_close(struct drm_device *dev, struct drm_file *file)
        kfree(vc4file);
 }
 
-static const struct file_operations vc4_drm_fops = {
-       .owner = THIS_MODULE,
-       .open = drm_open,
-       .release = drm_release,
-       .unlocked_ioctl = drm_ioctl,
-       .mmap = vc4_mmap,
-       .poll = drm_poll,
-       .read = drm_read,
-       .compat_ioctl = drm_compat_ioctl,
-       .llseek = noop_llseek,
-};
+DEFINE_DRM_GEM_FOPS(vc4_drm_fops);
 
 static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
        DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, DRM_RENDER_ALLOW),
@@ -193,7 +183,7 @@ static struct drm_driver vc4_drm_driver = {
        .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
        .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
        .gem_prime_import_sg_table = vc4_prime_import_sg_table,
-       .gem_prime_mmap = vc4_prime_mmap,
+       .gem_prime_mmap = drm_gem_prime_mmap,
 
        .dumb_create = vc4_dumb_create,
 
index a3d8d87fe3550f7f13ba13780d6678a225720127..0d9c0ecc4769f47623809ee4a0be32fb80028799 100644 (file)
@@ -801,8 +801,6 @@ int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
                             struct drm_file *file_priv);
 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
                       struct drm_file *file_priv);
-int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
-int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
 struct drm_gem_object *vc4_prime_import_sg_table(struct drm_device *dev,
                                                 struct dma_buf_attachment *attach,
                                                 struct sg_table *sgt);