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

int radeon_gem_object_init(struct drm_gem_object *obj)
{
35 36
	BUG();

37 38 39 40 41
	return 0;
}

void radeon_gem_object_free(struct drm_gem_object *gobj)
{
42
	struct radeon_bo *robj = gem_to_radeon_bo(gobj);
43 44

	if (robj) {
45
		radeon_bo_unref(&robj);
46 47 48 49
	}
}

int radeon_gem_object_create(struct radeon_device *rdev, int size,
50 51 52
				int alignment, int initial_domain,
				bool discardable, bool kernel,
				struct drm_gem_object **obj)
53
{
54
	struct radeon_bo *robj;
55 56 57 58 59 60 61
	int r;

	*obj = NULL;
	/* At least align on page size */
	if (alignment < PAGE_SIZE) {
		alignment = PAGE_SIZE;
	}
62
	r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain, &robj);
63
	if (r) {
64 65 66
		if (r != -ERESTARTSYS)
			DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n",
				  size, initial_domain, alignment, r);
67 68
		return r;
	}
69 70 71 72 73 74
	*obj = &robj->gem_base;

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

75 76 77 78 79 80
	return 0;
}

int radeon_gem_set_domain(struct drm_gem_object *gobj,
			  uint32_t rdomain, uint32_t wdomain)
{
81
	struct radeon_bo *robj;
82 83 84 85
	uint32_t domain;
	int r;

	/* FIXME: reeimplement */
86
	robj = gem_to_radeon_bo(gobj);
87 88 89 90 91 92 93 94 95 96 97 98
	/* work out where to validate the buffer to */
	domain = wdomain;
	if (!domain) {
		domain = rdomain;
	}
	if (!domain) {
		/* Do nothings */
		printk(KERN_WARNING "Set domain withou domain !\n");
		return 0;
	}
	if (domain == RADEON_GEM_DOMAIN_CPU) {
		/* Asking for cpu access wait for object idle */
99
		r = radeon_bo_wait(robj, NULL, false);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		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)
{
116
	radeon_bo_force_delete(rdev);
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * 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)
{
	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;
	struct radeon_bo_va *bo_va, *tmp;

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

	if (radeon_bo_reserve(rbo, false)) {
		return;
	}
	list_for_each_entry_safe(bo_va, tmp, &rbo->va, bo_list) {
		if (bo_va->vm == vm) {
			/* remove from this vm address space */
			mutex_lock(&vm->mutex);
			list_del(&bo_va->vm_list);
			mutex_unlock(&vm->mutex);
			list_del(&bo_va->bo_list);
			kfree(bo_va);
		}
	}
	radeon_bo_unreserve(rbo);
}

157 158 159 160 161 162 163 164 165 166 167
static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r)
{
	if (r == -EDEADLK) {
		radeon_mutex_lock(&rdev->cs_mutex);
		r = radeon_gpu_reset(rdev);
		if (!r)
			r = -EAGAIN;
		radeon_mutex_unlock(&rdev->cs_mutex);
	}
	return r;
}
168 169 170 171 172 173 174 175 176

/*
 * 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;
177
	struct ttm_mem_type_manager *man;
178
	unsigned i;
179 180

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

182
	args->vram_size = rdev->mc.real_vram_size;
183
	args->vram_visible = (u64)man->size << PAGE_SHIFT;
184
	if (rdev->stollen_vga_memory)
185
		args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory);
186
	args->vram_visible -= radeon_fbdev_total_size(rdev);
187
	args->gart_size = rdev->mc.gtt_size - 4096 - RADEON_IB_POOL_SIZE*64*1024;
188
	for(i = 0; i < RADEON_NUM_RINGS; ++i)
189
		args->gart_size -= rdev->ring[i].ring_size;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	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;

	/* 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,
221 222
					args->initial_domain, false,
					false, &gobj);
223
	if (r) {
224
		r = radeon_gem_handle_lockup(rdev, r);
225 226 227
		return r;
	}
	r = drm_gem_handle_create(filp, gobj, &handle);
228 229
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
230
	if (r) {
231
		r = radeon_gem_handle_lockup(rdev, r);
232 233 234 235 236 237 238 239 240 241 242 243 244
		return r;
	}
	args->handle = handle;
	return 0;
}

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 */
	struct drm_radeon_gem_set_domain *args = data;
	struct drm_gem_object *gobj;
245
	struct radeon_bo *robj;
246 247 248 249 250 251 252 253
	int r;

	/* for now if someone requests domain CPU -
	 * just make sure the buffer is finished with */

	/* just do a BO wait for now */
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
254
		return -ENOENT;
255
	}
256
	robj = gem_to_radeon_bo(gobj);
257 258 259

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

260
	drm_gem_object_unreference_unlocked(gobj);
261
	r = radeon_gem_handle_lockup(robj->rdev, r);
262 263 264
	return r;
}

265 266 267
int radeon_mode_dumb_mmap(struct drm_file *filp,
			  struct drm_device *dev,
			  uint32_t handle, uint64_t *offset_p)
