nouveau_gem.c 23.6 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_drv.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
#include "nouveau_mem.h"
35
#include "nouveau_vmm.h"
36

37 38
#include <nvif/class.h>

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

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

D
Dave Airlie 已提交
52 53 54
	if (gem->import_attach)
		drm_prime_gem_destroy(gem, nvbo->bo.sg);

55
	drm_gem_object_release(gem);
56 57 58 59

	/* reset filp so nouveau_bo_del_ttm() can test for it */
	gem->filp = NULL;
	ttm_bo_unref(&bo);
60 61 62

	pm_runtime_mark_last_busy(dev);
	pm_runtime_put_autosuspend(dev);
63 64
}

65 66 67
int
nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
{
68
	struct nouveau_cli *cli = nouveau_cli(file_priv);
69
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
70 71
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct device *dev = drm->dev->dev;
72
	struct nouveau_vma *vma;
73
	int ret;
74

75
	if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
76 77
		return 0;

78
	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
79 80 81
	if (ret)
		return ret;

82 83 84
	ret = pm_runtime_get_sync(dev);
	if (ret < 0 && ret != -EACCES)
		goto out;
85

86 87 88
	ret = nouveau_vma_new(nvbo, &cli->vmm, &vma);
	pm_runtime_mark_last_busy(dev);
	pm_runtime_put_autosuspend(dev);
89 90 91
out:
	ttm_bo_unreserve(&nvbo->bo);
	return ret;
92 93
}

94 95 96 97 98
struct nouveau_gem_object_unmap {
	struct nouveau_cli_work work;
	struct nouveau_vma *vma;
};

99
static void
100
nouveau_gem_object_delete(struct nouveau_vma *vma)
101
{
102
	nouveau_vma_del(&vma);
103 104
}

105 106 107 108 109 110 111 112 113
static void
nouveau_gem_object_delete_work(struct nouveau_cli_work *w)
{
	struct nouveau_gem_object_unmap *work =
		container_of(w, typeof(*work), work);
	nouveau_gem_object_delete(work->vma);
	kfree(work);
}

114
static void
115
nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_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 nouveau_gem_object_unmap *work;
121
	struct dma_fence *fence = NULL;
122

123 124
	fobj = reservation_object_get_list(resv);

125
	list_del_init(&vma->head);
126

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

135 136 137 138 139 140 141 142 143 144 145 146 147 148
	if (!fence || !mapped) {
		nouveau_gem_object_delete(vma);
		return;
	}

	if (!(work = kmalloc(sizeof(*work), GFP_KERNEL))) {
		WARN_ON(dma_fence_wait_timeout(fence, false, 2 * HZ) <= 0);
		nouveau_gem_object_delete(vma);
		return;
	}

	work->work.func = nouveau_gem_object_delete_work;
	work->vma = vma;
	nouveau_cli_work_queue(vma->vmm->cli, fence, &work->work);
149 150
}

151 152 153
void
nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
{
154
	struct nouveau_cli *cli = nouveau_cli(file_priv);
155
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
156 157
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct device *dev = drm->dev->dev;
158
	struct nouveau_vma *vma;
159
	int ret;
160

161
	if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
162
		return;
163

164
	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
165 166 167
	if (ret)
		return;

168
	vma = nouveau_vma_find(nvbo, &cli->vmm);
169
	if (vma) {
170
		if (--vma->refs == 0) {
171 172 173 174 175 176 177
			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);
			}
		}
178 179
	}
	ttm_bo_unreserve(&nvbo->bo);
180 181
}

182
int
B
Ben Skeggs 已提交
183
nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
184 185
		uint32_t tile_mode, uint32_t tile_flags,
		struct nouveau_bo **pnvbo)
186
{
187
	struct nouveau_drm *drm = cli->drm;
188
	struct nouveau_bo *nvbo;
189
	u32 flags = 0;
190 191
	int ret;

192 193 194 195 196 197 198
	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;

199 200 201
	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
		flags |= TTM_PL_FLAG_UNCACHED;

202
	ret = nouveau_bo_new(cli, size, align, flags, tile_mode,
203
			     tile_flags, NULL, NULL, pnvbo);
204 205 206 207
	if (ret)
		return ret;
	nvbo = *pnvbo;

208 209 210 211 212 213
	/* 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;
214
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
215 216
		nvbo->valid_domains &= domain;

217 218
	/* Initialize the embedded gem-object. We return a single gem-reference
	 * to the caller, instead of a normal nouveau_bo ttm reference. */
219
	ret = drm_gem_object_init(drm->dev, &nvbo->gem, nvbo->bo.mem.size);
220
	if (ret) {
221 222 223 224
		nouveau_bo_ref(NULL, pnvbo);
		return -ENOMEM;
	}

225
	nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
226 227 228 229
	return 0;
}

