0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #include <linux/list.h>
0033 #include <linux/slab.h>
0034 #include <linux/dma-buf.h>
0035
0036 #include <drm/drm_drv.h>
0037 #include <drm/amdgpu_drm.h>
0038 #include <drm/drm_cache.h>
0039 #include "amdgpu.h"
0040 #include "amdgpu_trace.h"
0041 #include "amdgpu_amdkfd.h"
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
0057 {
0058 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
0059
0060 amdgpu_bo_kunmap(bo);
0061
0062 if (bo->tbo.base.import_attach)
0063 drm_prime_gem_destroy(&bo->tbo.base, bo->tbo.sg);
0064 drm_gem_object_release(&bo->tbo.base);
0065 amdgpu_bo_unref(&bo->parent);
0066 kvfree(bo);
0067 }
0068
0069 static void amdgpu_bo_user_destroy(struct ttm_buffer_object *tbo)
0070 {
0071 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
0072 struct amdgpu_bo_user *ubo;
0073
0074 ubo = to_amdgpu_bo_user(bo);
0075 kfree(ubo->metadata);
0076 amdgpu_bo_destroy(tbo);
0077 }
0078
0079 static void amdgpu_bo_vm_destroy(struct ttm_buffer_object *tbo)
0080 {
0081 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
0082 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
0083 struct amdgpu_bo_vm *vmbo;
0084
0085 vmbo = to_amdgpu_bo_vm(bo);
0086
0087 if (!list_empty(&vmbo->shadow_list)) {
0088 mutex_lock(&adev->shadow_list_lock);
0089 list_del_init(&vmbo->shadow_list);
0090 mutex_unlock(&adev->shadow_list_lock);
0091 }
0092
0093 amdgpu_bo_destroy(tbo);
0094 }
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
0107 {
0108 if (bo->destroy == &amdgpu_bo_destroy ||
0109 bo->destroy == &amdgpu_bo_user_destroy ||
0110 bo->destroy == &amdgpu_bo_vm_destroy)
0111 return true;
0112
0113 return false;
0114 }
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
0125 {
0126 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
0127 struct ttm_placement *placement = &abo->placement;
0128 struct ttm_place *places = abo->placements;
0129 u64 flags = abo->flags;
0130 u32 c = 0;
0131
0132 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
0133 unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
0134
0135 places[c].fpfn = 0;
0136 places[c].lpfn = 0;
0137 places[c].mem_type = TTM_PL_VRAM;
0138 places[c].flags = 0;
0139
0140 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
0141 places[c].lpfn = visible_pfn;
0142 else
0143 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
0144
0145 if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
0146 places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
0147 c++;
0148 }
0149
0150 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
0151 places[c].fpfn = 0;
0152 places[c].lpfn = 0;
0153 places[c].mem_type =
0154 abo->flags & AMDGPU_GEM_CREATE_PREEMPTIBLE ?
0155 AMDGPU_PL_PREEMPT : TTM_PL_TT;
0156 places[c].flags = 0;
0157 c++;
0158 }
0159
0160 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
0161 places[c].fpfn = 0;
0162 places[c].lpfn = 0;
0163 places[c].mem_type = TTM_PL_SYSTEM;
0164 places[c].flags = 0;
0165 c++;
0166 }
0167
0168 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
0169 places[c].fpfn = 0;
0170 places[c].lpfn = 0;
0171 places[c].mem_type = AMDGPU_PL_GDS;
0172 places[c].flags = 0;
0173 c++;
0174 }
0175
0176 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
0177 places[c].fpfn = 0;
0178 places[c].lpfn = 0;
0179 places[c].mem_type = AMDGPU_PL_GWS;
0180 places[c].flags = 0;
0181 c++;
0182 }
0183
0184 if (domain & AMDGPU_GEM_DOMAIN_OA) {
0185 places[c].fpfn = 0;
0186 places[c].lpfn = 0;
0187 places[c].mem_type = AMDGPU_PL_OA;
0188 places[c].flags = 0;
0189 c++;
0190 }
0191
0192 if (!c) {
0193 places[c].fpfn = 0;
0194 places[c].lpfn = 0;
0195 places[c].mem_type = TTM_PL_SYSTEM;
0196 places[c].flags = 0;
0197 c++;
0198 }
0199
0200 BUG_ON(c > AMDGPU_BO_MAX_PLACEMENTS);
0201
0202 placement->num_placement = c;
0203 placement->placement = places;
0204
0205 placement->num_busy_placement = c;
0206 placement->busy_placement = places;
0207 }
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
0229 unsigned long size, int align,
0230 u32 domain, struct amdgpu_bo **bo_ptr,
0231 u64 *gpu_addr, void **cpu_addr)
0232 {
0233 struct amdgpu_bo_param bp;
0234 bool free = false;
0235 int r;
0236
0237 if (!size) {
0238 amdgpu_bo_unref(bo_ptr);
0239 return 0;
0240 }
0241
0242 memset(&bp, 0, sizeof(bp));
0243 bp.size = size;
0244 bp.byte_align = align;
0245 bp.domain = domain;
0246 bp.flags = cpu_addr ? AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
0247 : AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
0248 bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
0249 bp.type = ttm_bo_type_kernel;
0250 bp.resv = NULL;
0251 bp.bo_ptr_size = sizeof(struct amdgpu_bo);
0252
0253 if (!*bo_ptr) {
0254 r = amdgpu_bo_create(adev, &bp, bo_ptr);
0255 if (r) {
0256 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
0257 r);
0258 return r;
0259 }
0260 free = true;
0261 }
0262
0263 r = amdgpu_bo_reserve(*bo_ptr, false);
0264 if (r) {
0265 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
0266 goto error_free;
0267 }
0268
0269 r = amdgpu_bo_pin(*bo_ptr, domain);
0270 if (r) {
0271 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
0272 goto error_unreserve;
0273 }
0274
0275 r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
0276 if (r) {
0277 dev_err(adev->dev, "%p bind failed\n", *bo_ptr);
0278 goto error_unpin;
0279 }
0280
0281 if (gpu_addr)
0282 *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
0283
0284 if (cpu_addr) {
0285 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
0286 if (r) {
0287 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
0288 goto error_unpin;
0289 }
0290 }
0291
0292 return 0;
0293
0294 error_unpin:
0295 amdgpu_bo_unpin(*bo_ptr);
0296 error_unreserve:
0297 amdgpu_bo_unreserve(*bo_ptr);
0298
0299 error_free:
0300 if (free)
0301 amdgpu_bo_unref(bo_ptr);
0302
0303 return r;
0304 }
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
0325 unsigned long size, int align,
0326 u32 domain, struct amdgpu_bo **bo_ptr,
0327 u64 *gpu_addr, void **cpu_addr)
0328 {
0329 int r;
0330
0331 r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
0332 gpu_addr, cpu_addr);
0333
0334 if (r)
0335 return r;
0336
0337 if (*bo_ptr)
0338 amdgpu_bo_unreserve(*bo_ptr);
0339
0340 return 0;
0341 }
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
0359 uint64_t offset, uint64_t size, uint32_t domain,
0360 struct amdgpu_bo **bo_ptr, void **cpu_addr)
0361 {
0362 struct ttm_operation_ctx ctx = { false, false };
0363 unsigned int i;
0364 int r;
0365
0366 offset &= PAGE_MASK;
0367 size = ALIGN(size, PAGE_SIZE);
0368
0369 r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE, domain, bo_ptr,
0370 NULL, cpu_addr);
0371 if (r)
0372 return r;
0373
0374 if ((*bo_ptr) == NULL)
0375 return 0;
0376
0377
0378
0379
0380
0381 if (cpu_addr)
0382 amdgpu_bo_kunmap(*bo_ptr);
0383
0384 ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
0385
0386 for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
0387 (*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
0388 (*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
0389 }
0390 r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
0391 &(*bo_ptr)->tbo.resource, &ctx);
0392 if (r)
0393 goto error;
0394
0395 if (cpu_addr) {
0396 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
0397 if (r)
0398 goto error;
0399 }
0400
0401 amdgpu_bo_unreserve(*bo_ptr);
0402 return 0;
0403
0404 error:
0405 amdgpu_bo_unreserve(*bo_ptr);
0406 amdgpu_bo_unref(bo_ptr);
0407 return r;
0408 }
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
0420 void **cpu_addr)
0421 {
0422 if (*bo == NULL)
0423 return;
0424
0425 if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
0426 if (cpu_addr)
0427 amdgpu_bo_kunmap(*bo);
0428
0429 amdgpu_bo_unpin(*bo);
0430 amdgpu_bo_unreserve(*bo);
0431 }
0432 amdgpu_bo_unref(bo);
0433
0434 if (gpu_addr)
0435 *gpu_addr = 0;
0436
0437 if (cpu_addr)
0438 *cpu_addr = NULL;
0439 }
0440
0441
0442 static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
0443 unsigned long size, u32 domain)
0444 {
0445 struct ttm_resource_manager *man = NULL;
0446
0447
0448
0449
0450
0451 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
0452 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
0453
0454 if (size < man->size)
0455 return true;
0456 else
0457 goto fail;
0458 }
0459
0460 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
0461 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
0462
0463 if (size < man->size)
0464 return true;
0465 else
0466 goto fail;
0467 }
0468
0469
0470
0471 return true;
0472
0473 fail:
0474 DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
0475 man->size);
0476 return false;
0477 }
0478
0479 bool amdgpu_bo_support_uswc(u64 bo_flags)
0480 {
0481
0482 #ifdef CONFIG_X86_32
0483
0484
0485
0486 return false;
0487 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
0488
0489
0490
0491
0492
0493 #ifndef CONFIG_COMPILE_TEST
0494 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
0495 thanks to write-combining
0496 #endif
0497
0498 if (bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
0499 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
0500 "better performance thanks to write-combining\n");
0501 return false;
0502 #else
0503
0504
0505
0506 if (!drm_arch_can_wc_memory())
0507 return false;
0508
0509 return true;
0510 #endif
0511 }
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524 int amdgpu_bo_create(struct amdgpu_device *adev,
0525 struct amdgpu_bo_param *bp,
0526 struct amdgpu_bo **bo_ptr)
0527 {
0528 struct ttm_operation_ctx ctx = {
0529 .interruptible = (bp->type != ttm_bo_type_kernel),
0530 .no_wait_gpu = bp->no_wait_gpu,
0531
0532 .gfp_retry_mayfail = true,
0533 .allow_res_evict = bp->type != ttm_bo_type_kernel,
0534 .resv = bp->resv
0535 };
0536 struct amdgpu_bo *bo;
0537 unsigned long page_align, size = bp->size;
0538 int r;
0539
0540
0541 if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
0542
0543 page_align = bp->byte_align;
0544 size <<= PAGE_SHIFT;
0545 } else if (bp->domain & AMDGPU_GEM_DOMAIN_GDS) {
0546
0547 page_align = ALIGN(bp->byte_align, 4);
0548 size = ALIGN(size, 4) << PAGE_SHIFT;
0549 } else {
0550
0551 page_align = ALIGN(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT;
0552 size = ALIGN(size, PAGE_SIZE);
0553 }
0554
0555 if (!amdgpu_bo_validate_size(adev, size, bp->domain))
0556 return -ENOMEM;
0557
0558 BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo));
0559
0560 *bo_ptr = NULL;
0561 bo = kvzalloc(bp->bo_ptr_size, GFP_KERNEL);
0562 if (bo == NULL)
0563 return -ENOMEM;
0564 drm_gem_private_object_init(adev_to_drm(adev), &bo->tbo.base, size);
0565 bo->vm_bo = NULL;
0566 bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain :
0567 bp->domain;
0568 bo->allowed_domains = bo->preferred_domains;
0569 if (bp->type != ttm_bo_type_kernel &&
0570 !(bp->flags & AMDGPU_GEM_CREATE_DISCARDABLE) &&
0571 bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
0572 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
0573
0574 bo->flags = bp->flags;
0575
0576 if (!amdgpu_bo_support_uswc(bo->flags))
0577 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
0578
0579 if (adev->ras_enabled)
0580 bo->flags |= AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE;
0581
0582 bo->tbo.bdev = &adev->mman.bdev;
0583 if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA |
0584 AMDGPU_GEM_DOMAIN_GDS))
0585 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
0586 else
0587 amdgpu_bo_placement_from_domain(bo, bp->domain);
0588 if (bp->type == ttm_bo_type_kernel)
0589 bo->tbo.priority = 1;
0590
0591 if (!bp->destroy)
0592 bp->destroy = &amdgpu_bo_destroy;
0593
0594 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
0595 &bo->placement, page_align, &ctx, NULL,
0596 bp->resv, bp->destroy);
0597 if (unlikely(r != 0))
0598 return r;
0599
0600 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
0601 bo->tbo.resource->mem_type == TTM_PL_VRAM &&
0602 bo->tbo.resource->start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
0603 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
0604 ctx.bytes_moved);
0605 else
0606 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
0607
0608 if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
0609 bo->tbo.resource->mem_type == TTM_PL_VRAM) {
0610 struct dma_fence *fence;
0611
0612 r = amdgpu_fill_buffer(bo, 0, bo->tbo.base.resv, &fence);
0613 if (unlikely(r))
0614 goto fail_unreserve;
0615
0616 dma_resv_add_fence(bo->tbo.base.resv, fence,
0617 DMA_RESV_USAGE_KERNEL);
0618 dma_fence_put(fence);
0619 }
0620 if (!bp->resv)
0621 amdgpu_bo_unreserve(bo);
0622 *bo_ptr = bo;
0623
0624 trace_amdgpu_bo_create(bo);
0625
0626
0627 if (bp->type == ttm_bo_type_device)
0628 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
0629
0630 return 0;
0631
0632 fail_unreserve:
0633 if (!bp->resv)
0634 dma_resv_unlock(bo->tbo.base.resv);
0635 amdgpu_bo_unref(&bo);
0636 return r;
0637 }
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651 int amdgpu_bo_create_user(struct amdgpu_device *adev,
0652 struct amdgpu_bo_param *bp,
0653 struct amdgpu_bo_user **ubo_ptr)
0654 {
0655 struct amdgpu_bo *bo_ptr;
0656 int r;
0657
0658 bp->bo_ptr_size = sizeof(struct amdgpu_bo_user);
0659 bp->destroy = &amdgpu_bo_user_destroy;
0660 r = amdgpu_bo_create(adev, bp, &bo_ptr);
0661 if (r)
0662 return r;
0663
0664 *ubo_ptr = to_amdgpu_bo_user(bo_ptr);
0665 return r;
0666 }
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680 int amdgpu_bo_create_vm(struct amdgpu_device *adev,
0681 struct amdgpu_bo_param *bp,
0682 struct amdgpu_bo_vm **vmbo_ptr)
0683 {
0684 struct amdgpu_bo *bo_ptr;
0685 int r;
0686
0687
0688
0689
0690 BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo_vm));
0691 bp->destroy = &amdgpu_bo_vm_destroy;
0692 r = amdgpu_bo_create(adev, bp, &bo_ptr);
0693 if (r)
0694 return r;
0695
0696 *vmbo_ptr = to_amdgpu_bo_vm(bo_ptr);
0697 INIT_LIST_HEAD(&(*vmbo_ptr)->shadow_list);
0698 return r;
0699 }
0700
0701
0702
0703
0704
0705
0706
0707
0708 void amdgpu_bo_add_to_shadow_list(struct amdgpu_bo_vm *vmbo)
0709 {
0710 struct amdgpu_device *adev = amdgpu_ttm_adev(vmbo->bo.tbo.bdev);
0711
0712 mutex_lock(&adev->shadow_list_lock);
0713 list_add_tail(&vmbo->shadow_list, &adev->shadow_list);
0714 mutex_unlock(&adev->shadow_list_lock);
0715 }
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730 int amdgpu_bo_restore_shadow(struct amdgpu_bo *shadow, struct dma_fence **fence)
0731
0732 {
0733 struct amdgpu_device *adev = amdgpu_ttm_adev(shadow->tbo.bdev);
0734 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
0735 uint64_t shadow_addr, parent_addr;
0736
0737 shadow_addr = amdgpu_bo_gpu_offset(shadow);
0738 parent_addr = amdgpu_bo_gpu_offset(shadow->parent);
0739
0740 return amdgpu_copy_buffer(ring, shadow_addr, parent_addr,
0741 amdgpu_bo_size(shadow), NULL, fence,
0742 true, false, false);
0743 }
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
0757 {
0758 void *kptr;
0759 long r;
0760
0761 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
0762 return -EPERM;
0763
0764 r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_KERNEL,
0765 false, MAX_SCHEDULE_TIMEOUT);
0766 if (r < 0)
0767 return r;
0768
0769 kptr = amdgpu_bo_kptr(bo);
0770 if (kptr) {
0771 if (ptr)
0772 *ptr = kptr;
0773 return 0;
0774 }
0775
0776 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.resource->num_pages, &bo->kmap);
0777 if (r)
0778 return r;
0779
0780 if (ptr)
0781 *ptr = amdgpu_bo_kptr(bo);
0782
0783 return 0;
0784 }
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
0796 {
0797 bool is_iomem;
0798
0799 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
0800 }
0801
0802
0803
0804
0805
0806
0807
0808 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
0809 {
0810 if (bo->kmap.bo)
0811 ttm_bo_kunmap(&bo->kmap);
0812 }
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
0824 {
0825 if (bo == NULL)
0826 return NULL;
0827
0828 ttm_bo_get(&bo->tbo);
0829 return bo;
0830 }
0831
0832
0833
0834
0835
0836
0837
0838 void amdgpu_bo_unref(struct amdgpu_bo **bo)
0839 {
0840 struct ttm_buffer_object *tbo;
0841
0842 if ((*bo) == NULL)
0843 return;
0844
0845 tbo = &((*bo)->tbo);
0846 ttm_bo_put(tbo);
0847 *bo = NULL;
0848 }
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
0873 u64 min_offset, u64 max_offset)
0874 {
0875 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
0876 struct ttm_operation_ctx ctx = { false, false };
0877 int r, i;
0878
0879 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
0880 return -EPERM;
0881
0882 if (WARN_ON_ONCE(min_offset > max_offset))
0883 return -EINVAL;
0884
0885
0886 if (bo->preferred_domains & domain)
0887 domain = bo->preferred_domains & domain;
0888
0889
0890 if (bo->tbo.base.import_attach) {
0891 if (domain & AMDGPU_GEM_DOMAIN_GTT)
0892 domain = AMDGPU_GEM_DOMAIN_GTT;
0893 else
0894 return -EINVAL;
0895 }
0896
0897 if (bo->tbo.pin_count) {
0898 uint32_t mem_type = bo->tbo.resource->mem_type;
0899 uint32_t mem_flags = bo->tbo.resource->placement;
0900
0901 if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
0902 return -EINVAL;
0903
0904 if ((mem_type == TTM_PL_VRAM) &&
0905 (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) &&
0906 !(mem_flags & TTM_PL_FLAG_CONTIGUOUS))
0907 return -EINVAL;
0908
0909 ttm_bo_pin(&bo->tbo);
0910
0911 if (max_offset != 0) {
0912 u64 domain_start = amdgpu_ttm_domain_start(adev,
0913 mem_type);
0914 WARN_ON_ONCE(max_offset <
0915 (amdgpu_bo_gpu_offset(bo) - domain_start));
0916 }
0917
0918 return 0;
0919 }
0920
0921
0922
0923
0924 domain = amdgpu_bo_get_preferred_domain(adev, domain);
0925
0926 if (bo->tbo.base.import_attach)
0927 dma_buf_pin(bo->tbo.base.import_attach);
0928
0929
0930 if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
0931 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
0932 amdgpu_bo_placement_from_domain(bo, domain);
0933 for (i = 0; i < bo->placement.num_placement; i++) {
0934 unsigned fpfn, lpfn;
0935
0936 fpfn = min_offset >> PAGE_SHIFT;
0937 lpfn = max_offset >> PAGE_SHIFT;
0938
0939 if (fpfn > bo->placements[i].fpfn)
0940 bo->placements[i].fpfn = fpfn;
0941 if (!bo->placements[i].lpfn ||
0942 (lpfn && lpfn < bo->placements[i].lpfn))
0943 bo->placements[i].lpfn = lpfn;
0944 }
0945
0946 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
0947 if (unlikely(r)) {
0948 dev_err(adev->dev, "%p pin failed\n", bo);
0949 goto error;
0950 }
0951
0952 ttm_bo_pin(&bo->tbo);
0953
0954 domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
0955 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
0956 atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);
0957 atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo),
0958 &adev->visible_pin_size);
0959 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
0960 atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
0961 }
0962
0963 error:
0964 return r;
0965 }
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)
0980 {
0981 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
0982 return amdgpu_bo_pin_restricted(bo, domain, 0, 0);
0983 }
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995 void amdgpu_bo_unpin(struct amdgpu_bo *bo)
0996 {
0997 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
0998
0999 ttm_bo_unpin(&bo->tbo);
1000 if (bo->tbo.pin_count)
1001 return;
1002
1003 if (bo->tbo.base.import_attach)
1004 dma_buf_unpin(bo->tbo.base.import_attach);
1005
1006 if (bo->tbo.resource->mem_type == TTM_PL_VRAM) {
1007 atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size);
1008 atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo),
1009 &adev->visible_pin_size);
1010 } else if (bo->tbo.resource->mem_type == TTM_PL_TT) {
1011 atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size);
1012 }
1013 }
1014
1015 static const char *amdgpu_vram_names[] = {
1016 "UNKNOWN",
1017 "GDDR1",
1018 "DDR2",
1019 "GDDR3",
1020 "GDDR4",
1021 "GDDR5",
1022 "HBM",
1023 "DDR3",
1024 "DDR4",
1025 "GDDR6",
1026 "DDR5",
1027 "LPDDR4",
1028 "LPDDR5"
1029 };
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040 int amdgpu_bo_init(struct amdgpu_device *adev)
1041 {
1042
1043 if (!adev->gmc.xgmi.connected_to_cpu) {
1044
1045 int r = arch_io_reserve_memtype_wc(adev->gmc.aper_base,
1046 adev->gmc.aper_size);
1047
1048 if (r) {
1049 DRM_ERROR("Unable to set WC memtype for the aperture base\n");
1050 return r;
1051 }
1052
1053
1054 adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
1055 adev->gmc.aper_size);
1056 }
1057
1058 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
1059 adev->gmc.mc_vram_size >> 20,
1060 (unsigned long long)adev->gmc.aper_size >> 20);
1061 DRM_INFO("RAM width %dbits %s\n",
1062 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
1063 return amdgpu_ttm_init(adev);
1064 }
1065
1066
1067
1068
1069
1070
1071
1072 void amdgpu_bo_fini(struct amdgpu_device *adev)
1073 {
1074 int idx;
1075
1076 amdgpu_ttm_fini(adev);
1077
1078 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
1079
1080 if (!adev->gmc.xgmi.connected_to_cpu) {
1081 arch_phys_wc_del(adev->gmc.vram_mtrr);
1082 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
1083 }
1084 drm_dev_exit(idx);
1085 }
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
1100 {
1101 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1102 struct amdgpu_bo_user *ubo;
1103
1104 BUG_ON(bo->tbo.type == ttm_bo_type_kernel);
1105 if (adev->family <= AMDGPU_FAMILY_CZ &&
1106 AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
1107 return -EINVAL;
1108
1109 ubo = to_amdgpu_bo_user(bo);
1110 ubo->tiling_flags = tiling_flags;
1111 return 0;
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
1123 {
1124 struct amdgpu_bo_user *ubo;
1125
1126 BUG_ON(bo->tbo.type == ttm_bo_type_kernel);
1127 dma_resv_assert_held(bo->tbo.base.resv);
1128 ubo = to_amdgpu_bo_user(bo);
1129
1130 if (tiling_flags)
1131 *tiling_flags = ubo->tiling_flags;
1132 }
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
1148 uint32_t metadata_size, uint64_t flags)
1149 {
1150 struct amdgpu_bo_user *ubo;
1151 void *buffer;
1152
1153 BUG_ON(bo->tbo.type == ttm_bo_type_kernel);
1154 ubo = to_amdgpu_bo_user(bo);
1155 if (!metadata_size) {
1156 if (ubo->metadata_size) {
1157 kfree(ubo->metadata);
1158 ubo->metadata = NULL;
1159 ubo->metadata_size = 0;
1160 }
1161 return 0;
1162 }
1163
1164 if (metadata == NULL)
1165 return -EINVAL;
1166
1167 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
1168 if (buffer == NULL)
1169 return -ENOMEM;
1170
1171 kfree(ubo->metadata);
1172 ubo->metadata_flags = flags;
1173 ubo->metadata = buffer;
1174 ubo->metadata_size = metadata_size;
1175
1176 return 0;
1177 }
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
1195 size_t buffer_size, uint32_t *metadata_size,
1196 uint64_t *flags)
1197 {
1198 struct amdgpu_bo_user *ubo;
1199
1200 if (!buffer && !metadata_size)
1201 return -EINVAL;
1202
1203 BUG_ON(bo->tbo.type == ttm_bo_type_kernel);
1204 ubo = to_amdgpu_bo_user(bo);
1205 if (metadata_size)
1206 *metadata_size = ubo->metadata_size;
1207
1208 if (buffer) {
1209 if (buffer_size < ubo->metadata_size)
1210 return -EINVAL;
1211
1212 if (ubo->metadata_size)
1213 memcpy(buffer, ubo->metadata, ubo->metadata_size);
1214 }
1215
1216 if (flags)
1217 *flags = ubo->metadata_flags;
1218
1219 return 0;
1220 }
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
1233 bool evict,
1234 struct ttm_resource *new_mem)
1235 {
1236 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1237 struct amdgpu_bo *abo;
1238 struct ttm_resource *old_mem = bo->resource;
1239
1240 if (!amdgpu_bo_is_amdgpu_bo(bo))
1241 return;
1242
1243 abo = ttm_to_amdgpu_bo(bo);
1244 amdgpu_vm_bo_invalidate(adev, abo, evict);
1245
1246 amdgpu_bo_kunmap(abo);
1247
1248 if (abo->tbo.base.dma_buf && !abo->tbo.base.import_attach &&
1249 bo->resource->mem_type != TTM_PL_SYSTEM)
1250 dma_buf_move_notify(abo->tbo.base.dma_buf);
1251
1252
1253 if (evict)
1254 atomic64_inc(&adev->num_evictions);
1255
1256
1257 if (!new_mem)
1258 return;
1259
1260
1261 trace_amdgpu_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
1262 }
1263
1264 void amdgpu_bo_get_memory(struct amdgpu_bo *bo, uint64_t *vram_mem,
1265 uint64_t *gtt_mem, uint64_t *cpu_mem)
1266 {
1267 unsigned int domain;
1268
1269 domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
1270 switch (domain) {
1271 case AMDGPU_GEM_DOMAIN_VRAM:
1272 *vram_mem += amdgpu_bo_size(bo);
1273 break;
1274 case AMDGPU_GEM_DOMAIN_GTT:
1275 *gtt_mem += amdgpu_bo_size(bo);
1276 break;
1277 case AMDGPU_GEM_DOMAIN_CPU:
1278 default:
1279 *cpu_mem += amdgpu_bo_size(bo);
1280 break;
1281 }
1282 }
1283
1284
1285
1286
1287
1288
1289
1290
1291 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
1292 {
1293 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1294 struct dma_fence *fence = NULL;
1295 struct amdgpu_bo *abo;
1296 int r;
1297
1298 if (!amdgpu_bo_is_amdgpu_bo(bo))
1299 return;
1300
1301 abo = ttm_to_amdgpu_bo(bo);
1302
1303 if (abo->kfd_bo)
1304 amdgpu_amdkfd_release_notify(abo);
1305
1306
1307 WARN_ON_ONCE(bo->type == ttm_bo_type_kernel
1308 && bo->base.resv != &bo->base._resv);
1309 if (bo->base.resv == &bo->base._resv)
1310 amdgpu_amdkfd_remove_fence_on_pt_pd_bos(abo);
1311
1312 if (bo->resource->mem_type != TTM_PL_VRAM ||
1313 !(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE) ||
1314 adev->in_suspend || adev->shutdown)
1315 return;
1316
1317 if (WARN_ON_ONCE(!dma_resv_trylock(bo->base.resv)))
1318 return;
1319
1320 r = amdgpu_fill_buffer(abo, AMDGPU_POISON, bo->base.resv, &fence);
1321 if (!WARN_ON(r)) {
1322 amdgpu_bo_fence(abo, fence, false);
1323 dma_fence_put(fence);
1324 }
1325
1326 dma_resv_unlock(bo->base.resv);
1327 }
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
1341 {
1342 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1343 struct ttm_operation_ctx ctx = { false, false };
1344 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1345 unsigned long offset;
1346 int r;
1347
1348
1349 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
1350
1351 if (bo->resource->mem_type != TTM_PL_VRAM)
1352 return 0;
1353
1354 offset = bo->resource->start << PAGE_SHIFT;
1355 if ((offset + bo->base.size) <= adev->gmc.visible_vram_size)
1356 return 0;
1357
1358
1359 if (abo->tbo.pin_count > 0)
1360 return VM_FAULT_SIGBUS;
1361
1362
1363 atomic64_inc(&adev->num_vram_cpu_page_faults);
1364 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
1365 AMDGPU_GEM_DOMAIN_GTT);
1366
1367
1368 abo->placement.num_busy_placement = 1;
1369 abo->placement.busy_placement = &abo->placements[1];
1370
1371 r = ttm_bo_validate(bo, &abo->placement, &ctx);
1372 if (unlikely(r == -EBUSY || r == -ERESTARTSYS))
1373 return VM_FAULT_NOPAGE;
1374 else if (unlikely(r))
1375 return VM_FAULT_SIGBUS;
1376
1377 offset = bo->resource->start << PAGE_SHIFT;
1378
1379 if (bo->resource->mem_type == TTM_PL_VRAM &&
1380 (offset + bo->base.size) > adev->gmc.visible_vram_size)
1381 return VM_FAULT_SIGBUS;
1382
1383 ttm_bo_move_to_lru_tail_unlocked(bo);
1384 return 0;
1385 }
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
1396 bool shared)
1397 {
1398 struct dma_resv *resv = bo->tbo.base.resv;
1399 int r;
1400
1401 r = dma_resv_reserve_fences(resv, 1);
1402 if (r) {
1403
1404 dma_fence_wait(fence, false);
1405 return;
1406 }
1407
1408 dma_resv_add_fence(resv, fence, shared ? DMA_RESV_USAGE_READ :
1409 DMA_RESV_USAGE_WRITE);
1410 }
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426 int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv,
1427 enum amdgpu_sync_mode sync_mode, void *owner,
1428 bool intr)
1429 {
1430 struct amdgpu_sync sync;
1431 int r;
1432
1433 amdgpu_sync_create(&sync);
1434 amdgpu_sync_resv(adev, &sync, resv, sync_mode, owner);
1435 r = amdgpu_sync_wait(&sync, intr);
1436 amdgpu_sync_free(&sync);
1437 return r;
1438 }
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450 int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr)
1451 {
1452 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1453
1454 return amdgpu_bo_sync_wait_resv(adev, bo->tbo.base.resv,
1455 AMDGPU_SYNC_NE_OWNER, owner, intr);
1456 }
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
1469 {
1470 WARN_ON_ONCE(bo->tbo.resource->mem_type == TTM_PL_SYSTEM);
1471 WARN_ON_ONCE(!dma_resv_is_locked(bo->tbo.base.resv) &&
1472 !bo->tbo.pin_count && bo->tbo.type != ttm_bo_type_kernel);
1473 WARN_ON_ONCE(bo->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET);
1474 WARN_ON_ONCE(bo->tbo.resource->mem_type == TTM_PL_VRAM &&
1475 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
1476
1477 return amdgpu_bo_gpu_offset_no_check(bo);
1478 }
1479
1480
1481
1482
1483
1484
1485
1486
1487 u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo)
1488 {
1489 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1490 uint64_t offset;
1491
1492 offset = (bo->tbo.resource->start << PAGE_SHIFT) +
1493 amdgpu_ttm_domain_start(adev, bo->tbo.resource->mem_type);
1494
1495 return amdgpu_gmc_sign_extend(offset);
1496 }
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506 uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
1507 uint32_t domain)
1508 {
1509 if (domain == (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
1510 domain = AMDGPU_GEM_DOMAIN_VRAM;
1511 if (adev->gmc.real_vram_size <= AMDGPU_SG_THRESHOLD)
1512 domain = AMDGPU_GEM_DOMAIN_GTT;
1513 }
1514 return domain;
1515 }
1516
1517 #if defined(CONFIG_DEBUG_FS)
1518 #define amdgpu_bo_print_flag(m, bo, flag) \
1519 do { \
1520 if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \
1521 seq_printf((m), " " #flag); \
1522 } \
1523 } while (0)
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537 u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m)
1538 {
1539 struct dma_buf_attachment *attachment;
1540 struct dma_buf *dma_buf;
1541 unsigned int domain;
1542 const char *placement;
1543 unsigned int pin_count;
1544 u64 size;
1545
1546 domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
1547 switch (domain) {
1548 case AMDGPU_GEM_DOMAIN_VRAM:
1549 placement = "VRAM";
1550 break;
1551 case AMDGPU_GEM_DOMAIN_GTT:
1552 placement = " GTT";
1553 break;
1554 case AMDGPU_GEM_DOMAIN_CPU:
1555 default:
1556 placement = " CPU";
1557 break;
1558 }
1559
1560 size = amdgpu_bo_size(bo);
1561 seq_printf(m, "\t\t0x%08x: %12lld byte %s",
1562 id, size, placement);
1563
1564 pin_count = READ_ONCE(bo->tbo.pin_count);
1565 if (pin_count)
1566 seq_printf(m, " pin count %d", pin_count);
1567
1568 dma_buf = READ_ONCE(bo->tbo.base.dma_buf);
1569 attachment = READ_ONCE(bo->tbo.base.import_attach);
1570
1571 if (attachment)
1572 seq_printf(m, " imported from %p", dma_buf);
1573 else if (dma_buf)
1574 seq_printf(m, " exported as %p", dma_buf);
1575
1576 amdgpu_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED);
1577 amdgpu_bo_print_flag(m, bo, NO_CPU_ACCESS);
1578 amdgpu_bo_print_flag(m, bo, CPU_GTT_USWC);
1579 amdgpu_bo_print_flag(m, bo, VRAM_CLEARED);
1580 amdgpu_bo_print_flag(m, bo, VRAM_CONTIGUOUS);
1581 amdgpu_bo_print_flag(m, bo, VM_ALWAYS_VALID);
1582 amdgpu_bo_print_flag(m, bo, EXPLICIT_SYNC);
1583
1584 seq_puts(m, "\n");
1585
1586 return size;
1587 }
1588 #endif