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 static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
68 struct ttm_placement *placement,
69 struct ttm_place *places,
70 u32 domain, u64 flags)
74 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
75 unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
79 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
82 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
83 places[c].lpfn = visible_pfn;
85 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
87 if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
88 places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
92 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
95 places[c].flags = TTM_PL_FLAG_TT;
96 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
97 places[c].flags |= TTM_PL_FLAG_WC |
100 places[c].flags |= TTM_PL_FLAG_CACHED;
104 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
107 places[c].flags = TTM_PL_FLAG_SYSTEM;
108 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
109 places[c].flags |= TTM_PL_FLAG_WC |
110 TTM_PL_FLAG_UNCACHED;
112 places[c].flags |= TTM_PL_FLAG_CACHED;
116 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
119 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
123 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
126 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
130 if (domain & AMDGPU_GEM_DOMAIN_OA) {
133 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
140 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
144 placement->num_placement = c;
145 placement->placement = places;
147 placement->num_busy_placement = c;
148 placement->busy_placement = places;
151 void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
153 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
155 amdgpu_ttm_placement_init(adev, &abo->placement, abo->placements,
159 static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
160 struct ttm_placement *placement)
162 BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
164 memcpy(bo->placements, placement->placement,
165 placement->num_placement * sizeof(struct ttm_place));
166 bo->placement.num_placement = placement->num_placement;
167 bo->placement.num_busy_placement = placement->num_busy_placement;
168 bo->placement.placement = bo->placements;
169 bo->placement.busy_placement = bo->placements;
173 * amdgpu_bo_create_reserved - create reserved BO for kernel use
175 * @adev: amdgpu device object
176 * @size: size for the new BO
177 * @align: alignment for the new BO
178 * @domain: where to place it
179 * @bo_ptr: resulting BO
180 * @gpu_addr: GPU addr of the pinned BO
181 * @cpu_addr: optional CPU address mapping
183 * Allocates and pins a BO for kernel internal use, and returns it still
186 * Returns 0 on success, negative error code otherwise.
188 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
189 unsigned long size, int align,
190 u32 domain, struct amdgpu_bo **bo_ptr,
191 u64 *gpu_addr, void **cpu_addr)
197 r = amdgpu_bo_create(adev, size, align, true, domain,
198 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
199 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
200 NULL, NULL, 0, bo_ptr);
202 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
209 r = amdgpu_bo_reserve(*bo_ptr, false);
211 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
215 r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
217 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
218 goto error_unreserve;
222 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
224 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
225 goto error_unreserve;
232 amdgpu_bo_unreserve(*bo_ptr);
236 amdgpu_bo_unref(bo_ptr);
242 * amdgpu_bo_create_kernel - create BO for kernel use
244 * @adev: amdgpu device object
245 * @size: size for the new BO
246 * @align: alignment for the new BO
247 * @domain: where to place it
248 * @bo_ptr: resulting BO
249 * @gpu_addr: GPU addr of the pinned BO
250 * @cpu_addr: optional CPU address mapping
252 * Allocates and pins a BO for kernel internal use.
254 * Returns 0 on success, negative error code otherwise.
256 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
257 unsigned long size, int align,
258 u32 domain, struct amdgpu_bo **bo_ptr,
259 u64 *gpu_addr, void **cpu_addr)
263 r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
269 amdgpu_bo_unreserve(*bo_ptr);
275 * amdgpu_bo_free_kernel - free BO for kernel use
277 * @bo: amdgpu BO to free
279 * unmaps and unpin a BO for kernel internal use.
281 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
287 if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
289 amdgpu_bo_kunmap(*bo);
291 amdgpu_bo_unpin(*bo);
292 amdgpu_bo_unreserve(*bo);
303 int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
304 unsigned long size, int byte_align,
305 bool kernel, u32 domain, u64 flags,
307 struct ttm_placement *placement,
308 struct reservation_object *resv,
310 struct amdgpu_bo **bo_ptr)
312 struct amdgpu_bo *bo;
313 enum ttm_bo_type type;
314 unsigned long page_align;
315 u64 initial_bytes_moved, bytes_moved;
319 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
320 size = ALIGN(size, PAGE_SIZE);
323 type = ttm_bo_type_kernel;
325 type = ttm_bo_type_sg;
327 type = ttm_bo_type_device;
331 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
332 sizeof(struct amdgpu_bo));
334 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
337 r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
342 INIT_LIST_HEAD(&bo->shadow_list);
343 INIT_LIST_HEAD(&bo->va);
344 bo->preferred_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
345 AMDGPU_GEM_DOMAIN_GTT |
346 AMDGPU_GEM_DOMAIN_CPU |
347 AMDGPU_GEM_DOMAIN_GDS |
348 AMDGPU_GEM_DOMAIN_GWS |
349 AMDGPU_GEM_DOMAIN_OA);
350 bo->allowed_domains = bo->preferred_domains;
351 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
352 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
357 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
358 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
360 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
361 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
362 /* Don't try to enable write-combining when it can't work, or things
364 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
367 #ifndef CONFIG_COMPILE_TEST
368 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
369 thanks to write-combining
372 if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
373 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
374 "better performance thanks to write-combining\n");
375 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
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;
384 amdgpu_fill_placement_to_bo(bo, placement);
385 /* Kernel allocation are uninterruptible */
387 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
388 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type,
389 &bo->placement, page_align, !kernel, NULL,
390 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
391 bytes_moved = atomic64_read(&adev->num_bytes_moved) -
393 if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
394 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
395 bo->tbo.mem.start < adev->mc.visible_vram_size >> PAGE_SHIFT)
396 amdgpu_cs_report_moved_bytes(adev, bytes_moved, bytes_moved);
398 amdgpu_cs_report_moved_bytes(adev, bytes_moved, 0);
400 if (unlikely(r != 0))
404 bo->tbo.priority = 1;
406 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
407 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
408 struct dma_fence *fence;
410 r = amdgpu_fill_buffer(bo, init_value, bo->tbo.resv, &fence);
414 amdgpu_bo_fence(bo, fence, false);
415 dma_fence_put(bo->tbo.moving);
416 bo->tbo.moving = dma_fence_get(fence);
417 dma_fence_put(fence);
420 amdgpu_bo_unreserve(bo);
423 trace_amdgpu_bo_create(bo);
425 /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
426 if (type == ttm_bo_type_device)
427 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
433 ww_mutex_unlock(&bo->tbo.resv->lock);
434 amdgpu_bo_unref(&bo);
438 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
439 unsigned long size, int byte_align,
440 struct amdgpu_bo *bo)
442 struct ttm_placement placement = {0};
443 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
449 bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
450 memset(&placements, 0,
451 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
453 amdgpu_ttm_placement_init(adev, &placement,
454 placements, AMDGPU_GEM_DOMAIN_GTT,
455 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
457 r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
458 AMDGPU_GEM_DOMAIN_GTT,
459 AMDGPU_GEM_CREATE_CPU_GTT_USWC,
465 bo->shadow->parent = amdgpu_bo_ref(bo);
466 mutex_lock(&adev->shadow_list_lock);
467 list_add_tail(&bo->shadow_list, &adev->shadow_list);
468 mutex_unlock(&adev->shadow_list_lock);
474 /* init_value will only take effect when flags contains
475 * AMDGPU_GEM_CREATE_VRAM_CLEARED.
477 int amdgpu_bo_create(struct amdgpu_device *adev,
478 unsigned long size, int byte_align,
479 bool kernel, u32 domain, u64 flags,
481 struct reservation_object *resv,
483 struct amdgpu_bo **bo_ptr)
485 struct ttm_placement placement = {0};
486 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
489 memset(&placements, 0,
490 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
492 amdgpu_ttm_placement_init(adev, &placement,
493 placements, domain, flags);
495 r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
496 domain, flags, sg, &placement,
497 resv, init_value, bo_ptr);
501 if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
503 r = ww_mutex_lock(&(*bo_ptr)->tbo.resv->lock, NULL);
507 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
510 ww_mutex_unlock(&(*bo_ptr)->tbo.resv->lock);
513 amdgpu_bo_unref(bo_ptr);
519 int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
520 struct amdgpu_ring *ring,
521 struct amdgpu_bo *bo,
522 struct reservation_object *resv,
523 struct dma_fence **fence,
527 struct amdgpu_bo *shadow = bo->shadow;
528 uint64_t bo_addr, shadow_addr;
534 bo_addr = amdgpu_bo_gpu_offset(bo);
535 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
537 r = reservation_object_reserve_shared(bo->tbo.resv);
541 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
542 amdgpu_bo_size(bo), resv, fence,
545 amdgpu_bo_fence(bo, *fence, true);
551 int amdgpu_bo_validate(struct amdgpu_bo *bo)
559 domain = bo->preferred_domains;
562 amdgpu_ttm_placement_from_domain(bo, domain);
563 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
564 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
565 domain = bo->allowed_domains;
572 int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
573 struct amdgpu_ring *ring,
574 struct amdgpu_bo *bo,
575 struct reservation_object *resv,
576 struct dma_fence **fence,
580 struct amdgpu_bo *shadow = bo->shadow;
581 uint64_t bo_addr, shadow_addr;
587 bo_addr = amdgpu_bo_gpu_offset(bo);
588 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
590 r = reservation_object_reserve_shared(bo->tbo.resv);
594 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
595 amdgpu_bo_size(bo), resv, fence,
598 amdgpu_bo_fence(bo, *fence, true);
604 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
609 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
612 kptr = amdgpu_bo_kptr(bo);
619 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
620 MAX_SCHEDULE_TIMEOUT);
624 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
629 *ptr = amdgpu_bo_kptr(bo);
634 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
638 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
641 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
644 ttm_bo_kunmap(&bo->kmap);
647 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
652 ttm_bo_reference(&bo->tbo);
656 void amdgpu_bo_unref(struct amdgpu_bo **bo)
658 struct ttm_buffer_object *tbo;
669 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
670 u64 min_offset, u64 max_offset,
673 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
677 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
680 if (WARN_ON_ONCE(min_offset > max_offset))
683 /* A shared bo cannot be migrated to VRAM */
684 if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM))
688 uint32_t mem_type = bo->tbo.mem.mem_type;
690 if (domain != amdgpu_mem_type_to_domain(mem_type))
695 *gpu_addr = amdgpu_bo_gpu_offset(bo);
697 if (max_offset != 0) {
698 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
699 WARN_ON_ONCE(max_offset <
700 (amdgpu_bo_gpu_offset(bo) - domain_start));
706 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
707 amdgpu_ttm_placement_from_domain(bo, domain);
708 for (i = 0; i < bo->placement.num_placement; i++) {
709 /* force to pin into visible video ram */
710 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
711 !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
712 (!max_offset || max_offset >
713 adev->mc.visible_vram_size)) {
714 if (WARN_ON_ONCE(min_offset >
715 adev->mc.visible_vram_size))
717 fpfn = min_offset >> PAGE_SHIFT;
718 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
720 fpfn = min_offset >> PAGE_SHIFT;
721 lpfn = max_offset >> PAGE_SHIFT;
723 if (fpfn > bo->placements[i].fpfn)
724 bo->placements[i].fpfn = fpfn;
725 if (!bo->placements[i].lpfn ||
726 (lpfn && lpfn < bo->placements[i].lpfn))
727 bo->placements[i].lpfn = lpfn;
728 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
731 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
733 dev_err(adev->dev, "%p pin failed\n", bo);
738 if (gpu_addr != NULL) {
739 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
741 dev_err(adev->dev, "%p bind failed\n", bo);
744 *gpu_addr = amdgpu_bo_gpu_offset(bo);
746 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
747 adev->vram_pin_size += amdgpu_bo_size(bo);
748 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
749 adev->invisible_pin_size += amdgpu_bo_size(bo);
750 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
751 adev->gart_pin_size += amdgpu_bo_size(bo);
758 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
760 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
763 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
765 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
768 if (!bo->pin_count) {
769 dev_warn(adev->dev, "%p unpin not necessary\n", bo);
775 for (i = 0; i < bo->placement.num_placement; i++) {
776 bo->placements[i].lpfn = 0;
777 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
779 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
781 dev_err(adev->dev, "%p validate failed for unpin\n", bo);
785 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
786 adev->vram_pin_size -= amdgpu_bo_size(bo);
787 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
788 adev->invisible_pin_size -= amdgpu_bo_size(bo);
789 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
790 adev->gart_pin_size -= amdgpu_bo_size(bo);
797 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
799 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
800 if (0 && (adev->flags & AMD_IS_APU)) {
801 /* Useless to evict on IGP chips */
804 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
807 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->mc.aper_base,
824 /* Add an MTRR for the VRAM */
825 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
827 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
828 adev->mc.mc_vram_size >> 20,
829 (unsigned long long)adev->mc.aper_size >> 20);
830 DRM_INFO("RAM width %dbits %s\n",
831 adev->mc.vram_width, amdgpu_vram_names[adev->mc.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->mc.vram_mtrr);
839 arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.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 = container_of(bo, struct amdgpu_bo, tbo);
932 amdgpu_vm_bo_invalidate(adev, abo);
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 amdgpu_bo *abo;
952 unsigned long offset, size;
955 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
958 abo = container_of(bo, struct amdgpu_bo, tbo);
960 /* Remember that this BO was accessed by the CPU */
961 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
963 if (bo->mem.mem_type != TTM_PL_VRAM)
966 size = bo->mem.num_pages << PAGE_SHIFT;
967 offset = bo->mem.start << PAGE_SHIFT;
968 if ((offset + size) <= adev->mc.visible_vram_size)
971 /* Can't move a pinned BO to visible VRAM */
972 if (abo->pin_count > 0)
975 /* hurrah the memory is not visible ! */
976 atomic64_inc(&adev->num_vram_cpu_page_faults);
977 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
978 AMDGPU_GEM_DOMAIN_GTT);
980 /* Avoid costly evictions; only set GTT as a busy placement */
981 abo->placement.num_busy_placement = 1;
982 abo->placement.busy_placement = &abo->placements[1];
984 r = ttm_bo_validate(bo, &abo->placement, false, false);
985 if (unlikely(r != 0))
988 offset = bo->mem.start << PAGE_SHIFT;
989 /* this should never happen */
990 if (bo->mem.mem_type == TTM_PL_VRAM &&
991 (offset + size) > adev->mc.visible_vram_size)
998 * amdgpu_bo_fence - add fence to buffer object
1000 * @bo: buffer object in question
1001 * @fence: fence to add
1002 * @shared: true if fence should be added shared
1005 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
1008 struct reservation_object *resv = bo->tbo.resv;
1011 reservation_object_add_shared_fence(resv, fence);
1013 reservation_object_add_excl_fence(resv, fence);
1017 * amdgpu_bo_gpu_offset - return GPU offset of bo
1018 * @bo: amdgpu object for which we query the offset
1020 * Returns current GPU offset of the object.
1022 * Note: object should either be pinned or reserved when calling this
1023 * function, it might be useful to add check for this for debugging.
1025 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
1027 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
1028 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
1029 !amdgpu_ttm_is_bound(bo->tbo.ttm));
1030 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
1032 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
1033 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
1034 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
1036 return bo->tbo.offset;