radeon_cs.c 23.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
/*
 * Copyright 2008 Jerome Glisse.
 * All Rights Reserved.
 *
 * 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 (including the next
 * paragraph) 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
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS 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:
 *    Jerome Glisse <glisse@freedesktop.org>
 */
27
#include <linux/list_sort.h>
28 29
#include <drm/drmP.h>
#include <drm/radeon_drm.h>
30 31
#include "radeon_reg.h"
#include "radeon.h"
32
#include "radeon_trace.h"
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define RADEON_CS_MAX_PRIORITY		32u
#define RADEON_CS_NUM_BUCKETS		(RADEON_CS_MAX_PRIORITY + 1)

/* This is based on the bucket sort with O(n) time complexity.
 * An item with priority "i" is added to bucket[i]. The lists are then
 * concatenated in descending order.
 */
struct radeon_cs_buckets {
	struct list_head bucket[RADEON_CS_NUM_BUCKETS];
};

static void radeon_cs_buckets_init(struct radeon_cs_buckets *b)
{
	unsigned i;

	for (i = 0; i < RADEON_CS_NUM_BUCKETS; i++)
		INIT_LIST_HEAD(&b->bucket[i]);
}

static void radeon_cs_buckets_add(struct radeon_cs_buckets *b,
				  struct list_head *item, unsigned priority)
{
	/* Since buffers which appear sooner in the relocation list are
	 * likely to be used more often than buffers which appear later
	 * in the list, the sort mustn't change the ordering of buffers
	 * with the same priority, i.e. it must be stable.
	 */
	list_add_tail(item, &b->bucket[min(priority, RADEON_CS_MAX_PRIORITY)]);
}

static void radeon_cs_buckets_get_list(struct radeon_cs_buckets *b,
				       struct list_head *out_list)
{
	unsigned i;

	/* Connect the sorted buckets in the output list. */
	for (i = 0; i < RADEON_CS_NUM_BUCKETS; i++) {
		list_splice(&b->bucket[i], out_list);
	}
}

