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