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
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct device *dev = drm->dev->dev;
	int ret;

	ret = pm_runtime_get_sync(dev);
48 49
	if (WARN_ON(ret < 0 && ret != -EACCES)) {
		pm_runtime_put_autosuspend(dev);
50
		return;
51
	}
52

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

56
	ttm_bo_put(&nvbo->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_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
70
	struct nouveau_vma *vma;
71
	int ret;
72

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

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

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

86
	ret = nouveau_vma_new(nvbo, vmm, &vma);
87 88
	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_fence_unref(&vma->fence);
103
	nouveau_vma_del(&vma);
104 105
}

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

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

121
	list_del_init(&vma->head);
122

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

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

150
	if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50)
151
		return;
152

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

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

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

181 182 183 184 185 186 187
	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;

188 189 190
	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
		flags |= TTM_PL_FLAG_UNCACHED;

191 192
	nvbo = nouveau_bo_alloc(cli, &size, &align, flags, tile_mode,
				tile_flags);
193 194 195 196 197 198 199 200
	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);
201
		return ret;
202 203 204 205 206 207 208
	}

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

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

219
	nvbo->bo.persistent_swap_storage = nvbo->bo.base.filp;
220
	*pnvbo = nvbo;
221 222 223 224
	return 0;
}

static int
225 226
nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
		 struct drm_nouveau_gem_info *rep)
227
{
228
	struct nouveau_cli *cli = nouveau_cli(file_priv);
229
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
230
	struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
231
	struct nouveau_vma *vma;
232

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

245
		rep->offset = vma->addr;
246 247
	}

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

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

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

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

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

300
	if (!domains)
301 302
		return -EINVAL;

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

	return 0;
}

struct validate_op {
329
	struct list_head list;
330
	struct ww_acquire_ctx ticket;
331 332 333
};

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

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

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

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

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

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

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

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

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

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

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

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

443 444
		if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
			struct nouveau_vmm *vmm = chan->vmm;
445 446 447 448 449 450 451 452 453 454 455 456
			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;
		}

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

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

487 488 489
}

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

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

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

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

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

	return relocs;
}

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

553
	INIT_LIST_HEAD(&op->list);
554 555 556 557 558

	if (nr_buffers == 0)
		return 0;

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

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

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

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

588 589
	size *= nmemb;

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

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

	return mem;
}

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

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

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

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

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

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

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

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

		nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
673 674
	}

675
	u_free(reloc);
676 677 678 679 680 681 682
	return ret;
}

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

697 698
	if (unlikely(!abi16))
		return -ENOMEM;
699

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

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

712 713
	sync = req->vram_available & NOUVEAU_GEM_PUSHBUF_SYNC;

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

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

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

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

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

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

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

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

	/* Apply any relocations that are required */
	if (do_reloc) {
768 769 770 771 772 773 774 775 776 777 778 779
		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);
780
		if (ret) {
B
Ben Skeggs 已提交
781
			NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
782 783 784 785
			goto out;
		}
	}

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

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

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

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

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

822 823 824 825 826
		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;

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

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

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

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

868
out:
869
	validate_fini(&op, chan, fence, bo);
870
	nouveau_fence_unref(&fence);
871

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	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);
	}
888
out_prevalid:
889 890
	u_free(bo);
	u_free(push);
891 892

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

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

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

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

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

935
	nouveau_bo_sync_for_cpu(nvbo);
936
	drm_gem_object_put(gem);
937

938 939 940 941 942 943 944
	return ret;
}

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

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

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

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;

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

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