radeon_gem.c 20.3 KB
Newer Older
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
/*
 * 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
 */
28 29
#include <drm/drmP.h>
#include <drm/radeon_drm.h>
30 31 32 33
#include "radeon.h"

void radeon_gem_object_free(struct drm_gem_object *gobj)
{
34
	struct radeon_bo *robj = gem_to_radeon_bo(gobj);
35 36

	if (robj) {
37
		radeon_mn_unregister(robj);
38
		radeon_bo_unref(&robj);
39 40 41
	}
}

42
int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size,
43
				int alignment, int initial_domain,
44
				u32 flags, bool kernel,
45
				struct drm_gem_object **obj)
46
{
47
	struct radeon_bo *robj;
48
	unsigned long max_size;
49 50 51 52 53 54 55
	int r;

	*obj = NULL;
	/* At least align on page size */
	if (alignment < PAGE_SIZE) {
		alignment = PAGE_SIZE;
	}
56

57 58 59 60
	/* Maximum bo size is the unpinned gtt size since we use the gtt to
	 * handle vram to system pool migrations.
	 */
	max_size = rdev->mc.gtt_size - rdev->gart_pin_size;
61
	if (size > max_size) {
62
		DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
63
			  size >> 20, max_size >> 20);
64 65 66
		return -ENOMEM;
	}

67
retry:
68
	r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain,
69
			     flags, NULL, NULL, &robj);
70
	if (r) {
71 72 73 74 75
		if (r != -ERESTARTSYS) {
			if (initial_domain == RADEON_GEM_DOMAIN_VRAM) {
				initial_domain |= RADEON_GEM_DOMAIN_GTT;
				goto retry;
			}
76
			DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
77
				  size, initial_domain, alignment, r);
78
		}
79 80
		return r;
	}
81
	*obj = &robj->gem_base;
J
Jerome Glisse 已提交
82
	robj->pid = task_pid_nr(current);
83 84 85 86 87

	mutex_lock(&rdev->gem.mutex);
	list_add_tail(&robj->list, &rdev->gem.objects);
	mutex_unlock(&rdev->gem.mutex);

88 89 90
	return 0;
}

91
static int radeon_gem_set_domain(struct drm_gem_object *gobj,
92 93
			  uint32_t rdomain, uint32_t wdomain)
{
94
	struct radeon_bo *robj;
95
	uint32_t domain;
96
	long r;
97 98

	/* FIXME: reeimplement */
99
	robj = gem_to_radeon_bo(gobj);
100 101 102 103 104 105 106
	/* work out where to validate the buffer to */
	domain = wdomain;
	if (!domain) {
		domain = rdomain;
	}
	if (!domain) {
		/* Do nothings */
107
		pr_warn("Set domain without domain !\n");
108 109 110 111
		return 0;
	}
	if (domain == RADEON_GEM_DOMAIN_CPU) {
		/* Asking for cpu access wait for object idle */
112 113 114 115 116
		r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
		if (!r)
			r = -EBUSY;

		if (r < 0 && r != -EINTR) {
117
			pr_err("Failed to wait for object: %li\n", r);
118 119 120
			return r;
		}
	}
121 122 123 124
	if (domain == RADEON_GEM_DOMAIN_VRAM && robj->prime_shared_count) {
		/* A BO that is associated with a dma-buf cannot be sensibly migrated to VRAM */
		return -EINVAL;
	}
125 126 127 128 129 130 131 132 133 134 135
	return 0;
}

int radeon_gem_init(struct radeon_device *rdev)
{
	INIT_LIST_HEAD(&rdev->gem.objects);
	return 0;
}

void radeon_gem_fini(struct radeon_device *rdev)
{
136
	radeon_bo_force_delete(rdev);
137 138
}

139 140 141 142 143 144
/*
 * Call from drm_gem_handle_create which appear in both new and open ioctl
 * case.
 */
