radeon_gem.c 17.7 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 38
		if (robj->gem_base.import_attach)
			drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
39
		radeon_bo_unref(&robj);
40 41 42
	}
}

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

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

58 59 60 61
	/* 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;
62
	if (size > max_size) {
63
		DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n",
64
			  size >> 20, max_size >> 20);
65 66 67
		return -ENOMEM;
	}

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

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

89 90 91
	return 0;
}

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

	/* FIXME: reeimplement */
100
	robj = gem_to_radeon_bo(gobj);
101 102 103 104 105 106 107
	/* work out where to validate the buffer to */
	domain = wdomain;
	if (!domain) {
		domain = rdomain;
	}
	if (!domain) {
		/* Do nothings */
M
Masanari Iida 已提交
108
		printk(KERN_WARNING "Set domain without domain !\n");
109 110 111 112
		return 0;
	}
	if (domain == RADEON_GEM_DOMAIN_CPU) {
		/* Asking for cpu access wait for object idle */
113
		r = radeon_bo_wait(robj, NULL, false);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		if (r) {
			printk(KERN_ERR "Failed to wait for object !\n");
			return r;
		}
	}
	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)
{
130
	radeon_bo_force_delete(rdev);
131 132
}

133 134 135 136 137 138
/*
 * 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)
{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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;

	if (rdev->family < CHIP_CAYMAN) {
		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);

163 164 165 166 167 168 169 170 171 172
	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;
173
	struct radeon_bo_va *bo_va;
174
	int r;
175 176 177 178 179

	if (rdev->family < CHIP_CAYMAN) {
		return;
	}

180 181 182 183
	r = radeon_bo_reserve(rbo, true);
	if (r) {
		dev_err(rdev->dev, "leaking bo va because "
			"we fail to reserve bo (%d)\n", r);
184 185
		return;
	}
186 187 188 189 190 191
	bo_va = radeon_vm_bo_find(vm, rbo);
	if (bo_va) {
		if (--bo_va->ref_count == 0) {
			radeon_vm_bo_rmv(rdev, bo_va);
		}
	}
192 193 194
	radeon_bo_unreserve(rbo);
}

195 196 197 198 199 200 201 202 203
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;
}
204 205 206 207 208 209 210 211 212

/*
 * 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;
213 214 215
	struct ttm_mem_type_manager *man;

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

217
	args->vram_size = rdev->mc.real_vram_size;
218
	args->vram_visible = (u64)man->size << PAGE_SHIFT;
219 220 221 222
	args->vram_visible -= rdev->vram_pin_size;
	args->gart_size = rdev->mc.gtt_size;
	args->gart_size -= rdev->gart_pin_size;

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	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;

251
	down_read(&rdev->exclusive_lock);
252 253 254
	/* 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,
255
				     args->initial_domain, args->flags,
256
				     false, &gobj);
257
	if (r) {
258
		up_read(&rdev->exclusive_lock);
259
		r = radeon_gem_handle_lockup(rdev, r);
260 261 262
		return r;
	}
	r = drm_gem_handle_create(filp, gobj, &handle);
263 264
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
265
	if (r) {
266
		up_read(&rdev->exclusive_lock);
267
		r = radeon_gem_handle_lockup(rdev, r);
268 269 270
		return r;
	}
	args->handle = handle;
271
	up_read(&rdev->exclusive_lock);
272 273 274
	return 0;
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
			     struct drm_file *filp)
{
	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;

	/* we only support read only mappings for now */
	if (!(args->flags & RADEON_GEM_USERPTR_READONLY))
		return -EACCES;

	/* reject unknown flag values */
	if (args->flags & ~RADEON_GEM_USERPTR_READONLY)
		return -EINVAL;

	/* readonly pages not tested on older hardware */
	if (rdev->family < CHIP_R600)
		return -EINVAL;

	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;

	r = drm_gem_handle_create(filp, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
	if (r)
		goto handle_lockup;

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

release_object:
	drm_gem_object_unreference_unlocked(gobj);

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

	return r;
}

334 335 336 337 338
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 */
339
	struct radeon_device *rdev = dev->dev_private;
340 341
	struct drm_radeon_gem_set_domain *args = data;
	struct drm_gem_object *gobj;
342
	struct radeon_bo *robj;
343 344 345 346
	int r;

	/* for now if someone requests domain CPU -
	 * just make sure the buffer is finished with */
347
	down_read(&rdev->exclusive_lock);
348 349 350 351

	/* just do a BO wait for now */
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
352
		up_read(&rdev->exclusive_lock);
353
		return -ENOENT;
354
	}
355
	robj = gem_to_radeon_bo(gobj);
356 357 358

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

359
	drm_gem_object_unreference_unlocked(gobj);
360
	up_read(&rdev->exclusive_lock);
361
	r = radeon_gem_handle_lockup(robj->rdev, r);
362 363 364
	return r;
}

365 366 367
int radeon_mode_dumb_mmap(struct drm_file *filp,
			  struct drm_device *dev,
			  uint32_t handle, uint64_t *offset_p)
368 369
{
	struct drm_gem_object *gobj;
370
	struct radeon_bo *robj;
371

372
	gobj = drm_gem_object_lookup(dev, filp, handle);
373
	if (gobj == NULL) {
374
		return -ENOENT;
375
	}
376
	robj = gem_to_radeon_bo(gobj);
377 378 379 380
	if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) {
		drm_gem_object_unreference_unlocked(gobj);
		return -EPERM;
	}
