amdgpu_gem.c 22.1 KB
Newer Older
A
Alex Deucher 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright 2008 Advanced Micro Devices, Inc.
 * Copyright 2008 Red Hat Inc.
 * Copyright 2009 Jerome Glisse.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Dave Airlie
 *          Alex Deucher
 *          Jerome Glisse
 */
#include <linux/ktime.h>
29
#include <linux/pagemap.h>
A
Alex Deucher 已提交
30 31 32 33 34 35 36 37 38
#include <drm/drmP.h>
#include <drm/amdgpu_drm.h>
#include "amdgpu.h"

void amdgpu_gem_object_free(struct drm_gem_object *gobj)
{
	struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);

	if (robj) {
39
		amdgpu_mn_unregister(robj);
A
Alex Deucher 已提交
40 41 42 43 44
		amdgpu_bo_unref(&robj);
	}
}

int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
45
			     int alignment, u32 initial_domain,
46
			     u64 flags, enum ttm_bo_type type,
47 48
			     struct reservation_object *resv,
			     struct drm_gem_object **obj)
A
Alex Deucher 已提交
49
{
50
	struct amdgpu_bo *bo;
51
	struct amdgpu_bo_param bp;
A
Alex Deucher 已提交
52 53
	int r;

54
	memset(&bp, 0, sizeof(bp));
A
Alex Deucher 已提交
55 56 57 58 59 60
	*obj = NULL;
	/* At least align on page size */
	if (alignment < PAGE_SIZE) {
		alignment = PAGE_SIZE;
	}

61 62 63 64
	bp.size = size;
	bp.byte_align = alignment;
	bp.type = type;
	bp.resv = resv;
65
retry:
66 67 68
	bp.flags = flags;
	bp.domain = initial_domain;
	r = amdgpu_bo_create(adev, &bp, &bo);
A
Alex Deucher 已提交
69
	if (r) {
70 71 72 73 74 75 76 77 78 79 80 81 82
		if (r != -ERESTARTSYS) {
			if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
				flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
				goto retry;
			}

			if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
				initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
				goto retry;
			}
			DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
				  size, initial_domain, alignment, r);
		}
A
Alex Deucher 已提交
83 84
		return r;
	}
85
	*obj = &bo->gem_base;
A
Alex Deucher 已提交
86 87 88 89

	return 0;
}

90
void amdgpu_gem_force_release(struct amdgpu_device *adev)
A
Alex Deucher 已提交
91
{
92 93
	struct drm_device *ddev = adev->ddev;
	struct drm_file *file;
A
Alex Deucher 已提交
94

95
	mutex_lock(&ddev->filelist_mutex);
96 97 98 99 100 101 102 103 104

	list_for_each_entry(file, &ddev->filelist, lhead) {
		struct drm_gem_object *gobj;
		int handle;

		WARN_ONCE(1, "Still active user space clients!\n");
		spin_lock(&file->table_lock);
		idr_for_each_entry(&file->object_idr, gobj, handle) {
			WARN_ONCE(1, "And also active allocations!\n");
105
			drm_gem_object_put_unlocked(gobj);
106 107 108 109 110
		}
		idr_destroy(&file->object_idr);
		spin_unlock(&file->table_lock);
	}

111
	mutex_unlock(&ddev->filelist_mutex);
A
Alex Deucher 已提交
112 113 114 115 116 117
}

/*
 * Call from drm_gem_handle_create which appear in both new and open ioctl
 * case.
 */
118 119
int amdgpu_gem_object_open(struct drm_gem_object *obj,
			   struct drm_file *file_priv)
A
Alex Deucher 已提交
120
{
121
	struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
122
	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
A
Alex Deucher 已提交
123 124 125
	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
	struct amdgpu_vm *vm = &fpriv->vm;
	struct amdgpu_bo_va *bo_va;
126
	struct mm_struct *mm;
A
Alex Deucher 已提交
127
	int r;
128 129 130 131 132

	mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
	if (mm && mm != current->mm)
		return -EPERM;

133 134 135 136
	if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
	    abo->tbo.resv != vm->root.base.bo->tbo.resv)
		return -EPERM;

