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"
40 static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
42 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
45 bo = container_of(tbo, struct amdgpu_bo, tbo);
49 drm_gem_object_release(&bo->gem_base);
50 amdgpu_bo_unref(&bo->parent);
51 if (!list_empty(&bo->shadow_list)) {
52 mutex_lock(&adev->shadow_list_lock);
53 list_del_init(&bo->shadow_list);
54 mutex_unlock(&adev->shadow_list_lock);
60 bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
62 if (bo->destroy == &amdgpu_ttm_bo_destroy)
67 void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
69 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
70 struct ttm_placement *placement = &abo->placement;
71 struct ttm_place *places = abo->placements;
72 u64 flags = abo->flags;
75 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
76 unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
80 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
83 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
84 places[c].lpfn = visible_pfn;
86 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
88 if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
89 places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
93 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
95 if (flags & AMDGPU_GEM_CREATE_SHADOW)
96 places[c].lpfn = adev->mc.gart_size >> PAGE_SHIFT;
99 places[c].flags = TTM_PL_FLAG_TT;
100 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
101 places[c].flags |= TTM_PL_FLAG_WC |
102 TTM_PL_FLAG_UNCACHED;
104 places[c].flags |= TTM_PL_FLAG_CACHED;
108 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
111 places[c].flags = TTM_PL_FLAG_SYSTEM;
112 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
113 places[c].flags |= TTM_PL_FLAG_WC |
114 TTM_PL_FLAG_UNCACHED;
116 places[c].flags |= TTM_PL_FLAG_CACHED;
120 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
123 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
127 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
130 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
134 if (domain & AMDGPU_GEM_DOMAIN_OA) {
137 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
144 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
148 placement->num_placement = c;
149 placement->placement = places;
151 placement->num_busy_placement = c;
152 placement->busy_placement = places;
156 * amdgpu_bo_create_reserved - create reserved BO for kernel use
158 * @adev: amdgpu device object
159 * @size: size for the new BO
160 * @align: alignment for the new BO
161 * @domain: where to place it
162 * @bo_ptr: resulting BO
163 * @gpu_addr: GPU addr of the pinned BO
164 * @cpu_addr: optional CPU address mapping
166 * Allocates and pins a BO for kernel internal use, and returns it still
169 * Returns 0 on success, negative error code otherwise.
171 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
172 unsigned long size, int align,
173 u32 domain, struct amdgpu_bo **bo_ptr,
174 u64 *gpu_addr, void **cpu_addr)
180 r = amdgpu_bo_create(adev, size, align, true, domain,
181 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
182 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
183 NULL, NULL, 0, bo_ptr);
185 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
192 r = amdgpu_bo_reserve(*bo_ptr, false);
194 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
198 r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
200 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
201 goto error_unreserve;
205 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
207 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
208 goto error_unreserve;
215 amdgpu_bo_unreserve(*bo_ptr);
219 amdgpu_bo_unref(bo_ptr);
225 * amdgpu_bo_create_kernel - create BO for kernel use
227 * @adev: amdgpu device object
228 * @size: size for the new BO
229 * @align: alignment for the new BO
230 * @domain: where to place it
231 * @bo_ptr: resulting BO
232 * @gpu_addr: GPU addr of the pinned BO
233 * @cpu_addr: optional CPU address mapping
235 * Allocates and pins a BO for kernel internal use.
237 * Returns 0 on success, negative error code otherwise.
239 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
240 unsigned long size, int align,
241 u32 domain, struct amdgpu_bo **bo_ptr,
242 u64 *gpu_addr, void **cpu_addr)
246 r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
252 amdgpu_bo_unreserve(*bo_ptr);
258 * amdgpu_bo_free_kernel - free BO for kernel use
260 * @bo: amdgpu BO to free
262 * unmaps and unpin a BO for kernel internal use.
264 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
270 if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
272 amdgpu_bo_kunmap(*bo);
274 amdgpu_bo_unpin(*bo);
275 amdgpu_bo_unreserve(*bo);
286 static int amdgpu_bo_do_create(struct amdgpu_device *adev,
287 unsigned long size, int byte_align,
288 bool kernel, u32 domain, u64 flags,
290 struct reservation_object *resv,
292 struct amdgpu_bo **bo_ptr)
294 struct amdgpu_bo *bo;
295 enum ttm_bo_type type;
296 unsigned long page_align;
297 u64 initial_bytes_moved, bytes_moved;
301 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
302 size = ALIGN(size, PAGE_SIZE);
305 type = ttm_bo_type_kernel;
307 type = ttm_bo_type_sg;
309 type = ttm_bo_type_device;
313 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
314 sizeof(struct amdgpu_bo));
316 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
319 r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
324 INIT_LIST_HEAD(&bo->shadow_list);
325 INIT_LIST_HEAD(&bo->va);
326 bo->preferred_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
327 AMDGPU_GEM_DOMAIN_GTT |
328 AMDGPU_GEM_DOMAIN_CPU |
329 AMDGPU_GEM_DOMAIN_GDS |
330 AMDGPU_GEM_DOMAIN_GWS |
331 AMDGPU_GEM_DOMAIN_OA);
332 bo->allowed_domains = bo->preferred_domains;
333 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
334 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
339 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
340 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
342 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
343 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
344 /* Don't try to enable write-combining when it can't work, or things
346 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
349 #ifndef CONFIG_COMPILE_TEST
350 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
351 thanks to write-combining
354 if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
355 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
356 "better performance thanks to write-combining\n");
357 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
359 /* For architectures that don't support WC memory,
360 * mask out the WC flag from the BO
362 if (!drm_arch_can_wc_memory())
363 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
366 bo->tbo.bdev = &adev->mman.bdev;
367 amdgpu_ttm_placement_from_domain(bo, domain);
369 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
370 /* Kernel allocation are uninterruptible */
371 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
372 &bo->placement, page_align, !kernel, NULL,
373 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
374 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
376 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
377 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
378 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT)
379 amdgpu_cs_report_moved_bytes(adev, bytes_moved, bytes_moved);
381 amdgpu_cs_report_moved_bytes(adev, bytes_moved, 0);
383 if (unlikely(r != 0))
387 bo->tbo.priority = 1;
389 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
390 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
391 struct dma_fence *fence;
393 r = amdgpu_fill_buffer(bo, init_value, bo->tbo.resv, &fence);
397 amdgpu_bo_fence(bo, fence, false);
398 dma_fence_put(bo->tbo.moving);
399 bo->tbo.moving = dma_fence_get(fence);
400 dma_fence_put(fence);
403 amdgpu_bo_unreserve(bo);
406 trace_amdgpu_bo_create(bo);
408 /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
409 if (type == ttm_bo_type_device)
410 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
416 ww_mutex_unlock(&bo->tbo.resv->lock);
417 amdgpu_bo_unref(&bo);
421 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
422 unsigned long size, int byte_align,
423 struct amdgpu_bo *bo)
430 r = amdgpu_bo_do_create(adev, size, byte_align, true,
431 AMDGPU_GEM_DOMAIN_GTT,
432 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
433 AMDGPU_GEM_CREATE_SHADOW,
434 NULL, bo->tbo.resv, 0,
437 bo->shadow->parent = amdgpu_bo_ref(bo);
438 mutex_lock(&adev->shadow_list_lock);
439 list_add_tail(&bo->shadow_list, &adev->shadow_list);
440 mutex_unlock(&adev->shadow_list_lock);
446 /* init_value will only take effect when flags contains
447 * AMDGPU_GEM_CREATE_VRAM_CLEARED.
449 int amdgpu_bo_create(struct amdgpu_device *adev,
450 unsigned long size, int byte_align,
451 bool kernel, u32 domain, u64 flags,
453 struct reservation_object *resv,
455 struct amdgpu_bo **bo_ptr)
457 uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
460 r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain,
461 parent_flags, sg, resv, init_value, bo_ptr);
465 if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
467 WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
470 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
473 reservation_object_unlock((*bo_ptr)->tbo.resv);
476 amdgpu_bo_unref(bo_ptr);
482 int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
483 struct amdgpu_ring *ring,
484 struct amdgpu_bo *bo,
485 struct reservation_object *resv,
486 struct dma_fence **fence,
490 struct amdgpu_bo *shadow = bo->shadow;
491 uint64_t bo_addr, shadow_addr;
497 bo_addr = amdgpu_bo_gpu_offset(bo);
498 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
500 r = reservation_object_reserve_shared(bo->tbo.resv);
504 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
505 amdgpu_bo_size(bo), resv, fence,
508 amdgpu_bo_fence(bo, *fence, true);
514 int amdgpu_bo_validate(struct amdgpu_bo *bo)
522 domain = bo->preferred_domains;
525 amdgpu_ttm_placement_from_domain(bo, domain);
526 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
527 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
528 domain = bo->allowed_domains;
535 int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
536 struct amdgpu_ring *ring,
537 struct amdgpu_bo *bo,
538 struct reservation_object *resv,
539 struct dma_fence **fence,
543 struct amdgpu_bo *shadow = bo->shadow;
544 uint64_t bo_addr, shadow_addr;
550 bo_addr = amdgpu_bo_gpu_offset(bo);
551 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
553 r = reservation_object_reserve_shared(bo->tbo.resv);
557 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
558 amdgpu_bo_size(bo), resv, fence,
561 amdgpu_bo_fence(bo, *fence, true);
567 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
572 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
575 kptr = amdgpu_bo_kptr(bo);
582 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
583 MAX_SCHEDULE_TIMEOUT);
587 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
592 *ptr = amdgpu_bo_kptr(bo);
597 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
601 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
604 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
607 ttm_bo_kunmap(&bo->kmap);
610 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
615 ttm_bo_reference(&bo->tbo);
619 void amdgpu_bo_unref(struct amdgpu_bo **bo)
621 struct ttm_buffer_object *tbo;
632 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
633 u64 min_offset, u64 max_offset,
636 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
639 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
642 if (WARN_ON_ONCE(min_offset > max_offset))
645 /* A shared bo cannot be migrated to VRAM */
646 if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM))
650 uint32_t mem_type = bo->tbo.mem.mem_type;
652 if (domain != amdgpu_mem_type_to_domain(mem_type))
657 *gpu_addr = amdgpu_bo_gpu_offset(bo);
659 if (max_offset != 0) {
660 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
661 WARN_ON_ONCE(max_offset <
662 (amdgpu_bo_gpu_offset(bo) - domain_start));
668 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
669 /* force to pin into visible video ram */
670 if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
671 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
672 amdgpu_ttm_placement_from_domain(bo, domain);
673 for (i = 0; i < bo->placement.num_placement; i++) {
676 fpfn = min_offset >> PAGE_SHIFT;
677 lpfn = max_offset >> PAGE_SHIFT;
679 if (fpfn > bo->placements[i].fpfn)
680 bo->placements[i].fpfn = fpfn;
681 if (!bo->placements[i].lpfn ||
682 (lpfn && lpfn < bo->placements[i].lpfn))
683 bo->placements[i].lpfn = lpfn;
684 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
687 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
689 dev_err(adev->dev, "%p pin failed\n", bo);
694 if (gpu_addr != NULL) {
695 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
697 dev_err(adev->dev, "%p bind failed\n", bo);
700 *gpu_addr = amdgpu_bo_gpu_offset(bo);
702 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
703 adev->vram_pin_size += amdgpu_bo_size(bo);
704 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
705 adev->invisible_pin_size += amdgpu_bo_size(bo);
706 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
707 adev->gart_pin_size += amdgpu_bo_size(bo);
714 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
716 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
719 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
721 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
724 if (!bo->pin_count) {
725 dev_warn(adev->dev, "%p unpin not necessary\n", bo);
731 for (i = 0; i < bo->placement.num_placement; i++) {
732 bo->placements[i].lpfn = 0;
733 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
735 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
737 dev_err(adev->dev, "%p validate failed for unpin\n", bo);
741 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
742 adev->vram_pin_size -= amdgpu_bo_size(bo);
743 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
744 adev->invisible_pin_size -= amdgpu_bo_size(bo);
745 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
746 adev->gart_pin_size -= amdgpu_bo_size(bo);
753 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
755 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
756 if (0 && (adev->flags & AMD_IS_APU)) {
757 /* Useless to evict on IGP chips */
760 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
763 static const char *amdgpu_vram_names[] = {
774 int amdgpu_bo_init(struct amdgpu_device *adev)
776 /* reserve PAT memory space to WC for VRAM */
777 arch_io_reserve_memtype_wc(adev->mc.aper_base,
780 /* Add an MTRR for the VRAM */
781 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
783 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
784 adev->mc.mc_vram_size >> 20,
785 (unsigned long long)adev->mc.aper_size >> 20);
786 DRM_INFO("RAM width %dbits %s\n",
787 adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
788 return amdgpu_ttm_init(adev);
791 void amdgpu_bo_fini(struct amdgpu_device *adev)
793 amdgpu_ttm_fini(adev);
794 arch_phys_wc_del(adev->mc.vram_mtrr);
795 arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.aper_size);
798 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
799 struct vm_area_struct *vma)
801 return ttm_fbdev_mmap(vma, &bo->tbo);
804 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
806 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
808 if (adev->family <= AMDGPU_FAMILY_CZ &&
809 AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
812 bo->tiling_flags = tiling_flags;
816 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
818 lockdep_assert_held(&bo->tbo.resv->lock.base);
821 *tiling_flags = bo->tiling_flags;
824 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
825 uint32_t metadata_size, uint64_t flags)
829 if (!metadata_size) {
830 if (bo->metadata_size) {
833 bo->metadata_size = 0;
838 if (metadata == NULL)
841 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
846 bo->metadata_flags = flags;
847 bo->metadata = buffer;
848 bo->metadata_size = metadata_size;
853 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
854 size_t buffer_size, uint32_t *metadata_size,
857 if (!buffer && !metadata_size)
861 if (buffer_size < bo->metadata_size)
864 if (bo->metadata_size)
865 memcpy(buffer, bo->metadata, bo->metadata_size);
869 *metadata_size = bo->metadata_size;
871 *flags = bo->metadata_flags;
876 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
878 struct ttm_mem_reg *new_mem)
880 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
881 struct amdgpu_bo *abo;
882 struct ttm_mem_reg *old_mem = &bo->mem;
884 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
887 abo = container_of(bo, struct amdgpu_bo, tbo);
888 amdgpu_vm_bo_invalidate(adev, abo, evict);
890 amdgpu_bo_kunmap(abo);
892 /* remember the eviction */
894 atomic64_inc(&adev->num_evictions);
896 /* update statistics */
900 /* move_notify is called before move happens */
901 trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
904 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
906 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
907 struct amdgpu_bo *abo;
908 unsigned long offset, size;
911 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
914 abo = container_of(bo, struct amdgpu_bo, tbo);
916 /* Remember that this BO was accessed by the CPU */
917 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
919 if (bo->mem.mem_type != TTM_PL_VRAM)
922 size = bo->mem.num_pages << PAGE_SHIFT;
923 offset = bo->mem.start << PAGE_SHIFT;
924 if ((offset + size) <= adev->mc.visible_vram_size)
927 /* Can't move a pinned BO to visible VRAM */
928 if (abo->pin_count > 0)
931 /* hurrah the memory is not visible ! */
932 atomic64_inc(&adev->num_vram_cpu_page_faults);
933 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
934 AMDGPU_GEM_DOMAIN_GTT);
936 /* Avoid costly evictions; only set GTT as a busy placement */
937 abo->placement.num_busy_placement = 1;
938 abo->placement.busy_placement = &abo->placements[1];
940 r = ttm_bo_validate(bo, &abo->placement, false, false);
941 if (unlikely(r != 0))
944 offset = bo->mem.start << PAGE_SHIFT;
945 /* this should never happen */
946 if (bo->mem.mem_type == TTM_PL_VRAM &&
947 (offset + size) > adev->mc.visible_vram_size)
954 * amdgpu_bo_fence - add fence to buffer object
956 * @bo: buffer object in question
957 * @fence: fence to add
958 * @shared: true if fence should be added shared
961 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
964 struct reservation_object *resv = bo->tbo.resv;
967 reservation_object_add_shared_fence(resv, fence);
969 reservation_object_add_excl_fence(resv, fence);
973 * amdgpu_bo_gpu_offset - return GPU offset of bo
974 * @bo: amdgpu object for which we query the offset
976 * Returns current GPU offset of the object.
978 * Note: object should either be pinned or reserved when calling this
979 * function, it might be useful to add check for this for debugging.
981 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
983 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
984 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
985 !amdgpu_ttm_is_bound(bo->tbo.ttm));
986 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
988 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
989 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
990 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
992 return bo->tbo.offset;