blob: 941e5ffd1c7297bb1d7ea03d076c69dd7118b972 [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 {
38 struct ttm_base_object base;
39 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 Hellstromc0951b72012-11-20 12:19:35 +0000300 if (unlikely(base->object_type != converter->object_type))
301 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 Hellstromeffe1102010-01-13 22:28:39 +0100355void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
356{
357 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
Thomas Hellstromeffe1102010-01-13 22:28:39 +0100358
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000359 kfree(vmw_bo);
360}
361
362int vmw_dmabuf_init(struct vmw_private *dev_priv,
363 struct vmw_dma_buffer *vmw_bo,
364 size_t size, struct ttm_placement *placement,
365 bool interruptible,
366 void (*bo_free) (struct ttm_buffer_object *bo))
367{
368 struct ttm_bo_device *bdev = &dev_priv->bdev;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000369 size_t acc_size;
370 int ret;
371
372 BUG_ON(!bo_free);
373
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500374 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000375 memset(vmw_bo, 0, sizeof(*vmw_bo));
376
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000377 INIT_LIST_HEAD(&vmw_bo->res_list);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000378
379 ret = ttm_bo_init(bdev, &vmw_bo->base, size,
380 ttm_bo_type_device, placement,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +0000381 0, interruptible,
Dave Airlie129b78b2012-04-02 11:46:06 +0100382 NULL, acc_size, NULL, bo_free);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000383 return ret;
384}
385
386static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
387{
388 struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000389
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000390 ttm_base_object_kfree(vmw_user_bo, base);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000391}
392
393static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
394{
395 struct vmw_user_dma_buffer *vmw_user_bo;
396 struct ttm_base_object *base = *p_base;
397 struct ttm_buffer_object *bo;
398
399 *p_base = NULL;
400
401 if (unlikely(base == NULL))
402 return;
403
404 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
405 bo = &vmw_user_bo->dma.base;
406 ttm_bo_unref(&bo);
407}
408
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000409/**
410 * vmw_user_dmabuf_alloc - Allocate a user dma buffer
411 *
412 * @dev_priv: Pointer to a struct device private.
413 * @tfile: Pointer to a struct ttm_object_file on which to register the user
414 * object.
415 * @size: Size of the dma buffer.
416 * @shareable: Boolean whether the buffer is shareable with other open files.
417 * @handle: Pointer to where the handle value should be assigned.
418 * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
419 * should be assigned.
420 */
421int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
422 struct ttm_object_file *tfile,
423 uint32_t size,
424 bool shareable,
425 uint32_t *handle,
426 struct vmw_dma_buffer **p_dma_buf)
427{
428 struct vmw_user_dma_buffer *user_bo;
429 struct ttm_buffer_object *tmp;
430 int ret;
431
432 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
433 if (unlikely(user_bo == NULL)) {
434 DRM_ERROR("Failed to allocate a buffer.\n");
435 return -ENOMEM;
436 }
437
438 ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
439 &vmw_vram_sys_placement, true,
440 &vmw_user_dmabuf_destroy);
441 if (unlikely(ret != 0))
442 return ret;
443
444 tmp = ttm_bo_reference(&user_bo->dma.base);
445 ret = ttm_base_object_init(tfile,
446 &user_bo->base,
447 shareable,
448 ttm_buffer_type,
449 &vmw_user_dmabuf_release, NULL);
450 if (unlikely(ret != 0)) {
451 ttm_bo_unref(&tmp);
452 goto out_no_base_object;
453 }
454
455 *p_dma_buf = &user_bo->dma;
456 *handle = user_bo->base.hash.key;
457
458out_no_base_object:
459 return ret;
460}
461
Thomas Hellstromd08a9b92012-11-21 16:04:18 +0100462/**
463 * vmw_user_dmabuf_verify_access - verify access permissions on this
464 * buffer object.
465 *
466 * @bo: Pointer to the buffer object being accessed
467 * @tfile: Identifying the caller.
468 */
469int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
470 struct ttm_object_file *tfile)
471{
472 struct vmw_user_dma_buffer *vmw_user_bo;
473
474 if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
475 return -EPERM;
476
477 vmw_user_bo = vmw_user_dma_buffer(bo);
478 return (vmw_user_bo->base.tfile == tfile ||
479 vmw_user_bo->base.shareable) ? 0 : -EPERM;
480}
481
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000482int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
483 struct drm_file *file_priv)
484{
485 struct vmw_private *dev_priv = vmw_priv(dev);
486 union drm_vmw_alloc_dmabuf_arg *arg =
487 (union drm_vmw_alloc_dmabuf_arg *)data;
488 struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
489 struct drm_vmw_dmabuf_rep *rep = &arg->rep;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000490 struct vmw_dma_buffer *dma_buf;
491 uint32_t handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000492 struct vmw_master *vmaster = vmw_master(file_priv->master);
493 int ret;
494
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000495 ret = ttm_read_lock(&vmaster->lock, true);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000496 if (unlikely(ret != 0))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000497 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000498
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000499 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
500 req->size, false, &handle, &dma_buf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000501 if (unlikely(ret != 0))
Thomas Hellstrom2f5993c2010-11-17 13:24:48 +0100502 goto out_no_dmabuf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000503
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000504 rep->handle = handle;
David Herrmann72525b32013-07-24 21:08:53 +0200505 rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000506 rep->cur_gmr_id = handle;
507 rep->cur_gmr_offset = 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000508
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000509 vmw_dmabuf_unreference(&dma_buf);
510
Thomas Hellstrom2f5993c2010-11-17 13:24:48 +0100511out_no_dmabuf:
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000512 ttm_read_unlock(&vmaster->lock);
513
Thomas Hellstrom2f5993c2010-11-17 13:24:48 +0100514 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000515}
516
517int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
518 struct drm_file *file_priv)
519{
520 struct drm_vmw_unref_dmabuf_arg *arg =
521 (struct drm_vmw_unref_dmabuf_arg *)data;
522
523 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
524 arg->handle,
525 TTM_REF_USAGE);
526}
527
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000528int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
529 uint32_t handle, struct vmw_dma_buffer **out)
530{
531 struct vmw_user_dma_buffer *vmw_user_bo;
532 struct ttm_base_object *base;
533
534 base = ttm_base_object_lookup(tfile, handle);
535 if (unlikely(base == NULL)) {
536 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
537 (unsigned long)handle);
538 return -ESRCH;
539 }
540
541 if (unlikely(base->object_type != ttm_buffer_type)) {
542 ttm_base_object_unref(&base);
543 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
544 (unsigned long)handle);
545 return -EINVAL;
546 }
547
548 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
549 (void)ttm_bo_reference(&vmw_user_bo->dma.base);
550 ttm_base_object_unref(&base);
551 *out = &vmw_user_bo->dma;
552
553 return 0;
554}
555
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000556int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
557 struct vmw_dma_buffer *dma_buf)
558{
559 struct vmw_user_dma_buffer *user_bo;
560
561 if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
562 return -EINVAL;
563
564 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
565 return ttm_ref_object_add(tfile, &user_bo->base, TTM_REF_USAGE, NULL);
566}
567
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000568/*
Uwe Kleine-König65155b32010-06-11 12:17:01 +0200569 * Stream management
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000570 */
571
572static void vmw_stream_destroy(struct vmw_resource *res)
573{
574 struct vmw_private *dev_priv = res->dev_priv;
575 struct vmw_stream *stream;
576 int ret;
577
578 DRM_INFO("%s: unref\n", __func__);
579 stream = container_of(res, struct vmw_stream, res);
580
581 ret = vmw_overlay_unref(dev_priv, stream->stream_id);
582 WARN_ON(ret != 0);
583}
584
585static int vmw_stream_init(struct vmw_private *dev_priv,
586 struct vmw_stream *stream,
587 void (*res_free) (struct vmw_resource *res))
588{
589 struct vmw_resource *res = &stream->res;
590 int ret;
591
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000592 ret = vmw_resource_init(dev_priv, res, false, res_free,
593 &vmw_stream_func);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000594
595 if (unlikely(ret != 0)) {
596 if (res_free == NULL)
597 kfree(stream);
598 else
599 res_free(&stream->res);
600 return ret;
601 }
602
603 ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
604 if (ret) {
605 vmw_resource_unreference(&res);
606 return ret;
607 }
608
609 DRM_INFO("%s: claimed\n", __func__);
610
611 vmw_resource_activate(&stream->res, vmw_stream_destroy);
612 return 0;
613}
614
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000615static void vmw_user_stream_free(struct vmw_resource *res)
616{
617 struct vmw_user_stream *stream =
618 container_of(res, struct vmw_user_stream, stream.res);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200619 struct vmw_private *dev_priv = res->dev_priv;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000620
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000621 ttm_base_object_kfree(stream, base);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200622 ttm_mem_global_free(vmw_mem_glob(dev_priv),
623 vmw_user_stream_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000624}
625
626/**
627 * This function is called when user space has no more references on the
628 * base object. It releases the base-object's reference on the resource object.
629 */
630
631static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
632{
633 struct ttm_base_object *base = *p_base;
634 struct vmw_user_stream *stream =
635 container_of(base, struct vmw_user_stream, base);
636 struct vmw_resource *res = &stream->stream.res;
637
638 *p_base = NULL;
639 vmw_resource_unreference(&res);
640}
641
642int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
643 struct drm_file *file_priv)
644{
645 struct vmw_private *dev_priv = vmw_priv(dev);
646 struct vmw_resource *res;
647 struct vmw_user_stream *stream;
648 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
649 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000650 struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000651 int ret = 0;
652
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000653
654 res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000655 if (unlikely(res == NULL))
656 return -EINVAL;
657
658 if (res->res_free != &vmw_user_stream_free) {
659 ret = -EINVAL;
660 goto out;
661 }
662
663 stream = container_of(res, struct vmw_user_stream, stream.res);
664 if (stream->base.tfile != tfile) {
665 ret = -EINVAL;
666 goto out;
667 }
668
669 ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
670out:
671 vmw_resource_unreference(&res);
672 return ret;
673}
674
675int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
676 struct drm_file *file_priv)
677{
678 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200679 struct vmw_user_stream *stream;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000680 struct vmw_resource *res;
681 struct vmw_resource *tmp;
682 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
683 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200684 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000685 int ret;
686
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200687 /*
688 * Approximate idr memory usage with 128 bytes. It will be limited
689 * by maximum number_of streams anyway?
690 */
691
692 if (unlikely(vmw_user_stream_size == 0))
693 vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
694
695 ret = ttm_read_lock(&vmaster->lock, true);
696 if (unlikely(ret != 0))
697 return ret;
698
699 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
700 vmw_user_stream_size,
701 false, true);
702 if (unlikely(ret != 0)) {
703 if (ret != -ERESTARTSYS)
704 DRM_ERROR("Out of graphics memory for stream"
705 " creation.\n");
706 goto out_unlock;
707 }
708
709
710 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
711 if (unlikely(stream == NULL)) {
712 ttm_mem_global_free(vmw_mem_glob(dev_priv),
713 vmw_user_stream_size);
714 ret = -ENOMEM;
715 goto out_unlock;
716 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000717
718 res = &stream->stream.res;
719 stream->base.shareable = false;
720 stream->base.tfile = NULL;
721
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200722 /*
723 * From here on, the destructor takes over resource freeing.
724 */
725
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000726 ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
727 if (unlikely(ret != 0))
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200728 goto out_unlock;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000729
730 tmp = vmw_resource_reference(res);
731 ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
732 &vmw_user_stream_base_release, NULL);
733
734 if (unlikely(ret != 0)) {
735 vmw_resource_unreference(&tmp);
736 goto out_err;
737 }
738
739 arg->stream_id = res->id;
740out_err:
741 vmw_resource_unreference(&res);
Thomas Hellstrom414ee502011-10-07 15:23:06 +0200742out_unlock:
743 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000744 return ret;
745}
746
747int vmw_user_stream_lookup(struct vmw_private *dev_priv,
748 struct ttm_object_file *tfile,
749 uint32_t *inout_id, struct vmw_resource **out)
750{
751 struct vmw_user_stream *stream;
752 struct vmw_resource *res;
753 int ret;
754
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000755 res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
756 *inout_id);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000757 if (unlikely(res == NULL))
758 return -EINVAL;
759
760 if (res->res_free != &vmw_user_stream_free) {
761 ret = -EINVAL;
762 goto err_ref;
763 }
764
765 stream = container_of(res, struct vmw_user_stream, stream.res);
766 if (stream->base.tfile != tfile) {
767 ret = -EPERM;
768 goto err_ref;
769 }
770
771 *inout_id = stream->stream.stream_id;
772 *out = res;
773 return 0;
774err_ref:
775 vmw_resource_unreference(&res);
776 return ret;
777}
Dave Airlie5e1782d2012-08-28 01:53:54 +0000778
779
780int vmw_dumb_create(struct drm_file *file_priv,
781 struct drm_device *dev,
782 struct drm_mode_create_dumb *args)
783{
784 struct vmw_private *dev_priv = vmw_priv(dev);
785 struct vmw_master *vmaster = vmw_master(file_priv->master);
786 struct vmw_user_dma_buffer *vmw_user_bo;
787 struct ttm_buffer_object *tmp;
788 int ret;
789
790 args->pitch = args->width * ((args->bpp + 7) / 8);
791 args->size = args->pitch * args->height;
792
793 vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
794 if (vmw_user_bo == NULL)
795 return -ENOMEM;
796
797 ret = ttm_read_lock(&vmaster->lock, true);
798 if (ret != 0) {
799 kfree(vmw_user_bo);
800 return ret;
801 }
802
803 ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
804 &vmw_vram_sys_placement, true,
805 &vmw_user_dmabuf_destroy);
806 if (ret != 0)
807 goto out_no_dmabuf;
808
809 tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
810 ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
811 &vmw_user_bo->base,
812 false,
813 ttm_buffer_type,
814 &vmw_user_dmabuf_release, NULL);
815 if (unlikely(ret != 0))
816 goto out_no_base_object;
817
818 args->handle = vmw_user_bo->base.hash.key;
819
820out_no_base_object:
821 ttm_bo_unref(&tmp);
822out_no_dmabuf:
823 ttm_read_unlock(&vmaster->lock);
824 return ret;
825}
826
827int vmw_dumb_map_offset(struct drm_file *file_priv,
828 struct drm_device *dev, uint32_t handle,
829 uint64_t *offset)
830{
831 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
832 struct vmw_dma_buffer *out_buf;
833 int ret;
834
835 ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
836 if (ret != 0)
837 return -EINVAL;
838
David Herrmann72525b32013-07-24 21:08:53 +0200839 *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
Dave Airlie5e1782d2012-08-28 01:53:54 +0000840 vmw_dmabuf_unreference(&out_buf);
841 return 0;
842}
843
844int vmw_dumb_destroy(struct drm_file *file_priv,
845 struct drm_device *dev,
846 uint32_t handle)
847{
848 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
849 handle, TTM_REF_USAGE);
850}
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000851
852/**
853 * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
854 *
855 * @res: The resource for which to allocate a backup buffer.
856 * @interruptible: Whether any sleeps during allocation should be
857 * performed while interruptible.
858 */
859static int vmw_resource_buf_alloc(struct vmw_resource *res,
860 bool interruptible)
861{
862 unsigned long size =
863 (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
864 struct vmw_dma_buffer *backup;
865 int ret;
866
867 if (likely(res->backup)) {
868 BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
869 return 0;
870 }
871
872 backup = kzalloc(sizeof(*backup), GFP_KERNEL);
873 if (unlikely(backup == NULL))
874 return -ENOMEM;
875
876 ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
877 res->func->backup_placement,
878 interruptible,
879 &vmw_dmabuf_bo_free);
880 if (unlikely(ret != 0))
881 goto out_no_dmabuf;
882
883 res->backup = backup;
884
885out_no_dmabuf:
886 return ret;
887}
888
889/**
890 * vmw_resource_do_validate - Make a resource up-to-date and visible
891 * to the device.
892 *
893 * @res: The resource to make visible to the device.
894 * @val_buf: Information about a buffer possibly
895 * containing backup data if a bind operation is needed.
896 *
897 * On hardware resource shortage, this function returns -EBUSY and
898 * should be retried once resources have been freed up.
899 */
900static int vmw_resource_do_validate(struct vmw_resource *res,
901 struct ttm_validate_buffer *val_buf)
902{
903 int ret = 0;
904 const struct vmw_res_func *func = res->func;
905
906 if (unlikely(res->id == -1)) {
907 ret = func->create(res);
908 if (unlikely(ret != 0))
909 return ret;
910 }
911
912 if (func->bind &&
913 ((func->needs_backup && list_empty(&res->mob_head) &&
914 val_buf->bo != NULL) ||
915 (!func->needs_backup && val_buf->bo != NULL))) {
916 ret = func->bind(res, val_buf);
917 if (unlikely(ret != 0))
918 goto out_bind_failed;
919 if (func->needs_backup)
920 list_add_tail(&res->mob_head, &res->backup->res_list);
921 }
922
923 /*
924 * Only do this on write operations, and move to
925 * vmw_resource_unreserve if it can be called after
926 * backup buffers have been unreserved. Otherwise
927 * sort out locking.
928 */
929 res->res_dirty = true;
930
931 return 0;
932
933out_bind_failed:
934 func->destroy(res);
935
936 return ret;
937}
938
939/**
940 * vmw_resource_unreserve - Unreserve a resource previously reserved for
941 * command submission.
942 *
943 * @res: Pointer to the struct vmw_resource to unreserve.
944 * @new_backup: Pointer to new backup buffer if command submission
945 * switched.
946 * @new_backup_offset: New backup offset if @new_backup is !NULL.
947 *
948 * Currently unreserving a resource means putting it back on the device's
949 * resource lru list, so that it can be evicted if necessary.
950 */
951void vmw_resource_unreserve(struct vmw_resource *res,
952 struct vmw_dma_buffer *new_backup,
953 unsigned long new_backup_offset)
954{
955 struct vmw_private *dev_priv = res->dev_priv;
956
957 if (!list_empty(&res->lru_head))
958 return;
959
960 if (new_backup && new_backup != res->backup) {
961
962 if (res->backup) {
Maarten Lankhorst8bd4ce52013-06-27 13:48:27 +0200963 lockdep_assert_held(&res->backup->base.resv->lock.base);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000964 list_del_init(&res->mob_head);
965 vmw_dmabuf_unreference(&res->backup);
966 }
967
968 res->backup = vmw_dmabuf_reference(new_backup);
Maarten Lankhorst8bd4ce52013-06-27 13:48:27 +0200969 lockdep_assert_held(&new_backup->base.resv->lock.base);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000970 list_add_tail(&res->mob_head, &new_backup->res_list);
971 }
972 if (new_backup)
973 res->backup_offset = new_backup_offset;
974
975 if (!res->func->may_evict)
976 return;
977
978 write_lock(&dev_priv->resource_lock);
979 list_add_tail(&res->lru_head,
980 &res->dev_priv->res_lru[res->func->res_type]);
981 write_unlock(&dev_priv->resource_lock);
982}
983
984/**
985 * vmw_resource_check_buffer - Check whether a backup buffer is needed
986 * for a resource and in that case, allocate
987 * one, reserve and validate it.
988 *
989 * @res: The resource for which to allocate a backup buffer.
990 * @interruptible: Whether any sleeps during allocation should be
991 * performed while interruptible.
992 * @val_buf: On successful return contains data about the
993 * reserved and validated backup buffer.
994 */
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200995static int
996vmw_resource_check_buffer(struct vmw_resource *res,
997 struct ww_acquire_ctx *ticket,
998 bool interruptible,
999 struct ttm_validate_buffer *val_buf)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001000{
1001 struct list_head val_list;
1002 bool backup_dirty = false;
1003 int ret;
1004
1005 if (unlikely(res->backup == NULL)) {
1006 ret = vmw_resource_buf_alloc(res, interruptible);
1007 if (unlikely(ret != 0))
1008 return ret;
1009 }
1010
1011 INIT_LIST_HEAD(&val_list);
1012 val_buf->bo = ttm_bo_reference(&res->backup->base);
1013 list_add_tail(&val_buf->head, &val_list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001014 ret = ttm_eu_reserve_buffers(ticket, &val_list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001015 if (unlikely(ret != 0))
1016 goto out_no_reserve;
1017
1018 if (res->func->needs_backup && list_empty(&res->mob_head))
1019 return 0;
1020
1021 backup_dirty = res->backup_dirty;
1022 ret = ttm_bo_validate(&res->backup->base,
1023 res->func->backup_placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001024 true, false);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001025
1026 if (unlikely(ret != 0))
1027 goto out_no_validate;
1028
1029 return 0;
1030
1031out_no_validate:
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001032 ttm_eu_backoff_reservation(ticket, &val_list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001033out_no_reserve:
1034 ttm_bo_unref(&val_buf->bo);
1035 if (backup_dirty)
1036 vmw_dmabuf_unreference(&res->backup);
1037
1038 return ret;
1039}
1040
1041/**
1042 * vmw_resource_reserve - Reserve a resource for command submission
1043 *
1044 * @res: The resource to reserve.
1045 *
1046 * This function takes the resource off the LRU list and make sure
1047 * a backup buffer is present for guest-backed resources. However,
1048 * the buffer may not be bound to the resource at this point.
1049 *
1050 */
1051int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
1052{
1053 struct vmw_private *dev_priv = res->dev_priv;
1054 int ret;
1055
1056 write_lock(&dev_priv->resource_lock);
1057 list_del_init(&res->lru_head);
1058 write_unlock(&dev_priv->resource_lock);
1059
1060 if (res->func->needs_backup && res->backup == NULL &&
1061 !no_backup) {
1062 ret = vmw_resource_buf_alloc(res, true);
1063 if (unlikely(ret != 0))
1064 return ret;
1065 }
1066
1067 return 0;
1068}
1069
1070/**
1071 * vmw_resource_backoff_reservation - Unreserve and unreference a
1072 * backup buffer
1073 *.
1074 * @val_buf: Backup buffer information.
1075 */
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001076static void
1077vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
1078 struct ttm_validate_buffer *val_buf)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001079{
1080 struct list_head val_list;
1081
1082 if (likely(val_buf->bo == NULL))
1083 return;
1084
1085 INIT_LIST_HEAD(&val_list);
1086 list_add_tail(&val_buf->head, &val_list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001087 ttm_eu_backoff_reservation(ticket, &val_list);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001088 ttm_bo_unref(&val_buf->bo);
1089}
1090
1091/**
1092 * vmw_resource_do_evict - Evict a resource, and transfer its data
1093 * to a backup buffer.
1094 *
1095 * @res: The resource to evict.
Thomas Hellstromea029c22013-11-12 00:09:54 -08001096 * @interruptible: Whether to wait interruptible.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001097 */
Thomas Hellstromea029c22013-11-12 00:09:54 -08001098int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001099{
1100 struct ttm_validate_buffer val_buf;
1101 const struct vmw_res_func *func = res->func;
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001102 struct ww_acquire_ctx ticket;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001103 int ret;
1104
1105 BUG_ON(!func->may_evict);
1106
1107 val_buf.bo = NULL;
Thomas Hellstromea029c22013-11-12 00:09:54 -08001108 ret = vmw_resource_check_buffer(res, &ticket, interruptible,
1109 &val_buf);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001110 if (unlikely(ret != 0))
1111 return ret;
1112
1113 if (unlikely(func->unbind != NULL &&
1114 (!func->needs_backup || !list_empty(&res->mob_head)))) {
1115 ret = func->unbind(res, res->res_dirty, &val_buf);
1116 if (unlikely(ret != 0))
1117 goto out_no_unbind;
1118 list_del_init(&res->mob_head);
1119 }
1120 ret = func->destroy(res);
1121 res->backup_dirty = true;
1122 res->res_dirty = false;
1123out_no_unbind:
Maarten Lankhorstecff6652013-06-27 13:48:17 +02001124 vmw_resource_backoff_reservation(&ticket, &val_buf);
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001125
1126 return ret;
1127}
1128
1129
1130/**
1131 * vmw_resource_validate - Make a resource up-to-date and visible
1132 * to the device.
1133 *
1134 * @res: The resource to make visible to the device.
1135 *
1136 * On succesful return, any backup DMA buffer pointed to by @res->backup will
1137 * be reserved and validated.
1138 * On hardware resource shortage, this function will repeatedly evict
1139 * resources of the same type until the validation succeeds.
1140 */
1141int vmw_resource_validate(struct vmw_resource *res)
1142{
1143 int ret;
1144 struct vmw_resource *evict_res;
1145 struct vmw_private *dev_priv = res->dev_priv;
1146 struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
1147 struct ttm_validate_buffer val_buf;
Thomas Hellstromea029c22013-11-12 00:09:54 -08001148 unsigned err_count = 0;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001149
1150 if (likely(!res->func->may_evict))
1151 return 0;
1152
1153 val_buf.bo = NULL;
1154 if (res->backup)
1155 val_buf.bo = &res->backup->base;
1156 do {
1157 ret = vmw_resource_do_validate(res, &val_buf);
1158 if (likely(ret != -EBUSY))
1159 break;
1160
1161 write_lock(&dev_priv->resource_lock);
1162 if (list_empty(lru_list) || !res->func->may_evict) {
Thomas Hellstromea029c22013-11-12 00:09:54 -08001163 DRM_ERROR("Out of device device resources "
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001164 "for %s.\n", res->func->type_name);
1165 ret = -EBUSY;
1166 write_unlock(&dev_priv->resource_lock);
1167 break;
1168 }
1169
1170 evict_res = vmw_resource_reference
1171 (list_first_entry(lru_list, struct vmw_resource,
1172 lru_head));
1173 list_del_init(&evict_res->lru_head);
1174
1175 write_unlock(&dev_priv->resource_lock);
Thomas Hellstromea029c22013-11-12 00:09:54 -08001176
1177 ret = vmw_resource_do_evict(evict_res, true);
1178 if (unlikely(ret != 0)) {
1179 write_lock(&dev_priv->resource_lock);
1180 list_add_tail(&evict_res->lru_head, lru_list);
1181 write_unlock(&dev_priv->resource_lock);
1182 if (ret == -ERESTARTSYS ||
1183 ++err_count > VMW_RES_EVICT_ERR_COUNT) {
1184 vmw_resource_unreference(&evict_res);
1185 goto out_no_validate;
1186 }
1187 }
1188
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001189 vmw_resource_unreference(&evict_res);
1190 } while (1);
1191
1192 if (unlikely(ret != 0))
1193 goto out_no_validate;
1194 else if (!res->func->needs_backup && res->backup) {
1195 list_del_init(&res->mob_head);
1196 vmw_dmabuf_unreference(&res->backup);
1197 }
1198
1199 return 0;
1200
1201out_no_validate:
1202 return ret;
1203}
1204
1205/**
1206 * vmw_fence_single_bo - Utility function to fence a single TTM buffer
1207 * object without unreserving it.
1208 *
1209 * @bo: Pointer to the struct ttm_buffer_object to fence.
1210 * @fence: Pointer to the fence. If NULL, this function will
1211 * insert a fence into the command stream..
1212 *
1213 * Contrary to the ttm_eu version of this function, it takes only
1214 * a single buffer object instead of a list, and it also doesn't
1215 * unreserve the buffer object, which needs to be done separately.
1216 */
1217void vmw_fence_single_bo(struct ttm_buffer_object *bo,
1218 struct vmw_fence_obj *fence)
1219{
1220 struct ttm_bo_device *bdev = bo->bdev;
1221 struct ttm_bo_driver *driver = bdev->driver;
1222 struct vmw_fence_obj *old_fence_obj;
1223 struct vmw_private *dev_priv =
1224 container_of(bdev, struct vmw_private, bdev);
1225
1226 if (fence == NULL)
1227 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1228 else
1229 driver->sync_obj_ref(fence);
1230
1231 spin_lock(&bdev->fence_lock);
1232
1233 old_fence_obj = bo->sync_obj;
1234 bo->sync_obj = fence;
1235
1236 spin_unlock(&bdev->fence_lock);
1237
1238 if (old_fence_obj)
1239 vmw_fence_obj_unreference(&old_fence_obj);
1240}
1241
1242/**
1243 * vmw_resource_move_notify - TTM move_notify_callback
1244 *
1245 * @bo: The TTM buffer object about to move.
1246 * @mem: The truct ttm_mem_reg indicating to what memory
1247 * region the move is taking place.
1248 *
1249 * For now does nothing.
1250 */
1251void vmw_resource_move_notify(struct ttm_buffer_object *bo,
1252 struct ttm_mem_reg *mem)
1253{
1254}
1255
1256/**
1257 * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
1258 *
1259 * @res: The resource being queried.
1260 */
1261bool vmw_resource_needs_backup(const struct vmw_resource *res)
1262{
1263 return res->func->needs_backup;
1264}
1265
1266/**
1267 * vmw_resource_evict_type - Evict all resources of a specific type
1268 *
1269 * @dev_priv: Pointer to a device private struct
1270 * @type: The resource type to evict
1271 *
1272 * To avoid thrashing starvation or as part of the hibernation sequence,
Thomas Hellstromea029c22013-11-12 00:09:54 -08001273 * try to evict all evictable resources of a specific type.
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001274 */
1275static void vmw_resource_evict_type(struct vmw_private *dev_priv,
1276 enum vmw_res_type type)
1277{
1278 struct list_head *lru_list = &dev_priv->res_lru[type];
1279 struct vmw_resource *evict_res;
Thomas Hellstromea029c22013-11-12 00:09:54 -08001280 unsigned err_count = 0;
1281 int ret;
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001282
1283 do {
1284 write_lock(&dev_priv->resource_lock);
1285
1286 if (list_empty(lru_list))
1287 goto out_unlock;
1288
1289 evict_res = vmw_resource_reference(
1290 list_first_entry(lru_list, struct vmw_resource,
1291 lru_head));
1292 list_del_init(&evict_res->lru_head);
1293 write_unlock(&dev_priv->resource_lock);
Thomas Hellstromea029c22013-11-12 00:09:54 -08001294
1295 ret = vmw_resource_do_evict(evict_res, false);
1296 if (unlikely(ret != 0)) {
1297 write_lock(&dev_priv->resource_lock);
1298 list_add_tail(&evict_res->lru_head, lru_list);
1299 write_unlock(&dev_priv->resource_lock);
1300 if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
1301 vmw_resource_unreference(&evict_res);
1302 return;
1303 }
1304 }
1305
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001306 vmw_resource_unreference(&evict_res);
1307 } while (1);
1308
1309out_unlock:
1310 write_unlock(&dev_priv->resource_lock);
1311}
1312
1313/**
1314 * vmw_resource_evict_all - Evict all evictable resources
1315 *
1316 * @dev_priv: Pointer to a device private struct
1317 *
1318 * To avoid thrashing starvation or as part of the hibernation sequence,
1319 * evict all evictable resources. In particular this means that all
1320 * guest-backed resources that are registered with the device are
1321 * evicted and the OTable becomes clean.
1322 */
1323void vmw_resource_evict_all(struct vmw_private *dev_priv)
1324{
1325 enum vmw_res_type type;
1326
1327 mutex_lock(&dev_priv->cmdbuf_mutex);
1328
1329 for (type = 0; type < vmw_res_max; ++type)
1330 vmw_resource_evict_type(dev_priv, type);
1331
1332 mutex_unlock(&dev_priv->cmdbuf_mutex);
1333}