xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 #include <linux/ktime.h>
29 #include <linux/module.h>
30 #include <linux/pagemap.h>
31 #include <linux/pci.h>
32 #include <linux/dma-buf.h>
33 
34 #include <drm/amdgpu_drm.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_exec.h>
37 #include <drm/drm_gem_ttm_helper.h>
38 #include <drm/ttm/ttm_tt.h>
39 #include <drm/drm_syncobj.h>
40 
41 #include "amdgpu.h"
42 #include "amdgpu_display.h"
43 #include "amdgpu_dma_buf.h"
44 #include "amdgpu_hmm.h"
45 #include "amdgpu_xgmi.h"
46 #include "amdgpu_vm.h"
47 
48 static int
49 amdgpu_gem_add_input_fence(struct drm_file *filp,
50 			   uint64_t syncobj_handles_array,
51 			   uint32_t num_syncobj_handles)
52 {
53 	struct dma_fence *fence;
54 	uint32_t *syncobj_handles;
55 	int ret, i;
56 
57 	if (!num_syncobj_handles)
58 		return 0;
59 
60 	syncobj_handles = memdup_user(u64_to_user_ptr(syncobj_handles_array),
61 				      size_mul(sizeof(uint32_t), num_syncobj_handles));
62 	if (IS_ERR(syncobj_handles))
63 		return PTR_ERR(syncobj_handles);
64 
65 	for (i = 0; i < num_syncobj_handles; i++) {
66 
67 		if (!syncobj_handles[i]) {
68 			ret = -EINVAL;
69 			goto free_memdup;
70 		}
71 
72 		ret = drm_syncobj_find_fence(filp, syncobj_handles[i], 0, 0, &fence);
73 		if (ret)
74 			goto free_memdup;
75 
76 		dma_fence_wait(fence, false);
77 
78 		/* TODO: optimize async handling */
79 		dma_fence_put(fence);
80 	}
81 
82 free_memdup:
83 	kfree(syncobj_handles);
84 	return ret;
85 }
86 
87 static int
88 amdgpu_gem_update_timeline_node(struct drm_file *filp,
89 				uint32_t syncobj_handle,
90 				uint64_t point,
91 				struct drm_syncobj **syncobj,
92 				struct dma_fence_chain **chain)
93 {
94 	if (!syncobj_handle)
95 		return 0;
96 
97 	/* Find the sync object */
98 	*syncobj = drm_syncobj_find(filp, syncobj_handle);
99 	if (!*syncobj)
100 		return -ENOENT;
101 
102 	if (!point)
103 		return 0;
104 
105 	/* Allocate the chain node */
106 	*chain = dma_fence_chain_alloc();
107 	if (!*chain) {
108 		drm_syncobj_put(*syncobj);
109 		return -ENOMEM;
110 	}
111 
112 	return 0;
113 }
114 
115 static void
116 amdgpu_gem_update_bo_mapping(struct drm_file *filp,
117 			     struct amdgpu_bo_va *bo_va,
118 			     uint32_t operation,
119 			     uint64_t point,
120 			     struct dma_fence *fence,
121 			     struct drm_syncobj *syncobj,
122 			     struct dma_fence_chain *chain)
123 {
124 	struct amdgpu_bo *bo = bo_va ? bo_va->base.bo : NULL;
125 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
126 	struct amdgpu_vm *vm = &fpriv->vm;
127 	struct dma_fence *last_update;
128 
129 	if (!syncobj)
130 		return;
131 
132 	/* Find the last update fence */
133 	switch (operation) {
134 	case AMDGPU_VA_OP_MAP:
135 	case AMDGPU_VA_OP_REPLACE:
136 		if (bo && (bo->tbo.base.resv == vm->root.bo->tbo.base.resv))
137 			last_update = vm->last_update;
138 		else
139 			last_update = bo_va->last_pt_update;
140 		break;
141 	case AMDGPU_VA_OP_UNMAP:
142 	case AMDGPU_VA_OP_CLEAR:
143 		last_update = fence;
144 		break;
145 	default:
146 		return;
147 	}
148 
149 	/* Add fence to timeline */
150 	if (!point)
151 		drm_syncobj_replace_fence(syncobj, last_update);
152 	else
153 		drm_syncobj_add_point(syncobj, chain, last_update, point);
154 }
155 
156 static vm_fault_t amdgpu_gem_fault(struct vm_fault *vmf)
157 {
158 	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
159 	struct drm_device *ddev = bo->base.dev;
160 	vm_fault_t ret;
161 	int idx;
162 
163 	ret = ttm_bo_vm_reserve(bo, vmf);
164 	if (ret)
165 		return ret;
166 
167 	if (drm_dev_enter(ddev, &idx)) {
168 		ret = amdgpu_bo_fault_reserve_notify(bo);
169 		if (ret) {
170 			drm_dev_exit(idx);
171 			goto unlock;
172 		}
173 
174 		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
175 					       TTM_BO_VM_NUM_PREFAULT);
176 
177 		drm_dev_exit(idx);
178 	} else {
179 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
180 	}
181 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
182 		return ret;
183 
184 unlock:
185 	dma_resv_unlock(bo->base.resv);
186 	return ret;
187 }
188 
189 static const struct vm_operations_struct amdgpu_gem_vm_ops = {
190 	.fault = amdgpu_gem_fault,
191 	.open = ttm_bo_vm_open,
192 	.close = ttm_bo_vm_close,
193 	.access = ttm_bo_vm_access
194 };
195 
196 static void amdgpu_gem_object_free(struct drm_gem_object *gobj)
197 {
198 	struct amdgpu_bo *aobj = gem_to_amdgpu_bo(gobj);
199 
200 	amdgpu_hmm_unregister(aobj);
201 	ttm_bo_put(&aobj->tbo);
202 }
203 
204 int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
205 			     int alignment, u32 initial_domain,
206 			     u64 flags, enum ttm_bo_type type,
207 			     struct dma_resv *resv,
208 			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
209 {
210 	struct amdgpu_bo *bo;
211 	struct amdgpu_bo_user *ubo;
212 	struct amdgpu_bo_param bp;
213 	int r;
214 
215 	memset(&bp, 0, sizeof(bp));
216 	*obj = NULL;
217 	flags |= AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE;
218 
219 	bp.size = size;
220 	bp.byte_align = alignment;
221 	bp.type = type;
222 	bp.resv = resv;
223 	bp.preferred_domain = initial_domain;
224 	bp.flags = flags;
225 	bp.domain = initial_domain;
226 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
227 	bp.xcp_id_plus1 = xcp_id_plus1;
228 
229 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
230 	if (r)
231 		return r;
232 
233 	bo = &ubo->bo;
234 	*obj = &bo->tbo.base;
235 
236 	return 0;
237 }
238 
239 void amdgpu_gem_force_release(struct amdgpu_device *adev)
240 {
241 	struct drm_device *ddev = adev_to_drm(adev);
242 	struct drm_file *file;
243 
244 	mutex_lock(&ddev->filelist_mutex);
245 
246 	list_for_each_entry(file, &ddev->filelist, lhead) {
247 		struct drm_gem_object *gobj;
248 		int handle;
249 
250 		WARN_ONCE(1, "Still active user space clients!\n");
251 		spin_lock(&file->table_lock);
252 		idr_for_each_entry(&file->object_idr, gobj, handle) {
253 			WARN_ONCE(1, "And also active allocations!\n");
254 			drm_gem_object_put(gobj);
255 		}
256 		idr_destroy(&file->object_idr);
257 		spin_unlock(&file->table_lock);
258 	}
259 
260 	mutex_unlock(&ddev->filelist_mutex);
261 }
262 
263 /*
264  * Call from drm_gem_handle_create which appear in both new and open ioctl
265  * case.
266  */
267 static int amdgpu_gem_object_open(struct drm_gem_object *obj,
268 				  struct drm_file *file_priv)
269 {
270 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
271 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
272 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
273 	struct amdgpu_vm *vm = &fpriv->vm;
274 	struct amdgpu_bo_va *bo_va;
275 	struct mm_struct *mm;
276 	int r;
277 
278 	mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
279 	if (mm && mm != current->mm)
280 		return -EPERM;
281 
282 	if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
283 	    !amdgpu_vm_is_bo_always_valid(vm, abo))
284 		return -EPERM;
285 
286 	r = amdgpu_bo_reserve(abo, false);
287 	if (r)
288 		return r;
289 
290 	amdgpu_vm_bo_update_shared(abo);
291 	bo_va = amdgpu_vm_bo_find(vm, abo);
292 	if (!bo_va)
293 		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
294 	else
295 		++bo_va->ref_count;
296 
297 	/* attach gfx eviction fence */
298 	r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
299 	if (r) {
300 		DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
301 		amdgpu_bo_unreserve(abo);
302 		return r;
303 	}
304 
305 	amdgpu_bo_unreserve(abo);
306 
307 	/* Validate and add eviction fence to DMABuf imports with dynamic
308 	 * attachment in compute VMs. Re-validation will be done by
309 	 * amdgpu_vm_validate. Fences are on the reservation shared with the
310 	 * export, which is currently required to be validated and fenced
311 	 * already by amdgpu_amdkfd_gpuvm_restore_process_bos.
312 	 *
313 	 * Nested locking below for the case that a GEM object is opened in
314 	 * kfd_mem_export_dmabuf. Since the lock below is only taken for imports,
315 	 * but not for export, this is a different lock class that cannot lead to
316 	 * circular lock dependencies.
317 	 */
318 	if (!vm->is_compute_context || !vm->process_info)
319 		return 0;
320 	if (!drm_gem_is_imported(obj) ||
321 	    !dma_buf_is_dynamic(obj->import_attach->dmabuf))
322 		return 0;
323 	mutex_lock_nested(&vm->process_info->lock, 1);
324 	if (!WARN_ON(!vm->process_info->eviction_fence)) {
325 		r = amdgpu_amdkfd_bo_validate_and_fence(abo, AMDGPU_GEM_DOMAIN_GTT,
326 							&vm->process_info->eviction_fence->base);
327 		if (r) {
328 			struct amdgpu_task_info *ti = amdgpu_vm_get_task_info_vm(vm);
329 
330 			dev_warn(adev->dev, "validate_and_fence failed: %d\n", r);
331 			if (ti) {
332 				dev_warn(adev->dev, "pid %d\n", ti->task.pid);
333 				amdgpu_vm_put_task_info(ti);
334 			}
335 		}
336 	}
337 	mutex_unlock(&vm->process_info->lock);
338 
339 	return r;
340 }
341 
342 static void amdgpu_gem_object_close(struct drm_gem_object *obj,
343 				    struct drm_file *file_priv)
344 {
345 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
346 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
347 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
348 	struct amdgpu_vm *vm = &fpriv->vm;
349 
350 	struct dma_fence *fence = NULL;
351 	struct amdgpu_bo_va *bo_va;
352 	struct drm_exec exec;
353 	long r;
354 
355 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
356 	drm_exec_until_all_locked(&exec) {
357 		r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
358 		drm_exec_retry_on_contention(&exec);
359 		if (unlikely(r))
360 			goto out_unlock;
361 
362 		r = amdgpu_vm_lock_pd(vm, &exec, 0);
363 		drm_exec_retry_on_contention(&exec);
364 		if (unlikely(r))
365 			goto out_unlock;
366 	}
367 
368 	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
369 		amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
370 
371 	bo_va = amdgpu_vm_bo_find(vm, bo);
372 	if (!bo_va || --bo_va->ref_count)
373 		goto out_unlock;
374 
375 	amdgpu_vm_bo_del(adev, bo_va);
376 	amdgpu_vm_bo_update_shared(bo);
377 	if (!amdgpu_vm_ready(vm))
378 		goto out_unlock;
379 
380 	r = amdgpu_vm_clear_freed(adev, vm, &fence);
381 	if (unlikely(r < 0))
382 		dev_err(adev->dev, "failed to clear page "
383 			"tables on GEM object close (%ld)\n", r);
384 	if (r || !fence)
385 		goto out_unlock;
386 
387 	amdgpu_bo_fence(bo, fence, true);
388 	dma_fence_put(fence);
389 
390 out_unlock:
391 	if (r)
392 		dev_err(adev->dev, "leaking bo va (%ld)\n", r);
393 	drm_exec_fini(&exec);
394 }
395 
396 static int amdgpu_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
397 {
398 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
399 
400 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
401 		return -EPERM;
402 	if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
403 		return -EPERM;
404 
405 	/* Workaround for Thunk bug creating PROT_NONE,MAP_PRIVATE mappings
406 	 * for debugger access to invisible VRAM. Should have used MAP_SHARED
407 	 * instead. Clearing VM_MAYWRITE prevents the mapping from ever
408 	 * becoming writable and makes is_cow_mapping(vm_flags) false.
409 	 */
410 	if (is_cow_mapping(vma->vm_flags) &&
411 	    !(vma->vm_flags & VM_ACCESS_FLAGS))
412 		vm_flags_clear(vma, VM_MAYWRITE);
413 
414 	return drm_gem_ttm_mmap(obj, vma);
415 }
416 
417 const struct drm_gem_object_funcs amdgpu_gem_object_funcs = {
418 	.free = amdgpu_gem_object_free,
419 	.open = amdgpu_gem_object_open,
420 	.close = amdgpu_gem_object_close,
421 	.export = amdgpu_gem_prime_export,
422 	.vmap = drm_gem_ttm_vmap,
423 	.vunmap = drm_gem_ttm_vunmap,
424 	.mmap = amdgpu_gem_object_mmap,
425 	.vm_ops = &amdgpu_gem_vm_ops,
426 };
427 
428 /*
429  * GEM ioctls.
430  */
431 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
432 			    struct drm_file *filp)
433 {
434 	struct amdgpu_device *adev = drm_to_adev(dev);
435 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
436 	struct amdgpu_vm *vm = &fpriv->vm;
437 	union drm_amdgpu_gem_create *args = data;
438 	uint64_t flags = args->in.domain_flags;
439 	uint64_t size = args->in.bo_size;
440 	struct dma_resv *resv = NULL;
441 	struct drm_gem_object *gobj;
442 	uint32_t handle, initial_domain;
443 	int r;
444 
445 	/* reject invalid gem flags */
446 	if (flags & ~AMDGPU_GEM_CREATE_SETTABLE_MASK)
447 		return -EINVAL;
448 
449 	/* reject invalid gem domains */
450 	if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
451 		return -EINVAL;
452 
453 	if (!amdgpu_is_tmz(adev) && (flags & AMDGPU_GEM_CREATE_ENCRYPTED)) {
454 		DRM_NOTE_ONCE("Cannot allocate secure buffer since TMZ is disabled\n");
455 		return -EINVAL;
456 	}
457 
458 	/* always clear VRAM */
459 	flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
460 
461 	if (args->in.domains & AMDGPU_GEM_DOMAIN_MMIO_REMAP)
462 		return -EINVAL;
463 
464 	/* create a gem object to contain this object in */
465 	if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
466 	    AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
467 		if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
468 			/* if gds bo is created from user space, it must be
469 			 * passed to bo list
470 			 */
471 			DRM_ERROR("GDS bo cannot be per-vm-bo\n");
472 			return -EINVAL;
473 		}
474 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
475 	}
476 
477 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
478 		r = amdgpu_bo_reserve(vm->root.bo, false);
479 		if (r)
480 			return r;
481 
482 		resv = vm->root.bo->tbo.base.resv;
483 	}
484 
485 	initial_domain = (u32)(0xffffffff & args->in.domains);
486 retry:
487 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
488 				     initial_domain,
489 				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
490 	if (r && r != -ERESTARTSYS) {
491 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
492 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
493 			goto retry;
494 		}
495 
496 		if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
497 			initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
498 			goto retry;
499 		}
500 		DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n",
501 				size, initial_domain, args->in.alignment, r);
502 	}
503 
504 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
505 		if (!r) {
506 			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
507 
508 			abo->parent = amdgpu_bo_ref(vm->root.bo);
509 		}
510 		amdgpu_bo_unreserve(vm->root.bo);
511 	}
512 	if (r)
513 		return r;
514 
515 	r = drm_gem_handle_create(filp, gobj, &handle);
516 	/* drop reference from allocate - handle holds it now */
517 	drm_gem_object_put(gobj);
518 	if (r)
519 		return r;
520 
521 	memset(args, 0, sizeof(*args));
522 	args->out.handle = handle;
523 	return 0;
524 }
525 
526 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
527 			     struct drm_file *filp)
528 {
529 	struct ttm_operation_ctx ctx = { true, false };
530 	struct amdgpu_device *adev = drm_to_adev(dev);
531 	struct drm_amdgpu_gem_userptr *args = data;
532 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
533 	struct drm_gem_object *gobj;
534 	struct hmm_range *range;
535 	struct amdgpu_bo *bo;
536 	uint32_t handle;
537 	int r;
538 
539 	args->addr = untagged_addr(args->addr);
540 
541 	if (offset_in_page(args->addr | args->size))
542 		return -EINVAL;
543 
544 	/* reject unknown flag values */
545 	if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
546 	    AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
547 	    AMDGPU_GEM_USERPTR_REGISTER))
548 		return -EINVAL;
549 
550 	if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
551 	     !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
552 
553 		/* if we want to write to it we must install a MMU notifier */
554 		return -EACCES;
555 	}
556 
557 	/* create a gem object to contain this object in */
558 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
559 				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
560 	if (r)
561 		return r;
562 
563 	bo = gem_to_amdgpu_bo(gobj);
564 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
565 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
566 	r = amdgpu_ttm_tt_set_userptr(&bo->tbo, args->addr, args->flags);
567 	if (r)
568 		goto release_object;
569 
570 	r = amdgpu_hmm_register(bo, args->addr);
571 	if (r)
572 		goto release_object;
573 
574 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
575 		r = amdgpu_ttm_tt_get_user_pages(bo, &range);
576 		if (r)
577 			goto release_object;
578 
579 		r = amdgpu_bo_reserve(bo, true);
580 		if (r)
581 			goto user_pages_done;
582 
583 		amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
584 
585 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
586 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
587 		amdgpu_bo_unreserve(bo);
588 		if (r)
589 			goto user_pages_done;
590 	}
591 
592 	r = drm_gem_handle_create(filp, gobj, &handle);
593 	if (r)
594 		goto user_pages_done;
595 
596 	args->handle = handle;
597 
598 user_pages_done:
599 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
600 		amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, range);
601 
602 release_object:
603 	drm_gem_object_put(gobj);
604 
605 	return r;
606 }
607 
608 int amdgpu_mode_dumb_mmap(struct drm_file *filp,
609 			  struct drm_device *dev,
610 			  uint32_t handle, uint64_t *offset_p)
611 {
612 	struct drm_gem_object *gobj;
613 	struct amdgpu_bo *robj;
614 
615 	gobj = drm_gem_object_lookup(filp, handle);
616 	if (!gobj)
617 		return -ENOENT;
618 
619 	robj = gem_to_amdgpu_bo(gobj);
620 	if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
621 	    (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
622 		drm_gem_object_put(gobj);
623 		return -EPERM;
624 	}
625 	*offset_p = amdgpu_bo_mmap_offset(robj);
626 	drm_gem_object_put(gobj);
627 	return 0;
628 }
629 
630 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
631 			  struct drm_file *filp)
632 {
633 	union drm_amdgpu_gem_mmap *args = data;
634 	uint32_t handle = args->in.handle;
635 
636 	memset(args, 0, sizeof(*args));
637 	return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
638 }
639 
640 /**
641  * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
642  *
643  * @timeout_ns: timeout in ns
644  *
645  * Calculate the timeout in jiffies from an absolute timeout in ns.
646  */
647 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
648 {
649 	unsigned long timeout_jiffies;
650 	ktime_t timeout;
651 
652 	/* clamp timeout if it's to large */
653 	if (((int64_t)timeout_ns) < 0)
654 		return MAX_SCHEDULE_TIMEOUT;
655 
656 	timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
657 	if (ktime_to_ns(timeout) < 0)
658 		return 0;
659 
660 	timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
661 	/*  clamp timeout to avoid unsigned-> signed overflow */
662 	if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT)
663 		return MAX_SCHEDULE_TIMEOUT - 1;
664 
665 	return timeout_jiffies;
666 }
667 
668 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
669 			      struct drm_file *filp)
670 {
671 	union drm_amdgpu_gem_wait_idle *args = data;
672 	struct drm_gem_object *gobj;
673 	struct amdgpu_bo *robj;
674 	uint32_t handle = args->in.handle;
675 	unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
676 	int r = 0;
677 	long ret;
678 
679 	gobj = drm_gem_object_lookup(filp, handle);
680 	if (!gobj)
681 		return -ENOENT;
682 
683 	robj = gem_to_amdgpu_bo(gobj);
684 	ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ,
685 				    true, timeout);
686 
687 	/* ret == 0 means not signaled,
688 	 * ret > 0 means signaled
689 	 * ret < 0 means interrupted before timeout
690 	 */
691 	if (ret >= 0) {
692 		memset(args, 0, sizeof(*args));
693 		args->out.status = (ret == 0);
694 	} else
695 		r = ret;
696 
697 	drm_gem_object_put(gobj);
698 	return r;
699 }
700 
701 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
702 				struct drm_file *filp)
703 {
704 	struct drm_amdgpu_gem_metadata *args = data;
705 	struct drm_gem_object *gobj;
706 	struct amdgpu_bo *robj;
707 	int r = -1;
708 
709 	DRM_DEBUG("%d\n", args->handle);
710 	gobj = drm_gem_object_lookup(filp, args->handle);
711 	if (gobj == NULL)
712 		return -ENOENT;
713 	robj = gem_to_amdgpu_bo(gobj);
714 
715 	r = amdgpu_bo_reserve(robj, false);
716 	if (unlikely(r != 0))
717 		goto out;
718 
719 	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
720 		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
721 		r = amdgpu_bo_get_metadata(robj, args->data.data,
722 					   sizeof(args->data.data),
723 					   &args->data.data_size_bytes,
724 					   &args->data.flags);
725 	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
726 		if (args->data.data_size_bytes > sizeof(args->data.data)) {
727 			r = -EINVAL;
728 			goto unreserve;
729 		}
730 		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
731 		if (!r)
732 			r = amdgpu_bo_set_metadata(robj, args->data.data,
733 						   args->data.data_size_bytes,
734 						   args->data.flags);
735 	}
736 
737 unreserve:
738 	amdgpu_bo_unreserve(robj);
739 out:
740 	drm_gem_object_put(gobj);
741 	return r;
742 }
743 
744 /**
745  * amdgpu_gem_va_update_vm -update the bo_va in its VM
746  *
747  * @adev: amdgpu_device pointer
748  * @vm: vm to update
749  * @bo_va: bo_va to update
750  * @operation: map, unmap or clear
751  *
752  * Update the bo_va directly after setting its address. Errors are not
753  * vital here, so they are not reported back to userspace.
754  *
755  * Returns resulting fence if freed BO(s) got cleared from the PT.
756  * otherwise stub fence in case of error.
757  */
758 static struct dma_fence *
759 amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
760 			struct amdgpu_vm *vm,
761 			struct amdgpu_bo_va *bo_va,
762 			uint32_t operation)
763 {
764 	struct dma_fence *fence = dma_fence_get_stub();
765 	int r;
766 
767 	if (!amdgpu_vm_ready(vm))
768 		return fence;
769 
770 	r = amdgpu_vm_clear_freed(adev, vm, &fence);
771 	if (r)
772 		goto error;
773 
774 	if (operation == AMDGPU_VA_OP_MAP ||
775 	    operation == AMDGPU_VA_OP_REPLACE) {
776 		r = amdgpu_vm_bo_update(adev, bo_va, false);
777 		if (r)
778 			goto error;
779 	}
780 
781 	r = amdgpu_vm_update_pdes(adev, vm, false);
782 
783 error:
784 	if (r && r != -ERESTARTSYS)
785 		DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
786 
787 	return fence;
788 }
789 
790 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
791 			  struct drm_file *filp)
792 {
793 	const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
794 		AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
795 		AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
796 		AMDGPU_VM_PAGE_NOALLOC;
797 	const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
798 		AMDGPU_VM_PAGE_PRT;
799 
800 	struct drm_amdgpu_gem_va *args = data;
801 	struct drm_gem_object *gobj;
802 	struct amdgpu_device *adev = drm_to_adev(dev);
803 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
804 	struct amdgpu_bo *abo;
805 	struct amdgpu_bo_va *bo_va;
806 	struct drm_syncobj *timeline_syncobj = NULL;
807 	struct dma_fence_chain *timeline_chain = NULL;
808 	struct dma_fence *fence;
809 	struct drm_exec exec;
810 	uint64_t vm_size;
811 	int r = 0;
812 
813 	if (args->va_address < AMDGPU_VA_RESERVED_BOTTOM) {
814 		dev_dbg(dev->dev,
815 			"va_address 0x%llx is in reserved area 0x%llx\n",
816 			args->va_address, AMDGPU_VA_RESERVED_BOTTOM);
817 		return -EINVAL;
818 	}
819 
820 	if (args->va_address >= AMDGPU_GMC_HOLE_START &&
821 	    args->va_address < AMDGPU_GMC_HOLE_END) {
822 		dev_dbg(dev->dev,
823 			"va_address 0x%llx is in VA hole 0x%llx-0x%llx\n",
824 			args->va_address, AMDGPU_GMC_HOLE_START,
825 			AMDGPU_GMC_HOLE_END);
826 		return -EINVAL;
827 	}
828 
829 	args->va_address &= AMDGPU_GMC_HOLE_MASK;
830 
831 	vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
832 	vm_size -= AMDGPU_VA_RESERVED_TOP;
833 	if (args->va_address + args->map_size > vm_size) {
834 		dev_dbg(dev->dev,
835 			"va_address 0x%llx is in top reserved area 0x%llx\n",
836 			args->va_address + args->map_size, vm_size);
837 		return -EINVAL;
838 	}
839 
840 	if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
841 		dev_dbg(dev->dev, "invalid flags combination 0x%08X\n",
842 			args->flags);
843 		return -EINVAL;
844 	}
845 
846 	switch (args->operation) {
847 	case AMDGPU_VA_OP_MAP:
848 	case AMDGPU_VA_OP_UNMAP:
849 	case AMDGPU_VA_OP_CLEAR:
850 	case AMDGPU_VA_OP_REPLACE:
851 		break;
852 	default:
853 		dev_dbg(dev->dev, "unsupported operation %d\n",
854 			args->operation);
855 		return -EINVAL;
856 	}
857 
858 	if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
859 	    !(args->flags & AMDGPU_VM_PAGE_PRT)) {
860 		gobj = drm_gem_object_lookup(filp, args->handle);
861 		if (gobj == NULL)
862 			return -ENOENT;
863 		abo = gem_to_amdgpu_bo(gobj);
864 	} else {
865 		gobj = NULL;
866 		abo = NULL;
867 	}
868 
869 	r = amdgpu_gem_add_input_fence(filp,
870 				       args->input_fence_syncobj_handles,
871 				       args->num_syncobj_handles);
872 	if (r)
873 		goto error_put_gobj;
874 
875 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
876 		      DRM_EXEC_IGNORE_DUPLICATES, 0);
877 	drm_exec_until_all_locked(&exec) {
878 		if (gobj) {
879 			r = drm_exec_lock_obj(&exec, gobj);
880 			drm_exec_retry_on_contention(&exec);
881 			if (unlikely(r))
882 				goto error;
883 		}
884 
885 		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
886 		drm_exec_retry_on_contention(&exec);
887 		if (unlikely(r))
888 			goto error;
889 	}
890 
891 	if (abo) {
892 		bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
893 		if (!bo_va) {
894 			r = -ENOENT;
895 			goto error;
896 		}
897 	} else if (args->operation != AMDGPU_VA_OP_CLEAR) {
898 		bo_va = fpriv->prt_va;
899 	} else {
900 		bo_va = NULL;
901 	}
902 
903 	r = amdgpu_gem_update_timeline_node(filp,
904 					    args->vm_timeline_syncobj_out,
905 					    args->vm_timeline_point,
906 					    &timeline_syncobj,
907 					    &timeline_chain);
908 	if (r)
909 		goto error;
910 
911 	switch (args->operation) {
912 	case AMDGPU_VA_OP_MAP:
913 		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
914 				     args->offset_in_bo, args->map_size,
915 				     args->flags);
916 		break;
917 	case AMDGPU_VA_OP_UNMAP:
918 		r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
919 		break;
920 
921 	case AMDGPU_VA_OP_CLEAR:
922 		r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
923 						args->va_address,
924 						args->map_size);
925 		break;
926 	case AMDGPU_VA_OP_REPLACE:
927 		r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
928 					     args->offset_in_bo, args->map_size,
929 					     args->flags);
930 		break;
931 	default:
932 		break;
933 	}
934 	if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !adev->debug_vm) {
935 		fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
936 						args->operation);
937 
938 		if (timeline_syncobj)
939 			amdgpu_gem_update_bo_mapping(filp, bo_va,
940 					     args->operation,
941 					     args->vm_timeline_point,
942 					     fence, timeline_syncobj,
943 					     timeline_chain);
944 		else
945 			dma_fence_put(fence);
946 
947 	}
948 
949 error:
950 	drm_exec_fini(&exec);
951 error_put_gobj:
952 	drm_gem_object_put(gobj);
953 	return r;
954 }
955 
956 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
957 			struct drm_file *filp)
958 {
959 	struct drm_amdgpu_gem_op *args = data;
960 	struct drm_gem_object *gobj;
961 	struct amdgpu_vm_bo_base *base;
962 	struct amdgpu_bo *robj;
963 	struct drm_exec exec;
964 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
965 	int r;
966 
967 	if (args->padding)
968 		return -EINVAL;
969 
970 	gobj = drm_gem_object_lookup(filp, args->handle);
971 	if (!gobj)
972 		return -ENOENT;
973 
974 	robj = gem_to_amdgpu_bo(gobj);
975 
976 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
977 			  DRM_EXEC_IGNORE_DUPLICATES, 0);
978 	drm_exec_until_all_locked(&exec) {
979 		r = drm_exec_lock_obj(&exec, gobj);
980 		drm_exec_retry_on_contention(&exec);
981 		if (r)
982 			goto out_exec;
983 
984 		if (args->op == AMDGPU_GEM_OP_GET_MAPPING_INFO) {
985 			r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 0);
986 			drm_exec_retry_on_contention(&exec);
987 			if (r)
988 				goto out_exec;
989 		}
990 	}
991 
992 	switch (args->op) {
993 	case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
994 		struct drm_amdgpu_gem_create_in info;
995 		void __user *out = u64_to_user_ptr(args->value);
996 
997 		info.bo_size = robj->tbo.base.size;
998 		info.alignment = robj->tbo.page_alignment << PAGE_SHIFT;
999 		info.domains = robj->preferred_domains;
1000 		info.domain_flags = robj->flags;
1001 		drm_exec_fini(&exec);
1002 		if (copy_to_user(out, &info, sizeof(info)))
1003 			r = -EFAULT;
1004 		break;
1005 	}
1006 	case AMDGPU_GEM_OP_SET_PLACEMENT:
1007 		if (drm_gem_is_imported(&robj->tbo.base) &&
1008 		    args->value & AMDGPU_GEM_DOMAIN_VRAM) {
1009 			r = -EINVAL;
1010 			goto out_exec;
1011 		}
1012 		if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
1013 			r = -EPERM;
1014 			goto out_exec;
1015 		}
1016 		for (base = robj->vm_bo; base; base = base->next)
1017 			if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev),
1018 				amdgpu_ttm_adev(base->vm->root.bo->tbo.bdev))) {
1019 				r = -EINVAL;
1020 				goto out_exec;
1021 			}
1022 
1023 
1024 		robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
1025 							AMDGPU_GEM_DOMAIN_GTT |
1026 							AMDGPU_GEM_DOMAIN_CPU);
1027 		robj->allowed_domains = robj->preferred_domains;
1028 		if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
1029 			robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
1030 
1031 		if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
1032 			amdgpu_vm_bo_invalidate(robj, true);
1033 		drm_exec_fini(&exec);
1034 		break;
1035 	case AMDGPU_GEM_OP_GET_MAPPING_INFO: {
1036 		struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(&fpriv->vm, robj);
1037 		struct drm_amdgpu_gem_vm_entry *vm_entries;
1038 		struct amdgpu_bo_va_mapping *mapping;
1039 		int num_mappings = 0;
1040 		/*
1041 		 * num_entries is set as an input to the size of the user-allocated array of
1042 		 * drm_amdgpu_gem_vm_entry stored at args->value.
1043 		 * num_entries is sent back as output as the number of mappings the bo has.
1044 		 * If that number is larger than the size of the array, the ioctl must
1045 		 * be retried.
1046 		 */
1047 		vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
1048 		if (!vm_entries)
1049 			return -ENOMEM;
1050 
1051 		amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
1052 			if (num_mappings < args->num_entries) {
1053 				vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
1054 				vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
1055 				vm_entries[num_mappings].offset = mapping->offset;
1056 				vm_entries[num_mappings].flags = mapping->flags;
1057 			}
1058 			num_mappings += 1;
1059 		}
1060 
1061 		amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) {
1062 			if (num_mappings < args->num_entries) {
1063 				vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
1064 				vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
1065 				vm_entries[num_mappings].offset = mapping->offset;
1066 				vm_entries[num_mappings].flags = mapping->flags;
1067 			}
1068 			num_mappings += 1;
1069 		}
1070 
1071 		drm_exec_fini(&exec);
1072 
1073 		if (num_mappings > 0 && num_mappings <= args->num_entries)
1074 			if (copy_to_user(u64_to_user_ptr(args->value), vm_entries, num_mappings * sizeof(*vm_entries)))
1075 				r = -EFAULT;
1076 
1077 		args->num_entries = num_mappings;
1078 
1079 		kvfree(vm_entries);
1080 		break;
1081 	}
1082 	default:
1083 		drm_exec_fini(&exec);
1084 		r = -EINVAL;
1085 	}
1086 
1087 	drm_gem_object_put(gobj);
1088 	return r;
1089 out_exec:
1090 	drm_exec_fini(&exec);
1091 	drm_gem_object_put(gobj);
1092 	return r;
1093 }
1094 
1095 /**
1096  * amdgpu_gem_list_handles_ioctl - get information about a process' buffer objects
1097  *
1098  * @dev: drm device pointer
1099  * @data: drm_amdgpu_gem_list_handles
1100  * @filp: drm file pointer
1101  *
1102  * num_entries is set as an input to the size of the entries array.
1103  * num_entries is sent back as output as the number of bos in the process.
1104  * If that number is larger than the size of the array, the ioctl must
1105  * be retried.
1106  *
1107  * Returns:
1108  * 0 for success, -errno for errors.
1109  */
1110 int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
1111 				  struct drm_file *filp)
1112 {
1113 	struct drm_amdgpu_gem_list_handles *args = data;
1114 	struct drm_amdgpu_gem_list_handles_entry *bo_entries;
1115 	struct drm_gem_object *gobj;
1116 	int id, ret = 0;
1117 	int bo_index = 0;
1118 	int num_bos = 0;
1119 
1120 	spin_lock(&filp->table_lock);
1121 	idr_for_each_entry(&filp->object_idr, gobj, id)
1122 		num_bos += 1;
1123 	spin_unlock(&filp->table_lock);
1124 
1125 	if (args->num_entries < num_bos) {
1126 		args->num_entries = num_bos;
1127 		return 0;
1128 	}
1129 
1130 	if (num_bos == 0) {
1131 		args->num_entries = 0;
1132 		return 0;
1133 	}
1134 
1135 	bo_entries = kvcalloc(num_bos, sizeof(*bo_entries), GFP_KERNEL);
1136 	if (!bo_entries)
1137 		return -ENOMEM;
1138 
1139 	spin_lock(&filp->table_lock);
1140 	idr_for_each_entry(&filp->object_idr, gobj, id) {
1141 		struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1142 		struct drm_amdgpu_gem_list_handles_entry *bo_entry;
1143 
1144 		if (bo_index >= num_bos) {
1145 			ret = -EAGAIN;
1146 			break;
1147 		}
1148 
1149 		bo_entry = &bo_entries[bo_index];
1150 
1151 		bo_entry->size = amdgpu_bo_size(bo);
1152 		bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
1153 		bo_entry->preferred_domains = bo->preferred_domains;
1154 		bo_entry->gem_handle = id;
1155 		bo_entry->alignment = bo->tbo.page_alignment;
1156 
1157 		if (bo->tbo.base.import_attach)
1158 			bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
1159 
1160 		bo_index += 1;
1161 	}
1162 	spin_unlock(&filp->table_lock);
1163 
1164 	args->num_entries = bo_index;
1165 
1166 	if (!ret)
1167 		if (copy_to_user(u64_to_user_ptr(args->entries), bo_entries, num_bos * sizeof(*bo_entries)))
1168 			ret = -EFAULT;
1169 
1170 	kvfree(bo_entries);
1171 
1172 	return ret;
1173 }
1174 
1175 static int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
1176 				  int width,
1177 				  int cpp,
1178 				  bool tiled)
1179 {
1180 	int aligned = width;
1181 	int pitch_mask = 0;
1182 
1183 	switch (cpp) {
1184 	case 1:
1185 		pitch_mask = 255;
1186 		break;
1187 	case 2:
1188 		pitch_mask = 127;
1189 		break;
1190 	case 3:
1191 	case 4:
1192 		pitch_mask = 63;
1193 		break;
1194 	}
1195 
1196 	aligned += pitch_mask;
1197 	aligned &= ~pitch_mask;
1198 	return aligned * cpp;
1199 }
1200 
1201 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
1202 			    struct drm_device *dev,
1203 			    struct drm_mode_create_dumb *args)
1204 {
1205 	struct amdgpu_device *adev = drm_to_adev(dev);
1206 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1207 	struct drm_gem_object *gobj;
1208 	uint32_t handle;
1209 	u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1210 		    AMDGPU_GEM_CREATE_CPU_GTT_USWC |
1211 		    AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1212 	u32 domain;
1213 	int r;
1214 
1215 	/*
1216 	 * The buffer returned from this function should be cleared, but
1217 	 * it can only be done if the ring is enabled or we'll fail to
1218 	 * create the buffer.
1219 	 */
1220 	if (adev->mman.buffer_funcs_enabled)
1221 		flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
1222 
1223 	args->pitch = amdgpu_gem_align_pitch(adev, args->width,
1224 					     DIV_ROUND_UP(args->bpp, 8), 0);
1225 	args->size = (u64)args->pitch * args->height;
1226 	args->size = ALIGN(args->size, PAGE_SIZE);
1227 	domain = amdgpu_bo_get_preferred_domain(adev,
1228 				amdgpu_display_supported_domains(adev, flags));
1229 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
1230 				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
1231 	if (r)
1232 		return -ENOMEM;
1233 
1234 	r = drm_gem_handle_create(file_priv, gobj, &handle);
1235 	/* drop reference from allocate - handle holds it now */
1236 	drm_gem_object_put(gobj);
1237 	if (r)
1238 		return r;
1239 
1240 	args->handle = handle;
1241 	return 0;
1242 }
1243 
1244 #if defined(CONFIG_DEBUG_FS)
1245 static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
1246 {
1247 	struct amdgpu_device *adev = m->private;
1248 	struct drm_device *dev = adev_to_drm(adev);
1249 	struct drm_file *file;
1250 	int r;
1251 
1252 	r = mutex_lock_interruptible(&dev->filelist_mutex);
1253 	if (r)
1254 		return r;
1255 
1256 	list_for_each_entry(file, &dev->filelist, lhead) {
1257 		struct task_struct *task;
1258 		struct drm_gem_object *gobj;
1259 		struct pid *pid;
1260 		int id;
1261 
1262 		/*
1263 		 * Although we have a valid reference on file->pid, that does
1264 		 * not guarantee that the task_struct who called get_pid() is
1265 		 * still alive (e.g. get_pid(current) => fork() => exit()).
1266 		 * Therefore, we need to protect this ->comm access using RCU.
1267 		 */
1268 		rcu_read_lock();
1269 		pid = rcu_dereference(file->pid);
1270 		task = pid_task(pid, PIDTYPE_TGID);
1271 		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
1272 			   task ? task->comm : "<unknown>");
1273 		rcu_read_unlock();
1274 
1275 		spin_lock(&file->table_lock);
1276 		idr_for_each_entry(&file->object_idr, gobj, id) {
1277 			struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1278 
1279 			amdgpu_bo_print_info(id, bo, m);
1280 		}
1281 		spin_unlock(&file->table_lock);
1282 	}
1283 
1284 	mutex_unlock(&dev->filelist_mutex);
1285 	return 0;
1286 }
1287 
1288 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_gem_info);
1289 
1290 #endif
1291 
1292 void amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
1293 {
1294 #if defined(CONFIG_DEBUG_FS)
1295 	struct drm_minor *minor = adev_to_drm(adev)->primary;
1296 	struct dentry *root = minor->debugfs_root;
1297 
1298 	debugfs_create_file("amdgpu_gem_info", 0444, root, adev,
1299 			    &amdgpu_debugfs_gem_info_fops);
1300 #endif
1301 }
1302