137
	r = amdgpu_bo_reserve(abo, false);
C
Chunming Zhou 已提交
138
	if (r)
A
Alex Deucher 已提交
139 140
		return r;

141
	bo_va = amdgpu_vm_bo_find(vm, abo);
A
Alex Deucher 已提交
142
	if (!bo_va) {
143
		bo_va = amdgpu_vm_bo_add(adev, vm, abo);
A
Alex Deucher 已提交
144 145 146
	} else {
		++bo_va->ref_count;
	}
147
	amdgpu_bo_unreserve(abo);
A
Alex Deucher 已提交
148 149 150 151 152 153
	return 0;
}

void amdgpu_gem_object_close(struct drm_gem_object *obj,
			     struct drm_file *file_priv)
{
154
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
155
	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
A
Alex Deucher 已提交
156 157
	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
	struct amdgpu_vm *vm = &fpriv->vm;
158 159

	struct amdgpu_bo_list_entry vm_pd;
160
	struct list_head list, duplicates;
161 162
	struct ttm_validate_buffer tv;
	struct ww_acquire_ctx ticket;
A
Alex Deucher 已提交
163 164
	struct amdgpu_bo_va *bo_va;
	int r;
165 166

	INIT_LIST_HEAD(&list);
167
	INIT_LIST_HEAD(&duplicates);
168 169 170 171 172 173 174

	tv.bo = &bo->tbo;
	tv.shared = true;
	list_add(&tv.head, &list);

	amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);

175
	r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
A
Alex Deucher 已提交
176 177 178 179 180
	if (r) {
		dev_err(adev->dev, "leaking bo va because "
			"we fail to reserve bo (%d)\n", r);
		return;
	}
181
	bo_va = amdgpu_vm_bo_find(vm, bo);
182 183 184
	if (bo_va && --bo_va->ref_count == 0) {
		amdgpu_vm_bo_rmv(adev, bo_va);

185
		if (amdgpu_vm_ready(vm)) {
186
			struct dma_fence *fence = NULL;
187 188 189 190 191 192 193 194 195 196 197

			r = amdgpu_vm_clear_freed(adev, vm, &fence);
			if (unlikely(r)) {
				dev_err(adev->dev, "failed to clear page "
					"tables on GEM object close (%d)\n", r);
			}

			if (fence) {
				amdgpu_bo_fence(bo, fence, true);
				dma_fence_put(fence);
			}
A
Alex Deucher 已提交
198 199
		}
	}
200
	ttm_eu_backoff_reservation(&ticket, &list);
A
Alex Deucher 已提交
201 202 203 204 205 206 207 208 209
}

/*
 * GEM ioctls.
 */
int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
			    struct drm_file *filp)
{
	struct amdgpu_device *adev = dev->dev_private;
210 211
	struct amdgpu_fpriv *fpriv = filp->driver_priv;
	struct amdgpu_vm *vm = &fpriv->vm;
A
Alex Deucher 已提交
212
	union drm_amdgpu_gem_create *args = data;
213
	uint64_t flags = args->in.domain_flags;
A
Alex Deucher 已提交
214
	uint64_t size = args->in.bo_size;
215
	struct reservation_object *resv = NULL;
A
Alex Deucher 已提交
216 217 218 219
	struct drm_gem_object *gobj;
	uint32_t handle;
	int r;

220
	/* reject invalid gem flags */
221 222 223
	if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
		      AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
		      AMDGPU_GEM_CREATE_CPU_GTT_USWC |
224
		      AMDGPU_GEM_CREATE_VRAM_CLEARED |
225 226 227
		      AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
		      AMDGPU_GEM_CREATE_EXPLICIT_SYNC))

228 229
		return -EINVAL;

230 231 232 233 234 235
	/* reject invalid gem domains */
	if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
				 AMDGPU_GEM_DOMAIN_GTT |
				 AMDGPU_GEM_DOMAIN_VRAM |
				 AMDGPU_GEM_DOMAIN_GDS |
				 AMDGPU_GEM_DOMAIN_GWS |
236 237
				 AMDGPU_GEM_DOMAIN_OA))
		return -EINVAL;
