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 * @as_mob: Validate as mob.
37 * @cpu_blit: Validate for cpu blit access.
39 * Bit fields are used since these structures are allocated and freed in
40 * large numbers and space conservation is desired.
42 struct vmw_validation_bo_node {
43 struct ttm_validate_buffer base;
44 struct drm_hash_item hash;
50 * struct vmw_validation_res_node - Resource validation metadata.
51 * @head: List head for the resource validation list.
52 * @hash: A hash entry used for the duplicate detection hash table.
53 * @res: Reference counted resource pointer.
54 * @new_backup: Non ref-counted pointer to new backup buffer to be assigned
56 * @new_backup_offset: Offset into the new backup mob for resources that can
58 * @no_buffer_needed: Kernel does not need to allocate a MOB during validation,
59 * the command stream provides a mob bind operation.
60 * @switching_backup: The validation process is switching backup MOB.
61 * @first_usage: True iff the resource has been seen only once in the current
63 * @reserved: Whether the resource is currently reserved by this process.
64 * @private: Optionally additional memory for caller-private data.
66 * Bit fields are used since these structures are allocated and freed in
67 * large numbers and space conservation is desired.
69 struct vmw_validation_res_node {
70 struct list_head head;
71 struct drm_hash_item hash;
72 struct vmw_resource *res;
73 struct vmw_buffer_object *new_backup;
74 unsigned long new_backup_offset;
75 u32 no_buffer_needed : 1;
76 u32 switching_backup : 1;
79 unsigned long private[0];
83 * vmw_validation_mem_alloc - Allocate kernel memory from the validation
84 * context based allocator
85 * @ctx: The validation context
86 * @size: The number of bytes to allocated.
88 * The memory allocated may not exceed PAGE_SIZE, and the returned
89 * address is aligned to sizeof(long). All memory allocated this way is
90 * reclaimed after validation when calling any of the exported functions:
91 * vmw_validation_unref_lists()
92 * vmw_validation_revert()
93 * vmw_validation_done()
95 * Return: Pointer to the allocated memory on success. NULL on failure.
97 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
102 size = vmw_validation_align(size);
103 if (size > PAGE_SIZE)
106 if (ctx->mem_size_left < size) {
107 struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
112 list_add_tail(&page->lru, &ctx->page_list);
113 ctx->page_address = page_address(page);
114 ctx->mem_size_left = PAGE_SIZE;
117 addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left));
118 ctx->mem_size_left -= size;
124 * vmw_validation_mem_free - Free all memory allocated using
125 * vmw_validation_mem_alloc()
126 * @ctx: The validation context
128 * All memory previously allocated for this context using
129 * vmw_validation_mem_alloc() is freed.
131 static void vmw_validation_mem_free(struct vmw_validation_context *ctx)
133 struct page *entry, *next;
135 list_for_each_entry_safe(entry, next, &ctx->page_list, lru) {
136 list_del_init(&entry->lru);
140 ctx->mem_size_left = 0;
144 * vmw_validation_find_bo_dup - Find a duplicate buffer object entry in the
145 * validation context's lists.
146 * @ctx: The validation context to search.
147 * @vbo: The buffer object to search for.
149 * Return: Pointer to the struct vmw_validation_bo_node referencing the
150 * duplicate, or NULL if none found.
152 static struct vmw_validation_bo_node *
153 vmw_validation_find_bo_dup(struct vmw_validation_context *ctx,
154 struct vmw_buffer_object *vbo)
156 struct vmw_validation_bo_node *bo_node = NULL;
158 if (!ctx->merge_dups)
162 struct drm_hash_item *hash;
164 if (!drm_ht_find_item(ctx->ht, (unsigned long) vbo, &hash))
165 bo_node = container_of(hash, typeof(*bo_node), hash);
167 struct vmw_validation_bo_node *entry;
169 list_for_each_entry(entry, &ctx->bo_list, base.head) {
170 if (entry->base.bo == &vbo->base) {
181 * vmw_validation_find_res_dup - Find a duplicate resource entry in the
182 * validation context's lists.
183 * @ctx: The validation context to search.
184 * @vbo: The buffer object to search for.
186 * Return: Pointer to the struct vmw_validation_bo_node referencing the
187 * duplicate, or NULL if none found.
189 static struct vmw_validation_res_node *
190 vmw_validation_find_res_dup(struct vmw_validation_context *ctx,
191 struct vmw_resource *res)
193 struct vmw_validation_res_node *res_node = NULL;
195 if (!ctx->merge_dups)
199 struct drm_hash_item *hash;
201 if (!drm_ht_find_item(ctx->ht, (unsigned long) res, &hash))
202 res_node = container_of(hash, typeof(*res_node), hash);
204 struct vmw_validation_res_node *entry;
206 list_for_each_entry(entry, &ctx->resource_ctx_list, head) {
207 if (entry->res == res) {
213 list_for_each_entry(entry, &ctx->resource_list, head) {
214 if (entry->res == res) {
226 * vmw_validation_add_bo - Add a buffer object to the validation context.
227 * @ctx: The validation context.
228 * @vbo: The buffer object.
229 * @as_mob: Validate as mob, otherwise suitable for GMR operations.
230 * @cpu_blit: Validate in a page-mappable location.
232 * Return: Zero on success, negative error code otherwise.
234 int vmw_validation_add_bo(struct vmw_validation_context *ctx,
235 struct vmw_buffer_object *vbo,
239 struct vmw_validation_bo_node *bo_node;
241 bo_node = vmw_validation_find_bo_dup(ctx, vbo);
243 if (bo_node->as_mob != as_mob ||
244 bo_node->cpu_blit != cpu_blit) {
245 DRM_ERROR("Inconsistent buffer usage.\n");
249 struct ttm_validate_buffer *val_buf;
252 bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node));
257 bo_node->hash.key = (unsigned long) vbo;
258 ret = drm_ht_insert_item(ctx->ht, &bo_node->hash);
260 DRM_ERROR("Failed to initialize a buffer "
261 "validation entry.\n");
265 val_buf = &bo_node->base;
266 val_buf->bo = ttm_bo_get_unless_zero(&vbo->base);
269 val_buf->shared = false;
270 list_add_tail(&val_buf->head, &ctx->bo_list);
271 bo_node->as_mob = as_mob;
272 bo_node->cpu_blit = cpu_blit;
279 * vmw_validation_add_resource - Add a resource to the validation context.
280 * @ctx: The validation context.
281 * @res: The resource.
282 * @priv_size: Size of private, additional metadata.
283 * @p_node: Output pointer of additional metadata address.
284 * @first_usage: Whether this was the first time this resource was seen.
286 * Return: Zero on success, negative error code otherwise.
288 int vmw_validation_add_resource(struct vmw_validation_context *ctx,
289 struct vmw_resource *res,
294 struct vmw_validation_res_node *node;
297 node = vmw_validation_find_res_dup(ctx, res);
299 node->first_usage = 0;
303 node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
305 DRM_ERROR("Failed to allocate a resource validation "
311 node->hash.key = (unsigned long) res;
312 ret = drm_ht_insert_item(ctx->ht, &node->hash);
314 DRM_ERROR("Failed to initialize a resource validation "
319 node->res = vmw_resource_reference_unless_doomed(res);
323 node->first_usage = 1;
324 if (!res->dev_priv->has_mob) {
325 list_add_tail(&node->head, &ctx->resource_list);
327 switch (vmw_res_type(res)) {
328 case vmw_res_context:
329 case vmw_res_dx_context:
330 list_add(&node->head, &ctx->resource_ctx_list);
332 case vmw_res_cotable:
333 list_add_tail(&node->head, &ctx->resource_ctx_list);
336 list_add_tail(&node->head, &ctx->resource_list);
343 *first_usage = node->first_usage;
345 *p_node = &node->private;
351 * vmw_validation_res_switch_backup - Register a backup MOB switch during
353 * @ctx: The validation context.
354 * @val_private: The additional meta-data pointer returned when the
355 * resource was registered with the validation context. Used to identify
357 * @vbo: The new backup buffer object MOB. This buffer object needs to have
358 * already been registered with the validation context.
359 * @backup_offset: Offset into the new backup MOB.
361 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
363 struct vmw_buffer_object *vbo,
364 unsigned long backup_offset)
366 struct vmw_validation_res_node *val;
368 val = container_of(val_private, typeof(*val), private);
370 val->switching_backup = 1;
371 if (val->first_usage)
372 val->no_buffer_needed = 1;
374 val->new_backup = vbo;
375 val->new_backup_offset = backup_offset;
379 * vmw_validation_res_reserve - Reserve all resources registered with this
380 * validation context.
381 * @ctx: The validation context.
382 * @intr: Use interruptible waits when possible.
384 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
387 int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
390 struct vmw_validation_res_node *val;
393 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
395 list_for_each_entry(val, &ctx->resource_list, head) {
396 struct vmw_resource *res = val->res;
398 ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
404 struct vmw_buffer_object *vbo = res->backup;
406 ret = vmw_validation_add_bo
407 (ctx, vbo, vmw_resource_needs_backup(res),
417 vmw_validation_res_unreserve(ctx, true);
422 * vmw_validation_res_unreserve - Unreserve all reserved resources
423 * registered with this validation context.
424 * @ctx: The validation context.
425 * @backoff: Whether this is a backoff- of a commit-type operation. This
426 * is used to determine whether to switch backup MOBs or not.
428 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
431 struct vmw_validation_res_node *val;
433 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
435 list_for_each_entry(val, &ctx->resource_list, head) {
437 vmw_resource_unreserve(val->res,
439 val->switching_backup,
441 val->new_backup_offset);
446 * vmw_validation_bo_validate_single - Validate a single buffer object.
447 * @bo: The TTM buffer object base.
448 * @interruptible: Whether to perform waits interruptible if possible.
449 * @validate_as_mob: Whether to validate in MOB memory.
451 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
454 int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
456 bool validate_as_mob)
458 struct vmw_buffer_object *vbo =
459 container_of(bo, struct vmw_buffer_object, base);
460 struct ttm_operation_ctx ctx = {
461 .interruptible = interruptible,
466 if (vbo->pin_count > 0)
470 return ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
473 * Put BO in VRAM if there is space, otherwise as a GMR.
474 * If there is no space in VRAM and GMR ids are all used up,
475 * start evicting GMRs to make room. If the DMA buffer can't be
476 * used as a GMR, this will return -ENOMEM.
479 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx);
480 if (ret == 0 || ret == -ERESTARTSYS)
484 * If that failed, try VRAM again, this time evicting
488 ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx);
493 * vmw_validation_bo_validate - Validate all buffer objects registered with
494 * the validation context.
495 * @ctx: The validation context.
496 * @intr: Whether to perform waits interruptible if possible.
498 * Return: Zero on success, -ERESTARTSYS if interrupted,
499 * negative error code on failure.
501 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
503 struct vmw_validation_bo_node *entry;
506 list_for_each_entry(entry, &ctx->bo_list, base.head) {
507 if (entry->cpu_blit) {
508 struct ttm_operation_ctx ctx = {
509 .interruptible = intr,
513 ret = ttm_bo_validate(entry->base.bo,
514 &vmw_nonfixed_placement, &ctx);
516 ret = vmw_validation_bo_validate_single
517 (entry->base.bo, intr, entry->as_mob);
526 * vmw_validation_res_validate - Validate all resources registered with the
527 * validation context.
528 * @ctx: The validation context.
529 * @intr: Whether to perform waits interruptible if possible.
531 * Before this function is called, all resource backup buffers must have
534 * Return: Zero on success, -ERESTARTSYS if interrupted,
535 * negative error code on failure.
537 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
539 struct vmw_validation_res_node *val;
542 list_for_each_entry(val, &ctx->resource_list, head) {
543 struct vmw_resource *res = val->res;
544 struct vmw_buffer_object *backup = res->backup;
546 ret = vmw_resource_validate(res, intr);
548 if (ret != -ERESTARTSYS)
549 DRM_ERROR("Failed to validate resource.\n");
553 /* Check if the resource switched backup buffer */
554 if (backup && res->backup && (backup != res->backup)) {
555 struct vmw_buffer_object *vbo = res->backup;
557 ret = vmw_validation_add_bo
558 (ctx, vbo, vmw_resource_needs_backup(res),
568 * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
569 * and unregister it from this validation context.
570 * @ctx: The validation context.
572 * The hash table used for duplicate finding is an expensive resource and
573 * may be protected by mutexes that may cause deadlocks during resource
574 * unreferencing if held. After resource- and buffer object registering,
575 * there is no longer any use for this hash table, so allow freeing it
576 * either to shorten any mutex locking time, or before resources- and
577 * buffer objects are freed during validation context cleanup.
579 void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
581 struct vmw_validation_bo_node *entry;
582 struct vmw_validation_res_node *val;
587 list_for_each_entry(entry, &ctx->bo_list, base.head)
588 (void) drm_ht_remove_item(ctx->ht, &entry->hash);
590 list_for_each_entry(val, &ctx->resource_list, head)
591 (void) drm_ht_remove_item(ctx->ht, &val->hash);
593 list_for_each_entry(val, &ctx->resource_ctx_list, head)
594 (void) drm_ht_remove_item(ctx->ht, &val->hash);
600 * vmw_validation_unref_lists - Unregister previously registered buffer
601 * object and resources.
602 * @ctx: The validation context.
604 * Note that this function may cause buffer object- and resource destructors
607 void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
609 struct vmw_validation_bo_node *entry;
610 struct vmw_validation_res_node *val;
612 list_for_each_entry(entry, &ctx->bo_list, base.head)
613 ttm_bo_unref(&entry->base.bo);
615 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
616 list_for_each_entry(val, &ctx->resource_list, head)
617 vmw_resource_unreference(&val->res);
620 * No need to detach each list entry since they are all freed with
621 * vmw_validation_free_mem. Just make the inaccessible.
623 INIT_LIST_HEAD(&ctx->bo_list);
624 INIT_LIST_HEAD(&ctx->resource_list);
626 vmw_validation_mem_free(ctx);
630 * vmw_validation_prepare - Prepare a validation context for command
632 * @ctx: The validation context.
633 * @mutex: The mutex used to protect resource reservation.
634 * @intr: Whether to perform waits interruptible if possible.
636 * Note that the single reservation mutex @mutex is an unfortunate
637 * construct. Ideally resource reservation should be moved to per-resource
639 * If this functions doesn't return Zero to indicate success, all resources
640 * are left unreserved but still referenced.
641 * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
644 int vmw_validation_prepare(struct vmw_validation_context *ctx,
652 ret = mutex_lock_interruptible(mutex);
659 ctx->res_mutex = mutex;
660 ret = vmw_validation_res_reserve(ctx, intr);
662 goto out_no_res_reserve;
664 ret = vmw_validation_bo_reserve(ctx, intr);
666 goto out_no_bo_reserve;
668 ret = vmw_validation_bo_validate(ctx, intr);
670 goto out_no_validate;
672 ret = vmw_validation_res_validate(ctx, intr);
674 goto out_no_validate;
679 vmw_validation_bo_backoff(ctx);
681 vmw_validation_res_unreserve(ctx, true);
690 * vmw_validation_revert - Revert validation actions if command submission
693 * @ctx: The validation context.
695 * The caller still needs to unref resources after a call to this function.
697 void vmw_validation_revert(struct vmw_validation_context *ctx)
699 vmw_validation_bo_backoff(ctx);
700 vmw_validation_res_unreserve(ctx, true);
702 mutex_unlock(ctx->res_mutex);
703 vmw_validation_unref_lists(ctx);
707 * vmw_validation_cone - Commit validation actions after command submission
709 * @ctx: The validation context.
710 * @fence: Fence with which to fence all buffer objects taking part in the
711 * command submission.
713 * The caller does NOT need to unref resources after a call to this function.
715 void vmw_validation_done(struct vmw_validation_context *ctx,
716 struct vmw_fence_obj *fence)
718 vmw_validation_bo_fence(ctx, fence);
719 vmw_validation_res_unreserve(ctx, false);
721 mutex_unlock(ctx->res_mutex);
722 vmw_validation_unref_lists(ctx);
726 * vmw_validation_preload_bo - Preload the validation memory allocator for a
727 * call to vmw_validation_add_bo().
728 * @ctx: Pointer to the validation context.
730 * Iff this function returns successfully, the next call to
731 * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
732 * but voids the guarantee.
734 * Returns: Zero if successful, %-EINVAL otherwise.
736 int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
738 unsigned int size = sizeof(struct vmw_validation_bo_node);
740 if (!vmw_validation_mem_alloc(ctx, size))
743 ctx->mem_size_left += size;
748 * vmw_validation_preload_res - Preload the validation memory allocator for a
749 * call to vmw_validation_add_res().
750 * @ctx: Pointer to the validation context.
751 * @size: Size of the validation node extra data. See below.
753 * Iff this function returns successfully, the next call to
754 * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
755 * sleep. An error is not fatal but voids the guarantee.
757 * Returns: Zero if successful, %-EINVAL otherwise.
759 int vmw_validation_preload_res(struct vmw_validation_context *ctx,
762 size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
764 vmw_validation_align(sizeof(struct vmw_validation_bo_node));
765 if (!vmw_validation_mem_alloc(ctx, size))
768 ctx->mem_size_left += size;