5668ad980cb5a058e0d41c7b8ed936d4be2459c6
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / vmwgfx / vmwgfx_dmabuf.c
1 /**************************************************************************
2  *
3  * Copyright © 2011 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
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 "ttm/ttm_placement.h"
29
30 #include "drmP.h"
31 #include "vmwgfx_drv.h"
32
33
34 /**
35  * Validate a buffer to placement.
36  *
37  * May only be called by the current master as this function takes the
38  * its lock in write mode.
39  *
40  * Returns
41  *  -ERESTARTSYS if interrupted by a signal.
42  */
43 int vmw_dmabuf_to_placement(struct vmw_private *dev_priv,
44                             struct vmw_dma_buffer *buf,
45                             struct ttm_placement *placement,
46                             bool interruptible)
47 {
48         struct vmw_master *vmaster = dev_priv->active_master;
49         struct ttm_buffer_object *bo = &buf->base;
50         int ret;
51
52         ret = ttm_write_lock(&vmaster->lock, interruptible);
53         if (unlikely(ret != 0))
54                 return ret;
55
56         ret = ttm_bo_reserve(bo, interruptible, false, false, 0);
57         if (unlikely(ret != 0))
58                 goto err;
59
60         ret = ttm_bo_validate(bo, placement, interruptible, false, false);
61
62         ttm_bo_unreserve(bo);
63
64 err:
65         ttm_write_unlock(&vmaster->lock);
66         return ret;
67 }
68
69 /**
70  * Move a buffer to vram or gmr.
71  *
72  * May only be called by the current master as this function takes the
73  * its lock in write mode.
74  *
75  * @dev_priv:  Driver private.
76  * @buf:  DMA buffer to move.
77  * @pin:  Pin buffer if true.
78  * @interruptible:  Use interruptible wait.
79  *
80  * Returns
81  * -ERESTARTSYS if interrupted by a signal.
82  */
83 int vmw_dmabuf_to_vram_or_gmr(struct vmw_private *dev_priv,
84                               struct vmw_dma_buffer *buf,
85                               bool pin, bool interruptible)
86 {
87         struct vmw_master *vmaster = dev_priv->active_master;
88         struct ttm_buffer_object *bo = &buf->base;
89         struct ttm_placement *placement;
90         int ret;
91
92         ret = ttm_write_lock(&vmaster->lock, interruptible);
93         if (unlikely(ret != 0))
94                 return ret;
95
96         ret = ttm_bo_reserve(bo, interruptible, false, false, 0);
97         if (unlikely(ret != 0))
98                 goto err;
99
100         /**
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.
105          */
106
107         if (pin)
108                 placement = &vmw_vram_gmr_ne_placement;
109         else
110                 placement = &vmw_vram_gmr_placement;
111
112         ret = ttm_bo_validate(bo, placement, interruptible, false, false);
113         if (likely(ret == 0) || ret == -ERESTARTSYS)
114                 goto err_unreserve;
115
116
117         /**
118          * If that failed, try VRAM again, this time evicting
119          * previous contents.
120          */
121
122         if (pin)
123                 placement = &vmw_vram_ne_placement;
124         else
125                 placement = &vmw_vram_placement;
126
127         ret = ttm_bo_validate(bo, placement, interruptible, false, false);
128
129 err_unreserve:
130         ttm_bo_unreserve(bo);
131 err:
132         ttm_write_unlock(&vmaster->lock);
133         return ret;
134 }
135
136 /**
137  * Move a buffer to vram.
138  *
139  * May only be called by the current master as this function takes the
140  * its lock in write mode.
141  *
142  * @dev_priv:  Driver private.
143  * @buf:  DMA buffer to move.
144  * @pin:  Pin buffer in vram if true.
145  * @interruptible:  Use interruptible wait.
146  *
147  * Returns
148  * -ERESTARTSYS if interrupted by a signal.
149  */
150 int vmw_dmabuf_to_vram(struct vmw_private *dev_priv,
151                        struct vmw_dma_buffer *buf,
152                        bool pin, bool interruptible)
153 {
154         struct ttm_placement *placement;
155
156         if (pin)
157                 placement = &vmw_vram_ne_placement;
158         else
159                 placement = &vmw_vram_placement;
160
161         return vmw_dmabuf_to_placement(dev_priv, buf,
162                                        placement,
163                                        interruptible);
164 }
165
166 /**
167  * Move a buffer to start of vram.
168  *
169  * May only be called by the current master as this function takes the
170  * its lock in write mode.
171  *
172  * @dev_priv:  Driver private.
173  * @buf:  DMA buffer to move.
174  * @pin:  Pin buffer in vram if true.
175  * @interruptible:  Use interruptible wait.
176  *
177  * Returns
178  * -ERESTARTSYS if interrupted by a signal.
179  */
180 int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv,
181                                 struct vmw_dma_buffer *buf,
182                                 bool pin, bool interruptible)
183 {
184         struct vmw_master *vmaster = dev_priv->active_master;
185         struct ttm_buffer_object *bo = &buf->base;
186         struct ttm_placement placement;
187         int ret = 0;
188
189         if (pin)
190                 placement = vmw_vram_ne_placement;
191         else
192                 placement = vmw_vram_placement;
193         placement.lpfn = bo->num_pages;
194
195         ret = ttm_write_lock(&vmaster->lock, interruptible);
196         if (unlikely(ret != 0))
197                 return ret;
198
199         ret = ttm_bo_reserve(bo, interruptible, false, false, 0);
200         if (unlikely(ret != 0))
201                 goto err_unlock;
202
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 &&
206             bo->mem.start > 0)
207                 (void) ttm_bo_validate(bo, &vmw_sys_placement, false,
208                                        false, false);
209
210         ret = ttm_bo_validate(bo, &placement, interruptible, false, false);
211
212         /* For some reason we didn't up at the start of vram */
213         WARN_ON(ret == 0 && bo->offset != 0);
214
215         ttm_bo_unreserve(bo);
216 err_unlock:
217         ttm_write_unlock(&vmaster->lock);
218
219         return ret;
220 }
221
222 /**
223  * Unpin the buffer given buffer, does not move the buffer.
224  *
225  * May only be called by the current master as this function takes the
226  * its lock in write mode.
227  *
228  * @dev_priv:  Driver private.
229  * @buf:  DMA buffer to unpin.
230  * @interruptible:  Use interruptible wait.
231  *
232  * Returns
233  * -ERESTARTSYS if interrupted by a signal.
234  */
235 int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
236                      struct vmw_dma_buffer *buf,
237                      bool interruptible)
238 {
239         /*
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.
243          */
244         return vmw_dmabuf_to_placement(dev_priv, buf,
245                                        &vmw_evictable_placement,
246                                        interruptible);
247 }
248
249 /**
250  * Move a buffer to system memory, does not pin the buffer.
251  *
252  * May only be called by the current master as this function takes the
253  * its lock in write mode.
254  *
255  * @dev_priv:  Driver private.
256  * @buf:  DMA buffer to move.
257  * @interruptible:  Use interruptible wait.
258  *
259  * Returns
260  * -ERESTARTSYS if interrupted by a signal.
261  */
262 int vmw_dmabuf_to_system(struct vmw_private *dev_priv,
263                          struct vmw_dma_buffer *buf,
264                          bool interruptible)
265 {
266         return vmw_dmabuf_to_placement(dev_priv, buf,
267                                        &vmw_sys_placement,
268                                        interruptible);
269 }
270
271 void vmw_dmabuf_get_id_offset(struct vmw_dma_buffer *buf,
272                               uint32_t *gmrId, uint32_t *offset)
273 {
274         if (buf->base.mem.mem_type == TTM_PL_VRAM) {
275                 *gmrId = SVGA_GMR_FRAMEBUFFER;
276                 *offset = buf->base.offset;
277         } else {
278                 *gmrId = buf->base.mem.start;
279                 *offset = 0;
280         }
281 }
282
283 void vmw_dmabuf_get_guest_ptr(struct vmw_dma_buffer *buf, SVGAGuestPtr *ptr)
284 {
285         if (buf->base.mem.mem_type == TTM_PL_VRAM) {
286                 ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
287                 ptr->offset = buf->base.offset;
288         } else {
289                 ptr->gmrId = buf->base.mem.start;
290                 ptr->offset = 0;
291         }
292 }