xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c (revision ee15c8bf5d77a306614bdefe33828310662dee05)
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 
40 #include "amdgpu.h"
41 #include "amdgpu_display.h"
42 #include "amdgpu_dma_buf.h"
43 #include "amdgpu_hmm.h"
44 #include "amdgpu_xgmi.h"
45 
46 static const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
47 
48 static vm_fault_t amdgpu_gem_fault(struct vm_fault *vmf)
49 {
50 	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
51 	struct drm_device *ddev = bo->base.dev;
52 	vm_fault_t ret;
53 	int idx;
54 
55 	ret = ttm_bo_vm_reserve(bo, vmf);
56 	if (ret)
57 		return ret;
58 
59 	if (drm_dev_enter(ddev, &idx)) {
60 		ret = amdgpu_bo_fault_reserve_notify(bo);
61 		if (ret) {
62 			drm_dev_exit(idx);
63 			goto unlock;
64 		}
65 
66 		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
67 					       TTM_BO_VM_NUM_PREFAULT);
68 
69 		drm_dev_exit(idx);
70 	} else {
71 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
72 	}
73 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
74 		return ret;
75 
76 unlock:
77 	dma_resv_unlock(bo->base.resv);
78 	return ret;
79 }
80 
81 static const struct vm_operations_struct amdgpu_gem_vm_ops = {
82 	.fault = amdgpu_gem_fault,
83 	.open = ttm_bo_vm_open,
84 	.close = ttm_bo_vm_close,
85 	.access = ttm_bo_vm_access
86 };
87 
88 static void amdgpu_gem_object_free(struct drm_gem_object *gobj)
89 {
90 	struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
91 
92 	if (robj) {
93 		amdgpu_hmm_unregister(robj);
94 		amdgpu_bo_unref(&robj);
95 	}
96 }
97 
98 int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
99 			     int alignment, u32 initial_domain,
100 			     u64 flags, enum ttm_bo_type type,
101 			     struct dma_resv *resv,
102 			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
103 {
104 	struct amdgpu_bo *bo;
105 	struct amdgpu_bo_user *ubo;
106 	struct amdgpu_bo_param bp;
107 	int r;
108 
109 	memset(&bp, 0, sizeof(bp));
110 	*obj = NULL;
111 
112 	bp.size = size;
113 	bp.byte_align = alignment;
114 	bp.type = type;
115 	bp.resv = resv;
116 	bp.preferred_domain = initial_domain;
117 	bp.flags = flags;
118 	bp.domain = initial_domain;
119 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
120 	bp.xcp_id_plus1 = xcp_id_plus1;
121 
122 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
123 	if (r)
124 		return r;
125 
126 	bo = &ubo->bo;
127 	*obj = &bo->tbo.base;
128 	(*obj)->funcs = &amdgpu_gem_object_funcs;
129 
130 	return 0;
131 }
132 
133 void amdgpu_gem_force_release(struct amdgpu_device *adev)
134 {
135 	struct drm_device *ddev = adev_to_drm(adev);
136 	struct drm_file *file;
137 
138 	mutex_lock(&ddev->filelist_mutex);
139 
140 	list_for_each_entry(file, &ddev->filelist, lhead) {
141 		struct drm_gem_object *gobj;
142 		int handle;
143 
144 		WARN_ONCE(1, "Still active user space clients!\n");
145 		spin_lock(&file->table_lock);
146 		idr_for_each_entry(&file->object_idr, gobj, handle) {
147 			WARN_ONCE(1, "And also active allocations!\n");
148 			drm_gem_object_put(gobj);
149 		}
150 		idr_destroy(&file->object_idr);
151 		spin_unlock(&file->table_lock);
152 	}
153 
154 	mutex_unlock(&ddev->filelist_mutex);
155 }
156 
157 /*
158  * Call from drm_gem_handle_create which appear in both new and open ioctl
159  * case.
160  */
161 static int amdgpu_gem_object_open(struct drm_gem_object *obj,
162 				  struct drm_file *file_priv)
163 {
164 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
165 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
166 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
167 	struct amdgpu_vm *vm = &fpriv->vm;
168 	struct amdgpu_bo_va *bo_va;
169 	struct mm_struct *mm;
170 	int r;
171 
172 	mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
173 	if (mm && mm != current->mm)
174 		return -EPERM;
175 
176 	if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
177 	    abo->tbo.base.resv != vm->root.bo->tbo.base.resv)
178 		return -EPERM;
179 
180 	r = amdgpu_bo_reserve(abo, false);
181 	if (r)
182 		return r;
183 
184 	bo_va = amdgpu_vm_bo_find(vm, abo);
185 	if (!bo_va)
186 		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
187 	else
188 		++bo_va->ref_count;
189 	amdgpu_bo_unreserve(abo);
190 
191 	/* Validate and add eviction fence to DMABuf imports with dynamic
192 	 * attachment in compute VMs. Re-validation will be done by
193 	 * amdgpu_vm_validate. Fences are on the reservation shared with the
194 	 * export, which is currently required to be validated and fenced
195 	 * already by amdgpu_amdkfd_gpuvm_restore_process_bos.
196 	 *
197 	 * Nested locking below for the case that a GEM object is opened in
198 	 * kfd_mem_export_dmabuf. Since the lock below is only taken for imports,
199 	 * but not for export, this is a different lock class that cannot lead to
200 	 * circular lock dependencies.
201 	 */
202 	if (!vm->is_compute_context || !vm->process_info)
203 		return 0;
204 	if (!obj->import_attach ||
205 	    !dma_buf_is_dynamic(obj->import_attach->dmabuf))
206 		return 0;
207 	mutex_lock_nested(&vm->process_info->lock, 1);
208 	if (!WARN_ON(!vm->process_info->eviction_fence)) {
209 		r = amdgpu_amdkfd_bo_validate_and_fence(abo, AMDGPU_GEM_DOMAIN_GTT,
210 							&vm->process_info->eviction_fence->base);
211 		if (r)
212 			dev_warn(adev->dev, "%d: validate_and_fence failed: %d\n",
213 				 vm->task_info.pid, r);
214 	}
215 	mutex_unlock(&vm->process_info->lock);
216 
217 	return r;
218 }
219 
220 static void amdgpu_gem_object_close(struct drm_gem_object *obj,
221 				    struct drm_file *file_priv)
222 {
223 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
224 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
225 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
226 	struct amdgpu_vm *vm = &fpriv->vm;
227 
228 	struct dma_fence *fence = NULL;
229 	struct amdgpu_bo_va *bo_va;
230 	struct drm_exec exec;
231 	long r;
232 
233 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
234 	drm_exec_until_all_locked(&exec) {
235 		r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
236 		drm_exec_retry_on_contention(&exec);
237 		if (unlikely(r))
238 			goto out_unlock;
239 
240 		r = amdgpu_vm_lock_pd(vm, &exec, 0);
241 		drm_exec_retry_on_contention(&exec);
242 		if (unlikely(r))
243 			goto out_unlock;
244 	}
245 
246 	bo_va = amdgpu_vm_bo_find(vm, bo);
247 	if (!bo_va || --bo_va->ref_count)
248 		goto out_unlock;
249 
250 	amdgpu_vm_bo_del(adev, bo_va);
251 	if (!amdgpu_vm_ready(vm))
252 		goto out_unlock;
253 
254 	r = amdgpu_vm_clear_freed(adev, vm, &fence);
255 	if (unlikely(r < 0))
256 		dev_err(adev->dev, "failed to clear page "
257 			"tables on GEM object close (%ld)\n", r);
258 	if (r || !fence)
259 		goto out_unlock;
260 
261 	amdgpu_bo_fence(bo, fence, true);
262 	dma_fence_put(fence);
263 
264 out_unlock:
265 	if (r)
266 		dev_err(adev->dev, "leaking bo va (%ld)\n", r);
267 	drm_exec_fini(&exec);
268 }
269 
270 static int amdgpu_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
271 {
272 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
273 
274 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
275 		return -EPERM;
276 	if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
277 		return -EPERM;
278 
279 	/* Workaround for Thunk bug creating PROT_NONE,MAP_PRIVATE mappings
280 	 * for debugger access to invisible VRAM. Should have used MAP_SHARED
281 	 * instead. Clearing VM_MAYWRITE prevents the mapping from ever
282 	 * becoming writable and makes is_cow_mapping(vm_flags) false.
283 	 */
284 	if (is_cow_mapping(vma->vm_flags) &&
285 	    !(vma->vm_flags & VM_ACCESS_FLAGS))
286 		vm_flags_clear(vma, VM_MAYWRITE);
287 
288 	return drm_gem_ttm_mmap(obj, vma);
289 }
290 
291 static const struct drm_gem_object_funcs amdgpu_gem_object_funcs = {
292 	.free = amdgpu_gem_object_free,
293 	.open = amdgpu_gem_object_open,
294 	.close = amdgpu_gem_object_close,
295 	.export = amdgpu_gem_prime_export,
296 	.vmap = drm_gem_ttm_vmap,
297 	.vunmap = drm_gem_ttm_vunmap,
298 	.mmap = amdgpu_gem_object_mmap,
299 	.vm_ops = &amdgpu_gem_vm_ops,
300 };
301 
302 /*
303  * GEM ioctls.
304  */
305 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
306 			    struct drm_file *filp)
307 {
308 	struct amdgpu_device *adev = drm_to_adev(dev);
309 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
310 	struct amdgpu_vm *vm = &fpriv->vm;
311 	union drm_amdgpu_gem_create *args = data;
312 	uint64_t flags = args->in.domain_flags;
313 	uint64_t size = args->in.bo_size;
314 	struct dma_resv *resv = NULL;
315 	struct drm_gem_object *gobj;
316 	uint32_t handle, initial_domain;
317 	int r;
318 
319 	/* reject DOORBELLs until userspace code to use it is available */
320 	if (args->in.domains & AMDGPU_GEM_DOMAIN_DOORBELL)
321 		return -EINVAL;
322 
323 	/* reject invalid gem flags */
324 	if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
325 		      AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
326 		      AMDGPU_GEM_CREATE_CPU_GTT_USWC |
327 		      AMDGPU_GEM_CREATE_VRAM_CLEARED |
328 		      AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
329 		      AMDGPU_GEM_CREATE_EXPLICIT_SYNC |
330 		      AMDGPU_GEM_CREATE_ENCRYPTED |
331 		      AMDGPU_GEM_CREATE_DISCARDABLE))
332 		return -EINVAL;
333 
334 	/* reject invalid gem domains */
335 	if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
336 		return -EINVAL;
337 
338 	if (!amdgpu_is_tmz(adev) && (flags & AMDGPU_GEM_CREATE_ENCRYPTED)) {
339 		DRM_NOTE_ONCE("Cannot allocate secure buffer since TMZ is disabled\n");
340 		return -EINVAL;
341 	}
342 
343 	/* create a gem object to contain this object in */
344 	if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
345 	    AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
346 		if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
347 			/* if gds bo is created from user space, it must be
348 			 * passed to bo list
349 			 */
350 			DRM_ERROR("GDS bo cannot be per-vm-bo\n");
351 			return -EINVAL;
352 		}
353 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
354 	}
355 
356 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
357 		r = amdgpu_bo_reserve(vm->root.bo, false);
358 		if (r)
359 			return r;
360 
361 		resv = vm->root.bo->tbo.base.resv;
362 	}
363 
364 	initial_domain = (u32)(0xffffffff & args->in.domains);
365 retry:
366 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
367 				     initial_domain,
368 				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
369 	if (r && r != -ERESTARTSYS) {
370 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
371 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
372 			goto retry;
373 		}
374 
375 		if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
376 			initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
377 			goto retry;
378 		}
379 		DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n",
380 				size, initial_domain, args->in.alignment, r);
381 	}
382 
383 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
384 		if (!r) {
385 			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
386 
387 			abo->parent = amdgpu_bo_ref(vm->root.bo);
388 		}
389 		amdgpu_bo_unreserve(vm->root.bo);
390 	}
391 	if (r)
392 		return r;
393 
394 	r = drm_gem_handle_create(filp, gobj, &handle);
395 	/* drop reference from allocate - handle holds it now */
396 	drm_gem_object_put(gobj);
397 	if (r)
398 		return r;
399 
400 	memset(args, 0, sizeof(*args));
401 	args->out.handle = handle;
402 	return 0;
403 }
404 
405 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
406 			     struct drm_file *filp)
407 {
408 	struct ttm_operation_ctx ctx = { true, false };
409 	struct amdgpu_device *adev = drm_to_adev(dev);
410 	struct drm_amdgpu_gem_userptr *args = data;
411 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
412 	struct drm_gem_object *gobj;
413 	struct hmm_range *range;
414 	struct amdgpu_bo *bo;
415 	uint32_t handle;
416 	int r;
417 
418 	args->addr = untagged_addr(args->addr);
419 
420 	if (offset_in_page(args->addr | args->size))
421 		return -EINVAL;
422 
423 	/* reject unknown flag values */
424 	if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
425 	    AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
426 	    AMDGPU_GEM_USERPTR_REGISTER))
427 		return -EINVAL;
428 
429 	if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
430 	     !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
431 
432 		/* if we want to write to it we must install a MMU notifier */
433 		return -EACCES;
434 	}
435 
436 	/* create a gem object to contain this object in */
437 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
438 				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
439 	if (r)
440 		return r;
441 
442 	bo = gem_to_amdgpu_bo(gobj);
443 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
444 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
445 	r = amdgpu_ttm_tt_set_userptr(&bo->tbo, args->addr, args->flags);
446 	if (r)
447 		goto release_object;
448 
449 	r = amdgpu_hmm_register(bo, args->addr);
450 	if (r)
451 		goto release_object;
452 
453 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
454 		r = amdgpu_ttm_tt_get_user_pages(bo, bo->tbo.ttm->pages,
455 						 &range);
456 		if (r)
457 			goto release_object;
458 
459 		r = amdgpu_bo_reserve(bo, true);
460 		if (r)
461 			goto user_pages_done;
462 
463 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
464 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
465 		amdgpu_bo_unreserve(bo);
466 		if (r)
467 			goto user_pages_done;
468 	}
469 
470 	r = drm_gem_handle_create(filp, gobj, &handle);
471 	if (r)
472 		goto user_pages_done;
473 
474 	args->handle = handle;
475 
476 user_pages_done:
477 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
478 		amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, range);
479 
480 release_object:
481 	drm_gem_object_put(gobj);
482 
483 	return r;
484 }
485 
486 int amdgpu_mode_dumb_mmap(struct drm_file *filp,
487 			  struct drm_device *dev,
488 			  uint32_t handle, uint64_t *offset_p)
489 {
490 	struct drm_gem_object *gobj;
491 	struct amdgpu_bo *robj;
492 
493 	gobj = drm_gem_object_lookup(filp, handle);
494 	if (!gobj)
495 		return -ENOENT;
496 
497 	robj = gem_to_amdgpu_bo(gobj);
498 	if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
499 	    (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
500 		drm_gem_object_put(gobj);
501 		return -EPERM;
502 	}
503 	*offset_p = amdgpu_bo_mmap_offset(robj);
504 	drm_gem_object_put(gobj);
505 	return 0;
506 }
507 
508 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
509 			  struct drm_file *filp)
510 {
511 	union drm_amdgpu_gem_mmap *args = data;
512 	uint32_t handle = args->in.handle;
513 
514 	memset(args, 0, sizeof(*args));
515 	return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
516 }
517 
518 /**
519  * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
520  *
521  * @timeout_ns: timeout in ns
522  *
523  * Calculate the timeout in jiffies from an absolute timeout in ns.
524  */
525 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
526 {
527 	unsigned long timeout_jiffies;
528 	ktime_t timeout;
529 
530 	/* clamp timeout if it's to large */
531 	if (((int64_t)timeout_ns) < 0)
532 		return MAX_SCHEDULE_TIMEOUT;
533 
534 	timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
535 	if (ktime_to_ns(timeout) < 0)
536 		return 0;
537 
538 	timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
539 	/*  clamp timeout to avoid unsigned-> signed overflow */
540 	if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT)
541 		return MAX_SCHEDULE_TIMEOUT - 1;
542 
543 	return timeout_jiffies;
544 }
545 
546 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
547 			      struct drm_file *filp)
548 {
549 	union drm_amdgpu_gem_wait_idle *args = data;
550 	struct drm_gem_object *gobj;
551 	struct amdgpu_bo *robj;
552 	uint32_t handle = args->in.handle;
553 	unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
554 	int r = 0;
555 	long ret;
556 
557 	gobj = drm_gem_object_lookup(filp, handle);
558 	if (!gobj)
559 		return -ENOENT;
560 
561 	robj = gem_to_amdgpu_bo(gobj);
562 	ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ,
563 				    true, timeout);
564 
565 	/* ret == 0 means not signaled,
566 	 * ret > 0 means signaled
567 	 * ret < 0 means interrupted before timeout
568 	 */
569 	if (ret >= 0) {
570 		memset(args, 0, sizeof(*args));
571 		args->out.status = (ret == 0);
572 	} else
573 		r = ret;
574 
575 	drm_gem_object_put(gobj);
576 	return r;
577 }
578 
579 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
580 				struct drm_file *filp)
581 {
582 	struct drm_amdgpu_gem_metadata *args = data;
583 	struct drm_gem_object *gobj;
584 	struct amdgpu_bo *robj;
585 	int r = -1;
586 
587 	DRM_DEBUG("%d\n", args->handle);
588 	gobj = drm_gem_object_lookup(filp, args->handle);
589 	if (gobj == NULL)
590 		return -ENOENT;
591 	robj = gem_to_amdgpu_bo(gobj);
592 
593 	r = amdgpu_bo_reserve(robj, false);
594 	if (unlikely(r != 0))
595 		goto out;
596 
597 	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
598 		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
599 		r = amdgpu_bo_get_metadata(robj, args->data.data,
600 					   sizeof(args->data.data),
601 					   &args->data.data_size_bytes,
602 					   &args->data.flags);
603 	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
604 		if (args->data.data_size_bytes > sizeof(args->data.data)) {
605 			r = -EINVAL;
606 			goto unreserve;
607 		}
608 		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
609 		if (!r)
610 			r = amdgpu_bo_set_metadata(robj, args->data.data,
611 						   args->data.data_size_bytes,
612 						   args->data.flags);
613 	}
614 
615 unreserve:
616 	amdgpu_bo_unreserve(robj);
617 out:
618 	drm_gem_object_put(gobj);
619 	return r;
620 }
621 
622 /**
623  * amdgpu_gem_va_update_vm -update the bo_va in its VM
624  *
625  * @adev: amdgpu_device pointer
626  * @vm: vm to update
627  * @bo_va: bo_va to update
628  * @operation: map, unmap or clear
629  *
630  * Update the bo_va directly after setting its address. Errors are not
631  * vital here, so they are not reported back to userspace.
632  */
633 static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
634 				    struct amdgpu_vm *vm,
635 				    struct amdgpu_bo_va *bo_va,
636 				    uint32_t operation)
637 {
638 	int r;
639 
640 	if (!amdgpu_vm_ready(vm))
641 		return;
642 
643 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
644 	if (r)
645 		goto error;
646 
647 	if (operation == AMDGPU_VA_OP_MAP ||
648 	    operation == AMDGPU_VA_OP_REPLACE) {
649 		r = amdgpu_vm_bo_update(adev, bo_va, false);
650 		if (r)
651 			goto error;
652 	}
653 
654 	r = amdgpu_vm_update_pdes(adev, vm, false);
655 
656 error:
657 	if (r && r != -ERESTARTSYS)
658 		DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
659 }
660 
661 /**
662  * amdgpu_gem_va_map_flags - map GEM UAPI flags into hardware flags
663  *
664  * @adev: amdgpu_device pointer
665  * @flags: GEM UAPI flags
666  *
667  * Returns the GEM UAPI flags mapped into hardware for the ASIC.
668  */
669 uint64_t amdgpu_gem_va_map_flags(struct amdgpu_device *adev, uint32_t flags)
670 {
671 	uint64_t pte_flag = 0;
672 
673 	if (flags & AMDGPU_VM_PAGE_EXECUTABLE)
674 		pte_flag |= AMDGPU_PTE_EXECUTABLE;
675 	if (flags & AMDGPU_VM_PAGE_READABLE)
676 		pte_flag |= AMDGPU_PTE_READABLE;
677 	if (flags & AMDGPU_VM_PAGE_WRITEABLE)
678 		pte_flag |= AMDGPU_PTE_WRITEABLE;
679 	if (flags & AMDGPU_VM_PAGE_PRT)
680 		pte_flag |= AMDGPU_PTE_PRT;
681 	if (flags & AMDGPU_VM_PAGE_NOALLOC)
682 		pte_flag |= AMDGPU_PTE_NOALLOC;
683 
684 	if (adev->gmc.gmc_funcs->map_mtype)
685 		pte_flag |= amdgpu_gmc_map_mtype(adev,
686 						 flags & AMDGPU_VM_MTYPE_MASK);
687 
688 	return pte_flag;
689 }
690 
691 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
692 			  struct drm_file *filp)
693 {
694 	const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
695 		AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
696 		AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
697 		AMDGPU_VM_PAGE_NOALLOC;
698 	const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
699 		AMDGPU_VM_PAGE_PRT;
700 
701 	struct drm_amdgpu_gem_va *args = data;
702 	struct drm_gem_object *gobj;
703 	struct amdgpu_device *adev = drm_to_adev(dev);
704 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
705 	struct amdgpu_bo *abo;
706 	struct amdgpu_bo_va *bo_va;
707 	struct drm_exec exec;
708 	uint64_t va_flags;
709 	uint64_t vm_size;
710 	int r = 0;
711 
712 	if (args->va_address < AMDGPU_VA_RESERVED_BOTTOM) {
713 		dev_dbg(dev->dev,
714 			"va_address 0x%llx is in reserved area 0x%llx\n",
715 			args->va_address, AMDGPU_VA_RESERVED_BOTTOM);
716 		return -EINVAL;
717 	}
718 
719 	if (args->va_address >= AMDGPU_GMC_HOLE_START &&
720 	    args->va_address < AMDGPU_GMC_HOLE_END) {
721 		dev_dbg(dev->dev,
722 			"va_address 0x%llx is in VA hole 0x%llx-0x%llx\n",
723 			args->va_address, AMDGPU_GMC_HOLE_START,
724 			AMDGPU_GMC_HOLE_END);
725 		return -EINVAL;
726 	}
727 
728 	args->va_address &= AMDGPU_GMC_HOLE_MASK;
729 
730 	vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
731 	vm_size -= AMDGPU_VA_RESERVED_TOP;
732 	if (args->va_address + args->map_size > vm_size) {
733 		dev_dbg(dev->dev,
734 			"va_address 0x%llx is in top reserved area 0x%llx\n",
735 			args->va_address + args->map_size, vm_size);
736 		return -EINVAL;
737 	}
738 
739 	if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
740 		dev_dbg(dev->dev, "invalid flags combination 0x%08X\n",
741 			args->flags);
742 		return -EINVAL;
743 	}
744 
745 	switch (args->operation) {
746 	case AMDGPU_VA_OP_MAP:
747 	case AMDGPU_VA_OP_UNMAP:
748 	case AMDGPU_VA_OP_CLEAR:
749 	case AMDGPU_VA_OP_REPLACE:
750 		break;
751 	default:
752 		dev_dbg(dev->dev, "unsupported operation %d\n",
753 			args->operation);
754 		return -EINVAL;
755 	}
756 
757 	if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
758 	    !(args->flags & AMDGPU_VM_PAGE_PRT)) {
759 		gobj = drm_gem_object_lookup(filp, args->handle);
760 		if (gobj == NULL)
761 			return -ENOENT;
762 		abo = gem_to_amdgpu_bo(gobj);
763 	} else {
764 		gobj = NULL;
765 		abo = NULL;
766 	}
767 
768 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
769 		      DRM_EXEC_IGNORE_DUPLICATES, 0);
770 	drm_exec_until_all_locked(&exec) {
771 		if (gobj) {
772 			r = drm_exec_lock_obj(&exec, gobj);
773 			drm_exec_retry_on_contention(&exec);
774 			if (unlikely(r))
775 				goto error;
776 		}
777 
778 		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
779 		drm_exec_retry_on_contention(&exec);
780 		if (unlikely(r))
781 			goto error;
782 	}
783 
784 	if (abo) {
785 		bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
786 		if (!bo_va) {
787 			r = -ENOENT;
788 			goto error;
789 		}
790 	} else if (args->operation != AMDGPU_VA_OP_CLEAR) {
791 		bo_va = fpriv->prt_va;
792 	} else {
793 		bo_va = NULL;
794 	}
795 
796 	switch (args->operation) {
797 	case AMDGPU_VA_OP_MAP:
798 		va_flags = amdgpu_gem_va_map_flags(adev, args->flags);
799 		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
800 				     args->offset_in_bo, args->map_size,
801 				     va_flags);
802 		break;
803 	case AMDGPU_VA_OP_UNMAP:
804 		r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
805 		break;
806 
807 	case AMDGPU_VA_OP_CLEAR:
808 		r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
809 						args->va_address,
810 						args->map_size);
811 		break;
812 	case AMDGPU_VA_OP_REPLACE:
813 		va_flags = amdgpu_gem_va_map_flags(adev, args->flags);
814 		r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
815 					     args->offset_in_bo, args->map_size,
816 					     va_flags);
817 		break;
818 	default:
819 		break;
820 	}
821 	if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !adev->debug_vm)
822 		amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
823 					args->operation);
824 
825 error:
826 	drm_exec_fini(&exec);
827 	drm_gem_object_put(gobj);
828 	return r;
829 }
830 
831 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
832 			struct drm_file *filp)
833 {
834 	struct amdgpu_device *adev = drm_to_adev(dev);
835 	struct drm_amdgpu_gem_op *args = data;
836 	struct drm_gem_object *gobj;
837 	struct amdgpu_vm_bo_base *base;
838 	struct amdgpu_bo *robj;
839 	int r;
840 
841 	gobj = drm_gem_object_lookup(filp, args->handle);
842 	if (!gobj)
843 		return -ENOENT;
844 
845 	robj = gem_to_amdgpu_bo(gobj);
846 
847 	r = amdgpu_bo_reserve(robj, false);
848 	if (unlikely(r))
849 		goto out;
850 
851 	switch (args->op) {
852 	case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
853 		struct drm_amdgpu_gem_create_in info;
854 		void __user *out = u64_to_user_ptr(args->value);
855 
856 		info.bo_size = robj->tbo.base.size;
857 		info.alignment = robj->tbo.page_alignment << PAGE_SHIFT;
858 		info.domains = robj->preferred_domains;
859 		info.domain_flags = robj->flags;
860 		amdgpu_bo_unreserve(robj);
861 		if (copy_to_user(out, &info, sizeof(info)))
862 			r = -EFAULT;
863 		break;
864 	}
865 	case AMDGPU_GEM_OP_SET_PLACEMENT:
866 		if (robj->tbo.base.import_attach &&
867 		    args->value & AMDGPU_GEM_DOMAIN_VRAM) {
868 			r = -EINVAL;
869 			amdgpu_bo_unreserve(robj);
870 			break;
871 		}
872 		if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
873 			r = -EPERM;
874 			amdgpu_bo_unreserve(robj);
875 			break;
876 		}
877 		for (base = robj->vm_bo; base; base = base->next)
878 			if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev),
879 				amdgpu_ttm_adev(base->vm->root.bo->tbo.bdev))) {
880 				r = -EINVAL;
881 				amdgpu_bo_unreserve(robj);
882 				goto out;
883 			}
884 
885 
886 		robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
887 							AMDGPU_GEM_DOMAIN_GTT |
888 							AMDGPU_GEM_DOMAIN_CPU);
889 		robj->allowed_domains = robj->preferred_domains;
890 		if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
891 			robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
892 
893 		if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
894 			amdgpu_vm_bo_invalidate(adev, robj, true);
895 
896 		amdgpu_bo_unreserve(robj);
897 		break;
898 	default:
899 		amdgpu_bo_unreserve(robj);
900 		r = -EINVAL;
901 	}
902 
903 out:
904 	drm_gem_object_put(gobj);
905 	return r;
906 }
907 
908 static int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
909 				  int width,
910 				  int cpp,
911 				  bool tiled)
912 {
913 	int aligned = width;
914 	int pitch_mask = 0;
915 
916 	switch (cpp) {
917 	case 1:
918 		pitch_mask = 255;
919 		break;
920 	case 2:
921 		pitch_mask = 127;
922 		break;
923 	case 3:
924 	case 4:
925 		pitch_mask = 63;
926 		break;
927 	}
928 
929 	aligned += pitch_mask;
930 	aligned &= ~pitch_mask;
931 	return aligned * cpp;
932 }
933 
934 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
935 			    struct drm_device *dev,
936 			    struct drm_mode_create_dumb *args)
937 {
938 	struct amdgpu_device *adev = drm_to_adev(dev);
939 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
940 	struct drm_gem_object *gobj;
941 	uint32_t handle;
942 	u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
943 		    AMDGPU_GEM_CREATE_CPU_GTT_USWC |
944 		    AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
945 	u32 domain;
946 	int r;
947 
948 	/*
949 	 * The buffer returned from this function should be cleared, but
950 	 * it can only be done if the ring is enabled or we'll fail to
951 	 * create the buffer.
952 	 */
953 	if (adev->mman.buffer_funcs_enabled)
954 		flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
955 
956 	args->pitch = amdgpu_gem_align_pitch(adev, args->width,
957 					     DIV_ROUND_UP(args->bpp, 8), 0);
958 	args->size = (u64)args->pitch * args->height;
959 	args->size = ALIGN(args->size, PAGE_SIZE);
960 	domain = amdgpu_bo_get_preferred_domain(adev,
961 				amdgpu_display_supported_domains(adev, flags));
962 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
963 				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
964 	if (r)
965 		return -ENOMEM;
966 
967 	r = drm_gem_handle_create(file_priv, gobj, &handle);
968 	/* drop reference from allocate - handle holds it now */
969 	drm_gem_object_put(gobj);
970 	if (r)
971 		return r;
972 
973 	args->handle = handle;
974 	return 0;
975 }
976 
977 #if defined(CONFIG_DEBUG_FS)
978 static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
979 {
980 	struct amdgpu_device *adev = m->private;
981 	struct drm_device *dev = adev_to_drm(adev);
982 	struct drm_file *file;
983 	int r;
984 
985 	r = mutex_lock_interruptible(&dev->filelist_mutex);
986 	if (r)
987 		return r;
988 
989 	list_for_each_entry(file, &dev->filelist, lhead) {
990 		struct task_struct *task;
991 		struct drm_gem_object *gobj;
992 		struct pid *pid;
993 		int id;
994 
995 		/*
996 		 * Although we have a valid reference on file->pid, that does
997 		 * not guarantee that the task_struct who called get_pid() is
998 		 * still alive (e.g. get_pid(current) => fork() => exit()).
999 		 * Therefore, we need to protect this ->comm access using RCU.
1000 		 */
1001 		rcu_read_lock();
1002 		pid = rcu_dereference(file->pid);
1003 		task = pid_task(pid, PIDTYPE_TGID);
1004 		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
1005 			   task ? task->comm : "<unknown>");
1006 		rcu_read_unlock();
1007 
1008 		spin_lock(&file->table_lock);
1009 		idr_for_each_entry(&file->object_idr, gobj, id) {
1010 			struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1011 
1012 			amdgpu_bo_print_info(id, bo, m);
1013 		}
1014 		spin_unlock(&file->table_lock);
1015 	}
1016 
1017 	mutex_unlock(&dev->filelist_mutex);
1018 	return 0;
1019 }
1020 
1021 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_gem_info);
1022 
1023 #endif
1024 
1025 void amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
1026 {
1027 #if defined(CONFIG_DEBUG_FS)
1028 	struct drm_minor *minor = adev_to_drm(adev)->primary;
1029 	struct dentry *root = minor->debugfs_root;
1030 
1031 	debugfs_create_file("amdgpu_gem_info", 0444, root, adev,
1032 			    &amdgpu_debugfs_gem_info_fops);
1033 #endif
1034 }
1035