nouveau_bo.c 32.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 27 28 29
/*
 * Copyright 2007 Dave Airlied
 * 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
 * VA LINUX SYSTEMS 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.
 */
/*
 * Authors: Dave Airlied <airlied@linux.ie>
 *	    Ben Skeggs   <darktama@iinet.net.au>
 *	    Jeremy Kolb  <jkolb@brandeis.edu>
 */

30
#include <linux/dma-mapping.h>
31
#include <linux/swiotlb.h>
32

33
#include "nouveau_drv.h"
34
#include "nouveau_chan.h"
35
#include "nouveau_fence.h"
36

37 38 39
#include "nouveau_bo.h"
#include "nouveau_ttm.h"
#include "nouveau_gem.h"
40
#include "nouveau_mem.h"
41
#include "nouveau_vmm.h"
42

43 44 45 46
#include <nvif/class.h>
#include <nvif/if500b.h>
#include <nvif/if900b.h>

47 48 49 50 51
/*
 * NV10-NV40 tiling helpers
 */

static void
52 53
nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
			   u32 addr, u32 size, u32 pitch, u32 flags)
54
{
55
	struct nouveau_drm *drm = nouveau_drm(dev);
56
	int i = reg - drm->tile.reg;
57
	struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
B
Ben Skeggs 已提交
58
	struct nvkm_fb_tile *tile = &fb->tile.region[i];
59

60
	nouveau_fence_unref(&reg->fence);
61 62

	if (tile->pitch)
63
		nvkm_fb_tile_fini(fb, i, tile);
64 65

	if (pitch)
66
		nvkm_fb_tile_init(fb, i, addr, size, pitch, flags, tile);
67

68
	nvkm_fb_tile_prog(fb, i, tile);
69 70
}

71
static struct nouveau_drm_tile *
72 73
nv10_bo_get_tile_region(struct drm_device *dev, int i)
{
74
	struct nouveau_drm *drm = nouveau_drm(dev);
75
	struct nouveau_drm_tile *tile = &drm->tile.reg[i];
76

77
	spin_lock(&drm->tile.lock);
78 79 80 81 82 83 84

	if (!tile->used &&
	    (!tile->fence || nouveau_fence_done(tile->fence)))
		tile->used = true;
	else
		tile = NULL;

85
	spin_unlock(&drm->tile.lock);
86 87 88 89
	return tile;
}

static void
90
nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
91
			struct dma_fence *fence)
92
{
93
	struct nouveau_drm *drm = nouveau_drm(dev);
94 95

	if (tile) {
96
		spin_lock(&drm->tile.lock);
97
		tile->fence = (struct nouveau_fence *)dma_fence_get(fence);
98
		tile->used = false;
99
		spin_unlock(&drm->tile.lock);
100 101 102
	}
}

103 104
static struct nouveau_drm_tile *
nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
105
		   u32 size, u32 pitch, u32 zeta)
106
{
107
	struct nouveau_drm *drm = nouveau_drm(dev);
108
	struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
109
	struct nouveau_drm_tile *tile, *found = NULL;
110 111
	int i;

B
Ben Skeggs 已提交
112
	for (i = 0; i < fb->tile.regions; i++) {
113 114 115 116 117 118
		tile = nv10_bo_get_tile_region(dev, i);

		if (pitch && !found) {
			found = tile;
			continue;

B
Ben Skeggs 已提交
119
		} else if (tile && fb->tile.region[i].pitch) {
120 121 122 123 124 125 126 127
			/* Kill an unused tile region. */
			nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
		}

		nv10_bo_put_tile_region(dev, tile, NULL);
	}

	if (found)
128
		nv10_bo_update_tile_region(dev, found, addr, size, pitch, zeta);
129 130 131
	return found;
}

132 133 134
static void
nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
{
135 136
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
	struct drm_device *dev = drm->dev;
137 138
	struct nouveau_bo *nvbo = nouveau_bo(bo);

139
	WARN_ON(nvbo->pin_refcnt > 0);
140
	nouveau_bo_del_io_reserve_lru(bo);
141
	nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
142 143 144 145 146 147 148 149

	/*
	 * If nouveau_bo_new() allocated this buffer, the GEM object was never
	 * initialized, so don't attempt to release it.
	 */
	if (bo->base.dev)
		drm_gem_object_release(&bo->base);

150 151 152
	kfree(nvbo);
}

B
Ben Skeggs 已提交
153 154 155 156 157 158 159 160
static inline u64
roundup_64(u64 x, u32 y)
{
	x += y - 1;
	do_div(x, y);
	return x * y;
}

161
static void
162
nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
B
Ben Skeggs 已提交
163
		       int *align, u64 *size)
164
{
165
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
166
	struct nvif_device *device = &drm->client.device;
167

168
	if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
169
		if (nvbo->mode) {
170
			if (device->info.chipset >= 0x40) {
171
				*align = 65536;
172
				*size = roundup_64(*size, 64 * nvbo->mode);
173

174
			} else if (device->info.chipset >= 0x30) {
175
				*align = 32768;
176
				*size = roundup_64(*size, 64 * nvbo->mode);
177

178
			} else if (device->info.chipset >= 0x20) {
179
				*align = 16384;
180
				*size = roundup_64(*size, 64 * nvbo->mode);
181

182
			} else if (device->info.chipset >= 0x10) {
183
				*align = 16384;
184
				*size = roundup_64(*size, 32 * nvbo->mode);
185 186
			}
		}
187
	} else {
188 189
		*size = roundup_64(*size, (1 << nvbo->page));
		*align = max((1 <<  nvbo->page), *align);
190 191
	}

