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/ttm/ttm_bo.h> 35 #include <drm/ttm/ttm_placement.h> 36 #include <drm/ttm/ttm_tt.h> 37 38 #include <linux/export.h> 39 #include <linux/jiffies.h> 40 #include <linux/slab.h> 41 #include <linux/sched.h> 42 #include <linux/mm.h> 43 #include <linux/file.h> 44 #include <linux/module.h> 45 #include <linux/atomic.h> 46 #include <linux/cgroup_dmem.h> 47 #include <linux/dma-resv.h> 48 49 #include "ttm_module.h" 50 #include "ttm_bo_internal.h" 51 52 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, 53 struct ttm_placement *placement) 54 { 55 struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, TTM_PFX); 56 struct ttm_resource_manager *man; 57 int i, mem_type; 58 59 for (i = 0; i < placement->num_placement; i++) { 60 mem_type = placement->placement[i].mem_type; 61 drm_printf(&p, " placement[%d]=0x%08X (%d)\n", 62 i, placement->placement[i].flags, mem_type); 63 man = ttm_manager_type(bo->bdev, mem_type); 64 ttm_resource_manager_debug(man, &p); 65 } 66 } 67 68 /** 69 * ttm_bo_move_to_lru_tail 70 * 71 * @bo: The buffer object. 72 * 73 * Move this BO to the tail of all lru lists used to lookup and reserve an 74 * object. This function must be called with struct ttm_global::lru_lock 75 * held, and is used to make a BO less likely to be considered for eviction. 76 */ 77 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo) 78 { 79 dma_resv_assert_held(bo->base.resv); 80 81 if (bo->resource) 82 ttm_resource_move_to_lru_tail(bo->resource); 83 } 84 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail); 85 86 /** 87 * ttm_bo_set_bulk_move - update BOs bulk move object 88 * 89 * @bo: The buffer object. 90 * @bulk: bulk move structure 91 * 92 * Update the BOs bulk move object, making sure that resources are added/removed 93 * as well. A bulk move allows to move many resource on the LRU at once, 94 * resulting in much less overhead of maintaining the LRU. 95 * The only requirement is that the resources stay together on the LRU and are 96 * never separated. This is enforces by setting the bulk_move structure on a BO. 97 * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of 98 * their LRU list. 99 */ 100 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo, 101 struct ttm_lru_bulk_move *bulk) 102 { 103 dma_resv_assert_held(bo->base.resv); 104 105 if (bo->bulk_move == bulk) 106 return; 107 108 spin_lock(&bo->bdev->lru_lock); 109 if (bo->resource) 110 ttm_resource_del_bulk_move(bo->resource, bo); 111 bo->bulk_move = bulk; 112 if (bo->resource) 113 ttm_resource_add_bulk_move(bo->resource, bo); 114 spin_unlock(&bo->bdev->lru_lock); 115 } 116 EXPORT_SYMBOL(ttm_bo_set_bulk_move); 117 118 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, 119 struct ttm_resource *mem, bool evict, 120 struct ttm_operation_ctx *ctx, 121 struct ttm_place *hop) 122 { 123 struct ttm_device *bdev = bo->bdev; 124 bool old_use_tt, new_use_tt; 125 int ret; 126 127 old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt; 128 new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt; 129 130 ttm_bo_unmap_virtual(bo); 131 132 /* 133 * Create and bind a ttm if required. 134 */ 135 136 if (new_use_tt) { 137 /* Zero init the new TTM structure if the old location should 138 * have used one as well. 139 */ 140 ret = ttm_tt_create(bo, old_use_tt); 141 if (ret) 142 goto out_err; 143 144 if (mem->mem_type != TTM_PL_SYSTEM) { 145 ret = ttm_bo_populate(bo, ctx); 146 if (ret) 147 goto out_err; 148 } 149 } 150 151 ret = dma_resv_reserve_fences(bo->base.resv, 1); 152 if (ret) 153 goto out_err; 154 155 ret = bdev->funcs->move(bo, evict, ctx, mem, hop); 156 if (ret) { 157 if (ret == -EMULTIHOP) 158 return ret; 159 goto out_err; 160 } 161 162 ctx->bytes_moved += bo->base.size; 163 return 0; 164 165 out_err: 166 if (!old_use_tt) 167 ttm_bo_tt_destroy(bo); 168 169 return ret; 170 } 171 172 /* 173 * Call bo::reserved. 174 * Will release GPU memory type usage on destruction. 175 * This is the place to put in driver specific hooks to release 176 * driver private resources. 177 * Will release the bo::reserved lock. 178 */ 179 180 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) 181 { 182 if (bo->bdev->funcs->delete_mem_notify) 183 bo->bdev->funcs->delete_mem_notify(bo); 184 185 ttm_bo_tt_destroy(bo); 186 ttm_resource_free(bo, &bo->resource); 187 } 188 189 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo) 190 { 191 int r; 192 193 if (bo->base.resv == &bo->base._resv) 194 return 0; 195 196 BUG_ON(!dma_resv_trylock(&bo->base._resv)); 197 198 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv); 199 dma_resv_unlock(&bo->base._resv); 200 if (r) 201 return r; 202 203 if (bo->type != ttm_bo_type_sg) { 204 /* This works because the BO is about to be destroyed and nobody 205 * reference it any more. The only tricky case is the trylock on 206 * the resv object while holding the lru_lock. 207 */ 208 spin_lock(&bo->bdev->lru_lock); 209 bo->base.resv = &bo->base._resv; 210 spin_unlock(&bo->bdev->lru_lock); 211 } 212 213 return r; 214 } 215 216 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) 217 { 218 struct dma_resv *resv = &bo->base._resv; 219 struct dma_resv_iter cursor; 220 struct dma_fence *fence; 221 222 dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP); 223 dma_resv_for_each_fence_unlocked(&cursor, fence) { 224 if (!fence->ops->signaled) 225 dma_fence_enable_sw_signaling(fence); 226 } 227 dma_resv_iter_end(&cursor); 228 } 229 230 /* 231 * Block for the dma_resv object to become idle, lock the buffer and clean up 232 * the resource and tt object. 233 */ 234 static void ttm_bo_delayed_delete(struct work_struct *work) 235 { 236 struct ttm_buffer_object *bo; 237 238 bo = container_of(work, typeof(*bo), delayed_delete); 239 240 dma_resv_wait_timeout(&bo->base._resv, DMA_RESV_USAGE_BOOKKEEP, false, 241 MAX_SCHEDULE_TIMEOUT); 242 dma_resv_lock(bo->base.resv, NULL); 243 ttm_bo_cleanup_memtype_use(bo); 244 dma_resv_unlock(bo->base.resv); 245 ttm_bo_put(bo); 246 } 247 248 static void ttm_bo_release(struct kref *kref) 249 { 250 struct ttm_buffer_object *bo = 251 container_of(kref, struct ttm_buffer_object, kref); 252 struct ttm_device *bdev = bo->bdev; 253 int ret; 254 255 WARN_ON_ONCE(bo->pin_count); 256 WARN_ON_ONCE(bo->bulk_move); 257 258 if (!bo->deleted) { 259 ret = ttm_bo_individualize_resv(bo); 260 if (ret) { 261 /* Last resort, if we fail to allocate memory for the 262 * fences block for the BO to become idle 263 */ 264 dma_resv_wait_timeout(bo->base.resv, 265 DMA_RESV_USAGE_BOOKKEEP, false, 266 30 * HZ); 267 } 268 269 if (bo->bdev->funcs->release_notify) 270 bo->bdev->funcs->release_notify(bo); 271 272 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node); 273 ttm_mem_io_free(bdev, bo->resource); 274 275 if (!dma_resv_test_signaled(&bo->base._resv, 276 DMA_RESV_USAGE_BOOKKEEP) || 277 (want_init_on_free() && (bo->ttm != NULL)) || 278 bo->type == ttm_bo_type_sg || 279 !dma_resv_trylock(bo->base.resv)) { 280 /* The BO is not idle, resurrect it for delayed destroy */ 281 ttm_bo_flush_all_fences(bo); 282 bo->deleted = true; 283 284 spin_lock(&bo->bdev->lru_lock); 285 286 /* 287 * Make pinned bos immediately available to 288 * shrinkers, now that they are queued for 289 * destruction. 290 * 291 * FIXME: QXL is triggering this. Can be removed when the 292 * driver is fixed. 293 */ 294 if (bo->pin_count) { 295 bo->pin_count = 0; 296 ttm_resource_move_to_lru_tail(bo->resource); 297 } 298 299 kref_init(&bo->kref); 300 spin_unlock(&bo->bdev->lru_lock); 301 302 INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete); 303 304 /* Schedule the worker on the closest NUMA node. This 305 * improves performance since system memory might be 306 * cleared on free and that is best done on a CPU core 307 * close to it. 308 */ 309 queue_work_node(bdev->pool.nid, bdev->wq, &bo->delayed_delete); 310 return; 311 } 312 313 ttm_bo_cleanup_memtype_use(bo); 314 dma_resv_unlock(bo->base.resv); 315 } 316 317 atomic_dec(&ttm_glob.bo_count); 318 bo->destroy(bo); 319 } 320 321 /* TODO: remove! */ 322 void ttm_bo_put(struct ttm_buffer_object *bo) 323 { 324 kref_put(&bo->kref, ttm_bo_release); 325 } 326 327 void ttm_bo_fini(struct ttm_buffer_object *bo) 328 { 329 ttm_bo_put(bo); 330 } 331 EXPORT_SYMBOL(ttm_bo_fini); 332 333 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo, 334 struct ttm_operation_ctx *ctx, 335 struct ttm_place *hop) 336 { 337 struct ttm_placement hop_placement; 338 struct ttm_resource *hop_mem; 339 int ret; 340 341 hop_placement.num_placement = 1; 342 hop_placement.placement = hop; 343 344 /* find space in the bounce domain */ 345 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx); 346 if (ret) 347 return ret; 348 /* move to the bounce domain */ 349 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL); 350 if (ret) { 351 ttm_resource_free(bo, &hop_mem); 352 return ret; 353 } 354 return 0; 355 } 356 357 static int ttm_bo_evict(struct ttm_buffer_object *bo, 358 struct ttm_operation_ctx *ctx) 359 { 360 struct ttm_device *bdev = bo->bdev; 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 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 struct ttm_device *bdev = bo->bdev; 425 426 dma_resv_assert_held(bo->base.resv); 427 if (bo->resource->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(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 last move fence to the BO as kernel dependency and reserve a new 661 * fence slot. 662 */ 663 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo, 664 struct ttm_resource_manager *man, 665 bool no_wait_gpu) 666 { 667 struct dma_fence *fence; 668 int ret; 669 670 spin_lock(&man->move_lock); 671 fence = dma_fence_get(man->move); 672 spin_unlock(&man->move_lock); 673 674 if (!fence) 675 return 0; 676 677 if (no_wait_gpu) { 678 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY; 679 dma_fence_put(fence); 680 return ret; 681 } 682 683 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL); 684 685 ret = dma_resv_reserve_fences(bo->base.resv, 1); 686 dma_fence_put(fence); 687 return ret; 688 } 689 690 /** 691 * ttm_bo_alloc_resource - Allocate backing store for a BO 692 * 693 * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for 694 * @placement: Proposed new placement for the buffer object 695 * @ctx: if and how to sleep, lock buffers and alloc memory 696 * @force_space: If we should evict buffers to force space 697 * @res: The resulting struct ttm_resource. 698 * 699 * Allocates a resource for the buffer object pointed to by @bo, using the 700 * placement flags in @placement, potentially evicting other buffer objects when 701 * @force_space is true. 702 * This function may sleep while waiting for resources to become available. 703 * Returns: 704 * -EBUSY: No space available (only if no_wait == true). 705 * -ENOSPC: Could not allocate space for the buffer object, either due to 706 * fragmentation or concurrent allocators. 707 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal. 708 */ 709 static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo, 710 struct ttm_placement *placement, 711 struct ttm_operation_ctx *ctx, 712 bool force_space, 713 struct ttm_resource **res) 714 { 715 struct ttm_device *bdev = bo->bdev; 716 struct ww_acquire_ctx *ticket; 717 int i, ret; 718 719 ticket = dma_resv_locking_ctx(bo->base.resv); 720 ret = dma_resv_reserve_fences(bo->base.resv, 1); 721 if (unlikely(ret)) 722 return ret; 723 724 for (i = 0; i < placement->num_placement; ++i) { 725 const struct ttm_place *place = &placement->placement[i]; 726 struct dmem_cgroup_pool_state *limit_pool = NULL; 727 struct ttm_resource_manager *man; 728 bool may_evict; 729 730 man = ttm_manager_type(bdev, place->mem_type); 731 if (!man || !ttm_resource_manager_used(man)) 732 continue; 733 734 if (place->flags & (force_space ? TTM_PL_FLAG_DESIRED : 735 TTM_PL_FLAG_FALLBACK)) 736 continue; 737 738 may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM); 739 ret = ttm_resource_alloc(bo, place, res, force_space ? &limit_pool : NULL); 740 if (ret) { 741 if (ret != -ENOSPC && ret != -EAGAIN) { 742 dmem_cgroup_pool_state_put(limit_pool); 743 return ret; 744 } 745 if (!may_evict) { 746 dmem_cgroup_pool_state_put(limit_pool); 747 continue; 748 } 749 750 ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx, 751 ticket, res, limit_pool); 752 dmem_cgroup_pool_state_put(limit_pool); 753 if (ret == -EBUSY) 754 continue; 755 if (ret) 756 return ret; 757 } 758 759 ret = ttm_bo_add_move_fence(bo, man, ctx->no_wait_gpu); 760 if (unlikely(ret)) { 761 ttm_resource_free(bo, res); 762 if (ret == -EBUSY) 763 continue; 764 765 return ret; 766 } 767 return 0; 768 } 769 770 return -ENOSPC; 771 } 772 773 /* 774 * ttm_bo_mem_space - Wrapper around ttm_bo_alloc_resource 775 * 776 * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for 777 * @placement: Proposed new placement for the buffer object 778 * @res: The resulting struct ttm_resource. 779 * @ctx: if and how to sleep, lock buffers and alloc memory 780 * 781 * Tries both idle allocation and forcefully eviction of buffers. See 782 * ttm_bo_alloc_resource for details. 783 */ 784 int ttm_bo_mem_space(struct ttm_buffer_object *bo, 785 struct ttm_placement *placement, 786 struct ttm_resource **res, 787 struct ttm_operation_ctx *ctx) 788 { 789 bool force_space = false; 790 int ret; 791 792 do { 793 ret = ttm_bo_alloc_resource(bo, placement, ctx, 794 force_space, res); 795 force_space = !force_space; 796 } while (ret == -ENOSPC && force_space); 797 798 return ret; 799 } 800 EXPORT_SYMBOL(ttm_bo_mem_space); 801 802 /** 803 * ttm_bo_validate 804 * 805 * @bo: The buffer object. 806 * @placement: Proposed placement for the buffer object. 807 * @ctx: validation parameters. 808 * 809 * Changes placement and caching policy of the buffer object 810 * according proposed placement. 811 * Returns 812 * -EINVAL on invalid proposed placement. 813 * -ENOMEM on out-of-memory condition. 814 * -EBUSY if no_wait is true and buffer busy. 815 * -ERESTARTSYS if interrupted by a signal. 816 */ 817 int ttm_bo_validate(struct ttm_buffer_object *bo, 818 struct ttm_placement *placement, 819 struct ttm_operation_ctx *ctx) 820 { 821 struct ttm_resource *res; 822 struct ttm_place hop; 823 bool force_space; 824 int ret; 825 826 dma_resv_assert_held(bo->base.resv); 827 828 /* 829 * Remove the backing store if no placement is given. 830 */ 831 if (!placement->num_placement) 832 return ttm_bo_pipeline_gutting(bo); 833 834 force_space = false; 835 do { 836 /* Check whether we need to move buffer. */ 837 if (bo->resource && 838 ttm_resource_compatible(bo->resource, placement, 839 force_space)) 840 return 0; 841 842 /* Moving of pinned BOs is forbidden */ 843 if (bo->pin_count) 844 return -EINVAL; 845 846 /* 847 * Determine where to move the buffer. 848 * 849 * If driver determines move is going to need 850 * an extra step then it will return -EMULTIHOP 851 * and the buffer will be moved to the temporary 852 * stop and the driver will be called to make 853 * the second hop. 854 */ 855 ret = ttm_bo_alloc_resource(bo, placement, ctx, force_space, 856 &res); 857 force_space = !force_space; 858 if (ret == -ENOSPC) 859 continue; 860 if (ret) 861 return ret; 862 863 bounce: 864 ret = ttm_bo_handle_move_mem(bo, res, false, ctx, &hop); 865 if (ret == -EMULTIHOP) { 866 ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop); 867 /* try and move to final place now. */ 868 if (!ret) 869 goto bounce; 870 } 871 if (ret) { 872 ttm_resource_free(bo, &res); 873 return ret; 874 } 875 876 } while (ret && force_space); 877 878 /* For backward compatibility with userspace */ 879 if (ret == -ENOSPC) 880 return -ENOMEM; 881 882 /* 883 * We might need to add a TTM. 884 */ 885 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) { 886 ret = ttm_tt_create(bo, true); 887 if (ret) 888 return ret; 889 } 890 return 0; 891 } 892 EXPORT_SYMBOL(ttm_bo_validate); 893 894 /** 895 * ttm_bo_init_reserved 896 * 897 * @bdev: Pointer to a ttm_device struct. 898 * @bo: Pointer to a ttm_buffer_object to be initialized. 899 * @type: Requested type of buffer object. 900 * @placement: Initial placement for buffer object. 901 * @alignment: Data alignment in pages. 902 * @ctx: TTM operation context for memory allocation. 903 * @sg: Scatter-gather table. 904 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one. 905 * @destroy: Destroy function. Use NULL for kfree(). 906 * 907 * This function initializes a pre-allocated struct ttm_buffer_object. 908 * As this object may be part of a larger structure, this function, 909 * together with the @destroy function, enables driver-specific objects 910 * derived from a ttm_buffer_object. 911 * 912 * On successful return, the caller owns an object kref to @bo. The kref and 913 * list_kref are usually set to 1, but note that in some situations, other 914 * tasks may already be holding references to @bo as well. 915 * Furthermore, if resv == NULL, the buffer's reservation lock will be held, 916 * and it is the caller's responsibility to call ttm_bo_unreserve. 917 * 918 * If a failure occurs, the function will call the @destroy function. Thus, 919 * after a failure, dereferencing @bo is illegal and will likely cause memory 920 * corruption. 921 * 922 * Returns 923 * -ENOMEM: Out of memory. 924 * -EINVAL: Invalid placement flags. 925 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 926 */ 927 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo, 928 enum ttm_bo_type type, struct ttm_placement *placement, 929 uint32_t alignment, struct ttm_operation_ctx *ctx, 930 struct sg_table *sg, struct dma_resv *resv, 931 void (*destroy) (struct ttm_buffer_object *)) 932 { 933 int ret; 934 935 kref_init(&bo->kref); 936 bo->bdev = bdev; 937 bo->type = type; 938 bo->page_alignment = alignment; 939 bo->destroy = destroy; 940 bo->pin_count = 0; 941 bo->sg = sg; 942 bo->bulk_move = NULL; 943 if (resv) 944 bo->base.resv = resv; 945 else 946 bo->base.resv = &bo->base._resv; 947 atomic_inc(&ttm_glob.bo_count); 948 949 /* 950 * For ttm_bo_type_device buffers, allocate 951 * address space from the device. 952 */ 953 if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) { 954 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node, 955 PFN_UP(bo->base.size)); 956 if (ret) 957 goto err_put; 958 } 959 960 /* passed reservation objects should already be locked, 961 * since otherwise lockdep will be angered in radeon. 962 */ 963 if (!resv) 964 WARN_ON(!dma_resv_trylock(bo->base.resv)); 965 else 966 dma_resv_assert_held(resv); 967 968 ret = ttm_bo_validate(bo, placement, ctx); 969 if (unlikely(ret)) 970 goto err_unlock; 971 972 return 0; 973 974 err_unlock: 975 if (!resv) 976 dma_resv_unlock(bo->base.resv); 977 978 err_put: 979 ttm_bo_put(bo); 980 return ret; 981 } 982 EXPORT_SYMBOL(ttm_bo_init_reserved); 983 984 /** 985 * ttm_bo_init_validate 986 * 987 * @bdev: Pointer to a ttm_device struct. 988 * @bo: Pointer to a ttm_buffer_object to be initialized. 989 * @type: Requested type of buffer object. 990 * @placement: Initial placement for buffer object. 991 * @alignment: Data alignment in pages. 992 * @interruptible: If needing to sleep to wait for GPU resources, 993 * sleep interruptible. 994 * pinned in physical memory. If this behaviour is not desired, this member 995 * holds a pointer to a persistent shmem object. Typically, this would 996 * point to the shmem object backing a GEM object if TTM is used to back a 997 * GEM user interface. 998 * @sg: Scatter-gather table. 999 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one. 1000 * @destroy: Destroy function. Use NULL for kfree(). 1001 * 1002 * This function initializes a pre-allocated struct ttm_buffer_object. 1003 * As this object may be part of a larger structure, this function, 1004 * together with the @destroy function, 1005 * enables driver-specific objects derived from a ttm_buffer_object. 1006 * 1007 * On successful return, the caller owns an object kref to @bo. The kref and 1008 * list_kref are usually set to 1, but note that in some situations, other 1009 * tasks may already be holding references to @bo as well. 1010 * 1011 * If a failure occurs, the function will call the @destroy function, Thus, 1012 * after a failure, dereferencing @bo is illegal and will likely cause memory 1013 * corruption. 1014 * 1015 * Returns 1016 * -ENOMEM: Out of memory. 1017 * -EINVAL: Invalid placement flags. 1018 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 1019 */ 1020 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo, 1021 enum ttm_bo_type type, struct ttm_placement *placement, 1022 uint32_t alignment, bool interruptible, 1023 struct sg_table *sg, struct dma_resv *resv, 1024 void (*destroy) (struct ttm_buffer_object *)) 1025 { 1026 struct ttm_operation_ctx ctx = { interruptible, false }; 1027 int ret; 1028 1029 ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx, 1030 sg, resv, destroy); 1031 if (ret) 1032 return ret; 1033 1034 if (!resv) 1035 ttm_bo_unreserve(bo); 1036 1037 return 0; 1038 } 1039 EXPORT_SYMBOL(ttm_bo_init_validate); 1040 1041 /* 1042 * buffer object vm functions. 1043 */ 1044 1045 /** 1046 * ttm_bo_unmap_virtual 1047 * 1048 * @bo: tear down the virtual mappings for this BO 1049 */ 1050 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) 1051 { 1052 struct ttm_device *bdev = bo->bdev; 1053 1054 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping); 1055 ttm_mem_io_free(bdev, bo->resource); 1056 } 1057 EXPORT_SYMBOL(ttm_bo_unmap_virtual); 1058 1059 /** 1060 * ttm_bo_wait_ctx - wait for buffer idle. 1061 * 1062 * @bo: The buffer object. 1063 * @ctx: defines how to wait 1064 * 1065 * Waits for the buffer to be idle. Used timeout depends on the context. 1066 * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or 1067 * zero on success. 1068 */ 1069 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx) 1070 { 1071 long ret; 1072 1073 if (ctx->no_wait_gpu) { 1074 if (dma_resv_test_signaled(bo->base.resv, 1075 DMA_RESV_USAGE_BOOKKEEP)) 1076 return 0; 1077 else 1078 return -EBUSY; 1079 } 1080 1081 ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, 1082 ctx->interruptible, 15 * HZ); 1083 if (unlikely(ret < 0)) 1084 return ret; 1085 if (unlikely(ret == 0)) 1086 return -EBUSY; 1087 return 0; 1088 } 1089 EXPORT_SYMBOL(ttm_bo_wait_ctx); 1090 1091 /** 1092 * struct ttm_bo_swapout_walk - Parameters for the swapout walk 1093 */ 1094 struct ttm_bo_swapout_walk { 1095 /** @walk: The walk base parameters. */ 1096 struct ttm_lru_walk walk; 1097 /** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */ 1098 gfp_t gfp_flags; 1099 /** @hit_low: Whether we should attempt to swap BO's with low watermark threshold */ 1100 /** @evict_low: If we cannot swap a bo when @try_low is false (first pass) */ 1101 bool hit_low, evict_low; 1102 }; 1103 1104 static s64 1105 ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) 1106 { 1107 struct ttm_place place = {.mem_type = bo->resource->mem_type}; 1108 struct ttm_bo_swapout_walk *swapout_walk = 1109 container_of(walk, typeof(*swapout_walk), walk); 1110 struct ttm_operation_ctx *ctx = walk->arg.ctx; 1111 s64 ret; 1112 1113 /* 1114 * While the bo may already reside in SYSTEM placement, set 1115 * SYSTEM as new placement to cover also the move further below. 1116 * The driver may use the fact that we're moving from SYSTEM 1117 * as an indication that we're about to swap out. 1118 */ 1119 if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, &place)) { 1120 ret = -EBUSY; 1121 goto out; 1122 } 1123 1124 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) || 1125 bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL || 1126 bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED) { 1127 ret = -EBUSY; 1128 goto out; 1129 } 1130 1131 if (bo->deleted) { 1132 pgoff_t num_pages = bo->ttm->num_pages; 1133 1134 ret = ttm_bo_wait_ctx(bo, ctx); 1135 if (ret) 1136 goto out; 1137 1138 ttm_bo_cleanup_memtype_use(bo); 1139 ret = num_pages; 1140 goto out; 1141 } 1142 1143 /* 1144 * Move to system cached 1145 */ 1146 if (bo->resource->mem_type != TTM_PL_SYSTEM) { 1147 struct ttm_resource *evict_mem; 1148 struct ttm_place hop; 1149 1150 memset(&hop, 0, sizeof(hop)); 1151 place.mem_type = TTM_PL_SYSTEM; 1152 ret = ttm_resource_alloc(bo, &place, &evict_mem, NULL); 1153 if (ret) 1154 goto out; 1155 1156 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop); 1157 if (ret) { 1158 WARN(ret == -EMULTIHOP, 1159 "Unexpected multihop in swapout - likely driver bug.\n"); 1160 ttm_resource_free(bo, &evict_mem); 1161 goto out; 1162 } 1163 } 1164 1165 /* 1166 * Make sure BO is idle. 1167 */ 1168 ret = ttm_bo_wait_ctx(bo, ctx); 1169 if (ret) 1170 goto out; 1171 1172 ttm_bo_unmap_virtual(bo); 1173 if (bo->bdev->funcs->swap_notify) 1174 bo->bdev->funcs->swap_notify(bo); 1175 1176 if (ttm_tt_is_populated(bo->ttm)) { 1177 spin_lock(&bo->bdev->lru_lock); 1178 ttm_resource_del_bulk_move(bo->resource, bo); 1179 spin_unlock(&bo->bdev->lru_lock); 1180 1181 ret = ttm_tt_swapout(bo->bdev, bo->ttm, swapout_walk->gfp_flags); 1182 1183 spin_lock(&bo->bdev->lru_lock); 1184 if (ret) 1185 ttm_resource_add_bulk_move(bo->resource, bo); 1186 ttm_resource_move_to_lru_tail(bo->resource); 1187 spin_unlock(&bo->bdev->lru_lock); 1188 } 1189 1190 out: 1191 /* Consider -ENOMEM and -ENOSPC non-fatal. */ 1192 if (ret == -ENOMEM || ret == -ENOSPC) 1193 ret = -EBUSY; 1194 1195 return ret; 1196 } 1197 1198 const struct ttm_lru_walk_ops ttm_swap_ops = { 1199 .process_bo = ttm_bo_swapout_cb, 1200 }; 1201 1202 /** 1203 * ttm_bo_swapout() - Swap out buffer objects on the LRU list to shmem. 1204 * @bdev: The ttm device. 1205 * @ctx: The ttm_operation_ctx governing the swapout operation. 1206 * @man: The resource manager whose resources / buffer objects are 1207 * goint to be swapped out. 1208 * @gfp_flags: The gfp flags used for shmem page allocations. 1209 * @target: The desired number of bytes to swap out. 1210 * 1211 * Return: The number of bytes actually swapped out, or negative error code 1212 * on error. 1213 */ 1214 s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, 1215 struct ttm_resource_manager *man, gfp_t gfp_flags, 1216 s64 target) 1217 { 1218 struct ttm_bo_swapout_walk swapout_walk = { 1219 .walk = { 1220 .ops = &ttm_swap_ops, 1221 .arg = { 1222 .ctx = ctx, 1223 .trylock_only = true, 1224 }, 1225 }, 1226 .gfp_flags = gfp_flags, 1227 }; 1228 1229 return ttm_lru_walk_for_evict(&swapout_walk.walk, bdev, man, target); 1230 } 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_tt *tt = bo->ttm; 1261 bool swapped; 1262 int ret; 1263 1264 dma_resv_assert_held(bo->base.resv); 1265 1266 if (!tt) 1267 return 0; 1268 1269 swapped = ttm_tt_is_swapped(tt); 1270 ret = ttm_tt_populate(bo->bdev, tt, ctx); 1271 if (ret) 1272 return ret; 1273 1274 if (swapped && !ttm_tt_is_swapped(tt) && !bo->pin_count && 1275 bo->resource) { 1276 spin_lock(&bo->bdev->lru_lock); 1277 ttm_resource_add_bulk_move(bo->resource, bo); 1278 ttm_resource_move_to_lru_tail(bo->resource); 1279 spin_unlock(&bo->bdev->lru_lock); 1280 } 1281 1282 return 0; 1283 } 1284 EXPORT_SYMBOL(ttm_bo_populate); 1285 1286 int ttm_bo_setup_export(struct ttm_buffer_object *bo, 1287 struct ttm_operation_ctx *ctx) 1288 { 1289 int ret; 1290 1291 ret = ttm_bo_reserve(bo, false, false, NULL); 1292 if (ret != 0) 1293 return ret; 1294 1295 ret = ttm_bo_populate(bo, ctx); 1296 ttm_bo_unreserve(bo); 1297 return ret; 1298 } 1299 EXPORT_SYMBOL(ttm_bo_setup_export); 1300