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