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
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 #include <drm/ttm/ttm_bo_api.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_placement.h>
35 #include <drm/ttm/ttm_module.h>
36 #include <drm/ttm/ttm_page_alloc.h>
38 #include <drm/amdgpu_drm.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/swiotlb.h>
42 #include <linux/swap.h>
43 #include <linux/pagemap.h>
44 #include <linux/debugfs.h>
46 #include "amdgpu_trace.h"
47 #include "bif/bif_4_1_d.h"
49 #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
51 static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
52 struct ttm_mem_reg *mem, unsigned num_pages,
53 uint64_t offset, unsigned window,
54 struct amdgpu_ring *ring,
57 static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
58 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
63 static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
65 return ttm_mem_global_init(ref->object);
68 static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
70 ttm_mem_global_release(ref->object);
73 static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
75 struct drm_global_reference *global_ref;
76 struct amdgpu_ring *ring;
77 struct amd_sched_rq *rq;
80 adev->mman.mem_global_referenced = false;
81 global_ref = &adev->mman.mem_global_ref;
82 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
83 global_ref->size = sizeof(struct ttm_mem_global);
84 global_ref->init = &amdgpu_ttm_mem_global_init;
85 global_ref->release = &amdgpu_ttm_mem_global_release;
86 r = drm_global_item_ref(global_ref);
88 DRM_ERROR("Failed setting up TTM memory accounting "
93 adev->mman.bo_global_ref.mem_glob =
94 adev->mman.mem_global_ref.object;
95 global_ref = &adev->mman.bo_global_ref.ref;
96 global_ref->global_type = DRM_GLOBAL_TTM_BO;
97 global_ref->size = sizeof(struct ttm_bo_global);
98 global_ref->init = &ttm_bo_global_init;
99 global_ref->release = &ttm_bo_global_release;
100 r = drm_global_item_ref(global_ref);
102 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
106 mutex_init(&adev->mman.gtt_window_lock);
108 ring = adev->mman.buffer_funcs_ring;
109 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
110 r = amd_sched_entity_init(&ring->sched, &adev->mman.entity,
111 rq, amdgpu_sched_jobs);
113 DRM_ERROR("Failed setting up TTM BO move run queue.\n");
117 adev->mman.mem_global_referenced = true;
122 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
124 drm_global_item_unref(&adev->mman.mem_global_ref);
129 static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
131 if (adev->mman.mem_global_referenced) {
132 amd_sched_entity_fini(adev->mman.entity.sched,
134 mutex_destroy(&adev->mman.gtt_window_lock);
135 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
136 drm_global_item_unref(&adev->mman.mem_global_ref);
137 adev->mman.mem_global_referenced = false;
141 static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
146 static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
147 struct ttm_mem_type_manager *man)
149 struct amdgpu_device *adev;
151 adev = amdgpu_ttm_adev(bdev);
156 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
157 man->available_caching = TTM_PL_MASK_CACHING;
158 man->default_caching = TTM_PL_FLAG_CACHED;
161 man->func = &amdgpu_gtt_mgr_func;
162 man->gpu_offset = adev->mc.gart_start;
163 man->available_caching = TTM_PL_MASK_CACHING;
164 man->default_caching = TTM_PL_FLAG_CACHED;
165 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
168 /* "On-card" video ram */
169 man->func = &amdgpu_vram_mgr_func;
170 man->gpu_offset = adev->mc.vram_start;
171 man->flags = TTM_MEMTYPE_FLAG_FIXED |
172 TTM_MEMTYPE_FLAG_MAPPABLE;
173 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
174 man->default_caching = TTM_PL_FLAG_WC;
179 /* On-chip GDS memory*/
180 man->func = &ttm_bo_manager_func;
182 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
183 man->available_caching = TTM_PL_FLAG_UNCACHED;
184 man->default_caching = TTM_PL_FLAG_UNCACHED;
187 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
193 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
194 struct ttm_placement *placement)
196 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
197 struct amdgpu_bo *abo;
198 static const struct ttm_place placements = {
201 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
204 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
205 placement->placement = &placements;
206 placement->busy_placement = &placements;
207 placement->num_placement = 1;
208 placement->num_busy_placement = 1;
211 abo = container_of(bo, struct amdgpu_bo, tbo);
212 switch (bo->mem.mem_type) {
214 if (adev->mman.buffer_funcs &&
215 adev->mman.buffer_funcs_ring &&
216 adev->mman.buffer_funcs_ring->ready == false) {
217 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
218 } else if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
219 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
220 unsigned fpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
221 struct drm_mm_node *node = bo->mem.mm_node;
222 unsigned long pages_left;
224 for (pages_left = bo->mem.num_pages;
226 pages_left -= node->size, node++) {
227 if (node->start < fpfn)
234 /* Try evicting to the CPU inaccessible part of VRAM
235 * first, but only set GTT as busy placement, so this
236 * BO will be evicted to GTT rather than causing other
237 * BOs to be evicted from VRAM
239 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
240 AMDGPU_GEM_DOMAIN_GTT);
241 abo->placements[0].fpfn = fpfn;
242 abo->placements[0].lpfn = 0;
243 abo->placement.busy_placement = &abo->placements[1];
244 abo->placement.num_busy_placement = 1;
247 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
252 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
254 *placement = abo->placement;
257 static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
259 struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
261 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
263 return drm_vma_node_verify_access(&abo->gem_base.vma_node,
267 static void amdgpu_move_null(struct ttm_buffer_object *bo,
268 struct ttm_mem_reg *new_mem)
270 struct ttm_mem_reg *old_mem = &bo->mem;
272 BUG_ON(old_mem->mm_node != NULL);
274 new_mem->mm_node = NULL;
277 static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
278 struct drm_mm_node *mm_node,
279 struct ttm_mem_reg *mem)
283 if (mem->mem_type != TTM_PL_TT ||
284 amdgpu_gtt_mgr_is_allocated(mem)) {
285 addr = mm_node->start << PAGE_SHIFT;
286 addr += bo->bdev->man[mem->mem_type].gpu_offset;
291 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
292 bool evict, bool no_wait_gpu,
293 struct ttm_mem_reg *new_mem,
294 struct ttm_mem_reg *old_mem)
296 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
297 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
299 struct drm_mm_node *old_mm, *new_mm;
300 uint64_t old_start, old_size, new_start, new_size;
301 unsigned long num_pages;
302 struct dma_fence *fence = NULL;
305 BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
308 DRM_ERROR("Trying to move memory with ring turned off.\n");
312 old_mm = old_mem->mm_node;
313 old_size = old_mm->size;
314 old_start = amdgpu_mm_node_addr(bo, old_mm, old_mem);
316 new_mm = new_mem->mm_node;
317 new_size = new_mm->size;
318 new_start = amdgpu_mm_node_addr(bo, new_mm, new_mem);
320 num_pages = new_mem->num_pages;
321 mutex_lock(&adev->mman.gtt_window_lock);
323 unsigned long cur_pages = min(min(old_size, new_size),
324 (u64)AMDGPU_GTT_MAX_TRANSFER_SIZE);
325 uint64_t from = old_start, to = new_start;
326 struct dma_fence *next;
328 if (old_mem->mem_type == TTM_PL_TT &&
329 !amdgpu_gtt_mgr_is_allocated(old_mem)) {
330 r = amdgpu_map_buffer(bo, old_mem, cur_pages,
331 old_start, 0, ring, &from);
336 if (new_mem->mem_type == TTM_PL_TT &&
337 !amdgpu_gtt_mgr_is_allocated(new_mem)) {
338 r = amdgpu_map_buffer(bo, new_mem, cur_pages,
339 new_start, 1, ring, &to);
344 r = amdgpu_copy_buffer(ring, from, to,
345 cur_pages * PAGE_SIZE,
346 bo->resv, &next, false, true);
350 dma_fence_put(fence);
353 num_pages -= cur_pages;
357 old_size -= cur_pages;
359 old_start = amdgpu_mm_node_addr(bo, ++old_mm, old_mem);
360 old_size = old_mm->size;
362 old_start += cur_pages * PAGE_SIZE;
365 new_size -= cur_pages;
367 new_start = amdgpu_mm_node_addr(bo, ++new_mm, new_mem);
368 new_size = new_mm->size;
370 new_start += cur_pages * PAGE_SIZE;
373 mutex_unlock(&adev->mman.gtt_window_lock);
375 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
376 dma_fence_put(fence);
380 mutex_unlock(&adev->mman.gtt_window_lock);
383 dma_fence_wait(fence, false);
384 dma_fence_put(fence);
388 static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
389 bool evict, bool interruptible,
391 struct ttm_mem_reg *new_mem)
393 struct amdgpu_device *adev;
394 struct ttm_mem_reg *old_mem = &bo->mem;
395 struct ttm_mem_reg tmp_mem;
396 struct ttm_place placements;
397 struct ttm_placement placement;
400 adev = amdgpu_ttm_adev(bo->bdev);
402 tmp_mem.mm_node = NULL;
403 placement.num_placement = 1;
404 placement.placement = &placements;
405 placement.num_busy_placement = 1;
406 placement.busy_placement = &placements;
409 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
410 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
411 interruptible, no_wait_gpu);
416 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
421 r = ttm_tt_bind(bo->ttm, &tmp_mem);
425 r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
429 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem);
431 ttm_bo_mem_put(bo, &tmp_mem);
435 static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
436 bool evict, bool interruptible,
438 struct ttm_mem_reg *new_mem)
440 struct amdgpu_device *adev;
441 struct ttm_mem_reg *old_mem = &bo->mem;
442 struct ttm_mem_reg tmp_mem;
443 struct ttm_placement placement;
444 struct ttm_place placements;
447 adev = amdgpu_ttm_adev(bo->bdev);
449 tmp_mem.mm_node = NULL;
450 placement.num_placement = 1;
451 placement.placement = &placements;
452 placement.num_busy_placement = 1;
453 placement.busy_placement = &placements;
456 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
457 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
458 interruptible, no_wait_gpu);
462 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem);
466 r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
471 ttm_bo_mem_put(bo, &tmp_mem);
475 static int amdgpu_bo_move(struct ttm_buffer_object *bo,
476 bool evict, bool interruptible,
478 struct ttm_mem_reg *new_mem)
480 struct amdgpu_device *adev;
481 struct amdgpu_bo *abo;
482 struct ttm_mem_reg *old_mem = &bo->mem;
485 /* Can't move a pinned BO */
486 abo = container_of(bo, struct amdgpu_bo, tbo);
487 if (WARN_ON_ONCE(abo->pin_count > 0))
490 adev = amdgpu_ttm_adev(bo->bdev);
492 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
493 amdgpu_move_null(bo, new_mem);
496 if ((old_mem->mem_type == TTM_PL_TT &&
497 new_mem->mem_type == TTM_PL_SYSTEM) ||
498 (old_mem->mem_type == TTM_PL_SYSTEM &&
499 new_mem->mem_type == TTM_PL_TT)) {
501 amdgpu_move_null(bo, new_mem);
504 if (adev->mman.buffer_funcs == NULL ||
505 adev->mman.buffer_funcs_ring == NULL ||
506 !adev->mman.buffer_funcs_ring->ready) {
511 if (old_mem->mem_type == TTM_PL_VRAM &&
512 new_mem->mem_type == TTM_PL_SYSTEM) {
513 r = amdgpu_move_vram_ram(bo, evict, interruptible,
514 no_wait_gpu, new_mem);
515 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
516 new_mem->mem_type == TTM_PL_VRAM) {
517 r = amdgpu_move_ram_vram(bo, evict, interruptible,
518 no_wait_gpu, new_mem);
520 r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
525 r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem);
531 if (bo->type == ttm_bo_type_device &&
532 new_mem->mem_type == TTM_PL_VRAM &&
533 old_mem->mem_type != TTM_PL_VRAM) {
534 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
535 * accesses the BO after it's moved.
537 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
540 /* update statistics */
541 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
545 static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
547 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
548 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
550 mem->bus.addr = NULL;
552 mem->bus.size = mem->num_pages << PAGE_SHIFT;
554 mem->bus.is_iomem = false;
555 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
557 switch (mem->mem_type) {
564 mem->bus.offset = mem->start << PAGE_SHIFT;
565 /* check if it's visible */
566 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
568 mem->bus.base = adev->mc.aper_base;
569 mem->bus.is_iomem = true;
577 static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
581 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
582 unsigned long page_offset)
584 struct drm_mm_node *mm = bo->mem.mm_node;
585 uint64_t size = mm->size;
586 uint64_t offset = page_offset;
588 page_offset = do_div(offset, size);
590 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset;
594 * TTM backend functions.
596 struct amdgpu_ttm_gup_task_list {
597 struct list_head list;
598 struct task_struct *task;
601 struct amdgpu_ttm_tt {
602 struct ttm_dma_tt ttm;
603 struct amdgpu_device *adev;
606 struct mm_struct *usermm;
608 spinlock_t guptasklock;
609 struct list_head guptasks;
610 atomic_t mmu_invalidations;
611 struct list_head list;
614 int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
616 struct amdgpu_ttm_tt *gtt = (void *)ttm;
617 unsigned int flags = 0;
621 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
624 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
625 /* check that we only use anonymous memory
626 to prevent problems with writeback */
627 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
628 struct vm_area_struct *vma;
630 vma = find_vma(gtt->usermm, gtt->userptr);
631 if (!vma || vma->vm_file || vma->vm_end < end)
636 unsigned num_pages = ttm->num_pages - pinned;
637 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
638 struct page **p = pages + pinned;
639 struct amdgpu_ttm_gup_task_list guptask;
641 guptask.task = current;
642 spin_lock(>t->guptasklock);
643 list_add(&guptask.list, >t->guptasks);
644 spin_unlock(>t->guptasklock);
646 r = get_user_pages(userptr, num_pages, flags, p, NULL);
648 spin_lock(>t->guptasklock);
649 list_del(&guptask.list);
650 spin_unlock(>t->guptasklock);
657 } while (pinned < ttm->num_pages);
662 release_pages(pages, pinned, 0);
666 static void amdgpu_trace_dma_map(struct ttm_tt *ttm)
668 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
669 struct amdgpu_ttm_tt *gtt = (void *)ttm;
672 if (unlikely(trace_amdgpu_ttm_tt_populate_enabled())) {
673 for (i = 0; i < ttm->num_pages; i++) {
674 trace_amdgpu_ttm_tt_populate(
676 gtt->ttm.dma_address[i],
677 page_to_phys(ttm->pages[i]));
682 static void amdgpu_trace_dma_unmap(struct ttm_tt *ttm)
684 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
685 struct amdgpu_ttm_tt *gtt = (void *)ttm;
688 if (unlikely(trace_amdgpu_ttm_tt_unpopulate_enabled())) {
689 for (i = 0; i < ttm->num_pages; i++) {
690 trace_amdgpu_ttm_tt_unpopulate(
692 gtt->ttm.dma_address[i],
693 page_to_phys(ttm->pages[i]));
698 /* prepare the sg table with the user pages */
699 static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
701 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
702 struct amdgpu_ttm_tt *gtt = (void *)ttm;
706 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
707 enum dma_data_direction direction = write ?
708 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
710 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
711 ttm->num_pages << PAGE_SHIFT,
717 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
718 if (nents != ttm->sg->nents)
721 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
722 gtt->ttm.dma_address, ttm->num_pages);
724 amdgpu_trace_dma_map(ttm);
733 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
735 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
736 struct amdgpu_ttm_tt *gtt = (void *)ttm;
737 struct sg_page_iter sg_iter;
739 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
740 enum dma_data_direction direction = write ?
741 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
743 /* double check that we don't free the table twice */
747 /* free the sg table and pages again */
748 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
750 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
751 struct page *page = sg_page_iter_page(&sg_iter);
752 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
753 set_page_dirty(page);
755 mark_page_accessed(page);
759 amdgpu_trace_dma_unmap(ttm);
761 sg_free_table(ttm->sg);
764 static int amdgpu_ttm_do_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
766 struct amdgpu_ttm_tt *gtt = (void *)ttm;
770 spin_lock(>t->adev->gtt_list_lock);
771 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, mem);
772 gtt->offset = (u64)mem->start << PAGE_SHIFT;
773 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
774 ttm->pages, gtt->ttm.dma_address, flags);
777 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
778 ttm->num_pages, gtt->offset);
779 goto error_gart_bind;
782 list_add_tail(>t->list, >t->adev->gtt_list);
784 spin_unlock(>t->adev->gtt_list_lock);
789 static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
790 struct ttm_mem_reg *bo_mem)
792 struct amdgpu_ttm_tt *gtt = (void*)ttm;
796 r = amdgpu_ttm_tt_pin_userptr(ttm);
798 DRM_ERROR("failed to pin userptr\n");
802 if (!ttm->num_pages) {
803 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
804 ttm->num_pages, bo_mem, ttm);
807 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
808 bo_mem->mem_type == AMDGPU_PL_GWS ||
809 bo_mem->mem_type == AMDGPU_PL_OA)
812 if (amdgpu_gtt_mgr_is_allocated(bo_mem))
813 r = amdgpu_ttm_do_bind(ttm, bo_mem);
818 bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
820 struct amdgpu_ttm_tt *gtt = (void *)ttm;
822 return gtt && !list_empty(>t->list);
825 int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
827 struct ttm_tt *ttm = bo->ttm;
830 if (!ttm || amdgpu_ttm_is_bound(ttm))
833 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
836 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
840 return amdgpu_ttm_do_bind(ttm, bo_mem);
843 int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
845 struct amdgpu_ttm_tt *gtt, *tmp;
846 struct ttm_mem_reg bo_mem;
850 bo_mem.mem_type = TTM_PL_TT;
851 spin_lock(&adev->gtt_list_lock);
852 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
853 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, >t->ttm.ttm, &bo_mem);
854 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
855 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
858 spin_unlock(&adev->gtt_list_lock);
859 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
860 gtt->ttm.ttm.num_pages, gtt->offset);
864 spin_unlock(&adev->gtt_list_lock);
868 static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
870 struct amdgpu_ttm_tt *gtt = (void *)ttm;
874 amdgpu_ttm_tt_unpin_userptr(ttm);
876 if (!amdgpu_ttm_is_bound(ttm))
879 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
880 spin_lock(>t->adev->gtt_list_lock);
881 r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
883 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
884 gtt->ttm.ttm.num_pages, gtt->offset);
887 list_del_init(>t->list);
889 spin_unlock(>t->adev->gtt_list_lock);
893 static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
895 struct amdgpu_ttm_tt *gtt = (void *)ttm;
897 ttm_dma_tt_fini(>t->ttm);
901 static struct ttm_backend_func amdgpu_backend_func = {
902 .bind = &amdgpu_ttm_backend_bind,
903 .unbind = &amdgpu_ttm_backend_unbind,
904 .destroy = &amdgpu_ttm_backend_destroy,
907 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
908 unsigned long size, uint32_t page_flags,
909 struct page *dummy_read_page)
911 struct amdgpu_device *adev;
912 struct amdgpu_ttm_tt *gtt;
914 adev = amdgpu_ttm_adev(bdev);
916 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
920 gtt->ttm.ttm.func = &amdgpu_backend_func;
922 if (ttm_dma_tt_init(>t->ttm, bdev, size, page_flags, dummy_read_page)) {
926 INIT_LIST_HEAD(>t->list);
927 return >t->ttm.ttm;
930 static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
932 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
933 struct amdgpu_ttm_tt *gtt = (void *)ttm;
936 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
938 if (ttm->state != tt_unpopulated)
941 if (gtt && gtt->userptr) {
942 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
946 ttm->page_flags |= TTM_PAGE_FLAG_SG;
947 ttm->state = tt_unbound;
951 if (slave && ttm->sg) {
952 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
953 gtt->ttm.dma_address, ttm->num_pages);
954 ttm->state = tt_unbound;
959 #ifdef CONFIG_SWIOTLB
960 if (swiotlb_nr_tbl()) {
961 r = ttm_dma_populate(>t->ttm, adev->dev);
966 r = ttm_pool_populate(ttm);
971 for (i = 0; i < ttm->num_pages; i++) {
972 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
974 PCI_DMA_BIDIRECTIONAL);
975 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
977 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
978 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
979 gtt->ttm.dma_address[i] = 0;
981 ttm_pool_unpopulate(ttm);
989 amdgpu_trace_dma_map(ttm);
993 static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
995 struct amdgpu_device *adev;
996 struct amdgpu_ttm_tt *gtt = (void *)ttm;
998 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1000 if (gtt && gtt->userptr) {
1002 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
1009 adev = amdgpu_ttm_adev(ttm->bdev);
1011 amdgpu_trace_dma_unmap(ttm);
1013 #ifdef CONFIG_SWIOTLB
1014 if (swiotlb_nr_tbl()) {
1015 ttm_dma_unpopulate(>t->ttm, adev->dev);
1020 for (i = 0; i < ttm->num_pages; i++) {
1021 if (gtt->ttm.dma_address[i]) {
1022 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
1023 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1027 ttm_pool_unpopulate(ttm);
1030 int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
1033 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1038 gtt->userptr = addr;
1039 gtt->usermm = current->mm;
1040 gtt->userflags = flags;
1041 spin_lock_init(>t->guptasklock);
1042 INIT_LIST_HEAD(>t->guptasks);
1043 atomic_set(>t->mmu_invalidations, 0);
1048 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1050 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1058 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1061 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1062 struct amdgpu_ttm_gup_task_list *entry;
1065 if (gtt == NULL || !gtt->userptr)
1068 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
1069 if (gtt->userptr > end || gtt->userptr + size <= start)
1072 spin_lock(>t->guptasklock);
1073 list_for_each_entry(entry, >t->guptasks, list) {
1074 if (entry->task == current) {
1075 spin_unlock(>t->guptasklock);
1079 spin_unlock(>t->guptasklock);
1081 atomic_inc(>t->mmu_invalidations);
1086 bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1087 int *last_invalidated)
1089 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1090 int prev_invalidated = *last_invalidated;
1092 *last_invalidated = atomic_read(>t->mmu_invalidations);
1093 return prev_invalidated != *last_invalidated;
1096 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1098 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1103 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1106 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1107 struct ttm_mem_reg *mem)
1111 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1112 flags |= AMDGPU_PTE_VALID;
1114 if (mem && mem->mem_type == TTM_PL_TT) {
1115 flags |= AMDGPU_PTE_SYSTEM;
1117 if (ttm->caching_state == tt_cached)
1118 flags |= AMDGPU_PTE_SNOOPED;
1121 flags |= adev->gart.gart_pte_flags;
1122 flags |= AMDGPU_PTE_READABLE;
1124 if (!amdgpu_ttm_tt_is_readonly(ttm))
1125 flags |= AMDGPU_PTE_WRITEABLE;
1130 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1131 const struct ttm_place *place)
1133 unsigned long num_pages = bo->mem.num_pages;
1134 struct drm_mm_node *node = bo->mem.mm_node;
1136 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1137 return ttm_bo_eviction_valuable(bo, place);
1139 switch (bo->mem.mem_type) {
1144 /* Check each drm MM node individually */
1146 if (place->fpfn < (node->start + node->size) &&
1147 !(place->lpfn && place->lpfn <= node->start))
1150 num_pages -= node->size;
1159 return ttm_bo_eviction_valuable(bo, place);
1162 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1163 unsigned long offset,
1164 void *buf, int len, int write)
1166 struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
1167 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1168 struct drm_mm_node *nodes = abo->tbo.mem.mm_node;
1172 unsigned long flags;
1174 if (bo->mem.mem_type != TTM_PL_VRAM)
1177 while (offset >= (nodes->size << PAGE_SHIFT)) {
1178 offset -= nodes->size << PAGE_SHIFT;
1181 pos = (nodes->start << PAGE_SHIFT) + offset;
1183 while (len && pos < adev->mc.mc_vram_size) {
1184 uint64_t aligned_pos = pos & ~(uint64_t)3;
1185 uint32_t bytes = 4 - (pos & 3);
1186 uint32_t shift = (pos & 3) * 8;
1187 uint32_t mask = 0xffffffff << shift;
1190 mask &= 0xffffffff >> (bytes - len) * 8;
1194 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1195 WREG32(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
1196 WREG32(mmMM_INDEX_HI, aligned_pos >> 31);
1197 if (!write || mask != 0xffffffff)
1198 value = RREG32(mmMM_DATA);
1201 value |= (*(uint32_t *)buf << shift) & mask;
1202 WREG32(mmMM_DATA, value);
1204 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1206 value = (value & mask) >> shift;
1207 memcpy(buf, &value, bytes);
1211 buf = (uint8_t *)buf + bytes;
1214 if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
1216 pos = (nodes->start << PAGE_SHIFT);
1223 static struct ttm_bo_driver amdgpu_bo_driver = {
1224 .ttm_tt_create = &amdgpu_ttm_tt_create,
1225 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1226 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1227 .invalidate_caches = &amdgpu_invalidate_caches,
1228 .init_mem_type = &amdgpu_init_mem_type,
1229 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1230 .evict_flags = &amdgpu_evict_flags,
1231 .move = &amdgpu_bo_move,
1232 .verify_access = &amdgpu_verify_access,
1233 .move_notify = &amdgpu_bo_move_notify,
1234 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1235 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1236 .io_mem_free = &amdgpu_ttm_io_mem_free,
1237 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1238 .access_memory = &amdgpu_ttm_access_memory
1241 int amdgpu_ttm_init(struct amdgpu_device *adev)
1247 r = amdgpu_ttm_global_init(adev);
1251 /* No others user of address space so set it to 0 */
1252 r = ttm_bo_device_init(&adev->mman.bdev,
1253 adev->mman.bo_global_ref.ref.object,
1255 adev->ddev->anon_inode->i_mapping,
1256 DRM_FILE_PAGE_OFFSET,
1259 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1262 adev->mman.initialized = true;
1263 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1264 adev->mc.real_vram_size >> PAGE_SHIFT);
1266 DRM_ERROR("Failed initializing VRAM heap.\n");
1270 /* Reduce size of CPU-visible VRAM if requested */
1271 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1272 if (amdgpu_vis_vram_limit > 0 &&
1273 vis_vram_limit <= adev->mc.visible_vram_size)
1274 adev->mc.visible_vram_size = vis_vram_limit;
1276 /* Change the size here instead of the init above so only lpfn is affected */
1277 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1279 r = amdgpu_bo_create_kernel(adev, adev->mc.stolen_size, PAGE_SIZE,
1280 AMDGPU_GEM_DOMAIN_VRAM,
1281 &adev->stolen_vga_memory,
1285 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1286 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
1288 if (amdgpu_gtt_size == -1)
1289 gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1290 adev->mc.mc_vram_size);
1292 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1293 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
1295 DRM_ERROR("Failed initializing GTT heap.\n");
1298 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1299 (unsigned)(gtt_size / (1024 * 1024)));
1301 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1302 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1303 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1304 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1305 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1306 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1307 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1308 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1309 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1311 if (adev->gds.mem.total_size) {
1312 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1313 adev->gds.mem.total_size >> PAGE_SHIFT);
1315 DRM_ERROR("Failed initializing GDS heap.\n");
1321 if (adev->gds.gws.total_size) {
1322 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1323 adev->gds.gws.total_size >> PAGE_SHIFT);
1325 DRM_ERROR("Failed initializing gws heap.\n");
1331 if (adev->gds.oa.total_size) {
1332 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1333 adev->gds.oa.total_size >> PAGE_SHIFT);
1335 DRM_ERROR("Failed initializing oa heap.\n");
1340 r = amdgpu_ttm_debugfs_init(adev);
1342 DRM_ERROR("Failed to init debugfs\n");
1348 void amdgpu_ttm_fini(struct amdgpu_device *adev)
1352 if (!adev->mman.initialized)
1354 amdgpu_ttm_debugfs_fini(adev);
1355 if (adev->stolen_vga_memory) {
1356 r = amdgpu_bo_reserve(adev->stolen_vga_memory, true);
1358 amdgpu_bo_unpin(adev->stolen_vga_memory);
1359 amdgpu_bo_unreserve(adev->stolen_vga_memory);
1361 amdgpu_bo_unref(&adev->stolen_vga_memory);
1363 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1364 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
1365 if (adev->gds.mem.total_size)
1366 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1367 if (adev->gds.gws.total_size)
1368 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1369 if (adev->gds.oa.total_size)
1370 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
1371 ttm_bo_device_release(&adev->mman.bdev);
1372 amdgpu_gart_fini(adev);
1373 amdgpu_ttm_global_fini(adev);
1374 adev->mman.initialized = false;
1375 DRM_INFO("amdgpu: ttm finalized\n");
1378 /* this should only be called at bootup or when userspace
1380 void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1382 struct ttm_mem_type_manager *man;
1384 if (!adev->mman.initialized)
1387 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1388 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1389 man->size = size >> PAGE_SHIFT;
1392 int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1394 struct drm_file *file_priv;
1395 struct amdgpu_device *adev;
1397 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
1400 file_priv = filp->private_data;
1401 adev = file_priv->minor->dev->dev_private;
1405 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
1408 static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
1409 struct ttm_mem_reg *mem, unsigned num_pages,
1410 uint64_t offset, unsigned window,
1411 struct amdgpu_ring *ring,
1414 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1415 struct amdgpu_device *adev = ring->adev;
1416 struct ttm_tt *ttm = bo->ttm;
1417 struct amdgpu_job *job;
1418 unsigned num_dw, num_bytes;
1419 dma_addr_t *dma_address;
1420 struct dma_fence *fence;
1421 uint64_t src_addr, dst_addr;
1425 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
1426 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
1428 *addr = adev->mc.gart_start;
1429 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
1430 AMDGPU_GPU_PAGE_SIZE;
1432 num_dw = adev->mman.buffer_funcs->copy_num_dw;
1433 while (num_dw & 0x7)
1436 num_bytes = num_pages * 8;
1438 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
1442 src_addr = num_dw * 4;
1443 src_addr += job->ibs[0].gpu_addr;
1445 dst_addr = adev->gart.table_addr;
1446 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
1447 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
1448 dst_addr, num_bytes);
1450 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1451 WARN_ON(job->ibs[0].length_dw > num_dw);
1453 dma_address = >t->ttm.dma_address[offset >> PAGE_SHIFT];
1454 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
1455 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
1456 &job->ibs[0].ptr[num_dw]);
1460 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1461 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
1465 dma_fence_put(fence);
1470 amdgpu_job_free(job);
1474 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1475 uint64_t dst_offset, uint32_t byte_count,
1476 struct reservation_object *resv,
1477 struct dma_fence **fence, bool direct_submit,
1478 bool vm_needs_flush)
1480 struct amdgpu_device *adev = ring->adev;
1481 struct amdgpu_job *job;
1484 unsigned num_loops, num_dw;
1488 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1489 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1490 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1492 /* for IB padding */
1493 while (num_dw & 0x7)
1496 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1500 job->vm_needs_flush = vm_needs_flush;
1502 r = amdgpu_sync_resv(adev, &job->sync, resv,
1503 AMDGPU_FENCE_OWNER_UNDEFINED);
1505 DRM_ERROR("sync failed (%d).\n", r);
1510 for (i = 0; i < num_loops; i++) {
1511 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1513 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1514 dst_offset, cur_size_in_bytes);
1516 src_offset += cur_size_in_bytes;
1517 dst_offset += cur_size_in_bytes;
1518 byte_count -= cur_size_in_bytes;
1521 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1522 WARN_ON(job->ibs[0].length_dw > num_dw);
1523 if (direct_submit) {
1524 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
1526 job->fence = dma_fence_get(*fence);
1528 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1529 amdgpu_job_free(job);
1531 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1532 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1540 amdgpu_job_free(job);
1544 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
1546 struct reservation_object *resv,
1547 struct dma_fence **fence)
1549 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1550 /* max_bytes applies to SDMA_OP_PTEPDE as well as SDMA_OP_CONST_FILL*/
1551 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
1552 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1554 struct drm_mm_node *mm_node;
1555 unsigned long num_pages;
1556 unsigned int num_loops, num_dw;
1558 struct amdgpu_job *job;
1562 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1566 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
1567 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
1572 num_pages = bo->tbo.num_pages;
1573 mm_node = bo->tbo.mem.mm_node;
1576 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1578 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1579 num_pages -= mm_node->size;
1583 /* 10 double words for each SDMA_OP_PTEPDE cmd */
1584 num_dw = num_loops * 10;
1586 /* for IB padding */
1589 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1594 r = amdgpu_sync_resv(adev, &job->sync, resv,
1595 AMDGPU_FENCE_OWNER_UNDEFINED);
1597 DRM_ERROR("sync failed (%d).\n", r);
1602 num_pages = bo->tbo.num_pages;
1603 mm_node = bo->tbo.mem.mm_node;
1606 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1609 WARN_ONCE(byte_count & 0x7, "size should be a multiple of 8");
1611 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
1612 while (byte_count) {
1613 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1615 amdgpu_vm_set_pte_pde(adev, &job->ibs[0],
1617 cur_size_in_bytes >> 3, 0,
1620 dst_addr += cur_size_in_bytes;
1621 byte_count -= cur_size_in_bytes;
1624 num_pages -= mm_node->size;
1628 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1629 WARN_ON(job->ibs[0].length_dw > num_dw);
1630 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1631 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1638 amdgpu_job_free(job);
1642 #if defined(CONFIG_DEBUG_FS)
1644 static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1646 struct drm_info_node *node = (struct drm_info_node *)m->private;
1647 unsigned ttm_pl = *(int *)node->info_ent->data;
1648 struct drm_device *dev = node->minor->dev;
1649 struct amdgpu_device *adev = dev->dev_private;
1650 struct ttm_mem_type_manager *man = &adev->mman.bdev.man[ttm_pl];
1651 struct drm_printer p = drm_seq_file_printer(m);
1653 man->func->debug(man, &p);
1657 static int ttm_pl_vram = TTM_PL_VRAM;
1658 static int ttm_pl_tt = TTM_PL_TT;
1660 static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
1661 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1662 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1663 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1664 #ifdef CONFIG_SWIOTLB
1665 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1669 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1670 size_t size, loff_t *pos)
1672 struct amdgpu_device *adev = file_inode(f)->i_private;
1676 if (size & 0x3 || *pos & 0x3)
1679 if (*pos >= adev->mc.mc_vram_size)
1683 unsigned long flags;
1686 if (*pos >= adev->mc.mc_vram_size)
1689 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1690 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1691 WREG32(mmMM_INDEX_HI, *pos >> 31);
1692 value = RREG32(mmMM_DATA);
1693 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1695 r = put_user(value, (uint32_t *)buf);
1708 static const struct file_operations amdgpu_ttm_vram_fops = {
1709 .owner = THIS_MODULE,
1710 .read = amdgpu_ttm_vram_read,
1711 .llseek = default_llseek
1714 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1716 static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1717 size_t size, loff_t *pos)
1719 struct amdgpu_device *adev = file_inode(f)->i_private;
1724 loff_t p = *pos / PAGE_SIZE;
1725 unsigned off = *pos & ~PAGE_MASK;
1726 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1730 if (p >= adev->gart.num_cpu_pages)
1733 page = adev->gart.pages[p];
1738 r = copy_to_user(buf, ptr, cur_size);
1739 kunmap(adev->gart.pages[p]);
1741 r = clear_user(buf, cur_size);
1755 static const struct file_operations amdgpu_ttm_gtt_fops = {
1756 .owner = THIS_MODULE,
1757 .read = amdgpu_ttm_gtt_read,
1758 .llseek = default_llseek
1765 static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1767 #if defined(CONFIG_DEBUG_FS)
1770 struct drm_minor *minor = adev->ddev->primary;
1771 struct dentry *ent, *root = minor->debugfs_root;
1773 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1774 adev, &amdgpu_ttm_vram_fops);
1776 return PTR_ERR(ent);
1777 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1778 adev->mman.vram = ent;
1780 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1781 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1782 adev, &amdgpu_ttm_gtt_fops);
1784 return PTR_ERR(ent);
1785 i_size_write(ent->d_inode, adev->mc.gart_size);
1786 adev->mman.gtt = ent;
1789 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1791 #ifdef CONFIG_SWIOTLB
1792 if (!swiotlb_nr_tbl())
1796 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1803 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1805 #if defined(CONFIG_DEBUG_FS)
1807 debugfs_remove(adev->mman.vram);
1808 adev->mman.vram = NULL;
1810 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1811 debugfs_remove(adev->mman.gtt);
1812 adev->mman.gtt = NULL;