nouveau_gem.c 23.2 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
#include "nouveau_drm.h"
28
#include "nouveau_dma.h"
29
#include "nouveau_fence.h"
30
#include "nouveau_abi16.h"
31

32 33
#include "nouveau_ttm.h"
#include "nouveau_gem.h"
34 35 36 37

void
nouveau_gem_object_del(struct drm_gem_object *gem)
{
38
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
39
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
40
	struct ttm_buffer_object *bo = &nvbo->bo;
41 42 43 44 45 46
	struct device *dev = drm->dev->dev;
	int ret;

	ret = pm_runtime_get_sync(dev);
	if (WARN_ON(ret < 0 && ret != -EACCES))
		return;
47

D
Dave Airlie 已提交
48 49 50
	if (gem->import_attach)
		drm_prime_gem_destroy(gem, nvbo->bo.sg);

51
	drm_gem_object_release(gem);
52 53 54 55

	/* reset filp so nouveau_bo_del_ttm() can test for it */
	gem->filp = NULL;
	ttm_bo_unref(&bo);
56 57 58

	pm_runtime_mark_last_busy(dev);
	pm_runtime_put_autosuspend(dev);
59 60
}

61 62 63
int
nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
{
64
	struct nouveau_cli *cli = nouveau_cli(file_priv);
65
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
66
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
67
	struct nvkm_vma *vma;
68
	struct device *dev = drm->dev->dev;
69
	int ret;
70

71
	if (!cli->vm)
72 73
		return 0;

74
	ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
75 76 77
	if (ret)
		return ret;

78
	vma = nouveau_bo_vma_find(nvbo, cli->vm);
79 80 81 82 83 84 85
	if (!vma) {
		vma = kzalloc(sizeof(*vma), GFP_KERNEL);
		if (!vma) {
			ret = -ENOMEM;
			goto out;
		}

86 87 88 89
		ret = pm_runtime_get_sync(dev);
		if (ret < 0 && ret != -EACCES)
			goto out;

90
		ret = nouveau_bo_vma_add(nvbo, cli->vm, vma);
91
		if (ret)
92
			kfree(vma);
93 94 95

		pm_runtime_mark_last_busy(dev);
		pm_runtime_put_autosuspend(dev);
96 97 98 99 100 101 102
	} else {
		vma->refcount++;
	}

out:
	ttm_bo_unreserve(&nvbo->bo);
	return ret;
103 104
}

105 106 107
static void
nouveau_gem_object_delete(void *data)
{
108 109 110
	struct nvkm_vma *vma = data;
	nvkm_vm_unmap(vma);
	nvkm_vm_put(vma);
111 112 113 114
	kfree(vma);
}

static void
115
nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nvkm_vma *vma)
116 117
{
	const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
118 119
	struct reservation_object *resv = nvbo->bo.resv;
	struct reservation_object_list *fobj;
120
	struct fence *fence = NULL;
121

122 123
	fobj = reservation_object_get_list(resv);

124 125
	list_del(&vma->head);

126 127 128 129 130 131
	if (fobj && fobj->shared_count > 1)
		ttm_bo_wait(&nvbo->bo, true, false, false);
	else if (fobj && fobj->shared_count == 1)
		fence = rcu_dereference_protected(fobj->shared[0],
						reservation_object_held(resv));
	else
132
		fence = reservation_object_get_excl(nvbo->bo.resv);
133

134
	if (fence && mapped) {
135 136 137
		nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
	} else {
		if (mapped)
138 139
			nvkm_vm_unmap(vma);
		nvkm_vm_put(vma);
140 141 142 143
		kfree(vma);
	}
}

