1 /* 2 * Copyright 2009 Jerome Glisse. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19 * USE OR OTHER DEALINGS IN THE SOFTWARE. 20 * 21 * The above copyright notice and this permission notice (including the 22 * next paragraph) shall be included in all copies or substantial portions 23 * of the Software. 24 * 25 */ 26 /* 27 * Authors: 28 * Jerome Glisse <glisse@freedesktop.org> 29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> 30 * Dave Airlie 31 */ 32 #include <linux/list.h> 33 #include <linux/slab.h> 34 #include <linux/dma-buf.h> 35 36 #include <drm/drm_drv.h> 37 #include <drm/amdgpu_drm.h> 38 #include <drm/drm_cache.h> 39 #include "amdgpu.h" 40 #include "amdgpu_trace.h" 41 #include "amdgpu_amdkfd.h" 42 #include "amdgpu_vram_mgr.h" 43 44 /** 45 * DOC: amdgpu_object 46 * 47 * This defines the interfaces to operate on an &amdgpu_bo buffer object which 48 * represents memory used by driver (VRAM, system memory, etc.). The driver 49 * provides DRM/GEM APIs to userspace. DRM/GEM APIs then use these interfaces 50 * to create/destroy/set buffer object which are then managed by the kernel TTM 51 * memory manager. 52 * The interfaces are also used internally by kernel clients, including gfx, 53 * uvd, etc. for kernel managed allocations used by the GPU. 54 * 55 */ 56 57 static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo) 58 { 59 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo); 60 61 amdgpu_bo_kunmap(bo); 62 63 if (bo->tbo.base.import_attach) 64 drm_prime_gem_destroy(&bo->tbo.base, bo->tbo.sg); 65 drm_gem_object_release(&bo->tbo.base); 66 amdgpu_bo_unref(&bo->parent); 67 kvfree(bo); 68 } 69 70 static void amdgpu_bo_user_destroy(struct ttm_buffer_object *tbo) 71 { 72 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo); 73 struct amdgpu_bo_user *ubo; 74 75 ubo = to_amdgpu_bo_user(bo); 76 kfree(ubo->metadata); 77 amdgpu_bo_destroy(tbo); 78 } 79 80 /** 81 * amdgpu_bo_is_amdgpu_bo - check if the buffer object is an &amdgpu_bo 82 * @bo: buffer object to be checked 83 * 84 * Uses destroy function associated with the object to determine if this is 85 * an &amdgpu_bo. 86 * 87 * Returns: 88 * true if the object belongs to &amdgpu_bo, false if not. 89 */ 90 bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo) 91 { 92 if (bo->destroy == &amdgpu_bo_destroy || 93 bo->destroy == &amdgpu_bo_user_destroy) 94 return true; 95 96 return false; 97 } 98 99 /** 100 * amdgpu_bo_placement_from_domain - set buffer's placement 101 * @abo: &amdgpu_bo buffer object whose placement is to be set 102 * @domain: requested domain 103 * 104 * Sets buffer's placement according to requested domain and the buffer's 105 * flags. 106 */ 107 void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain) 108 { 109 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); 110 struct ttm_placement *placement = &abo->placement; 111 struct ttm_place *places = abo->placements; 112 u64 flags = abo->flags; 113 u32 c = 0; 114 115 if (domain & AMDGPU_GEM_DOMAIN_VRAM) { 116 unsigned int visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT; 117 int8_t mem_id = KFD_XCP_MEM_ID(adev, abo->xcp_id); 118 119 if (adev->gmc.mem_partitions && mem_id >= 0) { 120 places[c].fpfn = adev->gmc.mem_partitions[mem_id].range.fpfn; 121 /* 122 * memory partition range lpfn is inclusive start + size - 1 123 * TTM place lpfn is exclusive start + size 124 */ 125 places[c].lpfn = adev->gmc.mem_partitions[mem_id].range.lpfn + 1; 126 } else { 127 places[c].fpfn = 0; 128 places[c].lpfn = 0; 129 } 130 places[c].mem_type = TTM_PL_VRAM; 131 places[c].flags = 0; 132 133 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) 134 places[c].lpfn = min_not_zero(places[c].lpfn, visible_pfn); 135 else 136 places[c].flags |= TTM_PL_FLAG_TOPDOWN; 137 138 if (abo->tbo.type == ttm_bo_type_kernel && 139 flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) 140 places[c].flags |= TTM_PL_FLAG_CONTIGUOUS; 141 142 c++; 143 } 144 145 if (domain & AMDGPU_GEM_DOMAIN_DOORBELL) { 146 places[c].fpfn = 0; 147 places[c].lpfn = 0; 148 places[c].mem_type = AMDGPU_PL_DOORBELL; 149 places[c].flags = 0; 150 c++; 151 } 152 153 if (domain & AMDGPU_GEM_DOMAIN_GTT) { 154 places[c].fpfn = 0; 155 places[c].lpfn = 0; 156 places[c].mem_type = 157 abo->flags & AMDGPU_GEM_CREATE_PREEMPTIBLE ? 158 AMDGPU_PL_PREEMPT : TTM_PL_TT; 159 places[c].flags = 0; 160 /* 161 * When GTT is just an alternative to VRAM make sure that we 162 * only use it as fallback and still try to fill up VRAM first. 163 */ 164 if (domain & abo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) 165 places[c].flags |= TTM_PL_FLAG_FALLBACK; 166 c++; 167 } 168 169 if (domain & AMDGPU_GEM_DOMAIN_CPU) { 170 places[c].fpfn = 0; 171 places[c].lpfn = 0; 172 places[c].mem_type = TTM_PL_SYSTEM; 173 places[c].flags = 0; 174 c++; 175 } 176 177 if (domain & AMDGPU_GEM_DOMAIN_GDS) { 178 places[c].fpfn = 0; 179 places[c].lpfn = 0; 180 places[c].mem_type = AMDGPU_PL_GDS; 181 places[c].flags = 0; 182 c++; 183 } 184 185 if (domain & AMDGPU_GEM_DOMAIN_GWS) { 186 places[c].fpfn = 0; 187 places[c].lpfn = 0; 188 places[c].mem_type = AMDGPU_PL_GWS; 189 places[c].flags = 0; 190 c++; 191 } 192 193 if (domain & AMDGPU_GEM_DOMAIN_OA) { 194 places[c].fpfn = 0; 195 places[c].lpfn = 0; 196 places[c].mem_type = AMDGPU_PL_OA; 197 places[c].flags = 0; 198 c++; 199 } 200 201 if (!c) { 202 places[c].fpfn = 0; 203 places[c].lpfn = 0; 204 places[c].mem_type = TTM_PL_SYSTEM; 205 places[c].flags = 0; 206 c++; 207 } 208 209 BUG_ON(c > AMDGPU_BO_MAX_PLACEMENTS); 210 211 placement->num_placement = c; 212 placement->placement = places; 213 } 214 215 /** 216 * amdgpu_bo_create_reserved - create reserved BO for kernel use 217 * 218 * @adev: amdgpu device object 219 * @size: size for the new BO 220 * @align: alignment for the new BO 221 * @domain: where to place it 222 * @bo_ptr: used to initialize BOs in structures 223 * @gpu_addr: GPU addr of the pinned BO 224 * @cpu_addr: optional CPU address mapping 225 * 226 * Allocates and pins a BO for kernel internal use, and returns it still 227 * reserved. 228 * 229 * Note: For bo_ptr new BO is only created if bo_ptr points to NULL. 230 * 231 * Returns: 232 * 0 on success, negative error code otherwise. 233 */ 234 int amdgpu_bo_create_reserved(struct amdgpu_device *adev, 235 unsigned long size, int align, 236 u32 domain, struct amdgpu_bo **bo_ptr, 237 u64 *gpu_addr, void **cpu_addr) 238 { 239 struct amdgpu_bo_param bp; 240 bool free = false; 241 int r; 242 243 if (!size) { 244 amdgpu_bo_unref(bo_ptr); 245 return 0; 246 } 247 248 memset(&bp, 0, sizeof(bp)); 249 bp.size = size; 250 bp.byte_align = align; 251 bp.domain = domain; 252 bp.flags = cpu_addr ? AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED 253 : AMDGPU_GEM_CREATE_NO_CPU_ACCESS; 254 bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; 255 bp.type = ttm_bo_type_kernel; 256 bp.resv = NULL; 257 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 258 259 if (!*bo_ptr) { 260 r = amdgpu_bo_create(adev, &bp, bo_ptr); 261 if (r) { 262 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", 263 r); 264 return r; 265 } 266 free = true; 267 } 268 269 r = amdgpu_bo_reserve(*bo_ptr, false); 270 if (r) { 271 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r); 272 goto error_free; 273 } 274 275 r = amdgpu_bo_pin(*bo_ptr, domain); 276 if (r) { 277 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r); 278 goto error_unreserve; 279 } 280 281 r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo); 282 if (r) { 283 dev_err(adev->dev, "%p bind failed\n", *bo_ptr); 284 goto error_unpin; 285 } 286 287 if (gpu_addr) 288 *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr); 289 290 if (cpu_addr) { 291 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr); 292 if (r) { 293 dev_err(adev->dev, "(%d) kernel bo map failed\n", r); 294 goto error_unpin; 295 } 296 } 297 298 return 0; 299 300 error_unpin: 301 amdgpu_bo_unpin(*bo_ptr); 302 error_unreserve: 303 amdgpu_bo_unreserve(*bo_ptr); 304 305 error_free: 306 if (free) 307 amdgpu_bo_unref(bo_ptr); 308 309 return r; 310 } 311 312 /** 313 * amdgpu_bo_create_kernel - create BO for kernel use 314 * 315 * @adev: amdgpu device object 316 * @size: size for the new BO 317 * @align: alignment for the new BO 318 * @domain: where to place it 319 * @bo_ptr: used to initialize BOs in structures 320 * @gpu_addr: GPU addr of the pinned BO 321 * @cpu_addr: optional CPU address mapping 322 * 323 * Allocates and pins a BO for kernel internal use. 324 * 325 * Note: For bo_ptr new BO is only created if bo_ptr points to NULL. 326 * 327 * Returns: 328 * 0 on success, negative error code otherwise. 329 */ 330 int amdgpu_bo_create_kernel(struct amdgpu_device *adev, 331 unsigned long size, int align, 332 u32 domain, struct amdgpu_bo **bo_ptr, 333 u64 *gpu_addr, void **cpu_addr) 334 { 335 int r; 336 337 r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr, 338 gpu_addr, cpu_addr); 339 340 if (r) 341 return r; 342 343 if (*bo_ptr) 344 amdgpu_bo_unreserve(*bo_ptr); 345 346 return 0; 347 } 348 349 /** 350 * amdgpu_bo_create_kernel_at - create BO for kernel use at specific location 351 * 352 * @adev: amdgpu device object 353 * @offset: offset of the BO 354 * @size: size of the BO 355 * @bo_ptr: used to initialize BOs in structures 356 * @cpu_addr: optional CPU address mapping 357 * 358 * Creates a kernel BO at a specific offset in VRAM. 359 * 360 * Returns: 361 * 0 on success, negative error code otherwise. 362 */ 363 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev, 364 uint64_t offset, uint64_t size, 365 struct amdgpu_bo **bo_ptr, void **cpu_addr) 366 { 367 struct ttm_operation_ctx ctx = { false, false }; 368 unsigned int i; 369 int r; 370 371 offset &= PAGE_MASK; 372 size = ALIGN(size, PAGE_SIZE); 373 374 r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE, 375 AMDGPU_GEM_DOMAIN_VRAM, bo_ptr, NULL, 376 cpu_addr); 377 if (r) 378 return r; 379 380 if ((*bo_ptr) == NULL) 381 return 0; 382 383 /* 384 * Remove the original mem node and create a new one at the request 385 * position. 386 */ 387 if (cpu_addr) 388 amdgpu_bo_kunmap(*bo_ptr); 389 390 ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource); 391 392 for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) { 393 (*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT; 394 (*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT; 395 } 396 r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement, 397 &(*bo_ptr)->tbo.resource, &ctx); 398 if (r) 399 goto error; 400 401 if (cpu_addr) { 402 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr); 403 if (r) 404 goto error; 405 } 406 407 amdgpu_bo_unreserve(*bo_ptr); 408 return 0; 409 410 error: 411 amdgpu_bo_unreserve(*bo_ptr); 412 amdgpu_bo_unref(bo_ptr); 413 return r; 414 } 415 416 /** 417 * amdgpu_bo_free_kernel - free BO for kernel use 418 * 419 * @bo: amdgpu BO to free 420 * @gpu_addr: pointer to where the BO's GPU memory space address was stored 421 * @cpu_addr: pointer to where the BO's CPU memory space address was stored 422 * 423 * unmaps and unpin a BO for kernel internal use. 424 */ 425 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr, 426 void **cpu_addr) 427 { 428 if (*bo == NULL) 429 return; 430 431 WARN_ON(amdgpu_ttm_adev((*bo)->tbo.bdev)->in_suspend); 432 433 if (likely(amdgpu_bo_reserve(*bo, true) == 0)) { 434 if (cpu_addr) 435 amdgpu_bo_kunmap(*bo); 436 437 amdgpu_bo_unpin(*bo); 438 amdgpu_bo_unreserve(*bo); 439 } 440 amdgpu_bo_unref(bo); 441 442 if (gpu_addr) 443 *gpu_addr = 0; 444 445 if (cpu_addr) 446 *cpu_addr = NULL; 447 } 448 449 /* Validate bo size is bit bigger than the request domain */ 450 static bool amdgpu_bo_validate_size(struct amdgpu_device *adev, 451 unsigned long size, u32 domain) 452 { 453 struct ttm_resource_manager *man = NULL; 454 455 /* 456 * If GTT is part of requested domains the check must succeed to 457 * allow fall back to GTT. 458 */ 459 if (domain & AMDGPU_GEM_DOMAIN_GTT) 460 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT); 461 else if (domain & AMDGPU_GEM_DOMAIN_VRAM) 462 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM); 463 else 464 return true; 465 466 if (!man) { 467 if (domain & AMDGPU_GEM_DOMAIN_GTT) 468 WARN_ON_ONCE("GTT domain requested but GTT mem manager uninitialized"); 469 return false; 470 } 471 472 /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU, _DOMAIN_DOORBELL */ 473 if (size < man->size) 474 return true; 475 476 DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size, man->size); 477 return false; 478 } 479 480 bool amdgpu_bo_support_uswc(u64 bo_flags) 481 { 482 483 #ifdef CONFIG_X86_32 484 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit 485 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627 486 */ 487 return false; 488 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT) 489 /* Don't try to enable write-combining when it can't work, or things 490 * may be slow 491 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758 492 */ 493 494 #ifndef CONFIG_COMPILE_TEST 495 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \ 496 thanks to write-combining 497 #endif 498 499 if (bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) 500 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for " 501 "better performance thanks to write-combining\n"); 502 return false; 503 #else 504 /* For architectures that don't support WC memory, 505 * mask out the WC flag from the BO 506 */ 507 if (!drm_arch_can_wc_memory()) 508 return false; 509 510 return true; 511 #endif 512 } 513 514 /** 515 * amdgpu_bo_create - create an &amdgpu_bo buffer object 516 * @adev: amdgpu device object 517 * @bp: parameters to be used for the buffer object 518 * @bo_ptr: pointer to the buffer object pointer 519 * 520 * Creates an &amdgpu_bo buffer object. 521 * 522 * Returns: 523 * 0 for success or a negative error code on failure. 524 */ 525 int amdgpu_bo_create(struct amdgpu_device *adev, 526 struct amdgpu_bo_param *bp, 527 struct amdgpu_bo **bo_ptr) 528 { 529 struct ttm_operation_ctx ctx = { 530 .interruptible = (bp->type != ttm_bo_type_kernel), 531 .no_wait_gpu = bp->no_wait_gpu, 532 /* We opt to avoid OOM on system pages allocations */ 533 .gfp_retry_mayfail = true, 534 .allow_res_evict = bp->type != ttm_bo_type_kernel, 535 .resv = bp->resv 536 }; 537 struct amdgpu_bo *bo; 538 unsigned long page_align, size = bp->size; 539 int r; 540 541 /* Note that GDS/GWS/OA allocates 1 page per byte/resource. */ 542 if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) { 543 /* GWS and OA don't need any alignment. */ 544 page_align = bp->byte_align; 545 size <<= PAGE_SHIFT; 546 547 } else if (bp->domain & AMDGPU_GEM_DOMAIN_GDS) { 548 /* Both size and alignment must be a multiple of 4. */ 549 page_align = ALIGN(bp->byte_align, 4); 550 size = ALIGN(size, 4) << PAGE_SHIFT; 551 } else { 552 /* Memory should be aligned at least to a page size. */ 553 page_align = ALIGN(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT; 554 size = ALIGN(size, PAGE_SIZE); 555 } 556 557 if (!amdgpu_bo_validate_size(adev, size, bp->domain)) 558 return -ENOMEM; 559 560 BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo)); 561 562 *bo_ptr = NULL; 563 bo = kvzalloc(bp->bo_ptr_size, GFP_KERNEL); 564 if (bo == NULL) 565 return -ENOMEM; 566 drm_gem_private_object_init(adev_to_drm(adev), &bo->tbo.base, size); 567 bo->vm_bo = NULL; 568 bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain : 569 bp->domain; 570 bo->allowed_domains = bo->preferred_domains; 571 if (bp->type != ttm_bo_type_kernel && 572 !(bp->flags & AMDGPU_GEM_CREATE_DISCARDABLE) && 573 bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM) 574 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT; 575 576 bo->flags = bp->flags; 577 578 if (adev->gmc.mem_partitions) 579 /* For GPUs with spatial partitioning, bo->xcp_id=-1 means any partition */ 580 bo->xcp_id = bp->xcp_id_plus1 - 1; 581 else 582 /* For GPUs without spatial partitioning */ 583 bo->xcp_id = 0; 584 585 if (!amdgpu_bo_support_uswc(bo->flags)) 586 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC; 587 588 bo->tbo.bdev = &adev->mman.bdev; 589 if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA | 590 AMDGPU_GEM_DOMAIN_GDS)) 591 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU); 592 else 593 amdgpu_bo_placement_from_domain(bo, bp->domain); 594 if (bp->type == ttm_bo_type_kernel) 595 bo->tbo.priority = 2; 596 else if (!(bp->flags & AMDGPU_GEM_CREATE_DISCARDABLE)) 597 bo->tbo.priority = 1; 598 599 if (!bp->destroy) 600 bp->destroy = &amdgpu_bo_destroy; 601 602 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, bp->type, 603 &bo->placement, page_align, &ctx, NULL, 604 bp->resv, bp->destroy); 605 if (unlikely(r != 0)) 606 return r; 607 608 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) && 609 amdgpu_res_cpu_visible(adev, bo->tbo.resource)) 610 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 611 ctx.bytes_moved); 612 else 613 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0); 614 615 if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED && 616 bo->tbo.resource->mem_type == TTM_PL_VRAM) { 617 struct dma_fence *fence; 618 619 r = amdgpu_ttm_clear_buffer(bo, bo->tbo.base.resv, &fence); 620 if (unlikely(r)) 621 goto fail_unreserve; 622 623 dma_resv_add_fence(bo->tbo.base.resv, fence, 624 DMA_RESV_USAGE_KERNEL); 625 dma_fence_put(fence); 626 } 627 if (!bp->resv) 628 amdgpu_bo_unreserve(bo); 629 *bo_ptr = bo; 630 631 trace_amdgpu_bo_create(bo); 632 633 /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */ 634 if (bp->type == ttm_bo_type_device) 635 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 636 637 return 0; 638 639 fail_unreserve: 640 if (!bp->resv) 641 dma_resv_unlock(bo->tbo.base.resv); 642 amdgpu_bo_unref(&bo); 643 return r; 644 } 645 646 /** 647 * amdgpu_bo_create_user - create an &amdgpu_bo_user buffer object 648 * @adev: amdgpu device object 649 * @bp: parameters to be used for the buffer object 650 * @ubo_ptr: pointer to the buffer object pointer 651 * 652 * Create a BO to be used by user application; 653 * 654 * Returns: 655 * 0 for success or a negative error code on failure. 656 */ 657 658 int amdgpu_bo_create_user(struct amdgpu_device *adev, 659 struct amdgpu_bo_param *bp, 660 struct amdgpu_bo_user **ubo_ptr) 661 { 662 struct amdgpu_bo *bo_ptr; 663 int r; 664 665 bp->bo_ptr_size = sizeof(struct amdgpu_bo_user); 666 bp->destroy = &amdgpu_bo_user_destroy; 667 r = amdgpu_bo_create(adev, bp, &bo_ptr); 668 if (r) 669 return r; 670 671 *ubo_ptr = to_amdgpu_bo_user(bo_ptr); 672 return r; 673 } 674 675 /** 676 * amdgpu_bo_create_vm - create an &amdgpu_bo_vm buffer object 677 * @adev: amdgpu device object 678 * @bp: parameters to be used for the buffer object 679 * @vmbo_ptr: pointer to the buffer object pointer 680 * 681 * Create a BO to be for GPUVM. 682 * 683 * Returns: 684 * 0 for success or a negative error code on failure. 685 */ 686 687 int amdgpu_bo_create_vm(struct amdgpu_device *adev, 688 struct amdgpu_bo_param *bp, 689 struct amdgpu_bo_vm **vmbo_ptr) 690 { 691 struct amdgpu_bo *bo_ptr; 692 int r; 693 694 /* bo_ptr_size will be determined by the caller and it depends on 695 * num of amdgpu_vm_pt entries. 696 */ 697 BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo_vm)); 698 r = amdgpu_bo_create(adev, bp, &bo_ptr); 699 if (r) 700 return r; 701 702 *vmbo_ptr = to_amdgpu_bo_vm(bo_ptr); 703 return r; 704 } 705 706 /** 707 * amdgpu_bo_kmap - map an &amdgpu_bo buffer object 708 * @bo: &amdgpu_bo buffer object to be mapped 709 * @ptr: kernel virtual address to be returned 710 * 711 * Calls ttm_bo_kmap() to set up the kernel virtual mapping; calls 712 * amdgpu_bo_kptr() to get the kernel virtual address. 713 * 714 * Returns: 715 * 0 for success or a negative error code on failure. 716 */ 717 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr) 718 { 719 void *kptr; 720 long r; 721 722 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) 723 return -EPERM; 724 725 r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_KERNEL, 726 false, MAX_SCHEDULE_TIMEOUT); 727 if (r < 0) 728 return r; 729 730 kptr = amdgpu_bo_kptr(bo); 731 if (kptr) { 732 if (ptr) 733 *ptr = kptr; 734 return 0; 735 } 736 737 r = ttm_bo_kmap(&bo->tbo, 0, PFN_UP(bo->tbo.base.size), &bo->kmap); 738 if (r) 739 return r; 740 741 if (ptr) 742 *ptr = amdgpu_bo_kptr(bo); 743 744 return 0; 745 } 746 747 /** 748 * amdgpu_bo_kptr - returns a kernel virtual address of the buffer object 749 * @bo: &amdgpu_bo buffer object 750 * 751 * Calls ttm_kmap_obj_virtual() to get the kernel virtual address 752 * 753 * Returns: 754 * the virtual address of a buffer object area. 755 */ 756 void *amdgpu_bo_kptr(struct amdgpu_bo *bo) 757 { 758 bool is_iomem; 759 760 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); 761 } 762 763 /** 764 * amdgpu_bo_kunmap - unmap an &amdgpu_bo buffer object 765 * @bo: &amdgpu_bo buffer object to be unmapped 766 * 767 * Unmaps a kernel map set up by amdgpu_bo_kmap(). 768 */ 769 void amdgpu_bo_kunmap(struct amdgpu_bo *bo) 770 { 771 if (bo->kmap.bo) 772 ttm_bo_kunmap(&bo->kmap); 773 } 774 775 /** 776 * amdgpu_bo_ref - reference an &amdgpu_bo buffer object 777 * @bo: &amdgpu_bo buffer object 778 * 779 * References the contained &ttm_buffer_object. 780 * 781 * Returns: 782 * a refcounted pointer to the &amdgpu_bo buffer object. 783 */ 784 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo) 785 { 786 if (bo == NULL) 787 return NULL; 788 789 ttm_bo_get(&bo->tbo); 790 return bo; 791 } 792 793 /** 794 * amdgpu_bo_unref - unreference an &amdgpu_bo buffer object 795 * @bo: &amdgpu_bo buffer object 796 * 797 * Unreferences the contained &ttm_buffer_object and clear the pointer 798 */ 799 void amdgpu_bo_unref(struct amdgpu_bo **bo) 800 { 801 struct ttm_buffer_object *tbo; 802 803 if ((*bo) == NULL) 804 return; 805 806 tbo = &((*bo)->tbo); 807 ttm_bo_put(tbo); 808 *bo = NULL; 809 } 810 811 /** 812 * amdgpu_bo_pin_restricted - pin an &amdgpu_bo buffer object 813 * @bo: &amdgpu_bo buffer object to be pinned 814 * @domain: domain to be pinned to 815 * @min_offset: the start of requested address range 816 * @max_offset: the end of requested address range 817 * 818 * Pins the buffer object according to requested domain and address range. If 819 * the memory is unbound gart memory, binds the pages into gart table. Adjusts 820 * pin_count and pin_size accordingly. 821 * 822 * Pinning means to lock pages in memory along with keeping them at a fixed 823 * offset. It is required when a buffer can not be moved, for example, when 824 * a display buffer is being scanned out. 825 * 826 * Compared with amdgpu_bo_pin(), this function gives more flexibility on 827 * where to pin a buffer if there are specific restrictions on where a buffer 828 * must be located. 829 * 830 * Returns: 831 * 0 for success or a negative error code on failure. 832 */ 833 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain, 834 u64 min_offset, u64 max_offset) 835 { 836 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 837 struct ttm_operation_ctx ctx = { false, false }; 838 int r, i; 839 840 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) 841 return -EPERM; 842 843 if (WARN_ON_ONCE(min_offset > max_offset)) 844 return -EINVAL; 845 846 /* Check domain to be pinned to against preferred domains */ 847 if (bo->preferred_domains & domain) 848 domain = bo->preferred_domains & domain; 849 850 /* A shared bo cannot be migrated to VRAM */ 851 if (bo->tbo.base.import_attach) { 852 if (domain & AMDGPU_GEM_DOMAIN_GTT) 853 domain = AMDGPU_GEM_DOMAIN_GTT; 854 else 855 return -EINVAL; 856 } 857 858 if (bo->tbo.pin_count) { 859 uint32_t mem_type = bo->tbo.resource->mem_type; 860 uint32_t mem_flags = bo->tbo.resource->placement; 861 862 if (!(domain & amdgpu_mem_type_to_domain(mem_type))) 863 return -EINVAL; 864 865 if ((mem_type == TTM_PL_VRAM) && 866 (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) && 867 !(mem_flags & TTM_PL_FLAG_CONTIGUOUS)) 868 return -EINVAL; 869 870 ttm_bo_pin(&bo->tbo); 871 872 if (max_offset != 0) { 873 u64 domain_start = amdgpu_ttm_domain_start(adev, 874 mem_type); 875 WARN_ON_ONCE(max_offset < 876 (amdgpu_bo_gpu_offset(bo) - domain_start)); 877 } 878 879 return 0; 880 } 881 882 /* This assumes only APU display buffers are pinned with (VRAM|GTT). 883 * See function amdgpu_display_supported_domains() 884 */ 885 domain = amdgpu_bo_get_preferred_domain(adev, domain); 886 887 if (bo->tbo.base.import_attach) 888 dma_buf_pin(bo->tbo.base.import_attach); 889 890 /* force to pin into visible video ram */ 891 if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) 892 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 893 amdgpu_bo_placement_from_domain(bo, domain); 894 for (i = 0; i < bo->placement.num_placement; i++) { 895 unsigned int fpfn, lpfn; 896 897 fpfn = min_offset >> PAGE_SHIFT; 898 lpfn = max_offset >> PAGE_SHIFT; 899 900 if (fpfn > bo->placements[i].fpfn) 901 bo->placements[i].fpfn = fpfn; 902 if (!bo->placements[i].lpfn || 903 (lpfn && lpfn < bo->placements[i].lpfn)) 904 bo->placements[i].lpfn = lpfn; 905 906 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS && 907 bo->placements[i].mem_type == TTM_PL_VRAM) 908 bo->placements[i].flags |= TTM_PL_FLAG_CONTIGUOUS; 909 } 910 911 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 912 if (unlikely(r)) { 913 dev_err(adev->dev, "%p pin failed\n", bo); 914 goto error; 915 } 916 917 ttm_bo_pin(&bo->tbo); 918 919 if (bo->tbo.resource->mem_type == TTM_PL_VRAM) { 920 atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size); 921 atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo), 922 &adev->visible_pin_size); 923 } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { 924 atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size); 925 } 926 927 error: 928 return r; 929 } 930 931 /** 932 * amdgpu_bo_pin - pin an &amdgpu_bo buffer object 933 * @bo: &amdgpu_bo buffer object to be pinned 934 * @domain: domain to be pinned to 935 * 936 * A simple wrapper to amdgpu_bo_pin_restricted(). 937 * Provides a simpler API for buffers that do not have any strict restrictions 938 * on where a buffer must be located. 939 * 940 * Returns: 941 * 0 for success or a negative error code on failure. 942 */ 943 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain) 944 { 945 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; 946 return amdgpu_bo_pin_restricted(bo, domain, 0, 0); 947 } 948 949 /** 950 * amdgpu_bo_unpin - unpin an &amdgpu_bo buffer object 951 * @bo: &amdgpu_bo buffer object to be unpinned 952 * 953 * Decreases the pin_count, and clears the flags if pin_count reaches 0. 954 * Changes placement and pin size accordingly. 955 * 956 * Returns: 957 * 0 for success or a negative error code on failure. 958 */ 959 void amdgpu_bo_unpin(struct amdgpu_bo *bo) 960 { 961 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 962 963 ttm_bo_unpin(&bo->tbo); 964 if (bo->tbo.pin_count) 965 return; 966 967 if (bo->tbo.base.import_attach) 968 dma_buf_unpin(bo->tbo.base.import_attach); 969 970 if (bo->tbo.resource->mem_type == TTM_PL_VRAM) { 971 atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); 972 atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), 973 &adev->visible_pin_size); 974 } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { 975 atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); 976 } 977 978 } 979 980 static const char * const amdgpu_vram_names[] = { 981 "UNKNOWN", 982 "GDDR1", 983 "DDR2", 984 "GDDR3", 985 "GDDR4", 986 "GDDR5", 987 "HBM", 988 "DDR3", 989 "DDR4", 990 "GDDR6", 991 "DDR5", 992 "LPDDR4", 993 "LPDDR5" 994 }; 995 996 /** 997 * amdgpu_bo_init - initialize memory manager 998 * @adev: amdgpu device object 999 * 1000 * Calls amdgpu_ttm_init() to initialize amdgpu memory manager. 1001 * 1002 * Returns: 1003 * 0 for success or a negative error code on failure. 1004 */ 1005 int amdgpu_bo_init(struct amdgpu_device *adev) 1006 { 1007 /* On A+A platform, VRAM can be mapped as WB */ 1008 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) { 1009 /* reserve PAT memory space to WC for VRAM */ 1010 int r = arch_io_reserve_memtype_wc(adev->gmc.aper_base, 1011 adev->gmc.aper_size); 1012 1013 if (r) { 1014 DRM_ERROR("Unable to set WC memtype for the aperture base\n"); 1015 return r; 1016 } 1017 1018 /* Add an MTRR for the VRAM */ 1019 adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base, 1020 adev->gmc.aper_size); 1021 } 1022 1023 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n", 1024 adev->gmc.mc_vram_size >> 20, 1025 (unsigned long long)adev->gmc.aper_size >> 20); 1026 DRM_INFO("RAM width %dbits %s\n", 1027 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]); 1028 return amdgpu_ttm_init(adev); 1029 } 1030 1031 /** 1032 * amdgpu_bo_fini - tear down memory manager 1033 * @adev: amdgpu device object 1034 * 1035 * Reverses amdgpu_bo_init() to tear down memory manager. 1036 */ 1037 void amdgpu_bo_fini(struct amdgpu_device *adev) 1038 { 1039 int idx; 1040 1041 amdgpu_ttm_fini(adev); 1042 1043 if (drm_dev_enter(adev_to_drm(adev), &idx)) { 1044 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) { 1045 arch_phys_wc_del(adev->gmc.vram_mtrr); 1046 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size); 1047 } 1048 drm_dev_exit(idx); 1049 } 1050 } 1051 1052 /** 1053 * amdgpu_bo_set_tiling_flags - set tiling flags 1054 * @bo: &amdgpu_bo buffer object 1055 * @tiling_flags: new flags 1056 * 1057 * Sets buffer object's tiling flags with the new one. Used by GEM ioctl or 1058 * kernel driver to set the tiling flags on a buffer. 1059 * 1060 * Returns: 1061 * 0 for success or a negative error code on failure. 1062 */ 1063 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags) 1064 { 1065 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 1066 struct amdgpu_bo_user *ubo; 1067 1068 BUG_ON(bo->tbo.type == ttm_bo_type_kernel); 1069 if (adev->family <= AMDGPU_FAMILY_CZ && 1070 AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6) 1071 return -EINVAL; 1072 1073 ubo = to_amdgpu_bo_user(bo); 1074 ubo->tiling_flags = tiling_flags; 1075 return 0; 1076 } 1077 1078 /** 1079 * amdgpu_bo_get_tiling_flags - get tiling flags 1080 * @bo: &amdgpu_bo buffer object 1081 * @tiling_flags: returned flags 1082 * 1083 * Gets buffer object's tiling flags. Used by GEM ioctl or kernel driver to 1084 * set the tiling flags on a buffer. 1085 */ 1086 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags) 1087 { 1088 struct amdgpu_bo_user *ubo; 1089 1090 BUG_ON(bo->tbo.type == ttm_bo_type_kernel); 1091 dma_resv_assert_held(bo->tbo.base.resv); 1092 ubo = to_amdgpu_bo_user(bo); 1093 1094 if (tiling_flags) 1095 *tiling_flags = ubo->tiling_flags; 1096 } 1097 1098 /** 1099 * amdgpu_bo_set_metadata - set metadata 1100 * @bo: &amdgpu_bo buffer object 1101 * @metadata: new metadata 1102 * @metadata_size: size of the new metadata 1103 * @flags: flags of the new metadata 1104 * 1105 * Sets buffer object's metadata, its size and flags. 1106 * Used via GEM ioctl. 1107 * 1108 * Returns: 1109 * 0 for success or a negative error code on failure. 1110 */ 1111 int amdgpu_bo_set_metadata(struct amdgpu_bo *bo, void *metadata, 1112 u32 metadata_size, uint64_t flags) 1113 { 1114 struct amdgpu_bo_user *ubo; 1115 void *buffer; 1116 1117 BUG_ON(bo->tbo.type == ttm_bo_type_kernel); 1118 ubo = to_amdgpu_bo_user(bo); 1119 if (!metadata_size) { 1120 if (ubo->metadata_size) { 1121 kfree(ubo->metadata); 1122 ubo->metadata = NULL; 1123 ubo->metadata_size = 0; 1124 } 1125 return 0; 1126 } 1127 1128 if (metadata == NULL) 1129 return -EINVAL; 1130 1131 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL); 1132 if (buffer == NULL) 1133 return -ENOMEM; 1134 1135 kfree(ubo->metadata); 1136 ubo->metadata_flags = flags; 1137 ubo->metadata = buffer; 1138 ubo->metadata_size = metadata_size; 1139 1140 return 0; 1141 } 1142 1143 /** 1144 * amdgpu_bo_get_metadata - get metadata 1145 * @bo: &amdgpu_bo buffer object 1146 * @buffer: returned metadata 1147 * @buffer_size: size of the buffer 1148 * @metadata_size: size of the returned metadata 1149 * @flags: flags of the returned metadata 1150 * 1151 * Gets buffer object's metadata, its size and flags. buffer_size shall not be 1152 * less than metadata_size. 1153 * Used via GEM ioctl. 1154 * 1155 * Returns: 1156 * 0 for success or a negative error code on failure. 1157 */ 1158 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer, 1159 size_t buffer_size, uint32_t *metadata_size, 1160 uint64_t *flags) 1161 { 1162 struct amdgpu_bo_user *ubo; 1163 1164 if (!buffer && !metadata_size) 1165 return -EINVAL; 1166 1167 BUG_ON(bo->tbo.type == ttm_bo_type_kernel); 1168 ubo = to_amdgpu_bo_user(bo); 1169 if (metadata_size) 1170 *metadata_size = ubo->metadata_size; 1171 1172 if (buffer) { 1173 if (buffer_size < ubo->metadata_size) 1174 return -EINVAL; 1175 1176 if (ubo->metadata_size) 1177 memcpy(buffer, ubo->metadata, ubo->metadata_size); 1178 } 1179 1180 if (flags) 1181 *flags = ubo->metadata_flags; 1182 1183 return 0; 1184 } 1185 1186 /** 1187 * amdgpu_bo_move_notify - notification about a memory move 1188 * @bo: pointer to a buffer object 1189 * @evict: if this move is evicting the buffer from the graphics address space 1190 * @new_mem: new resource for backing the BO 1191 * 1192 * Marks the corresponding &amdgpu_bo buffer object as invalid, also performs 1193 * bookkeeping. 1194 * TTM driver callback which is called when ttm moves a buffer. 1195 */ 1196 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, 1197 bool evict, 1198 struct ttm_resource *new_mem) 1199 { 1200 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 1201 struct ttm_resource *old_mem = bo->resource; 1202 struct amdgpu_bo *abo; 1203 1204 if (!amdgpu_bo_is_amdgpu_bo(bo)) 1205 return; 1206 1207 abo = ttm_to_amdgpu_bo(bo); 1208 amdgpu_vm_bo_invalidate(adev, abo, evict); 1209 1210 amdgpu_bo_kunmap(abo); 1211 1212 if (abo->tbo.base.dma_buf && !abo->tbo.base.import_attach && 1213 old_mem && old_mem->mem_type != TTM_PL_SYSTEM) 1214 dma_buf_move_notify(abo->tbo.base.dma_buf); 1215 1216 /* move_notify is called before move happens */ 1217 trace_amdgpu_bo_move(abo, new_mem ? new_mem->mem_type : -1, 1218 old_mem ? old_mem->mem_type : -1); 1219 } 1220 1221 void amdgpu_bo_get_memory(struct amdgpu_bo *bo, 1222 struct amdgpu_mem_stats *stats) 1223 { 1224 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 1225 struct ttm_resource *res = bo->tbo.resource; 1226 uint64_t size = amdgpu_bo_size(bo); 1227 struct drm_gem_object *obj; 1228 bool shared; 1229 1230 /* Abort if the BO doesn't currently have a backing store */ 1231 if (!res) 1232 return; 1233 1234 obj = &bo->tbo.base; 1235 shared = drm_gem_object_is_shared_for_memory_stats(obj); 1236 1237 switch (res->mem_type) { 1238 case TTM_PL_VRAM: 1239 stats->vram += size; 1240 if (amdgpu_res_cpu_visible(adev, res)) 1241 stats->visible_vram += size; 1242 if (shared) 1243 stats->vram_shared += size; 1244 break; 1245 case TTM_PL_TT: 1246 stats->gtt += size; 1247 if (shared) 1248 stats->gtt_shared += size; 1249 break; 1250 case TTM_PL_SYSTEM: 1251 default: 1252 stats->cpu += size; 1253 if (shared) 1254 stats->cpu_shared += size; 1255 break; 1256 } 1257 1258 if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) { 1259 stats->requested_vram += size; 1260 if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) 1261 stats->requested_visible_vram += size; 1262 1263 if (res->mem_type != TTM_PL_VRAM) { 1264 stats->evicted_vram += size; 1265 if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) 1266 stats->evicted_visible_vram += size; 1267 } 1268 } else if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_GTT) { 1269 stats->requested_gtt += size; 1270 } 1271 } 1272 1273 /** 1274 * amdgpu_bo_release_notify - notification about a BO being released 1275 * @bo: pointer to a buffer object 1276 * 1277 * Wipes VRAM buffers whose contents should not be leaked before the 1278 * memory is released. 1279 */ 1280 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo) 1281 { 1282 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 1283 struct dma_fence *fence = NULL; 1284 struct amdgpu_bo *abo; 1285 int r; 1286 1287 if (!amdgpu_bo_is_amdgpu_bo(bo)) 1288 return; 1289 1290 abo = ttm_to_amdgpu_bo(bo); 1291 1292 WARN_ON(abo->vm_bo); 1293 1294 if (abo->kfd_bo) 1295 amdgpu_amdkfd_release_notify(abo); 1296 1297 /* We only remove the fence if the resv has individualized. */ 1298 WARN_ON_ONCE(bo->type == ttm_bo_type_kernel 1299 && bo->base.resv != &bo->base._resv); 1300 if (bo->base.resv == &bo->base._resv) 1301 amdgpu_amdkfd_remove_fence_on_pt_pd_bos(abo); 1302 1303 if (!bo->resource || bo->resource->mem_type != TTM_PL_VRAM || 1304 !(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE) || 1305 adev->in_suspend || drm_dev_is_unplugged(adev_to_drm(adev))) 1306 return; 1307 1308 if (WARN_ON_ONCE(!dma_resv_trylock(bo->base.resv))) 1309 return; 1310 1311 r = amdgpu_fill_buffer(abo, 0, bo->base.resv, &fence, true); 1312 if (!WARN_ON(r)) { 1313 amdgpu_vram_mgr_set_cleared(bo->resource); 1314 amdgpu_bo_fence(abo, fence, false); 1315 dma_fence_put(fence); 1316 } 1317 1318 dma_resv_unlock(bo->base.resv); 1319 } 1320 1321 /** 1322 * amdgpu_bo_fault_reserve_notify - notification about a memory fault 1323 * @bo: pointer to a buffer object 1324 * 1325 * Notifies the driver we are taking a fault on this BO and have reserved it, 1326 * also performs bookkeeping. 1327 * TTM driver callback for dealing with vm faults. 1328 * 1329 * Returns: 1330 * 0 for success or a negative error code on failure. 1331 */ 1332 vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo) 1333 { 1334 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 1335 struct ttm_operation_ctx ctx = { false, false }; 1336 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo); 1337 int r; 1338 1339 /* Remember that this BO was accessed by the CPU */ 1340 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 1341 1342 if (amdgpu_res_cpu_visible(adev, bo->resource)) 1343 return 0; 1344 1345 /* Can't move a pinned BO to visible VRAM */ 1346 if (abo->tbo.pin_count > 0) 1347 return VM_FAULT_SIGBUS; 1348 1349 /* hurrah the memory is not visible ! */ 1350 atomic64_inc(&adev->num_vram_cpu_page_faults); 1351 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM | 1352 AMDGPU_GEM_DOMAIN_GTT); 1353 1354 /* Avoid costly evictions; only set GTT as a busy placement */ 1355 abo->placements[0].flags |= TTM_PL_FLAG_DESIRED; 1356 1357 r = ttm_bo_validate(bo, &abo->placement, &ctx); 1358 if (unlikely(r == -EBUSY || r == -ERESTARTSYS)) 1359 return VM_FAULT_NOPAGE; 1360 else if (unlikely(r)) 1361 return VM_FAULT_SIGBUS; 1362 1363 /* this should never happen */ 1364 if (bo->resource->mem_type == TTM_PL_VRAM && 1365 !amdgpu_res_cpu_visible(adev, bo->resource)) 1366 return VM_FAULT_SIGBUS; 1367 1368 ttm_bo_move_to_lru_tail_unlocked(bo); 1369 return 0; 1370 } 1371 1372 /** 1373 * amdgpu_bo_fence - add fence to buffer object 1374 * 1375 * @bo: buffer object in question 1376 * @fence: fence to add 1377 * @shared: true if fence should be added shared 1378 * 1379 */ 1380 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence, 1381 bool shared) 1382 { 1383 struct dma_resv *resv = bo->tbo.base.resv; 1384 int r; 1385 1386 r = dma_resv_reserve_fences(resv, 1); 1387 if (r) { 1388 /* As last resort on OOM we block for the fence */ 1389 dma_fence_wait(fence, false); 1390 return; 1391 } 1392 1393 dma_resv_add_fence(resv, fence, shared ? DMA_RESV_USAGE_READ : 1394 DMA_RESV_USAGE_WRITE); 1395 } 1396 1397 /** 1398 * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences 1399 * 1400 * @adev: amdgpu device pointer 1401 * @resv: reservation object to sync to 1402 * @sync_mode: synchronization mode 1403 * @owner: fence owner 1404 * @intr: Whether the wait is interruptible 1405 * 1406 * Extract the fences from the reservation object and waits for them to finish. 1407 * 1408 * Returns: 1409 * 0 on success, errno otherwise. 1410 */ 1411 int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv, 1412 enum amdgpu_sync_mode sync_mode, void *owner, 1413 bool intr) 1414 { 1415 struct amdgpu_sync sync; 1416 int r; 1417 1418 amdgpu_sync_create(&sync); 1419 amdgpu_sync_resv(adev, &sync, resv, sync_mode, owner); 1420 r = amdgpu_sync_wait(&sync, intr); 1421 amdgpu_sync_free(&sync); 1422 return r; 1423 } 1424 1425 /** 1426 * amdgpu_bo_sync_wait - Wrapper for amdgpu_bo_sync_wait_resv 1427 * @bo: buffer object to wait for 1428 * @owner: fence owner 1429 * @intr: Whether the wait is interruptible 1430 * 1431 * Wrapper to wait for fences in a BO. 1432 * Returns: 1433 * 0 on success, errno otherwise. 1434 */ 1435 int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr) 1436 { 1437 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 1438 1439 return amdgpu_bo_sync_wait_resv(adev, bo->tbo.base.resv, 1440 AMDGPU_SYNC_NE_OWNER, owner, intr); 1441 } 1442 1443 /** 1444 * amdgpu_bo_gpu_offset - return GPU offset of bo 1445 * @bo: amdgpu object for which we query the offset 1446 * 1447 * Note: object should either be pinned or reserved when calling this 1448 * function, it might be useful to add check for this for debugging. 1449 * 1450 * Returns: 1451 * current GPU offset of the object. 1452 */ 1453 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo) 1454 { 1455 WARN_ON_ONCE(bo->tbo.resource->mem_type == TTM_PL_SYSTEM); 1456 WARN_ON_ONCE(!dma_resv_is_locked(bo->tbo.base.resv) && 1457 !bo->tbo.pin_count && bo->tbo.type != ttm_bo_type_kernel); 1458 WARN_ON_ONCE(bo->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET); 1459 WARN_ON_ONCE(bo->tbo.resource->mem_type == TTM_PL_VRAM && 1460 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)); 1461 1462 return amdgpu_bo_gpu_offset_no_check(bo); 1463 } 1464 1465 /** 1466 * amdgpu_bo_gpu_offset_no_check - return GPU offset of bo 1467 * @bo: amdgpu object for which we query the offset 1468 * 1469 * Returns: 1470 * current GPU offset of the object without raising warnings. 1471 */ 1472 u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo) 1473 { 1474 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 1475 uint64_t offset = AMDGPU_BO_INVALID_OFFSET; 1476 1477 if (bo->tbo.resource->mem_type == TTM_PL_TT) 1478 offset = amdgpu_gmc_agp_addr(&bo->tbo); 1479 1480 if (offset == AMDGPU_BO_INVALID_OFFSET) 1481 offset = (bo->tbo.resource->start << PAGE_SHIFT) + 1482 amdgpu_ttm_domain_start(adev, bo->tbo.resource->mem_type); 1483 1484 return amdgpu_gmc_sign_extend(offset); 1485 } 1486 1487 /** 1488 * amdgpu_bo_get_preferred_domain - get preferred domain 1489 * @adev: amdgpu device object 1490 * @domain: allowed :ref:`memory domains <amdgpu_memory_domains>` 1491 * 1492 * Returns: 1493 * Which of the allowed domains is preferred for allocating the BO. 1494 */ 1495 uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev, 1496 uint32_t domain) 1497 { 1498 if ((domain == (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) && 1499 ((adev->asic_type == CHIP_CARRIZO) || (adev->asic_type == CHIP_STONEY))) { 1500 domain = AMDGPU_GEM_DOMAIN_VRAM; 1501 if (adev->gmc.real_vram_size <= AMDGPU_SG_THRESHOLD) 1502 domain = AMDGPU_GEM_DOMAIN_GTT; 1503 } 1504 return domain; 1505 } 1506 1507 #if defined(CONFIG_DEBUG_FS) 1508 #define amdgpu_bo_print_flag(m, bo, flag) \ 1509 do { \ 1510 if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \ 1511 seq_printf((m), " " #flag); \ 1512 } \ 1513 } while (0) 1514 1515 /** 1516 * amdgpu_bo_print_info - print BO info in debugfs file 1517 * 1518 * @id: Index or Id of the BO 1519 * @bo: Requested BO for printing info 1520 * @m: debugfs file 1521 * 1522 * Print BO information in debugfs file 1523 * 1524 * Returns: 1525 * Size of the BO in bytes. 1526 */ 1527 u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) 1528 { 1529 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 1530 struct dma_buf_attachment *attachment; 1531 struct dma_buf *dma_buf; 1532 const char *placement; 1533 unsigned int pin_count; 1534 u64 size; 1535 1536 if (dma_resv_trylock(bo->tbo.base.resv)) { 1537 if (!bo->tbo.resource) { 1538 placement = "NONE"; 1539 } else { 1540 switch (bo->tbo.resource->mem_type) { 1541 case TTM_PL_VRAM: 1542 if (amdgpu_res_cpu_visible(adev, bo->tbo.resource)) 1543 placement = "VRAM VISIBLE"; 1544 else 1545 placement = "VRAM"; 1546 break; 1547 case TTM_PL_TT: 1548 placement = "GTT"; 1549 break; 1550 case AMDGPU_PL_GDS: 1551 placement = "GDS"; 1552 break; 1553 case AMDGPU_PL_GWS: 1554 placement = "GWS"; 1555 break; 1556 case AMDGPU_PL_OA: 1557 placement = "OA"; 1558 break; 1559 case AMDGPU_PL_PREEMPT: 1560 placement = "PREEMPTIBLE"; 1561 break; 1562 case AMDGPU_PL_DOORBELL: 1563 placement = "DOORBELL"; 1564 break; 1565 case TTM_PL_SYSTEM: 1566 default: 1567 placement = "CPU"; 1568 break; 1569 } 1570 } 1571 dma_resv_unlock(bo->tbo.base.resv); 1572 } else { 1573 placement = "UNKNOWN"; 1574 } 1575 1576 size = amdgpu_bo_size(bo); 1577 seq_printf(m, "\t\t0x%08x: %12lld byte %s", 1578 id, size, placement); 1579 1580 pin_count = READ_ONCE(bo->tbo.pin_count); 1581 if (pin_count) 1582 seq_printf(m, " pin count %d", pin_count); 1583 1584 dma_buf = READ_ONCE(bo->tbo.base.dma_buf); 1585 attachment = READ_ONCE(bo->tbo.base.import_attach); 1586 1587 if (attachment) 1588 seq_printf(m, " imported from ino:%lu", file_inode(dma_buf->file)->i_ino); 1589 else if (dma_buf) 1590 seq_printf(m, " exported as ino:%lu", file_inode(dma_buf->file)->i_ino); 1591 1592 amdgpu_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED); 1593 amdgpu_bo_print_flag(m, bo, NO_CPU_ACCESS); 1594 amdgpu_bo_print_flag(m, bo, CPU_GTT_USWC); 1595 amdgpu_bo_print_flag(m, bo, VRAM_CLEARED); 1596 amdgpu_bo_print_flag(m, bo, VRAM_CONTIGUOUS); 1597 amdgpu_bo_print_flag(m, bo, VM_ALWAYS_VALID); 1598 amdgpu_bo_print_flag(m, bo, EXPLICIT_SYNC); 1599 1600 seq_puts(m, "\n"); 1601 1602 return size; 1603 } 1604 #endif 1605