xref: /openbsd/sys/dev/pci/drm/amd/amdgpu/amdgpu_gem.c (revision f005ef32)
1fb4d8502Sjsg /*
2fb4d8502Sjsg  * Copyright 2008 Advanced Micro Devices, Inc.
3fb4d8502Sjsg  * Copyright 2008 Red Hat Inc.
4fb4d8502Sjsg  * Copyright 2009 Jerome Glisse.
5fb4d8502Sjsg  *
6fb4d8502Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
7fb4d8502Sjsg  * copy of this software and associated documentation files (the "Software"),
8fb4d8502Sjsg  * to deal in the Software without restriction, including without limitation
9fb4d8502Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10fb4d8502Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
11fb4d8502Sjsg  * Software is furnished to do so, subject to the following conditions:
12fb4d8502Sjsg  *
13fb4d8502Sjsg  * The above copyright notice and this permission notice shall be included in
14fb4d8502Sjsg  * all copies or substantial portions of the Software.
15fb4d8502Sjsg  *
16fb4d8502Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17fb4d8502Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18fb4d8502Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19fb4d8502Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20fb4d8502Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21fb4d8502Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22fb4d8502Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
23fb4d8502Sjsg  *
24fb4d8502Sjsg  * Authors: Dave Airlie
25fb4d8502Sjsg  *          Alex Deucher
26fb4d8502Sjsg  *          Jerome Glisse
27fb4d8502Sjsg  */
28fb4d8502Sjsg #include <linux/ktime.h>
29c349dbc7Sjsg #include <linux/module.h>
30fb4d8502Sjsg #include <linux/pagemap.h>
31c349dbc7Sjsg #include <linux/pci.h>
32ad8b1aafSjsg #include <linux/dma-buf.h>
33c349dbc7Sjsg 
34fb4d8502Sjsg #include <drm/amdgpu_drm.h>
355ca02815Sjsg #include <drm/drm_drv.h>
36*f005ef32Sjsg #include <drm/drm_exec.h>
375ca02815Sjsg #include <drm/drm_gem_ttm_helper.h>
38*f005ef32Sjsg #include <drm/ttm/ttm_tt.h>
39c349dbc7Sjsg 
40fb4d8502Sjsg #include "amdgpu.h"
41fb4d8502Sjsg #include "amdgpu_display.h"
425ca02815Sjsg #include "amdgpu_dma_buf.h"
43*f005ef32Sjsg #include "amdgpu_hmm.h"
44c349dbc7Sjsg #include "amdgpu_xgmi.h"
45fb4d8502Sjsg 
465ca02815Sjsg static const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
475ca02815Sjsg 
485ca02815Sjsg #ifdef __linux__
amdgpu_gem_fault(struct vm_fault * vmf)495ca02815Sjsg static vm_fault_t amdgpu_gem_fault(struct vm_fault *vmf)
505ca02815Sjsg {
515ca02815Sjsg 	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
525ca02815Sjsg 	struct drm_device *ddev = bo->base.dev;
535ca02815Sjsg 	vm_fault_t ret;
545ca02815Sjsg 	int idx;
555ca02815Sjsg 
565ca02815Sjsg 	ret = ttm_bo_vm_reserve(bo, vmf);
575ca02815Sjsg 	if (ret)
585ca02815Sjsg 		return ret;
595ca02815Sjsg 
605ca02815Sjsg 	if (drm_dev_enter(ddev, &idx)) {
615ca02815Sjsg 		ret = amdgpu_bo_fault_reserve_notify(bo);
625ca02815Sjsg 		if (ret) {
635ca02815Sjsg 			drm_dev_exit(idx);
645ca02815Sjsg 			goto unlock;
655ca02815Sjsg 		}
665ca02815Sjsg 
675ca02815Sjsg 		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
685ca02815Sjsg 					       TTM_BO_VM_NUM_PREFAULT);
695ca02815Sjsg 
705ca02815Sjsg 		drm_dev_exit(idx);
715ca02815Sjsg 	} else {
725ca02815Sjsg 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
735ca02815Sjsg 	}
745ca02815Sjsg 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
755ca02815Sjsg 		return ret;
765ca02815Sjsg 
775ca02815Sjsg unlock:
785ca02815Sjsg 	dma_resv_unlock(bo->base.resv);
795ca02815Sjsg 	return ret;
805ca02815Sjsg }
815ca02815Sjsg 
825ca02815Sjsg static const struct vm_operations_struct amdgpu_gem_vm_ops = {
835ca02815Sjsg 	.fault = amdgpu_gem_fault,
845ca02815Sjsg 	.open = ttm_bo_vm_open,
855ca02815Sjsg 	.close = ttm_bo_vm_close,
865ca02815Sjsg 	.access = ttm_bo_vm_access
875ca02815Sjsg };
885ca02815Sjsg #else /* !__linux__ */
895ca02815Sjsg int
amdgpu_gem_fault(struct uvm_faultinfo * ufi,vaddr_t vaddr,vm_page_t * pps,int npages,int centeridx,vm_fault_t fault_type,vm_prot_t access_type,int flags)905ca02815Sjsg amdgpu_gem_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, vm_page_t *pps,
915ca02815Sjsg     int npages, int centeridx, vm_fault_t fault_type,
925ca02815Sjsg     vm_prot_t access_type, int flags)
935ca02815Sjsg {
945ca02815Sjsg 	struct uvm_object *uobj = ufi->entry->object.uvm_obj;
955ca02815Sjsg 	struct ttm_buffer_object *bo = (struct ttm_buffer_object *)uobj;
965ca02815Sjsg 	struct drm_device *ddev = bo->base.dev;
975ca02815Sjsg 	vm_fault_t ret;
985ca02815Sjsg 	int idx;
995ca02815Sjsg 
1005ca02815Sjsg 	ret = ttm_bo_vm_reserve(bo);
1015ca02815Sjsg 	if (ret) {
1025ca02815Sjsg 		switch (ret) {
1035ca02815Sjsg 		case VM_FAULT_NOPAGE:
1045ca02815Sjsg 			ret = VM_PAGER_OK;
1055ca02815Sjsg 			break;
1065ca02815Sjsg 		case VM_FAULT_RETRY:
1075ca02815Sjsg 			ret = VM_PAGER_REFAULT;
1085ca02815Sjsg 			break;
1095ca02815Sjsg 		default:
1105ca02815Sjsg 			ret = VM_PAGER_BAD;
1115ca02815Sjsg 			break;
1125ca02815Sjsg 		}
1135ca02815Sjsg 		uvmfault_unlockall(ufi, NULL, uobj);
1145ca02815Sjsg 		return ret;
1155ca02815Sjsg 	}
1165ca02815Sjsg 
1175ca02815Sjsg 	if (drm_dev_enter(ddev, &idx)) {
1185ca02815Sjsg 		ret = amdgpu_bo_fault_reserve_notify(bo);
1195ca02815Sjsg 		if (ret) {
1205ca02815Sjsg 			drm_dev_exit(idx);
1215ca02815Sjsg 			goto unlock;
1225ca02815Sjsg 		}
1235ca02815Sjsg 
1245ca02815Sjsg 		 ret = ttm_bo_vm_fault_reserved(ufi, vaddr,
1255ca02815Sjsg 						TTM_BO_VM_NUM_PREFAULT, 1);
1265ca02815Sjsg 
1275ca02815Sjsg 		 drm_dev_exit(idx);
1285ca02815Sjsg 	} else {
1295ca02815Sjsg 		STUB();
1305ca02815Sjsg #ifdef notyet
1315ca02815Sjsg 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1325ca02815Sjsg #endif
1335ca02815Sjsg 	}
1345ca02815Sjsg #ifdef __linux__
1355ca02815Sjsg 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1365ca02815Sjsg 		return ret;
1375ca02815Sjsg #endif
1385ca02815Sjsg 
1395ca02815Sjsg unlock:
1405ca02815Sjsg 	switch (ret) {
1415ca02815Sjsg 	case VM_FAULT_NOPAGE:
1425ca02815Sjsg 		ret = VM_PAGER_OK;
1435ca02815Sjsg 		break;
1445ca02815Sjsg 	case VM_FAULT_RETRY:
1455ca02815Sjsg 		ret = VM_PAGER_REFAULT;
1465ca02815Sjsg 		break;
1475ca02815Sjsg 	default:
1485ca02815Sjsg 		ret = VM_PAGER_BAD;
1495ca02815Sjsg 		break;
1505ca02815Sjsg 	}
1515ca02815Sjsg 	dma_resv_unlock(bo->base.resv);
1525ca02815Sjsg 	uvmfault_unlockall(ufi, NULL, uobj);
1535ca02815Sjsg 	return ret;
1545ca02815Sjsg }
1555ca02815Sjsg 
1565ca02815Sjsg void
amdgpu_gem_vm_reference(struct uvm_object * uobj)1575ca02815Sjsg amdgpu_gem_vm_reference(struct uvm_object *uobj)
1585ca02815Sjsg {
1595ca02815Sjsg 	struct ttm_buffer_object *bo = (struct ttm_buffer_object *)uobj;
1605ca02815Sjsg 
1615ca02815Sjsg 	ttm_bo_get(bo);
1625ca02815Sjsg }
1635ca02815Sjsg 
1645ca02815Sjsg void
amdgpu_gem_vm_detach(struct uvm_object * uobj)1655ca02815Sjsg amdgpu_gem_vm_detach(struct uvm_object *uobj)
1665ca02815Sjsg {
1675ca02815Sjsg 	struct ttm_buffer_object *bo = (struct ttm_buffer_object *)uobj;
1685ca02815Sjsg 
1695ca02815Sjsg 	ttm_bo_put(bo);
1705ca02815Sjsg }
1715ca02815Sjsg 
1725ca02815Sjsg static const struct uvm_pagerops amdgpu_gem_vm_ops = {
1735ca02815Sjsg 	.pgo_fault = amdgpu_gem_fault,
1745ca02815Sjsg 	.pgo_reference = amdgpu_gem_vm_reference,
1755ca02815Sjsg 	.pgo_detach = amdgpu_gem_vm_detach
1765ca02815Sjsg };
1775ca02815Sjsg #endif /* !__linux__ */
1785ca02815Sjsg 
amdgpu_gem_object_free(struct drm_gem_object * gobj)1795ca02815Sjsg static void amdgpu_gem_object_free(struct drm_gem_object *gobj)
180fb4d8502Sjsg {
181fb4d8502Sjsg 	struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
182fb4d8502Sjsg 
183fb4d8502Sjsg 	if (robj) {
184*f005ef32Sjsg 		amdgpu_hmm_unregister(robj);
185fb4d8502Sjsg 		amdgpu_bo_unref(&robj);
186fb4d8502Sjsg 	}
187fb4d8502Sjsg }
188fb4d8502Sjsg 
amdgpu_gem_object_create(struct amdgpu_device * adev,unsigned long size,int alignment,u32 initial_domain,u64 flags,enum ttm_bo_type type,struct dma_resv * resv,struct drm_gem_object ** obj,int8_t xcp_id_plus1)189fb4d8502Sjsg int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
190fb4d8502Sjsg 			     int alignment, u32 initial_domain,
191fb4d8502Sjsg 			     u64 flags, enum ttm_bo_type type,
192c349dbc7Sjsg 			     struct dma_resv *resv,
193*f005ef32Sjsg 			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
194fb4d8502Sjsg {
195fb4d8502Sjsg 	struct amdgpu_bo *bo;
1965ca02815Sjsg 	struct amdgpu_bo_user *ubo;
197fb4d8502Sjsg 	struct amdgpu_bo_param bp;
198fb4d8502Sjsg 	int r;
199fb4d8502Sjsg 
200fb4d8502Sjsg 	memset(&bp, 0, sizeof(bp));
201fb4d8502Sjsg 	*obj = NULL;
202fb4d8502Sjsg 
203fb4d8502Sjsg 	bp.size = size;
204fb4d8502Sjsg 	bp.byte_align = alignment;
205fb4d8502Sjsg 	bp.type = type;
206fb4d8502Sjsg 	bp.resv = resv;
207fb4d8502Sjsg 	bp.preferred_domain = initial_domain;
208fb4d8502Sjsg 	bp.flags = flags;
209fb4d8502Sjsg 	bp.domain = initial_domain;
2105ca02815Sjsg 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
211*f005ef32Sjsg 	bp.xcp_id_plus1 = xcp_id_plus1;
2125ca02815Sjsg 
2135ca02815Sjsg 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
214ad8b1aafSjsg 	if (r)
215fb4d8502Sjsg 		return r;
216ad8b1aafSjsg 
2175ca02815Sjsg 	bo = &ubo->bo;
218c349dbc7Sjsg 	*obj = &bo->tbo.base;
2195ca02815Sjsg 	(*obj)->funcs = &amdgpu_gem_object_funcs;
220fb4d8502Sjsg 
221fb4d8502Sjsg 	return 0;
222fb4d8502Sjsg }
223fb4d8502Sjsg 
22464094178Sjsg int	drm_file_cmp(struct drm_file *, struct drm_file *);
22564094178Sjsg SPLAY_PROTOTYPE(drm_file_tree, drm_file, link, drm_file_cmp);
22664094178Sjsg 
amdgpu_gem_force_release(struct amdgpu_device * adev)227fb4d8502Sjsg void amdgpu_gem_force_release(struct amdgpu_device *adev)
228fb4d8502Sjsg {
229ad8b1aafSjsg 	struct drm_device *ddev = adev_to_drm(adev);
230fb4d8502Sjsg 	struct drm_file *file;
231fb4d8502Sjsg 
232fb4d8502Sjsg 	mutex_lock(&ddev->filelist_mutex);
233fb4d8502Sjsg 
2345ca02815Sjsg #ifdef __linux__
2355ca02815Sjsg 	list_for_each_entry(file, &ddev->filelist, lhead) {
2365ca02815Sjsg #else
23764094178Sjsg 	SPLAY_FOREACH(file, drm_file_tree, &ddev->files) {
2385ca02815Sjsg #endif
239fb4d8502Sjsg 		struct drm_gem_object *gobj;
240fb4d8502Sjsg 		int handle;
241fb4d8502Sjsg 
242fb4d8502Sjsg 		WARN_ONCE(1, "Still active user space clients!\n");
243fb4d8502Sjsg 		spin_lock(&file->table_lock);
244fb4d8502Sjsg 		idr_for_each_entry(&file->object_idr, gobj, handle) {
245fb4d8502Sjsg 			WARN_ONCE(1, "And also active allocations!\n");
246ad8b1aafSjsg 			drm_gem_object_put(gobj);
247fb4d8502Sjsg 		}
248fb4d8502Sjsg 		idr_destroy(&file->object_idr);
249fb4d8502Sjsg 		spin_unlock(&file->table_lock);
250fb4d8502Sjsg 	}
251fb4d8502Sjsg 
252fb4d8502Sjsg 	mutex_unlock(&ddev->filelist_mutex);
253fb4d8502Sjsg }
254fb4d8502Sjsg 
255fb4d8502Sjsg /*
256fb4d8502Sjsg  * Call from drm_gem_handle_create which appear in both new and open ioctl
257fb4d8502Sjsg  * case.
258fb4d8502Sjsg  */
2595ca02815Sjsg static int amdgpu_gem_object_open(struct drm_gem_object *obj,
260fb4d8502Sjsg 				  struct drm_file *file_priv)
261fb4d8502Sjsg {
262fb4d8502Sjsg 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
263fb4d8502Sjsg 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
264fb4d8502Sjsg 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
265fb4d8502Sjsg 	struct amdgpu_vm *vm = &fpriv->vm;
266fb4d8502Sjsg 	struct amdgpu_bo_va *bo_va;
267fb4d8502Sjsg #ifdef notyet
268fb4d8502Sjsg 	struct mm_struct *mm;
269fb4d8502Sjsg #endif
270fb4d8502Sjsg 	int r;
271fb4d8502Sjsg 
272fb4d8502Sjsg #ifdef notyet
273fb4d8502Sjsg 	mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
274fb4d8502Sjsg 	if (mm && mm != current->mm)
275fb4d8502Sjsg 		return -EPERM;
276fb4d8502Sjsg #endif
277fb4d8502Sjsg 
278fb4d8502Sjsg 	if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
2795ca02815Sjsg 	    abo->tbo.base.resv != vm->root.bo->tbo.base.resv)
280fb4d8502Sjsg 		return -EPERM;
281fb4d8502Sjsg 
282fb4d8502Sjsg 	r = amdgpu_bo_reserve(abo, false);
283fb4d8502Sjsg 	if (r)
284fb4d8502Sjsg 		return r;
285fb4d8502Sjsg 
286fb4d8502Sjsg 	bo_va = amdgpu_vm_bo_find(vm, abo);
287*f005ef32Sjsg 	if (!bo_va)
288fb4d8502Sjsg 		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
289*f005ef32Sjsg 	else
290fb4d8502Sjsg 		++bo_va->ref_count;
291fb4d8502Sjsg 	amdgpu_bo_unreserve(abo);
292fb4d8502Sjsg 	return 0;
293fb4d8502Sjsg }
294fb4d8502Sjsg 
2955ca02815Sjsg static void amdgpu_gem_object_close(struct drm_gem_object *obj,
296fb4d8502Sjsg 				    struct drm_file *file_priv)
297fb4d8502Sjsg {
298fb4d8502Sjsg 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
299fb4d8502Sjsg 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
300fb4d8502Sjsg 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
301fb4d8502Sjsg 	struct amdgpu_vm *vm = &fpriv->vm;
302fb4d8502Sjsg 
303ede47e28Sjsg 	struct dma_fence *fence = NULL;
304fb4d8502Sjsg 	struct amdgpu_bo_va *bo_va;
305*f005ef32Sjsg 	struct drm_exec exec;
306ede47e28Sjsg 	long r;
307fb4d8502Sjsg 
308*f005ef32Sjsg 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES);
309*f005ef32Sjsg 	drm_exec_until_all_locked(&exec) {
310*f005ef32Sjsg 		r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
311*f005ef32Sjsg 		drm_exec_retry_on_contention(&exec);
312*f005ef32Sjsg 		if (unlikely(r))
313*f005ef32Sjsg 			goto out_unlock;
314fb4d8502Sjsg 
315*f005ef32Sjsg 		r = amdgpu_vm_lock_pd(vm, &exec, 0);
316*f005ef32Sjsg 		drm_exec_retry_on_contention(&exec);
317*f005ef32Sjsg 		if (unlikely(r))
318*f005ef32Sjsg 			goto out_unlock;
319fb4d8502Sjsg 	}
320*f005ef32Sjsg 
321fb4d8502Sjsg 	bo_va = amdgpu_vm_bo_find(vm, bo);
322ede47e28Sjsg 	if (!bo_va || --bo_va->ref_count)
323ede47e28Sjsg 		goto out_unlock;
324ede47e28Sjsg 
3251bb76ff1Sjsg 	amdgpu_vm_bo_del(adev, bo_va);
326ede47e28Sjsg 	if (!amdgpu_vm_ready(vm))
327ede47e28Sjsg 		goto out_unlock;
328fb4d8502Sjsg 
329ede47e28Sjsg 	r = amdgpu_vm_clear_freed(adev, vm, &fence);
330*f005ef32Sjsg 	if (unlikely(r < 0))
331*f005ef32Sjsg 		dev_err(adev->dev, "failed to clear page "
332*f005ef32Sjsg 			"tables on GEM object close (%ld)\n", r);
333ede47e28Sjsg 	if (r || !fence)
334ede47e28Sjsg 		goto out_unlock;
335ede47e28Sjsg 
336ede47e28Sjsg 	amdgpu_bo_fence(bo, fence, true);
337fb4d8502Sjsg 	dma_fence_put(fence);
338ede47e28Sjsg 
339ede47e28Sjsg out_unlock:
340*f005ef32Sjsg 	if (r)
341*f005ef32Sjsg 		dev_err(adev->dev, "leaking bo va (%ld)\n", r);
342*f005ef32Sjsg 	drm_exec_fini(&exec);
343fb4d8502Sjsg }
344fb4d8502Sjsg 
3455ca02815Sjsg #ifdef __linux__
3465ca02815Sjsg static int amdgpu_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
3475ca02815Sjsg {
3485ca02815Sjsg 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
3495ca02815Sjsg 
3505ca02815Sjsg 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
3515ca02815Sjsg 		return -EPERM;
3525ca02815Sjsg 	if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
3535ca02815Sjsg 		return -EPERM;
3545ca02815Sjsg 
3555ca02815Sjsg 	/* Workaround for Thunk bug creating PROT_NONE,MAP_PRIVATE mappings
3565ca02815Sjsg 	 * for debugger access to invisible VRAM. Should have used MAP_SHARED
3575ca02815Sjsg 	 * instead. Clearing VM_MAYWRITE prevents the mapping from ever
3585ca02815Sjsg 	 * becoming writable and makes is_cow_mapping(vm_flags) false.
3595ca02815Sjsg 	 */
3605ca02815Sjsg 	if (is_cow_mapping(vma->vm_flags) &&
361*f005ef32Sjsg 	    !(vma->vm_flags & VM_ACCESS_FLAGS))
362*f005ef32Sjsg 		vm_flags_clear(vma, VM_MAYWRITE);
3635ca02815Sjsg 
3645ca02815Sjsg 	return drm_gem_ttm_mmap(obj, vma);
3655ca02815Sjsg }
3665ca02815Sjsg #else
3675ca02815Sjsg static int amdgpu_gem_object_mmap(struct drm_gem_object *obj,
3685ca02815Sjsg     vm_prot_t accessprot, voff_t off, vsize_t size)
3695ca02815Sjsg {
3705ca02815Sjsg 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
3715ca02815Sjsg 
3725ca02815Sjsg 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
3735ca02815Sjsg 		return -EPERM;
3745ca02815Sjsg 	if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
3755ca02815Sjsg 		return -EPERM;
3765ca02815Sjsg 
3775ca02815Sjsg 	/* Workaround for Thunk bug creating PROT_NONE,MAP_PRIVATE mappings
3785ca02815Sjsg 	 * for debugger access to invisible VRAM. Should have used MAP_SHARED
3795ca02815Sjsg 	 * instead. Clearing VM_MAYWRITE prevents the mapping from ever
3805ca02815Sjsg 	 * becoming writable and makes is_cow_mapping(vm_flags) false.
3815ca02815Sjsg 	 */
3825ca02815Sjsg #ifdef notyet
3835ca02815Sjsg 	if (is_cow_mapping(vma->vm_flags) &&
3845ca02815Sjsg 	    !(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
3855ca02815Sjsg 		vma->vm_flags &= ~VM_MAYWRITE;
3865ca02815Sjsg #endif
3875ca02815Sjsg 
3885ca02815Sjsg 	return drm_gem_ttm_mmap(obj, accessprot, off, size);
3895ca02815Sjsg }
3905ca02815Sjsg #endif
3915ca02815Sjsg 
3925ca02815Sjsg static const struct drm_gem_object_funcs amdgpu_gem_object_funcs = {
3935ca02815Sjsg 	.free = amdgpu_gem_object_free,
3945ca02815Sjsg 	.open = amdgpu_gem_object_open,
3955ca02815Sjsg 	.close = amdgpu_gem_object_close,
3965ca02815Sjsg 	.export = amdgpu_gem_prime_export,
3975ca02815Sjsg 	.vmap = drm_gem_ttm_vmap,
3985ca02815Sjsg 	.vunmap = drm_gem_ttm_vunmap,
3995ca02815Sjsg 	.mmap = amdgpu_gem_object_mmap,
4005ca02815Sjsg 	.vm_ops = &amdgpu_gem_vm_ops,
4015ca02815Sjsg };
4025ca02815Sjsg 
403fb4d8502Sjsg /*
404fb4d8502Sjsg  * GEM ioctls.
405fb4d8502Sjsg  */
406fb4d8502Sjsg int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
407fb4d8502Sjsg 			    struct drm_file *filp)
408fb4d8502Sjsg {
409ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
410fb4d8502Sjsg 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
411fb4d8502Sjsg 	struct amdgpu_vm *vm = &fpriv->vm;
412fb4d8502Sjsg 	union drm_amdgpu_gem_create *args = data;
413fb4d8502Sjsg 	uint64_t flags = args->in.domain_flags;
414fb4d8502Sjsg 	uint64_t size = args->in.bo_size;
415c349dbc7Sjsg 	struct dma_resv *resv = NULL;
416fb4d8502Sjsg 	struct drm_gem_object *gobj;
417ad8b1aafSjsg 	uint32_t handle, initial_domain;
418fb4d8502Sjsg 	int r;
419fb4d8502Sjsg 
420*f005ef32Sjsg 	/* reject DOORBELLs until userspace code to use it is available */
421*f005ef32Sjsg 	if (args->in.domains & AMDGPU_GEM_DOMAIN_DOORBELL)
422*f005ef32Sjsg 		return -EINVAL;
423*f005ef32Sjsg 
424fb4d8502Sjsg 	/* reject invalid gem flags */
425fb4d8502Sjsg 	if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
426fb4d8502Sjsg 		      AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
427fb4d8502Sjsg 		      AMDGPU_GEM_CREATE_CPU_GTT_USWC |
428fb4d8502Sjsg 		      AMDGPU_GEM_CREATE_VRAM_CLEARED |
429fb4d8502Sjsg 		      AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
430ad8b1aafSjsg 		      AMDGPU_GEM_CREATE_EXPLICIT_SYNC |
4311bb76ff1Sjsg 		      AMDGPU_GEM_CREATE_ENCRYPTED |
4321bb76ff1Sjsg 		      AMDGPU_GEM_CREATE_DISCARDABLE))
433fb4d8502Sjsg 		return -EINVAL;
434fb4d8502Sjsg 
435fb4d8502Sjsg 	/* reject invalid gem domains */
436fb4d8502Sjsg 	if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
437fb4d8502Sjsg 		return -EINVAL;
438fb4d8502Sjsg 
439ad8b1aafSjsg 	if (!amdgpu_is_tmz(adev) && (flags & AMDGPU_GEM_CREATE_ENCRYPTED)) {
440ad8b1aafSjsg 		DRM_NOTE_ONCE("Cannot allocate secure buffer since TMZ is disabled\n");
441ad8b1aafSjsg 		return -EINVAL;
442ad8b1aafSjsg 	}
443ad8b1aafSjsg 
444fb4d8502Sjsg 	/* create a gem object to contain this object in */
445fb4d8502Sjsg 	if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
446fb4d8502Sjsg 	    AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
447fb4d8502Sjsg 		if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
448fb4d8502Sjsg 			/* if gds bo is created from user space, it must be
449fb4d8502Sjsg 			 * passed to bo list
450fb4d8502Sjsg 			 */
451fb4d8502Sjsg 			DRM_ERROR("GDS bo cannot be per-vm-bo\n");
452fb4d8502Sjsg 			return -EINVAL;
453fb4d8502Sjsg 		}
454fb4d8502Sjsg 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
455fb4d8502Sjsg 	}
456fb4d8502Sjsg 
457fb4d8502Sjsg 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
4585ca02815Sjsg 		r = amdgpu_bo_reserve(vm->root.bo, false);
459fb4d8502Sjsg 		if (r)
460fb4d8502Sjsg 			return r;
461fb4d8502Sjsg 
4625ca02815Sjsg 		resv = vm->root.bo->tbo.base.resv;
463fb4d8502Sjsg 	}
464fb4d8502Sjsg 
465ad8b1aafSjsg 	initial_domain = (u32)(0xffffffff & args->in.domains);
4665ca02815Sjsg retry:
467fb4d8502Sjsg 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
468ad8b1aafSjsg 				     initial_domain,
469*f005ef32Sjsg 				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
4705ca02815Sjsg 	if (r && r != -ERESTARTSYS) {
471ad8b1aafSjsg 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
472ad8b1aafSjsg 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
473ad8b1aafSjsg 			goto retry;
474ad8b1aafSjsg 		}
475ad8b1aafSjsg 
476ad8b1aafSjsg 		if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
477ad8b1aafSjsg 			initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
478ad8b1aafSjsg 			goto retry;
479ad8b1aafSjsg 		}
480ad8b1aafSjsg 		DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n",
481ad8b1aafSjsg 				size, initial_domain, args->in.alignment, r);
482ad8b1aafSjsg 	}
483ad8b1aafSjsg 
484fb4d8502Sjsg 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
485fb4d8502Sjsg 		if (!r) {
486fb4d8502Sjsg 			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
487fb4d8502Sjsg 
4885ca02815Sjsg 			abo->parent = amdgpu_bo_ref(vm->root.bo);
489fb4d8502Sjsg 		}
4905ca02815Sjsg 		amdgpu_bo_unreserve(vm->root.bo);
491fb4d8502Sjsg 	}
492fb4d8502Sjsg 	if (r)
493fb4d8502Sjsg 		return r;
494fb4d8502Sjsg 
495fb4d8502Sjsg 	r = drm_gem_handle_create(filp, gobj, &handle);
496fb4d8502Sjsg 	/* drop reference from allocate - handle holds it now */
497ad8b1aafSjsg 	drm_gem_object_put(gobj);
498fb4d8502Sjsg 	if (r)
499fb4d8502Sjsg 		return r;
500fb4d8502Sjsg 
501fb4d8502Sjsg 	memset(args, 0, sizeof(*args));
502fb4d8502Sjsg 	args->out.handle = handle;
503fb4d8502Sjsg 	return 0;
504fb4d8502Sjsg }
505fb4d8502Sjsg 
506fb4d8502Sjsg int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
507fb4d8502Sjsg 			     struct drm_file *filp)
508fb4d8502Sjsg {
509fb4d8502Sjsg 	return -ENOSYS;
510c349dbc7Sjsg #ifdef notyet
511fb4d8502Sjsg 	struct ttm_operation_ctx ctx = { true, false };
512ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
513fb4d8502Sjsg 	struct drm_amdgpu_gem_userptr *args = data;
514*f005ef32Sjsg 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
515fb4d8502Sjsg 	struct drm_gem_object *gobj;
5161bb76ff1Sjsg 	struct hmm_range *range;
517fb4d8502Sjsg 	struct amdgpu_bo *bo;
518fb4d8502Sjsg 	uint32_t handle;
519fb4d8502Sjsg 	int r;
520fb4d8502Sjsg 
521c349dbc7Sjsg 	args->addr = untagged_addr(args->addr);
522c349dbc7Sjsg 
523fb4d8502Sjsg 	if (offset_in_page(args->addr | args->size))
524fb4d8502Sjsg 		return -EINVAL;
525fb4d8502Sjsg 
526fb4d8502Sjsg 	/* reject unknown flag values */
527fb4d8502Sjsg 	if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
528fb4d8502Sjsg 	    AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
529fb4d8502Sjsg 	    AMDGPU_GEM_USERPTR_REGISTER))
530fb4d8502Sjsg 		return -EINVAL;
531fb4d8502Sjsg 
532fb4d8502Sjsg 	if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
533fb4d8502Sjsg 	     !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
534fb4d8502Sjsg 
535fb4d8502Sjsg 		/* if we want to write to it we must install a MMU notifier */
536fb4d8502Sjsg 		return -EACCES;
537fb4d8502Sjsg 	}
538fb4d8502Sjsg 
539fb4d8502Sjsg 	/* create a gem object to contain this object in */
540fb4d8502Sjsg 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
541*f005ef32Sjsg 				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
542fb4d8502Sjsg 	if (r)
543fb4d8502Sjsg 		return r;
544fb4d8502Sjsg 
545fb4d8502Sjsg 	bo = gem_to_amdgpu_bo(gobj);
546fb4d8502Sjsg 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
547fb4d8502Sjsg 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
548ad8b1aafSjsg 	r = amdgpu_ttm_tt_set_userptr(&bo->tbo, args->addr, args->flags);
549fb4d8502Sjsg 	if (r)
550fb4d8502Sjsg 		goto release_object;
551fb4d8502Sjsg 
552*f005ef32Sjsg 	r = amdgpu_hmm_register(bo, args->addr);
553fb4d8502Sjsg 	if (r)
554fb4d8502Sjsg 		goto release_object;
555fb4d8502Sjsg 
556fb4d8502Sjsg 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
5571bb76ff1Sjsg 		r = amdgpu_ttm_tt_get_user_pages(bo, bo->tbo.ttm->pages,
5581bb76ff1Sjsg 						 &range);
559fb4d8502Sjsg 		if (r)
560fb4d8502Sjsg 			goto release_object;
561fb4d8502Sjsg 
562fb4d8502Sjsg 		r = amdgpu_bo_reserve(bo, true);
563fb4d8502Sjsg 		if (r)
564c349dbc7Sjsg 			goto user_pages_done;
565fb4d8502Sjsg 
566fb4d8502Sjsg 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
567fb4d8502Sjsg 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
568fb4d8502Sjsg 		amdgpu_bo_unreserve(bo);
569fb4d8502Sjsg 		if (r)
570c349dbc7Sjsg 			goto user_pages_done;
571fb4d8502Sjsg 	}
572fb4d8502Sjsg 
573fb4d8502Sjsg 	r = drm_gem_handle_create(filp, gobj, &handle);
574fb4d8502Sjsg 	if (r)
575c349dbc7Sjsg 		goto user_pages_done;
576fb4d8502Sjsg 
577fb4d8502Sjsg 	args->handle = handle;
578fb4d8502Sjsg 
579c349dbc7Sjsg user_pages_done:
580c349dbc7Sjsg 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
5811bb76ff1Sjsg 		amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, range);
582fb4d8502Sjsg 
583fb4d8502Sjsg release_object:
584ad8b1aafSjsg 	drm_gem_object_put(gobj);
585fb4d8502Sjsg 
586fb4d8502Sjsg 	return r;
587fb4d8502Sjsg #endif
588fb4d8502Sjsg }
589fb4d8502Sjsg 
590fb4d8502Sjsg int amdgpu_mode_dumb_mmap(struct drm_file *filp,
591fb4d8502Sjsg 			  struct drm_device *dev,
592fb4d8502Sjsg 			  uint32_t handle, uint64_t *offset_p)
593fb4d8502Sjsg {
594fb4d8502Sjsg 	struct drm_gem_object *gobj;
595fb4d8502Sjsg 	struct amdgpu_bo *robj;
596fb4d8502Sjsg 
597fb4d8502Sjsg 	gobj = drm_gem_object_lookup(filp, handle);
598*f005ef32Sjsg 	if (!gobj)
599fb4d8502Sjsg 		return -ENOENT;
600*f005ef32Sjsg 
601fb4d8502Sjsg 	robj = gem_to_amdgpu_bo(gobj);
602fb4d8502Sjsg 	if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
603fb4d8502Sjsg 	    (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
604ad8b1aafSjsg 		drm_gem_object_put(gobj);
605fb4d8502Sjsg 		return -EPERM;
606fb4d8502Sjsg 	}
607fb4d8502Sjsg 	*offset_p = amdgpu_bo_mmap_offset(robj);
608ad8b1aafSjsg 	drm_gem_object_put(gobj);
609fb4d8502Sjsg 	return 0;
610fb4d8502Sjsg }
611fb4d8502Sjsg 
612fb4d8502Sjsg int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
613fb4d8502Sjsg 			  struct drm_file *filp)
614fb4d8502Sjsg {
615fb4d8502Sjsg 	union drm_amdgpu_gem_mmap *args = data;
616fb4d8502Sjsg 	uint32_t handle = args->in.handle;
617*f005ef32Sjsg 
618fb4d8502Sjsg 	memset(args, 0, sizeof(*args));
619fb4d8502Sjsg 	return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
620fb4d8502Sjsg }
621fb4d8502Sjsg 
622fb4d8502Sjsg /**
623fb4d8502Sjsg  * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
624fb4d8502Sjsg  *
625fb4d8502Sjsg  * @timeout_ns: timeout in ns
626fb4d8502Sjsg  *
627fb4d8502Sjsg  * Calculate the timeout in jiffies from an absolute timeout in ns.
628fb4d8502Sjsg  */
629fb4d8502Sjsg unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
630fb4d8502Sjsg {
631fb4d8502Sjsg 	unsigned long timeout_jiffies;
632fb4d8502Sjsg 	ktime_t timeout;
633fb4d8502Sjsg 
634fb4d8502Sjsg 	/* clamp timeout if it's to large */
635fb4d8502Sjsg 	if (((int64_t)timeout_ns) < 0)
636fb4d8502Sjsg 		return MAX_SCHEDULE_TIMEOUT;
637fb4d8502Sjsg 
638fb4d8502Sjsg 	timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
639fb4d8502Sjsg 	if (ktime_to_ns(timeout) < 0)
640fb4d8502Sjsg 		return 0;
641fb4d8502Sjsg 
642fb4d8502Sjsg 	timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
643fb4d8502Sjsg 	/*  clamp timeout to avoid unsigned-> signed overflow */
644fb4d8502Sjsg 	if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT)
645fb4d8502Sjsg 		return MAX_SCHEDULE_TIMEOUT - 1;
646fb4d8502Sjsg 
647fb4d8502Sjsg 	return timeout_jiffies;
648fb4d8502Sjsg }
649fb4d8502Sjsg 
650fb4d8502Sjsg int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
651fb4d8502Sjsg 			      struct drm_file *filp)
652fb4d8502Sjsg {
653fb4d8502Sjsg 	union drm_amdgpu_gem_wait_idle *args = data;
654fb4d8502Sjsg 	struct drm_gem_object *gobj;
655fb4d8502Sjsg 	struct amdgpu_bo *robj;
656fb4d8502Sjsg 	uint32_t handle = args->in.handle;
657fb4d8502Sjsg 	unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
658fb4d8502Sjsg 	int r = 0;
659fb4d8502Sjsg 	long ret;
660fb4d8502Sjsg 
661fb4d8502Sjsg 	gobj = drm_gem_object_lookup(filp, handle);
662*f005ef32Sjsg 	if (!gobj)
663fb4d8502Sjsg 		return -ENOENT;
664*f005ef32Sjsg 
665fb4d8502Sjsg 	robj = gem_to_amdgpu_bo(gobj);
6661bb76ff1Sjsg 	ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ,
6671bb76ff1Sjsg 				    true, timeout);
668fb4d8502Sjsg 
669fb4d8502Sjsg 	/* ret == 0 means not signaled,
670fb4d8502Sjsg 	 * ret > 0 means signaled
671fb4d8502Sjsg 	 * ret < 0 means interrupted before timeout
672fb4d8502Sjsg 	 */
673fb4d8502Sjsg 	if (ret >= 0) {
674fb4d8502Sjsg 		memset(args, 0, sizeof(*args));
675fb4d8502Sjsg 		args->out.status = (ret == 0);
676fb4d8502Sjsg 	} else
677fb4d8502Sjsg 		r = ret;
678fb4d8502Sjsg 
679ad8b1aafSjsg 	drm_gem_object_put(gobj);
680fb4d8502Sjsg 	return r;
681fb4d8502Sjsg }
682fb4d8502Sjsg 
683fb4d8502Sjsg int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
684fb4d8502Sjsg 				struct drm_file *filp)
685fb4d8502Sjsg {
686fb4d8502Sjsg 	struct drm_amdgpu_gem_metadata *args = data;
687fb4d8502Sjsg 	struct drm_gem_object *gobj;
688fb4d8502Sjsg 	struct amdgpu_bo *robj;
689fb4d8502Sjsg 	int r = -1;
690fb4d8502Sjsg 
691fb4d8502Sjsg 	DRM_DEBUG("%d\n", args->handle);
692fb4d8502Sjsg 	gobj = drm_gem_object_lookup(filp, args->handle);
693fb4d8502Sjsg 	if (gobj == NULL)
694fb4d8502Sjsg 		return -ENOENT;
695fb4d8502Sjsg 	robj = gem_to_amdgpu_bo(gobj);
696fb4d8502Sjsg 
697fb4d8502Sjsg 	r = amdgpu_bo_reserve(robj, false);
698fb4d8502Sjsg 	if (unlikely(r != 0))
699fb4d8502Sjsg 		goto out;
700fb4d8502Sjsg 
701fb4d8502Sjsg 	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
702fb4d8502Sjsg 		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
703fb4d8502Sjsg 		r = amdgpu_bo_get_metadata(robj, args->data.data,
704fb4d8502Sjsg 					   sizeof(args->data.data),
705fb4d8502Sjsg 					   &args->data.data_size_bytes,
706fb4d8502Sjsg 					   &args->data.flags);
707fb4d8502Sjsg 	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
708fb4d8502Sjsg 		if (args->data.data_size_bytes > sizeof(args->data.data)) {
709fb4d8502Sjsg 			r = -EINVAL;
710fb4d8502Sjsg 			goto unreserve;
711fb4d8502Sjsg 		}
712fb4d8502Sjsg 		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
713fb4d8502Sjsg 		if (!r)
714fb4d8502Sjsg 			r = amdgpu_bo_set_metadata(robj, args->data.data,
715fb4d8502Sjsg 						   args->data.data_size_bytes,
716fb4d8502Sjsg 						   args->data.flags);
717fb4d8502Sjsg 	}
718fb4d8502Sjsg 
719fb4d8502Sjsg unreserve:
720fb4d8502Sjsg 	amdgpu_bo_unreserve(robj);
721fb4d8502Sjsg out:
722ad8b1aafSjsg 	drm_gem_object_put(gobj);
723fb4d8502Sjsg 	return r;
724fb4d8502Sjsg }
725fb4d8502Sjsg 
726fb4d8502Sjsg /**
727fb4d8502Sjsg  * amdgpu_gem_va_update_vm -update the bo_va in its VM
728fb4d8502Sjsg  *
729fb4d8502Sjsg  * @adev: amdgpu_device pointer
730fb4d8502Sjsg  * @vm: vm to update
731fb4d8502Sjsg  * @bo_va: bo_va to update
732fb4d8502Sjsg  * @operation: map, unmap or clear
733fb4d8502Sjsg  *
734fb4d8502Sjsg  * Update the bo_va directly after setting its address. Errors are not
735fb4d8502Sjsg  * vital here, so they are not reported back to userspace.
736fb4d8502Sjsg  */
737fb4d8502Sjsg static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
738fb4d8502Sjsg 				    struct amdgpu_vm *vm,
739fb4d8502Sjsg 				    struct amdgpu_bo_va *bo_va,
740fb4d8502Sjsg 				    uint32_t operation)
741fb4d8502Sjsg {
742fb4d8502Sjsg 	int r;
743fb4d8502Sjsg 
744fb4d8502Sjsg 	if (!amdgpu_vm_ready(vm))
745fb4d8502Sjsg 		return;
746fb4d8502Sjsg 
747fb4d8502Sjsg 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
748fb4d8502Sjsg 	if (r)
749fb4d8502Sjsg 		goto error;
750fb4d8502Sjsg 
751fb4d8502Sjsg 	if (operation == AMDGPU_VA_OP_MAP ||
752fb4d8502Sjsg 	    operation == AMDGPU_VA_OP_REPLACE) {
7531bb76ff1Sjsg 		r = amdgpu_vm_bo_update(adev, bo_va, false);
754fb4d8502Sjsg 		if (r)
755fb4d8502Sjsg 			goto error;
756fb4d8502Sjsg 	}
757fb4d8502Sjsg 
758c349dbc7Sjsg 	r = amdgpu_vm_update_pdes(adev, vm, false);
759fb4d8502Sjsg 
760fb4d8502Sjsg error:
761fb4d8502Sjsg 	if (r && r != -ERESTARTSYS)
762fb4d8502Sjsg 		DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
763fb4d8502Sjsg }
764fb4d8502Sjsg 
765c349dbc7Sjsg /**
766c349dbc7Sjsg  * amdgpu_gem_va_map_flags - map GEM UAPI flags into hardware flags
767c349dbc7Sjsg  *
768c349dbc7Sjsg  * @adev: amdgpu_device pointer
769c349dbc7Sjsg  * @flags: GEM UAPI flags
770c349dbc7Sjsg  *
771c349dbc7Sjsg  * Returns the GEM UAPI flags mapped into hardware for the ASIC.
772c349dbc7Sjsg  */
773c349dbc7Sjsg uint64_t amdgpu_gem_va_map_flags(struct amdgpu_device *adev, uint32_t flags)
774c349dbc7Sjsg {
775c349dbc7Sjsg 	uint64_t pte_flag = 0;
776c349dbc7Sjsg 
777c349dbc7Sjsg 	if (flags & AMDGPU_VM_PAGE_EXECUTABLE)
778c349dbc7Sjsg 		pte_flag |= AMDGPU_PTE_EXECUTABLE;
779c349dbc7Sjsg 	if (flags & AMDGPU_VM_PAGE_READABLE)
780c349dbc7Sjsg 		pte_flag |= AMDGPU_PTE_READABLE;
781c349dbc7Sjsg 	if (flags & AMDGPU_VM_PAGE_WRITEABLE)
782c349dbc7Sjsg 		pte_flag |= AMDGPU_PTE_WRITEABLE;
783c349dbc7Sjsg 	if (flags & AMDGPU_VM_PAGE_PRT)
784c349dbc7Sjsg 		pte_flag |= AMDGPU_PTE_PRT;
7851bb76ff1Sjsg 	if (flags & AMDGPU_VM_PAGE_NOALLOC)
7861bb76ff1Sjsg 		pte_flag |= AMDGPU_PTE_NOALLOC;
787c349dbc7Sjsg 
788c349dbc7Sjsg 	if (adev->gmc.gmc_funcs->map_mtype)
789c349dbc7Sjsg 		pte_flag |= amdgpu_gmc_map_mtype(adev,
790c349dbc7Sjsg 						 flags & AMDGPU_VM_MTYPE_MASK);
791c349dbc7Sjsg 
792c349dbc7Sjsg 	return pte_flag;
793c349dbc7Sjsg }
794c349dbc7Sjsg 
795fb4d8502Sjsg int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
796fb4d8502Sjsg 			  struct drm_file *filp)
797fb4d8502Sjsg {
798fb4d8502Sjsg 	const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
799fb4d8502Sjsg 		AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
8001bb76ff1Sjsg 		AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
8011bb76ff1Sjsg 		AMDGPU_VM_PAGE_NOALLOC;
802fb4d8502Sjsg 	const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
803fb4d8502Sjsg 		AMDGPU_VM_PAGE_PRT;
804fb4d8502Sjsg 
805fb4d8502Sjsg 	struct drm_amdgpu_gem_va *args = data;
806fb4d8502Sjsg 	struct drm_gem_object *gobj;
807ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
808fb4d8502Sjsg 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
809fb4d8502Sjsg 	struct amdgpu_bo *abo;
810fb4d8502Sjsg 	struct amdgpu_bo_va *bo_va;
811*f005ef32Sjsg 	struct drm_exec exec;
812fb4d8502Sjsg 	uint64_t va_flags;
813ad8b1aafSjsg 	uint64_t vm_size;
814fb4d8502Sjsg 	int r = 0;
815fb4d8502Sjsg 
816fb4d8502Sjsg 	if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
8175ca02815Sjsg 		dev_dbg(dev->dev,
818*f005ef32Sjsg 			"va_address 0x%llx is in reserved area 0x%llx\n",
819fb4d8502Sjsg 			args->va_address, AMDGPU_VA_RESERVED_SIZE);
820fb4d8502Sjsg 		return -EINVAL;
821fb4d8502Sjsg 	}
822fb4d8502Sjsg 
823c349dbc7Sjsg 	if (args->va_address >= AMDGPU_GMC_HOLE_START &&
824c349dbc7Sjsg 	    args->va_address < AMDGPU_GMC_HOLE_END) {
8255ca02815Sjsg 		dev_dbg(dev->dev,
826*f005ef32Sjsg 			"va_address 0x%llx is in VA hole 0x%llx-0x%llx\n",
827c349dbc7Sjsg 			args->va_address, AMDGPU_GMC_HOLE_START,
828c349dbc7Sjsg 			AMDGPU_GMC_HOLE_END);
829fb4d8502Sjsg 		return -EINVAL;
830fb4d8502Sjsg 	}
831fb4d8502Sjsg 
832c349dbc7Sjsg 	args->va_address &= AMDGPU_GMC_HOLE_MASK;
833fb4d8502Sjsg 
834ad8b1aafSjsg 	vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
835ad8b1aafSjsg 	vm_size -= AMDGPU_VA_RESERVED_SIZE;
836ad8b1aafSjsg 	if (args->va_address + args->map_size > vm_size) {
8375ca02815Sjsg 		dev_dbg(dev->dev,
838ad8b1aafSjsg 			"va_address 0x%llx is in top reserved area 0x%llx\n",
839ad8b1aafSjsg 			args->va_address + args->map_size, vm_size);
840ad8b1aafSjsg 		return -EINVAL;
841ad8b1aafSjsg 	}
842ad8b1aafSjsg 
843fb4d8502Sjsg 	if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
8445ca02815Sjsg 		dev_dbg(dev->dev, "invalid flags combination 0x%08X\n",
845fb4d8502Sjsg 			args->flags);
846fb4d8502Sjsg 		return -EINVAL;
847fb4d8502Sjsg 	}
848fb4d8502Sjsg 
849fb4d8502Sjsg 	switch (args->operation) {
850fb4d8502Sjsg 	case AMDGPU_VA_OP_MAP:
851fb4d8502Sjsg 	case AMDGPU_VA_OP_UNMAP:
852fb4d8502Sjsg 	case AMDGPU_VA_OP_CLEAR:
853fb4d8502Sjsg 	case AMDGPU_VA_OP_REPLACE:
854fb4d8502Sjsg 		break;
855fb4d8502Sjsg 	default:
8565ca02815Sjsg 		dev_dbg(dev->dev, "unsupported operation %d\n",
857fb4d8502Sjsg 			args->operation);
858fb4d8502Sjsg 		return -EINVAL;
859fb4d8502Sjsg 	}
860fb4d8502Sjsg 
861fb4d8502Sjsg 	if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
862fb4d8502Sjsg 	    !(args->flags & AMDGPU_VM_PAGE_PRT)) {
863fb4d8502Sjsg 		gobj = drm_gem_object_lookup(filp, args->handle);
864fb4d8502Sjsg 		if (gobj == NULL)
865fb4d8502Sjsg 			return -ENOENT;
866fb4d8502Sjsg 		abo = gem_to_amdgpu_bo(gobj);
867fb4d8502Sjsg 	} else {
868fb4d8502Sjsg 		gobj = NULL;
869fb4d8502Sjsg 		abo = NULL;
870fb4d8502Sjsg 	}
871fb4d8502Sjsg 
872*f005ef32Sjsg 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
873*f005ef32Sjsg 		      DRM_EXEC_IGNORE_DUPLICATES);
874*f005ef32Sjsg 	drm_exec_until_all_locked(&exec) {
875*f005ef32Sjsg 		if (gobj) {
876*f005ef32Sjsg 			r = drm_exec_lock_obj(&exec, gobj);
877*f005ef32Sjsg 			drm_exec_retry_on_contention(&exec);
878*f005ef32Sjsg 			if (unlikely(r))
879*f005ef32Sjsg 				goto error;
880*f005ef32Sjsg 		}
881fb4d8502Sjsg 
882*f005ef32Sjsg 		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
883*f005ef32Sjsg 		drm_exec_retry_on_contention(&exec);
884*f005ef32Sjsg 		if (unlikely(r))
885*f005ef32Sjsg 			goto error;
886*f005ef32Sjsg 	}
887fb4d8502Sjsg 
888fb4d8502Sjsg 	if (abo) {
889fb4d8502Sjsg 		bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
890fb4d8502Sjsg 		if (!bo_va) {
891fb4d8502Sjsg 			r = -ENOENT;
892*f005ef32Sjsg 			goto error;
893fb4d8502Sjsg 		}
894fb4d8502Sjsg 	} else if (args->operation != AMDGPU_VA_OP_CLEAR) {
895fb4d8502Sjsg 		bo_va = fpriv->prt_va;
896fb4d8502Sjsg 	} else {
897fb4d8502Sjsg 		bo_va = NULL;
898fb4d8502Sjsg 	}
899fb4d8502Sjsg 
900fb4d8502Sjsg 	switch (args->operation) {
901fb4d8502Sjsg 	case AMDGPU_VA_OP_MAP:
902c349dbc7Sjsg 		va_flags = amdgpu_gem_va_map_flags(adev, args->flags);
903fb4d8502Sjsg 		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
904fb4d8502Sjsg 				     args->offset_in_bo, args->map_size,
905fb4d8502Sjsg 				     va_flags);
906fb4d8502Sjsg 		break;
907fb4d8502Sjsg 	case AMDGPU_VA_OP_UNMAP:
908fb4d8502Sjsg 		r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
909fb4d8502Sjsg 		break;
910fb4d8502Sjsg 
911fb4d8502Sjsg 	case AMDGPU_VA_OP_CLEAR:
912fb4d8502Sjsg 		r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
913fb4d8502Sjsg 						args->va_address,
914fb4d8502Sjsg 						args->map_size);
915fb4d8502Sjsg 		break;
916fb4d8502Sjsg 	case AMDGPU_VA_OP_REPLACE:
917c349dbc7Sjsg 		va_flags = amdgpu_gem_va_map_flags(adev, args->flags);
918fb4d8502Sjsg 		r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
919fb4d8502Sjsg 					     args->offset_in_bo, args->map_size,
920fb4d8502Sjsg 					     va_flags);
921fb4d8502Sjsg 		break;
922fb4d8502Sjsg 	default:
923fb4d8502Sjsg 		break;
924fb4d8502Sjsg 	}
925fb4d8502Sjsg 	if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
926fb4d8502Sjsg 		amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
927fb4d8502Sjsg 					args->operation);
928fb4d8502Sjsg 
929*f005ef32Sjsg error:
930*f005ef32Sjsg 	drm_exec_fini(&exec);
931ad8b1aafSjsg 	drm_gem_object_put(gobj);
932fb4d8502Sjsg 	return r;
933fb4d8502Sjsg }
934fb4d8502Sjsg 
935fb4d8502Sjsg int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
936fb4d8502Sjsg 			struct drm_file *filp)
937fb4d8502Sjsg {
938ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
939fb4d8502Sjsg 	struct drm_amdgpu_gem_op *args = data;
940fb4d8502Sjsg 	struct drm_gem_object *gobj;
941c349dbc7Sjsg 	struct amdgpu_vm_bo_base *base;
942fb4d8502Sjsg 	struct amdgpu_bo *robj;
943fb4d8502Sjsg 	int r;
944fb4d8502Sjsg 
945fb4d8502Sjsg 	gobj = drm_gem_object_lookup(filp, args->handle);
946*f005ef32Sjsg 	if (!gobj)
947fb4d8502Sjsg 		return -ENOENT;
948*f005ef32Sjsg 
949fb4d8502Sjsg 	robj = gem_to_amdgpu_bo(gobj);
950fb4d8502Sjsg 
951fb4d8502Sjsg 	r = amdgpu_bo_reserve(robj, false);
952fb4d8502Sjsg 	if (unlikely(r))
953fb4d8502Sjsg 		goto out;
954fb4d8502Sjsg 
955fb4d8502Sjsg 	switch (args->op) {
956fb4d8502Sjsg 	case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
957fb4d8502Sjsg 		struct drm_amdgpu_gem_create_in info;
958fb4d8502Sjsg 		void __user *out = u64_to_user_ptr(args->value);
959fb4d8502Sjsg 
960c349dbc7Sjsg 		info.bo_size = robj->tbo.base.size;
9615ca02815Sjsg 		info.alignment = robj->tbo.page_alignment << PAGE_SHIFT;
962fb4d8502Sjsg 		info.domains = robj->preferred_domains;
963fb4d8502Sjsg 		info.domain_flags = robj->flags;
964fb4d8502Sjsg 		amdgpu_bo_unreserve(robj);
965fb4d8502Sjsg 		if (copy_to_user(out, &info, sizeof(info)))
966fb4d8502Sjsg 			r = -EFAULT;
967fb4d8502Sjsg 		break;
968fb4d8502Sjsg 	}
969fb4d8502Sjsg 	case AMDGPU_GEM_OP_SET_PLACEMENT:
9705ca02815Sjsg 		if (robj->tbo.base.import_attach &&
9715ca02815Sjsg 		    args->value & AMDGPU_GEM_DOMAIN_VRAM) {
972fb4d8502Sjsg 			r = -EINVAL;
973fb4d8502Sjsg 			amdgpu_bo_unreserve(robj);
974fb4d8502Sjsg 			break;
975fb4d8502Sjsg 		}
976fb4d8502Sjsg 		if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
977fb4d8502Sjsg 			r = -EPERM;
978fb4d8502Sjsg 			amdgpu_bo_unreserve(robj);
979fb4d8502Sjsg 			break;
980fb4d8502Sjsg 		}
981c349dbc7Sjsg 		for (base = robj->vm_bo; base; base = base->next)
982c349dbc7Sjsg 			if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev),
9835ca02815Sjsg 				amdgpu_ttm_adev(base->vm->root.bo->tbo.bdev))) {
984c349dbc7Sjsg 				r = -EINVAL;
985c349dbc7Sjsg 				amdgpu_bo_unreserve(robj);
986c349dbc7Sjsg 				goto out;
987c349dbc7Sjsg 			}
988c349dbc7Sjsg 
989c349dbc7Sjsg 
990fb4d8502Sjsg 		robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
991fb4d8502Sjsg 							AMDGPU_GEM_DOMAIN_GTT |
992fb4d8502Sjsg 							AMDGPU_GEM_DOMAIN_CPU);
993fb4d8502Sjsg 		robj->allowed_domains = robj->preferred_domains;
994fb4d8502Sjsg 		if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
995fb4d8502Sjsg 			robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
996fb4d8502Sjsg 
997fb4d8502Sjsg 		if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
998fb4d8502Sjsg 			amdgpu_vm_bo_invalidate(adev, robj, true);
999fb4d8502Sjsg 
1000fb4d8502Sjsg 		amdgpu_bo_unreserve(robj);
1001fb4d8502Sjsg 		break;
1002fb4d8502Sjsg 	default:
1003fb4d8502Sjsg 		amdgpu_bo_unreserve(robj);
1004fb4d8502Sjsg 		r = -EINVAL;
1005fb4d8502Sjsg 	}
1006fb4d8502Sjsg 
1007fb4d8502Sjsg out:
1008ad8b1aafSjsg 	drm_gem_object_put(gobj);
1009fb4d8502Sjsg 	return r;
1010fb4d8502Sjsg }
1011fb4d8502Sjsg 
10121bb76ff1Sjsg static int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
10131bb76ff1Sjsg 				  int width,
10141bb76ff1Sjsg 				  int cpp,
10151bb76ff1Sjsg 				  bool tiled)
10161bb76ff1Sjsg {
10171bb76ff1Sjsg 	int aligned = width;
10181bb76ff1Sjsg 	int pitch_mask = 0;
10191bb76ff1Sjsg 
10201bb76ff1Sjsg 	switch (cpp) {
10211bb76ff1Sjsg 	case 1:
10221bb76ff1Sjsg 		pitch_mask = 255;
10231bb76ff1Sjsg 		break;
10241bb76ff1Sjsg 	case 2:
10251bb76ff1Sjsg 		pitch_mask = 127;
10261bb76ff1Sjsg 		break;
10271bb76ff1Sjsg 	case 3:
10281bb76ff1Sjsg 	case 4:
10291bb76ff1Sjsg 		pitch_mask = 63;
10301bb76ff1Sjsg 		break;
10311bb76ff1Sjsg 	}
10321bb76ff1Sjsg 
10331bb76ff1Sjsg 	aligned += pitch_mask;
10341bb76ff1Sjsg 	aligned &= ~pitch_mask;
10351bb76ff1Sjsg 	return aligned * cpp;
10361bb76ff1Sjsg }
10371bb76ff1Sjsg 
1038fb4d8502Sjsg int amdgpu_mode_dumb_create(struct drm_file *file_priv,
1039fb4d8502Sjsg 			    struct drm_device *dev,
1040fb4d8502Sjsg 			    struct drm_mode_create_dumb *args)
1041fb4d8502Sjsg {
1042ad8b1aafSjsg 	struct amdgpu_device *adev = drm_to_adev(dev);
1043*f005ef32Sjsg 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1044fb4d8502Sjsg 	struct drm_gem_object *gobj;
1045fb4d8502Sjsg 	uint32_t handle;
1046c349dbc7Sjsg 	u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
10471bb76ff1Sjsg 		    AMDGPU_GEM_CREATE_CPU_GTT_USWC |
10481bb76ff1Sjsg 		    AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1049fb4d8502Sjsg 	u32 domain;
1050fb4d8502Sjsg 	int r;
1051fb4d8502Sjsg 
1052c349dbc7Sjsg 	/*
1053c349dbc7Sjsg 	 * The buffer returned from this function should be cleared, but
1054c349dbc7Sjsg 	 * it can only be done if the ring is enabled or we'll fail to
1055c349dbc7Sjsg 	 * create the buffer.
1056c349dbc7Sjsg 	 */
1057c349dbc7Sjsg 	if (adev->mman.buffer_funcs_enabled)
1058c349dbc7Sjsg 		flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
1059c349dbc7Sjsg 
10601bb76ff1Sjsg 	args->pitch = amdgpu_gem_align_pitch(adev, args->width,
1061fb4d8502Sjsg 					     DIV_ROUND_UP(args->bpp, 8), 0);
1062fb4d8502Sjsg 	args->size = (u64)args->pitch * args->height;
1063*f005ef32Sjsg 	args->size = ALIGN(args->size, PAGE_SIZE);
10645ca02815Sjsg 	domain = amdgpu_bo_get_preferred_domain(adev,
1065c349dbc7Sjsg 				amdgpu_display_supported_domains(adev, flags));
1066c349dbc7Sjsg 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
1067*f005ef32Sjsg 				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
1068fb4d8502Sjsg 	if (r)
1069fb4d8502Sjsg 		return -ENOMEM;
1070fb4d8502Sjsg 
1071fb4d8502Sjsg 	r = drm_gem_handle_create(file_priv, gobj, &handle);
1072fb4d8502Sjsg 	/* drop reference from allocate - handle holds it now */
1073ad8b1aafSjsg 	drm_gem_object_put(gobj);
1074*f005ef32Sjsg 	if (r)
1075fb4d8502Sjsg 		return r;
1076*f005ef32Sjsg 
1077fb4d8502Sjsg 	args->handle = handle;
1078fb4d8502Sjsg 	return 0;
1079fb4d8502Sjsg }
1080fb4d8502Sjsg 
1081fb4d8502Sjsg #if defined(CONFIG_DEBUG_FS)
10825ca02815Sjsg static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
1083fb4d8502Sjsg {
1084*f005ef32Sjsg 	struct amdgpu_device *adev = m->private;
10855ca02815Sjsg 	struct drm_device *dev = adev_to_drm(adev);
1086fb4d8502Sjsg 	struct drm_file *file;
1087fb4d8502Sjsg 	int r;
1088fb4d8502Sjsg 
1089fb4d8502Sjsg 	r = mutex_lock_interruptible(&dev->filelist_mutex);
1090fb4d8502Sjsg 	if (r)
1091fb4d8502Sjsg 		return r;
1092fb4d8502Sjsg 
1093fb4d8502Sjsg 	list_for_each_entry(file, &dev->filelist, lhead) {
1094fb4d8502Sjsg 		struct task_struct *task;
10955ca02815Sjsg 		struct drm_gem_object *gobj;
1096*f005ef32Sjsg 		struct pid *pid;
10975ca02815Sjsg 		int id;
1098fb4d8502Sjsg 
1099fb4d8502Sjsg 		/*
1100fb4d8502Sjsg 		 * Although we have a valid reference on file->pid, that does
1101fb4d8502Sjsg 		 * not guarantee that the task_struct who called get_pid() is
1102fb4d8502Sjsg 		 * still alive (e.g. get_pid(current) => fork() => exit()).
1103fb4d8502Sjsg 		 * Therefore, we need to protect this ->comm access using RCU.
1104fb4d8502Sjsg 		 */
1105fb4d8502Sjsg 		rcu_read_lock();
1106*f005ef32Sjsg 		pid = rcu_dereference(file->pid);
1107*f005ef32Sjsg 		task = pid_task(pid, PIDTYPE_TGID);
1108*f005ef32Sjsg 		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
1109fb4d8502Sjsg 			   task ? task->comm : "<unknown>");
1110fb4d8502Sjsg 		rcu_read_unlock();
1111fb4d8502Sjsg 
1112fb4d8502Sjsg 		spin_lock(&file->table_lock);
11135ca02815Sjsg 		idr_for_each_entry(&file->object_idr, gobj, id) {
11145ca02815Sjsg 			struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
11155ca02815Sjsg 
11165ca02815Sjsg 			amdgpu_bo_print_info(id, bo, m);
11175ca02815Sjsg 		}
1118fb4d8502Sjsg 		spin_unlock(&file->table_lock);
1119fb4d8502Sjsg 	}
1120fb4d8502Sjsg 
1121fb4d8502Sjsg 	mutex_unlock(&dev->filelist_mutex);
1122fb4d8502Sjsg 	return 0;
1123fb4d8502Sjsg }
1124fb4d8502Sjsg 
11255ca02815Sjsg DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_gem_info);
11265ca02815Sjsg 
1127fb4d8502Sjsg #endif
1128fb4d8502Sjsg 
11295ca02815Sjsg void amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
1130fb4d8502Sjsg {
1131fb4d8502Sjsg #if defined(CONFIG_DEBUG_FS)
11325ca02815Sjsg 	struct drm_minor *minor = adev_to_drm(adev)->primary;
11335ca02815Sjsg 	struct dentry *root = minor->debugfs_root;
11345ca02815Sjsg 
11355ca02815Sjsg 	debugfs_create_file("amdgpu_gem_info", 0444, root, adev,
11365ca02815Sjsg 			    &amdgpu_debugfs_gem_info_fops);
1137fb4d8502Sjsg #endif
1138fb4d8502Sjsg }
1139