1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright © 2018 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 <linux/slab.h>
29 #include "vmwgfx_validation.h"
30 #include "vmwgfx_drv.h"
33 * struct vmw_validation_bo_node - Buffer object validation metadata.
34 * @base: Metadata used for TTM reservation- and validation.
35 * @hash: A hash entry used for the duplicate detection hash table.
36 * @coherent_count: If switching backup buffers, number of new coherent
37 * resources that will have this buffer as a backup buffer.
38 * @as_mob: Validate as mob.
39 * @cpu_blit: Validate for cpu blit access.
41 * Bit fields are used since these structures are allocated and freed in
42 * large numbers and space conservation is desired.
44 struct vmw_validation_bo_node {
45 struct ttm_validate_buffer base;
46 struct drm_hash_item hash;
47 unsigned int coherent_count;
52 * struct vmw_validation_res_node - Resource validation metadata.
53 * @head: List head for the resource validation list.
54 * @hash: A hash entry used for the duplicate detection hash table.
55 * @res: Reference counted resource pointer.
56 * @new_backup: Non ref-counted pointer to new backup buffer to be assigned
58 * @new_backup_offset: Offset into the new backup mob for resources that can
60 * @no_buffer_needed: Kernel does not need to allocate a MOB during validation,
61 * the command stream provides a mob bind operation.
62 * @switching_backup: The validation process is switching backup MOB.
63 * @first_usage: True iff the resource has been seen only once in the current
65 * @reserved: Whether the resource is currently reserved by this process.
66 * @dirty_set: Change dirty status of the resource.
67 * @dirty: Dirty information VMW_RES_DIRTY_XX.
68 * @private: Optionally additional memory for caller-private data.
70 * Bit fields are used since these structures are allocated and freed in
71 * large numbers and space conservation is desired.
73 struct vmw_validation_res_node {
74 struct list_head head;
75 struct drm_hash_item hash;
76 struct vmw_resource *res;
77 struct vmw_buffer_object *new_backup;
78 unsigned long new_backup_offset;
79 u32 no_buffer_needed : 1;
80 u32 switching_backup : 1;
85 unsigned long private[];
89 * vmw_validation_mem_alloc - Allocate kernel memory from the validation
90 * context based allocator
91 * @ctx: The validation context
92 * @size: The number of bytes to allocated.
94 * The memory allocated may not exceed PAGE_SIZE, and the returned
95 * address is aligned to sizeof(long). All memory allocated this way is
96 * reclaimed after validation when calling any of the exported functions:
97 * vmw_validation_unref_lists()
98 * vmw_validation_revert()
99 * vmw_validation_done()
101 * Return: Pointer to the allocated memory on success. NULL on failure.
103 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
108 size = vmw_validation_align(size);
109 if (size > PAGE_SIZE)
112 if (ctx->mem_size_left < size) {
115 if (ctx->vm && ctx->vm_size_left < PAGE_SIZE) {
116 int ret = ctx->vm->reserve_mem(ctx->vm, ctx->vm->gran);
121 ctx->vm_size_left += ctx->vm->gran;
122 ctx->total_mem += ctx->vm->gran;
125 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
130 ctx->vm_size_left -= PAGE_SIZE;
132 list_add_tail(&page->lru, &ctx->page_list);
133 ctx->page_address = page_address(page);
134 ctx->mem_size_left = PAGE_SIZE;
137 addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left));
138 ctx->mem_size_left -= size;
144 * vmw_validation_mem_free - Free all memory allocated using
145 * vmw_validation_mem_alloc()
146 * @ctx: The validation context
148 * All memory previously allocated for this context using
149 * vmw_validation_mem_alloc() is freed.
151 static void vmw_validation_mem_free(struct vmw_validation_context *ctx)
153 struct page *entry, *next;
155 list_for_each_entry_safe(entry, next, &ctx->page_list, lru) {
156 list_del_init(&entry->lru);
160 ctx->mem_size_left = 0;
161 if (ctx->vm && ctx->total_mem) {
162 ctx->vm->unreserve_mem(ctx->vm, ctx->total_mem);
164 ctx->vm_size_left = 0;
169 * vmw_validation_find_bo_dup - Find a duplicate buffer object entry in the
170 * validation context's lists.
171 * @ctx: The validation context to search.
172 * @vbo: The buffer object to search for.
174 * Return: Pointer to the struct vmw_validation_bo_node referencing the
175 * duplicate, or NULL if none found.
177 static struct vmw_validation_bo_node *
178 vmw_validation_find_bo_dup(struct vmw_validation_context *ctx,
179 struct vmw_buffer_object *vbo)
181 struct vmw_validation_bo_node *bo_node = NULL;
183 if (!ctx->merge_dups)
187 struct drm_hash_item *hash;
189 if (!drm_ht_find_item(ctx->ht, (unsigned long) vbo, &hash))
190 bo_node = container_of(hash, typeof(*bo_node), hash);
192 struct vmw_validation_bo_node *entry;
194 list_for_each_entry(entry, &ctx->bo_list, base.head) {
195 if (entry->base.bo == &vbo->base) {
206 * vmw_validation_find_res_dup - Find a duplicate resource entry in the
207 * validation context's lists.
208 * @ctx: The validation context to search.
209 * @res: Reference counted resource pointer.
211 * Return: Pointer to the struct vmw_validation_bo_node referencing the
212 * duplicate, or NULL if none found.
214 static struct vmw_validation_res_node *
215 vmw_validation_find_res_dup(struct vmw_validation_context *ctx,
216 struct vmw_resource *res)
218 struct vmw_validation_res_node *res_node = NULL;
220 if (!ctx->merge_dups)
224 struct drm_hash_item *hash;
226 if (!drm_ht_find_item(ctx->ht, (unsigned long) res, &hash))
227 res_node = container_of(hash, typeof(*res_node), hash);
229 struct vmw_validation_res_node *entry;
231 list_for_each_entry(entry, &ctx->resource_ctx_list, head) {
232 if (entry->res == res) {
238 list_for_each_entry(entry, &ctx->resource_list, head) {
239 if (entry->res == res) {
251 * vmw_validation_add_bo - Add a buffer object to the validation context.
252 * @ctx: The validation context.
253 * @vbo: The buffer object.
254 * @as_mob: Validate as mob, otherwise suitable for GMR operations.
255 * @cpu_blit: Validate in a page-mappable location.
257 * Return: Zero on success, negative error code otherwise.
259 int vmw_validation_add_bo(struct vmw_validation_context *ctx,
260 struct vmw_buffer_object *vbo,
264 struct vmw_validation_bo_node *bo_node;
266 bo_node = vmw_validation_find_bo_dup(ctx, vbo);
268 if (bo_node->as_mob != as_mob ||
269 bo_node->cpu_blit != cpu_blit) {
270 DRM_ERROR("Inconsistent buffer usage.\n");
274 struct ttm_validate_buffer *val_buf;
277 bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node));
282 bo_node->hash.key = (unsigned long) vbo;
283 ret = drm_ht_insert_item(ctx->ht, &bo_node->hash);
285 DRM_ERROR("Failed to initialize a buffer "
286 "validation entry.\n");
290 val_buf = &bo_node->base;
291 val_buf->bo = ttm_bo_get_unless_zero(&vbo->base);
294 val_buf->num_shared = 0;
295 list_add_tail(&val_buf->head, &ctx->bo_list);
296 bo_node->as_mob = as_mob;
297 bo_node->cpu_blit = cpu_blit;
304 * vmw_validation_add_resource - Add a resource to the validation context.
305 * @ctx: The validation context.
306 * @res: The resource.
307 * @priv_size: Size of private, additional metadata.
308 * @dirty: Whether to change dirty status.
309 * @p_node: Output pointer of additional metadata address.
310 * @first_usage: Whether this was the first time this resource was seen.
312 * Return: Zero on success, negative error code otherwise.
314 int vmw_validation_add_resource(struct vmw_validation_context *ctx,
315 struct vmw_resource *res,
321 struct vmw_validation_res_node *node;
324 node = vmw_validation_find_res_dup(ctx, res);
326 node->first_usage = 0;
330 node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
332 VMW_DEBUG_USER("Failed to allocate a resource validation entry.\n");
337 node->hash.key = (unsigned long) res;
338 ret = drm_ht_insert_item(ctx->ht, &node->hash);
340 DRM_ERROR("Failed to initialize a resource validation "
345 node->res = vmw_resource_reference_unless_doomed(res);
349 node->first_usage = 1;
350 if (!res->dev_priv->has_mob) {
351 list_add_tail(&node->head, &ctx->resource_list);
353 switch (vmw_res_type(res)) {
354 case vmw_res_context:
355 case vmw_res_dx_context:
356 list_add(&node->head, &ctx->resource_ctx_list);
358 case vmw_res_cotable:
359 list_add_tail(&node->head, &ctx->resource_ctx_list);
362 list_add_tail(&node->head, &ctx->resource_list);
370 /* Overwriting previous information here is intentional! */
371 node->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
374 *first_usage = node->first_usage;
376 *p_node = &node->private;
382 * vmw_validation_res_set_dirty - Register a resource dirty set or clear during
384 * @ctx: The validation context.
385 * @val_private: The additional meta-data pointer returned when the
386 * resource was registered with the validation context. Used to identify
388 * @dirty: Dirty information VMW_RES_DIRTY_XX
390 void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
391 void *val_private, u32 dirty)
393 struct vmw_validation_res_node *val;
398 val = container_of(val_private, typeof(*val), private);
400 /* Overwriting previous information here is intentional! */
401 val->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
405 * vmw_validation_res_switch_backup - Register a backup MOB switch during
407 * @ctx: The validation context.
408 * @val_private: The additional meta-data pointer returned when the
409 * resource was registered with the validation context. Used to identify
411 * @vbo: The new backup buffer object MOB. This buffer object needs to have
412 * already been registered with the validation context.
413 * @backup_offset: Offset into the new backup MOB.
415 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
417 struct vmw_buffer_object *vbo,
418 unsigned long backup_offset)
420 struct vmw_validation_res_node *val;
422 val = container_of(val_private, typeof(*val), private);
424 val->switching_backup = 1;
425 if (val->first_usage)
426 val->no_buffer_needed = 1;
428 val->new_backup = vbo;
429 val->new_backup_offset = backup_offset;
433 * vmw_validation_res_reserve - Reserve all resources registered with this
434 * validation context.
435 * @ctx: The validation context.
436 * @intr: Use interruptible waits when possible.
438 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
441 int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
444 struct vmw_validation_res_node *val;
447 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
449 list_for_each_entry(val, &ctx->resource_list, head) {
450 struct vmw_resource *res = val->res;
452 ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
458 struct vmw_buffer_object *vbo = res->backup;
460 ret = vmw_validation_add_bo
461 (ctx, vbo, vmw_resource_needs_backup(res),
467 if (val->switching_backup && val->new_backup &&
469 struct vmw_validation_bo_node *bo_node =
470 vmw_validation_find_bo_dup(ctx,
473 if (WARN_ON(!bo_node)) {
477 bo_node->coherent_count++;
484 vmw_validation_res_unreserve(ctx, true);
489 * vmw_validation_res_unreserve - Unreserve all reserved resources
490 * registered with this validation context.
491 * @ctx: The validation context.
492 * @backoff: Whether this is a backoff- of a commit-type operation. This
493 * is used to determine whether to switch backup MOBs or not.
495 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
498 struct vmw_validation_res_node *val;
500 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
502 list_for_each_entry(val, &ctx->resource_list, head) {
504 vmw_resource_unreserve(val->res,
509 list_for_each_entry(val, &ctx->resource_list, head) {
511 vmw_resource_unreserve(val->res,
514 val->switching_backup,
516 val->new_backup_offset);
521 * vmw_validation_bo_validate_single - Validate a single buffer object.
522 * @bo: The TTM buffer object base.
523 * @interruptible: Whether to perform waits interruptible if possible.
524 * @validate_as_mob: Whether to validate in MOB memory.
526 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
529 int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
531 bool validate_as_mob)
533 struct vmw_buffer_object *vbo =
534 container_of(bo, struct vmw_buffer_object, base);
535 struct ttm_operation_ctx ctx = {
536 .interruptible = interruptible,
541 if (atomic_read(&vbo->cpu_writers))
544 if (vbo->base.pin_count > 0)
548 return ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
551 * Put BO in VRAM if there is space, otherwise as a GMR.
552 * If there is no space in VRAM and GMR ids are all used up,
553 * start evicting GMRs to make room. If the DMA buffer can't be
554 * used as a GMR, this will return -ENOMEM.
557 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx);
558 if (ret == 0 || ret == -ERESTARTSYS)
562 * If that failed, try VRAM again, this time evicting
566 ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx);
571 * vmw_validation_bo_validate - Validate all buffer objects registered with
572 * the validation context.
573 * @ctx: The validation context.
574 * @intr: Whether to perform waits interruptible if possible.
576 * Return: Zero on success, -ERESTARTSYS if interrupted,
577 * negative error code on failure.
579 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
581 struct vmw_validation_bo_node *entry;
584 list_for_each_entry(entry, &ctx->bo_list, base.head) {
585 struct vmw_buffer_object *vbo =
586 container_of(entry->base.bo, typeof(*vbo), base);
588 if (entry->cpu_blit) {
589 struct ttm_operation_ctx ttm_ctx = {
590 .interruptible = intr,
594 ret = ttm_bo_validate(entry->base.bo,
595 &vmw_nonfixed_placement, &ttm_ctx);
597 ret = vmw_validation_bo_validate_single
598 (entry->base.bo, intr, entry->as_mob);
604 * Rather than having the resource code allocating the bo
605 * dirty tracker in resource_unreserve() where we can't fail,
606 * Do it here when validating the buffer object.
608 if (entry->coherent_count) {
609 unsigned int coherent_count = entry->coherent_count;
611 while (coherent_count) {
612 ret = vmw_bo_dirty_add(vbo);
618 entry->coherent_count -= coherent_count;
622 vmw_bo_dirty_scan(vbo);
628 * vmw_validation_res_validate - Validate all resources registered with the
629 * validation context.
630 * @ctx: The validation context.
631 * @intr: Whether to perform waits interruptible if possible.
633 * Before this function is called, all resource backup buffers must have
636 * Return: Zero on success, -ERESTARTSYS if interrupted,
637 * negative error code on failure.
639 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
641 struct vmw_validation_res_node *val;
644 list_for_each_entry(val, &ctx->resource_list, head) {
645 struct vmw_resource *res = val->res;
646 struct vmw_buffer_object *backup = res->backup;
648 ret = vmw_resource_validate(res, intr, val->dirty_set &&
651 if (ret != -ERESTARTSYS)
652 DRM_ERROR("Failed to validate resource.\n");
656 /* Check if the resource switched backup buffer */
657 if (backup && res->backup && (backup != res->backup)) {
658 struct vmw_buffer_object *vbo = res->backup;
660 ret = vmw_validation_add_bo
661 (ctx, vbo, vmw_resource_needs_backup(res),
671 * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
672 * and unregister it from this validation context.
673 * @ctx: The validation context.
675 * The hash table used for duplicate finding is an expensive resource and
676 * may be protected by mutexes that may cause deadlocks during resource
677 * unreferencing if held. After resource- and buffer object registering,
678 * there is no longer any use for this hash table, so allow freeing it
679 * either to shorten any mutex locking time, or before resources- and
680 * buffer objects are freed during validation context cleanup.
682 void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
684 struct vmw_validation_bo_node *entry;
685 struct vmw_validation_res_node *val;
690 list_for_each_entry(entry, &ctx->bo_list, base.head)
691 (void) drm_ht_remove_item(ctx->ht, &entry->hash);
693 list_for_each_entry(val, &ctx->resource_list, head)
694 (void) drm_ht_remove_item(ctx->ht, &val->hash);
696 list_for_each_entry(val, &ctx->resource_ctx_list, head)
697 (void) drm_ht_remove_item(ctx->ht, &val->hash);
703 * vmw_validation_unref_lists - Unregister previously registered buffer
704 * object and resources.
705 * @ctx: The validation context.
707 * Note that this function may cause buffer object- and resource destructors
710 void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
712 struct vmw_validation_bo_node *entry;
713 struct vmw_validation_res_node *val;
715 list_for_each_entry(entry, &ctx->bo_list, base.head) {
716 ttm_bo_put(entry->base.bo);
717 entry->base.bo = NULL;
720 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
721 list_for_each_entry(val, &ctx->resource_list, head)
722 vmw_resource_unreference(&val->res);
725 * No need to detach each list entry since they are all freed with
726 * vmw_validation_free_mem. Just make the inaccessible.
728 INIT_LIST_HEAD(&ctx->bo_list);
729 INIT_LIST_HEAD(&ctx->resource_list);
731 vmw_validation_mem_free(ctx);
735 * vmw_validation_prepare - Prepare a validation context for command
737 * @ctx: The validation context.
738 * @mutex: The mutex used to protect resource reservation.
739 * @intr: Whether to perform waits interruptible if possible.
741 * Note that the single reservation mutex @mutex is an unfortunate
742 * construct. Ideally resource reservation should be moved to per-resource
744 * If this functions doesn't return Zero to indicate success, all resources
745 * are left unreserved but still referenced.
746 * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
749 int vmw_validation_prepare(struct vmw_validation_context *ctx,
757 ret = mutex_lock_interruptible(mutex);
764 ctx->res_mutex = mutex;
765 ret = vmw_validation_res_reserve(ctx, intr);
767 goto out_no_res_reserve;
769 ret = vmw_validation_bo_reserve(ctx, intr);
771 goto out_no_bo_reserve;
773 ret = vmw_validation_bo_validate(ctx, intr);
775 goto out_no_validate;
777 ret = vmw_validation_res_validate(ctx, intr);
779 goto out_no_validate;
784 vmw_validation_bo_backoff(ctx);
786 vmw_validation_res_unreserve(ctx, true);
795 * vmw_validation_revert - Revert validation actions if command submission
798 * @ctx: The validation context.
800 * The caller still needs to unref resources after a call to this function.
802 void vmw_validation_revert(struct vmw_validation_context *ctx)
804 vmw_validation_bo_backoff(ctx);
805 vmw_validation_res_unreserve(ctx, true);
807 mutex_unlock(ctx->res_mutex);
808 vmw_validation_unref_lists(ctx);
812 * vmw_validation_done - Commit validation actions after command submission
814 * @ctx: The validation context.
815 * @fence: Fence with which to fence all buffer objects taking part in the
816 * command submission.
818 * The caller does NOT need to unref resources after a call to this function.
820 void vmw_validation_done(struct vmw_validation_context *ctx,
821 struct vmw_fence_obj *fence)
823 vmw_validation_bo_fence(ctx, fence);
824 vmw_validation_res_unreserve(ctx, false);
826 mutex_unlock(ctx->res_mutex);
827 vmw_validation_unref_lists(ctx);
831 * vmw_validation_preload_bo - Preload the validation memory allocator for a
832 * call to vmw_validation_add_bo().
833 * @ctx: Pointer to the validation context.
835 * Iff this function returns successfully, the next call to
836 * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
837 * but voids the guarantee.
839 * Returns: Zero if successful, %-EINVAL otherwise.
841 int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
843 unsigned int size = sizeof(struct vmw_validation_bo_node);
845 if (!vmw_validation_mem_alloc(ctx, size))
848 ctx->mem_size_left += size;
853 * vmw_validation_preload_res - Preload the validation memory allocator for a
854 * call to vmw_validation_add_res().
855 * @ctx: Pointer to the validation context.
856 * @size: Size of the validation node extra data. See below.
858 * Iff this function returns successfully, the next call to
859 * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
860 * sleep. An error is not fatal but voids the guarantee.
862 * Returns: Zero if successful, %-EINVAL otherwise.
864 int vmw_validation_preload_res(struct vmw_validation_context *ctx,
867 size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
869 vmw_validation_align(sizeof(struct vmw_validation_bo_node));
870 if (!vmw_validation_mem_alloc(ctx, size))
873 ctx->mem_size_left += size;
878 * vmw_validation_bo_backoff - Unreserve buffer objects registered with a
880 * @ctx: The validation context
882 * This function unreserves the buffer objects previously reserved using
883 * vmw_validation_bo_reserve. It's typically used as part of an error path
885 void vmw_validation_bo_backoff(struct vmw_validation_context *ctx)
887 struct vmw_validation_bo_node *entry;
890 * Switching coherent resource backup buffers failed.
891 * Release corresponding buffer object dirty trackers.
893 list_for_each_entry(entry, &ctx->bo_list, base.head) {
894 if (entry->coherent_count) {
895 unsigned int coherent_count = entry->coherent_count;
896 struct vmw_buffer_object *vbo =
897 container_of(entry->base.bo, typeof(*vbo),
900 while (coherent_count--)
901 vmw_bo_dirty_release(vbo);
905 ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list);