268 269
{
	struct drm_gem_object *gobj;
270
	struct radeon_bo *robj;
271

272
	gobj = drm_gem_object_lookup(dev, filp, handle);
273
	if (gobj == NULL) {
274
		return -ENOENT;
275
	}
276
	robj = gem_to_radeon_bo(gobj);
277
	*offset_p = radeon_bo_mmap_offset(robj);
278
	drm_gem_object_unreference_unlocked(gobj);
279
	return 0;
280 281
}

282 283 284 285 286 287 288 289
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);
}

290 291 292
int radeon_gem_busy_ioctl(struct drm_device *dev, void *data,
			  struct drm_file *filp)
{
293 294
	struct drm_radeon_gem_busy *args = data;
	struct drm_gem_object *gobj;
295
	struct radeon_bo *robj;
296
	int r;
297
	uint32_t cur_placement = 0;
298 299 300

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
301
		return -ENOENT;
302
	}
303
	robj = gem_to_radeon_bo(gobj);
304
	r = radeon_bo_wait(robj, &cur_placement, true);
305 306
	switch (cur_placement) {
	case TTM_PL_VRAM:
307
		args->domain = RADEON_GEM_DOMAIN_VRAM;
308 309
		break;
	case TTM_PL_TT:
310
		args->domain = RADEON_GEM_DOMAIN_GTT;
311 312
		break;
	case TTM_PL_SYSTEM:
313
		args->domain = RADEON_GEM_DOMAIN_CPU;
314 315 316
	default:
		break;
	}
317
	drm_gem_object_unreference_unlocked(gobj);
318
	r = radeon_gem_handle_lockup(robj->rdev, r);
319
	return r;
320 321 322 323 324 325 326
}

int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
			      struct drm_file *filp)
{
	struct drm_radeon_gem_wait_idle *args = data;
	struct drm_gem_object *gobj;
327
	struct radeon_bo *robj;
328 329 330 331
	int r;

	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL) {
332
		return -ENOENT;
333
	}
334
	robj = gem_to_radeon_bo(gobj);
335
	r = radeon_bo_wait(robj, NULL, false);
336 337 338
	/* callback hw specific functions if any */
	if (robj->rdev->asic->ioctl_wait_idle)
		robj->rdev->asic->ioctl_wait_idle(robj->rdev, robj);
339
	drm_gem_object_unreference_unlocked(gobj);
340
	r = radeon_gem_handle_lockup(robj->rdev, r);
341 342
	return r;
}
343 344 345 346 347 348

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;
349
	struct radeon_bo *robj;
350 351 352 353 354
	int r = 0;

	DRM_DEBUG("%d \n", args->handle);
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL)
355
		return -ENOENT;
356
	robj = gem_to_radeon_bo(gobj);
357
	r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch);
358
	drm_gem_object_unreference_unlocked(gobj);
359 360 361 362 363 364 365 366
	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;
367
	struct radeon_bo *rbo;
368 369 370 371 372
	int r = 0;

	DRM_DEBUG("\n");
	gobj = drm_gem_object_lookup(dev, filp, args->handle);
	if (gobj == NULL)
373
		return -ENOENT;
374
	rbo = gem_to_radeon_bo(gobj);
375 376
	r = radeon_bo_reserve(rbo, false);
	if (unlikely(r != 0))
377
		goto out;
378 379
	radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch);
	radeon_bo_unreserve(rbo);
380
out:
381
	drm_gem_object_unreference_unlocked(gobj);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	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;

397 398 399 400 401
	if (!rdev->vm_manager.enabled) {
		args->operation = RADEON_VA_RESULT_ERROR;
		return -ENOTTY;
	}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	/* !! 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;
	}
	if (!(args->flags & RADEON_VM_PAGE_SNOOPED)) {
		dev_err(&dev->pdev->dev, "only supported snooped mapping for now\n");
		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;
	}
	switch (args->operation) {
	case RADEON_VA_MAP:
		bo_va = radeon_bo_va(rbo, &fpriv->vm);
		if (bo_va) {
			args->operation = RADEON_VA_RESULT_VA_EXIST;
			args->offset = bo_va->soffset;
			goto out;
		}
		r = radeon_vm_bo_add(rdev, &fpriv->vm, rbo,
				     args->offset, args->flags);
		break;
	case RADEON_VA_UNMAP:
		r = radeon_vm_bo_rmv(rdev, &fpriv->vm, rbo);
		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);
485 486
	return r;
}
487 488 489 490 491 492 493

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;
494
	uint32_t handle;
495 496 497 498 499 500 501 502 503 504 505 506 507
	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,
				     RADEON_GEM_DOMAIN_VRAM,
				     false, ttm_bo_type_device,
				     &gobj);
	if (r)
		return -ENOMEM;

508 509 510
	r = drm_gem_handle_create(file_priv, gobj, &handle);
	/* drop reference from allocate - handle holds it now */
	drm_gem_object_unreference_unlocked(gobj);
511 512 513
	if (r) {
		return r;
	}
514
	args->handle = handle;
515 516 517 518 519 520 521 522 523
	return 0;
}

int radeon_mode_dumb_destroy(struct drm_file *file_priv,
			     struct drm_device *dev,
			     uint32_t handle)
{
	return drm_gem_handle_delete(file_priv, handle);
}