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_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_vmm.h"
35

36 37
#include <nvif/class.h>

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

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

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

54
	drm_gem_object_release(gem);
55 56 57 58

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

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

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

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

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

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

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

93 94 95
static void
nouveau_gem_object_delete(void *data)
{
96 97
	struct nouveau_vma *vma = data;
	nouveau_vma_del(&vma);
98 99 100
}

static void
101
nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
102 103
{
	const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
104 105
	struct reservation_object *resv = nvbo->bo.resv;
	struct reservation_object_list *fobj;
106
	struct dma_fence *fence = NULL;
107

108 109
	fobj = reservation_object_get_list(resv);

110
	list_del_init(&vma->head);
111

112
	if (fobj && fobj->shared_count > 1)
113
		ttm_bo_wait(&nvbo->bo, false, false);
114 115 116 117
	else if (fobj && fobj->shared_count == 1)
		fence = rcu_dereference_protected(fobj->shared[0],
						reservation_object_held(resv));
	else
118
		fence = reservation_object_get_excl(nvbo->bo.resv);
119

120
	if (fence && mapped)
121
		nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
122 123
	else
		nouveau_vma_del(&vma);
124 125
}

126 127 128
void
nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
{
129
	struct nouveau_cli *cli = nouveau_cli(file_priv);
130
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
131 132
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct device *dev = drm->dev->dev;
133
	struct nouveau_vma *vma;
134
	int ret;
135

136
	if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
137
		return;
138

139
	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
140 141 142
	if (ret)
		return;

143
	vma = nouveau_vma_find(nvbo, &cli->vmm);
144
	if (vma) {
145
		if (--vma->refs == 0) {
146 147 148 149 150 151 152
			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);
			}
		}
153 154
	}
	ttm_bo_unreserve(&nvbo->bo);
155 156
}

157
int
B
Ben Skeggs 已提交
158
nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
159 160
		uint32_t tile_mode, uint32_t tile_flags,
		struct nouveau_bo **pnvbo)
161
{
162
	struct nouveau_drm *drm = cli->drm;
163
	struct nouveau_bo *nvbo;
164
	u32 flags = 0;
165 166
	int ret;

167 168 169 170 171 172 173
	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;

174 175 176
	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
		flags |= TTM_PL_FLAG_UNCACHED;

177
	ret = nouveau_bo_new(cli, size, align, flags, tile_mode,
178
			     tile_flags, NULL, NULL, pnvbo);
179 180 181 182
	if (ret)
		return ret;
	nvbo = *pnvbo;

183 184 185 186 187 188
	/* 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;
189
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
190 191
		nvbo->valid_domains &= domain;

192 193
	/* Initialize the embedded gem-object. We return a single gem-reference
	 * to the caller, instead of a normal nouveau_bo ttm reference. */
194
	ret = drm_gem_object_init(drm->dev, &nvbo->gem, nvbo->bo.mem.size);
195
	if (ret) {
196 197 198 199
		nouveau_bo_ref(NULL, pnvbo);
		return -ENOMEM;
	}

200
	nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
201 202 203 204
	return 0;
}

static int
205 206
nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
		 struct drm_nouveau_gem_info *rep)
207
{
208
	struct nouveau_cli *cli = nouveau_cli(file_priv);
209
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
210
	struct nouveau_vma *vma;
211

212 213 214
	if (is_power_of_2(nvbo->valid_domains))
		rep->domain = nvbo->valid_domains;
	else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
215 216 217
		rep->domain = NOUVEAU_GEM_DOMAIN_GART;
	else
		rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
218
	rep->offset = nvbo->bo.offset;
219
	if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
220
		vma = nouveau_vma_find(nvbo, &cli->vmm);
221 222 223
		if (!vma)
			return -EINVAL;

224
		rep->offset = vma->addr;
225 226
	}

227
	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
228
	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
229 230 231 232 233 234 235 236 237
	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;
238 239 240 241 242 243 244
	return 0;
}