B
Ben Skeggs 已提交
192
	*size = roundup_64(*size, PAGE_SIZE);
193 194
}

195
struct nouveau_bo *
196 197
nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 flags,
		 u32 tile_mode, u32 tile_flags)
198
{
199
	struct nouveau_drm *drm = cli->drm;
200
	struct nouveau_bo *nvbo;
201
	struct nvif_mmu *mmu = &cli->mmu;
202
	struct nvif_vmm *vmm = cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm;
203
	int i, pi = -1;
204

205 206
	if (!*size) {
		NV_WARN(drm, "skipped size %016llx\n", *size);
207
		return ERR_PTR(-EINVAL);
208
	}
D
Dave Airlie 已提交
209

210 211
	nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
	if (!nvbo)
212
		return ERR_PTR(-ENOMEM);
213 214
	INIT_LIST_HEAD(&nvbo->head);
	INIT_LIST_HEAD(&nvbo->entry);
215
	INIT_LIST_HEAD(&nvbo->vma_list);
216
	nvbo->bo.bdev = &drm->ttm.bdev;
217

218 219 220 221 222 223 224 225
	/* This is confusing, and doesn't actually mean we want an uncached
	 * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated
	 * into in nouveau_gem_new().
	 */
	if (flags & TTM_PL_FLAG_UNCACHED) {
		/* Determine if we can get a cache-coherent map, forcing
		 * uncached mapping if we can't.
		 */
226
		if (!nouveau_drm_use_coherent_gpu_mapping(drm))
227 228
			nvbo->force_coherent = true;
	}
229

230 231
	if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) {
		nvbo->kind = (tile_flags & 0x0000ff00) >> 8;
232 233
		if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
			kfree(nvbo);
234
			return ERR_PTR(-EINVAL);
235 236 237
		}

		nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind;
238 239 240 241
	} else
	if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
		nvbo->kind = (tile_flags & 0x00007f00) >> 8;
		nvbo->comp = (tile_flags & 0x00030000) >> 16;
242 243
		if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
			kfree(nvbo);
244
			return ERR_PTR(-EINVAL);
245
		}
246 247 248 249 250 251
	} else {
		nvbo->zeta = (tile_flags & 0x00000007);
	}
	nvbo->mode = tile_mode;
	nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG);

252 253 254 255 256 257 258 259 260 261 262 263
	/* Determine the desirable target GPU page size for the buffer. */
	for (i = 0; i < vmm->page_nr; i++) {
		/* Because we cannot currently allow VMM maps to fail
		 * during buffer migration, we need to determine page
		 * size for the buffer up-front, and pre-allocate its
		 * page tables.
		 *
		 * Skip page sizes that can't support needed domains.
		 */
		if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE &&
		    (flags & TTM_PL_FLAG_VRAM) && !vmm->page[i].vram)
			continue;
264 265
		if ((flags & TTM_PL_FLAG_TT) &&
		    (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT))
266 267 268 269 270 271 272 273 274 275
			continue;

		/* Select this page size if it's the first that supports
		 * the potential memory domains, or when it's compatible
		 * with the requested compression settings.
		 */
		if (pi < 0 || !nvbo->comp || vmm->page[i].comp)
			pi = i;

		/* Stop once the buffer is larger than the current page size. */
276
		if (*size >= 1ULL << vmm->page[i].shift)
277 278 279 280
			break;
	}

	if (WARN_ON(pi < 0))
281
		return ERR_PTR(-EINVAL);
282 283 284 285 286 287

	/* Disable compression if suitable settings couldn't be found. */
	if (nvbo->comp && !vmm->page[pi].comp) {
		if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
			nvbo->kind = mmu->kind[nvbo->kind];
		nvbo->comp = 0;
288
	}
289
	nvbo->page = vmm->page[pi].shift;
290

291 292
	nouveau_bo_fixup_align(nvbo, flags, align, size);

293 294 295 296 297 298 299 300 301 302 303 304 305
	return nvbo;
}

int
nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 flags,
		struct sg_table *sg, struct dma_resv *robj)
{
	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
	size_t acc_size;
	int ret;

	acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));

306 307
	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
	nouveau_bo_placement_set(nvbo, flags, 0);
308
	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
309

310 311 312
	ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
			  &nvbo->placement, align >> PAGE_SHIFT, false,
			  acc_size, sg, robj, nouveau_bo_del_ttm);
313 314 315 316 317
	if (ret) {
		/* ttm will call nouveau_bo_del_ttm if it fails.. */
		return ret;
	}

318 319 320 321 322 323 324 325 326 327 328 329
	return 0;
}

