1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright © 2011-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 29 #include <drm/ttm/ttm_placement.h> 30 31 #include "vmwgfx_drv.h" 32 #include "ttm_object.h" 33 34 35 /** 36 * struct vmw_user_buffer_object - User-space-visible buffer object 37 * 38 * @prime: The prime object providing user visibility. 39 * @vbo: The struct vmw_buffer_object 40 */ 41 struct vmw_user_buffer_object { 42 struct ttm_prime_object prime; 43 struct vmw_buffer_object vbo; 44 }; 45 46 47 /** 48 * vmw_buffer_object - Convert a struct ttm_buffer_object to a struct 49 * vmw_buffer_object. 50 * 51 * @bo: Pointer to the TTM buffer object. 52 * Return: Pointer to the struct vmw_buffer_object embedding the 53 * TTM buffer object. 54 */ 55 static struct vmw_buffer_object * 56 vmw_buffer_object(struct ttm_buffer_object *bo) 57 { 58 return container_of(bo, struct vmw_buffer_object, base); 59 } 60 61 62 /** 63 * vmw_user_buffer_object - Convert a struct ttm_buffer_object to a struct 64 * vmw_user_buffer_object. 65 * 66 * @bo: Pointer to the TTM buffer object. 67 * Return: Pointer to the struct vmw_buffer_object embedding the TTM buffer 68 * object. 69 */ 70 static struct vmw_user_buffer_object * 71 vmw_user_buffer_object(struct ttm_buffer_object *bo) 72 { 73 struct vmw_buffer_object *vmw_bo = vmw_buffer_object(bo); 74 75 return container_of(vmw_bo, struct vmw_user_buffer_object, vbo); 76 } 77 78 79 /** 80 * vmw_bo_pin_in_placement - Validate a buffer to placement. 81 * 82 * @dev_priv: Driver private. 83 * @buf: DMA buffer to move. 84 * @placement: The placement to pin it. 85 * @interruptible: Use interruptible wait. 86 * Return: Zero on success, Negative error code on failure. In particular 87 * -ERESTARTSYS if interrupted by a signal 88 */ 89 int vmw_bo_pin_in_placement(struct vmw_private *dev_priv, 90 struct vmw_buffer_object *buf, 91 struct ttm_placement *placement, 92 bool interruptible) 93 { 94 struct ttm_operation_ctx ctx = {interruptible, false }; 95 struct ttm_buffer_object *bo = &buf->base; 96 int ret; 97 uint32_t new_flags; 98 99 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible); 100 if (unlikely(ret != 0)) 101 return ret; 102 103 vmw_execbuf_release_pinned_bo(dev_priv); 104 105 ret = ttm_bo_reserve(bo, interruptible, false, NULL); 106 if (unlikely(ret != 0)) 107 goto err; 108 109 if (buf->pin_count > 0) 110 ret = ttm_bo_mem_compat(placement, &bo->mem, 111 &new_flags) == true ? 0 : -EINVAL; 112 else 113 ret = ttm_bo_validate(bo, placement, &ctx); 114 115 if (!ret) 116 vmw_bo_pin_reserved(buf, true); 117 118 ttm_bo_unreserve(bo); 119 120 err: 121 ttm_write_unlock(&dev_priv->reservation_sem); 122 return ret; 123 } 124 125 126 /** 127 * vmw_bo_pin_in_vram_or_gmr - Move a buffer to vram or gmr. 128 * 129 * This function takes the reservation_sem in write mode. 130 * Flushes and unpins the query bo to avoid failures. 131 * 132 * @dev_priv: Driver private. 133 * @buf: DMA buffer to move. 134 * @pin: Pin buffer if true. 135 * @interruptible: Use interruptible wait. 136 * Return: Zero on success, Negative error code on failure. In particular 137 * -ERESTARTSYS if interrupted by a signal 138 */ 139 int vmw_bo_pin_in_vram_or_gmr(struct vmw_private *dev_priv, 140 struct vmw_buffer_object *buf, 141 bool interruptible) 142 { 143 struct ttm_operation_ctx ctx = {interruptible, false }; 144 struct ttm_buffer_object *bo = &buf->base; 145 int ret; 146 uint32_t new_flags; 147 148 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible); 149 if (unlikely(ret != 0)) 150 return ret; 151 152 vmw_execbuf_release_pinned_bo(dev_priv); 153 154 ret = ttm_bo_reserve(bo, interruptible, false, NULL); 155 if (unlikely(ret != 0)) 156 goto err; 157 158 if (buf->pin_count > 0) { 159 ret = ttm_bo_mem_compat(&vmw_vram_gmr_placement, &bo->mem, 160 &new_flags) == true ? 0 : -EINVAL; 161 goto out_unreserve; 162 } 163 164 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx); 165 if (likely(ret == 0) || ret == -ERESTARTSYS) 166 goto out_unreserve; 167 168 ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx); 169 170 out_unreserve: 171 if (!ret) 172 vmw_bo_pin_reserved(buf, true); 173 174 ttm_bo_unreserve(bo); 175 err: 176 ttm_write_unlock(&dev_priv->reservation_sem); 177 return ret; 178 } 179 180 181 /** 182 * vmw_bo_pin_in_vram - Move a buffer to vram. 183 * 184 * This function takes the reservation_sem in write mode. 185 * Flushes and unpins the query bo to avoid failures. 186 * 187 * @dev_priv: Driver private. 188 * @buf: DMA buffer to move. 189 * @interruptible: Use interruptible wait. 190 * Return: Zero on success, Negative error code on failure. In particular 191 * -ERESTARTSYS if interrupted by a signal 192 */ 193 int vmw_bo_pin_in_vram(struct vmw_private *dev_priv, 194 struct vmw_buffer_object *buf, 195 bool interruptible) 196 { 197 return vmw_bo_pin_in_placement(dev_priv, buf, &vmw_vram_placement, 198 interruptible); 199 } 200 201 202 /** 203 * vmw_bo_pin_in_start_of_vram - Move a buffer to start of vram. 204 * 205 * This function takes the reservation_sem in write mode. 206 * Flushes and unpins the query bo to avoid failures. 207 * 208 * @dev_priv: Driver private. 209 * @buf: DMA buffer to pin. 210 * @interruptible: Use interruptible wait. 211 * Return: Zero on success, Negative error code on failure. In particular 212 * -ERESTARTSYS if interrupted by a signal 213 */ 214 int vmw_bo_pin_in_start_of_vram(struct vmw_private *dev_priv, 215 struct vmw_buffer_object *buf, 216 bool interruptible) 217 { 218 struct ttm_operation_ctx ctx = {interruptible, false }; 219 struct ttm_buffer_object *bo = &buf->base; 220 struct ttm_placement placement; 221 struct ttm_place place; 222 int ret = 0; 223 uint32_t new_flags; 224 225 place = vmw_vram_placement.placement[0]; 226 place.lpfn = bo->num_pages; 227 placement.num_placement = 1; 228 placement.placement = &place; 229 placement.num_busy_placement = 1; 230 placement.busy_placement = &place; 231 232 ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible); 233 if (unlikely(ret != 0)) 234 return ret; 235 236 vmw_execbuf_release_pinned_bo(dev_priv); 237 ret = ttm_bo_reserve(bo, interruptible, false, NULL); 238 if (unlikely(ret != 0)) 239 goto err_unlock; 240 241 /* 242 * Is this buffer already in vram but not at the start of it? 243 * In that case, evict it first because TTM isn't good at handling 244 * that situation. 245 */ 246 if (bo->mem.mem_type == TTM_PL_VRAM && 247 bo->mem.start < bo->num_pages && 248 bo->mem.start > 0 && 249 buf->pin_count == 0) { 250 ctx.interruptible = false; 251 (void) ttm_bo_validate(bo, &vmw_sys_placement, &ctx); 252 } 253 254 if (buf->pin_count > 0) 255 ret = ttm_bo_mem_compat(&placement, &bo->mem, 256 &new_flags) == true ? 0 : -EINVAL; 257 else 258 ret = ttm_bo_validate(bo, &placement, &ctx); 259 260 /* For some reason we didn't end up at the start of vram */ 261 WARN_ON(ret == 0 && bo->offset != 0); 262 if (!ret) 263 vmw_bo_pin_reserved(buf, true); 264 265 ttm_bo_unreserve(bo); 266 err_unlock: 267 ttm_write_unlock(&dev_priv->reservation_sem); 268 269 return ret; 270 } 271 272 273 /** 274 * vmw_bo_unpin - Unpin the buffer given buffer, does not move the buffer. 275 * 276 * This function takes the reservation_sem in write mode. 277 * 278 * @dev_priv: Driver private. 279 * @buf: DMA buffer to unpin. 280 * @interruptible: Use interruptible wait. 281 * Return: Zero on success, Negative error code on failure. In particular 282 * -ERESTARTSYS if interrupted by a signal 283 */ 284 int vmw_bo_unpin(struct vmw_private *dev_priv, 285 struct vmw_buffer_object *buf, 286 bool interruptible) 287 { 288 struct ttm_buffer_object *bo = &buf->base; 289 int ret; 290 291 ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible); 292 if (unlikely(ret != 0)) 293 return ret; 294 295 ret = ttm_bo_reserve(bo, interruptible, false, NULL); 296 if (unlikely(ret != 0)) 297 goto err; 298 299 vmw_bo_pin_reserved(buf, false); 300 301 ttm_bo_unreserve(bo); 302 303 err: 304 ttm_read_unlock(&dev_priv->reservation_sem); 305 return ret; 306 } 307 308 /** 309 * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement 310 * of a buffer. 311 * 312 * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved. 313 * @ptr: SVGAGuestPtr returning the result. 314 */ 315 void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo, 316 SVGAGuestPtr *ptr) 317 { 318 if (bo->mem.mem_type == TTM_PL_VRAM) { 319 ptr->gmrId = SVGA_GMR_FRAMEBUFFER; 320 ptr->offset = bo->offset; 321 } else { 322 ptr->gmrId = bo->mem.start; 323 ptr->offset = 0; 324 } 325 } 326 327 328 /** 329 * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it. 330 * 331 * @vbo: The buffer object. Must be reserved. 332 * @pin: Whether to pin or unpin. 333 * 334 */ 335 void vmw_bo_pin_reserved(struct vmw_buffer_object *vbo, bool pin) 336 { 337 struct ttm_operation_ctx ctx = { false, true }; 338 struct ttm_place pl; 339 struct ttm_placement placement; 340 struct ttm_buffer_object *bo = &vbo->base; 341 uint32_t old_mem_type = bo->mem.mem_type; 342 int ret; 343 344 dma_resv_assert_held(bo->base.resv); 345 346 if (pin) { 347 if (vbo->pin_count++ > 0) 348 return; 349 } else { 350 WARN_ON(vbo->pin_count <= 0); 351 if (--vbo->pin_count > 0) 352 return; 353 } 354 355 pl.fpfn = 0; 356 pl.lpfn = 0; 357 pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB 358 | TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; 359 if (pin) 360 pl.flags |= TTM_PL_FLAG_NO_EVICT; 361 362 memset(&placement, 0, sizeof(placement)); 363 placement.num_placement = 1; 364 placement.placement = &pl; 365 366 ret = ttm_bo_validate(bo, &placement, &ctx); 367 368 BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type); 369 } 370 371 372 /** 373 * vmw_bo_map_and_cache - Map a buffer object and cache the map 374 * 375 * @vbo: The buffer object to map 376 * Return: A kernel virtual address or NULL if mapping failed. 377 * 378 * This function maps a buffer object into the kernel address space, or 379 * returns the virtual kernel address of an already existing map. The virtual 380 * address remains valid as long as the buffer object is pinned or reserved. 381 * The cached map is torn down on either 382 * 1) Buffer object move 383 * 2) Buffer object swapout 384 * 3) Buffer object destruction 385 * 386 */ 387 void *vmw_bo_map_and_cache(struct vmw_buffer_object *vbo) 388 { 389 struct ttm_buffer_object *bo = &vbo->base; 390 bool not_used; 391 void *virtual; 392 int ret; 393 394 virtual = ttm_kmap_obj_virtual(&vbo->map, ¬_used); 395 if (virtual) 396 return virtual; 397 398 ret = ttm_bo_kmap(bo, 0, bo->num_pages, &vbo->map); 399 if (ret) 400 DRM_ERROR("Buffer object map failed: %d.\n", ret); 401 402 return ttm_kmap_obj_virtual(&vbo->map, ¬_used); 403 } 404 405 406 /** 407 * vmw_bo_unmap - Tear down a cached buffer object map. 408 * 409 * @vbo: The buffer object whose map we are tearing down. 410 * 411 * This function tears down a cached map set up using 412 * vmw_buffer_object_map_and_cache(). 413 */ 414 void vmw_bo_unmap(struct vmw_buffer_object *vbo) 415 { 416 if (vbo->map.bo == NULL) 417 return; 418 419 ttm_bo_kunmap(&vbo->map); 420 } 421 422 423 /** 424 * vmw_bo_acc_size - Calculate the pinned memory usage of buffers 425 * 426 * @dev_priv: Pointer to a struct vmw_private identifying the device. 427 * @size: The requested buffer size. 428 * @user: Whether this is an ordinary dma buffer or a user dma buffer. 429 */ 430 static size_t vmw_bo_acc_size(struct vmw_private *dev_priv, size_t size, 431 bool user) 432 { 433 static size_t struct_size, user_struct_size; 434 size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; 435 size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *)); 436 437 if (unlikely(struct_size == 0)) { 438 size_t backend_size = ttm_round_pot(vmw_tt_size); 439 440 struct_size = backend_size + 441 ttm_round_pot(sizeof(struct vmw_buffer_object)); 442 user_struct_size = backend_size + 443 ttm_round_pot(sizeof(struct vmw_user_buffer_object)) + 444 TTM_OBJ_EXTRA_SIZE; 445 } 446 447 if (dev_priv->map_mode == vmw_dma_alloc_coherent) 448 page_array_size += 449 ttm_round_pot(num_pages * sizeof(dma_addr_t)); 450 451 return ((user) ? user_struct_size : struct_size) + 452 page_array_size; 453 } 454 455 456 /** 457 * vmw_bo_bo_free - vmw buffer object destructor 458 * 459 * @bo: Pointer to the embedded struct ttm_buffer_object 460 */ 461 void vmw_bo_bo_free(struct ttm_buffer_object *bo) 462 { 463 struct vmw_buffer_object *vmw_bo = vmw_buffer_object(bo); 464 465 vmw_bo_unmap(vmw_bo); 466 kfree(vmw_bo); 467 } 468 469 470 /** 471 * vmw_user_bo_destroy - vmw buffer object destructor 472 * 473 * @bo: Pointer to the embedded struct ttm_buffer_object 474 */ 475 static void vmw_user_bo_destroy(struct ttm_buffer_object *bo) 476 { 477 struct vmw_user_buffer_object *vmw_user_bo = vmw_user_buffer_object(bo); 478 479 vmw_bo_unmap(&vmw_user_bo->vbo); 480 ttm_prime_object_kfree(vmw_user_bo, prime); 481 } 482 483 484 /** 485 * vmw_bo_init - Initialize a vmw buffer object 486 * 487 * @dev_priv: Pointer to the device private struct 488 * @vmw_bo: Pointer to the struct vmw_buffer_object to initialize. 489 * @size: Buffer object size in bytes. 490 * @placement: Initial placement. 491 * @interruptible: Whether waits should be performed interruptible. 492 * @bo_free: The buffer object destructor. 493 * Returns: Zero on success, negative error code on error. 494 * 495 * Note that on error, the code will free the buffer object. 496 */ 497 int vmw_bo_init(struct vmw_private *dev_priv, 498 struct vmw_buffer_object *vmw_bo, 499 size_t size, struct ttm_placement *placement, 500 bool interruptible, 501 void (*bo_free)(struct ttm_buffer_object *bo)) 502 { 503 struct ttm_bo_device *bdev = &dev_priv->bdev; 504 size_t acc_size; 505 int ret; 506 bool user = (bo_free == &vmw_user_bo_destroy); 507 508 WARN_ON_ONCE(!bo_free && (!user && (bo_free != vmw_bo_bo_free))); 509 510 acc_size = vmw_bo_acc_size(dev_priv, size, user); 511 memset(vmw_bo, 0, sizeof(*vmw_bo)); 512 BUILD_BUG_ON(TTM_MAX_BO_PRIORITY <= 3); 513 vmw_bo->base.priority = 3; 514 515 INIT_LIST_HEAD(&vmw_bo->res_list); 516 517 ret = ttm_bo_init(bdev, &vmw_bo->base, size, 518 ttm_bo_type_device, placement, 519 0, interruptible, acc_size, 520 NULL, NULL, bo_free); 521 return ret; 522 } 523 524 525 /** 526 * vmw_user_bo_release - TTM reference base object release callback for 527 * vmw user buffer objects 528 * 529 * @p_base: The TTM base object pointer about to be unreferenced. 530 * 531 * Clears the TTM base object pointer and drops the reference the 532 * base object has on the underlying struct vmw_buffer_object. 533 */ 534 static void vmw_user_bo_release(struct ttm_base_object **p_base) 535 { 536 struct vmw_user_buffer_object *vmw_user_bo; 537 struct ttm_base_object *base = *p_base; 538 539 *p_base = NULL; 540 541 if (unlikely(base == NULL)) 542 return; 543 544 vmw_user_bo = container_of(base, struct vmw_user_buffer_object, 545 prime.base); 546 ttm_bo_put(&vmw_user_bo->vbo.base); 547 } 548 549 550 /** 551 * vmw_user_bo_ref_obj-release - TTM synccpu reference object release callback 552 * for vmw user buffer objects 553 * 554 * @base: Pointer to the TTM base object 555 * @ref_type: Reference type of the reference reaching zero. 556 * 557 * Called when user-space drops its last synccpu reference on the buffer 558 * object, Either explicitly or as part of a cleanup file close. 559 */ 560 static void vmw_user_bo_ref_obj_release(struct ttm_base_object *base, 561 enum ttm_ref_type ref_type) 562 { 563 struct vmw_user_buffer_object *user_bo; 564 565 user_bo = container_of(base, struct vmw_user_buffer_object, prime.base); 566 567 switch (ref_type) { 568 case TTM_REF_SYNCCPU_WRITE: 569 atomic_dec(&user_bo->vbo.cpu_writers); 570 break; 571 default: 572 WARN_ONCE(true, "Undefined buffer object reference release.\n"); 573 } 574 } 575 576 577 /** 578 * vmw_user_bo_alloc - Allocate a user buffer object 579 * 580 * @dev_priv: Pointer to a struct device private. 581 * @tfile: Pointer to a struct ttm_object_file on which to register the user 582 * object. 583 * @size: Size of the buffer object. 584 * @shareable: Boolean whether the buffer is shareable with other open files. 585 * @handle: Pointer to where the handle value should be assigned. 586 * @p_vbo: Pointer to where the refcounted struct vmw_buffer_object pointer 587 * should be assigned. 588 * Return: Zero on success, negative error code on error. 589 */ 590 int vmw_user_bo_alloc(struct vmw_private *dev_priv, 591 struct ttm_object_file *tfile, 592 uint32_t size, 593 bool shareable, 594 uint32_t *handle, 595 struct vmw_buffer_object **p_vbo, 596 struct ttm_base_object **p_base) 597 { 598 struct vmw_user_buffer_object *user_bo; 599 int ret; 600 601 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL); 602 if (unlikely(!user_bo)) { 603 DRM_ERROR("Failed to allocate a buffer.\n"); 604 return -ENOMEM; 605 } 606 607 ret = vmw_bo_init(dev_priv, &user_bo->vbo, size, 608 (dev_priv->has_mob) ? 609 &vmw_sys_placement : 610 &vmw_vram_sys_placement, true, 611 &vmw_user_bo_destroy); 612 if (unlikely(ret != 0)) 613 return ret; 614 615 ttm_bo_get(&user_bo->vbo.base); 616 ret = ttm_prime_object_init(tfile, 617 size, 618 &user_bo->prime, 619 shareable, 620 ttm_buffer_type, 621 &vmw_user_bo_release, 622 &vmw_user_bo_ref_obj_release); 623 if (unlikely(ret != 0)) { 624 ttm_bo_put(&user_bo->vbo.base); 625 goto out_no_base_object; 626 } 627 628 *p_vbo = &user_bo->vbo; 629 if (p_base) { 630 *p_base = &user_bo->prime.base; 631 kref_get(&(*p_base)->refcount); 632 } 633 *handle = user_bo->prime.base.handle; 634 635 out_no_base_object: 636 return ret; 637 } 638 639 640 /** 641 * vmw_user_bo_verify_access - verify access permissions on this 642 * buffer object. 643 * 644 * @bo: Pointer to the buffer object being accessed 645 * @tfile: Identifying the caller. 646 */ 647 int vmw_user_bo_verify_access(struct ttm_buffer_object *bo, 648 struct ttm_object_file *tfile) 649 { 650 struct vmw_user_buffer_object *vmw_user_bo; 651 652 if (unlikely(bo->destroy != vmw_user_bo_destroy)) 653 return -EPERM; 654 655 vmw_user_bo = vmw_user_buffer_object(bo); 656 657 /* Check that the caller has opened the object. */ 658 if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base))) 659 return 0; 660 661 DRM_ERROR("Could not grant buffer access.\n"); 662 return -EPERM; 663 } 664 665 666 /** 667 * vmw_user_bo_synccpu_grab - Grab a struct vmw_user_buffer_object for cpu 668 * access, idling previous GPU operations on the buffer and optionally 669 * blocking it for further command submissions. 670 * 671 * @user_bo: Pointer to the buffer object being grabbed for CPU access 672 * @tfile: Identifying the caller. 673 * @flags: Flags indicating how the grab should be performed. 674 * Return: Zero on success, Negative error code on error. In particular, 675 * -EBUSY will be returned if a dontblock operation is requested and the 676 * buffer object is busy, and -ERESTARTSYS will be returned if a wait is 677 * interrupted by a signal. 678 * 679 * A blocking grab will be automatically released when @tfile is closed. 680 */ 681 static int vmw_user_bo_synccpu_grab(struct vmw_user_buffer_object *user_bo, 682 struct ttm_object_file *tfile, 683 uint32_t flags) 684 { 685 bool nonblock = !!(flags & drm_vmw_synccpu_dontblock); 686 struct ttm_buffer_object *bo = &user_bo->vbo.base; 687 bool existed; 688 int ret; 689 690 if (flags & drm_vmw_synccpu_allow_cs) { 691 long lret; 692 693 lret = dma_resv_wait_timeout_rcu 694 (bo->base.resv, true, true, 695 nonblock ? 0 : MAX_SCHEDULE_TIMEOUT); 696 if (!lret) 697 return -EBUSY; 698 else if (lret < 0) 699 return lret; 700 return 0; 701 } 702 703 ret = ttm_bo_reserve(bo, true, nonblock, NULL); 704 if (unlikely(ret != 0)) 705 return ret; 706 707 ret = ttm_bo_wait(bo, true, nonblock); 708 if (likely(ret == 0)) 709 atomic_inc(&user_bo->vbo.cpu_writers); 710 711 ttm_bo_unreserve(bo); 712 if (unlikely(ret != 0)) 713 return ret; 714 715 ret = ttm_ref_object_add(tfile, &user_bo->prime.base, 716 TTM_REF_SYNCCPU_WRITE, &existed, false); 717 if (ret != 0 || existed) 718 atomic_dec(&user_bo->vbo.cpu_writers); 719 720 return ret; 721 } 722 723 /** 724 * vmw_user_bo_synccpu_release - Release a previous grab for CPU access, 725 * and unblock command submission on the buffer if blocked. 726 * 727 * @handle: Handle identifying the buffer object. 728 * @tfile: Identifying the caller. 729 * @flags: Flags indicating the type of release. 730 */ 731 static int vmw_user_bo_synccpu_release(uint32_t handle, 732 struct ttm_object_file *tfile, 733 uint32_t flags) 734 { 735 if (!(flags & drm_vmw_synccpu_allow_cs)) 736 return ttm_ref_object_base_unref(tfile, handle, 737 TTM_REF_SYNCCPU_WRITE); 738 739 return 0; 740 } 741 742 743 /** 744 * vmw_user_bo_synccpu_ioctl - ioctl function implementing the synccpu 745 * functionality. 746 * 747 * @dev: Identifies the drm device. 748 * @data: Pointer to the ioctl argument. 749 * @file_priv: Identifies the caller. 750 * Return: Zero on success, negative error code on error. 751 * 752 * This function checks the ioctl arguments for validity and calls the 753 * relevant synccpu functions. 754 */ 755 int vmw_user_bo_synccpu_ioctl(struct drm_device *dev, void *data, 756 struct drm_file *file_priv) 757 { 758 struct drm_vmw_synccpu_arg *arg = 759 (struct drm_vmw_synccpu_arg *) data; 760 struct vmw_buffer_object *vbo; 761 struct vmw_user_buffer_object *user_bo; 762 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 763 struct ttm_base_object *buffer_base; 764 int ret; 765 766 if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0 767 || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write | 768 drm_vmw_synccpu_dontblock | 769 drm_vmw_synccpu_allow_cs)) != 0) { 770 DRM_ERROR("Illegal synccpu flags.\n"); 771 return -EINVAL; 772 } 773 774 switch (arg->op) { 775 case drm_vmw_synccpu_grab: 776 ret = vmw_user_bo_lookup(tfile, arg->handle, &vbo, 777 &buffer_base); 778 if (unlikely(ret != 0)) 779 return ret; 780 781 user_bo = container_of(vbo, struct vmw_user_buffer_object, 782 vbo); 783 ret = vmw_user_bo_synccpu_grab(user_bo, tfile, arg->flags); 784 vmw_bo_unreference(&vbo); 785 ttm_base_object_unref(&buffer_base); 786 if (unlikely(ret != 0 && ret != -ERESTARTSYS && 787 ret != -EBUSY)) { 788 DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n", 789 (unsigned int) arg->handle); 790 return ret; 791 } 792 break; 793 case drm_vmw_synccpu_release: 794 ret = vmw_user_bo_synccpu_release(arg->handle, tfile, 795 arg->flags); 796 if (unlikely(ret != 0)) { 797 DRM_ERROR("Failed synccpu release on handle 0x%08x.\n", 798 (unsigned int) arg->handle); 799 return ret; 800 } 801 break; 802 default: 803 DRM_ERROR("Invalid synccpu operation.\n"); 804 return -EINVAL; 805 } 806 807 return 0; 808 } 809 810 811 /** 812 * vmw_bo_alloc_ioctl - ioctl function implementing the buffer object 813 * allocation functionality. 814 * 815 * @dev: Identifies the drm device. 816 * @data: Pointer to the ioctl argument. 817 * @file_priv: Identifies the caller. 818 * Return: Zero on success, negative error code on error. 819 * 820 * This function checks the ioctl arguments for validity and allocates a 821 * struct vmw_user_buffer_object bo. 822 */ 823 int vmw_bo_alloc_ioctl(struct drm_device *dev, void *data, 824 struct drm_file *file_priv) 825 { 826 struct vmw_private *dev_priv = vmw_priv(dev); 827 union drm_vmw_alloc_dmabuf_arg *arg = 828 (union drm_vmw_alloc_dmabuf_arg *)data; 829 struct drm_vmw_alloc_dmabuf_req *req = &arg->req; 830 struct drm_vmw_dmabuf_rep *rep = &arg->rep; 831 struct vmw_buffer_object *vbo; 832 uint32_t handle; 833 int ret; 834 835 ret = ttm_read_lock(&dev_priv->reservation_sem, true); 836 if (unlikely(ret != 0)) 837 return ret; 838 839 ret = vmw_user_bo_alloc(dev_priv, vmw_fpriv(file_priv)->tfile, 840 req->size, false, &handle, &vbo, 841 NULL); 842 if (unlikely(ret != 0)) 843 goto out_no_bo; 844 845 rep->handle = handle; 846 rep->map_handle = drm_vma_node_offset_addr(&vbo->base.base.vma_node); 847 rep->cur_gmr_id = handle; 848 rep->cur_gmr_offset = 0; 849 850 vmw_bo_unreference(&vbo); 851 852 out_no_bo: 853 ttm_read_unlock(&dev_priv->reservation_sem); 854 855 return ret; 856 } 857 858 859 /** 860 * vmw_bo_unref_ioctl - Generic handle close ioctl. 861 * 862 * @dev: Identifies the drm device. 863 * @data: Pointer to the ioctl argument. 864 * @file_priv: Identifies the caller. 865 * Return: Zero on success, negative error code on error. 866 * 867 * This function checks the ioctl arguments for validity and closes a 868 * handle to a TTM base object, optionally freeing the object. 869 */ 870 int vmw_bo_unref_ioctl(struct drm_device *dev, void *data, 871 struct drm_file *file_priv) 872 { 873 struct drm_vmw_unref_dmabuf_arg *arg = 874 (struct drm_vmw_unref_dmabuf_arg *)data; 875 876 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, 877 arg->handle, 878 TTM_REF_USAGE); 879 } 880 881 882 /** 883 * vmw_user_bo_lookup - Look up a vmw user buffer object from a handle. 884 * 885 * @tfile: The TTM object file the handle is registered with. 886 * @handle: The user buffer object handle 887 * @out: Pointer to a where a pointer to the embedded 888 * struct vmw_buffer_object should be placed. 889 * @p_base: Pointer to where a pointer to the TTM base object should be 890 * placed, or NULL if no such pointer is required. 891 * Return: Zero on success, Negative error code on error. 892 * 893 * Both the output base object pointer and the vmw buffer object pointer 894 * will be refcounted. 895 */ 896 int vmw_user_bo_lookup(struct ttm_object_file *tfile, 897 uint32_t handle, struct vmw_buffer_object **out, 898 struct ttm_base_object **p_base) 899 { 900 struct vmw_user_buffer_object *vmw_user_bo; 901 struct ttm_base_object *base; 902 903 base = ttm_base_object_lookup(tfile, handle); 904 if (unlikely(base == NULL)) { 905 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n", 906 (unsigned long)handle); 907 return -ESRCH; 908 } 909 910 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) { 911 ttm_base_object_unref(&base); 912 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n", 913 (unsigned long)handle); 914 return -EINVAL; 915 } 916 917 vmw_user_bo = container_of(base, struct vmw_user_buffer_object, 918 prime.base); 919 ttm_bo_get(&vmw_user_bo->vbo.base); 920 if (p_base) 921 *p_base = base; 922 else 923 ttm_base_object_unref(&base); 924 *out = &vmw_user_bo->vbo; 925 926 return 0; 927 } 928 929 /** 930 * vmw_user_bo_noref_lookup - Look up a vmw user buffer object without reference 931 * @tfile: The TTM object file the handle is registered with. 932 * @handle: The user buffer object handle. 933 * 934 * This function looks up a struct vmw_user_bo and returns a pointer to the 935 * struct vmw_buffer_object it derives from without refcounting the pointer. 936 * The returned pointer is only valid until vmw_user_bo_noref_release() is 937 * called, and the object pointed to by the returned pointer may be doomed. 938 * Any persistent usage of the object requires a refcount to be taken using 939 * ttm_bo_reference_unless_doomed(). Iff this function returns successfully it 940 * needs to be paired with vmw_user_bo_noref_release() and no sleeping- 941 * or scheduling functions may be called inbetween these function calls. 942 * 943 * Return: A struct vmw_buffer_object pointer if successful or negative 944 * error pointer on failure. 945 */ 946 struct vmw_buffer_object * 947 vmw_user_bo_noref_lookup(struct ttm_object_file *tfile, u32 handle) 948 { 949 struct vmw_user_buffer_object *vmw_user_bo; 950 struct ttm_base_object *base; 951 952 base = ttm_base_object_noref_lookup(tfile, handle); 953 if (!base) { 954 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n", 955 (unsigned long)handle); 956 return ERR_PTR(-ESRCH); 957 } 958 959 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) { 960 ttm_base_object_noref_release(); 961 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n", 962 (unsigned long)handle); 963 return ERR_PTR(-EINVAL); 964 } 965 966 vmw_user_bo = container_of(base, struct vmw_user_buffer_object, 967 prime.base); 968 return &vmw_user_bo->vbo; 969 } 970 971 /** 972 * vmw_user_bo_reference - Open a handle to a vmw user buffer object. 973 * 974 * @tfile: The TTM object file to register the handle with. 975 * @vbo: The embedded vmw buffer object. 976 * @handle: Pointer to where the new handle should be placed. 977 * Return: Zero on success, Negative error code on error. 978 */ 979 int vmw_user_bo_reference(struct ttm_object_file *tfile, 980 struct vmw_buffer_object *vbo, 981 uint32_t *handle) 982 { 983 struct vmw_user_buffer_object *user_bo; 984 985 if (vbo->base.destroy != vmw_user_bo_destroy) 986 return -EINVAL; 987 988 user_bo = container_of(vbo, struct vmw_user_buffer_object, vbo); 989 990 *handle = user_bo->prime.base.handle; 991 return ttm_ref_object_add(tfile, &user_bo->prime.base, 992 TTM_REF_USAGE, NULL, false); 993 } 994 995 996 /** 997 * vmw_bo_fence_single - Utility function to fence a single TTM buffer 998 * object without unreserving it. 999 * 1000 * @bo: Pointer to the struct ttm_buffer_object to fence. 1001 * @fence: Pointer to the fence. If NULL, this function will 1002 * insert a fence into the command stream.. 1003 * 1004 * Contrary to the ttm_eu version of this function, it takes only 1005 * a single buffer object instead of a list, and it also doesn't 1006 * unreserve the buffer object, which needs to be done separately. 1007 */ 1008 void vmw_bo_fence_single(struct ttm_buffer_object *bo, 1009 struct vmw_fence_obj *fence) 1010 { 1011 struct ttm_bo_device *bdev = bo->bdev; 1012 1013 struct vmw_private *dev_priv = 1014 container_of(bdev, struct vmw_private, bdev); 1015 1016 if (fence == NULL) { 1017 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL); 1018 dma_resv_add_excl_fence(bo->base.resv, &fence->base); 1019 dma_fence_put(&fence->base); 1020 } else 1021 dma_resv_add_excl_fence(bo->base.resv, &fence->base); 1022 } 1023 1024 1025 /** 1026 * vmw_dumb_create - Create a dumb kms buffer 1027 * 1028 * @file_priv: Pointer to a struct drm_file identifying the caller. 1029 * @dev: Pointer to the drm device. 1030 * @args: Pointer to a struct drm_mode_create_dumb structure 1031 * Return: Zero on success, negative error code on failure. 1032 * 1033 * This is a driver callback for the core drm create_dumb functionality. 1034 * Note that this is very similar to the vmw_bo_alloc ioctl, except 1035 * that the arguments have a different format. 1036 */ 1037 int vmw_dumb_create(struct drm_file *file_priv, 1038 struct drm_device *dev, 1039 struct drm_mode_create_dumb *args) 1040 { 1041 struct vmw_private *dev_priv = vmw_priv(dev); 1042 struct vmw_buffer_object *vbo; 1043 int ret; 1044 1045 args->pitch = args->width * ((args->bpp + 7) / 8); 1046 args->size = args->pitch * args->height; 1047 1048 ret = ttm_read_lock(&dev_priv->reservation_sem, true); 1049 if (unlikely(ret != 0)) 1050 return ret; 1051 1052 ret = vmw_user_bo_alloc(dev_priv, vmw_fpriv(file_priv)->tfile, 1053 args->size, false, &args->handle, 1054 &vbo, NULL); 1055 if (unlikely(ret != 0)) 1056 goto out_no_bo; 1057 1058 vmw_bo_unreference(&vbo); 1059 out_no_bo: 1060 ttm_read_unlock(&dev_priv->reservation_sem); 1061 return ret; 1062 } 1063 1064 1065 /** 1066 * vmw_dumb_map_offset - Return the address space offset of a dumb buffer 1067 * 1068 * @file_priv: Pointer to a struct drm_file identifying the caller. 1069 * @dev: Pointer to the drm device. 1070 * @handle: Handle identifying the dumb buffer. 1071 * @offset: The address space offset returned. 1072 * Return: Zero on success, negative error code on failure. 1073 * 1074 * This is a driver callback for the core drm dumb_map_offset functionality. 1075 */ 1076 int vmw_dumb_map_offset(struct drm_file *file_priv, 1077 struct drm_device *dev, uint32_t handle, 1078 uint64_t *offset) 1079 { 1080 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 1081 struct vmw_buffer_object *out_buf; 1082 int ret; 1083 1084 ret = vmw_user_bo_lookup(tfile, handle, &out_buf, NULL); 1085 if (ret != 0) 1086 return -EINVAL; 1087 1088 *offset = drm_vma_node_offset_addr(&out_buf->base.base.vma_node); 1089 vmw_bo_unreference(&out_buf); 1090 return 0; 1091 } 1092 1093 1094 /** 1095 * vmw_dumb_destroy - Destroy a dumb boffer 1096 * 1097 * @file_priv: Pointer to a struct drm_file identifying the caller. 1098 * @dev: Pointer to the drm device. 1099 * @handle: Handle identifying the dumb buffer. 1100 * Return: Zero on success, negative error code on failure. 1101 * 1102 * This is a driver callback for the core drm dumb_destroy functionality. 1103 */ 1104 int vmw_dumb_destroy(struct drm_file *file_priv, 1105 struct drm_device *dev, 1106 uint32_t handle) 1107 { 1108 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, 1109 handle, TTM_REF_USAGE); 1110 } 1111 1112 1113 /** 1114 * vmw_bo_swap_notify - swapout notify callback. 1115 * 1116 * @bo: The buffer object to be swapped out. 1117 */ 1118 void vmw_bo_swap_notify(struct ttm_buffer_object *bo) 1119 { 1120 /* Is @bo embedded in a struct vmw_buffer_object? */ 1121 if (bo->destroy != vmw_bo_bo_free && 1122 bo->destroy != vmw_user_bo_destroy) 1123 return; 1124 1125 /* Kill any cached kernel maps before swapout */ 1126 vmw_bo_unmap(vmw_buffer_object(bo)); 1127 } 1128 1129 1130 /** 1131 * vmw_bo_move_notify - TTM move_notify_callback 1132 * 1133 * @bo: The TTM buffer object about to move. 1134 * @mem: The struct ttm_mem_reg indicating to what memory 1135 * region the move is taking place. 1136 * 1137 * Detaches cached maps and device bindings that require that the 1138 * buffer doesn't move. 1139 */ 1140 void vmw_bo_move_notify(struct ttm_buffer_object *bo, 1141 struct ttm_mem_reg *mem) 1142 { 1143 struct vmw_buffer_object *vbo; 1144 1145 if (mem == NULL) 1146 return; 1147 1148 /* Make sure @bo is embedded in a struct vmw_buffer_object? */ 1149 if (bo->destroy != vmw_bo_bo_free && 1150 bo->destroy != vmw_user_bo_destroy) 1151 return; 1152 1153 vbo = container_of(bo, struct vmw_buffer_object, base); 1154 1155 /* 1156 * Kill any cached kernel maps before move to or from VRAM. 1157 * With other types of moves, the underlying pages stay the same, 1158 * and the map can be kept. 1159 */ 1160 if (mem->mem_type == TTM_PL_VRAM || bo->mem.mem_type == TTM_PL_VRAM) 1161 vmw_bo_unmap(vbo); 1162 1163 /* 1164 * If we're moving a backup MOB out of MOB placement, then make sure we 1165 * read back all resource content first, and unbind the MOB from 1166 * the resource. 1167 */ 1168 if (mem->mem_type != VMW_PL_MOB && bo->mem.mem_type == VMW_PL_MOB) 1169 vmw_resource_unbind_list(vbo); 1170 } 1171