144 145 146
void
nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
{
147
	struct nouveau_cli *cli = nouveau_cli(file_priv);
148
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
149 150
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct device *dev = drm->dev->dev;
151
	struct nvkm_vma *vma;
152
	int ret;
153

154
	if (!cli->vm)
155
		return;
156

157
	ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
158 159 160
	if (ret)
		return;

161
	vma = nouveau_bo_vma_find(nvbo, cli->vm);
162
	if (vma) {
163 164 165 166 167 168 169 170
		if (--vma->refcount == 0) {
			ret = pm_runtime_get_sync(dev);
			if (!WARN_ON(ret < 0 && ret != -EACCES)) {
				nouveau_gem_object_unmap(nvbo, vma);
				pm_runtime_mark_last_busy(dev);
				pm_runtime_put_autosuspend(dev);
			}
		}
171 172
	}
	ttm_bo_unreserve(&nvbo->bo);
173 174
}

175
int
176 177 178
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)
179
{
180
	struct nouveau_drm *drm = nouveau_drm(dev);
181
	struct nouveau_bo *nvbo;
182
	u32 flags = 0;
183 184
	int ret;

185 186 187 188 189 190 191
	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;

192 193 194
	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
		flags |= TTM_PL_FLAG_UNCACHED;

195
	ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
196
			     tile_flags, NULL, NULL, pnvbo);
197 198 199 200
	if (ret)
		return ret;
	nvbo = *pnvbo;

201 202 203 204 205 206
	/* 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;
207
	if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
208 209
		nvbo->valid_domains &= domain;

210 211 212 213
	/* 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) {
214 215 216 217
		nouveau_bo_ref(NULL, pnvbo);
		return -ENOMEM;
	}

218
	nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
219 220 221 222
	return 0;
}

static int
223 224
nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
		 struct drm_nouveau_gem_info *rep)
225
{
226
	struct nouveau_cli *cli = nouveau_cli(file_priv);
227
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
228
	struct nvkm_vma *vma;
229 230 231 232 233 234

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

235
	rep->offset = nvbo->bo.offset;
236 237
	if (cli->vm) {
		vma = nouveau_bo_vma_find(nvbo, cli->vm);
238 239 240 241 242 243
		if (!vma)
			return -EINVAL;

		rep->offset = vma->offset;
	}

244
	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
245
	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
246 247 248 249 250 251 252 253 254
	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)
{
255
	struct nouveau_drm *drm = nouveau_drm(dev);
256
	struct nouveau_cli *cli = nouveau_cli(file_priv);
B
Ben Skeggs 已提交
257
	struct nvkm_fb *fb = nvxx_fb(&drm->device);
258 259 260 261
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

B
Ben Skeggs 已提交
262
	if (!fb->memtype_valid(fb, req->info.tile_flags)) {
B
Ben Skeggs 已提交
263
		NV_PRINTK(err, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
264
		return -EINVAL;
265
	}
266

267
	ret = nouveau_gem_new(dev, req->info.size, req->align,
268 269
			      req->info.domain, req->info.tile_mode,
			      req->info.tile_flags, &nvbo);
270 271 272
	if (ret)
		return ret;

273
	ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
274
	if (ret == 0) {
275
		ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
276 277 278 279
		if (ret)
			drm_gem_handle_delete(file_priv, req->info.handle);
	}

280
	/* drop reference from allocate - handle holds it now */
281
	drm_gem_object_unreference_unlocked(&nvbo->gem);
282 283 284 285 286 287 288
	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)
{
289
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
290
	struct ttm_buffer_object *bo = &nvbo->bo;
291
	uint32_t domains = valid_domains & nvbo->valid_domains &
292 293
		(write_domains ? write_domains : read_domains);
	uint32_t pref_flags = 0, valid_flags = 0;
294

295
	if (!domains)
296 297
		return -EINVAL;

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	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);
319 320 321 322 323

	return 0;
}

struct validate_op {
324
	struct list_head list;
325
	struct ww_acquire_ctx ticket;
326 327 328
};

static void
329 330
validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
			struct drm_nouveau_gem_pushbuf_bo *pbbo)