int
nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align,
	       uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
	       struct sg_table *sg, struct dma_resv *robj,
	       struct nouveau_bo **pnvbo)
{
	struct nouveau_bo *nvbo;
	int ret;

330 331
	nvbo = nouveau_bo_alloc(cli, &size, &align, flags, tile_mode,
				tile_flags);
332 333 334 335 336 337 338
	if (IS_ERR(nvbo))
		return PTR_ERR(nvbo);

	ret = nouveau_bo_init(nvbo, size, align, flags, sg, robj);
	if (ret)
		return ret;

339 340 341 342
	*pnvbo = nvbo;
	return 0;
}

343
static void
344
set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t type, uint32_t flags)
345 346 347 348
{
	*n = 0;

	if (type & TTM_PL_FLAG_VRAM)
349
		pl[(*n)++].flags = TTM_PL_FLAG_VRAM | flags;
350
	if (type & TTM_PL_FLAG_TT)
351
		pl[(*n)++].flags = TTM_PL_FLAG_TT | flags;
352
	if (type & TTM_PL_FLAG_SYSTEM)
353
		pl[(*n)++].flags = TTM_PL_FLAG_SYSTEM | flags;
354 355
}

356 357 358
static void
set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
{
359
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
360
	u32 vram_pages = drm->client.device.info.ram_size >> PAGE_SHIFT;
361
	unsigned i, fpfn, lpfn;
362

363
	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
364
	    nvbo->mode && (type & TTM_PL_FLAG_VRAM) &&
365
	    nvbo->bo.mem.num_pages < vram_pages / 4) {
366 367 368 369 370 371
		/*
		 * Make sure that the color and depth buffers are handled
		 * by independent memory controller units. Up to a 9x
		 * speed up when alpha-blending and depth-test are enabled
		 * at the same time.
		 */
372
		if (nvbo->zeta) {
373 374
			fpfn = vram_pages / 2;
			lpfn = ~0;
375
		} else {
376 377 378 379 380 381 382 383 384 385
			fpfn = 0;
			lpfn = vram_pages / 2;
		}
		for (i = 0; i < nvbo->placement.num_placement; ++i) {
			nvbo->placements[i].fpfn = fpfn;
			nvbo->placements[i].lpfn = lpfn;
		}
		for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
			nvbo->busy_placements[i].fpfn = fpfn;
			nvbo->busy_placements[i].lpfn = lpfn;
386 387 388 389
		}
	}
}

390
void
391
nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
392
{
393
	struct ttm_placement *pl = &nvbo->placement;
394 395 396
	uint32_t flags = (nvbo->force_coherent ? TTM_PL_FLAG_UNCACHED :
						 TTM_PL_MASK_CACHING) |
			 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
397 398 399 400 401 402 403 404

	pl->placement = nvbo->placements;
	set_placement_list(nvbo->placements, &pl->num_placement,
			   type, flags);

	pl->busy_placement = nvbo->busy_placements;
	set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
			   type | busy, flags);
405 406

	set_placement_range(nvbo, type);
407 408 409
}

int
410
nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype, bool contig)
411
{
412
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
413
	struct ttm_buffer_object *bo = &nvbo->bo;
414
	bool force = false, evict = false;
415
	int ret;
416

417
	ret = ttm_bo_reserve(bo, false, false, NULL);
418
	if (ret)
419
		return ret;
420

421
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
422
	    memtype == TTM_PL_FLAG_VRAM && contig) {
423 424
		if (!nvbo->contig) {
			nvbo->contig = true;
425
			force = true;
426
			evict = true;
427
		}
428 429
	}

430 431 432 433 434 435 436 437
	if (nvbo->pin_refcnt) {
		if (!(memtype & (1 << bo->mem.mem_type)) || evict) {
			NV_ERROR(drm, "bo %p pinned elsewhere: "
				      "0x%08x vs 0x%08x\n", bo,
				 1 << bo->mem.mem_type, memtype);
			ret = -EBUSY;
		}
		nvbo->pin_refcnt++;
438
		goto out;
439 440 441 442 443 444 445 446
	}

	if (evict) {
		nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT, 0);
		ret = nouveau_bo_validate(nvbo, false, false);
		if (ret)
			goto out;
	}
447

448
	nvbo->pin_refcnt++;
449
	nouveau_bo_placement_set(nvbo, memtype, 0);
450

451 452 453 454 455
	/* drop pin_refcnt temporarily, so we don't trip the assertion
	 * in nouveau_bo_move() that makes sure we're not trying to
	 * move a pinned buffer
	 */
	nvbo->pin_refcnt--;
456
	ret = nouveau_bo_validate(nvbo, false, false);
457 458
	if (ret)
		goto out;
459
	nvbo->pin_refcnt++;
460 461 462 463 464 465 466 467 468 469

	switch (bo->mem.mem_type) {
	case TTM_PL_VRAM:
		drm->gem.vram_available -= bo->mem.size;
		break;
	case TTM_PL_TT:
		drm->gem.gart_available -= bo->mem.size;
		break;
	default:
		break;
470
	}
471

472
out:
473
	if (force && ret)
474
		nvbo->contig = false;
475
	ttm_bo_unreserve(bo);
476 477 478 479 480 481
	return ret;
}