381
	*offset_p = radeon_bo_mmap_offset(robj);
382
	drm_gem_object_unreference_unlocked(gobj);
383
	return 0;
384 385
}

386 387 388 389 390 391 392 393
int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
	struct drm_radeon_gem_mmap *args = data;

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

394 395 396
int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
J
Jerome Glisse 已提交
397
	struct radeon_device *rdev = dev->dev_private;
398 399
	struct drm_radeon_gem_busy *args = data;
	struct drm_gem_object *gobj;
400
	struct radeon_bo *robj;
401
	int r;
402
	uint32_t cur_placement = 0;
403 404 405

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
406
		return -ENOENT;
407
	}
408
	robj = gem_to_radeon_bo(gobj);
409
	r = radeon_bo_wait(robj, &cur_placement, true);
410
	args->domain = radeon_mem_type_to_domain(cur_placement);
411
	drm_gem_object_unreference_unlocked(gobj);
J
Jerome Glisse 已提交
412
	r = radeon_gem_handle_lockup(rdev, r);
413
	return r;
414 415 416 417 418
}

int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
			      struct drm_file *filp)
{
J
Jerome Glisse 已提交
419
	struct radeon_device *rdev = dev->dev_private;
420 421
	struct drm_radeon_gem_wait_idle *args = data;
	struct drm_gem_object *gobj;
422
	struct radeon_bo *robj;
423
	int r;
424
	uint32_t cur_placement = 0;
425 426 427

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
428
		return -ENOENT;
429
	}
430
	robj = gem_to_radeon_bo(gobj);
431
	r = radeon_bo_wait(robj, &cur_placement, false);
432
	/* Flush HDP cache via MMIO if necessary */
433 434
	if (rdev->asic->mmio_hdp_flush &&
	    radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM)
435
		robj->rdev->asic->mmio_hdp_flush(rdev);
436
	drm_gem_object_unreference_unlocked(gobj);
J
Jerome Glisse 已提交
437
	r = radeon_gem_handle_lockup(rdev, r);
438 439
	return r;
}
440 441 442 443 444 445

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;
446
	struct radeon_bo *robj;
447 448 449 450 451
	int r = 0;

	DRM_DEBUG("%d \n", args->handle);
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL)
452
		return -ENOENT;
453
	robj = gem_to_radeon_bo(gobj);
454
	r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
455
	drm_gem_object_unreference_unlocked(gobj);
456 457 458 459 460 461 462 463
	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;
464
	struct radeon_bo *rbo;
465 466 467 468 469
	int r = 0;

	DRM_DEBUG("\n");
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL)
470
		return -ENOENT;
471
	rbo = gem_to_radeon_bo(gobj);
472 473
	r = radeon_bo_reserve(rbo, false);
	if (unlikely(r != 0))
474
		goto out;
475 476
	radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
	radeon_bo_unreserve(rbo);
477
out:
478
	drm_gem_object_unreference_unlocked(gobj);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	return r;
}

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;

494 495 496 497 498
	if (!rdev->vm_manager.enabled) {
		args->operation = RADEON_VA_RESULT_ERROR;
		return -ENOTTY;
	}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	/* !! 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;
	}

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	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;
		drm_gem_object_unreference_unlocked(gobj);
		return r;
	}
553 554 555 556 557 558 559
	bo_va = radeon_vm_bo_find(&fpriv->vm, rbo);
	if (!bo_va) {
		args->operation = RADEON_VA_RESULT_ERROR;
		drm_gem_object_unreference_unlocked(gobj);
		return -ENOENT;
	}

560 561
	switch (args->operation) {
	case RADEON_VA_MAP:
562
		if (bo_va->it.start) {
563
			args->operation = RADEON_VA_RESULT_VA_EXIST;
564
			args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE;
565 566
			goto out;
		}
567
		r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags);
568 569
		break;
	case RADEON_VA_UNMAP:
570
		r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0);
571 572 573 574 575 576 577 578 579 580 581
		break;
	default:
		break;
	}
	args->operation = RADEON_VA_RESULT_OK;
	if (r) {
		args->operation = RADEON_VA_RESULT_ERROR;
	}
out:
	radeon_bo_unreserve(rbo);
	drm_gem_object_unreference_unlocked(gobj);
582
	return r;
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
}

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;

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
		return -ENOENT;
	}
	robj = gem_to_radeon_bo(gobj);
598 599 600 601 602

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

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	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:
	drm_gem_object_unreference_unlocked(gobj);
	return r;
624
}
625 626 627 628 629 630 631

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;
632
	uint32_t handle;
633 634 635 636 637 638 639
	int r;

	args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8);
	args->size = args->pitch * args->height;
	args->size = ALIGN(args->size, PAGE_SIZE);

	r = radeon_gem_object_create(rdev, args->size, 0,
640
				     RADEON_GEM_DOMAIN_VRAM, 0,
641
				     false, &gobj);
642 643 644
	if (r)
		return -ENOMEM;

645 646 647
	r = drm_gem_handle_create(file_priv, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
648 649 650
	if (r) {
		return r;
	}
651
	args->handle = handle;
652 653 654
	return 0;
}

J
Jerome Glisse 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
#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;
}