238

A
Alex Deucher 已提交
239 240 241
	/* create a gem object to contain this object in */
	if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
	    AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
242
		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
A
Alex Deucher 已提交
243 244 245 246 247 248
		if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
			size = size << AMDGPU_GDS_SHIFT;
		else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
			size = size << AMDGPU_GWS_SHIFT;
		else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
			size = size << AMDGPU_OA_SHIFT;
249 250
		else
			return -EINVAL;
A
Alex Deucher 已提交
251 252 253
	}
	size = roundup(size, PAGE_SIZE);

254 255 256 257 258 259 260 261
	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
		r = amdgpu_bo_reserve(vm->root.base.bo, false);
		if (r)
			return r;

		resv = vm->root.base.bo->tbo.resv;
	}

A
Alex Deucher 已提交
262 263
	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
				     (u32)(0xffffffff & args->in.domains),
264 265 266 267 268 269 270 271 272
				     flags, false, resv, &gobj);
	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
		if (!r) {
			struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);

			abo->parent = amdgpu_bo_ref(vm->root.base.bo);
		}
		amdgpu_bo_unreserve(vm->root.base.bo);
	}
A
Alex Deucher 已提交
273
	if (r)
274
		return r;
A
Alex Deucher 已提交
275 276 277

	r = drm_gem_handle_create(filp, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
278
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
279
	if (r)
280
		return r;
A
Alex Deucher 已提交
281 282 283 284 285 286 287 288 289

	memset(args, 0, sizeof(*args));
	args->out.handle = handle;
	return 0;
}

int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
			     struct drm_file *filp)
{
290
	struct ttm_operation_ctx ctx = { true, false };
A
Alex Deucher 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	struct amdgpu_device *adev = dev->dev_private;
	struct drm_amdgpu_gem_userptr *args = data;
	struct drm_gem_object *gobj;
	struct amdgpu_bo *bo;
	uint32_t handle;
	int r;

	if (offset_in_page(args->addr | args->size))
		return -EINVAL;

	/* reject unknown flag values */
	if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
	    AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
	    AMDGPU_GEM_USERPTR_REGISTER))
		return -EINVAL;

307 308
	if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
	     !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
A
Alex Deucher 已提交
309

310
		/* if we want to write to it we must install a MMU notifier */
A
Alex Deucher 已提交
311 312 313 314
		return -EACCES;
	}

	/* create a gem object to contain this object in */
315 316
	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
				     0, 0, NULL, &gobj);
A
Alex Deucher 已提交
317
	if (r)
318
		return r;
A
Alex Deucher 已提交
319 320

	bo = gem_to_amdgpu_bo(gobj);
K
Kent Russell 已提交
321
	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
322
	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
A
Alex Deucher 已提交
323 324 325 326 327 328 329 330 331 332 333
	r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
	if (r)
		goto release_object;

	if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
		r = amdgpu_mn_register(bo, args->addr);
		if (r)
			goto release_object;
	}

	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
334 335 336
		r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
						 bo->tbo.ttm->pages);
		if (r)
337
			goto release_object;
338

A
Alex Deucher 已提交
339
		r = amdgpu_bo_reserve(bo, true);
340 341
		if (r)
			goto free_pages;
A
Alex Deucher 已提交
342 343

		amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
344
		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
A
Alex Deucher 已提交
345 346
		amdgpu_bo_unreserve(bo);
		if (r)
347
			goto free_pages;
A
Alex Deucher 已提交
348 349 350 351
	}

	r = drm_gem_handle_create(filp, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
352
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
353
	if (r)
354
		return r;
A
Alex Deucher 已提交
355 356 357 358

	args->handle = handle;
	return 0;

359
free_pages:
360
	release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages);
361

A
Alex Deucher 已提交
362
release_object:
363
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
364 365 366 367 368 369 370 371 372 373 374

	return r;
}

