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