int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
{
145 146 147 148 149 150 151
	struct radeon_bo *rbo = gem_to_radeon_bo(obj);
	struct radeon_device *rdev = rbo->rdev;
	struct radeon_fpriv *fpriv = file_priv->driver_priv;
	struct radeon_vm *vm = &fpriv->vm;
	struct radeon_bo_va *bo_va;
	int r;

152 153
	if ((rdev->family < CHIP_CAYMAN) ||
	    (!rdev->accel_working)) {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		return 0;
	}

	r = radeon_bo_reserve(rbo, false);
	if (r) {
		return r;
	}

	bo_va = radeon_vm_bo_find(vm, rbo);
	if (!bo_va) {
		bo_va = radeon_vm_bo_add(rdev, vm, rbo);
	} else {
		++bo_va->ref_count;
	}
	radeon_bo_unreserve(rbo);

170 171 172 173 174 175 176 177 178 179
	return 0;
}

void radeon_gem_object_close(struct drm_gem_object *obj,
			     struct drm_file *file_priv)
{
	struct radeon_bo *rbo = gem_to_radeon_bo(obj);
	struct radeon_device *rdev = rbo->rdev;
	struct radeon_fpriv *fpriv = file_priv->driver_priv;
	struct radeon_vm *vm = &fpriv->vm;
180
	struct radeon_bo_va *bo_va;
181
	int r;
182

183 184
	if ((rdev->family < CHIP_CAYMAN) ||
	    (!rdev->accel_working)) {
185 186 187
		return;
	}

188 189 190 191
	r = radeon_bo_reserve(rbo, true);
	if (r) {
		dev_err(rdev->dev, "leaking bo va because "
			"we fail to reserve bo (%d)\n", r);
192 193
		return;
	}
194 195 196 197 198 199
	bo_va = radeon_vm_bo_find(vm, rbo);
	if (bo_va) {
		if (--bo_va->ref_count == 0) {
			radeon_vm_bo_rmv(rdev, bo_va);
		}
	}
200 201 202
	radeon_bo_unreserve(rbo);
}

203 204 205 206 207 208 209 210 211
static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
{
	if (r == -EDEADLK) {
		r = radeon_gpu_reset(rdev);
		if (!r)
			r = -EAGAIN;
	}
	return r;
}
212 213 214 215 216 217 218 219 220

/*
 * GEM ioctls.
 */
int radeon_gem_info_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	struct radeon_device *rdev = dev->dev_private;
	struct drm_radeon_gem_info *args = data;
221 222 223
	struct ttm_mem_type_manager *man;

	man = &rdev->mman.bdev.man[TTM_PL_VRAM];
224

225 226
	args->vram_size = (u64)man->size << PAGE_SHIFT;
	args->vram_visible = rdev->mc.visible_vram_size;
227 228 229 230
	args->vram_visible -= rdev->vram_pin_size;
	args->gart_size = rdev->mc.gtt_size;
	args->gart_size -= rdev->gart_pin_size;

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	return 0;
}

int radeon_gem_pread_ioctl(struct drm_device *dev, void *data,
			   struct drm_file *filp)
{
	/* TODO: implement */
	DRM_ERROR("unimplemented %s\n", __func__);
	return -ENOSYS;
}

int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data,
			    struct drm_file *filp)
{
	/* TODO: implement */
	DRM_ERROR("unimplemented %s\n", __func__);
	return -ENOSYS;
}

int radeon_gem_create_ioctl(struct drm_device *dev, void *data,
			    struct drm_file *filp)
{
	struct radeon_device *rdev = dev->dev_private;
	struct drm_radeon_gem_create *args = data;
	struct drm_gem_object *gobj;
	uint32_t handle;
	int r;

259
	down_read(&rdev->exclusive_lock);
260 261 262
	/* create a gem object to contain this object in */
	args->size = roundup(args->size, PAGE_SIZE);
	r = radeon_gem_object_create(rdev, args->size, args->alignment,
263
				     args->initial_domain, args->flags,
264
				     false, &gobj);
265
	if (r) {
266
		up_read(&rdev->exclusive_lock);
267
		r = radeon_gem_handle_lockup(rdev, r);
268 269 270
		return r;
	}
	r = drm_gem_handle_create(filp, gobj, &handle);
271
	/* drop reference from allocate - handle holds it now */
272
	drm_gem_object_put_unlocked(gobj);
273
	if (r) {
274
		up_read(&rdev->exclusive_lock);
275
		r = radeon_gem_handle_lockup(rdev, r);
276 277 278
		return r;
	}
	args->handle = handle;
279
	up_read(&rdev->exclusive_lock);
280 281 282
	return 0;
}