int amdgpu_mode_dumb_mmap(struct drm_file *filp,
			  struct drm_device *dev,
			  uint32_t handle, uint64_t *offset_p)
{
	struct drm_gem_object *gobj;
	struct amdgpu_bo *robj;

375
	gobj = drm_gem_object_lookup(filp, handle);
A
Alex Deucher 已提交
376 377 378 379
	if (gobj == NULL) {
		return -ENOENT;
	}
	robj = gem_to_amdgpu_bo(gobj);
380
	if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
381
	    (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
382
		drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
383 384 385
		return -EPERM;
	}
	*offset_p = amdgpu_bo_mmap_offset(robj);
386
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	return 0;
}

int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	union drm_amdgpu_gem_mmap *args = data;
	uint32_t handle = args->in.handle;
	memset(args, 0, sizeof(*args));
	return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
}

/**
 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
 *
 * @timeout_ns: timeout in ns
 *
 * Calculate the timeout in jiffies from an absolute timeout in ns.
 */
unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
{
	unsigned long timeout_jiffies;
	ktime_t timeout;

	/* clamp timeout if it's to large */
	if (((int64_t)timeout_ns) < 0)
		return MAX_SCHEDULE_TIMEOUT;

415
	timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
A
Alex Deucher 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	if (ktime_to_ns(timeout) < 0)
		return 0;

	timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
	/*  clamp timeout to avoid unsigned-> signed overflow */
	if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
		return MAX_SCHEDULE_TIMEOUT - 1;

	return timeout_jiffies;
}

int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
			      struct drm_file *filp)
{
	union drm_amdgpu_gem_wait_idle *args = data;
	struct drm_gem_object *gobj;
	struct amdgpu_bo *robj;
	uint32_t handle = args->in.handle;
	unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
	int r = 0;
	long ret;

438
	gobj = drm_gem_object_lookup(filp, handle);
A
Alex Deucher 已提交
439 440 441 442
	if (gobj == NULL) {
		return -ENOENT;
	}
	robj = gem_to_amdgpu_bo(gobj);
443 444
	ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
						  timeout);
A
Alex Deucher 已提交
445 446 447 448 449 450 451 452 453 454 455

	/* ret == 0 means not signaled,
	 * ret > 0 means signaled
	 * ret < 0 means interrupted before timeout
	 */
	if (ret >= 0) {
		memset(args, 0, sizeof(*args));
		args->out.status = (ret == 0);
	} else
		r = ret;

456
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
457 458 459 460 461 462 463 464 465 466 467 468
	return r;
}

int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	struct drm_amdgpu_gem_metadata *args = data;
	struct drm_gem_object *gobj;
	struct amdgpu_bo *robj;
	int r = -1;

	DRM_DEBUG("%d \n", args->handle);
469
	gobj = drm_gem_object_lookup(filp, args->handle);
A
Alex Deucher 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	if (gobj == NULL)
		return -ENOENT;
	robj = gem_to_amdgpu_bo(gobj);

	r = amdgpu_bo_reserve(robj, false);
	if (unlikely(r != 0))
		goto out;

	if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
		amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
		r = amdgpu_bo_get_metadata(robj, args->data.data,
					   sizeof(args->data.data),
					   &args->data.data_size_bytes,
					   &args->data.flags);
	} else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
485 486 487 488
		if (args->data.data_size_bytes > sizeof(args->data.data)) {
			r = -EINVAL;
			goto unreserve;
		}
A
Alex Deucher 已提交
489 490 491 492 493 494 495
		r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
		if (!r)
			r = amdgpu_bo_set_metadata(robj, args->data.data,
						   args->data.data_size_bytes,
						   args->data.flags);
	}

496
unreserve:
A
Alex Deucher 已提交
497 498
	amdgpu_bo_unreserve(robj);
out:
499
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
500 501 502 503 504 505 506
	return r;
}

/**
 * amdgpu_gem_va_update_vm -update the bo_va in its VM
 *
 * @adev: amdgpu_device pointer
507
 * @vm: vm to update
A
Alex Deucher 已提交
508
 * @bo_va: bo_va to update
509
 * @list: validation list
510
 * @operation: map, unmap or clear
A
Alex Deucher 已提交
511
 *
512
 * Update the bo_va directly after setting its address. Errors are not
A
Alex Deucher 已提交
513 514 515
 * vital here, so they are not reported back to userspace.
 */
