nouveau_gem.c 24.3 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
#include <nvif/class.h>
38
#include <nvif/push206e.h>
39

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

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

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

57
	ttm_bo_put(&nvbo->bo);
58 59 60

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

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

74
	if (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
	ret = pm_runtime_get_sync(dev);
82 83
	if (ret < 0 && ret != -EACCES) {
		pm_runtime_put_autosuspend(dev);
84
		goto out;
85
	}
86

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

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

100
static void
101
nouveau_gem_object_delete(struct nouveau_vma *vma)
102
{
103
	nouveau_fence_unref(&vma->fence);
104
	nouveau_vma_del(&vma);
105 106
}

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

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

122
	list_del_init(&vma->head);
123

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

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

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

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

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

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

181 182
	if (!(domain & (NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART)))
		domain |= NOUVEAU_GEM_DOMAIN_CPU;
183

184
	nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode,
185
				tile_flags);
186 187 188 189 190 191 192 193
	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);
194
		return ret;
195 196
	}

197
	ret = nouveau_bo_init(nvbo, size, align, domain, NULL, NULL);
198 199 200 201
	if (ret) {
		nouveau_bo_ref(NULL, &nvbo);
		return ret;
	}
202

203 204 205 206 207 208
	/* 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;
209
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
210 211
		nvbo->valid_domains &= domain;

212
	nvbo->bo.persistent_swap_storage = nvbo->bo.base.filp;
213
	*pnvbo = nvbo;
214 215 216 217
	return 0;
}

static int
218 219
nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
		 struct drm_nouveau_gem_info *rep)
220
{
221
	struct nouveau_cli *cli = nouveau_cli(file_priv);
222
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
223
	struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
224
	struct nouveau_vma *vma;
225

226 227 228
	if (is_power_of_2(nvbo->valid_domains))
		rep->domain = nvbo->valid_domains;
	else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
229 230 231
		rep->domain = NOUVEAU_GEM_DOMAIN_GART;
	else
		rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
232
	rep->offset = nvbo->offset;
233 234
	if (vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
		vma = nouveau_vma_find(nvbo, vmm);
235 236 237
		if (!vma)
			return -EINVAL;

238
		rep->offset = vma->addr;
239 240
	}

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

int
nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
{
259
	struct nouveau_cli *cli = nouveau_cli(file_priv);
260 261 262 263
	struct drm_nouveau_gem_new *req = data;
	struct nouveau_bo *nvbo = NULL;
	int ret = 0;

264
	ret = nouveau_gem_new(cli, req->info.size, req->align,
265 266
			      req->info.domain, req->info.tile_mode,
			      req->info.tile_flags, &nvbo);
267 268 269
	if (ret)
		return ret;

270 271
	ret = drm_gem_handle_create(file_priv, &nvbo->bo.base,
				    &req->info.handle);
272
	if (ret == 0) {
273
		ret = nouveau_gem_info(file_priv, &nvbo->bo.base, &req->info);
274 275 276 277
		if (ret)
			drm_gem_handle_delete(file_priv, req->info.handle);
	}

278
	/* drop reference from allocate - handle holds it now */
279
	drm_gem_object_put(&nvbo->bo.base);
280 281 282 283 284 285 286
	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)
{
287
	struct nouveau_bo *nvbo = nouveau_gem_object(gem);
288
	struct ttm_buffer_object *bo = &nvbo->bo;
289
	uint32_t domains = valid_domains & nvbo->valid_domains &
290
		(write_domains ? write_domains : read_domains);
291
	uint32_t pref_domains = 0;;
292

293
	if (!domains)
294 295
		return -EINVAL;

296
	valid_domains &= ~(NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART);
297 298 299

	if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
	    bo->mem.mem_type == TTM_PL_VRAM)
300
		pref_domains |= NOUVEAU_GEM_DOMAIN_VRAM;
301 302 303

	else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
		 bo->mem.mem_type == TTM_PL_TT)
304
		pref_domains |= NOUVEAU_GEM_DOMAIN_GART;
305 306

	else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
307
		pref_domains |= NOUVEAU_GEM_DOMAIN_VRAM;
308 309

	else
310
		pref_domains |= NOUVEAU_GEM_DOMAIN_GART;
311

312
	nouveau_bo_placement_set(nvbo, pref_domains, valid_domains);
313 314 315 316 317

	return 0;
}

struct validate_op {
318
	struct list_head list;
319
	struct ww_acquire_ctx ticket;
320 321 322
};

static void
323 324
validate_fini_no_ticket(struct validate_op *op, struct nouveau_channel *chan,
			struct nouveau_fence *fence,
325
			struct drm_nouveau_gem_pushbuf_bo *pbbo)
