1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2015 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 <drm/ttm/ttm_placement.h>
30 #include "vmwgfx_resource_priv.h"
31 #include "vmwgfx_binding.h"
32 #include "vmwgfx_drv.h"
34 #define VMW_RES_EVICT_ERR_COUNT 10
37 * vmw_resource_mob_attach - Mark a resource as attached to its backing mob
40 void vmw_resource_mob_attach(struct vmw_resource *res)
42 struct vmw_buffer_object *backup = res->backup;
43 struct rb_node **new = &backup->res_tree.rb_node, *parent = NULL;
45 dma_resv_assert_held(res->backup->base.base.resv);
46 res->used_prio = (res->res_dirty) ? res->func->dirty_prio :
50 struct vmw_resource *this =
51 container_of(*new, struct vmw_resource, mob_node);
54 new = (res->backup_offset < this->backup_offset) ?
55 &((*new)->rb_left) : &((*new)->rb_right);
58 rb_link_node(&res->mob_node, parent, new);
59 rb_insert_color(&res->mob_node, &backup->res_tree);
61 vmw_bo_prio_add(backup, res->used_prio);
65 * vmw_resource_mob_detach - Mark a resource as detached from its backing mob
68 void vmw_resource_mob_detach(struct vmw_resource *res)
70 struct vmw_buffer_object *backup = res->backup;
72 dma_resv_assert_held(backup->base.base.resv);
73 if (vmw_resource_mob_attached(res)) {
74 rb_erase(&res->mob_node, &backup->res_tree);
75 RB_CLEAR_NODE(&res->mob_node);
76 vmw_bo_prio_del(backup, res->used_prio);
80 struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
87 vmw_resource_reference_unless_doomed(struct vmw_resource *res)
89 return kref_get_unless_zero(&res->kref) ? res : NULL;
93 * vmw_resource_release_id - release a resource id to the id manager.
95 * @res: Pointer to the resource.
97 * Release the resource id to the resource id manager and set it to -1
99 void vmw_resource_release_id(struct vmw_resource *res)
101 struct vmw_private *dev_priv = res->dev_priv;
102 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
104 spin_lock(&dev_priv->resource_lock);
106 idr_remove(idr, res->id);
108 spin_unlock(&dev_priv->resource_lock);
111 static void vmw_resource_release(struct kref *kref)
113 struct vmw_resource *res =
114 container_of(kref, struct vmw_resource, kref);
115 struct vmw_private *dev_priv = res->dev_priv;
118 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
120 spin_lock(&dev_priv->resource_lock);
121 list_del_init(&res->lru_head);
122 spin_unlock(&dev_priv->resource_lock);
124 struct ttm_buffer_object *bo = &res->backup->base;
126 ret = ttm_bo_reserve(bo, false, false, NULL);
128 if (vmw_resource_mob_attached(res) &&
129 res->func->unbind != NULL) {
130 struct ttm_validate_buffer val_buf;
133 val_buf.num_shared = 0;
134 res->func->unbind(res, false, &val_buf);
136 res->backup_dirty = false;
137 vmw_resource_mob_detach(res);
139 res->func->dirty_free(res);
141 vmw_bo_dirty_release(res->backup);
142 ttm_bo_unreserve(bo);
143 vmw_bo_unreference(&res->backup);
146 if (likely(res->hw_destroy != NULL)) {
147 mutex_lock(&dev_priv->binding_mutex);
148 vmw_binding_res_list_kill(&res->binding_head);
149 mutex_unlock(&dev_priv->binding_mutex);
150 res->hw_destroy(res);
154 if (res->res_free != NULL)
159 spin_lock(&dev_priv->resource_lock);
162 spin_unlock(&dev_priv->resource_lock);
165 void vmw_resource_unreference(struct vmw_resource **p_res)
167 struct vmw_resource *res = *p_res;
170 kref_put(&res->kref, vmw_resource_release);
175 * vmw_resource_alloc_id - release a resource id to the id manager.
177 * @res: Pointer to the resource.
179 * Allocate the lowest free resource from the resource manager, and set
180 * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
182 int vmw_resource_alloc_id(struct vmw_resource *res)
184 struct vmw_private *dev_priv = res->dev_priv;
186 struct idr *idr = &dev_priv->res_idr[res->func->res_type];
188 BUG_ON(res->id != -1);
190 idr_preload(GFP_KERNEL);
191 spin_lock(&dev_priv->resource_lock);
193 ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
197 spin_unlock(&dev_priv->resource_lock);
199 return ret < 0 ? ret : 0;
203 * vmw_resource_init - initialize a struct vmw_resource
205 * @dev_priv: Pointer to a device private struct.
206 * @res: The struct vmw_resource to initialize.
207 * @delay_id: Boolean whether to defer device id allocation until
208 * the first validation.
209 * @res_free: Resource destructor.
210 * @func: Resource function table.
212 int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
214 void (*res_free) (struct vmw_resource *res),
215 const struct vmw_res_func *func)
217 kref_init(&res->kref);
218 res->hw_destroy = NULL;
219 res->res_free = res_free;
220 res->dev_priv = dev_priv;
222 RB_CLEAR_NODE(&res->mob_node);
223 INIT_LIST_HEAD(&res->lru_head);
224 INIT_LIST_HEAD(&res->binding_head);
227 res->backup_offset = 0;
228 res->backup_dirty = false;
229 res->res_dirty = false;
230 res->coherent = false;
236 return vmw_resource_alloc_id(res);
241 * vmw_user_resource_lookup_handle - lookup a struct resource from a
242 * TTM user-space handle and perform basic type checks
244 * @dev_priv: Pointer to a device private struct
245 * @tfile: Pointer to a struct ttm_object_file identifying the caller
246 * @handle: The TTM user-space handle
247 * @converter: Pointer to an object describing the resource type
248 * @p_res: On successful return the location pointed to will contain
249 * a pointer to a refcounted struct vmw_resource.
251 * If the handle can't be found or is associated with an incorrect resource
252 * type, -EINVAL will be returned.
254 int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
255 struct ttm_object_file *tfile,
257 const struct vmw_user_resource_conv
259 struct vmw_resource **p_res)
261 struct ttm_base_object *base;
262 struct vmw_resource *res;
265 base = ttm_base_object_lookup(tfile, handle);
266 if (unlikely(base == NULL))
269 if (unlikely(ttm_base_object_type(base) != converter->object_type))
270 goto out_bad_resource;
272 res = converter->base_obj_to_res(base);
273 kref_get(&res->kref);
279 ttm_base_object_unref(&base);
285 * vmw_user_resource_noref_lookup_handle - lookup a struct resource from a
286 * TTM user-space handle and perform basic type checks
288 * @dev_priv: Pointer to a device private struct
289 * @tfile: Pointer to a struct ttm_object_file identifying the caller
290 * @handle: The TTM user-space handle
291 * @converter: Pointer to an object describing the resource type
293 * If the handle can't be found or is associated with an incorrect resource
294 * type, -EINVAL will be returned.
296 struct vmw_resource *
297 vmw_user_resource_noref_lookup_handle(struct vmw_private *dev_priv,
298 struct ttm_object_file *tfile,
300 const struct vmw_user_resource_conv
303 struct ttm_base_object *base;
305 base = ttm_base_object_noref_lookup(tfile, handle);
307 return ERR_PTR(-ESRCH);
309 if (unlikely(ttm_base_object_type(base) != converter->object_type)) {
310 ttm_base_object_noref_release();
311 return ERR_PTR(-EINVAL);
314 return converter->base_obj_to_res(base);
318 * Helper function that looks either a surface or bo.
320 * The pointer this pointed at by out_surf and out_buf needs to be null.
322 int vmw_user_lookup_handle(struct vmw_private *dev_priv,
323 struct ttm_object_file *tfile,
325 struct vmw_surface **out_surf,
326 struct vmw_buffer_object **out_buf)
328 struct vmw_resource *res;
331 BUG_ON(*out_surf || *out_buf);
333 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
334 user_surface_converter,
337 *out_surf = vmw_res_to_srf(res);
342 ret = vmw_user_bo_lookup(tfile, handle, out_buf, NULL);
347 * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
349 * @res: The resource for which to allocate a backup buffer.
350 * @interruptible: Whether any sleeps during allocation should be
351 * performed while interruptible.
353 static int vmw_resource_buf_alloc(struct vmw_resource *res,
356 unsigned long size = PFN_ALIGN(res->backup_size);
357 struct vmw_buffer_object *backup;
360 if (likely(res->backup)) {
361 BUG_ON(res->backup->base.base.size < size);
365 backup = kzalloc(sizeof(*backup), GFP_KERNEL);
366 if (unlikely(!backup))
369 ret = vmw_bo_init(res->dev_priv, backup, res->backup_size,
370 res->func->backup_placement,
371 interruptible, false,
373 if (unlikely(ret != 0))
376 res->backup = backup;
383 * vmw_resource_do_validate - Make a resource up-to-date and visible
386 * @res: The resource to make visible to the device.
387 * @val_buf: Information about a buffer possibly
388 * containing backup data if a bind operation is needed.
389 * @dirtying: Transfer dirty regions.
391 * On hardware resource shortage, this function returns -EBUSY and
392 * should be retried once resources have been freed up.
394 static int vmw_resource_do_validate(struct vmw_resource *res,
395 struct ttm_validate_buffer *val_buf,
399 const struct vmw_res_func *func = res->func;
401 if (unlikely(res->id == -1)) {
402 ret = func->create(res);
403 if (unlikely(ret != 0))
408 ((func->needs_backup && !vmw_resource_mob_attached(res) &&
409 val_buf->bo != NULL) ||
410 (!func->needs_backup && val_buf->bo != NULL))) {
411 ret = func->bind(res, val_buf);
412 if (unlikely(ret != 0))
413 goto out_bind_failed;
414 if (func->needs_backup)
415 vmw_resource_mob_attach(res);
419 * Handle the case where the backup mob is marked coherent but
420 * the resource isn't.
422 if (func->dirty_alloc && vmw_resource_mob_attached(res) &&
424 if (res->backup->dirty && !res->dirty) {
425 ret = func->dirty_alloc(res);
428 } else if (!res->backup->dirty && res->dirty) {
429 func->dirty_free(res);
434 * Transfer the dirty regions to the resource and update
438 if (dirtying && !res->res_dirty) {
439 pgoff_t start = res->backup_offset >> PAGE_SHIFT;
440 pgoff_t end = __KERNEL_DIV_ROUND_UP
441 (res->backup_offset + res->backup_size,
444 vmw_bo_dirty_unmap(res->backup, start, end);
447 vmw_bo_dirty_transfer_to_res(res);
448 return func->dirty_sync(res);
460 * vmw_resource_unreserve - Unreserve a resource previously reserved for
461 * command submission.
463 * @res: Pointer to the struct vmw_resource to unreserve.
464 * @dirty_set: Change dirty status of the resource.
465 * @dirty: When changing dirty status indicates the new status.
466 * @switch_backup: Backup buffer has been switched.
467 * @new_backup: Pointer to new backup buffer if command submission
468 * switched. May be NULL.
469 * @new_backup_offset: New backup offset if @switch_backup is true.
471 * Currently unreserving a resource means putting it back on the device's
472 * resource lru list, so that it can be evicted if necessary.
474 void vmw_resource_unreserve(struct vmw_resource *res,
478 struct vmw_buffer_object *new_backup,
479 unsigned long new_backup_offset)
481 struct vmw_private *dev_priv = res->dev_priv;
483 if (!list_empty(&res->lru_head))
486 if (switch_backup && new_backup != res->backup) {
488 vmw_resource_mob_detach(res);
490 vmw_bo_dirty_release(res->backup);
491 vmw_bo_unreference(&res->backup);
495 res->backup = vmw_bo_reference(new_backup);
498 * The validation code should already have added a
499 * dirty tracker here.
501 WARN_ON(res->coherent && !new_backup->dirty);
503 vmw_resource_mob_attach(res);
507 } else if (switch_backup && res->coherent) {
508 vmw_bo_dirty_release(res->backup);
512 res->backup_offset = new_backup_offset;
515 res->res_dirty = dirty;
517 if (!res->func->may_evict || res->id == -1 || res->pin_count)
520 spin_lock(&dev_priv->resource_lock);
521 list_add_tail(&res->lru_head,
522 &res->dev_priv->res_lru[res->func->res_type]);
523 spin_unlock(&dev_priv->resource_lock);
527 * vmw_resource_check_buffer - Check whether a backup buffer is needed
528 * for a resource and in that case, allocate
529 * one, reserve and validate it.
531 * @ticket: The ww aqcquire context to use, or NULL if trylocking.
532 * @res: The resource for which to allocate a backup buffer.
533 * @interruptible: Whether any sleeps during allocation should be
534 * performed while interruptible.
535 * @val_buf: On successful return contains data about the
536 * reserved and validated backup buffer.
539 vmw_resource_check_buffer(struct ww_acquire_ctx *ticket,
540 struct vmw_resource *res,
542 struct ttm_validate_buffer *val_buf)
544 struct ttm_operation_ctx ctx = { true, false };
545 struct list_head val_list;
546 bool backup_dirty = false;
549 if (unlikely(res->backup == NULL)) {
550 ret = vmw_resource_buf_alloc(res, interruptible);
551 if (unlikely(ret != 0))
555 INIT_LIST_HEAD(&val_list);
556 ttm_bo_get(&res->backup->base);
557 val_buf->bo = &res->backup->base;
558 val_buf->num_shared = 0;
559 list_add_tail(&val_buf->head, &val_list);
560 ret = ttm_eu_reserve_buffers(ticket, &val_list, interruptible, NULL);
561 if (unlikely(ret != 0))
564 if (res->func->needs_backup && !vmw_resource_mob_attached(res))
567 backup_dirty = res->backup_dirty;
568 ret = ttm_bo_validate(&res->backup->base,
569 res->func->backup_placement,
572 if (unlikely(ret != 0))
573 goto out_no_validate;
578 ttm_eu_backoff_reservation(ticket, &val_list);
580 ttm_bo_put(val_buf->bo);
583 vmw_bo_unreference(&res->backup);
589 * vmw_resource_reserve - Reserve a resource for command submission
591 * @res: The resource to reserve.
593 * This function takes the resource off the LRU list and make sure
594 * a backup buffer is present for guest-backed resources. However,
595 * the buffer may not be bound to the resource at this point.
598 int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
601 struct vmw_private *dev_priv = res->dev_priv;
604 spin_lock(&dev_priv->resource_lock);
605 list_del_init(&res->lru_head);
606 spin_unlock(&dev_priv->resource_lock);
608 if (res->func->needs_backup && res->backup == NULL &&
610 ret = vmw_resource_buf_alloc(res, interruptible);
611 if (unlikely(ret != 0)) {
612 DRM_ERROR("Failed to allocate a backup buffer "
613 "of size %lu. bytes\n",
614 (unsigned long) res->backup_size);
623 * vmw_resource_backoff_reservation - Unreserve and unreference a
626 * @ticket: The ww acquire ctx used for reservation.
627 * @val_buf: Backup buffer information.
630 vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
631 struct ttm_validate_buffer *val_buf)
633 struct list_head val_list;
635 if (likely(val_buf->bo == NULL))
638 INIT_LIST_HEAD(&val_list);
639 list_add_tail(&val_buf->head, &val_list);
640 ttm_eu_backoff_reservation(ticket, &val_list);
641 ttm_bo_put(val_buf->bo);
646 * vmw_resource_do_evict - Evict a resource, and transfer its data
647 * to a backup buffer.
649 * @ticket: The ww acquire ticket to use, or NULL if trylocking.
650 * @res: The resource to evict.
651 * @interruptible: Whether to wait interruptible.
653 static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket,
654 struct vmw_resource *res, bool interruptible)
656 struct ttm_validate_buffer val_buf;
657 const struct vmw_res_func *func = res->func;
660 BUG_ON(!func->may_evict);
663 val_buf.num_shared = 0;
664 ret = vmw_resource_check_buffer(ticket, res, interruptible, &val_buf);
665 if (unlikely(ret != 0))
668 if (unlikely(func->unbind != NULL &&
669 (!func->needs_backup || vmw_resource_mob_attached(res)))) {
670 ret = func->unbind(res, res->res_dirty, &val_buf);
671 if (unlikely(ret != 0))
673 vmw_resource_mob_detach(res);
675 ret = func->destroy(res);
676 res->backup_dirty = true;
677 res->res_dirty = false;
679 vmw_resource_backoff_reservation(ticket, &val_buf);
686 * vmw_resource_validate - Make a resource up-to-date and visible
688 * @res: The resource to make visible to the device.
689 * @intr: Perform waits interruptible if possible.
690 * @dirtying: Pending GPU operation will dirty the resource
692 * On succesful return, any backup DMA buffer pointed to by @res->backup will
693 * be reserved and validated.
694 * On hardware resource shortage, this function will repeatedly evict
695 * resources of the same type until the validation succeeds.
697 * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
700 int vmw_resource_validate(struct vmw_resource *res, bool intr,
704 struct vmw_resource *evict_res;
705 struct vmw_private *dev_priv = res->dev_priv;
706 struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
707 struct ttm_validate_buffer val_buf;
708 unsigned err_count = 0;
710 if (!res->func->create)
714 val_buf.num_shared = 0;
716 val_buf.bo = &res->backup->base;
718 ret = vmw_resource_do_validate(res, &val_buf, dirtying);
719 if (likely(ret != -EBUSY))
722 spin_lock(&dev_priv->resource_lock);
723 if (list_empty(lru_list) || !res->func->may_evict) {
724 DRM_ERROR("Out of device device resources "
725 "for %s.\n", res->func->type_name);
727 spin_unlock(&dev_priv->resource_lock);
731 evict_res = vmw_resource_reference
732 (list_first_entry(lru_list, struct vmw_resource,
734 list_del_init(&evict_res->lru_head);
736 spin_unlock(&dev_priv->resource_lock);
738 /* Trylock backup buffers with a NULL ticket. */
739 ret = vmw_resource_do_evict(NULL, evict_res, intr);
740 if (unlikely(ret != 0)) {
741 spin_lock(&dev_priv->resource_lock);
742 list_add_tail(&evict_res->lru_head, lru_list);
743 spin_unlock(&dev_priv->resource_lock);
744 if (ret == -ERESTARTSYS ||
745 ++err_count > VMW_RES_EVICT_ERR_COUNT) {
746 vmw_resource_unreference(&evict_res);
747 goto out_no_validate;
751 vmw_resource_unreference(&evict_res);
754 if (unlikely(ret != 0))
755 goto out_no_validate;
756 else if (!res->func->needs_backup && res->backup) {
757 WARN_ON_ONCE(vmw_resource_mob_attached(res));
758 vmw_bo_unreference(&res->backup);
769 * vmw_resource_unbind_list
771 * @vbo: Pointer to the current backing MOB.
773 * Evicts the Guest Backed hardware resource if the backup
774 * buffer is being moved out of MOB memory.
775 * Note that this function will not race with the resource
776 * validation code, since resource validation and eviction
777 * both require the backup buffer to be reserved.
779 void vmw_resource_unbind_list(struct vmw_buffer_object *vbo)
781 struct ttm_validate_buffer val_buf = {
786 dma_resv_assert_held(vbo->base.base.resv);
787 while (!RB_EMPTY_ROOT(&vbo->res_tree)) {
788 struct rb_node *node = vbo->res_tree.rb_node;
789 struct vmw_resource *res =
790 container_of(node, struct vmw_resource, mob_node);
792 if (!WARN_ON_ONCE(!res->func->unbind))
793 (void) res->func->unbind(res, res->res_dirty, &val_buf);
795 res->backup_dirty = true;
796 res->res_dirty = false;
797 vmw_resource_mob_detach(res);
800 (void) ttm_bo_wait(&vbo->base, false, false);
805 * vmw_query_readback_all - Read back cached query states
807 * @dx_query_mob: Buffer containing the DX query MOB
809 * Read back cached states from the device if they exist. This function
810 * assumings binding_mutex is held.
812 int vmw_query_readback_all(struct vmw_buffer_object *dx_query_mob)
814 struct vmw_resource *dx_query_ctx;
815 struct vmw_private *dev_priv;
817 SVGA3dCmdHeader header;
818 SVGA3dCmdDXReadbackAllQuery body;
822 /* No query bound, so do nothing */
823 if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
826 dx_query_ctx = dx_query_mob->dx_query_ctx;
827 dev_priv = dx_query_ctx->dev_priv;
829 cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), dx_query_ctx->id);
830 if (unlikely(cmd == NULL))
833 cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
834 cmd->header.size = sizeof(cmd->body);
835 cmd->body.cid = dx_query_ctx->id;
837 vmw_cmd_commit(dev_priv, sizeof(*cmd));
839 /* Triggers a rebind the next time affected context is bound */
840 dx_query_mob->dx_query_ctx = NULL;
848 * vmw_query_move_notify - Read back cached query states
850 * @bo: The TTM buffer object about to move.
851 * @old_mem: The memory region @bo is moving from.
852 * @new_mem: The memory region @bo is moving to.
854 * Called before the query MOB is swapped out to read back cached query
855 * states from the device.
857 void vmw_query_move_notify(struct ttm_buffer_object *bo,
858 struct ttm_resource *old_mem,
859 struct ttm_resource *new_mem)
861 struct vmw_buffer_object *dx_query_mob;
862 struct ttm_device *bdev = bo->bdev;
863 struct vmw_private *dev_priv;
866 dev_priv = container_of(bdev, struct vmw_private, bdev);
868 mutex_lock(&dev_priv->binding_mutex);
870 dx_query_mob = container_of(bo, struct vmw_buffer_object, base);
871 if (!dx_query_mob || !dx_query_mob->dx_query_ctx) {
872 mutex_unlock(&dev_priv->binding_mutex);
876 /* If BO is being moved from MOB to system memory */
877 if (new_mem->mem_type == TTM_PL_SYSTEM &&
878 old_mem->mem_type == VMW_PL_MOB) {
879 struct vmw_fence_obj *fence;
881 (void) vmw_query_readback_all(dx_query_mob);
882 mutex_unlock(&dev_priv->binding_mutex);
884 /* Create a fence and attach the BO to it */
885 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
886 vmw_bo_fence_single(bo, fence);
889 vmw_fence_obj_unreference(&fence);
891 (void) ttm_bo_wait(bo, false, false);
893 mutex_unlock(&dev_priv->binding_mutex);
898 * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
900 * @res: The resource being queried.
902 bool vmw_resource_needs_backup(const struct vmw_resource *res)
904 return res->func->needs_backup;
908 * vmw_resource_evict_type - Evict all resources of a specific type
910 * @dev_priv: Pointer to a device private struct
911 * @type: The resource type to evict
913 * To avoid thrashing starvation or as part of the hibernation sequence,
914 * try to evict all evictable resources of a specific type.
916 static void vmw_resource_evict_type(struct vmw_private *dev_priv,
917 enum vmw_res_type type)
919 struct list_head *lru_list = &dev_priv->res_lru[type];
920 struct vmw_resource *evict_res;
921 unsigned err_count = 0;
923 struct ww_acquire_ctx ticket;
926 spin_lock(&dev_priv->resource_lock);
928 if (list_empty(lru_list))
931 evict_res = vmw_resource_reference(
932 list_first_entry(lru_list, struct vmw_resource,
934 list_del_init(&evict_res->lru_head);
935 spin_unlock(&dev_priv->resource_lock);
937 /* Wait lock backup buffers with a ticket. */
938 ret = vmw_resource_do_evict(&ticket, evict_res, false);
939 if (unlikely(ret != 0)) {
940 spin_lock(&dev_priv->resource_lock);
941 list_add_tail(&evict_res->lru_head, lru_list);
942 spin_unlock(&dev_priv->resource_lock);
943 if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
944 vmw_resource_unreference(&evict_res);
949 vmw_resource_unreference(&evict_res);
953 spin_unlock(&dev_priv->resource_lock);
957 * vmw_resource_evict_all - Evict all evictable resources
959 * @dev_priv: Pointer to a device private struct
961 * To avoid thrashing starvation or as part of the hibernation sequence,
962 * evict all evictable resources. In particular this means that all
963 * guest-backed resources that are registered with the device are
964 * evicted and the OTable becomes clean.
966 void vmw_resource_evict_all(struct vmw_private *dev_priv)
968 enum vmw_res_type type;
970 mutex_lock(&dev_priv->cmdbuf_mutex);
972 for (type = 0; type < vmw_res_max; ++type)
973 vmw_resource_evict_type(dev_priv, type);
975 mutex_unlock(&dev_priv->cmdbuf_mutex);
979 * vmw_resource_pin - Add a pin reference on a resource
981 * @res: The resource to add a pin reference on
983 * This function adds a pin reference, and if needed validates the resource.
984 * Having a pin reference means that the resource can never be evicted, and
985 * its id will never change as long as there is a pin reference.
986 * This function returns 0 on success and a negative error code on failure.
988 int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
990 struct ttm_operation_ctx ctx = { interruptible, false };
991 struct vmw_private *dev_priv = res->dev_priv;
994 mutex_lock(&dev_priv->cmdbuf_mutex);
995 ret = vmw_resource_reserve(res, interruptible, false);
999 if (res->pin_count == 0) {
1000 struct vmw_buffer_object *vbo = NULL;
1005 ret = ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
1007 goto out_no_validate;
1008 if (!vbo->base.pin_count) {
1009 ret = ttm_bo_validate
1011 res->func->backup_placement,
1014 ttm_bo_unreserve(&vbo->base);
1015 goto out_no_validate;
1019 /* Do we really need to pin the MOB as well? */
1020 vmw_bo_pin_reserved(vbo, true);
1022 ret = vmw_resource_validate(res, interruptible, true);
1024 ttm_bo_unreserve(&vbo->base);
1026 goto out_no_validate;
1031 vmw_resource_unreserve(res, false, false, false, NULL, 0UL);
1033 mutex_unlock(&dev_priv->cmdbuf_mutex);
1039 * vmw_resource_unpin - Remove a pin reference from a resource
1041 * @res: The resource to remove a pin reference from
1043 * Having a pin reference means that the resource can never be evicted, and
1044 * its id will never change as long as there is a pin reference.
1046 void vmw_resource_unpin(struct vmw_resource *res)
1048 struct vmw_private *dev_priv = res->dev_priv;
1051 mutex_lock(&dev_priv->cmdbuf_mutex);
1053 ret = vmw_resource_reserve(res, false, true);
1056 WARN_ON(res->pin_count == 0);
1057 if (--res->pin_count == 0 && res->backup) {
1058 struct vmw_buffer_object *vbo = res->backup;
1060 (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
1061 vmw_bo_pin_reserved(vbo, false);
1062 ttm_bo_unreserve(&vbo->base);
1065 vmw_resource_unreserve(res, false, false, false, NULL, 0UL);
1067 mutex_unlock(&dev_priv->cmdbuf_mutex);
1071 * vmw_res_type - Return the resource type
1073 * @res: Pointer to the resource
1075 enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
1077 return res->func->res_type;
1081 * vmw_resource_dirty_update - Update a resource's dirty tracker with a
1082 * sequential range of touched backing store memory.
1083 * @res: The resource.
1084 * @start: The first page touched.
1085 * @end: The last page touched + 1.
1087 void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start,
1091 res->func->dirty_range_add(res, start << PAGE_SHIFT,
1096 * vmw_resources_clean - Clean resources intersecting a mob range
1097 * @vbo: The mob buffer object
1098 * @start: The mob page offset starting the range
1099 * @end: The mob page offset ending the range
1100 * @num_prefault: Returns how many pages including the first have been
1101 * cleaned and are ok to prefault
1103 int vmw_resources_clean(struct vmw_buffer_object *vbo, pgoff_t start,
1104 pgoff_t end, pgoff_t *num_prefault)
1106 struct rb_node *cur = vbo->res_tree.rb_node;
1107 struct vmw_resource *found = NULL;
1108 unsigned long res_start = start << PAGE_SHIFT;
1109 unsigned long res_end = end << PAGE_SHIFT;
1110 unsigned long last_cleaned = 0;
1113 * Find the resource with lowest backup_offset that intersects the
1117 struct vmw_resource *cur_res =
1118 container_of(cur, struct vmw_resource, mob_node);
1120 if (cur_res->backup_offset >= res_end) {
1122 } else if (cur_res->backup_offset + cur_res->backup_size <=
1124 cur = cur->rb_right;
1128 /* Continue to look for resources with lower offsets */
1133 * In order of increasing backup_offset, clean dirty resorces
1134 * intersecting the range.
1137 if (found->res_dirty) {
1140 if (!found->func->clean)
1143 ret = found->func->clean(found);
1147 found->res_dirty = false;
1149 last_cleaned = found->backup_offset + found->backup_size;
1150 cur = rb_next(&found->mob_node);
1154 found = container_of(cur, struct vmw_resource, mob_node);
1155 if (found->backup_offset >= res_end)
1160 * Set number of pages allowed prefaulting and fence the buffer object
1163 if (last_cleaned > res_start) {
1164 struct ttm_buffer_object *bo = &vbo->base;
1166 *num_prefault = __KERNEL_DIV_ROUND_UP(last_cleaned - res_start,
1168 vmw_bo_fence_single(bo, NULL);
1170 dma_fence_put(bo->moving);
1171 bo->moving = dma_fence_get
1172 (dma_resv_excl_fence(bo->base.resv));