283 284 285
int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
			     struct drm_file *filp)
{
286
	struct ttm_operation_ctx ctx = { true, false };
287 288 289 290 291 292 293 294 295 296 297
	struct radeon_device *rdev = dev->dev_private;
	struct drm_radeon_gem_userptr *args = data;
	struct drm_gem_object *gobj;
	struct radeon_bo *bo;
	uint32_t handle;
	int r;

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

	/* reject unknown flag values */
298
	if (args->flags & ~(RADEON_GEM_USERPTR_READONLY |
299 300
	    RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE |
	    RADEON_GEM_USERPTR_REGISTER))
301 302
		return -EINVAL;

303 304 305 306 307 308 309 310 311 312 313 314
	if (args->flags & RADEON_GEM_USERPTR_READONLY) {
		/* readonly pages not tested on older hardware */
		if (rdev->family < CHIP_R600)
			return -EINVAL;

	} else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) ||
		   !(args->flags & RADEON_GEM_USERPTR_REGISTER)) {

		/* if we want to write to it we must require anonymous
		   memory and install a MMU notifier */
		return -EACCES;
	}
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

	down_read(&rdev->exclusive_lock);

	/* create a gem object to contain this object in */
	r = radeon_gem_object_create(rdev, args->size, 0,
				     RADEON_GEM_DOMAIN_CPU, 0,
				     false, &gobj);
	if (r)
		goto handle_lockup;

	bo = gem_to_radeon_bo(gobj);
	r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
	if (r)
		goto release_object;

330 331 332 333 334 335
	if (args->flags & RADEON_GEM_USERPTR_REGISTER) {
		r = radeon_mn_register(bo, args->addr);
		if (r)
			goto release_object;
	}

336 337 338 339 340 341 342 343 344
	if (args->flags & RADEON_GEM_USERPTR_VALIDATE) {
		down_read(&current->mm->mmap_sem);
		r = radeon_bo_reserve(bo, true);
		if (r) {
			up_read(&current->mm->mmap_sem);
			goto release_object;
		}

		radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT);
345
		r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
346 347 348 349 350 351
		radeon_bo_unreserve(bo);
		up_read(&current->mm->mmap_sem);
		if (r)
			goto release_object;
	}

352 353
	r = drm_gem_handle_create(filp, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
354
	drm_gem_object_put_unlocked(gobj);
355 356 357 358 359 360 361 362
	if (r)
		goto handle_lockup;

	args->handle = handle;
	up_read(&rdev->exclusive_lock);
	return 0;

release_object:
363
	drm_gem_object_put_unlocked(gobj);
364 365 366 367 368 369 370 371

handle_lockup:
	up_read(&rdev->exclusive_lock);
	r = radeon_gem_handle_lockup(rdev, r);

	return r;
}

372 373 374 375 376
int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	/* transition the BO to a domain -
	 * just validate the BO into a certain domain */
377
	struct radeon_device *rdev = dev->dev_private;
378 379
	struct drm_radeon_gem_set_domain *args = data;
	struct drm_gem_object *gobj;
380
	struct radeon_bo *robj;
381 382 383 384
	int r;

	/* for now if someone requests domain CPU -
	 * just make sure the buffer is finished with */
385
	down_read(&rdev->exclusive_lock);
386 387

	/* just do a BO wait for now */
388
	gobj = drm_gem_object_lookup(filp, args->handle);
389
	if (gobj == NULL) {
390
		up_read(&rdev->exclusive_lock);
391
		return -ENOENT;
392
	}
393
	robj = gem_to_radeon_bo(gobj);
394 395 396

	r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain);

397
	drm_gem_object_put_unlocked(gobj);
398
	up_read(&rdev->exclusive_lock);
399
	r = radeon_gem_handle_lockup(robj->rdev, r);
400 401 402
	return r;
}

403 404 405
int radeon_mode_dumb_mmap(struct drm_file *filp,
			  struct drm_device *dev,
			  uint32_t handle, uint64_t *offset_p)
