blob: b40978f0ca967e13090087cb4b08ccb87d71f5ab [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
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 Howells760285e2012-10-02 18:01:07 +010029#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 Hellstrom543831c2012-11-20 12:19:36 +000033#include "vmwgfx_resource_priv.h"
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000034
Thomas Hellstromea029c22013-11-12 00:09:54 -080035#define VMW_RES_EVICT_ERR_COUNT 10
36
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000037struct vmw_user_dma_buffer {
Thomas Hellstromc486d4f2013-11-08 02:30:50 -080038 struct ttm_prime_object prime;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000039 struct vmw_dma_buffer dma;
40};
41
42struct vmw_bo_user_rep {
43 uint32_t handle;
44 uint64_t map_handle;
45};
46
47struct vmw_stream {
48 struct vmw_resource res;
49 uint32_t stream_id;
50};
51
52struct vmw_user_stream {
53 struct ttm_base_object base;
54 struct vmw_stream stream;
55};
56
Thomas Hellstromc0951b72012-11-20 12:19:35 +000057
58static uint64_t vmw_user_stream_size;
59
60static 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 Bornecrantzfb1d9732009-12-10 00:19:58 +000072static inline struct vmw_dma_buffer *
73vmw_dma_buffer(struct ttm_buffer_object *bo)
74{
75 return container_of(bo, struct vmw_dma_buffer, base);
76}
77
78static inline struct vmw_user_dma_buffer *
79vmw_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
85struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
86{
87 kref_get(&res->kref);
88 return res;
89}
90
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +020091
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 Hellstrom543831c2012-11-20 12:19:36 +000099void vmw_resource_release_id(struct vmw_resource *res)
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200100{
101 struct vmw_private *dev_priv = res->dev_priv;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000102 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200103
104 write_lock(&dev_priv->resource_lock);
105 if (res->id != -1)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000106 idr_remove(idr, res->id);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200107 res->id = -1;
108 write_unlock(&dev_priv->resource_lock);
109}
110
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000111static 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 Hellstromc0951b72012-11-20 12:19:35 +0000116 int id;
117 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000118
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200119 res->avail = false;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000120 list_del_init(&res->lru_head);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000121 write_unlock(&dev_priv->resource_lock);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000122 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000138
139 if (likely(res->hw_destroy != NULL))
140 res->hw_destroy(res);
141
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000142 id = res->id;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000143 if (res->res_free != NULL)
144 res->res_free(res);
145 else
146 kfree(res);
147
148 write_lock(&dev_priv->resource_lock);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200149
150 if (id != -1)
151 idr_remove(idr, id);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000152}
153
154void 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 Hellstrom5bb39e82011-10-04 20:13:33 +0200165
166/**
167 * vmw_resource_alloc_id - release a resource id to the id manager.
168 *
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200169 * @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 Hellstrom543831c2012-11-20 12:19:36 +0000174int vmw_resource_alloc_id(struct vmw_resource *res)
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200175{
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000176 struct vmw_private *dev_priv = res->dev_priv;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200177 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000178 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200179
180 BUG_ON(res->id != -1);
181
Tejun Heocc39a8f2013-02-27 17:04:14 -0800182 idr_preload(GFP_KERNEL);
183 write_lock(&dev_priv->resource_lock);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200184
Tejun Heocc39a8f2013-02-27 17:04:14 -0800185 ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
186 if (ret >= 0)
187 res->id = ret;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200188
Tejun Heocc39a8f2013-02-27 17:04:14 -0800189 write_unlock(&dev_priv->resource_lock);
190 idr_preload_end();
191 return ret < 0 ? ret : 0;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200192}
193
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000194/**
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 Hellstrom543831c2012-11-20 12:19:36 +0000205int 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000209{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000210 kref_init(&res->kref);
211 res->hw_destroy = NULL;
212 res->res_free = res_free;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000213 res->avail = false;
214 res->dev_priv = dev_priv;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000215 res->func = func;
216 INIT_LIST_HEAD(&res->lru_head);
217 INIT_LIST_HEAD(&res->mob_head);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200218 res->id = -1;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000219 res->backup = NULL;
220 res->backup_offset = 0;
221 res->backup_dirty = false;
222 res->res_dirty = false;
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200223 if (delay_id)
224 return 0;
225 else
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000226 return vmw_resource_alloc_id(res);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000227}
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 Hellstrom543831c2012-11-20 12:19:36 +0000241void vmw_resource_activate(struct vmw_resource *res,
242 void (*hw_destroy) (struct vmw_resource *))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000243{
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
252struct 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 Hellstromc0951b72012-11-20 12:19:35 +0000272 * 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 */
285int 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000291{
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100292 struct ttm_base_object *base;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000293 struct vmw_resource *res;
294 int ret = -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000295
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100296 base = ttm_base_object_lookup(tfile, handle);
297 if (unlikely(base == NULL))
298 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000299
Thomas Hellstrom79e5f812013-11-08 02:12:51 -0800300 if (unlikely(ttm_base_object_type(base) != converter->object_type))
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000301 goto out_bad_resource;
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100302
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000303 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 Hellstrom7a73ba72009-12-22 16:53:41 +0100315 ret = 0;
316
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000317out_bad_resource:
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100318 ttm_base_object_unref(&base);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000319
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 */
328int 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000349 return ret;
350}
351
352/**
353 * Buffer management.
354 */
Thomas Hellstrom308d17e2013-11-28 01:46:56 -0800355
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 */
363static 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 Hellstromeffe1102010-01-13 22:28:39 +0100387void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
388{
389 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
Thomas Hellstromeffe1102010-01-13 22:28:39 +0100390
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000391 kfree(vmw_bo);
392}
393
Thomas Hellstrom308d17e2013-11-28 01:46:56 -0800394static 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000401int 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000408 size_t acc_size;
409 int ret;
Thomas Hellstrom308d17e2013-11-28 01:46:56 -0800410 bool user = (bo_free == &vmw_user_dmabuf_destroy);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000411
Thomas Hellstrom308d17e2013-11-28 01:46:56 -0800412 BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000413
Thomas Hellstrom308d17e2013-11-28 01:46:56 -0800414 acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000415 memset(vmw_bo, 0, sizeof(*vmw_bo));
416
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000417 INIT_LIST_HEAD(&vmw_bo->res_list);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000418
419 ret = ttm_bo_init(bdev, &vmw_bo->base, size,
Thomas Hellstrom308d17e2013-11-28 01:46:56 -0800420 (user) ? ttm_bo_type_device :
421 ttm_bo_type_kernel, placement,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +0000422 0, interruptible,
Dave Airlie129b78b2012-04-02 11:46:06 +0100423 NULL, acc_size, NULL, bo_free);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000424 return ret;
425}
426
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000427static 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 Hellstromc486d4f2013-11-08 02:30:50 -0800438 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
439 prime.base);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000440 bo = &vmw_user_bo->dma.base;
441 ttm_bo_unref(&bo);
442}
443
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000444/**
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 */
456int 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 Hellstrom96c5f0d2012-11-21 11:19:53 +0100474 (dev_priv->has_mob) ?
475 &vmw_sys_placement :
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000476 &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 Hellstromc486d4f2013-11-08 02:30:50 -0800482 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 Hellstromc0951b72012-11-20 12:19:35 +0000488 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 Hellstromc486d4f2013-11-08 02:30:50 -0800494 *handle = user_bo->prime.base.hash.key;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000495
496out_no_base_object:
497 return ret;
498}
499
Thomas Hellstromd08a9b92012-11-21 16:04:18 +0100500/**
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 */
507int 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 Hellstromc486d4f2013-11-08 02:30:50 -0800516 return (vmw_user_bo->prime.base.tfile == tfile ||
517 vmw_user_bo->prime.base.shareable) ? 0 : -EPERM;
Thomas Hellstromd08a9b92012-11-21 16:04:18 +0100518}
519
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000520int 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 Hellstromc0951b72012-11-20 12:19:35 +0000528 struct vmw_dma_buffer *dma_buf;
529 uint32_t handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000530 struct vmw_master *vmaster = vmw_master(file_priv->master);
531 int ret;
532
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000533 ret = ttm_read_lock(&vmaster->lock, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000534 if (unlikely(ret != 0))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000535 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000536
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000537 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
538 req->size, false, &handle, &dma_buf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000539 if (unlikely(ret != 0))
Thomas Hellstrom2f5993c2010-11-17 13:24:48 +0100540 goto out_no_dmabuf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000541
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000542 rep->handle = handle;
David Herrmann72525b32013-07-24 21:08:53 +0200543 rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000544 rep->cur_gmr_id = handle;
545 rep->cur_gmr_offset = 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000546
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000547 vmw_dmabuf_unreference(&dma_buf);
548
Thomas Hellstrom2f5993c2010-11-17 13:24:48 +0100549out_no_dmabuf:
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000550 ttm_read_unlock(&vmaster->lock);
551
Thomas Hellstrom2f5993c2010-11-17 13:24:48 +0100552 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000553}
554
555int 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 Bornecrantzfb1d9732009-12-10 00:19:58 +0000566int 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 Hellstromc486d4f2013-11-08 02:30:50 -0800579 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000580 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 Hellstromc486d4f2013-11-08 02:30:50 -0800586 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
587 prime.base);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000588 (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 Hellstromc0951b72012-11-20 12:19:35 +0000595int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100596 struct vmw_dma_buffer *dma_buf,
597 uint32_t *handle)
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000598{
599 struct vmw_user_dma_buffer *user_bo;
600
601 if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
602 return -EINVAL;
603
604 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
Thomas Hellstroma97e2192012-11-21 11:45:13 +0100605
606 *handle = user_bo->prime.base.hash.key;
Thomas Hellstromc486d4f2013-11-08 02:30:50 -0800607 return ttm_ref_object_add(tfile, &user_bo->prime.base,
608 TTM_REF_USAGE, NULL);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000609}
610
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000611/*
Uwe Kleine-König65155b32010-06-11 12:17:01 +0200612 * Stream management
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000613 */
614
615static void vmw_stream_destroy(struct vmw_resource *res)
616{
617 struct vmw_private *dev_priv = res->dev_priv;
618 struct vmw_stream *stream;
619 int ret;
620
621 DRM_INFO("%s: unref\n", __func__);
622 stream = container_of(res, struct vmw_stream, res);
623
624 ret = vmw_overlay_unref(dev_priv, stream->stream_id);
625 WARN_ON(ret != 0);
626}
627
628static int vmw_stream_init(struct vmw_private *dev_priv,
629 struct vmw_stream *stream,
630 void (*res_free) (struct vmw_resource *res))
631{
632 struct vmw_resource *res = &stream->res;
633 int ret;
634
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000635 ret = vmw_resource_init(dev_priv, res, false, res_free,
636 &vmw_stream_func);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000637
638 if (unlikely(ret != 0)) {
639 if (res_free == NULL)
640 kfree(stream);
641 else
642 res_free(&stream->res);
643 return ret;
644 }
645
646 ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
647 if (ret) {
648 vmw_resource_unreference(&res);
649 return ret;
650 }
651
652 DRM_INFO("%s: claimed\n", __func__);
653
654 vmw_resource_activate(&stream->res, vmw_stream_destroy);
655 return 0;
656}
657
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000658static void vmw_user_stream_free(struct vmw_resource *res)
659{
660 struct vmw_user_stream *stream =
661 container_of(res, struct vmw_user_stream, stream.res);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200662 struct vmw_private *dev_priv = res->dev_priv;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000663
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000664 ttm_base_object_kfree(stream, base);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200665 ttm_mem_global_free(vmw_mem_glob(dev_priv),
666 vmw_user_stream_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000667}
668
669/**
670 * This function is called when user space has no more references on the
671 * base object. It releases the base-object's reference on the resource object.
672 */
673
674static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
675{
676 struct ttm_base_object *base = *p_base;
677 struct vmw_user_stream *stream =
678 container_of(base, struct vmw_user_stream, base);
679 struct vmw_resource *res = &stream->stream.res;
680
681 *p_base = NULL;
682 vmw_resource_unreference(&res);
683}
684
685int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
686 struct drm_file *file_priv)
687{
688 struct vmw_private *dev_priv = vmw_priv(dev);
689 struct vmw_resource *res;
690 struct vmw_user_stream *stream;
691 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
692 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000693 struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000694 int ret = 0;
695
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000696
697 res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000698 if (unlikely(res == NULL))
699 return -EINVAL;
700
701 if (res->res_free != &vmw_user_stream_free) {
702 ret = -EINVAL;
703 goto out;
704 }
705
706 stream = container_of(res, struct vmw_user_stream, stream.res);
707 if (stream->base.tfile != tfile) {
708 ret = -EINVAL;
709 goto out;
710 }
711
712 ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
713out:
714 vmw_resource_unreference(&res);
715 return ret;
716}
717
718int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
719 struct drm_file *file_priv)
720{
721 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200722 struct vmw_user_stream *stream;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000723 struct vmw_resource *res;
724 struct vmw_resource *tmp;
725 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
726 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200727 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000728 int ret;
729
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200730 /*
731 * Approximate idr memory usage with 128 bytes. It will be limited
732 * by maximum number_of streams anyway?
733 */
734
735 if (unlikely(vmw_user_stream_size == 0))
736 vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
737
738 ret = ttm_read_lock(&vmaster->lock, true);
739 if (unlikely(ret != 0))
740 return ret;
741
742 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
743 vmw_user_stream_size,
744 false, true);
745 if (unlikely(ret != 0)) {
746 if (ret != -ERESTARTSYS)
747 DRM_ERROR("Out of graphics memory for stream"
748 " creation.\n");
749 goto out_unlock;
750 }
751
752
753 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
754 if (unlikely(stream == NULL)) {
755 ttm_mem_global_free(vmw_mem_glob(dev_priv),
756 vmw_user_stream_size);
757 ret = -ENOMEM;
758 goto out_unlock;
759 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000760
761 res = &stream->stream.res;
762 stream->base.shareable = false;
763 stream->base.tfile = NULL;
764
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200765 /*
766 * From here on, the destructor takes over resource freeing.
767 */
768
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000769 ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
770 if (unlikely(ret != 0))
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200771 goto out_unlock;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000772
773 tmp = vmw_resource_reference(res);
774 ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
775 &vmw_user_stream_base_release, NULL);
776
777 if (unlikely(ret != 0)) {
778 vmw_resource_unreference(&tmp);
779 goto out_err;
780 }
781
782 arg->stream_id = res->id;
783out_err:
784 vmw_resource_unreference(&res);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200785out_unlock:
786 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000787 return ret;
788}
789
790int vmw_user_stream_lookup(struct vmw_private *dev_priv,
791 struct ttm_object_file *tfile,
792 uint32_t *inout_id, struct vmw_resource **out)
793{
794 struct vmw_user_stream *stream;
795 struct vmw_resource *res;
796 int ret;
797
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000798 res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
799 *inout_id);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000800 if (unlikely(res == NULL))
801 return -EINVAL;
802
803 if (res->res_free != &vmw_user_stream_free) {
804 ret = -EINVAL;
805 goto err_ref;
806 }
807
808 stream = container_of(res, struct vmw_user_stream, stream.res);
809 if (stream->base.tfile != tfile) {
810 ret = -EPERM;
811 goto err_ref;
812 }
813
814 *inout_id = stream->stream.stream_id;
815 *out = res;
816 return 0;
817err_ref:
818 vmw_resource_unreference(&res);
819 return ret;
820}
Dave Airlie5e1782d2012-08-28 01:53:54 +0000821
822
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800823/**
824 * vmw_dumb_create - Create a dumb kms buffer
825 *
826 * @file_priv: Pointer to a struct drm_file identifying the caller.
827 * @dev: Pointer to the drm device.
828 * @args: Pointer to a struct drm_mode_create_dumb structure
829 *
830 * This is a driver callback for the core drm create_dumb functionality.
831 * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
832 * that the arguments have a different format.
833 */
Dave Airlie5e1782d2012-08-28 01:53:54 +0000834int vmw_dumb_create(struct drm_file *file_priv,
835 struct drm_device *dev,
836 struct drm_mode_create_dumb *args)
837{
838 struct vmw_private *dev_priv = vmw_priv(dev);
839 struct vmw_master *vmaster = vmw_master(file_priv->master);
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800840 struct vmw_dma_buffer *dma_buf;
Dave Airlie5e1782d2012-08-28 01:53:54 +0000841 int ret;
842
843 args->pitch = args->width * ((args->bpp + 7) / 8);
844 args->size = args->pitch * args->height;
845
Dave Airlie5e1782d2012-08-28 01:53:54 +0000846 ret = ttm_read_lock(&vmaster->lock, true);
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800847 if (unlikely(ret != 0))
Dave Airlie5e1782d2012-08-28 01:53:54 +0000848 return ret;
Dave Airlie5e1782d2012-08-28 01:53:54 +0000849
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800850 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
851 args->size, false, &args->handle,
852 &dma_buf);
853 if (unlikely(ret != 0))
Dave Airlie5e1782d2012-08-28 01:53:54 +0000854 goto out_no_dmabuf;
855
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800856 vmw_dmabuf_unreference(&dma_buf);
Dave Airlie5e1782d2012-08-28 01:53:54 +0000857out_no_dmabuf:
858 ttm_read_unlock(&vmaster->lock);
859 return ret;
860}
861
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800862/**
863 * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
864 *
865 * @file_priv: Pointer to a struct drm_file identifying the caller.
866 * @dev: Pointer to the drm device.
867 * @handle: Handle identifying the dumb buffer.
868 * @offset: The address space offset returned.
869 *
870 * This is a driver callback for the core drm dumb_map_offset functionality.
871 */
Dave Airlie5e1782d2012-08-28 01:53:54 +0000872int vmw_dumb_map_offset(struct drm_file *file_priv,
873 struct drm_device *dev, uint32_t handle,
874 uint64_t *offset)
875{
876 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
877 struct vmw_dma_buffer *out_buf;
878 int ret;
879
880 ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
881 if (ret != 0)
882 return -EINVAL;
883
David Herrmann72525b32013-07-24 21:08:53 +0200884 *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
Dave Airlie5e1782d2012-08-28 01:53:54 +0000885 vmw_dmabuf_unreference(&out_buf);
886 return 0;
887}
888
Thomas Hellstromd69d51d2013-11-28 00:28:30 -0800889/**
890 * vmw_dumb_destroy - Destroy a dumb boffer
891 *
892 * @file_priv: Pointer to a struct drm_file identifying the caller.
893 * @dev: Pointer to the drm device.
894 * @handle: Handle identifying the dumb buffer.
895 *
896 * This is a driver callback for the core drm dumb_destroy functionality.
897 */
Dave Airlie5e1782d2012-08-28 01:53:54 +0000898int vmw_dumb_destroy(struct drm_file *file_priv,
899 struct drm_device *dev,
900 uint32_t handle)
901{
902 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
903 handle, TTM_REF_USAGE);
904}
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000905
906/**
907 * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
908 *
909 * @res: The resource for which to allocate a backup buffer.
910 * @interruptible: Whether any sleeps during allocation should be
911 * performed while interruptible.
912 */
913static int vmw_resource_buf_alloc(struct vmw_resource *res,
914 bool interruptible)
915{
916 unsigned long size =
917 (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
918 struct vmw_dma_buffer *backup;
919 int ret;
920
921 if (likely(res->backup)) {
922 BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
923 return 0;
924 }
925
926 backup = kzalloc(sizeof(*backup), GFP_KERNEL);
927 if (unlikely(backup == NULL))
928 return -ENOMEM;
929
930 ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
931 res->func->backup_placement,
932 interruptible,
933 &vmw_dmabuf_bo_free);
934 if (unlikely(ret != 0))
935 goto out_no_dmabuf;
936
937 res->backup = backup;
938
939out_no_dmabuf:
940 return ret;
941}
942
943/**
944 * vmw_resource_do_validate - Make a resource up-to-date and visible
945 * to the device.
946 *
947 * @res: The resource to make visible to the device.
948 * @val_buf: Information about a buffer possibly
949 * containing backup data if a bind operation is needed.
950 *
951 * On hardware resource shortage, this function returns -EBUSY and
952 * should be retried once resources have been freed up.
953 */
954static int vmw_resource_do_validate(struct vmw_resource *res,
955 struct ttm_validate_buffer *val_buf)
956{
957 int ret = 0;
958 const struct vmw_res_func *func = res->func;
959
960 if (unlikely(res->id == -1)) {
961 ret = func->create(res);
962 if (unlikely(ret != 0))
963 return ret;
964 }
965
966 if (func->bind &&
967 ((func->needs_backup && list_empty(&res->mob_head) &&
968 val_buf->bo != NULL) ||
969 (!func->needs_backup && val_buf->bo != NULL))) {
970 ret = func->bind(res, val_buf);
971 if (unlikely(ret != 0))
972 goto out_bind_failed;
973 if (func->needs_backup)
974 list_add_tail(&res->mob_head, &res->backup->res_list);
975 }
976
977 /*
978 * Only do this on write operations, and move to
979 * vmw_resource_unreserve if it can be called after
980 * backup buffers have been unreserved. Otherwise
981 * sort out locking.
982 */
983 res->res_dirty = true;
984
985 return 0;
986
987out_bind_failed:
988 func->destroy(res);
989
990 return ret;
991}
992
993/**
994 * vmw_resource_unreserve - Unreserve a resource previously reserved for
995 * command submission.
996 *
997 * @res: Pointer to the struct vmw_resource to unreserve.
998 * @new_backup: Pointer to new backup buffer if command submission
999 * switched.
1000 * @new_backup_offset: New backup offset if @new_backup is !NULL.
1001 *
1002 * Currently unreserving a resource means putting it back on the device's
1003 * resource lru list, so that it can be evicted if necessary.
1004 */
1005void vmw_resource_unreserve(struct vmw_resource *res,
1006 struct vmw_dma_buffer *new_backup,
1007 unsigned long new_backup_offset)
1008{
1009 struct vmw_private *dev_priv = res->dev_priv;
1010
1011 if (!list_empty(&res->lru_head))
1012 return;
1013
1014 if (new_backup && new_backup != res->backup) {
1015
1016 if (res->backup) {
Maarten Lankhorst8bd4ce52013-06-27 13:48:27 +02001017 lockdep_assert_held(&res->backup->base.resv->lock.base);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001018 list_del_init(&res->mob_head);
1019 vmw_dmabuf_unreference(&res->backup);
1020 }
1021
1022 res->backup = vmw_dmabuf_reference(new_backup);
Maarten Lankhorst8bd4ce52013-06-27 13:48:27 +02001023 lockdep_assert_held(&new_backup->base.resv->lock.base);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001024 list_add_tail(&res->mob_head, &new_backup->res_list);
1025 }
1026 if (new_backup)
1027 res->backup_offset = new_backup_offset;
1028
Thomas Hellstrom26682482013-10-09 01:42:50 -07001029 if (!res->func->may_evict || res->id == -1)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001030 return;
1031
1032 write_lock(&dev_priv->resource_lock);
1033 list_add_tail(&res->lru_head,
1034 &res->dev_priv->res_lru[res->func->res_type]);
1035 write_unlock(&dev_priv->resource_lock);
1036}
1037
1038/**
1039 * vmw_resource_check_buffer - Check whether a backup buffer is needed
1040 * for a resource and in that case, allocate
1041 * one, reserve and validate it.
1042 *
1043 * @res: The resource for which to allocate a backup buffer.
1044 * @interruptible: Whether any sleeps during allocation should be
1045 * performed while interruptible.
1046 * @val_buf: On successful return contains data about the
1047 * reserved and validated backup buffer.
1048 */
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001049static int
1050vmw_resource_check_buffer(struct vmw_resource *res,
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001051 bool interruptible,
1052 struct ttm_validate_buffer *val_buf)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001053{
1054 struct list_head val_list;
1055 bool backup_dirty = false;
1056 int ret;
1057
1058 if (unlikely(res->backup == NULL)) {
1059 ret = vmw_resource_buf_alloc(res, interruptible);
1060 if (unlikely(ret != 0))
1061 return ret;
1062 }
1063
1064 INIT_LIST_HEAD(&val_list);
1065 val_buf->bo = ttm_bo_reference(&res->backup->base);
1066 list_add_tail(&val_buf->head, &val_list);
Thomas Hellstromac492512013-11-15 00:06:47 -08001067 ret = ttm_eu_reserve_buffers(NULL, &val_list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001068 if (unlikely(ret != 0))
1069 goto out_no_reserve;
1070
1071 if (res->func->needs_backup && list_empty(&res->mob_head))
1072 return 0;
1073
1074 backup_dirty = res->backup_dirty;
1075 ret = ttm_bo_validate(&res->backup->base,
1076 res->func->backup_placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001077 true, false);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001078
1079 if (unlikely(ret != 0))
1080 goto out_no_validate;
1081
1082 return 0;
1083
1084out_no_validate:
Thomas Hellstromac492512013-11-15 00:06:47 -08001085 ttm_eu_backoff_reservation(NULL, &val_list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001086out_no_reserve:
1087 ttm_bo_unref(&val_buf->bo);
1088 if (backup_dirty)
1089 vmw_dmabuf_unreference(&res->backup);
1090
1091 return ret;
1092}
1093
1094/**
1095 * vmw_resource_reserve - Reserve a resource for command submission
1096 *
1097 * @res: The resource to reserve.
1098 *
1099 * This function takes the resource off the LRU list and make sure
1100 * a backup buffer is present for guest-backed resources. However,
1101 * the buffer may not be bound to the resource at this point.
1102 *
1103 */
1104int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
1105{
1106 struct vmw_private *dev_priv = res->dev_priv;
1107 int ret;
1108
1109 write_lock(&dev_priv->resource_lock);
1110 list_del_init(&res->lru_head);
1111 write_unlock(&dev_priv->resource_lock);
1112
1113 if (res->func->needs_backup && res->backup == NULL &&
1114 !no_backup) {
1115 ret = vmw_resource_buf_alloc(res, true);
1116 if (unlikely(ret != 0))
1117 return ret;
1118 }
1119
1120 return 0;
1121}
1122
1123/**
1124 * vmw_resource_backoff_reservation - Unreserve and unreference a
1125 * backup buffer
1126 *.
1127 * @val_buf: Backup buffer information.
1128 */
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001129static void
Thomas Hellstromac492512013-11-15 00:06:47 -08001130vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001131{
1132 struct list_head val_list;
1133
1134 if (likely(val_buf->bo == NULL))
1135 return;
1136
1137 INIT_LIST_HEAD(&val_list);
1138 list_add_tail(&val_buf->head, &val_list);
Thomas Hellstromac492512013-11-15 00:06:47 -08001139 ttm_eu_backoff_reservation(NULL, &val_list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001140 ttm_bo_unref(&val_buf->bo);
1141}
1142
1143/**
1144 * vmw_resource_do_evict - Evict a resource, and transfer its data
1145 * to a backup buffer.
1146 *
1147 * @res: The resource to evict.
Thomas Hellstromea029c22013-11-12 00:09:54 -08001148 * @interruptible: Whether to wait interruptible.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001149 */
Thomas Hellstromea029c22013-11-12 00:09:54 -08001150int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001151{
1152 struct ttm_validate_buffer val_buf;
1153 const struct vmw_res_func *func = res->func;
1154 int ret;
1155
1156 BUG_ON(!func->may_evict);
1157
1158 val_buf.bo = NULL;
Thomas Hellstromac492512013-11-15 00:06:47 -08001159 ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001160 if (unlikely(ret != 0))
1161 return ret;
1162
1163 if (unlikely(func->unbind != NULL &&
1164 (!func->needs_backup || !list_empty(&res->mob_head)))) {
1165 ret = func->unbind(res, res->res_dirty, &val_buf);
1166 if (unlikely(ret != 0))
1167 goto out_no_unbind;
1168 list_del_init(&res->mob_head);
1169 }
1170 ret = func->destroy(res);
1171 res->backup_dirty = true;
1172 res->res_dirty = false;
1173out_no_unbind:
Thomas Hellstromac492512013-11-15 00:06:47 -08001174 vmw_resource_backoff_reservation(&val_buf);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001175
1176 return ret;
1177}
1178
1179
1180/**
1181 * vmw_resource_validate - Make a resource up-to-date and visible
1182 * to the device.
1183 *
1184 * @res: The resource to make visible to the device.
1185 *
1186 * On succesful return, any backup DMA buffer pointed to by @res->backup will
1187 * be reserved and validated.
1188 * On hardware resource shortage, this function will repeatedly evict
1189 * resources of the same type until the validation succeeds.
1190 */
1191int vmw_resource_validate(struct vmw_resource *res)
1192{
1193 int ret;
1194 struct vmw_resource *evict_res;
1195 struct vmw_private *dev_priv = res->dev_priv;
1196 struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
1197 struct ttm_validate_buffer val_buf;
Thomas Hellstromea029c22013-11-12 00:09:54 -08001198 unsigned err_count = 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001199
1200 if (likely(!res->func->may_evict))
1201 return 0;
1202
1203 val_buf.bo = NULL;
1204 if (res->backup)
1205 val_buf.bo = &res->backup->base;
1206 do {
1207 ret = vmw_resource_do_validate(res, &val_buf);
1208 if (likely(ret != -EBUSY))
1209 break;
1210
1211 write_lock(&dev_priv->resource_lock);
1212 if (list_empty(lru_list) || !res->func->may_evict) {
Thomas Hellstromea029c22013-11-12 00:09:54 -08001213 DRM_ERROR("Out of device device resources "
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001214 "for %s.\n", res->func->type_name);
1215 ret = -EBUSY;
1216 write_unlock(&dev_priv->resource_lock);
1217 break;
1218 }
1219
1220 evict_res = vmw_resource_reference
1221 (list_first_entry(lru_list, struct vmw_resource,
1222 lru_head));
1223 list_del_init(&evict_res->lru_head);
1224
1225 write_unlock(&dev_priv->resource_lock);
Thomas Hellstromea029c22013-11-12 00:09:54 -08001226
1227 ret = vmw_resource_do_evict(evict_res, true);
1228 if (unlikely(ret != 0)) {
1229 write_lock(&dev_priv->resource_lock);
1230 list_add_tail(&evict_res->lru_head, lru_list);
1231 write_unlock(&dev_priv->resource_lock);
1232 if (ret == -ERESTARTSYS ||
1233 ++err_count > VMW_RES_EVICT_ERR_COUNT) {
1234 vmw_resource_unreference(&evict_res);
1235 goto out_no_validate;
1236 }
1237 }
1238
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001239 vmw_resource_unreference(&evict_res);
1240 } while (1);
1241
1242 if (unlikely(ret != 0))
1243 goto out_no_validate;
1244 else if (!res->func->needs_backup && res->backup) {
1245 list_del_init(&res->mob_head);
1246 vmw_dmabuf_unreference(&res->backup);
1247 }
1248
1249 return 0;
1250
1251out_no_validate:
1252 return ret;
1253}
1254
1255/**
1256 * vmw_fence_single_bo - Utility function to fence a single TTM buffer
1257 * object without unreserving it.
1258 *
1259 * @bo: Pointer to the struct ttm_buffer_object to fence.
1260 * @fence: Pointer to the fence. If NULL, this function will
1261 * insert a fence into the command stream..
1262 *
1263 * Contrary to the ttm_eu version of this function, it takes only
1264 * a single buffer object instead of a list, and it also doesn't
1265 * unreserve the buffer object, which needs to be done separately.
1266 */
1267void vmw_fence_single_bo(struct ttm_buffer_object *bo,
1268 struct vmw_fence_obj *fence)
1269{
1270 struct ttm_bo_device *bdev = bo->bdev;
1271 struct ttm_bo_driver *driver = bdev->driver;
1272 struct vmw_fence_obj *old_fence_obj;
1273 struct vmw_private *dev_priv =
1274 container_of(bdev, struct vmw_private, bdev);
1275
1276 if (fence == NULL)
1277 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1278 else
1279 driver->sync_obj_ref(fence);
1280
1281 spin_lock(&bdev->fence_lock);
1282
1283 old_fence_obj = bo->sync_obj;
1284 bo->sync_obj = fence;
1285
1286 spin_unlock(&bdev->fence_lock);
1287
1288 if (old_fence_obj)
1289 vmw_fence_obj_unreference(&old_fence_obj);
1290}
1291
1292/**
1293 * vmw_resource_move_notify - TTM move_notify_callback
1294 *
1295 * @bo: The TTM buffer object about to move.
1296 * @mem: The truct ttm_mem_reg indicating to what memory
1297 * region the move is taking place.
1298 *
Thomas Hellstromf4689112012-11-21 11:29:13 +01001299 * Evicts the Guest Backed hardware resource if the backup
1300 * buffer is being moved out of MOB memory.
1301 * Note that this function should not race with the resource
1302 * validation code as long as it accesses only members of struct
1303 * resource that remain static while bo::res is !NULL and
1304 * while we have @bo reserved. struct resource::backup is *not* a
1305 * static member. The resource validation code will take care
1306 * to set @bo::res to NULL, while having @bo reserved when the
1307 * buffer is no longer bound to the resource, so @bo:res can be
1308 * used to determine whether there is a need to unbind and whether
1309 * it is safe to unbind.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001310 */
1311void vmw_resource_move_notify(struct ttm_buffer_object *bo,
1312 struct ttm_mem_reg *mem)
1313{
Thomas Hellstromf4689112012-11-21 11:29:13 +01001314 struct vmw_dma_buffer *dma_buf;
1315
1316 if (mem == NULL)
1317 return;
1318
1319 if (bo->destroy != vmw_dmabuf_bo_free &&
1320 bo->destroy != vmw_user_dmabuf_destroy)
1321 return;
1322
1323 dma_buf = container_of(bo, struct vmw_dma_buffer, base);
1324
1325 if (mem->mem_type != VMW_PL_MOB) {
1326 struct vmw_resource *res, *n;
1327 struct ttm_bo_device *bdev = bo->bdev;
1328 struct ttm_validate_buffer val_buf;
1329
1330 val_buf.bo = bo;
1331
1332 list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
1333
1334 if (unlikely(res->func->unbind == NULL))
1335 continue;
1336
1337 (void) res->func->unbind(res, true, &val_buf);
1338 res->backup_dirty = true;
1339 res->res_dirty = false;
1340 list_del_init(&res->mob_head);
1341 }
1342
1343 spin_lock(&bdev->fence_lock);
1344 (void) ttm_bo_wait(bo, false, false, false);
1345 spin_unlock(&bdev->fence_lock);
1346 }
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001347}
1348
1349/**
1350 * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
1351 *
1352 * @res: The resource being queried.
1353 */
1354bool vmw_resource_needs_backup(const struct vmw_resource *res)
1355{
1356 return res->func->needs_backup;
1357}
1358
1359/**
1360 * vmw_resource_evict_type - Evict all resources of a specific type
1361 *
1362 * @dev_priv: Pointer to a device private struct
1363 * @type: The resource type to evict
1364 *
1365 * To avoid thrashing starvation or as part of the hibernation sequence,
Thomas Hellstromea029c22013-11-12 00:09:54 -08001366 * try to evict all evictable resources of a specific type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001367 */
1368static void vmw_resource_evict_type(struct vmw_private *dev_priv,
1369 enum vmw_res_type type)
1370{
1371 struct list_head *lru_list = &dev_priv->res_lru[type];
1372 struct vmw_resource *evict_res;
Thomas Hellstromea029c22013-11-12 00:09:54 -08001373 unsigned err_count = 0;
1374 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001375
1376 do {
1377 write_lock(&dev_priv->resource_lock);
1378
1379 if (list_empty(lru_list))
1380 goto out_unlock;
1381
1382 evict_res = vmw_resource_reference(
1383 list_first_entry(lru_list, struct vmw_resource,
1384 lru_head));
1385 list_del_init(&evict_res->lru_head);
1386 write_unlock(&dev_priv->resource_lock);
Thomas Hellstromea029c22013-11-12 00:09:54 -08001387
1388 ret = vmw_resource_do_evict(evict_res, false);
1389 if (unlikely(ret != 0)) {
1390 write_lock(&dev_priv->resource_lock);
1391 list_add_tail(&evict_res->lru_head, lru_list);
1392 write_unlock(&dev_priv->resource_lock);
1393 if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
1394 vmw_resource_unreference(&evict_res);
1395 return;
1396 }
1397 }
1398
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001399 vmw_resource_unreference(&evict_res);
1400 } while (1);
1401
1402out_unlock:
1403 write_unlock(&dev_priv->resource_lock);
1404}
1405
1406/**
1407 * vmw_resource_evict_all - Evict all evictable resources
1408 *
1409 * @dev_priv: Pointer to a device private struct
1410 *
1411 * To avoid thrashing starvation or as part of the hibernation sequence,
1412 * evict all evictable resources. In particular this means that all
1413 * guest-backed resources that are registered with the device are
1414 * evicted and the OTable becomes clean.
1415 */
1416void vmw_resource_evict_all(struct vmw_private *dev_priv)
1417{
1418 enum vmw_res_type type;
1419
1420 mutex_lock(&dev_priv->cmdbuf_mutex);
1421
1422 for (type = 0; type < vmw_res_max; ++type)
1423 vmw_resource_evict_type(dev_priv, type);
1424
1425 mutex_unlock(&dev_priv->cmdbuf_mutex);
1426}