1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /************************************************************************** 3 * 4 * Copyright (c) 2006-2009 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 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 30 */ 31 32 #define pr_fmt(fmt) "[TTM] " fmt 33 34 #include <drm/ttm/ttm_bo_driver.h> 35 #include <drm/ttm/ttm_placement.h> 36 #include <linux/jiffies.h> 37 #include <linux/slab.h> 38 #include <linux/sched.h> 39 #include <linux/mm.h> 40 #include <linux/file.h> 41 #include <linux/module.h> 42 #include <linux/atomic.h> 43 #include <linux/dma-resv.h> 44 45 #include "ttm_module.h" 46 47 static void ttm_bo_global_kobj_release(struct kobject *kobj); 48 49 /* 50 * ttm_global_mutex - protecting the global BO state 51 */ 52 DEFINE_MUTEX(ttm_global_mutex); 53 unsigned ttm_bo_glob_use_count; 54 struct ttm_bo_global ttm_bo_glob; 55 EXPORT_SYMBOL(ttm_bo_glob); 56 57 static struct attribute ttm_bo_count = { 58 .name = "bo_count", 59 .mode = S_IRUGO 60 }; 61 62 /* default destructor */ 63 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo) 64 { 65 kfree(bo); 66 } 67 68 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, 69 struct ttm_placement *placement) 70 { 71 struct drm_printer p = drm_debug_printer(TTM_PFX); 72 struct ttm_resource_manager *man; 73 int i, mem_type; 74 75 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n", 76 bo, bo->mem.num_pages, bo->base.size >> 10, 77 bo->base.size >> 20); 78 for (i = 0; i < placement->num_placement; i++) { 79 mem_type = placement->placement[i].mem_type; 80 drm_printf(&p, " placement[%d]=0x%08X (%d)\n", 81 i, placement->placement[i].flags, mem_type); 82 man = ttm_manager_type(bo->bdev, mem_type); 83 ttm_resource_manager_debug(man, &p); 84 } 85 } 86 87 static ssize_t ttm_bo_global_show(struct kobject *kobj, 88 struct attribute *attr, 89 char *buffer) 90 { 91 struct ttm_bo_global *glob = 92 container_of(kobj, struct ttm_bo_global, kobj); 93 94 return snprintf(buffer, PAGE_SIZE, "%d\n", 95 atomic_read(&glob->bo_count)); 96 } 97 98 static struct attribute *ttm_bo_global_attrs[] = { 99 &ttm_bo_count, 100 NULL 101 }; 102 103 static const struct sysfs_ops ttm_bo_global_ops = { 104 .show = &ttm_bo_global_show 105 }; 106 107 static struct kobj_type ttm_bo_glob_kobj_type = { 108 .release = &ttm_bo_global_kobj_release, 109 .sysfs_ops = &ttm_bo_global_ops, 110 .default_attrs = ttm_bo_global_attrs 111 }; 112 113 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo) 114 { 115 struct ttm_bo_device *bdev = bo->bdev; 116 117 list_del_init(&bo->swap); 118 list_del_init(&bo->lru); 119 120 if (bdev->driver->del_from_lru_notify) 121 bdev->driver->del_from_lru_notify(bo); 122 } 123 124 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos, 125 struct ttm_buffer_object *bo) 126 { 127 if (!pos->first) 128 pos->first = bo; 129 pos->last = bo; 130 } 131 132 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo, 133 struct ttm_resource *mem, 134 struct ttm_lru_bulk_move *bulk) 135 { 136 struct ttm_bo_device *bdev = bo->bdev; 137 struct ttm_resource_manager *man; 138 139 dma_resv_assert_held(bo->base.resv); 140 141 if (bo->pin_count) { 142 ttm_bo_del_from_lru(bo); 143 return; 144 } 145 146 man = ttm_manager_type(bdev, mem->mem_type); 147 list_move_tail(&bo->lru, &man->lru[bo->priority]); 148 if (man->use_tt && bo->ttm && 149 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG | 150 TTM_PAGE_FLAG_SWAPPED))) { 151 struct list_head *swap; 152 153 swap = &ttm_bo_glob.swap_lru[bo->priority]; 154 list_move_tail(&bo->swap, swap); 155 } 156 157 if (bdev->driver->del_from_lru_notify) 158 bdev->driver->del_from_lru_notify(bo); 159 160 if (bulk && !bo->pin_count) { 161 switch (bo->mem.mem_type) { 162 case TTM_PL_TT: 163 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo); 164 break; 165 166 case TTM_PL_VRAM: 167 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo); 168 break; 169 } 170 if (bo->ttm && !(bo->ttm->page_flags & 171 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED))) 172 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo); 173 } 174 } 175 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail); 176 177 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk) 178 { 179 unsigned i; 180 181 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 182 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i]; 183 struct ttm_resource_manager *man; 184 185 if (!pos->first) 186 continue; 187 188 dma_resv_assert_held(pos->first->base.resv); 189 dma_resv_assert_held(pos->last->base.resv); 190 191 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT); 192 list_bulk_move_tail(&man->lru[i], &pos->first->lru, 193 &pos->last->lru); 194 } 195 196 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 197 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i]; 198 struct ttm_resource_manager *man; 199 200 if (!pos->first) 201 continue; 202 203 dma_resv_assert_held(pos->first->base.resv); 204 dma_resv_assert_held(pos->last->base.resv); 205 206 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM); 207 list_bulk_move_tail(&man->lru[i], &pos->first->lru, 208 &pos->last->lru); 209 } 210 211 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 212 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i]; 213 struct list_head *lru; 214 215 if (!pos->first) 216 continue; 217 218 dma_resv_assert_held(pos->first->base.resv); 219 dma_resv_assert_held(pos->last->base.resv); 220 221 lru = &ttm_bo_glob.swap_lru[i]; 222 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap); 223 } 224 } 225 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail); 226 227 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, 228 struct ttm_resource *mem, bool evict, 229 struct ttm_operation_ctx *ctx, 230 struct ttm_place *hop) 231 { 232 struct ttm_bo_device *bdev = bo->bdev; 233 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type); 234 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type); 235 int ret; 236 237 ttm_bo_unmap_virtual(bo); 238 239 /* 240 * Create and bind a ttm if required. 241 */ 242 243 if (new_man->use_tt) { 244 /* Zero init the new TTM structure if the old location should 245 * have used one as well. 246 */ 247 ret = ttm_tt_create(bo, old_man->use_tt); 248 if (ret) 249 goto out_err; 250 251 if (mem->mem_type != TTM_PL_SYSTEM) { 252 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx); 253 if (ret) 254 goto out_err; 255 } 256 } 257 258 ret = bdev->driver->move(bo, evict, ctx, mem, hop); 259 if (ret) { 260 if (ret == -EMULTIHOP) 261 return ret; 262 goto out_err; 263 } 264 265 ctx->bytes_moved += bo->base.size; 266 return 0; 267 268 out_err: 269 new_man = ttm_manager_type(bdev, bo->mem.mem_type); 270 if (!new_man->use_tt) 271 ttm_bo_tt_destroy(bo); 272 273 return ret; 274 } 275 276 /* 277 * Call bo::reserved. 278 * Will release GPU memory type usage on destruction. 279 * This is the place to put in driver specific hooks to release 280 * driver private resources. 281 * Will release the bo::reserved lock. 282 */ 283 284 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) 285 { 286 if (bo->bdev->driver->delete_mem_notify) 287 bo->bdev->driver->delete_mem_notify(bo); 288 289 ttm_bo_tt_destroy(bo); 290 ttm_resource_free(bo, &bo->mem); 291 } 292 293 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo) 294 { 295 int r; 296 297 if (bo->base.resv == &bo->base._resv) 298 return 0; 299 300 BUG_ON(!dma_resv_trylock(&bo->base._resv)); 301 302 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv); 303 dma_resv_unlock(&bo->base._resv); 304 if (r) 305 return r; 306 307 if (bo->type != ttm_bo_type_sg) { 308 /* This works because the BO is about to be destroyed and nobody 309 * reference it any more. The only tricky case is the trylock on 310 * the resv object while holding the lru_lock. 311 */ 312 spin_lock(&ttm_bo_glob.lru_lock); 313 bo->base.resv = &bo->base._resv; 314 spin_unlock(&ttm_bo_glob.lru_lock); 315 } 316 317 return r; 318 } 319 320 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) 321 { 322 struct dma_resv *resv = &bo->base._resv; 323 struct dma_resv_list *fobj; 324 struct dma_fence *fence; 325 int i; 326 327 rcu_read_lock(); 328 fobj = rcu_dereference(resv->fence); 329 fence = rcu_dereference(resv->fence_excl); 330 if (fence && !fence->ops->signaled) 331 dma_fence_enable_sw_signaling(fence); 332 333 for (i = 0; fobj && i < fobj->shared_count; ++i) { 334 fence = rcu_dereference(fobj->shared[i]); 335 336 if (!fence->ops->signaled) 337 dma_fence_enable_sw_signaling(fence); 338 } 339 rcu_read_unlock(); 340 } 341 342 /** 343 * function ttm_bo_cleanup_refs 344 * If bo idle, remove from lru lists, and unref. 345 * If not idle, block if possible. 346 * 347 * Must be called with lru_lock and reservation held, this function 348 * will drop the lru lock and optionally the reservation lock before returning. 349 * 350 * @bo: The buffer object to clean-up 351 * @interruptible: Any sleeps should occur interruptibly. 352 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead. 353 * @unlock_resv: Unlock the reservation lock as well. 354 */ 355 356 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, 357 bool interruptible, bool no_wait_gpu, 358 bool unlock_resv) 359 { 360 struct dma_resv *resv = &bo->base._resv; 361 int ret; 362 363 if (dma_resv_test_signaled_rcu(resv, true)) 364 ret = 0; 365 else 366 ret = -EBUSY; 367 368 if (ret && !no_wait_gpu) { 369 long lret; 370 371 if (unlock_resv) 372 dma_resv_unlock(bo->base.resv); 373 spin_unlock(&ttm_bo_glob.lru_lock); 374 375 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible, 376 30 * HZ); 377 378 if (lret < 0) 379 return lret; 380 else if (lret == 0) 381 return -EBUSY; 382 383 spin_lock(&ttm_bo_glob.lru_lock); 384 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) { 385 /* 386 * We raced, and lost, someone else holds the reservation now, 387 * and is probably busy in ttm_bo_cleanup_memtype_use. 388 * 389 * Even if it's not the case, because we finished waiting any 390 * delayed destruction would succeed, so just return success 391 * here. 392 */ 393 spin_unlock(&ttm_bo_glob.lru_lock); 394 return 0; 395 } 396 ret = 0; 397 } 398 399 if (ret || unlikely(list_empty(&bo->ddestroy))) { 400 if (unlock_resv) 401 dma_resv_unlock(bo->base.resv); 402 spin_unlock(&ttm_bo_glob.lru_lock); 403 return ret; 404 } 405 406 ttm_bo_del_from_lru(bo); 407 list_del_init(&bo->ddestroy); 408 spin_unlock(&ttm_bo_glob.lru_lock); 409 ttm_bo_cleanup_memtype_use(bo); 410 411 if (unlock_resv) 412 dma_resv_unlock(bo->base.resv); 413 414 ttm_bo_put(bo); 415 416 return 0; 417 } 418 419 /* 420 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all 421 * encountered buffers. 422 */ 423 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) 424 { 425 struct ttm_bo_global *glob = &ttm_bo_glob; 426 struct list_head removed; 427 bool empty; 428 429 INIT_LIST_HEAD(&removed); 430 431 spin_lock(&glob->lru_lock); 432 while (!list_empty(&bdev->ddestroy)) { 433 struct ttm_buffer_object *bo; 434 435 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object, 436 ddestroy); 437 list_move_tail(&bo->ddestroy, &removed); 438 if (!ttm_bo_get_unless_zero(bo)) 439 continue; 440 441 if (remove_all || bo->base.resv != &bo->base._resv) { 442 spin_unlock(&glob->lru_lock); 443 dma_resv_lock(bo->base.resv, NULL); 444 445 spin_lock(&glob->lru_lock); 446 ttm_bo_cleanup_refs(bo, false, !remove_all, true); 447 448 } else if (dma_resv_trylock(bo->base.resv)) { 449 ttm_bo_cleanup_refs(bo, false, !remove_all, true); 450 } else { 451 spin_unlock(&glob->lru_lock); 452 } 453 454 ttm_bo_put(bo); 455 spin_lock(&glob->lru_lock); 456 } 457 list_splice_tail(&removed, &bdev->ddestroy); 458 empty = list_empty(&bdev->ddestroy); 459 spin_unlock(&glob->lru_lock); 460 461 return empty; 462 } 463 464 static void ttm_bo_delayed_workqueue(struct work_struct *work) 465 { 466 struct ttm_bo_device *bdev = 467 container_of(work, struct ttm_bo_device, wq.work); 468 469 if (!ttm_bo_delayed_delete(bdev, false)) 470 schedule_delayed_work(&bdev->wq, 471 ((HZ / 100) < 1) ? 1 : HZ / 100); 472 } 473 474 static void ttm_bo_release(struct kref *kref) 475 { 476 struct ttm_buffer_object *bo = 477 container_of(kref, struct ttm_buffer_object, kref); 478 struct ttm_bo_device *bdev = bo->bdev; 479 size_t acc_size = bo->acc_size; 480 int ret; 481 482 if (!bo->deleted) { 483 ret = ttm_bo_individualize_resv(bo); 484 if (ret) { 485 /* Last resort, if we fail to allocate memory for the 486 * fences block for the BO to become idle 487 */ 488 dma_resv_wait_timeout_rcu(bo->base.resv, true, false, 489 30 * HZ); 490 } 491 492 if (bo->bdev->driver->release_notify) 493 bo->bdev->driver->release_notify(bo); 494 495 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node); 496 ttm_mem_io_free(bdev, &bo->mem); 497 } 498 499 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) || 500 !dma_resv_trylock(bo->base.resv)) { 501 /* The BO is not idle, resurrect it for delayed destroy */ 502 ttm_bo_flush_all_fences(bo); 503 bo->deleted = true; 504 505 spin_lock(&ttm_bo_glob.lru_lock); 506 507 /* 508 * Make pinned bos immediately available to 509 * shrinkers, now that they are queued for 510 * destruction. 511 */ 512 if (WARN_ON(bo->pin_count)) { 513 bo->pin_count = 0; 514 ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL); 515 } 516 517 kref_init(&bo->kref); 518 list_add_tail(&bo->ddestroy, &bdev->ddestroy); 519 spin_unlock(&ttm_bo_glob.lru_lock); 520 521 schedule_delayed_work(&bdev->wq, 522 ((HZ / 100) < 1) ? 1 : HZ / 100); 523 return; 524 } 525 526 spin_lock(&ttm_bo_glob.lru_lock); 527 ttm_bo_del_from_lru(bo); 528 list_del(&bo->ddestroy); 529 spin_unlock(&ttm_bo_glob.lru_lock); 530 531 ttm_bo_cleanup_memtype_use(bo); 532 dma_resv_unlock(bo->base.resv); 533 534 atomic_dec(&ttm_bo_glob.bo_count); 535 dma_fence_put(bo->moving); 536 if (!ttm_bo_uses_embedded_gem_object(bo)) 537 dma_resv_fini(&bo->base._resv); 538 bo->destroy(bo); 539 ttm_mem_global_free(&ttm_mem_glob, acc_size); 540 } 541 542 void ttm_bo_put(struct ttm_buffer_object *bo) 543 { 544 kref_put(&bo->kref, ttm_bo_release); 545 } 546 EXPORT_SYMBOL(ttm_bo_put); 547 548 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) 549 { 550 return cancel_delayed_work_sync(&bdev->wq); 551 } 552 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); 553 554 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) 555 { 556 if (resched) 557 schedule_delayed_work(&bdev->wq, 558 ((HZ / 100) < 1) ? 1 : HZ / 100); 559 } 560 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); 561 562 static int ttm_bo_evict(struct ttm_buffer_object *bo, 563 struct ttm_operation_ctx *ctx) 564 { 565 struct ttm_bo_device *bdev = bo->bdev; 566 struct ttm_resource evict_mem; 567 struct ttm_placement placement; 568 struct ttm_place hop; 569 int ret = 0; 570 571 memset(&hop, 0, sizeof(hop)); 572 573 dma_resv_assert_held(bo->base.resv); 574 575 placement.num_placement = 0; 576 placement.num_busy_placement = 0; 577 bdev->driver->evict_flags(bo, &placement); 578 579 if (!placement.num_placement && !placement.num_busy_placement) { 580 ttm_bo_wait(bo, false, false); 581 582 ttm_bo_cleanup_memtype_use(bo); 583 return ttm_tt_create(bo, false); 584 } 585 586 evict_mem = bo->mem; 587 evict_mem.mm_node = NULL; 588 evict_mem.bus.offset = 0; 589 evict_mem.bus.addr = NULL; 590 591 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx); 592 if (ret) { 593 if (ret != -ERESTARTSYS) { 594 pr_err("Failed to find memory space for buffer 0x%p eviction\n", 595 bo); 596 ttm_bo_mem_space_debug(bo, &placement); 597 } 598 goto out; 599 } 600 601 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop); 602 if (unlikely(ret)) { 603 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n"); 604 if (ret != -ERESTARTSYS) 605 pr_err("Buffer eviction failed\n"); 606 ttm_resource_free(bo, &evict_mem); 607 } 608 out: 609 return ret; 610 } 611 612 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 613 const struct ttm_place *place) 614 { 615 /* Don't evict this BO if it's outside of the 616 * requested placement range 617 */ 618 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) || 619 (place->lpfn && place->lpfn <= bo->mem.start)) 620 return false; 621 622 return true; 623 } 624 EXPORT_SYMBOL(ttm_bo_eviction_valuable); 625 626 /* 627 * Check the target bo is allowable to be evicted or swapout, including cases: 628 * 629 * a. if share same reservation object with ctx->resv, have assumption 630 * reservation objects should already be locked, so not lock again and 631 * return true directly when either the opreation allow_reserved_eviction 632 * or the target bo already is in delayed free list; 633 * 634 * b. Otherwise, trylock it. 635 */ 636 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo, 637 struct ttm_operation_ctx *ctx, bool *locked, bool *busy) 638 { 639 bool ret = false; 640 641 if (bo->base.resv == ctx->resv) { 642 dma_resv_assert_held(bo->base.resv); 643 if (ctx->allow_res_evict) 644 ret = true; 645 *locked = false; 646 if (busy) 647 *busy = false; 648 } else { 649 ret = dma_resv_trylock(bo->base.resv); 650 *locked = ret; 651 if (busy) 652 *busy = !ret; 653 } 654 655 return ret; 656 } 657 658 /** 659 * ttm_mem_evict_wait_busy - wait for a busy BO to become available 660 * 661 * @busy_bo: BO which couldn't be locked with trylock 662 * @ctx: operation context 663 * @ticket: acquire ticket 664 * 665 * Try to lock a busy buffer object to avoid failing eviction. 666 */ 667 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo, 668 struct ttm_operation_ctx *ctx, 669 struct ww_acquire_ctx *ticket) 670 { 671 int r; 672 673 if (!busy_bo || !ticket) 674 return -EBUSY; 675 676 if (ctx->interruptible) 677 r = dma_resv_lock_interruptible(busy_bo->base.resv, 678 ticket); 679 else 680 r = dma_resv_lock(busy_bo->base.resv, ticket); 681 682 /* 683 * TODO: It would be better to keep the BO locked until allocation is at 684 * least tried one more time, but that would mean a much larger rework 685 * of TTM. 686 */ 687 if (!r) 688 dma_resv_unlock(busy_bo->base.resv); 689 690 return r == -EDEADLK ? -EBUSY : r; 691 } 692 693 int ttm_mem_evict_first(struct ttm_bo_device *bdev, 694 struct ttm_resource_manager *man, 695 const struct ttm_place *place, 696 struct ttm_operation_ctx *ctx, 697 struct ww_acquire_ctx *ticket) 698 { 699 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL; 700 bool locked = false; 701 unsigned i; 702 int ret; 703 704 spin_lock(&ttm_bo_glob.lru_lock); 705 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 706 list_for_each_entry(bo, &man->lru[i], lru) { 707 bool busy; 708 709 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, 710 &busy)) { 711 if (busy && !busy_bo && ticket != 712 dma_resv_locking_ctx(bo->base.resv)) 713 busy_bo = bo; 714 continue; 715 } 716 717 if (place && !bdev->driver->eviction_valuable(bo, 718 place)) { 719 if (locked) 720 dma_resv_unlock(bo->base.resv); 721 continue; 722 } 723 if (!ttm_bo_get_unless_zero(bo)) { 724 if (locked) 725 dma_resv_unlock(bo->base.resv); 726 continue; 727 } 728 break; 729 } 730 731 /* If the inner loop terminated early, we have our candidate */ 732 if (&bo->lru != &man->lru[i]) 733 break; 734 735 bo = NULL; 736 } 737 738 if (!bo) { 739 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo)) 740 busy_bo = NULL; 741 spin_unlock(&ttm_bo_glob.lru_lock); 742 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket); 743 if (busy_bo) 744 ttm_bo_put(busy_bo); 745 return ret; 746 } 747 748 if (bo->deleted) { 749 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible, 750 ctx->no_wait_gpu, locked); 751 ttm_bo_put(bo); 752 return ret; 753 } 754 755 spin_unlock(&ttm_bo_glob.lru_lock); 756 757 ret = ttm_bo_evict(bo, ctx); 758 if (locked) 759 ttm_bo_unreserve(bo); 760 761 ttm_bo_put(bo); 762 return ret; 763 } 764 765 /* 766 * Add the last move fence to the BO and reserve a new shared slot. 767 */ 768 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo, 769 struct ttm_resource_manager *man, 770 struct ttm_resource *mem, 771 bool no_wait_gpu) 772 { 773 struct dma_fence *fence; 774 int ret; 775 776 spin_lock(&man->move_lock); 777 fence = dma_fence_get(man->move); 778 spin_unlock(&man->move_lock); 779 780 if (!fence) 781 return 0; 782 783 if (no_wait_gpu) { 784 dma_fence_put(fence); 785 return -EBUSY; 786 } 787 788 dma_resv_add_shared_fence(bo->base.resv, fence); 789 790 ret = dma_resv_reserve_shared(bo->base.resv, 1); 791 if (unlikely(ret)) { 792 dma_fence_put(fence); 793 return ret; 794 } 795 796 dma_fence_put(bo->moving); 797 bo->moving = fence; 798 return 0; 799 } 800 801 /* 802 * Repeatedly evict memory from the LRU for @mem_type until we create enough 803 * space, or we've evicted everything and there isn't enough space. 804 */ 805 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, 806 const struct ttm_place *place, 807 struct ttm_resource *mem, 808 struct ttm_operation_ctx *ctx) 809 { 810 struct ttm_bo_device *bdev = bo->bdev; 811 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type); 812 struct ww_acquire_ctx *ticket; 813 int ret; 814 815 ticket = dma_resv_locking_ctx(bo->base.resv); 816 do { 817 ret = ttm_resource_alloc(bo, place, mem); 818 if (likely(!ret)) 819 break; 820 if (unlikely(ret != -ENOSPC)) 821 return ret; 822 ret = ttm_mem_evict_first(bdev, man, place, ctx, 823 ticket); 824 if (unlikely(ret != 0)) 825 return ret; 826 } while (1); 827 828 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu); 829 } 830 831 /** 832 * ttm_bo_mem_placement - check if placement is compatible 833 * @bo: BO to find memory for 834 * @place: where to search 835 * @mem: the memory object to fill in 836 * 837 * Check if placement is compatible and fill in mem structure. 838 * Returns -EBUSY if placement won't work or negative error code. 839 * 0 when placement can be used. 840 */ 841 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo, 842 const struct ttm_place *place, 843 struct ttm_resource *mem) 844 { 845 struct ttm_bo_device *bdev = bo->bdev; 846 struct ttm_resource_manager *man; 847 848 man = ttm_manager_type(bdev, place->mem_type); 849 if (!man || !ttm_resource_manager_used(man)) 850 return -EBUSY; 851 852 mem->mem_type = place->mem_type; 853 mem->placement = place->flags; 854 855 spin_lock(&ttm_bo_glob.lru_lock); 856 ttm_bo_move_to_lru_tail(bo, mem, NULL); 857 spin_unlock(&ttm_bo_glob.lru_lock); 858 859 return 0; 860 } 861 862 /* 863 * Creates space for memory region @mem according to its type. 864 * 865 * This function first searches for free space in compatible memory types in 866 * the priority order defined by the driver. If free space isn't found, then 867 * ttm_bo_mem_force_space is attempted in priority order to evict and find 868 * space. 869 */ 870 int ttm_bo_mem_space(struct ttm_buffer_object *bo, 871 struct ttm_placement *placement, 872 struct ttm_resource *mem, 873 struct ttm_operation_ctx *ctx) 874 { 875 struct ttm_bo_device *bdev = bo->bdev; 876 bool type_found = false; 877 int i, ret; 878 879 ret = dma_resv_reserve_shared(bo->base.resv, 1); 880 if (unlikely(ret)) 881 return ret; 882 883 for (i = 0; i < placement->num_placement; ++i) { 884 const struct ttm_place *place = &placement->placement[i]; 885 struct ttm_resource_manager *man; 886 887 ret = ttm_bo_mem_placement(bo, place, mem); 888 if (ret) 889 continue; 890 891 type_found = true; 892 ret = ttm_resource_alloc(bo, place, mem); 893 if (ret == -ENOSPC) 894 continue; 895 if (unlikely(ret)) 896 goto error; 897 898 man = ttm_manager_type(bdev, mem->mem_type); 899 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu); 900 if (unlikely(ret)) { 901 ttm_resource_free(bo, mem); 902 if (ret == -EBUSY) 903 continue; 904 905 goto error; 906 } 907 return 0; 908 } 909 910 for (i = 0; i < placement->num_busy_placement; ++i) { 911 const struct ttm_place *place = &placement->busy_placement[i]; 912 913 ret = ttm_bo_mem_placement(bo, place, mem); 914 if (ret) 915 continue; 916 917 type_found = true; 918 ret = ttm_bo_mem_force_space(bo, place, mem, ctx); 919 if (likely(!ret)) 920 return 0; 921 922 if (ret && ret != -EBUSY) 923 goto error; 924 } 925 926 ret = -ENOMEM; 927 if (!type_found) { 928 pr_err(TTM_PFX "No compatible memory type found\n"); 929 ret = -EINVAL; 930 } 931 932 error: 933 if (bo->mem.mem_type == TTM_PL_SYSTEM && !bo->pin_count) 934 ttm_bo_move_to_lru_tail_unlocked(bo); 935 936 return ret; 937 } 938 EXPORT_SYMBOL(ttm_bo_mem_space); 939 940 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo, 941 struct ttm_resource *mem, 942 struct ttm_operation_ctx *ctx, 943 struct ttm_place *hop) 944 { 945 struct ttm_placement hop_placement; 946 int ret; 947 struct ttm_resource hop_mem = *mem; 948 949 hop_mem.mm_node = NULL; 950 hop_mem.mem_type = TTM_PL_SYSTEM; 951 hop_mem.placement = 0; 952 953 hop_placement.num_placement = hop_placement.num_busy_placement = 1; 954 hop_placement.placement = hop_placement.busy_placement = hop; 955 956 /* find space in the bounce domain */ 957 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx); 958 if (ret) 959 return ret; 960 /* move to the bounce domain */ 961 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL); 962 if (ret) 963 return ret; 964 return 0; 965 } 966 967 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, 968 struct ttm_placement *placement, 969 struct ttm_operation_ctx *ctx) 970 { 971 int ret = 0; 972 struct ttm_place hop; 973 struct ttm_resource mem; 974 975 dma_resv_assert_held(bo->base.resv); 976 977 memset(&hop, 0, sizeof(hop)); 978 979 mem.num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT; 980 mem.page_alignment = bo->mem.page_alignment; 981 mem.bus.offset = 0; 982 mem.bus.addr = NULL; 983 mem.mm_node = NULL; 984 985 /* 986 * Determine where to move the buffer. 987 * 988 * If driver determines move is going to need 989 * an extra step then it will return -EMULTIHOP 990 * and the buffer will be moved to the temporary 991 * stop and the driver will be called to make 992 * the second hop. 993 */ 994 bounce: 995 ret = ttm_bo_mem_space(bo, placement, &mem, ctx); 996 if (ret) 997 return ret; 998 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop); 999 if (ret == -EMULTIHOP) { 1000 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop); 1001 if (ret) 1002 return ret; 1003 /* try and move to final place now. */ 1004 goto bounce; 1005 } 1006 if (ret) 1007 ttm_resource_free(bo, &mem); 1008 return ret; 1009 } 1010 1011 static bool ttm_bo_places_compat(const struct ttm_place *places, 1012 unsigned num_placement, 1013 struct ttm_resource *mem, 1014 uint32_t *new_flags) 1015 { 1016 unsigned i; 1017 1018 for (i = 0; i < num_placement; i++) { 1019 const struct ttm_place *heap = &places[i]; 1020 1021 if ((mem->start < heap->fpfn || 1022 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn))) 1023 continue; 1024 1025 *new_flags = heap->flags; 1026 if ((mem->mem_type == heap->mem_type) && 1027 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) || 1028 (mem->placement & TTM_PL_FLAG_CONTIGUOUS))) 1029 return true; 1030 } 1031 return false; 1032 } 1033 1034 bool ttm_bo_mem_compat(struct ttm_placement *placement, 1035 struct ttm_resource *mem, 1036 uint32_t *new_flags) 1037 { 1038 if (ttm_bo_places_compat(placement->placement, placement->num_placement, 1039 mem, new_flags)) 1040 return true; 1041 1042 if ((placement->busy_placement != placement->placement || 1043 placement->num_busy_placement > placement->num_placement) && 1044 ttm_bo_places_compat(placement->busy_placement, 1045 placement->num_busy_placement, 1046 mem, new_flags)) 1047 return true; 1048 1049 return false; 1050 } 1051 EXPORT_SYMBOL(ttm_bo_mem_compat); 1052 1053 int ttm_bo_validate(struct ttm_buffer_object *bo, 1054 struct ttm_placement *placement, 1055 struct ttm_operation_ctx *ctx) 1056 { 1057 int ret; 1058 uint32_t new_flags; 1059 1060 dma_resv_assert_held(bo->base.resv); 1061 1062 /* 1063 * Remove the backing store if no placement is given. 1064 */ 1065 if (!placement->num_placement && !placement->num_busy_placement) { 1066 ret = ttm_bo_pipeline_gutting(bo); 1067 if (ret) 1068 return ret; 1069 1070 return ttm_tt_create(bo, false); 1071 } 1072 1073 /* 1074 * Check whether we need to move buffer. 1075 */ 1076 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { 1077 ret = ttm_bo_move_buffer(bo, placement, ctx); 1078 if (ret) 1079 return ret; 1080 } 1081 /* 1082 * We might need to add a TTM. 1083 */ 1084 if (bo->mem.mem_type == TTM_PL_SYSTEM) { 1085 ret = ttm_tt_create(bo, true); 1086 if (ret) 1087 return ret; 1088 } 1089 return 0; 1090 } 1091 EXPORT_SYMBOL(ttm_bo_validate); 1092 1093 int ttm_bo_init_reserved(struct ttm_bo_device *bdev, 1094 struct ttm_buffer_object *bo, 1095 size_t size, 1096 enum ttm_bo_type type, 1097 struct ttm_placement *placement, 1098 uint32_t page_alignment, 1099 struct ttm_operation_ctx *ctx, 1100 size_t acc_size, 1101 struct sg_table *sg, 1102 struct dma_resv *resv, 1103 void (*destroy) (struct ttm_buffer_object *)) 1104 { 1105 struct ttm_mem_global *mem_glob = &ttm_mem_glob; 1106 bool locked; 1107 int ret = 0; 1108 1109 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx); 1110 if (ret) { 1111 pr_err("Out of kernel memory\n"); 1112 if (destroy) 1113 (*destroy)(bo); 1114 else 1115 kfree(bo); 1116 return -ENOMEM; 1117 } 1118 1119 bo->destroy = destroy ? destroy : ttm_bo_default_destroy; 1120 1121 kref_init(&bo->kref); 1122 INIT_LIST_HEAD(&bo->lru); 1123 INIT_LIST_HEAD(&bo->ddestroy); 1124 INIT_LIST_HEAD(&bo->swap); 1125 bo->bdev = bdev; 1126 bo->type = type; 1127 bo->mem.mem_type = TTM_PL_SYSTEM; 1128 bo->mem.num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; 1129 bo->mem.mm_node = NULL; 1130 bo->mem.page_alignment = page_alignment; 1131 bo->mem.bus.offset = 0; 1132 bo->mem.bus.addr = NULL; 1133 bo->moving = NULL; 1134 bo->mem.placement = 0; 1135 bo->acc_size = acc_size; 1136 bo->pin_count = 0; 1137 bo->sg = sg; 1138 if (resv) { 1139 bo->base.resv = resv; 1140 dma_resv_assert_held(bo->base.resv); 1141 } else { 1142 bo->base.resv = &bo->base._resv; 1143 } 1144 if (!ttm_bo_uses_embedded_gem_object(bo)) { 1145 /* 1146 * bo.base is not initialized, so we have to setup the 1147 * struct elements we want use regardless. 1148 */ 1149 bo->base.size = size; 1150 dma_resv_init(&bo->base._resv); 1151 drm_vma_node_reset(&bo->base.vma_node); 1152 } 1153 atomic_inc(&ttm_bo_glob.bo_count); 1154 1155 /* 1156 * For ttm_bo_type_device buffers, allocate 1157 * address space from the device. 1158 */ 1159 if (bo->type == ttm_bo_type_device || 1160 bo->type == ttm_bo_type_sg) 1161 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node, 1162 bo->mem.num_pages); 1163 1164 /* passed reservation objects should already be locked, 1165 * since otherwise lockdep will be angered in radeon. 1166 */ 1167 if (!resv) { 1168 locked = dma_resv_trylock(bo->base.resv); 1169 WARN_ON(!locked); 1170 } 1171 1172 if (likely(!ret)) 1173 ret = ttm_bo_validate(bo, placement, ctx); 1174 1175 if (unlikely(ret)) { 1176 if (!resv) 1177 ttm_bo_unreserve(bo); 1178 1179 ttm_bo_put(bo); 1180 return ret; 1181 } 1182 1183 ttm_bo_move_to_lru_tail_unlocked(bo); 1184 1185 return ret; 1186 } 1187 EXPORT_SYMBOL(ttm_bo_init_reserved); 1188 1189 int ttm_bo_init(struct ttm_bo_device *bdev, 1190 struct ttm_buffer_object *bo, 1191 size_t size, 1192 enum ttm_bo_type type, 1193 struct ttm_placement *placement, 1194 uint32_t page_alignment, 1195 bool interruptible, 1196 size_t acc_size, 1197 struct sg_table *sg, 1198 struct dma_resv *resv, 1199 void (*destroy) (struct ttm_buffer_object *)) 1200 { 1201 struct ttm_operation_ctx ctx = { interruptible, false }; 1202 int ret; 1203 1204 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement, 1205 page_alignment, &ctx, acc_size, 1206 sg, resv, destroy); 1207 if (ret) 1208 return ret; 1209 1210 if (!resv) 1211 ttm_bo_unreserve(bo); 1212 1213 return 0; 1214 } 1215 EXPORT_SYMBOL(ttm_bo_init); 1216 1217 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, 1218 unsigned long bo_size, 1219 unsigned struct_size) 1220 { 1221 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1222 size_t size = 0; 1223 1224 size += ttm_round_pot(struct_size); 1225 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t))); 1226 size += ttm_round_pot(sizeof(struct ttm_tt)); 1227 return size; 1228 } 1229 EXPORT_SYMBOL(ttm_bo_dma_acc_size); 1230 1231 static void ttm_bo_global_kobj_release(struct kobject *kobj) 1232 { 1233 struct ttm_bo_global *glob = 1234 container_of(kobj, struct ttm_bo_global, kobj); 1235 1236 __free_page(glob->dummy_read_page); 1237 } 1238 1239 static void ttm_bo_global_release(void) 1240 { 1241 struct ttm_bo_global *glob = &ttm_bo_glob; 1242 1243 mutex_lock(&ttm_global_mutex); 1244 if (--ttm_bo_glob_use_count > 0) 1245 goto out; 1246 1247 kobject_del(&glob->kobj); 1248 kobject_put(&glob->kobj); 1249 ttm_mem_global_release(&ttm_mem_glob); 1250 memset(glob, 0, sizeof(*glob)); 1251 out: 1252 mutex_unlock(&ttm_global_mutex); 1253 } 1254 1255 static int ttm_bo_global_init(void) 1256 { 1257 struct ttm_bo_global *glob = &ttm_bo_glob; 1258 int ret = 0; 1259 unsigned i; 1260 1261 mutex_lock(&ttm_global_mutex); 1262 if (++ttm_bo_glob_use_count > 1) 1263 goto out; 1264 1265 ret = ttm_mem_global_init(&ttm_mem_glob); 1266 if (ret) 1267 goto out; 1268 1269 spin_lock_init(&glob->lru_lock); 1270 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); 1271 1272 if (unlikely(glob->dummy_read_page == NULL)) { 1273 ret = -ENOMEM; 1274 goto out; 1275 } 1276 1277 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1278 INIT_LIST_HEAD(&glob->swap_lru[i]); 1279 INIT_LIST_HEAD(&glob->device_list); 1280 atomic_set(&glob->bo_count, 0); 1281 1282 ret = kobject_init_and_add( 1283 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); 1284 if (unlikely(ret != 0)) 1285 kobject_put(&glob->kobj); 1286 out: 1287 mutex_unlock(&ttm_global_mutex); 1288 return ret; 1289 } 1290 1291 int ttm_bo_device_release(struct ttm_bo_device *bdev) 1292 { 1293 struct ttm_bo_global *glob = &ttm_bo_glob; 1294 int ret = 0; 1295 unsigned i; 1296 struct ttm_resource_manager *man; 1297 1298 man = ttm_manager_type(bdev, TTM_PL_SYSTEM); 1299 ttm_resource_manager_set_used(man, false); 1300 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL); 1301 1302 mutex_lock(&ttm_global_mutex); 1303 list_del(&bdev->device_list); 1304 mutex_unlock(&ttm_global_mutex); 1305 1306 cancel_delayed_work_sync(&bdev->wq); 1307 1308 if (ttm_bo_delayed_delete(bdev, true)) 1309 pr_debug("Delayed destroy list was clean\n"); 1310 1311 spin_lock(&glob->lru_lock); 1312 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1313 if (list_empty(&man->lru[0])) 1314 pr_debug("Swap list %d was clean\n", i); 1315 spin_unlock(&glob->lru_lock); 1316 1317 ttm_pool_fini(&bdev->pool); 1318 1319 if (!ret) 1320 ttm_bo_global_release(); 1321 1322 return ret; 1323 } 1324 EXPORT_SYMBOL(ttm_bo_device_release); 1325 1326 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev) 1327 { 1328 struct ttm_resource_manager *man = &bdev->sysman; 1329 1330 /* 1331 * Initialize the system memory buffer type. 1332 * Other types need to be driver / IOCTL initialized. 1333 */ 1334 man->use_tt = true; 1335 1336 ttm_resource_manager_init(man, 0); 1337 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man); 1338 ttm_resource_manager_set_used(man, true); 1339 } 1340 1341 int ttm_bo_device_init(struct ttm_bo_device *bdev, 1342 struct ttm_bo_driver *driver, 1343 struct device *dev, 1344 struct address_space *mapping, 1345 struct drm_vma_offset_manager *vma_manager, 1346 bool use_dma_alloc, bool use_dma32) 1347 { 1348 struct ttm_bo_global *glob = &ttm_bo_glob; 1349 int ret; 1350 1351 if (WARN_ON(vma_manager == NULL)) 1352 return -EINVAL; 1353 1354 ret = ttm_bo_global_init(); 1355 if (ret) 1356 return ret; 1357 1358 bdev->driver = driver; 1359 1360 ttm_bo_init_sysman(bdev); 1361 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32); 1362 1363 bdev->vma_manager = vma_manager; 1364 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); 1365 INIT_LIST_HEAD(&bdev->ddestroy); 1366 bdev->dev_mapping = mapping; 1367 mutex_lock(&ttm_global_mutex); 1368 list_add_tail(&bdev->device_list, &glob->device_list); 1369 mutex_unlock(&ttm_global_mutex); 1370 1371 return 0; 1372 } 1373 EXPORT_SYMBOL(ttm_bo_device_init); 1374 1375 /* 1376 * buffer object vm functions. 1377 */ 1378 1379 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) 1380 { 1381 struct ttm_bo_device *bdev = bo->bdev; 1382 1383 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping); 1384 ttm_mem_io_free(bdev, &bo->mem); 1385 } 1386 EXPORT_SYMBOL(ttm_bo_unmap_virtual); 1387 1388 int ttm_bo_wait(struct ttm_buffer_object *bo, 1389 bool interruptible, bool no_wait) 1390 { 1391 long timeout = 15 * HZ; 1392 1393 if (no_wait) { 1394 if (dma_resv_test_signaled_rcu(bo->base.resv, true)) 1395 return 0; 1396 else 1397 return -EBUSY; 1398 } 1399 1400 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true, 1401 interruptible, timeout); 1402 if (timeout < 0) 1403 return timeout; 1404 1405 if (timeout == 0) 1406 return -EBUSY; 1407 1408 dma_resv_add_excl_fence(bo->base.resv, NULL); 1409 return 0; 1410 } 1411 EXPORT_SYMBOL(ttm_bo_wait); 1412 1413 /* 1414 * A buffer object shrink method that tries to swap out the first 1415 * buffer object on the bo_global::swap_lru list. 1416 */ 1417 int ttm_bo_swapout(struct ttm_operation_ctx *ctx) 1418 { 1419 struct ttm_bo_global *glob = &ttm_bo_glob; 1420 struct ttm_buffer_object *bo; 1421 int ret = -EBUSY; 1422 bool locked; 1423 unsigned i; 1424 1425 spin_lock(&glob->lru_lock); 1426 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 1427 list_for_each_entry(bo, &glob->swap_lru[i], swap) { 1428 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, 1429 NULL)) 1430 continue; 1431 1432 if (!ttm_bo_get_unless_zero(bo)) { 1433 if (locked) 1434 dma_resv_unlock(bo->base.resv); 1435 continue; 1436 } 1437 1438 ret = 0; 1439 break; 1440 } 1441 if (!ret) 1442 break; 1443 } 1444 1445 if (ret) { 1446 spin_unlock(&glob->lru_lock); 1447 return ret; 1448 } 1449 1450 if (bo->deleted) { 1451 ret = ttm_bo_cleanup_refs(bo, false, false, locked); 1452 ttm_bo_put(bo); 1453 return ret; 1454 } 1455 1456 ttm_bo_del_from_lru(bo); 1457 spin_unlock(&glob->lru_lock); 1458 1459 /** 1460 * Move to system cached 1461 */ 1462 1463 if (bo->mem.mem_type != TTM_PL_SYSTEM) { 1464 struct ttm_operation_ctx ctx = { false, false }; 1465 struct ttm_resource evict_mem; 1466 struct ttm_place hop; 1467 1468 memset(&hop, 0, sizeof(hop)); 1469 1470 evict_mem = bo->mem; 1471 evict_mem.mm_node = NULL; 1472 evict_mem.placement = 0; 1473 evict_mem.mem_type = TTM_PL_SYSTEM; 1474 1475 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop); 1476 if (unlikely(ret != 0)) { 1477 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n"); 1478 goto out; 1479 } 1480 } 1481 1482 /** 1483 * Make sure BO is idle. 1484 */ 1485 1486 ret = ttm_bo_wait(bo, false, false); 1487 if (unlikely(ret != 0)) 1488 goto out; 1489 1490 ttm_bo_unmap_virtual(bo); 1491 1492 /** 1493 * Swap out. Buffer will be swapped in again as soon as 1494 * anyone tries to access a ttm page. 1495 */ 1496 1497 if (bo->bdev->driver->swap_notify) 1498 bo->bdev->driver->swap_notify(bo); 1499 1500 ret = ttm_tt_swapout(bo->bdev, bo->ttm); 1501 out: 1502 1503 /** 1504 * 1505 * Unreserve without putting on LRU to avoid swapping out an 1506 * already swapped buffer. 1507 */ 1508 if (locked) 1509 dma_resv_unlock(bo->base.resv); 1510 ttm_bo_put(bo); 1511 return ret; 1512 } 1513 EXPORT_SYMBOL(ttm_bo_swapout); 1514 1515 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo) 1516 { 1517 if (bo->ttm == NULL) 1518 return; 1519 1520 ttm_tt_destroy(bo->bdev, bo->ttm); 1521 bo->ttm = NULL; 1522 } 1523 1524