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"
42 static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
43 struct ttm_mem_reg *mem)
45 if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size)
48 return ((mem->start << PAGE_SHIFT) + mem->size) >
49 adev->mc.visible_vram_size ?
50 adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) :
54 static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
55 struct ttm_mem_reg *old_mem,
56 struct ttm_mem_reg *new_mem)
63 switch (new_mem->mem_type) {
65 atomic64_add(new_mem->size, &adev->gtt_usage);
68 atomic64_add(new_mem->size, &adev->vram_usage);
69 vis_size = amdgpu_get_vis_part_size(adev, new_mem);
70 atomic64_add(vis_size, &adev->vram_vis_usage);
76 switch (old_mem->mem_type) {
78 atomic64_sub(old_mem->size, &adev->gtt_usage);
81 atomic64_sub(old_mem->size, &adev->vram_usage);
82 vis_size = amdgpu_get_vis_part_size(adev, old_mem);
83 atomic64_sub(vis_size, &adev->vram_vis_usage);
89 static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
91 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
94 bo = container_of(tbo, struct amdgpu_bo, tbo);
96 amdgpu_update_memory_usage(adev, &bo->tbo.mem, NULL);
98 drm_gem_object_release(&bo->gem_base);
99 amdgpu_bo_unref(&bo->parent);
100 if (!list_empty(&bo->shadow_list)) {
101 mutex_lock(&adev->shadow_list_lock);
102 list_del_init(&bo->shadow_list);
103 mutex_unlock(&adev->shadow_list_lock);
109 bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
111 if (bo->destroy == &amdgpu_ttm_bo_destroy)
116 static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
117 struct ttm_placement *placement,
118 struct ttm_place *places,
119 u32 domain, u64 flags)
123 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
124 unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
127 /* This forces a reallocation if the flag wasn't set before */
128 if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
129 lpfn = adev->mc.real_vram_size >> PAGE_SHIFT;
131 if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS &&
132 !(flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
133 adev->mc.visible_vram_size < adev->mc.real_vram_size) {
134 places[c].fpfn = visible_pfn;
135 places[c].lpfn = lpfn;
136 places[c].flags = TTM_PL_FLAG_WC |
137 TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM |
143 places[c].lpfn = lpfn;
144 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
146 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
147 places[c].lpfn = visible_pfn;
149 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
153 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
156 places[c].flags = TTM_PL_FLAG_TT;
157 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
158 places[c].flags |= TTM_PL_FLAG_WC |
159 TTM_PL_FLAG_UNCACHED;
161 places[c].flags |= TTM_PL_FLAG_CACHED;
165 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
168 places[c].flags = TTM_PL_FLAG_SYSTEM;
169 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
170 places[c].flags |= TTM_PL_FLAG_WC |
171 TTM_PL_FLAG_UNCACHED;
173 places[c].flags |= TTM_PL_FLAG_CACHED;
177 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
180 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
184 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
187 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
191 if (domain & AMDGPU_GEM_DOMAIN_OA) {
194 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
201 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
205 placement->num_placement = c;
206 placement->placement = places;
208 placement->num_busy_placement = c;
209 placement->busy_placement = places;
212 void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
214 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
216 amdgpu_ttm_placement_init(adev, &abo->placement, abo->placements,
220 static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
221 struct ttm_placement *placement)
223 BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
225 memcpy(bo->placements, placement->placement,
226 placement->num_placement * sizeof(struct ttm_place));
227 bo->placement.num_placement = placement->num_placement;
228 bo->placement.num_busy_placement = placement->num_busy_placement;
229 bo->placement.placement = bo->placements;
230 bo->placement.busy_placement = bo->placements;
234 * amdgpu_bo_create_kernel - create BO for kernel use
236 * @adev: amdgpu device object
237 * @size: size for the new BO
238 * @align: alignment for the new BO
239 * @domain: where to place it
240 * @bo_ptr: resulting BO
241 * @gpu_addr: GPU addr of the pinned BO
242 * @cpu_addr: optional CPU address mapping
244 * Allocates and pins a BO for kernel internal use.
246 * Returns 0 on success, negative error code otherwise.
248 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
249 unsigned long size, int align,
250 u32 domain, struct amdgpu_bo **bo_ptr,
251 u64 *gpu_addr, void **cpu_addr)
255 r = amdgpu_bo_create(adev, size, align, true, domain,
256 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
257 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
260 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r);
264 r = amdgpu_bo_reserve(*bo_ptr, false);
266 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
270 r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
272 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
273 goto error_unreserve;
277 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
279 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
280 goto error_unreserve;
284 amdgpu_bo_unreserve(*bo_ptr);
289 amdgpu_bo_unreserve(*bo_ptr);
292 amdgpu_bo_unref(bo_ptr);
298 * amdgpu_bo_free_kernel - free BO for kernel use
300 * @bo: amdgpu BO to free
302 * unmaps and unpin a BO for kernel internal use.
304 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
310 if (likely(amdgpu_bo_reserve(*bo, false) == 0)) {
312 amdgpu_bo_kunmap(*bo);
314 amdgpu_bo_unpin(*bo);
315 amdgpu_bo_unreserve(*bo);
326 int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
327 unsigned long size, int byte_align,
328 bool kernel, u32 domain, u64 flags,
330 struct ttm_placement *placement,
331 struct reservation_object *resv,
332 struct amdgpu_bo **bo_ptr)
334 struct amdgpu_bo *bo;
335 enum ttm_bo_type type;
336 unsigned long page_align;
340 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
341 size = ALIGN(size, PAGE_SIZE);
344 type = ttm_bo_type_kernel;
346 type = ttm_bo_type_sg;
348 type = ttm_bo_type_device;
352 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
353 sizeof(struct amdgpu_bo));
355 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
358 r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
363 INIT_LIST_HEAD(&bo->shadow_list);
364 INIT_LIST_HEAD(&bo->va);
365 bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
366 AMDGPU_GEM_DOMAIN_GTT |
367 AMDGPU_GEM_DOMAIN_CPU |
368 AMDGPU_GEM_DOMAIN_GDS |
369 AMDGPU_GEM_DOMAIN_GWS |
370 AMDGPU_GEM_DOMAIN_OA);
371 bo->allowed_domains = bo->prefered_domains;
372 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
373 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
377 /* For architectures that don't support WC memory,
378 * mask out the WC flag from the BO
380 if (!drm_arch_can_wc_memory())
381 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
383 amdgpu_fill_placement_to_bo(bo, placement);
384 /* Kernel allocation are uninterruptible */
385 r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
386 &bo->placement, page_align, !kernel, NULL,
387 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
388 if (unlikely(r != 0)) {
392 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
393 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
396 if (adev->mman.buffer_funcs_ring == NULL ||
397 !adev->mman.buffer_funcs_ring->ready) {
402 r = amdgpu_bo_reserve(bo, false);
403 if (unlikely(r != 0))
406 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
407 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
408 if (unlikely(r != 0))
411 amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
412 amdgpu_bo_fence(bo, fence, false);
413 amdgpu_bo_unreserve(bo);
414 fence_put(bo->tbo.moving);
415 bo->tbo.moving = fence_get(fence);
420 trace_amdgpu_bo_create(bo);
425 amdgpu_bo_unreserve(bo);
427 amdgpu_bo_unref(&bo);
431 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
432 unsigned long size, int byte_align,
433 struct amdgpu_bo *bo)
435 struct ttm_placement placement = {0};
436 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
442 bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
443 memset(&placements, 0,
444 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
446 amdgpu_ttm_placement_init(adev, &placement,
447 placements, AMDGPU_GEM_DOMAIN_GTT,
448 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
450 r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
451 AMDGPU_GEM_DOMAIN_GTT,
452 AMDGPU_GEM_CREATE_CPU_GTT_USWC,
457 bo->shadow->parent = amdgpu_bo_ref(bo);
458 mutex_lock(&adev->shadow_list_lock);
459 list_add_tail(&bo->shadow_list, &adev->shadow_list);
460 mutex_unlock(&adev->shadow_list_lock);
466 int amdgpu_bo_create(struct amdgpu_device *adev,
467 unsigned long size, int byte_align,
468 bool kernel, u32 domain, u64 flags,
470 struct reservation_object *resv,
471 struct amdgpu_bo **bo_ptr)
473 struct ttm_placement placement = {0};
474 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
477 memset(&placements, 0,
478 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
480 amdgpu_ttm_placement_init(adev, &placement,
481 placements, domain, flags);
483 r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
484 domain, flags, sg, &placement,
489 if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
490 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
492 amdgpu_bo_unref(bo_ptr);
498 int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
499 struct amdgpu_ring *ring,
500 struct amdgpu_bo *bo,
501 struct reservation_object *resv,
502 struct fence **fence,
506 struct amdgpu_bo *shadow = bo->shadow;
507 uint64_t bo_addr, shadow_addr;
513 bo_addr = amdgpu_bo_gpu_offset(bo);
514 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
516 r = reservation_object_reserve_shared(bo->tbo.resv);
520 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
521 amdgpu_bo_size(bo), resv, fence,
524 amdgpu_bo_fence(bo, *fence, true);
530 int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
531 struct amdgpu_ring *ring,
532 struct amdgpu_bo *bo,
533 struct reservation_object *resv,
534 struct 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, shadow_addr, bo_addr,
553 amdgpu_bo_size(bo), resv, fence,
556 amdgpu_bo_fence(bo, *fence, true);
562 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
567 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
577 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
578 MAX_SCHEDULE_TIMEOUT);
582 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
586 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
593 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
595 if (bo->kptr == NULL)
598 ttm_bo_kunmap(&bo->kmap);
601 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
606 ttm_bo_reference(&bo->tbo);
610 void amdgpu_bo_unref(struct amdgpu_bo **bo)
612 struct ttm_buffer_object *tbo;
623 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
624 u64 min_offset, u64 max_offset,
627 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
631 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
634 if (WARN_ON_ONCE(min_offset > max_offset))
638 uint32_t mem_type = bo->tbo.mem.mem_type;
640 if (domain != amdgpu_mem_type_to_domain(mem_type))
645 *gpu_addr = amdgpu_bo_gpu_offset(bo);
647 if (max_offset != 0) {
648 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
649 WARN_ON_ONCE(max_offset <
650 (amdgpu_bo_gpu_offset(bo) - domain_start));
656 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
657 amdgpu_ttm_placement_from_domain(bo, domain);
658 for (i = 0; i < bo->placement.num_placement; i++) {
659 /* force to pin into visible video ram */
660 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
661 !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
662 (!max_offset || max_offset >
663 adev->mc.visible_vram_size)) {
664 if (WARN_ON_ONCE(min_offset >
665 adev->mc.visible_vram_size))
667 fpfn = min_offset >> PAGE_SHIFT;
668 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
670 fpfn = min_offset >> PAGE_SHIFT;
671 lpfn = max_offset >> PAGE_SHIFT;
673 if (fpfn > bo->placements[i].fpfn)
674 bo->placements[i].fpfn = fpfn;
675 if (!bo->placements[i].lpfn ||
676 (lpfn && lpfn < bo->placements[i].lpfn))
677 bo->placements[i].lpfn = lpfn;
678 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
681 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
683 dev_err(adev->dev, "%p pin failed\n", bo);
686 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
688 dev_err(adev->dev, "%p bind failed\n", bo);
693 if (gpu_addr != NULL)
694 *gpu_addr = amdgpu_bo_gpu_offset(bo);
695 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
696 adev->vram_pin_size += amdgpu_bo_size(bo);
697 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
698 adev->invisible_pin_size += amdgpu_bo_size(bo);
699 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
700 adev->gart_pin_size += amdgpu_bo_size(bo);
707 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
709 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
712 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
714 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
717 if (!bo->pin_count) {
718 dev_warn(adev->dev, "%p unpin not necessary\n", bo);
724 for (i = 0; i < bo->placement.num_placement; i++) {
725 bo->placements[i].lpfn = 0;
726 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
728 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
730 dev_err(adev->dev, "%p validate failed for unpin\n", bo);
734 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
735 adev->vram_pin_size -= amdgpu_bo_size(bo);
736 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
737 adev->invisible_pin_size -= amdgpu_bo_size(bo);
738 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
739 adev->gart_pin_size -= amdgpu_bo_size(bo);
746 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
748 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
749 if (0 && (adev->flags & AMD_IS_APU)) {
750 /* Useless to evict on IGP chips */
753 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
756 static const char *amdgpu_vram_names[] = {
767 int amdgpu_bo_init(struct amdgpu_device *adev)
769 /* Add an MTRR for the VRAM */
770 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
772 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
773 adev->mc.mc_vram_size >> 20,
774 (unsigned long long)adev->mc.aper_size >> 20);
775 DRM_INFO("RAM width %dbits %s\n",
776 adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
777 return amdgpu_ttm_init(adev);
780 void amdgpu_bo_fini(struct amdgpu_device *adev)
782 amdgpu_ttm_fini(adev);
783 arch_phys_wc_del(adev->mc.vram_mtrr);
786 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
787 struct vm_area_struct *vma)
789 return ttm_fbdev_mmap(vma, &bo->tbo);
792 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
794 if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
797 bo->tiling_flags = tiling_flags;
801 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
803 lockdep_assert_held(&bo->tbo.resv->lock.base);
806 *tiling_flags = bo->tiling_flags;
809 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
810 uint32_t metadata_size, uint64_t flags)
814 if (!metadata_size) {
815 if (bo->metadata_size) {
818 bo->metadata_size = 0;
823 if (metadata == NULL)
826 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
831 bo->metadata_flags = flags;
832 bo->metadata = buffer;
833 bo->metadata_size = metadata_size;
838 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
839 size_t buffer_size, uint32_t *metadata_size,
842 if (!buffer && !metadata_size)
846 if (buffer_size < bo->metadata_size)
849 if (bo->metadata_size)
850 memcpy(buffer, bo->metadata, bo->metadata_size);
854 *metadata_size = bo->metadata_size;
856 *flags = bo->metadata_flags;
861 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
862 struct ttm_mem_reg *new_mem)
864 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
865 struct amdgpu_bo *abo;
866 struct ttm_mem_reg *old_mem = &bo->mem;
868 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
871 abo = container_of(bo, struct amdgpu_bo, tbo);
872 amdgpu_vm_bo_invalidate(adev, abo);
874 /* update statistics */
878 /* move_notify is called before move happens */
879 amdgpu_update_memory_usage(adev, &bo->mem, new_mem);
881 trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
884 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
886 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
887 struct amdgpu_bo *abo;
888 unsigned long offset, size, lpfn;
891 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
894 abo = container_of(bo, struct amdgpu_bo, tbo);
895 if (bo->mem.mem_type != TTM_PL_VRAM)
898 size = bo->mem.num_pages << PAGE_SHIFT;
899 offset = bo->mem.start << PAGE_SHIFT;
900 /* TODO: figure out how to map scattered VRAM to the CPU */
901 if ((offset + size) <= adev->mc.visible_vram_size &&
902 (abo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS))
905 /* Can't move a pinned BO to visible VRAM */
906 if (abo->pin_count > 0)
909 /* hurrah the memory is not visible ! */
910 abo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
911 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
912 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
913 for (i = 0; i < abo->placement.num_placement; i++) {
914 /* Force into visible VRAM */
915 if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
916 (!abo->placements[i].lpfn ||
917 abo->placements[i].lpfn > lpfn))
918 abo->placements[i].lpfn = lpfn;
920 r = ttm_bo_validate(bo, &abo->placement, false, false);
921 if (unlikely(r == -ENOMEM)) {
922 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
923 return ttm_bo_validate(bo, &abo->placement, false, false);
924 } else if (unlikely(r != 0)) {
928 offset = bo->mem.start << PAGE_SHIFT;
929 /* this should never happen */
930 if ((offset + size) > adev->mc.visible_vram_size)
937 * amdgpu_bo_fence - add fence to buffer object
939 * @bo: buffer object in question
940 * @fence: fence to add
941 * @shared: true if fence should be added shared
944 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence,
947 struct reservation_object *resv = bo->tbo.resv;
950 reservation_object_add_shared_fence(resv, fence);
952 reservation_object_add_excl_fence(resv, fence);
956 * amdgpu_bo_gpu_offset - return GPU offset of bo
957 * @bo: amdgpu object for which we query the offset
959 * Returns current GPU offset of the object.
961 * Note: object should either be pinned or reserved when calling this
962 * function, it might be useful to add check for this for debugging.
964 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
966 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
967 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
968 !amdgpu_ttm_is_bound(bo->tbo.ttm));
969 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
971 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
972 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
973 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
975 return bo->tbo.offset;