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