int
nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
{
245
	struct nouveau_drm *drm = nouveau_drm(dev);
246
	struct nouveau_cli *cli = nouveau_cli(file_priv);
247
	struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
248 249 250 251
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

252
	if (!nvkm_fb_memtype_valid(fb, req->info.tile_flags)) {
B
Ben Skeggs 已提交
253
		NV_PRINTK(err, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
254
		return -EINVAL;
255
	}
256

257
	ret = nouveau_gem_new(cli, req->info.size, req->align,
258 259
			      req->info.domain, req->info.tile_mode,
			      req->info.tile_flags, &nvbo);
260 261 262
	if (ret)
		return ret;

263
	ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
264
	if (ret == 0) {
265
		ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
266 267 268 269
		if (ret)
			drm_gem_handle_delete(file_priv, req->info.handle);
	}

270
	/* drop reference from allocate - handle holds it now */
271
	drm_gem_object_unreference_unlocked(&nvbo->gem);
272 273 274 275 276 277 278
	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)
{
279
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
280
	struct ttm_buffer_object *bo = &nvbo->bo;
281
	uint32_t domains = valid_domains & nvbo->valid_domains &
282 283
		(write_domains ? write_domains : read_domains);
	uint32_t pref_flags = 0, valid_flags = 0;
284

285
	if (!domains)
286 287
		return -EINVAL;

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	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);
309 310 311 312 313

	return 0;
}

struct validate_op {
314
	struct list_head list;
315
	struct ww_acquire_ctx ticket;
316 317 318
};

static void
319 320
validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
			struct drm_nouveau_gem_pushbuf_bo *pbbo)
321 322
{
	struct nouveau_bo *nvbo;
323
	struct drm_nouveau_gem_pushbuf_bo *b;
324

325 326
	while (!list_empty(&op->list)) {
		nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
327
		b = &pbbo[nvbo->pbbo_index];
328

329
		if (likely(fence))
330
			nouveau_bo_fence(nvbo, fence, !!b->write_domains);
331

332 333 334 335 336
		if (unlikely(nvbo->validate_mapped)) {
			ttm_bo_kunmap(&nvbo->kmap);
			nvbo->validate_mapped = false;
		}

337 338
		list_del(&nvbo->entry);
		nvbo->reserved_by = NULL;
339
		ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket);
340
		drm_gem_object_unreference_unlocked(&nvbo->gem);
341 342 343
	}
}

344
static void
345 346
validate_fini(struct validate_op *op, struct nouveau_fence *fence,
	      struct drm_nouveau_gem_pushbuf_bo *pbbo)
347
{
348
	validate_fini_no_ticket(op, fence, pbbo);
349
	ww_acquire_fini(&op->ticket);
350 351 352 353 354 355 356
}

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)
{
357
	struct nouveau_cli *cli = nouveau_cli(file_priv);
358
	int trycnt = 0;
359
	int ret = -EINVAL, i;
360
	struct nouveau_bo *res_bo = NULL;
361 362 363
	LIST_HEAD(gart_list);
	LIST_HEAD(vram_list);
	LIST_HEAD(both_list);
364

365
	ww_acquire_init(&op->ticket, &reservation_ww_class);
366 367
retry:
	if (++trycnt > 100000) {
B
Ben Skeggs 已提交
368
		NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
369 370 371 372 373 374 375 376
		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;

377
		gem = drm_gem_object_lookup(file_priv, b->handle);
378
		if (!gem) {
B
Ben Skeggs 已提交
379
			NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
380 381
			ret = -ENOENT;
			break;
382
		}
383
		nvbo = nouveau_gem_object(gem);
384 385 386 387 388
		if (nvbo == res_bo) {
			res_bo = NULL;
			drm_gem_object_unreference_unlocked(gem);
			continue;
		}
389 390

		if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
B
Ben Skeggs 已提交
391
			NV_PRINTK(err, cli, "multiple instances of buffer %d on "
392
				      "validation list\n", b->handle);
393
			drm_gem_object_unreference_unlocked(gem);
394 395
			ret = -EINVAL;
			break;
396 397
		}

398
		ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
399
		if (ret) {
400 401 402
			list_splice_tail_init(&vram_list, &op->list);
			list_splice_tail_init(&gart_list, &op->list);
			list_splice_tail_init(&both_list, &op->list);
403
			validate_fini_no_ticket(op, NULL, NULL);
404
			if (unlikely(ret == -EDEADLK)) {
405
				ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
406
							      &op->ticket);
407 408 409
				if (!ret)
					res_bo = nvbo;
			}
410 411
			if (unlikely(ret)) {
				if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
412
					NV_PRINTK(err, cli, "fail reserve\n");
413
				break;
414
			}
415 416
		}

