2 * Copyright 2009 Jerome Glisse.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 #include <linux/list.h>
33 #include <linux/slab.h>
35 #include <drm/amdgpu_drm.h>
36 #include <drm/drm_cache.h>
38 #include "amdgpu_trace.h"
39 #include "amdgpu_amdkfd.h"
41 static bool amdgpu_need_backup(struct amdgpu_device *adev)
43 if (adev->flags & AMD_IS_APU)
46 if (amdgpu_gpu_recovery == 0 ||
47 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))
53 static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
55 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
56 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
59 amdgpu_amdkfd_unreserve_system_memory_limit(bo);
63 drm_gem_object_release(&bo->gem_base);
64 amdgpu_bo_unref(&bo->parent);
65 if (!list_empty(&bo->shadow_list)) {
66 mutex_lock(&adev->shadow_list_lock);
67 list_del_init(&bo->shadow_list);
68 mutex_unlock(&adev->shadow_list_lock);
74 bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
76 if (bo->destroy == &amdgpu_ttm_bo_destroy)
81 void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
83 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
84 struct ttm_placement *placement = &abo->placement;
85 struct ttm_place *places = abo->placements;
86 u64 flags = abo->flags;
89 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
90 unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
94 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
97 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
98 places[c].lpfn = visible_pfn;
100 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
102 if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
103 places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
107 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
109 if (flags & AMDGPU_GEM_CREATE_SHADOW)
110 places[c].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
113 places[c].flags = TTM_PL_FLAG_TT;
114 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
115 places[c].flags |= TTM_PL_FLAG_WC |
116 TTM_PL_FLAG_UNCACHED;
118 places[c].flags |= TTM_PL_FLAG_CACHED;
122 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
125 places[c].flags = TTM_PL_FLAG_SYSTEM;
126 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
127 places[c].flags |= TTM_PL_FLAG_WC |
128 TTM_PL_FLAG_UNCACHED;
130 places[c].flags |= TTM_PL_FLAG_CACHED;
134 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
137 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
141 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
144 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
148 if (domain & AMDGPU_GEM_DOMAIN_OA) {
151 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
158 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
162 placement->num_placement = c;
163 placement->placement = places;
165 placement->num_busy_placement = c;
166 placement->busy_placement = places;
170 * amdgpu_bo_create_reserved - create reserved BO for kernel use
172 * @adev: amdgpu device object
173 * @size: size for the new BO
174 * @align: alignment for the new BO
175 * @domain: where to place it
176 * @bo_ptr: resulting BO
177 * @gpu_addr: GPU addr of the pinned BO
178 * @cpu_addr: optional CPU address mapping
180 * Allocates and pins a BO for kernel internal use, and returns it still
183 * Returns 0 on success, negative error code otherwise.
185 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
186 unsigned long size, int align,
187 u32 domain, struct amdgpu_bo **bo_ptr,
188 u64 *gpu_addr, void **cpu_addr)
194 r = amdgpu_bo_create(adev, size, align, domain,
195 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
196 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
197 ttm_bo_type_kernel, NULL, bo_ptr);
199 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
206 r = amdgpu_bo_reserve(*bo_ptr, false);
208 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
212 r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
214 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
215 goto error_unreserve;
219 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
221 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
222 goto error_unreserve;
229 amdgpu_bo_unreserve(*bo_ptr);
233 amdgpu_bo_unref(bo_ptr);
239 * amdgpu_bo_create_kernel - create BO for kernel use
241 * @adev: amdgpu device object
242 * @size: size for the new BO
243 * @align: alignment for the new BO
244 * @domain: where to place it
245 * @bo_ptr: resulting BO
246 * @gpu_addr: GPU addr of the pinned BO
247 * @cpu_addr: optional CPU address mapping
249 * Allocates and pins a BO for kernel internal use.
251 * Returns 0 on success, negative error code otherwise.
253 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
254 unsigned long size, int align,
255 u32 domain, struct amdgpu_bo **bo_ptr,
256 u64 *gpu_addr, void **cpu_addr)
260 r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
266 amdgpu_bo_unreserve(*bo_ptr);
272 * amdgpu_bo_free_kernel - free BO for kernel use
274 * @bo: amdgpu BO to free
276 * unmaps and unpin a BO for kernel internal use.
278 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
284 if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
286 amdgpu_bo_kunmap(*bo);
288 amdgpu_bo_unpin(*bo);
289 amdgpu_bo_unreserve(*bo);
300 /* Validate bo size is bit bigger then the request domain */
301 static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
302 unsigned long size, u32 domain)
304 struct ttm_mem_type_manager *man = NULL;
307 * If GTT is part of requested domains the check must succeed to
308 * allow fall back to GTT
310 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
311 man = &adev->mman.bdev.man[TTM_PL_TT];
313 if (size < (man->size << PAGE_SHIFT))
319 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
320 man = &adev->mman.bdev.man[TTM_PL_VRAM];
322 if (size < (man->size << PAGE_SHIFT))
329 /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
333 DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
334 man->size << PAGE_SHIFT);
338 static int amdgpu_bo_do_create(struct amdgpu_device *adev, unsigned long size,
339 int byte_align, u32 domain,
340 u64 flags, enum ttm_bo_type type,
341 struct reservation_object *resv,
342 struct amdgpu_bo **bo_ptr)
344 struct ttm_operation_ctx ctx = {
345 .interruptible = (type != ttm_bo_type_kernel),
346 .no_wait_gpu = false,
348 .flags = TTM_OPT_FLAG_ALLOW_RES_EVICT
350 struct amdgpu_bo *bo;
351 unsigned long page_align;
355 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
356 size = ALIGN(size, PAGE_SIZE);
358 if (!amdgpu_bo_validate_size(adev, size, domain))
363 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
364 sizeof(struct amdgpu_bo));
366 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
369 drm_gem_private_object_init(adev->ddev, &bo->gem_base, size);
370 INIT_LIST_HEAD(&bo->shadow_list);
371 INIT_LIST_HEAD(&bo->va);
372 bo->preferred_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
373 AMDGPU_GEM_DOMAIN_GTT |
374 AMDGPU_GEM_DOMAIN_CPU |
375 AMDGPU_GEM_DOMAIN_GDS |
376 AMDGPU_GEM_DOMAIN_GWS |
377 AMDGPU_GEM_DOMAIN_OA);
378 bo->allowed_domains = bo->preferred_domains;
379 if (type != ttm_bo_type_kernel &&
380 bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
381 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
386 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
387 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
389 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
390 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
391 /* Don't try to enable write-combining when it can't work, or things
393 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
396 #ifndef CONFIG_COMPILE_TEST
397 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
398 thanks to write-combining
401 if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
402 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
403 "better performance thanks to write-combining\n");
404 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
406 /* For architectures that don't support WC memory,
407 * mask out the WC flag from the BO
409 if (!drm_arch_can_wc_memory())
410 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
413 bo->tbo.bdev = &adev->mman.bdev;
414 amdgpu_ttm_placement_from_domain(bo, domain);
416 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
417 &bo->placement, page_align, &ctx, acc_size,
418 NULL, resv, &amdgpu_ttm_bo_destroy);
419 if (unlikely(r != 0))
422 if (adev->gmc.visible_vram_size < adev->gmc.real_vram_size &&
423 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
424 bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
425 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
428 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
430 if (type == ttm_bo_type_kernel)
431 bo->tbo.priority = 1;
433 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
434 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
435 struct dma_fence *fence;
437 r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
441 amdgpu_bo_fence(bo, fence, false);
442 dma_fence_put(bo->tbo.moving);
443 bo->tbo.moving = dma_fence_get(fence);
444 dma_fence_put(fence);
447 amdgpu_bo_unreserve(bo);
450 trace_amdgpu_bo_create(bo);
452 /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
453 if (type == ttm_bo_type_device)
454 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
460 ww_mutex_unlock(&bo->tbo.resv->lock);
461 amdgpu_bo_unref(&bo);
465 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
466 unsigned long size, int byte_align,
467 struct amdgpu_bo *bo)
474 r = amdgpu_bo_do_create(adev, size, byte_align, AMDGPU_GEM_DOMAIN_GTT,
475 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
476 AMDGPU_GEM_CREATE_SHADOW,
478 bo->tbo.resv, &bo->shadow);
480 bo->shadow->parent = amdgpu_bo_ref(bo);
481 mutex_lock(&adev->shadow_list_lock);
482 list_add_tail(&bo->shadow_list, &adev->shadow_list);
483 mutex_unlock(&adev->shadow_list_lock);
489 int amdgpu_bo_create(struct amdgpu_device *adev, unsigned long size,
490 int byte_align, u32 domain,
491 u64 flags, enum ttm_bo_type type,
492 struct reservation_object *resv,
493 struct amdgpu_bo **bo_ptr)
495 uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
498 r = amdgpu_bo_do_create(adev, size, byte_align, domain,
499 parent_flags, type, resv, bo_ptr);
503 if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
505 WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
508 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
511 reservation_object_unlock((*bo_ptr)->tbo.resv);
514 amdgpu_bo_unref(bo_ptr);
520 int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
521 struct amdgpu_ring *ring,
522 struct amdgpu_bo *bo,
523 struct reservation_object *resv,
524 struct dma_fence **fence,
528 struct amdgpu_bo *shadow = bo->shadow;
529 uint64_t bo_addr, shadow_addr;
535 bo_addr = amdgpu_bo_gpu_offset(bo);
536 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
538 r = reservation_object_reserve_shared(bo->tbo.resv);
542 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
543 amdgpu_bo_size(bo), resv, fence,
546 amdgpu_bo_fence(bo, *fence, true);
552 int amdgpu_bo_validate(struct amdgpu_bo *bo)
554 struct ttm_operation_ctx ctx = { false, false };
561 domain = bo->preferred_domains;
564 amdgpu_ttm_placement_from_domain(bo, domain);
565 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
566 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
567 domain = bo->allowed_domains;
574 int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
575 struct amdgpu_ring *ring,
576 struct amdgpu_bo *bo,
577 struct reservation_object *resv,
578 struct dma_fence **fence,
582 struct amdgpu_bo *shadow = bo->shadow;
583 uint64_t bo_addr, shadow_addr;
589 bo_addr = amdgpu_bo_gpu_offset(bo);
590 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
592 r = reservation_object_reserve_shared(bo->tbo.resv);
596 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
597 amdgpu_bo_size(bo), resv, fence,
600 amdgpu_bo_fence(bo, *fence, true);
606 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
611 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
614 kptr = amdgpu_bo_kptr(bo);
621 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
622 MAX_SCHEDULE_TIMEOUT);
626 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
631 *ptr = amdgpu_bo_kptr(bo);
636 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
640 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
643 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
646 ttm_bo_kunmap(&bo->kmap);
649 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
654 ttm_bo_reference(&bo->tbo);
658 void amdgpu_bo_unref(struct amdgpu_bo **bo)
660 struct ttm_buffer_object *tbo;
671 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
672 u64 min_offset, u64 max_offset,
675 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
676 struct ttm_operation_ctx ctx = { false, false };
679 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
682 if (WARN_ON_ONCE(min_offset > max_offset))
685 /* A shared bo cannot be migrated to VRAM */
686 if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM))
690 uint32_t mem_type = bo->tbo.mem.mem_type;
692 if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
697 *gpu_addr = amdgpu_bo_gpu_offset(bo);
699 if (max_offset != 0) {
700 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
701 WARN_ON_ONCE(max_offset <
702 (amdgpu_bo_gpu_offset(bo) - domain_start));
708 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
709 /* force to pin into visible video ram */
710 if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
711 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
712 amdgpu_ttm_placement_from_domain(bo, domain);
713 for (i = 0; i < bo->placement.num_placement; i++) {
716 fpfn = min_offset >> PAGE_SHIFT;
717 lpfn = max_offset >> PAGE_SHIFT;
719 if (fpfn > bo->placements[i].fpfn)
720 bo->placements[i].fpfn = fpfn;
721 if (!bo->placements[i].lpfn ||
722 (lpfn && lpfn < bo->placements[i].lpfn))
723 bo->placements[i].lpfn = lpfn;
724 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
727 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
729 dev_err(adev->dev, "%p pin failed\n", bo);
733 r = amdgpu_ttm_alloc_gart(&bo->tbo);
735 dev_err(adev->dev, "%p bind failed\n", bo);
740 if (gpu_addr != NULL)
741 *gpu_addr = amdgpu_bo_gpu_offset(bo);
743 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
744 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
745 adev->vram_pin_size += amdgpu_bo_size(bo);
746 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
747 adev->invisible_pin_size += amdgpu_bo_size(bo);
748 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
749 adev->gart_pin_size += amdgpu_bo_size(bo);
756 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
758 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
761 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
763 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
764 struct ttm_operation_ctx ctx = { false, false };
767 if (!bo->pin_count) {
768 dev_warn(adev->dev, "%p unpin not necessary\n", bo);
774 for (i = 0; i < bo->placement.num_placement; i++) {
775 bo->placements[i].lpfn = 0;
776 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
778 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
780 dev_err(adev->dev, "%p validate failed for unpin\n", bo);
784 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
785 adev->vram_pin_size -= amdgpu_bo_size(bo);
786 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
787 adev->invisible_pin_size -= amdgpu_bo_size(bo);
788 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
789 adev->gart_pin_size -= amdgpu_bo_size(bo);
796 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
798 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
799 if (0 && (adev->flags & AMD_IS_APU)) {
800 /* Useless to evict on IGP chips */
803 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
806 static const char *amdgpu_vram_names[] = {
818 int amdgpu_bo_init(struct amdgpu_device *adev)
820 /* reserve PAT memory space to WC for VRAM */
821 arch_io_reserve_memtype_wc(adev->gmc.aper_base,
822 adev->gmc.aper_size);
824 /* Add an MTRR for the VRAM */
825 adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
826 adev->gmc.aper_size);
827 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
828 adev->gmc.mc_vram_size >> 20,
829 (unsigned long long)adev->gmc.aper_size >> 20);
830 DRM_INFO("RAM width %dbits %s\n",
831 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
832 return amdgpu_ttm_init(adev);
835 void amdgpu_bo_fini(struct amdgpu_device *adev)
837 amdgpu_ttm_fini(adev);
838 arch_phys_wc_del(adev->gmc.vram_mtrr);
839 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
842 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
843 struct vm_area_struct *vma)
845 return ttm_fbdev_mmap(vma, &bo->tbo);
848 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
850 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
852 if (adev->family <= AMDGPU_FAMILY_CZ &&
853 AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
856 bo->tiling_flags = tiling_flags;
860 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
862 lockdep_assert_held(&bo->tbo.resv->lock.base);
865 *tiling_flags = bo->tiling_flags;
868 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
869 uint32_t metadata_size, uint64_t flags)
873 if (!metadata_size) {
874 if (bo->metadata_size) {
877 bo->metadata_size = 0;
882 if (metadata == NULL)
885 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
890 bo->metadata_flags = flags;
891 bo->metadata = buffer;
892 bo->metadata_size = metadata_size;
897 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
898 size_t buffer_size, uint32_t *metadata_size,
901 if (!buffer && !metadata_size)
905 if (buffer_size < bo->metadata_size)
908 if (bo->metadata_size)
909 memcpy(buffer, bo->metadata, bo->metadata_size);
913 *metadata_size = bo->metadata_size;
915 *flags = bo->metadata_flags;
920 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
922 struct ttm_mem_reg *new_mem)
924 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
925 struct amdgpu_bo *abo;
926 struct ttm_mem_reg *old_mem = &bo->mem;
928 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
931 abo = ttm_to_amdgpu_bo(bo);
932 amdgpu_vm_bo_invalidate(adev, abo, evict);
934 amdgpu_bo_kunmap(abo);
936 /* remember the eviction */
938 atomic64_inc(&adev->num_evictions);
940 /* update statistics */
944 /* move_notify is called before move happens */
945 trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
948 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
950 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
951 struct ttm_operation_ctx ctx = { false, false };
952 struct amdgpu_bo *abo;
953 unsigned long offset, size;
956 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
959 abo = ttm_to_amdgpu_bo(bo);
961 /* Remember that this BO was accessed by the CPU */
962 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
964 if (bo->mem.mem_type != TTM_PL_VRAM)
967 size = bo->mem.num_pages << PAGE_SHIFT;
968 offset = bo->mem.start << PAGE_SHIFT;
969 if ((offset + size) <= adev->gmc.visible_vram_size)
972 /* Can't move a pinned BO to visible VRAM */
973 if (abo->pin_count > 0)
976 /* hurrah the memory is not visible ! */
977 atomic64_inc(&adev->num_vram_cpu_page_faults);
978 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
979 AMDGPU_GEM_DOMAIN_GTT);
981 /* Avoid costly evictions; only set GTT as a busy placement */
982 abo->placement.num_busy_placement = 1;
983 abo->placement.busy_placement = &abo->placements[1];
985 r = ttm_bo_validate(bo, &abo->placement, &ctx);
986 if (unlikely(r != 0))
989 offset = bo->mem.start << PAGE_SHIFT;
990 /* this should never happen */
991 if (bo->mem.mem_type == TTM_PL_VRAM &&
992 (offset + size) > adev->gmc.visible_vram_size)
999 * amdgpu_bo_fence - add fence to buffer object
1001 * @bo: buffer object in question
1002 * @fence: fence to add
1003 * @shared: true if fence should be added shared
1006 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
1009 struct reservation_object *resv = bo->tbo.resv;
1012 reservation_object_add_shared_fence(resv, fence);
1014 reservation_object_add_excl_fence(resv, fence);
1018 * amdgpu_bo_gpu_offset - return GPU offset of bo
1019 * @bo: amdgpu object for which we query the offset
1021 * Returns current GPU offset of the object.
1023 * Note: object should either be pinned or reserved when calling this
1024 * function, it might be useful to add check for this for debugging.
1026 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
1028 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
1029 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
1030 !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem));
1031 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
1033 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
1034 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
1035 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
1037 return bo->tbo.offset;