331 332
{
	struct nouveau_bo *nvbo;
333
	struct drm_nouveau_gem_pushbuf_bo *b;
334

335 336
	while (!list_empty(&op->list)) {
		nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
337
		b = &pbbo[nvbo->pbbo_index];
338

339
		if (likely(fence))
340
			nouveau_bo_fence(nvbo, fence, !!b->write_domains);
341

342 343 344 345 346
		if (unlikely(nvbo->validate_mapped)) {
			ttm_bo_kunmap(&nvbo->kmap);
			nvbo->validate_mapped = false;
		}

347 348
		list_del(&nvbo->entry);
		nvbo->reserved_by = NULL;
349
		ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket);
350
		drm_gem_object_unreference_unlocked(&nvbo->gem);
351 352 353
	}
}

354
static void
355 356
validate_fini(struct validate_op *op, struct nouveau_fence *fence,
	      struct drm_nouveau_gem_pushbuf_bo *pbbo)
357
{
358
	validate_fini_no_ticket(op, fence, pbbo);
359
	ww_acquire_fini(&op->ticket);
360 361 362 363 364 365 366
}

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)
{
367
	struct nouveau_cli *cli = nouveau_cli(file_priv);
368
	struct drm_device *dev = chan->drm->dev;
369 370
	int trycnt = 0;
	int ret, i;
371
	struct nouveau_bo *res_bo = NULL;
372 373 374
	LIST_HEAD(gart_list);
	LIST_HEAD(vram_list);
	LIST_HEAD(both_list);
375

376
	ww_acquire_init(&op->ticket, &reservation_ww_class);
377 378
retry:
	if (++trycnt > 100000) {
B
Ben Skeggs 已提交
379
		NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
380 381 382 383 384 385 386 387 388 389
		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) {
B
Ben Skeggs 已提交
390
			NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
391 392
			ret = -ENOENT;
			break;
393
		}
394
		nvbo = nouveau_gem_object(gem);
395 396 397 398 399
		if (nvbo == res_bo) {
			res_bo = NULL;
			drm_gem_object_unreference_unlocked(gem);
			continue;
		}
400 401

		if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
B
Ben Skeggs 已提交
402
			NV_PRINTK(err, cli, "multiple instances of buffer %d on "
403
				      "validation list\n", b->handle);
404
			drm_gem_object_unreference_unlocked(gem);
405 406
			ret = -EINVAL;
			break;
407 408
		}

409
		ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
410
		if (ret) {
411 412 413
			list_splice_tail_init(&vram_list, &op->list);
			list_splice_tail_init(&gart_list, &op->list);
			list_splice_tail_init(&both_list, &op->list);
414
			validate_fini_no_ticket(op, NULL, NULL);
415
			if (unlikely(ret == -EDEADLK)) {
416
				ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
417
							      &op->ticket);
418 419 420
				if (!ret)
					res_bo = nvbo;
			}
421 422
			if (unlikely(ret)) {
				if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
423
					NV_PRINTK(err, cli, "fail reserve\n");
424
				break;
425
			}
426 427
		}

428
		b->user_priv = (uint64_t)(unsigned long)nvbo;
429 430 431 432
		nvbo->reserved_by = file_priv;
		nvbo->pbbo_index = i;
		if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
		    (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
433
			list_add_tail(&nvbo->entry, &both_list);
434 435
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
436
			list_add_tail(&nvbo->entry, &vram_list);
437 438
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
439
			list_add_tail(&nvbo->entry, &gart_list);
440
		else {
B
Ben Skeggs 已提交
441
			NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
442
				 b->valid_domains);
443 444 445
			list_add_tail(&nvbo->entry, &both_list);
			ret = -EINVAL;
			break;
446
		}
447 448
		if (nvbo == res_bo)
			goto retry;
449 450
	}

451
	ww_acquire_done(&op->ticket);