326 327
{
	struct nouveau_bo *nvbo;
328
	struct drm_nouveau_gem_pushbuf_bo *b;
329

330 331
	while (!list_empty(&op->list)) {
		nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
332
		b = &pbbo[nvbo->pbbo_index];
333

334
		if (likely(fence)) {
335
			nouveau_bo_fence(nvbo, fence, !!b->write_domains);
336

337 338 339
			if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
				struct nouveau_vma *vma =
					(void *)(unsigned long)b->user_priv;
340 341 342 343 344 345
				nouveau_fence_unref(&vma->fence);
				dma_fence_get(&fence->base);
				vma->fence = fence;
			}
		}

346 347 348 349 350
		if (unlikely(nvbo->validate_mapped)) {
			ttm_bo_kunmap(&nvbo->kmap);
			nvbo->validate_mapped = false;
		}

351 352
		list_del(&nvbo->entry);
		nvbo->reserved_by = NULL;
353
		ttm_bo_unreserve(&nvbo->bo);
354
		drm_gem_object_put(&nvbo->bo.base);
355 356 357
	}
}

358
static void
359 360
validate_fini(struct validate_op *op, struct nouveau_channel *chan,
	      struct nouveau_fence *fence,
361
	      struct drm_nouveau_gem_pushbuf_bo *pbbo)
362
{
363
	validate_fini_no_ticket(op, chan, fence, pbbo);
364
	ww_acquire_fini(&op->ticket);
365 366 367 368 369 370 371
}

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

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

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

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

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

432 433
		if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
			struct nouveau_vmm *vmm = chan->vmm;
434 435 436 437 438 439 440 441 442 443 444 445
			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;
		}

446 447 448 449
		nvbo->reserved_by = file_priv;
		nvbo->pbbo_index = i;
		if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
		    (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
450
			list_add_tail(&nvbo->entry, &both_list);
451 452
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
453
			list_add_tail(&nvbo->entry, &vram_list);
454 455
		else
		if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
456
			list_add_tail(&nvbo->entry, &gart_list);
457
		else {
B
Ben Skeggs 已提交
458
			NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
459
				 b->valid_domains);
460 461 462
			list_add_tail(&nvbo->entry, &both_list);
			ret = -EINVAL;
			break;
463
		}
464 465
		if (nvbo == res_bo)
			goto retry;
466 467
	}

468
	ww_acquire_done(&op->ticket);
469 470 471 472
	list_splice_tail(&vram_list, &op->list);
	list_splice_tail(&gart_list, &op->list);
	list_splice_tail(&both_list, &op->list);
	if (ret)
473
		validate_fini(op, chan, NULL, NULL);
474 475
	return ret;

476 477 478
}

static int
479
validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
480
	      struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo)
481
{
482
	struct nouveau_drm *drm = chan->drm;
483 484 485 486 487 488
	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];

489
		ret = nouveau_gem_set_domain(&nvbo->bo.base, b->read_domains,
490 491
					     b->write_domains,
					     b->valid_domains);
492
		if (unlikely(ret)) {
B
Ben Skeggs 已提交
493
			NV_PRINTK(err, cli, "fail set_domain\n");
494
			return ret;
495
		}
496

497
		ret = nouveau_bo_validate(nvbo, true, false);
498
		if (unlikely(ret)) {
499
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
500
				NV_PRINTK(err, cli, "fail ttm_validate\n");
501
			return ret;
502
		}
503

504
		ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
505
		if (unlikely(ret)) {
506
			if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
507
				NV_PRINTK(err, cli, "fail post-validate sync\n");
508 509 510
			return ret;
		}

511
		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
512
			if (nvbo->offset == b->presumed.offset &&
513 514 515 516 517 518 519 520 521 522
			    ((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;
523
			b->presumed.offset = nvbo->offset;
524 525 526
			b->presumed.valid = 0;
			relocs++;
		}
527 528 529 530 531 532 533 534 535
	}

	return relocs;
}

static int
nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
			     struct drm_file *file_priv,
			     struct drm_nouveau_gem_pushbuf_bo *pbbo,
536 537
			     int nr_buffers,
			     struct validate_op *op, bool *apply_relocs)
538
{
539
	struct nouveau_cli *cli = nouveau_cli(file_priv);
540
	int ret;
541

542
	INIT_LIST_HEAD(&op->list);
543 544 545 546 547

	if (nr_buffers == 0)
		return 0;

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

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

565 566 567
static inline void
u_free(void *addr)
{
568
	kvfree(addr);
569 570
}

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

577 578
	size *= nmemb;

579
	mem = kvmalloc(size, GFP_KERNEL);
580 581 582
	if (!mem)
		return ERR_PTR(-ENOMEM);

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

	return mem;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

696 697
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);
698 699
	if (unlikely(atomic_read(&chan->killed)))
		return nouveau_abi16_put(abi16, -ENODEV);
700

701 702
	sync = req->vram_available & NOUVEAU_GEM_PUSHBUF_SYNC;

703 704
	req->vram_available = drm->gem.vram_available;
	req->gart_available = drm->gem.gart_available;
705 706
	if (unlikely(req->nr_push == 0))
		goto out_next;
707

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

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

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

726
	push = u_memcpya(req->push, req->nr_push, sizeof(*push));
727 728
	if (IS_ERR(push))
		return nouveau_abi16_put(abi16, PTR_ERR(push));
729

730
	bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
731
	if (IS_ERR(bo)) {
732
		u_free(push);
733
		return nouveau_abi16_put(abi16, PTR_ERR(bo));
734
	}
735

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