406 407
{
	struct drm_gem_object *gobj;
408
	struct radeon_bo *robj;
409

410
	gobj = drm_gem_object_lookup(filp, handle);
411
	if (gobj == NULL) {
412
		return -ENOENT;
413
	}
414
	robj = gem_to_radeon_bo(gobj);
415
	if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
416
		drm_gem_object_put_unlocked(gobj);
417 418
		return -EPERM;
	}
419
	*offset_p = radeon_bo_mmap_offset(robj);
420
	drm_gem_object_put_unlocked(gobj);
421
	return 0;
422 423
}

424 425 426 427 428
int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	struct drm_radeon_gem_mmap *args = data;

429
	return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr);
430 431
}

432 433 434
int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
435 436
	struct drm_radeon_gem_busy *args = data;
	struct drm_gem_object *gobj;
437
	struct radeon_bo *robj;
438
	int r;
439
	uint32_t cur_placement = 0;
440

441
	gobj = drm_gem_object_lookup(filp, args->handle);
442
	if (gobj == NULL) {
443
		return -ENOENT;
444
	}
445
	robj = gem_to_radeon_bo(gobj);
446 447 448 449 450 451 452

	r = reservation_object_test_signaled_rcu(robj->tbo.resv, true);
	if (r == 0)
		r = -EBUSY;
	else
		r = 0;

453
	cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
454
	args->domain = radeon_mem_type_to_domain(cur_placement);
455
	drm_gem_object_put_unlocked(gobj);
456
	return r;
457 458 459 460 461
}

int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
			      struct drm_file *filp)
{
J
Jerome Glisse 已提交
462
	struct radeon_device *rdev = dev->dev_private;
463 464
	struct drm_radeon_gem_wait_idle *args = data;
	struct drm_gem_object *gobj;
465
	struct radeon_bo *robj;
466
	int r = 0;
467
	uint32_t cur_placement = 0;
468
	long ret;
469

470
	gobj = drm_gem_object_lookup(filp, args->handle);
471
	if (gobj == NULL) {
472
		return -ENOENT;
473
	}
474
	robj = gem_to_radeon_bo(gobj);
475 476 477 478 479 480 481

	ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ);
	if (ret == 0)
		r = -EBUSY;
	else if (ret < 0)
		r = ret;

482
	/* Flush HDP cache via MMIO if necessary */
483
	cur_placement = READ_ONCE(robj->tbo.mem.mem_type);
484 485
	if (rdev->asic->mmio_hdp_flush &&
	    radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
486
		robj->rdev->asic->mmio_hdp_flush(rdev);
487
	drm_gem_object_put_unlocked(gobj);
J
Jerome Glisse 已提交
488
	r = radeon_gem_handle_lockup(rdev, r);
489 490
	return r;
}
491 492 493 494 495 496

int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	struct drm_radeon_gem_set_tiling *args = data;
	struct drm_gem_object *gobj;
497
	struct radeon_bo *robj;
498 499 500
	int r = 0;

	DRM_DEBUG("%d \n", args->handle);
501
	gobj = drm_gem_object_lookup(filp, args->handle);
502
	if (gobj == NULL)
503
		return -ENOENT;
504
	robj = gem_to_radeon_bo(gobj);
505
	r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
506
	drm_gem_object_put_unlocked(gobj);
507 508 509 510 511 512 513 514
	return r;
}

int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
				struct drm_file *filp)
{
	struct drm_radeon_gem_get_tiling *args = data;
	struct drm_gem_object *gobj;
515
	struct radeon_bo *rbo;
516 517 518
	int r = 0;

	DRM_DEBUG("\n");
519
	gobj = drm_gem_object_lookup(filp, args->handle);
520
	if (gobj == NULL)
521
		return -ENOENT;
522
	rbo = gem_to_radeon_bo(gobj);
523 524
	r = radeon_bo_reserve(rbo, false);
	if (unlikely(r != 0))
525
		goto out;
526 527
	radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
	radeon_bo_unreserve(rbo);
528
out:
529
	drm_gem_object_put_unlocked(gobj);
530 531 532
	return r;
}

533 534 535 536 537 538 539 540 541 542 543 544 545
/**
 * radeon_gem_va_update_vm -update the bo_va in its VM
 *
 * @rdev: radeon_device pointer
 * @bo_va: bo_va to update
 *
 * Update the bo_va directly after setting it's address. Errors are not
 * vital here, so they are not reported back to userspace.
 */
