1 /**************************************************************************
3 * Copyright © 2011 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 "ttm/ttm_placement.h"
31 #include "vmwgfx_drv.h"
35 * Validate a buffer to placement.
37 * May only be called by the current master as this function takes the
38 * its lock in write mode.
41 * -ERESTARTSYS if interrupted by a signal.
43 int vmw_dmabuf_to_placement(struct vmw_private *dev_priv,
44 struct vmw_dma_buffer *buf,
45 struct ttm_placement *placement,
48 struct vmw_master *vmaster = dev_priv->active_master;
49 struct ttm_buffer_object *bo = &buf->base;
52 ret = ttm_write_lock(&vmaster->lock, interruptible);
53 if (unlikely(ret != 0))
56 ret = ttm_bo_reserve(bo, interruptible, false, false, 0);
57 if (unlikely(ret != 0))
60 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
65 ttm_write_unlock(&vmaster->lock);
70 * Move a buffer to vram or gmr.
72 * May only be called by the current master as this function takes the
73 * its lock in write mode.
75 * @dev_priv: Driver private.
76 * @buf: DMA buffer to move.
77 * @pin: Pin buffer if true.
78 * @interruptible: Use interruptible wait.
81 * -ERESTARTSYS if interrupted by a signal.
83 int vmw_dmabuf_to_vram_or_gmr(struct vmw_private *dev_priv,
84 struct vmw_dma_buffer *buf,
85 bool pin, bool interruptible)
87 struct vmw_master *vmaster = dev_priv->active_master;
88 struct ttm_buffer_object *bo = &buf->base;
89 struct ttm_placement *placement;
92 ret = ttm_write_lock(&vmaster->lock, interruptible);
93 if (unlikely(ret != 0))
96 ret = ttm_bo_reserve(bo, interruptible, false, false, 0);
97 if (unlikely(ret != 0))
101 * Put BO in VRAM if there is space, otherwise as a GMR.
102 * If there is no space in VRAM and GMR ids are all used up,
103 * start evicting GMRs to make room. If the DMA buffer can't be
104 * used as a GMR, this will return -ENOMEM.
108 placement = &vmw_vram_gmr_ne_placement;
110 placement = &vmw_vram_gmr_placement;
112 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
113 if (likely(ret == 0) || ret == -ERESTARTSYS)
118 * If that failed, try VRAM again, this time evicting
123 placement = &vmw_vram_ne_placement;
125 placement = &vmw_vram_placement;
127 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
130 ttm_bo_unreserve(bo);
132 ttm_write_unlock(&vmaster->lock);
137 * Move a buffer to vram.
139 * May only be called by the current master as this function takes the
140 * its lock in write mode.
142 * @dev_priv: Driver private.
143 * @buf: DMA buffer to move.
144 * @pin: Pin buffer in vram if true.
145 * @interruptible: Use interruptible wait.
148 * -ERESTARTSYS if interrupted by a signal.
150 int vmw_dmabuf_to_vram(struct vmw_private *dev_priv,
151 struct vmw_dma_buffer *buf,
152 bool pin, bool interruptible)
154 struct ttm_placement *placement;
157 placement = &vmw_vram_ne_placement;
159 placement = &vmw_vram_placement;
161 return vmw_dmabuf_to_placement(dev_priv, buf,
167 * Move a buffer to start of vram.
169 * May only be called by the current master as this function takes the
170 * its lock in write mode.
172 * @dev_priv: Driver private.
173 * @buf: DMA buffer to move.
174 * @pin: Pin buffer in vram if true.
175 * @interruptible: Use interruptible wait.
178 * -ERESTARTSYS if interrupted by a signal.
180 int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv,
181 struct vmw_dma_buffer *buf,
182 bool pin, bool interruptible)
184 struct vmw_master *vmaster = dev_priv->active_master;
185 struct ttm_buffer_object *bo = &buf->base;
186 struct ttm_placement placement;
190 placement = vmw_vram_ne_placement;
192 placement = vmw_vram_placement;
193 placement.lpfn = bo->num_pages;
195 ret = ttm_write_lock(&vmaster->lock, interruptible);
196 if (unlikely(ret != 0))
199 ret = ttm_bo_reserve(bo, interruptible, false, false, 0);
200 if (unlikely(ret != 0))
203 /* Is this buffer already in vram but not at the start of it? */
204 if (bo->mem.mem_type == TTM_PL_VRAM &&
205 bo->mem.start < bo->num_pages &&
207 (void) ttm_bo_validate(bo, &vmw_sys_placement, false,
210 ret = ttm_bo_validate(bo, &placement, interruptible, false, false);
212 /* For some reason we didn't up at the start of vram */
213 WARN_ON(ret == 0 && bo->offset != 0);
215 ttm_bo_unreserve(bo);
217 ttm_write_unlock(&vmaster->lock);
223 * Unpin the buffer given buffer, does not move the buffer.
225 * May only be called by the current master as this function takes the
226 * its lock in write mode.
228 * @dev_priv: Driver private.
229 * @buf: DMA buffer to unpin.
230 * @interruptible: Use interruptible wait.
233 * -ERESTARTSYS if interrupted by a signal.
235 int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
236 struct vmw_dma_buffer *buf,
240 * We could in theory early out if the buffer is
241 * unpinned but we need to lock and reserve the buffer
242 * anyways so we don't gain much by that.
244 return vmw_dmabuf_to_placement(dev_priv, buf,
245 &vmw_evictable_placement,
250 * Move a buffer to system memory, does not pin the buffer.
252 * May only be called by the current master as this function takes the
253 * its lock in write mode.
255 * @dev_priv: Driver private.
256 * @buf: DMA buffer to move.
257 * @interruptible: Use interruptible wait.
260 * -ERESTARTSYS if interrupted by a signal.
262 int vmw_dmabuf_to_system(struct vmw_private *dev_priv,
263 struct vmw_dma_buffer *buf,
266 return vmw_dmabuf_to_placement(dev_priv, buf,
271 void vmw_dmabuf_get_id_offset(struct vmw_dma_buffer *buf,
272 uint32_t *gmrId, uint32_t *offset)
274 if (buf->base.mem.mem_type == TTM_PL_VRAM) {
275 *gmrId = SVGA_GMR_FRAMEBUFFER;
276 *offset = buf->base.offset;
278 *gmrId = buf->base.mem.start;
283 void vmw_dmabuf_get_guest_ptr(struct vmw_dma_buffer *buf, SVGAGuestPtr *ptr)
285 if (buf->base.mem.mem_type == TTM_PL_VRAM) {
286 ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
287 ptr->offset = buf->base.offset;
289 ptr->gmrId = buf->base.mem.start;