int
nouveau_bo_unpin(struct nouveau_bo *nvbo)
{
482
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
483
	struct ttm_buffer_object *bo = &nvbo->bo;
484
	int ret, ref;
485

486
	ret = ttm_bo_reserve(bo, false, false, NULL);
487 488 489
	if (ret)
		return ret;

490 491 492
	ref = --nvbo->pin_refcnt;
	WARN_ON_ONCE(ref < 0);
	if (ref)
493 494
		goto out;

495
	nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
496

497
	ret = nouveau_bo_validate(nvbo, false, false);
498 499 500
	if (ret == 0) {
		switch (bo->mem.mem_type) {
		case TTM_PL_VRAM:
501
			drm->gem.vram_available += bo->mem.size;
502 503
			break;
		case TTM_PL_TT:
504
			drm->gem.gart_available += bo->mem.size;
505 506 507 508 509 510
			break;
		default:
			break;
		}
	}

511
out:
512 513 514 515 516 517 518 519 520
	ttm_bo_unreserve(bo);
	return ret;
}

int
nouveau_bo_map(struct nouveau_bo *nvbo)
{
	int ret;

521
	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
522 523 524
	if (ret)
		return ret;

525
	ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
526

527 528 529 530 531 532 533
	ttm_bo_unreserve(&nvbo->bo);
	return ret;
}

void
nouveau_bo_unmap(struct nouveau_bo *nvbo)
{
534 535 536
	if (!nvbo)
		return;

537
	ttm_bo_kunmap(&nvbo->kmap);
538 539
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
void
nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
{
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
	int i;

	if (!ttm_dma)
		return;

	/* Don't waste time looping if the object is coherent */
	if (nvbo->force_coherent)
		return;

	for (i = 0; i < ttm_dma->ttm.num_pages; i++)
555 556
		dma_sync_single_for_device(drm->dev->dev,
					   ttm_dma->dma_address[i],
557
					   PAGE_SIZE, DMA_TO_DEVICE);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
}

void
nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
{
	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
	struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
	int i;

	if (!ttm_dma)
		return;

	/* Don't waste time looping if the object is coherent */
	if (nvbo->force_coherent)
		return;

	for (i = 0; i < ttm_dma->ttm.num_pages; i++)
575
		dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i],
576
					PAGE_SIZE, DMA_FROM_DEVICE);
577 578
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo)
{
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
	struct nouveau_bo *nvbo = nouveau_bo(bo);

	mutex_lock(&drm->ttm.io_reserve_mutex);
	list_move_tail(&nvbo->io_reserve_lru, &drm->ttm.io_reserve_lru);
	mutex_unlock(&drm->ttm.io_reserve_mutex);
}

void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo)
{
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
	struct nouveau_bo *nvbo = nouveau_bo(bo);

	mutex_lock(&drm->ttm.io_reserve_mutex);
	list_del_init(&nvbo->io_reserve_lru);
	mutex_unlock(&drm->ttm.io_reserve_mutex);
}

599 600
int
nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
601
		    bool no_wait_gpu)
602
{
603
	struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu };
604 605
	int ret;

606
	ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx);
607 608 609
	if (ret)
		return ret;

610 611
	nouveau_bo_sync_for_device(nvbo);

612 613 614
	return 0;
}

615 616 617 618 619
void
nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
{
	bool is_iomem;
	u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
620

621
	mem += index;
622

623 624 625 626 627 628 629 630 631 632 633
	if (is_iomem)
		iowrite16_native(val, (void __force __iomem *)mem);
	else
		*mem = val;
}

u32
nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
{
	bool is_iomem;
	u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
634

635
	mem += index;
636

637 638 639 640 641 642 643 644 645 646 647
	if (is_iomem)
		return ioread32_native((void __force __iomem *)mem);
	else
		return *mem;
}

void
nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
{
	bool is_iomem;
	u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
648

649
	mem += index;
650

651 652 653 654 655 656
	if (is_iomem)
		iowrite32_native(val, (void __force __iomem *)mem);
	else
		*mem = val;
}

657
static struct ttm_tt *
658
nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags)
659
{
D
Daniel Vetter 已提交
660
#if IS_ENABLED(CONFIG_AGP)
661
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
662

663
	if (drm->agp.bridge) {
664
		return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags);
665
	}
666
#endif
667

668
	return nouveau_sgdma_create_ttm(bo, page_flags);
669 670 671 672 673 674 675 676
}

static void
nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
{
	struct nouveau_bo *nvbo = nouveau_bo(bo);

	switch (bo->mem.mem_type) {
677
	case TTM_PL_VRAM:
678 679
		nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
					 TTM_PL_FLAG_SYSTEM);
680
		break;
681
	default:
682
		nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
683 684
		break;
	}
685 686

	*pl = nvbo->placement;
687 688
}

689
static int
690
nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
691
		     struct ttm_resource *reg)
692
{
693 694
	struct nouveau_mem *old_mem = nouveau_mem(&bo->mem);
	struct nouveau_mem *new_mem = nouveau_mem(reg);
695
	struct nvif_vmm *vmm = &drm->client.vmm.vmm;
696 697
	int ret;

698 699
	ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0,
			   old_mem->mem.size, &old_mem->vma[0]);
