1 /**************************************************************************
3 * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include <drm/ttm/ttm_placement.h>
31 #include "vmwgfx_drv.h"
35 * vmw_dmabuf_pin_in_placement - Validate a buffer to placement.
37 * @dev_priv: Driver private.
38 * @buf: DMA buffer to move.
39 * @placement: The placement to pin it.
40 * @interruptible: Use interruptible wait.
43 * -ERESTARTSYS if interrupted by a signal.
45 int vmw_dmabuf_pin_in_placement(struct vmw_private
*dev_priv
,
46 struct vmw_dma_buffer
*buf
,
47 struct ttm_placement
*placement
,
50 struct ttm_operation_ctx ctx
= {interruptible
, false };
51 struct ttm_buffer_object
*bo
= &buf
->base
;
55 ret
= ttm_write_lock(&dev_priv
->reservation_sem
, interruptible
);
56 if (unlikely(ret
!= 0))
59 vmw_execbuf_release_pinned_bo(dev_priv
);
61 ret
= ttm_bo_reserve(bo
, interruptible
, false, NULL
);
62 if (unlikely(ret
!= 0))
65 if (buf
->pin_count
> 0)
66 ret
= ttm_bo_mem_compat(placement
, &bo
->mem
,
67 &new_flags
) == true ? 0 : -EINVAL
;
69 ret
= ttm_bo_validate(bo
, placement
, &ctx
);
72 vmw_bo_pin_reserved(buf
, true);
77 ttm_write_unlock(&dev_priv
->reservation_sem
);
82 * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
84 * This function takes the reservation_sem in write mode.
85 * Flushes and unpins the query bo to avoid failures.
87 * @dev_priv: Driver private.
88 * @buf: DMA buffer to move.
89 * @pin: Pin buffer if true.
90 * @interruptible: Use interruptible wait.
93 * -ERESTARTSYS if interrupted by a signal.
95 int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private
*dev_priv
,
96 struct vmw_dma_buffer
*buf
,
99 struct ttm_operation_ctx ctx
= {interruptible
, false };
100 struct ttm_buffer_object
*bo
= &buf
->base
;
104 ret
= ttm_write_lock(&dev_priv
->reservation_sem
, interruptible
);
105 if (unlikely(ret
!= 0))
108 vmw_execbuf_release_pinned_bo(dev_priv
);
110 ret
= ttm_bo_reserve(bo
, interruptible
, false, NULL
);
111 if (unlikely(ret
!= 0))
114 if (buf
->pin_count
> 0) {
115 ret
= ttm_bo_mem_compat(&vmw_vram_gmr_placement
, &bo
->mem
,
116 &new_flags
) == true ? 0 : -EINVAL
;
120 ret
= ttm_bo_validate(bo
, &vmw_vram_gmr_placement
, &ctx
);
121 if (likely(ret
== 0) || ret
== -ERESTARTSYS
)
124 ret
= ttm_bo_validate(bo
, &vmw_vram_placement
, &ctx
);
128 vmw_bo_pin_reserved(buf
, true);
130 ttm_bo_unreserve(bo
);
132 ttm_write_unlock(&dev_priv
->reservation_sem
);
137 * vmw_dmabuf_pin_in_vram - Move a buffer to vram.
139 * This function takes the reservation_sem in write mode.
140 * Flushes and unpins the query bo to avoid failures.
142 * @dev_priv: Driver private.
143 * @buf: DMA buffer to move.
144 * @interruptible: Use interruptible wait.
147 * -ERESTARTSYS if interrupted by a signal.
149 int vmw_dmabuf_pin_in_vram(struct vmw_private
*dev_priv
,
150 struct vmw_dma_buffer
*buf
,
153 return vmw_dmabuf_pin_in_placement(dev_priv
, buf
, &vmw_vram_placement
,
158 * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram.
160 * This function takes the reservation_sem in write mode.
161 * Flushes and unpins the query bo to avoid failures.
163 * @dev_priv: Driver private.
164 * @buf: DMA buffer to pin.
165 * @interruptible: Use interruptible wait.
168 * -ERESTARTSYS if interrupted by a signal.
170 int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private
*dev_priv
,
171 struct vmw_dma_buffer
*buf
,
174 struct ttm_operation_ctx ctx
= {interruptible
, false };
175 struct ttm_buffer_object
*bo
= &buf
->base
;
176 struct ttm_placement placement
;
177 struct ttm_place place
;
181 place
= vmw_vram_placement
.placement
[0];
182 place
.lpfn
= bo
->num_pages
;
183 placement
.num_placement
= 1;
184 placement
.placement
= &place
;
185 placement
.num_busy_placement
= 1;
186 placement
.busy_placement
= &place
;
188 ret
= ttm_write_lock(&dev_priv
->reservation_sem
, interruptible
);
189 if (unlikely(ret
!= 0))
192 vmw_execbuf_release_pinned_bo(dev_priv
);
193 ret
= ttm_bo_reserve(bo
, interruptible
, false, NULL
);
194 if (unlikely(ret
!= 0))
198 * Is this buffer already in vram but not at the start of it?
199 * In that case, evict it first because TTM isn't good at handling
202 if (bo
->mem
.mem_type
== TTM_PL_VRAM
&&
203 bo
->mem
.start
< bo
->num_pages
&&
205 buf
->pin_count
== 0) {
206 ctx
.interruptible
= false;
207 (void) ttm_bo_validate(bo
, &vmw_sys_placement
, &ctx
);
210 if (buf
->pin_count
> 0)
211 ret
= ttm_bo_mem_compat(&placement
, &bo
->mem
,
212 &new_flags
) == true ? 0 : -EINVAL
;
214 ret
= ttm_bo_validate(bo
, &placement
, &ctx
);
216 /* For some reason we didn't end up at the start of vram */
217 WARN_ON(ret
== 0 && bo
->offset
!= 0);
219 vmw_bo_pin_reserved(buf
, true);
221 ttm_bo_unreserve(bo
);
223 ttm_write_unlock(&dev_priv
->reservation_sem
);
229 * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer.
231 * This function takes the reservation_sem in write mode.
233 * @dev_priv: Driver private.
234 * @buf: DMA buffer to unpin.
235 * @interruptible: Use interruptible wait.
238 * -ERESTARTSYS if interrupted by a signal.
240 int vmw_dmabuf_unpin(struct vmw_private
*dev_priv
,
241 struct vmw_dma_buffer
*buf
,
244 struct ttm_buffer_object
*bo
= &buf
->base
;
247 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, interruptible
);
248 if (unlikely(ret
!= 0))
251 ret
= ttm_bo_reserve(bo
, interruptible
, false, NULL
);
252 if (unlikely(ret
!= 0))
255 vmw_bo_pin_reserved(buf
, false);
257 ttm_bo_unreserve(bo
);
260 ttm_read_unlock(&dev_priv
->reservation_sem
);
265 * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
268 * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
269 * @ptr: SVGAGuestPtr returning the result.
271 void vmw_bo_get_guest_ptr(const struct ttm_buffer_object
*bo
,
274 if (bo
->mem
.mem_type
== TTM_PL_VRAM
) {
275 ptr
->gmrId
= SVGA_GMR_FRAMEBUFFER
;
276 ptr
->offset
= bo
->offset
;
278 ptr
->gmrId
= bo
->mem
.start
;
285 * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
287 * @vbo: The buffer object. Must be reserved.
288 * @pin: Whether to pin or unpin.
291 void vmw_bo_pin_reserved(struct vmw_dma_buffer
*vbo
, bool pin
)
293 struct ttm_operation_ctx ctx
= { false, true };
295 struct ttm_placement placement
;
296 struct ttm_buffer_object
*bo
= &vbo
->base
;
297 uint32_t old_mem_type
= bo
->mem
.mem_type
;
300 lockdep_assert_held(&bo
->resv
->lock
.base
);
303 if (vbo
->pin_count
++ > 0)
306 WARN_ON(vbo
->pin_count
<= 0);
307 if (--vbo
->pin_count
> 0)
313 pl
.flags
= TTM_PL_FLAG_VRAM
| VMW_PL_FLAG_GMR
| VMW_PL_FLAG_MOB
314 | TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
316 pl
.flags
|= TTM_PL_FLAG_NO_EVICT
;
318 memset(&placement
, 0, sizeof(placement
));
319 placement
.num_placement
= 1;
320 placement
.placement
= &pl
;
322 ret
= ttm_bo_validate(bo
, &placement
, &ctx
);
324 BUG_ON(ret
!= 0 || bo
->mem
.mem_type
!= old_mem_type
);