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, true, domain,
195 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
196 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
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,
339 unsigned long size, int byte_align,
340 bool kernel, u32 domain, u64 flags,
342 struct reservation_object *resv,
343 struct amdgpu_bo **bo_ptr)
345 struct ttm_operation_ctx ctx = {
346 .interruptible = !kernel,
347 .no_wait_gpu = false,
349 .flags = TTM_OPT_FLAG_ALLOW_RES_EVICT
351 struct amdgpu_bo *bo;
352 enum ttm_bo_type type;
353 unsigned long page_align;
357 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
358 size = ALIGN(size, PAGE_SIZE);
360 if (!amdgpu_bo_validate_size(adev, size, domain))
364 type = ttm_bo_type_kernel;
366 type = ttm_bo_type_sg;
368 type = ttm_bo_type_device;
372 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
373 sizeof(struct amdgpu_bo));
375 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
378 drm_gem_private_object_init(adev->ddev, &bo->gem_base, size);
379 INIT_LIST_HEAD(&bo->shadow_list);
380 INIT_LIST_HEAD(&bo->va);
381 bo->preferred_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
382 AMDGPU_GEM_DOMAIN_GTT |
383 AMDGPU_GEM_DOMAIN_CPU |
384 AMDGPU_GEM_DOMAIN_GDS |
385 AMDGPU_GEM_DOMAIN_GWS |
386 AMDGPU_GEM_DOMAIN_OA);
387 bo->allowed_domains = bo->preferred_domains;
388 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
389 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
394 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
395 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
397 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
398 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
399 /* Don't try to enable write-combining when it can't work, or things
401 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
404 #ifndef CONFIG_COMPILE_TEST
405 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
406 thanks to write-combining
409 if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
410 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
411 "better performance thanks to write-combining\n");
412 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
414 /* For architectures that don't support WC memory,
415 * mask out the WC flag from the BO
417 if (!drm_arch_can_wc_memory())
418 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
421 bo->tbo.bdev = &adev->mman.bdev;
422 amdgpu_ttm_placement_from_domain(bo, domain);
424 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
425 &bo->placement, page_align, &ctx, acc_size,
426 sg, resv, &amdgpu_ttm_bo_destroy);
427 if (unlikely(r != 0))
430 if (adev->gmc.visible_vram_size < adev->gmc.real_vram_size &&
431 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
432 bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
433 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
436 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
439 bo->tbo.priority = 1;
441 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
442 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
443 struct dma_fence *fence;
445 r = amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
449 amdgpu_bo_fence(bo, fence, false);
450 dma_fence_put(bo->tbo.moving);
451 bo->tbo.moving = dma_fence_get(fence);
452 dma_fence_put(fence);
455 amdgpu_bo_unreserve(bo);
458 trace_amdgpu_bo_create(bo);
460 /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
461 if (type == ttm_bo_type_device)
462 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
468 ww_mutex_unlock(&bo->tbo.resv->lock);
469 amdgpu_bo_unref(&bo);
473 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
474 unsigned long size, int byte_align,
475 struct amdgpu_bo *bo)
482 r = amdgpu_bo_do_create(adev, size, byte_align, true,
483 AMDGPU_GEM_DOMAIN_GTT,
484 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
485 AMDGPU_GEM_CREATE_SHADOW,
489 bo->shadow->parent = amdgpu_bo_ref(bo);
490 mutex_lock(&adev->shadow_list_lock);
491 list_add_tail(&bo->shadow_list, &adev->shadow_list);
492 mutex_unlock(&adev->shadow_list_lock);
498 int amdgpu_bo_create(struct amdgpu_device *adev,
499 unsigned long size, int byte_align,
500 bool kernel, u32 domain, u64 flags,
502 struct reservation_object *resv,
503 struct amdgpu_bo **bo_ptr)
505 uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
508 r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain,
509 parent_flags, sg, resv, bo_ptr);
513 if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
515 WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
518 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
521 reservation_object_unlock((*bo_ptr)->tbo.resv);
524 amdgpu_bo_unref(bo_ptr);
530 int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
531 struct amdgpu_ring *ring,
532 struct amdgpu_bo *bo,
533 struct reservation_object *resv,
534 struct dma_fence **fence,
538 struct amdgpu_bo *shadow = bo->shadow;
539 uint64_t bo_addr, shadow_addr;
545 bo_addr = amdgpu_bo_gpu_offset(bo);
546 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
548 r = reservation_object_reserve_shared(bo->tbo.resv);
552 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
553 amdgpu_bo_size(bo), resv, fence,
556 amdgpu_bo_fence(bo, *fence, true);
562 int amdgpu_bo_validate(struct amdgpu_bo *bo)
564 struct ttm_operation_ctx ctx = { false, false };
571 domain = bo->preferred_domains;
574 amdgpu_ttm_placement_from_domain(bo, domain);
575 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
576 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
577 domain = bo->allowed_domains;
584 int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
585 struct amdgpu_ring *ring,
586 struct amdgpu_bo *bo,
587 struct reservation_object *resv,
588 struct dma_fence **fence,
592 struct amdgpu_bo *shadow = bo->shadow;
593 uint64_t bo_addr, shadow_addr;
599 bo_addr = amdgpu_bo_gpu_offset(bo);
600 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
602 r = reservation_object_reserve_shared(bo->tbo.resv);
606 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
607 amdgpu_bo_size(bo), resv, fence,
610 amdgpu_bo_fence(bo, *fence, true);
616 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
621 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
624 kptr = amdgpu_bo_kptr(bo);
631 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
632 MAX_SCHEDULE_TIMEOUT);
636 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
641 *ptr = amdgpu_bo_kptr(bo);
646 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
650 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
653 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
656 ttm_bo_kunmap(&bo->kmap);
659 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
664 ttm_bo_reference(&bo->tbo);
668 void amdgpu_bo_unref(struct amdgpu_bo **bo)
670 struct ttm_buffer_object *tbo;
681 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
682 u64 min_offset, u64 max_offset,
685 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
686 struct ttm_operation_ctx ctx = { false, false };
689 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
692 if (WARN_ON_ONCE(min_offset > max_offset))
695 /* A shared bo cannot be migrated to VRAM */
696 if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM))
700 uint32_t mem_type = bo->tbo.mem.mem_type;
702 if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
707 *gpu_addr = amdgpu_bo_gpu_offset(bo);
709 if (max_offset != 0) {
710 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
711 WARN_ON_ONCE(max_offset <
712 (amdgpu_bo_gpu_offset(bo) - domain_start));
718 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
719 /* force to pin into visible video ram */
720 if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
721 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
722 amdgpu_ttm_placement_from_domain(bo, domain);
723 for (i = 0; i < bo->placement.num_placement; i++) {
726 fpfn = min_offset >> PAGE_SHIFT;
727 lpfn = max_offset >> PAGE_SHIFT;
729 if (fpfn > bo->placements[i].fpfn)
730 bo->placements[i].fpfn = fpfn;
731 if (!bo->placements[i].lpfn ||
732 (lpfn && lpfn < bo->placements[i].lpfn))
733 bo->placements[i].lpfn = lpfn;
734 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
737 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
739 dev_err(adev->dev, "%p pin failed\n", bo);
743 r = amdgpu_ttm_alloc_gart(&bo->tbo);
745 dev_err(adev->dev, "%p bind failed\n", bo);
750 if (gpu_addr != NULL)
751 *gpu_addr = amdgpu_bo_gpu_offset(bo);
753 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
754 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
755 adev->vram_pin_size += amdgpu_bo_size(bo);
756 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
757 adev->invisible_pin_size += amdgpu_bo_size(bo);
758 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
759 adev->gart_pin_size += amdgpu_bo_size(bo);
766 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
768 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
771 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
773 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
774 struct ttm_operation_ctx ctx = { false, false };
777 if (!bo->pin_count) {
778 dev_warn(adev->dev, "%p unpin not necessary\n", bo);
784 for (i = 0; i < bo->placement.num_placement; i++) {
785 bo->placements[i].lpfn = 0;
786 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
788 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
790 dev_err(adev->dev, "%p validate failed for unpin\n", bo);
794 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
795 adev->vram_pin_size -= amdgpu_bo_size(bo);
796 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
797 adev->invisible_pin_size -= amdgpu_bo_size(bo);
798 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
799 adev->gart_pin_size -= amdgpu_bo_size(bo);
806 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
808 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
809 if (0 && (adev->flags & AMD_IS_APU)) {
810 /* Useless to evict on IGP chips */
813 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
816 static const char *amdgpu_vram_names[] = {
827 int amdgpu_bo_init(struct amdgpu_device *adev)
829 /* reserve PAT memory space to WC for VRAM */
830 arch_io_reserve_memtype_wc(adev->gmc.aper_base,
831 adev->gmc.aper_size);
833 /* Add an MTRR for the VRAM */
834 adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
835 adev->gmc.aper_size);
836 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
837 adev->gmc.mc_vram_size >> 20,
838 (unsigned long long)adev->gmc.aper_size >> 20);
839 DRM_INFO("RAM width %dbits %s\n",
840 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
841 return amdgpu_ttm_init(adev);
844 void amdgpu_bo_fini(struct amdgpu_device *adev)
846 amdgpu_ttm_fini(adev);
847 arch_phys_wc_del(adev->gmc.vram_mtrr);
848 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
851 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
852 struct vm_area_struct *vma)
854 return ttm_fbdev_mmap(vma, &bo->tbo);
857 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
859 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
861 if (adev->family <= AMDGPU_FAMILY_CZ &&
862 AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
865 bo->tiling_flags = tiling_flags;
869 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
871 lockdep_assert_held(&bo->tbo.resv->lock.base);
874 *tiling_flags = bo->tiling_flags;
877 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
878 uint32_t metadata_size, uint64_t flags)
882 if (!metadata_size) {
883 if (bo->metadata_size) {
886 bo->metadata_size = 0;
891 if (metadata == NULL)
894 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
899 bo->metadata_flags = flags;
900 bo->metadata = buffer;
901 bo->metadata_size = metadata_size;
906 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
907 size_t buffer_size, uint32_t *metadata_size,
910 if (!buffer && !metadata_size)
914 if (buffer_size < bo->metadata_size)
917 if (bo->metadata_size)
918 memcpy(buffer, bo->metadata, bo->metadata_size);
922 *metadata_size = bo->metadata_size;
924 *flags = bo->metadata_flags;
929 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
931 struct ttm_mem_reg *new_mem)
933 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
934 struct amdgpu_bo *abo;
935 struct ttm_mem_reg *old_mem = &bo->mem;
937 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
940 abo = ttm_to_amdgpu_bo(bo);
941 amdgpu_vm_bo_invalidate(adev, abo, evict);
943 amdgpu_bo_kunmap(abo);
945 /* remember the eviction */
947 atomic64_inc(&adev->num_evictions);
949 /* update statistics */
953 /* move_notify is called before move happens */
954 trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
957 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
959 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
960 struct ttm_operation_ctx ctx = { false, false };
961 struct amdgpu_bo *abo;
962 unsigned long offset, size;
965 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
968 abo = ttm_to_amdgpu_bo(bo);
970 /* Remember that this BO was accessed by the CPU */
971 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
973 if (bo->mem.mem_type != TTM_PL_VRAM)
976 size = bo->mem.num_pages << PAGE_SHIFT;
977 offset = bo->mem.start << PAGE_SHIFT;
978 if ((offset + size) <= adev->gmc.visible_vram_size)
981 /* Can't move a pinned BO to visible VRAM */
982 if (abo->pin_count > 0)
985 /* hurrah the memory is not visible ! */
986 atomic64_inc(&adev->num_vram_cpu_page_faults);
987 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
988 AMDGPU_GEM_DOMAIN_GTT);
990 /* Avoid costly evictions; only set GTT as a busy placement */
991 abo->placement.num_busy_placement = 1;
992 abo->placement.busy_placement = &abo->placements[1];
994 r = ttm_bo_validate(bo, &abo->placement, &ctx);
995 if (unlikely(r != 0))
998 offset = bo->mem.start << PAGE_SHIFT;
999 /* this should never happen */
1000 if (bo->mem.mem_type == TTM_PL_VRAM &&
1001 (offset + size) > adev->gmc.visible_vram_size)
1008 * amdgpu_bo_fence - add fence to buffer object
1010 * @bo: buffer object in question
1011 * @fence: fence to add
1012 * @shared: true if fence should be added shared
1015 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
1018 struct reservation_object *resv = bo->tbo.resv;
1021 reservation_object_add_shared_fence(resv, fence);
1023 reservation_object_add_excl_fence(resv, fence);
1027 * amdgpu_bo_gpu_offset - return GPU offset of bo
1028 * @bo: amdgpu object for which we query the offset
1030 * Returns current GPU offset of the object.
1032 * Note: object should either be pinned or reserved when calling this
1033 * function, it might be useful to add check for this for debugging.
1035 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
1037 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
1038 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
1039 !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem));
1040 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
1042 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
1043 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
1044 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
1046 return bo->tbo.offset;