1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright © 2018 - 2023 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
28 #include "vmwgfx_bo.h"
29 #include "vmwgfx_drv.h"
30 #include "vmwgfx_resource_priv.h"
31 #include "vmwgfx_validation.h"
33 #include <linux/slab.h>
36 #define VMWGFX_VALIDATION_MEM_GRAN (16*PAGE_SIZE)
39 * struct vmw_validation_bo_node - Buffer object validation metadata.
40 * @base: Metadata used for TTM reservation- and validation.
41 * @hash: A hash entry used for the duplicate detection hash table.
42 * @coherent_count: If switching backup buffers, number of new coherent
43 * resources that will have this buffer as a backup buffer.
45 * Bit fields are used since these structures are allocated and freed in
46 * large numbers and space conservation is desired.
48 struct vmw_validation_bo_node {
49 struct ttm_validate_buffer base;
50 struct vmwgfx_hash_item hash;
51 unsigned int coherent_count;
54 * struct vmw_validation_res_node - Resource validation metadata.
55 * @head: List head for the resource validation list.
56 * @hash: A hash entry used for the duplicate detection hash table.
57 * @res: Reference counted resource pointer.
58 * @new_guest_memory_bo: Non ref-counted pointer to new guest memory buffer
59 * to be assigned to a resource.
60 * @new_guest_memory_offset: Offset into the new backup mob for resources
61 * that can share MOBs.
62 * @no_buffer_needed: Kernel does not need to allocate a MOB during validation,
63 * the command stream provides a mob bind operation.
64 * @switching_guest_memory_bo: The validation process is switching backup MOB.
65 * @first_usage: True iff the resource has been seen only once in the current
67 * @reserved: Whether the resource is currently reserved by this process.
68 * @dirty_set: Change dirty status of the resource.
69 * @dirty: Dirty information VMW_RES_DIRTY_XX.
70 * @private: Optionally additional memory for caller-private data.
72 * Bit fields are used since these structures are allocated and freed in
73 * large numbers and space conservation is desired.
75 struct vmw_validation_res_node {
76 struct list_head head;
77 struct vmwgfx_hash_item hash;
78 struct vmw_resource *res;
79 struct vmw_bo *new_guest_memory_bo;
80 unsigned long new_guest_memory_offset;
81 u32 no_buffer_needed : 1;
82 u32 switching_guest_memory_bo : 1;
87 unsigned long private[];
91 * vmw_validation_mem_alloc - Allocate kernel memory from the validation
92 * context based allocator
93 * @ctx: The validation context
94 * @size: The number of bytes to allocated.
96 * The memory allocated may not exceed PAGE_SIZE, and the returned
97 * address is aligned to sizeof(long). All memory allocated this way is
98 * reclaimed after validation when calling any of the exported functions:
99 * vmw_validation_unref_lists()
100 * vmw_validation_revert()
101 * vmw_validation_done()
103 * Return: Pointer to the allocated memory on success. NULL on failure.
105 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
110 size = vmw_validation_align(size);
111 if (size > PAGE_SIZE)
114 if (ctx->mem_size_left < size) {
117 if (ctx->vm && ctx->vm_size_left < PAGE_SIZE) {
118 ctx->vm_size_left += VMWGFX_VALIDATION_MEM_GRAN;
119 ctx->total_mem += VMWGFX_VALIDATION_MEM_GRAN;
122 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
127 ctx->vm_size_left -= PAGE_SIZE;
129 list_add_tail(&page->lru, &ctx->page_list);
130 ctx->page_address = page_address(page);
131 ctx->mem_size_left = PAGE_SIZE;
134 addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left));
135 ctx->mem_size_left -= size;
141 * vmw_validation_mem_free - Free all memory allocated using
142 * vmw_validation_mem_alloc()
143 * @ctx: The validation context
145 * All memory previously allocated for this context using
146 * vmw_validation_mem_alloc() is freed.
148 static void vmw_validation_mem_free(struct vmw_validation_context *ctx)
150 struct page *entry, *next;
152 list_for_each_entry_safe(entry, next, &ctx->page_list, lru) {
153 list_del_init(&entry->lru);
157 ctx->mem_size_left = 0;
158 if (ctx->vm && ctx->total_mem) {
160 ctx->vm_size_left = 0;
165 * vmw_validation_find_bo_dup - Find a duplicate buffer object entry in the
166 * validation context's lists.
167 * @ctx: The validation context to search.
168 * @vbo: The buffer object to search for.
170 * Return: Pointer to the struct vmw_validation_bo_node referencing the
171 * duplicate, or NULL if none found.
173 static struct vmw_validation_bo_node *
174 vmw_validation_find_bo_dup(struct vmw_validation_context *ctx,
177 struct vmw_validation_bo_node *bo_node = NULL;
179 if (!ctx->merge_dups)
182 if (ctx->sw_context) {
183 struct vmwgfx_hash_item *hash;
184 unsigned long key = (unsigned long) vbo;
186 hash_for_each_possible_rcu(ctx->sw_context->res_ht, hash, head, key) {
187 if (hash->key == key) {
188 bo_node = container_of(hash, typeof(*bo_node), hash);
193 struct vmw_validation_bo_node *entry;
195 list_for_each_entry(entry, &ctx->bo_list, base.head) {
196 if (entry->base.bo == &vbo->tbo) {
207 * vmw_validation_find_res_dup - Find a duplicate resource entry in the
208 * validation context's lists.
209 * @ctx: The validation context to search.
210 * @res: Reference counted resource pointer.
212 * Return: Pointer to the struct vmw_validation_bo_node referencing the
213 * duplicate, or NULL if none found.
215 static struct vmw_validation_res_node *
216 vmw_validation_find_res_dup(struct vmw_validation_context *ctx,
217 struct vmw_resource *res)
219 struct vmw_validation_res_node *res_node = NULL;
221 if (!ctx->merge_dups)
224 if (ctx->sw_context) {
225 struct vmwgfx_hash_item *hash;
226 unsigned long key = (unsigned long) res;
228 hash_for_each_possible_rcu(ctx->sw_context->res_ht, hash, head, key) {
229 if (hash->key == key) {
230 res_node = container_of(hash, typeof(*res_node), hash);
235 struct vmw_validation_res_node *entry;
237 list_for_each_entry(entry, &ctx->resource_ctx_list, head) {
238 if (entry->res == res) {
244 list_for_each_entry(entry, &ctx->resource_list, head) {
245 if (entry->res == res) {
257 * vmw_validation_add_bo - Add a buffer object to the validation context.
258 * @ctx: The validation context.
259 * @vbo: The buffer object.
261 * Return: Zero on success, negative error code otherwise.
263 int vmw_validation_add_bo(struct vmw_validation_context *ctx,
266 struct vmw_validation_bo_node *bo_node;
268 bo_node = vmw_validation_find_bo_dup(ctx, vbo);
270 struct ttm_validate_buffer *val_buf;
272 bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node));
276 if (ctx->sw_context) {
277 bo_node->hash.key = (unsigned long) vbo;
278 hash_add_rcu(ctx->sw_context->res_ht, &bo_node->hash.head,
281 val_buf = &bo_node->base;
282 val_buf->bo = ttm_bo_get_unless_zero(&vbo->tbo);
285 val_buf->num_shared = 0;
286 list_add_tail(&val_buf->head, &ctx->bo_list);
293 * vmw_validation_add_resource - Add a resource to the validation context.
294 * @ctx: The validation context.
295 * @res: The resource.
296 * @priv_size: Size of private, additional metadata.
297 * @dirty: Whether to change dirty status.
298 * @p_node: Output pointer of additional metadata address.
299 * @first_usage: Whether this was the first time this resource was seen.
301 * Return: Zero on success, negative error code otherwise.
303 int vmw_validation_add_resource(struct vmw_validation_context *ctx,
304 struct vmw_resource *res,
310 struct vmw_validation_res_node *node;
312 node = vmw_validation_find_res_dup(ctx, res);
314 node->first_usage = 0;
318 node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
320 VMW_DEBUG_USER("Failed to allocate a resource validation entry.\n");
324 if (ctx->sw_context) {
325 node->hash.key = (unsigned long) res;
326 hash_add_rcu(ctx->sw_context->res_ht, &node->hash.head, node->hash.key);
328 node->res = vmw_resource_reference_unless_doomed(res);
332 node->first_usage = 1;
333 if (!res->dev_priv->has_mob) {
334 list_add_tail(&node->head, &ctx->resource_list);
336 switch (vmw_res_type(res)) {
337 case vmw_res_context:
338 case vmw_res_dx_context:
339 list_add(&node->head, &ctx->resource_ctx_list);
341 case vmw_res_cotable:
342 list_add_tail(&node->head, &ctx->resource_ctx_list);
345 list_add_tail(&node->head, &ctx->resource_list);
353 /* Overwriting previous information here is intentional! */
354 node->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
357 *first_usage = node->first_usage;
359 *p_node = &node->private;
365 * vmw_validation_res_set_dirty - Register a resource dirty set or clear during
367 * @ctx: The validation context.
368 * @val_private: The additional meta-data pointer returned when the
369 * resource was registered with the validation context. Used to identify
371 * @dirty: Dirty information VMW_RES_DIRTY_XX
373 void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
374 void *val_private, u32 dirty)
376 struct vmw_validation_res_node *val;
381 val = container_of(val_private, typeof(*val), private);
383 /* Overwriting previous information here is intentional! */
384 val->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
388 * vmw_validation_res_switch_backup - Register a backup MOB switch during
390 * @ctx: The validation context.
391 * @val_private: The additional meta-data pointer returned when the
392 * resource was registered with the validation context. Used to identify
394 * @vbo: The new backup buffer object MOB. This buffer object needs to have
395 * already been registered with the validation context.
396 * @guest_memory_offset: Offset into the new backup MOB.
398 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
401 unsigned long guest_memory_offset)
403 struct vmw_validation_res_node *val;
405 val = container_of(val_private, typeof(*val), private);
407 val->switching_guest_memory_bo = 1;
408 if (val->first_usage)
409 val->no_buffer_needed = 1;
411 val->new_guest_memory_bo = vbo;
412 val->new_guest_memory_offset = guest_memory_offset;
416 * vmw_validation_res_reserve - Reserve all resources registered with this
417 * validation context.
418 * @ctx: The validation context.
419 * @intr: Use interruptible waits when possible.
421 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
424 int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
427 struct vmw_validation_res_node *val;
430 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
432 list_for_each_entry(val, &ctx->resource_list, head) {
433 struct vmw_resource *res = val->res;
435 ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
440 if (res->guest_memory_bo) {
441 struct vmw_bo *vbo = res->guest_memory_bo;
443 vmw_bo_placement_set(vbo,
445 res->func->busy_domain);
446 ret = vmw_validation_add_bo(ctx, vbo);
451 if (val->switching_guest_memory_bo && val->new_guest_memory_bo &&
453 struct vmw_validation_bo_node *bo_node =
454 vmw_validation_find_bo_dup(ctx,
455 val->new_guest_memory_bo);
457 if (WARN_ON(!bo_node)) {
461 bo_node->coherent_count++;
468 vmw_validation_res_unreserve(ctx, true);
473 * vmw_validation_res_unreserve - Unreserve all reserved resources
474 * registered with this validation context.
475 * @ctx: The validation context.
476 * @backoff: Whether this is a backoff- of a commit-type operation. This
477 * is used to determine whether to switch backup MOBs or not.
479 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
482 struct vmw_validation_res_node *val;
484 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
486 list_for_each_entry(val, &ctx->resource_list, head) {
488 vmw_resource_unreserve(val->res,
493 list_for_each_entry(val, &ctx->resource_list, head) {
495 vmw_resource_unreserve(val->res,
498 val->switching_guest_memory_bo,
499 val->new_guest_memory_bo,
500 val->new_guest_memory_offset);
505 * vmw_validation_bo_validate_single - Validate a single buffer object.
506 * @bo: The TTM buffer object base.
507 * @interruptible: Whether to perform waits interruptible if possible.
509 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
512 static int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
515 struct vmw_bo *vbo = to_vmw_bo(&bo->base);
516 struct ttm_operation_ctx ctx = {
517 .interruptible = interruptible,
522 if (atomic_read(&vbo->cpu_writers))
525 if (vbo->tbo.pin_count > 0)
528 ret = ttm_bo_validate(bo, &vbo->placement, &ctx);
529 if (ret == 0 || ret == -ERESTARTSYS)
533 * If that failed, try again, this time evicting
536 ctx.allow_res_evict = true;
538 return ttm_bo_validate(bo, &vbo->placement, &ctx);
542 * vmw_validation_bo_validate - Validate all buffer objects registered with
543 * the validation context.
544 * @ctx: The validation context.
545 * @intr: Whether to perform waits interruptible if possible.
547 * Return: Zero on success, -ERESTARTSYS if interrupted,
548 * negative error code on failure.
550 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
552 struct vmw_validation_bo_node *entry;
555 list_for_each_entry(entry, &ctx->bo_list, base.head) {
556 struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
558 ret = vmw_validation_bo_validate_single(entry->base.bo, intr);
564 * Rather than having the resource code allocating the bo
565 * dirty tracker in resource_unreserve() where we can't fail,
566 * Do it here when validating the buffer object.
568 if (entry->coherent_count) {
569 unsigned int coherent_count = entry->coherent_count;
571 while (coherent_count) {
572 ret = vmw_bo_dirty_add(vbo);
578 entry->coherent_count -= coherent_count;
582 vmw_bo_dirty_scan(vbo);
588 * vmw_validation_res_validate - Validate all resources registered with the
589 * validation context.
590 * @ctx: The validation context.
591 * @intr: Whether to perform waits interruptible if possible.
593 * Before this function is called, all resource backup buffers must have
596 * Return: Zero on success, -ERESTARTSYS if interrupted,
597 * negative error code on failure.
599 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
601 struct vmw_validation_res_node *val;
604 list_for_each_entry(val, &ctx->resource_list, head) {
605 struct vmw_resource *res = val->res;
606 struct vmw_bo *backup = res->guest_memory_bo;
608 ret = vmw_resource_validate(res, intr, val->dirty_set &&
611 if (ret != -ERESTARTSYS)
612 DRM_ERROR("Failed to validate resource.\n");
616 /* Check if the resource switched backup buffer */
617 if (backup && res->guest_memory_bo && backup != res->guest_memory_bo) {
618 struct vmw_bo *vbo = res->guest_memory_bo;
620 vmw_bo_placement_set(vbo, res->func->domain,
621 res->func->busy_domain);
622 ret = vmw_validation_add_bo(ctx, vbo);
631 * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
632 * and unregister it from this validation context.
633 * @ctx: The validation context.
635 * The hash table used for duplicate finding is an expensive resource and
636 * may be protected by mutexes that may cause deadlocks during resource
637 * unreferencing if held. After resource- and buffer object registering,
638 * there is no longer any use for this hash table, so allow freeing it
639 * either to shorten any mutex locking time, or before resources- and
640 * buffer objects are freed during validation context cleanup.
642 void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
644 struct vmw_validation_bo_node *entry;
645 struct vmw_validation_res_node *val;
647 if (!ctx->sw_context)
650 list_for_each_entry(entry, &ctx->bo_list, base.head)
651 hash_del_rcu(&entry->hash.head);
653 list_for_each_entry(val, &ctx->resource_list, head)
654 hash_del_rcu(&val->hash.head);
656 list_for_each_entry(val, &ctx->resource_ctx_list, head)
657 hash_del_rcu(&entry->hash.head);
659 ctx->sw_context = NULL;
663 * vmw_validation_unref_lists - Unregister previously registered buffer
664 * object and resources.
665 * @ctx: The validation context.
667 * Note that this function may cause buffer object- and resource destructors
670 void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
672 struct vmw_validation_bo_node *entry;
673 struct vmw_validation_res_node *val;
675 list_for_each_entry(entry, &ctx->bo_list, base.head) {
676 ttm_bo_put(entry->base.bo);
677 entry->base.bo = NULL;
680 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
681 list_for_each_entry(val, &ctx->resource_list, head)
682 vmw_resource_unreference(&val->res);
685 * No need to detach each list entry since they are all freed with
686 * vmw_validation_free_mem. Just make the inaccessible.
688 INIT_LIST_HEAD(&ctx->bo_list);
689 INIT_LIST_HEAD(&ctx->resource_list);
691 vmw_validation_mem_free(ctx);
695 * vmw_validation_prepare - Prepare a validation context for command
697 * @ctx: The validation context.
698 * @mutex: The mutex used to protect resource reservation.
699 * @intr: Whether to perform waits interruptible if possible.
701 * Note that the single reservation mutex @mutex is an unfortunate
702 * construct. Ideally resource reservation should be moved to per-resource
704 * If this functions doesn't return Zero to indicate success, all resources
705 * are left unreserved but still referenced.
706 * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
709 int vmw_validation_prepare(struct vmw_validation_context *ctx,
717 ret = mutex_lock_interruptible(mutex);
724 ctx->res_mutex = mutex;
725 ret = vmw_validation_res_reserve(ctx, intr);
727 goto out_no_res_reserve;
729 ret = vmw_validation_bo_reserve(ctx, intr);
731 goto out_no_bo_reserve;
733 ret = vmw_validation_bo_validate(ctx, intr);
735 goto out_no_validate;
737 ret = vmw_validation_res_validate(ctx, intr);
739 goto out_no_validate;
744 vmw_validation_bo_backoff(ctx);
746 vmw_validation_res_unreserve(ctx, true);
755 * vmw_validation_revert - Revert validation actions if command submission
758 * @ctx: The validation context.
760 * The caller still needs to unref resources after a call to this function.
762 void vmw_validation_revert(struct vmw_validation_context *ctx)
764 vmw_validation_bo_backoff(ctx);
765 vmw_validation_res_unreserve(ctx, true);
767 mutex_unlock(ctx->res_mutex);
768 vmw_validation_unref_lists(ctx);
772 * vmw_validation_done - Commit validation actions after command submission
774 * @ctx: The validation context.
775 * @fence: Fence with which to fence all buffer objects taking part in the
776 * command submission.
778 * The caller does NOT need to unref resources after a call to this function.
780 void vmw_validation_done(struct vmw_validation_context *ctx,
781 struct vmw_fence_obj *fence)
783 vmw_validation_bo_fence(ctx, fence);
784 vmw_validation_res_unreserve(ctx, false);
786 mutex_unlock(ctx->res_mutex);
787 vmw_validation_unref_lists(ctx);
791 * vmw_validation_preload_bo - Preload the validation memory allocator for a
792 * call to vmw_validation_add_bo().
793 * @ctx: Pointer to the validation context.
795 * Iff this function returns successfully, the next call to
796 * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
797 * but voids the guarantee.
799 * Returns: Zero if successful, %-EINVAL otherwise.
801 int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
803 unsigned int size = sizeof(struct vmw_validation_bo_node);
805 if (!vmw_validation_mem_alloc(ctx, size))
808 ctx->mem_size_left += size;
813 * vmw_validation_preload_res - Preload the validation memory allocator for a
814 * call to vmw_validation_add_res().
815 * @ctx: Pointer to the validation context.
816 * @size: Size of the validation node extra data. See below.
818 * Iff this function returns successfully, the next call to
819 * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
820 * sleep. An error is not fatal but voids the guarantee.
822 * Returns: Zero if successful, %-EINVAL otherwise.
824 int vmw_validation_preload_res(struct vmw_validation_context *ctx,
827 size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
829 vmw_validation_align(sizeof(struct vmw_validation_bo_node));
830 if (!vmw_validation_mem_alloc(ctx, size))
833 ctx->mem_size_left += size;
838 * vmw_validation_bo_backoff - Unreserve buffer objects registered with a
840 * @ctx: The validation context
842 * This function unreserves the buffer objects previously reserved using
843 * vmw_validation_bo_reserve. It's typically used as part of an error path
845 void vmw_validation_bo_backoff(struct vmw_validation_context *ctx)
847 struct vmw_validation_bo_node *entry;
850 * Switching coherent resource backup buffers failed.
851 * Release corresponding buffer object dirty trackers.
853 list_for_each_entry(entry, &ctx->bo_list, base.head) {
854 if (entry->coherent_count) {
855 unsigned int coherent_count = entry->coherent_count;
856 struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
858 while (coherent_count--)
859 vmw_bo_dirty_release(vbo);
863 ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list);