static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
516
				    struct amdgpu_vm *vm,
517
				    struct amdgpu_bo_va *bo_va,
518
				    struct list_head *list,
519
				    uint32_t operation)
A
Alex Deucher 已提交
520
{
521
	int r;
A
Alex Deucher 已提交
522

523 524
	if (!amdgpu_vm_ready(vm))
		return;
525

526
	r = amdgpu_vm_clear_freed(adev, vm, NULL);
A
Alex Deucher 已提交
527
	if (r)
528
		goto error;
529

530
	if (operation == AMDGPU_VA_OP_MAP ||
531
	    operation == AMDGPU_VA_OP_REPLACE) {
532
		r = amdgpu_vm_bo_update(adev, bo_va, false);
533 534 535
		if (r)
			goto error;
	}
A
Alex Deucher 已提交
536

537 538
	r = amdgpu_vm_update_directories(adev, vm);

539
error:
540
	if (r && r != -ERESTARTSYS)
A
Alex Deucher 已提交
541 542 543 544 545 546
		DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
}

int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
547 548
	const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
		AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
549
		AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
550 551 552
	const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
		AMDGPU_VM_PAGE_PRT;

C
Christian König 已提交
553
	struct drm_amdgpu_gem_va *args = data;
A
Alex Deucher 已提交
554 555 556
	struct drm_gem_object *gobj;
	struct amdgpu_device *adev = dev->dev_private;
	struct amdgpu_fpriv *fpriv = filp->driver_priv;
557
	struct amdgpu_bo *abo;
A
Alex Deucher 已提交
558
	struct amdgpu_bo_va *bo_va;
559 560
	struct amdgpu_bo_list_entry vm_pd;
	struct ttm_validate_buffer tv;
561
	struct ww_acquire_ctx ticket;
562
	struct list_head list, duplicates;
563
	uint64_t va_flags;
A
Alex Deucher 已提交
564 565
	int r = 0;

C
Christian König 已提交
566
	if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
567
		dev_dbg(&dev->pdev->dev,
568 569
			"va_address 0x%LX is in reserved area 0x%LX\n",
			args->va_address, AMDGPU_VA_RESERVED_SIZE);
A
Alex Deucher 已提交
570 571 572
		return -EINVAL;
	}

573 574 575 576 577 578 579 580 581 582 583
	if (args->va_address >= AMDGPU_VA_HOLE_START &&
	    args->va_address < AMDGPU_VA_HOLE_END) {
		dev_dbg(&dev->pdev->dev,
			"va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
			args->va_address, AMDGPU_VA_HOLE_START,
			AMDGPU_VA_HOLE_END);
		return -EINVAL;
	}

	args->va_address &= AMDGPU_VA_HOLE_MASK;

584
	if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
585
		dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
586
			args->flags);
A
Alex Deucher 已提交
587 588 589
		return -EINVAL;
	}

C
Christian König 已提交
590
	switch (args->operation) {
A
Alex Deucher 已提交
591 592
	case AMDGPU_VA_OP_MAP:
	case AMDGPU_VA_OP_UNMAP:
593
	case AMDGPU_VA_OP_CLEAR:
594
	case AMDGPU_VA_OP_REPLACE:
A
Alex Deucher 已提交
595 596
		break;
	default:
597
		dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
C
Christian König 已提交
598
			args->operation);
A
Alex Deucher 已提交
599 600 601
		return -EINVAL;
	}

602
	INIT_LIST_HEAD(&list);
603
	INIT_LIST_HEAD(&duplicates);
604 605
	if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
	    !(args->flags & AMDGPU_VM_PAGE_PRT)) {
606 607 608 609 610 611 612 613 614 615 616
		gobj = drm_gem_object_lookup(filp, args->handle);
		if (gobj == NULL)
			return -ENOENT;
		abo = gem_to_amdgpu_bo(gobj);
		tv.bo = &abo->tbo;
		tv.shared = false;
		list_add(&tv.head, &list);
	} else {
		gobj = NULL;
		abo = NULL;
	}
617

618
	amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
619

