drm/amdgpu: Add gem_prime_mmap support
authorSamuel Li <Samuel.Li@amd.com>
Tue, 22 Aug 2017 19:25:33 +0000 (15:25 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Tue, 26 Sep 2017 19:14:37 +0000 (15:14 -0400)
v2: drop hdp invalidate/flush.
v3: honor pgoff during prime mmap. Add a barrier after cpu access.
v4: drop begin/end_cpu_access() for now, revisit later.

Signed-off-by: Samuel Li <Samuel.Li@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu.h
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c

index 7c43add4e44447e6f1ee2eb77e2c56658640e61b..ebfc267467eeba6aa1c8b8157d3a46c65d09e8e0 100644 (file)
@@ -413,6 +413,7 @@ void amdgpu_gem_prime_unpin(struct drm_gem_object *obj);
 struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *);
 void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj);
 void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
+int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
 int amdgpu_gem_debugfs_init(struct amdgpu_device *adev);
 
 /* sub-allocation manager, it has to be protected by another lock.
index 91e42b60d66b589170dc2c3de6cf43e007e06cb9..4f98960e47f9a20adba00c2854629994f8d7b51e 100644 (file)
@@ -855,6 +855,7 @@ static struct drm_driver kms_driver = {
        .gem_prime_import_sg_table = amdgpu_gem_prime_import_sg_table,
        .gem_prime_vmap = amdgpu_gem_prime_vmap,
        .gem_prime_vunmap = amdgpu_gem_prime_vunmap,
+       .gem_prime_mmap = amdgpu_gem_prime_mmap,
 
        .name = DRIVER_NAME,
        .desc = DRIVER_DESC,
index 7e0826469b5e8009e6aebc93e0e3d4d3934b318e..90af8e82b16af3f97990b7e71bb672de4b6d0b72 100644 (file)
@@ -57,6 +57,40 @@ void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
        ttm_bo_kunmap(&bo->dma_buf_vmap);
 }
 
+int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
+{
+       struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+       struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
+       unsigned asize = amdgpu_bo_size(bo);
+       int ret;
+
+       if (!vma->vm_file)
+               return -ENODEV;
+
+       if (adev == NULL)
+               return -ENODEV;
+
+       /* Check for valid size. */
+       if (asize < vma->vm_end - vma->vm_start)
+               return -EINVAL;
+
+       if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
+           (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
+               return -EPERM;
+       }
+       vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
+
+       /* prime mmap does not need to check access, so allow here */
+       ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
+       if (ret)
+               return ret;
+
+       ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
+       drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
+
+       return ret;
+}
+
 struct drm_gem_object *
 amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
                                 struct dma_buf_attachment *attach,