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
27
28#include <drm/ttm/ttm_placement.h>
29
30#include <drm/drmP.h>
31#include "vmwgfx_drv.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
45int vmw_dmabuf_pin_in_placement(struct vmw_private *dev_priv,
46 struct vmw_dma_buffer *buf,
47 struct ttm_placement *placement,
48 bool interruptible)
49{
50 struct ttm_buffer_object *bo = &buf->base;
51 int ret;
52
53 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
54 if (unlikely(ret != 0))
55 return ret;
56
57 vmw_execbuf_release_pinned_bo(dev_priv);
58
59 ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
60 if (unlikely(ret != 0))
61 goto err;
62
63 ret = ttm_bo_validate(bo, placement, interruptible, false);
64 if (!ret)
65 vmw_bo_pin_reserved(buf, true);
66
67 ttm_bo_unreserve(bo);
68
69err:
70 ttm_write_unlock(&dev_priv->reservation_sem);
71 return ret;
72}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
89 struct vmw_dma_buffer *buf,
90 bool interruptible)
91{
92 struct ttm_buffer_object *bo = &buf->base;
93 int ret;
94
95 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
96 if (unlikely(ret != 0))
97 return ret;
98
99 vmw_execbuf_release_pinned_bo(dev_priv);
100
101 ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
102 if (unlikely(ret != 0))
103 goto err;
104
105 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible,
106 false);
107 if (likely(ret == 0) || ret == -ERESTARTSYS)
108 goto out_unreserve;
109
110 ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false);
111
112out_unreserve:
113 if (!ret)
114 vmw_bo_pin_reserved(buf, true);
115
116 ttm_bo_unreserve(bo);
117err:
118 ttm_write_unlock(&dev_priv->reservation_sem);
119 return ret;
120}
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv,
136 struct vmw_dma_buffer *buf,
137 bool interruptible)
138{
139 return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
140 interruptible);
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *dev_priv,
157 struct vmw_dma_buffer *buf,
158 bool interruptible)
159{
160 struct ttm_buffer_object *bo = &buf->base;
161 struct ttm_placement placement;
162 struct ttm_place place;
163 int ret = 0;
164
165 place = vmw_vram_placement.placement[0];
166 place.lpfn = bo->num_pages;
167 placement.num_placement = 1;
168 placement.placement = &place;
169 placement.num_busy_placement = 1;
170 placement.busy_placement = &place;
171
172 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
173 if (unlikely(ret != 0))
174 return ret;
175
176 vmw_execbuf_release_pinned_bo(dev_priv);
177 ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
178 if (unlikely(ret != 0))
179 goto err_unlock;
180
181
182
183
184
185
186 if (bo->mem.mem_type == TTM_PL_VRAM &&
187 bo->mem.start < bo->num_pages &&
188 bo->mem.start > 0)
189 (void) ttm_bo_validate(bo, &vmw_sys_placement, false, false);
190
191 ret = ttm_bo_validate(bo, &placement, interruptible, false);
192
193
194 WARN_ON(ret == 0 && bo->offset != 0);
195 if (!ret)
196 vmw_bo_pin_reserved(buf, true);
197
198 ttm_bo_unreserve(bo);
199err_unlock:
200 ttm_write_unlock(&dev_priv->reservation_sem);
201
202 return ret;
203}
204
205
206
207
208
209
210
211
212
213
214
215
216
217int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
218 struct vmw_dma_buffer *buf,
219 bool interruptible)
220{
221 struct ttm_buffer_object *bo = &buf->base;
222 int ret;
223
224 ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible);
225 if (unlikely(ret != 0))
226 return ret;
227
228 ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
229 if (unlikely(ret != 0))
230 goto err;
231
232 vmw_bo_pin_reserved(buf, false);
233
234 ttm_bo_unreserve(bo);
235
236err:
237 ttm_read_unlock(&dev_priv->reservation_sem);
238 return ret;
239}
240
241
242
243
244
245
246
247
248void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
249 SVGAGuestPtr *ptr)
250{
251 if (bo->mem.mem_type == TTM_PL_VRAM) {
252 ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
253 ptr->offset = bo->offset;
254 } else {
255 ptr->gmrId = bo->mem.start;
256 ptr->offset = 0;
257 }
258}
259
260
261
262
263
264
265
266
267
268void vmw_bo_pin_reserved(struct vmw_dma_buffer *vbo, bool pin)
269{
270 struct ttm_place pl;
271 struct ttm_placement placement;
272 struct ttm_buffer_object *bo = &vbo->base;
273 uint32_t old_mem_type = bo->mem.mem_type;
274 int ret;
275
276 lockdep_assert_held(&bo->resv->lock.base);
277
278 if (pin) {
279 if (vbo->pin_count++ > 0)
280 return;
281 } else {
282 WARN_ON(vbo->pin_count <= 0);
283 if (--vbo->pin_count > 0)
284 return;
285 }
286
287 pl.fpfn = 0;
288 pl.lpfn = 0;
289 pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB
290 | TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
291 if (pin)
292 pl.flags |= TTM_PL_FLAG_NO_EVICT;
293
294 memset(&placement, 0, sizeof(placement));
295 placement.num_placement = 1;
296 placement.placement = &pl;
297
298 ret = ttm_bo_validate(bo, &placement, false, true);
299
300 BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type);
301}
302