Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / vmwgfx / vmwgfx_dmabuf.c
blobd45d2caffa5ad6d51a623d56e312e21b186a5955
1 /**************************************************************************
3 * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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>
30 #include <drm/drmP.h>
31 #include "vmwgfx_drv.h"
34 /**
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.
42 * Returns
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,
48 bool interruptible)
50 struct ttm_operation_ctx ctx = {interruptible, false };
51 struct ttm_buffer_object *bo = &buf->base;
52 int ret;
53 uint32_t new_flags;
55 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
56 if (unlikely(ret != 0))
57 return ret;
59 vmw_execbuf_release_pinned_bo(dev_priv);
61 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
62 if (unlikely(ret != 0))
63 goto err;
65 if (buf->pin_count > 0)
66 ret = ttm_bo_mem_compat(placement, &bo->mem,
67 &new_flags) == true ? 0 : -EINVAL;
68 else
69 ret = ttm_bo_validate(bo, placement, &ctx);
71 if (!ret)
72 vmw_bo_pin_reserved(buf, true);
74 ttm_bo_unreserve(bo);
76 err:
77 ttm_write_unlock(&dev_priv->reservation_sem);
78 return ret;
81 /**
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.
92 * Returns
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,
97 bool interruptible)
99 struct ttm_operation_ctx ctx = {interruptible, false };
100 struct ttm_buffer_object *bo = &buf->base;
101 int ret;
102 uint32_t new_flags;
104 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
105 if (unlikely(ret != 0))
106 return ret;
108 vmw_execbuf_release_pinned_bo(dev_priv);
110 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
111 if (unlikely(ret != 0))
112 goto err;
114 if (buf->pin_count > 0) {
115 ret = ttm_bo_mem_compat(&vmw_vram_gmr_placement, &bo->mem,
116 &new_flags) == true ? 0 : -EINVAL;
117 goto out_unreserve;
120 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx);
121 if (likely(ret == 0) || ret == -ERESTARTSYS)
122 goto out_unreserve;
124 ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx);
126 out_unreserve:
127 if (!ret)
128 vmw_bo_pin_reserved(buf, true);
130 ttm_bo_unreserve(bo);
131 err:
132 ttm_write_unlock(&dev_priv->reservation_sem);
133 return ret;
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.
146 * Returns
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,
151 bool interruptible)
153 return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
154 interruptible);
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.
167 * Returns
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,
172 bool interruptible)
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;
178 int ret = 0;
179 uint32_t new_flags;
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))
190 return ret;
192 vmw_execbuf_release_pinned_bo(dev_priv);
193 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
194 if (unlikely(ret != 0))
195 goto err_unlock;
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
200 * that situation.
202 if (bo->mem.mem_type == TTM_PL_VRAM &&
203 bo->mem.start < bo->num_pages &&
204 bo->mem.start > 0 &&
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;
213 else
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);
218 if (!ret)
219 vmw_bo_pin_reserved(buf, true);
221 ttm_bo_unreserve(bo);
222 err_unlock:
223 ttm_write_unlock(&dev_priv->reservation_sem);
225 return ret;
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.
237 * Returns
238 * -ERESTARTSYS if interrupted by a signal.
240 int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
241 struct vmw_dma_buffer *buf,
242 bool interruptible)
244 struct ttm_buffer_object *bo = &buf->base;
245 int ret;
247 ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible);
248 if (unlikely(ret != 0))
249 return ret;
251 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
252 if (unlikely(ret != 0))
253 goto err;
255 vmw_bo_pin_reserved(buf, false);
257 ttm_bo_unreserve(bo);
259 err:
260 ttm_read_unlock(&dev_priv->reservation_sem);
261 return ret;
265 * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
266 * of a buffer.
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,
272 SVGAGuestPtr *ptr)
274 if (bo->mem.mem_type == TTM_PL_VRAM) {
275 ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
276 ptr->offset = bo->offset;
277 } else {
278 ptr->gmrId = bo->mem.start;
279 ptr->offset = 0;
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 };
294 struct ttm_place pl;
295 struct ttm_placement placement;
296 struct ttm_buffer_object *bo = &vbo->base;
297 uint32_t old_mem_type = bo->mem.mem_type;
298 int ret;
300 lockdep_assert_held(&bo->resv->lock.base);
302 if (pin) {
303 if (vbo->pin_count++ > 0)
304 return;
305 } else {
306 WARN_ON(vbo->pin_count <= 0);
307 if (--vbo->pin_count > 0)
308 return;
311 pl.fpfn = 0;
312 pl.lpfn = 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;
315 if (pin)
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);