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 
9 struct nouveau_sgdma_be {
10 	/* this has to be the first field so populate/unpopulated in
11 	 * nouve_bo.c works properly, otherwise have to move them here
12 	 */
13 	struct ttm_dma_tt ttm;
14 	struct nouveau_mem *mem;
15 };
16 
17 void
nouveau_sgdma_destroy(struct ttm_bo_device * bdev,struct ttm_tt * ttm)18 nouveau_sgdma_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
19 {
20 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
21 
22 	if (ttm) {
23 		nouveau_sgdma_unbind(bdev, ttm);
24 		ttm_tt_destroy_common(bdev, ttm);
25 		ttm_dma_tt_fini(&nvbe->ttm);
26 		kfree(nvbe);
27 	}
28 }
29 
30 int
nouveau_sgdma_bind(struct ttm_bo_device * bdev,struct ttm_tt * ttm,struct ttm_resource * reg)31 nouveau_sgdma_bind(struct ttm_bo_device *bdev, struct ttm_tt *ttm, struct ttm_resource *reg)
32 {
33 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
34 	struct nouveau_drm *drm = nouveau_bdev(bdev);
35 	struct nouveau_mem *mem = nouveau_mem(reg);
36 	int ret;
37 
38 	if (nvbe->mem)
39 		return 0;
40 
41 	ret = nouveau_mem_host(reg, &nvbe->ttm);
42 	if (ret)
43 		return ret;
44 
45 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
46 		ret = nouveau_mem_map(mem, &mem->cli->vmm.vmm, &mem->vma[0]);
47 		if (ret) {
48 			nouveau_mem_fini(mem);
49 			return ret;
50 		}
51 	}
52 
53 	nvbe->mem = mem;
54 	return 0;
55 }
56 
57 void
nouveau_sgdma_unbind(struct ttm_bo_device * bdev,struct ttm_tt * ttm)58 nouveau_sgdma_unbind(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
59 {
60 	struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
61 	if (nvbe->mem) {
62 		nouveau_mem_fini(nvbe->mem);
63 		nvbe->mem = NULL;
64 	}
65 }
66 
67 struct ttm_tt *
nouveau_sgdma_create_ttm(struct ttm_buffer_object * bo,uint32_t page_flags)68 nouveau_sgdma_create_ttm(struct ttm_buffer_object *bo, uint32_t page_flags)
69 {
70 	struct nouveau_sgdma_be *nvbe;
71 
72 	nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL);
73 	if (!nvbe)
74 		return NULL;
75 
76 	if (ttm_dma_tt_init(&nvbe->ttm, bo, page_flags)) {
77 		kfree(nvbe);
78 		return NULL;
79 	}
80 	return &nvbe->ttm.ttm;
81 }
82