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