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/pagemap.h>
30 #include <drm/drmP.h>
31 #include <drm/amdgpu_drm.h>
32 #include "amdgpu.h"
33 #include "amdgpu_display.h"
34 #include "amdgpu_xgmi.h"
35 
36 void amdgpu_gem_object_free(struct drm_gem_object *gobj)
37 {
38 	struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
39 
40 	if (robj) {
41 		amdgpu_mn_unregister(robj);
42 		amdgpu_bo_unref(&robj);
43 	}
44 }
45 
46 int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
47 			     int alignment, u32 initial_domain,
48 			     u64 flags, enum ttm_bo_type type,
49 			     struct reservation_object *resv,
50 			     struct drm_gem_object **obj)
51 {
52 	struct amdgpu_bo *bo;
53 	struct amdgpu_bo_param bp;
54 	int r;
55 
56 	memset(&bp, 0, sizeof(bp));
57 	*obj = NULL;
58 
59 	bp.size = size;
60 	bp.byte_align = alignment;
61 	bp.type = type;
62 	bp.resv = resv;
63 	bp.preferred_domain = initial_domain;
64 retry:
65 	bp.flags = flags;
66 	bp.domain = initial_domain;
67 	r = amdgpu_bo_create(adev, &bp, &bo);
68 	if (r) {
69 		if (r != -ERESTARTSYS) {
70 			if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
71 				flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
72 				goto retry;
73 			}
74 
75 			if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
76 				initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
77 				goto retry;
78 			}
79 			DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
80 				  size, initial_domain, alignment, r);
81 		}
82 		return r;
83 	}
84 	*obj = &bo->gem_base;
85 
86 	return 0;
87 }
88 
89 void amdgpu_gem_force_release(struct amdgpu_device *adev)
90 {
91 	struct drm_device *ddev = adev->ddev;
92 	struct drm_file *file;
93 
94 	mutex_lock(&ddev->filelist_mutex);
95 
96 	list_for_each_entry(file, &ddev->filelist, lhead) {
97 		struct drm_gem_object *gobj;
98 		int handle;
99 
100 		WARN_ONCE(1, "Still active user space clients!\n");
101 		spin_lock(&file->table_lock);
102 		idr_for_each_entry(&file->object_idr, gobj, handle) {
103 			WARN_ONCE(1, "And also active allocations!\n");
104 			drm_gem_object_put_unlocked(gobj);
105 		}
106 		idr_destroy(&file->object_idr);
107 		spin_unlock(&file->table_lock);
108 	}
109 
110 	mutex_unlock(&ddev->filelist_mutex);
111 }
112 
113 /*
114  * Call from drm_gem_handle_create which appear in both new and open ioctl
115  * case.
116  */
117 int amdgpu_gem_object_open(struct drm_gem_object *obj,
118 			   struct drm_file *file_priv)
119 {
120 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
121 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
122 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
123 	struct amdgpu_vm *vm = &fpriv->vm;
124 	struct amdgpu_bo_va *bo_va;
125 	struct mm_struct *mm;
126 	int r;
127 
128 	mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
129 	if (mm && mm != current->mm)
130 		return -EPERM;
131 
132 	if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
133 	    abo->tbo.resv != vm->root.base.bo->tbo.resv)
134 		return -EPERM;
135 
136 	r = amdgpu_bo_reserve(abo, false);
137 	if (r)
138 		return r;
139 
140 	bo_va = amdgpu_vm_bo_find(vm, abo);
141 	if (!bo_va) {
142 		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
143 	} else {
144 		++bo_va->ref_count;
145 	}
146 	amdgpu_bo_unreserve(abo);
147 	return 0;
148 }
149 
150 void amdgpu_gem_object_close(struct drm_gem_object *obj,
151 			     struct drm_file *file_priv)
152 {
153 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
154 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
155 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
156 	struct amdgpu_vm *vm = &fpriv->vm;
157 
158 	struct amdgpu_bo_list_entry vm_pd;
159 	struct list_head list, duplicates;
160 	struct ttm_validate_buffer tv;
161 	struct ww_acquire_ctx ticket;
162 	struct amdgpu_bo_va *bo_va;
163 	int r;
164 
165 	INIT_LIST_HEAD(&list);
166 	INIT_LIST_HEAD(&duplicates);
167 
168 	tv.bo = &bo->tbo;
169 	tv.num_shared = 1;
170 	list_add(&tv.head, &list);
171 
172 	amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
173 
174 	r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates, false);
175 	if (r) {
176 		dev_err(adev->dev, "leaking bo va because "
177 			"we fail to reserve bo (%d)\n", r);
178 		return;
179 	}
180 	bo_va = amdgpu_vm_bo_find(vm, bo);
181 	if (bo_va && --bo_va->ref_count == 0) {
182 		amdgpu_vm_bo_rmv(adev, bo_va);
183 
184 		if (amdgpu_vm_ready(vm)) {
185 			struct dma_fence *fence = NULL;
186 
187 			r = amdgpu_vm_clear_freed(adev, vm, &fence);
188 			if (unlikely(r)) {
189 				dev_err(adev->dev, "failed to clear page "
190 					"tables on GEM object close (%d)\n", r);
191 			}
192 
193 			if (fence) {
194 				amdgpu_bo_fence(bo, fence, true);
195 				dma_fence_put(fence);
196 			}
197 		}
198 	}
199 	ttm_eu_backoff_reservation(&ticket, &list);
200 }
201 
202 /*
203  * GEM ioctls.
204  */
205 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
206 			    struct drm_file *filp)
207 {
208 	struct amdgpu_device *adev = dev->dev_private;
209 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
210 	struct amdgpu_vm *vm = &fpriv->vm;
211 	union drm_amdgpu_gem_create *args = data;
212 	uint64_t flags = args->in.domain_flags;
213 	uint64_t size = args->in.bo_size;
214 	struct reservation_object *resv = NULL;
215 	struct drm_gem_object *gobj;
216 	uint32_t handle;
217 	int r;
218 
219 	/* reject invalid gem flags */
220 	if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
221 		      AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
222 		      AMDGPU_GEM_CREATE_CPU_GTT_USWC |
223 		      AMDGPU_GEM_CREATE_VRAM_CLEARED |
224 		      AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
225 		      AMDGPU_GEM_CREATE_EXPLICIT_SYNC))
226 
227 		return -EINVAL;
228 
229 	/* reject invalid gem domains */
230 	if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
231 		return -EINVAL;
232 
233 	/* create a gem object to contain this object in */
234 	if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
235 	    AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
236 		if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
237 			/* if gds bo is created from user space, it must be
238 			 * passed to bo list
239 			 */
240 			DRM_ERROR("GDS bo cannot be per-vm-bo\n");
241 			return -EINVAL;
242 		}
243 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
244 	}
245 
246 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
247 		r = amdgpu_bo_reserve(vm->root.base.bo, false);
248 		if (r)
249 			return r;
250 
251 		resv = vm->root.base.bo->tbo.resv;
252 	}
253 
254 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
255 				     (u32)(0xffffffff & args->in.domains),
256 				     flags, ttm_bo_type_device, resv, &gobj);
257 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
258 		if (!r) {
259 			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
260 
261 			abo->parent = amdgpu_bo_ref(vm->root.base.bo);
262 		}
263 		amdgpu_bo_unreserve(vm->root.base.bo);
264 	}
265 	if (r)
266 		return r;
267 
268 	r = drm_gem_handle_create(filp, gobj, &handle);
269 	/* drop reference from allocate - handle holds it now */
270 	drm_gem_object_put_unlocked(gobj);
271 	if (r)
272 		return r;
273 
274 	memset(args, 0, sizeof(*args));
275 	args->out.handle = handle;
276 	return 0;
277 }
278 
279 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
280 			     struct drm_file *filp)
281 {
282 	struct ttm_operation_ctx ctx = { true, false };
283 	struct amdgpu_device *adev = dev->dev_private;
284 	struct drm_amdgpu_gem_userptr *args = data;
285 	struct drm_gem_object *gobj;
286 	struct amdgpu_bo *bo;
287 	uint32_t handle;
288 	int r;
289 
290 	if (offset_in_page(args->addr | args->size))
291 		return -EINVAL;
292 
293 	/* reject unknown flag values */
294 	if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
295 	    AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
296 	    AMDGPU_GEM_USERPTR_REGISTER))
297 		return -EINVAL;
298 
299 	if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
300 	     !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
301 
302 		/* if we want to write to it we must install a MMU notifier */
303 		return -EACCES;
304 	}
305 
306 	/* create a gem object to contain this object in */
307 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
308 				     0, ttm_bo_type_device, NULL, &gobj);
309 	if (r)
310 		return r;
311 
312 	bo = gem_to_amdgpu_bo(gobj);
313 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
314 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
315 	r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
316 	if (r)
317 		goto release_object;
318 
319 	if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
320 		r = amdgpu_mn_register(bo, args->addr);
321 		if (r)
322 			goto release_object;
323 	}
324 
325 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
326 		r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
327 						 bo->tbo.ttm->pages);
328 		if (r)
329 			goto release_object;
330 
331 		r = amdgpu_bo_reserve(bo, true);
332 		if (r)
333 			goto user_pages_done;
334 
335 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
336 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
337 		amdgpu_bo_unreserve(bo);
338 		if (r)
339 			goto user_pages_done;
340 	}
341 
342 	r = drm_gem_handle_create(filp, gobj, &handle);
343 	if (r)
344 		goto user_pages_done;
345 
346 	args->handle = handle;
347 
348 user_pages_done:
349 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
350 		amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm);
351 
352 release_object:
353 	drm_gem_object_put_unlocked(gobj);
354 
355 	return r;
356 }
357 
358 int amdgpu_mode_dumb_mmap(struct drm_file *filp,
359 			  struct drm_device *dev,
360 			  uint32_t handle, uint64_t *offset_p)
361 {
362 	struct drm_gem_object *gobj;
363 	struct amdgpu_bo *robj;
364 
365 	gobj = drm_gem_object_lookup(filp, handle);
366 	if (gobj == NULL) {
367 		return -ENOENT;
368 	}
369 	robj = gem_to_amdgpu_bo(gobj);
370 	if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
371 	    (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
372 		drm_gem_object_put_unlocked(gobj);
373 		return -EPERM;
374 	}
375 	*offset_p = amdgpu_bo_mmap_offset(robj);
376 	drm_gem_object_put_unlocked(gobj);
377 	return 0;
378 }
379 
380 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
381 			  struct drm_file *filp)
382 {
383 	union drm_amdgpu_gem_mmap *args = data;
384 	uint32_t handle = args->in.handle;
385 	memset(args, 0, sizeof(*args));
386 	return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
387 }
388 
389 /**
390  * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
391  *
392  * @timeout_ns: timeout in ns
393  *
394  * Calculate the timeout in jiffies from an absolute timeout in ns.
395  */
396 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
397 {
398 	unsigned long timeout_jiffies;
399 	ktime_t timeout;
400 
401 	/* clamp timeout if it's to large */
402 	if (((int64_t)timeout_ns) < 0)
403 		return MAX_SCHEDULE_TIMEOUT;
404 
405 	timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
406 	if (ktime_to_ns(timeout) < 0)
407 		return 0;
408 
409 	timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
410 	/*  clamp timeout to avoid unsigned-> signed overflow */
411 	if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
412 		return MAX_SCHEDULE_TIMEOUT - 1;
413 
414 	return timeout_jiffies;
415 }
416 
417 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
418 			      struct drm_file *filp)
419 {
420 	union drm_amdgpu_gem_wait_idle *args = data;
421 	struct drm_gem_object *gobj;
422 	struct amdgpu_bo *robj;
423 	uint32_t handle = args->in.handle;
424 	unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
425 	int r = 0;
426 	long ret;
427 
428 	gobj = drm_gem_object_lookup(filp, handle);
429 	if (gobj == NULL) {
430 		return -ENOENT;
431 	}
432 	robj = gem_to_amdgpu_bo(gobj);
433 	ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
434 						  timeout);
435 
436 	/* ret == 0 means not signaled,
437 	 * ret > 0 means signaled
438 	 * ret < 0 means interrupted before timeout
439 	 */
440 	if (ret >= 0) {
441 		memset(args, 0, sizeof(*args));
442 		args->out.status = (ret == 0);
443 	} else
444 		r = ret;
445 
446 	drm_gem_object_put_unlocked(gobj);
447 	return r;
448 }
449 
450 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
451 				struct drm_file *filp)
452 {
453 	struct drm_amdgpu_gem_metadata *args = data;
454 	struct drm_gem_object *gobj;
455 	struct amdgpu_bo *robj;
456 	int r = -1;
457 
458 	DRM_DEBUG("%d \n", args->handle);
459 	gobj = drm_gem_object_lookup(filp, args->handle);
460 	if (gobj == NULL)
461 		return -ENOENT;
462 	robj = gem_to_amdgpu_bo(gobj);
463 
464 	r = amdgpu_bo_reserve(robj, false);
465 	if (unlikely(r != 0))
466 		goto out;
467 
468 	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
469 		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
470 		r = amdgpu_bo_get_metadata(robj, args->data.data,
471 					   sizeof(args->data.data),
472 					   &args->data.data_size_bytes,
473 					   &args->data.flags);
474 	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
475 		if (args->data.data_size_bytes > sizeof(args->data.data)) {
476 			r = -EINVAL;
477 			goto unreserve;
478 		}
479 		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
480 		if (!r)
481 			r = amdgpu_bo_set_metadata(robj, args->data.data,
482 						   args->data.data_size_bytes,
483 						   args->data.flags);
484 	}
485 
486 unreserve:
487 	amdgpu_bo_unreserve(robj);
488 out:
489 	drm_gem_object_put_unlocked(gobj);
490 	return r;
491 }
492 
493 /**
494  * amdgpu_gem_va_update_vm -update the bo_va in its VM
495  *
496  * @adev: amdgpu_device pointer
497  * @vm: vm to update
498  * @bo_va: bo_va to update
499  * @operation: map, unmap or clear
500  *
501  * Update the bo_va directly after setting its address. Errors are not
502  * vital here, so they are not reported back to userspace.
503  */
504 static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
505 				    struct amdgpu_vm *vm,
506 				    struct amdgpu_bo_va *bo_va,
507 				    uint32_t operation)
508 {
509 	int r;
510 
511 	if (!amdgpu_vm_ready(vm))
512 		return;
513 
514 	r = amdgpu_vm_clear_freed(adev, vm, NULL);
515 	if (r)
516 		goto error;
517 
518 	if (operation == AMDGPU_VA_OP_MAP ||
519 	    operation == AMDGPU_VA_OP_REPLACE) {
520 		r = amdgpu_vm_bo_update(adev, bo_va, false);
521 		if (r)
522 			goto error;
523 	}
524 
525 	r = amdgpu_vm_update_directories(adev, vm);
526 
527 error:
528 	if (r && r != -ERESTARTSYS)
529 		DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
530 }
531 
532 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
533 			  struct drm_file *filp)
534 {
535 	const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
536 		AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
537 		AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
538 	const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
539 		AMDGPU_VM_PAGE_PRT;
540 
541 	struct drm_amdgpu_gem_va *args = data;
542 	struct drm_gem_object *gobj;
543 	struct amdgpu_device *adev = dev->dev_private;
544 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
545 	struct amdgpu_bo *abo;
546 	struct amdgpu_bo_va *bo_va;
547 	struct amdgpu_bo_list_entry vm_pd;
548 	struct ttm_validate_buffer tv;
549 	struct ww_acquire_ctx ticket;
550 	struct list_head list, duplicates;
551 	uint64_t va_flags;
552 	int r = 0;
553 
554 	if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
555 		dev_dbg(&dev->pdev->dev,
556 			"va_address 0x%LX is in reserved area 0x%LX\n",
557 			args->va_address, AMDGPU_VA_RESERVED_SIZE);
558 		return -EINVAL;
559 	}
560 
561 	if (args->va_address >= AMDGPU_GMC_HOLE_START &&
562 	    args->va_address < AMDGPU_GMC_HOLE_END) {
563 		dev_dbg(&dev->pdev->dev,
564 			"va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
565 			args->va_address, AMDGPU_GMC_HOLE_START,
566 			AMDGPU_GMC_HOLE_END);
567 		return -EINVAL;
568 	}
569 
570 	args->va_address &= AMDGPU_GMC_HOLE_MASK;
571 
572 	if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
573 		dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
574 			args->flags);
575 		return -EINVAL;
576 	}
577 
578 	switch (args->operation) {
579 	case AMDGPU_VA_OP_MAP:
580 	case AMDGPU_VA_OP_UNMAP:
581 	case AMDGPU_VA_OP_CLEAR:
582 	case AMDGPU_VA_OP_REPLACE:
583 		break;
584 	default:
585 		dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
586 			args->operation);
587 		return -EINVAL;
588 	}
589 
590 	INIT_LIST_HEAD(&list);
591 	INIT_LIST_HEAD(&duplicates);
592 	if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
593 	    !(args->flags & AMDGPU_VM_PAGE_PRT)) {
594 		gobj = drm_gem_object_lookup(filp, args->handle);
595 		if (gobj == NULL)
596 			return -ENOENT;
597 		abo = gem_to_amdgpu_bo(gobj);
598 		tv.bo = &abo->tbo;
599 		if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
600 			tv.num_shared = 1;
601 		else
602 			tv.num_shared = 0;
603 		list_add(&tv.head, &list);
604 	} else {
605 		gobj = NULL;
606 		abo = NULL;
607 	}
608 
609 	amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
610 
611 	r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates, false);
612 	if (r)
613 		goto error_unref;
614 
615 	if (abo) {
616 		bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
617 		if (!bo_va) {
618 			r = -ENOENT;
619 			goto error_backoff;
620 		}
621 	} else if (args->operation != AMDGPU_VA_OP_CLEAR) {
622 		bo_va = fpriv->prt_va;
623 	} else {
624 		bo_va = NULL;
625 	}
626 
627 	switch (args->operation) {
628 	case AMDGPU_VA_OP_MAP:
629 		va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
630 		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
631 				     args->offset_in_bo, args->map_size,
632 				     va_flags);
633 		break;
634 	case AMDGPU_VA_OP_UNMAP:
635 		r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
636 		break;
637 
638 	case AMDGPU_VA_OP_CLEAR:
639 		r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
640 						args->va_address,
641 						args->map_size);
642 		break;
643 	case AMDGPU_VA_OP_REPLACE:
644 		va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
645 		r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
646 					     args->offset_in_bo, args->map_size,
647 					     va_flags);
648 		break;
649 	default:
650 		break;
651 	}
652 	if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
653 		amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
654 					args->operation);
655 
656 error_backoff:
657 	ttm_eu_backoff_reservation(&ticket, &list);
658 
659 error_unref:
660 	drm_gem_object_put_unlocked(gobj);
661 	return r;
662 }
663 
664 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
665 			struct drm_file *filp)
666 {
667 	struct amdgpu_device *adev = dev->dev_private;
668 	struct drm_amdgpu_gem_op *args = data;
669 	struct drm_gem_object *gobj;
670 	struct amdgpu_vm_bo_base *base;
671 	struct amdgpu_bo *robj;
672 	int r;
673 
674 	gobj = drm_gem_object_lookup(filp, args->handle);
675 	if (gobj == NULL) {
676 		return -ENOENT;
677 	}
678 	robj = gem_to_amdgpu_bo(gobj);
679 
680 	r = amdgpu_bo_reserve(robj, false);
681 	if (unlikely(r))
682 		goto out;
683 
684 	switch (args->op) {
685 	case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
686 		struct drm_amdgpu_gem_create_in info;
687 		void __user *out = u64_to_user_ptr(args->value);
688 
689 		info.bo_size = robj->gem_base.size;
690 		info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
691 		info.domains = robj->preferred_domains;
692 		info.domain_flags = robj->flags;
693 		amdgpu_bo_unreserve(robj);
694 		if (copy_to_user(out, &info, sizeof(info)))
695 			r = -EFAULT;
696 		break;
697 	}
698 	case AMDGPU_GEM_OP_SET_PLACEMENT:
699 		if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
700 			r = -EINVAL;
701 			amdgpu_bo_unreserve(robj);
702 			break;
703 		}
704 		if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
705 			r = -EPERM;
706 			amdgpu_bo_unreserve(robj);
707 			break;
708 		}
709 		for (base = robj->vm_bo; base; base = base->next)
710 			if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev),
711 				amdgpu_ttm_adev(base->vm->root.base.bo->tbo.bdev))) {
712 				r = -EINVAL;
713 				amdgpu_bo_unreserve(robj);
714 				goto out;
715 			}
716 
717 
718 		robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
719 							AMDGPU_GEM_DOMAIN_GTT |
720 							AMDGPU_GEM_DOMAIN_CPU);
721 		robj->allowed_domains = robj->preferred_domains;
722 		if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
723 			robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
724 
725 		if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
726 			amdgpu_vm_bo_invalidate(adev, robj, true);
727 
728 		amdgpu_bo_unreserve(robj);
729 		break;
730 	default:
731 		amdgpu_bo_unreserve(robj);
732 		r = -EINVAL;
733 	}
734 
735 out:
736 	drm_gem_object_put_unlocked(gobj);
737 	return r;
738 }
739 
740 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
741 			    struct drm_device *dev,
742 			    struct drm_mode_create_dumb *args)
743 {
744 	struct amdgpu_device *adev = dev->dev_private;
745 	struct drm_gem_object *gobj;
746 	uint32_t handle;
747 	u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
748 	u32 domain;
749 	int r;
750 
751 	/*
752 	 * The buffer returned from this function should be cleared, but
753 	 * it can only be done if the ring is enabled or we'll fail to
754 	 * create the buffer.
755 	 */
756 	if (adev->mman.buffer_funcs_enabled)
757 		flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
758 
759 	args->pitch = amdgpu_align_pitch(adev, args->width,
760 					 DIV_ROUND_UP(args->bpp, 8), 0);
761 	args->size = (u64)args->pitch * args->height;
762 	args->size = ALIGN(args->size, PAGE_SIZE);
763 	domain = amdgpu_bo_get_preferred_pin_domain(adev,
764 				amdgpu_display_supported_domains(adev));
765 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
766 				     ttm_bo_type_device, NULL, &gobj);
767 	if (r)
768 		return -ENOMEM;
769 
770 	r = drm_gem_handle_create(file_priv, gobj, &handle);
771 	/* drop reference from allocate - handle holds it now */
772 	drm_gem_object_put_unlocked(gobj);
773 	if (r) {
774 		return r;
775 	}
776 	args->handle = handle;
777 	return 0;
778 }
779 
780 #if defined(CONFIG_DEBUG_FS)
781 
782 #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag)	\
783 	if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) {	\
784 		seq_printf((m), " " #flag);		\
785 	}
786 
787 static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
788 {
789 	struct drm_gem_object *gobj = ptr;
790 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
791 	struct seq_file *m = data;
792 
793 	struct dma_buf_attachment *attachment;
794 	struct dma_buf *dma_buf;
795 	unsigned domain;
796 	const char *placement;
797 	unsigned pin_count;
798 
799 	domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
800 	switch (domain) {
801 	case AMDGPU_GEM_DOMAIN_VRAM:
802 		placement = "VRAM";
803 		break;
804 	case AMDGPU_GEM_DOMAIN_GTT:
805 		placement = " GTT";
806 		break;
807 	case AMDGPU_GEM_DOMAIN_CPU:
808 	default:
809 		placement = " CPU";
810 		break;
811 	}
812 	seq_printf(m, "\t0x%08x: %12ld byte %s",
813 		   id, amdgpu_bo_size(bo), placement);
814 
815 	pin_count = READ_ONCE(bo->pin_count);
816 	if (pin_count)
817 		seq_printf(m, " pin count %d", pin_count);
818 
819 	dma_buf = READ_ONCE(bo->gem_base.dma_buf);
820 	attachment = READ_ONCE(bo->gem_base.import_attach);
821 
822 	if (attachment)
823 		seq_printf(m, " imported from %p", dma_buf);
824 	else if (dma_buf)
825 		seq_printf(m, " exported as %p", dma_buf);
826 
827 	amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
828 	amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS);
829 	amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC);
830 	amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED);
831 	amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW);
832 	amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
833 	amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID);
834 	amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC);
835 
836 	seq_printf(m, "\n");
837 
838 	return 0;
839 }
840 
841 static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
842 {
843 	struct drm_info_node *node = (struct drm_info_node *)m->private;
844 	struct drm_device *dev = node->minor->dev;
845 	struct drm_file *file;
846 	int r;
847 
848 	r = mutex_lock_interruptible(&dev->filelist_mutex);
849 	if (r)
850 		return r;
851 
852 	list_for_each_entry(file, &dev->filelist, lhead) {
853 		struct task_struct *task;
854 
855 		/*
856 		 * Although we have a valid reference on file->pid, that does
857 		 * not guarantee that the task_struct who called get_pid() is
858 		 * still alive (e.g. get_pid(current) => fork() => exit()).
859 		 * Therefore, we need to protect this ->comm access using RCU.
860 		 */
861 		rcu_read_lock();
862 		task = pid_task(file->pid, PIDTYPE_PID);
863 		seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
864 			   task ? task->comm : "<unknown>");
865 		rcu_read_unlock();
866 
867 		spin_lock(&file->table_lock);
868 		idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
869 		spin_unlock(&file->table_lock);
870 	}
871 
872 	mutex_unlock(&dev->filelist_mutex);
873 	return 0;
874 }
875 
876 static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
877 	{"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
878 };
879 #endif
880 
881 int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
882 {
883 #if defined(CONFIG_DEBUG_FS)
884 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
885 #endif
886 	return 0;
887 }
888