static void radeon_gem_va_update_vm(struct radeon_device *rdev,
				    struct radeon_bo_va *bo_va)
{
	struct ttm_validate_buffer tv, *entry;
546
	struct radeon_bo_list *vm_bos;
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	struct ww_acquire_ctx ticket;
	struct list_head list;
	unsigned domain;
	int r;

	INIT_LIST_HEAD(&list);

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

	vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
	if (!vm_bos)
		return;

562
	r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
	if (r)
		goto error_free;

	list_for_each_entry(entry, &list, head) {
		domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type);
		/* if anything is swapped out don't swap it in here,
		   just abort and wait for the next CS */
		if (domain == RADEON_GEM_DOMAIN_CPU)
			goto error_unreserve;
	}

	mutex_lock(&bo_va->vm->mutex);
	r = radeon_vm_clear_freed(rdev, bo_va->vm);
	if (r)
		goto error_unlock;

	if (bo_va->it.start)
		r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem);

error_unlock:
	mutex_unlock(&bo_va->vm->mutex);

error_unreserve:
	ttm_eu_backoff_reservation(&ticket, &list);

error_free:
M
Michal Hocko 已提交
589
	kvfree(vm_bos);
590

591
	if (r && r != -ERESTARTSYS)
592 593 594
		DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
}

595 596 597 598 599 600 601 602 603 604 605 606
int radeon_gem_va_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	struct drm_radeon_gem_va *args = data;
	struct drm_gem_object *gobj;
	struct radeon_device *rdev = dev->dev_private;
	struct radeon_fpriv *fpriv = filp->driver_priv;
	struct radeon_bo *rbo;
	struct radeon_bo_va *bo_va;
	u32 invalid_flags;
	int r = 0;

607 608 609 610 611
	if (!rdev->vm_manager.enabled) {
		args->operation = RADEON_VA_RESULT_ERROR;
		return -ENOTTY;
	}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	/* !! DONT REMOVE !!
	 * We don't support vm_id yet, to be sure we don't have have broken
	 * userspace, reject anyone trying to use non 0 value thus moving
	 * forward we can use those fields without breaking existant userspace
	 */
	if (args->vm_id) {
		args->operation = RADEON_VA_RESULT_ERROR;
		return -EINVAL;
	}

	if (args->offset < RADEON_VA_RESERVED_SIZE) {
		dev_err(&dev->pdev->dev,
			"offset 0x%lX is in reserved area 0x%X\n",
			(unsigned long)args->offset,
			RADEON_VA_RESERVED_SIZE);
		args->operation = RADEON_VA_RESULT_ERROR;
		return -EINVAL;
	}

	/* don't remove, we need to enforce userspace to set the snooped flag
	 * otherwise we will endup with broken userspace and we won't be able
	 * to enable this feature without adding new interface
	 */
	invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM;
	if ((args->flags & invalid_flags)) {
		dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n",
			args->flags, invalid_flags);
		args->operation = RADEON_VA_RESULT_ERROR;
		return -EINVAL;
	}

	switch (args->operation) {
	case RADEON_VA_MAP:
	case RADEON_VA_UNMAP:
		break;
	default:
		dev_err(&dev->pdev->dev, "unsupported operation %d\n",
			args->operation);
		args->operation = RADEON_VA_RESULT_ERROR;
		return -EINVAL;
	}

654
	gobj = drm_gem_object_lookup(filp, args->handle);
655 656 657 658 659 660 661 662
	if (gobj == NULL) {
		args->operation = RADEON_VA_RESULT_ERROR;
		return -ENOENT;
	}
	rbo = gem_to_radeon_bo(gobj);
	r = radeon_bo_reserve(rbo, false);
	if (r) {
		args->operation = RADEON_VA_RESULT_ERROR;
663
		drm_gem_object_put_unlocked(gobj);
664 665
		return r;
	}
666 667 668
	bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
	if (!bo_va) {
		args->operation = RADEON_VA_RESULT_ERROR;
669
		radeon_bo_unreserve(rbo);
670
		drm_gem_object_put_unlocked(gobj);
671 672 673
		return -ENOENT;
	}