75
static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
76 77 78
{
	struct drm_device *ddev = p->rdev->ddev;
	struct radeon_cs_chunk *chunk;
79
	struct radeon_cs_buckets buckets;
80
	unsigned i, j;
81 82
	bool duplicate, need_mmap_lock = false;
	int r;
83 84 85 86 87

	if (p->chunk_relocs_idx == -1) {
		return 0;
	}
	chunk = &p->chunks[p->chunk_relocs_idx];
88
	p->dma_reloc_idx = 0;
89 90 91 92 93 94 95 96 97 98
	/* FIXME: we assume that each relocs use 4 dwords */
	p->nrelocs = chunk->length_dw / 4;
	p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
	if (p->relocs_ptr == NULL) {
		return -ENOMEM;
	}
	p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
	if (p->relocs == NULL) {
		return -ENOMEM;
	}
99 100 101

	radeon_cs_buckets_init(&buckets);

102 103
	for (i = 0; i < p->nrelocs; i++) {
		struct drm_radeon_cs_reloc *r;
104
		unsigned priority;
105 106 107

		duplicate = false;
		r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
108
		for (j = 0; j < i; j++) {
109 110 111 112 113 114
			if (r->handle == p->relocs[j].handle) {
				p->relocs_ptr[i] = &p->relocs[j];
				duplicate = true;
				break;
			}
		}
115
		if (duplicate) {
116
			p->relocs[i].handle = 0;
117 118 119 120 121 122 123 124 125 126 127 128
			continue;
		}

		p->relocs[i].gobj = drm_gem_object_lookup(ddev, p->filp,
							  r->handle);
		if (p->relocs[i].gobj == NULL) {
			DRM_ERROR("gem object lookup failed 0x%x\n",
				  r->handle);
			return -ENOENT;
		}
		p->relocs_ptr[i] = &p->relocs[i];
		p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
129 130 131 132 133 134 135

		/* The userspace buffer priorities are from 0 to 15. A higher
		 * number means the buffer is more important.
		 * Also, the buffers used for write have a higher priority than
		 * the buffers used for read only, which doubles the range
		 * to 0 to 31. 32 is reserved for the kernel driver.
		 */
136 137
		priority = (r->flags & RADEON_RELOC_PRIO_MASK) * 2
			   + !!r->write_domain;
138

139
		/* the first reloc of an UVD job is the msg and that must be in
140 141
		   VRAM, also but everything into VRAM on AGP cards and older
		   IGP chips to avoid image corruptions */
142
		if (p->ring == R600_RING_TYPE_UVD_INDEX &&
143 144 145 146
		    (i == 0 || drm_pci_device_is_agp(p->rdev->ddev) ||
		     p->rdev->family == CHIP_RS780 ||
		     p->rdev->family == CHIP_RS880)) {

147
			/* TODO: is this still needed for NI+ ? */
148
			p->relocs[i].prefered_domains =
C
Christian König 已提交
149 150
				RADEON_GEM_DOMAIN_VRAM;

151
			p->relocs[i].allowed_domains =
C
Christian König 已提交
152 153
				RADEON_GEM_DOMAIN_VRAM;

154 155
			/* prioritize this over any other relocation */
			priority = RADEON_CS_MAX_PRIORITY;
C
Christian König 已提交
156 157 158 159
		} else {
			uint32_t domain = r->write_domain ?
				r->write_domain : r->read_domains;

160 161 162 163 164 165
			if (domain & RADEON_GEM_DOMAIN_CPU) {
				DRM_ERROR("RADEON_GEM_DOMAIN_CPU is not valid "
					  "for command submission\n");
				return -EINVAL;
			}

166
			p->relocs[i].prefered_domains = domain;
C
Christian König 已提交
167 168
			if (domain == RADEON_GEM_DOMAIN_VRAM)
				domain |= RADEON_GEM_DOMAIN_GTT;
169
			p->relocs[i].allowed_domains = domain;
C
Christian König 已提交
170
		}
171

172 173 174 175 176 177 178 179 180 181 182 183 184
		if (radeon_ttm_tt_has_userptr(p->relocs[i].robj->tbo.ttm)) {
			uint32_t domain = p->relocs[i].prefered_domains;
			if (!(domain & RADEON_GEM_DOMAIN_GTT)) {
				DRM_ERROR("Only RADEON_GEM_DOMAIN_GTT is "
					  "allowed for userptr BOs\n");
				return -EINVAL;
			}
			need_mmap_lock = true;
			domain = RADEON_GEM_DOMAIN_GTT;
			p->relocs[i].prefered_domains = domain;
			p->relocs[i].allowed_domains = domain;
		}

185
		p->relocs[i].tv.bo = &p->relocs[i].robj->tbo;
186
		p->relocs[i].tv.shared = !r->write_domain;
187 188
		p->relocs[i].handle = r->handle;

189
		radeon_cs_buckets_add(&buckets, &p->relocs[i].tv.head,
190
				      priority);
191
	}
192 193 194

	radeon_cs_buckets_get_list(&buckets, &p->validated);

195 196 197
	if (p->cs_flags & RADEON_CS_USE_VM)
		p->vm_bos = radeon_vm_get_bos(p->rdev, p->ib.vm,
					      &p->validated);
198 199 200 201 202 203 204
	if (need_mmap_lock)
		down_read(&current->mm->mmap_sem);

	r = radeon_bo_list_validate(p->rdev, &p->ticket, &p->validated, p->ring);

	if (need_mmap_lock)
		up_read(&current->mm->mmap_sem);
205

206
	return r;
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220
static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
{
	p->priority = priority;

	switch (ring) {
	default:
		DRM_ERROR("unknown ring id: %d\n", ring);
		return -EINVAL;
	case RADEON_CS_RING_GFX:
		p->ring = RADEON_RING_TYPE_GFX_INDEX;
		break;
	case RADEON_CS_RING_COMPUTE:
221
		if (p->rdev->family >= CHIP_TAHITI) {
222 223 224 225 226 227
			if (p->priority > 0)
				p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
			else
				p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
		} else
			p->ring = RADEON_RING_TYPE_GFX_INDEX;
228
		break;
229 230 231 232 233 234
	case RADEON_CS_RING_DMA:
		if (p->rdev->family >= CHIP_CAYMAN) {
			if (p->priority > 0)
				p->ring = R600_RING_TYPE_DMA_INDEX;
			else
				p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
235
		} else if (p->rdev->family >= CHIP_RV770) {
236 237 238 239 240
			p->ring = R600_RING_TYPE_DMA_INDEX;
		} else {
			return -EINVAL;
		}
		break;
C
Christian König 已提交
241 242 243
	case RADEON_CS_RING_UVD:
		p->ring = R600_RING_TYPE_UVD_INDEX;
		break;
244 245 246 247
	case RADEON_CS_RING_VCE:
		/* TODO: only use the low priority ring for now */
		p->ring = TN_RING_TYPE_VCE1_INDEX;
		break;
248 249 250 251
	}
	return 0;
}

252
static int radeon_cs_sync_rings(struct radeon_cs_parser *p)
253
{
254
	int i, r = 0;
255

256
	for (i = 0; i < p->nrelocs; i++) {
257 258
		struct reservation_object *resv;

259
		if (!p->relocs[i].robj)
260 261
			continue;

262
		resv = p->relocs[i].robj->tbo.resv;
263 264 265 266 267
		r = radeon_semaphore_sync_resv(p->rdev, p->ib.semaphore, resv,
					       p->relocs[i].tv.shared);

		if (r)
			break;
268
	}
269
	return r;
270 271
}

272
/* XXX: note that this is called from the legacy UMS CS ioctl as well */
273 274 275 276
int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
{
	struct drm_radeon_cs *cs = data;
	uint64_t *chunk_array_ptr;
277 278 279
	unsigned size, i;
	u32 ring = RADEON_CS_RING_GFX;
	s32 priority = 0;
280 281 282 283 284 285 286

	if (!cs->num_chunks) {
		return 0;
	}
	/* get chunks */
	INIT_LIST_HEAD(&p->validated);
	p->idx = 0;
287 288 289 290
	p->ib.sa_bo = NULL;
	p->ib.semaphore = NULL;
	p->const_ib.sa_bo = NULL;
	p->const_ib.semaphore = NULL;
291 292
	p->chunk_ib_idx = -1;
	p->chunk_relocs_idx = -1;
293
	p->chunk_flags_idx = -1;
294
	p->chunk_const_ib_idx = -1;
295 296 297 298 299
	p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
	if (p->chunks_array == NULL) {
		return -ENOMEM;
	}
	chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
D
Daniel Vetter 已提交
300
	if (copy_from_user(p->chunks_array, chunk_array_ptr,
301 302 303
			       sizeof(uint64_t)*cs->num_chunks)) {
		return -EFAULT;
	}
304
	p->cs_flags = 0;
305 306 307 308 309 310 311 312 313 314 315
	p->nchunks = cs->num_chunks;
	p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
	if (p->chunks == NULL) {
		return -ENOMEM;
	}
	for (i = 0; i < p->nchunks; i++) {
		struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
		struct drm_radeon_cs_chunk user_chunk;
		uint32_t __user *cdata;

		chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
D
Daniel Vetter 已提交
316
		if (copy_from_user(&user_chunk, chunk_ptr,
317 318 319
				       sizeof(struct drm_radeon_cs_chunk))) {
			return -EFAULT;
		}
320
		p->chunks[i].length_dw = user_chunk.length_dw;
321 322 323 324 325 326
		p->chunks[i].chunk_id = user_chunk.chunk_id;
		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
			p->chunk_relocs_idx = i;
		}
		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
			p->chunk_ib_idx = i;
327 328 329
			/* zero length IB isn't useful */
			if (p->chunks[i].length_dw == 0)
				return -EINVAL;
330
		}
331 332 333 334 335 336
		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
			p->chunk_const_ib_idx = i;
			/* zero length CONST IB isn't useful */
			if (p->chunks[i].length_dw == 0)
				return -EINVAL;
		}
337 338 339 340 341
		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
			p->chunk_flags_idx = i;
			/* zero length flags aren't useful */
			if (p->chunks[i].length_dw == 0)
				return -EINVAL;
342
		}
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
		size = p->chunks[i].length_dw;
		cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
		p->chunks[i].user_ptr = cdata;
		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB)
			continue;

		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
			if (!p->rdev || !(p->rdev->flags & RADEON_IS_AGP))
				continue;
		}

		p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
		size *= sizeof(uint32_t);
		if (p->chunks[i].kdata == NULL) {
			return -ENOMEM;
		}
D
Daniel Vetter 已提交
360
		if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
361 362 363 364 365 366 367 368
			return -EFAULT;
		}
		if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
			p->cs_flags = p->chunks[i].kdata[0];
			if (p->chunks[i].length_dw > 1)
				ring = p->chunks[i].kdata[1];
			if (p->chunks[i].length_dw > 2)
				priority = (s32)p->chunks[i].kdata[2];
369 370
		}
	}
371

372 373 374 375 376 377 378
	/* these are KMS only */
	if (p->rdev) {
		if ((p->cs_flags & RADEON_CS_USE_VM) &&
		    !p->rdev->vm_manager.enabled) {
			DRM_ERROR("VM not active on asic!\n");
			return -EINVAL;
		}
379

380
		if (radeon_cs_get_ring(p, ring, priority))
381
			return -EINVAL;
382

383
		/* we only support VM on some SI+ rings */
384 385 386 387 388 389 390 391 392 393 394
		if ((p->cs_flags & RADEON_CS_USE_VM) == 0) {
			if (p->rdev->asic->ring[p->ring]->cs_parse == NULL) {
				DRM_ERROR("Ring %d requires VM!\n", p->ring);
				return -EINVAL;
			}
		} else {
			if (p->rdev->asic->ring[p->ring]->ib_parse == NULL) {
				DRM_ERROR("VM not supported on ring %d!\n",
					  p->ring);
				return -EINVAL;
			}
395
		}
396
	}
397

398 399 400
	return 0;
}

401 402 403
static int cmp_size_smaller_first(void *priv, struct list_head *a,
				  struct list_head *b)
{
404 405
	struct radeon_cs_reloc *la = list_entry(a, struct radeon_cs_reloc, tv.head);
	struct radeon_cs_reloc *lb = list_entry(b, struct radeon_cs_reloc, tv.head);
406 407

	/* Sort A before B if A is smaller. */
408
	return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
409 410
}

411 412 413 414 415 416 417 418
/**
 * cs_parser_fini() - clean parser states
 * @parser:	parser structure holding parsing context.
 * @error:	error number
 *
 * If error is set than unvalidate buffer, otherwise just free memory
 * used by parsing context.
 **/
419
static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error, bool backoff)
420 421 422
{
	unsigned i;

423
	if (!error) {
424 425 426 427 428 429 430 431 432 433 434 435
		/* Sort the buffer list from the smallest to largest buffer,
		 * which affects the order of buffers in the LRU list.
		 * This assures that the smallest buffers are added first
		 * to the LRU list, so they are likely to be later evicted
		 * first, instead of large buffers whose eviction is more
		 * expensive.
		 *
		 * This slightly lowers the number of bytes moved by TTM
		 * per frame under memory pressure.
		 */
		list_sort(NULL, &parser->validated, cmp_size_smaller_first);

436 437
		ttm_eu_fence_buffer_objects(&parser->ticket,
					    &parser->validated,
438
					    &parser->ib.fence->base);
439 440 441
	} else if (backoff) {
		ttm_eu_backoff_reservation(&parser->ticket,
					   &parser->validated);
442
	}
443

444 445 446 447 448
	if (parser->relocs != NULL) {
		for (i = 0; i < parser->nrelocs; i++) {
			if (parser->relocs[i].gobj)
				drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
		}
449
	}
450
	kfree(parser->track);
451 452
	kfree(parser->relocs);
	kfree(parser->relocs_ptr);
453
	kfree(parser->vm_bos);
454 455
	for (i = 0; i < parser->nchunks; i++)
		drm_free_large(parser->chunks[i].kdata);
456 457 458
	kfree(parser->chunks);
	kfree(parser->chunks_array);
	radeon_ib_free(parser->rdev, &parser->ib);
459
	radeon_ib_free(parser->rdev, &parser->const_ib);
460 461
}

462 463 464 465 466 467 468 469 470 471 472
static int radeon_cs_ib_chunk(struct radeon_device *rdev,
			      struct radeon_cs_parser *parser)
{
	int r;

	if (parser->chunk_ib_idx == -1)
		return 0;

	if (parser->cs_flags & RADEON_CS_USE_VM)
		return 0;

473
	r = radeon_cs_parse(rdev, parser->ring, parser);
474 475 476 477
	if (r || parser->parser_error) {
		DRM_ERROR("Invalid command stream !\n");
		return r;
	}
478

479 480 481 482 483 484 485
	r = radeon_cs_sync_rings(parser);
	if (r) {
		if (r != -ERESTARTSYS)
			DRM_ERROR("Failed to sync rings: %i\n", r);
		return r;
	}

486 487
	if (parser->ring == R600_RING_TYPE_UVD_INDEX)
		radeon_uvd_note_usage(rdev);
488 489 490
	else if ((parser->ring == TN_RING_TYPE_VCE1_INDEX) ||
		 (parser->ring == TN_RING_TYPE_VCE2_INDEX))
		radeon_vce_note_usage(rdev);
491

492
	r = radeon_ib_schedule(rdev, &parser->ib, NULL, true);
493 494 495
	if (r) {
		DRM_ERROR("Failed to schedule IB !\n");
	}
496
	return r;
497 498
}

499
static int radeon_bo_vm_update_pte(struct radeon_cs_parser *p,
500 501
				   struct radeon_vm *vm)
{
502
	struct radeon_device *rdev = p->rdev;
503
	struct radeon_bo_va *bo_va;
504
	int i, r;
505

506 507
	r = radeon_vm_update_page_directory(rdev, vm);
	if (r)
508
		return r;
509

510 511 512 513
	r = radeon_vm_clear_freed(rdev, vm);
	if (r)
		return r;

514
	if (vm->ib_bo_va == NULL) {
515 516 517 518
		DRM_ERROR("Tmp BO not in VM!\n");
		return -EINVAL;
	}

519 520
	r = radeon_vm_bo_update(rdev, vm->ib_bo_va,
				&rdev->ring_tmp_bo.bo->tbo.mem);
521 522 523 524 525 526 527 528 529 530 531
	if (r)
		return r;

	for (i = 0; i < p->nrelocs; i++) {
		struct radeon_bo *bo;

		/* ignore duplicates */
		if (p->relocs_ptr[i] != &p->relocs[i])
			continue;

		bo = p->relocs[i].robj;
532 533 534 535 536 537 538
		bo_va = radeon_vm_bo_find(vm, bo);
		if (bo_va == NULL) {
			dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
			return -EINVAL;
		}

		r = radeon_vm_bo_update(rdev, bo_va, &bo->tbo.mem);
539
		if (r)
540 541
			return r;
	}
542 543

	return radeon_vm_clear_invalids(rdev, vm);
544 545 546 547 548 549 550 551 552 553 554 555 556 557
}

static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
				 struct radeon_cs_parser *parser)
{
	struct radeon_fpriv *fpriv = parser->filp->driver_priv;
	struct radeon_vm *vm = &fpriv->vm;
	int r;

	if (parser->chunk_ib_idx == -1)
		return 0;
	if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
		return 0;

558
	if (parser->const_ib.length_dw) {
559
		r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
560 561 562 563 564
		if (r) {
			return r;
		}
	}

565
	r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
566 567 568 569
	if (r) {
		return r;
	}

570 571 572
	if (parser->ring == R600_RING_TYPE_UVD_INDEX)
		radeon_uvd_note_usage(rdev);

573 574 575 576 577
	mutex_lock(&vm->mutex);
	r = radeon_bo_vm_update_pte(parser, vm);
	if (r) {
		goto out;
	}
578 579 580 581 582 583 584

	r = radeon_cs_sync_rings(parser);
	if (r) {
		if (r != -ERESTARTSYS)
			DRM_ERROR("Failed to sync rings: %i\n", r);
		goto out;
	}
585
	radeon_semaphore_sync_fence(parser->ib.semaphore, vm->fence);
586

587 588
	if ((rdev->family >= CHIP_TAHITI) &&
	    (parser->chunk_const_ib_idx != -1)) {
589
		r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib, true);
590
	} else {
591
		r = radeon_ib_schedule(rdev, &parser->ib, NULL, true);
592 593
	}

594
out:
595
	mutex_unlock(&vm->mutex);
596 597 598
	return r;
}

599 600 601 602 603 604 605 606 607 608
static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
{
	if (r == -EDEADLK) {
		r = radeon_gpu_reset(rdev);
		if (!r)
			r = -EAGAIN;
	}
	return r;
}

