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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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);
		if (nvbo->bo.sync_obj)
			fence = nouveau_fence_ref(nvbo->bo.sync_obj);
		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);
}

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

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

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

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

148
int
149 150 151
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)
152
{
153
	struct nouveau_drm *drm = nouveau_drm(dev);
154
	struct nouveau_bo *nvbo;
155
	u32 flags = 0;
156 157
	int ret;

158 159 160 161 162 163 164
	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;

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

171 172 173 174 175 176
	/* 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;
177
	if (nv_device(drm->device)->card_type >= NV_50)
178 179
		nvbo->valid_domains &= domain;

180 181 182 183
	/* 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) {
184 185 186 187
		nouveau_bo_ref(NULL, pnvbo);
		return -ENOMEM;
	}

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

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

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

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

		rep->offset = vma->offset;
	}

214
	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
215
	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
216 217 218 219 220 221 222 223 224
	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)
{
225
	struct nouveau_drm *drm = nouveau_drm(dev);
226
	struct nouveau_cli *cli = nouveau_cli(file_priv);
227
	struct nouveau_fb *pfb = nouveau_fb(drm->device);
228 229 230 231
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

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

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

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

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

252
	/* drop reference from allocate - handle holds it now */
253
	drm_gem_object_unreference_unlocked(&nvbo->gem);
254 255 256 257 258 259 260
	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)
{
261
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
262
	struct ttm_buffer_object *bo = &nvbo->bo;
263
	uint32_t domains = valid_domains & nvbo->valid_domains &
264 265
		(write_domains ? write_domains : read_domains);
	uint32_t pref_flags = 0, valid_flags = 0;
266

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

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	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);
291 292 293 294 295 296 297 298

	return 0;
}

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

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

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

		nouveau_bo_fence(nvbo, fence);
313

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

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

static void
327
validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
328
{
329 330 331 332 333 334 335 336 337 338
	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);
339 340 341 342 343 344 345
}

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)
{
346
	struct nouveau_cli *cli = nouveau_cli(file_priv);
347
	struct drm_device *dev = chan->drm->dev;
348 349
	int trycnt = 0;
	int ret, i;
350
	struct nouveau_bo *res_bo = NULL;
351

352
	ww_acquire_init(&op->ticket, &reservation_ww_class);
353 354
retry:
	if (++trycnt > 100000) {
355
		NV_ERROR(cli, "%s failed and gave up.\n", __func__);
356 357 358 359 360 361 362 363 364 365
		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) {
366
			NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
367
			ww_acquire_done(&op->ticket);
368
			validate_fini(op, NULL);
369
			return -ENOENT;
370
		}
371
		nvbo = nouveau_gem_object(gem);
372 373 374 375 376
		if (nvbo == res_bo) {
			res_bo = NULL;
			drm_gem_object_unreference_unlocked(gem);
			continue;
		}
377 378

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

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

406
		b->user_priv = (uint64_t)(unsigned long)nvbo;
407 408 409 410 411 412 413 414 415 416 417 418
		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 {
419
			NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
420
				 b->valid_domains);
421
			list_add_tail(&nvbo->entry, &op->both_list);
422
			ww_acquire_done(&op->ticket);
423 424 425
			validate_fini(op, NULL);
			return -EINVAL;
		}
426 427
		if (nvbo == res_bo)
			goto retry;
428 429
	}

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

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
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);
	if (nvbo->bo.sync_obj)
		fence = nouveau_fence_ref(nvbo->bo.sync_obj);
	spin_unlock(&nvbo->bo.bdev->fence_lock);

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

	return ret;
}

453
static int
454 455 456
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)
457
{
458
	struct nouveau_drm *drm = chan->drm;
459 460 461 462 463 464 465 466
	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];

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

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

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

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

494
		if (nv_device(drm->device)->card_type < NV_50) {
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
			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;
		}
514 515 516 517 518 519 520 521 522 523 524 525
	}

	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)
{
526
	struct nouveau_cli *cli = nouveau_cli(file_priv);
527 528 529 530 531 532 533 534 535 536
	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);
537
	if (unlikely(ret)) {
538
		if (ret != -ERESTARTSYS)
539
			NV_ERROR(cli, "validate_init\n");
540
		return ret;
541
	}
542

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

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

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

	*apply_relocs = relocs;
	return 0;
}

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

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

589 590 591 592 593
	size *= nmemb;

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

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

	return mem;
}

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

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

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

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

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

634
		if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
635
			NV_ERROR(cli, "reloc container bo index invalid\n");
636 637 638 639 640 641 642
			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)) {
643
			NV_ERROR(cli, "reloc outside of bo\n");
644 645 646 647 648 649 650 651
			ret = -EINVAL;
			break;
		}

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

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

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

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

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
682 683
	}

684
	u_free(reloc);
685 686 687 688 689 690 691
	return ret;
}

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

704 705
	if (unlikely(!abi16))
		return -ENOMEM;
706

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

714 715 716 717 718
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

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

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

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

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

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

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

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

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

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

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

784 785 786 787 788 789 790
		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);
		}
791
	} else
792
	if (nv_device(drm->device)->chipset >= 0x25) {
793
		ret = RING_SPACE(chan, req->nr_push * 2);
794
		if (ret) {
795
			NV_ERROR(cli, "cal_space: %d\n", ret);
796 797
			goto out;
		}
798 799 800 801 802

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

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

813 814 815 816 817
		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;

818
			cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
			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);
			}

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

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

out:
853
	validate_fini(&op, fence);
854
	nouveau_fence_unref(&fence);
855 856

out_prevalid:
857 858
	u_free(bo);
	u_free(push);
859 860

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

874
	return nouveau_abi16_put(abi16, ret);
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 900 901
}

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)
902
		return -ENOENT;
903 904
	nvbo = nouveau_gem_object(gem);

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

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

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)
929
		return -ENOENT;
930

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