Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright © 2009 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 "vmwgfx_drv.h" |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/vmwgfx_drm.h> |
| 30 | #include <drm/ttm/ttm_object.h> |
| 31 | #include <drm/ttm/ttm_placement.h> |
| 32 | #include <drm/drmP.h> |
Thomas Hellstrom | 543831c | 2012-11-20 12:19:36 +0000 | [diff] [blame] | 33 | #include "vmwgfx_resource_priv.h" |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 34 | |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 35 | #define VMW_RES_EVICT_ERR_COUNT 10 |
| 36 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 37 | struct vmw_user_dma_buffer { |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 38 | struct ttm_prime_object prime; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 39 | struct vmw_dma_buffer dma; |
| 40 | }; |
| 41 | |
| 42 | struct vmw_bo_user_rep { |
| 43 | uint32_t handle; |
| 44 | uint64_t map_handle; |
| 45 | }; |
| 46 | |
| 47 | struct vmw_stream { |
| 48 | struct vmw_resource res; |
| 49 | uint32_t stream_id; |
| 50 | }; |
| 51 | |
| 52 | struct vmw_user_stream { |
| 53 | struct ttm_base_object base; |
| 54 | struct vmw_stream stream; |
| 55 | }; |
| 56 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 57 | |
| 58 | static uint64_t vmw_user_stream_size; |
| 59 | |
| 60 | static const struct vmw_res_func vmw_stream_func = { |
| 61 | .res_type = vmw_res_stream, |
| 62 | .needs_backup = false, |
| 63 | .may_evict = false, |
| 64 | .type_name = "video streams", |
| 65 | .backup_placement = NULL, |
| 66 | .create = NULL, |
| 67 | .destroy = NULL, |
| 68 | .bind = NULL, |
| 69 | .unbind = NULL |
| 70 | }; |
| 71 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 72 | static inline struct vmw_dma_buffer * |
| 73 | vmw_dma_buffer(struct ttm_buffer_object *bo) |
| 74 | { |
| 75 | return container_of(bo, struct vmw_dma_buffer, base); |
| 76 | } |
| 77 | |
| 78 | static inline struct vmw_user_dma_buffer * |
| 79 | vmw_user_dma_buffer(struct ttm_buffer_object *bo) |
| 80 | { |
| 81 | struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo); |
| 82 | return container_of(vmw_bo, struct vmw_user_dma_buffer, dma); |
| 83 | } |
| 84 | |
| 85 | struct vmw_resource *vmw_resource_reference(struct vmw_resource *res) |
| 86 | { |
| 87 | kref_get(&res->kref); |
| 88 | return res; |
| 89 | } |
| 90 | |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * vmw_resource_release_id - release a resource id to the id manager. |
| 94 | * |
| 95 | * @res: Pointer to the resource. |
| 96 | * |
| 97 | * Release the resource id to the resource id manager and set it to -1 |
| 98 | */ |
Thomas Hellstrom | 543831c | 2012-11-20 12:19:36 +0000 | [diff] [blame] | 99 | void vmw_resource_release_id(struct vmw_resource *res) |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 100 | { |
| 101 | struct vmw_private *dev_priv = res->dev_priv; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 102 | struct idr *idr = &dev_priv->res_idr[res->func->res_type]; |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 103 | |
| 104 | write_lock(&dev_priv->resource_lock); |
| 105 | if (res->id != -1) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 106 | idr_remove(idr, res->id); |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 107 | res->id = -1; |
| 108 | write_unlock(&dev_priv->resource_lock); |
| 109 | } |
| 110 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 111 | static void vmw_resource_release(struct kref *kref) |
| 112 | { |
| 113 | struct vmw_resource *res = |
| 114 | container_of(kref, struct vmw_resource, kref); |
| 115 | struct vmw_private *dev_priv = res->dev_priv; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 116 | int id; |
| 117 | struct idr *idr = &dev_priv->res_idr[res->func->res_type]; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 118 | |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 119 | res->avail = false; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 120 | list_del_init(&res->lru_head); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 121 | write_unlock(&dev_priv->resource_lock); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 122 | if (res->backup) { |
| 123 | struct ttm_buffer_object *bo = &res->backup->base; |
| 124 | |
| 125 | ttm_bo_reserve(bo, false, false, false, 0); |
| 126 | if (!list_empty(&res->mob_head) && |
| 127 | res->func->unbind != NULL) { |
| 128 | struct ttm_validate_buffer val_buf; |
| 129 | |
| 130 | val_buf.bo = bo; |
| 131 | res->func->unbind(res, false, &val_buf); |
| 132 | } |
| 133 | res->backup_dirty = false; |
| 134 | list_del_init(&res->mob_head); |
| 135 | ttm_bo_unreserve(bo); |
| 136 | vmw_dmabuf_unreference(&res->backup); |
| 137 | } |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 138 | |
| 139 | if (likely(res->hw_destroy != NULL)) |
| 140 | res->hw_destroy(res); |
| 141 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 142 | id = res->id; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 143 | if (res->res_free != NULL) |
| 144 | res->res_free(res); |
| 145 | else |
| 146 | kfree(res); |
| 147 | |
| 148 | write_lock(&dev_priv->resource_lock); |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 149 | |
| 150 | if (id != -1) |
| 151 | idr_remove(idr, id); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void vmw_resource_unreference(struct vmw_resource **p_res) |
| 155 | { |
| 156 | struct vmw_resource *res = *p_res; |
| 157 | struct vmw_private *dev_priv = res->dev_priv; |
| 158 | |
| 159 | *p_res = NULL; |
| 160 | write_lock(&dev_priv->resource_lock); |
| 161 | kref_put(&res->kref, vmw_resource_release); |
| 162 | write_unlock(&dev_priv->resource_lock); |
| 163 | } |
| 164 | |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * vmw_resource_alloc_id - release a resource id to the id manager. |
| 168 | * |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 169 | * @res: Pointer to the resource. |
| 170 | * |
| 171 | * Allocate the lowest free resource from the resource manager, and set |
| 172 | * @res->id to that id. Returns 0 on success and -ENOMEM on failure. |
| 173 | */ |
Thomas Hellstrom | 543831c | 2012-11-20 12:19:36 +0000 | [diff] [blame] | 174 | int vmw_resource_alloc_id(struct vmw_resource *res) |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 175 | { |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 176 | struct vmw_private *dev_priv = res->dev_priv; |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 177 | int ret; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 178 | struct idr *idr = &dev_priv->res_idr[res->func->res_type]; |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 179 | |
| 180 | BUG_ON(res->id != -1); |
| 181 | |
Tejun Heo | cc39a8f | 2013-02-27 17:04:14 -0800 | [diff] [blame] | 182 | idr_preload(GFP_KERNEL); |
| 183 | write_lock(&dev_priv->resource_lock); |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 184 | |
Tejun Heo | cc39a8f | 2013-02-27 17:04:14 -0800 | [diff] [blame] | 185 | ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT); |
| 186 | if (ret >= 0) |
| 187 | res->id = ret; |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 188 | |
Tejun Heo | cc39a8f | 2013-02-27 17:04:14 -0800 | [diff] [blame] | 189 | write_unlock(&dev_priv->resource_lock); |
| 190 | idr_preload_end(); |
| 191 | return ret < 0 ? ret : 0; |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 194 | /** |
| 195 | * vmw_resource_init - initialize a struct vmw_resource |
| 196 | * |
| 197 | * @dev_priv: Pointer to a device private struct. |
| 198 | * @res: The struct vmw_resource to initialize. |
| 199 | * @obj_type: Resource object type. |
| 200 | * @delay_id: Boolean whether to defer device id allocation until |
| 201 | * the first validation. |
| 202 | * @res_free: Resource destructor. |
| 203 | * @func: Resource function table. |
| 204 | */ |
Thomas Hellstrom | 543831c | 2012-11-20 12:19:36 +0000 | [diff] [blame] | 205 | int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res, |
| 206 | bool delay_id, |
| 207 | void (*res_free) (struct vmw_resource *res), |
| 208 | const struct vmw_res_func *func) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 209 | { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 210 | kref_init(&res->kref); |
| 211 | res->hw_destroy = NULL; |
| 212 | res->res_free = res_free; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 213 | res->avail = false; |
| 214 | res->dev_priv = dev_priv; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 215 | res->func = func; |
| 216 | INIT_LIST_HEAD(&res->lru_head); |
| 217 | INIT_LIST_HEAD(&res->mob_head); |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 218 | res->id = -1; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 219 | res->backup = NULL; |
| 220 | res->backup_offset = 0; |
| 221 | res->backup_dirty = false; |
| 222 | res->res_dirty = false; |
Thomas Hellstrom | 5bb39e8 | 2011-10-04 20:13:33 +0200 | [diff] [blame] | 223 | if (delay_id) |
| 224 | return 0; |
| 225 | else |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 226 | return vmw_resource_alloc_id(res); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * vmw_resource_activate |
| 231 | * |
| 232 | * @res: Pointer to the newly created resource |
| 233 | * @hw_destroy: Destroy function. NULL if none. |
| 234 | * |
| 235 | * Activate a resource after the hardware has been made aware of it. |
| 236 | * Set tye destroy function to @destroy. Typically this frees the |
| 237 | * resource and destroys the hardware resources associated with it. |
| 238 | * Activate basically means that the function vmw_resource_lookup will |
| 239 | * find it. |
| 240 | */ |
Thomas Hellstrom | 543831c | 2012-11-20 12:19:36 +0000 | [diff] [blame] | 241 | void vmw_resource_activate(struct vmw_resource *res, |
| 242 | void (*hw_destroy) (struct vmw_resource *)) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 243 | { |
| 244 | struct vmw_private *dev_priv = res->dev_priv; |
| 245 | |
| 246 | write_lock(&dev_priv->resource_lock); |
| 247 | res->avail = true; |
| 248 | res->hw_destroy = hw_destroy; |
| 249 | write_unlock(&dev_priv->resource_lock); |
| 250 | } |
| 251 | |
| 252 | struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv, |
| 253 | struct idr *idr, int id) |
| 254 | { |
| 255 | struct vmw_resource *res; |
| 256 | |
| 257 | read_lock(&dev_priv->resource_lock); |
| 258 | res = idr_find(idr, id); |
| 259 | if (res && res->avail) |
| 260 | kref_get(&res->kref); |
| 261 | else |
| 262 | res = NULL; |
| 263 | read_unlock(&dev_priv->resource_lock); |
| 264 | |
| 265 | if (unlikely(res == NULL)) |
| 266 | return NULL; |
| 267 | |
| 268 | return res; |
| 269 | } |
| 270 | |
| 271 | /** |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 272 | * vmw_user_resource_lookup_handle - lookup a struct resource from a |
| 273 | * TTM user-space handle and perform basic type checks |
| 274 | * |
| 275 | * @dev_priv: Pointer to a device private struct |
| 276 | * @tfile: Pointer to a struct ttm_object_file identifying the caller |
| 277 | * @handle: The TTM user-space handle |
| 278 | * @converter: Pointer to an object describing the resource type |
| 279 | * @p_res: On successful return the location pointed to will contain |
| 280 | * a pointer to a refcounted struct vmw_resource. |
| 281 | * |
| 282 | * If the handle can't be found or is associated with an incorrect resource |
| 283 | * type, -EINVAL will be returned. |
| 284 | */ |
| 285 | int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv, |
| 286 | struct ttm_object_file *tfile, |
| 287 | uint32_t handle, |
| 288 | const struct vmw_user_resource_conv |
| 289 | *converter, |
| 290 | struct vmw_resource **p_res) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 291 | { |
Thomas Hellstrom | 7a73ba7 | 2009-12-22 16:53:41 +0100 | [diff] [blame] | 292 | struct ttm_base_object *base; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 293 | struct vmw_resource *res; |
| 294 | int ret = -EINVAL; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 295 | |
Thomas Hellstrom | 7a73ba7 | 2009-12-22 16:53:41 +0100 | [diff] [blame] | 296 | base = ttm_base_object_lookup(tfile, handle); |
| 297 | if (unlikely(base == NULL)) |
| 298 | return -EINVAL; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 299 | |
Thomas Hellstrom | 79e5f81 | 2013-11-08 02:12:51 -0800 | [diff] [blame] | 300 | if (unlikely(ttm_base_object_type(base) != converter->object_type)) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 301 | goto out_bad_resource; |
Thomas Hellstrom | 7a73ba7 | 2009-12-22 16:53:41 +0100 | [diff] [blame] | 302 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 303 | res = converter->base_obj_to_res(base); |
| 304 | |
| 305 | read_lock(&dev_priv->resource_lock); |
| 306 | if (!res->avail || res->res_free != converter->res_free) { |
| 307 | read_unlock(&dev_priv->resource_lock); |
| 308 | goto out_bad_resource; |
| 309 | } |
| 310 | |
| 311 | kref_get(&res->kref); |
| 312 | read_unlock(&dev_priv->resource_lock); |
| 313 | |
| 314 | *p_res = res; |
Thomas Hellstrom | 7a73ba7 | 2009-12-22 16:53:41 +0100 | [diff] [blame] | 315 | ret = 0; |
| 316 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 317 | out_bad_resource: |
Thomas Hellstrom | 7a73ba7 | 2009-12-22 16:53:41 +0100 | [diff] [blame] | 318 | ttm_base_object_unref(&base); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 319 | |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Helper function that looks either a surface or dmabuf. |
| 325 | * |
| 326 | * The pointer this pointed at by out_surf and out_buf needs to be null. |
| 327 | */ |
| 328 | int vmw_user_lookup_handle(struct vmw_private *dev_priv, |
| 329 | struct ttm_object_file *tfile, |
| 330 | uint32_t handle, |
| 331 | struct vmw_surface **out_surf, |
| 332 | struct vmw_dma_buffer **out_buf) |
| 333 | { |
| 334 | struct vmw_resource *res; |
| 335 | int ret; |
| 336 | |
| 337 | BUG_ON(*out_surf || *out_buf); |
| 338 | |
| 339 | ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle, |
| 340 | user_surface_converter, |
| 341 | &res); |
| 342 | if (!ret) { |
| 343 | *out_surf = vmw_res_to_srf(res); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | *out_surf = NULL; |
| 348 | ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Buffer management. |
| 354 | */ |
Thomas Hellstrom | 308d17e | 2013-11-28 01:46:56 -0800 | [diff] [blame] | 355 | |
| 356 | /** |
| 357 | * vmw_dmabuf_acc_size - Calculate the pinned memory usage of buffers |
| 358 | * |
| 359 | * @dev_priv: Pointer to a struct vmw_private identifying the device. |
| 360 | * @size: The requested buffer size. |
| 361 | * @user: Whether this is an ordinary dma buffer or a user dma buffer. |
| 362 | */ |
| 363 | static size_t vmw_dmabuf_acc_size(struct vmw_private *dev_priv, size_t size, |
| 364 | bool user) |
| 365 | { |
| 366 | static size_t struct_size, user_struct_size; |
| 367 | size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 368 | size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *)); |
| 369 | |
| 370 | if (unlikely(struct_size == 0)) { |
| 371 | size_t backend_size = ttm_round_pot(vmw_tt_size); |
| 372 | |
| 373 | struct_size = backend_size + |
| 374 | ttm_round_pot(sizeof(struct vmw_dma_buffer)); |
| 375 | user_struct_size = backend_size + |
| 376 | ttm_round_pot(sizeof(struct vmw_user_dma_buffer)); |
| 377 | } |
| 378 | |
| 379 | if (dev_priv->map_mode == vmw_dma_alloc_coherent) |
| 380 | page_array_size += |
| 381 | ttm_round_pot(num_pages * sizeof(dma_addr_t)); |
| 382 | |
| 383 | return ((user) ? user_struct_size : struct_size) + |
| 384 | page_array_size; |
| 385 | } |
| 386 | |
Thomas Hellstrom | effe110 | 2010-01-13 22:28:39 +0100 | [diff] [blame] | 387 | void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo) |
| 388 | { |
| 389 | struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo); |
Thomas Hellstrom | effe110 | 2010-01-13 22:28:39 +0100 | [diff] [blame] | 390 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 391 | kfree(vmw_bo); |
| 392 | } |
| 393 | |
Thomas Hellstrom | 308d17e | 2013-11-28 01:46:56 -0800 | [diff] [blame] | 394 | static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo) |
| 395 | { |
| 396 | struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo); |
| 397 | |
| 398 | ttm_prime_object_kfree(vmw_user_bo, prime); |
| 399 | } |
| 400 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 401 | int vmw_dmabuf_init(struct vmw_private *dev_priv, |
| 402 | struct vmw_dma_buffer *vmw_bo, |
| 403 | size_t size, struct ttm_placement *placement, |
| 404 | bool interruptible, |
| 405 | void (*bo_free) (struct ttm_buffer_object *bo)) |
| 406 | { |
| 407 | struct ttm_bo_device *bdev = &dev_priv->bdev; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 408 | size_t acc_size; |
| 409 | int ret; |
Thomas Hellstrom | 308d17e | 2013-11-28 01:46:56 -0800 | [diff] [blame] | 410 | bool user = (bo_free == &vmw_user_dmabuf_destroy); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 411 | |
Thomas Hellstrom | 308d17e | 2013-11-28 01:46:56 -0800 | [diff] [blame] | 412 | BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free))); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 413 | |
Thomas Hellstrom | 308d17e | 2013-11-28 01:46:56 -0800 | [diff] [blame] | 414 | acc_size = vmw_dmabuf_acc_size(dev_priv, size, user); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 415 | memset(vmw_bo, 0, sizeof(*vmw_bo)); |
| 416 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 417 | INIT_LIST_HEAD(&vmw_bo->res_list); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 418 | |
| 419 | ret = ttm_bo_init(bdev, &vmw_bo->base, size, |
Thomas Hellstrom | 308d17e | 2013-11-28 01:46:56 -0800 | [diff] [blame] | 420 | (user) ? ttm_bo_type_device : |
| 421 | ttm_bo_type_kernel, placement, |
Marcin Slusarz | 0b91c4a | 2012-11-06 21:49:51 +0000 | [diff] [blame] | 422 | 0, interruptible, |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 423 | NULL, acc_size, NULL, bo_free); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 424 | return ret; |
| 425 | } |
| 426 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 427 | static void vmw_user_dmabuf_release(struct ttm_base_object **p_base) |
| 428 | { |
| 429 | struct vmw_user_dma_buffer *vmw_user_bo; |
| 430 | struct ttm_base_object *base = *p_base; |
| 431 | struct ttm_buffer_object *bo; |
| 432 | |
| 433 | *p_base = NULL; |
| 434 | |
| 435 | if (unlikely(base == NULL)) |
| 436 | return; |
| 437 | |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 438 | vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, |
| 439 | prime.base); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 440 | bo = &vmw_user_bo->dma.base; |
| 441 | ttm_bo_unref(&bo); |
| 442 | } |
| 443 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 444 | /** |
| 445 | * vmw_user_dmabuf_alloc - Allocate a user dma buffer |
| 446 | * |
| 447 | * @dev_priv: Pointer to a struct device private. |
| 448 | * @tfile: Pointer to a struct ttm_object_file on which to register the user |
| 449 | * object. |
| 450 | * @size: Size of the dma buffer. |
| 451 | * @shareable: Boolean whether the buffer is shareable with other open files. |
| 452 | * @handle: Pointer to where the handle value should be assigned. |
| 453 | * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer |
| 454 | * should be assigned. |
| 455 | */ |
| 456 | int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv, |
| 457 | struct ttm_object_file *tfile, |
| 458 | uint32_t size, |
| 459 | bool shareable, |
| 460 | uint32_t *handle, |
| 461 | struct vmw_dma_buffer **p_dma_buf) |
| 462 | { |
| 463 | struct vmw_user_dma_buffer *user_bo; |
| 464 | struct ttm_buffer_object *tmp; |
| 465 | int ret; |
| 466 | |
| 467 | user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL); |
| 468 | if (unlikely(user_bo == NULL)) { |
| 469 | DRM_ERROR("Failed to allocate a buffer.\n"); |
| 470 | return -ENOMEM; |
| 471 | } |
| 472 | |
| 473 | ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size, |
Thomas Hellstrom | 96c5f0d | 2012-11-21 11:19:53 +0100 | [diff] [blame] | 474 | (dev_priv->has_mob) ? |
| 475 | &vmw_sys_placement : |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 476 | &vmw_vram_sys_placement, true, |
| 477 | &vmw_user_dmabuf_destroy); |
| 478 | if (unlikely(ret != 0)) |
| 479 | return ret; |
| 480 | |
| 481 | tmp = ttm_bo_reference(&user_bo->dma.base); |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 482 | ret = ttm_prime_object_init(tfile, |
| 483 | size, |
| 484 | &user_bo->prime, |
| 485 | shareable, |
| 486 | ttm_buffer_type, |
| 487 | &vmw_user_dmabuf_release, NULL); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 488 | if (unlikely(ret != 0)) { |
| 489 | ttm_bo_unref(&tmp); |
| 490 | goto out_no_base_object; |
| 491 | } |
| 492 | |
| 493 | *p_dma_buf = &user_bo->dma; |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 494 | *handle = user_bo->prime.base.hash.key; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 495 | |
| 496 | out_no_base_object: |
| 497 | return ret; |
| 498 | } |
| 499 | |
Thomas Hellstrom | d08a9b9 | 2012-11-21 16:04:18 +0100 | [diff] [blame] | 500 | /** |
| 501 | * vmw_user_dmabuf_verify_access - verify access permissions on this |
| 502 | * buffer object. |
| 503 | * |
| 504 | * @bo: Pointer to the buffer object being accessed |
| 505 | * @tfile: Identifying the caller. |
| 506 | */ |
| 507 | int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo, |
| 508 | struct ttm_object_file *tfile) |
| 509 | { |
| 510 | struct vmw_user_dma_buffer *vmw_user_bo; |
| 511 | |
| 512 | if (unlikely(bo->destroy != vmw_user_dmabuf_destroy)) |
| 513 | return -EPERM; |
| 514 | |
| 515 | vmw_user_bo = vmw_user_dma_buffer(bo); |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 516 | return (vmw_user_bo->prime.base.tfile == tfile || |
| 517 | vmw_user_bo->prime.base.shareable) ? 0 : -EPERM; |
Thomas Hellstrom | d08a9b9 | 2012-11-21 16:04:18 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 520 | int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data, |
| 521 | struct drm_file *file_priv) |
| 522 | { |
| 523 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 524 | union drm_vmw_alloc_dmabuf_arg *arg = |
| 525 | (union drm_vmw_alloc_dmabuf_arg *)data; |
| 526 | struct drm_vmw_alloc_dmabuf_req *req = &arg->req; |
| 527 | struct drm_vmw_dmabuf_rep *rep = &arg->rep; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 528 | struct vmw_dma_buffer *dma_buf; |
| 529 | uint32_t handle; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 530 | struct vmw_master *vmaster = vmw_master(file_priv->master); |
| 531 | int ret; |
| 532 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 533 | ret = ttm_read_lock(&vmaster->lock, true); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 534 | if (unlikely(ret != 0)) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 535 | return ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 536 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 537 | ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile, |
| 538 | req->size, false, &handle, &dma_buf); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 539 | if (unlikely(ret != 0)) |
Thomas Hellstrom | 2f5993c | 2010-11-17 13:24:48 +0100 | [diff] [blame] | 540 | goto out_no_dmabuf; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 541 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 542 | rep->handle = handle; |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 543 | rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 544 | rep->cur_gmr_id = handle; |
| 545 | rep->cur_gmr_offset = 0; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 546 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 547 | vmw_dmabuf_unreference(&dma_buf); |
| 548 | |
Thomas Hellstrom | 2f5993c | 2010-11-17 13:24:48 +0100 | [diff] [blame] | 549 | out_no_dmabuf: |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 550 | ttm_read_unlock(&vmaster->lock); |
| 551 | |
Thomas Hellstrom | 2f5993c | 2010-11-17 13:24:48 +0100 | [diff] [blame] | 552 | return ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data, |
| 556 | struct drm_file *file_priv) |
| 557 | { |
| 558 | struct drm_vmw_unref_dmabuf_arg *arg = |
| 559 | (struct drm_vmw_unref_dmabuf_arg *)data; |
| 560 | |
| 561 | return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, |
| 562 | arg->handle, |
| 563 | TTM_REF_USAGE); |
| 564 | } |
| 565 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 566 | int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile, |
| 567 | uint32_t handle, struct vmw_dma_buffer **out) |
| 568 | { |
| 569 | struct vmw_user_dma_buffer *vmw_user_bo; |
| 570 | struct ttm_base_object *base; |
| 571 | |
| 572 | base = ttm_base_object_lookup(tfile, handle); |
| 573 | if (unlikely(base == NULL)) { |
| 574 | printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n", |
| 575 | (unsigned long)handle); |
| 576 | return -ESRCH; |
| 577 | } |
| 578 | |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 579 | if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 580 | ttm_base_object_unref(&base); |
| 581 | printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n", |
| 582 | (unsigned long)handle); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 586 | vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, |
| 587 | prime.base); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 588 | (void)ttm_bo_reference(&vmw_user_bo->dma.base); |
| 589 | ttm_base_object_unref(&base); |
| 590 | *out = &vmw_user_bo->dma; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 595 | int vmw_user_dmabuf_reference(struct ttm_object_file *tfile, |
| 596 | struct vmw_dma_buffer *dma_buf) |
| 597 | { |
| 598 | struct vmw_user_dma_buffer *user_bo; |
| 599 | |
| 600 | if (dma_buf->base.destroy != vmw_user_dmabuf_destroy) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma); |
Thomas Hellstrom | c486d4f | 2013-11-08 02:30:50 -0800 | [diff] [blame] | 604 | return ttm_ref_object_add(tfile, &user_bo->prime.base, |
| 605 | TTM_REF_USAGE, NULL); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 608 | /* |
Uwe Kleine-König | 65155b3 | 2010-06-11 12:17:01 +0200 | [diff] [blame] | 609 | * Stream management |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 610 | */ |
| 611 | |
| 612 | static void vmw_stream_destroy(struct vmw_resource *res) |
| 613 | { |
| 614 | struct vmw_private *dev_priv = res->dev_priv; |
| 615 | struct vmw_stream *stream; |
| 616 | int ret; |
| 617 | |
| 618 | DRM_INFO("%s: unref\n", __func__); |
| 619 | stream = container_of(res, struct vmw_stream, res); |
| 620 | |
| 621 | ret = vmw_overlay_unref(dev_priv, stream->stream_id); |
| 622 | WARN_ON(ret != 0); |
| 623 | } |
| 624 | |
| 625 | static int vmw_stream_init(struct vmw_private *dev_priv, |
| 626 | struct vmw_stream *stream, |
| 627 | void (*res_free) (struct vmw_resource *res)) |
| 628 | { |
| 629 | struct vmw_resource *res = &stream->res; |
| 630 | int ret; |
| 631 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 632 | ret = vmw_resource_init(dev_priv, res, false, res_free, |
| 633 | &vmw_stream_func); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 634 | |
| 635 | if (unlikely(ret != 0)) { |
| 636 | if (res_free == NULL) |
| 637 | kfree(stream); |
| 638 | else |
| 639 | res_free(&stream->res); |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | ret = vmw_overlay_claim(dev_priv, &stream->stream_id); |
| 644 | if (ret) { |
| 645 | vmw_resource_unreference(&res); |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | DRM_INFO("%s: claimed\n", __func__); |
| 650 | |
| 651 | vmw_resource_activate(&stream->res, vmw_stream_destroy); |
| 652 | return 0; |
| 653 | } |
| 654 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 655 | static void vmw_user_stream_free(struct vmw_resource *res) |
| 656 | { |
| 657 | struct vmw_user_stream *stream = |
| 658 | container_of(res, struct vmw_user_stream, stream.res); |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 659 | struct vmw_private *dev_priv = res->dev_priv; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 660 | |
Thomas Hellstrom | cdad052 | 2012-11-06 11:31:50 +0000 | [diff] [blame] | 661 | ttm_base_object_kfree(stream, base); |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 662 | ttm_mem_global_free(vmw_mem_glob(dev_priv), |
| 663 | vmw_user_stream_size); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
| 667 | * This function is called when user space has no more references on the |
| 668 | * base object. It releases the base-object's reference on the resource object. |
| 669 | */ |
| 670 | |
| 671 | static void vmw_user_stream_base_release(struct ttm_base_object **p_base) |
| 672 | { |
| 673 | struct ttm_base_object *base = *p_base; |
| 674 | struct vmw_user_stream *stream = |
| 675 | container_of(base, struct vmw_user_stream, base); |
| 676 | struct vmw_resource *res = &stream->stream.res; |
| 677 | |
| 678 | *p_base = NULL; |
| 679 | vmw_resource_unreference(&res); |
| 680 | } |
| 681 | |
| 682 | int vmw_stream_unref_ioctl(struct drm_device *dev, void *data, |
| 683 | struct drm_file *file_priv) |
| 684 | { |
| 685 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 686 | struct vmw_resource *res; |
| 687 | struct vmw_user_stream *stream; |
| 688 | struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data; |
| 689 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 690 | struct idr *idr = &dev_priv->res_idr[vmw_res_stream]; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 691 | int ret = 0; |
| 692 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 693 | |
| 694 | res = vmw_resource_lookup(dev_priv, idr, arg->stream_id); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 695 | if (unlikely(res == NULL)) |
| 696 | return -EINVAL; |
| 697 | |
| 698 | if (res->res_free != &vmw_user_stream_free) { |
| 699 | ret = -EINVAL; |
| 700 | goto out; |
| 701 | } |
| 702 | |
| 703 | stream = container_of(res, struct vmw_user_stream, stream.res); |
| 704 | if (stream->base.tfile != tfile) { |
| 705 | ret = -EINVAL; |
| 706 | goto out; |
| 707 | } |
| 708 | |
| 709 | ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE); |
| 710 | out: |
| 711 | vmw_resource_unreference(&res); |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | int vmw_stream_claim_ioctl(struct drm_device *dev, void *data, |
| 716 | struct drm_file *file_priv) |
| 717 | { |
| 718 | struct vmw_private *dev_priv = vmw_priv(dev); |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 719 | struct vmw_user_stream *stream; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 720 | struct vmw_resource *res; |
| 721 | struct vmw_resource *tmp; |
| 722 | struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data; |
| 723 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 724 | struct vmw_master *vmaster = vmw_master(file_priv->master); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 725 | int ret; |
| 726 | |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 727 | /* |
| 728 | * Approximate idr memory usage with 128 bytes. It will be limited |
| 729 | * by maximum number_of streams anyway? |
| 730 | */ |
| 731 | |
| 732 | if (unlikely(vmw_user_stream_size == 0)) |
| 733 | vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128; |
| 734 | |
| 735 | ret = ttm_read_lock(&vmaster->lock, true); |
| 736 | if (unlikely(ret != 0)) |
| 737 | return ret; |
| 738 | |
| 739 | ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), |
| 740 | vmw_user_stream_size, |
| 741 | false, true); |
| 742 | if (unlikely(ret != 0)) { |
| 743 | if (ret != -ERESTARTSYS) |
| 744 | DRM_ERROR("Out of graphics memory for stream" |
| 745 | " creation.\n"); |
| 746 | goto out_unlock; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | stream = kmalloc(sizeof(*stream), GFP_KERNEL); |
| 751 | if (unlikely(stream == NULL)) { |
| 752 | ttm_mem_global_free(vmw_mem_glob(dev_priv), |
| 753 | vmw_user_stream_size); |
| 754 | ret = -ENOMEM; |
| 755 | goto out_unlock; |
| 756 | } |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 757 | |
| 758 | res = &stream->stream.res; |
| 759 | stream->base.shareable = false; |
| 760 | stream->base.tfile = NULL; |
| 761 | |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 762 | /* |
| 763 | * From here on, the destructor takes over resource freeing. |
| 764 | */ |
| 765 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 766 | ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free); |
| 767 | if (unlikely(ret != 0)) |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 768 | goto out_unlock; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 769 | |
| 770 | tmp = vmw_resource_reference(res); |
| 771 | ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM, |
| 772 | &vmw_user_stream_base_release, NULL); |
| 773 | |
| 774 | if (unlikely(ret != 0)) { |
| 775 | vmw_resource_unreference(&tmp); |
| 776 | goto out_err; |
| 777 | } |
| 778 | |
| 779 | arg->stream_id = res->id; |
| 780 | out_err: |
| 781 | vmw_resource_unreference(&res); |
Thomas Hellstrom | 414ee50 | 2011-10-07 15:23:06 +0200 | [diff] [blame] | 782 | out_unlock: |
| 783 | ttm_read_unlock(&vmaster->lock); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | int vmw_user_stream_lookup(struct vmw_private *dev_priv, |
| 788 | struct ttm_object_file *tfile, |
| 789 | uint32_t *inout_id, struct vmw_resource **out) |
| 790 | { |
| 791 | struct vmw_user_stream *stream; |
| 792 | struct vmw_resource *res; |
| 793 | int ret; |
| 794 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 795 | res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream], |
| 796 | *inout_id); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 797 | if (unlikely(res == NULL)) |
| 798 | return -EINVAL; |
| 799 | |
| 800 | if (res->res_free != &vmw_user_stream_free) { |
| 801 | ret = -EINVAL; |
| 802 | goto err_ref; |
| 803 | } |
| 804 | |
| 805 | stream = container_of(res, struct vmw_user_stream, stream.res); |
| 806 | if (stream->base.tfile != tfile) { |
| 807 | ret = -EPERM; |
| 808 | goto err_ref; |
| 809 | } |
| 810 | |
| 811 | *inout_id = stream->stream.stream_id; |
| 812 | *out = res; |
| 813 | return 0; |
| 814 | err_ref: |
| 815 | vmw_resource_unreference(&res); |
| 816 | return ret; |
| 817 | } |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 818 | |
| 819 | |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 820 | /** |
| 821 | * vmw_dumb_create - Create a dumb kms buffer |
| 822 | * |
| 823 | * @file_priv: Pointer to a struct drm_file identifying the caller. |
| 824 | * @dev: Pointer to the drm device. |
| 825 | * @args: Pointer to a struct drm_mode_create_dumb structure |
| 826 | * |
| 827 | * This is a driver callback for the core drm create_dumb functionality. |
| 828 | * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except |
| 829 | * that the arguments have a different format. |
| 830 | */ |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 831 | int vmw_dumb_create(struct drm_file *file_priv, |
| 832 | struct drm_device *dev, |
| 833 | struct drm_mode_create_dumb *args) |
| 834 | { |
| 835 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 836 | struct vmw_master *vmaster = vmw_master(file_priv->master); |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 837 | struct vmw_dma_buffer *dma_buf; |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 838 | int ret; |
| 839 | |
| 840 | args->pitch = args->width * ((args->bpp + 7) / 8); |
| 841 | args->size = args->pitch * args->height; |
| 842 | |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 843 | ret = ttm_read_lock(&vmaster->lock, true); |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 844 | if (unlikely(ret != 0)) |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 845 | return ret; |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 846 | |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 847 | ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile, |
| 848 | args->size, false, &args->handle, |
| 849 | &dma_buf); |
| 850 | if (unlikely(ret != 0)) |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 851 | goto out_no_dmabuf; |
| 852 | |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 853 | vmw_dmabuf_unreference(&dma_buf); |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 854 | out_no_dmabuf: |
| 855 | ttm_read_unlock(&vmaster->lock); |
| 856 | return ret; |
| 857 | } |
| 858 | |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 859 | /** |
| 860 | * vmw_dumb_map_offset - Return the address space offset of a dumb buffer |
| 861 | * |
| 862 | * @file_priv: Pointer to a struct drm_file identifying the caller. |
| 863 | * @dev: Pointer to the drm device. |
| 864 | * @handle: Handle identifying the dumb buffer. |
| 865 | * @offset: The address space offset returned. |
| 866 | * |
| 867 | * This is a driver callback for the core drm dumb_map_offset functionality. |
| 868 | */ |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 869 | int vmw_dumb_map_offset(struct drm_file *file_priv, |
| 870 | struct drm_device *dev, uint32_t handle, |
| 871 | uint64_t *offset) |
| 872 | { |
| 873 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
| 874 | struct vmw_dma_buffer *out_buf; |
| 875 | int ret; |
| 876 | |
| 877 | ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf); |
| 878 | if (ret != 0) |
| 879 | return -EINVAL; |
| 880 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 881 | *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node); |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 882 | vmw_dmabuf_unreference(&out_buf); |
| 883 | return 0; |
| 884 | } |
| 885 | |
Thomas Hellstrom | d69d51d | 2013-11-28 00:28:30 -0800 | [diff] [blame] | 886 | /** |
| 887 | * vmw_dumb_destroy - Destroy a dumb boffer |
| 888 | * |
| 889 | * @file_priv: Pointer to a struct drm_file identifying the caller. |
| 890 | * @dev: Pointer to the drm device. |
| 891 | * @handle: Handle identifying the dumb buffer. |
| 892 | * |
| 893 | * This is a driver callback for the core drm dumb_destroy functionality. |
| 894 | */ |
Dave Airlie | 5e1782d | 2012-08-28 01:53:54 +0000 | [diff] [blame] | 895 | int vmw_dumb_destroy(struct drm_file *file_priv, |
| 896 | struct drm_device *dev, |
| 897 | uint32_t handle) |
| 898 | { |
| 899 | return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, |
| 900 | handle, TTM_REF_USAGE); |
| 901 | } |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 902 | |
| 903 | /** |
| 904 | * vmw_resource_buf_alloc - Allocate a backup buffer for a resource. |
| 905 | * |
| 906 | * @res: The resource for which to allocate a backup buffer. |
| 907 | * @interruptible: Whether any sleeps during allocation should be |
| 908 | * performed while interruptible. |
| 909 | */ |
| 910 | static int vmw_resource_buf_alloc(struct vmw_resource *res, |
| 911 | bool interruptible) |
| 912 | { |
| 913 | unsigned long size = |
| 914 | (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK; |
| 915 | struct vmw_dma_buffer *backup; |
| 916 | int ret; |
| 917 | |
| 918 | if (likely(res->backup)) { |
| 919 | BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size); |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | backup = kzalloc(sizeof(*backup), GFP_KERNEL); |
| 924 | if (unlikely(backup == NULL)) |
| 925 | return -ENOMEM; |
| 926 | |
| 927 | ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size, |
| 928 | res->func->backup_placement, |
| 929 | interruptible, |
| 930 | &vmw_dmabuf_bo_free); |
| 931 | if (unlikely(ret != 0)) |
| 932 | goto out_no_dmabuf; |
| 933 | |
| 934 | res->backup = backup; |
| 935 | |
| 936 | out_no_dmabuf: |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * vmw_resource_do_validate - Make a resource up-to-date and visible |
| 942 | * to the device. |
| 943 | * |
| 944 | * @res: The resource to make visible to the device. |
| 945 | * @val_buf: Information about a buffer possibly |
| 946 | * containing backup data if a bind operation is needed. |
| 947 | * |
| 948 | * On hardware resource shortage, this function returns -EBUSY and |
| 949 | * should be retried once resources have been freed up. |
| 950 | */ |
| 951 | static int vmw_resource_do_validate(struct vmw_resource *res, |
| 952 | struct ttm_validate_buffer *val_buf) |
| 953 | { |
| 954 | int ret = 0; |
| 955 | const struct vmw_res_func *func = res->func; |
| 956 | |
| 957 | if (unlikely(res->id == -1)) { |
| 958 | ret = func->create(res); |
| 959 | if (unlikely(ret != 0)) |
| 960 | return ret; |
| 961 | } |
| 962 | |
| 963 | if (func->bind && |
| 964 | ((func->needs_backup && list_empty(&res->mob_head) && |
| 965 | val_buf->bo != NULL) || |
| 966 | (!func->needs_backup && val_buf->bo != NULL))) { |
| 967 | ret = func->bind(res, val_buf); |
| 968 | if (unlikely(ret != 0)) |
| 969 | goto out_bind_failed; |
| 970 | if (func->needs_backup) |
| 971 | list_add_tail(&res->mob_head, &res->backup->res_list); |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Only do this on write operations, and move to |
| 976 | * vmw_resource_unreserve if it can be called after |
| 977 | * backup buffers have been unreserved. Otherwise |
| 978 | * sort out locking. |
| 979 | */ |
| 980 | res->res_dirty = true; |
| 981 | |
| 982 | return 0; |
| 983 | |
| 984 | out_bind_failed: |
| 985 | func->destroy(res); |
| 986 | |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * vmw_resource_unreserve - Unreserve a resource previously reserved for |
| 992 | * command submission. |
| 993 | * |
| 994 | * @res: Pointer to the struct vmw_resource to unreserve. |
| 995 | * @new_backup: Pointer to new backup buffer if command submission |
| 996 | * switched. |
| 997 | * @new_backup_offset: New backup offset if @new_backup is !NULL. |
| 998 | * |
| 999 | * Currently unreserving a resource means putting it back on the device's |
| 1000 | * resource lru list, so that it can be evicted if necessary. |
| 1001 | */ |
| 1002 | void vmw_resource_unreserve(struct vmw_resource *res, |
| 1003 | struct vmw_dma_buffer *new_backup, |
| 1004 | unsigned long new_backup_offset) |
| 1005 | { |
| 1006 | struct vmw_private *dev_priv = res->dev_priv; |
| 1007 | |
| 1008 | if (!list_empty(&res->lru_head)) |
| 1009 | return; |
| 1010 | |
| 1011 | if (new_backup && new_backup != res->backup) { |
| 1012 | |
| 1013 | if (res->backup) { |
Maarten Lankhorst | 8bd4ce5 | 2013-06-27 13:48:27 +0200 | [diff] [blame] | 1014 | lockdep_assert_held(&res->backup->base.resv->lock.base); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1015 | list_del_init(&res->mob_head); |
| 1016 | vmw_dmabuf_unreference(&res->backup); |
| 1017 | } |
| 1018 | |
| 1019 | res->backup = vmw_dmabuf_reference(new_backup); |
Maarten Lankhorst | 8bd4ce5 | 2013-06-27 13:48:27 +0200 | [diff] [blame] | 1020 | lockdep_assert_held(&new_backup->base.resv->lock.base); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1021 | list_add_tail(&res->mob_head, &new_backup->res_list); |
| 1022 | } |
| 1023 | if (new_backup) |
| 1024 | res->backup_offset = new_backup_offset; |
| 1025 | |
Thomas Hellstrom | 2668248 | 2013-10-09 01:42:50 -0700 | [diff] [blame] | 1026 | if (!res->func->may_evict || res->id == -1) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1027 | return; |
| 1028 | |
| 1029 | write_lock(&dev_priv->resource_lock); |
| 1030 | list_add_tail(&res->lru_head, |
| 1031 | &res->dev_priv->res_lru[res->func->res_type]); |
| 1032 | write_unlock(&dev_priv->resource_lock); |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * vmw_resource_check_buffer - Check whether a backup buffer is needed |
| 1037 | * for a resource and in that case, allocate |
| 1038 | * one, reserve and validate it. |
| 1039 | * |
| 1040 | * @res: The resource for which to allocate a backup buffer. |
| 1041 | * @interruptible: Whether any sleeps during allocation should be |
| 1042 | * performed while interruptible. |
| 1043 | * @val_buf: On successful return contains data about the |
| 1044 | * reserved and validated backup buffer. |
| 1045 | */ |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 1046 | static int |
| 1047 | vmw_resource_check_buffer(struct vmw_resource *res, |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 1048 | bool interruptible, |
| 1049 | struct ttm_validate_buffer *val_buf) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1050 | { |
| 1051 | struct list_head val_list; |
| 1052 | bool backup_dirty = false; |
| 1053 | int ret; |
| 1054 | |
| 1055 | if (unlikely(res->backup == NULL)) { |
| 1056 | ret = vmw_resource_buf_alloc(res, interruptible); |
| 1057 | if (unlikely(ret != 0)) |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | INIT_LIST_HEAD(&val_list); |
| 1062 | val_buf->bo = ttm_bo_reference(&res->backup->base); |
| 1063 | list_add_tail(&val_buf->head, &val_list); |
Thomas Hellstrom | ac49251 | 2013-11-15 00:06:47 -0800 | [diff] [blame] | 1064 | ret = ttm_eu_reserve_buffers(NULL, &val_list); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1065 | if (unlikely(ret != 0)) |
| 1066 | goto out_no_reserve; |
| 1067 | |
| 1068 | if (res->func->needs_backup && list_empty(&res->mob_head)) |
| 1069 | return 0; |
| 1070 | |
| 1071 | backup_dirty = res->backup_dirty; |
| 1072 | ret = ttm_bo_validate(&res->backup->base, |
| 1073 | res->func->backup_placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1074 | true, false); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (unlikely(ret != 0)) |
| 1077 | goto out_no_validate; |
| 1078 | |
| 1079 | return 0; |
| 1080 | |
| 1081 | out_no_validate: |
Thomas Hellstrom | ac49251 | 2013-11-15 00:06:47 -0800 | [diff] [blame] | 1082 | ttm_eu_backoff_reservation(NULL, &val_list); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1083 | out_no_reserve: |
| 1084 | ttm_bo_unref(&val_buf->bo); |
| 1085 | if (backup_dirty) |
| 1086 | vmw_dmabuf_unreference(&res->backup); |
| 1087 | |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * vmw_resource_reserve - Reserve a resource for command submission |
| 1093 | * |
| 1094 | * @res: The resource to reserve. |
| 1095 | * |
| 1096 | * This function takes the resource off the LRU list and make sure |
| 1097 | * a backup buffer is present for guest-backed resources. However, |
| 1098 | * the buffer may not be bound to the resource at this point. |
| 1099 | * |
| 1100 | */ |
| 1101 | int vmw_resource_reserve(struct vmw_resource *res, bool no_backup) |
| 1102 | { |
| 1103 | struct vmw_private *dev_priv = res->dev_priv; |
| 1104 | int ret; |
| 1105 | |
| 1106 | write_lock(&dev_priv->resource_lock); |
| 1107 | list_del_init(&res->lru_head); |
| 1108 | write_unlock(&dev_priv->resource_lock); |
| 1109 | |
| 1110 | if (res->func->needs_backup && res->backup == NULL && |
| 1111 | !no_backup) { |
| 1112 | ret = vmw_resource_buf_alloc(res, true); |
| 1113 | if (unlikely(ret != 0)) |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * vmw_resource_backoff_reservation - Unreserve and unreference a |
| 1122 | * backup buffer |
| 1123 | *. |
| 1124 | * @val_buf: Backup buffer information. |
| 1125 | */ |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 1126 | static void |
Thomas Hellstrom | ac49251 | 2013-11-15 00:06:47 -0800 | [diff] [blame] | 1127 | vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1128 | { |
| 1129 | struct list_head val_list; |
| 1130 | |
| 1131 | if (likely(val_buf->bo == NULL)) |
| 1132 | return; |
| 1133 | |
| 1134 | INIT_LIST_HEAD(&val_list); |
| 1135 | list_add_tail(&val_buf->head, &val_list); |
Thomas Hellstrom | ac49251 | 2013-11-15 00:06:47 -0800 | [diff] [blame] | 1136 | ttm_eu_backoff_reservation(NULL, &val_list); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1137 | ttm_bo_unref(&val_buf->bo); |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * vmw_resource_do_evict - Evict a resource, and transfer its data |
| 1142 | * to a backup buffer. |
| 1143 | * |
| 1144 | * @res: The resource to evict. |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1145 | * @interruptible: Whether to wait interruptible. |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1146 | */ |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1147 | int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible) |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1148 | { |
| 1149 | struct ttm_validate_buffer val_buf; |
| 1150 | const struct vmw_res_func *func = res->func; |
| 1151 | int ret; |
| 1152 | |
| 1153 | BUG_ON(!func->may_evict); |
| 1154 | |
| 1155 | val_buf.bo = NULL; |
Thomas Hellstrom | ac49251 | 2013-11-15 00:06:47 -0800 | [diff] [blame] | 1156 | ret = vmw_resource_check_buffer(res, interruptible, &val_buf); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1157 | if (unlikely(ret != 0)) |
| 1158 | return ret; |
| 1159 | |
| 1160 | if (unlikely(func->unbind != NULL && |
| 1161 | (!func->needs_backup || !list_empty(&res->mob_head)))) { |
| 1162 | ret = func->unbind(res, res->res_dirty, &val_buf); |
| 1163 | if (unlikely(ret != 0)) |
| 1164 | goto out_no_unbind; |
| 1165 | list_del_init(&res->mob_head); |
| 1166 | } |
| 1167 | ret = func->destroy(res); |
| 1168 | res->backup_dirty = true; |
| 1169 | res->res_dirty = false; |
| 1170 | out_no_unbind: |
Thomas Hellstrom | ac49251 | 2013-11-15 00:06:47 -0800 | [diff] [blame] | 1171 | vmw_resource_backoff_reservation(&val_buf); |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1172 | |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | /** |
| 1178 | * vmw_resource_validate - Make a resource up-to-date and visible |
| 1179 | * to the device. |
| 1180 | * |
| 1181 | * @res: The resource to make visible to the device. |
| 1182 | * |
| 1183 | * On succesful return, any backup DMA buffer pointed to by @res->backup will |
| 1184 | * be reserved and validated. |
| 1185 | * On hardware resource shortage, this function will repeatedly evict |
| 1186 | * resources of the same type until the validation succeeds. |
| 1187 | */ |
| 1188 | int vmw_resource_validate(struct vmw_resource *res) |
| 1189 | { |
| 1190 | int ret; |
| 1191 | struct vmw_resource *evict_res; |
| 1192 | struct vmw_private *dev_priv = res->dev_priv; |
| 1193 | struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type]; |
| 1194 | struct ttm_validate_buffer val_buf; |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1195 | unsigned err_count = 0; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (likely(!res->func->may_evict)) |
| 1198 | return 0; |
| 1199 | |
| 1200 | val_buf.bo = NULL; |
| 1201 | if (res->backup) |
| 1202 | val_buf.bo = &res->backup->base; |
| 1203 | do { |
| 1204 | ret = vmw_resource_do_validate(res, &val_buf); |
| 1205 | if (likely(ret != -EBUSY)) |
| 1206 | break; |
| 1207 | |
| 1208 | write_lock(&dev_priv->resource_lock); |
| 1209 | if (list_empty(lru_list) || !res->func->may_evict) { |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1210 | DRM_ERROR("Out of device device resources " |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1211 | "for %s.\n", res->func->type_name); |
| 1212 | ret = -EBUSY; |
| 1213 | write_unlock(&dev_priv->resource_lock); |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | evict_res = vmw_resource_reference |
| 1218 | (list_first_entry(lru_list, struct vmw_resource, |
| 1219 | lru_head)); |
| 1220 | list_del_init(&evict_res->lru_head); |
| 1221 | |
| 1222 | write_unlock(&dev_priv->resource_lock); |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1223 | |
| 1224 | ret = vmw_resource_do_evict(evict_res, true); |
| 1225 | if (unlikely(ret != 0)) { |
| 1226 | write_lock(&dev_priv->resource_lock); |
| 1227 | list_add_tail(&evict_res->lru_head, lru_list); |
| 1228 | write_unlock(&dev_priv->resource_lock); |
| 1229 | if (ret == -ERESTARTSYS || |
| 1230 | ++err_count > VMW_RES_EVICT_ERR_COUNT) { |
| 1231 | vmw_resource_unreference(&evict_res); |
| 1232 | goto out_no_validate; |
| 1233 | } |
| 1234 | } |
| 1235 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1236 | vmw_resource_unreference(&evict_res); |
| 1237 | } while (1); |
| 1238 | |
| 1239 | if (unlikely(ret != 0)) |
| 1240 | goto out_no_validate; |
| 1241 | else if (!res->func->needs_backup && res->backup) { |
| 1242 | list_del_init(&res->mob_head); |
| 1243 | vmw_dmabuf_unreference(&res->backup); |
| 1244 | } |
| 1245 | |
| 1246 | return 0; |
| 1247 | |
| 1248 | out_no_validate: |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * vmw_fence_single_bo - Utility function to fence a single TTM buffer |
| 1254 | * object without unreserving it. |
| 1255 | * |
| 1256 | * @bo: Pointer to the struct ttm_buffer_object to fence. |
| 1257 | * @fence: Pointer to the fence. If NULL, this function will |
| 1258 | * insert a fence into the command stream.. |
| 1259 | * |
| 1260 | * Contrary to the ttm_eu version of this function, it takes only |
| 1261 | * a single buffer object instead of a list, and it also doesn't |
| 1262 | * unreserve the buffer object, which needs to be done separately. |
| 1263 | */ |
| 1264 | void vmw_fence_single_bo(struct ttm_buffer_object *bo, |
| 1265 | struct vmw_fence_obj *fence) |
| 1266 | { |
| 1267 | struct ttm_bo_device *bdev = bo->bdev; |
| 1268 | struct ttm_bo_driver *driver = bdev->driver; |
| 1269 | struct vmw_fence_obj *old_fence_obj; |
| 1270 | struct vmw_private *dev_priv = |
| 1271 | container_of(bdev, struct vmw_private, bdev); |
| 1272 | |
| 1273 | if (fence == NULL) |
| 1274 | vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL); |
| 1275 | else |
| 1276 | driver->sync_obj_ref(fence); |
| 1277 | |
| 1278 | spin_lock(&bdev->fence_lock); |
| 1279 | |
| 1280 | old_fence_obj = bo->sync_obj; |
| 1281 | bo->sync_obj = fence; |
| 1282 | |
| 1283 | spin_unlock(&bdev->fence_lock); |
| 1284 | |
| 1285 | if (old_fence_obj) |
| 1286 | vmw_fence_obj_unreference(&old_fence_obj); |
| 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * vmw_resource_move_notify - TTM move_notify_callback |
| 1291 | * |
| 1292 | * @bo: The TTM buffer object about to move. |
| 1293 | * @mem: The truct ttm_mem_reg indicating to what memory |
| 1294 | * region the move is taking place. |
| 1295 | * |
| 1296 | * For now does nothing. |
| 1297 | */ |
| 1298 | void vmw_resource_move_notify(struct ttm_buffer_object *bo, |
| 1299 | struct ttm_mem_reg *mem) |
| 1300 | { |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * vmw_resource_needs_backup - Return whether a resource needs a backup buffer. |
| 1305 | * |
| 1306 | * @res: The resource being queried. |
| 1307 | */ |
| 1308 | bool vmw_resource_needs_backup(const struct vmw_resource *res) |
| 1309 | { |
| 1310 | return res->func->needs_backup; |
| 1311 | } |
| 1312 | |
| 1313 | /** |
| 1314 | * vmw_resource_evict_type - Evict all resources of a specific type |
| 1315 | * |
| 1316 | * @dev_priv: Pointer to a device private struct |
| 1317 | * @type: The resource type to evict |
| 1318 | * |
| 1319 | * To avoid thrashing starvation or as part of the hibernation sequence, |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1320 | * try to evict all evictable resources of a specific type. |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1321 | */ |
| 1322 | static void vmw_resource_evict_type(struct vmw_private *dev_priv, |
| 1323 | enum vmw_res_type type) |
| 1324 | { |
| 1325 | struct list_head *lru_list = &dev_priv->res_lru[type]; |
| 1326 | struct vmw_resource *evict_res; |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1327 | unsigned err_count = 0; |
| 1328 | int ret; |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1329 | |
| 1330 | do { |
| 1331 | write_lock(&dev_priv->resource_lock); |
| 1332 | |
| 1333 | if (list_empty(lru_list)) |
| 1334 | goto out_unlock; |
| 1335 | |
| 1336 | evict_res = vmw_resource_reference( |
| 1337 | list_first_entry(lru_list, struct vmw_resource, |
| 1338 | lru_head)); |
| 1339 | list_del_init(&evict_res->lru_head); |
| 1340 | write_unlock(&dev_priv->resource_lock); |
Thomas Hellstrom | ea029c2 | 2013-11-12 00:09:54 -0800 | [diff] [blame] | 1341 | |
| 1342 | ret = vmw_resource_do_evict(evict_res, false); |
| 1343 | if (unlikely(ret != 0)) { |
| 1344 | write_lock(&dev_priv->resource_lock); |
| 1345 | list_add_tail(&evict_res->lru_head, lru_list); |
| 1346 | write_unlock(&dev_priv->resource_lock); |
| 1347 | if (++err_count > VMW_RES_EVICT_ERR_COUNT) { |
| 1348 | vmw_resource_unreference(&evict_res); |
| 1349 | return; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Thomas Hellstrom | c0951b7 | 2012-11-20 12:19:35 +0000 | [diff] [blame] | 1353 | vmw_resource_unreference(&evict_res); |
| 1354 | } while (1); |
| 1355 | |
| 1356 | out_unlock: |
| 1357 | write_unlock(&dev_priv->resource_lock); |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * vmw_resource_evict_all - Evict all evictable resources |
| 1362 | * |
| 1363 | * @dev_priv: Pointer to a device private struct |
| 1364 | * |
| 1365 | * To avoid thrashing starvation or as part of the hibernation sequence, |
| 1366 | * evict all evictable resources. In particular this means that all |
| 1367 | * guest-backed resources that are registered with the device are |
| 1368 | * evicted and the OTable becomes clean. |
| 1369 | */ |
| 1370 | void vmw_resource_evict_all(struct vmw_private *dev_priv) |
| 1371 | { |
| 1372 | enum vmw_res_type type; |
| 1373 | |
| 1374 | mutex_lock(&dev_priv->cmdbuf_mutex); |
| 1375 | |
| 1376 | for (type = 0; type < vmw_res_max; ++type) |
| 1377 | vmw_resource_evict_type(dev_priv, type); |
| 1378 | |
| 1379 | mutex_unlock(&dev_priv->cmdbuf_mutex); |
| 1380 | } |