nouveau_gem.c 24.4 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 44 45 46 47 48 49
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	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
	ttm_bo_put(&nvbo->bo);
55 56 57

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

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

71
	if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50)
72 73
		return 0;

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

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

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

92 93 94 95 96
struct nouveau_gem_object_unmap {
	struct nouveau_cli_work work;
	struct nouveau_vma *vma;
};

97
static void
98
nouveau_gem_object_delete(struct nouveau_vma *vma)
99
{
100
	nouveau_fence_unref(&vma->fence);
101
	nouveau_vma_del(&vma);
102 103
}

104 105 106 107 108 109 110 111 112
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);
}

113
static void
114
nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
115
{
116
	struct dma_fence *fence = vma->fence ? &vma->fence->base : NULL;
117
	struct nouveau_gem_object_unmap *work;
118

119
	list_del_init(&vma->head);
120

121
	if (!fence) {
122 123 124 125 126 127 128 129 130 131 132 133 134
		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);
135 136
}

137 138 139
void
nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
{
140
	struct nouveau_cli *cli = nouveau_cli(file_priv);
141
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
142 143
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct device *dev = drm->dev->dev;
144
	struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : & cli->vmm;
145
	struct nouveau_vma *vma;
146
	int ret;
147

148
	if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50)
149
		return;
150

151
	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
152 153 154
	if (ret)
		return;

155
	vma = nouveau_vma_find(nvbo, vmm);
156
	if (vma) {
157
		if (--vma->refs == 0) {
158 159 160 161 162
			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);
			}
163
			pm_runtime_put_autosuspend(dev);
164
		}
165 166
	}
	ttm_bo_unreserve(&nvbo->bo);
167 168
}

169
int
B
Ben Skeggs 已提交
170
nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
171 172
		uint32_t tile_mode, uint32_t tile_flags,
		struct nouveau_bo **pnvbo)
