Commit 645e9541 authored by Thomas Zimmermann's avatar Thomas Zimmermann

drm/radeon: Implement mmap as GEM object function

Moving the driver-specific mmap code into a GEM object function allows
for using DRM helpers for various mmap callbacks.

This change also allows to support prime-based mmap via DRM's helper
drm_gem_prime_mmap().

Permission checks are implemented by drm_gem_mmap(), with an additional
check for radeon_ttm_tt_has_userptr() in the GEM object function. The
function radeon_verify_access() is now unused and has thus been removed.

As a side effect, radeon_ttm_vm_ops and radeon_ttm_fault() are now
implemented in amdgpu's GEM code.

v3:
	* remove unnecessary checks from mmap (Christian)
v2:
	* rename radeon_ttm_vm_ops and radeon_ttm_fault() to
	  radeon_gem_vm_ops and radeon_gem_fault() (Christian)
	* fix commit description (Alex)
Signed-off-by: default avatarThomas Zimmermann <tzimmermann@suse.de>
Reviewed-by: default avatarChristian König <christian.koenig@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20210525151055.8174-4-tzimmermann@suse.de
parent 71df0368
...@@ -545,7 +545,7 @@ static const struct file_operations radeon_driver_kms_fops = { ...@@ -545,7 +545,7 @@ static const struct file_operations radeon_driver_kms_fops = {
.open = drm_open, .open = drm_open,
.release = drm_release, .release = drm_release,
.unlocked_ioctl = radeon_drm_ioctl, .unlocked_ioctl = radeon_drm_ioctl,
.mmap = radeon_mmap, .mmap = drm_gem_mmap,
.poll = drm_poll, .poll = drm_poll,
.read = drm_read, .read = drm_read,
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
...@@ -620,6 +620,7 @@ static const struct drm_driver kms_driver = { ...@@ -620,6 +620,7 @@ static const struct drm_driver kms_driver = {
.prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
.prime_fd_to_handle = drm_gem_prime_fd_to_handle, .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
.gem_prime_import_sg_table = radeon_gem_prime_import_sg_table, .gem_prime_import_sg_table = radeon_gem_prime_import_sg_table,
.gem_prime_mmap = drm_gem_prime_mmap,
.name = DRIVER_NAME, .name = DRIVER_NAME,
.desc = DRIVER_DESC, .desc = DRIVER_DESC,
......
...@@ -44,6 +44,42 @@ void radeon_gem_prime_unpin(struct drm_gem_object *obj); ...@@ -44,6 +44,42 @@ void radeon_gem_prime_unpin(struct drm_gem_object *obj);
const struct drm_gem_object_funcs radeon_gem_object_funcs; const struct drm_gem_object_funcs radeon_gem_object_funcs;
static vm_fault_t radeon_gem_fault(struct vm_fault *vmf)
{
struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
vm_fault_t ret;
down_read(&rdev->pm.mclk_lock);
ret = ttm_bo_vm_reserve(bo, vmf);
if (ret)
goto unlock_mclk;
ret = radeon_bo_fault_reserve_notify(bo);
if (ret)
goto unlock_resv;
ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
TTM_BO_VM_NUM_PREFAULT, 1);
if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
goto unlock_mclk;
unlock_resv:
dma_resv_unlock(bo->base.resv);
unlock_mclk:
up_read(&rdev->pm.mclk_lock);
return ret;
}
static const struct vm_operations_struct radeon_gem_vm_ops = {
.fault = radeon_gem_fault,
.open = ttm_bo_vm_open,
.close = ttm_bo_vm_close,
.access = ttm_bo_vm_access
};
static void radeon_gem_object_free(struct drm_gem_object *gobj) static void radeon_gem_object_free(struct drm_gem_object *gobj)
{ {
struct radeon_bo *robj = gem_to_radeon_bo(gobj); struct radeon_bo *robj = gem_to_radeon_bo(gobj);
...@@ -226,6 +262,17 @@ static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r) ...@@ -226,6 +262,17 @@ static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
return r; return r;
} }
static int radeon_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
{
struct radeon_bo *bo = gem_to_radeon_bo(obj);
struct radeon_device *rdev = radeon_get_rdev(bo->tbo.bdev);
if (radeon_ttm_tt_has_userptr(rdev, bo->tbo.ttm))
return -EPERM;
return drm_gem_ttm_mmap(obj, vma);
}
const struct drm_gem_object_funcs radeon_gem_object_funcs = { const struct drm_gem_object_funcs radeon_gem_object_funcs = {
.free = radeon_gem_object_free, .free = radeon_gem_object_free,
.open = radeon_gem_object_open, .open = radeon_gem_object_open,
...@@ -236,6 +283,8 @@ const struct drm_gem_object_funcs radeon_gem_object_funcs = { ...@@ -236,6 +283,8 @@ const struct drm_gem_object_funcs radeon_gem_object_funcs = {
.get_sg_table = radeon_gem_prime_get_sg_table, .get_sg_table = radeon_gem_prime_get_sg_table,
.vmap = drm_gem_ttm_vmap, .vmap = drm_gem_ttm_vmap,
.vunmap = drm_gem_ttm_vunmap, .vunmap = drm_gem_ttm_vunmap,
.mmap = radeon_gem_object_mmap,
.vm_ops = &radeon_gem_vm_ops,
}; };
/* /*
......
...@@ -135,17 +135,6 @@ static void radeon_evict_flags(struct ttm_buffer_object *bo, ...@@ -135,17 +135,6 @@ static void radeon_evict_flags(struct ttm_buffer_object *bo,
*placement = rbo->placement; *placement = rbo->placement;
} }
static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp)
{
struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
if (radeon_ttm_tt_has_userptr(rdev, bo->ttm))
return -EPERM;
return drm_vma_node_verify_access(&rbo->tbo.base.vma_node,
filp->private_data);
}
static int radeon_move_blit(struct ttm_buffer_object *bo, static int radeon_move_blit(struct ttm_buffer_object *bo,
bool evict, bool evict,
struct ttm_resource *new_mem, struct ttm_resource *new_mem,
...@@ -703,7 +692,6 @@ static struct ttm_device_funcs radeon_bo_driver = { ...@@ -703,7 +692,6 @@ static struct ttm_device_funcs radeon_bo_driver = {
.eviction_valuable = ttm_bo_eviction_valuable, .eviction_valuable = ttm_bo_eviction_valuable,
.evict_flags = &radeon_evict_flags, .evict_flags = &radeon_evict_flags,
.move = &radeon_bo_move, .move = &radeon_bo_move,
.verify_access = &radeon_verify_access,
.delete_mem_notify = &radeon_bo_delete_mem_notify, .delete_mem_notify = &radeon_bo_delete_mem_notify,
.io_mem_reserve = &radeon_ttm_io_mem_reserve, .io_mem_reserve = &radeon_ttm_io_mem_reserve,
}; };
...@@ -800,59 +788,6 @@ void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size) ...@@ -800,59 +788,6 @@ void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size)
man->size = size >> PAGE_SHIFT; man->size = size >> PAGE_SHIFT;
} }
static vm_fault_t radeon_ttm_fault(struct vm_fault *vmf)
{
struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
vm_fault_t ret;
down_read(&rdev->pm.mclk_lock);
ret = ttm_bo_vm_reserve(bo, vmf);
if (ret)
goto unlock_mclk;
ret = radeon_bo_fault_reserve_notify(bo);
if (ret)
goto unlock_resv;
ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
TTM_BO_VM_NUM_PREFAULT, 1);
if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
goto unlock_mclk;
unlock_resv:
dma_resv_unlock(bo->base.resv);
unlock_mclk:
up_read(&rdev->pm.mclk_lock);
return ret;
}
static const struct vm_operations_struct radeon_ttm_vm_ops = {
.fault = radeon_ttm_fault,
.open = ttm_bo_vm_open,
.close = ttm_bo_vm_close,
.access = ttm_bo_vm_access
};
int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
{
int r;
struct drm_file *file_priv = filp->private_data;
struct radeon_device *rdev = file_priv->minor->dev->dev_private;
if (rdev == NULL)
return -EINVAL;
r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
if (unlikely(r != 0))
return r;
vma->vm_ops = &radeon_ttm_vm_ops;
return 0;
}
#if defined(CONFIG_DEBUG_FS) #if defined(CONFIG_DEBUG_FS)
static int radeon_mm_vram_dump_table_show(struct seq_file *m, void *unused) static int radeon_mm_vram_dump_table_show(struct seq_file *m, void *unused)
......
...@@ -32,6 +32,5 @@ struct radeon_device; ...@@ -32,6 +32,5 @@ struct radeon_device;
int radeon_ttm_init(struct radeon_device *rdev); int radeon_ttm_init(struct radeon_device *rdev);
void radeon_ttm_fini(struct radeon_device *rdev); void radeon_ttm_fini(struct radeon_device *rdev);
int radeon_mmap(struct file *filp, struct vm_area_struct *vma);
#endif /* __RADEON_TTM_H__ */ #endif /* __RADEON_TTM_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment