nouveau_gem.c 23.1 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 (C) 2008 Ben Skeggs.
 * 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 THE COPYRIGHT OWNER(S) 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.
 *
 */

27 28 29
#include <subdev/fb.h>

#include "nouveau_drm.h"
30
#include "nouveau_dma.h"
31
#include "nouveau_fence.h"
32
#include "nouveau_abi16.h"
33

34 35
#include "nouveau_ttm.h"
#include "nouveau_gem.h"
36 37 38 39

void
nouveau_gem_object_del(struct drm_gem_object *gem)
{
40
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
41 42
	struct ttm_buffer_object *bo = &nvbo->bo;

D
Dave Airlie 已提交
43 44 45
	if (gem->import_attach)
		drm_prime_gem_destroy(gem, nvbo->bo.sg);

46
	drm_gem_object_release(gem);
47 48 49 50

	/* reset filp so nouveau_bo_del_ttm() can test for it */
	gem->filp = NULL;
	ttm_bo_unref(&bo);
51 52
}

53 54 55
int
nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
{
56
	struct nouveau_cli *cli = nouveau_cli(file_priv);
57 58 59
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
	struct nouveau_vma *vma;
	int ret;
60

61
	if (!cli->base.vm)
62 63
		return 0;

64 65 66 67
	ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
	if (ret)
		return ret;

68
	vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
69 70 71 72 73 74 75
	if (!vma) {
		vma = kzalloc(sizeof(*vma), GFP_KERNEL);
		if (!vma) {
			ret = -ENOMEM;
			goto out;
		}

76
		ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
77 78 79 80 81 82 83 84 85 86 87
		if (ret) {
			kfree(vma);
			goto out;
		}
	} else {
		vma->refcount++;
	}

out:
	ttm_bo_unreserve(&nvbo->bo);
	return ret;
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static void
nouveau_gem_object_delete(void *data)
{
	struct nouveau_vma *vma = data;
	nouveau_vm_unmap(vma);
	nouveau_vm_put(vma);
	kfree(vma);
}

static void
nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
{
	const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
	struct nouveau_fence *fence = NULL;

	list_del(&vma->head);

	if (mapped) {
		spin_lock(&nvbo->bo.bdev->fence_lock);
109
		fence = nouveau_fence_ref(nvbo->bo.sync_obj);
110 111 112 113 114 115 116 117 118 119 120 121 122 123
		spin_unlock(&nvbo->bo.bdev->fence_lock);
	}

	if (fence) {
		nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
	} else {
		if (mapped)
			nouveau_vm_unmap(vma);
		nouveau_vm_put(vma);
		kfree(vma);
	}
	nouveau_fence_unref(&fence);
}

124 125 126
void
nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
{
127
	struct nouveau_cli *cli = nouveau_cli(file_priv);
128 129 130
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
	struct nouveau_vma *vma;
	int ret;
131

132
	if (!cli->base.vm)
133
		return;
134 135 136 137 138

	ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
	if (ret)
		return;

139
	vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
140
	if (vma) {
141 142
		if (--vma->refcount == 0)
			nouveau_gem_object_unmap(nvbo, vma);
143 144
	}
	ttm_bo_unreserve(&nvbo->bo);
145 146
}

147
int
148 149 150
nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
		uint32_t tile_mode, uint32_t tile_flags,
		struct nouveau_bo **pnvbo)
151
{
152
	struct nouveau_drm *drm = nouveau_drm(dev);
153
	struct nouveau_bo *nvbo;
154
	u32 flags = 0;
155 156
	int ret;

157 158 159 160 161 162 163
	if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
		flags |= TTM_PL_FLAG_VRAM;
	if (domain & NOUVEAU_GEM_DOMAIN_GART)
		flags |= TTM_PL_FLAG_TT;
	if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
		flags |= TTM_PL_FLAG_SYSTEM;

164
	ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
D
Dave Airlie 已提交
165
			     tile_flags, NULL, pnvbo);
166 167 168 169
	if (ret)
		return ret;
	nvbo = *pnvbo;

170 171 172 173 174 175
	/* we restrict allowed domains on nv50+ to only the types
	 * that were requested at creation time.  not possibly on
	 * earlier chips without busting the ABI.
	 */
	nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
			      NOUVEAU_GEM_DOMAIN_GART;
176
	if (nv_device(drm->device)->card_type >= NV_50)
177 178
		nvbo->valid_domains &= domain;

179 180 181 182
	/* Initialize the embedded gem-object. We return a single gem-reference
	 * to the caller, instead of a normal nouveau_bo ttm reference. */
	ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
	if (ret) {
183 184 185 186
		nouveau_bo_ref(NULL, pnvbo);
		return -ENOMEM;
	}

187
	nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
188 189 190 191
	return 0;
}

static int
192 193
nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
		 struct drm_nouveau_gem_info *rep)
194
{
195
	struct nouveau_cli *cli = nouveau_cli(file_priv);
196
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
197
	struct nouveau_vma *vma;
198 199 200 201 202 203

	if (nvbo->bo.mem.mem_type == TTM_PL_TT)
		rep->domain = NOUVEAU_GEM_DOMAIN_GART;
	else
		rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;

204
	rep->offset = nvbo->bo.offset;
205 206
	if (cli->base.vm) {
		vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
207 208 209 210 211 212
		if (!vma)
			return -EINVAL;

		rep->offset = vma->offset;
	}

213
	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
214
	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
215 216 217 218 219 220 221 222 223
	rep->tile_mode = nvbo->tile_mode;
	rep->tile_flags = nvbo->tile_flags;
	return 0;
}

int
nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
{
224
	struct nouveau_drm *drm = nouveau_drm(dev);
225
	struct nouveau_cli *cli = nouveau_cli(file_priv);
226
	struct nouveau_fb *pfb = nouveau_fb(drm->device);
227 228 229 230
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

231
	drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
232

233
	if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
234
		NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
235
		return -EINVAL;
236
	}
237

238
	ret = nouveau_gem_new(dev, req->info.size, req->align,
239 240
			      req->info.domain, req->info.tile_mode,
			      req->info.tile_flags, &nvbo);
241 242 243
	if (ret)
		return ret;

244
	ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
245
	if (ret == 0) {
246
		ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
247 248 249 250
		if (ret)
			drm_gem_handle_delete(file_priv, req->info.handle);
	}

251
	/* drop reference from allocate - handle holds it now */
252
	drm_gem_object_unreference_unlocked(&nvbo->gem);
253 254 255 256 257 258 259
	return ret;
}

static int
nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
		       uint32_t write_domains, uint32_t valid_domains)
{
260
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
261
	struct ttm_buffer_object *bo = &nvbo->bo;
262
	uint32_t domains = valid_domains & nvbo->valid_domains &
263 264
		(write_domains ? write_domains : read_domains);
	uint32_t pref_flags = 0, valid_flags = 0;
265

266
	if (!domains)
267 268
		return -EINVAL;

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
		valid_flags |= TTM_PL_FLAG_VRAM;

	if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
		valid_flags |= TTM_PL_FLAG_TT;

	if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
	    bo->mem.mem_type == TTM_PL_VRAM)
		pref_flags |= TTM_PL_FLAG_VRAM;

	else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
		 bo->mem.mem_type == TTM_PL_TT)
		pref_flags |= TTM_PL_FLAG_TT;

	else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
		pref_flags |= TTM_PL_FLAG_VRAM;

	else
		pref_flags |= TTM_PL_FLAG_TT;

	nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
290 291 292 293 294 295 296 297

	return 0;
}

struct validate_op {
	struct list_head vram_list;
	struct list_head gart_list;
	struct list_head both_list;
298
	struct ww_acquire_ctx ticket;
299 300 301
};

static void
302 303
validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
		   struct ww_acquire_ctx *ticket)
304 305 306 307 308 309
{
	struct list_head *entry, *tmp;
	struct nouveau_bo *nvbo;

	list_for_each_safe(entry, tmp, list) {
		nvbo = list_entry(entry, struct nouveau_bo, entry);
310 311

		nouveau_bo_fence(nvbo, fence);
312

313 314 315 316 317
		if (unlikely(nvbo->validate_mapped)) {
			ttm_bo_kunmap(&nvbo->kmap);
			nvbo->validate_mapped = false;
		}

318 319
		list_del(&nvbo->entry);
		nvbo->reserved_by = NULL;
320
		ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
321
		drm_gem_object_unreference_unlocked(&nvbo->gem);
322 323 324 325
	}
}

static void
326
validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
327
{
328 329 330 331 332 333 334 335 336 337
	validate_fini_list(&op->vram_list, fence, &op->ticket);
	validate_fini_list(&op->gart_list, fence, &op->ticket);
	validate_fini_list(&op->both_list, fence, &op->ticket);
}

static void
validate_fini(struct validate_op *op, struct nouveau_fence *fence)
{
	validate_fini_no_ticket(op, fence);
	ww_acquire_fini(&op->ticket);
338 339 340 341 342 343 344
}

static int
validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
	      struct drm_nouveau_gem_pushbuf_bo *pbbo,
	      int nr_buffers, struct validate_op *op)
{
345
	struct nouveau_cli *cli = nouveau_cli(file_priv);
346
	struct drm_device *dev = chan->drm->dev;
347 348
	int trycnt = 0;
	int ret, i;
349
	struct nouveau_bo *res_bo = NULL;
350

351
	ww_acquire_init(&op->ticket, &reservation_ww_class);
352 353
retry:
	if (++trycnt > 100000) {
354
		NV_ERROR(cli, "%s failed and gave up.\n", __func__);
355 356 357 358 359 360 361 362 363 364
		return -EINVAL;
	}

	for (i = 0; i < nr_buffers; i++) {
		struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
		struct drm_gem_object *gem;
		struct nouveau_bo *nvbo;

		gem = drm_gem_object_lookup(dev, file_priv, b->handle);
		if (!gem) {
365
			NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
366
			ww_acquire_done(&op->ticket);
367
			validate_fini(op, NULL);
368
			return -ENOENT;
369
		}
370
		nvbo = nouveau_gem_object(gem);
371 372 373 374 375
		if (nvbo == res_bo) {
			res_bo = NULL;
			drm_gem_object_unreference_unlocked(gem);
			continue;
		}
376 377

		if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
378
			NV_ERROR(cli, "multiple instances of buffer %d on "
379
				      "validation list\n", b->handle);
380
			drm_gem_object_unreference_unlocked(gem);
381
			ww_acquire_done(&op->ticket);
382 383 384 385
			validate_fini(op, NULL);
			return -EINVAL;
		}

386
		ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
387
		if (ret) {
388
			validate_fini_no_ticket(op, NULL);
389
			if (unlikely(ret == -EDEADLK)) {
390
				ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
391
							      &op->ticket);
392 393 394
				if (!ret)
					res_bo = nvbo;
			}
395
			if (unlikely(ret)) {
396 397
				ww_acquire_done(&op->ticket);
				ww_acquire_fini(&op->ticket);
398
				drm_gem_object_unreference_unlocked(gem);
399
				if (ret != -ERESTARTSYS)
400
					NV_ERROR(cli, "fail reserve\n");
401
				return ret;
402
			}
403 404
		}

405
		b->user_priv = (uint64_t)(unsigned long)nvbo;
406 407 408 409 410 411 412 413 414 415 416 417
		nvbo->reserved_by = file_priv;
		nvbo->pbbo_index = i;
		if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
		    (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
			list_add_tail(&nvbo->entry, &op->both_list);
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
			list_add_tail(&nvbo->entry, &op->vram_list);
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
			list_add_tail(&nvbo->entry, &op->gart_list);
		else {
418
			NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
419
				 b->valid_domains);
420
			list_add_tail(&nvbo->entry, &op->both_list);
421
			ww_acquire_done(&op->ticket);
422 423 424
			validate_fini(op, NULL);
			return -EINVAL;
		}
425 426
		if (nvbo == res_bo)
			goto retry;
427 428
	}

429
	ww_acquire_done(&op->ticket);
430 431 432
	return 0;
}

433 434 435 436 437 438 439
static int
validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
{
	struct nouveau_fence *fence = NULL;
	int ret = 0;

	spin_lock(&nvbo->bo.bdev->fence_lock);
440
	fence = nouveau_fence_ref(nvbo->bo.sync_obj);
441 442 443 444 445 446 447 448 449 450
	spin_unlock(&nvbo->bo.bdev->fence_lock);

	if (fence) {
		ret = nouveau_fence_sync(fence, chan);
		nouveau_fence_unref(&fence);
	}

	return ret;
}

451
static int
452 453 454
validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
	      struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
	      uint64_t user_pbbo_ptr)
455
{
456
	struct nouveau_drm *drm = chan->drm;
457 458 459 460 461 462 463 464
	struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
				(void __force __user *)(uintptr_t)user_pbbo_ptr;
	struct nouveau_bo *nvbo;
	int ret, relocs = 0;

	list_for_each_entry(nvbo, list, entry) {
		struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];

465
		ret = validate_sync(chan, nvbo);
466
		if (unlikely(ret)) {
467
			NV_ERROR(cli, "fail pre-validate sync\n");
468
			return ret;
469 470
		}

471
		ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
472 473
					     b->write_domains,
					     b->valid_domains);
474
		if (unlikely(ret)) {
475
			NV_ERROR(cli, "fail set_domain\n");
476
			return ret;
477
		}
478

479
		ret = nouveau_bo_validate(nvbo, true, false);
480
		if (unlikely(ret)) {
481
			if (ret != -ERESTARTSYS)
482
				NV_ERROR(cli, "fail ttm_validate\n");
483
			return ret;
484
		}
485

486
		ret = validate_sync(chan, nvbo);
487
		if (unlikely(ret)) {
488
			NV_ERROR(cli, "fail post-validate sync\n");
489 490 491
			return ret;
		}

492
		if (nv_device(drm->device)->card_type < NV_50) {
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
			if (nvbo->bo.offset == b->presumed.offset &&
			    ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
			      b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
			     (nvbo->bo.mem.mem_type == TTM_PL_TT &&
			      b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
				continue;

			if (nvbo->bo.mem.mem_type == TTM_PL_TT)
				b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
			else
				b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
			b->presumed.offset = nvbo->bo.offset;
			b->presumed.valid = 0;
			relocs++;

			if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
					     &b->presumed, sizeof(b->presumed)))
				return -EFAULT;
		}
512 513 514 515 516 517 518 519 520 521 522 523
	}

	return relocs;
}

static int
nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
			     struct drm_file *file_priv,
			     struct drm_nouveau_gem_pushbuf_bo *pbbo,
			     uint64_t user_buffers, int nr_buffers,
			     struct validate_op *op, int *apply_relocs)
{
524
	struct nouveau_cli *cli = nouveau_cli(file_priv);
525 526 527 528 529 530 531 532 533 534
	int ret, relocs = 0;

	INIT_LIST_HEAD(&op->vram_list);
	INIT_LIST_HEAD(&op->gart_list);
	INIT_LIST_HEAD(&op->both_list);

	if (nr_buffers == 0)
		return 0;

	ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
535
	if (unlikely(ret)) {
536
		if (ret != -ERESTARTSYS)
537
			NV_ERROR(cli, "validate_init\n");
538
		return ret;
539
	}
540

541
	ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
542
	if (unlikely(ret < 0)) {
543
		if (ret != -ERESTARTSYS)
544
			NV_ERROR(cli, "validate vram_list\n");
545 546 547 548 549
		validate_fini(op, NULL);
		return ret;
	}
	relocs += ret;

550
	ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
551
	if (unlikely(ret < 0)) {
552
		if (ret != -ERESTARTSYS)
553
			NV_ERROR(cli, "validate gart_list\n");
554 555 556 557 558
		validate_fini(op, NULL);
		return ret;
	}
	relocs += ret;

559
	ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
560
	if (unlikely(ret < 0)) {
561
		if (ret != -ERESTARTSYS)
562
			NV_ERROR(cli, "validate both_list\n");
563 564 565 566 567 568 569 570 571
		validate_fini(op, NULL);
		return ret;
	}
	relocs += ret;

	*apply_relocs = relocs;
	return 0;
}

572 573 574 575 576 577 578 579 580
static inline void
u_free(void *addr)
{
	if (!is_vmalloc_addr(addr))
		kfree(addr);
	else
		vfree(addr);
}

581 582 583 584 585 586
static inline void *
u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
{
	void *mem;
	void __user *userptr = (void __force __user *)(uintptr_t)user;

587 588 589 590 591
	size *= nmemb;

	mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
	if (!mem)
		mem = vmalloc(size);
592 593 594
	if (!mem)
		return ERR_PTR(-ENOMEM);

595 596
	if (DRM_COPY_FROM_USER(mem, userptr, size)) {
		u_free(mem);
597 598 599 600 601 602 603
		return ERR_PTR(-EFAULT);
	}

	return mem;
}

static int
604
nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
605 606
				struct drm_nouveau_gem_pushbuf *req,
				struct drm_nouveau_gem_pushbuf_bo *bo)
607 608
{
	struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
609 610
	int ret = 0;
	unsigned i;
611

612
	reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
613 614 615
	if (IS_ERR(reloc))
		return PTR_ERR(reloc);

616
	for (i = 0; i < req->nr_relocs; i++) {
617 618
		struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
		struct drm_nouveau_gem_pushbuf_bo *b;
619
		struct nouveau_bo *nvbo;
620 621
		uint32_t data;

622
		if (unlikely(r->bo_index > req->nr_buffers)) {
623
			NV_ERROR(cli, "reloc bo index invalid\n");
624 625 626 627 628
			ret = -EINVAL;
			break;
		}

		b = &bo[r->bo_index];
629
		if (b->presumed.valid)
630 631
			continue;

632
		if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
633
			NV_ERROR(cli, "reloc container bo index invalid\n");
634 635 636 637 638 639 640
			ret = -EINVAL;
			break;
		}
		nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;

		if (unlikely(r->reloc_bo_offset + 4 >
			     nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
641
			NV_ERROR(cli, "reloc outside of bo\n");
642 643 644 645 646 647 648 649
			ret = -EINVAL;
			break;
		}

		if (!nvbo->kmap.virtual) {
			ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
					  &nvbo->kmap);
			if (ret) {
650
				NV_ERROR(cli, "failed kmap for reloc\n");
651 652 653 654 655
				break;
			}
			nvbo->validate_mapped = true;
		}

656
		if (r->flags & NOUVEAU_GEM_RELOC_LOW)
657
			data = b->presumed.offset + r->data;
658 659
		else
		if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
660
			data = (b->presumed.offset + r->data) >> 32;
661 662 663 664
		else
			data = r->data;

		if (r->flags & NOUVEAU_GEM_RELOC_OR) {
665
			if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
666 667 668 669 670
				data |= r->tor;
			else
				data |= r->vor;
		}

671
		spin_lock(&nvbo->bo.bdev->fence_lock);
672
		ret = ttm_bo_wait(&nvbo->bo, false, false, false);
673
		spin_unlock(&nvbo->bo.bdev->fence_lock);
674
		if (ret) {
675
			NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
676 677 678 679
			break;
		}

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
680 681
	}

682
	u_free(reloc);
683 684 685 686 687 688 689
	return ret;
}

int
nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
			  struct drm_file *file_priv)
{
690
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
691
	struct nouveau_cli *cli = nouveau_cli(file_priv);
692 693
	struct nouveau_abi16_chan *temp;
	struct nouveau_drm *drm = nouveau_drm(dev);
694
	struct drm_nouveau_gem_pushbuf *req = data;
695 696
	struct drm_nouveau_gem_pushbuf_push *push;
	struct drm_nouveau_gem_pushbuf_bo *bo;
697
	struct nouveau_channel *chan = NULL;
698
	struct validate_op op;
699
	struct nouveau_fence *fence = NULL;
700
	int i, j, ret = 0, do_reloc = 0;
701

702 703
	if (unlikely(!abi16))
		return -ENOMEM;
704

705 706 707 708 709 710
	list_for_each_entry(temp, &abi16->channels, head) {
		if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
			chan = temp->chan;
			break;
		}
	}
711

712 713 714 715 716
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

	req->vram_available = drm->gem.vram_available;
	req->gart_available = drm->gem.gart_available;
717 718
	if (unlikely(req->nr_push == 0))
		goto out_next;
719

720
	if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
721
		NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
722
			 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
723
		return nouveau_abi16_put(abi16, -EINVAL);
724 725
	}

726
	if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
727
		NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
728
			 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
729
		return nouveau_abi16_put(abi16, -EINVAL);
730 731
	}

732
	if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
733
		NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
734
			 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
735
		return nouveau_abi16_put(abi16, -EINVAL);
736 737
	}

738
	push = u_memcpya(req->push, req->nr_push, sizeof(*push));
739 740
	if (IS_ERR(push))
		return nouveau_abi16_put(abi16, PTR_ERR(push));
741

742
	bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
743
	if (IS_ERR(bo)) {
744
		u_free(push);
745
		return nouveau_abi16_put(abi16, PTR_ERR(bo));
746
	}
747

748
	/* Ensure all push buffers are on validate list */
749 750
	for (i = 0; i < req->nr_push; i++) {
		if (push[i].bo_index >= req->nr_buffers) {
751
			NV_ERROR(cli, "push %d buffer not in list\n", i);
752
			ret = -EINVAL;
753
			goto out_prevalid;
754 755 756
		}
	}

757 758 759 760
	/* Validate buffer list */
	ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
					   req->nr_buffers, &op, &do_reloc);
	if (ret) {
761
		if (ret != -ERESTARTSYS)
762
			NV_ERROR(cli, "validate: %d\n", ret);
763
		goto out_prevalid;
764 765 766 767
	}

	/* Apply any relocations that are required */
	if (do_reloc) {
768
		ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
769
		if (ret) {
770
			NV_ERROR(cli, "reloc apply: %d\n", ret);
771 772 773 774
			goto out;
		}
	}

775
	if (chan->dma.ib_max) {
776
		ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
777
		if (ret) {
778
			NV_ERROR(cli, "nv50cal_space: %d\n", ret);
779 780 781
			goto out;
		}

782 783 784 785 786 787 788
		for (i = 0; i < req->nr_push; i++) {
			struct nouveau_bo *nvbo = (void *)(unsigned long)
				bo[push[i].bo_index].user_priv;

			nv50_dma_push(chan, nvbo, push[i].offset,
				      push[i].length);
		}
789
	} else
790
	if (nv_device(drm->device)->chipset >= 0x25) {
791
		ret = RING_SPACE(chan, req->nr_push * 2);
792
		if (ret) {
793
			NV_ERROR(cli, "cal_space: %d\n", ret);
794 795
			goto out;
		}
796 797 798 799 800

		for (i = 0; i < req->nr_push; i++) {
			struct nouveau_bo *nvbo = (void *)(unsigned long)
				bo[push[i].bo_index].user_priv;

801
			OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
802 803
			OUT_RING(chan, 0);
		}
804
	} else {
805
		ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
806
		if (ret) {
807
			NV_ERROR(cli, "jmp_space: %d\n", ret);
808 809 810
			goto out;
		}

811 812 813 814 815
		for (i = 0; i < req->nr_push; i++) {
			struct nouveau_bo *nvbo = (void *)(unsigned long)
				bo[push[i].bo_index].user_priv;
			uint32_t cmd;

816
			cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
			cmd |= 0x20000000;
			if (unlikely(cmd != req->suffix0)) {
				if (!nvbo->kmap.virtual) {
					ret = ttm_bo_kmap(&nvbo->bo, 0,
							  nvbo->bo.mem.
							  num_pages,
							  &nvbo->kmap);
					if (ret) {
						WIND_RING(chan);
						goto out;
					}
					nvbo->validate_mapped = true;
				}

				nouveau_bo_wr32(nvbo, (push[i].offset +
						push[i].length - 8) / 4, cmd);
			}

835 836
			OUT_RING(chan, 0x20000000 |
				      (nvbo->bo.offset + push[i].offset));
837
			OUT_RING(chan, 0);
838 839 840
			for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
				OUT_RING(chan, 0);
		}
841 842
	}

843
	ret = nouveau_fence_new(chan, false, &fence);
844
	if (ret) {
845
		NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
846 847 848 849 850
		WIND_RING(chan);
		goto out;
	}

out:
851
	validate_fini(&op, fence);
852
	nouveau_fence_unref(&fence);
853 854

out_prevalid:
855 856
	u_free(bo);
	u_free(push);
857 858

out_next:
859 860 861 862
	if (chan->dma.ib_max) {
		req->suffix0 = 0x00000000;
		req->suffix1 = 0x00000000;
	} else
863
	if (nv_device(drm->device)->chipset >= 0x25) {
864 865 866 867
		req->suffix0 = 0x00020000;
		req->suffix1 = 0x00000000;
	} else {
		req->suffix0 = 0x20000000 |
868
			      (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
869 870 871
		req->suffix1 = 0x00000000;
	}

872
	return nouveau_abi16_put(abi16, ret);
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
}

static inline uint32_t
domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
{
	uint32_t flags = 0;

	if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
		flags |= TTM_PL_FLAG_VRAM;
	if (domain & NOUVEAU_GEM_DOMAIN_GART)
		flags |= TTM_PL_FLAG_TT;

	return flags;
}

int
nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
	struct drm_nouveau_gem_cpu_prep *req = data;
	struct drm_gem_object *gem;
	struct nouveau_bo *nvbo;
	bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
	int ret = -EINVAL;

	gem = drm_gem_object_lookup(dev, file_priv, req->handle);
	if (!gem)
900
		return -ENOENT;
901 902
	nvbo = nouveau_gem_object(gem);

B
Ben Skeggs 已提交
903 904 905
	spin_lock(&nvbo->bo.bdev->fence_lock);
	ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
	spin_unlock(&nvbo->bo.bdev->fence_lock);
906
	drm_gem_object_unreference_unlocked(gem);
907 908 909 910 911 912 913
	return ret;
}

int
nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
B
Ben Skeggs 已提交
914
	return 0;
915 916 917 918 919 920 921 922 923 924 925 926
}

int
nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct drm_nouveau_gem_info *req = data;
	struct drm_gem_object *gem;
	int ret;

	gem = drm_gem_object_lookup(dev, file_priv, req->handle);
	if (!gem)
927
		return -ENOENT;
928

929
	ret = nouveau_gem_info(file_priv, gem, req);
930
	drm_gem_object_unreference_unlocked(gem);
931 932 933
	return ret;
}