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_buffer_object
*bo
= &buf
->base
;
53 ret
= ttm_write_lock(&dev_priv
->reservation_sem
, interruptible
);
54 if (unlikely(ret
!= 0))
57 vmw_execbuf_release_pinned_bo(dev_priv
);
59 ret
= ttm_bo_reserve(bo
, interruptible
, false, false, NULL
);
60 if (unlikely(ret
!= 0))
63 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false);
65 vmw_bo_pin_reserved(buf
, true);
70 ttm_write_unlock(&dev_priv
->reservation_sem
);
75 * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
77 * This function takes the reservation_sem in write mode.
78 * Flushes and unpins the query bo to avoid failures.
80 * @dev_priv: Driver private.
81 * @buf: DMA buffer to move.
82 * @pin: Pin buffer if true.
83 * @interruptible: Use interruptible wait.
86 * -ERESTARTSYS if interrupted by a signal.
88 int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private
*dev_priv
,
89 struct vmw_dma_buffer
*buf
,
92 struct ttm_buffer_object
*bo
= &buf
->base
;
95 ret
= ttm_write_lock(&dev_priv
->reservation_sem
, interruptible
);
96 if (unlikely(ret
!= 0))
99 vmw_execbuf_release_pinned_bo(dev_priv
);
101 ret
= ttm_bo_reserve(bo
, interruptible
, false, false, NULL
);
102 if (unlikely(ret
!= 0))
105 ret
= ttm_bo_validate(bo
, &vmw_vram_gmr_placement
, interruptible
,
107 if (likely(ret
== 0) || ret
== -ERESTARTSYS
)
110 ret
= ttm_bo_validate(bo
, &vmw_vram_placement
, interruptible
, false);
114 vmw_bo_pin_reserved(buf
, true);
116 ttm_bo_unreserve(bo
);
118 ttm_write_unlock(&dev_priv
->reservation_sem
);
123 * vmw_dmabuf_pin_in_vram - Move a buffer to vram.
125 * This function takes the reservation_sem in write mode.
126 * Flushes and unpins the query bo to avoid failures.
128 * @dev_priv: Driver private.
129 * @buf: DMA buffer to move.
130 * @interruptible: Use interruptible wait.
133 * -ERESTARTSYS if interrupted by a signal.
135 int vmw_dmabuf_pin_in_vram(struct vmw_private
*dev_priv
,
136 struct vmw_dma_buffer
*buf
,
139 return vmw_dmabuf_pin_in_placement(dev_priv
, buf
, &vmw_vram_placement
,
144 * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram.
146 * This function takes the reservation_sem in write mode.
147 * Flushes and unpins the query bo to avoid failures.
149 * @dev_priv: Driver private.
150 * @buf: DMA buffer to pin.
151 * @interruptible: Use interruptible wait.
154 * -ERESTARTSYS if interrupted by a signal.
156 int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private
*dev_priv
,
157 struct vmw_dma_buffer
*buf
,
160 struct ttm_buffer_object
*bo
= &buf
->base
;
161 struct ttm_placement placement
;
162 struct ttm_place place
;
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
;
172 ret
= ttm_write_lock(&dev_priv
->reservation_sem
, interruptible
);
173 if (unlikely(ret
!= 0))
176 vmw_execbuf_release_pinned_bo(dev_priv
);
177 ret
= ttm_bo_reserve(bo
, interruptible
, false, false, NULL
);
178 if (unlikely(ret
!= 0))
182 * Is this buffer already in vram but not at the start of it?
183 * In that case, evict it first because TTM isn't good at handling
186 if (bo
->mem
.mem_type
== TTM_PL_VRAM
&&
187 bo
->mem
.start
< bo
->num_pages
&&
189 (void) ttm_bo_validate(bo
, &vmw_sys_placement
, false, false);
191 ret
= ttm_bo_validate(bo
, &placement
, interruptible
, false);
193 /* For some reason we didn't end up at the start of vram */
194 WARN_ON(ret
== 0 && bo
->offset
!= 0);
196 vmw_bo_pin_reserved(buf
, true);
198 ttm_bo_unreserve(bo
);
200 ttm_write_unlock(&dev_priv
->reservation_sem
);
206 * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer.
208 * This function takes the reservation_sem in write mode.
210 * @dev_priv: Driver private.
211 * @buf: DMA buffer to unpin.
212 * @interruptible: Use interruptible wait.
215 * -ERESTARTSYS if interrupted by a signal.
217 int vmw_dmabuf_unpin(struct vmw_private
*dev_priv
,
218 struct vmw_dma_buffer
*buf
,
221 struct ttm_buffer_object
*bo
= &buf
->base
;
224 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, interruptible
);
225 if (unlikely(ret
!= 0))
228 ret
= ttm_bo_reserve(bo
, interruptible
, false, false, NULL
);
229 if (unlikely(ret
!= 0))
232 vmw_bo_pin_reserved(buf
, false);
234 ttm_bo_unreserve(bo
);
237 ttm_read_unlock(&dev_priv
->reservation_sem
);
242 * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
245 * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
246 * @ptr: SVGAGuestPtr returning the result.
248 void vmw_bo_get_guest_ptr(const struct ttm_buffer_object
*bo
,
251 if (bo
->mem
.mem_type
== TTM_PL_VRAM
) {
252 ptr
->gmrId
= SVGA_GMR_FRAMEBUFFER
;
253 ptr
->offset
= bo
->offset
;
255 ptr
->gmrId
= bo
->mem
.start
;
262 * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
264 * @vbo: The buffer object. Must be reserved.
265 * @pin: Whether to pin or unpin.
268 void vmw_bo_pin_reserved(struct vmw_dma_buffer
*vbo
, bool pin
)
271 struct ttm_placement placement
;
272 struct ttm_buffer_object
*bo
= &vbo
->base
;
273 uint32_t old_mem_type
= bo
->mem
.mem_type
;
276 lockdep_assert_held(&bo
->resv
->lock
.base
);
279 if (vbo
->pin_count
++ > 0)
282 WARN_ON(vbo
->pin_count
<= 0);
283 if (--vbo
->pin_count
> 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
;
292 pl
.flags
|= TTM_PL_FLAG_NO_EVICT
;
294 memset(&placement
, 0, sizeof(placement
));
295 placement
.num_placement
= 1;
296 placement
.placement
= &pl
;
298 ret
= ttm_bo_validate(bo
, &placement
, false, true);
300 BUG_ON(ret
!= 0 || bo
->mem
.mem_type
!= old_mem_type
);