static int
230 231
nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
		 struct drm_nouveau_gem_info *rep)
232
{
233
	struct nouveau_cli *cli = nouveau_cli(file_priv);
234
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
235
	struct nouveau_vma *vma;
236

237 238 239
	if (is_power_of_2(nvbo->valid_domains))
		rep->domain = nvbo->valid_domains;
	else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
240 241 242
		rep->domain = NOUVEAU_GEM_DOMAIN_GART;
	else
		rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
243
	rep->offset = nvbo->bo.offset;
244
	if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
245
		vma = nouveau_vma_find(nvbo, &cli->vmm);
246 247 248
		if (!vma)
			return -EINVAL;

249
		rep->offset = vma->addr;
250 251
	}

252
	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
253
	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
254 255 256 257 258 259 260 261 262
	rep->tile_mode = nvbo->mode;
	rep->tile_flags = nvbo->contig ? 0 : NOUVEAU_GEM_TILE_NONCONTIG;
	if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI)
		rep->tile_flags |= nvbo->kind << 8;
	else
	if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
		rep->tile_flags |= nvbo->kind << 8 | nvbo->comp << 16;
	else
		rep->tile_flags |= nvbo->zeta;
263 264 265 266 267 268 269
	return 0;
}

int
nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
{
270
	struct nouveau_cli *cli = nouveau_cli(file_priv);
271 272 273 274
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

275
	ret = nouveau_gem_new(cli, req->info.size, req->align,
276 277
			      req->info.domain, req->info.tile_mode,
			      req->info.tile_flags, &nvbo);
278 279 280
	if (ret)
		return ret;

281
	ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
282
	if (ret == 0) {
283
		ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
284 285 286 287
		if (ret)
			drm_gem_handle_delete(file_priv, req->info.handle);
	}

288
	/* drop reference from allocate - handle holds it now */
289
	drm_gem_object_unreference_unlocked(&nvbo->gem);
290 291 292 293 294 295 296
	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)
{
297
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
298
	struct ttm_buffer_object *bo = &nvbo->bo;
299
	uint32_t domains = valid_domains & nvbo->valid_domains &
300 301
		(write_domains ? write_domains : read_domains);
	uint32_t pref_flags = 0, valid_flags = 0;
302

303
	if (!domains)
304 305
		return -EINVAL;

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	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);
327 328 329 330 331

	return 0;
}

struct validate_op {
332
	struct list_head list;
333
	struct ww_acquire_ctx ticket;
334 335 336
};

static void
337 338
validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
			struct drm_nouveau_gem_pushbuf_bo *pbbo)
339 340
{
	struct nouveau_bo *nvbo;
341
	struct drm_nouveau_gem_pushbuf_bo *b;
342

343 344
	while (!list_empty(&op->list)) {
		nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
345
		b = &pbbo[nvbo->pbbo_index];
346

347
		if (likely(fence))
348
			nouveau_bo_fence(nvbo, fence, !!b->write_domains);
349

350 351 352 353 354
		if (unlikely(nvbo->validate_mapped)) {
			ttm_bo_kunmap(&nvbo->kmap);
			nvbo->validate_mapped = false;
		}

355 356
		list_del(&nvbo->entry);
		nvbo->reserved_by = NULL;
357
		ttm_bo_unreserve(&nvbo->bo);
358
		drm_gem_object_unreference_unlocked(&nvbo->gem);
359 360 361
	}
}

362
static void
363 364
validate_fini(struct validate_op *op, struct nouveau_fence *fence,
	      struct drm_nouveau_gem_pushbuf_bo *pbbo)