173
{
174
	struct nouveau_drm *drm = cli->drm;
175
	struct nouveau_bo *nvbo;
176
	u32 flags = 0;
177 178
	int ret;

179 180 181 182 183 184 185
	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;

186 187 188
	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
		flags |= TTM_PL_FLAG_UNCACHED;

189 190
	nvbo = nouveau_bo_alloc(cli, &size, &align, flags, tile_mode,
				tile_flags);
191 192 193 194 195 196 197 198
	if (IS_ERR(nvbo))
		return PTR_ERR(nvbo);

	/* 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(drm->dev, &nvbo->bo.base, size);
	if (ret) {
		nouveau_bo_ref(NULL, &nvbo);
199
		return ret;
200 201 202 203 204 205 206
	}

	ret = nouveau_bo_init(nvbo, size, align, flags, NULL, NULL);
	if (ret) {
		nouveau_bo_ref(NULL, &nvbo);
		return ret;
	}
207

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
	nvbo->bo.persistent_swap_storage = nvbo->bo.base.filp;
218
	*pnvbo = nvbo;
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 nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
229
	struct nouveau_vma *vma;
230

231 232 233
	if (is_power_of_2(nvbo->valid_domains))
		rep->domain = nvbo->valid_domains;
	else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
234 235 236
		rep->domain = NOUVEAU_GEM_DOMAIN_GART;
	else
		rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
237
	rep->offset = nvbo->offset;
238 239
	if (vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
		vma = nouveau_vma_find(nvbo, vmm);
240 241 242
		if (!vma)
			return -EINVAL;

243
		rep->offset = vma->addr;
244 245
	}

246
	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
G
Gerd Hoffmann 已提交
247
	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.base.vma_node);
248 249 250 251 252 253 254 255 256
	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;
257 258 259 260 261 262 263
	return 0;
}

int
nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
{
264
	struct nouveau_cli *cli = nouveau_cli(file_priv);
265 266 267 268
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

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

275 276
	ret = drm_gem_handle_create(file_priv, &nvbo->bo.base,
				    &req->info.handle);
277
	if (ret == 0) {
278
		ret = nouveau_gem_info(file_priv, &nvbo->bo.base, &req->info);
279 280 281 282
		if (ret)
			drm_gem_handle_delete(file_priv, req->info.handle);
	}

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

298
	if (!domains)
299 300
		return -EINVAL;

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

	return 0;
}

struct validate_op {
327
	struct list_head list;
328
	struct ww_acquire_ctx ticket;
329 330 331
};

static void
332 333
validate_fini_no_ticket(struct validate_op *op, struct nouveau_channel *chan,
			struct nouveau_fence *fence,
334
			struct drm_nouveau_gem_pushbuf_bo *pbbo)
335 336
{
	struct nouveau_bo *nvbo;
337
	struct drm_nouveau_gem_pushbuf_bo *b;
338

339 340
	while (!list_empty(&op->list)) {
		nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
341
		b = &pbbo[nvbo->pbbo_index];
342

343
		if (likely(fence)) {
344
			nouveau_bo_fence(nvbo, fence, !!b->write_domains);
345

346 347 348
			if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
				struct nouveau_vma *vma =
					(void *)(unsigned long)b->user_priv;
349 350 351 352 353 354
				nouveau_fence_unref(&vma->fence);
				dma_fence_get(&fence->base);
				vma->fence = fence;
			}
		}

355 356 357 358 359
		if (unlikely(nvbo->validate_mapped)) {
			ttm_bo_kunmap(&nvbo->kmap);
			nvbo->validate_mapped = false;
		}

360 361
		list_del(&nvbo->entry);
		nvbo->reserved_by = NULL;
362
		ttm_bo_unreserve(&nvbo->bo);
363
		drm_gem_object_put(&nvbo->bo.base);
364 365 366
	}
}

367
static void
368 369
validate_fini(struct validate_op *op, struct nouveau_channel *chan,
	      struct nouveau_fence *fence,
370
	      struct drm_nouveau_gem_pushbuf_bo *pbbo)
371
{
372
	validate_fini_no_ticket(op, chan, fence, pbbo);
373
	ww_acquire_fini(&op->ticket);
374 375 376 377 378 379 380
}

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)
{
381
	struct nouveau_cli *cli = nouveau_cli(file_priv);
382
	int trycnt = 0;
383
	int ret = -EINVAL, i;
384
	struct nouveau_bo *res_bo = NULL;
385 386 387
	LIST_HEAD(gart_list);
	LIST_HEAD(vram_list);
	LIST_HEAD(both_list);
388

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

401
		gem = drm_gem_object_lookup(file_priv, b->handle);
402
		if (!gem) {
B
Ben Skeggs 已提交
403
			NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
404 405
			ret = -ENOENT;
			break;
406
		}
407
		nvbo = nouveau_gem_object(gem);
408 409
		if (nvbo == res_bo) {
			res_bo = NULL;
410
			drm_gem_object_put(gem);
411 412
			continue;
		}
413 414

		if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
B
Ben Skeggs 已提交
415
			NV_PRINTK(err, cli, "multiple instances of buffer %d on "
416
				      "validation list\n", b->handle);
417
			drm_gem_object_put(gem);
418 419
			ret = -EINVAL;
			break;
420 421
		}

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

441 442
		if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
			struct nouveau_vmm *vmm = chan->vmm;
443 444 445 446 447 448 449 450 451 452 453 454
			struct nouveau_vma *vma = nouveau_vma_find(nvbo, vmm);
			if (!vma) {
				NV_PRINTK(err, cli, "vma not found!\n");
				ret = -EINVAL;
				break;
			}

			b->user_priv = (uint64_t)(unsigned long)vma;
		} else {
			b->user_priv = (uint64_t)(unsigned long)nvbo;
		}

455 456 457 458
		nvbo->reserved_by = file_priv;
		nvbo->pbbo_index = i;
		if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
		    (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
459
			list_add_tail(&nvbo->entry, &both_list);
460 461
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
462
			list_add_tail(&nvbo->entry, &vram_list);
463 464
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
465
			list_add_tail(&nvbo->entry, &gart_list);
466
		else {
B
Ben Skeggs 已提交
467
			NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
468
				 b->valid_domains);
469 470 471
			list_add_tail(&nvbo->entry, &both_list);
			ret = -EINVAL;
			break;
472
		}
473 474
		if (nvbo == res_bo)
			goto retry;
475 476
	}

477
	ww_acquire_done(&op->ticket);
478 479 480 481
	list_splice_tail(&vram_list, &op->list);
	list_splice_tail(&gart_list, &op->list);
	list_splice_tail(&both_list, &op->list);
	if (ret)
482
		validate_fini(op, chan, NULL, NULL);
483 484
	return ret;

485 486 487
}

static int
488
validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
489
	      struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo)
490
{
491
	struct nouveau_drm *drm = chan->drm;
492 493 494 495 496 497
	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];

498
		ret = nouveau_gem_set_domain(&nvbo->bo.base, b->read_domains,
499 500
					     b->write_domains,
					     b->valid_domains);
501
		if (unlikely(ret)) {
B
Ben Skeggs 已提交
502
			NV_PRINTK(err, cli, "fail set_domain\n");
503
			return ret;
504
		}
505

506
		ret = nouveau_bo_validate(nvbo, true, false);
507
		if (unlikely(ret)) {
508
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
509
				NV_PRINTK(err, cli, "fail ttm_validate\n");
510
			return ret;
511
		}
512

513
		ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
514
		if (unlikely(ret)) {
515
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
516
				NV_PRINTK(err, cli, "fail post-validate sync\n");
517 518 519
			return ret;
		}

520
		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
521
			if (nvbo->offset == b->presumed.offset &&
522 523 524 525 526 527 528 529 530 531
			    ((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;
532
			b->presumed.offset = nvbo->offset;
533 534 535
			b->presumed.valid = 0;
			relocs++;
		}
536 537 538 539 540 541 542 543 544
	}

	return relocs;
}

static int
nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
			     struct drm_file *file_priv,
			     struct drm_nouveau_gem_pushbuf_bo *pbbo,
545 546
			     int nr_buffers,
			     struct validate_op *op, bool *apply_relocs)
547
{
548
	struct nouveau_cli *cli = nouveau_cli(file_priv);
549
	int ret;
550

551
	INIT_LIST_HEAD(&op->list);
552 553 554 555 556

	if (nr_buffers == 0)
		return 0;

	ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
557
	if (unlikely(ret)) {
558
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
559
			NV_PRINTK(err, cli, "validate_init\n");
560
		return ret;
561
	}
562

563
	ret = validate_list(chan, cli, &op->list, pbbo);
564
	if (unlikely(ret < 0)) {
565
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
566
			NV_PRINTK(err, cli, "validating bo list\n");
567
		validate_fini(op, chan, NULL, NULL);
568 569
		return ret;
	}
570
	*apply_relocs = ret;
571 572 573
	return 0;
}

574 575 576
static inline void
u_free(void *addr)
{
577
	kvfree(addr);
578 579
}

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

586 587
	size *= nmemb;

588
	mem = kvmalloc(size, GFP_KERNEL);
589 590 591
	if (!mem)
		return ERR_PTR(-ENOMEM);

D
Daniel Vetter 已提交
592
	if (copy_from_user(mem, userptr, size)) {
593
		u_free(mem);
594 595 596 597 598 599 600
		return ERR_PTR(-EFAULT);
	}

	return mem;
}

static int
601
nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
602
				struct drm_nouveau_gem_pushbuf *req,
603
				struct drm_nouveau_gem_pushbuf_reloc *reloc,
604
				struct drm_nouveau_gem_pushbuf_bo *bo)
605
{
606 607
	int ret = 0;
	unsigned i;
608

609
	for (i = 0; i < req->nr_relocs; i++) {
610 611
		struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
		struct drm_nouveau_gem_pushbuf_bo *b;
612
		struct nouveau_bo *nvbo;
613 614
		uint32_t data;

615
		if (unlikely(r->bo_index >= req->nr_buffers)) {
B
Ben Skeggs 已提交
616
			NV_PRINTK(err, cli, "reloc bo index invalid\n");
617 618 619 620 621
			ret = -EINVAL;
			break;
		}

		b = &bo[r->bo_index];
622
		if (b->presumed.valid)
623 624
			continue;

625
		if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
B
Ben Skeggs 已提交
626
			NV_PRINTK(err, cli, "reloc container bo index invalid\n");
627 628 629 630 631 632 633
			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 已提交
634
			NV_PRINTK(err, cli, "reloc outside of bo\n");
635 636 637 638 639 640 641 642
			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 已提交
643
				NV_PRINTK(err, cli, "failed kmap for reloc\n");
644 645 646 647 648
				break;
			}
			nvbo->validate_mapped = true;
		}

649
		if (r->flags & NOUVEAU_GEM_RELOC_LOW)
650
			data = b->presumed.offset + r->data;
651 652
		else
		if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
653
			data = (b->presumed.offset + r->data) >> 32;
654 655 656 657
		else
			data = r->data;

		if (r->flags & NOUVEAU_GEM_RELOC_OR) {
658
			if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
659 660 661 662 663
				data |= r->tor;
			else
				data |= r->vor;
		}

664
		ret = ttm_bo_wait(&nvbo->bo, false, false);
665
		if (ret) {
B
Ben Skeggs 已提交
666
			NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
667 668 669 670
			break;
		}

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
671 672
	}

673
	u_free(reloc);
674 675 676 677 678 679 680
	return ret;
}

int
nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
			  struct drm_file *file_priv)
{
681
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
682
	struct nouveau_cli *cli = nouveau_cli(file_priv);
683 684
	struct nouveau_abi16_chan *temp;
	struct nouveau_drm *drm = nouveau_drm(dev);
685
	struct drm_nouveau_gem_pushbuf *req = data;
686
	struct drm_nouveau_gem_pushbuf_push *push;
687
	struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
688
	struct drm_nouveau_gem_pushbuf_bo *bo;
689
	struct nouveau_channel *chan = NULL;
690
	struct validate_op op;
691
	struct nouveau_fence *fence = NULL;
692
	int i, j, ret = 0;
693
	bool do_reloc = false, sync = false;
694

695 696
	if (unlikely(!abi16))
		return -ENOMEM;
697

698
	list_for_each_entry(temp, &abi16->channels, head) {
699
		if (temp->chan->chid == req->channel) {
700 701 702 703
			chan = temp->chan;
			break;
		}
	}
704

705 706
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);
707 708
	if (unlikely(atomic_read(&chan->killed)))
		return nouveau_abi16_put(abi16, -ENODEV);
709

710 711
	sync = req->vram_available & NOUVEAU_GEM_PUSHBUF_SYNC;

712 713
	req->vram_available = drm->gem.vram_available;
	req->gart_available = drm->gem.gart_available;
714 715
	if (unlikely(req->nr_push == 0))
		goto out_next;
716

717
	if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
B
Ben Skeggs 已提交
718
		NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
719
			 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
720
		return nouveau_abi16_put(abi16, -EINVAL);
721 722
	}

723
	if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
B
Ben Skeggs 已提交
724
		NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
725
			 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
726
		return nouveau_abi16_put(abi16, -EINVAL);
727 728
	}

729
	if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
B
Ben Skeggs 已提交
730
		NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
731
			 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
732
		return nouveau_abi16_put(abi16, -EINVAL);
733 734
	}

735
	push = u_memcpya(req->push, req->nr_push, sizeof(*push));
736 737
	if (IS_ERR(push))
		return nouveau_abi16_put(abi16, PTR_ERR(push));
738

739
	bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
740
	if (IS_ERR(bo)) {
741
		u_free(push);
742
		return nouveau_abi16_put(abi16, PTR_ERR(bo));
743
	}
744

745
	/* Ensure all push buffers are on validate list */
746 747
	for (i = 0; i < req->nr_push; i++) {
		if (push[i].bo_index >= req->nr_buffers) {
B
Ben Skeggs 已提交
748
			NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
749
			ret = -EINVAL;
750
			goto out_prevalid;
751 752 753
		}
	}

754
	/* Validate buffer list */
755 756
revalidate:
	ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo,
757 758
					   req->nr_buffers, &op, &do_reloc);
	if (ret) {
759
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
760
			NV_PRINTK(err, cli, "validate: %d\n", ret);
761
		goto out_prevalid;
762 763 764 765
	}

	/* Apply any relocations that are required */
	if (do_reloc) {
766 767 768 769 770 771 772 773 774 775 776 777
		if (!reloc) {
			validate_fini(&op, chan, NULL, bo);
			reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
			if (IS_ERR(reloc)) {
				ret = PTR_ERR(reloc);
				goto out_prevalid;
			}

			goto revalidate;
		}

		ret = nouveau_gem_pushbuf_reloc_apply(cli, req, reloc, bo);
778
		if (ret) {
B
Ben Skeggs 已提交
779
			NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
780 781 782 783
			goto out;
		}
	}