700 701 702
	if (ret)
		return ret;

703 704 705 706
	ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0,
			   new_mem->mem.size, &old_mem->vma[1]);
	if (ret)
		goto done;
707

708 709 710 711 712 713 714
	ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]);
	if (ret)
		goto done;

	ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]);
done:
	if (ret) {
715 716
		nvif_vmm_put(vmm, &old_mem->vma[1]);
		nvif_vmm_put(vmm, &old_mem->vma[0]);
717
	}
718 719 720
	return 0;
}

721 722
static int
nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
723
		     bool no_wait_gpu, struct ttm_resource *new_reg)
724
{
725
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
726
	struct nouveau_channel *chan = drm->ttm.chan;
727
	struct nouveau_cli *cli = (void *)chan->user.client;
728
	struct nouveau_fence *fence;
729 730
	int ret;

731
	/* create temporary vmas for the transfer and attach them to the
732
	 * old nvkm_mem node, these will get cleaned up after ttm has
733
	 * destroyed the ttm_resource
734
	 */
735
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
736
		ret = nouveau_bo_move_prep(drm, bo, new_reg);
737
		if (ret)
738
			return ret;
739 740
	}

741
	mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
742
	ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, intr);
743
	if (ret == 0) {
744
		ret = drm->ttm.move(chan, bo, &bo->mem, new_reg);
745 746 747
		if (ret == 0) {
			ret = nouveau_fence_new(chan, false, &fence);
			if (ret == 0) {
748 749
				ret = ttm_bo_move_accel_cleanup(bo,
								&fence->base,
750
								evict,
751
								new_reg);
752 753 754
				nouveau_fence_unref(&fence);
			}
		}
755
	}
756
	mutex_unlock(&cli->mutex);
757
	return ret;
758 759
}