417
		b->user_priv = (uint64_t)(unsigned long)nvbo;
418 419 420 421
		nvbo->reserved_by = file_priv;
		nvbo->pbbo_index = i;
		if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
		    (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
422
			list_add_tail(&nvbo->entry, &both_list);
423 424
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
425
			list_add_tail(&nvbo->entry, &vram_list);
426 427
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
428
			list_add_tail(&nvbo->entry, &gart_list);
429
		else {
B
Ben Skeggs 已提交
430
			NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
431
				 b->valid_domains);
432 433 434
			list_add_tail(&nvbo->entry, &both_list);
			ret = -EINVAL;
			break;
435
		}
436 437
		if (nvbo == res_bo)
			goto retry;
438 439
	}

440
	ww_acquire_done(&op->ticket);
441 442 443 444
	list_splice_tail(&vram_list, &op->list);
	list_splice_tail(&gart_list, &op->list);
	list_splice_tail(&both_list, &op->list);
	if (ret)
445
		validate_fini(op, NULL, NULL);
446 447
	return ret;

448 449 450
}

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

464
		ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
465 466
					     b->write_domains,
					     b->valid_domains);
467
		if (unlikely(ret)) {
B
Ben Skeggs 已提交
468
			NV_PRINTK(err, cli, "fail set_domain\n");
469
			return ret;
470
		}
471

472
		ret = nouveau_bo_validate(nvbo, true, false);
473
		if (unlikely(ret)) {
474
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
475
				NV_PRINTK(err, cli, "fail ttm_validate\n");
476
			return ret;
477
		}
478

479
		ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
480
		if (unlikely(ret)) {
481
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
482
				NV_PRINTK(err, cli, "fail post-validate sync\n");
483 484 485
			return ret;
		}

486
		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
			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 已提交
502
			if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
503 504 505
					     &b->presumed, sizeof(b->presumed)))
				return -EFAULT;
		}
506 507 508 509 510 511 512 513 514 515 516 517
	}

	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)
{
518
	struct nouveau_cli *cli = nouveau_cli(file_priv);
519
	int ret;
520

521
	INIT_LIST_HEAD(&op->list);
522 523 524 525 526

	if (nr_buffers == 0)
		return 0;

	ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
527
	if (unlikely(ret)) {
528
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
529
			NV_PRINTK(err, cli, "validate_init\n");
530
		return ret;
531
	}
532

533
	ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
534
	if (unlikely(ret < 0)) {
535
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
536
			NV_PRINTK(err, cli, "validating bo list\n");
537
		validate_fini(op, NULL, NULL);
538 539
		return ret;
	}
540
	*apply_relocs = ret;
541 542 543
	return 0;
}

544 545 546
static inline void
u_free(void *addr)
{
547
	kvfree(addr);
548 549
}

550 551 552 553 554 555
static inline void *
u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
{
	void *mem;
	void __user *userptr = (void __force __user *)(uintptr_t)user;

556 557
	size *= nmemb;

558
	mem = kvmalloc(size, GFP_KERNEL);
559 560 561
	if (!mem)
		return ERR_PTR(-ENOMEM);

D
Daniel Vetter 已提交
562
	if (copy_from_user(mem, userptr, size)) {
563
		u_free(mem);
564 565 566 567 568 569 570
		return ERR_PTR(-EFAULT);
	}

	return mem;
}

static int
571
nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
572 573
				struct drm_nouveau_gem_pushbuf *req,
				struct drm_nouveau_gem_pushbuf_bo *bo)
