1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /************************************************************************** 3 * 4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 /* 29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 30 */ 31 32 #define pr_fmt(fmt) "[TTM] " fmt 33 34 #include <drm/drm_print.h> 35 #include <drm/drm_util.h> 36 #include <drm/ttm/ttm_allocation.h> 37 #include <drm/ttm/ttm_bo.h> 38 #include <drm/ttm/ttm_placement.h> 39 #include <drm/ttm/ttm_tt.h> 40 41 #include <linux/export.h> 42 #include <linux/jiffies.h> 43 #include <linux/slab.h> 44 #include <linux/sched.h> 45 #include <linux/mm.h> 46 #include <linux/file.h> 47 #include <linux/module.h> 48 #include <linux/atomic.h> 49 #include <linux/cgroup_dmem.h> 50 #include <linux/dma-resv.h> 51 52 #include "ttm_module.h" 53 #include "ttm_bo_internal.h" 54 55 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, 56 struct ttm_placement *placement) 57 { 58 struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, TTM_PFX); 59 struct ttm_resource_manager *man; 60 int i, mem_type; 61 62 for (i = 0; i < placement->num_placement; i++) { 63 mem_type = placement->placement[i].mem_type; 64 drm_printf(&p, " placement[%d]=0x%08X (%d)\n", 65 i, placement->placement[i].flags, mem_type); 66 man = ttm_manager_type(bo->bdev, mem_type); 67 ttm_resource_manager_debug(man, &p); 68 } 69 } 70 71 /** 72 * ttm_bo_move_to_lru_tail 73 * 74 * @bo: The buffer object. 75 * 76 * Move this BO to the tail of all lru lists used to lookup and reserve an 77 * object. This function must be called with struct ttm_global::lru_lock 78 * held, and is used to make a BO less likely to be considered for eviction. 79 */ 80 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo) 81 { 82 dma_resv_assert_held(bo->base.resv); 83 84 if (bo->resource) 85 ttm_resource_move_to_lru_tail(bo->resource); 86 } 87 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail); 88 89 /** 90 * ttm_bo_set_bulk_move - update BOs bulk move object 91 * 92 * @bo: The buffer object. 93 * @bulk: bulk move structure 94 * 95 * Update the BOs bulk move object, making sure that resources are added/removed 96 * as well. A bulk move allows to move many resource on the LRU at once, 97 * resulting in much less overhead of maintaining the LRU. 98 * The only requirement is that the resources stay together on the LRU and are 99 * never separated. This is enforces by setting the bulk_move structure on a BO. 100 * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of 101 * their LRU list. 102 */ 103 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo, 104 struct ttm_lru_bulk_move *bulk) 105 { 106 dma_resv_assert_held(bo->base.resv); 107 108 if (bo->bulk_move == bulk) 109 return; 110 111 spin_lock(&bo->bdev->lru_lock); 112 if (bo->resource) 113 ttm_resource_del_bulk_move(bo->resource, bo); 114 bo->bulk_move = bulk; 115 if (bo->resource) 116 ttm_resource_add_bulk_move(bo->resource, bo); 117 spin_unlock(&bo->bdev->lru_lock); 118 } 119 EXPORT_SYMBOL(ttm_bo_set_bulk_move); 120 121 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, 122 struct ttm_resource *mem, bool evict, 123 struct ttm_operation_ctx *ctx, 124 struct ttm_place *hop) 125 { 126 struct ttm_device *bdev = bo->bdev; 127 bool old_use_tt, new_use_tt; 128 int ret; 129 130 old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt; 131 new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt; 132 133 ttm_bo_unmap_virtual(bo); 134 135 /* 136 * Create and bind a ttm if required. 137 */ 138 139 if (new_use_tt) { 140 /* Zero init the new TTM structure if the old location should 141 * have used one as well. 142 */ 143 ret = ttm_tt_create(bo, old_use_tt); 144 if (ret) 145 goto out_err; 146 147 if (mem->mem_type != TTM_PL_SYSTEM) { 148 ret = ttm_bo_populate(bo, ctx); 149 if (ret) 150 goto out_err; 151 } 152 } 153 154 ret = dma_resv_reserve_fences(bo->base.resv, 1); 155 if (ret) 156 goto out_err; 157 158 ret = bdev->funcs->move(bo, evict, ctx, mem, hop); 159 if (ret) { 160 if (ret == -EMULTIHOP) 161 return ret; 162 goto out_err; 163 } 164 165 ctx->bytes_moved += bo->base.size; 166 return 0; 167 168 out_err: 169 if (!old_use_tt) 170 ttm_bo_tt_destroy(bo); 171 172 return ret; 173 } 174 175 /* 176 * Call bo::reserved. 177 * Will release GPU memory type usage on destruction. 178 * This is the place to put in driver specific hooks to release 179 * driver private resources. 180 * Will release the bo::reserved lock. 181 */ 182 183 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) 184 { 185 if (bo->bdev->funcs->delete_mem_notify) 186 bo->bdev->funcs->delete_mem_notify(bo); 187 188 ttm_bo_tt_destroy(bo); 189 ttm_resource_free(bo, &bo->resource); 190 } 191 192 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo) 193 { 194 int r; 195 196 if (bo->base.resv == &bo->base._resv) 197 return 0; 198 199 BUG_ON(!dma_resv_trylock(&bo->base._resv)); 200 201 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv); 202 dma_resv_unlock(&bo->base._resv); 203 if (r) 204 return r; 205 206 if (bo->type != ttm_bo_type_sg) { 207 /* This works because the BO is about to be destroyed and nobody 208 * reference it any more. The only tricky case is the trylock on 209 * the resv object while holding the lru_lock. 210 */ 211 spin_lock(&bo->bdev->lru_lock); 212 bo->base.resv = &bo->base._resv; 213 spin_unlock(&bo->bdev->lru_lock); 214 } 215 216 return r; 217 } 218 219 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) 220 { 221 struct dma_resv *resv = &bo->base._resv; 222 struct dma_resv_iter cursor; 223 struct dma_fence *fence; 224 225 dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP); 226 dma_resv_for_each_fence_unlocked(&cursor, fence) 227 dma_fence_enable_sw_signaling(fence); 228 dma_resv_iter_end(&cursor); 229 } 230 231 /* 232 * Block for the dma_resv object to become idle, lock the buffer and clean up 233 * the resource and tt object. 234 */ 235 static void ttm_bo_delayed_delete(struct work_struct *work) 236 { 237 struct ttm_buffer_object *bo; 238 239 bo = container_of(work, typeof(*bo), delayed_delete); 240 241 dma_resv_wait_timeout(&bo->base._resv, DMA_RESV_USAGE_BOOKKEEP, false, 242 MAX_SCHEDULE_TIMEOUT); 243 dma_resv_lock(bo->base.resv, NULL); 244 ttm_bo_cleanup_memtype_use(bo); 245 dma_resv_unlock(bo->base.resv); 246 ttm_bo_put(bo); 247 } 248 249 static void ttm_bo_release(struct kref *kref) 250 { 251 struct ttm_buffer_object *bo = 252 container_of(kref, struct ttm_buffer_object, kref); 253 struct ttm_device *bdev = bo->bdev; 254 int ret; 255 256 WARN_ON_ONCE(bo->pin_count); 257 WARN_ON_ONCE(bo->bulk_move); 258 259 if (!bo->deleted) { 260 ret = ttm_bo_individualize_resv(bo); 261 if (ret) { 262 /* Last resort, if we fail to allocate memory for the 263 * fences block for the BO to become idle 264 */ 265 dma_resv_wait_timeout(bo->base.resv, 266 DMA_RESV_USAGE_BOOKKEEP, false, 267 30 * HZ); 268 } 269 270 if (bdev->funcs->release_notify) 271 bdev->funcs->release_notify(bo); 272 273 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node); 274 ttm_mem_io_free(bdev, bo->resource); 275 276 if (!dma_resv_test_signaled(&bo->base._resv, 277 DMA_RESV_USAGE_BOOKKEEP) || 278 (want_init_on_free() && (bo->ttm != NULL)) || 279 bo->type == ttm_bo_type_sg || 280 !dma_resv_trylock(bo->base.resv)) { 281 /* The BO is not idle, resurrect it for delayed destroy */ 282 ttm_bo_flush_all_fences(bo); 283 bo->deleted = true; 284 285 spin_lock(&bdev->lru_lock); 286 287 /* 288 * Make pinned bos immediately available to 289 * shrinkers, now that they are queued for 290 * destruction. 291 * 292 * FIXME: QXL is triggering this. Can be removed when the 293 * driver is fixed. 294 */ 295 if (bo->pin_count) { 296 bo->pin_count = 0; 297 ttm_resource_move_to_lru_tail(bo->resource); 298 } 299 300 kref_init(&bo->kref); 301 spin_unlock(&bdev->lru_lock); 302 303 INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete); 304 305 /* Schedule the worker on the closest NUMA node. This 306 * improves performance since system memory might be 307 * cleared on free and that is best done on a CPU core 308 * close to it. 309 */ 310 queue_work_node(bdev->pool.nid, bdev->wq, &bo->delayed_delete); 311 return; 312 } 313 314 ttm_bo_cleanup_memtype_use(bo); 315 dma_resv_unlock(bo->base.resv); 316 } 317 318 atomic_dec(&ttm_glob.bo_count); 319 bo->destroy(bo); 320 } 321 322 /* TODO: remove! */ 323 void ttm_bo_put(struct ttm_buffer_object *bo) 324 { 325 kref_put(&bo->kref, ttm_bo_release); 326 } 327 328 void ttm_bo_fini(struct ttm_buffer_object *bo) 329 { 330 ttm_bo_put(bo); 331 } 332 EXPORT_SYMBOL(ttm_bo_fini); 333 334 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo, 335 struct ttm_operation_ctx *ctx, 336 struct ttm_place *hop) 337 { 338 struct ttm_placement hop_placement; 339 struct ttm_resource *hop_mem; 340 int ret; 341 342 hop_placement.num_placement = 1; 343 hop_placement.placement = hop; 344 345 /* find space in the bounce domain */ 346 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx); 347 if (ret) 348 return ret; 349 /* move to the bounce domain */ 350 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL); 351 if (ret) { 352 ttm_resource_free(bo, &hop_mem); 353 return ret; 354 } 355 return 0; 356 } 357 358 static int ttm_bo_evict(struct ttm_buffer_object *bo, 359 struct ttm_operation_ctx *ctx) 360 { 361 struct ttm_resource *evict_mem; 362 struct ttm_placement placement; 363 struct ttm_place hop; 364 int ret = 0; 365 366 memset(&hop, 0, sizeof(hop)); 367 368 dma_resv_assert_held(bo->base.resv); 369 370 placement.num_placement = 0; 371 bo->bdev->funcs->evict_flags(bo, &placement); 372 373 if (!placement.num_placement) { 374 ret = ttm_bo_wait_ctx(bo, ctx); 375 if (ret) 376 return ret; 377 378 /* 379 * Since we've already synced, this frees backing store 380 * immediately. 381 */ 382 return ttm_bo_pipeline_gutting(bo); 383 } 384 385 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx); 386 if (ret) { 387 if (ret != -ERESTARTSYS) { 388 pr_err("Failed to find memory space for buffer 0x%p eviction\n", 389 bo); 390 ttm_bo_mem_space_debug(bo, &placement); 391 } 392 goto out; 393 } 394 395 do { 396 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop); 397 if (ret != -EMULTIHOP) 398 break; 399 400 ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop); 401 } while (!ret); 402 403 if (ret) { 404 ttm_resource_free(bo, &evict_mem); 405 if (ret != -ERESTARTSYS && ret != -EINTR) 406 pr_err("Buffer eviction failed\n"); 407 } 408 out: 409 return ret; 410 } 411 412 /** 413 * ttm_bo_eviction_valuable 414 * 415 * @bo: The buffer object to evict 416 * @place: the placement we need to make room for 417 * 418 * Check if it is valuable to evict the BO to make room for the given placement. 419 */ 420 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 421 const struct ttm_place *place) 422 { 423 struct ttm_resource *res = bo->resource; 424 425 dma_resv_assert_held(bo->base.resv); 426 427 if (res->mem_type == TTM_PL_SYSTEM) 428 return true; 429 430 /* Don't evict this BO if it's outside of the 431 * requested placement range 432 */ 433 return ttm_resource_intersects(bo->bdev, res, place, bo->base.size); 434 } 435 EXPORT_SYMBOL(ttm_bo_eviction_valuable); 436 437 /** 438 * ttm_bo_evict_first() - Evict the first bo on the manager's LRU list. 439 * @bdev: The ttm device. 440 * @man: The manager whose bo to evict. 441 * @ctx: The TTM operation ctx governing the eviction. 442 * 443 * Return: 0 if successful or the resource disappeared. Negative error code on error. 444 */ 445 int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man, 446 struct ttm_operation_ctx *ctx) 447 { 448 struct ttm_resource_cursor cursor; 449 struct ttm_buffer_object *bo; 450 struct ttm_resource *res; 451 unsigned int mem_type; 452 int ret = 0; 453 454 spin_lock(&bdev->lru_lock); 455 ttm_resource_cursor_init(&cursor, man); 456 res = ttm_resource_manager_first(&cursor); 457 ttm_resource_cursor_fini(&cursor); 458 if (!res) { 459 ret = -ENOENT; 460 goto out_no_ref; 461 } 462 bo = res->bo; 463 if (!ttm_bo_get_unless_zero(bo)) 464 goto out_no_ref; 465 mem_type = res->mem_type; 466 spin_unlock(&bdev->lru_lock); 467 ret = ttm_bo_reserve(bo, ctx->interruptible, ctx->no_wait_gpu, NULL); 468 if (ret) 469 goto out_no_lock; 470 if (!bo->resource || bo->resource->mem_type != mem_type) 471 goto out_bo_moved; 472 473 if (bo->deleted) { 474 ret = ttm_bo_wait_ctx(bo, ctx); 475 if (!ret) 476 ttm_bo_cleanup_memtype_use(bo); 477 } else { 478 ret = ttm_bo_evict(bo, ctx); 479 } 480 out_bo_moved: 481 dma_resv_unlock(bo->base.resv); 482 out_no_lock: 483 ttm_bo_put(bo); 484 return ret; 485 486 out_no_ref: 487 spin_unlock(&bdev->lru_lock); 488 return ret; 489 } 490 491 /** 492 * struct ttm_bo_evict_walk - Parameters for the evict walk. 493 */ 494 struct ttm_bo_evict_walk { 495 /** @walk: The walk base parameters. */ 496 struct ttm_lru_walk walk; 497 /** @place: The place passed to the resource allocation. */ 498 const struct ttm_place *place; 499 /** @evictor: The buffer object we're trying to make room for. */ 500 struct ttm_buffer_object *evictor; 501 /** @res: The allocated resource if any. */ 502 struct ttm_resource **res; 503 /** @evicted: Number of successful evictions. */ 504 unsigned long evicted; 505 506 /** @limit_pool: Which pool limit we should test against */ 507 struct dmem_cgroup_pool_state *limit_pool; 508 /** @try_low: Whether we should attempt to evict BO's with low watermark threshold */ 509 bool try_low; 510 /** @hit_low: If we cannot evict a bo when @try_low is false (first pass) */ 511 bool hit_low; 512 }; 513 514 static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) 515 { 516 struct ttm_bo_evict_walk *evict_walk = 517 container_of(walk, typeof(*evict_walk), walk); 518 s64 lret; 519 520 if (!dmem_cgroup_state_evict_valuable(evict_walk->limit_pool, bo->resource->css, 521 evict_walk->try_low, &evict_walk->hit_low)) 522 return 0; 523 524 if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->place)) 525 return 0; 526 527 if (bo->deleted) { 528 lret = ttm_bo_wait_ctx(bo, walk->arg.ctx); 529 if (!lret) 530 ttm_bo_cleanup_memtype_use(bo); 531 } else { 532 lret = ttm_bo_evict(bo, walk->arg.ctx); 533 } 534 535 if (lret) 536 goto out; 537 538 evict_walk->evicted++; 539 if (evict_walk->res) 540 lret = ttm_resource_alloc(evict_walk->evictor, evict_walk->place, 541 evict_walk->res, NULL); 542 if (lret == 0) 543 return 1; 544 out: 545 /* Errors that should terminate the walk. */ 546 if (lret == -ENOSPC) 547 return -EBUSY; 548 549 return lret; 550 } 551 552 static const struct ttm_lru_walk_ops ttm_evict_walk_ops = { 553 .process_bo = ttm_bo_evict_cb, 554 }; 555 556 static int ttm_bo_evict_alloc(struct ttm_device *bdev, 557 struct ttm_resource_manager *man, 558 const struct ttm_place *place, 559 struct ttm_buffer_object *evictor, 560 struct ttm_operation_ctx *ctx, 561 struct ww_acquire_ctx *ticket, 562 struct ttm_resource **res, 563 struct dmem_cgroup_pool_state *limit_pool) 564 { 565 struct ttm_bo_evict_walk evict_walk = { 566 .walk = { 567 .ops = &ttm_evict_walk_ops, 568 .arg = { 569 .ctx = ctx, 570 .ticket = ticket, 571 } 572 }, 573 .place = place, 574 .evictor = evictor, 575 .res = res, 576 .limit_pool = limit_pool, 577 }; 578 s64 lret; 579 580 evict_walk.walk.arg.trylock_only = true; 581 lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); 582 583 /* One more attempt if we hit low limit? */ 584 if (!lret && evict_walk.hit_low) { 585 evict_walk.try_low = true; 586 lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); 587 } 588 if (lret || !ticket) 589 goto out; 590 591 /* Reset low limit */ 592 evict_walk.try_low = evict_walk.hit_low = false; 593 /* If ticket-locking, repeat while making progress. */ 594 evict_walk.walk.arg.trylock_only = false; 595 596 retry: 597 do { 598 /* The walk may clear the evict_walk.walk.ticket field */ 599 evict_walk.walk.arg.ticket = ticket; 600 evict_walk.evicted = 0; 601 lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); 602 } while (!lret && evict_walk.evicted); 603 604 /* We hit the low limit? Try once more */ 605 if (!lret && evict_walk.hit_low && !evict_walk.try_low) { 606 evict_walk.try_low = true; 607 goto retry; 608 } 609 out: 610 if (lret < 0) 611 return lret; 612 if (lret == 0) 613 return -EBUSY; 614 return 0; 615 } 616 617 /** 618 * ttm_bo_pin - Pin the buffer object. 619 * @bo: The buffer object to pin 620 * 621 * Make sure the buffer is not evicted any more during memory pressure. 622 * @bo must be unpinned again by calling ttm_bo_unpin(). 623 */ 624 void ttm_bo_pin(struct ttm_buffer_object *bo) 625 { 626 dma_resv_assert_held(bo->base.resv); 627 WARN_ON_ONCE(!kref_read(&bo->kref)); 628 spin_lock(&bo->bdev->lru_lock); 629 if (bo->resource) 630 ttm_resource_del_bulk_move(bo->resource, bo); 631 if (!bo->pin_count++ && bo->resource) 632 ttm_resource_move_to_lru_tail(bo->resource); 633 spin_unlock(&bo->bdev->lru_lock); 634 } 635 EXPORT_SYMBOL(ttm_bo_pin); 636 637 /** 638 * ttm_bo_unpin - Unpin the buffer object. 639 * @bo: The buffer object to unpin 640 * 641 * Allows the buffer object to be evicted again during memory pressure. 642 */ 643 void ttm_bo_unpin(struct ttm_buffer_object *bo) 644 { 645 dma_resv_assert_held(bo->base.resv); 646 WARN_ON_ONCE(!kref_read(&bo->kref)); 647 if (WARN_ON_ONCE(!bo->pin_count)) 648 return; 649 650 spin_lock(&bo->bdev->lru_lock); 651 if (!--bo->pin_count && bo->resource) { 652 ttm_resource_add_bulk_move(bo->resource, bo); 653 ttm_resource_move_to_lru_tail(bo->resource); 654 } 655 spin_unlock(&bo->bdev->lru_lock); 656 } 657 EXPORT_SYMBOL(ttm_bo_unpin); 658 659 /* 660 * Add the pipelined eviction fencesto the BO as kernel dependency and reserve new 661 * fence slots. 662 */ 663 static int ttm_bo_add_pipelined_eviction_fences(struct ttm_buffer_object *bo, 664 struct ttm_resource_manager *man, 665 bool no_wait_gpu) 666 { 667 struct dma_fence *fence; 668 int i; 669 670 spin_lock(&man->eviction_lock); 671 for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) { 672 fence = man->eviction_fences[i]; 673 if (!fence) 674 continue; 675 676 if (no_wait_gpu) { 677 if (!dma_fence_is_signaled(fence)) { 678 spin_unlock(&man->eviction_lock); 679 return -EBUSY; 680 } 681 } else { 682 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL); 683 } 684 } 685 spin_unlock(&man->eviction_lock); 686 687 /* TODO: this call should be removed. */ 688 return dma_resv_reserve_fences(bo->base.resv, 1); 689 } 690 691 /** 692 * ttm_bo_alloc_resource - Allocate backing store for a BO 693 * 694 * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for 695 * @placement: Proposed new placement for the buffer object 696 * @ctx: if and how to sleep, lock buffers and alloc memory 697 * @force_space: If we should evict buffers to force space 698 * @res: The resulting struct ttm_resource. 699 * 700 * Allocates a resource for the buffer object pointed to by @bo, using the 701 * placement flags in @placement, potentially evicting other buffer objects when 702 * @force_space is true. 703 * This function may sleep while waiting for resources to become available. 704 * Returns: 705 * -EBUSY: No space available (only if no_wait == true). 706 * -ENOSPC: Could not allocate space for the buffer object, either due to 707 * fragmentation or concurrent allocators. 708 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal. 709 */ 710 static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo, 711 struct ttm_placement *placement, 712 struct ttm_operation_ctx *ctx, 713 bool force_space, 714 struct ttm_resource **res) 715 { 716 struct ttm_device *bdev = bo->bdev; 717 struct ww_acquire_ctx *ticket; 718 int i, ret; 719 720 ticket = dma_resv_locking_ctx(bo->base.resv); 721 ret = dma_resv_reserve_fences(bo->base.resv, TTM_NUM_MOVE_FENCES); 722 if (unlikely(ret)) 723 return ret; 724 725 for (i = 0; i < placement->num_placement; ++i) { 726 const struct ttm_place *place = &placement->placement[i]; 727 struct dmem_cgroup_pool_state *limit_pool = NULL; 728 struct ttm_resource_manager *man; 729 bool may_evict; 730 731 man = ttm_manager_type(bdev, place->mem_type); 732 if (!man || !ttm_resource_manager_used(man)) 733 continue; 734 735 if (place->flags & (force_space ? TTM_PL_FLAG_DESIRED : 736 TTM_PL_FLAG_FALLBACK)) 737 continue; 738 739 may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM); 740 ret = ttm_resource_alloc(bo, place, res, force_space ? &limit_pool : NULL); 741 if (ret) { 742 if (ret != -ENOSPC && ret != -EAGAIN) { 743 dmem_cgroup_pool_state_put(limit_pool); 744 return ret; 745 } 746 if (!may_evict) { 747 dmem_cgroup_pool_state_put(limit_pool); 748 continue; 749 } 750 751 ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx, 752 ticket, res, limit_pool); 753 dmem_cgroup_pool_state_put(limit_pool); 754 if (ret == -EBUSY) 755 continue; 756 if (ret) 757 return ret; 758 } 759 760 ret = ttm_bo_add_pipelined_eviction_fences(bo, man, ctx->no_wait_gpu); 761 if (unlikely(ret)) { 762 ttm_resource_free(bo, res); 763 if (ret == -EBUSY) 764 continue; 765 766 return ret; 767 } 768 return 0; 769 } 770 771 return -ENOSPC; 772 } 773 774 /* 775 * ttm_bo_mem_space - Wrapper around ttm_bo_alloc_resource 776 * 777 * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for 778 * @placement: Proposed new placement for the buffer object 779 * @res: The resulting struct ttm_resource. 780 * @ctx: if and how to sleep, lock buffers and alloc memory 781 * 782 * Tries both idle allocation and forcefully eviction of buffers. See 783 * ttm_bo_alloc_resource for details. 784 */ 785 int ttm_bo_mem_space(struct ttm_buffer_object *bo, 786 struct ttm_placement *placement, 787 struct ttm_resource **res, 788 struct ttm_operation_ctx *ctx) 789 { 790 bool force_space = false; 791 int ret; 792 793 do { 794 ret = ttm_bo_alloc_resource(bo, placement, ctx, 795 force_space, res); 796 force_space = !force_space; 797 } while (ret == -ENOSPC && force_space); 798 799 return ret; 800 } 801 EXPORT_SYMBOL(ttm_bo_mem_space); 802 803 /** 804 * ttm_bo_validate 805 * 806 * @bo: The buffer object. 807 * @placement: Proposed placement for the buffer object. 808 * @ctx: validation parameters. 809 * 810 * Changes placement and caching policy of the buffer object 811 * according proposed placement. 812 * Returns 813 * -EINVAL on invalid proposed placement. 814 * -ENOMEM on out-of-memory condition. 815 * -EBUSY if no_wait is true and buffer busy. 816 * -ERESTARTSYS if interrupted by a signal. 817 */ 818 int ttm_bo_validate(struct ttm_buffer_object *bo, 819 struct ttm_placement *placement, 820 struct ttm_operation_ctx *ctx) 821 { 822 struct ttm_resource *res; 823 struct ttm_place hop; 824 bool force_space; 825 int ret; 826 827 dma_resv_assert_held(bo->base.resv); 828 829 /* 830 * Remove the backing store if no placement is given. 831 */ 832 if (!placement->num_placement) 833 return ttm_bo_pipeline_gutting(bo); 834 835 force_space = false; 836 do { 837 /* Check whether we need to move buffer. */ 838 if (bo->resource && 839 ttm_resource_compatible(bo->resource, placement, 840 force_space)) 841 return 0; 842 843 /* Moving of pinned BOs is forbidden */ 844 if (bo->pin_count) 845 return -EINVAL; 846 847 /* 848 * Determine where to move the buffer. 849 * 850 * If driver determines move is going to need 851 * an extra step then it will return -EMULTIHOP 852 * and the buffer will be moved to the temporary 853 * stop and the driver will be called to make 854 * the second hop. 855 */ 856 ret = ttm_bo_alloc_resource(bo, placement, ctx, force_space, 857 &res); 858 force_space = !force_space; 859 if (ret == -ENOSPC) 860 continue; 861 if (ret) 862 return ret; 863 864 bounce: 865 ret = ttm_bo_handle_move_mem(bo, res, false, ctx, &hop); 866 if (ret == -EMULTIHOP) { 867 ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop); 868 /* try and move to final place now. */ 869 if (!ret) 870 goto bounce; 871 } 872 if (ret) { 873 ttm_resource_free(bo, &res); 874 return ret; 875 } 876 877 } while (ret && force_space); 878 879 /* For backward compatibility with userspace */ 880 if (ret == -ENOSPC) 881 return bo->bdev->alloc_flags & TTM_ALLOCATION_PROPAGATE_ENOSPC ? 882 ret : -ENOMEM; 883 884 /* 885 * We might need to add a TTM. 886 */ 887 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) { 888 ret = ttm_tt_create(bo, true); 889 if (ret) 890 return ret; 891 } 892 return 0; 893 } 894 EXPORT_SYMBOL(ttm_bo_validate); 895 896 /** 897 * ttm_bo_init_reserved 898 * 899 * @bdev: Pointer to a ttm_device struct. 900 * @bo: Pointer to a ttm_buffer_object to be initialized. 901 * @type: Requested type of buffer object. 902 * @placement: Initial placement for buffer object. 903 * @alignment: Data alignment in pages. 904 * @ctx: TTM operation context for memory allocation. 905 * @sg: Scatter-gather table. 906 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one. 907 * @destroy: Destroy function. Use NULL for kfree(). 908 * 909 * This function initializes a pre-allocated struct ttm_buffer_object. 910 * As this object may be part of a larger structure, this function, 911 * together with the @destroy function, enables driver-specific objects 912 * derived from a ttm_buffer_object. 913 * 914 * On successful return, the caller owns an object kref to @bo. The kref and 915 * list_kref are usually set to 1, but note that in some situations, other 916 * tasks may already be holding references to @bo as well. 917 * Furthermore, if resv == NULL, the buffer's reservation lock will be held, 918 * and it is the caller's responsibility to call ttm_bo_unreserve. 919 * 920 * If a failure occurs, the function will call the @destroy function. Thus, 921 * after a failure, dereferencing @bo is illegal and will likely cause memory 922 * corruption. 923 * 924 * Returns 925 * -ENOMEM: Out of memory. 926 * -EINVAL: Invalid placement flags. 927 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 928 */ 929 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo, 930 enum ttm_bo_type type, struct ttm_placement *placement, 931 uint32_t alignment, struct ttm_operation_ctx *ctx, 932 struct sg_table *sg, struct dma_resv *resv, 933 void (*destroy) (struct ttm_buffer_object *)) 934 { 935 int ret; 936 937 kref_init(&bo->kref); 938 bo->bdev = bdev; 939 bo->type = type; 940 bo->page_alignment = alignment; 941 bo->destroy = destroy; 942 bo->pin_count = 0; 943 bo->sg = sg; 944 bo->bulk_move = NULL; 945 if (resv) 946 bo->base.resv = resv; 947 else 948 bo->base.resv = &bo->base._resv; 949 atomic_inc(&ttm_glob.bo_count); 950 951 /* 952 * For ttm_bo_type_device buffers, allocate 953 * address space from the device. 954 */ 955 if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) { 956 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node, 957 PFN_UP(bo->base.size)); 958 if (ret) 959 goto err_put; 960 } 961 962 /* passed reservation objects should already be locked, 963 * since otherwise lockdep will be angered in radeon. 964 */ 965 if (!resv) 966 WARN_ON(!dma_resv_trylock(bo->base.resv)); 967 else 968 dma_resv_assert_held(resv); 969 970 ret = ttm_bo_validate(bo, placement, ctx); 971 if (unlikely(ret)) 972 goto err_unlock; 973 974 return 0; 975 976 err_unlock: 977 if (!resv) 978 dma_resv_unlock(bo->base.resv); 979 980 err_put: 981 ttm_bo_put(bo); 982 return ret; 983 } 984 EXPORT_SYMBOL(ttm_bo_init_reserved); 985 986 /** 987 * ttm_bo_init_validate 988 * 989 * @bdev: Pointer to a ttm_device struct. 990 * @bo: Pointer to a ttm_buffer_object to be initialized. 991 * @type: Requested type of buffer object. 992 * @placement: Initial placement for buffer object. 993 * @alignment: Data alignment in pages. 994 * @interruptible: If needing to sleep to wait for GPU resources, 995 * sleep interruptible. 996 * pinned in physical memory. If this behaviour is not desired, this member 997 * holds a pointer to a persistent shmem object. Typically, this would 998 * point to the shmem object backing a GEM object if TTM is used to back a 999 * GEM user interface. 1000 * @sg: Scatter-gather table. 1001 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one. 1002 * @destroy: Destroy function. Use NULL for kfree(). 1003 * 1004 * This function initializes a pre-allocated struct ttm_buffer_object. 1005 * As this object may be part of a larger structure, this function, 1006 * together with the @destroy function, 1007 * enables driver-specific objects derived from a ttm_buffer_object. 1008 * 1009 * On successful return, the caller owns an object kref to @bo. The kref and 1010 * list_kref are usually set to 1, but note that in some situations, other 1011 * tasks may already be holding references to @bo as well. 1012 * 1013 * If a failure occurs, the function will call the @destroy function, Thus, 1014 * after a failure, dereferencing @bo is illegal and will likely cause memory 1015 * corruption. 1016 * 1017 * Returns 1018 * -ENOMEM: Out of memory. 1019 * -EINVAL: Invalid placement flags. 1020 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 1021 */ 1022 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo, 1023 enum ttm_bo_type type, struct ttm_placement *placement, 1024 uint32_t alignment, bool interruptible, 1025 struct sg_table *sg, struct dma_resv *resv, 1026 void (*destroy) (struct ttm_buffer_object *)) 1027 { 1028 struct ttm_operation_ctx ctx = { .interruptible = interruptible }; 1029 int ret; 1030 1031 ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx, 1032 sg, resv, destroy); 1033 if (ret) 1034 return ret; 1035 1036 if (!resv) 1037 ttm_bo_unreserve(bo); 1038 1039 return 0; 1040 } 1041 EXPORT_SYMBOL(ttm_bo_init_validate); 1042 1043 /* 1044 * buffer object vm functions. 1045 */ 1046 1047 /** 1048 * ttm_bo_unmap_virtual 1049 * 1050 * @bo: tear down the virtual mappings for this BO 1051 */ 1052 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) 1053 { 1054 struct ttm_device *bdev = bo->bdev; 1055 1056 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping); 1057 ttm_mem_io_free(bdev, bo->resource); 1058 } 1059 EXPORT_SYMBOL(ttm_bo_unmap_virtual); 1060 1061 /** 1062 * ttm_bo_wait_ctx - wait for buffer idle. 1063 * 1064 * @bo: The buffer object. 1065 * @ctx: defines how to wait 1066 * 1067 * Waits for the buffer to be idle. Used timeout depends on the context. 1068 * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or 1069 * zero on success. 1070 */ 1071 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx) 1072 { 1073 long ret; 1074 1075 if (ctx->no_wait_gpu) { 1076 if (dma_resv_test_signaled(bo->base.resv, 1077 DMA_RESV_USAGE_BOOKKEEP)) 1078 return 0; 1079 else 1080 return -EBUSY; 1081 } 1082 1083 ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, 1084 ctx->interruptible, 15 * HZ); 1085 if (unlikely(ret < 0)) 1086 return ret; 1087 if (unlikely(ret == 0)) 1088 return -EBUSY; 1089 return 0; 1090 } 1091 EXPORT_SYMBOL(ttm_bo_wait_ctx); 1092 1093 /** 1094 * struct ttm_bo_swapout_walk - Parameters for the swapout walk 1095 */ 1096 struct ttm_bo_swapout_walk { 1097 /** @walk: The walk base parameters. */ 1098 struct ttm_lru_walk walk; 1099 /** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */ 1100 gfp_t gfp_flags; 1101 /** @hit_low: Whether we should attempt to swap BO's with low watermark threshold */ 1102 /** @evict_low: If we cannot swap a bo when @try_low is false (first pass) */ 1103 bool hit_low, evict_low; 1104 }; 1105 1106 static s64 1107 ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) 1108 { 1109 struct ttm_place place = { .mem_type = bo->resource->mem_type }; 1110 struct ttm_bo_swapout_walk *swapout_walk = 1111 container_of(walk, typeof(*swapout_walk), walk); 1112 struct ttm_operation_ctx *ctx = walk->arg.ctx; 1113 struct ttm_device *bdev = bo->bdev; 1114 struct ttm_tt *tt = bo->ttm; 1115 s64 ret; 1116 1117 /* 1118 * While the bo may already reside in SYSTEM placement, set 1119 * SYSTEM as new placement to cover also the move further below. 1120 * The driver may use the fact that we're moving from SYSTEM 1121 * as an indication that we're about to swap out. 1122 */ 1123 if (bo->pin_count || !bdev->funcs->eviction_valuable(bo, &place)) { 1124 ret = -EBUSY; 1125 goto out; 1126 } 1127 1128 if (!tt || !ttm_tt_is_populated(tt) || 1129 tt->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_SWAPPED)) { 1130 ret = -EBUSY; 1131 goto out; 1132 } 1133 1134 if (bo->deleted) { 1135 pgoff_t num_pages = tt->num_pages; 1136 1137 ret = ttm_bo_wait_ctx(bo, ctx); 1138 if (ret) 1139 goto out; 1140 1141 ttm_bo_cleanup_memtype_use(bo); 1142 ret = num_pages; 1143 goto out; 1144 } 1145 1146 /* 1147 * Move to system cached 1148 */ 1149 if (bo->resource->mem_type != TTM_PL_SYSTEM) { 1150 struct ttm_resource *evict_mem; 1151 struct ttm_place hop; 1152 1153 memset(&hop, 0, sizeof(hop)); 1154 place.mem_type = TTM_PL_SYSTEM; 1155 ret = ttm_resource_alloc(bo, &place, &evict_mem, NULL); 1156 if (ret) 1157 goto out; 1158 1159 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop); 1160 if (ret) { 1161 WARN(ret == -EMULTIHOP, 1162 "Unexpected multihop in swapout - likely driver bug.\n"); 1163 ttm_resource_free(bo, &evict_mem); 1164 goto out; 1165 } 1166 } 1167 1168 /* 1169 * Make sure BO is idle. 1170 */ 1171 ret = ttm_bo_wait_ctx(bo, ctx); 1172 if (ret) 1173 goto out; 1174 1175 ttm_bo_unmap_virtual(bo); 1176 if (bdev->funcs->swap_notify) 1177 bdev->funcs->swap_notify(bo); 1178 1179 if (ttm_tt_is_populated(tt)) { 1180 ret = ttm_tt_swapout(bdev, tt, swapout_walk->gfp_flags); 1181 if (!ret) { 1182 spin_lock(&bdev->lru_lock); 1183 ttm_resource_del_bulk_move_unevictable(bo->resource, bo); 1184 ttm_resource_move_to_lru_tail(bo->resource); 1185 spin_unlock(&bdev->lru_lock); 1186 } 1187 } 1188 1189 out: 1190 /* Consider -ENOMEM and -ENOSPC non-fatal. */ 1191 if (ret == -ENOMEM || ret == -ENOSPC) 1192 ret = -EBUSY; 1193 1194 return ret; 1195 } 1196 1197 const struct ttm_lru_walk_ops ttm_swap_ops = { 1198 .process_bo = ttm_bo_swapout_cb, 1199 }; 1200 1201 /** 1202 * ttm_bo_swapout() - Swap out buffer objects on the LRU list to shmem. 1203 * @bdev: The ttm device. 1204 * @ctx: The ttm_operation_ctx governing the swapout operation. 1205 * @man: The resource manager whose resources / buffer objects are 1206 * goint to be swapped out. 1207 * @gfp_flags: The gfp flags used for shmem page allocations. 1208 * @target: The desired number of pages to swap out. 1209 * 1210 * Return: The number of pages actually swapped out, or negative error code 1211 * on error. 1212 */ 1213 s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, 1214 struct ttm_resource_manager *man, gfp_t gfp_flags, 1215 s64 target) 1216 { 1217 struct ttm_bo_swapout_walk swapout_walk = { 1218 .walk = { 1219 .ops = &ttm_swap_ops, 1220 .arg = { 1221 .ctx = ctx, 1222 .trylock_only = true, 1223 }, 1224 }, 1225 .gfp_flags = gfp_flags, 1226 }; 1227 1228 return ttm_lru_walk_for_evict(&swapout_walk.walk, bdev, man, target); 1229 } 1230 EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_bo_swapout); 1231 1232 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo) 1233 { 1234 if (bo->ttm == NULL) 1235 return; 1236 1237 ttm_tt_unpopulate(bo->bdev, bo->ttm); 1238 ttm_tt_destroy(bo->bdev, bo->ttm); 1239 bo->ttm = NULL; 1240 } 1241 1242 /** 1243 * ttm_bo_populate() - Ensure that a buffer object has backing pages 1244 * @bo: The buffer object 1245 * @ctx: The ttm_operation_ctx governing the operation. 1246 * 1247 * For buffer objects in a memory type whose manager uses 1248 * struct ttm_tt for backing pages, ensure those backing pages 1249 * are present and with valid content. The bo's resource is also 1250 * placed on the correct LRU list if it was previously swapped 1251 * out. 1252 * 1253 * Return: 0 if successful, negative error code on failure. 1254 * Note: May return -EINTR or -ERESTARTSYS if @ctx::interruptible 1255 * is set to true. 1256 */ 1257 int ttm_bo_populate(struct ttm_buffer_object *bo, 1258 struct ttm_operation_ctx *ctx) 1259 { 1260 struct ttm_device *bdev = bo->bdev; 1261 struct ttm_tt *tt = bo->ttm; 1262 bool swapped; 1263 int ret; 1264 1265 dma_resv_assert_held(bo->base.resv); 1266 1267 if (!tt) 1268 return 0; 1269 1270 swapped = ttm_tt_is_swapped(tt); 1271 ret = ttm_tt_populate(bdev, tt, ctx); 1272 if (ret) 1273 return ret; 1274 1275 if (swapped && !ttm_tt_is_swapped(tt) && !bo->pin_count && 1276 bo->resource) { 1277 spin_lock(&bdev->lru_lock); 1278 ttm_resource_add_bulk_move(bo->resource, bo); 1279 ttm_resource_move_to_lru_tail(bo->resource); 1280 spin_unlock(&bdev->lru_lock); 1281 } 1282 1283 return 0; 1284 } 1285 EXPORT_SYMBOL(ttm_bo_populate); 1286 1287 int ttm_bo_setup_export(struct ttm_buffer_object *bo, 1288 struct ttm_operation_ctx *ctx) 1289 { 1290 int ret; 1291 1292 ret = ttm_bo_reserve(bo, false, false, NULL); 1293 if (ret != 0) 1294 return ret; 1295 1296 ret = ttm_bo_populate(bo, ctx); 1297 ttm_bo_unreserve(bo); 1298 return ret; 1299 } 1300 EXPORT_SYMBOL(ttm_bo_setup_export); 1301