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