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