760
void
761
nouveau_bo_move_init(struct nouveau_drm *drm)
762
{
763
	static const struct _method_table {
764
		const char *name;
765
		int engine;
766
		s32 oclass;
767 768
		int (*exec)(struct nouveau_channel *,
			    struct ttm_buffer_object *,
769
			    struct ttm_resource *, struct ttm_resource *);
770 771
		int (*init)(struct nouveau_channel *, u32 handle);
	} _methods[] = {
B
Ben Skeggs 已提交
772 773
		{  "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init },
		{  "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init },
774 775
		{  "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init },
		{  "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init },
776 777
		{  "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init },
		{  "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init },
778 779
		{  "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init },
		{  "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init },
780 781
		{  "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
		{  "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
782
		{  "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
783
		{  "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
784 785 786 787 788 789 790
		{ "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
		{ "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
		{  "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
		{ "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
		{  "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
		{  "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
		{  "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
791
		{},
792 793
	};
	const struct _method_table *mthd = _methods;
794 795 796 797
	const char *name = "CPU";
	int ret;

	do {
798
		struct nouveau_channel *chan;
799

800
		if (mthd->engine)
801 802 803 804 805 806
			chan = drm->cechan;
		else
			chan = drm->channel;
		if (chan == NULL)
			continue;

807
		ret = nvif_object_ctor(&chan->user, "ttmBoMove",
808 809 810
				       mthd->oclass | (mthd->engine << 16),
				       mthd->oclass, NULL, 0,
				       &drm->ttm.copy);
811
		if (ret == 0) {
812
			ret = mthd->init(chan, drm->ttm.copy.handle);
813
			if (ret) {
814
				nvif_object_dtor(&drm->ttm.copy);
815
				continue;
816
			}
817 818

			drm->ttm.move = mthd->exec;
819
			drm->ttm.chan = chan;
820 821
			name = mthd->name;
			break;
822 823 824
		}
	} while ((++mthd)->exec);

825
	NV_INFO(drm, "MM: using %s for buffer copies\n", name);
826 827
}

828 829
static int
nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
830
		      bool no_wait_gpu, struct ttm_resource *new_reg)
831
{
832
	struct ttm_operation_ctx ctx = { intr, no_wait_gpu };
833 834 835 836 837
	struct ttm_place placement_memtype = {
		.fpfn = 0,
		.lpfn = 0,
		.flags = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING
	};
838
	struct ttm_placement placement;
839
	struct ttm_resource tmp_reg;
840 841 842
	int ret;

	placement.num_placement = placement.num_busy_placement = 1;
843
	placement.placement = placement.busy_placement = &placement_memtype;
844

845 846
	tmp_reg = *new_reg;
	tmp_reg.mm_node = NULL;
847
	ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, &ctx);
848 849 850
	if (ret)
		return ret;

D
Dave Airlie 已提交
851
	ret = ttm_tt_bind(bo->bdev, bo->ttm, &tmp_reg, &ctx);
852 853 854
	if (ret)
		goto out;

855
	ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_reg);
856 857 858
	if (ret)
		goto out;

859
	ret = ttm_bo_move_ttm(bo, &ctx, new_reg);
860
out:
861
	ttm_resource_free(bo, &tmp_reg);
862 863 864 865 866
	return ret;
}

static int
nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
867
		      bool no_wait_gpu, struct ttm_resource *new_reg)
868
{
869
	struct ttm_operation_ctx ctx = { intr, no_wait_gpu };
870 871 872 873 874
	struct ttm_place placement_memtype = {
		.fpfn = 0,
		.lpfn = 0,
		.flags = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING
	};
875
	struct ttm_placement placement;
876
	struct ttm_resource tmp_reg;
877 878 879
	int ret;

	placement.num_placement = placement.num_busy_placement = 1;
880
	placement.placement = placement.busy_placement = &placement_memtype;
881

882 883
	tmp_reg = *new_reg;
	tmp_reg.mm_node = NULL;
884
	ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, &ctx);
885 886 887
	if (ret)
		return ret;

888
	ret = ttm_bo_move_ttm(bo, &ctx, &tmp_reg);
889 890 891
	if (ret)
		goto out;

892
	ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_reg);
893 894 895 896
	if (ret)
		goto out;

out:
897
	ttm_resource_free(bo, &tmp_reg);
898 899 900
	return ret;
}

901
static void
902
nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, bool evict,
903
		     struct ttm_resource *new_reg)
904
{
905
	struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL;
906
	struct nouveau_bo *nvbo = nouveau_bo(bo);
907
	struct nouveau_vma *vma;
908

909 910 911 912
	/* ttm can now (stupidly) pass the driver bos it didn't create... */
	if (bo->destroy != nouveau_bo_del_ttm)
		return;

913 914
	nouveau_bo_del_io_reserve_lru(bo);

915
	if (mem && new_reg->mem_type != TTM_PL_SYSTEM &&
916
	    mem->mem.page == nvbo->page) {
917
		list_for_each_entry(vma, &nvbo->vma_list, head) {
918
			nouveau_vma_map(vma, mem);
919 920 921
		}
	} else {
		list_for_each_entry(vma, &nvbo->vma_list, head) {
922
			WARN_ON(ttm_bo_wait(bo, false, false));
923
			nouveau_vma_unmap(vma);
924
		}
925
	}
926 927 928 929 930 931 932 933

	if (new_reg) {
		if (new_reg->mm_node)
			nvbo->offset = (new_reg->start << PAGE_SHIFT);
		else
			nvbo->offset = 0;
	}

934 935
}

936
static int
937
nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_resource *new_reg,
938
		   struct nouveau_drm_tile **new_tile)
939
{
940 941
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
	struct drm_device *dev = drm->dev;
942
	struct nouveau_bo *nvbo = nouveau_bo(bo);
943
	u64 offset = new_reg->start << PAGE_SHIFT;
944

945
	*new_tile = NULL;
946
	if (new_reg->mem_type != TTM_PL_VRAM)
947 948
		return 0;

949
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
950
		*new_tile = nv10_bo_set_tiling(dev, offset, new_reg->size,
951
					       nvbo->mode, nvbo->zeta);
952 953
	}

954 955 956 957 958
	return 0;
}

static void
nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
959 960
		      struct nouveau_drm_tile *new_tile,
		      struct nouveau_drm_tile **old_tile)
961
{
962 963
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
	struct drm_device *dev = drm->dev;
964
	struct dma_fence *fence = dma_resv_get_excl(bo->base.resv);
965

966
	nv10_bo_put_tile_region(dev, *old_tile, fence);
967
	*old_tile = new_tile;
968 969 970
}

static int
971 972
nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
		struct ttm_operation_ctx *ctx,
973
		struct ttm_resource *new_reg)
974
{
975
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
976
	struct nouveau_bo *nvbo = nouveau_bo(bo);
977
	struct ttm_resource *old_reg = &bo->mem;
978
	struct nouveau_drm_tile *new_tile = NULL;
979 980
	int ret = 0;

981
	ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
982 983 984
	if (ret)
		return ret;

985 986 987
	if (nvbo->pin_refcnt)
		NV_WARN(drm, "Moving pinned object %p!\n", nvbo);

988
	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
989
		ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile);
990 991 992
		if (ret)
			return ret;
	}
993 994

	/* Fake bo copy. */
995
	if (old_reg->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
996
		BUG_ON(bo->mem.mm_node != NULL);
997 998
		bo->mem = *new_reg;
		new_reg->mm_node = NULL;
999
		goto out;
1000 1001
	}

1002
	/* Hardware assisted copy. */
1003
	if (drm->ttm.move) {
1004
		if (new_reg->mem_type == TTM_PL_SYSTEM)
1005 1006 1007
			ret = nouveau_bo_move_flipd(bo, evict,
						    ctx->interruptible,
						    ctx->no_wait_gpu, new_reg);
1008
		else if (old_reg->mem_type == TTM_PL_SYSTEM)
1009 1010 1011
			ret = nouveau_bo_move_flips(bo, evict,
						    ctx->interruptible,
						    ctx->no_wait_gpu, new_reg);
1012
		else
1013 1014 1015
			ret = nouveau_bo_move_m2mf(bo, evict,
						   ctx->interruptible,
						   ctx->no_wait_gpu, new_reg);
1016 1017 1018
		if (!ret)
			goto out;
	}
1019 1020

	/* Fallback to software copy. */
1021
	ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
1022
	if (ret == 0)
1023
		ret = ttm_bo_move_memcpy(bo, ctx, new_reg);
1024 1025

out:
1026
	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
1027 1028 1029 1030 1031
		if (ret)
			nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
		else
			nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
	}
1032 1033

	return ret;
1034 1035 1036 1037 1038
}

static int
nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
{
1039 1040
	struct nouveau_bo *nvbo = nouveau_bo(bo);

1041
	return drm_vma_node_verify_access(&nvbo->bo.base.vma_node,
D
David Herrmann 已提交
1042
					  filp->private_data);
1043 1044
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
static void
nouveau_ttm_io_mem_free_locked(struct nouveau_drm *drm,
			       struct ttm_resource *reg)
{
	struct nouveau_mem *mem = nouveau_mem(reg);

	if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
		switch (reg->mem_type) {
		case TTM_PL_TT:
			if (mem->kind)
				nvif_object_unmap_handle(&mem->mem.object);
			break;
		case TTM_PL_VRAM:
			nvif_object_unmap_handle(&mem->mem.object);
			break;
		default:
			break;
		}
	}
}

1066
static int
1067
nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *reg)
1068
{
1069
	struct nouveau_drm *drm = nouveau_bdev(bdev);
1070
	struct nvkm_device *device = nvxx_device(&drm->client.device);
1071
	struct nouveau_mem *mem = nouveau_mem(reg);
1072
	int ret;
1073

1074 1075
	mutex_lock(&drm->ttm.io_reserve_mutex);
retry:
1076
	switch (reg->mem_type) {
1077 1078
	case TTM_PL_SYSTEM:
		/* System memory */
1079 1080
		ret = 0;
		goto out;
1081
	case TTM_PL_TT:
D
Daniel Vetter 已提交
1082
#if IS_ENABLED(CONFIG_AGP)
1083
		if (drm->agp.bridge) {
1084 1085 1086
			reg->bus.offset = reg->start << PAGE_SHIFT;
			reg->bus.base = drm->agp.base;
			reg->bus.is_iomem = !drm->agp.cma;
1087 1088
		}
#endif
1089 1090
		if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 ||
		    !mem->kind) {
1091
			/* untiled */
1092
			ret = 0;
1093
			break;
1094
		}
1095
		fallthrough;	/* tiled memory */
1096
	case TTM_PL_VRAM:
1097 1098 1099
		reg->bus.offset = reg->start << PAGE_SHIFT;
		reg->bus.base = device->func->resource_addr(device, 1);
		reg->bus.is_iomem = true;
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
		if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
			union {
				struct nv50_mem_map_v0 nv50;
				struct gf100_mem_map_v0 gf100;
			} args;
			u64 handle, length;
			u32 argc = 0;

			switch (mem->mem.object.oclass) {
			case NVIF_CLASS_MEM_NV50:
				args.nv50.version = 0;
				args.nv50.ro = 0;
				args.nv50.kind = mem->kind;
				args.nv50.comp = mem->comp;
1114
				argc = sizeof(args.nv50);
1115 1116 1117 1118 1119
				break;
			case NVIF_CLASS_MEM_GF100:
				args.gf100.version = 0;
				args.gf100.ro = 0;
				args.gf100.kind = mem->kind;
1120
				argc = sizeof(args.gf100);
1121 1122 1123 1124 1125 1126 1127
				break;
			default:
				WARN_ON(1);
				break;
			}

			ret = nvif_object_map_handle(&mem->mem.object,
1128
						     &args, argc,
1129
						     &handle, &length);
1130 1131
			if (ret != 1) {
				if (WARN_ON(ret == 0))
1132 1133
					ret = -EINVAL;
				goto out;
1134
			}
1135 1136 1137

			reg->bus.base = 0;
			reg->bus.offset = handle;
1138
			ret = 0;
1139
		}
1140 1141
		break;
	default:
1142
		ret = -EINVAL;
1143
	}
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

out:
	if (ret == -ENOSPC) {
		struct nouveau_bo *nvbo;

		nvbo = list_first_entry_or_null(&drm->ttm.io_reserve_lru,
						typeof(*nvbo),
						io_reserve_lru);
		if (nvbo) {
			list_del_init(&nvbo->io_reserve_lru);
			drm_vma_node_unmap(&nvbo->bo.base.vma_node,
					   bdev->dev_mapping);
			nouveau_ttm_io_mem_free_locked(drm, &nvbo->bo.mem);
			goto retry;
		}

	}
	mutex_unlock(&drm->ttm.io_reserve_mutex);
	return ret;
1163 1164 1165
}

static void
1166
nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_resource *reg)
1167
{
1168
	struct nouveau_drm *drm = nouveau_bdev(bdev);
1169

1170 1171 1172
	mutex_lock(&drm->ttm.io_reserve_mutex);
	nouveau_ttm_io_mem_free_locked(drm, reg);
	mutex_unlock(&drm->ttm.io_reserve_mutex);
1173 1174 1175 1176 1177
}

static int
nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
{
1178
	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1179
	struct nouveau_bo *nvbo = nouveau_bo(bo);
1180
	struct nvkm_device *device = nvxx_device(&drm->client.device);
1181
	u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT;
1182
	int i, ret;
1183 1184 1185 1186 1187

	/* as long as the bo isn't in vram, and isn't tiled, we've got
	 * nothing to do here.
	 */
	if (bo->mem.mem_type != TTM_PL_VRAM) {
1188
		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA ||
1189
		    !nvbo->kind)
1190
			return 0;
1191 1192 1193 1194 1195 1196 1197 1198 1199

		if (bo->mem.mem_type == TTM_PL_SYSTEM) {
			nouveau_bo_placement_set(nvbo, TTM_PL_TT, 0);

			ret = nouveau_bo_validate(nvbo, false, false);
			if (ret)
				return ret;
		}
		return 0;
1200 1201 1202
	}

	/* make sure bo is in mappable vram */
1203
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
1204
	    bo->mem.start + bo->mem.num_pages < mappable)