784
	if (chan->dma.ib_max) {
785
		ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
786
		if (ret) {
B
Ben Skeggs 已提交
787
			NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
788 789 790
			goto out;
		}

791
		for (i = 0; i < req->nr_push; i++) {
792
			struct nouveau_vma *vma = (void *)(unsigned long)
793 794
				bo[push[i].bo_index].user_priv;

795
			nv50_dma_push(chan, vma->addr + push[i].offset,
796 797
				      push[i].length);
		}
798
	} else
799
	if (drm->client.device.info.chipset >= 0x25) {
800
		ret = RING_SPACE(chan, req->nr_push * 2);
801
		if (ret) {
B
Ben Skeggs 已提交
802
			NV_PRINTK(err, cli, "cal_space: %d\n", ret);
803 804
			goto out;
		}
805 806 807 808 809

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

810
			OUT_RING(chan, (nvbo->offset + push[i].offset) | 2);
811 812
			OUT_RING(chan, 0);
		}
813
	} else {
814
		ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
815
		if (ret) {
B
Ben Skeggs 已提交
816
			NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
817 818 819
			goto out;
		}

820 821 822 823 824
		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;

825
			cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
			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);
			}

844
			OUT_RING(chan, 0x20000000 |
845
				      (nvbo->offset + push[i].offset));
