1 // SPDX-License-Identifier: MIT
2 #include <linux/pagemap.h>
3 #include <linux/slab.h>
4 
5 #include "nouveau_drv.h"
6 #include "nouveau_mem.h"
7 #include "nouveau_ttm.h"
8 #include "nouveau_bo.h"
9 
10 struct nouveau_sgdma_be {
11 	/* this has to be the first field so populate/unpopulated in
12 	 * nouve_bo.c works properly, otherwise have to move them here
13 	 */
14 	struct ttm_tt ttm;
15 	struct nouveau_mem *mem;
16 };
17 
18 void
19 nouveau_sgdma_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
20 {
21 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
22 
23 	if (ttm) {
24 		ttm_tt_fini(&nvbe->ttm);
25 		kfree(nvbe);
26 	}
27 }
28 
29 int
30 nouveau_sgdma_bind(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_resource *reg)
31 {
32 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
33 	struct nouveau_drm *drm = nouveau_bdev(bdev);
34 	struct nouveau_mem *mem = nouveau_mem(reg);
35 	int ret;
36 
37 	if (nvbe->mem)
38 		return 0;
39 
40 	ret = nouveau_mem_host(reg, &nvbe->ttm);
41 	if (ret)
42 		return ret;
43 
44 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
45 		ret = nouveau_mem_map(mem, &mem->cli->vmm.vmm, &mem->vma[0]);
46 		if (ret) {
47 			nouveau_mem_fini(mem);
48 			return ret;
49 		}
50 	}
51 
52 	nvbe->mem = mem;
53 	return 0;
54 }
55 
56 void
57 nouveau_sgdma_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
58 {
59 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
60 	if (nvbe->mem) {
61 		nouveau_mem_fini(nvbe->mem);
62 		nvbe->mem = NULL;
63 	}
64 }
65 
66 struct ttm_tt *
67 nouveau_sgdma_create_ttm(struct ttm_buffer_object *bo, uint32_t page_flags)
68 {
69 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
70 	struct nouveau_bo *nvbo = nouveau_bo(bo);
71 	struct nouveau_sgdma_be *nvbe;
72 	enum ttm_caching caching;
73 
74 	if (nvbo->force_coherent)
75 		caching = ttm_uncached;
76 	else if (drm->agp.bridge)
77 		caching = ttm_write_combined;
78 	else
79 		caching = ttm_cached;
80 
81 	nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL);
82 	if (!nvbe)
83 		return NULL;
84 
85 	if (ttm_sg_tt_init(&nvbe->ttm, bo, page_flags, caching)) {
86 		kfree(nvbe);
87 		return NULL;
88 	}
89 	return &nvbe->ttm;
90 }
91