1205 1206
		return 0;

1207 1208 1209 1210 1211 1212 1213 1214 1215
	for (i = 0; i < nvbo->placement.num_placement; ++i) {
		nvbo->placements[i].fpfn = 0;
		nvbo->placements[i].lpfn = mappable;
	}

	for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
		nvbo->busy_placements[i].fpfn = 0;
		nvbo->busy_placements[i].lpfn = mappable;
	}
1216

1217
	nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
1218
	return nouveau_bo_validate(nvbo, false, false);
1219 1220
}

1221
static int
D
Dave Airlie 已提交
1222 1223
nouveau_ttm_tt_populate(struct ttm_bo_device *bdev,
			struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
1224
{
1225
	struct ttm_dma_tt *ttm_dma = (void *)ttm;
1226
	struct nouveau_drm *drm;
1227
	struct device *dev;
D
Dave Airlie 已提交
1228
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1229 1230 1231 1232

	if (ttm->state != tt_unpopulated)
		return 0;

D
Dave Airlie 已提交
1233 1234 1235 1236 1237 1238 1239 1240
	if (slave && ttm->sg) {
		/* make userspace faulting work */
		drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
						 ttm_dma->dma_address, ttm->num_pages);
		ttm->state = tt_unbound;
		return 0;
	}

D
Dave Airlie 已提交
1241
	drm = nouveau_bdev(bdev);
1242
	dev = drm->dev->dev;
1243

D
Daniel Vetter 已提交
1244
#if IS_ENABLED(CONFIG_AGP)
1245
	if (drm->agp.bridge) {
D
Dave Airlie 已提交
1246
		return ttm_agp_tt_populate(bdev, ttm, ctx);
J
Jerome Glisse 已提交
1247 1248 1249
	}
#endif

1250
#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
1251
	if (swiotlb_nr_tbl()) {
1252
		return ttm_dma_populate((void *)ttm, dev, ctx);
1253 1254
	}
#endif
1255
	return ttm_populate_and_map_pages(dev, ttm_dma, ctx);
1256 1257 1258
}