674 675
	switch (args->operation) {
	case RADEON_VA_MAP:
676
		if (bo_va->it.start) {
677
			args->operation = RADEON_VA_RESULT_VA_EXIST;
678
			args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
679
			radeon_bo_unreserve(rbo);
680 681
			goto out;
		}
682
		r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
683 684
		break;
	case RADEON_VA_UNMAP:
685
		r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
686 687 688 689
		break;
	default:
		break;
	}
690 691
	if (!r)
		radeon_gem_va_update_vm(rdev, bo_va);
692 693 694 695 696
	args->operation = RADEON_VA_RESULT_OK;
	if (r) {
		args->operation = RADEON_VA_RESULT_ERROR;
	}
out:
697
	drm_gem_object_put_unlocked(gobj);
698
	return r;
699 700 701 702 703 704 705 706 707 708
}

int radeon_gem_op_ioctl(struct drm_device *dev, void *data,
			struct drm_file *filp)
{
	struct drm_radeon_gem_op *args = data;
	struct drm_gem_object *gobj;
	struct radeon_bo *robj;
	int r;

709
	gobj = drm_gem_object_lookup(filp, args->handle);
710 711 712 713
	if (gobj == NULL) {
		return -ENOENT;
	}
	robj = gem_to_radeon_bo(gobj);
714 715 716 717 718

	r = -EPERM;
	if (radeon_ttm_tt_has_userptr(robj->tbo.ttm))
		goto out;

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	r = radeon_bo_reserve(robj, false);
	if (unlikely(r))
		goto out;

	switch (args->op) {
	case RADEON_GEM_OP_GET_INITIAL_DOMAIN:
		args->value = robj->initial_domain;
		break;
	case RADEON_GEM_OP_SET_INITIAL_DOMAIN:
		robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM |
						      RADEON_GEM_DOMAIN_GTT |
						      RADEON_GEM_DOMAIN_CPU);
		break;
	default:
		r = -EINVAL;
	}

	radeon_bo_unreserve(robj);
out:
738
	drm_gem_object_put_unlocked(gobj);
739
	return r;
740
}
741 742 743 744 745 746 747

int radeon_mode_dumb_create(struct drm_file *file_priv,
			    struct drm_device *dev,
			    struct drm_mode_create_dumb *args)
{
	struct radeon_device *rdev = dev->dev_private;
	struct drm_gem_object *gobj;
748
	uint32_t handle;
749 750
	int r;

751 752
	args->pitch = radeon_align_pitch(rdev, args->width,
					 DIV_ROUND_UP(args->bpp, 8), 0);
753 754 755 756
	args->size = args->pitch * args->height;
	args->size = ALIGN(args->size, PAGE_SIZE);

	r = radeon_gem_object_create(rdev, args->size, 0,
757
				     RADEON_GEM_DOMAIN_VRAM, 0,
758
				     false, &gobj);
759 760 761
	if (r)
		return -ENOMEM;

762 763
	r = drm_gem_handle_create(file_priv, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
764
	drm_gem_object_put_unlocked(gobj);
765 766 767
	if (r) {
		return r;
	}
768
	args->handle = handle;
769 770 771
	return 0;
}

J
Jerome Glisse 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
#if defined(CONFIG_DEBUG_FS)
static int radeon_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;
	struct radeon_device *rdev = dev->dev_private;
	struct radeon_bo *rbo;
	unsigned i = 0;

	mutex_lock(&rdev->gem.mutex);
	list_for_each_entry(rbo, &rdev->gem.objects, list) {
		unsigned domain;
		const char *placement;

		domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type);
		switch (domain) {
		case RADEON_GEM_DOMAIN_VRAM:
			placement = "VRAM";
			break;
		case RADEON_GEM_DOMAIN_GTT:
			placement = " GTT";
			break;
		case RADEON_GEM_DOMAIN_CPU:
		default:
			placement = " CPU";
			break;
		}
		seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n",
			   i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20,
			   placement, (unsigned long)rbo->pid);
		i++;
	}
	mutex_unlock(&rdev->gem.mutex);
	return 0;
}

static struct drm_info_list radeon_debugfs_gem_list[] = {
	{"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL},
};
#endif

int radeon_gem_debugfs_init(struct radeon_device *rdev)
{
#if defined(CONFIG_DEBUG_FS)
	return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1);
#endif
	return 0;
}