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
#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 38

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

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

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

52
	drm_gem_object_release(gem);
53 54 55 56

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

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

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

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

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

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

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

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

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

106 107
	fobj = reservation_object_get_list(resv);

108
	list_del_init(&vma->head);
109

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

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

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

134
	if (!cli->vm)
135
		return;
136

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

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

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

165 166 167 168 169 170 171
	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;

172 173 174
	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
		flags |= TTM_PL_FLAG_UNCACHED;

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

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

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

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

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

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

222
		rep->offset = vma->addr;
223 224
	}

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

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

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

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

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

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

283
	if (!domains)
284 285
		return -EINVAL;

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

	return 0;
}

struct validate_op {
312
	struct list_head list;
313
	struct ww_acquire_ctx ticket;
314 315 316
};

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

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

327
		if (likely(fence))
328
			nouveau_bo_fence(nvbo, fence, !!b->write_domains);
329

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

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

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

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

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

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

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

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

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

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

446 447 448
}

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

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

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

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

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

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

519
	INIT_LIST_HEAD(&op->list);
520 521 522 523 524

	if (nr_buffers == 0)
		return 0;

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

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

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

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

554 555
	size *= nmemb;

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

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

	return mem;
}

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

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

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

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

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

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

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

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

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

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
643 644
	}

645
	u_free(reloc);
646 647 648 649 650 651 652
	return ret;
}

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

665 666
	if (unlikely(!abi16))
		return -ENOMEM;
667

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

675 676 677 678 679
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

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

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

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

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

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

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

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

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

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

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

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

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

764
			OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
765 766
			OUT_RING(chan, 0);
		}
767
	} else {
768
		ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
769
		if (ret) {
B
Ben Skeggs 已提交
770
			NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
771 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;
			uint32_t cmd;

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

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

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

out:
814
	validate_fini(&op, fence, bo);
815
	nouveau_fence_unref(&fence);
816 817

out_prevalid:
818 819
	u_free(bo);
	u_free(push);
820 821

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

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

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

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

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

864
	nouveau_bo_sync_for_cpu(nvbo);
865
	drm_gem_object_unreference_unlocked(gem);
866

867 868 869 870 871 872 873
	return ret;
}

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

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

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

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;

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

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