static void
D
Dave Airlie 已提交
1259 1260
nouveau_ttm_tt_unpopulate(struct ttm_bo_device *bdev,
			  struct ttm_tt *ttm)
1261
{
1262
	struct ttm_dma_tt *ttm_dma = (void *)ttm;
1263
	struct nouveau_drm *drm;
1264
	struct device *dev;
D
Dave Airlie 已提交
1265 1266 1267 1268
	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);

	if (slave)
		return;
1269

D
Dave Airlie 已提交
1270
	drm = nouveau_bdev(bdev);
1271
	dev = drm->dev->dev;
1272

D
Daniel Vetter 已提交
1273
#if IS_ENABLED(CONFIG_AGP)
1274
	if (drm->agp.bridge) {
D
Dave Airlie 已提交
1275
		ttm_agp_tt_unpopulate(bdev, ttm);
J
Jerome Glisse 已提交
1276 1277 1278 1279
		return;
	}
#endif

1280
#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
1281
	if (swiotlb_nr_tbl()) {
1282
		ttm_dma_unpopulate((void *)ttm, dev);
1283 1284 1285 1286
		return;
	}
#endif

1287
	ttm_unmap_and_unpopulate_pages(dev, ttm_dma);
1288 1289
}

1290
void
1291
nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
1292
{
1293
	struct dma_resv *resv = nvbo->bo.base.resv;
1294

1295
	if (exclusive)
1296
		dma_resv_add_excl_fence(resv, &fence->base);
1297
	else if (fence)
1298
		dma_resv_add_shared_fence(resv, &fence->base);
1299 1300
}

1301
struct ttm_bo_driver nouveau_bo_driver = {
1302
	.ttm_tt_create = &nouveau_ttm_tt_create,
1303 1304
	.ttm_tt_populate = &nouveau_ttm_tt_populate,
	.ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
1305
	.eviction_valuable = ttm_bo_eviction_valuable,
1306
	.evict_flags = nouveau_bo_evict_flags,
1307
	.move_notify = nouveau_bo_move_ntfy,
1308 1309
	.move = nouveau_bo_move,
	.verify_access = nouveau_bo_verify_access,
1310 1311 1312
	.fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
	.io_mem_reserve = &nouveau_ttm_io_mem_reserve,
	.io_mem_free = &nouveau_ttm_io_mem_free,
1313
};