1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include <drm/vmwgfx_drm.h>
30 #include <drm/ttm/ttm_object.h>
31 #include <drm/ttm/ttm_placement.h>
33 #include "vmwgfx_resource_priv.h"
35 struct vmw_user_dma_buffer {
36 struct ttm_base_object base;
37 struct vmw_dma_buffer dma;
40 struct vmw_bo_user_rep {
46 struct vmw_resource res;
50 struct vmw_user_stream {
51 struct ttm_base_object base;
52 struct vmw_stream stream;
56 static uint64_t vmw_user_stream_size;
58 static const struct vmw_res_func vmw_stream_func = {
59 .res_type = vmw_res_stream,
60 .needs_backup = false,
62 .type_name = "video streams",
63 .backup_placement = NULL,
70 static inline struct vmw_dma_buffer *
71 vmw_dma_buffer(struct ttm_buffer_object *bo)
73 return container_of(bo, struct vmw_dma_buffer, base);
76 static inline struct vmw_user_dma_buffer *
77 vmw_user_dma_buffer(struct ttm_buffer_object *bo)
79 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
80 return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
83 struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
91 * vmw_resource_release_id - release a resource id to the id manager.
93 * @res: Pointer to the resource.
95 * Release the resource id to the resource id manager and set it to -1
97 void vmw_resource_release_id(struct vmw_resource *res)
99 struct vmw_private *dev_priv = res->dev_priv;
100 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
102 write_lock(&dev_priv->resource_lock);
104 idr_remove(idr, res->id);
106 write_unlock(&dev_priv->resource_lock);
109 static void vmw_resource_release(struct kref *kref)
111 struct vmw_resource *res =
112 container_of(kref, struct vmw_resource, kref);
113 struct vmw_private *dev_priv = res->dev_priv;
115 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
118 list_del_init(&res->lru_head);
119 write_unlock(&dev_priv->resource_lock);
121 struct ttm_buffer_object *bo = &res->backup->base;
123 ttm_bo_reserve(bo, false, false, false, 0);
124 if (!list_empty(&res->mob_head) &&
125 res->func->unbind != NULL) {
126 struct ttm_validate_buffer val_buf;
129 res->func->unbind(res, false, &val_buf);
131 res->backup_dirty = false;
132 list_del_init(&res->mob_head);
133 ttm_bo_unreserve(bo);
134 vmw_dmabuf_unreference(&res->backup);
137 if (likely(res->hw_destroy != NULL))
138 res->hw_destroy(res);
141 if (res->res_free != NULL)
146 write_lock(&dev_priv->resource_lock);
152 void vmw_resource_unreference(struct vmw_resource **p_res)
154 struct vmw_resource *res = *p_res;
155 struct vmw_private *dev_priv = res->dev_priv;
158 write_lock(&dev_priv->resource_lock);
159 kref_put(&res->kref, vmw_resource_release);
160 write_unlock(&dev_priv->resource_lock);
165 * vmw_resource_alloc_id - release a resource id to the id manager.
167 * @res: Pointer to the resource.
169 * Allocate the lowest free resource from the resource manager, and set
170 * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
172 int vmw_resource_alloc_id(struct vmw_resource *res)
174 struct vmw_private *dev_priv = res->dev_priv;
176 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
178 BUG_ON(res->id != -1);
181 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
184 write_lock(&dev_priv->resource_lock);
185 ret = idr_get_new_above(idr, res, 1, &res->id);
186 write_unlock(&dev_priv->resource_lock);
188 } while (ret == -EAGAIN);
194 * vmw_resource_init - initialize a struct vmw_resource
196 * @dev_priv: Pointer to a device private struct.
197 * @res: The struct vmw_resource to initialize.
198 * @obj_type: Resource object type.
199 * @delay_id: Boolean whether to defer device id allocation until
200 * the first validation.
201 * @res_free: Resource destructor.
202 * @func: Resource function table.
204 int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
206 void (*res_free) (struct vmw_resource *res),
207 const struct vmw_res_func *func)
209 kref_init(&res->kref);
210 res->hw_destroy = NULL;
211 res->res_free = res_free;
213 res->dev_priv = dev_priv;
215 INIT_LIST_HEAD(&res->lru_head);
216 INIT_LIST_HEAD(&res->mob_head);
219 res->backup_offset = 0;
220 res->backup_dirty = false;
221 res->res_dirty = false;
225 return vmw_resource_alloc_id(res);
229 * vmw_resource_activate
231 * @res: Pointer to the newly created resource
232 * @hw_destroy: Destroy function. NULL if none.
234 * Activate a resource after the hardware has been made aware of it.
235 * Set tye destroy function to @destroy. Typically this frees the
236 * resource and destroys the hardware resources associated with it.
237 * Activate basically means that the function vmw_resource_lookup will
240 void vmw_resource_activate(struct vmw_resource *res,
241 void (*hw_destroy) (struct vmw_resource *))
243 struct vmw_private *dev_priv = res->dev_priv;
245 write_lock(&dev_priv->resource_lock);
247 res->hw_destroy = hw_destroy;
248 write_unlock(&dev_priv->resource_lock);
251 struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
252 struct idr *idr, int id)
254 struct vmw_resource *res;
256 read_lock(&dev_priv->resource_lock);
257 res = idr_find(idr, id);
258 if (res && res->avail)
259 kref_get(&res->kref);
262 read_unlock(&dev_priv->resource_lock);
264 if (unlikely(res == NULL))
271 * vmw_user_resource_lookup_handle - lookup a struct resource from a
272 * TTM user-space handle and perform basic type checks
274 * @dev_priv: Pointer to a device private struct
275 * @tfile: Pointer to a struct ttm_object_file identifying the caller
276 * @handle: The TTM user-space handle
277 * @converter: Pointer to an object describing the resource type
278 * @p_res: On successful return the location pointed to will contain
279 * a pointer to a refcounted struct vmw_resource.
281 * If the handle can't be found or is associated with an incorrect resource
282 * type, -EINVAL will be returned.
284 int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
285 struct ttm_object_file *tfile,
287 const struct vmw_user_resource_conv
289 struct vmw_resource **p_res)
291 struct ttm_base_object *base;
292 struct vmw_resource *res;
295 base = ttm_base_object_lookup(tfile, handle);
296 if (unlikely(base == NULL))
299 if (unlikely(base->object_type != converter->object_type))
300 goto out_bad_resource;
302 res = converter->base_obj_to_res(base);
304 read_lock(&dev_priv->resource_lock);
305 if (!res->avail || res->res_free != converter->res_free) {
306 read_unlock(&dev_priv->resource_lock);
307 goto out_bad_resource;
310 kref_get(&res->kref);
311 read_unlock(&dev_priv->resource_lock);
317 ttm_base_object_unref(&base);
323 * Helper function that looks either a surface or dmabuf.
325 * The pointer this pointed at by out_surf and out_buf needs to be null.
327 int vmw_user_lookup_handle(struct vmw_private *dev_priv,
328 struct ttm_object_file *tfile,
330 struct vmw_surface **out_surf,
331 struct vmw_dma_buffer **out_buf)
333 struct vmw_resource *res;
336 BUG_ON(*out_surf || *out_buf);
338 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
339 user_surface_converter,
342 *out_surf = vmw_res_to_srf(res);
347 ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
354 void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
356 struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
361 int vmw_dmabuf_init(struct vmw_private *dev_priv,
362 struct vmw_dma_buffer *vmw_bo,
363 size_t size, struct ttm_placement *placement,
365 void (*bo_free) (struct ttm_buffer_object *bo))
367 struct ttm_bo_device *bdev = &dev_priv->bdev;
373 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
374 memset(vmw_bo, 0, sizeof(*vmw_bo));
376 INIT_LIST_HEAD(&vmw_bo->res_list);
378 ret = ttm_bo_init(bdev, &vmw_bo->base, size,
379 ttm_bo_type_device, placement,
381 NULL, acc_size, NULL, bo_free);
385 static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
387 struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
389 ttm_base_object_kfree(vmw_user_bo, base);
392 static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
394 struct vmw_user_dma_buffer *vmw_user_bo;
395 struct ttm_base_object *base = *p_base;
396 struct ttm_buffer_object *bo;
400 if (unlikely(base == NULL))
403 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
404 bo = &vmw_user_bo->dma.base;
409 * vmw_user_dmabuf_alloc - Allocate a user dma buffer
411 * @dev_priv: Pointer to a struct device private.
412 * @tfile: Pointer to a struct ttm_object_file on which to register the user
414 * @size: Size of the dma buffer.
415 * @shareable: Boolean whether the buffer is shareable with other open files.
416 * @handle: Pointer to where the handle value should be assigned.
417 * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
418 * should be assigned.
420 int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
421 struct ttm_object_file *tfile,
425 struct vmw_dma_buffer **p_dma_buf)
427 struct vmw_user_dma_buffer *user_bo;
428 struct ttm_buffer_object *tmp;
431 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
432 if (unlikely(user_bo == NULL)) {
433 DRM_ERROR("Failed to allocate a buffer.\n");
437 ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
438 &vmw_vram_sys_placement, true,
439 &vmw_user_dmabuf_destroy);
440 if (unlikely(ret != 0))
443 tmp = ttm_bo_reference(&user_bo->dma.base);
444 ret = ttm_base_object_init(tfile,
448 &vmw_user_dmabuf_release, NULL);
449 if (unlikely(ret != 0)) {
451 goto out_no_base_object;
454 *p_dma_buf = &user_bo->dma;
455 *handle = user_bo->base.hash.key;
462 * vmw_user_dmabuf_verify_access - verify access permissions on this
465 * @bo: Pointer to the buffer object being accessed
466 * @tfile: Identifying the caller.
468 int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
469 struct ttm_object_file *tfile)
471 struct vmw_user_dma_buffer *vmw_user_bo;
473 if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
476 vmw_user_bo = vmw_user_dma_buffer(bo);
477 return (vmw_user_bo->base.tfile == tfile ||
478 vmw_user_bo->base.shareable) ? 0 : -EPERM;
481 int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
482 struct drm_file *file_priv)
484 struct vmw_private *dev_priv = vmw_priv(dev);
485 union drm_vmw_alloc_dmabuf_arg *arg =
486 (union drm_vmw_alloc_dmabuf_arg *)data;
487 struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
488 struct drm_vmw_dmabuf_rep *rep = &arg->rep;
489 struct vmw_dma_buffer *dma_buf;
491 struct vmw_master *vmaster = vmw_master(file_priv->master);
494 ret = ttm_read_lock(&vmaster->lock, true);
495 if (unlikely(ret != 0))
498 ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
499 req->size, false, &handle, &dma_buf);
500 if (unlikely(ret != 0))
503 rep->handle = handle;
504 rep->map_handle = dma_buf->base.addr_space_offset;
505 rep->cur_gmr_id = handle;
506 rep->cur_gmr_offset = 0;
508 vmw_dmabuf_unreference(&dma_buf);
511 ttm_read_unlock(&vmaster->lock);
516 int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
517 struct drm_file *file_priv)
519 struct drm_vmw_unref_dmabuf_arg *arg =
520 (struct drm_vmw_unref_dmabuf_arg *)data;
522 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
527 int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
528 uint32_t handle, struct vmw_dma_buffer **out)
530 struct vmw_user_dma_buffer *vmw_user_bo;
531 struct ttm_base_object *base;
533 base = ttm_base_object_lookup(tfile, handle);
534 if (unlikely(base == NULL)) {
535 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
536 (unsigned long)handle);
540 if (unlikely(base->object_type != ttm_buffer_type)) {
541 ttm_base_object_unref(&base);
542 printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
543 (unsigned long)handle);
547 vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
548 (void)ttm_bo_reference(&vmw_user_bo->dma.base);
549 ttm_base_object_unref(&base);
550 *out = &vmw_user_bo->dma;
555 int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
556 struct vmw_dma_buffer *dma_buf)
558 struct vmw_user_dma_buffer *user_bo;
560 if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
563 user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
564 return ttm_ref_object_add(tfile, &user_bo->base, TTM_REF_USAGE, NULL);
571 static void vmw_stream_destroy(struct vmw_resource *res)
573 struct vmw_private *dev_priv = res->dev_priv;
574 struct vmw_stream *stream;
577 DRM_INFO("%s: unref\n", __func__);
578 stream = container_of(res, struct vmw_stream, res);
580 ret = vmw_overlay_unref(dev_priv, stream->stream_id);
584 static int vmw_stream_init(struct vmw_private *dev_priv,
585 struct vmw_stream *stream,
586 void (*res_free) (struct vmw_resource *res))
588 struct vmw_resource *res = &stream->res;
591 ret = vmw_resource_init(dev_priv, res, false, res_free,
594 if (unlikely(ret != 0)) {
595 if (res_free == NULL)
598 res_free(&stream->res);
602 ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
604 vmw_resource_unreference(&res);
608 DRM_INFO("%s: claimed\n", __func__);
610 vmw_resource_activate(&stream->res, vmw_stream_destroy);
614 static void vmw_user_stream_free(struct vmw_resource *res)
616 struct vmw_user_stream *stream =
617 container_of(res, struct vmw_user_stream, stream.res);
618 struct vmw_private *dev_priv = res->dev_priv;
620 ttm_base_object_kfree(stream, base);
621 ttm_mem_global_free(vmw_mem_glob(dev_priv),
622 vmw_user_stream_size);
626 * This function is called when user space has no more references on the
627 * base object. It releases the base-object's reference on the resource object.
630 static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
632 struct ttm_base_object *base = *p_base;
633 struct vmw_user_stream *stream =
634 container_of(base, struct vmw_user_stream, base);
635 struct vmw_resource *res = &stream->stream.res;
638 vmw_resource_unreference(&res);
641 int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
642 struct drm_file *file_priv)
644 struct vmw_private *dev_priv = vmw_priv(dev);
645 struct vmw_resource *res;
646 struct vmw_user_stream *stream;
647 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
648 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
649 struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
653 res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
654 if (unlikely(res == NULL))
657 if (res->res_free != &vmw_user_stream_free) {
662 stream = container_of(res, struct vmw_user_stream, stream.res);
663 if (stream->base.tfile != tfile) {
668 ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
670 vmw_resource_unreference(&res);
674 int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
675 struct drm_file *file_priv)
677 struct vmw_private *dev_priv = vmw_priv(dev);
678 struct vmw_user_stream *stream;
679 struct vmw_resource *res;
680 struct vmw_resource *tmp;
681 struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
682 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
683 struct vmw_master *vmaster = vmw_master(file_priv->master);
687 * Approximate idr memory usage with 128 bytes. It will be limited
688 * by maximum number_of streams anyway?
691 if (unlikely(vmw_user_stream_size == 0))
692 vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
694 ret = ttm_read_lock(&vmaster->lock, true);
695 if (unlikely(ret != 0))
698 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
699 vmw_user_stream_size,
701 if (unlikely(ret != 0)) {
702 if (ret != -ERESTARTSYS)
703 DRM_ERROR("Out of graphics memory for stream"
709 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
710 if (unlikely(stream == NULL)) {
711 ttm_mem_global_free(vmw_mem_glob(dev_priv),
712 vmw_user_stream_size);
717 res = &stream->stream.res;
718 stream->base.shareable = false;
719 stream->base.tfile = NULL;
722 * From here on, the destructor takes over resource freeing.
725 ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
726 if (unlikely(ret != 0))
729 tmp = vmw_resource_reference(res);
730 ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
731 &vmw_user_stream_base_release, NULL);
733 if (unlikely(ret != 0)) {
734 vmw_resource_unreference(&tmp);
738 arg->stream_id = res->id;
740 vmw_resource_unreference(&res);
742 ttm_read_unlock(&vmaster->lock);
746 int vmw_user_stream_lookup(struct vmw_private *dev_priv,
747 struct ttm_object_file *tfile,
748 uint32_t *inout_id, struct vmw_resource **out)
750 struct vmw_user_stream *stream;
751 struct vmw_resource *res;
754 res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
756 if (unlikely(res == NULL))
759 if (res->res_free != &vmw_user_stream_free) {
764 stream = container_of(res, struct vmw_user_stream, stream.res);
765 if (stream->base.tfile != tfile) {
770 *inout_id = stream->stream.stream_id;
774 vmw_resource_unreference(&res);
779 int vmw_dumb_create(struct drm_file *file_priv,
780 struct drm_device *dev,
781 struct drm_mode_create_dumb *args)
783 struct vmw_private *dev_priv = vmw_priv(dev);
784 struct vmw_master *vmaster = vmw_master(file_priv->master);
785 struct vmw_user_dma_buffer *vmw_user_bo;
786 struct ttm_buffer_object *tmp;
789 args->pitch = args->width * ((args->bpp + 7) / 8);
790 args->size = args->pitch * args->height;
792 vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
793 if (vmw_user_bo == NULL)
796 ret = ttm_read_lock(&vmaster->lock, true);
802 ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
803 &vmw_vram_sys_placement, true,
804 &vmw_user_dmabuf_destroy);
808 tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
809 ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
813 &vmw_user_dmabuf_release, NULL);
814 if (unlikely(ret != 0))
815 goto out_no_base_object;
817 args->handle = vmw_user_bo->base.hash.key;
822 ttm_read_unlock(&vmaster->lock);
826 int vmw_dumb_map_offset(struct drm_file *file_priv,
827 struct drm_device *dev, uint32_t handle,
830 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
831 struct vmw_dma_buffer *out_buf;
834 ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
838 *offset = out_buf->base.addr_space_offset;
839 vmw_dmabuf_unreference(&out_buf);
843 int vmw_dumb_destroy(struct drm_file *file_priv,
844 struct drm_device *dev,
847 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
848 handle, TTM_REF_USAGE);
852 * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
854 * @res: The resource for which to allocate a backup buffer.
855 * @interruptible: Whether any sleeps during allocation should be
856 * performed while interruptible.
858 static int vmw_resource_buf_alloc(struct vmw_resource *res,
862 (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
863 struct vmw_dma_buffer *backup;
866 if (likely(res->backup)) {
867 BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
871 backup = kzalloc(sizeof(*backup), GFP_KERNEL);
872 if (unlikely(backup == NULL))
875 ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
876 res->func->backup_placement,
878 &vmw_dmabuf_bo_free);
879 if (unlikely(ret != 0))
882 res->backup = backup;
889 * vmw_resource_do_validate - Make a resource up-to-date and visible
892 * @res: The resource to make visible to the device.
893 * @val_buf: Information about a buffer possibly
894 * containing backup data if a bind operation is needed.
896 * On hardware resource shortage, this function returns -EBUSY and
897 * should be retried once resources have been freed up.
899 static int vmw_resource_do_validate(struct vmw_resource *res,
900 struct ttm_validate_buffer *val_buf)
903 const struct vmw_res_func *func = res->func;
905 if (unlikely(res->id == -1)) {
906 ret = func->create(res);
907 if (unlikely(ret != 0))
912 ((func->needs_backup && list_empty(&res->mob_head) &&
913 val_buf->bo != NULL) ||
914 (!func->needs_backup && val_buf->bo != NULL))) {
915 ret = func->bind(res, val_buf);
916 if (unlikely(ret != 0))
917 goto out_bind_failed;
918 if (func->needs_backup)
919 list_add_tail(&res->mob_head, &res->backup->res_list);
923 * Only do this on write operations, and move to
924 * vmw_resource_unreserve if it can be called after
925 * backup buffers have been unreserved. Otherwise
928 res->res_dirty = true;
939 * vmw_resource_unreserve - Unreserve a resource previously reserved for
940 * command submission.
942 * @res: Pointer to the struct vmw_resource to unreserve.
943 * @new_backup: Pointer to new backup buffer if command submission
945 * @new_backup_offset: New backup offset if @new_backup is !NULL.
947 * Currently unreserving a resource means putting it back on the device's
948 * resource lru list, so that it can be evicted if necessary.
950 void vmw_resource_unreserve(struct vmw_resource *res,
951 struct vmw_dma_buffer *new_backup,
952 unsigned long new_backup_offset)
954 struct vmw_private *dev_priv = res->dev_priv;
956 if (!list_empty(&res->lru_head))
959 if (new_backup && new_backup != res->backup) {
962 BUG_ON(atomic_read(&res->backup->base.reserved) == 0);
963 list_del_init(&res->mob_head);
964 vmw_dmabuf_unreference(&res->backup);
967 res->backup = vmw_dmabuf_reference(new_backup);
968 BUG_ON(atomic_read(&new_backup->base.reserved) == 0);
969 list_add_tail(&res->mob_head, &new_backup->res_list);
972 res->backup_offset = new_backup_offset;
974 if (!res->func->may_evict)
977 write_lock(&dev_priv->resource_lock);
978 list_add_tail(&res->lru_head,
979 &res->dev_priv->res_lru[res->func->res_type]);
980 write_unlock(&dev_priv->resource_lock);
984 * vmw_resource_check_buffer - Check whether a backup buffer is needed
985 * for a resource and in that case, allocate
986 * one, reserve and validate it.
988 * @res: The resource for which to allocate a backup buffer.
989 * @interruptible: Whether any sleeps during allocation should be
990 * performed while interruptible.
991 * @val_buf: On successful return contains data about the
992 * reserved and validated backup buffer.
994 int vmw_resource_check_buffer(struct vmw_resource *res,
996 struct ttm_validate_buffer *val_buf)
998 struct list_head val_list;
999 bool backup_dirty = false;
1002 if (unlikely(res->backup == NULL)) {
1003 ret = vmw_resource_buf_alloc(res, interruptible);
1004 if (unlikely(ret != 0))
1008 INIT_LIST_HEAD(&val_list);
1009 val_buf->bo = ttm_bo_reference(&res->backup->base);
1010 list_add_tail(&val_buf->head, &val_list);
1011 ret = ttm_eu_reserve_buffers(&val_list);
1012 if (unlikely(ret != 0))
1013 goto out_no_reserve;
1015 if (res->func->needs_backup && list_empty(&res->mob_head))
1018 backup_dirty = res->backup_dirty;
1019 ret = ttm_bo_validate(&res->backup->base,
1020 res->func->backup_placement,
1023 if (unlikely(ret != 0))
1024 goto out_no_validate;
1029 ttm_eu_backoff_reservation(&val_list);
1031 ttm_bo_unref(&val_buf->bo);
1033 vmw_dmabuf_unreference(&res->backup);
1039 * vmw_resource_reserve - Reserve a resource for command submission
1041 * @res: The resource to reserve.
1043 * This function takes the resource off the LRU list and make sure
1044 * a backup buffer is present for guest-backed resources. However,
1045 * the buffer may not be bound to the resource at this point.
1048 int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
1050 struct vmw_private *dev_priv = res->dev_priv;
1053 write_lock(&dev_priv->resource_lock);
1054 list_del_init(&res->lru_head);
1055 write_unlock(&dev_priv->resource_lock);
1057 if (res->func->needs_backup && res->backup == NULL &&
1059 ret = vmw_resource_buf_alloc(res, true);
1060 if (unlikely(ret != 0))
1068 * vmw_resource_backoff_reservation - Unreserve and unreference a
1071 * @val_buf: Backup buffer information.
1073 void vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
1075 struct list_head val_list;
1077 if (likely(val_buf->bo == NULL))
1080 INIT_LIST_HEAD(&val_list);
1081 list_add_tail(&val_buf->head, &val_list);
1082 ttm_eu_backoff_reservation(&val_list);
1083 ttm_bo_unref(&val_buf->bo);
1087 * vmw_resource_do_evict - Evict a resource, and transfer its data
1088 * to a backup buffer.
1090 * @res: The resource to evict.
1092 int vmw_resource_do_evict(struct vmw_resource *res)
1094 struct ttm_validate_buffer val_buf;
1095 const struct vmw_res_func *func = res->func;
1098 BUG_ON(!func->may_evict);
1101 ret = vmw_resource_check_buffer(res, true, &val_buf);
1102 if (unlikely(ret != 0))
1105 if (unlikely(func->unbind != NULL &&
1106 (!func->needs_backup || !list_empty(&res->mob_head)))) {
1107 ret = func->unbind(res, res->res_dirty, &val_buf);
1108 if (unlikely(ret != 0))
1110 list_del_init(&res->mob_head);
1112 ret = func->destroy(res);
1113 res->backup_dirty = true;
1114 res->res_dirty = false;
1116 vmw_resource_backoff_reservation(&val_buf);
1123 * vmw_resource_validate - Make a resource up-to-date and visible
1126 * @res: The resource to make visible to the device.
1128 * On succesful return, any backup DMA buffer pointed to by @res->backup will
1129 * be reserved and validated.
1130 * On hardware resource shortage, this function will repeatedly evict
1131 * resources of the same type until the validation succeeds.
1133 int vmw_resource_validate(struct vmw_resource *res)
1136 struct vmw_resource *evict_res;
1137 struct vmw_private *dev_priv = res->dev_priv;
1138 struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
1139 struct ttm_validate_buffer val_buf;
1141 if (likely(!res->func->may_evict))
1146 val_buf.bo = &res->backup->base;
1148 ret = vmw_resource_do_validate(res, &val_buf);
1149 if (likely(ret != -EBUSY))
1152 write_lock(&dev_priv->resource_lock);
1153 if (list_empty(lru_list) || !res->func->may_evict) {
1154 DRM_ERROR("Out of device device id entries "
1155 "for %s.\n", res->func->type_name);
1157 write_unlock(&dev_priv->resource_lock);
1161 evict_res = vmw_resource_reference
1162 (list_first_entry(lru_list, struct vmw_resource,
1164 list_del_init(&evict_res->lru_head);
1166 write_unlock(&dev_priv->resource_lock);
1167 vmw_resource_do_evict(evict_res);
1168 vmw_resource_unreference(&evict_res);
1171 if (unlikely(ret != 0))
1172 goto out_no_validate;
1173 else if (!res->func->needs_backup && res->backup) {
1174 list_del_init(&res->mob_head);
1175 vmw_dmabuf_unreference(&res->backup);
1185 * vmw_fence_single_bo - Utility function to fence a single TTM buffer
1186 * object without unreserving it.
1188 * @bo: Pointer to the struct ttm_buffer_object to fence.
1189 * @fence: Pointer to the fence. If NULL, this function will
1190 * insert a fence into the command stream..
1192 * Contrary to the ttm_eu version of this function, it takes only
1193 * a single buffer object instead of a list, and it also doesn't
1194 * unreserve the buffer object, which needs to be done separately.
1196 void vmw_fence_single_bo(struct ttm_buffer_object *bo,
1197 struct vmw_fence_obj *fence)
1199 struct ttm_bo_device *bdev = bo->bdev;
1200 struct ttm_bo_driver *driver = bdev->driver;
1201 struct vmw_fence_obj *old_fence_obj;
1202 struct vmw_private *dev_priv =
1203 container_of(bdev, struct vmw_private, bdev);
1206 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1208 driver->sync_obj_ref(fence);
1210 spin_lock(&bdev->fence_lock);
1212 old_fence_obj = bo->sync_obj;
1213 bo->sync_obj = fence;
1215 spin_unlock(&bdev->fence_lock);
1218 vmw_fence_obj_unreference(&old_fence_obj);
1222 * vmw_resource_move_notify - TTM move_notify_callback
1224 * @bo: The TTM buffer object about to move.
1225 * @mem: The truct ttm_mem_reg indicating to what memory
1226 * region the move is taking place.
1228 * For now does nothing.
1230 void vmw_resource_move_notify(struct ttm_buffer_object *bo,
1231 struct ttm_mem_reg *mem)
1236 * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
1238 * @res: The resource being queried.
1240 bool vmw_resource_needs_backup(const struct vmw_resource *res)
1242 return res->func->needs_backup;
1246 * vmw_resource_evict_type - Evict all resources of a specific type
1248 * @dev_priv: Pointer to a device private struct
1249 * @type: The resource type to evict
1251 * To avoid thrashing starvation or as part of the hibernation sequence,
1252 * evict all evictable resources of a specific type.
1254 static void vmw_resource_evict_type(struct vmw_private *dev_priv,
1255 enum vmw_res_type type)
1257 struct list_head *lru_list = &dev_priv->res_lru[type];
1258 struct vmw_resource *evict_res;
1261 write_lock(&dev_priv->resource_lock);
1263 if (list_empty(lru_list))
1266 evict_res = vmw_resource_reference(
1267 list_first_entry(lru_list, struct vmw_resource,
1269 list_del_init(&evict_res->lru_head);
1270 write_unlock(&dev_priv->resource_lock);
1271 vmw_resource_do_evict(evict_res);
1272 vmw_resource_unreference(&evict_res);
1276 write_unlock(&dev_priv->resource_lock);
1280 * vmw_resource_evict_all - Evict all evictable resources
1282 * @dev_priv: Pointer to a device private struct
1284 * To avoid thrashing starvation or as part of the hibernation sequence,
1285 * evict all evictable resources. In particular this means that all
1286 * guest-backed resources that are registered with the device are
1287 * evicted and the OTable becomes clean.
1289 void vmw_resource_evict_all(struct vmw_private *dev_priv)
1291 enum vmw_res_type type;
1293 mutex_lock(&dev_priv->cmdbuf_mutex);
1295 for (type = 0; type < vmw_res_max; ++type)
1296 vmw_resource_evict_type(dev_priv, type);
1298 mutex_unlock(&dev_priv->cmdbuf_mutex);