846
			OUT_RING(chan, 0);
847 848 849
			for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
				OUT_RING(chan, 0);
		}
850 851
	}

852
	ret = nouveau_fence_new(chan, false, &fence);
853
	if (ret) {
B
Ben Skeggs 已提交
854
		NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
855 856 857 858
		WIND_RING(chan);
		goto out;
	}

859 860 861 862 863 864 865
	if (sync) {
		if (!(ret = nouveau_fence_wait(fence, false, false))) {
			if ((ret = dma_fence_get_status(&fence->base)) == 1)
				ret = 0;
		}
	}

866
out:
867
	validate_fini(&op, chan, fence, bo);
868
	nouveau_fence_unref(&fence);
869

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	if (do_reloc) {
		struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
			u64_to_user_ptr(req->buffers);

		for (i = 0; i < req->nr_buffers; i++) {
			if (bo[i].presumed.valid)
				continue;

			if (copy_to_user(&upbbo[i].presumed, &bo[i].presumed,
					 sizeof(bo[i].presumed))) {
				ret = -EFAULT;
				break;
			}
		}
		u_free(reloc);
	}
886
out_prevalid:
887 888
	u_free(bo);
	u_free(push);
889 890

out_next:
891 892 893 894
	if (chan->dma.ib_max) {
		req->suffix0 = 0x00000000;
		req->suffix1 = 0x00000000;
	} else