452 453 454 455
	list_splice_tail(&vram_list, &op->list);
	list_splice_tail(&gart_list, &op->list);
	list_splice_tail(&both_list, &op->list);
	if (ret)
456
		validate_fini(op, NULL, NULL);
457 458
	return ret;

459 460 461
}

static int
462 463 464
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)
465
{
466
	struct nouveau_drm *drm = chan->drm;
467 468 469 470 471 472 473 474
	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];

475
		ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
476 477
					     b->write_domains,
					     b->valid_domains);
478
		if (unlikely(ret)) {
B
Ben Skeggs 已提交
479
			NV_PRINTK(err, cli, "fail set_domain\n");
480
			return ret;
481
		}
482

483
		ret = nouveau_bo_validate(nvbo, true, false);
484
		if (unlikely(ret)) {
485
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
486
				NV_PRINTK(err, cli, "fail ttm_validate\n");
487
			return ret;
488
		}
489

490
		ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
491
		if (unlikely(ret)) {
492
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
493
				NV_PRINTK(err, cli, "fail post-validate sync\n");
494 495 496
			return ret;
		}

497
		if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
			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++;

D
Daniel Vetter 已提交
513
			if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
514 515 516
					     &b->presumed, sizeof(b->presumed)))
				return -EFAULT;
		}
517 518 519 520 521 522 523 524 525 526 527 528
	}

	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)
{
529
	struct nouveau_cli *cli = nouveau_cli(file_priv);
530
	int ret;
531

532
	INIT_LIST_HEAD(&op->list);
533 534 535 536 537

	if (nr_buffers == 0)
		return 0;

	ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
538
	if (unlikely(ret)) {
539
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
540
			NV_PRINTK(err, cli, "validate_init\n");
541
		return ret;
542
	}
543

544
	ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
545
	if (unlikely(ret < 0)) {
546
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
547
			NV_PRINTK(err, cli, "validating bo list\n");
548
		validate_fini(op, NULL, NULL);
549 550
		return ret;
	}
551
	*apply_relocs = ret;
552 553 554
	return 0;
}

555 556 557
static inline void
u_free(void *addr)
{
558
	kvfree(addr);
559 560
}

561 562 563 564 565 566
static inline void *
u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
{
	void *mem;
	void __user *userptr = (void __force __user *)(uintptr_t)user;

567 568 569 570 571
	size *= nmemb;

	mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
	if (!mem)
		mem = vmalloc(size);
572 573 574
	if (!mem)
		return ERR_PTR(-ENOMEM);

D
Daniel Vetter 已提交
575
	if (copy_from_user(mem, userptr, size)) {
576
		u_free(mem);
577 578 579 580 581 582 583
		return ERR_PTR(-EFAULT);
	}

	return mem;
}

static int
584
nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
585 586
				struct drm_nouveau_gem_pushbuf *req,
				struct drm_nouveau_gem_pushbuf_bo *bo)
587 588
{
	struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
589 590
	int ret = 0;
	unsigned i;
591

592
	reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
593 594 595
	if (IS_ERR(reloc))
		return PTR_ERR(reloc);

596
	for (i = 0; i < req->nr_relocs; i++) {
597 598
		struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
		struct drm_nouveau_gem_pushbuf_bo *b;
599
		struct nouveau_bo *nvbo;
600 601
		uint32_t data;

602
		if (unlikely(r->bo_index > req->nr_buffers)) {
B
Ben Skeggs 已提交
603
			NV_PRINTK(err, cli, "reloc bo index invalid\n");
604 605 606 607 608
			ret = -EINVAL;
			break;
		}

		b = &bo[r->bo_index];
609
		if (b->presumed.valid)
610 611
			continue;

612
		if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
B
Ben Skeggs 已提交
613
			NV_PRINTK(err, cli, "reloc container bo index invalid\n");
614 615 616 617 618 619 620
			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)) {
B
Ben Skeggs 已提交
621
			NV_PRINTK(err, cli, "reloc outside of bo\n");
622 623 624 625 626 627 628 629
			ret = -EINVAL;
			break;
		}

		if (!nvbo->kmap.virtual) {
			ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
					  &nvbo->kmap);
			if (ret) {
B
Ben Skeggs 已提交
630
				NV_PRINTK(err, cli, "failed kmap for reloc\n");
631 632 633 634 635
				break;
			}
			nvbo->validate_mapped = true;
		}