609 610 611 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
static int radeon_cs_ib_fill(struct radeon_device *rdev, struct radeon_cs_parser *parser)
{
	struct radeon_cs_chunk *ib_chunk;
	struct radeon_vm *vm = NULL;
	int r;

	if (parser->chunk_ib_idx == -1)
		return 0;

	if (parser->cs_flags & RADEON_CS_USE_VM) {
		struct radeon_fpriv *fpriv = parser->filp->driver_priv;
		vm = &fpriv->vm;

		if ((rdev->family >= CHIP_TAHITI) &&
		    (parser->chunk_const_ib_idx != -1)) {
			ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
			if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
				DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
				return -EINVAL;
			}
			r =  radeon_ib_get(rdev, parser->ring, &parser->const_ib,
					   vm, ib_chunk->length_dw * 4);
			if (r) {
				DRM_ERROR("Failed to get const ib !\n");
				return r;
			}
			parser->const_ib.is_const_ib = true;
			parser->const_ib.length_dw = ib_chunk->length_dw;
D
Daniel Vetter 已提交
637
			if (copy_from_user(parser->const_ib.ptr,
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
					       ib_chunk->user_ptr,
					       ib_chunk->length_dw * 4))
				return -EFAULT;
		}

		ib_chunk = &parser->chunks[parser->chunk_ib_idx];
		if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
			DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
			return -EINVAL;
		}
	}
	ib_chunk = &parser->chunks[parser->chunk_ib_idx];

	r =  radeon_ib_get(rdev, parser->ring, &parser->ib,
			   vm, ib_chunk->length_dw * 4);
	if (r) {
		DRM_ERROR("Failed to get ib !\n");
		return r;
	}
	parser->ib.length_dw = ib_chunk->length_dw;
	if (ib_chunk->kdata)
		memcpy(parser->ib.ptr, ib_chunk->kdata, ib_chunk->length_dw * 4);
D
Daniel Vetter 已提交
660
	else if (copy_from_user(parser->ib.ptr, ib_chunk->user_ptr, ib_chunk->length_dw * 4))
661 662 663 664
		return -EFAULT;
	return 0;
}

665 666 667 668 669 670
int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
{
	struct radeon_device *rdev = dev->dev_private;
	struct radeon_cs_parser parser;
	int r;

671
	down_read(&rdev->exclusive_lock);
672
	if (!rdev->accel_working) {
673
		up_read(&rdev->exclusive_lock);
674 675
		return -EBUSY;
	}
676 677 678 679 680 681 682
	if (rdev->in_reset) {
		up_read(&rdev->exclusive_lock);
		r = radeon_gpu_reset(rdev);
		if (!r)
			r = -EAGAIN;
		return r;
	}
683 684 685 686
	/* initialize parser */
	memset(&parser, 0, sizeof(struct radeon_cs_parser));
	parser.filp = filp;
	parser.rdev = rdev;
687
	parser.dev = rdev->dev;
688
	parser.family = rdev->family;
689 690 691
	r = radeon_cs_parser_init(&parser, data);
	if (r) {
		DRM_ERROR("Failed to initialize parser !\n");
692
		radeon_cs_parser_fini(&parser, r, false);
693
		up_read(&rdev->exclusive_lock);
694
		r = radeon_cs_handle_lockup(rdev, r);
695 696
		return r;
	}
697 698 699 700 701

	r = radeon_cs_ib_fill(rdev, &parser);
	if (!r) {
		r = radeon_cs_parser_relocs(&parser);
		if (r && r != -ERESTARTSYS)
702
			DRM_ERROR("Failed to parse relocation %d!\n", r);
703 704 705
	}

	if (r) {
706
		radeon_cs_parser_fini(&parser, r, false);
707
		up_read(&rdev->exclusive_lock);
708
		r = radeon_cs_handle_lockup(rdev, r);
709 710
		return r;
	}
711

712 713
	trace_radeon_cs(&parser);

714
	r = radeon_cs_ib_chunk(rdev, &parser);
715
	if (r) {
716
		goto out;
717
	}
718
	r = radeon_cs_ib_vm_chunk(rdev, &parser);
719
	if (r) {
720
		goto out;
721
	}
722
out:
723
	radeon_cs_parser_fini(&parser, r, true);
724
	up_read(&rdev->exclusive_lock);
725
	r = radeon_cs_handle_lockup(rdev, r);
726 727
	return r;
}
728

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
/**
 * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
 * @parser:	parser structure holding parsing context.
 * @pkt:	where to store packet information
 *
 * Assume that chunk_ib_index is properly set. Will return -EINVAL
 * if packet is bigger than remaining ib size. or if packets is unknown.
 **/
