xref: /linux/drivers/gpu/drm/nouveau/nouveau_bo.c (revision cbaf84e73811ed0ff7ff6d7f52b73fd7ed082d65)
1 /*
2  * Copyright 2007 Dave Airlied
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 /*
25  * Authors: Dave Airlied <airlied@linux.ie>
26  *	    Ben Skeggs   <darktama@iinet.net.au>
27  *	    Jeremy Kolb  <jkolb@brandeis.edu>
28  */
29 
30 #include <linux/dma-mapping.h>
31 #include <drm/ttm/ttm_tt.h>
32 
33 #include "nouveau_drv.h"
34 #include "nouveau_chan.h"
35 #include "nouveau_fence.h"
36 
37 #include "nouveau_bo.h"
38 #include "nouveau_ttm.h"
39 #include "nouveau_gem.h"
40 #include "nouveau_mem.h"
41 #include "nouveau_vmm.h"
42 
43 #include <nvif/class.h>
44 #include <nvif/if500b.h>
45 #include <nvif/if900b.h>
46 
47 static int nouveau_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
48 			       struct ttm_resource *reg);
49 static void nouveau_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm);
50 
51 /*
52  * NV10-NV40 tiling helpers
53  */
54 
55 static void
56 nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
57 			   u32 addr, u32 size, u32 pitch, u32 flags)
58 {
59 	struct nouveau_drm *drm = nouveau_drm(dev);
60 	int i = reg - drm->tile.reg;
61 	struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
62 	struct nvkm_fb_tile *tile = &fb->tile.region[i];
63 
64 	nouveau_fence_unref(&reg->fence);
65 
66 	if (tile->pitch)
67 		nvkm_fb_tile_fini(fb, i, tile);
68 
69 	if (pitch)
70 		nvkm_fb_tile_init(fb, i, addr, size, pitch, flags, tile);
71 
72 	nvkm_fb_tile_prog(fb, i, tile);
73 }
74 
75 static struct nouveau_drm_tile *
76 nv10_bo_get_tile_region(struct drm_device *dev, int i)
77 {
78 	struct nouveau_drm *drm = nouveau_drm(dev);
79 	struct nouveau_drm_tile *tile = &drm->tile.reg[i];
80 
81 	spin_lock(&drm->tile.lock);
82 
83 	if (!tile->used &&
84 	    (!tile->fence || nouveau_fence_done(tile->fence)))
85 		tile->used = true;
86 	else
87 		tile = NULL;
88 
89 	spin_unlock(&drm->tile.lock);
90 	return tile;
91 }
92 
93 static void
94 nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
95 			struct dma_fence *fence)
96 {
97 	struct nouveau_drm *drm = nouveau_drm(dev);
98 
99 	if (tile) {
100 		spin_lock(&drm->tile.lock);
101 		tile->fence = (struct nouveau_fence *)dma_fence_get(fence);
102 		tile->used = false;
103 		spin_unlock(&drm->tile.lock);
104 	}
105 }
106 
107 static struct nouveau_drm_tile *
108 nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
109 		   u32 size, u32 pitch, u32 zeta)
110 {
111 	struct nouveau_drm *drm = nouveau_drm(dev);
112 	struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
113 	struct nouveau_drm_tile *tile, *found = NULL;
114 	int i;
115 
116 	for (i = 0; i < fb->tile.regions; i++) {
117 		tile = nv10_bo_get_tile_region(dev, i);
118 
119 		if (pitch && !found) {
120 			found = tile;
121 			continue;
122 
123 		} else if (tile && fb->tile.region[i].pitch) {
124 			/* Kill an unused tile region. */
125 			nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
126 		}
127 
128 		nv10_bo_put_tile_region(dev, tile, NULL);
129 	}
130 
131 	if (found)
132 		nv10_bo_update_tile_region(dev, found, addr, size, pitch, zeta);
133 	return found;
134 }
135 
136 static void
137 nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
138 {
139 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
140 	struct drm_device *dev = drm->dev;
141 	struct nouveau_bo *nvbo = nouveau_bo(bo);
142 
143 	WARN_ON(nvbo->bo.pin_count > 0);
144 	nouveau_bo_del_io_reserve_lru(bo);
145 	nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
146 
147 	/*
148 	 * If nouveau_bo_new() allocated this buffer, the GEM object was never
149 	 * initialized, so don't attempt to release it.
150 	 */
151 	if (bo->base.dev) {
152 		/* Gem objects not being shared with other VMs get their
153 		 * dma_resv from a root GEM object.
154 		 */
155 		if (nvbo->no_share)
156 			drm_gem_object_put(nvbo->r_obj);
157 
158 		drm_gem_object_release(&bo->base);
159 	} else {
160 		dma_resv_fini(&bo->base._resv);
161 	}
162 
163 	kfree(nvbo);
164 }
165 
166 static inline u64
167 roundup_64(u64 x, u32 y)
168 {
169 	x += y - 1;
170 	do_div(x, y);
171 	return x * y;
172 }
173 
174 static void
175 nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *align, u64 *size)
176 {
177 	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
178 	struct nvif_device *device = &drm->client.device;
179 
180 	if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
181 		if (nvbo->mode) {
182 			if (device->info.chipset >= 0x40) {
183 				*align = 65536;
184 				*size = roundup_64(*size, 64 * nvbo->mode);
185 
186 			} else if (device->info.chipset >= 0x30) {
187 				*align = 32768;
188 				*size = roundup_64(*size, 64 * nvbo->mode);
189 
190 			} else if (device->info.chipset >= 0x20) {
191 				*align = 16384;
192 				*size = roundup_64(*size, 64 * nvbo->mode);
193 
194 			} else if (device->info.chipset >= 0x10) {
195 				*align = 16384;
196 				*size = roundup_64(*size, 32 * nvbo->mode);
197 			}
198 		}
199 	} else {
200 		*size = roundup_64(*size, (1 << nvbo->page));
201 		*align = max((1 <<  nvbo->page), *align);
202 	}
203 
204 	*size = roundup_64(*size, PAGE_SIZE);
205 }
206 
207 struct nouveau_bo *
208 nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 domain,
209 		 u32 tile_mode, u32 tile_flags, bool internal)
210 {
211 	struct nouveau_drm *drm = cli->drm;
212 	struct nouveau_bo *nvbo;
213 	struct nvif_mmu *mmu = &cli->mmu;
214 	struct nvif_vmm *vmm = &nouveau_cli_vmm(cli)->vmm;
215 	int i, pi = -1;
216 
217 	if (!*size) {
218 		NV_WARN(drm, "skipped size %016llx\n", *size);
219 		return ERR_PTR(-EINVAL);
220 	}
221 
222 	nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
223 	if (!nvbo)
224 		return ERR_PTR(-ENOMEM);
225 
226 	INIT_LIST_HEAD(&nvbo->head);
227 	INIT_LIST_HEAD(&nvbo->entry);
228 	INIT_LIST_HEAD(&nvbo->vma_list);
229 	nvbo->bo.bdev = &drm->ttm.bdev;
230 
231 	/* This is confusing, and doesn't actually mean we want an uncached
232 	 * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated
233 	 * into in nouveau_gem_new().
234 	 */
235 	if (domain & NOUVEAU_GEM_DOMAIN_COHERENT) {
236 		/* Determine if we can get a cache-coherent map, forcing
237 		 * uncached mapping if we can't.
238 		 */
239 		if (!nouveau_drm_use_coherent_gpu_mapping(drm))
240 			nvbo->force_coherent = true;
241 	}
242 
243 	nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG);
244 	if (!nouveau_cli_uvmm(cli) || internal) {
245 		/* for BO noVM allocs, don't assign kinds */
246 		if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) {
247 			nvbo->kind = (tile_flags & 0x0000ff00) >> 8;
248 			if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
249 				kfree(nvbo);
250 				return ERR_PTR(-EINVAL);
251 			}
252 
253 			nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind;
254 		} else if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
255 			nvbo->kind = (tile_flags & 0x00007f00) >> 8;
256 			nvbo->comp = (tile_flags & 0x00030000) >> 16;
257 			if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
258 				kfree(nvbo);
259 				return ERR_PTR(-EINVAL);
260 			}
261 		} else {
262 			nvbo->zeta = (tile_flags & 0x00000007);
263 		}
264 		nvbo->mode = tile_mode;
265 
266 		/* Determine the desirable target GPU page size for the buffer. */
267 		for (i = 0; i < vmm->page_nr; i++) {
268 			/* Because we cannot currently allow VMM maps to fail
269 			 * during buffer migration, we need to determine page
270 			 * size for the buffer up-front, and pre-allocate its
271 			 * page tables.
272 			 *
273 			 * Skip page sizes that can't support needed domains.
274 			 */
275 			if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE &&
276 			    (domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram)
277 				continue;
278 			if ((domain & NOUVEAU_GEM_DOMAIN_GART) &&
279 			    (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT))
280 				continue;
281 
282 			/* Select this page size if it's the first that supports
283 			 * the potential memory domains, or when it's compatible
284 			 * with the requested compression settings.
285 			 */
286 			if (pi < 0 || !nvbo->comp || vmm->page[i].comp)
287 				pi = i;
288 
289 			/* Stop once the buffer is larger than the current page size. */
290 			if (*size >= 1ULL << vmm->page[i].shift)
291 				break;
292 		}
293 
294 		if (WARN_ON(pi < 0)) {
295 			kfree(nvbo);
296 			return ERR_PTR(-EINVAL);
297 		}
298 
299 		/* Disable compression if suitable settings couldn't be found. */
300 		if (nvbo->comp && !vmm->page[pi].comp) {
301 			if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
302 				nvbo->kind = mmu->kind[nvbo->kind];
303 			nvbo->comp = 0;
304 		}
305 		nvbo->page = vmm->page[pi].shift;
306 	} else {
307 		/* reject other tile flags when in VM mode. */
308 		if (tile_mode)
309 			return ERR_PTR(-EINVAL);
310 		if (tile_flags & ~NOUVEAU_GEM_TILE_NONCONTIG)
311 			return ERR_PTR(-EINVAL);
312 
313 		/* Determine the desirable target GPU page size for the buffer. */
314 		for (i = 0; i < vmm->page_nr; i++) {
315 			/* Because we cannot currently allow VMM maps to fail
316 			 * during buffer migration, we need to determine page
317 			 * size for the buffer up-front, and pre-allocate its
318 			 * page tables.
319 			 *
320 			 * Skip page sizes that can't support needed domains.
321 			 */
322 			if ((domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram)
323 				continue;
324 			if ((domain & NOUVEAU_GEM_DOMAIN_GART) &&
325 			    (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT))
326 				continue;
327 
328 			if (pi < 0)
329 				pi = i;
330 			/* Stop once the buffer is larger than the current page size. */
331 			if (*size >= 1ULL << vmm->page[i].shift)
332 				break;
333 		}
334 		if (WARN_ON(pi < 0)) {
335 			kfree(nvbo);
336 			return ERR_PTR(-EINVAL);
337 		}
338 		nvbo->page = vmm->page[pi].shift;
339 	}
340 
341 	nouveau_bo_fixup_align(nvbo, align, size);
342 
343 	return nvbo;
344 }
345 
346 int
347 nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
348 		struct sg_table *sg, struct dma_resv *robj)
349 {
350 	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
351 	int ret;
352 	struct ttm_operation_ctx ctx = {
353 		.interruptible = false,
354 		.no_wait_gpu = false,
355 		.resv = robj,
356 	};
357 
358 	nouveau_bo_placement_set(nvbo, domain, 0);
359 	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
360 
361 	ret = ttm_bo_init_reserved(nvbo->bo.bdev, &nvbo->bo, type,
362 				   &nvbo->placement, align >> PAGE_SHIFT, &ctx,
363 				   sg, robj, nouveau_bo_del_ttm);
364 	if (ret) {
365 		/* ttm will call nouveau_bo_del_ttm if it fails.. */
366 		return ret;
367 	}
368 
369 	if (!robj)
370 		ttm_bo_unreserve(&nvbo->bo);
371 
372 	return 0;
373 }
374 
375 int
376 nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align,
377 	       uint32_t domain, uint32_t tile_mode, uint32_t tile_flags,
378 	       struct sg_table *sg, struct dma_resv *robj,
379 	       struct nouveau_bo **pnvbo)
380 {
381 	struct nouveau_bo *nvbo;
382 	int ret;
383 
384 	nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode,
385 				tile_flags, true);
386 	if (IS_ERR(nvbo))
387 		return PTR_ERR(nvbo);
388 
389 	nvbo->bo.base.size = size;
390 	dma_resv_init(&nvbo->bo.base._resv);
391 	drm_vma_node_reset(&nvbo->bo.base.vma_node);
392 
393 	/* This must be called before ttm_bo_init_reserved(). Subsequent
394 	 * bo_move() callbacks might already iterate the GEMs GPUVA list.
395 	 */
396 	drm_gem_gpuva_init(&nvbo->bo.base);
397 
398 	ret = nouveau_bo_init(nvbo, size, align, domain, sg, robj);
399 	if (ret)
400 		return ret;
401 
402 	*pnvbo = nvbo;
403 	return 0;
404 }
405 
406 static void
407 set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t domain)
408 {
409 	*n = 0;
410 
411 	if (domain & NOUVEAU_GEM_DOMAIN_VRAM) {
412 		pl[*n].mem_type = TTM_PL_VRAM;
413 		pl[*n].flags = 0;
414 		(*n)++;
415 	}
416 	if (domain & NOUVEAU_GEM_DOMAIN_GART) {
417 		pl[*n].mem_type = TTM_PL_TT;
418 		pl[*n].flags = 0;
419 		(*n)++;
420 	}
421 	if (domain & NOUVEAU_GEM_DOMAIN_CPU) {
422 		pl[*n].mem_type = TTM_PL_SYSTEM;
423 		pl[(*n)++].flags = 0;
424 	}
425 }
426 
427 static void
428 set_placement_range(struct nouveau_bo *nvbo, uint32_t domain)
429 {
430 	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
431 	u64 vram_size = drm->client.device.info.ram_size;
432 	unsigned i, fpfn, lpfn;
433 
434 	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
435 	    nvbo->mode && (domain & NOUVEAU_GEM_DOMAIN_VRAM) &&
436 	    nvbo->bo.base.size < vram_size / 4) {
437 		/*
438 		 * Make sure that the color and depth buffers are handled
439 		 * by independent memory controller units. Up to a 9x
440 		 * speed up when alpha-blending and depth-test are enabled
441 		 * at the same time.
442 		 */
443 		if (nvbo->zeta) {
444 			fpfn = (vram_size / 2) >> PAGE_SHIFT;
445 			lpfn = ~0;
446 		} else {
447 			fpfn = 0;
448 			lpfn = (vram_size / 2) >> PAGE_SHIFT;
449 		}
450 		for (i = 0; i < nvbo->placement.num_placement; ++i) {
451 			nvbo->placements[i].fpfn = fpfn;
452 			nvbo->placements[i].lpfn = lpfn;
453 		}
454 		for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
455 			nvbo->busy_placements[i].fpfn = fpfn;
456 			nvbo->busy_placements[i].lpfn = lpfn;
457 		}
458 	}
459 }
460 
461 void
462 nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t domain,
463 			 uint32_t busy)
464 {
465 	struct ttm_placement *pl = &nvbo->placement;
466 
467 	pl->placement = nvbo->placements;
468 	set_placement_list(nvbo->placements, &pl->num_placement, domain);
469 
470 	pl->busy_placement = nvbo->busy_placements;
471 	set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
472 			   domain | busy);
473 
474 	set_placement_range(nvbo, domain);
475 }
476 
477 int
478 nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t domain, bool contig)
479 {
480 	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
481 	struct ttm_buffer_object *bo = &nvbo->bo;
482 	bool force = false, evict = false;
483 	int ret;
484 
485 	ret = ttm_bo_reserve(bo, false, false, NULL);
486 	if (ret)
487 		return ret;
488 
489 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
490 	    domain == NOUVEAU_GEM_DOMAIN_VRAM && contig) {
491 		if (!nvbo->contig) {
492 			nvbo->contig = true;
493 			force = true;
494 			evict = true;
495 		}
496 	}
497 
498 	if (nvbo->bo.pin_count) {
499 		bool error = evict;
500 
501 		switch (bo->resource->mem_type) {
502 		case TTM_PL_VRAM:
503 			error |= !(domain & NOUVEAU_GEM_DOMAIN_VRAM);
504 			break;
505 		case TTM_PL_TT:
506 			error |= !(domain & NOUVEAU_GEM_DOMAIN_GART);
507 			break;
508 		default:
509 			break;
510 		}
511 
512 		if (error) {
513 			NV_ERROR(drm, "bo %p pinned elsewhere: "
514 				      "0x%08x vs 0x%08x\n", bo,
515 				 bo->resource->mem_type, domain);
516 			ret = -EBUSY;
517 		}
518 		ttm_bo_pin(&nvbo->bo);
519 		goto out;
520 	}
521 
522 	if (evict) {
523 		nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
524 		ret = nouveau_bo_validate(nvbo, false, false);
525 		if (ret)
526 			goto out;
527 	}
528 
529 	nouveau_bo_placement_set(nvbo, domain, 0);
530 	ret = nouveau_bo_validate(nvbo, false, false);
531 	if (ret)
532 		goto out;
533 
534 	ttm_bo_pin(&nvbo->bo);
535 
536 	switch (bo->resource->mem_type) {
537 	case TTM_PL_VRAM:
538 		drm->gem.vram_available -= bo->base.size;
539 		break;
540 	case TTM_PL_TT:
541 		drm->gem.gart_available -= bo->base.size;
542 		break;
543 	default:
544 		break;
545 	}
546 
547 out:
548 	if (force && ret)
549 		nvbo->contig = false;
550 	ttm_bo_unreserve(bo);
551 	return ret;
552 }
553 
554 int
555 nouveau_bo_unpin(struct nouveau_bo *nvbo)
556 {
557 	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
558 	struct ttm_buffer_object *bo = &nvbo->bo;
559 	int ret;
560 
561 	ret = ttm_bo_reserve(bo, false, false, NULL);
562 	if (ret)
563 		return ret;
564 
565 	ttm_bo_unpin(&nvbo->bo);
566 	if (!nvbo->bo.pin_count) {
567 		switch (bo->resource->mem_type) {
568 		case TTM_PL_VRAM:
569 			drm->gem.vram_available += bo->base.size;
570 			break;
571 		case TTM_PL_TT:
572 			drm->gem.gart_available += bo->base.size;
573 			break;
574 		default:
575 			break;
576 		}
577 	}
578 
579 	ttm_bo_unreserve(bo);
580 	return 0;
581 }
582 
583 int
584 nouveau_bo_map(struct nouveau_bo *nvbo)
585 {
586 	int ret;
587 
588 	ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
589 	if (ret)
590 		return ret;
591 
592 	ret = ttm_bo_kmap(&nvbo->bo, 0, PFN_UP(nvbo->bo.base.size), &nvbo->kmap);
593 
594 	ttm_bo_unreserve(&nvbo->bo);
595 	return ret;
596 }
597 
598 void
599 nouveau_bo_unmap(struct nouveau_bo *nvbo)
600 {
601 	if (!nvbo)
602 		return;
603 
604 	ttm_bo_kunmap(&nvbo->kmap);
605 }
606 
607 void
608 nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
609 {
610 	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
611 	struct ttm_tt *ttm_dma = (struct ttm_tt *)nvbo->bo.ttm;
612 	int i, j;
613 
614 	if (!ttm_dma || !ttm_dma->dma_address)
615 		return;
616 	if (!ttm_dma->pages) {
617 		NV_DEBUG(drm, "ttm_dma 0x%p: pages NULL\n", ttm_dma);
618 		return;
619 	}
620 
621 	/* Don't waste time looping if the object is coherent */
622 	if (nvbo->force_coherent)
623 		return;
624 
625 	i = 0;
626 	while (i < ttm_dma->num_pages) {
627 		struct page *p = ttm_dma->pages[i];
628 		size_t num_pages = 1;
629 
630 		for (j = i + 1; j < ttm_dma->num_pages; ++j) {
631 			if (++p != ttm_dma->pages[j])
632 				break;
633 
634 			++num_pages;
635 		}
636 		dma_sync_single_for_device(drm->dev->dev,
637 					   ttm_dma->dma_address[i],
638 					   num_pages * PAGE_SIZE, DMA_TO_DEVICE);
639 		i += num_pages;
640 	}
641 }
642 
643 void
644 nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
645 {
646 	struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
647 	struct ttm_tt *ttm_dma = (struct ttm_tt *)nvbo->bo.ttm;
648 	int i, j;
649 
650 	if (!ttm_dma || !ttm_dma->dma_address)
651 		return;
652 	if (!ttm_dma->pages) {
653 		NV_DEBUG(drm, "ttm_dma 0x%p: pages NULL\n", ttm_dma);
654 		return;
655 	}
656 
657 	/* Don't waste time looping if the object is coherent */
658 	if (nvbo->force_coherent)
659 		return;
660 
661 	i = 0;
662 	while (i < ttm_dma->num_pages) {
663 		struct page *p = ttm_dma->pages[i];
664 		size_t num_pages = 1;
665 
666 		for (j = i + 1; j < ttm_dma->num_pages; ++j) {
667 			if (++p != ttm_dma->pages[j])
668 				break;
669 
670 			++num_pages;
671 		}
672 
673 		dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i],
674 					num_pages * PAGE_SIZE, DMA_FROM_DEVICE);
675 		i += num_pages;
676 	}
677 }
678 
679 void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo)
680 {
681 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
682 	struct nouveau_bo *nvbo = nouveau_bo(bo);
683 
684 	mutex_lock(&drm->ttm.io_reserve_mutex);
685 	list_move_tail(&nvbo->io_reserve_lru, &drm->ttm.io_reserve_lru);
686 	mutex_unlock(&drm->ttm.io_reserve_mutex);
687 }
688 
689 void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo)
690 {
691 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
692 	struct nouveau_bo *nvbo = nouveau_bo(bo);
693 
694 	mutex_lock(&drm->ttm.io_reserve_mutex);
695 	list_del_init(&nvbo->io_reserve_lru);
696 	mutex_unlock(&drm->ttm.io_reserve_mutex);
697 }
698 
699 int
700 nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
701 		    bool no_wait_gpu)
702 {
703 	struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu };
704 	int ret;
705 
706 	ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx);
707 	if (ret)
708 		return ret;
709 
710 	nouveau_bo_sync_for_device(nvbo);
711 
712 	return 0;
713 }
714 
715 void
716 nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
717 {
718 	bool is_iomem;
719 	u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
720 
721 	mem += index;
722 
723 	if (is_iomem)
724 		iowrite16_native(val, (void __force __iomem *)mem);
725 	else
726 		*mem = val;
727 }
728 
729 u32
730 nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
731 {
732 	bool is_iomem;
733 	u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
734 
735 	mem += index;
736 
737 	if (is_iomem)
738 		return ioread32_native((void __force __iomem *)mem);
739 	else
740 		return *mem;
741 }
742 
743 void
744 nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
745 {
746 	bool is_iomem;
747 	u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
748 
749 	mem += index;
750 
751 	if (is_iomem)
752 		iowrite32_native(val, (void __force __iomem *)mem);
753 	else
754 		*mem = val;
755 }
756 
757 static struct ttm_tt *
758 nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags)
759 {
760 #if IS_ENABLED(CONFIG_AGP)
761 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
762 
763 	if (drm->agp.bridge) {
764 		return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags);
765 	}
766 #endif
767 
768 	return nouveau_sgdma_create_ttm(bo, page_flags);
769 }
770 
771 static int
772 nouveau_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
773 		    struct ttm_resource *reg)
774 {
775 #if IS_ENABLED(CONFIG_AGP)
776 	struct nouveau_drm *drm = nouveau_bdev(bdev);
777 #endif
778 	if (!reg)
779 		return -EINVAL;
780 #if IS_ENABLED(CONFIG_AGP)
781 	if (drm->agp.bridge)
782 		return ttm_agp_bind(ttm, reg);
783 #endif
784 	return nouveau_sgdma_bind(bdev, ttm, reg);
785 }
786 
787 static void
788 nouveau_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
789 {
790 #if IS_ENABLED(CONFIG_AGP)
791 	struct nouveau_drm *drm = nouveau_bdev(bdev);
792 
793 	if (drm->agp.bridge) {
794 		ttm_agp_unbind(ttm);
795 		return;
796 	}
797 #endif
798 	nouveau_sgdma_unbind(bdev, ttm);
799 }
800 
801 static void
802 nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
803 {
804 	struct nouveau_bo *nvbo = nouveau_bo(bo);
805 
806 	switch (bo->resource->mem_type) {
807 	case TTM_PL_VRAM:
808 		nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART,
809 					 NOUVEAU_GEM_DOMAIN_CPU);
810 		break;
811 	default:
812 		nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_CPU, 0);
813 		break;
814 	}
815 
816 	*pl = nvbo->placement;
817 }
818 
819 static int
820 nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
821 		     struct ttm_resource *reg)
822 {
823 	struct nouveau_mem *old_mem = nouveau_mem(bo->resource);
824 	struct nouveau_mem *new_mem = nouveau_mem(reg);
825 	struct nvif_vmm *vmm = &drm->client.vmm.vmm;
826 	int ret;
827 
828 	ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0,
829 			   old_mem->mem.size, &old_mem->vma[0]);
830 	if (ret)
831 		return ret;
832 
833 	ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0,
834 			   new_mem->mem.size, &old_mem->vma[1]);
835 	if (ret)
836 		goto done;
837 
838 	ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]);
839 	if (ret)
840 		goto done;
841 
842 	ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]);
843 done:
844 	if (ret) {
845 		nvif_vmm_put(vmm, &old_mem->vma[1]);
846 		nvif_vmm_put(vmm, &old_mem->vma[0]);
847 	}
848 	return 0;
849 }
850 
851 static int
852 nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict,
853 		     struct ttm_operation_ctx *ctx,
854 		     struct ttm_resource *new_reg)
855 {
856 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
857 	struct nouveau_channel *chan = drm->ttm.chan;
858 	struct nouveau_cli *cli = (void *)chan->user.client;
859 	struct nouveau_fence *fence;
860 	int ret;
861 
862 	/* create temporary vmas for the transfer and attach them to the
863 	 * old nvkm_mem node, these will get cleaned up after ttm has
864 	 * destroyed the ttm_resource
865 	 */
866 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
867 		ret = nouveau_bo_move_prep(drm, bo, new_reg);
868 		if (ret)
869 			return ret;
870 	}
871 
872 	if (drm_drv_uses_atomic_modeset(drm->dev))
873 		mutex_lock(&cli->mutex);
874 	else
875 		mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
876 
877 	ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, ctx->interruptible);
878 	if (ret)
879 		goto out_unlock;
880 
881 	ret = drm->ttm.move(chan, bo, bo->resource, new_reg);
882 	if (ret)
883 		goto out_unlock;
884 
885 	ret = nouveau_fence_new(&fence, chan);
886 	if (ret)
887 		goto out_unlock;
888 
889 	/* TODO: figure out a better solution here
890 	 *
891 	 * wait on the fence here explicitly as going through
892 	 * ttm_bo_move_accel_cleanup somehow doesn't seem to do it.
893 	 *
894 	 * Without this the operation can timeout and we'll fallback to a
895 	 * software copy, which might take several minutes to finish.
896 	 */
897 	nouveau_fence_wait(fence, false, false);
898 	ret = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, false,
899 					new_reg);
900 	nouveau_fence_unref(&fence);
901 
902 out_unlock:
903 	mutex_unlock(&cli->mutex);
904 	return ret;
905 }
906 
907 void
908 nouveau_bo_move_init(struct nouveau_drm *drm)
909 {
910 	static const struct _method_table {
911 		const char *name;
912 		int engine;
913 		s32 oclass;
914 		int (*exec)(struct nouveau_channel *,
915 			    struct ttm_buffer_object *,
916 			    struct ttm_resource *, struct ttm_resource *);
917 		int (*init)(struct nouveau_channel *, u32 handle);
918 	} _methods[] = {
919 		{  "COPY", 4, 0xc7b5, nve0_bo_move_copy, nve0_bo_move_init },
920 		{  "GRCE", 0, 0xc7b5, nve0_bo_move_copy, nvc0_bo_move_init },
921 		{  "COPY", 4, 0xc6b5, nve0_bo_move_copy, nve0_bo_move_init },
922 		{  "GRCE", 0, 0xc6b5, nve0_bo_move_copy, nvc0_bo_move_init },
923 		{  "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init },
924 		{  "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init },
925 		{  "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init },
926 		{  "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init },
927 		{  "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init },
928 		{  "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init },
929 		{  "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init },
930 		{  "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init },
931 		{  "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
932 		{  "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
933 		{  "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
934 		{  "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
935 		{ "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
936 		{ "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
937 		{  "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
938 		{ "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
939 		{  "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
940 		{  "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
941 		{  "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
942 		{},
943 	};
944 	const struct _method_table *mthd = _methods;
945 	const char *name = "CPU";
946 	int ret;
947 
948 	do {
949 		struct nouveau_channel *chan;
950 
951 		if (mthd->engine)
952 			chan = drm->cechan;
953 		else
954 			chan = drm->channel;
955 		if (chan == NULL)
956 			continue;
957 
958 		ret = nvif_object_ctor(&chan->user, "ttmBoMove",
959 				       mthd->oclass | (mthd->engine << 16),
960 				       mthd->oclass, NULL, 0,
961 				       &drm->ttm.copy);
962 		if (ret == 0) {
963 			ret = mthd->init(chan, drm->ttm.copy.handle);
964 			if (ret) {
965 				nvif_object_dtor(&drm->ttm.copy);
966 				continue;
967 			}
968 
969 			drm->ttm.move = mthd->exec;
970 			drm->ttm.chan = chan;
971 			name = mthd->name;
972 			break;
973 		}
974 	} while ((++mthd)->exec);
975 
976 	NV_INFO(drm, "MM: using %s for buffer copies\n", name);
977 }
978 
979 static void nouveau_bo_move_ntfy(struct ttm_buffer_object *bo,
980 				 struct ttm_resource *new_reg)
981 {
982 	struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL;
983 	struct nouveau_bo *nvbo = nouveau_bo(bo);
984 	struct nouveau_vma *vma;
985 	long ret;
986 
987 	/* ttm can now (stupidly) pass the driver bos it didn't create... */
988 	if (bo->destroy != nouveau_bo_del_ttm)
989 		return;
990 
991 	nouveau_bo_del_io_reserve_lru(bo);
992 
993 	if (mem && new_reg->mem_type != TTM_PL_SYSTEM &&
994 	    mem->mem.page == nvbo->page) {
995 		list_for_each_entry(vma, &nvbo->vma_list, head) {
996 			nouveau_vma_map(vma, mem);
997 		}
998 		nouveau_uvmm_bo_map_all(nvbo, mem);
999 	} else {
1000 		list_for_each_entry(vma, &nvbo->vma_list, head) {
1001 			ret = dma_resv_wait_timeout(bo->base.resv,
1002 						    DMA_RESV_USAGE_BOOKKEEP,
1003 						    false, 15 * HZ);
1004 			WARN_ON(ret <= 0);
1005 			nouveau_vma_unmap(vma);
1006 		}
1007 		nouveau_uvmm_bo_unmap_all(nvbo);
1008 	}
1009 
1010 	if (new_reg)
1011 		nvbo->offset = (new_reg->start << PAGE_SHIFT);
1012 
1013 }
1014 
1015 static int
1016 nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_resource *new_reg,
1017 		   struct nouveau_drm_tile **new_tile)
1018 {
1019 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1020 	struct drm_device *dev = drm->dev;
1021 	struct nouveau_bo *nvbo = nouveau_bo(bo);
1022 	u64 offset = new_reg->start << PAGE_SHIFT;
1023 
1024 	*new_tile = NULL;
1025 	if (new_reg->mem_type != TTM_PL_VRAM)
1026 		return 0;
1027 
1028 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
1029 		*new_tile = nv10_bo_set_tiling(dev, offset, bo->base.size,
1030 					       nvbo->mode, nvbo->zeta);
1031 	}
1032 
1033 	return 0;
1034 }
1035 
1036 static void
1037 nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
1038 		      struct nouveau_drm_tile *new_tile,
1039 		      struct nouveau_drm_tile **old_tile)
1040 {
1041 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1042 	struct drm_device *dev = drm->dev;
1043 	struct dma_fence *fence;
1044 	int ret;
1045 
1046 	ret = dma_resv_get_singleton(bo->base.resv, DMA_RESV_USAGE_WRITE,
1047 				     &fence);
1048 	if (ret)
1049 		dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_WRITE,
1050 				      false, MAX_SCHEDULE_TIMEOUT);
1051 
1052 	nv10_bo_put_tile_region(dev, *old_tile, fence);
1053 	*old_tile = new_tile;
1054 }
1055 
1056 static int
1057 nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
1058 		struct ttm_operation_ctx *ctx,
1059 		struct ttm_resource *new_reg,
1060 		struct ttm_place *hop)
1061 {
1062 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1063 	struct nouveau_bo *nvbo = nouveau_bo(bo);
1064 	struct drm_gem_object *obj = &bo->base;
1065 	struct ttm_resource *old_reg = bo->resource;
1066 	struct nouveau_drm_tile *new_tile = NULL;
1067 	int ret = 0;
1068 
1069 	if (new_reg->mem_type == TTM_PL_TT) {
1070 		ret = nouveau_ttm_tt_bind(bo->bdev, bo->ttm, new_reg);
1071 		if (ret)
1072 			return ret;
1073 	}
1074 
1075 	drm_gpuvm_bo_gem_evict(obj, evict);
1076 	nouveau_bo_move_ntfy(bo, new_reg);
1077 	ret = ttm_bo_wait_ctx(bo, ctx);
1078 	if (ret)
1079 		goto out_ntfy;
1080 
1081 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
1082 		ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile);
1083 		if (ret)
1084 			goto out_ntfy;
1085 	}
1086 
1087 	/* Fake bo copy. */
1088 	if (!old_reg || (old_reg->mem_type == TTM_PL_SYSTEM &&
1089 			 !bo->ttm)) {
1090 		ttm_bo_move_null(bo, new_reg);
1091 		goto out;
1092 	}
1093 
1094 	if (old_reg->mem_type == TTM_PL_SYSTEM &&
1095 	    new_reg->mem_type == TTM_PL_TT) {
1096 		ttm_bo_move_null(bo, new_reg);
1097 		goto out;
1098 	}
1099 
1100 	if (old_reg->mem_type == TTM_PL_TT &&
1101 	    new_reg->mem_type == TTM_PL_SYSTEM) {
1102 		nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
1103 		ttm_resource_free(bo, &bo->resource);
1104 		ttm_bo_assign_mem(bo, new_reg);
1105 		goto out;
1106 	}
1107 
1108 	/* Hardware assisted copy. */
1109 	if (drm->ttm.move) {
1110 		if ((old_reg->mem_type == TTM_PL_SYSTEM &&
1111 		     new_reg->mem_type == TTM_PL_VRAM) ||
1112 		    (old_reg->mem_type == TTM_PL_VRAM &&
1113 		     new_reg->mem_type == TTM_PL_SYSTEM)) {
1114 			hop->fpfn = 0;
1115 			hop->lpfn = 0;
1116 			hop->mem_type = TTM_PL_TT;
1117 			hop->flags = 0;
1118 			return -EMULTIHOP;
1119 		}
1120 		ret = nouveau_bo_move_m2mf(bo, evict, ctx,
1121 					   new_reg);
1122 	} else
1123 		ret = -ENODEV;
1124 
1125 	if (ret) {
1126 		/* Fallback to software copy. */
1127 		ret = ttm_bo_move_memcpy(bo, ctx, new_reg);
1128 	}
1129 
1130 out:
1131 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
1132 		if (ret)
1133 			nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1134 		else
1135 			nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1136 	}
1137 out_ntfy:
1138 	if (ret) {
1139 		nouveau_bo_move_ntfy(bo, bo->resource);
1140 		drm_gpuvm_bo_gem_evict(obj, !evict);
1141 	}
1142 	return ret;
1143 }
1144 
1145 static void
1146 nouveau_ttm_io_mem_free_locked(struct nouveau_drm *drm,
1147 			       struct ttm_resource *reg)
1148 {
1149 	struct nouveau_mem *mem = nouveau_mem(reg);
1150 
1151 	if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1152 		switch (reg->mem_type) {
1153 		case TTM_PL_TT:
1154 			if (mem->kind)
1155 				nvif_object_unmap_handle(&mem->mem.object);
1156 			break;
1157 		case TTM_PL_VRAM:
1158 			nvif_object_unmap_handle(&mem->mem.object);
1159 			break;
1160 		default:
1161 			break;
1162 		}
1163 	}
1164 }
1165 
1166 static int
1167 nouveau_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *reg)
1168 {
1169 	struct nouveau_drm *drm = nouveau_bdev(bdev);
1170 	struct nvkm_device *device = nvxx_device(&drm->client.device);
1171 	struct nouveau_mem *mem = nouveau_mem(reg);
1172 	struct nvif_mmu *mmu = &drm->client.mmu;
1173 	int ret;
1174 
1175 	mutex_lock(&drm->ttm.io_reserve_mutex);
1176 retry:
1177 	switch (reg->mem_type) {
1178 	case TTM_PL_SYSTEM:
1179 		/* System memory */
1180 		ret = 0;
1181 		goto out;
1182 	case TTM_PL_TT:
1183 #if IS_ENABLED(CONFIG_AGP)
1184 		if (drm->agp.bridge) {
1185 			reg->bus.offset = (reg->start << PAGE_SHIFT) +
1186 				drm->agp.base;
1187 			reg->bus.is_iomem = !drm->agp.cma;
1188 			reg->bus.caching = ttm_write_combined;
1189 		}
1190 #endif
1191 		if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 ||
1192 		    !mem->kind) {
1193 			/* untiled */
1194 			ret = 0;
1195 			break;
1196 		}
1197 		fallthrough;	/* tiled memory */
1198 	case TTM_PL_VRAM:
1199 		reg->bus.offset = (reg->start << PAGE_SHIFT) +
1200 			device->func->resource_addr(device, 1);
1201 		reg->bus.is_iomem = true;
1202 
1203 		/* Some BARs do not support being ioremapped WC */
1204 		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
1205 		    mmu->type[drm->ttm.type_vram].type & NVIF_MEM_UNCACHED)
1206 			reg->bus.caching = ttm_uncached;
1207 		else
1208 			reg->bus.caching = ttm_write_combined;
1209 
1210 		if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1211 			union {
1212 				struct nv50_mem_map_v0 nv50;
1213 				struct gf100_mem_map_v0 gf100;
1214 			} args;
1215 			u64 handle, length;
1216 			u32 argc = 0;
1217 
1218 			switch (mem->mem.object.oclass) {
1219 			case NVIF_CLASS_MEM_NV50:
1220 				args.nv50.version = 0;
1221 				args.nv50.ro = 0;
1222 				args.nv50.kind = mem->kind;
1223 				args.nv50.comp = mem->comp;
1224 				argc = sizeof(args.nv50);
1225 				break;
1226 			case NVIF_CLASS_MEM_GF100:
1227 				args.gf100.version = 0;
1228 				args.gf100.ro = 0;
1229 				args.gf100.kind = mem->kind;
1230 				argc = sizeof(args.gf100);
1231 				break;
1232 			default:
1233 				WARN_ON(1);
1234 				break;
1235 			}
1236 
1237 			ret = nvif_object_map_handle(&mem->mem.object,
1238 						     &args, argc,
1239 						     &handle, &length);
1240 			if (ret != 1) {
1241 				if (WARN_ON(ret == 0))
1242 					ret = -EINVAL;
1243 				goto out;
1244 			}
1245 
1246 			reg->bus.offset = handle;
1247 		}
1248 		ret = 0;
1249 		break;
1250 	default:
1251 		ret = -EINVAL;
1252 	}
1253 
1254 out:
1255 	if (ret == -ENOSPC) {
1256 		struct nouveau_bo *nvbo;
1257 
1258 		nvbo = list_first_entry_or_null(&drm->ttm.io_reserve_lru,
1259 						typeof(*nvbo),
1260 						io_reserve_lru);
1261 		if (nvbo) {
1262 			list_del_init(&nvbo->io_reserve_lru);
1263 			drm_vma_node_unmap(&nvbo->bo.base.vma_node,
1264 					   bdev->dev_mapping);
1265 			nouveau_ttm_io_mem_free_locked(drm, nvbo->bo.resource);
1266 			goto retry;
1267 		}
1268 
1269 	}
1270 	mutex_unlock(&drm->ttm.io_reserve_mutex);
1271 	return ret;
1272 }
1273 
1274 static void
1275 nouveau_ttm_io_mem_free(struct ttm_device *bdev, struct ttm_resource *reg)
1276 {
1277 	struct nouveau_drm *drm = nouveau_bdev(bdev);
1278 
1279 	mutex_lock(&drm->ttm.io_reserve_mutex);
1280 	nouveau_ttm_io_mem_free_locked(drm, reg);
1281 	mutex_unlock(&drm->ttm.io_reserve_mutex);
1282 }
1283 
1284 vm_fault_t nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
1285 {
1286 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1287 	struct nouveau_bo *nvbo = nouveau_bo(bo);
1288 	struct nvkm_device *device = nvxx_device(&drm->client.device);
1289 	u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT;
1290 	int i, ret;
1291 
1292 	/* as long as the bo isn't in vram, and isn't tiled, we've got
1293 	 * nothing to do here.
1294 	 */
1295 	if (bo->resource->mem_type != TTM_PL_VRAM) {
1296 		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA ||
1297 		    !nvbo->kind)
1298 			return 0;
1299 
1300 		if (bo->resource->mem_type != TTM_PL_SYSTEM)
1301 			return 0;
1302 
1303 		nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
1304 
1305 	} else {
1306 		/* make sure bo is in mappable vram */
1307 		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
1308 		    bo->resource->start + PFN_UP(bo->resource->size) < mappable)
1309 			return 0;
1310 
1311 		for (i = 0; i < nvbo->placement.num_placement; ++i) {
1312 			nvbo->placements[i].fpfn = 0;
1313 			nvbo->placements[i].lpfn = mappable;
1314 		}
1315 
1316 		for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
1317 			nvbo->busy_placements[i].fpfn = 0;
1318 			nvbo->busy_placements[i].lpfn = mappable;
1319 		}
1320 
1321 		nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, 0);
1322 	}
1323 
1324 	ret = nouveau_bo_validate(nvbo, false, false);
1325 	if (unlikely(ret == -EBUSY || ret == -ERESTARTSYS))
1326 		return VM_FAULT_NOPAGE;
1327 	else if (unlikely(ret))
1328 		return VM_FAULT_SIGBUS;
1329 
1330 	ttm_bo_move_to_lru_tail_unlocked(bo);
1331 	return 0;
1332 }
1333 
1334 static int
1335 nouveau_ttm_tt_populate(struct ttm_device *bdev,
1336 			struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
1337 {
1338 	struct ttm_tt *ttm_dma = (void *)ttm;
1339 	struct nouveau_drm *drm;
1340 	bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
1341 
1342 	if (ttm_tt_is_populated(ttm))
1343 		return 0;
1344 
1345 	if (slave && ttm->sg) {
1346 		drm_prime_sg_to_dma_addr_array(ttm->sg, ttm_dma->dma_address,
1347 					       ttm->num_pages);
1348 		return 0;
1349 	}
1350 
1351 	drm = nouveau_bdev(bdev);
1352 
1353 	return ttm_pool_alloc(&drm->ttm.bdev.pool, ttm, ctx);
1354 }
1355 
1356 static void
1357 nouveau_ttm_tt_unpopulate(struct ttm_device *bdev,
1358 			  struct ttm_tt *ttm)
1359 {
1360 	struct nouveau_drm *drm;
1361 	bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
1362 
1363 	if (slave)
1364 		return;
1365 
1366 	nouveau_ttm_tt_unbind(bdev, ttm);
1367 
1368 	drm = nouveau_bdev(bdev);
1369 
1370 	return ttm_pool_free(&drm->ttm.bdev.pool, ttm);
1371 }
1372 
1373 static void
1374 nouveau_ttm_tt_destroy(struct ttm_device *bdev,
1375 		       struct ttm_tt *ttm)
1376 {
1377 #if IS_ENABLED(CONFIG_AGP)
1378 	struct nouveau_drm *drm = nouveau_bdev(bdev);
1379 	if (drm->agp.bridge) {
1380 		ttm_agp_destroy(ttm);
1381 		return;
1382 	}
1383 #endif
1384 	nouveau_sgdma_destroy(bdev, ttm);
1385 }
1386 
1387 void
1388 nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
1389 {
1390 	struct dma_resv *resv = nvbo->bo.base.resv;
1391 
1392 	if (!fence)
1393 		return;
1394 
1395 	dma_resv_add_fence(resv, &fence->base, exclusive ?
1396 			   DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
1397 }
1398 
1399 static void
1400 nouveau_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1401 {
1402 	nouveau_bo_move_ntfy(bo, NULL);
1403 }
1404 
1405 struct ttm_device_funcs nouveau_bo_driver = {
1406 	.ttm_tt_create = &nouveau_ttm_tt_create,
1407 	.ttm_tt_populate = &nouveau_ttm_tt_populate,
1408 	.ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
1409 	.ttm_tt_destroy = &nouveau_ttm_tt_destroy,
1410 	.eviction_valuable = ttm_bo_eviction_valuable,
1411 	.evict_flags = nouveau_bo_evict_flags,
1412 	.delete_mem_notify = nouveau_bo_delete_mem_notify,
1413 	.move = nouveau_bo_move,
1414 	.io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1415 	.io_mem_free = &nouveau_ttm_io_mem_free,
1416 };
1417