895
	if (drm->client.device.info.chipset >= 0x25) {
896 897 898 899
		req->suffix0 = 0x00020000;
		req->suffix1 = 0x00000000;
	} else {
		req->suffix0 = 0x20000000 |
900
			      (chan->push.addr + ((chan->dma.cur + 2) << 2));
901 902 903
		req->suffix1 = 0x00000000;
	}

904
	return nouveau_abi16_put(abi16, ret);
905 906 907 908 909 910 911 912 913 914
}

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);
915
	bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
916
	long lret;
917
	int ret;
918

919
	gem = drm_gem_object_lookup(file_priv, req->handle);
920
	if (!gem)
921
		return -ENOENT;
922 923
	nvbo = nouveau_gem_object(gem);

924
	lret = dma_resv_wait_timeout_rcu(nvbo->bo.base.resv, write, true,
925 926 927 928 929 930 931
						   no_wait ? 0 : 30 * HZ);
	if (!lret)
		ret = -EBUSY;
	else if (lret > 0)
		ret = 0;
	else
		ret = lret;
932

933
	nouveau_bo_sync_for_cpu(nvbo);
934
	drm_gem_object_put(gem);
935

936 937 938 939 940 941 942
	return ret;
}

int
nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
943 944 945 946
	struct drm_nouveau_gem_cpu_fini *req = data;
	struct drm_gem_object *gem;
	struct nouveau_bo *nvbo;

947
	gem = drm_gem_object_lookup(file_priv, req->handle);
948 949 950 951 952
	if (!gem)
		return -ENOENT;
	nvbo = nouveau_gem_object(gem);

	nouveau_bo_sync_for_device(nvbo);
953
	drm_gem_object_put(gem);
B
Ben Skeggs 已提交
954
	return 0;
955 956 957 958 959 960 961 962 963 964
}

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;

965
	gem = drm_gem_object_lookup(file_priv, req->handle);
966
	if (!gem)
967
		return -ENOENT;
968

969
	ret = nouveau_gem_info(file_priv, gem, req);
970
	drm_gem_object_put(gem);
971 972 973
	return ret;
}