1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "virtgpu_drv.h"
27
28static void virtio_gpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
29{
30 struct virtio_gpu_object *bo;
31 struct virtio_gpu_device *vgdev;
32
33 bo = container_of(tbo, struct virtio_gpu_object, tbo);
34 vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
35
36 if (bo->hw_res_handle)
37 virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle);
38 if (bo->pages)
39 virtio_gpu_object_free_sg_table(bo);
40 drm_gem_object_release(&bo->gem_base);
41 kfree(bo);
42}
43
44static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo,
45 bool pinned)
46{
47 u32 c = 1;
48 u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
49
50 vgbo->placement.placement = &vgbo->placement_code;
51 vgbo->placement.busy_placement = &vgbo->placement_code;
52 vgbo->placement_code.fpfn = 0;
53 vgbo->placement_code.lpfn = 0;
54 vgbo->placement_code.flags =
55 TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT | pflag;
56 vgbo->placement.num_placement = c;
57 vgbo->placement.num_busy_placement = c;
58
59}
60
61int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
62 unsigned long size, bool kernel, bool pinned,
63 struct virtio_gpu_object **bo_ptr)
64{
65 struct virtio_gpu_object *bo;
66 enum ttm_bo_type type;
67 size_t acc_size;
68 int ret;
69
70 if (kernel)
71 type = ttm_bo_type_kernel;
72 else
73 type = ttm_bo_type_device;
74 *bo_ptr = NULL;
75
76 acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size,
77 sizeof(struct virtio_gpu_object));
78
79 bo = kzalloc(sizeof(struct virtio_gpu_object), GFP_KERNEL);
80 if (bo == NULL)
81 return -ENOMEM;
82 size = roundup(size, PAGE_SIZE);
83 ret = drm_gem_object_init(vgdev->ddev, &bo->gem_base, size);
84 if (ret != 0) {
85 kfree(bo);
86 return ret;
87 }
88 bo->dumb = false;
89 virtio_gpu_init_ttm_placement(bo, pinned);
90
91 ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type,
92 &bo->placement, 0, !kernel, NULL, acc_size,
93 NULL, NULL, &virtio_gpu_ttm_bo_destroy);
94
95 if (ret != 0)
96 return ret;
97
98 *bo_ptr = bo;
99 return 0;
100}
101
102int virtio_gpu_object_kmap(struct virtio_gpu_object *bo, void **ptr)
103{
104 bool is_iomem;
105 int r;
106
107 if (bo->vmap) {
108 if (ptr)
109 *ptr = bo->vmap;
110 return 0;
111 }
112 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
113 if (r)
114 return r;
115 bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
116 if (ptr)
117 *ptr = bo->vmap;
118 return 0;
119}
120
121int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
122 struct virtio_gpu_object *bo)
123{
124 int ret;
125 struct page **pages = bo->tbo.ttm->pages;
126 int nr_pages = bo->tbo.num_pages;
127
128
129 if (bo->pages)
130 return 0;
131
132 if (bo->tbo.ttm->state == tt_unpopulated)
133 bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm);
134 bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
135 if (!bo->pages)
136 goto out;
137
138 ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
139 nr_pages << PAGE_SHIFT, GFP_KERNEL);
140 if (ret)
141 goto out;
142 return 0;
143out:
144 kfree(bo->pages);
145 bo->pages = NULL;
146 return -ENOMEM;
147}
148
149void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
150{
151 sg_free_table(bo->pages);
152 kfree(bo->pages);
153 bo->pages = NULL;
154}
155
156int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
157{
158 int r;
159
160 r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
161 if (unlikely(r != 0))
162 return r;
163 r = ttm_bo_wait(&bo->tbo, true, no_wait);
164 ttm_bo_unreserve(&bo->tbo);
165 return r;
166}
167
168