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/atomic.h> 37 #include <linux/errno.h> 38 #include <linux/export.h> 39 #include <linux/wait.h> 40 41 #define TTM_ASSERT_LOCKED(param) 42 #define TTM_DEBUG(fmt, arg...) 43 #define TTM_BO_HASH_ORDER 13 44 45 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo); 46 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); 47 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob); 48 49 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type) 50 { 51 int i; 52 53 for (i = 0; i <= TTM_PL_PRIV5; i++) 54 if (flags & (1 << i)) { 55 *mem_type = i; 56 return 0; 57 } 58 return -EINVAL; 59 } 60 61 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type) 62 { 63 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 64 65 kprintf(" has_type: %d\n", man->has_type); 66 kprintf(" use_type: %d\n", man->use_type); 67 kprintf(" flags: 0x%08X\n", man->flags); 68 kprintf(" gpu_offset: 0x%08lX\n", man->gpu_offset); 69 kprintf(" size: %ju\n", (uintmax_t)man->size); 70 kprintf(" available_caching: 0x%08X\n", man->available_caching); 71 kprintf(" default_caching: 0x%08X\n", man->default_caching); 72 if (mem_type != TTM_PL_SYSTEM) 73 (*man->func->debug)(man, TTM_PFX); 74 } 75 76 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, 77 struct ttm_placement *placement) 78 { 79 int i, ret, mem_type; 80 81 kprintf("No space for %p (%lu pages, %luK, %luM)\n", 82 bo, bo->mem.num_pages, bo->mem.size >> 10, 83 bo->mem.size >> 20); 84 for (i = 0; i < placement->num_placement; i++) { 85 ret = ttm_mem_type_from_flags(placement->placement[i], 86 &mem_type); 87 if (ret) 88 return; 89 kprintf(" placement[%d]=0x%08X (%d)\n", 90 i, placement->placement[i], mem_type); 91 ttm_mem_type_debug(bo->bdev, mem_type); 92 } 93 } 94 95 #if 0 96 static ssize_t ttm_bo_global_show(struct ttm_bo_global *glob, 97 char *buffer) 98 { 99 100 return snprintf(buffer, PAGE_SIZE, "%lu\n", 101 (unsigned long) atomic_read(&glob->bo_count)); 102 } 103 #endif 104 105 static inline uint32_t ttm_bo_type_flags(unsigned type) 106 { 107 return 1 << (type); 108 } 109 110 static void ttm_bo_release_list(struct kref *list_kref) 111 { 112 struct ttm_buffer_object *bo = 113 container_of(list_kref, struct ttm_buffer_object, list_kref); 114 struct ttm_bo_device *bdev = bo->bdev; 115 size_t acc_size = bo->acc_size; 116 117 BUG_ON(atomic_read(&bo->list_kref.refcount)); 118 BUG_ON(atomic_read(&bo->kref.refcount)); 119 BUG_ON(atomic_read(&bo->cpu_writers)); 120 BUG_ON(bo->sync_obj != NULL); 121 BUG_ON(bo->mem.mm_node != NULL); 122 BUG_ON(!list_empty(&bo->lru)); 123 BUG_ON(!list_empty(&bo->ddestroy)); 124 125 if (bo->ttm) 126 ttm_tt_destroy(bo->ttm); 127 atomic_dec(&bo->glob->bo_count); 128 if (bo->destroy) 129 bo->destroy(bo); 130 else { 131 kfree(bo); 132 } 133 ttm_mem_global_free(bdev->glob->mem_glob, acc_size); 134 } 135 136 static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, 137 bool interruptible) 138 { 139 if (interruptible) { 140 return wait_event_interruptible(bo->event_queue, 141 !ttm_bo_is_reserved(bo)); 142 } else { 143 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo)); 144 return 0; 145 } 146 } 147 148 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) 149 { 150 struct ttm_bo_device *bdev = bo->bdev; 151 struct ttm_mem_type_manager *man; 152 153 BUG_ON(!ttm_bo_is_reserved(bo)); 154 155 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { 156 157 BUG_ON(!list_empty(&bo->lru)); 158 159 man = &bdev->man[bo->mem.mem_type]; 160 list_add_tail(&bo->lru, &man->lru); 161 kref_get(&bo->list_kref); 162 163 if (bo->ttm != NULL) { 164 list_add_tail(&bo->swap, &bo->glob->swap_lru); 165 kref_get(&bo->list_kref); 166 } 167 } 168 } 169 170 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) 171 { 172 int put_count = 0; 173 174 if (!list_empty(&bo->swap)) { 175 list_del_init(&bo->swap); 176 ++put_count; 177 } 178 if (!list_empty(&bo->lru)) { 179 list_del_init(&bo->lru); 180 ++put_count; 181 } 182 183 /* 184 * TODO: Add a driver hook to delete from 185 * driver-specific LRU's here. 186 */ 187 188 return put_count; 189 } 190 191 int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo, 192 bool interruptible, 193 bool no_wait, bool use_ticket, 194 struct ww_acquire_ctx *ticket) 195 { 196 int ret; 197 198 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) { 199 /** 200 * Deadlock avoidance for multi-bo reserving. 201 */ 202 if (use_ticket && bo->seq_valid) { 203 /** 204 * We've already reserved this one. 205 */ 206 if (unlikely(ticket->stamp == bo->val_seq)) 207 return -EDEADLK; 208 /** 209 * Already reserved by a thread that will not back 210 * off for us. We need to back off. 211 */ 212 if (unlikely(ticket->stamp - bo->val_seq <= LONG_MAX)) 213 return -EAGAIN; 214 } 215 216 if (no_wait) 217 return -EBUSY; 218 219 ret = ttm_bo_wait_unreserved(bo, interruptible); 220 221 if (unlikely(ret)) 222 return ret; 223 } 224 225 if (use_ticket) { 226 bool wake_up = false; 227 228 /** 229 * Wake up waiters that may need to recheck for deadlock, 230 * if we decreased the sequence number. 231 */ 232 if (unlikely((bo->val_seq - ticket->stamp <= LONG_MAX) 233 || !bo->seq_valid)) 234 wake_up = true; 235 236 /* 237 * In the worst case with memory ordering these values can be 238 * seen in the wrong order. However since we call wake_up_all 239 * in that case, this will hopefully not pose a problem, 240 * and the worst case would only cause someone to accidentally 241 * hit -EAGAIN in ttm_bo_reserve when they see old value of 242 * val_seq. However this would only happen if seq_valid was 243 * written before val_seq was, and just means some slightly 244 * increased cpu usage 245 */ 246 bo->val_seq = ticket->stamp; 247 bo->seq_valid = true; 248 if (wake_up) 249 wake_up_all(&bo->event_queue); 250 } else { 251 bo->seq_valid = false; 252 } 253 254 return 0; 255 } 256 EXPORT_SYMBOL(ttm_bo_reserve); 257 258 static void ttm_bo_ref_bug(struct kref *list_kref) 259 { 260 BUG(); 261 } 262 263 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count, 264 bool never_free) 265 { 266 kref_sub(&bo->list_kref, count, 267 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list); 268 } 269 270 int ttm_bo_reserve(struct ttm_buffer_object *bo, 271 bool interruptible, 272 bool no_wait, bool use_ticket, 273 struct ww_acquire_ctx *ticket) 274 { 275 struct ttm_bo_global *glob = bo->glob; 276 int put_count = 0; 277 int ret; 278 279 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket, 280 ticket); 281 if (likely(ret == 0)) { 282 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 283 put_count = ttm_bo_del_from_lru(bo); 284 lockmgr(&glob->lru_lock, LK_RELEASE); 285 ttm_bo_list_ref_sub(bo, put_count, true); 286 } 287 288 return ret; 289 } 290 291 int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo, 292 bool interruptible, 293 struct ww_acquire_ctx *ticket) 294 { 295 bool wake_up = false; 296 int ret; 297 298 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) { 299 WARN_ON(bo->seq_valid && ticket->stamp == bo->val_seq); 300 301 ret = ttm_bo_wait_unreserved(bo, interruptible); 302 303 if (unlikely(ret)) 304 return ret; 305 } 306 307 if (bo->val_seq - ticket->stamp < LONG_MAX || !bo->seq_valid) 308 wake_up = true; 309 310 /** 311 * Wake up waiters that may need to recheck for deadlock, 312 * if we decreased the sequence number. 313 */ 314 bo->val_seq = ticket->stamp; 315 bo->seq_valid = true; 316 if (wake_up) 317 wake_up_all(&bo->event_queue); 318 319 return 0; 320 } 321 322 int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo, 323 bool interruptible, struct ww_acquire_ctx *ticket) 324 { 325 struct ttm_bo_global *glob = bo->glob; 326 int put_count, ret; 327 328 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, ticket); 329 if (likely(!ret)) { 330 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 331 put_count = ttm_bo_del_from_lru(bo); 332 lockmgr(&glob->lru_lock, LK_RELEASE); 333 ttm_bo_list_ref_sub(bo, put_count, true); 334 } 335 return ret; 336 } 337 EXPORT_SYMBOL(ttm_bo_reserve_slowpath); 338 339 /* 340 * Must interlock with event_queue to avoid race against 341 * wait_event_common() which can cause wait_event_common() 342 * to become stuck. 343 */ 344 static void 345 ttm_bo_unreserve_core(struct ttm_buffer_object *bo) 346 { 347 lockmgr(&bo->event_queue.lock, LK_EXCLUSIVE); 348 atomic_set(&bo->reserved, 0); 349 lockmgr(&bo->event_queue.lock, LK_RELEASE); 350 wake_up_all(&bo->event_queue); 351 } 352 353 void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket) 354 { 355 ttm_bo_add_to_lru(bo); 356 ttm_bo_unreserve_core(bo); 357 } 358 359 void ttm_bo_unreserve(struct ttm_buffer_object *bo) 360 { 361 struct ttm_bo_global *glob = bo->glob; 362 363 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 364 ttm_bo_unreserve_ticket_locked(bo, NULL); 365 lockmgr(&glob->lru_lock, LK_RELEASE); 366 } 367 EXPORT_SYMBOL(ttm_bo_unreserve); 368 369 void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket) 370 { 371 struct ttm_bo_global *glob = bo->glob; 372 373 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 374 ttm_bo_unreserve_ticket_locked(bo, ticket); 375 lockmgr(&glob->lru_lock, LK_RELEASE); 376 } 377 EXPORT_SYMBOL(ttm_bo_unreserve_ticket); 378 379 /* 380 * Call bo->mutex locked. 381 */ 382 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) 383 { 384 struct ttm_bo_device *bdev = bo->bdev; 385 struct ttm_bo_global *glob = bo->glob; 386 int ret = 0; 387 uint32_t page_flags = 0; 388 389 TTM_ASSERT_LOCKED(&bo->mutex); 390 bo->ttm = NULL; 391 392 if (bdev->need_dma32) 393 page_flags |= TTM_PAGE_FLAG_DMA32; 394 395 switch (bo->type) { 396 case ttm_bo_type_device: 397 if (zero_alloc) 398 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; 399 case ttm_bo_type_kernel: 400 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, 401 page_flags, glob->dummy_read_page); 402 if (unlikely(bo->ttm == NULL)) 403 ret = -ENOMEM; 404 break; 405 case ttm_bo_type_sg: 406 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, 407 page_flags | TTM_PAGE_FLAG_SG, 408 glob->dummy_read_page); 409 if (unlikely(bo->ttm == NULL)) { 410 ret = -ENOMEM; 411 break; 412 } 413 bo->ttm->sg = bo->sg; 414 break; 415 default: 416 kprintf("[TTM] Illegal buffer object type\n"); 417 ret = -EINVAL; 418 break; 419 } 420 421 return ret; 422 } 423 424 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, 425 struct ttm_mem_reg *mem, 426 bool evict, bool interruptible, 427 bool no_wait_gpu) 428 { 429 struct ttm_bo_device *bdev = bo->bdev; 430 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); 431 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); 432 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; 433 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; 434 int ret = 0; 435 436 if (old_is_pci || new_is_pci || 437 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { 438 ret = ttm_mem_io_lock(old_man, true); 439 if (unlikely(ret != 0)) 440 goto out_err; 441 ttm_bo_unmap_virtual_locked(bo); 442 ttm_mem_io_unlock(old_man); 443 } 444 445 /* 446 * Create and bind a ttm if required. 447 */ 448 449 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { 450 if (bo->ttm == NULL) { 451 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); 452 ret = ttm_bo_add_ttm(bo, zero); 453 if (ret) 454 goto out_err; 455 } 456 457 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); 458 if (ret) 459 goto out_err; 460 461 if (mem->mem_type != TTM_PL_SYSTEM) { 462 ret = ttm_tt_bind(bo->ttm, mem); 463 if (ret) 464 goto out_err; 465 } 466 467 if (bo->mem.mem_type == TTM_PL_SYSTEM) { 468 if (bdev->driver->move_notify) 469 bdev->driver->move_notify(bo, mem); 470 bo->mem = *mem; 471 mem->mm_node = NULL; 472 goto moved; 473 } 474 } 475 476 if (bdev->driver->move_notify) 477 bdev->driver->move_notify(bo, mem); 478 479 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && 480 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) 481 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem); 482 else if (bdev->driver->move) 483 ret = bdev->driver->move(bo, evict, interruptible, 484 no_wait_gpu, mem); 485 else 486 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem); 487 488 if (ret) { 489 if (bdev->driver->move_notify) { 490 struct ttm_mem_reg tmp_mem = *mem; 491 *mem = bo->mem; 492 bo->mem = tmp_mem; 493 bdev->driver->move_notify(bo, mem); 494 bo->mem = *mem; 495 *mem = tmp_mem; 496 } 497 498 goto out_err; 499 } 500 501 moved: 502 if (bo->evicted) { 503 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); 504 if (ret) 505 kprintf("[TTM] Can not flush read caches\n"); 506 bo->evicted = false; 507 } 508 509 if (bo->mem.mm_node) { 510 bo->offset = (bo->mem.start << PAGE_SHIFT) + 511 bdev->man[bo->mem.mem_type].gpu_offset; 512 bo->cur_placement = bo->mem.placement; 513 } else 514 bo->offset = 0; 515 516 return 0; 517 518 out_err: 519 new_man = &bdev->man[bo->mem.mem_type]; 520 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { 521 ttm_tt_unbind(bo->ttm); 522 ttm_tt_destroy(bo->ttm); 523 bo->ttm = NULL; 524 } 525 526 return ret; 527 } 528 529 /** 530 * Call bo::reserved. 531 * Will release GPU memory type usage on destruction. 532 * This is the place to put in driver specific hooks to release 533 * driver private resources. 534 * Will release the bo::reserved lock. 535 */ 536 537 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) 538 { 539 if (bo->bdev->driver->move_notify) 540 bo->bdev->driver->move_notify(bo, NULL); 541 542 if (bo->ttm) { 543 ttm_tt_unbind(bo->ttm); 544 ttm_tt_destroy(bo->ttm); 545 bo->ttm = NULL; 546 } 547 ttm_bo_mem_put(bo, &bo->mem); 548 ttm_bo_unreserve_core(bo); 549 550 /* 551 * Since the final reference to this bo may not be dropped by 552 * the current task we have to put a memory barrier here to make 553 * sure the changes done in this function are always visible. 554 * 555 * This function only needs protection against the final kref_put. 556 */ 557 cpu_mfence(); 558 } 559 560 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) 561 { 562 struct ttm_bo_device *bdev = bo->bdev; 563 struct ttm_bo_global *glob = bo->glob; 564 struct ttm_bo_driver *driver = bdev->driver; 565 void *sync_obj = NULL; 566 int put_count; 567 int ret; 568 569 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 570 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); 571 572 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 573 (void) ttm_bo_wait(bo, false, false, true); 574 if (!ret && !bo->sync_obj) { 575 lockmgr(&bdev->fence_lock, LK_RELEASE); 576 put_count = ttm_bo_del_from_lru(bo); 577 578 lockmgr(&glob->lru_lock, LK_RELEASE); 579 ttm_bo_cleanup_memtype_use(bo); 580 581 ttm_bo_list_ref_sub(bo, put_count, true); 582 583 return; 584 } 585 if (bo->sync_obj) 586 sync_obj = driver->sync_obj_ref(bo->sync_obj); 587 lockmgr(&bdev->fence_lock, LK_RELEASE); 588 589 if (!ret) { 590 591 /* 592 * Make NO_EVICT bos immediately available to 593 * shrinkers, now that they are queued for 594 * destruction. 595 */ 596 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { 597 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; 598 ttm_bo_add_to_lru(bo); 599 } 600 601 ttm_bo_unreserve_core(bo); 602 } 603 604 kref_get(&bo->list_kref); 605 list_add_tail(&bo->ddestroy, &bdev->ddestroy); 606 lockmgr(&glob->lru_lock, LK_RELEASE); 607 608 if (sync_obj) { 609 driver->sync_obj_flush(sync_obj); 610 driver->sync_obj_unref(&sync_obj); 611 } 612 schedule_delayed_work(&bdev->wq, 613 ((hz / 100) < 1) ? 1 : hz / 100); 614 } 615 616 /** 617 * function ttm_bo_cleanup_refs_and_unlock 618 * If bo idle, remove from delayed- and lru lists, and unref. 619 * If not idle, do nothing. 620 * 621 * Must be called with lru_lock and reservation held, this function 622 * will drop both before returning. 623 * 624 * @interruptible Any sleeps should occur interruptibly. 625 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. 626 */ 627 628 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, 629 bool interruptible, 630 bool no_wait_gpu) 631 { 632 struct ttm_bo_device *bdev = bo->bdev; 633 struct ttm_bo_driver *driver = bdev->driver; 634 struct ttm_bo_global *glob = bo->glob; 635 int put_count; 636 int ret; 637 638 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 639 ret = ttm_bo_wait(bo, false, false, true); 640 641 if (ret && !no_wait_gpu) { 642 void *sync_obj; 643 644 /* 645 * Take a reference to the fence and unreserve, 646 * at this point the buffer should be dead, so 647 * no new sync objects can be attached. 648 */ 649 sync_obj = driver->sync_obj_ref(bo->sync_obj); 650 lockmgr(&bdev->fence_lock, LK_RELEASE); 651 652 ttm_bo_unreserve_core(bo); 653 lockmgr(&glob->lru_lock, LK_RELEASE); 654 655 ret = driver->sync_obj_wait(sync_obj, false, interruptible); 656 driver->sync_obj_unref(&sync_obj); 657 if (ret) 658 return ret; 659 660 /* 661 * remove sync_obj with ttm_bo_wait, the wait should be 662 * finished, and no new wait object should have been added. 663 */ 664 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 665 ret = ttm_bo_wait(bo, false, false, true); 666 WARN_ON(ret); 667 lockmgr(&bdev->fence_lock, LK_RELEASE); 668 if (ret) 669 return ret; 670 671 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 672 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); 673 674 /* 675 * We raced, and lost, someone else holds the reservation now, 676 * and is probably busy in ttm_bo_cleanup_memtype_use. 677 * 678 * Even if it's not the case, because we finished waiting any 679 * delayed destruction would succeed, so just return success 680 * here. 681 */ 682 if (ret) { 683 lockmgr(&glob->lru_lock, LK_RELEASE); 684 return 0; 685 } 686 } else 687 lockmgr(&bdev->fence_lock, LK_RELEASE); 688 689 if (ret || unlikely(list_empty(&bo->ddestroy))) { 690 ttm_bo_unreserve_core(bo); 691 lockmgr(&glob->lru_lock, LK_RELEASE); 692 return ret; 693 } 694 695 put_count = ttm_bo_del_from_lru(bo); 696 list_del_init(&bo->ddestroy); 697 ++put_count; 698 699 lockmgr(&glob->lru_lock, LK_RELEASE); 700 ttm_bo_cleanup_memtype_use(bo); 701 702 ttm_bo_list_ref_sub(bo, put_count, true); 703 704 return 0; 705 } 706 707 /** 708 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all 709 * encountered buffers. 710 */ 711 712 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) 713 { 714 struct ttm_bo_global *glob = bdev->glob; 715 struct ttm_buffer_object *entry = NULL; 716 int ret = 0; 717 718 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 719 if (list_empty(&bdev->ddestroy)) 720 goto out_unlock; 721 722 entry = list_first_entry(&bdev->ddestroy, 723 struct ttm_buffer_object, ddestroy); 724 kref_get(&entry->list_kref); 725 726 for (;;) { 727 struct ttm_buffer_object *nentry = NULL; 728 729 if (entry->ddestroy.next != &bdev->ddestroy) { 730 nentry = list_first_entry(&entry->ddestroy, 731 struct ttm_buffer_object, ddestroy); 732 kref_get(&nentry->list_kref); 733 } 734 735 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0); 736 if (remove_all && ret) { 737 lockmgr(&glob->lru_lock, LK_RELEASE); 738 ret = ttm_bo_reserve_nolru(entry, false, false, 739 false, 0); 740 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 741 } 742 743 if (!ret) 744 ret = ttm_bo_cleanup_refs_and_unlock(entry, false, 745 !remove_all); 746 else 747 lockmgr(&glob->lru_lock, LK_RELEASE); 748 749 kref_put(&entry->list_kref, ttm_bo_release_list); 750 entry = nentry; 751 752 if (ret || !entry) 753 goto out; 754 755 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 756 if (list_empty(&entry->ddestroy)) 757 break; 758 } 759 760 out_unlock: 761 lockmgr(&glob->lru_lock, LK_RELEASE); 762 out: 763 if (entry) 764 kref_put(&entry->list_kref, ttm_bo_release_list); 765 return ret; 766 } 767 768 static void ttm_bo_delayed_workqueue(struct work_struct *work) 769 { 770 struct ttm_bo_device *bdev = 771 container_of(work, struct ttm_bo_device, wq.work); 772 773 if (ttm_bo_delayed_delete(bdev, false)) { 774 schedule_delayed_work(&bdev->wq, 775 ((hz / 100) < 1) ? 1 : hz / 100); 776 } 777 } 778 779 /* 780 * NOTE: bdev->vm_lock already held on call, this function release it. 781 */ 782 static void ttm_bo_release(struct kref *kref) 783 { 784 struct ttm_buffer_object *bo = 785 container_of(kref, struct ttm_buffer_object, kref); 786 struct ttm_bo_device *bdev = bo->bdev; 787 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; 788 int release_active; 789 790 if (atomic_read(&bo->kref.refcount) > 0) { 791 lockmgr(&bdev->vm_lock, LK_RELEASE); 792 return; 793 } 794 if (likely(bo->vm_node != NULL)) { 795 RB_REMOVE(ttm_bo_device_buffer_objects, 796 &bdev->addr_space_rb, bo); 797 drm_mm_put_block(bo->vm_node); 798 bo->vm_node = NULL; 799 } 800 801 /* 802 * Should we clean up our implied list_kref? Because ttm_bo_release() 803 * can be called reentrantly due to races (this may not be true any 804 * more with the lock management changes in the deref), it is possible 805 * to get here twice, but there's only one list_kref ref to drop and 806 * in the other path 'bo' can be kfree()d by another thread the 807 * instant we release our lock. 808 */ 809 release_active = test_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags); 810 if (release_active) { 811 clear_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags); 812 lockmgr(&bdev->vm_lock, LK_RELEASE); 813 ttm_mem_io_lock(man, false); 814 ttm_mem_io_free_vm(bo); 815 ttm_mem_io_unlock(man); 816 ttm_bo_cleanup_refs_or_queue(bo); 817 kref_put(&bo->list_kref, ttm_bo_release_list); 818 } else { 819 lockmgr(&bdev->vm_lock, LK_RELEASE); 820 } 821 } 822 823 void ttm_bo_unref(struct ttm_buffer_object **p_bo) 824 { 825 struct ttm_buffer_object *bo = *p_bo; 826 struct ttm_bo_device *bdev = bo->bdev; 827 828 *p_bo = NULL; 829 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE); 830 if (kref_put(&bo->kref, ttm_bo_release) == 0) 831 lockmgr(&bdev->vm_lock, LK_RELEASE); 832 } 833 EXPORT_SYMBOL(ttm_bo_unref); 834 835 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) 836 { 837 return cancel_delayed_work_sync(&bdev->wq); 838 } 839 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); 840 841 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) 842 { 843 if (resched) 844 schedule_delayed_work(&bdev->wq, 845 ((hz / 100) < 1) ? 1 : hz / 100); 846 } 847 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); 848 849 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, 850 bool no_wait_gpu) 851 { 852 struct ttm_bo_device *bdev = bo->bdev; 853 struct ttm_mem_reg evict_mem; 854 struct ttm_placement placement; 855 int ret = 0; 856 857 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 858 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); 859 lockmgr(&bdev->fence_lock, LK_RELEASE); 860 861 if (unlikely(ret != 0)) { 862 if (ret != -ERESTARTSYS) { 863 pr_err("Failed to expire sync object before buffer eviction\n"); 864 } 865 goto out; 866 } 867 868 BUG_ON(!ttm_bo_is_reserved(bo)); 869 870 evict_mem = bo->mem; 871 evict_mem.mm_node = NULL; 872 evict_mem.bus.io_reserved_vm = false; 873 evict_mem.bus.io_reserved_count = 0; 874 875 placement.fpfn = 0; 876 placement.lpfn = 0; 877 placement.num_placement = 0; 878 placement.num_busy_placement = 0; 879 bdev->driver->evict_flags(bo, &placement); 880 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, 881 no_wait_gpu); 882 if (ret) { 883 if (ret != -ERESTARTSYS) { 884 pr_err("Failed to find memory space for buffer 0x%p eviction\n", 885 bo); 886 ttm_bo_mem_space_debug(bo, &placement); 887 } 888 goto out; 889 } 890 891 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, 892 no_wait_gpu); 893 if (ret) { 894 if (ret != -ERESTARTSYS) 895 pr_err("Buffer eviction failed\n"); 896 ttm_bo_mem_put(bo, &evict_mem); 897 goto out; 898 } 899 bo->evicted = true; 900 out: 901 return ret; 902 } 903 904 static int ttm_mem_evict_first(struct ttm_bo_device *bdev, 905 uint32_t mem_type, 906 bool interruptible, 907 bool no_wait_gpu) 908 { 909 struct ttm_bo_global *glob = bdev->glob; 910 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 911 struct ttm_buffer_object *bo; 912 int ret = -EBUSY, put_count; 913 914 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 915 list_for_each_entry(bo, &man->lru, lru) { 916 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); 917 if (!ret) 918 break; 919 } 920 921 if (ret) { 922 lockmgr(&glob->lru_lock, LK_RELEASE); 923 return ret; 924 } 925 926 kref_get(&bo->list_kref); 927 928 if (!list_empty(&bo->ddestroy)) { 929 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, 930 no_wait_gpu); 931 kref_put(&bo->list_kref, ttm_bo_release_list); 932 return ret; 933 } 934 935 put_count = ttm_bo_del_from_lru(bo); 936 lockmgr(&glob->lru_lock, LK_RELEASE); 937 938 BUG_ON(ret != 0); 939 940 ttm_bo_list_ref_sub(bo, put_count, true); 941 942 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); 943 ttm_bo_unreserve(bo); 944 945 kref_put(&bo->list_kref, ttm_bo_release_list); 946 return ret; 947 } 948 949 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) 950 { 951 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; 952 953 if (mem->mm_node) 954 (*man->func->put_node)(man, mem); 955 } 956 EXPORT_SYMBOL(ttm_bo_mem_put); 957 958 /** 959 * Repeatedly evict memory from the LRU for @mem_type until we create enough 960 * space, or we've evicted everything and there isn't enough space. 961 */ 962 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, 963 uint32_t mem_type, 964 struct ttm_placement *placement, 965 struct ttm_mem_reg *mem, 966 bool interruptible, 967 bool no_wait_gpu) 968 { 969 struct ttm_bo_device *bdev = bo->bdev; 970 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 971 int ret; 972 973 do { 974 ret = (*man->func->get_node)(man, bo, placement, 0, mem); 975 if (unlikely(ret != 0)) 976 return ret; 977 if (mem->mm_node) 978 break; 979 ret = ttm_mem_evict_first(bdev, mem_type, 980 interruptible, no_wait_gpu); 981 if (unlikely(ret != 0)) 982 return ret; 983 } while (1); 984 if (mem->mm_node == NULL) 985 return -ENOMEM; 986 mem->mem_type = mem_type; 987 return 0; 988 } 989 990 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, 991 uint32_t cur_placement, 992 uint32_t proposed_placement) 993 { 994 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; 995 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; 996 997 /** 998 * Keep current caching if possible. 999 */ 1000 1001 if ((cur_placement & caching) != 0) 1002 result |= (cur_placement & caching); 1003 else if ((man->default_caching & caching) != 0) 1004 result |= man->default_caching; 1005 else if ((TTM_PL_FLAG_CACHED & caching) != 0) 1006 result |= TTM_PL_FLAG_CACHED; 1007 else if ((TTM_PL_FLAG_WC & caching) != 0) 1008 result |= TTM_PL_FLAG_WC; 1009 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) 1010 result |= TTM_PL_FLAG_UNCACHED; 1011 1012 return result; 1013 } 1014 1015 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, 1016 uint32_t mem_type, 1017 uint32_t proposed_placement, 1018 uint32_t *masked_placement) 1019 { 1020 uint32_t cur_flags = ttm_bo_type_flags(mem_type); 1021 1022 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0) 1023 return false; 1024 1025 if ((proposed_placement & man->available_caching) == 0) 1026 return false; 1027 1028 cur_flags |= (proposed_placement & man->available_caching); 1029 1030 *masked_placement = cur_flags; 1031 return true; 1032 } 1033 1034 /** 1035 * Creates space for memory region @mem according to its type. 1036 * 1037 * This function first searches for free space in compatible memory types in 1038 * the priority order defined by the driver. If free space isn't found, then 1039 * ttm_bo_mem_force_space is attempted in priority order to evict and find 1040 * space. 1041 */ 1042 int ttm_bo_mem_space(struct ttm_buffer_object *bo, 1043 struct ttm_placement *placement, 1044 struct ttm_mem_reg *mem, 1045 bool interruptible, 1046 bool no_wait_gpu) 1047 { 1048 struct ttm_bo_device *bdev = bo->bdev; 1049 struct ttm_mem_type_manager *man; 1050 uint32_t mem_type = TTM_PL_SYSTEM; 1051 uint32_t cur_flags = 0; 1052 bool type_found = false; 1053 bool type_ok = false; 1054 bool has_erestartsys = false; 1055 int i, ret; 1056 1057 mem->mm_node = NULL; 1058 for (i = 0; i < placement->num_placement; ++i) { 1059 ret = ttm_mem_type_from_flags(placement->placement[i], 1060 &mem_type); 1061 if (ret) 1062 return ret; 1063 man = &bdev->man[mem_type]; 1064 1065 type_ok = ttm_bo_mt_compatible(man, 1066 mem_type, 1067 placement->placement[i], 1068 &cur_flags); 1069 1070 if (!type_ok) 1071 continue; 1072 1073 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, 1074 cur_flags); 1075 /* 1076 * Use the access and other non-mapping-related flag bits from 1077 * the memory placement flags to the current flags 1078 */ 1079 ttm_flag_masked(&cur_flags, placement->placement[i], 1080 ~TTM_PL_MASK_MEMTYPE); 1081 1082 if (mem_type == TTM_PL_SYSTEM) 1083 break; 1084 1085 if (man->has_type && man->use_type) { 1086 type_found = true; 1087 ret = (*man->func->get_node)(man, bo, placement, 1088 cur_flags, mem); 1089 if (unlikely(ret)) 1090 return ret; 1091 } 1092 if (mem->mm_node) 1093 break; 1094 } 1095 1096 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { 1097 mem->mem_type = mem_type; 1098 mem->placement = cur_flags; 1099 return 0; 1100 } 1101 1102 if (!type_found) 1103 return -EINVAL; 1104 1105 for (i = 0; i < placement->num_busy_placement; ++i) { 1106 ret = ttm_mem_type_from_flags(placement->busy_placement[i], 1107 &mem_type); 1108 if (ret) 1109 return ret; 1110 man = &bdev->man[mem_type]; 1111 if (!man->has_type) 1112 continue; 1113 if (!ttm_bo_mt_compatible(man, 1114 mem_type, 1115 placement->busy_placement[i], 1116 &cur_flags)) 1117 continue; 1118 1119 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, 1120 cur_flags); 1121 /* 1122 * Use the access and other non-mapping-related flag bits from 1123 * the memory placement flags to the current flags 1124 */ 1125 ttm_flag_masked(&cur_flags, placement->busy_placement[i], 1126 ~TTM_PL_MASK_MEMTYPE); 1127 1128 if (mem_type == TTM_PL_SYSTEM) { 1129 mem->mem_type = mem_type; 1130 mem->placement = cur_flags; 1131 mem->mm_node = NULL; 1132 return 0; 1133 } 1134 1135 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem, 1136 interruptible, no_wait_gpu); 1137 if (ret == 0 && mem->mm_node) { 1138 mem->placement = cur_flags; 1139 return 0; 1140 } 1141 if (ret == -ERESTARTSYS) 1142 has_erestartsys = true; 1143 } 1144 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; 1145 return ret; 1146 } 1147 EXPORT_SYMBOL(ttm_bo_mem_space); 1148 1149 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, 1150 struct ttm_placement *placement, 1151 bool interruptible, 1152 bool no_wait_gpu) 1153 { 1154 int ret = 0; 1155 struct ttm_mem_reg mem; 1156 struct ttm_bo_device *bdev = bo->bdev; 1157 1158 BUG_ON(!ttm_bo_is_reserved(bo)); 1159 1160 /* 1161 * FIXME: It's possible to pipeline buffer moves. 1162 * Have the driver move function wait for idle when necessary, 1163 * instead of doing it here. 1164 */ 1165 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1166 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); 1167 lockmgr(&bdev->fence_lock, LK_RELEASE); 1168 if (ret) 1169 return ret; 1170 mem.num_pages = bo->num_pages; 1171 mem.size = mem.num_pages << PAGE_SHIFT; 1172 mem.page_alignment = bo->mem.page_alignment; 1173 mem.bus.io_reserved_vm = false; 1174 mem.bus.io_reserved_count = 0; 1175 /* 1176 * Determine where to move the buffer. 1177 */ 1178 ret = ttm_bo_mem_space(bo, placement, &mem, 1179 interruptible, no_wait_gpu); 1180 if (ret) 1181 goto out_unlock; 1182 ret = ttm_bo_handle_move_mem(bo, &mem, false, 1183 interruptible, no_wait_gpu); 1184 out_unlock: 1185 if (ret && mem.mm_node) 1186 ttm_bo_mem_put(bo, &mem); 1187 return ret; 1188 } 1189 1190 static bool ttm_bo_mem_compat(struct ttm_placement *placement, 1191 struct ttm_mem_reg *mem, 1192 uint32_t *new_flags) 1193 { 1194 int i; 1195 1196 if (mem->mm_node && placement->lpfn != 0 && 1197 (mem->start < placement->fpfn || 1198 mem->start + mem->num_pages > placement->lpfn)) 1199 return false; 1200 1201 for (i = 0; i < placement->num_placement; i++) { 1202 *new_flags = placement->placement[i]; 1203 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && 1204 (*new_flags & mem->placement & TTM_PL_MASK_MEM)) 1205 return true; 1206 } 1207 1208 for (i = 0; i < placement->num_busy_placement; i++) { 1209 *new_flags = placement->busy_placement[i]; 1210 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && 1211 (*new_flags & mem->placement & TTM_PL_MASK_MEM)) 1212 return true; 1213 } 1214 1215 return false; 1216 } 1217 1218 int ttm_bo_validate(struct ttm_buffer_object *bo, 1219 struct ttm_placement *placement, 1220 bool interruptible, 1221 bool no_wait_gpu) 1222 { 1223 int ret; 1224 uint32_t new_flags; 1225 1226 BUG_ON(!ttm_bo_is_reserved(bo)); 1227 /* Check that range is valid */ 1228 if (placement->lpfn || placement->fpfn) 1229 if (placement->fpfn > placement->lpfn || 1230 (placement->lpfn - placement->fpfn) < bo->num_pages) 1231 return -EINVAL; 1232 /* 1233 * Check whether we need to move buffer. 1234 */ 1235 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { 1236 ret = ttm_bo_move_buffer(bo, placement, interruptible, 1237 no_wait_gpu); 1238 if (ret) 1239 return ret; 1240 } else { 1241 /* 1242 * Use the access and other non-mapping-related flag bits from 1243 * the compatible memory placement flags to the active flags 1244 */ 1245 ttm_flag_masked(&bo->mem.placement, new_flags, 1246 ~TTM_PL_MASK_MEMTYPE); 1247 } 1248 /* 1249 * We might need to add a TTM. 1250 */ 1251 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { 1252 ret = ttm_bo_add_ttm(bo, true); 1253 if (ret) 1254 return ret; 1255 } 1256 return 0; 1257 } 1258 EXPORT_SYMBOL(ttm_bo_validate); 1259 1260 int ttm_bo_check_placement(struct ttm_buffer_object *bo, 1261 struct ttm_placement *placement) 1262 { 1263 BUG_ON((placement->fpfn || placement->lpfn) && 1264 (bo->mem.num_pages > (placement->lpfn - placement->fpfn))); 1265 1266 return 0; 1267 } 1268 1269 int ttm_bo_init(struct ttm_bo_device *bdev, 1270 struct ttm_buffer_object *bo, 1271 unsigned long size, 1272 enum ttm_bo_type type, 1273 struct ttm_placement *placement, 1274 uint32_t page_alignment, 1275 bool interruptible, 1276 struct vm_object *persistent_swap_storage, 1277 size_t acc_size, 1278 struct sg_table *sg, 1279 void (*destroy) (struct ttm_buffer_object *)) 1280 { 1281 int ret = 0; 1282 unsigned long num_pages; 1283 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; 1284 1285 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); 1286 if (ret) { 1287 kprintf("[TTM] Out of kernel memory\n"); 1288 if (destroy) 1289 (*destroy)(bo); 1290 else 1291 kfree(bo); 1292 return -ENOMEM; 1293 } 1294 1295 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1296 if (num_pages == 0) { 1297 kprintf("[TTM] Illegal buffer object size\n"); 1298 if (destroy) 1299 (*destroy)(bo); 1300 else 1301 kfree(bo); 1302 ttm_mem_global_free(mem_glob, acc_size); 1303 return -EINVAL; 1304 } 1305 bo->destroy = destroy; 1306 1307 kref_init(&bo->kref); 1308 kref_init(&bo->list_kref); 1309 atomic_set(&bo->cpu_writers, 0); 1310 atomic_set(&bo->reserved, 1); 1311 init_waitqueue_head(&bo->event_queue); 1312 INIT_LIST_HEAD(&bo->lru); 1313 INIT_LIST_HEAD(&bo->ddestroy); 1314 INIT_LIST_HEAD(&bo->swap); 1315 INIT_LIST_HEAD(&bo->io_reserve_lru); 1316 /*bzero(&bo->vm_rb, sizeof(bo->vm_rb));*/ 1317 bo->bdev = bdev; 1318 bo->glob = bdev->glob; 1319 bo->type = type; 1320 bo->num_pages = num_pages; 1321 bo->mem.size = num_pages << PAGE_SHIFT; 1322 bo->mem.mem_type = TTM_PL_SYSTEM; 1323 bo->mem.num_pages = bo->num_pages; 1324 bo->mem.mm_node = NULL; 1325 bo->mem.page_alignment = page_alignment; 1326 bo->mem.bus.io_reserved_vm = false; 1327 bo->mem.bus.io_reserved_count = 0; 1328 bo->priv_flags = 0; 1329 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); 1330 bo->seq_valid = false; 1331 bo->persistent_swap_storage = persistent_swap_storage; 1332 bo->acc_size = acc_size; 1333 bo->sg = sg; 1334 atomic_inc(&bo->glob->bo_count); 1335 1336 /* 1337 * Mirror ref from kref_init() for list_kref. 1338 */ 1339 set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &bo->priv_flags); 1340 1341 ret = ttm_bo_check_placement(bo, placement); 1342 if (unlikely(ret != 0)) 1343 goto out_err; 1344 1345 /* 1346 * For ttm_bo_type_device buffers, allocate 1347 * address space from the device. 1348 */ 1349 if (bo->type == ttm_bo_type_device || 1350 bo->type == ttm_bo_type_sg) { 1351 ret = ttm_bo_setup_vm(bo); 1352 if (ret) 1353 goto out_err; 1354 } 1355 1356 ret = ttm_bo_validate(bo, placement, interruptible, false); 1357 if (ret) 1358 goto out_err; 1359 1360 ttm_bo_unreserve(bo); 1361 return 0; 1362 1363 out_err: 1364 ttm_bo_unreserve(bo); 1365 ttm_bo_unref(&bo); 1366 1367 return ret; 1368 } 1369 EXPORT_SYMBOL(ttm_bo_init); 1370 1371 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, 1372 unsigned long bo_size, 1373 unsigned struct_size) 1374 { 1375 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1376 size_t size = 0; 1377 1378 size += ttm_round_pot(struct_size); 1379 size += PAGE_ALIGN(npages * sizeof(void *)); 1380 size += ttm_round_pot(sizeof(struct ttm_tt)); 1381 return size; 1382 } 1383 EXPORT_SYMBOL(ttm_bo_acc_size); 1384 1385 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, 1386 unsigned long bo_size, 1387 unsigned struct_size) 1388 { 1389 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1390 size_t size = 0; 1391 1392 size += ttm_round_pot(struct_size); 1393 size += PAGE_ALIGN(npages * sizeof(void *)); 1394 size += PAGE_ALIGN(npages * sizeof(dma_addr_t)); 1395 size += ttm_round_pot(sizeof(struct ttm_dma_tt)); 1396 return size; 1397 } 1398 EXPORT_SYMBOL(ttm_bo_dma_acc_size); 1399 1400 int ttm_bo_create(struct ttm_bo_device *bdev, 1401 unsigned long size, 1402 enum ttm_bo_type type, 1403 struct ttm_placement *placement, 1404 uint32_t page_alignment, 1405 bool interruptible, 1406 struct vm_object *persistent_swap_storage, 1407 struct ttm_buffer_object **p_bo) 1408 { 1409 struct ttm_buffer_object *bo; 1410 size_t acc_size; 1411 int ret; 1412 1413 *p_bo = NULL; 1414 bo = kmalloc(sizeof(*bo), M_DRM, M_WAITOK | M_ZERO); 1415 if (unlikely(bo == NULL)) 1416 return -ENOMEM; 1417 1418 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object)); 1419 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, 1420 interruptible, persistent_swap_storage, acc_size, 1421 NULL, NULL); 1422 if (likely(ret == 0)) 1423 *p_bo = bo; 1424 1425 return ret; 1426 } 1427 EXPORT_SYMBOL(ttm_bo_create); 1428 1429 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, 1430 unsigned mem_type, bool allow_errors) 1431 { 1432 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 1433 struct ttm_bo_global *glob = bdev->glob; 1434 int ret; 1435 1436 /* 1437 * Can't use standard list traversal since we're unlocking. 1438 */ 1439 1440 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 1441 while (!list_empty(&man->lru)) { 1442 lockmgr(&glob->lru_lock, LK_RELEASE); 1443 ret = ttm_mem_evict_first(bdev, mem_type, false, false); 1444 if (ret) { 1445 if (allow_errors) { 1446 return ret; 1447 } else { 1448 kprintf("[TTM] Cleanup eviction failed\n"); 1449 } 1450 } 1451 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 1452 } 1453 lockmgr(&glob->lru_lock, LK_RELEASE); 1454 return 0; 1455 } 1456 1457 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) 1458 { 1459 struct ttm_mem_type_manager *man; 1460 int ret = -EINVAL; 1461 1462 if (mem_type >= TTM_NUM_MEM_TYPES) { 1463 kprintf("[TTM] Illegal memory type %d\n", mem_type); 1464 return ret; 1465 } 1466 man = &bdev->man[mem_type]; 1467 1468 if (!man->has_type) { 1469 kprintf("[TTM] Trying to take down uninitialized memory manager type %u\n", 1470 mem_type); 1471 return ret; 1472 } 1473 1474 man->use_type = false; 1475 man->has_type = false; 1476 1477 ret = 0; 1478 if (mem_type > 0) { 1479 ttm_bo_force_list_clean(bdev, mem_type, false); 1480 1481 ret = (*man->func->takedown)(man); 1482 } 1483 1484 return ret; 1485 } 1486 EXPORT_SYMBOL(ttm_bo_clean_mm); 1487 1488 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) 1489 { 1490 struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 1491 1492 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { 1493 kprintf("[TTM] Illegal memory manager memory type %u\n", mem_type); 1494 return -EINVAL; 1495 } 1496 1497 if (!man->has_type) { 1498 kprintf("[TTM] Memory type %u has not been initialized\n", mem_type); 1499 return 0; 1500 } 1501 1502 return ttm_bo_force_list_clean(bdev, mem_type, true); 1503 } 1504 EXPORT_SYMBOL(ttm_bo_evict_mm); 1505 1506 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, 1507 unsigned long p_size) 1508 { 1509 int ret = -EINVAL; 1510 struct ttm_mem_type_manager *man; 1511 1512 BUG_ON(type >= TTM_NUM_MEM_TYPES); 1513 man = &bdev->man[type]; 1514 BUG_ON(man->has_type); 1515 man->io_reserve_fastpath = true; 1516 man->use_io_reserve_lru = false; 1517 lockinit(&man->io_reserve_mutex, "ttmman", 0, LK_CANRECURSE); 1518 INIT_LIST_HEAD(&man->io_reserve_lru); 1519 1520 ret = bdev->driver->init_mem_type(bdev, type, man); 1521 if (ret) 1522 return ret; 1523 man->bdev = bdev; 1524 1525 ret = 0; 1526 if (type != TTM_PL_SYSTEM) { 1527 ret = (*man->func->init)(man, p_size); 1528 if (ret) 1529 return ret; 1530 } 1531 man->has_type = true; 1532 man->use_type = true; 1533 man->size = p_size; 1534 1535 INIT_LIST_HEAD(&man->lru); 1536 1537 return 0; 1538 } 1539 EXPORT_SYMBOL(ttm_bo_init_mm); 1540 1541 static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob) 1542 { 1543 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); 1544 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE); 1545 glob->dummy_read_page = NULL; 1546 /* 1547 vm_page_free(glob->dummy_read_page); 1548 */ 1549 } 1550 1551 void ttm_bo_global_release(struct drm_global_reference *ref) 1552 { 1553 struct ttm_bo_global *glob = ref->object; 1554 1555 if (refcount_release(&glob->kobj_ref)) 1556 ttm_bo_global_kobj_release(glob); 1557 } 1558 EXPORT_SYMBOL(ttm_bo_global_release); 1559 1560 int ttm_bo_global_init(struct drm_global_reference *ref) 1561 { 1562 struct ttm_bo_global_ref *bo_ref = 1563 container_of(ref, struct ttm_bo_global_ref, ref); 1564 struct ttm_bo_global *glob = ref->object; 1565 int ret; 1566 1567 lockinit(&glob->device_list_mutex, "ttmdlm", 0, LK_CANRECURSE); 1568 lockinit(&glob->lru_lock, "ttmlru", 0, LK_CANRECURSE); 1569 glob->mem_glob = bo_ref->mem_glob; 1570 glob->dummy_read_page = vm_page_alloc_contig( 1571 0, VM_MAX_ADDRESS, PAGE_SIZE, 0, 1*PAGE_SIZE, VM_MEMATTR_UNCACHEABLE); 1572 1573 if (unlikely(glob->dummy_read_page == NULL)) { 1574 ret = -ENOMEM; 1575 goto out_no_drp; 1576 } 1577 1578 INIT_LIST_HEAD(&glob->swap_lru); 1579 INIT_LIST_HEAD(&glob->device_list); 1580 1581 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); 1582 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); 1583 if (unlikely(ret != 0)) { 1584 kprintf("[TTM] Could not register buffer object swapout\n"); 1585 goto out_no_shrink; 1586 } 1587 1588 atomic_set(&glob->bo_count, 0); 1589 1590 refcount_init(&glob->kobj_ref, 1); 1591 return (0); 1592 1593 out_no_shrink: 1594 vm_page_free_contig(glob->dummy_read_page, PAGE_SIZE); 1595 glob->dummy_read_page = NULL; 1596 /* 1597 vm_page_free(glob->dummy_read_page); 1598 */ 1599 out_no_drp: 1600 kfree(glob); 1601 return ret; 1602 } 1603 EXPORT_SYMBOL(ttm_bo_global_init); 1604 1605 1606 int ttm_bo_device_release(struct ttm_bo_device *bdev) 1607 { 1608 int ret = 0; 1609 unsigned i = TTM_NUM_MEM_TYPES; 1610 struct ttm_mem_type_manager *man; 1611 struct ttm_bo_global *glob = bdev->glob; 1612 1613 while (i--) { 1614 man = &bdev->man[i]; 1615 if (man->has_type) { 1616 man->use_type = false; 1617 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { 1618 ret = -EBUSY; 1619 kprintf("[TTM] DRM memory manager type %d is not clean\n", 1620 i); 1621 } 1622 man->has_type = false; 1623 } 1624 } 1625 1626 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE); 1627 list_del(&bdev->device_list); 1628 lockmgr(&glob->device_list_mutex, LK_RELEASE); 1629 1630 cancel_delayed_work_sync(&bdev->wq); 1631 1632 while (ttm_bo_delayed_delete(bdev, true)) 1633 ; 1634 1635 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 1636 if (list_empty(&bdev->ddestroy)) 1637 TTM_DEBUG("Delayed destroy list was clean\n"); 1638 1639 if (list_empty(&bdev->man[0].lru)) 1640 TTM_DEBUG("Swap list was clean\n"); 1641 lockmgr(&glob->lru_lock, LK_RELEASE); 1642 1643 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm)); 1644 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE); 1645 drm_mm_takedown(&bdev->addr_space_mm); 1646 lockmgr(&bdev->vm_lock, LK_RELEASE); 1647 1648 return ret; 1649 } 1650 EXPORT_SYMBOL(ttm_bo_device_release); 1651 1652 int ttm_bo_device_init(struct ttm_bo_device *bdev, 1653 struct ttm_bo_global *glob, 1654 struct ttm_bo_driver *driver, 1655 uint64_t file_page_offset, 1656 bool need_dma32) 1657 { 1658 int ret = -EINVAL; 1659 1660 lockinit(&bdev->vm_lock, "ttmvml", 0, LK_CANRECURSE); 1661 bdev->driver = driver; 1662 1663 memset(bdev->man, 0, sizeof(bdev->man)); 1664 1665 /* 1666 * Initialize the system memory buffer type. 1667 * Other types need to be driver / IOCTL initialized. 1668 */ 1669 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); 1670 if (unlikely(ret != 0)) 1671 goto out_no_sys; 1672 1673 RB_INIT(&bdev->addr_space_rb); 1674 drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000); 1675 1676 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); 1677 INIT_LIST_HEAD(&bdev->ddestroy); 1678 bdev->dev_mapping = NULL; 1679 bdev->glob = glob; 1680 bdev->need_dma32 = need_dma32; 1681 bdev->val_seq = 0; 1682 lockinit(&bdev->fence_lock, "ttmfence", 0, LK_CANRECURSE); 1683 lockmgr(&glob->device_list_mutex, LK_EXCLUSIVE); 1684 list_add_tail(&bdev->device_list, &glob->device_list); 1685 lockmgr(&glob->device_list_mutex, LK_RELEASE); 1686 1687 return 0; 1688 out_no_sys: 1689 return ret; 1690 } 1691 EXPORT_SYMBOL(ttm_bo_device_init); 1692 1693 /* 1694 * buffer object vm functions. 1695 */ 1696 1697 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) 1698 { 1699 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 1700 1701 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { 1702 if (mem->mem_type == TTM_PL_SYSTEM) 1703 return false; 1704 1705 if (man->flags & TTM_MEMTYPE_FLAG_CMA) 1706 return false; 1707 1708 if (mem->placement & TTM_PL_FLAG_CACHED) 1709 return false; 1710 } 1711 return true; 1712 } 1713 1714 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) 1715 { 1716 1717 ttm_bo_release_mmap(bo); 1718 ttm_mem_io_free_vm(bo); 1719 } 1720 1721 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) 1722 { 1723 struct ttm_bo_device *bdev = bo->bdev; 1724 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; 1725 1726 ttm_mem_io_lock(man, false); 1727 ttm_bo_unmap_virtual_locked(bo); 1728 ttm_mem_io_unlock(man); 1729 } 1730 1731 1732 EXPORT_SYMBOL(ttm_bo_unmap_virtual); 1733 1734 static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo) 1735 { 1736 struct ttm_bo_device *bdev = bo->bdev; 1737 1738 /* The caller acquired bdev->vm_lock. */ 1739 RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo); 1740 } 1741 1742 /** 1743 * ttm_bo_setup_vm: 1744 * 1745 * @bo: the buffer to allocate address space for 1746 * 1747 * Allocate address space in the drm device so that applications 1748 * can mmap the buffer and access the contents. This only 1749 * applies to ttm_bo_type_device objects as others are not 1750 * placed in the drm device address space. 1751 */ 1752 1753 static int ttm_bo_setup_vm(struct ttm_buffer_object *bo) 1754 { 1755 struct ttm_bo_device *bdev = bo->bdev; 1756 int ret; 1757 1758 retry_pre_get: 1759 ret = drm_mm_pre_get(&bdev->addr_space_mm); 1760 if (unlikely(ret != 0)) 1761 return ret; 1762 1763 lockmgr(&bdev->vm_lock, LK_EXCLUSIVE); 1764 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm, 1765 bo->mem.num_pages, 0, 0); 1766 1767 if (unlikely(bo->vm_node == NULL)) { 1768 ret = -ENOMEM; 1769 goto out_unlock; 1770 } 1771 1772 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node, 1773 bo->mem.num_pages, 0); 1774 1775 if (unlikely(bo->vm_node == NULL)) { 1776 lockmgr(&bdev->vm_lock, LK_RELEASE); 1777 goto retry_pre_get; 1778 } 1779 1780 ttm_bo_vm_insert_rb(bo); 1781 lockmgr(&bdev->vm_lock, LK_RELEASE); 1782 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT; 1783 1784 return 0; 1785 out_unlock: 1786 lockmgr(&bdev->vm_lock, LK_RELEASE); 1787 return ret; 1788 } 1789 1790 int ttm_bo_wait(struct ttm_buffer_object *bo, 1791 bool lazy, bool interruptible, bool no_wait) 1792 { 1793 struct ttm_bo_driver *driver = bo->bdev->driver; 1794 struct ttm_bo_device *bdev = bo->bdev; 1795 void *sync_obj; 1796 int ret = 0; 1797 1798 if (likely(bo->sync_obj == NULL)) 1799 return 0; 1800 1801 while (bo->sync_obj) { 1802 1803 if (driver->sync_obj_signaled(bo->sync_obj)) { 1804 void *tmp_obj = bo->sync_obj; 1805 bo->sync_obj = NULL; 1806 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); 1807 lockmgr(&bdev->fence_lock, LK_RELEASE); 1808 driver->sync_obj_unref(&tmp_obj); 1809 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1810 continue; 1811 } 1812 1813 if (no_wait) 1814 return -EBUSY; 1815 1816 sync_obj = driver->sync_obj_ref(bo->sync_obj); 1817 lockmgr(&bdev->fence_lock, LK_RELEASE); 1818 ret = driver->sync_obj_wait(sync_obj, 1819 lazy, interruptible); 1820 if (unlikely(ret != 0)) { 1821 driver->sync_obj_unref(&sync_obj); 1822 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1823 return ret; 1824 } 1825 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1826 if (likely(bo->sync_obj == sync_obj)) { 1827 void *tmp_obj = bo->sync_obj; 1828 bo->sync_obj = NULL; 1829 clear_bit(TTM_BO_PRIV_FLAG_MOVING, 1830 &bo->priv_flags); 1831 lockmgr(&bdev->fence_lock, LK_RELEASE); 1832 driver->sync_obj_unref(&sync_obj); 1833 driver->sync_obj_unref(&tmp_obj); 1834 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1835 } else { 1836 lockmgr(&bdev->fence_lock, LK_RELEASE); 1837 driver->sync_obj_unref(&sync_obj); 1838 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1839 } 1840 } 1841 return 0; 1842 } 1843 EXPORT_SYMBOL(ttm_bo_wait); 1844 1845 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) 1846 { 1847 struct ttm_bo_device *bdev = bo->bdev; 1848 int ret = 0; 1849 1850 /* 1851 * Using ttm_bo_reserve makes sure the lru lists are updated. 1852 */ 1853 1854 ret = ttm_bo_reserve(bo, true, no_wait, false, 0); 1855 if (unlikely(ret != 0)) 1856 return ret; 1857 lockmgr(&bdev->fence_lock, LK_EXCLUSIVE); 1858 ret = ttm_bo_wait(bo, false, true, no_wait); 1859 lockmgr(&bdev->fence_lock, LK_RELEASE); 1860 if (likely(ret == 0)) 1861 atomic_inc(&bo->cpu_writers); 1862 ttm_bo_unreserve(bo); 1863 return ret; 1864 } 1865 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); 1866 1867 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) 1868 { 1869 atomic_dec(&bo->cpu_writers); 1870 } 1871 EXPORT_SYMBOL(ttm_bo_synccpu_write_release); 1872 1873 /** 1874 * A buffer object shrink method that tries to swap out the first 1875 * buffer object on the bo_global::swap_lru list. 1876 */ 1877 1878 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) 1879 { 1880 struct ttm_bo_global *glob = 1881 container_of(shrink, struct ttm_bo_global, shrink); 1882 struct ttm_buffer_object *bo; 1883 int ret = -EBUSY; 1884 int put_count; 1885 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM); 1886 1887 lockmgr(&glob->lru_lock, LK_EXCLUSIVE); 1888 list_for_each_entry(bo, &glob->swap_lru, swap) { 1889 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); 1890 if (!ret) 1891 break; 1892 } 1893 1894 if (ret) { 1895 lockmgr(&glob->lru_lock, LK_RELEASE); 1896 return ret; 1897 } 1898 1899 kref_get(&bo->list_kref); 1900 1901 if (!list_empty(&bo->ddestroy)) { 1902 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false); 1903 kref_put(&bo->list_kref, ttm_bo_release_list); 1904 return ret; 1905 } 1906 1907 put_count = ttm_bo_del_from_lru(bo); 1908 lockmgr(&glob->lru_lock, LK_RELEASE); 1909 1910 ttm_bo_list_ref_sub(bo, put_count, true); 1911 1912 /** 1913 * Wait for GPU, then move to system cached. 1914 */ 1915 1916 lockmgr(&bo->bdev->fence_lock, LK_EXCLUSIVE); 1917 ret = ttm_bo_wait(bo, false, false, false); 1918 lockmgr(&bo->bdev->fence_lock, LK_RELEASE); 1919 1920 if (unlikely(ret != 0)) 1921 goto out; 1922 1923 if ((bo->mem.placement & swap_placement) != swap_placement) { 1924 struct ttm_mem_reg evict_mem; 1925 1926 evict_mem = bo->mem; 1927 evict_mem.mm_node = NULL; 1928 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; 1929 evict_mem.mem_type = TTM_PL_SYSTEM; 1930 1931 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, 1932 false, false); 1933 if (unlikely(ret != 0)) 1934 goto out; 1935 } 1936 1937 ttm_bo_unmap_virtual(bo); 1938 1939 /** 1940 * Swap out. Buffer will be swapped in again as soon as 1941 * anyone tries to access a ttm page. 1942 */ 1943 1944 if (bo->bdev->driver->swap_notify) 1945 bo->bdev->driver->swap_notify(bo); 1946 1947 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); 1948 out: 1949 1950 /** 1951 * 1952 * Unreserve without putting on LRU to avoid swapping out an 1953 * already swapped buffer. 1954 */ 1955 1956 ttm_bo_unreserve_core(bo); 1957 kref_put(&bo->list_kref, ttm_bo_release_list); 1958 return ret; 1959 } 1960 1961 void ttm_bo_swapout_all(struct ttm_bo_device *bdev) 1962 { 1963 while (ttm_bo_swapout(&bdev->glob->shrink) == 0) 1964 ; 1965 } 1966 EXPORT_SYMBOL(ttm_bo_swapout_all); 1967