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