636
		if (r->flags & NOUVEAU_GEM_RELOC_LOW)
637
			data = b->presumed.offset + r->data;
638 639
		else
		if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
640
			data = (b->presumed.offset + r->data) >> 32;
641 642 643 644
		else
			data = r->data;

		if (r->flags & NOUVEAU_GEM_RELOC_OR) {
645
			if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
646 647 648 649 650
				data |= r->tor;
			else
				data |= r->vor;
		}

651
		ret = ttm_bo_wait(&nvbo->bo, true, false, false);
652
		if (ret) {
B
Ben Skeggs 已提交
653
			NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
654 655 656 657
			break;
		}

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
658 659
	}

660
	u_free(reloc);
661 662 663 664 665 666 667
	return ret;
}

int
nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
			  struct drm_file *file_priv)
{
668
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
669
	struct nouveau_cli *cli = nouveau_cli(file_priv);
670 671
	struct nouveau_abi16_chan *temp;
	struct nouveau_drm *drm = nouveau_drm(dev);
672
	struct drm_nouveau_gem_pushbuf *req = data;
673 674
	struct drm_nouveau_gem_pushbuf_push *push;
	struct drm_nouveau_gem_pushbuf_bo *bo;
675
	struct nouveau_channel *chan = NULL;
676
	struct validate_op op;
677
	struct nouveau_fence *fence = NULL;
678
	int i, j, ret = 0, do_reloc = 0;
679

680 681
	if (unlikely(!abi16))
		return -ENOMEM;
682

683
	list_for_each_entry(temp, &abi16->channels, head) {
684
		if (temp->chan->user.handle == (NVDRM_CHAN | req->channel)) {
685 686 687 688
			chan = temp->chan;
			break;
		}
	}
689

690 691 692 693 694
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

	req->vram_available = drm->gem.vram_available;
	req->gart_available = drm->gem.gart_available;
695 696
	if (unlikely(req->nr_push == 0))
		goto out_next;
697

698
	if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
B
Ben Skeggs 已提交
699
		NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
700
			 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
701
		return nouveau_abi16_put(abi16, -EINVAL);
702 703
	}

704
	if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
B
Ben Skeggs 已提交
705
		NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
706
			 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
707
		return nouveau_abi16_put(abi16, -EINVAL);
708 709
	}

710
	if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
B
Ben Skeggs 已提交
711
		NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
712
			 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
713
		return nouveau_abi16_put(abi16, -EINVAL);
714 715
	}

716
	push = u_memcpya(req->push, req->nr_push, sizeof(*push));
717 718
	if (IS_ERR(push))
		return nouveau_abi16_put(abi16, PTR_ERR(push));
719

720
	bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
721
	if (IS_ERR(bo)) {
722
		u_free(push);
723
		return nouveau_abi16_put(abi16, PTR_ERR(bo));
724
	}
725

726
	/* Ensure all push buffers are on validate list */
727 728
	for (i = 0; i < req->nr_push; i++) {
		if (push[i].bo_index >= req->nr_buffers) {
B
Ben Skeggs 已提交
729
			NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
730
			ret = -EINVAL;
731
			goto out_prevalid;
732 733 734
		}
	}