620
	r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
621 622
	if (r)
		goto error_unref;
C
Christian König 已提交
623

624 625 626 627 628 629
	if (abo) {
		bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
		if (!bo_va) {
			r = -ENOENT;
			goto error_backoff;
		}
630
	} else if (args->operation != AMDGPU_VA_OP_CLEAR) {
631
		bo_va = fpriv->prt_va;
632 633
	} else {
		bo_va = NULL;
A
Alex Deucher 已提交
634 635
	}

C
Christian König 已提交
636
	switch (args->operation) {
A
Alex Deucher 已提交
637
	case AMDGPU_VA_OP_MAP:
638
		r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
639 640 641
					args->map_size);
		if (r)
			goto error_backoff;
642

643
		va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
C
Christian König 已提交
644 645
		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
				     args->offset_in_bo, args->map_size,
646
				     va_flags);
A
Alex Deucher 已提交
647 648
		break;
	case AMDGPU_VA_OP_UNMAP:
C
Christian König 已提交
649
		r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
A
Alex Deucher 已提交
650
		break;
651 652 653 654 655 656

	case AMDGPU_VA_OP_CLEAR:
		r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
						args->va_address,
						args->map_size);
		break;
657
	case AMDGPU_VA_OP_REPLACE:
658
		r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
659 660 661 662
					args->map_size);
		if (r)
			goto error_backoff;

663
		va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
664 665 666 667
		r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
					     args->offset_in_bo, args->map_size,
					     va_flags);
		break;
A
Alex Deucher 已提交
668 669 670
	default:
		break;
	}
671
	if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
672 673
		amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
					args->operation);
674 675

error_backoff:
676
	ttm_eu_backoff_reservation(&ticket, &list);
C
Chunming Zhou 已提交
677

678
error_unref:
679
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
680 681 682 683 684 685
	return r;
}

int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
			struct drm_file *filp)
{
686
	struct amdgpu_device *adev = dev->dev_private;
A
Alex Deucher 已提交
687 688 689 690 691
	struct drm_amdgpu_gem_op *args = data;
	struct drm_gem_object *gobj;
	struct amdgpu_bo *robj;
	int r;

692
	gobj = drm_gem_object_lookup(filp, args->handle);
A
Alex Deucher 已提交
693 694 695 696 697 698 699 700 701 702 703 704
	if (gobj == NULL) {
		return -ENOENT;
	}
	robj = gem_to_amdgpu_bo(gobj);

	r = amdgpu_bo_reserve(robj, false);
	if (unlikely(r))
		goto out;

	switch (args->op) {
	case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
		struct drm_amdgpu_gem_create_in info;
705
		void __user *out = u64_to_user_ptr(args->value);
A
Alex Deucher 已提交
706 707 708

		info.bo_size = robj->gem_base.size;
		info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
K
Kent Russell 已提交
709
		info.domains = robj->preferred_domains;
A
Alex Deucher 已提交
710
		info.domain_flags = robj->flags;
711
		amdgpu_bo_unreserve(robj);
A
Alex Deucher 已提交
712 713 714 715
		if (copy_to_user(out, &info, sizeof(info)))
			r = -EFAULT;
		break;
	}
716
	case AMDGPU_GEM_OP_SET_PLACEMENT:
717 718 719 720 721
		if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
			r = -EINVAL;
			amdgpu_bo_unreserve(robj);
			break;
		}
722
		if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
A
Alex Deucher 已提交
723
			r = -EPERM;
724
			amdgpu_bo_unreserve(robj);
A
Alex Deucher 已提交
725 726
			break;
		}
K
Kent Russell 已提交
727
		robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
728 729
							AMDGPU_GEM_DOMAIN_GTT |
							AMDGPU_GEM_DOMAIN_CPU);
K
Kent Russell 已提交
730
		robj->allowed_domains = robj->preferred_domains;
731 732 733
		if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
			robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;

734 735 736
		if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
			amdgpu_vm_bo_invalidate(adev, robj, true);

737
		amdgpu_bo_unreserve(robj);
A
Alex Deucher 已提交
738 739
		break;
	default:
