drm/vmwgfx: add SPDX idenitifier and clarify license
[sfrench/cifs-2.6.git] / drivers / gpu / drm / vmwgfx / vmwgfx_dmabuf.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2011-2015 VMware, Inc., Palo Alto, CA., USA
5  *
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:
13  *
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.
17  *
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.
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  * vmw_dmabuf_pin_in_placement - Validate a buffer to placement.
36  *
37  * @dev_priv:  Driver private.
38  * @buf:  DMA buffer to move.
39  * @placement:  The placement to pin it.
40  * @interruptible:  Use interruptible wait.
41  *
42  * Returns
43  *  -ERESTARTSYS if interrupted by a signal.
44  */
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)
49 {
50         struct ttm_operation_ctx ctx = {interruptible, false };
51         struct ttm_buffer_object *bo = &buf->base;
52         int ret;
53         uint32_t new_flags;
54
55         ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
56         if (unlikely(ret != 0))
57                 return ret;
58
59         vmw_execbuf_release_pinned_bo(dev_priv);
60
61         ret = ttm_bo_reserve(bo, interruptible, false, NULL);
62         if (unlikely(ret != 0))
63                 goto err;
64
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);
70
71         if (!ret)
72                 vmw_bo_pin_reserved(buf, true);
73
74         ttm_bo_unreserve(bo);
75
76 err:
77         ttm_write_unlock(&dev_priv->reservation_sem);
78         return ret;
79 }
80
81 /**
82  * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
83  *
84  * This function takes the reservation_sem in write mode.
85  * Flushes and unpins the query bo to avoid failures.
86  *
87  * @dev_priv:  Driver private.
88  * @buf:  DMA buffer to move.
89  * @pin:  Pin buffer if true.
90  * @interruptible:  Use interruptible wait.
91  *
92  * Returns
93  * -ERESTARTSYS if interrupted by a signal.
94  */
95 int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
96                                   struct vmw_dma_buffer *buf,
97                                   bool interruptible)
98 {
99         struct ttm_operation_ctx ctx = {interruptible, false };
100         struct ttm_buffer_object *bo = &buf->base;
101         int ret;
102         uint32_t new_flags;
103
104         ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
105         if (unlikely(ret != 0))
106                 return ret;
107
108         vmw_execbuf_release_pinned_bo(dev_priv);
109
110         ret = ttm_bo_reserve(bo, interruptible, false, NULL);
111         if (unlikely(ret != 0))
112                 goto err;
113
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;
118         }
119
120         ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx);
121         if (likely(ret == 0) || ret == -ERESTARTSYS)
122                 goto out_unreserve;
123
124         ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx);
125
126 out_unreserve:
127         if (!ret)
128                 vmw_bo_pin_reserved(buf, true);
129
130         ttm_bo_unreserve(bo);
131 err:
132         ttm_write_unlock(&dev_priv->reservation_sem);
133         return ret;
134 }
135
136 /**
137  * vmw_dmabuf_pin_in_vram - Move a buffer to vram.
138  *
139  * This function takes the reservation_sem in write mode.
140  * Flushes and unpins the query bo to avoid failures.
141  *
142  * @dev_priv:  Driver private.
143  * @buf:  DMA buffer to move.
144  * @interruptible:  Use interruptible wait.
145  *
146  * Returns
147  * -ERESTARTSYS if interrupted by a signal.
148  */
149 int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv,
150                            struct vmw_dma_buffer *buf,
151                            bool interruptible)
152 {
153         return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
154                                            interruptible);
155 }
156
157 /**
158  * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram.
159  *
160  * This function takes the reservation_sem in write mode.
161  * Flushes and unpins the query bo to avoid failures.
162  *
163  * @dev_priv:  Driver private.
164  * @buf:  DMA buffer to pin.
165  * @interruptible:  Use interruptible wait.
166  *
167  * Returns
168  * -ERESTARTSYS if interrupted by a signal.
169  */
170 int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *dev_priv,
171                                     struct vmw_dma_buffer *buf,
172                                     bool interruptible)
173 {
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;
180
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;
187
188         ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
189         if (unlikely(ret != 0))
190                 return ret;
191
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;
196
197         /*
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.
201          */
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);
208         }
209
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);
215
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);
220
221         ttm_bo_unreserve(bo);
222 err_unlock:
223         ttm_write_unlock(&dev_priv->reservation_sem);
224
225         return ret;
226 }
227
228 /**
229  * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer.
230  *
231  * This function takes the reservation_sem in write mode.
232  *
233  * @dev_priv:  Driver private.
234  * @buf:  DMA buffer to unpin.
235  * @interruptible:  Use interruptible wait.
236  *
237  * Returns
238  * -ERESTARTSYS if interrupted by a signal.
239  */
240 int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
241                      struct vmw_dma_buffer *buf,
242                      bool interruptible)
243 {
244         struct ttm_buffer_object *bo = &buf->base;
245         int ret;
246
247         ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible);
248         if (unlikely(ret != 0))
249                 return ret;
250
251         ret = ttm_bo_reserve(bo, interruptible, false, NULL);
252         if (unlikely(ret != 0))
253                 goto err;
254
255         vmw_bo_pin_reserved(buf, false);
256
257         ttm_bo_unreserve(bo);
258
259 err:
260         ttm_read_unlock(&dev_priv->reservation_sem);
261         return ret;
262 }
263
264 /**
265  * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
266  * of a buffer.
267  *
268  * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
269  * @ptr: SVGAGuestPtr returning the result.
270  */
271 void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
272                           SVGAGuestPtr *ptr)
273 {
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;
280         }
281 }
282
283
284 /**
285  * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
286  *
287  * @vbo: The buffer object. Must be reserved.
288  * @pin: Whether to pin or unpin.
289  *
290  */
291 void vmw_bo_pin_reserved(struct vmw_dma_buffer *vbo, bool pin)
292 {
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;
299
300         lockdep_assert_held(&bo->resv->lock.base);
301
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;
309         }
310
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;
317
318         memset(&placement, 0, sizeof(placement));
319         placement.num_placement = 1;
320         placement.placement = &pl;
321
322         ret = ttm_bo_validate(bo, &placement, &ctx);
323
324         BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type);
325 }
326
327
328 /*
329  * vmw_dma_buffer_unmap - Tear down a cached buffer object map.
330  *
331  * @vbo: The buffer object whose map we are tearing down.
332  *
333  * This function tears down a cached map set up using
334  * vmw_dma_buffer_map_and_cache().
335  */
336 void vmw_dma_buffer_unmap(struct vmw_dma_buffer *vbo)
337 {
338         if (vbo->map.bo == NULL)
339                 return;
340
341         ttm_bo_kunmap(&vbo->map);
342 }
343
344
345 /*
346  * vmw_dma_buffer_map_and_cache - Map a buffer object and cache the map
347  *
348  * @vbo: The buffer object to map
349  * Return: A kernel virtual address or NULL if mapping failed.
350  *
351  * This function maps a buffer object into the kernel address space, or
352  * returns the virtual kernel address of an already existing map. The virtual
353  * address remains valid as long as the buffer object is pinned or reserved.
354  * The cached map is torn down on either
355  * 1) Buffer object move
356  * 2) Buffer object swapout
357  * 3) Buffer object destruction
358  *
359  */
360 void *vmw_dma_buffer_map_and_cache(struct vmw_dma_buffer *vbo)
361 {
362         struct ttm_buffer_object *bo = &vbo->base;
363         bool not_used;
364         void *virtual;
365         int ret;
366
367         virtual = ttm_kmap_obj_virtual(&vbo->map, &not_used);
368         if (virtual)
369                 return virtual;
370
371         ret = ttm_bo_kmap(bo, 0, bo->num_pages, &vbo->map);
372         if (ret)
373                 DRM_ERROR("Buffer object map failed: %d.\n", ret);
374
375         return ttm_kmap_obj_virtual(&vbo->map, &not_used);
376 }