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