574 575
{
	struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
576 577
	int ret = 0;
	unsigned i;
578

579
	reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
580 581 582
	if (IS_ERR(reloc))
		return PTR_ERR(reloc);

583
	for (i = 0; i < req->nr_relocs; i++) {
584 585
		struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
		struct drm_nouveau_gem_pushbuf_bo *b;
586
		struct nouveau_bo *nvbo;
587 588
		uint32_t data;

589
		if (unlikely(r->bo_index > req->nr_buffers)) {
B
Ben Skeggs 已提交
590
			NV_PRINTK(err, cli, "reloc bo index invalid\n");
591 592 593 594 595
			ret = -EINVAL;
			break;
		}

		b = &bo[r->bo_index];
596
		if (b->presumed.valid)
597 598
			continue;

599
		if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
B
Ben Skeggs 已提交
600
			NV_PRINTK(err, cli, "reloc container bo index invalid\n");
601 602 603 604 605 606 607
			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 已提交
608
			NV_PRINTK(err, cli, "reloc outside of bo\n");
609 610 611 612 613 614 615 616
			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 已提交
617
				NV_PRINTK(err, cli, "failed kmap for reloc\n");
618 619 620 621 622
				break;
			}
			nvbo->validate_mapped = true;
		}

623
		if (r->flags & NOUVEAU_GEM_RELOC_LOW)
624
			data = b->presumed.offset + r->data;
625 626
		else
		if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
627
			data = (b->presumed.offset + r->data) >> 32;
628 629 630 631
		else
			data = r->data;

		if (r->flags & NOUVEAU_GEM_RELOC_OR) {
632
			if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
633 634 635 636 637
				data |= r->tor;
			else
				data |= r->vor;
		}

638
		ret = ttm_bo_wait(&nvbo->bo, false, false);
639
		if (ret) {
B
Ben Skeggs 已提交
640
			NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
641 642 643 644
			break;
		}

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
645 646
	}

647
	u_free(reloc);
648 649 650 651 652 653 654
	return ret;
}

int
nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
			  struct drm_file *file_priv)
{
655
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
656
	struct nouveau_cli *cli = nouveau_cli(file_priv);
657 658
	struct nouveau_abi16_chan *temp;
	struct nouveau_drm *drm = nouveau_drm(dev);
659
	struct drm_nouveau_gem_pushbuf *req = data;
660 661
	struct drm_nouveau_gem_pushbuf_push *push;
	struct drm_nouveau_gem_pushbuf_bo *bo;
662
	struct nouveau_channel *chan = NULL;
663
	struct validate_op op;
664
	struct nouveau_fence *fence = NULL;
665
	int i, j, ret = 0, do_reloc = 0;
666

667 668
	if (unlikely(!abi16))
		return -ENOMEM;
669

670
	list_for_each_entry(temp, &abi16->channels, head) {
671
		if (temp->chan->chid == req->channel) {
672 673 674 675
			chan = temp->chan;
			break;
		}
	}
676

677 678 679 680 681
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

	req->vram_available = drm->gem.vram_available;
	req->gart_available = drm->gem.gart_available;
682 683
	if (unlikely(req->nr_push == 0))
		goto out_next;
684

685
	if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
B
Ben Skeggs 已提交
686
		NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
687
			 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
688
		return nouveau_abi16_put(abi16, -EINVAL);
689 690
	}

691
	if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
B
Ben Skeggs 已提交
692
		NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
693
			 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
694
		return nouveau_abi16_put(abi16, -EINVAL);
695 696
	}

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

703
	push = u_memcpya(req->push, req->nr_push, sizeof(*push));
704 705
	if (IS_ERR(push))
		return nouveau_abi16_put(abi16, PTR_ERR(push));
706

707
	bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
708
	if (IS_ERR(bo)) {
709
		u_free(push);
710
		return nouveau_abi16_put(abi16, PTR_ERR(bo));
711
	}
712

713
	/* Ensure all push buffers are on validate list */
714 715
	for (i = 0; i < req->nr_push; i++) {
		if (push[i].bo_index >= req->nr_buffers) {
B
Ben Skeggs 已提交
716
			NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
717
			ret = -EINVAL;
718
			goto out_prevalid;
719 720 721
		}
	}