int radeon_cs_packet_parse(struct radeon_cs_parser *p,
			   struct radeon_cs_packet *pkt,
			   unsigned idx)
{
	struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
	struct radeon_device *rdev = p->rdev;
	uint32_t header;

	if (idx >= ib_chunk->length_dw) {
		DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
			  idx, ib_chunk->length_dw);
		return -EINVAL;
	}
	header = radeon_get_ib_value(p, idx);
	pkt->idx = idx;
	pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
	pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
	pkt->one_reg_wr = 0;
	switch (pkt->type) {
	case RADEON_PACKET_TYPE0:
		if (rdev->family < CHIP_R600) {
			pkt->reg = R100_CP_PACKET0_GET_REG(header);
			pkt->one_reg_wr =
				RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
		} else
			pkt->reg = R600_CP_PACKET0_GET_REG(header);
		break;
	case RADEON_PACKET_TYPE3:
		pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
		break;
	case RADEON_PACKET_TYPE2:
		pkt->count = -1;
		break;
	default:
		DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
		return -EINVAL;
	}
	if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
		DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
			  pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
		return -EINVAL;
	}
	return 0;
}
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

/**
 * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
 * @p:		structure holding the parser context.
 *
 * Check if the next packet is NOP relocation packet3.
 **/
bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
{
	struct radeon_cs_packet p3reloc;
	int r;

	r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
	if (r)
		return false;
	if (p3reloc.type != RADEON_PACKET_TYPE3)
		return false;
	if (p3reloc.opcode != RADEON_PACKET3_NOP)
		return false;
	return true;
}
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

/**
 * radeon_cs_dump_packet() - dump raw packet context
 * @p:		structure holding the parser context.
 * @pkt:	structure holding the packet.
 *
 * Used mostly for debugging and error reporting.
 **/
void radeon_cs_dump_packet(struct radeon_cs_parser *p,
			   struct radeon_cs_packet *pkt)
{
	volatile uint32_t *ib;
	unsigned i;
	unsigned idx;

	ib = p->ib.ptr;
	idx = pkt->idx;
	for (i = 0; i <= (pkt->count + 1); i++, idx++)
		DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/**
 * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
 * @parser:		parser structure holding parsing context.
 * @data:		pointer to relocation data
 * @offset_start:	starting offset
 * @offset_mask:	offset mask (to align start offset on)
 * @reloc:		reloc informations
 *
 * Check if next packet is relocation packet3, do bo validation and compute
 * GPU offset using the provided start.
 **/
int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
				struct radeon_cs_reloc **cs_reloc,
				int nomm)
{
	struct radeon_cs_chunk *relocs_chunk;
	struct radeon_cs_packet p3reloc;
	unsigned idx;
	int r;

	if (p->chunk_relocs_idx == -1) {
		DRM_ERROR("No relocation chunk !\n");
		return -EINVAL;
	}
	*cs_reloc = NULL;
	relocs_chunk = &p->chunks[p->chunk_relocs_idx];
	r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
	if (r)
		return r;
	p->idx += p3reloc.count + 2;
	if (p3reloc.type != RADEON_PACKET_TYPE3 ||
	    p3reloc.opcode != RADEON_PACKET3_NOP) {
		DRM_ERROR("No packet3 for relocation for packet at %d.\n",
			  p3reloc.idx);
		radeon_cs_dump_packet(p, &p3reloc);
		return -EINVAL;
	}
	idx = radeon_get_ib_value(p, p3reloc.idx + 1);
	if (idx >= relocs_chunk->length_dw) {
		DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
			  idx, relocs_chunk->length_dw);
		radeon_cs_dump_packet(p, &p3reloc);
		return -EINVAL;
	}
	/* FIXME: we assume reloc size is 4 dwords */
	if (nomm) {
		*cs_reloc = p->relocs;
870
		(*cs_reloc)->gpu_offset =
871
			(u64)relocs_chunk->kdata[idx + 3] << 32;
872
		(*cs_reloc)->gpu_offset |= relocs_chunk->kdata[idx + 0];
873 874 875 876
	} else
		*cs_reloc = p->relocs_ptr[(idx / 4)];
	return 0;
}