740
		amdgpu_bo_unreserve(robj);
A
Alex Deucher 已提交
741 742 743 744
		r = -EINVAL;
	}

out:
745
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
746 747 748 749 750 751 752 753 754 755 756 757
	return r;
}

int amdgpu_mode_dumb_create(struct drm_file *file_priv,
			    struct drm_device *dev,
			    struct drm_mode_create_dumb *args)
{
	struct amdgpu_device *adev = dev->dev_private;
	struct drm_gem_object *gobj;
	uint32_t handle;
	int r;

758 759
	args->pitch = amdgpu_align_pitch(adev, args->width,
					 DIV_ROUND_UP(args->bpp, 8), 0);
760
	args->size = (u64)args->pitch * args->height;
A
Alex Deucher 已提交
761 762 763 764
	args->size = ALIGN(args->size, PAGE_SIZE);

	r = amdgpu_gem_object_create(adev, args->size, 0,
				     AMDGPU_GEM_DOMAIN_VRAM,
765
				     AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
766
				     false, NULL, &gobj);
A
Alex Deucher 已提交
767 768 769 770 771
	if (r)
		return -ENOMEM;

	r = drm_gem_handle_create(file_priv, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
772
	drm_gem_object_put_unlocked(gobj);
A
Alex Deucher 已提交
773 774 775 776 777 778 779 780
	if (r) {
		return r;
	}
	args->handle = handle;
	return 0;
}

#if defined(CONFIG_DEBUG_FS)
781 782 783 784 785 786 787 788 789
static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
{
	struct drm_gem_object *gobj = ptr;
	struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
	struct seq_file *m = data;

	unsigned domain;
	const char *placement;
	unsigned pin_count;
790
	uint64_t offset;
791 792 793 794 795 796 797 798 799 800 801 802 803 804

	domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
	switch (domain) {
	case AMDGPU_GEM_DOMAIN_VRAM:
		placement = "VRAM";
		break;
	case AMDGPU_GEM_DOMAIN_GTT:
		placement = " GTT";
		break;
	case AMDGPU_GEM_DOMAIN_CPU:
	default:
		placement = " CPU";
		break;
	}
805 806 807
	seq_printf(m, "\t0x%08x: %12ld byte %s",
		   id, amdgpu_bo_size(bo), placement);

808
	offset = READ_ONCE(bo->tbo.mem.start);
809 810
	if (offset != AMDGPU_BO_INVALID_OFFSET)
		seq_printf(m, " @ 0x%010Lx", offset);
811

812
	pin_count = READ_ONCE(bo->pin_count);
813 814 815 816 817 818 819
	if (pin_count)
		seq_printf(m, " pin count %d", pin_count);
	seq_printf(m, "\n");

	return 0;
}

A
Alex Deucher 已提交
820 821 822 823
static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
	struct drm_device *dev = node->minor->dev;
824 825
	struct drm_file *file;
	int r;
A
Alex Deucher 已提交
826

827
	r = mutex_lock_interruptible(&dev->filelist_mutex);
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
	if (r)
		return r;

	list_for_each_entry(file, &dev->filelist, lhead) {
		struct task_struct *task;

		/*
		 * Although we have a valid reference on file->pid, that does
		 * not guarantee that the task_struct who called get_pid() is
		 * still alive (e.g. get_pid(current) => fork() => exit()).
		 * Therefore, we need to protect this ->comm access using RCU.
		 */
		rcu_read_lock();
		task = pid_task(file->pid, PIDTYPE_PID);
		seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
			   task ? task->comm : "<unknown>");
		rcu_read_unlock();

		spin_lock(&file->table_lock);
		idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
		spin_unlock(&file->table_lock);
A
Alex Deucher 已提交
849
	}
850

851
	mutex_unlock(&dev->filelist_mutex);
A
Alex Deucher 已提交
852 853 854
	return 0;
}

855
static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
A
Alex Deucher 已提交
856 857 858 859
	{"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
};
#endif

860
int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
A
Alex Deucher 已提交
861 862 863 864 865 866
{
#if defined(CONFIG_DEBUG_FS)
	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
#endif
	return 0;
}