1 /************************************************************************** 2 * 3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 /* 28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 29 */ 30 31 #define pr_fmt(fmt) "[TTM] " fmt 32 33 #include <drm/ttm/ttm_module.h> 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/reservation.h> 44 45 #define TTM_ASSERT_LOCKED(param) 46 #define TTM_DEBUG(fmt, arg...) 47 #define TTM_BO_HASH_ORDER 13 48 49 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); 50 static void ttm_bo_global_kobj_release(struct kobject *kobj); 51 52 static struct attribute ttm_bo_count = { 53 .name = "bo_count", 54 .mode = S_IRUGO 55 }; 56 57 static inline int ttm_mem_type_from_place(const struct ttm_place *place, 58 uint32_t *mem_type) 59 { 60 int pos; 61 62 pos = ffs(place->flags & TTM_PL_MASK_MEM); 63 if (unlikely(!pos)) 64 return -EINVAL; 65 66 *mem_type = pos - 1; 67 return 0; 68 } 69 70 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type) 71 { 72 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 73 74 pr_err(" has_type: %d\n", man->has_type); 75 pr_err(" use_type: %d\n", man->use_type); 76 pr_err(" flags: 0x%08X\n", man->flags); 77 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset); 78 pr_err(" size: %llu\n", man->size); 79 pr_err(" available_caching: 0x%08X\n", man->available_caching); 80 pr_err(" default_caching: 0x%08X\n", man->default_caching); 81 if (mem_type != TTM_PL_SYSTEM) 82 (*man->func->debug)(man, TTM_PFX); 83 } 84 85 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, 86 struct ttm_placement *placement) 87 { 88 int i, ret, mem_type; 89 90 pr_err("No space for %p (%lu pages, %luK, %luM)\n", 91 bo, bo->mem.num_pages, bo->mem.size >> 10, 92 bo->mem.size >> 20); 93 for (i = 0; i < placement->num_placement; i++) { 94 ret = ttm_mem_type_from_place(&placement->placement[i], 95 &mem_type); 96 if (ret) 97 return; 98 pr_err(" placement[%d]=0x%08X (%d)\n", 99 i, placement->placement[i].flags, mem_type); 100 ttm_mem_type_debug(bo->bdev, mem_type); 101 } 102 } 103 104 static ssize_t ttm_bo_global_show(struct kobject *kobj, 105 struct attribute *attr, 106 char *buffer) 107 { 108 struct ttm_bo_global *glob = 109 container_of(kobj, struct ttm_bo_global, kobj); 110 111 return snprintf(buffer, PAGE_SIZE, "%lu\n", 112 (unsigned long) atomic_read(&glob->bo_count)); 113 } 114 115 static struct attribute *ttm_bo_global_attrs[] = { 116 &ttm_bo_count, 117 NULL 118 }; 119 120 static const struct sysfs_ops ttm_bo_global_ops = { 121 .show = &ttm_bo_global_show 122 }; 123 124 static struct kobj_type ttm_bo_glob_kobj_type = { 125 .release = &ttm_bo_global_kobj_release, 126 .sysfs_ops = &ttm_bo_global_ops, 127 .default_attrs = ttm_bo_global_attrs 128 }; 129 130 131 static inline uint32_t ttm_bo_type_flags(unsigned type) 132 { 133 return 1 << (type); 134 } 135 136 static void ttm_bo_release_list(struct kref *list_kref) 137 { 138 struct ttm_buffer_object *bo = 139 container_of(list_kref, struct ttm_buffer_object, list_kref); 140 struct ttm_bo_device *bdev = bo->bdev; 141 size_t acc_size = bo->acc_size; 142 143 BUG_ON(kref_read(&bo->list_kref)); 144 BUG_ON(kref_read(&bo->kref)); 145 BUG_ON(atomic_read(&bo->cpu_writers)); 146 BUG_ON(bo->mem.mm_node != NULL); 147 BUG_ON(!list_empty(&bo->lru)); 148 BUG_ON(!list_empty(&bo->ddestroy)); 149 ttm_tt_destroy(bo->ttm); 150 atomic_dec(&bo->glob->bo_count); 151 dma_fence_put(bo->moving); 152 if (bo->resv == &bo->ttm_resv) 153 reservation_object_fini(&bo->ttm_resv); 154 mutex_destroy(&bo->wu_mutex); 155 if (bo->destroy) 156 bo->destroy(bo); 157 else { 158 kfree(bo); 159 } 160 ttm_mem_global_free(bdev->glob->mem_glob, acc_size); 161 } 162 163 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) 164 { 165 struct ttm_bo_device *bdev = bo->bdev; 166 struct ttm_mem_type_manager *man; 167 168 lockdep_assert_held(&bo->resv->lock.base); 169 170 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { 171 172 BUG_ON(!list_empty(&bo->lru)); 173 174 man = &bdev->man[bo->mem.mem_type]; 175 list_add_tail(&bo->lru, &man->lru[bo->priority]); 176 kref_get(&bo->list_kref); 177 178 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) { 179 list_add_tail(&bo->swap, 180 &bo->glob->swap_lru[bo->priority]); 181 kref_get(&bo->list_kref); 182 } 183 } 184 } 185 EXPORT_SYMBOL(ttm_bo_add_to_lru); 186 187 static void ttm_bo_ref_bug(struct kref *list_kref) 188 { 189 BUG(); 190 } 191 192 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo) 193 { 194 if (!list_empty(&bo->swap)) { 195 list_del_init(&bo->swap); 196 kref_put(&bo->list_kref, ttm_bo_ref_bug); 197 } 198 if (!list_empty(&bo->lru)) { 199 list_del_init(&bo->lru); 200 kref_put(&bo->list_kref, ttm_bo_ref_bug); 201 } 202 203 /* 204 * TODO: Add a driver hook to delete from 205 * driver-specific LRU's here. 206 */ 207 } 208 209 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo) 210 { 211 spin_lock(&bo->glob->lru_lock); 212 ttm_bo_del_from_lru(bo); 213 spin_unlock(&bo->glob->lru_lock); 214 } 215 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru); 216 217 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo) 218 { 219 lockdep_assert_held(&bo->resv->lock.base); 220 221 ttm_bo_del_from_lru(bo); 222 ttm_bo_add_to_lru(bo); 223 } 224 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail); 225 226 /* 227 * Call bo->mutex locked. 228 */ 229 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) 230 { 231 struct ttm_bo_device *bdev = bo->bdev; 232 struct ttm_bo_global *glob = bo->glob; 233 int ret = 0; 234 uint32_t page_flags = 0; 235 236 TTM_ASSERT_LOCKED(&bo->mutex); 237 bo->ttm = NULL; 238 239 if (bdev->need_dma32) 240 page_flags |= TTM_PAGE_FLAG_DMA32; 241 242 switch (bo->type) { 243 case ttm_bo_type_device: 244 if (zero_alloc) 245 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; 246 case ttm_bo_type_kernel: 247 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, 248 page_flags, glob->dummy_read_page); 249 if (unlikely(bo->ttm == NULL)) 250 ret = -ENOMEM; 251 break; 252 case ttm_bo_type_sg: 253 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, 254 page_flags | TTM_PAGE_FLAG_SG, 255 glob->dummy_read_page); 256 if (unlikely(bo->ttm == NULL)) { 257 ret = -ENOMEM; 258 break; 259 } 260 bo->ttm->sg = bo->sg; 261 break; 262 default: 263 pr_err("Illegal buffer object type\n"); 264 ret = -EINVAL; 265 break; 266 } 267 268 return ret; 269 } 270 271 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, 272 struct ttm_mem_reg *mem, 273 bool evict, bool interruptible, 274 bool no_wait_gpu) 275 { 276 struct ttm_bo_device *bdev = bo->bdev; 277 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); 278 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); 279 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; 280 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; 281 int ret = 0; 282 283 if (old_is_pci || new_is_pci || 284 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { 285 ret = ttm_mem_io_lock(old_man, true); 286 if (unlikely(ret != 0)) 287 goto out_err; 288 ttm_bo_unmap_virtual_locked(bo); 289 ttm_mem_io_unlock(old_man); 290 } 291 292 /* 293 * Create and bind a ttm if required. 294 */ 295 296 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { 297 if (bo->ttm == NULL) { 298 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); 299 ret = ttm_bo_add_ttm(bo, zero); 300 if (ret) 301 goto out_err; 302 } 303 304 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); 305 if (ret) 306 goto out_err; 307 308 if (mem->mem_type != TTM_PL_SYSTEM) { 309 ret = ttm_tt_bind(bo->ttm, mem); 310 if (ret) 311 goto out_err; 312 } 313 314 if (bo->mem.mem_type == TTM_PL_SYSTEM) { 315 if (bdev->driver->move_notify) 316 bdev->driver->move_notify(bo, evict, mem); 317 bo->mem = *mem; 318 mem->mm_node = NULL; 319 goto moved; 320 } 321 } 322 323 if (bdev->driver->move_notify) 324 bdev->driver->move_notify(bo, evict, mem); 325 326 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && 327 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) 328 ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem); 329 else if (bdev->driver->move) 330 ret = bdev->driver->move(bo, evict, interruptible, 331 no_wait_gpu, mem); 332 else 333 ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem); 334 335 if (ret) { 336 if (bdev->driver->move_notify) { 337 struct ttm_mem_reg tmp_mem = *mem; 338 *mem = bo->mem; 339 bo->mem = tmp_mem; 340 bdev->driver->move_notify(bo, false, mem); 341 bo->mem = *mem; 342 *mem = tmp_mem; 343 } 344 345 goto out_err; 346 } 347 348 moved: 349 if (bo->evicted) { 350 if (bdev->driver->invalidate_caches) { 351 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); 352 if (ret) 353 pr_err("Can not flush read caches\n"); 354 } 355 bo->evicted = false; 356 } 357 358 if (bo->mem.mm_node) { 359 bo->offset = (bo->mem.start << PAGE_SHIFT) + 360 bdev->man[bo->mem.mem_type].gpu_offset; 361 bo->cur_placement = bo->mem.placement; 362 } else 363 bo->offset = 0; 364 365 return 0; 366 367 out_err: 368 new_man = &bdev->man[bo->mem.mem_type]; 369 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) { 370 ttm_tt_destroy(bo->ttm); 371 bo->ttm = NULL; 372 } 373 374 return ret; 375 } 376 377 /** 378 * Call bo::reserved. 379 * Will release GPU memory type usage on destruction. 380 * This is the place to put in driver specific hooks to release 381 * driver private resources. 382 * Will release the bo::reserved lock. 383 */ 384 385 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) 386 { 387 if (bo->bdev->driver->move_notify) 388 bo->bdev->driver->move_notify(bo, false, NULL); 389 390 ttm_tt_destroy(bo->ttm); 391 bo->ttm = NULL; 392 ttm_bo_mem_put(bo, &bo->mem); 393 394 ww_mutex_unlock (&bo->resv->lock); 395 } 396 397 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) 398 { 399 struct reservation_object_list *fobj; 400 struct dma_fence *fence; 401 int i; 402 403 fobj = reservation_object_get_list(bo->resv); 404 fence = reservation_object_get_excl(bo->resv); 405 if (fence && !fence->ops->signaled) 406 dma_fence_enable_sw_signaling(fence); 407 408 for (i = 0; fobj && i < fobj->shared_count; ++i) { 409 fence = rcu_dereference_protected(fobj->shared[i], 410 reservation_object_held(bo->resv)); 411 412 if (!fence->ops->signaled) 413 dma_fence_enable_sw_signaling(fence); 414 } 415 } 416 417 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) 418 { 419 struct ttm_bo_device *bdev = bo->bdev; 420 struct ttm_bo_global *glob = bo->glob; 421 int ret; 422 423 spin_lock(&glob->lru_lock); 424 ret = __ttm_bo_reserve(bo, false, true, NULL); 425 426 if (!ret) { 427 if (!ttm_bo_wait(bo, false, true)) { 428 ttm_bo_del_from_lru(bo); 429 spin_unlock(&glob->lru_lock); 430 ttm_bo_cleanup_memtype_use(bo); 431 432 return; 433 } else 434 ttm_bo_flush_all_fences(bo); 435 436 /* 437 * Make NO_EVICT bos immediately available to 438 * shrinkers, now that they are queued for 439 * destruction. 440 */ 441 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { 442 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; 443 ttm_bo_add_to_lru(bo); 444 } 445 446 __ttm_bo_unreserve(bo); 447 } 448 449 kref_get(&bo->list_kref); 450 list_add_tail(&bo->ddestroy, &bdev->ddestroy); 451 spin_unlock(&glob->lru_lock); 452 453 schedule_delayed_work(&bdev->wq, 454 ((HZ / 100) < 1) ? 1 : HZ / 100); 455 } 456 457 /** 458 * function ttm_bo_cleanup_refs_and_unlock 459 * If bo idle, remove from delayed- and lru lists, and unref. 460 * If not idle, do nothing. 461 * 462 * Must be called with lru_lock and reservation held, this function 463 * will drop both before returning. 464 * 465 * @interruptible Any sleeps should occur interruptibly. 466 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. 467 */ 468 469 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, 470 bool interruptible, 471 bool no_wait_gpu) 472 { 473 struct ttm_bo_global *glob = bo->glob; 474 int ret; 475 476 ret = ttm_bo_wait(bo, false, true); 477 478 if (ret && !no_wait_gpu) { 479 long lret; 480 ww_mutex_unlock(&bo->resv->lock); 481 spin_unlock(&glob->lru_lock); 482 483 lret = reservation_object_wait_timeout_rcu(bo->resv, 484 true, 485 interruptible, 486 30 * HZ); 487 488 if (lret < 0) 489 return lret; 490 else if (lret == 0) 491 return -EBUSY; 492 493 spin_lock(&glob->lru_lock); 494 ret = __ttm_bo_reserve(bo, false, true, NULL); 495 496 /* 497 * We raced, and lost, someone else holds the reservation now, 498 * and is probably busy in ttm_bo_cleanup_memtype_use. 499 * 500 * Even if it's not the case, because we finished waiting any 501 * delayed destruction would succeed, so just return success 502 * here. 503 */ 504 if (ret) { 505 spin_unlock(&glob->lru_lock); 506 return 0; 507 } 508 509 /* 510 * remove sync_obj with ttm_bo_wait, the wait should be 511 * finished, and no new wait object should have been added. 512 */ 513 ret = ttm_bo_wait(bo, false, true); 514 WARN_ON(ret); 515 } 516 517 if (ret || unlikely(list_empty(&bo->ddestroy))) { 518 __ttm_bo_unreserve(bo); 519 spin_unlock(&glob->lru_lock); 520 return ret; 521 } 522 523 ttm_bo_del_from_lru(bo); 524 list_del_init(&bo->ddestroy); 525 kref_put(&bo->list_kref, ttm_bo_ref_bug); 526 527 spin_unlock(&glob->lru_lock); 528 ttm_bo_cleanup_memtype_use(bo); 529 530 return 0; 531 } 532 533 /** 534 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all 535 * encountered buffers. 536 */ 537 538 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) 539 { 540 struct ttm_bo_global *glob = bdev->glob; 541 struct ttm_buffer_object *entry = NULL; 542 int ret = 0; 543 544 spin_lock(&glob->lru_lock); 545 if (list_empty(&bdev->ddestroy)) 546 goto out_unlock; 547 548 entry = list_first_entry(&bdev->ddestroy, 549 struct ttm_buffer_object, ddestroy); 550 kref_get(&entry->list_kref); 551 552 for (;;) { 553 struct ttm_buffer_object *nentry = NULL; 554 555 if (entry->ddestroy.next != &bdev->ddestroy) { 556 nentry = list_first_entry(&entry->ddestroy, 557 struct ttm_buffer_object, ddestroy); 558 kref_get(&nentry->list_kref); 559 } 560 561 ret = __ttm_bo_reserve(entry, false, true, NULL); 562 if (remove_all && ret) { 563 spin_unlock(&glob->lru_lock); 564 ret = __ttm_bo_reserve(entry, false, false, NULL); 565 spin_lock(&glob->lru_lock); 566 } 567 568 if (!ret) 569 ret = ttm_bo_cleanup_refs_and_unlock(entry, false, 570 !remove_all); 571 else 572 spin_unlock(&glob->lru_lock); 573 574 kref_put(&entry->list_kref, ttm_bo_release_list); 575 entry = nentry; 576 577 if (ret || !entry) 578 goto out; 579 580 spin_lock(&glob->lru_lock); 581 if (list_empty(&entry->ddestroy)) 582 break; 583 } 584 585 out_unlock: 586 spin_unlock(&glob->lru_lock); 587 out: 588 if (entry) 589 kref_put(&entry->list_kref, ttm_bo_release_list); 590 return ret; 591 } 592 593 static void ttm_bo_delayed_workqueue(struct work_struct *work) 594 { 595 struct ttm_bo_device *bdev = 596 container_of(work, struct ttm_bo_device, wq.work); 597 598 if (ttm_bo_delayed_delete(bdev, false)) { 599 schedule_delayed_work(&bdev->wq, 600 ((HZ / 100) < 1) ? 1 : HZ / 100); 601 } 602 } 603 604 static void ttm_bo_release(struct kref *kref) 605 { 606 struct ttm_buffer_object *bo = 607 container_of(kref, struct ttm_buffer_object, kref); 608 struct ttm_bo_device *bdev = bo->bdev; 609 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; 610 611 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node); 612 ttm_mem_io_lock(man, false); 613 ttm_mem_io_free_vm(bo); 614 ttm_mem_io_unlock(man); 615 ttm_bo_cleanup_refs_or_queue(bo); 616 kref_put(&bo->list_kref, ttm_bo_release_list); 617 } 618 619 void ttm_bo_unref(struct ttm_buffer_object **p_bo) 620 { 621 struct ttm_buffer_object *bo = *p_bo; 622 623 *p_bo = NULL; 624 kref_put(&bo->kref, ttm_bo_release); 625 } 626 EXPORT_SYMBOL(ttm_bo_unref); 627 628 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) 629 { 630 return cancel_delayed_work_sync(&bdev->wq); 631 } 632 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); 633 634 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) 635 { 636 if (resched) 637 schedule_delayed_work(&bdev->wq, 638 ((HZ / 100) < 1) ? 1 : HZ / 100); 639 } 640 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); 641 642 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, 643 bool no_wait_gpu) 644 { 645 struct ttm_bo_device *bdev = bo->bdev; 646 struct ttm_mem_reg evict_mem; 647 struct ttm_placement placement; 648 int ret = 0; 649 650 lockdep_assert_held(&bo->resv->lock.base); 651 652 evict_mem = bo->mem; 653 evict_mem.mm_node = NULL; 654 evict_mem.bus.io_reserved_vm = false; 655 evict_mem.bus.io_reserved_count = 0; 656 657 placement.num_placement = 0; 658 placement.num_busy_placement = 0; 659 bdev->driver->evict_flags(bo, &placement); 660 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, 661 no_wait_gpu); 662 if (ret) { 663 if (ret != -ERESTARTSYS) { 664 pr_err("Failed to find memory space for buffer 0x%p eviction\n", 665 bo); 666 ttm_bo_mem_space_debug(bo, &placement); 667 } 668 goto out; 669 } 670 671 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, 672 no_wait_gpu); 673 if (unlikely(ret)) { 674 if (ret != -ERESTARTSYS) 675 pr_err("Buffer eviction failed\n"); 676 ttm_bo_mem_put(bo, &evict_mem); 677 goto out; 678 } 679 bo->evicted = true; 680 out: 681 return ret; 682 } 683 684 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 685 const struct ttm_place *place) 686 { 687 /* Don't evict this BO if it's outside of the 688 * requested placement range 689 */ 690 if (place->fpfn >= (bo->mem.start + bo->mem.size) || 691 (place->lpfn && place->lpfn <= bo->mem.start)) 692 return false; 693 694 return true; 695 } 696 EXPORT_SYMBOL(ttm_bo_eviction_valuable); 697 698 static int ttm_mem_evict_first(struct ttm_bo_device *bdev, 699 uint32_t mem_type, 700 const struct ttm_place *place, 701 bool interruptible, 702 bool no_wait_gpu) 703 { 704 struct ttm_bo_global *glob = bdev->glob; 705 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 706 struct ttm_buffer_object *bo; 707 int ret = -EBUSY; 708 unsigned i; 709 710 spin_lock(&glob->lru_lock); 711 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 712 list_for_each_entry(bo, &man->lru[i], lru) { 713 ret = __ttm_bo_reserve(bo, false, true, NULL); 714 if (ret) 715 continue; 716 717 if (place && !bdev->driver->eviction_valuable(bo, 718 place)) { 719 __ttm_bo_unreserve(bo); 720 ret = -EBUSY; 721 continue; 722 } 723 724 break; 725 } 726 727 if (!ret) 728 break; 729 } 730 731 if (ret) { 732 spin_unlock(&glob->lru_lock); 733 return ret; 734 } 735 736 kref_get(&bo->list_kref); 737 738 if (!list_empty(&bo->ddestroy)) { 739 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, 740 no_wait_gpu); 741 kref_put(&bo->list_kref, ttm_bo_release_list); 742 return ret; 743 } 744 745 ttm_bo_del_from_lru(bo); 746 spin_unlock(&glob->lru_lock); 747 748 BUG_ON(ret != 0); 749 750 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); 751 ttm_bo_unreserve(bo); 752 753 kref_put(&bo->list_kref, ttm_bo_release_list); 754 return ret; 755 } 756 757 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) 758 { 759 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; 760 761 if (mem->mm_node) 762 (*man->func->put_node)(man, mem); 763 } 764 EXPORT_SYMBOL(ttm_bo_mem_put); 765 766 /** 767 * Add the last move fence to the BO and reserve a new shared slot. 768 */ 769 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo, 770 struct ttm_mem_type_manager *man, 771 struct ttm_mem_reg *mem) 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 reservation_object_add_shared_fence(bo->resv, fence); 782 783 ret = reservation_object_reserve_shared(bo->resv); 784 if (unlikely(ret)) 785 return ret; 786 787 dma_fence_put(bo->moving); 788 bo->moving = fence; 789 } 790 791 return 0; 792 } 793 794 /** 795 * Repeatedly evict memory from the LRU for @mem_type until we create enough 796 * space, or we've evicted everything and there isn't enough space. 797 */ 798 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, 799 uint32_t mem_type, 800 const struct ttm_place *place, 801 struct ttm_mem_reg *mem, 802 bool interruptible, 803 bool no_wait_gpu) 804 { 805 struct ttm_bo_device *bdev = bo->bdev; 806 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 807 int ret; 808 809 do { 810 ret = (*man->func->get_node)(man, bo, place, mem); 811 if (unlikely(ret != 0)) 812 return ret; 813 if (mem->mm_node) 814 break; 815 ret = ttm_mem_evict_first(bdev, mem_type, place, 816 interruptible, no_wait_gpu); 817 if (unlikely(ret != 0)) 818 return ret; 819 } while (1); 820 mem->mem_type = mem_type; 821 return ttm_bo_add_move_fence(bo, man, mem); 822 } 823 824 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, 825 uint32_t cur_placement, 826 uint32_t proposed_placement) 827 { 828 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; 829 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; 830 831 /** 832 * Keep current caching if possible. 833 */ 834 835 if ((cur_placement & caching) != 0) 836 result |= (cur_placement & caching); 837 else if ((man->default_caching & caching) != 0) 838 result |= man->default_caching; 839 else if ((TTM_PL_FLAG_CACHED & caching) != 0) 840 result |= TTM_PL_FLAG_CACHED; 841 else if ((TTM_PL_FLAG_WC & caching) != 0) 842 result |= TTM_PL_FLAG_WC; 843 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) 844 result |= TTM_PL_FLAG_UNCACHED; 845 846 return result; 847 } 848 849 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, 850 uint32_t mem_type, 851 const struct ttm_place *place, 852 uint32_t *masked_placement) 853 { 854 uint32_t cur_flags = ttm_bo_type_flags(mem_type); 855 856 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0) 857 return false; 858 859 if ((place->flags & man->available_caching) == 0) 860 return false; 861 862 cur_flags |= (place->flags & man->available_caching); 863 864 *masked_placement = cur_flags; 865 return true; 866 } 867 868 /** 869 * Creates space for memory region @mem according to its type. 870 * 871 * This function first searches for free space in compatible memory types in 872 * the priority order defined by the driver. If free space isn't found, then 873 * ttm_bo_mem_force_space is attempted in priority order to evict and find 874 * space. 875 */ 876 int ttm_bo_mem_space(struct ttm_buffer_object *bo, 877 struct ttm_placement *placement, 878 struct ttm_mem_reg *mem, 879 bool interruptible, 880 bool no_wait_gpu) 881 { 882 struct ttm_bo_device *bdev = bo->bdev; 883 struct ttm_mem_type_manager *man; 884 uint32_t mem_type = TTM_PL_SYSTEM; 885 uint32_t cur_flags = 0; 886 bool type_found = false; 887 bool type_ok = false; 888 bool has_erestartsys = false; 889 int i, ret; 890 891 ret = reservation_object_reserve_shared(bo->resv); 892 if (unlikely(ret)) 893 return ret; 894 895 mem->mm_node = NULL; 896 for (i = 0; i < placement->num_placement; ++i) { 897 const struct ttm_place *place = &placement->placement[i]; 898 899 ret = ttm_mem_type_from_place(place, &mem_type); 900 if (ret) 901 return ret; 902 man = &bdev->man[mem_type]; 903 if (!man->has_type || !man->use_type) 904 continue; 905 906 type_ok = ttm_bo_mt_compatible(man, mem_type, place, 907 &cur_flags); 908 909 if (!type_ok) 910 continue; 911 912 type_found = true; 913 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, 914 cur_flags); 915 /* 916 * Use the access and other non-mapping-related flag bits from 917 * the memory placement flags to the current flags 918 */ 919 ttm_flag_masked(&cur_flags, place->flags, 920 ~TTM_PL_MASK_MEMTYPE); 921 922 if (mem_type == TTM_PL_SYSTEM) 923 break; 924 925 ret = (*man->func->get_node)(man, bo, place, mem); 926 if (unlikely(ret)) 927 return ret; 928 929 if (mem->mm_node) { 930 ret = ttm_bo_add_move_fence(bo, man, mem); 931 if (unlikely(ret)) { 932 (*man->func->put_node)(man, mem); 933 return ret; 934 } 935 break; 936 } 937 } 938 939 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { 940 mem->mem_type = mem_type; 941 mem->placement = cur_flags; 942 return 0; 943 } 944 945 for (i = 0; i < placement->num_busy_placement; ++i) { 946 const struct ttm_place *place = &placement->busy_placement[i]; 947 948 ret = ttm_mem_type_from_place(place, &mem_type); 949 if (ret) 950 return ret; 951 man = &bdev->man[mem_type]; 952 if (!man->has_type || !man->use_type) 953 continue; 954 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags)) 955 continue; 956 957 type_found = true; 958 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, 959 cur_flags); 960 /* 961 * Use the access and other non-mapping-related flag bits from 962 * the memory placement flags to the current flags 963 */ 964 ttm_flag_masked(&cur_flags, place->flags, 965 ~TTM_PL_MASK_MEMTYPE); 966 967 if (mem_type == TTM_PL_SYSTEM) { 968 mem->mem_type = mem_type; 969 mem->placement = cur_flags; 970 mem->mm_node = NULL; 971 return 0; 972 } 973 974 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem, 975 interruptible, no_wait_gpu); 976 if (ret == 0 && mem->mm_node) { 977 mem->placement = cur_flags; 978 return 0; 979 } 980 if (ret == -ERESTARTSYS) 981 has_erestartsys = true; 982 } 983 984 if (!type_found) { 985 pr_err(TTM_PFX "No compatible memory type found\n"); 986 return -EINVAL; 987 } 988 989 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; 990 } 991 EXPORT_SYMBOL(ttm_bo_mem_space); 992 993 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, 994 struct ttm_placement *placement, 995 bool interruptible, 996 bool no_wait_gpu) 997 { 998 int ret = 0; 999 struct ttm_mem_reg mem; 1000 1001 lockdep_assert_held(&bo->resv->lock.base); 1002 1003 mem.num_pages = bo->num_pages; 1004 mem.size = mem.num_pages << PAGE_SHIFT; 1005 mem.page_alignment = bo->mem.page_alignment; 1006 mem.bus.io_reserved_vm = false; 1007 mem.bus.io_reserved_count = 0; 1008 /* 1009 * Determine where to move the buffer. 1010 */ 1011 ret = ttm_bo_mem_space(bo, placement, &mem, 1012 interruptible, no_wait_gpu); 1013 if (ret) 1014 goto out_unlock; 1015 ret = ttm_bo_handle_move_mem(bo, &mem, false, 1016 interruptible, no_wait_gpu); 1017 out_unlock: 1018 if (ret && mem.mm_node) 1019 ttm_bo_mem_put(bo, &mem); 1020 return ret; 1021 } 1022 1023 static bool ttm_bo_places_compat(const struct ttm_place *places, 1024 unsigned num_placement, 1025 struct ttm_mem_reg *mem, 1026 uint32_t *new_flags) 1027 { 1028 unsigned i; 1029 1030 for (i = 0; i < num_placement; i++) { 1031 const struct ttm_place *heap = &places[i]; 1032 1033 if (mem->mm_node && (mem->start < heap->fpfn || 1034 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn))) 1035 continue; 1036 1037 *new_flags = heap->flags; 1038 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && 1039 (*new_flags & mem->placement & TTM_PL_MASK_MEM) && 1040 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) || 1041 (mem->placement & TTM_PL_FLAG_CONTIGUOUS))) 1042 return true; 1043 } 1044 return false; 1045 } 1046 1047 bool ttm_bo_mem_compat(struct ttm_placement *placement, 1048 struct ttm_mem_reg *mem, 1049 uint32_t *new_flags) 1050 { 1051 if (ttm_bo_places_compat(placement->placement, placement->num_placement, 1052 mem, new_flags)) 1053 return true; 1054 1055 if ((placement->busy_placement != placement->placement || 1056 placement->num_busy_placement > placement->num_placement) && 1057 ttm_bo_places_compat(placement->busy_placement, 1058 placement->num_busy_placement, 1059 mem, new_flags)) 1060 return true; 1061 1062 return false; 1063 } 1064 EXPORT_SYMBOL(ttm_bo_mem_compat); 1065 1066 int ttm_bo_validate(struct ttm_buffer_object *bo, 1067 struct ttm_placement *placement, 1068 bool interruptible, 1069 bool no_wait_gpu) 1070 { 1071 int ret; 1072 uint32_t new_flags; 1073 1074 lockdep_assert_held(&bo->resv->lock.base); 1075 /* 1076 * Check whether we need to move buffer. 1077 */ 1078 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { 1079 ret = ttm_bo_move_buffer(bo, placement, interruptible, 1080 no_wait_gpu); 1081 if (ret) 1082 return ret; 1083 } else { 1084 /* 1085 * Use the access and other non-mapping-related flag bits from 1086 * the compatible memory placement flags to the active flags 1087 */ 1088 ttm_flag_masked(&bo->mem.placement, new_flags, 1089 ~TTM_PL_MASK_MEMTYPE); 1090 } 1091 /* 1092 * We might need to add a TTM. 1093 */ 1094 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { 1095 ret = ttm_bo_add_ttm(bo, true); 1096 if (ret) 1097 return ret; 1098 } 1099 return 0; 1100 } 1101 EXPORT_SYMBOL(ttm_bo_validate); 1102 1103 int ttm_bo_init_reserved(struct ttm_bo_device *bdev, 1104 struct ttm_buffer_object *bo, 1105 unsigned long size, 1106 enum ttm_bo_type type, 1107 struct ttm_placement *placement, 1108 uint32_t page_alignment, 1109 bool interruptible, 1110 struct file *persistent_swap_storage, 1111 size_t acc_size, 1112 struct sg_table *sg, 1113 struct reservation_object *resv, 1114 void (*destroy) (struct ttm_buffer_object *)) 1115 { 1116 int ret = 0; 1117 unsigned long num_pages; 1118 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; 1119 bool locked; 1120 1121 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); 1122 if (ret) { 1123 pr_err("Out of kernel memory\n"); 1124 if (destroy) 1125 (*destroy)(bo); 1126 else 1127 kfree(bo); 1128 return -ENOMEM; 1129 } 1130 1131 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1132 if (num_pages == 0) { 1133 pr_err("Illegal buffer object size\n"); 1134 if (destroy) 1135 (*destroy)(bo); 1136 else 1137 kfree(bo); 1138 ttm_mem_global_free(mem_glob, acc_size); 1139 return -EINVAL; 1140 } 1141 bo->destroy = destroy; 1142 1143 kref_init(&bo->kref); 1144 kref_init(&bo->list_kref); 1145 atomic_set(&bo->cpu_writers, 0); 1146 INIT_LIST_HEAD(&bo->lru); 1147 INIT_LIST_HEAD(&bo->ddestroy); 1148 INIT_LIST_HEAD(&bo->swap); 1149 INIT_LIST_HEAD(&bo->io_reserve_lru); 1150 mutex_init(&bo->wu_mutex); 1151 bo->bdev = bdev; 1152 bo->glob = bdev->glob; 1153 bo->type = type; 1154 bo->num_pages = num_pages; 1155 bo->mem.size = num_pages << PAGE_SHIFT; 1156 bo->mem.mem_type = TTM_PL_SYSTEM; 1157 bo->mem.num_pages = bo->num_pages; 1158 bo->mem.mm_node = NULL; 1159 bo->mem.page_alignment = page_alignment; 1160 bo->mem.bus.io_reserved_vm = false; 1161 bo->mem.bus.io_reserved_count = 0; 1162 bo->moving = NULL; 1163 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); 1164 bo->persistent_swap_storage = persistent_swap_storage; 1165 bo->acc_size = acc_size; 1166 bo->sg = sg; 1167 if (resv) { 1168 bo->resv = resv; 1169 lockdep_assert_held(&bo->resv->lock.base); 1170 } else { 1171 bo->resv = &bo->ttm_resv; 1172 reservation_object_init(&bo->ttm_resv); 1173 } 1174 atomic_inc(&bo->glob->bo_count); 1175 drm_vma_node_reset(&bo->vma_node); 1176 bo->priority = 0; 1177 1178 /* 1179 * For ttm_bo_type_device buffers, allocate 1180 * address space from the device. 1181 */ 1182 if (bo->type == ttm_bo_type_device || 1183 bo->type == ttm_bo_type_sg) 1184 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node, 1185 bo->mem.num_pages); 1186 1187 /* passed reservation objects should already be locked, 1188 * since otherwise lockdep will be angered in radeon. 1189 */ 1190 if (!resv) { 1191 locked = ww_mutex_trylock(&bo->resv->lock); 1192 WARN_ON(!locked); 1193 } 1194 1195 if (likely(!ret)) 1196 ret = ttm_bo_validate(bo, placement, interruptible, false); 1197 1198 if (unlikely(ret)) { 1199 if (!resv) 1200 ttm_bo_unreserve(bo); 1201 1202 ttm_bo_unref(&bo); 1203 return ret; 1204 } 1205 1206 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { 1207 spin_lock(&bo->glob->lru_lock); 1208 ttm_bo_add_to_lru(bo); 1209 spin_unlock(&bo->glob->lru_lock); 1210 } 1211 1212 return ret; 1213 } 1214 EXPORT_SYMBOL(ttm_bo_init_reserved); 1215 1216 int ttm_bo_init(struct ttm_bo_device *bdev, 1217 struct ttm_buffer_object *bo, 1218 unsigned long size, 1219 enum ttm_bo_type type, 1220 struct ttm_placement *placement, 1221 uint32_t page_alignment, 1222 bool interruptible, 1223 struct file *persistent_swap_storage, 1224 size_t acc_size, 1225 struct sg_table *sg, 1226 struct reservation_object *resv, 1227 void (*destroy) (struct ttm_buffer_object *)) 1228 { 1229 int ret; 1230 1231 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement, 1232 page_alignment, interruptible, 1233 persistent_swap_storage, acc_size, 1234 sg, resv, destroy); 1235 if (ret) 1236 return ret; 1237 1238 if (!resv) 1239 ttm_bo_unreserve(bo); 1240 1241 return 0; 1242 } 1243 EXPORT_SYMBOL(ttm_bo_init); 1244 1245 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, 1246 unsigned long bo_size, 1247 unsigned struct_size) 1248 { 1249 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1250 size_t size = 0; 1251 1252 size += ttm_round_pot(struct_size); 1253 size += ttm_round_pot(npages * sizeof(void *)); 1254 size += ttm_round_pot(sizeof(struct ttm_tt)); 1255 return size; 1256 } 1257 EXPORT_SYMBOL(ttm_bo_acc_size); 1258 1259 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, 1260 unsigned long bo_size, 1261 unsigned struct_size) 1262 { 1263 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1264 size_t size = 0; 1265 1266 size += ttm_round_pot(struct_size); 1267 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t))); 1268 size += ttm_round_pot(sizeof(struct ttm_dma_tt)); 1269 return size; 1270 } 1271 EXPORT_SYMBOL(ttm_bo_dma_acc_size); 1272 1273 int ttm_bo_create(struct ttm_bo_device *bdev, 1274 unsigned long size, 1275 enum ttm_bo_type type, 1276 struct ttm_placement *placement, 1277 uint32_t page_alignment, 1278 bool interruptible, 1279 struct file *persistent_swap_storage, 1280 struct ttm_buffer_object **p_bo) 1281 { 1282 struct ttm_buffer_object *bo; 1283 size_t acc_size; 1284 int ret; 1285 1286 bo = kzalloc(sizeof(*bo), GFP_KERNEL); 1287 if (unlikely(bo == NULL)) 1288 return -ENOMEM; 1289 1290 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object)); 1291 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, 1292 interruptible, persistent_swap_storage, acc_size, 1293 NULL, NULL, NULL); 1294 if (likely(ret == 0)) 1295 *p_bo = bo; 1296 1297 return ret; 1298 } 1299 EXPORT_SYMBOL(ttm_bo_create); 1300 1301 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, 1302 unsigned mem_type) 1303 { 1304 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 1305 struct ttm_bo_global *glob = bdev->glob; 1306 struct dma_fence *fence; 1307 int ret; 1308 unsigned i; 1309 1310 /* 1311 * Can't use standard list traversal since we're unlocking. 1312 */ 1313 1314 spin_lock(&glob->lru_lock); 1315 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 1316 while (!list_empty(&man->lru[i])) { 1317 spin_unlock(&glob->lru_lock); 1318 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false); 1319 if (ret) 1320 return ret; 1321 spin_lock(&glob->lru_lock); 1322 } 1323 } 1324 spin_unlock(&glob->lru_lock); 1325 1326 spin_lock(&man->move_lock); 1327 fence = dma_fence_get(man->move); 1328 spin_unlock(&man->move_lock); 1329 1330 if (fence) { 1331 ret = dma_fence_wait(fence, false); 1332 dma_fence_put(fence); 1333 if (ret) 1334 return ret; 1335 } 1336 1337 return 0; 1338 } 1339 1340 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) 1341 { 1342 struct ttm_mem_type_manager *man; 1343 int ret = -EINVAL; 1344 1345 if (mem_type >= TTM_NUM_MEM_TYPES) { 1346 pr_err("Illegal memory type %d\n", mem_type); 1347 return ret; 1348 } 1349 man = &bdev->man[mem_type]; 1350 1351 if (!man->has_type) { 1352 pr_err("Trying to take down uninitialized memory manager type %u\n", 1353 mem_type); 1354 return ret; 1355 } 1356 1357 man->use_type = false; 1358 man->has_type = false; 1359 1360 ret = 0; 1361 if (mem_type > 0) { 1362 ret = ttm_bo_force_list_clean(bdev, mem_type); 1363 if (ret) { 1364 pr_err("Cleanup eviction failed\n"); 1365 return ret; 1366 } 1367 1368 ret = (*man->func->takedown)(man); 1369 } 1370 1371 dma_fence_put(man->move); 1372 man->move = NULL; 1373 1374 return ret; 1375 } 1376 EXPORT_SYMBOL(ttm_bo_clean_mm); 1377 1378 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) 1379 { 1380 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 1381 1382 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { 1383 pr_err("Illegal memory manager memory type %u\n", mem_type); 1384 return -EINVAL; 1385 } 1386 1387 if (!man->has_type) { 1388 pr_err("Memory type %u has not been initialized\n", mem_type); 1389 return 0; 1390 } 1391 1392 return ttm_bo_force_list_clean(bdev, mem_type); 1393 } 1394 EXPORT_SYMBOL(ttm_bo_evict_mm); 1395 1396 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, 1397 unsigned long p_size) 1398 { 1399 int ret; 1400 struct ttm_mem_type_manager *man; 1401 unsigned i; 1402 1403 BUG_ON(type >= TTM_NUM_MEM_TYPES); 1404 man = &bdev->man[type]; 1405 BUG_ON(man->has_type); 1406 man->io_reserve_fastpath = true; 1407 man->use_io_reserve_lru = false; 1408 mutex_init(&man->io_reserve_mutex); 1409 spin_lock_init(&man->move_lock); 1410 INIT_LIST_HEAD(&man->io_reserve_lru); 1411 1412 ret = bdev->driver->init_mem_type(bdev, type, man); 1413 if (ret) 1414 return ret; 1415 man->bdev = bdev; 1416 1417 if (type != TTM_PL_SYSTEM) { 1418 ret = (*man->func->init)(man, p_size); 1419 if (ret) 1420 return ret; 1421 } 1422 man->has_type = true; 1423 man->use_type = true; 1424 man->size = p_size; 1425 1426 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1427 INIT_LIST_HEAD(&man->lru[i]); 1428 man->move = NULL; 1429 1430 return 0; 1431 } 1432 EXPORT_SYMBOL(ttm_bo_init_mm); 1433 1434 static void ttm_bo_global_kobj_release(struct kobject *kobj) 1435 { 1436 struct ttm_bo_global *glob = 1437 container_of(kobj, struct ttm_bo_global, kobj); 1438 1439 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); 1440 __free_page(glob->dummy_read_page); 1441 kfree(glob); 1442 } 1443 1444 void ttm_bo_global_release(struct drm_global_reference *ref) 1445 { 1446 struct ttm_bo_global *glob = ref->object; 1447 1448 kobject_del(&glob->kobj); 1449 kobject_put(&glob->kobj); 1450 } 1451 EXPORT_SYMBOL(ttm_bo_global_release); 1452 1453 int ttm_bo_global_init(struct drm_global_reference *ref) 1454 { 1455 struct ttm_bo_global_ref *bo_ref = 1456 container_of(ref, struct ttm_bo_global_ref, ref); 1457 struct ttm_bo_global *glob = ref->object; 1458 int ret; 1459 unsigned i; 1460 1461 mutex_init(&glob->device_list_mutex); 1462 spin_lock_init(&glob->lru_lock); 1463 glob->mem_glob = bo_ref->mem_glob; 1464 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); 1465 1466 if (unlikely(glob->dummy_read_page == NULL)) { 1467 ret = -ENOMEM; 1468 goto out_no_drp; 1469 } 1470 1471 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1472 INIT_LIST_HEAD(&glob->swap_lru[i]); 1473 INIT_LIST_HEAD(&glob->device_list); 1474 1475 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); 1476 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); 1477 if (unlikely(ret != 0)) { 1478 pr_err("Could not register buffer object swapout\n"); 1479 goto out_no_shrink; 1480 } 1481 1482 atomic_set(&glob->bo_count, 0); 1483 1484 ret = kobject_init_and_add( 1485 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); 1486 if (unlikely(ret != 0)) 1487 kobject_put(&glob->kobj); 1488 return ret; 1489 out_no_shrink: 1490 __free_page(glob->dummy_read_page); 1491 out_no_drp: 1492 kfree(glob); 1493 return ret; 1494 } 1495 EXPORT_SYMBOL(ttm_bo_global_init); 1496 1497 1498 int ttm_bo_device_release(struct ttm_bo_device *bdev) 1499 { 1500 int ret = 0; 1501 unsigned i = TTM_NUM_MEM_TYPES; 1502 struct ttm_mem_type_manager *man; 1503 struct ttm_bo_global *glob = bdev->glob; 1504 1505 while (i--) { 1506 man = &bdev->man[i]; 1507 if (man->has_type) { 1508 man->use_type = false; 1509 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { 1510 ret = -EBUSY; 1511 pr_err("DRM memory manager type %d is not clean\n", 1512 i); 1513 } 1514 man->has_type = false; 1515 } 1516 } 1517 1518 mutex_lock(&glob->device_list_mutex); 1519 list_del(&bdev->device_list); 1520 mutex_unlock(&glob->device_list_mutex); 1521 1522 cancel_delayed_work_sync(&bdev->wq); 1523 1524 while (ttm_bo_delayed_delete(bdev, true)) 1525 ; 1526 1527 spin_lock(&glob->lru_lock); 1528 if (list_empty(&bdev->ddestroy)) 1529 TTM_DEBUG("Delayed destroy list was clean\n"); 1530 1531 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1532 if (list_empty(&bdev->man[0].lru[0])) 1533 TTM_DEBUG("Swap list %d was clean\n", i); 1534 spin_unlock(&glob->lru_lock); 1535 1536 drm_vma_offset_manager_destroy(&bdev->vma_manager); 1537 1538 return ret; 1539 } 1540 EXPORT_SYMBOL(ttm_bo_device_release); 1541 1542 int ttm_bo_device_init(struct ttm_bo_device *bdev, 1543 struct ttm_bo_global *glob, 1544 struct ttm_bo_driver *driver, 1545 struct address_space *mapping, 1546 uint64_t file_page_offset, 1547 bool need_dma32) 1548 { 1549 int ret = -EINVAL; 1550 1551 bdev->driver = driver; 1552 1553 memset(bdev->man, 0, sizeof(bdev->man)); 1554 1555 /* 1556 * Initialize the system memory buffer type. 1557 * Other types need to be driver / IOCTL initialized. 1558 */ 1559 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); 1560 if (unlikely(ret != 0)) 1561 goto out_no_sys; 1562 1563 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset, 1564 0x10000000); 1565 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); 1566 INIT_LIST_HEAD(&bdev->ddestroy); 1567 bdev->dev_mapping = mapping; 1568 bdev->glob = glob; 1569 bdev->need_dma32 = need_dma32; 1570 mutex_lock(&glob->device_list_mutex); 1571 list_add_tail(&bdev->device_list, &glob->device_list); 1572 mutex_unlock(&glob->device_list_mutex); 1573 1574 return 0; 1575 out_no_sys: 1576 return ret; 1577 } 1578 EXPORT_SYMBOL(ttm_bo_device_init); 1579 1580 /* 1581 * buffer object vm functions. 1582 */ 1583 1584 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) 1585 { 1586 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 1587 1588 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { 1589 if (mem->mem_type == TTM_PL_SYSTEM) 1590 return false; 1591 1592 if (man->flags & TTM_MEMTYPE_FLAG_CMA) 1593 return false; 1594 1595 if (mem->placement & TTM_PL_FLAG_CACHED) 1596 return false; 1597 } 1598 return true; 1599 } 1600 1601 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) 1602 { 1603 struct ttm_bo_device *bdev = bo->bdev; 1604 1605 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping); 1606 ttm_mem_io_free_vm(bo); 1607 } 1608 1609 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) 1610 { 1611 struct ttm_bo_device *bdev = bo->bdev; 1612 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; 1613 1614 ttm_mem_io_lock(man, false); 1615 ttm_bo_unmap_virtual_locked(bo); 1616 ttm_mem_io_unlock(man); 1617 } 1618 1619 1620 EXPORT_SYMBOL(ttm_bo_unmap_virtual); 1621 1622 int ttm_bo_wait(struct ttm_buffer_object *bo, 1623 bool interruptible, bool no_wait) 1624 { 1625 long timeout = 15 * HZ; 1626 1627 if (no_wait) { 1628 if (reservation_object_test_signaled_rcu(bo->resv, true)) 1629 return 0; 1630 else 1631 return -EBUSY; 1632 } 1633 1634 timeout = reservation_object_wait_timeout_rcu(bo->resv, true, 1635 interruptible, timeout); 1636 if (timeout < 0) 1637 return timeout; 1638 1639 if (timeout == 0) 1640 return -EBUSY; 1641 1642 reservation_object_add_excl_fence(bo->resv, NULL); 1643 return 0; 1644 } 1645 EXPORT_SYMBOL(ttm_bo_wait); 1646 1647 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) 1648 { 1649 int ret = 0; 1650 1651 /* 1652 * Using ttm_bo_reserve makes sure the lru lists are updated. 1653 */ 1654 1655 ret = ttm_bo_reserve(bo, true, no_wait, NULL); 1656 if (unlikely(ret != 0)) 1657 return ret; 1658 ret = ttm_bo_wait(bo, true, no_wait); 1659 if (likely(ret == 0)) 1660 atomic_inc(&bo->cpu_writers); 1661 ttm_bo_unreserve(bo); 1662 return ret; 1663 } 1664 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); 1665 1666 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) 1667 { 1668 atomic_dec(&bo->cpu_writers); 1669 } 1670 EXPORT_SYMBOL(ttm_bo_synccpu_write_release); 1671 1672 /** 1673 * A buffer object shrink method that tries to swap out the first 1674 * buffer object on the bo_global::swap_lru list. 1675 */ 1676 1677 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) 1678 { 1679 struct ttm_bo_global *glob = 1680 container_of(shrink, struct ttm_bo_global, shrink); 1681 struct ttm_buffer_object *bo; 1682 int ret = -EBUSY; 1683 unsigned i; 1684 1685 spin_lock(&glob->lru_lock); 1686 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 1687 list_for_each_entry(bo, &glob->swap_lru[i], swap) { 1688 ret = __ttm_bo_reserve(bo, false, true, NULL); 1689 if (!ret) 1690 break; 1691 } 1692 if (!ret) 1693 break; 1694 } 1695 1696 if (ret) { 1697 spin_unlock(&glob->lru_lock); 1698 return ret; 1699 } 1700 1701 kref_get(&bo->list_kref); 1702 1703 if (!list_empty(&bo->ddestroy)) { 1704 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false); 1705 kref_put(&bo->list_kref, ttm_bo_release_list); 1706 return ret; 1707 } 1708 1709 ttm_bo_del_from_lru(bo); 1710 spin_unlock(&glob->lru_lock); 1711 1712 /** 1713 * Move to system cached 1714 */ 1715 1716 if (bo->mem.mem_type != TTM_PL_SYSTEM || 1717 bo->ttm->caching_state != tt_cached) { 1718 struct ttm_mem_reg evict_mem; 1719 1720 evict_mem = bo->mem; 1721 evict_mem.mm_node = NULL; 1722 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; 1723 evict_mem.mem_type = TTM_PL_SYSTEM; 1724 1725 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, 1726 false, false); 1727 if (unlikely(ret != 0)) 1728 goto out; 1729 } 1730 1731 /** 1732 * Make sure BO is idle. 1733 */ 1734 1735 ret = ttm_bo_wait(bo, false, false); 1736 if (unlikely(ret != 0)) 1737 goto out; 1738 1739 ttm_bo_unmap_virtual(bo); 1740 1741 /** 1742 * Swap out. Buffer will be swapped in again as soon as 1743 * anyone tries to access a ttm page. 1744 */ 1745 1746 if (bo->bdev->driver->swap_notify) 1747 bo->bdev->driver->swap_notify(bo); 1748 1749 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); 1750 out: 1751 1752 /** 1753 * 1754 * Unreserve without putting on LRU to avoid swapping out an 1755 * already swapped buffer. 1756 */ 1757 1758 __ttm_bo_unreserve(bo); 1759 kref_put(&bo->list_kref, ttm_bo_release_list); 1760 return ret; 1761 } 1762 1763 void ttm_bo_swapout_all(struct ttm_bo_device *bdev) 1764 { 1765 while (ttm_bo_swapout(&bdev->glob->shrink) == 0) 1766 ; 1767 } 1768 EXPORT_SYMBOL(ttm_bo_swapout_all); 1769 1770 /** 1771 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become 1772 * unreserved 1773 * 1774 * @bo: Pointer to buffer 1775 */ 1776 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo) 1777 { 1778 int ret; 1779 1780 /* 1781 * In the absense of a wait_unlocked API, 1782 * Use the bo::wu_mutex to avoid triggering livelocks due to 1783 * concurrent use of this function. Note that this use of 1784 * bo::wu_mutex can go away if we change locking order to 1785 * mmap_sem -> bo::reserve. 1786 */ 1787 ret = mutex_lock_interruptible(&bo->wu_mutex); 1788 if (unlikely(ret != 0)) 1789 return -ERESTARTSYS; 1790 if (!ww_mutex_is_locked(&bo->resv->lock)) 1791 goto out_unlock; 1792 ret = __ttm_bo_reserve(bo, true, false, NULL); 1793 if (unlikely(ret != 0)) 1794 goto out_unlock; 1795 __ttm_bo_unreserve(bo); 1796 1797 out_unlock: 1798 mutex_unlock(&bo->wu_mutex); 1799 return ret; 1800 } 1801