735 736 737 738
	/* Validate buffer list */
	ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
					   req->nr_buffers, &op, &do_reloc);
	if (ret) {
739
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
740
			NV_PRINTK(err, cli, "validate: %d\n", ret);
741
		goto out_prevalid;
742 743 744 745
	}

	/* Apply any relocations that are required */
	if (do_reloc) {
746
		ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
747
		if (ret) {
B
Ben Skeggs 已提交
748
			NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
749 750 751 752
			goto out;
		}
	}

753
	if (chan->dma.ib_max) {
754
		ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
755
		if (ret) {
B
Ben Skeggs 已提交
756
			NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
757 758 759
			goto out;
		}

760 761 762 763 764 765 766
		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);
		}
767
	} else
768
	if (drm->device.info.chipset >= 0x25) {
769
		ret = RING_SPACE(chan, req->nr_push * 2);
770
		if (ret) {
B
Ben Skeggs 已提交
771
			NV_PRINTK(err, cli, "cal_space: %d\n", ret);
772 773
			goto out;
		}
774 775 776 777 778

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

779
			OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
780 781
			OUT_RING(chan, 0);
		}
782
	} else {
783
		ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
784
		if (ret) {
B
Ben Skeggs 已提交
785
			NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
786 787 788
			goto out;
		}

789 790 791 792 793
		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;

794
			cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
			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);
			}

813 814
			OUT_RING(chan, 0x20000000 |
				      (nvbo->bo.offset + push[i].offset));
815
			OUT_RING(chan, 0);
816 817 818
			for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
				OUT_RING(chan, 0);
		}
819 820
	}

821
	ret = nouveau_fence_new(chan, false, &fence);
822
	if (ret) {
B
Ben Skeggs 已提交
823
		NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
824 825 826 827 828
		WIND_RING(chan);
		goto out;
	}

out:
829
	validate_fini(&op, fence, bo);
830
	nouveau_fence_unref(&fence);
831 832

out_prevalid:
833 834
	u_free(bo);
	u_free(push);
835 836

out_next:
837 838 839 840
	if (chan->dma.ib_max) {
		req->suffix0 = 0x00000000;
		req->suffix1 = 0x00000000;
	} else
841
	if (drm->device.info.chipset >= 0x25) {
842 843 844 845
		req->suffix0 = 0x00020000;
		req->suffix1 = 0x00000000;
	} else {
		req->suffix0 = 0x20000000 |
846
			      (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
847 848 849
		req->suffix1 = 0x00000000;
	}

850
	return nouveau_abi16_put(abi16, ret);
851 852 853 854 855 856 857 858 859 860
}

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);
861
	bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
862
	int ret;
863 864 865

	gem = drm_gem_object_lookup(dev, file_priv, req->handle);
	if (!gem)
866
		return -ENOENT;
867 868
	nvbo = nouveau_gem_object(gem);

869 870 871 872
	if (no_wait)
		ret = reservation_object_test_signaled_rcu(nvbo->bo.resv, write) ? 0 : -EBUSY;
	else {
		long lret;
873

874 875 876 877 878 879 880
		lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true, 30 * HZ);
		if (!lret)
			ret = -EBUSY;
		else if (lret > 0)
			ret = 0;
		else
			ret = lret;
881
	}
882
	nouveau_bo_sync_for_cpu(nvbo);
883
	drm_gem_object_unreference_unlocked(gem);
884

885 886 887 888 889 890 891
	return ret;
}

int
nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
892 893 894 895 896 897 898 899 900 901 902
	struct drm_nouveau_gem_cpu_fini *req = data;
	struct drm_gem_object *gem;
	struct nouveau_bo *nvbo;

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

	nouveau_bo_sync_for_device(nvbo);
	drm_gem_object_unreference_unlocked(gem);
B
Ben Skeggs 已提交
903
	return 0;
904 905 906 907 908 909 910 911 912 913 914 915
}

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)
916
		return -ENOENT;
917

918
	ret = nouveau_gem_info(file_priv, gem, req);
919
	drm_gem_object_unreference_unlocked(gem);
920 921 922
	return ret;
}