365
{
366
	validate_fini_no_ticket(op, fence, pbbo);
367
	ww_acquire_fini(&op->ticket);
368 369 370 371 372 373 374
}

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)
{
375
	struct nouveau_cli *cli = nouveau_cli(file_priv);
376
	int trycnt = 0;
377
	int ret = -EINVAL, i;
378
	struct nouveau_bo *res_bo = NULL;
379 380 381
	LIST_HEAD(gart_list);
	LIST_HEAD(vram_list);
	LIST_HEAD(both_list);
382

383
	ww_acquire_init(&op->ticket, &reservation_ww_class);
384 385
retry:
	if (++trycnt > 100000) {
B
Ben Skeggs 已提交
386
		NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
387 388 389 390 391 392 393 394
		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;

395
		gem = drm_gem_object_lookup(file_priv, b->handle);
396
		if (!gem) {
B
Ben Skeggs 已提交
397
			NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
398 399
			ret = -ENOENT;
			break;
400
		}
401
		nvbo = nouveau_gem_object(gem);
402 403 404 405 406
		if (nvbo == res_bo) {
			res_bo = NULL;
			drm_gem_object_unreference_unlocked(gem);
			continue;
		}
407 408

		if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
B
Ben Skeggs 已提交
409
			NV_PRINTK(err, cli, "multiple instances of buffer %d on "
410
				      "validation list\n", b->handle);
411
			drm_gem_object_unreference_unlocked(gem);
412 413
			ret = -EINVAL;
			break;
414 415
		}

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

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

458
	ww_acquire_done(&op->ticket);
459 460 461 462
	list_splice_tail(&vram_list, &op->list);
	list_splice_tail(&gart_list, &op->list);
	list_splice_tail(&both_list, &op->list);
	if (ret)
463
		validate_fini(op, NULL, NULL);
464 465
	return ret;

466 467 468
}

static int
469 470 471
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)
472
{
473
	struct nouveau_drm *drm = chan->drm;
474 475 476 477 478 479 480 481
	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];

482
		ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
483 484
					     b->write_domains,
					     b->valid_domains);
485
		if (unlikely(ret)) {
B
Ben Skeggs 已提交
486
			NV_PRINTK(err, cli, "fail set_domain\n");
487
			return ret;
488
		}
489

490
		ret = nouveau_bo_validate(nvbo, true, false);
491
		if (unlikely(ret)) {
492
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
493
				NV_PRINTK(err, cli, "fail ttm_validate\n");
494
			return ret;
495
		}
496

497
		ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
498
		if (unlikely(ret)) {
499
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
500
				NV_PRINTK(err, cli, "fail post-validate sync\n");
501 502 503
			return ret;
		}

504
		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
			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 已提交
520
			if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
521 522 523
					     &b->presumed, sizeof(b->presumed)))
				return -EFAULT;
		}
524 525 526 527 528 529 530 531 532 533 534 535
	}

	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)
{
536
	struct nouveau_cli *cli = nouveau_cli(file_priv);
537
	int ret;
538

539
	INIT_LIST_HEAD(&op->list);
540 541 542 543 544

	if (nr_buffers == 0)
		return 0;

	ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
545
	if (unlikely(ret)) {
546
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
547
			NV_PRINTK(err, cli, "validate_init\n");
548
		return ret;
549
	}
550

551
	ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
552
	if (unlikely(ret < 0)) {
553
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
554
			NV_PRINTK(err, cli, "validating bo list\n");
555
		validate_fini(op, NULL, NULL);
556 557
		return ret;
	}
558
	*apply_relocs = ret;
559 560 561
	return 0;
}

562 563 564
static inline void
u_free(void *addr)
{
565
	kvfree(addr);
566 567
}

568 569 570 571 572 573
static inline void *
u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
{
	void *mem;
	void __user *userptr = (void __force __user *)(uintptr_t)user;

574 575
	size *= nmemb;

576
	mem = kvmalloc(size, GFP_KERNEL);
577 578 579
	if (!mem)
		return ERR_PTR(-ENOMEM);

D
Daniel Vetter 已提交
580
	if (copy_from_user(mem, userptr, size)) {
581
		u_free(mem);
582 583 584 585 586 587 588
		return ERR_PTR(-EFAULT);
	}

	return mem;
}

static int
589
nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
590 591
				struct drm_nouveau_gem_pushbuf *req,
				struct drm_nouveau_gem_pushbuf_bo *bo)
592 593
{
	struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
594 595
	int ret = 0;
	unsigned i;
596

597
	reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
598 599 600
	if (IS_ERR(reloc))
		return PTR_ERR(reloc);

601
	for (i = 0; i < req->nr_relocs; i++) {
602 603
		struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
		struct drm_nouveau_gem_pushbuf_bo *b;
604
		struct nouveau_bo *nvbo;
605 606
		uint32_t data;

607
		if (unlikely(r->bo_index > req->nr_buffers)) {
B
Ben Skeggs 已提交
608
			NV_PRINTK(err, cli, "reloc bo index invalid\n");
609 610 611 612 613
			ret = -EINVAL;
			break;
		}

		b = &bo[r->bo_index];
614
		if (b->presumed.valid)
615 616
			continue;

617
		if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
B
Ben Skeggs 已提交
618
			NV_PRINTK(err, cli, "reloc container bo index invalid\n");
619 620 621 622 623 624 625
			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 已提交
626
			NV_PRINTK(err, cli, "reloc outside of bo\n");
627 628 629 630 631 632 633 634
			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 已提交
635
				NV_PRINTK(err, cli, "failed kmap for reloc\n");
636 637 638 639 640
				break;
			}
			nvbo->validate_mapped = true;
		}

