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 * struct vmw_validation_bo_node - Buffer object validation metadata.
37 * @base: Metadata used for TTM reservation- and validation.
38 * @hash: A hash entry used for the duplicate detection hash table.
39 * @coherent_count: If switching backup buffers, number of new coherent
40 * resources that will have this buffer as a backup buffer.
42 * Bit fields are used since these structures are allocated and freed in
43 * large numbers and space conservation is desired.
45 struct vmw_validation_bo_node {
46 struct ttm_validate_buffer base;
47 struct vmwgfx_hash_item hash;
48 unsigned int coherent_count;
51 * struct vmw_validation_res_node - Resource validation metadata.
52 * @head: List head for the resource validation list.
53 * @hash: A hash entry used for the duplicate detection hash table.
54 * @res: Reference counted resource pointer.
55 * @new_guest_memory_bo: Non ref-counted pointer to new guest memory buffer
56 * to be assigned to a resource.
57 * @new_guest_memory_offset: Offset into the new backup mob for resources
58 * that can share MOBs.
59 * @no_buffer_needed: Kernel does not need to allocate a MOB during validation,
60 * the command stream provides a mob bind operation.
61 * @switching_guest_memory_bo: The validation process is switching backup MOB.
62 * @first_usage: True iff the resource has been seen only once in the current
64 * @reserved: Whether the resource is currently reserved by this process.
65 * @dirty_set: Change dirty status of the resource.
66 * @dirty: Dirty information VMW_RES_DIRTY_XX.
67 * @private: Optionally additional memory for caller-private data.
69 * Bit fields are used since these structures are allocated and freed in
70 * large numbers and space conservation is desired.
72 struct vmw_validation_res_node {
73 struct list_head head;
74 struct vmwgfx_hash_item hash;
75 struct vmw_resource *res;
76 struct vmw_bo *new_guest_memory_bo;
77 unsigned long new_guest_memory_offset;
78 u32 no_buffer_needed : 1;
79 u32 switching_guest_memory_bo : 1;
84 unsigned long private[];
88 * vmw_validation_mem_alloc - Allocate kernel memory from the validation
89 * context based allocator
90 * @ctx: The validation context
91 * @size: The number of bytes to allocated.
93 * The memory allocated may not exceed PAGE_SIZE, and the returned
94 * address is aligned to sizeof(long). All memory allocated this way is
95 * reclaimed after validation when calling any of the exported functions:
96 * vmw_validation_unref_lists()
97 * vmw_validation_revert()
98 * vmw_validation_done()
100 * Return: Pointer to the allocated memory on success. NULL on failure.
102 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
107 size = vmw_validation_align(size);
108 if (size > PAGE_SIZE)
111 if (ctx->mem_size_left < size) {
112 struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
116 list_add_tail(&page->lru, &ctx->page_list);
117 ctx->page_address = page_address(page);
118 ctx->mem_size_left = PAGE_SIZE;
121 addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left));
122 ctx->mem_size_left -= size;
128 * vmw_validation_mem_free - Free all memory allocated using
129 * vmw_validation_mem_alloc()
130 * @ctx: The validation context
132 * All memory previously allocated for this context using
133 * vmw_validation_mem_alloc() is freed.
135 static void vmw_validation_mem_free(struct vmw_validation_context *ctx)
137 struct page *entry, *next;
139 list_for_each_entry_safe(entry, next, &ctx->page_list, lru) {
140 list_del_init(&entry->lru);
144 ctx->mem_size_left = 0;
148 * vmw_validation_find_bo_dup - Find a duplicate buffer object entry in the
149 * validation context's lists.
150 * @ctx: The validation context to search.
151 * @vbo: The buffer object to search for.
153 * Return: Pointer to the struct vmw_validation_bo_node referencing the
154 * duplicate, or NULL if none found.
156 static struct vmw_validation_bo_node *
157 vmw_validation_find_bo_dup(struct vmw_validation_context *ctx,
160 struct vmw_validation_bo_node *bo_node = NULL;
162 if (!ctx->merge_dups)
165 if (ctx->sw_context) {
166 struct vmwgfx_hash_item *hash;
167 unsigned long key = (unsigned long) vbo;
169 hash_for_each_possible_rcu(ctx->sw_context->res_ht, hash, head, key) {
170 if (hash->key == key) {
171 bo_node = container_of(hash, typeof(*bo_node), hash);
176 struct vmw_validation_bo_node *entry;
178 list_for_each_entry(entry, &ctx->bo_list, base.head) {
179 if (entry->base.bo == &vbo->tbo) {
190 * vmw_validation_find_res_dup - Find a duplicate resource entry in the
191 * validation context's lists.
192 * @ctx: The validation context to search.
193 * @res: Reference counted resource pointer.
195 * Return: Pointer to the struct vmw_validation_bo_node referencing the
196 * duplicate, or NULL if none found.
198 static struct vmw_validation_res_node *
199 vmw_validation_find_res_dup(struct vmw_validation_context *ctx,
200 struct vmw_resource *res)
202 struct vmw_validation_res_node *res_node = NULL;
204 if (!ctx->merge_dups)
207 if (ctx->sw_context) {
208 struct vmwgfx_hash_item *hash;
209 unsigned long key = (unsigned long) res;
211 hash_for_each_possible_rcu(ctx->sw_context->res_ht, hash, head, key) {
212 if (hash->key == key) {
213 res_node = container_of(hash, typeof(*res_node), hash);
218 struct vmw_validation_res_node *entry;
220 list_for_each_entry(entry, &ctx->resource_ctx_list, head) {
221 if (entry->res == res) {
227 list_for_each_entry(entry, &ctx->resource_list, head) {
228 if (entry->res == res) {
240 * vmw_validation_add_bo - Add a buffer object to the validation context.
241 * @ctx: The validation context.
242 * @vbo: The buffer object.
244 * Return: Zero on success, negative error code otherwise.
246 int vmw_validation_add_bo(struct vmw_validation_context *ctx,
249 struct vmw_validation_bo_node *bo_node;
251 bo_node = vmw_validation_find_bo_dup(ctx, vbo);
253 struct ttm_validate_buffer *val_buf;
255 bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node));
259 if (ctx->sw_context) {
260 bo_node->hash.key = (unsigned long) vbo;
261 hash_add_rcu(ctx->sw_context->res_ht, &bo_node->hash.head,
264 val_buf = &bo_node->base;
265 val_buf->bo = ttm_bo_get_unless_zero(&vbo->tbo);
268 val_buf->num_shared = 0;
269 list_add_tail(&val_buf->head, &ctx->bo_list);
276 * vmw_validation_add_resource - Add a resource to the validation context.
277 * @ctx: The validation context.
278 * @res: The resource.
279 * @priv_size: Size of private, additional metadata.
280 * @dirty: Whether to change dirty status.
281 * @p_node: Output pointer of additional metadata address.
282 * @first_usage: Whether this was the first time this resource was seen.
284 * Return: Zero on success, negative error code otherwise.
286 int vmw_validation_add_resource(struct vmw_validation_context *ctx,
287 struct vmw_resource *res,
293 struct vmw_validation_res_node *node;
295 node = vmw_validation_find_res_dup(ctx, res);
297 node->first_usage = 0;
301 node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
303 VMW_DEBUG_USER("Failed to allocate a resource validation entry.\n");
307 if (ctx->sw_context) {
308 node->hash.key = (unsigned long) res;
309 hash_add_rcu(ctx->sw_context->res_ht, &node->hash.head, node->hash.key);
311 node->res = vmw_resource_reference_unless_doomed(res);
315 node->first_usage = 1;
316 if (!res->dev_priv->has_mob) {
317 list_add_tail(&node->head, &ctx->resource_list);
319 switch (vmw_res_type(res)) {
320 case vmw_res_context:
321 case vmw_res_dx_context:
322 list_add(&node->head, &ctx->resource_ctx_list);
324 case vmw_res_cotable:
325 list_add_tail(&node->head, &ctx->resource_ctx_list);
328 list_add_tail(&node->head, &ctx->resource_list);
336 /* Overwriting previous information here is intentional! */
337 node->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
340 *first_usage = node->first_usage;
342 *p_node = &node->private;
348 * vmw_validation_res_set_dirty - Register a resource dirty set or clear during
350 * @ctx: The validation context.
351 * @val_private: The additional meta-data pointer returned when the
352 * resource was registered with the validation context. Used to identify
354 * @dirty: Dirty information VMW_RES_DIRTY_XX
356 void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
357 void *val_private, u32 dirty)
359 struct vmw_validation_res_node *val;
364 val = container_of(val_private, typeof(*val), private);
366 /* Overwriting previous information here is intentional! */
367 val->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
371 * vmw_validation_res_switch_backup - Register a backup MOB switch during
373 * @ctx: The validation context.
374 * @val_private: The additional meta-data pointer returned when the
375 * resource was registered with the validation context. Used to identify
377 * @vbo: The new backup buffer object MOB. This buffer object needs to have
378 * already been registered with the validation context.
379 * @guest_memory_offset: Offset into the new backup MOB.
381 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
384 unsigned long guest_memory_offset)
386 struct vmw_validation_res_node *val;
388 val = container_of(val_private, typeof(*val), private);
390 val->switching_guest_memory_bo = 1;
391 if (val->first_usage)
392 val->no_buffer_needed = 1;
394 val->new_guest_memory_bo = vbo;
395 val->new_guest_memory_offset = guest_memory_offset;
399 * vmw_validation_res_reserve - Reserve all resources registered with this
400 * validation context.
401 * @ctx: The validation context.
402 * @intr: Use interruptible waits when possible.
404 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
407 int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
410 struct vmw_validation_res_node *val;
413 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
415 list_for_each_entry(val, &ctx->resource_list, head) {
416 struct vmw_resource *res = val->res;
418 ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
423 if (res->guest_memory_bo) {
424 struct vmw_bo *vbo = res->guest_memory_bo;
426 vmw_bo_placement_set(vbo,
428 res->func->busy_domain);
429 ret = vmw_validation_add_bo(ctx, vbo);
434 if (val->switching_guest_memory_bo && val->new_guest_memory_bo &&
436 struct vmw_validation_bo_node *bo_node =
437 vmw_validation_find_bo_dup(ctx,
438 val->new_guest_memory_bo);
440 if (WARN_ON(!bo_node)) {
444 bo_node->coherent_count++;
451 vmw_validation_res_unreserve(ctx, true);
456 * vmw_validation_res_unreserve - Unreserve all reserved resources
457 * registered with this validation context.
458 * @ctx: The validation context.
459 * @backoff: Whether this is a backoff- of a commit-type operation. This
460 * is used to determine whether to switch backup MOBs or not.
462 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
465 struct vmw_validation_res_node *val;
467 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
469 list_for_each_entry(val, &ctx->resource_list, head) {
471 vmw_resource_unreserve(val->res,
476 list_for_each_entry(val, &ctx->resource_list, head) {
478 vmw_resource_unreserve(val->res,
481 val->switching_guest_memory_bo,
482 val->new_guest_memory_bo,
483 val->new_guest_memory_offset);
488 * vmw_validation_bo_validate_single - Validate a single buffer object.
489 * @bo: The TTM buffer object base.
490 * @interruptible: Whether to perform waits interruptible if possible.
492 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
495 static int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
498 struct vmw_bo *vbo = to_vmw_bo(&bo->base);
499 struct ttm_operation_ctx ctx = {
500 .interruptible = interruptible,
505 if (atomic_read(&vbo->cpu_writers))
508 if (vbo->tbo.pin_count > 0)
511 ret = ttm_bo_validate(bo, &vbo->placement, &ctx);
512 if (ret == 0 || ret == -ERESTARTSYS)
516 * If that failed, try again, this time evicting
519 ctx.allow_res_evict = true;
521 return ttm_bo_validate(bo, &vbo->placement, &ctx);
525 * vmw_validation_bo_validate - Validate all buffer objects registered with
526 * the validation context.
527 * @ctx: The validation context.
528 * @intr: Whether to perform waits interruptible if possible.
530 * Return: Zero on success, -ERESTARTSYS if interrupted,
531 * negative error code on failure.
533 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
535 struct vmw_validation_bo_node *entry;
538 list_for_each_entry(entry, &ctx->bo_list, base.head) {
539 struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
541 ret = vmw_validation_bo_validate_single(entry->base.bo, intr);
547 * Rather than having the resource code allocating the bo
548 * dirty tracker in resource_unreserve() where we can't fail,
549 * Do it here when validating the buffer object.
551 if (entry->coherent_count) {
552 unsigned int coherent_count = entry->coherent_count;
554 while (coherent_count) {
555 ret = vmw_bo_dirty_add(vbo);
561 entry->coherent_count -= coherent_count;
565 vmw_bo_dirty_scan(vbo);
571 * vmw_validation_res_validate - Validate all resources registered with the
572 * validation context.
573 * @ctx: The validation context.
574 * @intr: Whether to perform waits interruptible if possible.
576 * Before this function is called, all resource backup buffers must have
579 * Return: Zero on success, -ERESTARTSYS if interrupted,
580 * negative error code on failure.
582 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
584 struct vmw_validation_res_node *val;
587 list_for_each_entry(val, &ctx->resource_list, head) {
588 struct vmw_resource *res = val->res;
589 struct vmw_bo *backup = res->guest_memory_bo;
591 ret = vmw_resource_validate(res, intr, val->dirty_set &&
594 if (ret != -ERESTARTSYS)
595 DRM_ERROR("Failed to validate resource.\n");
599 /* Check if the resource switched backup buffer */
600 if (backup && res->guest_memory_bo && backup != res->guest_memory_bo) {
601 struct vmw_bo *vbo = res->guest_memory_bo;
603 vmw_bo_placement_set(vbo, res->func->domain,
604 res->func->busy_domain);
605 ret = vmw_validation_add_bo(ctx, vbo);
614 * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
615 * and unregister it from this validation context.
616 * @ctx: The validation context.
618 * The hash table used for duplicate finding is an expensive resource and
619 * may be protected by mutexes that may cause deadlocks during resource
620 * unreferencing if held. After resource- and buffer object registering,
621 * there is no longer any use for this hash table, so allow freeing it
622 * either to shorten any mutex locking time, or before resources- and
623 * buffer objects are freed during validation context cleanup.
625 void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
627 struct vmw_validation_bo_node *entry;
628 struct vmw_validation_res_node *val;
630 if (!ctx->sw_context)
633 list_for_each_entry(entry, &ctx->bo_list, base.head)
634 hash_del_rcu(&entry->hash.head);
636 list_for_each_entry(val, &ctx->resource_list, head)
637 hash_del_rcu(&val->hash.head);
639 list_for_each_entry(val, &ctx->resource_ctx_list, head)
640 hash_del_rcu(&entry->hash.head);
642 ctx->sw_context = NULL;
646 * vmw_validation_unref_lists - Unregister previously registered buffer
647 * object and resources.
648 * @ctx: The validation context.
650 * Note that this function may cause buffer object- and resource destructors
653 void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
655 struct vmw_validation_bo_node *entry;
656 struct vmw_validation_res_node *val;
658 list_for_each_entry(entry, &ctx->bo_list, base.head) {
659 ttm_bo_put(entry->base.bo);
660 entry->base.bo = NULL;
663 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
664 list_for_each_entry(val, &ctx->resource_list, head)
665 vmw_resource_unreference(&val->res);
668 * No need to detach each list entry since they are all freed with
669 * vmw_validation_free_mem. Just make the inaccessible.
671 INIT_LIST_HEAD(&ctx->bo_list);
672 INIT_LIST_HEAD(&ctx->resource_list);
674 vmw_validation_mem_free(ctx);
678 * vmw_validation_prepare - Prepare a validation context for command
680 * @ctx: The validation context.
681 * @mutex: The mutex used to protect resource reservation.
682 * @intr: Whether to perform waits interruptible if possible.
684 * Note that the single reservation mutex @mutex is an unfortunate
685 * construct. Ideally resource reservation should be moved to per-resource
687 * If this functions doesn't return Zero to indicate success, all resources
688 * are left unreserved but still referenced.
689 * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
692 int vmw_validation_prepare(struct vmw_validation_context *ctx,
700 ret = mutex_lock_interruptible(mutex);
707 ctx->res_mutex = mutex;
708 ret = vmw_validation_res_reserve(ctx, intr);
710 goto out_no_res_reserve;
712 ret = vmw_validation_bo_reserve(ctx, intr);
714 goto out_no_bo_reserve;
716 ret = vmw_validation_bo_validate(ctx, intr);
718 goto out_no_validate;
720 ret = vmw_validation_res_validate(ctx, intr);
722 goto out_no_validate;
727 vmw_validation_bo_backoff(ctx);
729 vmw_validation_res_unreserve(ctx, true);
738 * vmw_validation_revert - Revert validation actions if command submission
741 * @ctx: The validation context.
743 * The caller still needs to unref resources after a call to this function.
745 void vmw_validation_revert(struct vmw_validation_context *ctx)
747 vmw_validation_bo_backoff(ctx);
748 vmw_validation_res_unreserve(ctx, true);
750 mutex_unlock(ctx->res_mutex);
751 vmw_validation_unref_lists(ctx);
755 * vmw_validation_done - Commit validation actions after command submission
757 * @ctx: The validation context.
758 * @fence: Fence with which to fence all buffer objects taking part in the
759 * command submission.
761 * The caller does NOT need to unref resources after a call to this function.
763 void vmw_validation_done(struct vmw_validation_context *ctx,
764 struct vmw_fence_obj *fence)
766 vmw_validation_bo_fence(ctx, fence);
767 vmw_validation_res_unreserve(ctx, false);
769 mutex_unlock(ctx->res_mutex);
770 vmw_validation_unref_lists(ctx);
774 * vmw_validation_preload_bo - Preload the validation memory allocator for a
775 * call to vmw_validation_add_bo().
776 * @ctx: Pointer to the validation context.
778 * Iff this function returns successfully, the next call to
779 * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
780 * but voids the guarantee.
782 * Returns: Zero if successful, %-EINVAL otherwise.
784 int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
786 unsigned int size = sizeof(struct vmw_validation_bo_node);
788 if (!vmw_validation_mem_alloc(ctx, size))
791 ctx->mem_size_left += size;
796 * vmw_validation_preload_res - Preload the validation memory allocator for a
797 * call to vmw_validation_add_res().
798 * @ctx: Pointer to the validation context.
799 * @size: Size of the validation node extra data. See below.
801 * Iff this function returns successfully, the next call to
802 * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
803 * sleep. An error is not fatal but voids the guarantee.
805 * Returns: Zero if successful, %-EINVAL otherwise.
807 int vmw_validation_preload_res(struct vmw_validation_context *ctx,
810 size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
812 vmw_validation_align(sizeof(struct vmw_validation_bo_node));
813 if (!vmw_validation_mem_alloc(ctx, size))
816 ctx->mem_size_left += size;
821 * vmw_validation_bo_backoff - Unreserve buffer objects registered with a
823 * @ctx: The validation context
825 * This function unreserves the buffer objects previously reserved using
826 * vmw_validation_bo_reserve. It's typically used as part of an error path
828 void vmw_validation_bo_backoff(struct vmw_validation_context *ctx)
830 struct vmw_validation_bo_node *entry;
833 * Switching coherent resource backup buffers failed.
834 * Release corresponding buffer object dirty trackers.
836 list_for_each_entry(entry, &ctx->bo_list, base.head) {
837 if (entry->coherent_count) {
838 unsigned int coherent_count = entry->coherent_count;
839 struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
841 while (coherent_count--)
842 vmw_bo_dirty_release(vbo);
846 ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list);