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 | |
| 28 | #include "ttm/ttm_placement.h" |
| 29 | |
| 30 | #include "drmP.h" |
| 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. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 45 | * |
| 46 | * Returns |
| 47 | * -ERESTARTSYS if interrupted by a signal. |
| 48 | */ |
| 49 | int vmw_dmabuf_to_placement(struct vmw_private *dev_priv, |
| 50 | struct vmw_dma_buffer *buf, |
| 51 | struct ttm_placement *placement, |
| 52 | bool interruptible) |
| 53 | { |
| 54 | struct vmw_master *vmaster = dev_priv->active_master; |
| 55 | struct ttm_buffer_object *bo = &buf->base; |
| 56 | int ret; |
| 57 | |
| 58 | ret = ttm_write_lock(&vmaster->lock, interruptible); |
| 59 | if (unlikely(ret != 0)) |
| 60 | return ret; |
| 61 | |
| 62 | ret = ttm_bo_reserve(bo, interruptible, false, false, 0); |
| 63 | if (unlikely(ret != 0)) |
| 64 | goto err; |
| 65 | |
| 66 | ret = ttm_bo_validate(bo, placement, interruptible, false, false); |
| 67 | |
| 68 | ttm_bo_unreserve(bo); |
| 69 | |
| 70 | err: |
| 71 | ttm_write_unlock(&vmaster->lock); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 76 | * 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] | 77 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 78 | * May only be called by the current master since it assumes that the |
| 79 | * master lock is the current master's lock. |
| 80 | * This function takes the master's lock in write mode. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 81 | * |
| 82 | * @dev_priv: Driver private. |
| 83 | * @buf: DMA buffer to move. |
| 84 | * @pin: Pin buffer if true. |
| 85 | * @interruptible: Use interruptible wait. |
| 86 | * |
| 87 | * Returns |
| 88 | * -ERESTARTSYS if interrupted by a signal. |
| 89 | */ |
| 90 | int vmw_dmabuf_to_vram_or_gmr(struct vmw_private *dev_priv, |
| 91 | struct vmw_dma_buffer *buf, |
| 92 | bool pin, bool interruptible) |
| 93 | { |
| 94 | struct vmw_master *vmaster = dev_priv->active_master; |
| 95 | struct ttm_buffer_object *bo = &buf->base; |
| 96 | struct ttm_placement *placement; |
| 97 | int ret; |
| 98 | |
| 99 | ret = ttm_write_lock(&vmaster->lock, interruptible); |
| 100 | if (unlikely(ret != 0)) |
| 101 | return ret; |
| 102 | |
| 103 | ret = ttm_bo_reserve(bo, interruptible, false, false, 0); |
| 104 | if (unlikely(ret != 0)) |
| 105 | goto err; |
| 106 | |
| 107 | /** |
| 108 | * Put BO in VRAM if there is space, otherwise as a GMR. |
| 109 | * If there is no space in VRAM and GMR ids are all used up, |
| 110 | * start evicting GMRs to make room. If the DMA buffer can't be |
| 111 | * used as a GMR, this will return -ENOMEM. |
| 112 | */ |
| 113 | |
| 114 | if (pin) |
| 115 | placement = &vmw_vram_gmr_ne_placement; |
| 116 | else |
| 117 | placement = &vmw_vram_gmr_placement; |
| 118 | |
| 119 | ret = ttm_bo_validate(bo, placement, interruptible, false, false); |
| 120 | if (likely(ret == 0) || ret == -ERESTARTSYS) |
| 121 | goto err_unreserve; |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * If that failed, try VRAM again, this time evicting |
| 126 | * previous contents. |
| 127 | */ |
| 128 | |
| 129 | if (pin) |
| 130 | placement = &vmw_vram_ne_placement; |
| 131 | else |
| 132 | placement = &vmw_vram_placement; |
| 133 | |
| 134 | ret = ttm_bo_validate(bo, placement, interruptible, false, false); |
| 135 | |
| 136 | err_unreserve: |
| 137 | ttm_bo_unreserve(bo); |
| 138 | err: |
| 139 | ttm_write_unlock(&vmaster->lock); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 144 | * vmw_dmabuf_to_vram - Move a buffer to vram. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 145 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 146 | * May only be called by the current master since it assumes that the |
| 147 | * master lock is the current master's lock. |
| 148 | * This function takes the master's lock in write mode. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 149 | * |
| 150 | * @dev_priv: Driver private. |
| 151 | * @buf: DMA buffer to move. |
| 152 | * @pin: Pin buffer in vram if true. |
| 153 | * @interruptible: Use interruptible wait. |
| 154 | * |
| 155 | * Returns |
| 156 | * -ERESTARTSYS if interrupted by a signal. |
| 157 | */ |
| 158 | int vmw_dmabuf_to_vram(struct vmw_private *dev_priv, |
| 159 | struct vmw_dma_buffer *buf, |
| 160 | bool pin, bool interruptible) |
| 161 | { |
| 162 | struct ttm_placement *placement; |
| 163 | |
| 164 | if (pin) |
| 165 | placement = &vmw_vram_ne_placement; |
| 166 | else |
| 167 | placement = &vmw_vram_placement; |
| 168 | |
| 169 | return vmw_dmabuf_to_placement(dev_priv, buf, |
| 170 | placement, |
| 171 | interruptible); |
| 172 | } |
| 173 | |
| 174 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 175 | * 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] | 176 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 177 | * May only be called by the current master since it assumes that the |
| 178 | * master lock is the current master's lock. |
| 179 | * This function takes the master's lock in write mode. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 180 | * |
| 181 | * @dev_priv: Driver private. |
| 182 | * @buf: DMA buffer to move. |
| 183 | * @pin: Pin buffer in vram if true. |
| 184 | * @interruptible: Use interruptible wait. |
| 185 | * |
| 186 | * Returns |
| 187 | * -ERESTARTSYS if interrupted by a signal. |
| 188 | */ |
| 189 | int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv, |
| 190 | struct vmw_dma_buffer *buf, |
| 191 | bool pin, bool interruptible) |
| 192 | { |
| 193 | struct vmw_master *vmaster = dev_priv->active_master; |
| 194 | struct ttm_buffer_object *bo = &buf->base; |
| 195 | struct ttm_placement placement; |
| 196 | int ret = 0; |
| 197 | |
| 198 | if (pin) |
| 199 | placement = vmw_vram_ne_placement; |
| 200 | else |
| 201 | placement = vmw_vram_placement; |
| 202 | placement.lpfn = bo->num_pages; |
| 203 | |
| 204 | ret = ttm_write_lock(&vmaster->lock, interruptible); |
| 205 | if (unlikely(ret != 0)) |
| 206 | return ret; |
| 207 | |
| 208 | ret = ttm_bo_reserve(bo, interruptible, false, false, 0); |
| 209 | if (unlikely(ret != 0)) |
| 210 | goto err_unlock; |
| 211 | |
| 212 | /* Is this buffer already in vram but not at the start of it? */ |
| 213 | if (bo->mem.mem_type == TTM_PL_VRAM && |
| 214 | bo->mem.start < bo->num_pages && |
| 215 | bo->mem.start > 0) |
| 216 | (void) ttm_bo_validate(bo, &vmw_sys_placement, false, |
| 217 | false, false); |
| 218 | |
| 219 | ret = ttm_bo_validate(bo, &placement, interruptible, false, false); |
| 220 | |
| 221 | /* For some reason we didn't up at the start of vram */ |
| 222 | WARN_ON(ret == 0 && bo->offset != 0); |
| 223 | |
| 224 | ttm_bo_unreserve(bo); |
| 225 | err_unlock: |
| 226 | ttm_write_unlock(&vmaster->lock); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 231 | |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 232 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 233 | * 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] | 234 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 235 | * May only be called by the current master since it assumes that the |
| 236 | * master lock is the current master's lock. |
| 237 | * This function takes the master's lock in write mode. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 238 | * |
| 239 | * @dev_priv: Driver private. |
| 240 | * @buf: DMA buffer to unpin. |
| 241 | * @interruptible: Use interruptible wait. |
| 242 | * |
| 243 | * Returns |
| 244 | * -ERESTARTSYS if interrupted by a signal. |
| 245 | */ |
| 246 | int vmw_dmabuf_unpin(struct vmw_private *dev_priv, |
| 247 | struct vmw_dma_buffer *buf, |
| 248 | bool interruptible) |
| 249 | { |
| 250 | /* |
| 251 | * We could in theory early out if the buffer is |
| 252 | * unpinned but we need to lock and reserve the buffer |
| 253 | * anyways so we don't gain much by that. |
| 254 | */ |
| 255 | return vmw_dmabuf_to_placement(dev_priv, buf, |
| 256 | &vmw_evictable_placement, |
| 257 | interruptible); |
| 258 | } |
| 259 | |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 260 | |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 261 | /** |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 262 | * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement |
| 263 | * of a buffer. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 264 | * |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 265 | * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved. |
| 266 | * @ptr: SVGAGuestPtr returning the result. |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 267 | */ |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 268 | void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo, |
| 269 | SVGAGuestPtr *ptr) |
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 | if (bo->mem.mem_type == TTM_PL_VRAM) { |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 272 | ptr->gmrId = SVGA_GMR_FRAMEBUFFER; |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 273 | ptr->offset = bo->offset; |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 274 | } else { |
Thomas Hellstrom | b37a6b9 | 2011-10-04 20:13:28 +0200 | [diff] [blame^] | 275 | ptr->gmrId = bo->mem.start; |
Jakob Bornecrantz | d991ef0 | 2011-10-04 20:13:21 +0200 | [diff] [blame] | 276 | ptr->offset = 0; |
| 277 | } |
| 278 | } |