Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/ttm/ttm_placement.h> |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 29 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/drmP.h> |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 31 | #include "vmwgfx_drv.h" |
| 32 | |
| 33 | |
| 34 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 35 | * vmw_dmabuf_to_placement - Validate a buffer to placement. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 36 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 37 | * @dev_priv: Driver private. |
| 38 | * @buf: DMA buffer to move. |
| 39 | * @pin: Pin buffer if true. |
| 40 | * @interruptible: Use interruptible wait. |
| 41 | * |
| 42 | * May only be called by the current master since it assumes that the |
| 43 | * master lock is the current master's lock. |
| 44 | * This function takes the master's lock in write mode. |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 45 | * Flushes and unpins the query bo to avoid failures. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 46 | * |
| 47 | * Returns |
| 48 | * -ERESTARTSYS if interrupted by a signal. |
| 49 | */ |
| 50 | int vmw_dmabuf_to_placement(struct vmw_private *dev_priv, |
| 51 | struct vmw_dma_buffer *buf, |
| 52 | struct ttm_placement *placement, |
| 53 | bool interruptible) |
| 54 | { |
| 55 | struct vmw_master *vmaster = dev_priv->active_master; |
| 56 | struct ttm_buffer_object *bo = &buf->base; |
| 57 | int ret; |
| 58 | |
| 59 | ret = ttm_write_lock(&vmaster->lock, interruptible); |
| 60 | if (unlikely(ret != 0)) |
| 61 | return ret; |
| 62 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 63 | vmw_execbuf_release_pinned_bo(dev_priv); |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 64 | |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 65 | ret = ttm_bo_reserve(bo, interruptible, false, false, 0); |
| 66 | if (unlikely(ret != 0)) |
| 67 | goto err; |
| 68 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame^] | 69 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 70 | |
| 71 | ttm_bo_unreserve(bo); |
| 72 | |
| 73 | err: |
| 74 | ttm_write_unlock(&vmaster->lock); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 79 | * vmw_dmabuf_to_vram_or_gmr - Move a buffer to vram or gmr. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 80 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 81 | * May only be called by the current master since it assumes that the |
| 82 | * master lock is the current master's lock. |
| 83 | * This function takes the master's lock in write mode. |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 84 | * Flushes and unpins the query bo if @pin == true to avoid failures. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 85 | * |
| 86 | * @dev_priv: Driver private. |
| 87 | * @buf: DMA buffer to move. |
| 88 | * @pin: Pin buffer if true. |
| 89 | * @interruptible: Use interruptible wait. |
| 90 | * |
| 91 | * Returns |
| 92 | * -ERESTARTSYS if interrupted by a signal. |
| 93 | */ |
| 94 | int vmw_dmabuf_to_vram_or_gmr(struct vmw_private *dev_priv, |
| 95 | struct vmw_dma_buffer *buf, |
| 96 | bool pin, bool interruptible) |
| 97 | { |
| 98 | struct vmw_master *vmaster = dev_priv->active_master; |
| 99 | struct ttm_buffer_object *bo = &buf->base; |
| 100 | struct ttm_placement *placement; |
| 101 | int ret; |
| 102 | |
| 103 | ret = ttm_write_lock(&vmaster->lock, interruptible); |
| 104 | if (unlikely(ret != 0)) |
| 105 | return ret; |
| 106 | |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 107 | if (pin) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 108 | vmw_execbuf_release_pinned_bo(dev_priv); |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 109 | |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 110 | ret = ttm_bo_reserve(bo, interruptible, false, false, 0); |
| 111 | if (unlikely(ret != 0)) |
| 112 | goto err; |
| 113 | |
| 114 | /** |
| 115 | * Put BO in VRAM if there is space, otherwise as a GMR. |
| 116 | * If there is no space in VRAM and GMR ids are all used up, |
| 117 | * start evicting GMRs to make room. If the DMA buffer can't be |
| 118 | * used as a GMR, this will return -ENOMEM. |
| 119 | */ |
| 120 | |
| 121 | if (pin) |
| 122 | placement = &vmw_vram_gmr_ne_placement; |
| 123 | else |
| 124 | placement = &vmw_vram_gmr_placement; |
| 125 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame^] | 126 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 127 | if (likely(ret == 0) || ret == -ERESTARTSYS) |
| 128 | goto err_unreserve; |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * If that failed, try VRAM again, this time evicting |
| 133 | * previous contents. |
| 134 | */ |
| 135 | |
| 136 | if (pin) |
| 137 | placement = &vmw_vram_ne_placement; |
| 138 | else |
| 139 | placement = &vmw_vram_placement; |
| 140 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame^] | 141 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 142 | |
| 143 | err_unreserve: |
| 144 | ttm_bo_unreserve(bo); |
| 145 | err: |
| 146 | ttm_write_unlock(&vmaster->lock); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 151 | * vmw_dmabuf_to_vram - Move a buffer to vram. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 152 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 153 | * May only be called by the current master since it assumes that the |
| 154 | * master lock is the current master's lock. |
| 155 | * This function takes the master's lock in write mode. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 156 | * |
| 157 | * @dev_priv: Driver private. |
| 158 | * @buf: DMA buffer to move. |
| 159 | * @pin: Pin buffer in vram if true. |
| 160 | * @interruptible: Use interruptible wait. |
| 161 | * |
| 162 | * Returns |
| 163 | * -ERESTARTSYS if interrupted by a signal. |
| 164 | */ |
| 165 | int vmw_dmabuf_to_vram(struct vmw_private *dev_priv, |
| 166 | struct vmw_dma_buffer *buf, |
| 167 | bool pin, bool interruptible) |
| 168 | { |
| 169 | struct ttm_placement *placement; |
| 170 | |
| 171 | if (pin) |
| 172 | placement = &vmw_vram_ne_placement; |
| 173 | else |
| 174 | placement = &vmw_vram_placement; |
| 175 | |
| 176 | return vmw_dmabuf_to_placement(dev_priv, buf, |
| 177 | placement, |
| 178 | interruptible); |
| 179 | } |
| 180 | |
| 181 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 182 | * vmw_dmabuf_to_start_of_vram - Move a buffer to start of vram. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 183 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 184 | * May only be called by the current master since it assumes that the |
| 185 | * master lock is the current master's lock. |
| 186 | * This function takes the master's lock in write mode. |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 187 | * Flushes and unpins the query bo if @pin == true to avoid failures. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 188 | * |
| 189 | * @dev_priv: Driver private. |
| 190 | * @buf: DMA buffer to move. |
| 191 | * @pin: Pin buffer in vram if true. |
| 192 | * @interruptible: Use interruptible wait. |
| 193 | * |
| 194 | * Returns |
| 195 | * -ERESTARTSYS if interrupted by a signal. |
| 196 | */ |
| 197 | int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv, |
| 198 | struct vmw_dma_buffer *buf, |
| 199 | bool pin, bool interruptible) |
| 200 | { |
| 201 | struct vmw_master *vmaster = dev_priv->active_master; |
| 202 | struct ttm_buffer_object *bo = &buf->base; |
| 203 | struct ttm_placement placement; |
| 204 | int ret = 0; |
| 205 | |
| 206 | if (pin) |
| 207 | placement = vmw_vram_ne_placement; |
| 208 | else |
| 209 | placement = vmw_vram_placement; |
| 210 | placement.lpfn = bo->num_pages; |
| 211 | |
| 212 | ret = ttm_write_lock(&vmaster->lock, interruptible); |
| 213 | if (unlikely(ret != 0)) |
| 214 | return ret; |
| 215 | |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 216 | if (pin) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 217 | vmw_execbuf_release_pinned_bo(dev_priv); |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 218 | ret = ttm_bo_reserve(bo, interruptible, false, false, 0); |
| 219 | if (unlikely(ret != 0)) |
| 220 | goto err_unlock; |
| 221 | |
| 222 | /* Is this buffer already in vram but not at the start of it? */ |
| 223 | if (bo->mem.mem_type == TTM_PL_VRAM && |
| 224 | bo->mem.start < bo->num_pages && |
| 225 | bo->mem.start > 0) |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame^] | 226 | (void) ttm_bo_validate(bo, &vmw_sys_placement, false, false); |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 227 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame^] | 228 | ret = ttm_bo_validate(bo, &placement, interruptible, false); |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 229 | |
| 230 | /* For some reason we didn't up at the start of vram */ |
| 231 | WARN_ON(ret == 0 && bo->offset != 0); |
| 232 | |
| 233 | ttm_bo_unreserve(bo); |
| 234 | err_unlock: |
| 235 | ttm_write_unlock(&vmaster->lock); |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 240 | |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 241 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 242 | * vmw_dmabuf_upin - Unpin the buffer given buffer, does not move the buffer. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 243 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 244 | * May only be called by the current master since it assumes that the |
| 245 | * master lock is the current master's lock. |
| 246 | * This function takes the master's lock in write mode. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 247 | * |
| 248 | * @dev_priv: Driver private. |
| 249 | * @buf: DMA buffer to unpin. |
| 250 | * @interruptible: Use interruptible wait. |
| 251 | * |
| 252 | * Returns |
| 253 | * -ERESTARTSYS if interrupted by a signal. |
| 254 | */ |
| 255 | int vmw_dmabuf_unpin(struct vmw_private *dev_priv, |
| 256 | struct vmw_dma_buffer *buf, |
| 257 | bool interruptible) |
| 258 | { |
| 259 | /* |
| 260 | * We could in theory early out if the buffer is |
| 261 | * unpinned but we need to lock and reserve the buffer |
| 262 | * anyways so we don't gain much by that. |
| 263 | */ |
| 264 | return vmw_dmabuf_to_placement(dev_priv, buf, |
| 265 | &vmw_evictable_placement, |
| 266 | interruptible); |
| 267 | } |
| 268 | |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 269 | |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 270 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 271 | * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement |
| 272 | * of a buffer. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 273 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 274 | * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved. |
| 275 | * @ptr: SVGAGuestPtr returning the result. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 276 | */ |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 277 | void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo, |
| 278 | SVGAGuestPtr *ptr) |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 279 | { |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 280 | if (bo->mem.mem_type == TTM_PL_VRAM) { |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 281 | ptr->gmrId = SVGA_GMR_FRAMEBUFFER; |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 282 | ptr->offset = bo->offset; |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 283 | } else { |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame] | 284 | ptr->gmrId = bo->mem.start; |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 285 | ptr->offset = 0; |
| 286 | } |
| 287 | } |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 288 | |
| 289 | |
| 290 | /** |
| 291 | * vmw_bo_pin - Pin or unpin a buffer object without moving it. |
| 292 | * |
| 293 | * @bo: The buffer object. Must be reserved, and present either in VRAM |
| 294 | * or GMR memory. |
| 295 | * @pin: Whether to pin or unpin. |
| 296 | * |
| 297 | */ |
| 298 | void vmw_bo_pin(struct ttm_buffer_object *bo, bool pin) |
| 299 | { |
| 300 | uint32_t pl_flags; |
| 301 | struct ttm_placement placement; |
| 302 | uint32_t old_mem_type = bo->mem.mem_type; |
| 303 | int ret; |
| 304 | |
Maarten Lankhorst | 94baf0e | 2012-10-12 14:59:50 +0000 | [diff] [blame] | 305 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 306 | BUG_ON(old_mem_type != TTM_PL_VRAM && |
Thomas Hellstrom | afcc87a | 2012-11-09 10:45:14 +0100 | [diff] [blame] | 307 | old_mem_type != VMW_PL_GMR); |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 308 | |
| 309 | pl_flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED; |
| 310 | if (pin) |
| 311 | pl_flags |= TTM_PL_FLAG_NO_EVICT; |
| 312 | |
| 313 | memset(&placement, 0, sizeof(placement)); |
| 314 | placement.num_placement = 1; |
| 315 | placement.placement = &pl_flags; |
| 316 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame^] | 317 | ret = ttm_bo_validate(bo, &placement, false, true); |
Thomas Hellstrom | e2fa3a7 | 2011-10-04 20:13:30 +0200 | [diff] [blame] | 318 | |
| 319 | BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type); |
| 320 | } |