722 723 724 725
	/* Validate buffer list */
	ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
					   req->nr_buffers, &op, &do_reloc);
	if (ret) {
726
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
727
			NV_PRINTK(err, cli, "validate: %d\n", ret);
728
		goto out_prevalid;
729 730 731 732
	}

	/* Apply any relocations that are required */
	if (do_reloc) {
733
		ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
734
		if (ret) {
B
Ben Skeggs 已提交
735
			NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
736 737 738 739
			goto out;
		}
	}

740
	if (chan->dma.ib_max) {
741
		ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
742
		if (ret) {
B
Ben Skeggs 已提交
743
			NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
744 745 746
			goto out;
		}

747 748 749 750 751 752 753
		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);
		}
754
	} else
755
	if (drm->client.device.info.chipset >= 0x25) {
756
		ret = RING_SPACE(chan, req->nr_push * 2);
757
		if (ret) {
B
Ben Skeggs 已提交
758
			NV_PRINTK(err, cli, "cal_space: %d\n", ret);
759 760
			goto out;
		}
761 762 763 764 765

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

766
			OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
767 768
			OUT_RING(chan, 0);
		}
769
	} else {
770
		ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
771
		if (ret) {
B
Ben Skeggs 已提交
772
			NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
773 774 775
			goto out;
		}

776 777 778 779 780
		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;

781
			cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
			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);
			}

800 801
			OUT_RING(chan, 0x20000000 |
				      (nvbo->bo.offset + push[i].offset));
802
			OUT_RING(chan, 0);
803 804 805
			for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
				OUT_RING(chan, 0);
		}
806 807
	}

808
	ret = nouveau_fence_new(chan, false, &fence);
809
	if (ret) {
B
Ben Skeggs 已提交
810
		NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
811 812 813 814 815
		WIND_RING(chan);
		goto out;
	}

out:
816
	validate_fini(&op, fence, bo);
817
	nouveau_fence_unref(&fence);
818 819

out_prevalid:
820 821
	u_free(bo);
	u_free(push);
822 823

out_next:
824 825 826 827
	if (chan->dma.ib_max) {
		req->suffix0 = 0x00000000;
		req->suffix1 = 0x00000000;
	} else
828
	if (drm->client.device.info.chipset >= 0x25) {
829 830 831 832
		req->suffix0 = 0x00020000;
		req->suffix1 = 0x00000000;
	} else {
		req->suffix0 = 0x20000000 |
833
			      (chan->push.addr + ((chan->dma.cur + 2) << 2));
834 835 836
		req->suffix1 = 0x00000000;
	}

837
	return nouveau_abi16_put(abi16, ret);
838 839 840 841 842 843 844 845 846 847
}

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);
848
	bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
849
	long lret;
850
	int ret;
851

852
	gem = drm_gem_object_lookup(file_priv, req->handle);
853
	if (!gem)
854
		return -ENOENT;
855 856
	nvbo = nouveau_gem_object(gem);

857 858 859 860 861 862 863 864
	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;
865

866
	nouveau_bo_sync_for_cpu(nvbo);
867
	drm_gem_object_unreference_unlocked(gem);
868

869 870 871 872 873 874 875
	return ret;
}

int
nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
876 877 878 879
	struct drm_nouveau_gem_cpu_fini *req = data;
	struct drm_gem_object *gem;
	struct nouveau_bo *nvbo;

880
	gem = drm_gem_object_lookup(file_priv, req->handle);
881 882 883 884 885 886
	if (!gem)
		return -ENOENT;
	nvbo = nouveau_gem_object(gem);

	nouveau_bo_sync_for_device(nvbo);
	drm_gem_object_unreference_unlocked(gem);
B
Ben Skeggs 已提交
887
	return 0;
888 889 890 891 892 893 894 895 896 897
}

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;

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

902
	ret = nouveau_gem_info(file_priv, gem, req);
903
	drm_gem_object_unreference_unlocked(gem);
904 905 906
	return ret;
}