745
	/* Validate buffer list */
746 747
revalidate:
	ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo,
748 749
					   req->nr_buffers, &op, &do_reloc);
	if (ret) {
750
		if (ret != -ERESTARTSYS)
B
Ben Skeggs 已提交
751
			NV_PRINTK(err, cli, "validate: %d\n", ret);
752
		goto out_prevalid;
753 754 755 756
	}

	/* Apply any relocations that are required */
	if (do_reloc) {
757 758 759 760 761 762 763 764 765 766 767 768
		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);
769
		if (ret) {
B
Ben Skeggs 已提交
770
			NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
771 772 773 774
			goto out;
		}
	}

775
	if (chan->dma.ib_max) {
776
		ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
777
		if (ret) {
B
Ben Skeggs 已提交
778
			NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
779 780 781
			goto out;
		}

782
		for (i = 0; i < req->nr_push; i++) {
783
			struct nouveau_vma *vma = (void *)(unsigned long)
784 785
				bo[push[i].bo_index].user_priv;

786
			nv50_dma_push(chan, vma->addr + push[i].offset,
787 788
				      push[i].length);
		}
789
	} else
790
	if (drm->client.device.info.chipset >= 0x25) {
791
		ret = PUSH_WAIT(chan->chan.push, req->nr_push * 2);
792
		if (ret) {
B
Ben Skeggs 已提交
793
			NV_PRINTK(err, cli, "cal_space: %d\n", ret);
794 795
			goto out;
		}
796 797 798 799 800

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

801 802
			PUSH_CALL(chan->chan.push, nvbo->offset + push[i].offset);
			PUSH_DATA(chan->chan.push, 0);
803
		}
804
	} else {
805
		ret = PUSH_WAIT(chan->chan.push, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
806
		if (ret) {
B
Ben Skeggs 已提交
807
			NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
808 809 810
			goto out;
		}

811 812 813 814 815
		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;

816
			cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
			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);
			}

835 836
			PUSH_JUMP(chan->chan.push, nvbo->offset + push[i].offset);
			PUSH_DATA(chan->chan.push, 0);
837
			for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
838
				PUSH_DATA(chan->chan.push, 0);
839
		}
840 841
	}

842
	ret = nouveau_fence_new(chan, false, &fence);
843
	if (ret) {
B
Ben Skeggs 已提交
844
		NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
845 846 847 848
		WIND_RING(chan);
		goto out;
	}

849 850 851 852 853 854 855
	if (sync) {
		if (!(ret = nouveau_fence_wait(fence, false, false))) {
			if ((ret = dma_fence_get_status(&fence->base)) == 1)
				ret = 0;
		}
	}

856
out:
857
	validate_fini(&op, chan, fence, bo);
858
	nouveau_fence_unref(&fence);
859

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
	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);
	}
876
out_prevalid:
877 878
	u_free(bo);
	u_free(push);
879 880

out_next:
881 882 883 884
	if (chan->dma.ib_max) {
		req->suffix0 = 0x00000000;
		req->suffix1 = 0x00000000;
	} else
885
	if (drm->client.device.info.chipset >= 0x25) {
886 887 888 889
		req->suffix0 = 0x00020000;
		req->suffix1 = 0x00000000;
	} else {
		req->suffix0 = 0x20000000 |
890
			      (chan->push.addr + ((chan->dma.cur + 2) << 2));
891 892 893
		req->suffix1 = 0x00000000;
	}

894
	return nouveau_abi16_put(abi16, ret);
895 896 897 898 899 900 901 902 903 904
}

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);
905
	bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
906
	long lret;
907
	int ret;
908

909
	gem = drm_gem_object_lookup(file_priv, req->handle);
910
	if (!gem)
911
		return -ENOENT;
912 913
	nvbo = nouveau_gem_object(gem);

914
	lret = dma_resv_wait_timeout_rcu(nvbo->bo.base.resv, write, true,
915 916 917 918 919 920 921
						   no_wait ? 0 : 30 * HZ);
	if (!lret)
		ret = -EBUSY;
	else if (lret > 0)
		ret = 0;
	else
		ret = lret;
922

923
	nouveau_bo_sync_for_cpu(nvbo);
924
	drm_gem_object_put(gem);
925

926 927 928 929 930 931 932
	return ret;
}

int
nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
{
933 934 935 936
	struct drm_nouveau_gem_cpu_fini *req = data;
	struct drm_gem_object *gem;
	struct nouveau_bo *nvbo;

937
	gem = drm_gem_object_lookup(file_priv, req->handle);
938 939 940 941 942
	if (!gem)
		return -ENOENT;
	nvbo = nouveau_gem_object(gem);

	nouveau_bo_sync_for_device(nvbo);
943
	drm_gem_object_put(gem);
B
Ben Skeggs 已提交
944
	return 0;
945 946 947 948 949 950 951 952 953 954
}

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;

955
	gem = drm_gem_object_lookup(file_priv, req->handle);
956
	if (!gem)
957
		return -ENOENT;
958

959
	ret = nouveau_gem_info(file_priv, gem, req);
960
	drm_gem_object_put(gem);
961 962 963
	return ret;
}