Commit | Line | Data |
---|---|---|
d991ef03 JB |
1 | /************************************************************************** |
2 | * | |
54fbde8a | 3 | * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA |
d991ef03 JB |
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 | ||
760285e7 | 28 | #include <drm/ttm/ttm_placement.h> |
d991ef03 | 29 | |
760285e7 | 30 | #include <drm/drmP.h> |
d991ef03 JB |
31 | #include "vmwgfx_drv.h" |
32 | ||
33 | ||
34 | /** | |
459d0fa7 | 35 | * vmw_dmabuf_pin_in_placement - Validate a buffer to placement. |
d991ef03 | 36 | * |
b37a6b9a TH |
37 | * @dev_priv: Driver private. |
38 | * @buf: DMA buffer to move. | |
459d0fa7 | 39 | * @placement: The placement to pin it. |
b37a6b9a TH |
40 | * @interruptible: Use interruptible wait. |
41 | * | |
d991ef03 JB |
42 | * Returns |
43 | * -ERESTARTSYS if interrupted by a signal. | |
44 | */ | |
459d0fa7 TH |
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) | |
d991ef03 | 49 | { |
d991ef03 JB |
50 | struct ttm_buffer_object *bo = &buf->base; |
51 | int ret; | |
52 | ||
294adf7d | 53 | ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible); |
d991ef03 JB |
54 | if (unlikely(ret != 0)) |
55 | return ret; | |
56 | ||
c0951b79 | 57 | vmw_execbuf_release_pinned_bo(dev_priv); |
e2fa3a76 | 58 | |
dfd5e50e | 59 | ret = ttm_bo_reserve(bo, interruptible, false, NULL); |
d991ef03 JB |
60 | if (unlikely(ret != 0)) |
61 | goto err; | |
62 | ||
97a875cb | 63 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
459d0fa7 TH |
64 | if (!ret) |
65 | vmw_bo_pin_reserved(buf, true); | |
d991ef03 JB |
66 | |
67 | ttm_bo_unreserve(bo); | |
68 | ||
69 | err: | |
294adf7d | 70 | ttm_write_unlock(&dev_priv->reservation_sem); |
d991ef03 JB |
71 | return ret; |
72 | } | |
73 | ||
74 | /** | |
459d0fa7 | 75 | * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr. |
d991ef03 | 76 | * |
459d0fa7 TH |
77 | * This function takes the reservation_sem in write mode. |
78 | * Flushes and unpins the query bo to avoid failures. | |
d991ef03 JB |
79 | * |
80 | * @dev_priv: Driver private. | |
81 | * @buf: DMA buffer to move. | |
82 | * @pin: Pin buffer if true. | |
83 | * @interruptible: Use interruptible wait. | |
84 | * | |
85 | * Returns | |
86 | * -ERESTARTSYS if interrupted by a signal. | |
87 | */ | |
459d0fa7 TH |
88 | int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv, |
89 | struct vmw_dma_buffer *buf, | |
90 | bool interruptible) | |
d991ef03 | 91 | { |
d991ef03 | 92 | struct ttm_buffer_object *bo = &buf->base; |
d991ef03 JB |
93 | int ret; |
94 | ||
294adf7d | 95 | ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible); |
d991ef03 JB |
96 | if (unlikely(ret != 0)) |
97 | return ret; | |
98 | ||
459d0fa7 | 99 | vmw_execbuf_release_pinned_bo(dev_priv); |
e2fa3a76 | 100 | |
dfd5e50e | 101 | ret = ttm_bo_reserve(bo, interruptible, false, NULL); |
d991ef03 JB |
102 | if (unlikely(ret != 0)) |
103 | goto err; | |
104 | ||
459d0fa7 TH |
105 | ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible, |
106 | false); | |
d991ef03 | 107 | if (likely(ret == 0) || ret == -ERESTARTSYS) |
459d0fa7 | 108 | goto out_unreserve; |
d991ef03 | 109 | |
459d0fa7 | 110 | ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false); |
d991ef03 | 111 | |
459d0fa7 TH |
112 | out_unreserve: |
113 | if (!ret) | |
114 | vmw_bo_pin_reserved(buf, true); | |
d991ef03 | 115 | |
d991ef03 JB |
116 | ttm_bo_unreserve(bo); |
117 | err: | |
294adf7d | 118 | ttm_write_unlock(&dev_priv->reservation_sem); |
d991ef03 JB |
119 | return ret; |
120 | } | |
121 | ||
122 | /** | |
459d0fa7 | 123 | * vmw_dmabuf_pin_in_vram - Move a buffer to vram. |
d991ef03 | 124 | * |
459d0fa7 TH |
125 | * This function takes the reservation_sem in write mode. |
126 | * Flushes and unpins the query bo to avoid failures. | |
d991ef03 JB |
127 | * |
128 | * @dev_priv: Driver private. | |
129 | * @buf: DMA buffer to move. | |
d991ef03 JB |
130 | * @interruptible: Use interruptible wait. |
131 | * | |
132 | * Returns | |
133 | * -ERESTARTSYS if interrupted by a signal. | |
134 | */ | |
459d0fa7 TH |
135 | int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv, |
136 | struct vmw_dma_buffer *buf, | |
137 | bool interruptible) | |
d991ef03 | 138 | { |
459d0fa7 TH |
139 | return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement, |
140 | interruptible); | |
d991ef03 JB |
141 | } |
142 | ||
143 | /** | |
459d0fa7 | 144 | * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram. |
d991ef03 | 145 | * |
459d0fa7 TH |
146 | * This function takes the reservation_sem in write mode. |
147 | * Flushes and unpins the query bo to avoid failures. | |
d991ef03 JB |
148 | * |
149 | * @dev_priv: Driver private. | |
459d0fa7 | 150 | * @buf: DMA buffer to pin. |
d991ef03 JB |
151 | * @interruptible: Use interruptible wait. |
152 | * | |
153 | * Returns | |
154 | * -ERESTARTSYS if interrupted by a signal. | |
155 | */ | |
459d0fa7 TH |
156 | int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *dev_priv, |
157 | struct vmw_dma_buffer *buf, | |
158 | bool interruptible) | |
d991ef03 | 159 | { |
d991ef03 JB |
160 | struct ttm_buffer_object *bo = &buf->base; |
161 | struct ttm_placement placement; | |
f1217ed0 | 162 | struct ttm_place place; |
d991ef03 JB |
163 | int ret = 0; |
164 | ||
459d0fa7 | 165 | place = vmw_vram_placement.placement[0]; |
f1217ed0 | 166 | place.lpfn = bo->num_pages; |
f1217ed0 CK |
167 | placement.num_placement = 1; |
168 | placement.placement = &place; | |
169 | placement.num_busy_placement = 1; | |
170 | placement.busy_placement = &place; | |
d991ef03 | 171 | |
294adf7d | 172 | ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible); |
d991ef03 JB |
173 | if (unlikely(ret != 0)) |
174 | return ret; | |
175 | ||
459d0fa7 | 176 | vmw_execbuf_release_pinned_bo(dev_priv); |
dfd5e50e | 177 | ret = ttm_bo_reserve(bo, interruptible, false, NULL); |
d991ef03 JB |
178 | if (unlikely(ret != 0)) |
179 | goto err_unlock; | |
180 | ||
459d0fa7 TH |
181 | /* |
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 | |
184 | * that situation. | |
185 | */ | |
d991ef03 JB |
186 | if (bo->mem.mem_type == TTM_PL_VRAM && |
187 | bo->mem.start < bo->num_pages && | |
188 | bo->mem.start > 0) | |
97a875cb | 189 | (void) ttm_bo_validate(bo, &vmw_sys_placement, false, false); |
d991ef03 | 190 | |
97a875cb | 191 | ret = ttm_bo_validate(bo, &placement, interruptible, false); |
d991ef03 | 192 | |
459d0fa7 | 193 | /* For some reason we didn't end up at the start of vram */ |
d991ef03 | 194 | WARN_ON(ret == 0 && bo->offset != 0); |
459d0fa7 TH |
195 | if (!ret) |
196 | vmw_bo_pin_reserved(buf, true); | |
d991ef03 JB |
197 | |
198 | ttm_bo_unreserve(bo); | |
199 | err_unlock: | |
294adf7d | 200 | ttm_write_unlock(&dev_priv->reservation_sem); |
d991ef03 JB |
201 | |
202 | return ret; | |
203 | } | |
204 | ||
205 | /** | |
459d0fa7 | 206 | * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer. |
d991ef03 | 207 | * |
459d0fa7 | 208 | * This function takes the reservation_sem in write mode. |
d991ef03 JB |
209 | * |
210 | * @dev_priv: Driver private. | |
211 | * @buf: DMA buffer to unpin. | |
212 | * @interruptible: Use interruptible wait. | |
213 | * | |
214 | * Returns | |
215 | * -ERESTARTSYS if interrupted by a signal. | |
216 | */ | |
217 | int vmw_dmabuf_unpin(struct vmw_private *dev_priv, | |
218 | struct vmw_dma_buffer *buf, | |
219 | bool interruptible) | |
220 | { | |
459d0fa7 TH |
221 | struct ttm_buffer_object *bo = &buf->base; |
222 | int ret; | |
d991ef03 | 223 | |
459d0fa7 TH |
224 | ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible); |
225 | if (unlikely(ret != 0)) | |
226 | return ret; | |
227 | ||
dfd5e50e | 228 | ret = ttm_bo_reserve(bo, interruptible, false, NULL); |
459d0fa7 TH |
229 | if (unlikely(ret != 0)) |
230 | goto err; | |
231 | ||
232 | vmw_bo_pin_reserved(buf, false); | |
233 | ||
234 | ttm_bo_unreserve(bo); | |
235 | ||
236 | err: | |
237 | ttm_read_unlock(&dev_priv->reservation_sem); | |
238 | return ret; | |
239 | } | |
b37a6b9a | 240 | |
d991ef03 | 241 | /** |
b37a6b9a TH |
242 | * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement |
243 | * of a buffer. | |
d991ef03 | 244 | * |
b37a6b9a TH |
245 | * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved. |
246 | * @ptr: SVGAGuestPtr returning the result. | |
d991ef03 | 247 | */ |
b37a6b9a TH |
248 | void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo, |
249 | SVGAGuestPtr *ptr) | |
d991ef03 | 250 | { |
b37a6b9a | 251 | if (bo->mem.mem_type == TTM_PL_VRAM) { |
d991ef03 | 252 | ptr->gmrId = SVGA_GMR_FRAMEBUFFER; |
b37a6b9a | 253 | ptr->offset = bo->offset; |
d991ef03 | 254 | } else { |
b37a6b9a | 255 | ptr->gmrId = bo->mem.start; |
d991ef03 JB |
256 | ptr->offset = 0; |
257 | } | |
258 | } | |
e2fa3a76 TH |
259 | |
260 | ||
261 | /** | |
459d0fa7 | 262 | * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it. |
e2fa3a76 | 263 | * |
459d0fa7 | 264 | * @vbo: The buffer object. Must be reserved. |
e2fa3a76 TH |
265 | * @pin: Whether to pin or unpin. |
266 | * | |
267 | */ | |
459d0fa7 | 268 | void vmw_bo_pin_reserved(struct vmw_dma_buffer *vbo, bool pin) |
e2fa3a76 | 269 | { |
f1217ed0 | 270 | struct ttm_place pl; |
e2fa3a76 | 271 | struct ttm_placement placement; |
459d0fa7 | 272 | struct ttm_buffer_object *bo = &vbo->base; |
e2fa3a76 TH |
273 | uint32_t old_mem_type = bo->mem.mem_type; |
274 | int ret; | |
275 | ||
8bd4ce56 | 276 | lockdep_assert_held(&bo->resv->lock.base); |
e2fa3a76 | 277 | |
459d0fa7 TH |
278 | if (pin) { |
279 | if (vbo->pin_count++ > 0) | |
280 | return; | |
281 | } else { | |
282 | WARN_ON(vbo->pin_count <= 0); | |
283 | if (--vbo->pin_count > 0) | |
284 | return; | |
285 | } | |
286 | ||
f1217ed0 CK |
287 | pl.fpfn = 0; |
288 | pl.lpfn = 0; | |
289 | pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB | |
4b9e45e6 | 290 | | TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; |
e2fa3a76 | 291 | if (pin) |
f1217ed0 | 292 | pl.flags |= TTM_PL_FLAG_NO_EVICT; |
e2fa3a76 TH |
293 | |
294 | memset(&placement, 0, sizeof(placement)); | |
295 | placement.num_placement = 1; | |
f1217ed0 | 296 | placement.placement = &pl; |
e2fa3a76 | 297 | |
97a875cb | 298 | ret = ttm_bo_validate(bo, &placement, false, true); |
e2fa3a76 TH |
299 | |
300 | BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type); | |
301 | } |