1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright © 2018 - 2023 VMware, Inc., Palo Alto, CA., USA 5 * All Rights Reserved. 6 * 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: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 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. 26 * 27 **************************************************************************/ 28 #include "vmwgfx_bo.h" 29 #include "vmwgfx_drv.h" 30 #include "vmwgfx_resource_priv.h" 31 #include "vmwgfx_validation.h" 32 33 #include <linux/slab.h> 34 35 /** 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. 41 * 42 * Bit fields are used since these structures are allocated and freed in 43 * large numbers and space conservation is desired. 44 */ 45 struct vmw_validation_bo_node { 46 struct ttm_validate_buffer base; 47 struct vmwgfx_hash_item hash; 48 unsigned int coherent_count; 49 }; 50 /** 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 63 * validation batch. 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. 68 * 69 * Bit fields are used since these structures are allocated and freed in 70 * large numbers and space conservation is desired. 71 */ 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; 80 u32 first_usage : 1; 81 u32 reserved : 1; 82 u32 dirty : 1; 83 u32 dirty_set : 1; 84 unsigned long private[]; 85 }; 86 87 /** 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. 92 * 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() 99 * 100 * Return: Pointer to the allocated memory on success. NULL on failure. 101 */ 102 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx, 103 unsigned int size) 104 { 105 void *addr; 106 107 size = vmw_validation_align(size); 108 if (size > PAGE_SIZE) 109 return NULL; 110 111 if (ctx->mem_size_left < size) { 112 struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO); 113 if (!page) 114 return NULL; 115 116 list_add_tail(&page->lru, &ctx->page_list); 117 ctx->page_address = page_address(page); 118 ctx->mem_size_left = PAGE_SIZE; 119 } 120 121 addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left)); 122 ctx->mem_size_left -= size; 123 124 return addr; 125 } 126 127 /** 128 * vmw_validation_mem_free - Free all memory allocated using 129 * vmw_validation_mem_alloc() 130 * @ctx: The validation context 131 * 132 * All memory previously allocated for this context using 133 * vmw_validation_mem_alloc() is freed. 134 */ 135 static void vmw_validation_mem_free(struct vmw_validation_context *ctx) 136 { 137 struct page *entry, *next; 138 139 list_for_each_entry_safe(entry, next, &ctx->page_list, lru) { 140 list_del_init(&entry->lru); 141 __free_page(entry); 142 } 143 144 ctx->mem_size_left = 0; 145 } 146 147 /** 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. 152 * 153 * Return: Pointer to the struct vmw_validation_bo_node referencing the 154 * duplicate, or NULL if none found. 155 */ 156 static struct vmw_validation_bo_node * 157 vmw_validation_find_bo_dup(struct vmw_validation_context *ctx, 158 struct vmw_bo *vbo) 159 { 160 struct vmw_validation_bo_node *bo_node = NULL; 161 162 if (!ctx->merge_dups) 163 return NULL; 164 165 if (ctx->sw_context) { 166 struct vmwgfx_hash_item *hash; 167 unsigned long key = (unsigned long) vbo; 168 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); 172 break; 173 } 174 } 175 } else { 176 struct vmw_validation_bo_node *entry; 177 178 list_for_each_entry(entry, &ctx->bo_list, base.head) { 179 if (entry->base.bo == &vbo->tbo) { 180 bo_node = entry; 181 break; 182 } 183 } 184 } 185 186 return bo_node; 187 } 188 189 /** 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. 194 * 195 * Return: Pointer to the struct vmw_validation_bo_node referencing the 196 * duplicate, or NULL if none found. 197 */ 198 static struct vmw_validation_res_node * 199 vmw_validation_find_res_dup(struct vmw_validation_context *ctx, 200 struct vmw_resource *res) 201 { 202 struct vmw_validation_res_node *res_node = NULL; 203 204 if (!ctx->merge_dups) 205 return NULL; 206 207 if (ctx->sw_context) { 208 struct vmwgfx_hash_item *hash; 209 unsigned long key = (unsigned long) res; 210 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); 214 break; 215 } 216 } 217 } else { 218 struct vmw_validation_res_node *entry; 219 220 list_for_each_entry(entry, &ctx->resource_ctx_list, head) { 221 if (entry->res == res) { 222 res_node = entry; 223 goto out; 224 } 225 } 226 227 list_for_each_entry(entry, &ctx->resource_list, head) { 228 if (entry->res == res) { 229 res_node = entry; 230 break; 231 } 232 } 233 234 } 235 out: 236 return res_node; 237 } 238 239 /** 240 * vmw_validation_add_bo - Add a buffer object to the validation context. 241 * @ctx: The validation context. 242 * @vbo: The buffer object. 243 * 244 * Return: Zero on success, negative error code otherwise. 245 */ 246 int vmw_validation_add_bo(struct vmw_validation_context *ctx, 247 struct vmw_bo *vbo) 248 { 249 struct vmw_validation_bo_node *bo_node; 250 251 bo_node = vmw_validation_find_bo_dup(ctx, vbo); 252 if (!bo_node) { 253 struct ttm_validate_buffer *val_buf; 254 255 bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node)); 256 if (!bo_node) 257 return -ENOMEM; 258 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, 262 bo_node->hash.key); 263 } 264 val_buf = &bo_node->base; 265 vmw_bo_reference(vbo); 266 val_buf->bo = &vbo->tbo; 267 val_buf->num_shared = 0; 268 list_add_tail(&val_buf->head, &ctx->bo_list); 269 } 270 271 return 0; 272 } 273 274 /** 275 * vmw_validation_add_resource - Add a resource to the validation context. 276 * @ctx: The validation context. 277 * @res: The resource. 278 * @priv_size: Size of private, additional metadata. 279 * @dirty: Whether to change dirty status. 280 * @p_node: Output pointer of additional metadata address. 281 * @first_usage: Whether this was the first time this resource was seen. 282 * 283 * Return: Zero on success, negative error code otherwise. 284 */ 285 int vmw_validation_add_resource(struct vmw_validation_context *ctx, 286 struct vmw_resource *res, 287 size_t priv_size, 288 u32 dirty, 289 void **p_node, 290 bool *first_usage) 291 { 292 struct vmw_validation_res_node *node; 293 294 node = vmw_validation_find_res_dup(ctx, res); 295 if (node) { 296 node->first_usage = 0; 297 goto out_fill; 298 } 299 300 node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size); 301 if (!node) { 302 VMW_DEBUG_USER("Failed to allocate a resource validation entry.\n"); 303 return -ENOMEM; 304 } 305 306 if (ctx->sw_context) { 307 node->hash.key = (unsigned long) res; 308 hash_add_rcu(ctx->sw_context->res_ht, &node->hash.head, node->hash.key); 309 } 310 node->res = vmw_resource_reference_unless_doomed(res); 311 if (!node->res) { 312 if (ctx->sw_context) 313 hash_del_rcu(&node->hash.head); 314 return -ESRCH; 315 } 316 317 node->first_usage = 1; 318 if (!res->dev_priv->has_mob) { 319 list_add_tail(&node->head, &ctx->resource_list); 320 } else { 321 switch (vmw_res_type(res)) { 322 case vmw_res_context: 323 case vmw_res_dx_context: 324 list_add(&node->head, &ctx->resource_ctx_list); 325 break; 326 case vmw_res_cotable: 327 list_add_tail(&node->head, &ctx->resource_ctx_list); 328 break; 329 default: 330 list_add_tail(&node->head, &ctx->resource_list); 331 break; 332 } 333 } 334 335 out_fill: 336 if (dirty) { 337 node->dirty_set = 1; 338 /* Overwriting previous information here is intentional! */ 339 node->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0; 340 } 341 if (first_usage) 342 *first_usage = node->first_usage; 343 if (p_node) 344 *p_node = &node->private; 345 346 return 0; 347 } 348 349 /** 350 * vmw_validation_res_set_dirty - Register a resource dirty set or clear during 351 * validation. 352 * @ctx: The validation context. 353 * @val_private: The additional meta-data pointer returned when the 354 * resource was registered with the validation context. Used to identify 355 * the resource. 356 * @dirty: Dirty information VMW_RES_DIRTY_XX 357 */ 358 void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx, 359 void *val_private, u32 dirty) 360 { 361 struct vmw_validation_res_node *val; 362 363 if (!dirty) 364 return; 365 366 val = container_of(val_private, typeof(*val), private); 367 val->dirty_set = 1; 368 /* Overwriting previous information here is intentional! */ 369 val->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0; 370 } 371 372 /** 373 * vmw_validation_res_switch_backup - Register a backup MOB switch during 374 * validation. 375 * @ctx: The validation context. 376 * @val_private: The additional meta-data pointer returned when the 377 * resource was registered with the validation context. Used to identify 378 * the resource. 379 * @vbo: The new backup buffer object MOB. This buffer object needs to have 380 * already been registered with the validation context. 381 * @guest_memory_offset: Offset into the new backup MOB. 382 */ 383 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx, 384 void *val_private, 385 struct vmw_bo *vbo, 386 unsigned long guest_memory_offset) 387 { 388 struct vmw_validation_res_node *val; 389 390 val = container_of(val_private, typeof(*val), private); 391 392 val->switching_guest_memory_bo = 1; 393 if (val->first_usage) 394 val->no_buffer_needed = 1; 395 396 val->new_guest_memory_bo = vbo; 397 val->new_guest_memory_offset = guest_memory_offset; 398 } 399 400 /** 401 * vmw_validation_res_reserve - Reserve all resources registered with this 402 * validation context. 403 * @ctx: The validation context. 404 * @intr: Use interruptible waits when possible. 405 * 406 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error 407 * code on failure. 408 */ 409 int vmw_validation_res_reserve(struct vmw_validation_context *ctx, 410 bool intr) 411 { 412 struct vmw_validation_res_node *val; 413 int ret = 0; 414 415 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list); 416 417 list_for_each_entry(val, &ctx->resource_list, head) { 418 struct vmw_resource *res = val->res; 419 420 ret = vmw_resource_reserve(res, intr, val->no_buffer_needed); 421 if (ret) 422 goto out_unreserve; 423 424 val->reserved = 1; 425 if (res->guest_memory_bo) { 426 struct vmw_bo *vbo = res->guest_memory_bo; 427 428 vmw_bo_placement_set(vbo, 429 res->func->domain, 430 res->func->busy_domain); 431 ret = vmw_validation_add_bo(ctx, vbo); 432 if (ret) 433 goto out_unreserve; 434 } 435 436 if (val->switching_guest_memory_bo && val->new_guest_memory_bo && 437 res->coherent) { 438 struct vmw_validation_bo_node *bo_node = 439 vmw_validation_find_bo_dup(ctx, 440 val->new_guest_memory_bo); 441 442 if (WARN_ON(!bo_node)) { 443 ret = -EINVAL; 444 goto out_unreserve; 445 } 446 bo_node->coherent_count++; 447 } 448 } 449 450 return 0; 451 452 out_unreserve: 453 vmw_validation_res_unreserve(ctx, true); 454 return ret; 455 } 456 457 /** 458 * vmw_validation_res_unreserve - Unreserve all reserved resources 459 * registered with this validation context. 460 * @ctx: The validation context. 461 * @backoff: Whether this is a backoff- of a commit-type operation. This 462 * is used to determine whether to switch backup MOBs or not. 463 */ 464 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx, 465 bool backoff) 466 { 467 struct vmw_validation_res_node *val; 468 469 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list); 470 if (backoff) 471 list_for_each_entry(val, &ctx->resource_list, head) { 472 if (val->reserved) 473 vmw_resource_unreserve(val->res, 474 false, false, false, 475 NULL, 0); 476 } 477 else 478 list_for_each_entry(val, &ctx->resource_list, head) { 479 if (val->reserved) 480 vmw_resource_unreserve(val->res, 481 val->dirty_set, 482 val->dirty, 483 val->switching_guest_memory_bo, 484 val->new_guest_memory_bo, 485 val->new_guest_memory_offset); 486 } 487 } 488 489 /** 490 * vmw_validation_bo_validate_single - Validate a single buffer object. 491 * @bo: The TTM buffer object base. 492 * @interruptible: Whether to perform waits interruptible if possible. 493 * 494 * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error 495 * code on failure. 496 */ 497 static int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo, 498 bool interruptible) 499 { 500 struct vmw_bo *vbo = to_vmw_bo(&bo->base); 501 struct ttm_operation_ctx ctx = { 502 .interruptible = interruptible, 503 .no_wait_gpu = false 504 }; 505 int ret; 506 507 if (atomic_read(&vbo->cpu_writers)) 508 return -EBUSY; 509 510 if (vbo->tbo.pin_count > 0) 511 return 0; 512 513 ret = ttm_bo_validate(bo, &vbo->placement, &ctx); 514 if (ret == 0 || ret == -ERESTARTSYS) 515 return ret; 516 517 /* 518 * If that failed, try again, this time evicting 519 * previous contents. 520 */ 521 ctx.allow_res_evict = true; 522 523 return ttm_bo_validate(bo, &vbo->placement, &ctx); 524 } 525 526 /** 527 * vmw_validation_bo_validate - Validate all buffer objects registered with 528 * the validation context. 529 * @ctx: The validation context. 530 * @intr: Whether to perform waits interruptible if possible. 531 * 532 * Return: Zero on success, -ERESTARTSYS if interrupted, 533 * negative error code on failure. 534 */ 535 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr) 536 { 537 struct vmw_validation_bo_node *entry; 538 int ret; 539 540 list_for_each_entry(entry, &ctx->bo_list, base.head) { 541 struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base); 542 543 ret = vmw_validation_bo_validate_single(entry->base.bo, intr); 544 545 if (ret) 546 return ret; 547 548 /* 549 * Rather than having the resource code allocating the bo 550 * dirty tracker in resource_unreserve() where we can't fail, 551 * Do it here when validating the buffer object. 552 */ 553 if (entry->coherent_count) { 554 unsigned int coherent_count = entry->coherent_count; 555 556 while (coherent_count) { 557 ret = vmw_bo_dirty_add(vbo); 558 if (ret) 559 return ret; 560 561 coherent_count--; 562 } 563 entry->coherent_count -= coherent_count; 564 } 565 566 if (vbo->dirty) 567 vmw_bo_dirty_scan(vbo); 568 } 569 return 0; 570 } 571 572 /** 573 * vmw_validation_res_validate - Validate all resources registered with the 574 * validation context. 575 * @ctx: The validation context. 576 * @intr: Whether to perform waits interruptible if possible. 577 * 578 * Before this function is called, all resource backup buffers must have 579 * been validated. 580 * 581 * Return: Zero on success, -ERESTARTSYS if interrupted, 582 * negative error code on failure. 583 */ 584 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr) 585 { 586 struct vmw_validation_res_node *val; 587 int ret; 588 589 list_for_each_entry(val, &ctx->resource_list, head) { 590 struct vmw_resource *res = val->res; 591 struct vmw_bo *backup = res->guest_memory_bo; 592 593 ret = vmw_resource_validate(res, intr, val->dirty_set && 594 val->dirty); 595 if (ret) { 596 if (ret != -ERESTARTSYS) 597 DRM_ERROR("Failed to validate resource.\n"); 598 return ret; 599 } 600 601 /* Check if the resource switched backup buffer */ 602 if (backup && res->guest_memory_bo && backup != res->guest_memory_bo) { 603 struct vmw_bo *vbo = res->guest_memory_bo; 604 605 vmw_bo_placement_set(vbo, res->func->domain, 606 res->func->busy_domain); 607 ret = vmw_validation_add_bo(ctx, vbo); 608 if (ret) 609 return ret; 610 } 611 } 612 return 0; 613 } 614 615 /** 616 * vmw_validation_drop_ht - Reset the hash table used for duplicate finding 617 * and unregister it from this validation context. 618 * @ctx: The validation context. 619 * 620 * The hash table used for duplicate finding is an expensive resource and 621 * may be protected by mutexes that may cause deadlocks during resource 622 * unreferencing if held. After resource- and buffer object registering, 623 * there is no longer any use for this hash table, so allow freeing it 624 * either to shorten any mutex locking time, or before resources- and 625 * buffer objects are freed during validation context cleanup. 626 */ 627 void vmw_validation_drop_ht(struct vmw_validation_context *ctx) 628 { 629 struct vmw_validation_bo_node *entry; 630 struct vmw_validation_res_node *val; 631 632 if (!ctx->sw_context) 633 return; 634 635 list_for_each_entry(entry, &ctx->bo_list, base.head) 636 hash_del_rcu(&entry->hash.head); 637 638 list_for_each_entry(val, &ctx->resource_list, head) 639 hash_del_rcu(&val->hash.head); 640 641 list_for_each_entry(val, &ctx->resource_ctx_list, head) 642 hash_del_rcu(&val->hash.head); 643 644 ctx->sw_context = NULL; 645 } 646 647 /** 648 * vmw_validation_unref_lists - Unregister previously registered buffer 649 * object and resources. 650 * @ctx: The validation context. 651 * 652 * Note that this function may cause buffer object- and resource destructors 653 * to be invoked. 654 */ 655 void vmw_validation_unref_lists(struct vmw_validation_context *ctx) 656 { 657 struct vmw_validation_bo_node *entry; 658 struct vmw_validation_res_node *val; 659 660 list_for_each_entry(entry, &ctx->bo_list, base.head) { 661 drm_gem_object_put(&entry->base.bo->base); 662 entry->base.bo = NULL; 663 } 664 665 list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list); 666 list_for_each_entry(val, &ctx->resource_list, head) 667 vmw_resource_unreference(&val->res); 668 669 /* 670 * No need to detach each list entry since they are all freed with 671 * vmw_validation_free_mem. Just make the inaccessible. 672 */ 673 INIT_LIST_HEAD(&ctx->bo_list); 674 INIT_LIST_HEAD(&ctx->resource_list); 675 676 vmw_validation_mem_free(ctx); 677 } 678 679 /** 680 * vmw_validation_prepare - Prepare a validation context for command 681 * submission. 682 * @ctx: The validation context. 683 * @mutex: The mutex used to protect resource reservation. 684 * @intr: Whether to perform waits interruptible if possible. 685 * 686 * Note that the single reservation mutex @mutex is an unfortunate 687 * construct. Ideally resource reservation should be moved to per-resource 688 * ww_mutexes. 689 * If this functions doesn't return Zero to indicate success, all resources 690 * are left unreserved but still referenced. 691 * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code 692 * on error. 693 */ 694 int vmw_validation_prepare(struct vmw_validation_context *ctx, 695 struct mutex *mutex, 696 bool intr) 697 { 698 int ret = 0; 699 700 if (mutex) { 701 if (intr) 702 ret = mutex_lock_interruptible(mutex); 703 else 704 mutex_lock(mutex); 705 if (ret) 706 return -ERESTARTSYS; 707 } 708 709 ctx->res_mutex = mutex; 710 ret = vmw_validation_res_reserve(ctx, intr); 711 if (ret) 712 goto out_no_res_reserve; 713 714 ret = vmw_validation_bo_reserve(ctx, intr); 715 if (ret) 716 goto out_no_bo_reserve; 717 718 ret = vmw_validation_bo_validate(ctx, intr); 719 if (ret) 720 goto out_no_validate; 721 722 ret = vmw_validation_res_validate(ctx, intr); 723 if (ret) 724 goto out_no_validate; 725 726 return 0; 727 728 out_no_validate: 729 vmw_validation_bo_backoff(ctx); 730 out_no_bo_reserve: 731 vmw_validation_res_unreserve(ctx, true); 732 out_no_res_reserve: 733 if (mutex) 734 mutex_unlock(mutex); 735 736 return ret; 737 } 738 739 /** 740 * vmw_validation_revert - Revert validation actions if command submission 741 * failed. 742 * 743 * @ctx: The validation context. 744 * 745 * The caller still needs to unref resources after a call to this function. 746 */ 747 void vmw_validation_revert(struct vmw_validation_context *ctx) 748 { 749 vmw_validation_bo_backoff(ctx); 750 vmw_validation_res_unreserve(ctx, true); 751 if (ctx->res_mutex) 752 mutex_unlock(ctx->res_mutex); 753 vmw_validation_unref_lists(ctx); 754 } 755 756 /** 757 * vmw_validation_done - Commit validation actions after command submission 758 * success. 759 * @ctx: The validation context. 760 * @fence: Fence with which to fence all buffer objects taking part in the 761 * command submission. 762 * 763 * The caller does NOT need to unref resources after a call to this function. 764 */ 765 void vmw_validation_done(struct vmw_validation_context *ctx, 766 struct vmw_fence_obj *fence) 767 { 768 vmw_validation_bo_fence(ctx, fence); 769 vmw_validation_res_unreserve(ctx, false); 770 if (ctx->res_mutex) 771 mutex_unlock(ctx->res_mutex); 772 vmw_validation_unref_lists(ctx); 773 } 774 775 /** 776 * vmw_validation_preload_bo - Preload the validation memory allocator for a 777 * call to vmw_validation_add_bo(). 778 * @ctx: Pointer to the validation context. 779 * 780 * Iff this function returns successfully, the next call to 781 * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal 782 * but voids the guarantee. 783 * 784 * Returns: Zero if successful, %-EINVAL otherwise. 785 */ 786 int vmw_validation_preload_bo(struct vmw_validation_context *ctx) 787 { 788 unsigned int size = sizeof(struct vmw_validation_bo_node); 789 790 if (!vmw_validation_mem_alloc(ctx, size)) 791 return -ENOMEM; 792 793 ctx->mem_size_left += size; 794 return 0; 795 } 796 797 /** 798 * vmw_validation_preload_res - Preload the validation memory allocator for a 799 * call to vmw_validation_add_res(). 800 * @ctx: Pointer to the validation context. 801 * @size: Size of the validation node extra data. See below. 802 * 803 * Iff this function returns successfully, the next call to 804 * vmw_validation_add_res() with the same or smaller @size is guaranteed not to 805 * sleep. An error is not fatal but voids the guarantee. 806 * 807 * Returns: Zero if successful, %-EINVAL otherwise. 808 */ 809 int vmw_validation_preload_res(struct vmw_validation_context *ctx, 810 unsigned int size) 811 { 812 size = vmw_validation_align(sizeof(struct vmw_validation_res_node) + 813 size) + 814 vmw_validation_align(sizeof(struct vmw_validation_bo_node)); 815 if (!vmw_validation_mem_alloc(ctx, size)) 816 return -ENOMEM; 817 818 ctx->mem_size_left += size; 819 return 0; 820 } 821 822 /** 823 * vmw_validation_bo_backoff - Unreserve buffer objects registered with a 824 * validation context 825 * @ctx: The validation context 826 * 827 * This function unreserves the buffer objects previously reserved using 828 * vmw_validation_bo_reserve. It's typically used as part of an error path 829 */ 830 void vmw_validation_bo_backoff(struct vmw_validation_context *ctx) 831 { 832 struct vmw_validation_bo_node *entry; 833 834 /* 835 * Switching coherent resource backup buffers failed. 836 * Release corresponding buffer object dirty trackers. 837 */ 838 list_for_each_entry(entry, &ctx->bo_list, base.head) { 839 if (entry->coherent_count) { 840 unsigned int coherent_count = entry->coherent_count; 841 struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base); 842 843 while (coherent_count--) 844 vmw_bo_dirty_release(vbo); 845 } 846 } 847 848 ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list); 849 } 850