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