641
		if (r->flags & NOUVEAU_GEM_RELOC_LOW)
642
			data = b->presumed.offset + r->data;
643 644
		else
		if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
645
			data = (b->presumed.offset + r->data) >> 32;
646 647 648 649
		else
			data = r->data;

		if (r->flags & NOUVEAU_GEM_RELOC_OR) {
650
			if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
651 652 653 654 655
				data |= r->tor;
			else
				data |= r->vor;
		}

656
		ret = ttm_bo_wait(&nvbo->bo, false, false);
657
		if (ret) {
B
Ben Skeggs 已提交
658
			NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
659 660 661 662
			break;
		}

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
663 664
	}

665
	u_free(reloc);
666 667 668 669 670 671 672
	return ret;
}

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

685 686
	if (unlikely(!abi16))
		return -ENOMEM;
687

688
	list_for_each_entry(temp, &abi16->channels, head) {
689
		if (temp->chan->chid == req->channel) {
690 691 692 693
			chan = temp->chan;
			break;
		}
	}
694

695 696 697 698 699
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

	req->vram_available = drm->gem.vram_available;
	req->gart_available = drm->gem.gart_available;
700 701
	if (unlikely(req->nr_push == 0))
		goto out_next;
702

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

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

715
	if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
B
Ben Skeggs 已提交
716
		NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
717
			 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
718
		return nouveau_abi16_put(abi16, -EINVAL);
719 720
	}

721
	push = u_memcpya(req->push, req->nr_push, sizeof(*push));
722 723
	if (IS_ERR(push))
		return nouveau_abi16_put(abi16, PTR_ERR(push));
724

725
	bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
726
	if (IS_ERR(bo)) {
727
		u_free(push);
728
		return nouveau_abi16_put(abi16, PTR_ERR(bo));
729
	}
730

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

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

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

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

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

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

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

794 795 796 797 798
		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;

799
			cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
			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);
			}

818 819
			OUT_RING(chan, 0x20000000 |
				      (nvbo->bo.offset + push[i].offset));
820
			OUT_RING(chan, 0);
821 822 823
			for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
				OUT_RING(chan, 0);
		}
824 825
	}

826
	ret = nouveau_fence_new(chan, false, &fence);
827
	if (ret) {
B
Ben Skeggs 已提交
828
		NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
829 830 831 832 833
		WIND_RING(chan);
		goto out;
	}

out:
834
	validate_fini(&op, fence, bo);
835
	nouveau_fence_unref(&fence);
836 837

out_prevalid:
838 839
	u_free(bo);
	u_free(push);
840 841

out_next:
842 843 844 845
	if (chan->dma.ib_max) {
		req->suffix0 = 0x00000000;
		req->suffix1 = 0x00000000;
	} else
846
	if (drm->client.device.info.chipset >= 0x25) {
847 848 849 850
		req->suffix0 = 0x00020000;
		req->suffix1 = 0x00000000;
	} else {
		req->suffix0 = 0x20000000 |
851
			      (chan->push.addr + ((chan->dma.cur + 2) << 2));
852 853 854
		req->suffix1 = 0x00000000;
	}

855
	return nouveau_abi16_put(abi16, ret);
856 857 858 859 860 861 862 863 864 865
}

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);
866
	bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
867
	long lret;
868
	int ret;
869

870
	gem = drm_gem_object_lookup(file_priv, req->handle);
871
	if (!gem)
872
		return -ENOENT;
873 874
	nvbo = nouveau_gem_object(gem);

875 876 877 878 879 880 881 882
	lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
						   no_wait ? 0 : 30 * HZ);
	if (!lret)
		ret = -EBUSY;
	else if (lret > 0)
		ret = 0;
	else
		ret = lret;
883

884
	nouveau_bo_sync_for_cpu(nvbo);
885
	drm_gem_object_unreference_unlocked(gem);
886

887 888 889 890 891 892 893
	return ret;
}

int
nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
894 895 896 897
	struct drm_nouveau_gem_cpu_fini *req = data;
	struct drm_gem_object *gem;
	struct nouveau_bo *nvbo;

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

	nouveau_bo_sync_for_device(nvbo);
	drm_gem_object_unreference_unlocked(gem);
B
Ben Skeggs 已提交
905
	return 0;
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;

916
	gem = drm_gem_object_lookup(file_priv, req->handle);
917
	if (!gem)
918
		return -ENOENT;
919

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